// File: GUILifeBoard.cpp // // Adapted from "Programming with GTKmm": // // #include #include "GUILifeBoard.h" GUILifeBoard::GUILifeBoard() : B(0,0,0) { // don't use this } GUILifeBoard::GUILifeBoard(unsigned int r, unsigned int c, bool *b) : m_ButtonStart("Go"), m_ButtonStop("Stop"), m_ButtonStep1("+1"), m_ButtonStep10("+10"), m_ButtonStep100("+100"), m_ButtonQuit("Quit"), B(r,c,b), keepRunning(false), runSteps(0), stepsTaken(0) { set_title("Game of Life"); set_border_width(5); set_default_size(1200, 800); add(m_VBox); //Add the TreeView, inside a ScrolledWindow, with the button underneath: m_ScrolledWindow.add(m_TextView); //Only show the scrollbars when they are necessary: m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); m_VBox.pack_start(m_ScrolledWindow); // Add buttons: // PACK_SHRINK means container shrunk down to size of child widgets // m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK); m_ButtonBox.pack_start(m_ButtonStart, Gtk::PACK_SHRINK); m_ButtonBox.pack_start(m_ButtonStop, Gtk::PACK_SHRINK); m_ButtonBox.pack_start(m_ButtonStep1, Gtk::PACK_SHRINK); m_ButtonBox.pack_start(m_ButtonStep10, Gtk::PACK_SHRINK); m_ButtonBox.pack_start(m_ButtonStep100, Gtk::PACK_SHRINK); m_ButtonBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK); m_ButtonBox.set_border_width(5); m_ButtonBox.set_spacing(5); m_ButtonBox.set_layout(Gtk::BUTTONBOX_END); // Connect button signals: // // sigc::mem_fun(*this, &GUILifeBoard::on_button_xxx) returns an // object (a functor) that can be used to call a member function of // the host object. The object is a "functor" because it has the () // operator overlaoded and can be used to make function calls. m_ButtonStart.signal_clicked().connect( sigc::mem_fun(*this, &GUILifeBoard::on_button_start) ) ; m_ButtonStop.signal_clicked().connect( sigc::mem_fun(*this, &GUILifeBoard::on_button_stop) ) ; m_ButtonStep1.signal_clicked().connect( sigc::mem_fun(*this, &GUILifeBoard::on_button_step1) ) ; m_ButtonStep10.signal_clicked().connect( sigc::mem_fun(*this, &GUILifeBoard::on_button_step10) ) ; m_ButtonStep100.signal_clicked().connect( sigc::mem_fun(*this, &GUILifeBoard::on_button_step100) ) ; m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this, &GUILifeBoard::on_button_quit) ) ; // Connect timeout signal. The on_timeout function is called every 0.2 // seconds when the main loop is idle. // sigc::slot my_slot = sigc::mem_fun(*this, &GUILifeBoard::on_timeout); Glib::signal_timeout().connect(my_slot, 200); // initialize game of life board ostringstream oss ; B.display(oss) ; // Configure the text window's parameters. // m_refTextBuffer = Gtk::TextBuffer::create() ; m_refTextBuffer->set_text(oss.str()) ; m_TextView.set_editable(false) ; Pango::FontDescription myFont("monaco 14") ; m_TextView.modify_font(myFont) ; m_TextView.set_buffer(m_refTextBuffer) ; // Let's see it! (Otherwise all elements are invisible.) // show_all_children(); } GUILifeBoard::~GUILifeBoard() { // Doesn't do anything, but needed } bool GUILifeBoard::on_timeout() { ostringstream oss ; if (!keepRunning && runSteps == 0) return true ; B.update() ; B.display(oss) ; stepsTaken++ ; oss << "Iteration #" << stepsTaken ; if (runSteps == 0) { oss << " Keep Running\n" ; } else { runSteps-- ; oss << " Run for " << runSteps << " steps\n" ; } m_refTextBuffer->set_text(oss.str()) ; return true ; } void GUILifeBoard::on_button_start() { keepRunning = true ; // yes, run! } void GUILifeBoard::on_button_stop() { ostringstream oss ; keepRunning = false ; // stop! // update display to not show "Keep Running" // B.display(oss) ; oss << "Iteration #" << stepsTaken ; if (runSteps > 0) { oss << " Run for " << runSteps << " steps" ; runSteps = 0 ; } oss << endl ; m_refTextBuffer->set_text(oss.str()) ; } void GUILifeBoard::on_button_step1() { keepRunning = false ; // not continuously runSteps = 1 ; } void GUILifeBoard::on_button_step10() { keepRunning = false ; // not continuously runSteps = 10 ; } void GUILifeBoard::on_button_step100() { keepRunning = false ; // not continuously runSteps = 100 ; } void GUILifeBoard::on_button_quit() { hide() ; // G'bye! }