]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/layout.cpp
Combine common parts of Column and Row into LinearArrangement
[libs/gltk.git] / source / layout.cpp
index 66eb66b8b98c3a1e817a5dcbca9a11116aea3829..917d47a7cbc9e2f5b0c2184f0e4fd7bfe6cea85f 100644 (file)
@@ -1,5 +1,7 @@
 #include <algorithm>
 #include <limits>
+#include <msp/strings/format.h>
+#include "arrangement.h"
 #include "container.h"
 #include "layout.h"
 #include "widget.h"
@@ -88,7 +90,10 @@ Layout::Layout():
        margin(8),
        row_spacing(5),
        col_spacing(4)
-{ }
+{
+       n_slack_constraints[0] = 0;
+       n_slack_constraints[1] = 0;
+}
 
 Layout::~Layout()
 {
@@ -133,16 +138,45 @@ void Layout::set_column_spacing(unsigned s)
                update();
 }
 
+void Layout::push_arrangement(Arrangement &arr)
+{
+       arrangement_stack.push_back(&arr);
+}
+
+Arrangement *Layout::get_arrangement() const
+{
+       if(arrangement_stack.empty())
+               return 0;
+       else
+               return arrangement_stack.back();
+}
+
+void Layout::pop_arrangement(Arrangement &arr)
+{
+       list<Arrangement *>::iterator begin = find(arrangement_stack.begin(), arrangement_stack.end(), &arr);
+       if(begin==arrangement_stack.end())
+               return;
+
+       while(1)
+       {
+               Arrangement *top = arrangement_stack.back();
+               arrangement_stack.pop_back();
+               if(!arrangement_stack.empty())
+                       arrangement_stack.back()->arrange(*top);
+               if(top==&arr)
+                       break;
+       }
+}
+
 void Layout::add_widget(Widget &wdg)
 {
        if(!container)
                throw logic_error("!container");
 
-       Slot *slot = create_slot(wdg);
-       for(list<Constraint>::iterator i=slot->constraints.begin(); i!=slot->constraints.end(); ++i)
-               i->target.constraints.push_back(Constraint(complement(i->type), *slot));
-       slots.push_back(slot);
+       slots.push_back(new Slot(*this, wdg));
        update_slot_indices();
+       if(!arrangement_stack.empty())
+               arrangement_stack.back()->arrange(wdg);
        if(container)
                update();
 }
@@ -167,20 +201,12 @@ void Layout::remove_widget(Widget &wdg)
                        delete *i;
                        slots.erase(i);
 
-                       unsigned n = 0;
-                       for(i=slots.begin(); i!=slots.end(); ++i, ++n)
-                               (*i)->index = n;
-
+                       update_slot_indices();
                        update();
                        return;
                }
 }
 
-Layout::Slot *Layout::create_slot(Widget &wdg)
-{
-       return new Slot(*this, wdg);
-}
-
 void Layout::update_slot_indices()
 {
        n_active_slots = 0;
@@ -191,6 +217,16 @@ void Layout::update_slot_indices()
                else
                        (*i)->index = -1;
        }
+
+       n_slack_constraints[0] = 0;
+       n_slack_constraints[1] = 0;
+       for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
+               if((*i)->index>=0)
+               {
+                       for(list<Constraint>::iterator j=(*i)->constraints.begin(); j!=(*i)->constraints.end(); ++j)
+                               if(j->target.index>(*i)->index && (j->type&SLACK))
+                                       ++n_slack_constraints[j->type&1];
+               }
 }
 
 Layout::Slot &Layout::get_slot_for_widget(Widget &wdg)
@@ -224,6 +260,7 @@ void Layout::create_constraint(Widget &src, ConstraintType type, Widget &tgt, in
        tgt_slot.constraints.push_back(Constraint(complement(type), src_slot));
        tgt_slot.constraints.back().spacing = sp;
 
+       update_slot_indices();
        update();
 }
 
@@ -286,7 +323,7 @@ void Layout::solve_constraints(int dir, SolveMode mode)
        five columns for each widget, and one constant column.  The first and second
        columns of a widget are its position and dimension, respectively.  The
        remaining three are slack columns; see below for their purposes. */
-       LinearProgram linprog(n_active_slots*5+1);
+       LinearProgram linprog(n_active_slots*5+n_slack_constraints[dir]+1);
        float weight = slots.size();
        for(list<Slot *>::iterator i=slots.begin(); i!=slots.end(); ++i)
        {
@@ -344,6 +381,7 @@ void Layout::solve_constraints(int dir, SolveMode mode)
 
                /* Add rows for user-defined constraints.  Constraints are always added
                in pairs, so it's only necessary to create a row for one half. */
+               unsigned k = n_active_slots*5;
                for(list<Constraint>::iterator j=(*i)->constraints.begin(); j!=(*i)->constraints.end(); ++j)
                        if(j->target.index>(*i)->index && (j->type&1)==dir)
                        {
@@ -359,6 +397,8 @@ void Layout::solve_constraints(int dir, SolveMode mode)
                                        row[j->target.index*5+1] = -polarity;
                                if(j->type&SPACING)
                                        row.back() = (j->spacing>=0 ? j->spacing : this->*(ptrs.spacing));
+                               if(j->type&SLACK)
+                                       row[k++] = -1;
                        }
        }
 
@@ -441,6 +481,42 @@ void Layout::Slot::visibility_changed(bool v)
 }
 
 
+void operator>>(const LexicalConverter &conv, Layout::ConstraintType &ctype)
+{
+       const string &str = conv.get();
+       if(str=="ABOVE")
+               ctype = Layout::ABOVE;
+       else if(str=="BELOW")
+               ctype = Layout::BELOW;
+       else if(str=="RIGHT_OF")
+               ctype = Layout::RIGHT_OF;
+       else if(str=="LEFT_OF")
+               ctype = Layout::LEFT_OF;
+       else if(str=="FAR_ABOVE")
+               ctype = Layout::FAR_ABOVE;
+       else if(str=="FAR_BELOW")
+               ctype = Layout::FAR_BELOW;
+       else if(str=="FAR_RIGHT_OF")
+               ctype = Layout::FAR_RIGHT_OF;
+       else if(str=="FAR_LEFT_OF")
+               ctype = Layout::FAR_LEFT_OF;
+       else if(str=="ALIGN_TOP")
+               ctype = Layout::ALIGN_TOP;
+       else if(str=="ALIGN_BOTTOM")
+               ctype = Layout::ALIGN_BOTTOM;
+       else if(str=="ALIGN_RIGHT")
+               ctype = Layout::ALIGN_RIGHT;
+       else if(str=="ALIGN_LEFT")
+               ctype = Layout::ALIGN_LEFT;
+       else if(str=="COPY_WIDTH")
+               ctype = Layout::COPY_WIDTH;
+       else if(str=="COPY_HEIGHT")
+               ctype = Layout::COPY_HEIGHT;
+       else
+               throw lexical_error(format("conversion of '%s' to ConstraintType", str));
+}
+
+
 Layout::LinearProgram::LinearProgram(unsigned s):
        n_columns(s),
        n_rows(1),