]> Shamusworld >> Repos - ttedit/blob - src/ttedit.cpp
116bd4abfcea9b1daa9dd7dfb0446e3ad3546bdc
[ttedit] / src / ttedit.cpp
1 //
2 // TTEDIT.CPP - The TrueType Editor
3 // by James L. Hammons
4 // (C) 2004 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  04/10/2002  Created this file
11 // JLH  05/10/2004  Translated file from ASM to CPP
12 // JLH  05/14/2004  Added rudimentary editing capability to tool palette tools
13 // JLH  11/18/2006  Initial port to Linux
14 // JLH  08/27/2008  Fixed tool palette focus problems
15 // JLH  08/28/2008  Split out classes defined here into separate files
16 //
17
18 // FIXED:
19 //
20 // - Fix problem with tool palette not getting focus 1st time it's called up [DONE]
21 // - Split out windows/classes defined here into their own files [DONE]
22 //
23 // STILL TO BE DONE:
24 //
25 // - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font
26 // - Fix scrolling, zooming, settings (ini)
27 // - Finish conversion to wxWidgets for cross-platform portability
28 // - Fix problem with owned window causing main window refresh problems
29 //   (ironically enough, it doesn't seem to be a problem anymore...)
30 //
31
32 // Uncomment this for debugging...
33 #define DEBUG
34 #define DEBUGFOO                        // Various tool debugging...
35 #define DEBUGTP                         // Toolpalette debugging...
36
37 #include "ttedit.h"
38 #include "charwindow.h"
39 #include "toolwindow.h"
40 #include "editwindow.h"
41 #include "tte_res.h"                                                    // Resource IDs
42 #ifdef DEBUG
43 #include "debug.h"
44 #endif
45
46 // Pixmap resouces
47
48 #include "res/cur1.xpm"
49 #include "res/cur2.xpm"
50 #include "res/cur3.xpm"
51 #include "res/cur4.xpm"
52 #include "res/cur5.xpm"
53 #include "res/cur6.xpm"
54 #include "res/cur7.xpm"
55 #include "res/cur8.xpm"
56 #include "res/ttedit.xpm"                                               // *nix only, but small enough to not matter
57 #include "res/tool1.xpm"
58 #include "res/tool2.xpm"
59 #include "res/tool3.xpm"
60
61
62 IMPLEMENT_APP(TTEditApp)                                                // Run the main application loop
63
64 bool TTEditApp::OnInit()
65 {
66         wxLog * logTarget = new wxLogStderr();//fopen("!ttedit_log.txt", "wb"));
67         wxLog::SetActiveTarget(logTarget);
68 #ifdef DEBUG
69         OpenDebugLog();
70 #endif
71
72         // Initialize all the top-level window members to NULL.
73         mainFrame = NULL;
74         charWin = NULL;
75         toolPalette = NULL;
76
77 //Shouldn't we check to see if it was successful? This just assumes
78         CreateResources();
79
80         mainFrame = new TTEditFrame(NULL, _("TTEdit"), wxPoint(155, 165), wxSize(300, 300),
81                 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE);
82 //              wxMINIMIZE_BOX | wxRESIZE_BOX | wxMAXIMIZE_BOX | | wxSYSTEM_MENU | wxCAPTION);
83
84 //      charWin = new CharWindow(NULL);//, _T("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize);
85         charWin = new CharWindow(mainFrame, _("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize,
86                 wxCAPTION | wxRESIZE_BORDER);
87
88         toolPalette = new ToolWindow(mainFrame, _(""), wxDefaultPosition, wxDefaultSize,
89                 wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
90
91         return true;
92 }
93
94 int TTEditApp::OnExit()
95 {
96 #ifdef DEBUG
97         CloseDebugLog();
98 #endif
99
100         return 0;
101 }
102
103 void TTEditApp::CreateResources(void)
104 {
105         // This is a sucky way to create cursors, but at least it's cross-platform...
106         // NOTE: Need to fix hotspots on a few... !!! FIX !!!
107
108         wxBitmap cursorBM1(cur1_xpm);
109         wxImage cursorImage1 = cursorBM1.ConvertToImage();
110         cursorImage1.SetMaskFromImage(cursorImage1, 0xFF, 0xFF, 0xFF);
111         cur[0] = new wxCursor(cursorImage1);
112
113         wxBitmap cursorBM2(cur2_xpm);
114         wxImage cursorImage2 = cursorBM2.ConvertToImage();
115         cursorImage2.SetMaskFromImage(cursorImage2, 0xFF, 0xFF, 0xFF);
116         cur[1] = new wxCursor(cursorImage2);
117
118         wxBitmap cursorBM3(cur3_xpm);
119         wxImage cursorImage3 = cursorBM3.ConvertToImage();
120         cursorImage3.SetMaskFromImage(cursorImage3, 0xFF, 0xFF, 0xFF);
121         cur[2] = new wxCursor(cursorImage3);
122
123         wxBitmap cursorBM4(cur4_xpm);
124         wxImage cursorImage4 = cursorBM4.ConvertToImage();
125         cursorImage4.SetMaskFromImage(cursorImage4, 0xFF, 0xFF, 0xFF);
126         cur[3] = new wxCursor(cursorImage4);
127
128         wxBitmap cursorBM5(cur5_xpm);
129         wxImage cursorImage5 = cursorBM5.ConvertToImage();
130         cursorImage5.SetMaskFromImage(cursorImage5, 0xFF, 0xFF, 0xFF);
131         cur[4] = new wxCursor(cursorImage5);
132
133         wxBitmap cursorBM6(cur6_xpm);
134         wxImage cursorImage6 = cursorBM6.ConvertToImage();
135         cursorImage6.SetMaskFromImage(cursorImage6, 0xFF, 0xFF, 0xFF);
136         cur[5] = new wxCursor(cursorImage6);
137
138         wxBitmap cursorBM7(cur7_xpm);
139         wxImage cursorImage7 = cursorBM7.ConvertToImage();
140         cursorImage7.SetMaskFromImage(cursorImage7, 0xFF, 0xFF, 0xFF);
141         cur[6] = new wxCursor(cursorImage7);
142
143         wxBitmap cursorBM8(cur8_xpm);
144         wxImage cursorImage8 = cursorBM8.ConvertToImage();
145         cursorImage8.SetMaskFromImage(cursorImage8, 0xFF, 0xFF, 0xFF);
146         cur[7] = new wxCursor(cursorImage8);
147 }
148
149
150 BEGIN_EVENT_TABLE(TTEditFrame, wxFrame)
151         EVT_MENU(IDM_OPEN, TTEditFrame::OnOpen)
152         EVT_MENU(IDM_EXIT, TTEditFrame::OnExit)
153         EVT_MENU(IDM_ABOUT, TTEditFrame::OnAbout)
154         EVT_CLOSE(TTEditFrame::OnCloseWindow)
155 END_EVENT_TABLE()
156
157 TTEditFrame::TTEditFrame(wxFrame * parent, const wxString &title, const wxPoint &pos,
158         const wxSize &size, long style): wxFrame(parent, -1, title, pos, size, style), app(wxGetApp()), mainWindow(NULL)
159 {
160         // Initialize child subwindow members (and hopefully avoid subtle bugs)
161 //      mainWindow = NULL;
162
163         SetIcon(wxICON(ttedit));
164 //      CreateStatusBar(2);                                                     // Create 2 panes
165         int widths[2] = { -1, 120 };
166         wxStatusBar * sb = CreateStatusBar(2, 0);       // Create 2 panes
167         sb->SetStatusWidths(2, widths);
168         wxToolBar * tb = CreateToolBar();
169
170         if (tb != NULL)
171         {
172                 // Create buttons
173
174                 wxBitmap tool1(tool1_xpm);
175                 wxBitmap tool2(tool2_xpm);
176                 wxBitmap tool3(tool3_xpm);
177
178                 tb->AddTool(ID_TBLEFT, _("Prev char"), tool1, _("Go to prev char"), wxITEM_NORMAL);
179                 tb->AddTool(ID_TBRIGHT, _("Next char"), tool2, _("Go to next char"), wxITEM_NORMAL);
180                 tb->AddTool(ID_TBCHARWIN, _("Char Wnd"), tool3, _("Toggle char window"), wxITEM_NORMAL);
181                 tb->Realize();
182         }
183
184         // Create a menu bar for the frame
185         menuBar = new wxMenuBar;
186         wxMenu * menu1 = new wxMenu;
187         menu1->Append(IDM_NEW, _("&New\tCtrl+N"), _("Create a new font."));
188         menu1->Append(IDM_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing font."));
189         menu1->Append(IDM_SAVE, _("&Save\tCtrl+S"), _("Save the current font."));
190         menu1->Append(IDM_SAVEAS, _("Save &As..."), _("Save the current font under a different name."));
191         menu1->AppendSeparator();
192         menu1->Append(IDM_EXIT, _("E&xit\tAlt+X"), _("Quits the TTEdit program."));
193         menuBar->Append(menu1, _("&File"));
194         wxMenu * menu2 = new wxMenu;
195         menu2->Append(IDM_HELPTOPICS, _("&Help Topics"), _("Displays the Help contents and index."));
196         menu2->AppendSeparator();
197         menu2->Append(IDM_ABOUT, _("&About TTEdit"), _("Displays information about TTEdit."));
198         menuBar->Append(menu2, _("&Help"));
199         SetMenuBar(menuBar);
200
201         // Create child subwindows
202         mainWindow = new TTEditWindow(this);
203
204         Centre(wxBOTH);                                                         // Centre frame on the screen
205         Show(true);                                                                     // Show the frame
206 }
207
208 TTEditFrame::~TTEditFrame()
209 {
210 }
211
212 void TTEditFrame::OnOpen(wxCommandEvent &e)
213 {
214         wxFileDialog fd(this, _("Choose a font to load"), _(""), _(""), _("TTF files (*.ttf)|*.ttf|All files (*.*)|*.*"), wxOPEN);
215
216         if (fd.ShowModal() != wxID_OK)
217             return;
218
219 // Hmm. The font object is causing a massive crash... (gdb says it's in "Load")
220         if (app.font.Load((char *)fd.GetPath().c_str()) != true)
221         {
222                 wxMessageDialog dlg(NULL, _("Load font failed!"), _("Houston, we have a problem..."), wxOK | wxICON_ERROR);
223                 dlg.ShowModal();
224         }
225
226 //Huzzah! It works! Now just need scaling, scrolling, etc...
227 //      pts = app.font.GetGlyphPoints(45);
228 }
229
230 void TTEditFrame::OnAbout(wxCommandEvent &e)
231 {
232         wxMessageDialog dlg(NULL, _("TrueType Edit v1.0.1\n\nA handy tool for editing TrueType fonts!\nby James \"Shamus\" Hammons\n(C) 2006 Underground Software"), _("About TrueType Edit"), wxOK | wxICON_INFORMATION);
233         dlg.ShowModal();
234 }
235
236 void TTEditFrame::OnExit(wxCommandEvent &e)
237 {
238         app.toolPalette->Destroy();
239         this->Destroy();
240 }
241
242 void TTEditFrame::OnCloseWindow(wxCloseEvent &e)
243 {
244         app.toolPalette->Destroy();
245         this->Destroy();
246 }