--- /dev/null
+#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
--- /dev/null
+#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
+
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;
-#ifndef LABEL_H_
-#define LABEL_H_
+#ifndef MSP_GLTK_LABEL_H_
+#define MSP_GLTK_LABEL_H_
#include "widget.h"
Panel::Panel(const Resources &r):
Widget(r),
pointer_focus(0),
- pointer_grab(0)
+ pointer_grab(0),
+ input_focus(0)
{
update_style();
}
{
(*i)->button_press(x-geom.x, y-geom.y, btn);
pointer_grab=btn;
+ set_input_focus(*i);
}
}
}
}
}
+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);
}
}
+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
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
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;