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