]> Shamusworld >> Repos - ttedit/blobdiff - src/mainwindow.cpp
Added rotation tool, save/load capability.
[ttedit] / src / mainwindow.cpp
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
new file mode 100644 (file)
index 0000000..8c7524f
--- /dev/null
@@ -0,0 +1,460 @@
+//
+// MAINWINDOW.CPP - The TrueType Editor
+// by James L. Hammons
+// (C) 2004 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// Who  When        What
+// ---  ----------  -------------------------------------------------------------
+// JLH  04/10/2002  Created this file
+// JLH  05/10/2004  Translated file from ASM to CPP
+// JLH  05/14/2004  Added rudimentary editing capability to tool palette tools
+// JLH  11/18/2006  Initial port to Linux
+// JLH  08/27/2008  Fixed tool palette focus problems
+// JLH  08/28/2008  Split out classes defined here into separate files
+// JLH  03/05/2009  Initial conversion from wxWidgets to Qt
+//
+
+// FIXED:
+//
+// - Fix problem with tool palette not getting focus 1st time it's called up [DONE]
+// - Split out windows/classes defined here into their own files [DONE]
+//
+// STILL TO BE DONE:
+//
+// - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font
+// - Fix scrolling, zooming, settings (ini)
+// - Fix problem with character window disappearing when bringing up the tool
+//   palette
+//
+
+// Uncomment this for debugging...
+#define DEBUG
+#define DEBUGFOO                       // Various tool debugging...
+//#define DEBUGTP                              // Toolpalette debugging...
+
+#include "mainwindow.h"
+#include "charwindow.h"
+#include "editwindow.h"
+#include "ttedit.h"
+
+
+MainWindow::MainWindow()
+{
+       ((TTEdit *)qApp)->charWnd = new CharWindow(this);
+       editWnd = new EditWindow(this);
+       setCentralWidget(editWnd);
+       setWindowIcon(QIcon(":/res/ttedit.png"));
+       setWindowTitle("TTEdit!");
+
+#if 0
+//     createActions();
+       newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
+       newAct->setShortcuts(QKeySequence::New);
+       newAct->setStatusTip(tr("Create a new file"));
+       connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
+
+       openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
+       openAct->setShortcuts(QKeySequence::Open);
+       openAct->setStatusTip(tr("Open an existing file"));
+       connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
+
+       aboutQtAct = new QAction(tr("About &Qt"), this);
+       aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
+       connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
+
+//     createMenus();
+       fileMenu = menuBar()->addMenu(tr("&File"));
+       fileMenu->addAction(newAct);
+       fileMenu->addAction(openAct);
+       fileMenu->addAction(saveAct);
+       fileMenu->addAction(saveAsAct);
+       fileMenu->addSeparator();
+       fileMenu->addAction(exitAct);
+
+       editMenu = menuBar()->addMenu(tr("&Edit"));
+       editMenu->addAction(cutAct);
+       editMenu->addAction(copyAct);
+       editMenu->addAction(pasteAct);
+
+       menuBar()->addSeparator();
+
+       helpMenu = menuBar()->addMenu(tr("&Help"));
+       helpMenu->addAction(aboutAct);
+       helpMenu->addAction(aboutQtAct);
+
+//     createToolBars();
+       fileToolBar = addToolBar(tr("File"));
+       fileToolBar->addAction(newAct);
+       fileToolBar->addAction(openAct);
+       fileToolBar->addAction(saveAct);
+
+       editToolBar = addToolBar(tr("Edit"));
+       editToolBar->addAction(cutAct);
+       editToolBar->addAction(copyAct);
+       editToolBar->addAction(pasteAct);
+#else
+       CreateActions();
+       CreateMenus();
+       CreateToolbars();
+#endif
+
+       //      Create status bar
+       statusBar()->showMessage(tr("Ready"));
+
+       ReadSettings();
+
+//     connect(textEdit->document(), SIGNAL(contentsChanged()),
+//                     this, SLOT(documentWasModified()));
+
+//     setCurrentFile("");
+       setUnifiedTitleAndToolBarOnMac(true);
+
+       ((TTEdit *)qApp)->charWnd->show();//eh?
+}
+
+
+//
+// Consolidates action creation from a multi-step process to a single-step one.
+//
+QAction * MainWindow::CreateAction(QString name, QString tooltip, QString statustip,
+       QIcon icon, QKeySequence key, bool checkable/*= false*/)
+{
+       QAction * action = new QAction(icon, name, this);
+       action->setToolTip(tooltip);
+       action->setStatusTip(statustip);
+       action->setShortcut(key);
+       action->setCheckable(checkable);
+
+       return action;
+}
+
+
+//
+// This is essentially the same as the previous function, but this allows more
+// than one key sequence to be added as key shortcuts.
+//
+QAction * MainWindow::CreateAction(QString name, QString tooltip, QString statustip,
+       QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
+{
+       QAction * action = new QAction(icon, name, this);
+       action->setToolTip(tooltip);
+       action->setStatusTip(statustip);
+       QList<QKeySequence> keyList;
+       keyList.append(key1);
+       keyList.append(key2);
+       action->setShortcuts(keyList);
+       action->setCheckable(checkable);
+
+       return action;
+}
+
+
+void MainWindow::CreateActions(void)
+{
+       newGlyphAct = CreateAction("&New Glyph", "New Glyph", "Create a new glyph", QIcon(), QKeySequence());
+       openFileAct = CreateAction("&Open File", "Open File", "Open a glyph file", QIcon(), QKeySequence());
+       saveFileAct = CreateAction("&Save File", "Save File", "Save a glyph file", QIcon(), QKeySequence());
+
+       connect(newGlyphAct, SIGNAL(triggered()), this, SLOT(NewGlyph()));
+       connect(openFileAct, SIGNAL(triggered()), this, SLOT(OpenFile()));
+       connect(saveFileAct, SIGNAL(triggered()), this, SLOT(SaveFile()));
+}
+
+
+void MainWindow::CreateMenus(void)
+{
+       QMenu * menu = menuBar()->addMenu(tr("&File"));
+       menu->addAction(newGlyphAct);
+       menu->addAction(openFileAct);
+       menu->addAction(saveFileAct);
+//     menu->addAction(fileSaveAsAct);
+//     menu->addAction(fileCloseAct);
+}
+
+
+void MainWindow::CreateToolbars(void)
+{
+}
+
+
+void MainWindow::closeEvent(QCloseEvent * event)
+{
+       WriteSettings();
+       event->accept(); // ignore() if can't close for some reason
+}
+
+
+void MainWindow::NewGlyph(void)
+{
+       editWnd->pts.Clear();
+       ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
+       ((TTEdit *)qApp)->charWnd->update();
+       editWnd->update();
+}
+
+
+void MainWindow::OpenFile(void)
+{
+       QString filename = QFileDialog::getOpenFileName(this, tr("Open Glyph File"),
+               "./", tr("Glyph files (*.glyph)"));
+       FILE * file = fopen(filename.toAscii().data(), "r");
+
+       //need to pop an error box here...
+       if (file == 0)
+               return;
+
+       editWnd->pts.LoadGlyphFromFile(file);
+       fclose(file);
+
+       ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
+       ((TTEdit *)qApp)->charWnd->update();
+       editWnd->update();
+}
+
+
+void MainWindow::SaveFile(void)
+{
+       QString filename = QFileDialog::getSaveFileName(this, tr("Save Glyph File"),
+               "./", tr("Glyph files (*.glyph)"));
+       FILE * file = fopen(filename.toAscii().data(), "w");
+
+       //need to pop an error box here...
+       if (file == 0)
+               return;
+
+       editWnd->pts.SaveGlyphToFile(file);
+       fclose(file);
+}
+
+
+void MainWindow::ReadSettings(void)
+{
+       QSettings settings("Underground Software", "TTEdit");
+       QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
+       QSize size = settings.value("size", QSize(400, 400)).toSize();
+       resize(size);
+       move(pos);
+       pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
+       size = settings.value("charWndSize", QSize(200, 200)).toSize();
+       ((TTEdit *)qApp)->charWnd->resize(size);
+       ((TTEdit *)qApp)->charWnd->move(pos);
+}
+
+
+void MainWindow::WriteSettings(void)
+{
+       QSettings settings("Underground Software", "TTEdit");
+       settings.setValue("pos", pos());
+       settings.setValue("size", size());
+       settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
+       settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
+}
+
+
+#if 0
+#include "ttedit.h"
+#include "charwindow.h"
+#include "toolwindow.h"
+#include "editwindow.h"
+#include "tte_res.h"                                                   // Resource IDs
+#ifdef DEBUG
+#include "debug.h"
+#endif
+
+// Pixmap resouces
+
+#include "res/cur1.xpm"
+#include "res/cur2.xpm"
+#include "res/cur3.xpm"
+#include "res/cur4.xpm"
+#include "res/cur5.xpm"
+#include "res/cur6.xpm"
+#include "res/cur7.xpm"
+#include "res/cur8.xpm"
+#include "res/ttedit.xpm"                                              // *nix only, but small enough to not matter
+#include "res/tool1.xpm"
+#include "res/tool2.xpm"
+#include "res/tool3.xpm"
+
+
+IMPLEMENT_APP(TTEditApp)                                               // Run the main application loop
+
+bool TTEditApp::OnInit()
+{
+       wxLog * logTarget = new wxLogStderr();//fopen("!ttedit_log.txt", "wb"));
+       wxLog::SetActiveTarget(logTarget);
+#ifdef DEBUG
+       OpenDebugLog();
+#endif
+
+       // Initialize all the top-level window members to NULL.
+       mainFrame = NULL;
+       charWin = NULL;
+       toolPalette = NULL;
+       for(int i=0; i<8; i++)
+               cur[i] = NULL;
+
+//Shouldn't we check to see if it was successful? This just assumes
+       CreateResources();
+
+       mainFrame = new TTEditFrame(NULL, _("TTEdit"), wxPoint(155, 165), wxSize(300, 300),
+               wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE);
+//             wxMINIMIZE_BOX | wxRESIZE_BOX | wxMAXIMIZE_BOX | | wxSYSTEM_MENU | wxCAPTION);
+
+//     charWin = new CharWindow(NULL);//, _T("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize);
+       charWin = new CharWindow(mainFrame, _("Own3d W1nd0w"), wxDefaultPosition, wxDefaultSize,
+               wxCAPTION | wxRESIZE_BORDER | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
+
+       toolPalette = new ToolWindow(mainFrame, _(""), wxDefaultPosition, wxDefaultSize,
+               wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT);
+
+       return true;
+}
+
+int TTEditApp::OnExit()
+{
+#ifdef DEBUG
+       CloseDebugLog();
+#endif
+       for(int i=0; i<8; i++)
+               if (cur[i])
+                       delete cur[i];
+
+       return 0;
+}
+
+//
+// OS dependent method of creating cursor (works for Win32 and GTK+)
+//
+#define CREATE_CURSOR(storage, name, hsx, hsy) \
+       wxBitmap name##_bitmap(name##_xpm); \
+       wxImage name##_image = name##_bitmap.ConvertToImage(); \
+       name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hsx); \
+       name##_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hsy); \
+       storage = new wxCursor(name##_image);
+
+void TTEditApp::CreateResources(void)
+{
+       // This is a sucky way to create cursors, but at least it's cross-platform...
+       // NOTE: Need to fix hotspots on a few... !!! FIX !!! [DONE]
+
+       CREATE_CURSOR(cur[0], cur1, 1, 1);
+       CREATE_CURSOR(cur[1], cur2, 1, 1);
+       CREATE_CURSOR(cur[2], cur3, 11, 11);    // Prolly won't need this soon (scroll)...
+       CREATE_CURSOR(cur[3], cur4, 15, 13);    // Prolly won't need this soon (zoom)...
+       CREATE_CURSOR(cur[4], cur5, 1, 1);
+       CREATE_CURSOR(cur[5], cur6, 1, 1);
+       CREATE_CURSOR(cur[6], cur7, 1, 1);
+       CREATE_CURSOR(cur[7], cur8, 1, 1);
+}
+
+
+BEGIN_EVENT_TABLE(TTEditFrame, wxFrame)
+       EVT_MENU(IDM_OPEN, TTEditFrame::OnOpen)
+       EVT_MENU(IDM_EXIT, TTEditFrame::OnExit)
+       EVT_MENU(IDM_ABOUT, TTEditFrame::OnAbout)
+       EVT_MENU(ID_TBCHARWIN, TTEditFrame::OnCharWindow)
+       EVT_CLOSE(TTEditFrame::OnCloseWindow)
+END_EVENT_TABLE()
+
+TTEditFrame::TTEditFrame(wxFrame * parent, const wxString &title, const wxPoint &pos,
+       const wxSize &size, long style): wxFrame(parent, -1, title, pos, size, style), app(wxGetApp()), mainWindow(NULL)
+{
+       // Initialize child subwindow members (and hopefully avoid subtle bugs)
+//     mainWindow = NULL;
+
+       SetIcon(wxICON(ttedit));
+//     CreateStatusBar(2);                                                     // Create 2 panes
+       int widths[2] = { -1, 120 };
+       wxStatusBar * sb = CreateStatusBar(2, 0);       // Create 2 panes
+       sb->SetStatusWidths(2, widths);
+       wxToolBar * tb = CreateToolBar();
+
+       if (tb != NULL)
+       {
+               // Create buttons
+
+               wxBitmap tool1(tool1_xpm);
+               wxBitmap tool2(tool2_xpm);
+               wxBitmap tool3(tool3_xpm);
+
+               tb->AddTool(ID_TBLEFT, _("Prev char"), tool1, _("Go to prev char"), wxITEM_NORMAL);
+               tb->AddTool(ID_TBRIGHT, _("Next char"), tool2, _("Go to next char"), wxITEM_NORMAL);
+               tb->AddTool(ID_TBCHARWIN, _("Char Wnd"), tool3, _("Toggle char window"), wxITEM_CHECK);
+               tb->Realize();
+       }
+
+       // Create a menu bar for the frame
+       menuBar = new wxMenuBar;
+       wxMenu * menu1 = new wxMenu;
+       menu1->Append(IDM_NEW, _("&New\tCtrl+N"), _("Create a new font."));
+       menu1->Append(IDM_OPEN, _("&Open...\tCtrl+O"), _("Opens an existing font."));
+       menu1->Append(IDM_SAVE, _("&Save\tCtrl+S"), _("Save the current font."));
+       menu1->Append(IDM_SAVEAS, _("Save &As..."), _("Save the current font under a different name."));
+       menu1->AppendSeparator();
+       menu1->Append(IDM_EXIT, _("E&xit\tAlt+X"), _("Quits the TTEdit program."));
+       menuBar->Append(menu1, _("&File"));
+       wxMenu * menu2 = new wxMenu;
+       menu2->Append(IDM_HELPTOPICS, _("&Help Topics"), _("Displays the Help contents and index."));
+       menu2->AppendSeparator();
+       menu2->Append(IDM_ABOUT, _("&About TTEdit"), _("Displays information about TTEdit."));
+       menuBar->Append(menu2, _("&Help"));
+       SetMenuBar(menuBar);
+
+       // Create child subwindows
+       mainWindow = new TTEditWindow(this);
+
+       Centre(wxBOTH);                                                         // Centre frame on the screen
+       Show(true);                                                                     // Show the frame
+}
+
+TTEditFrame::~TTEditFrame()
+{
+}
+
+void TTEditFrame::OnOpen(wxCommandEvent &e)
+{
+       wxFileDialog fd(this, _("Choose a font to load"), _(""), _(""), _("TTF files (*.ttf)|*.ttf|All files (*.*)|*.*"), wxOPEN);
+
+       if (fd.ShowModal() != wxID_OK)
+           return;
+
+// Hmm. The font object is causing a massive crash... (gdb says it's in "Load")
+       if (app.font.Load((char *)fd.GetPath().c_str()) != true)
+       {
+               wxMessageDialog dlg(NULL, _("Load font failed!"), _("Houston, we have a problem..."), wxOK | wxICON_ERROR);
+               dlg.ShowModal();
+       }
+
+//Huzzah! It works! Now just need scaling, scrolling, etc...
+//     pts = app.font.GetGlyphPoints(45);
+}
+
+void TTEditFrame::OnAbout(wxCommandEvent &e)
+{
+       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);
+       dlg.ShowModal();
+}
+
+void TTEditFrame::OnExit(wxCommandEvent &e)
+{
+       app.toolPalette->Destroy();
+       this->Destroy();
+}
+
+void TTEditFrame::OnCharWindow(wxCommandEvent &e)
+{
+       app.charWin->Show(e.IsChecked() ? true : false);
+
+       if (e.IsChecked())
+               Raise();
+}
+
+void TTEditFrame::OnCloseWindow(wxCloseEvent &e)
+{
+       app.toolPalette->Destroy();
+       this->Destroy();
+}
+#endif