Static Data Members

One way to avoid global variables such as appeared in my previous article is to use static data members.

class MyArea : public Gtk::DrawingArea
{
public:
  MyArea();
  virtual ~MyArea();
protected:
  virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
  virtual bool on_button_press_event  ( GdkEventButton* event );
  virtual bool on_button_release_event( GdkEventButton* event );
public:
  static double x_min;
  static double x_max;
  static double y_min;
  static double y_max;
private:
  double x_press;
  double y_press;
  double x_release;
  double y_release;
};
double MyArea::x_min {-1.5};
double MyArea::x_max { 0.5};
double MyArea::y_min {-1.0};
double MyArea::y_max { 1.0};

MyArea::MyArea() : x_press {0.0}, y_press {0.0}, x_release {0.0}, y_release {0.0}
{
	add_events( Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK );
}
void MyButton::on_button1_clicked() {
	double xc = (MyArea::x_min + MyArea::x_max) / 2.0;
	double yc = (MyArea::y_min + MyArea::y_max) / 2.0;
	double xs = (MyArea::x_max - xc) * 0.67;
	double ys = (MyArea::y_max - yc) * 0.67;
	MyArea::x_min = xc - xs;
	MyArea::x_max = xc + xs;
	MyArea::y_min = yc - ys;
	MyArea::y_max = yc + ys;
	get_window()->invalidate(true);
}

I do not know if this is any improvement.

Leave a Reply

Your email address will not be published. Required fields are marked *