]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add DragHandle widget
authorMikko Rasa <tdb@tdb.fi>
Wed, 26 Jun 2013 18:47:13 +0000 (21:47 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 26 Jun 2013 18:47:13 +0000 (21:47 +0300)
source/draghandle.cpp [new file with mode: 0644]
source/draghandle.h [new file with mode: 0644]
source/panel.cpp

diff --git a/source/draghandle.cpp b/source/draghandle.cpp
new file mode 100644 (file)
index 0000000..067a515
--- /dev/null
@@ -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 (file)
index 0000000..5643df4
--- /dev/null
@@ -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
index ab8f54fb0cc5471e6a1415ff17add966d51c02a5..836d991eb73e3b226d859b5c2c4a2da7acf5037a 100644 (file)
@@ -3,6 +3,7 @@
 #include <msp/core/refptr.h>
 #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<string, Widget *> &m):
        add("button",    &Loader::child<Button>);
        add("column",    &Loader::arrangement<Column>);
        add("constraint",&Loader::constraint);
+       add("draghandle",&Loader::child<DragHandle>);
        add("dropdown",  &Loader::child<Dropdown>);
        add("entry",     &Loader::child<Entry>);
        add("expand",    &Loader::expand);