]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add an input method subsystem
authorMikko Rasa <tdb@tdb.fi>
Tue, 30 Aug 2016 13:26:57 +0000 (16:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 30 Aug 2016 13:36:39 +0000 (16:36 +0300)
It's necessary for implementing keyboard navigation and virtual keyboards.

15 files changed:
source/dropdown.cpp
source/entry.cpp
source/image.cpp
source/indicator.cpp
source/indicator.h
source/inputmethod.cpp [new file with mode: 0644]
source/inputmethod.h [new file with mode: 0644]
source/label.cpp
source/panel.cpp
source/root.cpp
source/root.h
source/systemkeyboardinput.cpp [new file with mode: 0644]
source/systemkeyboardinput.h [new file with mode: 0644]
source/widget.cpp
source/widget.h

index 34ca1781153f946fddaaf69f82e993643b2864f1..8cc6187f2243a552b0943dde919dff6e6c0d18cb 100644 (file)
@@ -25,6 +25,9 @@ Dropdown::Dropdown(ListData &d):
 
 void Dropdown::init()
 {
+       // Necessary to have the parent container raise the dropdown on click
+       input_type = INPUT_TEXT;
+
        dropped = false;
 
        add(list);
index 43dad5e7de46dbcb69569427c083352eee0aabac..16954a70f5774858b34b86622de200abe2a4376b 100644 (file)
@@ -25,6 +25,7 @@ Entry::Entry(const string &t):
        slider(0),
        got_key_press(false)
 {
+       input_type = INPUT_TEXT;
        set_text(t);
 }
 
index f1180c39e1664491fd239560d59f251b8194074a..383f906911993d2a83d8d434ec791f8c0e90d303 100644 (file)
@@ -12,7 +12,6 @@ Image::Image(const GL::Texture2D *i):
        image(i),
        keep_aspect(true)
 {
-       focusable = false;
 }
 
 void Image::autosize_special(const Part &part, Geometry &ageom) const
index fba81665c2092a66cc057f5ff0f8882865b1f0ed..f9f6ed51089640813df1281257e4b6fd69d46986 100644 (file)
@@ -3,11 +3,6 @@
 namespace Msp {
 namespace GLtk {
 
-Indicator::Indicator()
-{
-       focusable = false;
-}
-
 void Indicator::set_active(bool a)
 {
        set_state(ACTIVE, (a ? ACTIVE : NORMAL));
index cf20e3138f29618ff0a98eca5d7637324de70d16..90c920c574100ea190166180ea7a3858dfa0888e 100644 (file)
@@ -12,8 +12,6 @@ An Indicator visualizes a boolean state.  It can be either active or inactive.
 class Indicator: public Widget
 {
 public:
-       Indicator();
-
        virtual const char *get_class() const { return "indicator"; }
 
        void set_active(bool);
diff --git a/source/inputmethod.cpp b/source/inputmethod.cpp
new file mode 100644 (file)
index 0000000..3062a27
--- /dev/null
@@ -0,0 +1,11 @@
+#include "inputmethod.h"
+
+namespace Msp {
+namespace GLtk {
+
+InputMethod::InputMethod(Root &r):
+       root(r)
+{ }
+
+} // namespace GLtk
+} // namespace Msp
diff --git a/source/inputmethod.h b/source/inputmethod.h
new file mode 100644 (file)
index 0000000..31cbaf3
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef INPUTMETHOD_H_
+#define INPUTMETHOD_H_
+
+namespace Msp {
+namespace GLtk {
+
+class Root;
+
+enum InputType
+{
+       INPUT_NONE,
+       INPUT_TEXT
+};
+
+class InputMethod
+{
+protected:
+       Root &root;
+
+       InputMethod(Root &);
+public:
+       virtual ~InputMethod() { }
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif
index c59e71851d3ceaee9b91ce71203087b79220e35d..263b2a4229d23c7c80747e37f3a66f880f2f5140 100644 (file)
@@ -9,7 +9,6 @@ namespace GLtk {
 
 Label::Label(const string &t)
 {
-       focusable = false;
        set_text(t);
 }
 
index f1eef110848abed72e114742ea2ff24b6e2ead11..44c41eac98fd6e62d0d69e2ace47c5a52e6910af 100644 (file)
@@ -26,7 +26,9 @@ namespace GLtk {
 
 Panel::Panel():
        layout(0)
-{ }
+{
+       input_type = INPUT_TEXT;
+}
 
 Panel::~Panel()
 {
index c54ab7b00682b78bd39041eb09565bc46cd3254d..b4c16cfe8bf7973ded8a5f2b387b23c189f80bbd 100644 (file)
@@ -8,6 +8,7 @@
 #include "label.h"
 #include "style.h"
 #include "root.h"
+#include "systemkeyboardinput.h"
 
 namespace Msp {
 namespace GLtk {
@@ -15,6 +16,7 @@ namespace GLtk {
 Root::Root(const Resources &r, Graphics::Window &window):
        resources(r),
        keyboard(new Input::Keyboard(window)),
+       input_method(0),
        mouse(new Input::Mouse(window)),
        touchscreen(0),
        own_input(true)
@@ -25,6 +27,7 @@ Root::Root(const Resources &r, Graphics::Window &window):
 Root::Root(const Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
        resources(r),
        keyboard(k),
+       input_method(0),
        mouse(m),
        touchscreen(t),
        own_input(false)
@@ -64,12 +67,8 @@ void Root::init(Graphics::Window *window)
                mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
        }
 
-       if(keyboard)
-       {
-               keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
-               keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
-               keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
-       }
+       if(keyboard && !input_method)
+               input_method = new SystemKeyboardInput(*this, *keyboard);
 
        if(touchscreen)
        {
@@ -82,6 +81,7 @@ void Root::init(Graphics::Window *window)
 Root::~Root()
 {
        delete shprog;
+       delete input_method;
        if(own_input)
        {
                delete keyboard;
@@ -206,31 +206,6 @@ bool Root::axis_motion_event(unsigned, float, float)
        return false;
 }
 
-bool Root::key_press_event(unsigned key)
-{
-       // XXX Modifiers
-       if(visible)
-               return key_press(key, 0);
-
-       return false;
-}
-
-bool Root::key_release_event(unsigned key)
-{
-       if(visible)
-               return key_release(key, 0);
-
-       return false;
-}
-
-bool Root::character_event(StringCodec::unichar ch)
-{
-       if(visible)
-               return character(ch);
-
-       return false;
-}
-
 bool Root::touch_press_event(unsigned finger)
 {
        if(visible)
index 5d721c0c2370f5e1ac68d68ef1aee8e987bdbde0..b0751b688985265d76566894c5dab694863ef5c5 100644 (file)
@@ -28,6 +28,7 @@ public:
 private:
        const Resources &resources;
        Input::Keyboard *keyboard;
+       InputMethod *input_method;
        Input::Mouse *mouse;
        Input::Touchscreen *touchscreen;
        bool own_input;
@@ -65,9 +66,6 @@ private:
        bool button_press_event(unsigned);
        bool button_release_event(unsigned);
        bool axis_motion_event(unsigned, float, float);
-       bool key_press_event(unsigned);
-       bool key_release_event(unsigned);
-       bool character_event(StringCodec::unichar);
        bool touch_press_event(unsigned);
        bool touch_release_event(unsigned);
        bool touch_motion_event(unsigned, float, float);
diff --git a/source/systemkeyboardinput.cpp b/source/systemkeyboardinput.cpp
new file mode 100644 (file)
index 0000000..cedddd1
--- /dev/null
@@ -0,0 +1,37 @@
+#include <msp/input/keys.h>
+#include "root.h"
+#include "systemkeyboardinput.h"
+
+namespace Msp {
+namespace GLtk {
+
+SystemKeyboardInput::SystemKeyboardInput(Root &r, Input::Keyboard &k):
+       InputMethod(r),
+       keyboard(k)
+{
+       keyboard.signal_button_press.connect(sigc::mem_fun(this, &SystemKeyboardInput::key_press));
+       keyboard.signal_button_release.connect(sigc::mem_fun(this, &SystemKeyboardInput::key_release));
+       keyboard.signal_character.connect(sigc::mem_fun(this, &SystemKeyboardInput::character));
+}
+
+bool SystemKeyboardInput::key_press(unsigned key)
+{
+       // TODO modifiers
+       if(root.key_press(key, 0))
+               return true;
+       
+       return false;
+}
+
+bool SystemKeyboardInput::key_release(unsigned key)
+{
+       return root.key_release(key, 0);
+}
+
+bool SystemKeyboardInput::character(StringCodec::unichar ch)
+{
+       return root.character(ch);
+}
+
+} // namespace GLtk
+} // namespace Msp
diff --git a/source/systemkeyboardinput.h b/source/systemkeyboardinput.h
new file mode 100644 (file)
index 0000000..ae988ca
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef MSP_GLTK_SYSTEMKEYBOARDINPUT_H_
+#define MSP_GLTK_SYSTEMKEYBOARDINPUT_H_
+
+#include <msp/input/keyboard.h>
+#include "inputmethod.h"
+
+namespace Msp {
+namespace GLtk {
+
+class SystemKeyboardInput: public InputMethod, public sigc::trackable
+{
+private:
+       Input::Keyboard &keyboard;
+
+public:
+       SystemKeyboardInput(Root &, Input::Keyboard &);
+
+private:
+       bool key_press(unsigned);
+       bool key_release(unsigned);
+       bool character(StringCodec::unichar);
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif
index 38f8e92a67d4db6b97bef640e539b5c367ebbc73..5fc208268d06e117d7b181b6542154fbf47eb450 100644 (file)
@@ -14,7 +14,7 @@ Widget::Widget():
        style(0),
        state(NORMAL),
        visible(true),
-       focusable(true),
+       input_type(INPUT_NONE),
        parent(0)
 { }
 
@@ -162,7 +162,7 @@ void Widget::set_visible(bool v)
 
 void Widget::set_focusable(bool f)
 {
-       focusable = f;
+       input_type = (f ? INPUT_TEXT : INPUT_NONE);
 }
 
 void Widget::set_focus()
index f852ec354f4874c75bf91945090ab4330c9bc342..f1a83f78a5532c0e355306dcd826369d31caa1e3 100644 (file)
@@ -5,6 +5,7 @@
 #include <msp/datafile/objectloader.h>
 #include <msp/gl/renderer.h>
 #include "geometry.h"
+#include "inputmethod.h"
 #include "partcache.h"
 #include "state.h"
 
@@ -46,7 +47,7 @@ protected:
        const Style *style;
        State state;
        bool visible;
-       bool focusable;
+       InputType input_type;
        Container *parent;
        std::string tooltip;
        PartCache part_cache;
@@ -107,12 +108,15 @@ public:
 
        void set_visible(bool);
        bool is_visible() const { return visible; }
-       void set_focusable(bool);
-       bool is_focusable() const { return focusable; }
+       InputType get_input_type() const { return input_type; }
+       bool is_focusable() const { return visible && input_type!=INPUT_NONE; }
        void set_focus();
        void set_enabled(bool);
        bool is_enabled() const { return !(state&DISABLED); }
 
+       // Deprecated
+       void set_focusable(bool);
+
 protected:
        void set_state(State s) { set_state(s, s); }
        void clear_state(State s) { set_state(s, NORMAL); }