]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add key events
authorMikko Rasa <tdb@tdb.fi>
Sun, 15 Jul 2007 10:31:16 +0000 (10:31 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 15 Jul 2007 10:31:16 +0000 (10:31 +0000)
Add Entry widget
Fix HSlider clicks

source/entry.cpp [new file with mode: 0644]
source/entry.h [new file with mode: 0644]
source/hslider.cpp
source/label.h
source/panel.cpp
source/panel.h
source/widget.h

diff --git a/source/entry.cpp b/source/entry.cpp
new file mode 100644 (file)
index 0000000..6daa8ef
--- /dev/null
@@ -0,0 +1,98 @@
+#include <SDL/SDL_keysym.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/transform.h>
+#include "entry.h"
+#include "part.h"
+#include "style.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GLtk {
+
+Entry::Entry(const Resources &r, const string &t):
+       Widget(r),
+       text(t),
+       edit_pos(0),
+       active(false)
+{
+}
+
+void Entry::set_text(const string &t)
+{
+       text=t;
+       if(edit_pos>text.size())
+               edit_pos=text.size();
+}
+
+void Entry::key_press(unsigned key, unsigned, wchar_t ch)
+{
+       if(key==SDLK_LEFT)
+       {
+               if(edit_pos>0)
+                       --edit_pos;
+       }
+       else if(key==SDLK_RIGHT)
+       {
+               if(edit_pos<text.size())
+                       ++edit_pos;
+       }
+       else if(key==SDLK_BACKSPACE)
+       {
+               if(edit_pos>0)
+                       text.erase(--edit_pos, 1);
+       }
+       else
+       {
+               text+=ch;
+       }
+}
+
+void Entry::focus_in()
+{
+       active=true;
+}
+
+void Entry::focus_out()
+{
+       active=false;
+}
+
+void Entry::render_part(const Part &part) const
+{
+       if(part.get_name()=="text")
+       {
+               const GL::Font *const font=style->get_font();
+
+               const float font_size=font->get_default_size();
+               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+
+               GL::push_matrix();
+
+               part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
+               GL::scale_uniform(font_size);
+
+               const Color &color=style->get_font_color();
+               glColor3f(color.r, color.g, color.b);
+               if(active)
+               {
+                       font->draw_string(text.substr(0, edit_pos));
+                       glBegin(GL_LINES);
+                       glVertex2f(0, 0);
+                       glVertex2f(0, 1);
+                       glEnd();
+                       font->draw_string(text.substr(edit_pos));
+               }
+               else
+                       font->draw_string(text);
+               glColor3f(1, 1, 1);
+
+               GL::pop_matrix();
+               render_text(part, text);
+       }
+       else
+               Widget::render_part(part);
+}
+
+} // namespace GLtk
+} // namespace Msp
diff --git a/source/entry.h b/source/entry.h
new file mode 100644 (file)
index 0000000..b09ecb1
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef MSP_GLTK_ENTRY_H_
+#define MSP_GLTK_ENTRY_H_
+
+#include "widget.h"
+
+namespace Msp {
+namespace GLtk {
+
+class Entry: public Widget
+{
+public:
+       Entry(const Resources &, const std::string & =std::string());
+       void set_text(const std::string &);
+       void key_press(unsigned, unsigned, wchar_t);
+       void focus_in();
+       void focus_out();
+private:
+       std::string text;
+       unsigned edit_pos;
+       bool active;
+
+       const char *get_class() const { return "entry"; }
+       void render_part(const Part &) const;
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif
+
index dda8b43168643a74825480c72e3017891adce8fd..8a9cbb86fdb4ef0615119cb694e932ce5bfb7ba7 100644 (file)
@@ -20,7 +20,7 @@ void HSlider::button_press(int x, int y, unsigned btn)
        if(geom.is_inside(x, y))
        {
                int sw=get_slider_width();
-               int sx=static_cast<int>((geom.w-sw)*(value-min)/(max-min));
+               int sx=geom.x+static_cast<int>((geom.w-sw)*(value-min)/(max-min));
                if(btn==1 && x>=sx && x<sx+sw)
                {
                        dragging=true;
index 0ab20290c216f30943096c692b0a00ac0261a134..3de71b764c3db95d41341a7f630cf1eac0faf5da 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef LABEL_H_
-#define LABEL_H_
+#ifndef MSP_GLTK_LABEL_H_
+#define MSP_GLTK_LABEL_H_
 
 #include "widget.h"
 
index f6aad86a856d90aa0440ebb1f0634544a2b3f51b..d3e8df0c374c0f190e4da38ffb50617a588613a4 100644 (file)
@@ -7,7 +7,8 @@ namespace GLtk {
 Panel::Panel(const Resources &r):
        Widget(r),
        pointer_focus(0),
-       pointer_grab(0)
+       pointer_grab(0),
+       input_focus(0)
 {
        update_style();
 }
@@ -29,6 +30,7 @@ void Panel::button_press(int x, int y, unsigned btn)
                        {
                                (*i)->button_press(x-geom.x, y-geom.y, btn);
                                pointer_grab=btn;
+                               set_input_focus(*i);
                        }
        }
 }
@@ -79,6 +81,23 @@ void Panel::pointer_motion(int x, int y)
        }
 }
 
+void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
+{
+       if(input_focus)
+               input_focus->key_press(key, mod, ch);
+}
+
+void Panel::key_release(unsigned key, unsigned mod)
+{
+       if(input_focus)
+               input_focus->key_release(key, mod);
+}
+
+void Panel::focus_out()
+{
+       set_input_focus(0);
+}
+
 void Panel::add(Widget &wdg)
 {
        children.push_back(&wdg);
@@ -109,5 +128,19 @@ void Panel::set_pointer_focus(Widget *wdg)
        }
 }
 
+void Panel::set_input_focus(Widget *wdg)
+{
+       if(wdg!=input_focus)
+       {
+               if(input_focus)
+                       input_focus->focus_out();
+
+               input_focus=wdg;
+
+               if(input_focus)
+                       input_focus->focus_in();
+       }
+}
+
 } // namespace GLtk
 } // namespace Msp
index 31be7e57c1afb530c0ef58600d69c9e31110c12b..2fa7d8f88944480757d1676eeb8ac18a514503d5 100644 (file)
@@ -16,18 +16,23 @@ public:
        void button_press(int, int, unsigned);
        void button_release(int, int, unsigned);
        void pointer_motion(int, int);
+       void key_press(unsigned, unsigned, wchar_t);
+       void key_release(unsigned, unsigned);
+       void focus_out();
 private:
        typedef std::list<Widget *> ChildSeq;
 
        ChildSeq children;
        Widget *pointer_focus;
        unsigned pointer_grab;
+       Widget *input_focus;
 
        Panel(const Panel &);
        Panel &operator=(const Panel &);
        const char *get_class() const { return "panel"; }
        void render_part(const Part &) const;
        void set_pointer_focus(Widget *);
+       void set_input_focus(Widget *);
 };
 
 } // namespace GLtk
index 322fbfa8a53f0b052ce94c45ab5fed847c7a6837..47bc41096de58db1b43ada855d63875c83103ea1 100644 (file)
@@ -27,6 +27,10 @@ public:
        virtual void pointer_motion(int, int) { }
        virtual void pointer_enter() { }
        virtual void pointer_leave() { }
+       virtual void key_press(unsigned, unsigned, wchar_t) { }
+       virtual void key_release(unsigned, unsigned) { }
+       virtual void focus_in() { }
+       virtual void focus_out() { }
 protected:
        const Resources &res;
        Geometry geom;