]> git.tdb.fi Git - libs/gltk.git/blob - source/systemkeyboardinput.cpp
Don't pass events to an invisible root widget
[libs/gltk.git] / source / systemkeyboardinput.cpp
1 #include <msp/input/keys.h>
2 #include "root.h"
3 #include "systemkeyboardinput.h"
4
5 namespace Msp {
6 namespace GLtk {
7
8 SystemKeyboardInput::SystemKeyboardInput(Root &r, Input::Keyboard &k):
9         InputMethod(r),
10         keyboard(k)
11 {
12         keyboard.signal_button_press.connect(sigc::mem_fun(this, &SystemKeyboardInput::key_press));
13         keyboard.signal_button_release.connect(sigc::mem_fun(this, &SystemKeyboardInput::key_release));
14         keyboard.signal_character.connect(sigc::mem_fun(this, &SystemKeyboardInput::character));
15 }
16
17 bool SystemKeyboardInput::key_press(unsigned key)
18 {
19         if(!root.is_visible())
20                 return false;
21
22         // TODO modifiers
23         if(root.key_press(key, 0))
24                 return true;
25         
26         switch(key)
27         {
28         case Input::KEY_LEFT: return root.navigate(NAV_LEFT);
29         case Input::KEY_RIGHT: return root.navigate(NAV_RIGHT);
30         case Input::KEY_UP: return root.navigate(NAV_UP);
31         case Input::KEY_DOWN: return root.navigate(NAV_DOWN);
32         case Input::KEY_TAB: return root.navigate(NAV_NEXT);
33         case Input::KEY_SPACE: return root.navigate(NAV_ACTIVATE);
34         case Input::KEY_ENTER: return root.navigate(NAV_ACCEPT);
35         case Input::KEY_ESC: return root.navigate(NAV_CANCEL);
36         }
37
38         return false;
39 }
40
41 bool SystemKeyboardInput::key_release(unsigned key)
42 {
43         if(root.is_visible())
44                 return root.key_release(key, 0);
45         else
46                 return false;
47 }
48
49 bool SystemKeyboardInput::character(StringCodec::unichar ch)
50 {
51         if(root.is_visible())
52                 return root.character(ch);
53         else
54                 return false;
55 }
56
57 } // namespace GLtk
58 } // namespace Msp