From: Mikko Rasa Date: Wed, 26 Jun 2013 18:47:13 +0000 (+0300) Subject: Add DragHandle widget X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=31e9ee682f8a9cd77c97ed9dc142283559ddaacc;p=libs%2Fgltk.git Add DragHandle widget --- diff --git a/source/draghandle.cpp b/source/draghandle.cpp new file mode 100644 index 0000000..067a515 --- /dev/null +++ b/source/draghandle.cpp @@ -0,0 +1,44 @@ +#include "container.h" +#include "draghandle.h" + +namespace Msp { +namespace GLtk { + +DragHandle::DragHandle(): + dragging(false), + drag_x(0), + drag_y(0) +{ } + +void DragHandle::button_press(int x, int y, unsigned btn) +{ + if(btn==1 && parent) + { + dragging = true; + drag_x = x; + drag_y = y; + } +} + +void DragHandle::button_release(int, int, unsigned btn) +{ + if(btn==1) + dragging = false; +} + +void DragHandle::pointer_motion(int x, int y) +{ + if(dragging) + { + const Geometry &pgeom = parent->get_geometry(); + parent->set_position(pgeom.x+x-drag_x, pgeom.y+y-drag_y); + } +} + +void DragHandle::on_reparent() +{ + dragging = false; +} + +} // namespace GLtk +} // namespace Msp diff --git a/source/draghandle.h b/source/draghandle.h new file mode 100644 index 0000000..5643df4 --- /dev/null +++ b/source/draghandle.h @@ -0,0 +1,35 @@ +#ifndef MSP_GLTK_DRAGHANDLE_H_ +#define MSP_GLTK_DRAGHANDLE_H_ + +#include "widget.h" + +namespace Msp { +namespace GLtk { + +/** +Moves its parent widget when dragged. This allows turning a Panel or Dialog +into a movable window. +*/ +class DragHandle: public Widget +{ +private: + bool dragging; + int drag_x; + int drag_y; + +public: + DragHandle(); + + virtual const char *get_class() const { return "draghandle"; } + + virtual void button_press(int, int, unsigned); + virtual void button_release(int, int, unsigned); + virtual void pointer_motion(int, int); +private: + virtual void on_reparent(); +}; + +} // namespace GLtk +} // namespace Msp + +#endif diff --git a/source/panel.cpp b/source/panel.cpp index ab8f54f..836d991 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -3,6 +3,7 @@ #include #include "button.h" #include "column.h" +#include "draghandle.h" #include "dropdown.h" #include "entry.h" #include "grid.h" @@ -88,6 +89,7 @@ Panel::Loader::Loader(Panel &p, map &m): add("button", &Loader::child