]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add Widget::set_focus
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Aug 2008 12:35:31 +0000 (12:35 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Aug 2008 12:35:31 +0000 (12:35 +0000)
Add Entry::signal_enter
Make Panel throw on an attempt to handle a widget which is not in it

source/entry.cpp
source/entry.h
source/panel.cpp
source/panel.h
source/widget.cpp
source/widget.h

index 7f50ed038fbdd6cc4a758adcc0973d84f550b726..523fea5cc945f8a6bf6b321990c74ff449ee63d1 100644 (file)
@@ -52,6 +52,8 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
                if(edit_pos>0)
                        text.erase(--edit_pos, 1);
        }
+       else if(key==Input::KEY_ENTER)
+               signal_enter.emit();
        else if(ch>=' ')
        {
                text.insert(edit_pos, Codecs::encode<Codecs::Utf8>(Codecs::ustring(1, ch)));
index e5f3fd88be639bb22d083b93822cdb5109de23ec..29fec417ec966bb0931fbc53ea5627877954c409 100644 (file)
@@ -35,6 +35,8 @@ private:
        unsigned edit_pos;
 
 public:
+       sigc::signal<void> signal_enter;
+
        Entry(const Resources &, const std::string & =std::string());
 
        void set_text(const std::string &);
index ac277a98ae1bfaa4b4823fd0ffe83401f2d73eb4..3123db41bccd2dc6036a7fd89a1facfdea593e06 100644 (file)
@@ -48,27 +48,27 @@ void Panel::add(Widget &wdg)
 
 void Panel::remove(Widget &wdg)
 {
-       ChildSeq::iterator i=find(children.begin(), children.end(), &wdg);
-       if(i!=children.end())
-       {
-               if(&wdg==pointer_focus)
-                       set_pointer_focus(0, 0);
-               if(&wdg==input_focus)
-                       set_input_focus(0);
+       list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
+       if(i==children.end())
+               throw InvalidState("That Widget is not in this Panel");
 
-               set_parent(wdg, 0);
-               children.erase(i);
-       }
+       if(&wdg==pointer_focus)
+               set_pointer_focus(0, 0);
+       if(&wdg==input_focus)
+               set_input_focus(0);
+
+       set_parent(wdg, 0);
+       children.erase(i);
 }
 
 void Panel::raise(Widget &wdg)
 {
-       ChildSeq::iterator i=find(children.begin(), children.end(), &wdg);
-       if(i!=children.end())
-       {
-               children.erase(i);
-               children.push_back(&wdg);
-       }
+       list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
+       if(i==children.end())
+               throw InvalidState("That Widget is not in this Panel");
+
+       children.erase(i);
+       children.push_back(&wdg);
 }
 
 void Panel::button_press(int x, int y, unsigned btn)
@@ -174,11 +174,22 @@ void Panel::ungrab_pointer(Widget &wdg)
                throw Exception("Someone is trying to steal the pointer!");
 }
 
+void Panel::grab_focus(Widget &wdg)
+{
+       list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
+       if(i==children.end())
+               throw InvalidState("That Widget is not in this Panel");
+       
+       set_input_focus(&wdg);
+       if(parent)
+               parent->grab_focus(*this);
+}
+
 void Panel::render_special(const Part &part) const
 {
        if(part.get_name()=="children")
        {
-               for(ChildSeq::const_iterator i=children.begin(); i!=children.end(); ++i)
+               for(list<Widget *>::const_iterator i=children.begin(); i!=children.end(); ++i)
                        if((*i)->is_visible())
                                (*i)->render();
        }
@@ -219,7 +230,7 @@ void Panel::set_input_focus(Widget *wdg)
 
 Widget *Panel::get_child_at(int x, int y)
 {
-       for(ChildSeq::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
+       for(list<Widget *>::reverse_iterator i=children.rbegin(); i!=children.rend(); ++i)
                if((*i)->is_visible() && (*i)->get_geometry().is_inside(x, y))
                        return *i;
 
index 529354ddb92959cce47bea069a74a0efe4036c06..397d71fc3974adac96bedec9763a32e283141d24 100644 (file)
@@ -36,9 +36,7 @@ public:
        };
 
 private:
-       typedef std::list<Widget *> ChildSeq;
-
-       ChildSeq children;
+       std::list<Widget *> children;
        Widget *pointer_focus;
        unsigned pointer_grab;
        Widget *input_focus;
@@ -52,6 +50,7 @@ public:
        void add(Widget &);
        void remove(Widget &);
        void raise(Widget &);
+       void set_focus(Widget &);
 
        virtual void button_press(int, int, unsigned);
        virtual void button_release(int, int, unsigned);
@@ -61,9 +60,11 @@ public:
        virtual void key_release(unsigned, unsigned);
        virtual void focus_out();
 
+       // These functions are not intended to be called from outside GLtk
        void child_hidden(Widget &);
        void grab_pointer(Widget &);
        void ungrab_pointer(Widget &);
+       void grab_focus(Widget &);
 private:
        virtual const char *get_class() const { return "panel"; }
        virtual void render_special(const Part &) const;
index efb6155ef4a51d8fb4e69bde95323ea4e10c250d..48b647b71850b464bf8da56f102170d64bc8cf6e 100644 (file)
@@ -69,6 +69,16 @@ void Widget::set_visible(bool v)
                parent->child_hidden(*this);
 }
 
+void Widget::set_focus()
+{
+       if(!parent)
+               throw InvalidState("No parent");
+       if(!visible)
+               throw InvalidState("Can't set focus on invisible widget");
+
+       parent->grab_focus(*this);
+}
+
 void Widget::render() const
 {
        if(!style)
index 7fdc9066ec7d395c75fe437f97e049dad267730c..86638149c8640bdf57b95715cb5cab4b06fc6e25 100644 (file)
@@ -64,6 +64,7 @@ public:
        void set_style(const std::string &);
 
        void set_visible(bool);
+       void set_focus();
 
        const Geometry &get_geometry() const { return geom; }
        bool is_visible() const { return visible; }