]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Make sure that at least one line of text is always rendered
[libs/gltk.git] / source / dropdown.cpp
index a4816d48d593bb430e8b1aa764c35aac131b5744..b911df370de3579e89d3430a8882362346071e14 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -11,6 +11,7 @@ Distributed under the LGPL
 #include "panel.h"
 #include "part.h"
 #include "style.h"
+#include "text.h"
 
 using namespace std;
 
@@ -19,19 +20,16 @@ namespace GLtk {
 
 Dropdown::Dropdown(const Resources &r):
        Widget(r),
+       Container(r),
        list(r),
-       dropped(false),
-       list_active(false)
+       dropped(false)
 {
+       add(list);
        list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
 
        update_style();
 }
 
-Dropdown::~Dropdown()
-{
-}
-
 void Dropdown::append(const string &item)
 {
        list.append(item);
@@ -78,47 +76,21 @@ int Dropdown::get_selected_index() const
 
 void Dropdown::button_press(int x, int y, unsigned btn)
 {
-       if(list.get_geometry().is_inside(x, y))
-       {
-               const Geometry &lgeom=list.get_geometry();
-               list.button_press(x-lgeom.x, y-lgeom.y, btn);
-               list_active=true;
-       }
-       else if(dropped)
-       {
-               dropped=false;
-               state&=~ACTIVE;
-               parent->ungrab_pointer(*this);
-       }
-       else if(btn==1)
+       if(dropped)
        {
-               dropped=true;
-               state|=ACTIVE;
-
-               if(parent)
+               Container::button_press(x, y, btn);
+               if(!click_focus)
                {
-                       parent->raise(*this);
-                       parent->grab_pointer(*this);
+                       dropped = false;
+                       state &= ~ACTIVE;
+                       signal_ungrab_pointer.emit();
                }
        }
-}
-
-void Dropdown::button_release(int x, int y, unsigned btn)
-{
-       if(list_active)
-       {
-               const Geometry &lgeom=list.get_geometry();
-               list.button_release(x-lgeom.x, y-lgeom.y, btn);
-               list_active=false;
-       }
-}
-
-void Dropdown::pointer_motion(int x, int y)
-{
-       if(list_active)
+       else if(btn==1)
        {
-               const Geometry &lgeom=list.get_geometry();
-               list.pointer_motion(x-lgeom.x, y-lgeom.y);
+               dropped = true;
+               state |= ACTIVE;
+               signal_grab_pointer.emit();
        }
 }
 
@@ -127,7 +99,7 @@ void Dropdown::render_special(const Part &part) const
        if(part.get_name()=="text")
        {
                if(list.get_selected_index()>=0)
-                       render_text(part, list.get_selected());
+                       Text(*style, list.get_selected()).render(part, geom);
        }
        else if(part.get_name()=="list" && dropped)
                list.render();
@@ -141,15 +113,15 @@ void Dropdown::on_geometry_change()
 void Dropdown::resize_list()
 {
        // XXX This is a hack.
-       unsigned n_items=list.get_n_items();
-       const Style &stl=list.get_style();
-       const GL::Font &font=*stl.get_font();
-       unsigned h=min(max(n_items, 1U), 20U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
+       unsigned n_items = list.get_n_items();
+       const Style &stl = list.get_style();
+       const GL::Font &font = *stl.get_font();
+       unsigned h = min(max(n_items, 1U), 10U)*static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
        for(std::list<Part>::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i)
                if(i->get_name()=="items")
                {
-                       const Sides &margin=i->get_margin();
-                       h+=margin.top+margin.bottom;
+                       const Sides &margin = i->get_margin();
+                       h += margin.top+margin.bottom;
                }
        list.set_geometry(Geometry(0, -h, geom.w, h));
 }
@@ -158,11 +130,9 @@ void Dropdown::list_item_selected(unsigned index, const std::string &item)
 {
        if(dropped)
        {
-               list_active=false;
-               dropped=false;
-               state&=~ACTIVE;
-               if(parent)
-                       parent->ungrab_pointer(*this);
+               dropped = false;
+               state &= ~ACTIVE;
+               signal_ungrab_pointer.emit();
        }
 
        signal_item_selected.emit(index, item);
@@ -177,7 +147,7 @@ Dropdown::Loader::Loader(Dropdown &d):
 
 void Dropdown::Loader::item(const string &str)
 {
-       static_cast<Dropdown &>(wdg).append(str);
+       dynamic_cast<Dropdown &>(wdg).append(str);
 }
 
 } // namespace GLtk