dragging figure over drawing area



Hi! I have made a subclass of DrawingArea called DrawingPanel. The purpose of this class is drag a Figure (circle, rectangle, etc.) when the user moves the mouse over the area with the button pressed.

The code works fine but I don't know if this solution is a good solution, here is the code (maybe it can be useful for other programmers)

class DrawingPanel : public Gtk::DrawingArea {

   bool buttonPressed;
   int incr_x, incr_y;
   // ...

public:
   DrawingPanel() : DrawingArea(), ... {
      add_events(
         Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK
      );
      buttonPressed = false;
   }
   // handler methods...
}

bool DrawingPanel::on_button_release_event(GdkEventButton* event)  {

   buttonPressed = false;
   return true;
}

bool DrawingPanel::on_button_press_event(GdkEventButton* event) {

   buttonPressed = true;
   point_t pc = figure.getCenter(); // point_t is an structure with (x, y) attributes
   incr_x = pc.x - (int)event->x;
   incr_y = pc.y - (int)event->y;
   return true;
}

bool DrawingPanel::on_motion_notify_event(GdkEventMotion* event) {

   if( buttonPressed ) {
      point_t pc = figure.getCenter();
      figure.setCenter(event->x + incr_x, event->y + incr_y);

      queue_draw(); // update the figure
   }
   return true;
}

bool DrawingPanel::on_expose_event(GdkEventExpose* event) {
   // ...
   figure.draw(context);  // draws the figure with the new center coordinates
   return true;
}

Now I have a second question. I would like to associate a region limits to each figure and I don't know (I'm starting with gtkmm) if exists some class to do this or not. The basic idea is drag the figure only when the mouse pointer is over the region limits of the figure. I have no problem to do this "from scratch" but better if gtkmm provides some utility class.

Thanks in advance and sorry if my english is not correct.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]