// -*-c++-*- //------------------------------------------------------------------------------ // xword - (http://xword.sourceforge.net) // Copyright 2002 Patrick Crosby //------------------------------------------------------------------------------ // Interface.cpp // // $Id: Interface.cpp,v 1.11 2002/01/23 19:43:03 pcrosby Exp $ //------------------------------------------------------------------------------ // This program is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free Software // Foundation; either version 2 of the License, or (at your option) any later // version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // You should have received a copy of the GNU General Public License along with // this program; if not, write to the Free Software Foundation, Inc., 59 Temple // Place, Suite 330, Boston, MA 02111-1307 USA //------------------------------------------------------------------------------ #include #include "Interface.h" // #include // #include #include #include #include #include #include #include #include #include "Error.h" #include "Filename.h" #include "GridWidget.h" #include "Puzzle.h" #include "String.h" #include "Namespace.h" using namespace std; NAMESPACE_OPEN //------------------------------------------------------------------------------ Interface::Interface() : Gtk::Window(GTK_WINDOW_TOPLEVEL), m_pPuzzle(NULL), m_pGrid(NULL), m_FileSelection("Open puzzle") { set_title(PACKAGE " v" VERSION); set_border_width(10); set_usize(300, 300); Gtk::Table* pTable = manage(new Gtk::Table(5, 2)); add(*pTable); // toolbar // Gtk::Toolbar* pToolbar = manage(new Gtk::Toolbar(GTK_ORIENTATION_HORIZONTAL)); Gtk::Pixmap* pPixmap = manage(new Gtk::Pixmap(PACKAGE_DATA_DIR "/open.xpm")); pToolbar->tools().push_back( Gtk::Toolbar_Helpers::ButtonElem(*pPixmap, slot(this, &Interface::ButtonOpen), "Open")); pPixmap = manage(new Gtk::Pixmap(PACKAGE_DATA_DIR "/save.xpm")); pToolbar->tools().push_back( Gtk::Toolbar_Helpers::ButtonElem(*pPixmap, slot(this, &Interface::ButtonSave), "Save")); pPixmap = manage(new Gtk::Pixmap(PACKAGE_DATA_DIR "/exit.xpm")); pToolbar->tools().push_back( Gtk::Toolbar_Helpers::ButtonElem(*pPixmap, slot(this, &Interface::ButtonExit), "Exit")); pTable->attach(*pToolbar, 0, 1, 0, 1); // logo // pPixmap = manage(new Gtk::Pixmap(PACKAGE_DATA_DIR "/xword2.xpm")); pTable->attach(*pPixmap, 1, 2, 0, 1); // clues // Gtk::Table* pTableClues = manage(new Gtk::Table(2, 3)); pTable->attach(*pTableClues, 0, 2, 1, 2); m_labelAcrossNum.set_alignment(1.0, 0.5); m_labelDownNum.set_alignment(1.0, 0.5); pTableClues->attach(m_labelAcrossNum, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 5); pTableClues->attach(m_labelDownNum, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 5); Gtk::Label* pLabel = manage(new Gtk::Label("Across: ")); pLabel->set_alignment(0.0, 0.5); pTableClues->attach(*pLabel, 1, 2, 0, 1, GTK_SHRINK); pLabel = manage(new Gtk::Label("Down: ")); pLabel->set_alignment(0.0, 0.5); pTableClues->attach(*pLabel, 1, 2, 1, 2, GTK_SHRINK); m_labelAcross.set_alignment(0.0, 0.5); m_labelDown.set_alignment(0.0, 0.5); pTableClues->attach(m_labelAcross, 2, 3, 0, 1); pTableClues->attach(m_labelDown, 2, 3, 1, 2); // grid // m_pGrid = manage(new GridWidget()); pTable->attach(*m_pGrid, 0, 2, 2, 3, GTK_SHRINK, GTK_SHRINK); // title/author // pTable->attach(m_labelTitle, 0, 2, 3, 4); pTable->attach(m_labelAuthor, 0, 2, 4, 5); // file open // // m_FileSelection.complete("*.puz"); m_FileSelection.set_modal(true); m_FileSelection.get_ok_button()->clicked.connect( slot(this, &Interface::ButtonFileOK)); m_FileSelection.get_cancel_button()->clicked.connect( slot(m_FileSelection, &Gtk::FileSelection::hide_all)); // events/signals // add_events(GDK_KEY_PRESS_MASK); m_pGrid->CursorMoved.connect(slot(this, &Interface::CursorMoved)); show_all(); } //------------------------------------------------------------------------------ Interface::~Interface() { } //------------------------------------------------------------------------------ gint Interface::delete_event_impl(GdkEventAny* pEvent) { Gtk::Main::quit(); return 0; } //------------------------------------------------------------------------------ gint Interface::key_press_event_impl(GdkEventKey* pEvent) { bool bHandled = false; if (pEvent->state & GDK_CONTROL_MASK) { if (pEvent->keyval == 'S' || pEvent->keyval == 's') { m_pPuzzle->Save(); bHandled = true; } } if (!bHandled) { if (!m_pGrid->key_press_event_impl(pEvent)) { Gtk::Window::key_press_event_impl(pEvent); } } return 1; } //------------------------------------------------------------------------------ void Interface::SetPuzzle(const std::string& strFilename) { delete m_pPuzzle; m_pPuzzle = new Puzzle(strFilename); // m_pPuzzle->Dump(); m_labelTitle.set(m_pPuzzle->GetTitle()); m_labelAuthor.set(m_pPuzzle->GetAuthor() + " -- " + m_pPuzzle->GetCopyright()); if (m_pGrid != NULL) { m_pGrid->SetPuzzle(m_pPuzzle); // m_pGrid->SetGrid(m_pPuzzle->GetGrid()); int nWidth = max(m_pGrid->GetWidth(), m_labelAuthor.width()) + 20; int nHeight = m_pGrid->GetHeight() + 150; set_usize(nWidth, nHeight); get_window().move_resize(0, 0, nWidth, nHeight); } else { throw(runtime_error("m_pGrid is null")); } } //------------------------------------------------------------------------------ void Interface::CursorMoved() { int nX = m_pGrid->GetCursorX(); int nY = m_pGrid->GetCursorY(); // cout << "cursor: (" << nX << ", " << nY << ")" << endl; std::string s; int nClue; m_pPuzzle->GetClue(nX, nY, true, s, nClue); m_labelAcross.set(s); m_labelAcrossNum.set(String(nClue)); m_pPuzzle->GetClue(nX, nY, false, s, nClue); m_labelDown.set(s); m_labelDownNum.set(String(nClue)); } //------------------------------------------------------------------------------ void Interface::ButtonOpen() { m_FileSelection.show_all(); } //------------------------------------------------------------------------------ void Interface::ButtonSave() { AssertNotNull(m_pPuzzle); m_pPuzzle->Save(); } //------------------------------------------------------------------------------ void Interface::ButtonExit() { Gtk::Main::quit(); } //------------------------------------------------------------------------------ void Interface::ButtonFileOK() { m_FileSelection.hide_all(); Filename strFile = m_FileSelection.get_filename(); SetPuzzle(strFile); } //------------------------------------------------------------------------------ NAMESPACE_CLOSE //------------------------------------------------------------------------------