2 // MAINWINDOW.CPP - The TrueType Editor
4 // (C) 2004 Underground Software
6 // JLH = James L. Hammons <jlhamm@acm.org>
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
21 // - Fix problem with tool palette not getting focus 1st time it's called up
23 // - Split out windows/classes defined here into their own files [DONE]
27 // - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font
28 // - Fix scrolling, zooming, settings (ini)
29 // - Fix problem with character window disappearing when bringing up the tool
33 // Uncomment this for debugging...
35 #define DEBUGFOO // Various tool debugging...
36 //#define DEBUGTP // Toolpalette debugging...
38 #include "mainwindow.h"
39 #include "charwindow.h"
40 #include "editwindow.h"
44 MainWindow::MainWindow()
46 ((TTEdit *)qApp)->charWnd = new CharWindow(this);
47 editWnd = new EditWindow(this);
48 setCentralWidget(editWnd);
50 setWindowIcon(QIcon(":/res/ttedit.png"));
51 setWindowTitle("TTEdit! - Untitled");
55 newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
56 newAct->setShortcuts(QKeySequence::New);
57 newAct->setStatusTip(tr("Create a new file"));
58 connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
60 openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
61 openAct->setShortcuts(QKeySequence::Open);
62 openAct->setStatusTip(tr("Open an existing file"));
63 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
65 aboutQtAct = new QAction(tr("About &Qt"), this);
66 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
67 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
70 fileMenu = menuBar()->addMenu(tr("&File"));
71 fileMenu->addAction(newAct);
72 fileMenu->addAction(openAct);
73 fileMenu->addAction(saveAct);
74 fileMenu->addAction(saveAsAct);
75 fileMenu->addSeparator();
76 fileMenu->addAction(exitAct);
78 editMenu = menuBar()->addMenu(tr("&Edit"));
79 editMenu->addAction(cutAct);
80 editMenu->addAction(copyAct);
81 editMenu->addAction(pasteAct);
83 menuBar()->addSeparator();
85 helpMenu = menuBar()->addMenu(tr("&Help"));
86 helpMenu->addAction(aboutAct);
87 helpMenu->addAction(aboutQtAct);
90 fileToolBar = addToolBar(tr("File"));
91 fileToolBar->addAction(newAct);
92 fileToolBar->addAction(openAct);
93 fileToolBar->addAction(saveAct);
95 editToolBar = addToolBar(tr("Edit"));
96 editToolBar->addAction(cutAct);
97 editToolBar->addAction(copyAct);
98 editToolBar->addAction(pasteAct);
106 scaleIndicator = new QLabel("Scale: 100%");
107 statusBar()->addPermanentWidget(scaleIndicator);
108 statusBar()->showMessage(tr("Ready"));
112 // connect(textEdit->document(), SIGNAL(contentsChanged()),
113 // this, SLOT(documentWasModified()));
115 // setCurrentFile("");
116 setUnifiedTitleAndToolBarOnMac(true);
118 ((TTEdit *)qApp)->charWnd->show();//eh?
123 // Consolidates action creation from a multi-step process to a single-step one.
125 QAction * MainWindow::CreateAction(QString name, QString tooltip, QString
126 statustip, QIcon icon, QKeySequence key, bool checkable/*= false*/)
128 QAction * action = new QAction(icon, name, this);
129 action->setToolTip(tooltip);
130 action->setStatusTip(statustip);
131 action->setShortcut(key);
132 action->setCheckable(checkable);
139 // This is essentially the same as the previous function, but this allows more
140 // than one key sequence to be added as key shortcuts.
142 QAction * MainWindow::CreateAction(QString name, QString tooltip, QString
143 statustip, QIcon icon, QKeySequence key1, QKeySequence key2, bool
144 checkable/*= false*/)
146 QAction * action = new QAction(icon, name, this);
147 action->setToolTip(tooltip);
148 action->setStatusTip(statustip);
149 QList<QKeySequence> keyList;
150 keyList.append(key1);
151 keyList.append(key2);
152 action->setShortcuts(keyList);
153 action->setCheckable(checkable);
159 void MainWindow::CreateActions(void)
161 newGlyphAct = CreateAction("&New Glyph", "New Glyph", "Create a new glyph", QIcon(), QKeySequence());
162 openFileAct = CreateAction("&Open File", "Open File", "Open a glyph file", QIcon(), QKeySequence("ctrl+o"));
163 saveFileAct = CreateAction("&Save File", "Save File", "Save a glyph file", QIcon(), QKeySequence("ctrl+s"));
164 quitAct = CreateAction("&Quit", "Quit", "Exits from the TTEdit application", QIcon(), QKeySequence("ctrl+q"));
165 zoomInAct = CreateAction("Zoom In", "Zoom In", "Zoom into the canvas", QIcon(), QKeySequence("+"), QKeySequence("="));
166 zoomOutAct = CreateAction("Zoom Out", "Zoom Out", "Zoom out of canvas", QIcon(), QKeySequence("-"));
168 connect(newGlyphAct, SIGNAL(triggered()), this, SLOT(NewGlyph()));
169 connect(openFileAct, SIGNAL(triggered()), this, SLOT(OpenFile()));
170 connect(saveFileAct, SIGNAL(triggered()), this, SLOT(SaveFile()));
171 connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
172 connect(zoomInAct, SIGNAL(triggered()), this, SLOT(ZoomIn()));
173 connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(ZoomOut()));
177 void MainWindow::CreateMenus(void)
179 QMenu * menu = menuBar()->addMenu(tr("&File"));
180 menu->addAction(newGlyphAct);
181 menu->addAction(openFileAct);
182 menu->addAction(saveFileAct);
183 menu->addSeparator();
184 menu->addAction(quitAct);
185 // menu->addAction(fileSaveAsAct);
186 // menu->addAction(fileCloseAct);
187 menu = menuBar()->addMenu(tr("&View"));
188 menu->addAction(zoomInAct);
189 menu->addAction(zoomOutAct);
193 void MainWindow::CreateToolbars(void)
198 void MainWindow::closeEvent(QCloseEvent * event)
201 event->accept(); // ignore() if can't close for some reason
205 void MainWindow::NewGlyph(void)
207 editWnd->pts.Clear();
208 ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
209 ((TTEdit *)qApp)->charWnd->update();
210 // editWnd->polyFirstPoint = true;
212 setWindowTitle("TTEdit! - Untitled");
217 void MainWindow::OpenFile(void)
219 QString filename = QFileDialog::getOpenFileName(this, tr("Open Glyph File"),
220 "./", tr("Glyph files (*.glyph)"));
221 FILE * file = fopen(filename.toUtf8().data(), "r");
223 //need to pop an error box here...
227 editWnd->pts.LoadGlyphFromFile(file);
230 ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
231 ((TTEdit *)qApp)->charWnd->update();
233 setWindowTitle(QString("TTEdit! - %1").arg(filename));
238 void MainWindow::SaveFile(void)
240 QString filename = QFileDialog::getSaveFileName(this, tr("Save Glyph File"),
241 "./", tr("Glyph files (*.glyph)"));
242 FILE * file = fopen(filename.toUtf8().data(), "w");
244 //need to pop an error box here...
248 editWnd->pts.SaveGlyphToFile(file);
250 setWindowTitle(QString("TTEdit! - %1").arg(filename));
255 void MainWindow::ZoomIn(void)
257 if (editWnd->scale == 4.0)
260 editWnd->scale *= 2.0;
261 scaleIndicator->setText(QString("Scale: %1%").arg(editWnd->scale * 100.0));
266 void MainWindow::ZoomOut(void)
268 if (editWnd->scale == 0.25)
271 editWnd->scale *= 0.5;
272 scaleIndicator->setText(QString("Scale: %1%").arg(editWnd->scale * 100.0));
277 void MainWindow::ReadSettings(void)
279 QSettings settings("Underground Software", "TTEdit");
280 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
281 QSize size = settings.value("size", QSize(400, 400)).toSize();
284 pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
285 size = settings.value("charWndSize", QSize(200, 200)).toSize();
286 ((TTEdit *)qApp)->charWnd->resize(size);
287 ((TTEdit *)qApp)->charWnd->move(pos);
291 void MainWindow::WriteSettings(void)
293 QSettings settings("Underground Software", "TTEdit");
294 settings.setValue("pos", pos());
295 settings.setValue("size", size());
296 settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
297 settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
304 IMPLEMENT_APP(TTEditApp) // Run the main application loop
306 bool TTEditApp::OnInit()
308 wxLog * logTarget = new wxLogStderr();//fopen("!ttedit_log.txt", "wb"));
309 wxLog::SetActiveTarget(logTarget);
314 // Initialize all the top-level window members to NULL.
318 for(int i=0; i<8; i++)
321 //Shouldn't we check to see if it was successful? This just assumes
324 mainFrame = new TTEditFrame(NULL, _("TTEdit"), wxPoint(155, 165), wxSize(300, 300),
325 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE);
326 // wxMINIMIZE_BOX | wxRESIZE_BOX | wxMAXIMIZE_BOX | | wxSYSTEM_MENU | wxCAPTION);
328 // charWin = new CharWindow(NULL);//, _T("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize);
329 charWin = new CharWindow(mainFrame, _("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize,
330 wxCAPTION | wxRESIZE_BORDER | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
332 toolPalette = new ToolWindow(mainFrame, _(""), wxDefaultPosition, wxDefaultSize,
333 wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
338 int TTEditApp::OnExit()
343 for(int i=0; i<8; i++)
351 // OS dependent method of creating cursor (works for Win32 and GTK+)
353 #define CREATE_CURSOR(storage, name, hsx, hsy) \
354 wxBitmap name##_bitmap(name##_xpm); \
355 wxImage name##_image = name##_bitmap.ConvertToImage(); \
356 name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hsx); \
357 name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hsy); \
358 storage = new wxCursor(name##_image);
360 void TTEditApp::CreateResources(void)
362 // This is a sucky way to create cursors, but at least it's cross-platform...
363 // NOTE: Need to fix hotspots on a few... !!! FIX !!! [DONE]
365 CREATE_CURSOR(cur[0], cur1, 1, 1);
366 CREATE_CURSOR(cur[1], cur2, 1, 1);
367 CREATE_CURSOR(cur[2], cur3, 11, 11); // Prolly won't need this soon (scroll)...
368 CREATE_CURSOR(cur[3], cur4, 15, 13); // Prolly won't need this soon (zoom)...
369 CREATE_CURSOR(cur[4], cur5, 1, 1);
370 CREATE_CURSOR(cur[5], cur6, 1, 1);
371 CREATE_CURSOR(cur[6], cur7, 1, 1);
372 CREATE_CURSOR(cur[7], cur8, 1, 1);
376 BEGIN_EVENT_TABLE(TTEditFrame, wxFrame)
377 EVT_MENU(IDM_OPEN, TTEditFrame::OnOpen)
378 EVT_MENU(IDM_EXIT, TTEditFrame::OnExit)
379 EVT_MENU(IDM_ABOUT, TTEditFrame::OnAbout)
380 EVT_MENU(ID_TBCHARWIN, TTEditFrame::OnCharWindow)
381 EVT_CLOSE(TTEditFrame::OnCloseWindow)
384 TTEditFrame::TTEditFrame(wxFrame * parent, const wxString &title, const wxPoint &pos,
385 const wxSize &size, long style): wxFrame(parent, -1, title, pos, size, style), app(wxGetApp()), mainWindow(NULL)
387 // Initialize child subwindow members (and hopefully avoid subtle bugs)
388 // mainWindow = NULL;
390 SetIcon(wxICON(ttedit));
391 // CreateStatusBar(2); // Create 2 panes
392 int widths[2] = { -1, 120 };
393 wxStatusBar * sb = CreateStatusBar(2, 0); // Create 2 panes
394 sb->SetStatusWidths(2, widths);
395 wxToolBar * tb = CreateToolBar();
401 wxBitmap tool1(tool1_xpm);
402 wxBitmap tool2(tool2_xpm);
403 wxBitmap tool3(tool3_xpm);
405 tb->AddTool(ID_TBLEFT, _("Prev char"), tool1, _("Go to prev char"), wxITEM_NORMAL);
406 tb->AddTool(ID_TBRIGHT, _("Next char"), tool2, _("Go to next char"), wxITEM_NORMAL);
407 tb->AddTool(ID_TBCHARWIN, _("Char Wnd"), tool3, _("Toggle char window"), wxITEM_CHECK);
411 // Create a menu bar for the frame
412 menuBar = new wxMenuBar;
413 wxMenu * menu1 = new wxMenu;
414 menu1->Append(IDM_NEW, _("&New\tCtrl+N"), _("Create a new font."));
415 menu1->Append(IDM_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing font."));
416 menu1->Append(IDM_SAVE, _("&Save\tCtrl+S"), _("Save the current font."));
417 menu1->Append(IDM_SAVEAS, _("Save &As..."), _("Save the current font under a different name."));
418 menu1->AppendSeparator();
419 menu1->Append(IDM_EXIT, _("E&xit\tAlt+X"), _("Quits the TTEdit program."));
420 menuBar->Append(menu1, _("&File"));
421 wxMenu * menu2 = new wxMenu;
422 menu2->Append(IDM_HELPTOPICS, _("&Help Topics"), _("Displays the Help contents and index."));
423 menu2->AppendSeparator();
424 menu2->Append(IDM_ABOUT, _("&About TTEdit"), _("Displays information about TTEdit."));
425 menuBar->Append(menu2, _("&Help"));
428 // Create child subwindows
429 mainWindow = new TTEditWindow(this);
431 Centre(wxBOTH); // Centre frame on the screen
432 Show(true); // Show the frame
435 TTEditFrame::~TTEditFrame()
439 void TTEditFrame::OnOpen(wxCommandEvent &e)
441 wxFileDialog fd(this, _("Choose a font to load"), _(""), _(""), _("TTF files (*.ttf)|*.ttf|All files (*.*)|*.*"), wxOPEN);
443 if (fd.ShowModal() != wxID_OK)
446 // Hmm. The font object is causing a massive crash... (gdb says it's in "Load")
447 if (app.font.Load((char *)fd.GetPath().c_str()) != true)
449 wxMessageDialog dlg(NULL, _("Load font failed!"), _("Houston, we have a problem..."), wxOK | wxICON_ERROR);
453 //Huzzah! It works! Now just need scaling, scrolling, etc...
454 // pts = app.font.GetGlyphPoints(45);
457 void TTEditFrame::OnAbout(wxCommandEvent &e)
459 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);
463 void TTEditFrame::OnExit(wxCommandEvent &e)
465 app.toolPalette->Destroy();
469 void TTEditFrame::OnCharWindow(wxCommandEvent &e)
471 app.charWin->Show(e.IsChecked() ? true : false);
477 void TTEditFrame::OnCloseWindow(wxCloseEvent &e)
479 app.toolPalette->Destroy();