]> Shamusworld >> Repos - ttedit/blob - src/ttedit.cpp
Fix cursors so that the use proper masks, added char window toggle,
[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         for(int i=0; i<8; i++)
77                 cur[i] = NULL;
78
79 //Shouldn't we check to see if it was successful? This just assumes
80         CreateResources();
81
82         mainFrame = new TTEditFrame(NULL, _("TTEdit"), wxPoint(155, 165), wxSize(300, 300),
83                 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE);
84 //              wxMINIMIZE_BOX | wxRESIZE_BOX | wxMAXIMIZE_BOX | | wxSYSTEM_MENU | wxCAPTION);
85
86 //      charWin = new CharWindow(NULL);//, _T("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize);
87         charWin = new CharWindow(mainFrame, _("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize,
88                 wxCAPTION | wxRESIZE_BORDER | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
89
90         toolPalette = new ToolWindow(mainFrame, _(""), wxDefaultPosition, wxDefaultSize,
91                 wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
92
93         return true;
94 }
95
96 int TTEditApp::OnExit()
97 {
98 #ifdef DEBUG
99         CloseDebugLog();
100 #endif
101         for(int i=0; i<8; i++)
102                 if (cur[i])
103                         delete cur[i];
104
105         return 0;
106 }
107
108 //
109 // OS dependent method of creating cursor (works for Win32 and GTK+)
110 //
111 #define CREATE_CURSOR(storage, name, hsx, hsy) \
112         wxBitmap name##_bitmap(name##_xpm); \
113         wxImage name##_image = name##_bitmap.ConvertToImage(); \
114         name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hsx); \
115         name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hsy); \
116         storage = new wxCursor(name##_image);
117
118 void TTEditApp::CreateResources(void)
119 {
120         // This is a sucky way to create cursors, but at least it's cross-platform...
121         // NOTE: Need to fix hotspots on a few... !!! FIX !!! [DONE]
122
123         CREATE_CURSOR(cur[0], cur1, 1, 1);
124         CREATE_CURSOR(cur[1], cur2, 1, 1);
125         CREATE_CURSOR(cur[2], cur3, 11, 11);    // Prolly won't need this soon (scroll)...
126         CREATE_CURSOR(cur[3], cur4, 15, 13);    // Prolly won't need this soon (zoom)...
127         CREATE_CURSOR(cur[4], cur5, 1, 1);
128         CREATE_CURSOR(cur[5], cur6, 1, 1);
129         CREATE_CURSOR(cur[6], cur7, 1, 1);
130         CREATE_CURSOR(cur[7], cur8, 1, 1);
131 }
132
133
134 BEGIN_EVENT_TABLE(TTEditFrame, wxFrame)
135         EVT_MENU(IDM_OPEN, TTEditFrame::OnOpen)
136         EVT_MENU(IDM_EXIT, TTEditFrame::OnExit)
137         EVT_MENU(IDM_ABOUT, TTEditFrame::OnAbout)
138         EVT_MENU(ID_TBCHARWIN, TTEditFrame::OnCharWindow)
139         EVT_CLOSE(TTEditFrame::OnCloseWindow)
140 END_EVENT_TABLE()
141
142 TTEditFrame::TTEditFrame(wxFrame * parent, const wxString &title, const wxPoint &pos,
143         const wxSize &size, long style): wxFrame(parent, -1, title, pos, size, style), app(wxGetApp()), mainWindow(NULL)
144 {
145         // Initialize child subwindow members (and hopefully avoid subtle bugs)
146 //      mainWindow = NULL;
147
148         SetIcon(wxICON(ttedit));
149 //      CreateStatusBar(2);                                                     // Create 2 panes
150         int widths[2] = { -1, 120 };
151         wxStatusBar * sb = CreateStatusBar(2, 0);       // Create 2 panes
152         sb->SetStatusWidths(2, widths);
153         wxToolBar * tb = CreateToolBar();
154
155         if (tb != NULL)
156         {
157                 // Create buttons
158
159                 wxBitmap tool1(tool1_xpm);
160                 wxBitmap tool2(tool2_xpm);
161                 wxBitmap tool3(tool3_xpm);
162
163                 tb->AddTool(ID_TBLEFT, _("Prev char"), tool1, _("Go to prev char"), wxITEM_NORMAL);
164                 tb->AddTool(ID_TBRIGHT, _("Next char"), tool2, _("Go to next char"), wxITEM_NORMAL);
165                 tb->AddTool(ID_TBCHARWIN, _("Char Wnd"), tool3, _("Toggle char window"), wxITEM_CHECK);
166                 tb->Realize();
167         }
168
169         // Create a menu bar for the frame
170         menuBar = new wxMenuBar;
171         wxMenu * menu1 = new wxMenu;
172         menu1->Append(IDM_NEW, _("&New\tCtrl+N"), _("Create a new font."));
173         menu1->Append(IDM_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing font."));
174         menu1->Append(IDM_SAVE, _("&Save\tCtrl+S"), _("Save the current font."));
175         menu1->Append(IDM_SAVEAS, _("Save &As..."), _("Save the current font under a different name."));
176         menu1->AppendSeparator();
177         menu1->Append(IDM_EXIT, _("E&xit\tAlt+X"), _("Quits the TTEdit program."));
178         menuBar->Append(menu1, _("&File"));
179         wxMenu * menu2 = new wxMenu;
180         menu2->Append(IDM_HELPTOPICS, _("&Help Topics"), _("Displays the Help contents and index."));
181         menu2->AppendSeparator();
182         menu2->Append(IDM_ABOUT, _("&About TTEdit"), _("Displays information about TTEdit."));
183         menuBar->Append(menu2, _("&Help"));
184         SetMenuBar(menuBar);
185
186         // Create child subwindows
187         mainWindow = new TTEditWindow(this);
188
189         Centre(wxBOTH);                                                         // Centre frame on the screen
190         Show(true);                                                                     // Show the frame
191 }
192
193 TTEditFrame::~TTEditFrame()
194 {
195 }
196
197 void TTEditFrame::OnOpen(wxCommandEvent &e)
198 {
199         wxFileDialog fd(this, _("Choose a font to load"), _(""), _(""), _("TTF files (*.ttf)|*.ttf|All files (*.*)|*.*"), wxOPEN);
200
201         if (fd.ShowModal() != wxID_OK)
202             return;
203
204 // Hmm. The font object is causing a massive crash... (gdb says it's in "Load")
205         if (app.font.Load((char *)fd.GetPath().c_str()) != true)
206         {
207                 wxMessageDialog dlg(NULL, _("Load font failed!"), _("Houston, we have a problem..."), wxOK | wxICON_ERROR);
208                 dlg.ShowModal();
209         }
210
211 //Huzzah! It works! Now just need scaling, scrolling, etc...
212 //      pts = app.font.GetGlyphPoints(45);
213 }
214
215 void TTEditFrame::OnAbout(wxCommandEvent &e)
216 {
217         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);
218         dlg.ShowModal();
219 }
220
221 void TTEditFrame::OnExit(wxCommandEvent &e)
222 {
223         app.toolPalette->Destroy();
224         this->Destroy();
225 }
226
227 void TTEditFrame::OnCharWindow(wxCommandEvent &e)
228 {
229         app.charWin->Show(e.IsChecked() ? true : false);
230
231         if (e.IsChecked())
232                 Raise();
233 }
234
235 void TTEditFrame::OnCloseWindow(wxCloseEvent &e)
236 {
237         app.toolPalette->Destroy();
238         this->Destroy();
239 }