]> Shamusworld >> Repos - ttedit/blob - src/ttemainwindow.cpp
2b2b6a1be539a4c5757e5159c0ae05af57912913
[ttedit] / src / ttemainwindow.cpp
1 //
2 // TTEMAINWINDOW.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 // JLH  03/05/2009  Initial conversion from wxWidgets to Qt
17 //
18
19 // FIXED:
20 //
21 // - Fix problem with tool palette not getting focus 1st time it's called up [DONE]
22 // - Split out windows/classes defined here into their own files [DONE]
23 //
24 // STILL TO BE DONE:
25 //
26 // - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font
27 // - Fix scrolling, zooming, settings (ini)
28 // - Finish conversion to wxWidgets for cross-platform portability
29 // - Fix problem with owned window causing main window refresh problems
30 //   (ironically enough, it doesn't seem to be a problem anymore...)
31 //
32
33 // Uncomment this for debugging...
34 #define DEBUG
35 #define DEBUGFOO                        // Various tool debugging...
36 //#define DEBUGTP                               // Toolpalette debugging...
37
38 //#include <QtGui>
39 #include "ttemainwindow.h"
40 #include "editwindow.h"
41 #include "charwindow.h"
42 #include "ttedit.h"
43
44 TTEMainWindow::TTEMainWindow()
45 {
46         ((TTEdit *)qApp)->charWnd = new CharWindow(this);
47         editWnd = new EditWindow(this);
48         setCentralWidget(editWnd);
49         setWindowIcon(QIcon(":/res/ttedit.png"));
50         setWindowTitle("TTEdit!");
51
52 #if 0
53 //      createActions();
54         newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
55         newAct->setShortcuts(QKeySequence::New);
56         newAct->setStatusTip(tr("Create a new file"));
57         connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
58
59         openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
60         openAct->setShortcuts(QKeySequence::Open);
61         openAct->setStatusTip(tr("Open an existing file"));
62         connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
63
64         aboutQtAct = new QAction(tr("About &Qt"), this);
65         aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
66         connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
67
68 //      createMenus();
69         fileMenu = menuBar()->addMenu(tr("&File"));
70         fileMenu->addAction(newAct);
71         fileMenu->addAction(openAct);
72         fileMenu->addAction(saveAct);
73         fileMenu->addAction(saveAsAct);
74         fileMenu->addSeparator();
75         fileMenu->addAction(exitAct);
76
77         editMenu = menuBar()->addMenu(tr("&Edit"));
78         editMenu->addAction(cutAct);
79         editMenu->addAction(copyAct);
80         editMenu->addAction(pasteAct);
81
82         menuBar()->addSeparator();
83
84         helpMenu = menuBar()->addMenu(tr("&Help"));
85         helpMenu->addAction(aboutAct);
86         helpMenu->addAction(aboutQtAct);
87
88 //      createToolBars();
89         fileToolBar = addToolBar(tr("File"));
90         fileToolBar->addAction(newAct);
91         fileToolBar->addAction(openAct);
92         fileToolBar->addAction(saveAct);
93
94         editToolBar = addToolBar(tr("Edit"));
95         editToolBar->addAction(cutAct);
96         editToolBar->addAction(copyAct);
97         editToolBar->addAction(pasteAct);
98 #endif
99
100         //      Create status bar
101         statusBar()->showMessage(tr("Ready"));
102
103         ReadSettings();
104
105 //      connect(textEdit->document(), SIGNAL(contentsChanged()),
106 //                      this, SLOT(documentWasModified()));
107
108 //      setCurrentFile("");
109         setUnifiedTitleAndToolBarOnMac(true);
110
111         ((TTEdit *)qApp)->charWnd->show();//eh?
112 }
113
114 void TTEMainWindow::closeEvent(QCloseEvent * event)
115 {
116         WriteSettings();
117         event->accept(); // ignore() if can't close for some reason
118 }
119
120 void TTEMainWindow::Open(void)
121 {
122 }
123
124 void TTEMainWindow::ReadSettings(void)
125 {
126         QSettings settings("Underground Software", "TTEdit");
127         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
128         QSize size = settings.value("size", QSize(400, 400)).toSize();
129         resize(size);
130         move(pos);
131         pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
132         size = settings.value("charWndSize", QSize(200, 200)).toSize();
133         ((TTEdit *)qApp)->charWnd->resize(size);
134         ((TTEdit *)qApp)->charWnd->move(pos);
135 }
136
137 void TTEMainWindow::WriteSettings(void)
138 {
139         QSettings settings("Underground Software", "TTEdit");
140         settings.setValue("pos", pos());
141         settings.setValue("size", size());
142         settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
143         settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
144 }
145
146
147 #if 0
148 #include "ttedit.h"
149 #include "charwindow.h"
150 #include "toolwindow.h"
151 #include "editwindow.h"
152 #include "tte_res.h"                                                    // Resource IDs
153 #ifdef DEBUG
154 #include "debug.h"
155 #endif
156
157 // Pixmap resouces
158
159 #include "res/cur1.xpm"
160 #include "res/cur2.xpm"
161 #include "res/cur3.xpm"
162 #include "res/cur4.xpm"
163 #include "res/cur5.xpm"
164 #include "res/cur6.xpm"
165 #include "res/cur7.xpm"
166 #include "res/cur8.xpm"
167 #include "res/ttedit.xpm"                                               // *nix only, but small enough to not matter
168 #include "res/tool1.xpm"
169 #include "res/tool2.xpm"
170 #include "res/tool3.xpm"
171
172
173 IMPLEMENT_APP(TTEditApp)                                                // Run the main application loop
174
175 bool TTEditApp::OnInit()
176 {
177         wxLog * logTarget = new wxLogStderr();//fopen("!ttedit_log.txt", "wb"));
178         wxLog::SetActiveTarget(logTarget);
179 #ifdef DEBUG
180         OpenDebugLog();
181 #endif
182
183         // Initialize all the top-level window members to NULL.
184         mainFrame = NULL;
185         charWin = NULL;
186         toolPalette = NULL;
187         for(int i=0; i<8; i++)
188                 cur[i] = NULL;
189
190 //Shouldn't we check to see if it was successful? This just assumes
191         CreateResources();
192
193         mainFrame = new TTEditFrame(NULL, _("TTEdit"), wxPoint(155, 165), wxSize(300, 300),
194                 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE);
195 //              wxMINIMIZE_BOX | wxRESIZE_BOX | wxMAXIMIZE_BOX | | wxSYSTEM_MENU | wxCAPTION);
196
197 //      charWin = new CharWindow(NULL);//, _T("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize);
198         charWin = new CharWindow(mainFrame, _("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize,
199                 wxCAPTION | wxRESIZE_BORDER | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
200
201         toolPalette = new ToolWindow(mainFrame, _(""), wxDefaultPosition, wxDefaultSize,
202                 wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
203
204         return true;
205 }
206
207 int TTEditApp::OnExit()
208 {
209 #ifdef DEBUG
210         CloseDebugLog();
211 #endif
212         for(int i=0; i<8; i++)
213                 if (cur[i])
214                         delete cur[i];
215
216         return 0;
217 }
218
219 //
220 // OS dependent method of creating cursor (works for Win32 and GTK+)
221 //
222 #define CREATE_CURSOR(storage, name, hsx, hsy) \
223         wxBitmap name##_bitmap(name##_xpm); \
224         wxImage name##_image = name##_bitmap.ConvertToImage(); \
225         name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hsx); \
226         name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hsy); \
227         storage = new wxCursor(name##_image);
228
229 void TTEditApp::CreateResources(void)
230 {
231         // This is a sucky way to create cursors, but at least it's cross-platform...
232         // NOTE: Need to fix hotspots on a few... !!! FIX !!! [DONE]
233
234         CREATE_CURSOR(cur[0], cur1, 1, 1);
235         CREATE_CURSOR(cur[1], cur2, 1, 1);
236         CREATE_CURSOR(cur[2], cur3, 11, 11);    // Prolly won't need this soon (scroll)...
237         CREATE_CURSOR(cur[3], cur4, 15, 13);    // Prolly won't need this soon (zoom)...
238         CREATE_CURSOR(cur[4], cur5, 1, 1);
239         CREATE_CURSOR(cur[5], cur6, 1, 1);
240         CREATE_CURSOR(cur[6], cur7, 1, 1);
241         CREATE_CURSOR(cur[7], cur8, 1, 1);
242 }
243
244
245 BEGIN_EVENT_TABLE(TTEditFrame, wxFrame)
246         EVT_MENU(IDM_OPEN, TTEditFrame::OnOpen)
247         EVT_MENU(IDM_EXIT, TTEditFrame::OnExit)
248         EVT_MENU(IDM_ABOUT, TTEditFrame::OnAbout)
249         EVT_MENU(ID_TBCHARWIN, TTEditFrame::OnCharWindow)
250         EVT_CLOSE(TTEditFrame::OnCloseWindow)
251 END_EVENT_TABLE()
252
253 TTEditFrame::TTEditFrame(wxFrame * parent, const wxString &title, const wxPoint &pos,
254         const wxSize &size, long style): wxFrame(parent, -1, title, pos, size, style), app(wxGetApp()), mainWindow(NULL)
255 {
256         // Initialize child subwindow members (and hopefully avoid subtle bugs)
257 //      mainWindow = NULL;
258
259         SetIcon(wxICON(ttedit));
260 //      CreateStatusBar(2);                                                     // Create 2 panes
261         int widths[2] = { -1, 120 };
262         wxStatusBar * sb = CreateStatusBar(2, 0);       // Create 2 panes
263         sb->SetStatusWidths(2, widths);
264         wxToolBar * tb = CreateToolBar();
265
266         if (tb != NULL)
267         {
268                 // Create buttons
269
270                 wxBitmap tool1(tool1_xpm);
271                 wxBitmap tool2(tool2_xpm);
272                 wxBitmap tool3(tool3_xpm);
273
274                 tb->AddTool(ID_TBLEFT, _("Prev char"), tool1, _("Go to prev char"), wxITEM_NORMAL);
275                 tb->AddTool(ID_TBRIGHT, _("Next char"), tool2, _("Go to next char"), wxITEM_NORMAL);
276                 tb->AddTool(ID_TBCHARWIN, _("Char Wnd"), tool3, _("Toggle char window"), wxITEM_CHECK);
277                 tb->Realize();
278         }
279
280         // Create a menu bar for the frame
281         menuBar = new wxMenuBar;
282         wxMenu * menu1 = new wxMenu;
283         menu1->Append(IDM_NEW, _("&New\tCtrl+N"), _("Create a new font."));
284         menu1->Append(IDM_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing font."));
285         menu1->Append(IDM_SAVE, _("&Save\tCtrl+S"), _("Save the current font."));
286         menu1->Append(IDM_SAVEAS, _("Save &As..."), _("Save the current font under a different name."));
287         menu1->AppendSeparator();
288         menu1->Append(IDM_EXIT, _("E&xit\tAlt+X"), _("Quits the TTEdit program."));
289         menuBar->Append(menu1, _("&File"));
290         wxMenu * menu2 = new wxMenu;
291         menu2->Append(IDM_HELPTOPICS, _("&Help Topics"), _("Displays the Help contents and index."));
292         menu2->AppendSeparator();
293         menu2->Append(IDM_ABOUT, _("&About TTEdit"), _("Displays information about TTEdit."));
294         menuBar->Append(menu2, _("&Help"));
295         SetMenuBar(menuBar);
296
297         // Create child subwindows
298         mainWindow = new TTEditWindow(this);
299
300         Centre(wxBOTH);                                                         // Centre frame on the screen
301         Show(true);                                                                     // Show the frame
302 }
303
304 TTEditFrame::~TTEditFrame()
305 {
306 }
307
308 void TTEditFrame::OnOpen(wxCommandEvent &e)
309 {
310         wxFileDialog fd(this, _("Choose a font to load"), _(""), _(""), _("TTF files (*.ttf)|*.ttf|All files (*.*)|*.*"), wxOPEN);
311
312         if (fd.ShowModal() != wxID_OK)
313             return;
314
315 // Hmm. The font object is causing a massive crash... (gdb says it's in "Load")
316         if (app.font.Load((char *)fd.GetPath().c_str()) != true)
317         {
318                 wxMessageDialog dlg(NULL, _("Load font failed!"), _("Houston, we have a problem..."), wxOK | wxICON_ERROR);
319                 dlg.ShowModal();
320         }
321
322 //Huzzah! It works! Now just need scaling, scrolling, etc...
323 //      pts = app.font.GetGlyphPoints(45);
324 }
325
326 void TTEditFrame::OnAbout(wxCommandEvent &e)
327 {
328         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);
329         dlg.ShowModal();
330 }
331
332 void TTEditFrame::OnExit(wxCommandEvent &e)
333 {
334         app.toolPalette->Destroy();
335         this->Destroy();
336 }
337
338 void TTEditFrame::OnCharWindow(wxCommandEvent &e)
339 {
340         app.charWin->Show(e.IsChecked() ? true : false);
341
342         if (e.IsChecked())
343                 Raise();
344 }
345
346 void TTEditFrame::OnCloseWindow(wxCloseEvent &e)
347 {
348         app.toolPalette->Destroy();
349         this->Destroy();
350 }
351 #endif