Add mousehover event (only SDL).
authorRadek Brich <radek.brich@devl.cz>
Wed, 30 Jan 2013 20:21:08 +0100
changeset 73 85a282b5e4fc
parent 72 6e0656600754
child 74 23767a33a781
Add mousehover event (only SDL).
demo_input.py
docs/events.rst
sdlterm/cython/sdlterm.pyx
sdlterm/src/sdlterm.cc
sdlterm/src/sdlterm.h
tuikit/container.py
tuikit/widget.py
--- a/demo_input.py	Wed Jan 30 19:44:01 2013 +0100
+++ b/demo_input.py	Wed Jan 30 20:21:08 2013 +0100
@@ -21,6 +21,7 @@
         editbox.add_handler('mouseup', self.on_any_input)
         editbox.add_handler('mousewheel', self.on_any_input)
         editbox.add_handler('mousemove', self.on_any_input)
+        editbox.add_handler('mousehover', self.on_any_input)
 
     def on_any_input(self, ev):
         if ev.event_name == 'keypress' and ev.char == 'q':
--- a/docs/events.rst	Wed Jan 30 19:44:01 2013 +0100
+++ b/docs/events.rst	Wed Jan 30 20:21:08 2013 +0100
@@ -52,3 +52,19 @@
 Normal events are propagated in top down order, to all children in each level, in order of child addition.
 Some events choose one or more of children for event propagation. In case of mouse events, child is chosen based on mouse cursor position.
 
+
+Standard events
+---------------
+
+* mousedown
+  - report button press (left, mdiddle, right)
+* mouseup
+  - report button released
+* mousemove
+  - report mouse position change (only when button pressed)
+  - for use in combination with mousedown, mouseup
+* mousehover
+  - report mouse position change (only when all buttons released)
+* mousewheel
+  - report wheel up, down (as buttons)
+
--- a/sdlterm/cython/sdlterm.pyx	Wed Jan 30 19:44:01 2013 +0100
+++ b/sdlterm/cython/sdlterm.pyx	Wed Jan 30 20:21:08 2013 +0100
@@ -28,6 +28,7 @@
         int MOUSEDOWN
         int MOUSEUP
         int MOUSEMOVE
+        int MOUSEHOVER
         int MOUSEWHEEL
 
         int type
@@ -92,6 +93,8 @@
             # timeout
             return None
         event = self.event
+        if event.type == event.MOUSEHOVER:
+            return ('mousehover', 0, event.mouse.x, event.mouse.y)
         if event.type == event.MOUSEMOVE:
             return ('mousemove', 0, event.mouse.x, event.mouse.y, event.mouse.relx, event.mouse.rely)
         if event.type == event.MOUSEDOWN:
--- a/sdlterm/src/sdlterm.cc	Wed Jan 30 19:44:01 2013 +0100
+++ b/sdlterm/src/sdlterm.cc	Wed Jan 30 20:21:08 2013 +0100
@@ -500,25 +500,29 @@
             return true;
 
         case SDL_MOUSEMOTION:
-            if (sdl_event.motion.state == 0 || _mousemove_last_x == -1)
-            {
-                // do not report move events when no button is pressed
-                return false;
-            }
-            event.type = Event::MOUSEMOVE;
             event.mouse.x = sdl_event.motion.x / _screen.get_cell_width();
             event.mouse.y = sdl_event.motion.y / _screen.get_cell_height();
-            if (_mousemove_last_x != event.mouse.x ||
-                _mousemove_last_y != event.mouse.y)
+            if (_mousemove_last_x == event.mouse.x &&
+                _mousemove_last_y == event.mouse.y)
+            {
+                // mouse position did not change
+                return false;
+            }
+            if (sdl_event.motion.state == 0 || _mousemove_last_x == -1)
             {
+                // no button is pressed or last pos not initialized
+                event.type = Event::MOUSEHOVER;
+            }
+            else
+            {
+                // some button pressed
+                event.type = Event::MOUSEMOVE;
                 event.mouse.relx = event.mouse.x - _mousemove_last_x;
                 event.mouse.rely = event.mouse.y - _mousemove_last_y;
-                _mousemove_last_x = event.mouse.x;
-                _mousemove_last_y = event.mouse.y;
-                return true;
             }
-            // mouse position did not change
-            return false;
+            _mousemove_last_x = event.mouse.x;
+            _mousemove_last_y = event.mouse.y;
+            return true;
 
         default:
             // unknown event
--- a/sdlterm/src/sdlterm.h	Wed Jan 30 19:44:01 2013 +0100
+++ b/sdlterm/src/sdlterm.h	Wed Jan 30 20:21:08 2013 +0100
@@ -177,7 +177,7 @@
 
 struct Event
 {
-    enum { QUIT, RESIZE, KEYPRESS, MOUSEDOWN, MOUSEUP, MOUSEMOVE, MOUSEWHEEL };
+    enum { QUIT, RESIZE, KEYPRESS, MOUSEDOWN, MOUSEUP, MOUSEMOVE, MOUSEHOVER, MOUSEWHEEL };
     int type;
 
     union
--- a/tuikit/container.py	Wed Jan 30 19:44:01 2013 +0100
+++ b/tuikit/container.py	Wed Jan 30 20:21:08 2013 +0100
@@ -206,6 +206,9 @@
             self.mousechild.emit('mousemove', child_ev)
             return True
 
+    def on_mousehover(self, ev):
+        self.cascade_mouse_event(ev)
+
     def on_mousewheel(self, ev):
         self.cascade_mouse_event(ev)
 
--- a/tuikit/widget.py	Wed Jan 30 19:44:01 2013 +0100
+++ b/tuikit/widget.py	Wed Jan 30 20:21:08 2013 +0100
@@ -73,6 +73,7 @@
             'mousedown', MouseEvent,
             'mouseup', MouseEvent,
             'mousemove', MouseEvent,
+            'mousehover', MouseEvent,
             'mousewheel', MouseEvent,
             'sizereq', Event,
             'spotmove', Event,