X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flayout.cpp;h=c616ff6fc7caff9833070cf1c30e7c7710e20ad9;hb=8eeb6ee5d40c21150839e24996cc3e9ef308374d;hp=e9f9b40d0e9e0cced67238e62f2b0d8b480754d2;hpb=a6733b168083926ccc11eb3896642348b13817a2;p=libs%2Fgltk.git diff --git a/source/layout.cpp b/source/layout.cpp index e9f9b40..c616ff6 100644 --- a/source/layout.cpp +++ b/source/layout.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include "arrangement.h" #include "container.h" #include "layout.h" #include "widget.h" @@ -88,7 +91,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 +139,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::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::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 +202,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 +218,16 @@ void Layout::update_slot_indices() else (*i)->index = -1; } + + n_slack_constraints[0] = 0; + n_slack_constraints[1] = 0; + for(list::iterator i=slots.begin(); i!=slots.end(); ++i) + if((*i)->index>=0) + { + for(list::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) @@ -204,16 +241,7 @@ Layout::Slot &Layout::get_slot_for_widget(Widget &wdg) Layout::ConstraintType Layout::complement(ConstraintType type) { - if(type==RIGHT_OF) - return LEFT_OF; - else if(type==LEFT_OF) - return RIGHT_OF; - else if(type==ABOVE) - return BELOW; - else if(type==BELOW) - return ABOVE; - else - return type; + return static_cast((type&~(SELF_MASK|TARGET_MASK)) | ((type&SELF_MASK)<<2) | ((type&TARGET_MASK)>>2)); } void Layout::create_constraint(Widget &src, ConstraintType type, Widget &tgt, int sp) @@ -233,6 +261,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(); } @@ -295,7 +324,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::iterator i=slots.begin(); i!=slots.end(); ++i) { @@ -343,31 +372,34 @@ void Layout::solve_constraints(int dir, SolveMode mode) } { - /* Only allow the widget's dimension to increase. The geometry has - previously been set to the smallest allowable size. */ + /* Don't allow the widget's dimension to get below that determined + by autosizing. */ LinearProgram::Row row = linprog.add_row(); row[(*i)->index*5+1] = 1; row[(*i)->index*5+4] = -1; row.back() = (*i)->autosize_geom.*(ptrs.dim); } - /* Add rows for user-defined constraints. Below/above and left/right of - constraints are always added in pairs, so it's only necessary to create a - row for one half. */ + /* 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::iterator j=(*i)->constraints.begin(); j!=(*i)->constraints.end(); ++j) - if(j->target.index>=0 && (j->type&1)==dir && j->type!=BELOW && j->type!=LEFT_OF) + if(j->target.index>(*i)->index && (j->type&1)==dir) { LinearProgram::Row row = linprog.add_row(); + float polarity = ((j->type&SELF_DIM) ? -1 : 1); if(j->type&SELF_POS) - row[(*i)->index*5] = 1; + row[(*i)->index*5] = polarity; if(j->type&SELF_DIM) - row[(*i)->index*5+1] = 1; + row[(*i)->index*5+1] = polarity; if(j->type&TARGET_POS) - row[j->target.index*5] = -1; + row[j->target.index*5] = -polarity; if(j->type&TARGET_DIM) - row[j->target.index*5+1] = -1; + 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; } } @@ -450,6 +482,110 @@ void Layout::Slot::visibility_changed(bool v) } +Layout::Loader::Loader(Layout &l, const WidgetMap &wm): + DataFile::ObjectLoader(l), + wdg_map(wm) +{ + add("column_spacing", &Loader::column_spacing); + add("margin", &Loader::margin); + add("row_spacing", &Loader::row_spacing); + add("spacing", &Loader::spacing); + add("widget", &Loader::widget); +} + +void Layout::Loader::column_spacing(unsigned s) +{ + obj.set_column_spacing(s); +} + +void Layout::Loader::margin() +{ + Sides sides; + load_sub(sides); + obj.set_margin(sides); +} + +void Layout::Loader::spacing(unsigned s) +{ + obj.set_spacing(s); +} + +void Layout::Loader::row_spacing(unsigned s) +{ + obj.set_row_spacing(s); +} + +void Layout::Loader::widget(const string &n) +{ + Widget &wdg = *get_item(wdg_map, n); + WidgetLoader ldr(obj, wdg, wdg_map); + load_sub_with(ldr); +} + + +Layout::WidgetLoader::WidgetLoader(Layout &l, Widget &w, const Layout::Loader::WidgetMap &wm): + layout(l), + widget(w), + wdg_map(wm) +{ + add("constraint", &WidgetLoader::constraint); + add("expand", &WidgetLoader::expand); + add("gravity", &WidgetLoader::gravity); +} + +void Layout::WidgetLoader::constraint(ConstraintType type, const string &n) +{ + Widget &target = *get_item(wdg_map, n); + layout.add_constraint(widget, type, target); +} + +void Layout::WidgetLoader::expand(bool h, bool v) +{ + layout.set_expand(widget, h, v); +} + +void Layout::WidgetLoader::gravity(int h, int v) +{ + layout.set_gravity(widget, h, 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),