I think I am now having less difficulty than before.
/* main.cpp */ #include "MyWindow.h" #include "MyArea.h" #include "MyButton.h" #include <gtkmm.h> int main(int argc, char** argv) { Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); MyWindow win; win.set_title("Object Oriented?"); win.set_default_size(400, 400); win.show_all_children(); return app->run(win); }
/* MyWindow.h */ #ifndef MYWINDOW_H_ #define MYWINDOW_H_ #include "MyArea.h" #include "MyButton.h" #include <gtkmm.h> class MyWindow : public Gtk::Window { public: MyWindow(); virtual ~MyWindow(); protected: Gtk::Box m_box; MyArea m_area; MyButton m_buttons; }; #endif /* MYWINDOW_H_ */
/* MyWindow.cpp */ #include "MyWindow.h" #include <gtkmm.h> MyWindow::MyWindow() : m_box(Gtk::ORIENTATION_VERTICAL) { add(m_box); m_box.pack_start(m_area); m_box.pack_start(m_buttons, false, true); } MyWindow::~MyWindow() { }
/* MyArea.h */ #ifndef MYAREA_H_ #define MYAREA_H_ #include <gtkmm.h> class MyArea : public Gtk::DrawingArea { public: MyArea(); virtual ~MyArea(); protected: virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr); }; #endif /* MYAREA_H_ */
/* MyArea.cpp */ #include "MyArea.h" #include <gtkmm.h> #include <cairomm/context.h> #include <complex> using namespace std; double x_min = -1.5; double x_max = 0.5; double y_min = -1.0; double y_max = 1.0; MyArea::MyArea() { } MyArea::~MyArea() { } bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr) { Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); cr->set_line_width(1.0); double large = 100.0; int kmax = 50; for(int i=0;i<width;i++) { for(int j=0;j<height;j++) { double x=x_min+(x_max-x_min)*(double)i/(double)width; double y=y_min+(y_max-y_min)*(double)j/(double)height; complex<double> z=0.0; complex<double> w=complex<double>(x,y); int count=0; for(int k=0;k<kmax;k++) { z=z*z+w; if(abs(z)>large) { count = k; break; } } if(count) { cr->set_source_rgba(0.9,(count%10)/10.0,0.3,1.0); } else { cr->set_source_rgba(0.0,0.2,0.7,1.0); } cr->rectangle((double)i, (double)j, 1.0, 1.0); cr->stroke(); } } return true; }
/* MyButton.h */ #ifndef MYBUTTON_H_ #define MYBUTTON_H_ #include <gtkmm.h> class MyButton : public Gtk::Box { public: MyButton(); virtual ~MyButton(); protected: void on_button1_clicked(); void on_button2_clicked(); void on_button3_clicked(); void on_button4_clicked(); void on_button5_clicked(); void on_button6_clicked(); Gtk::Button m_button1; Gtk::Button m_button2; Gtk::Button m_button3; Gtk::Button m_button4; Gtk::Button m_button5; Gtk::Button m_button6; }; #endif /* MYBUTTON_H_ */
/* MyButton.cpp */ #include "MyButton.h" extern double x_min; extern double x_max; extern double y_min; extern double y_max; MyButton::MyButton() : m_button1("enlarge"), m_button2("shrink"), m_button3("left"), m_button4("right"), m_button5("up"), m_button6("down") { pack_start(m_button1); pack_start(m_button2); pack_start(m_button3); pack_start(m_button4); pack_start(m_button5); pack_start(m_button6); m_button1.signal_clicked().connect(sigc::mem_fun(*this, &MyButton::on_button1_clicked) ); m_button2.signal_clicked().connect(sigc::mem_fun(*this, &MyButton::on_button2_clicked) ); m_button3.signal_clicked().connect(sigc::mem_fun(*this, &MyButton::on_button3_clicked) ); m_button4.signal_clicked().connect(sigc::mem_fun(*this, &MyButton::on_button4_clicked) ); m_button5.signal_clicked().connect(sigc::mem_fun(*this, &MyButton::on_button5_clicked) ); m_button6.signal_clicked().connect(sigc::mem_fun(*this, &MyButton::on_button6_clicked) ); } MyButton::~MyButton() { } void MyButton::on_button1_clicked() { double xc = (x_min + x_max) / 2.0; double yc = (y_min + y_max) / 2.0; double xs = (x_max - xc) * 0.75; double ys = (y_max - yc) * 0.75; x_min = xc - xs; x_max = xc + xs; y_min = yc - ys; y_max = yc + ys; get_window()->invalidate(true); } void MyButton::on_button2_clicked() { double xc = (x_min + x_max) / 2.0; double yc = (y_min + y_max) / 2.0; double xs = (x_max - xc) / 0.75; double ys = (y_max - yc) / 0.75; x_min = xc - xs; x_max = xc + xs; y_min = yc - ys; y_max = yc + ys; get_window()->invalidate(true); } void MyButton::on_button3_clicked() { double xc = (x_min + x_max) / 2.0; double xs = (x_max - xc) * 0.75; x_min += xs; x_max += xs; get_window()->invalidate(true); } void MyButton::on_button4_clicked() { double xc = (x_min + x_max) / 2.0; double xs = (x_max - xc) * 0.75; x_min -= xs; x_max -= xs; get_window()->invalidate(true); } void MyButton::on_button5_clicked() { double yc = (y_min + y_max) / 2.0; double ys = (y_max - yc) * 0.75; y_min += ys; y_max += ys; get_window()->invalidate(true); } void MyButton::on_button6_clicked() { double yc = (y_min + y_max) / 2.0; double ys = (y_max - yc) * 0.75; y_min -= ys; y_max -= ys; get_window()->invalidate(true); }