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