]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/panel.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / panel.cpp
index 97c9ca4f3438d336cc77f1104dcad9e2d379ecf2..ba66c682e85decb9608c084caeaaa6b5763c0318 100644 (file)
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <algorithm>
-#include <msp/core/refptr.h>
+#include <msp/core/algorithm.h>
+#include <msp/core/maputils.h>
 #include "button.h"
+#include "column.h"
+#include "draghandle.h"
 #include "dropdown.h"
 #include "entry.h"
-#include "hslider.h"
+#include "grid.h"
+#include "image.h"
 #include "indicator.h"
 #include "label.h"
 #include "list.h"
 #include "panel.h"
 #include "part.h"
-#include "table.h"
+#include "progressbar.h"
+#include "row.h"
+#include "slider.h"
+#include "stack.h"
 #include "toggle.h"
-#include "vslider.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GLtk {
 
-Panel::Panel(const Resources &r):
-       Widget(r),
-       pointer_focus(0),
-       pointer_grab(0),
-       input_focus(0)
-{
-       update_style();
-}
-
-Panel::~Panel()
-{
-       while(!children.empty())
-               delete children.front();
-}
+TypeRegistry<Panel::Loader::AddChildType, Panel::Loader &> Panel::widget_registry;
+bool Panel::widget_registry_init_done = false;
 
-void Panel::add(Widget &wdg)
+Panel::Panel()
 {
-       set_parent(wdg, this);
-       children.push_back(&wdg);
+       input_type = INPUT_NAVIGATION;
 }
 
-void Panel::remove(Widget &wdg)
+Layout &Panel::get_or_create_layout()
 {
-       list<Widget *>::iterator i=find(children.begin(), children.end(), &wdg);
-       if(i==children.end())
-               throw InvalidState("That Widget is not in this Panel");
-
-       if(&wdg==pointer_focus)
-               set_pointer_focus(0, 0);
-       if(&wdg==input_focus)
-               set_input_focus(0);
+       if(!layout)
+       {
+               layout = make_unique<Layout>();
+               layout->set_container(*this);
+       }
 
-       set_parent(wdg, 0);
-       children.erase(i);
+       return *layout;
 }
 
-void Panel::raise(Widget &wdg)
+void Panel::autosize_special(const Part &part, Geometry &ageom) const
 {
-       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);
+       if(part.get_name()=="children" && layout)
+               layout->autosize(ageom);
 }
 
-void Panel::button_press(int x, int y, unsigned btn)
+void Panel::render_special(const Part &part, GL::Renderer &renderer) const
 {
-       if(pointer_grab>0)
-       {
-               const Geometry &cgeom=pointer_focus->get_geometry();
-               pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
-       }
-       else if(geom.is_inside_relative(x, y))
+       if(part.get_name()=="children")
        {
-               if(Widget *wdg=get_child_at(x, y))
-               {
-                       set_pointer_focus(wdg, btn);
-                       set_input_focus(wdg);
-
-                       const Geometry &cgeom=wdg->get_geometry();
-                       wdg->button_press(x-cgeom.x, y-cgeom.y, btn);
-               }
+               for(const unique_ptr<Child> &c: children)
+                       if(c->widget->is_visible())
+                               c->widget->render(renderer);
        }
 }
 
-void Panel::button_release(int x, int y, unsigned btn)
+bool Panel::navigate(Navigation nav)
 {
-       if(pointer_grab>0)
+       if(Container::navigate(nav))
+               return true;
+
+       if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT)
        {
-               const Geometry &cgeom=pointer_focus->get_geometry();
-               pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
+               int nav_x = (nav==NAV_RIGHT ? 1 : nav==NAV_LEFT ? -1 : 0);
+               int nav_y = (nav==NAV_UP ? 1 : nav==NAV_DOWN ? -1 : 0);
 
-               if(btn==pointer_grab)
-                       set_pointer_focus(get_child_at(x, y), 0);
+               int origin_x, origin_y, origin_dim;
+               if(input_focus)
+               {
+                       const Geometry &fgeom = input_focus->get_geometry();
+                       origin_x = fgeom.x+(nav_x*0.5+0.5)*fgeom.w;
+                       origin_y = fgeom.y+(nav_y*0.5+0.5)*fgeom.h;
+                       origin_dim = abs(nav_x)*fgeom.h+abs(nav_y)*fgeom.w;
+               }
+               else
+               {
+                       origin_x = geom.w*(0.5-nav_x*0.5);
+                       origin_y = geom.h*(0.5-nav_y*0.5);
+                       origin_dim = abs(nav_x)*geom.h+abs(nav_y)*geom.w;
+               }
+
+               if(pointer_grabbed && pointer_focus==input_focus)
+                       return false;
+
+               Widget *sibling = find_next_child(origin_x, origin_y, origin_dim, nav_x, nav_y);
+               if(!sibling && input_focus)
+               {
+                       const Geometry &fgeom = input_focus->get_geometry();
+                       origin_x -= fgeom.w*(nav_x*0.5);
+                       origin_y -= fgeom.h*(nav_y*0.5);
+                       sibling = find_next_child(origin_x, origin_y, origin_dim, nav_x, nav_y);
+               }
+
+               if(sibling)
+               {
+                       set_input_focus(sibling);
+                       if(Panel *panel = dynamic_cast<Panel *>(sibling))
+                               panel->navigate(nav);
+                       return true;
+               }
        }
-       else if(geom.is_inside_relative(x, y))
+       else if(nav==NAV_NEXT || nav==NAV_PREVIOUS)
        {
-               if(Widget *wdg=get_child_at(x, y))
+               auto i = find(nav_order, input_focus);
+
+               if(nav==NAV_NEXT)
+               {
+                       if(i!=nav_order.end())
+                               ++i;
+                       if(i==nav_order.end())
+                               i = nav_order.begin();
+               }
+               else
                {
-                       const Geometry &cgeom=wdg->get_geometry();
-                       wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
+                       if(i==nav_order.begin())
+                               i = nav_order.end();
+                       --i;
                }
+
+               set_input_focus(*i);
+
+               return true;
        }
+
+       return false;
 }
 
-void Panel::pointer_motion(int x, int y)
+Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int nav_x, int nav_y) const
 {
-       if(pointer_grab>0)
-       {
-               const Geometry &cgeom=pointer_focus->get_geometry();
-               pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
-       }
-       else if(geom.is_inside_relative(x, y))
+       Widget *sibling = nullptr;
+       int best_score = 0;
+       for(const unique_ptr<Child> &c: children)
        {
-               Widget *wdg=get_child_at(x, y);
-               set_pointer_focus(wdg, 0);
-               if(wdg)
+               if(c->widget==input_focus || !c->widget->is_focusable())
+                       continue;
+
+               const Geometry &cgeom = c->widget->get_geometry();
+               int dx = compute_delta(cgeom.x, cgeom.w, origin_x, origin_dim, nav_x);
+               int dy = compute_delta(cgeom.y, cgeom.h, origin_y, origin_dim, nav_y);
+
+               int score = -1;
+               if(nav_y && nav_y*dy>=0)
+                       score = nav_y*dy+abs(dx)*4;
+               else if(nav_x && nav_x*dx>=0)
+                       score = nav_x*dx+abs(dy)*4;
+
+               if(score>=0 && (!sibling || score<best_score))
                {
-                       const Geometry &cgeom=wdg->get_geometry();
-                       wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
+                       sibling = c->widget;
+                       best_score = score;
                }
        }
+
+       return sibling;
 }
 
-void Panel::pointer_leave()
+int Panel::compute_delta(int pos, int dim, int origin_pos, int origin_dim, int nav)
 {
-       set_pointer_focus(0, 0);
+       if(nav<0)
+               return pos+dim-origin_pos;
+       else if(nav>0)
+               return pos-origin_pos;
+       else if(pos+dim<origin_pos-origin_dim/2)
+               return pos+dim+origin_dim/2-origin_pos;
+       else if(pos>origin_pos+origin_dim/2)
+               return pos-origin_pos-origin_dim/2;
+       else
+               return 0;
 }
 
-void Panel::key_press(unsigned key, unsigned mod, wchar_t ch)
+void Panel::on_size_change()
 {
-       if(input_focus)
-               input_focus->key_press(key, mod, ch);
+       if(layout)
+               layout->update();
 }
 
-void Panel::key_release(unsigned key, unsigned mod)
+void Panel::on_child_added(Widget &wdg)
 {
-       if(input_focus)
-               input_focus->key_release(key, mod);
+       if(wdg.get_input_type()!=INPUT_NONE)
+               nav_order.push_back(&wdg);
+
+       if(layout)
+       {
+               layout->add_widget(wdg);
+               signal_autosize_changed.emit();
+       }
 }
 
-void Panel::focus_out()
+void Panel::on_child_removed(Widget &wdg)
 {
-       set_input_focus(0);
+       auto i = std::remove(nav_order.begin(), nav_order.end(), &wdg);
+       if(i!=nav_order.end())
+               nav_order.erase(i, nav_order.end());
+
+       if(layout)
+       {
+               layout->remove_widget(wdg);
+               signal_autosize_changed.emit();
+       }
 }
 
-void Panel::child_hidden(Widget &wdg)
+
+Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
+       DataFile::DerivedObjectLoader<Panel, Widget::Loader>(p),
+       wdg_map(m),
+       last_widget(nullptr)
 {
-       if(&wdg==pointer_focus)
-               set_pointer_focus(0, 0);
-       if(&wdg==input_focus)
-               set_input_focus(0);
+       if(!widget_registry_init_done)
+       {
+               widget_registry_init_done = true;
+               register_child_type<Button>("button");
+               register_child_type<DragHandle>("draghandle");
+               register_child_type<Dropdown>("dropdown");
+               register_child_type<Entry>("entry");
+               register_child_type<HSlider>("hslider");
+               register_child_type<Image>("image");
+               register_child_type<Indicator>("indicator");
+               register_child_type<Label>("label");
+               register_child_type<List>("list");
+               register_child_type<Panel>("panel");
+               register_child_type<ProgressBar>("progressbar");
+               register_child_type<Toggle>("toggle");
+               register_child_type<VSlider>("vslider");
+       }
+
+       add("column",    &Loader::arrangement<Column>);
+       add("constraint",&Loader::constraint);
+       add("expand",    &Loader::expand);
+       add("ghost",     &Loader::ghost);
+       add("gravity",   &Loader::gravity);
+       add("grid",      &Loader::grid);
+       add("layout",    &Loader::layout);
+       add("row",       &Loader::arrangement<Row>);
+       add("stack",     &Loader::arrangement<Stack>);
+       widget_registry.invoke_all(*this);
 }
 
-void Panel::grab_pointer(Widget &wdg)
+Widget &Panel::Loader::get_last_widget()
 {
-       if(pointer_grab==0 || pointer_focus==&wdg)
-               set_pointer_focus(&wdg, 255);
-       else
-               throw InvalidState("Pointer is already grabbed");
+       if(!last_widget)
+               throw logic_error("no widget loaded");
+
+       return *last_widget;
 }
 
-void Panel::ungrab_pointer(Widget &wdg)
+template<typename T>
+void Panel::Loader::arrangement()
 {
-       if(pointer_focus==&wdg)
-               set_pointer_focus(0, 0);
-       else if(pointer_grab>0)
-               throw Exception("Someone is trying to steal the pointer!");
+       T arr(obj.get_or_create_layout());
+       ArrangedLoader<T> ldr(*this, arr);
+       load_sub_with(ldr);
 }
 
-void Panel::grab_focus(Widget &wdg)
+void Panel::Loader::constraint(Layout::ConstraintType type, const string &n)
 {
-       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);
+       Widget &src = get_last_widget();
+       Widget &tgt = *get_item(wdg_map, n);
+       obj.get_or_create_layout().add_constraint(src, type, tgt);
 }
 
-void Panel::render_special(const Part &part) const
+void Panel::Loader::expand(bool h, bool v)
 {
-       if(part.get_name()=="children")
-       {
-               for(list<Widget *>::const_iterator i=children.begin(); i!=children.end(); ++i)
-                       if((*i)->is_visible())
-                               (*i)->render();
-       }
+       obj.get_or_create_layout().set_expand(get_last_widget(), h, v);
 }
 
-void Panel::set_pointer_focus(Widget *wdg, int grab)
+void Panel::Loader::ghost(bool g)
 {
-       if(grab>0 && !wdg)
-               throw InvalidParameterValue("Can't grab on null widget");
-
-       if(wdg!=pointer_focus)
-       {
-               if(pointer_focus)
-                       pointer_focus->pointer_leave();
-
-               pointer_focus=wdg;
-
-               if(pointer_focus)
-                       pointer_focus->pointer_enter();
-       }
-
-       pointer_grab=grab;
+       obj.get_or_create_layout().set_ghost(get_last_widget(), g);
 }
 
-void Panel::set_input_focus(Widget *wdg)
+void Panel::Loader::gravity(int h, int v)
 {
-       if(wdg!=input_focus)
-       {
-               if(input_focus)
-                       input_focus->focus_out();
-
-               input_focus=wdg;
-
-               if(input_focus)
-                       input_focus->focus_in();
-       }
+       obj.get_or_create_layout().set_gravity(get_last_widget(), h, v);
 }
 
-Widget *Panel::get_child_at(int x, int y)
+void Panel::Loader::grid(size_t cols)
 {
-       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;
-
-       return 0;
+       Grid grd(obj.get_or_create_layout(), cols);
+       ArrangedLoader<Grid> ldr(*this, grd);
+       load_sub_with(ldr);
 }
 
-
-Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
-       Widget::Loader(p),
-       pnl(p),
-       wdg_map(m)
+void Panel::Loader::layout()
 {
-       add("button",    &Loader::child<Button>);
-       add("dropdown",  &Loader::child<Dropdown>);
-       add("entry",     &Loader::child<Entry>);
-       add("hslider",   &Loader::child<HSlider>);
-       add("indicator", &Loader::child<Indicator>);
-       add("label",     &Loader::child<Label>);
-       add("list",      &Loader::child<List>);
-       add("panel",     &Loader::panel);
-       add("table",     &Loader::child<Table>);
-       add("toggle",    &Loader::child<Toggle>);
-       add("vslider",   &Loader::child<VSlider>);
+       Layout::Loader ldr(obj.get_or_create_layout(), wdg_map);
+       load_sub_with(ldr);
 }
 
-template<typename T>
-void Panel::Loader::child(const string &n)
+template<>
+void Panel::Loader::unnamed_child<Panel>()
 {
-       RefPtr<T> chl=new T(pnl.res);
-       load_sub(*chl);
-       pnl.add(*chl.get());
-       wdg_map[n]=chl.release();
+       unique_ptr<Panel> pnl = make_unique<Panel>();
+       load_sub(*pnl, wdg_map);
+       Widget *wdg = pnl.get();
+       obj.add(move(pnl));
+       last_widget = wdg;
 }
 
-void Panel::Loader::panel(const string &n)
+
+template<typename T>
+Panel::ArrangedLoader<T>::ArrangedLoader(Loader &ldr, T &arr):
+       arr_loader(arr)
 {
-       RefPtr<Panel> p=new Panel(pnl.res);
-       load_sub(*p, wdg_map);
-       pnl.add(*p.get());
-       wdg_map[n]=p.release();
+       add_auxiliary_loader(ldr);
+       add_auxiliary_loader(arr_loader);
 }
 
 } // namespace GLtk