]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Make font size a property of Style
[libs/gltk.git] / source / dropdown.cpp
index b00438e54c40ef7bc21dfb887ecd1945229a72bb..d1a7bd7e1433490e364a8d7cf2ec356c809ce0a7 100644 (file)
@@ -1,15 +1,9 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007-2011  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <msp/gl/font.h>
 #include "dropdown.h"
 #include "list.h"
 #include "panel.h"
 #include "part.h"
+#include "root.h"
 #include "style.h"
 #include "text.h"
 
@@ -23,30 +17,45 @@ Dropdown::Dropdown():
 {
        add(list);
        list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
+       list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
+}
+
+void Dropdown::autosize()
+{
+       if(!style)
+               return;
+
+       Widget::autosize();
+       geom.w = max(geom.w, list.get_geometry().w);
+
+       if(const Part *text_part = style->get_part("text"))
+       {
+               const Sides &margin = text_part->get_margin();
+               const GL::Font *font = style->get_font();
+               float font_size = style->get_font_size();
+               unsigned line_height = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
+               geom.h = max(geom.h, line_height+margin.top+margin.bottom);
+       }
 }
 
 void Dropdown::append(const string &item)
 {
        list.append(item);
-       resize_list();
 }
 
 void Dropdown::insert(unsigned i, const string &v)
 {
        list.insert(i, v);
-       resize_list();
 }
 
 void Dropdown::remove(unsigned i)
 {
        list.remove(i);
-       resize_list();
 }
 
 void Dropdown::clear()
 {
        list.clear();
-       resize_list();
 }
 
 unsigned Dropdown::get_n_items() const
@@ -110,11 +119,40 @@ void Dropdown::on_style_change()
        resize_list();
 }
 
+void Dropdown::list_autosize_changed()
+{
+       resize_list();
+       signal_autosize_changed.emit();
+}
+
 void Dropdown::resize_list()
 {
-       list.autosize();
-       const Geometry &lgeom = list.get_geometry();
-       list.set_geometry(Geometry(0, -lgeom.h, max(geom.w, lgeom.w), lgeom.h));
+       list.autosize_all();
+       Geometry lgeom = list.get_geometry();
+       lgeom.x = 0;
+       lgeom.y = -lgeom.h;
+       lgeom.w = max(geom.w, lgeom.w);
+       int root_x = geom.x;
+       int root_y = geom.y;
+       for(Widget *p=parent; p; p=p->get_parent())
+       {
+               root_x += p->get_geometry().x;
+               root_y += p->get_geometry().y;
+               if(Root *root = dynamic_cast<Root *>(p))
+               {
+                       const Geometry &rgeom = root->get_geometry();
+                       if(lgeom.h*2>rgeom.h)
+                               lgeom.h = rgeom.h/2;
+                       if(root_y+lgeom.y<0)
+                               lgeom.y = -root_y;
+                       if(root_y+lgeom.y+lgeom.h>rgeom.h)
+                               lgeom.y = rgeom.h-lgeom.h-root_y;
+                       if(root_x+lgeom.x+lgeom.w>rgeom.w)
+                               lgeom.x = rgeom.w-lgeom.w-root_x;
+                       break;
+               }
+       }
+       list.set_geometry(lgeom);
 }
 
 void Dropdown::list_item_selected(unsigned index, const std::string &item)