From: Mikko Rasa Date: Sun, 15 Jul 2007 10:31:16 +0000 (+0000) Subject: Add key events X-Git-Tag: 0.9~34 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=68c4aa0eaaade8b163cf9b3a96aa640ea16b1def;p=libs%2Fgltk.git Add key events Add Entry widget Fix HSlider clicks --- diff --git a/source/entry.cpp b/source/entry.cpp new file mode 100644 index 0000000..6daa8ef --- /dev/null +++ b/source/entry.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#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_pos0) + 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(font->get_string_width(text)*font_size); + + GL::push_matrix(); + + part.get_alignment().apply(geom, text_w, static_cast(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 index 0000000..b09ecb1 --- /dev/null +++ b/source/entry.h @@ -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 + diff --git a/source/hslider.cpp b/source/hslider.cpp index dda8b43..8a9cbb8 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -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((geom.w-sw)*(value-min)/(max-min)); + int sx=geom.x+static_cast((geom.w-sw)*(value-min)/(max-min)); if(btn==1 && x>=sx && xbutton_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 diff --git a/source/panel.h b/source/panel.h index 31be7e5..2fa7d8f 100644 --- a/source/panel.h +++ b/source/panel.h @@ -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 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 diff --git a/source/widget.h b/source/widget.h index 322fbfa..47bc410 100644 --- a/source/widget.h +++ b/source/widget.h @@ -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;