2 * ion/mod_tiling/split.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
16 #include <libtu/minmax.h>
18 #include <libtu/objp.h>
19 #include <ioncore/common.h>
20 #include <ioncore/focus.h>
21 #include <ioncore/global.h>
22 #include <ioncore/window.h>
23 #include <ioncore/resize.h>
24 #include <ioncore/attach.h>
25 #include <ioncore/manage.h>
26 #include <ioncore/extlconv.h>
27 #include <ioncore/rectangle.h>
28 #include <ioncore/saveload.h>
29 #include <ioncore/names.h>
32 #include "split-stdisp.h"
35 static Rb_node split_of_map=NULL;
38 /*{{{ Geometry helper functions */
41 int split_size(WSplit *split, int dir)
43 return (dir==SPLIT_HORIZONTAL ? split->geom.w : split->geom.h);
46 int split_other_size(WSplit *split, int dir)
48 return (dir==SPLIT_VERTICAL ? split->geom.w : split->geom.h);
51 int split_pos(WSplit *split, int dir)
53 return (dir==SPLIT_HORIZONTAL ? split->geom.x : split->geom.y);
56 int split_other_pos(WSplit *split, int dir)
58 return (dir==SPLIT_VERTICAL ? split->geom.x : split->geom.y);
62 static int reg_calcresize(WRegion *reg, int dir, int nsize)
66 if(dir==SPLIT_HORIZONTAL)
67 tmp=region_min_w(reg);
69 tmp=region_min_h(reg);
71 return (nsize<tmp ? tmp : nsize);
75 /* No, these are not even supposed to be proper/consistent
76 * Z \cup {\infty, -\infty} calculation rules.
79 static int infadd(int x, int y)
81 if(x==INT_MAX || y==INT_MAX)
88 static int infsub(int x, int y)
99 /* Negative "unused space" means no SPLIT_UNUSED under a node, while
100 * zero unused space means there's a zero-sized SPLIT_UNUSED under the
103 static int unusedadd(int x, int y)
107 return maxof(x, 0)+maxof(y, 0);
111 static void bound(int *what, int min, int max)
123 /*{{{ Functions to get and set a region's containing node */
126 #define node_of_reg splittree_node_of
128 WSplitRegion *splittree_node_of(WRegion *reg)
133 /*assert(REGION_MANAGER_CHK(reg, WTiling)!=NULL);*/
135 if(split_of_map!=NULL){
136 node=rb_find_pkey_n(split_of_map, reg, &found);
138 return (WSplitRegion*)(node->v.val);
145 #define set_node_of_reg splittree_set_node_of
148 bool splittree_set_node_of(WRegion *reg, WSplitRegion *split)
153 /*assert(REGION_MANAGER_CHK(reg, WTiling)!=NULL);*/
155 if(split_of_map==NULL){
158 split_of_map=make_rb();
159 if(split_of_map==NULL)
163 node=rb_find_pkey_n(split_of_map, reg, &found);
165 rb_delete_node(node);
167 return (rb_insertp(split_of_map, reg, split)!=NULL);
177 WPrimn primn_invert(WPrimn primn)
179 return (primn==PRIMN_TL
187 WPrimn primn_none2any(WPrimn primn)
189 return (primn==PRIMN_NONE ? PRIMN_ANY : primn);
199 bool split_init(WSplit *split, const WRectangle *geom)
202 split->ws_if_root=NULL;
206 split->max_w=INT_MAX;
207 split->max_h=INT_MAX;
213 bool splitinner_init(WSplitInner *split, const WRectangle *geom)
215 return split_init(&(split->split), geom);
219 bool splitsplit_init(WSplitSplit *split, const WRectangle *geom, int dir)
221 splitinner_init(&(split->isplit), geom);
225 split->current=SPLIT_CURRENT_TL;
230 bool splitregion_init(WSplitRegion *split, const WRectangle *geom,
233 split_init(&(split->split), geom);
236 set_node_of_reg(reg, split);
241 bool splitst_init(WSplitST *split, const WRectangle *geom, WRegion *reg)
243 splitregion_init(&(split->regnode), geom, reg);
244 split->orientation=REGION_ORIENTATION_HORIZONTAL;
245 split->corner=MPLEX_STDISP_BL;
250 WSplitSplit *create_splitsplit(const WRectangle *geom, int dir)
252 CREATEOBJ_IMPL(WSplitSplit, splitsplit, (p, geom, dir));
256 WSplitRegion *create_splitregion(const WRectangle *geom, WRegion *reg)
258 CREATEOBJ_IMPL(WSplitRegion, splitregion, (p, geom, reg));
262 WSplitST *create_splitst(const WRectangle *geom, WRegion *reg)
264 CREATEOBJ_IMPL(WSplitST, splitst, (p, geom, reg));
274 void split_deinit(WSplit *split)
276 assert(split->parent==NULL);
280 void splitinner_deinit(WSplitInner *split)
282 split_deinit(&(split->split));
286 void splitsplit_deinit(WSplitSplit *split)
289 split->tl->parent=NULL;
290 destroy_obj((Obj*)(split->tl));
293 split->br->parent=NULL;
294 destroy_obj((Obj*)(split->br));
297 splitinner_deinit(&(split->isplit));
301 void splitregion_deinit(WSplitRegion *split)
303 if(split->reg!=NULL){
304 set_node_of_reg(split->reg, NULL);
308 split_deinit(&(split->split));
312 void splitst_deinit(WSplitST *split)
314 splitregion_deinit(&(split->regnode));
321 /*{{{ Size bounds management */
324 static void splitregion_update_bounds(WSplitRegion *node, bool recursive)
327 WSplit *snode=(WSplit*)node;
329 assert(node->reg!=NULL);
331 region_size_hints(node->reg, &hints);
333 snode->min_w=maxof(1, hints.min_set ? hints.min_width : 1);
334 snode->max_w=INT_MAX;
337 snode->min_h=maxof(1, hints.min_set ? hints.min_height : 1);
338 snode->max_h=INT_MAX;
343 static void splitst_update_bounds(WSplitST *node, bool rec)
345 WSplit *snode=(WSplit*)node;
347 if(node->regnode.reg==NULL){
348 snode->min_w=CF_STDISP_MIN_SZ;
349 snode->min_h=CF_STDISP_MIN_SZ;
350 snode->max_w=CF_STDISP_MIN_SZ;
351 snode->max_h=CF_STDISP_MIN_SZ;
354 region_size_hints(node->regnode.reg, &hints);
355 snode->min_w=maxof(1, hints.min_set ? hints.min_width : 1);
356 snode->max_w=maxof(snode->min_w, hints.min_width);
357 snode->min_h=maxof(1, hints.min_set ? hints.min_height : 1);
358 snode->max_h=maxof(snode->min_h, hints.min_height);
364 if(node->orientation==REGION_ORIENTATION_HORIZONTAL){
365 snode->min_w=CF_STDISP_MIN_SZ;
366 snode->max_w=INT_MAX;
368 snode->min_h=CF_STDISP_MIN_SZ;
369 snode->max_h=INT_MAX;
374 static void splitsplit_update_bounds(WSplitSplit *split, bool recursive)
377 WSplit *node=(WSplit*)split;
379 assert(split->tl!=NULL && split->br!=NULL);
385 split_update_bounds(tl, TRUE);
386 split_update_bounds(br, TRUE);
389 if(split->dir==SPLIT_HORIZONTAL){
390 node->max_w=infadd(tl->max_w, br->max_w);
391 node->min_w=infadd(tl->min_w, br->min_w);
392 node->unused_w=unusedadd(tl->unused_w, br->unused_w);
393 node->min_h=maxof(tl->min_h, br->min_h);
394 node->max_h=maxof(minof(tl->max_h, br->max_h), node->min_h);
395 node->unused_h=minof(tl->unused_h, br->unused_h);
397 node->max_h=infadd(tl->max_h, br->max_h);
398 node->min_h=infadd(tl->min_h, br->min_h);
399 node->unused_h=unusedadd(tl->unused_h, br->unused_h);
400 node->min_w=maxof(tl->min_w, br->min_w);
401 node->max_w=maxof(minof(tl->max_w, br->max_w), node->min_w);
402 node->unused_w=minof(tl->unused_w, br->unused_w);
407 void split_update_bounds(WSplit *node, bool recursive)
409 CALL_DYN(split_update_bounds, node, (node, recursive));
413 void splitsplit_update_geom_from_children(WSplitSplit *node)
415 WSplit *split=(WSplit*)node;
417 if(node->dir==SPLIT_VERTICAL){
418 ((WSplit*)node)->geom.h=node->tl->geom.h+node->br->geom.h;
419 ((WSplit*)node)->geom.y=node->tl->geom.y;
420 }else if(node->dir==SPLIT_HORIZONTAL){
421 ((WSplit*)node)->geom.w=node->tl->geom.w+node->br->geom.w;
422 ((WSplit*)node)->geom.x=node->tl->geom.x;
430 /*{{{ Status display handling helper functions. */
433 static WSplitST *saw_stdisp=NULL;
436 void splittree_begin_resize()
442 void splittree_end_resize()
444 if(saw_stdisp!=NULL){
445 split_regularise_stdisp(saw_stdisp);
451 static void splittree_scan_stdisp_rootward_(WSplitInner *node_)
453 WSplitSplit *node=OBJ_CAST(node_, WSplitSplit);
456 if(OBJ_IS(node->tl, WSplitST)){
457 saw_stdisp=(WSplitST*)(node->tl);
459 }else if(OBJ_IS(node->br, WSplitST)){
460 saw_stdisp=(WSplitST*)(node->br);
465 if(node_->split.parent!=NULL)
466 splittree_scan_stdisp_rootward_(node_->split.parent);
470 void splittree_scan_stdisp_rootward(WSplit *node)
472 if(node->parent!=NULL)
473 splittree_scan_stdisp_rootward_(node->parent);
477 static WSplitSplit *splittree_scan_stdisp_parent(WSplit *node_, bool set_saw)
479 WSplitSplit *r, *node=OBJ_CAST(node_, WSplitSplit);
484 if(OBJ_IS(node->tl, WSplitST)){
486 saw_stdisp=(WSplitST*)node->tl;
490 if(OBJ_IS(node->br, WSplitST)){
492 saw_stdisp=(WSplitST*)node->br;
496 r=splittree_scan_stdisp_parent(node->tl, set_saw);
498 r=splittree_scan_stdisp_parent(node->br, set_saw);
503 static bool stdisp_immediate_child(WSplitSplit *node)
505 return (node!=NULL && (OBJ_IS(node->tl, WSplitST) ||
506 OBJ_IS(node->br, WSplitST)));
510 static WSplit *move_stdisp_out_of_way(WSplit *node)
512 WSplitSplit *stdispp;
514 if(!OBJ_IS(node, WSplitSplit))
517 stdispp=splittree_scan_stdisp_parent(node, TRUE);
522 while(stdispp->tl!=node && stdispp->br!=node){
523 if(!split_try_unsink_stdisp(stdispp, FALSE, TRUE)){
524 warn(TR("Unable to move the status display out of way."));
529 return (WSplit*)stdispp;
536 /*{{{ Low-level resize code; from root to leaf */
539 static void split_do_resize_default(WSplit *node, const WRectangle *ng,
540 WPrimn hprimn, WPrimn vprimn,
547 static void splitregion_do_resize(WSplitRegion *node, const WRectangle *ng,
548 WPrimn hprimn, WPrimn vprimn,
551 assert(node->reg!=NULL);
552 region_fit(node->reg, ng, REGION_FIT_EXACT);
553 split_update_bounds(&(node->split), FALSE);
554 node->split.geom=*ng;
558 static void splitst_do_resize(WSplitST *node, const WRectangle *ng,
559 WPrimn hprimn, WPrimn vprimn,
564 if(node->regnode.reg==NULL){
565 ((WSplit*)node)->geom=*ng;
567 splitregion_do_resize(&(node->regnode), ng, hprimn, vprimn,
573 static int other_dir(int dir)
575 return (dir==SPLIT_VERTICAL ? SPLIT_HORIZONTAL : SPLIT_VERTICAL);
579 static void adjust_sizes(int *tls_, int *brs_, int nsize, int sz,
580 int tlmin, int brmin, int tlmax, int brmax,
588 bound(&tls, tlmin, tlmax);
590 bound(&brs, brmin, brmax);
592 bound(&tls, tlmin, tlmax);
593 }else if(primn==PRIMN_BR){
595 bound(&brs, brmin, brmax);
597 bound(&tls, tlmin, tlmax);
599 bound(&brs, brmin, brmax);
600 }else{ /* && PRIMN_ANY */
602 bound(&tls, tlmin, tlmax);
604 bound(&brs, brmin, brmax);
606 bound(&tls, tlmin, tlmax);
614 static void get_minmaxunused(WSplit *node, int dir,
615 int *min, int *max, int *unused)
617 if(dir==SPLIT_VERTICAL){
619 *max=maxof(*min, node->max_h);
620 *unused=minof(node->unused_h, node->geom.h);
623 *max=maxof(*min, node->max_w);
624 *unused=minof(node->unused_w, node->geom.w);
629 void splitsplit_do_resize(WSplitSplit *node, const WRectangle *ng,
630 WPrimn hprimn, WPrimn vprimn, bool transpose)
632 assert(ng->w>=0 && ng->h>=0);
633 assert(node->tl!=NULL && node->br!=NULL);
634 assert(!transpose || (hprimn==PRIMN_ANY && vprimn==PRIMN_ANY));
637 WSplit *tl=node->tl, *br=node->br;
638 int tls=split_size((WSplit*)tl, node->dir);
639 int brs=split_size((WSplit*)br, node->dir);
641 /* Status display can not be transposed. */
642 int dir=((transpose && !stdisp_immediate_child(node))
643 ? other_dir(node->dir)
645 int nsize=(dir==SPLIT_VERTICAL ? ng->h : ng->w);
646 int primn=(dir==SPLIT_VERTICAL ? vprimn : hprimn);
647 int tlmin, tlmax, tlunused, tlused;
648 int brmin, brmax, brunused, brused;
649 WRectangle tlg=*ng, brg=*ng;
651 get_minmaxunused(tl, dir, &tlmin, &tlmax, &tlunused);
652 get_minmaxunused(br, dir, &brmin, &brmax, &brunused);
654 tlused=maxof(0, tls-maxof(0, tlunused));
655 brused=maxof(0, brs-maxof(0, brunused));
656 /* tlmin, brmin >= 1 => (tls>=tlmin, brs>=brmin => sz>0) */
659 if(primn==PRIMN_ANY && (tlunused>=0 || brunused>=0)){
660 if(nsize<=tlused+brused){
661 /* Need to shrink a tangible node */
662 adjust_sizes(&tls, &brs, nsize, sz,
663 tlmin, brmin, tlused, brused, primn);
665 /* Just expand or shrink unused space */
666 adjust_sizes(&tls, &brs, nsize, sz,
668 (tlunused<0 ? tlused : tlmax),
669 (brunused<0 ? brused : brmax), primn);
673 adjust_sizes(&tls, &brs, nsize, sz,
674 tlmin, brmin, tlmax, brmax, primn);
679 /* Bad fit; just size proportionally. */
684 tls=split_size(tl, node->dir)*nsize/sz;
689 if(dir==SPLIT_VERTICAL){
699 split_do_resize(tl, &tlg, hprimn, vprimn, transpose);
700 split_do_resize(br, &brg, hprimn, vprimn, transpose);
703 ((WSplit*)node)->geom=*ng;
704 split_update_bounds((WSplit*)node, FALSE);
709 void split_do_resize(WSplit *node, const WRectangle *ng,
710 WPrimn hprimn, WPrimn vprimn, bool transpose)
712 CALL_DYN(split_do_resize, node, (node, ng, hprimn, vprimn, transpose));
716 void split_resize(WSplit *node, const WRectangle *ng,
717 WPrimn hprimn, WPrimn vprimn)
719 split_update_bounds(node, TRUE);
720 splittree_begin_resize();
721 split_do_resize(node, ng, hprimn, vprimn, FALSE);
722 splittree_end_resize();
729 /*{{{ Low-level resize code; request towards root */
732 static void flexibility(WSplit *node, int dir, int *shrink, int *stretch)
734 if(dir==SPLIT_VERTICAL){
735 *shrink=maxof(0, node->geom.h-node->min_h);
736 if(OBJ_IS(node, WSplitST))
737 *stretch=maxof(0, node->max_h-node->geom.h);
741 *shrink=maxof(0, node->geom.w-node->min_w);
742 if(OBJ_IS(node, WSplitST))
743 *stretch=maxof(0, node->max_w-node->geom.w);
750 static void calc_amount(int *amount, int rs, WSplit *other, int dir)
754 flexibility(other, dir, &shrink, &stretch);
757 *amount=minof(rs, shrink);
759 *amount=-minof(-rs, stretch);
766 static void splitsplit_do_rqsize(WSplitSplit *p, WSplit *node,
767 RootwardAmount *ha, RootwardAmount *va,
768 WRectangle *rg, bool tryonly)
770 WPrimn hprimn=PRIMN_ANY, vprimn=PRIMN_ANY;
771 WRectangle og, pg, ng;
777 assert(!ha->any || ha->tl==0);
778 assert(!va->any || va->tl==0);
779 assert(p->tl==node || p->br==node);
789 ca=(p->dir==SPLIT_VERTICAL ? va : ha);
791 if(thisnode==PRIMN_TL || ca->any){
792 calc_amount(&amount, ca->br, other, p->dir);
794 }else/*if(thisnode==PRIMN_BR)*/{
795 calc_amount(&amount, ca->tl, other, p->dir);
799 if(((WSplit*)p)->parent==NULL /*||
800 (ha->tl==0 && ha->br==0 && va->tl==0 && va->br==0)*/){
801 if(((WSplit*)p)->ws_if_root!=NULL)
802 pg=REGION_GEOM((WTiling*)(((WSplit*)p)->ws_if_root));
804 pg=((WSplit*)p)->geom;
806 splitinner_do_rqsize(((WSplit*)p)->parent, (WSplit*)p, ha, va,
810 assert(pg.w>=0 && pg.h>=0);
815 if(p->dir==SPLIT_VERTICAL){
816 ng.h=maxof(0, node->geom.h+amount);
817 og.h=maxof(0, other->geom.h-amount);
818 adjust_sizes(&(ng.h), &(og.h), pg.h, ng.h+og.h,
819 node->min_h, other->min_h, node->max_h, other->max_h,
820 PRIMN_TL /* node is passed as tl param */);
821 if(thisnode==PRIMN_TL)
827 ng.w=maxof(0, node->geom.w+amount);
828 og.w=maxof(0, other->geom.w-amount);
829 adjust_sizes(&(ng.w), &(og.w), pg.w, ng.w+og.w,
830 node->min_w, other->min_w, node->max_w, other->max_w,
831 PRIMN_TL /* node is passed as tl param */);
832 if(thisnode==PRIMN_TL)
840 /* Entä jos 'other' on stdisp? */
841 split_do_resize(other, &og, hprimn, vprimn, FALSE);
843 ((WSplit*)p)->geom=pg;
850 void splitinner_do_rqsize(WSplitInner *p, WSplit *node,
851 RootwardAmount *ha, RootwardAmount *va,
852 WRectangle *rg, bool tryonly)
854 CALL_DYN(splitinner_do_rqsize, p, (p, node, ha, va, rg, tryonly));
858 static void initra(RootwardAmount *ra, int p, int s, int op, int os,
863 ra->br=(p+s)-(op+os);
871 void split_do_rqgeom_(WSplit *node, const WRectangle *ng,
872 bool hany, bool vany, WRectangle *rg,
875 RootwardAmount ha, va;
877 if(node->parent==NULL){
878 if(node->ws_if_root!=NULL)
879 *rg=REGION_GEOM((WTiling*)(node->ws_if_root));
883 initra(&ha, ng->x, ng->w, node->geom.x, node->geom.w, hany);
884 initra(&va, ng->y, ng->h, node->geom.y, node->geom.h, vany);
886 splitinner_do_rqsize(node->parent, node, &ha, &va, rg, tryonly);
894 /*{{{ Resize interface */
897 static void bnd(int *pos, int *sz, int opos, int osz, int minsz, int maxsz)
899 int ud=abs(*pos-opos);
900 int dd=abs((*pos+*sz)-(opos+osz));
904 bound(sz, minsz, maxsz);
905 *pos+=(szrq-*sz)*ud/(ud+dd);
910 WSplit *split_find_root(WSplit *split)
912 if(split->parent==NULL)
914 return split_find_root((WSplit*)split->parent);
918 void splittree_rqgeom(WSplit *sub, int flags, const WRectangle *geom_,
921 bool hany=flags®ION_RQGEOM_WEAK_X;
922 bool vany=flags®ION_RQGEOM_WEAK_Y;
923 bool tryonly=flags®ION_RQGEOM_TRYONLY;
924 WRectangle geom=*geom_;
926 WSplit *root=split_find_root(sub);
931 split_update_bounds(root, TRUE);
933 if(OBJ_IS(sub, WSplitST)){
934 WSplitST *sub_as_stdisp=(WSplitST*)sub;
936 if(flags®ION_RQGEOM_TRYONLY){
937 warn(TR("REGION_RQGEOM_TRYONLY unsupported for status display."));
941 split_regularise_stdisp(sub_as_stdisp);
943 if(sub_as_stdisp->orientation==REGION_ORIENTATION_HORIZONTAL){
952 split_update_bounds(root, TRUE);
955 /* Handle internal size bounds */
956 bnd(&(geom.x), &(geom.w), sub->geom.x, sub->geom.w,
957 sub->min_w, sub->max_w);
958 bnd(&(geom.y), &(geom.h), sub->geom.y, sub->geom.h,
959 sub->min_h, sub->max_h);
961 /* Check if we should resize to both tl and br */
964 geom.w+=sub->geom.x-geom.x;
969 geom.h+=sub->geom.y-geom.y;
973 splittree_begin_resize();
975 split_do_rqgeom_(sub, &geom, hany, vany, geomret, tryonly);
978 split_do_resize(sub, geomret, hany, vany, FALSE);
979 splittree_end_resize();
988 * Attempt to resize and/or move the split tree starting at \var{node}.
989 * Behaviour and the \var{g} parameter are as for \fnref{WRegion.rqgeom}
990 * operating on \var{node} (if it were a \type{WRegion}).
993 ExtlTab split_rqgeom(WSplit *node, ExtlTab g)
995 WRectangle geom, ogeom;
996 int flags=REGION_RQGEOM_WEAK_ALL;
1001 if(extl_table_gets_i(g, "x", &(geom.x)))
1002 flags&=~REGION_RQGEOM_WEAK_X;
1003 if(extl_table_gets_i(g, "y", &(geom.y)))
1004 flags&=~REGION_RQGEOM_WEAK_Y;
1005 if(extl_table_gets_i(g, "w", &(geom.w)))
1006 flags&=~REGION_RQGEOM_WEAK_W;
1007 if(extl_table_gets_i(g, "h", &(geom.h)))
1008 flags&=~REGION_RQGEOM_WEAK_H;
1010 geom.w=maxof(1, geom.w);
1011 geom.h=maxof(1, geom.h);
1013 splittree_rqgeom(node, flags, &geom, &ogeom);
1015 return extl_table_from_rectangle(&ogeom);
1018 warn(TR("Invalid node."));
1019 return extl_table_none();
1029 void splittree_changeroot(WSplit *root, WSplit *node)
1031 WTiling *ws=(WTiling*)(root->ws_if_root);
1034 assert(ws->split_tree==root);
1035 root->ws_if_root=NULL;
1036 ws->split_tree=node;
1038 node->ws_if_root=ws;
1044 static void splitsplit_replace(WSplitSplit *split, WSplit *child,
1047 assert(split->tl==child || split->br==child);
1049 if(split->tl==child)
1056 what->parent=(WSplitInner*)split;
1057 what->ws_if_root=NULL; /* May not be needed. */
1061 void splitinner_replace(WSplitInner *split, WSplit *child, WSplit *what)
1063 CALL_DYN(splitinner_replace, split, (split, child, what));
1067 WSplitRegion *splittree_split(WSplit *node, int dir, WPrimn primn,
1068 int minsize, WRegionSimpleCreateFn *fn,
1073 WSplitSplit *nsplit;
1074 WSplitRegion *nnode;
1075 WSplitInner *psplit;
1080 assert(node!=NULL && parent!=NULL);
1082 if(OBJ_IS(node, WSplitST)){
1083 warn(TR("Splitting the status display is not allowed."));
1087 splittree_begin_resize();
1089 if(!move_stdisp_out_of_way(node))
1092 if(primn!=PRIMN_TL && primn!=PRIMN_BR)
1094 if(dir!=SPLIT_HORIZONTAL && dir!=SPLIT_VERTICAL)
1097 split_update_bounds(split_find_root(node), TRUE);
1098 objmin=(dir==SPLIT_VERTICAL ? node->min_h : node->min_w);
1100 s=split_size(node, dir);
1101 sn=maxof(minsize, s/2);
1102 so=maxof(objmin, s-sn);
1107 if(dir==SPLIT_VERTICAL)
1111 split_do_rqgeom_(node, &ng, TRUE, TRUE, &rg, TRUE);
1112 rs=(dir==SPLIT_VERTICAL ? rg.h : rg.w);
1113 if(rs<minsize+objmin){
1114 warn(TR("Unable to split: not enough free space."));
1117 split_do_rqgeom_(node, &ng, TRUE, TRUE, &rg, FALSE);
1118 rs=(dir==SPLIT_VERTICAL ? rg.h : rg.w);
1123 so=maxof(rs/2, objmin);
1128 splittree_scan_stdisp_rootward(node);
1131 /* Create split and new window
1133 fp.mode=REGION_FIT_EXACT;
1136 nsplit=create_splitsplit(&(fp.g), dir);
1141 if(dir==SPLIT_VERTICAL){
1151 nreg=fn(parent, &fp);
1154 destroy_obj((Obj*)nsplit);
1158 nnode=create_splitregion(&(fp.g), nreg);
1160 destroy_obj((Obj*)nreg);
1161 destroy_obj((Obj*)nsplit);
1165 /* Now that everything's ok, resize and move original node.
1168 if(dir==SPLIT_VERTICAL){
1178 split_do_resize(node, &ng,
1179 (dir==SPLIT_HORIZONTAL ? primn : PRIMN_ANY),
1180 (dir==SPLIT_VERTICAL ? primn : PRIMN_ANY),
1183 /* Set up split structure
1185 psplit=node->parent;
1188 splitinner_replace(psplit, node, (WSplit*)nsplit);
1190 splittree_changeroot(node, (WSplit*)nsplit);
1192 node->parent=(WSplitInner*)nsplit;
1193 ((WSplit*)nnode)->parent=(WSplitInner*)nsplit;
1195 if(primn==PRIMN_BR){
1197 nsplit->br=(WSplit*)nnode;
1198 nsplit->current=SPLIT_CURRENT_TL;
1200 nsplit->tl=(WSplit*)nnode;
1202 nsplit->current=SPLIT_CURRENT_BR;
1205 splittree_end_resize();
1217 static void splitsplit_remove(WSplitSplit *node, WSplit *child,
1220 static int nstdisp=0;
1221 WSplitInner *parent;
1224 assert(node->tl==child || node->br==child);
1231 assert(other!=NULL);
1233 if(nstdisp==0 && reclaim_space && OBJ_IS(other, WSplitST)){
1234 /* Try to move stdisp out of the way. */
1235 split_try_unsink_stdisp(node, FALSE, TRUE);
1236 assert(child->parent!=NULL);
1238 splitinner_remove(child->parent, child, reclaim_space);
1243 parent=((WSplit*)node)->parent;
1246 splitinner_replace(parent, (WSplit*)node, other);
1248 splittree_changeroot((WSplit*)node, other);
1251 split_resize(other, &(((WSplit*)node)->geom), PRIMN_ANY, PRIMN_ANY);
1257 ((WSplit*)node)->parent=NULL;
1258 destroy_obj((Obj*)node);
1262 void splitinner_remove(WSplitInner *node, WSplit *child, bool reclaim_space)
1264 CALL_DYN(splitinner_remove, node, (node, child, reclaim_space));
1268 void splittree_remove(WSplit *node, bool reclaim_space)
1270 if(node->parent!=NULL)
1271 splitinner_remove(node->parent, node, reclaim_space);
1272 else if(node->ws_if_root!=NULL)
1273 splittree_changeroot(node, NULL);
1275 destroy_obj((Obj*)node);
1282 /*{{{ Tree traversal */
1285 static bool defaultfilter(WSplit *node)
1287 return (OBJ_IS(node, WSplitRegion) &&
1288 ((WSplitRegion*)node)->reg!=NULL);
1292 static WSplit *split_current_todir_default(WSplit *node,
1293 WPrimn hprimn, WPrimn vprimn,
1294 WSplitFilter *filter)
1297 filter=defaultfilter;
1299 return (filter(node) ? node : NULL);
1303 static WSplit *splitsplit_current_todir(WSplitSplit *node,
1304 WPrimn hprimn, WPrimn vprimn,
1305 WSplitFilter *filter)
1307 WPrimn primn=(node->dir==SPLIT_HORIZONTAL ? hprimn : vprimn);
1308 WSplit *first, *second, *ret;
1310 if(primn==PRIMN_TL ||
1311 (primn==PRIMN_ANY && node->current==SPLIT_CURRENT_TL)){
1314 }else if(primn==PRIMN_BR ||
1315 (primn==PRIMN_ANY && node->current==SPLIT_CURRENT_BR)){
1322 ret=split_current_todir(first, hprimn, vprimn, filter);
1324 ret=split_current_todir(second, hprimn, vprimn, filter);
1325 if(ret==NULL && filter!=NULL){
1326 if(filter((WSplit*)node))
1334 WSplit *split_current_todir(WSplit *node, WPrimn hprimn, WPrimn vprimn,
1335 WSplitFilter *filter)
1338 CALL_DYN_RET(ret, WSplit*, split_current_todir, node,
1339 (node, hprimn, vprimn, filter));
1344 WSplit *splitsplit_nextto(WSplitSplit *node, WSplit *child,
1345 WPrimn hprimn, WPrimn vprimn,
1346 WSplitFilter *filter)
1348 WPrimn primn=(node->dir==SPLIT_HORIZONTAL ? hprimn : vprimn);
1349 WSplit *split=NULL, *nnode=NULL;
1351 if(node->tl==child && (primn==PRIMN_BR || primn==PRIMN_ANY)){
1354 }else if(node->br==child && (primn==PRIMN_TL || primn==PRIMN_ANY)){
1360 if(node->dir==SPLIT_HORIZONTAL){
1362 vprimn=primn_none2any(vprimn);
1365 hprimn=primn_none2any(vprimn);
1368 nnode=split_current_todir(split, hprimn, vprimn, filter);
1375 WSplit *splitinner_nextto(WSplitInner *node, WSplit *child,
1376 WPrimn hprimn, WPrimn vprimn,
1377 WSplitFilter *filter)
1380 CALL_DYN_RET(ret, WSplit*, splitinner_nextto, node,
1381 (node, child, hprimn, vprimn, filter));
1386 WSplit *split_nextto(WSplit *node, WPrimn hprimn, WPrimn vprimn,
1387 WSplitFilter *filter)
1389 while(node->parent!=NULL){
1390 WSplit *ret=splitinner_nextto(node->parent, node,
1391 hprimn, vprimn, filter);
1394 node=(WSplit*)node->parent;
1400 void splitinner_mark_current_default(WSplitInner *split, WSplit *child)
1402 if(((WSplit*)split)->parent!=NULL)
1403 splitinner_mark_current(((WSplit*)split)->parent, (WSplit*)split);
1407 void splitsplit_mark_current(WSplitSplit *split, WSplit *child)
1409 assert(child==split->tl || child==split->br);
1411 split->current=(split->tl==child ? SPLIT_CURRENT_TL : SPLIT_CURRENT_BR);
1413 splitinner_mark_current_default(&(split->isplit), child);
1417 void splitinner_mark_current(WSplitInner *split, WSplit *child)
1419 CALL_DYN(splitinner_mark_current, split, (split, child));
1423 static void splitsplit_forall(WSplitSplit *node, WSplitFn *fn)
1430 void splitinner_forall(WSplitInner *node, WSplitFn *fn)
1432 CALL_DYN(splitinner_forall, node, (node, fn));
1436 static WSplit *splitsplit_current(WSplitSplit *split)
1438 return (split->current==SPLIT_CURRENT_TL ? split->tl : split->br);
1443 * Returns the most previously active child node of \var{split}.
1447 WSplit *splitinner_current(WSplitInner *node)
1450 CALL_DYN_RET(ret, WSplit*, splitinner_current, node, (node));
1458 /*{{{ X window handling */
1461 static void splitregion_stacking(WSplitRegion *split,
1462 Window *bottomret, Window *topret)
1466 if(split->reg!=NULL)
1467 region_stacking(split->reg, bottomret, topret);
1471 void splitsplit_stacking(WSplitSplit *split,
1472 Window *bottomret, Window *topret)
1474 Window tlb=None, tlt=None;
1475 Window brb=None, brt=None;
1477 split_stacking(split->tl, &tlb, &tlt);
1478 split_stacking(split->br, &brb, &brt);
1480 /* To make sure that this condition holds is left to the workspace
1481 * code to do after a split tree has been loaded or modified.
1483 if(split->current==SPLIT_CURRENT_TL){
1484 *topret=(tlt!=None ? tlt : brt);
1485 *bottomret=(brb!=None ? brb : tlb);
1487 *topret=(brt!=None ? brt : tlt);
1488 *bottomret=(tlb!=None ? tlb : brb);
1492 void split_stacking(WSplit *split, Window *bottomret, Window *topret)
1497 CALL_DYN(split_stacking, split, (split, bottomret, topret));
1502 static void splitregion_restack(WSplitRegion *split, Window other, int mode)
1504 if(split->reg!=NULL)
1505 region_restack(split->reg, other, mode);
1508 void splitsplit_restack(WSplitSplit *split, Window other, int mode)
1510 Window bottom=None, top=None;
1511 WSplit *first, *second;
1513 if(split->current==SPLIT_CURRENT_TL){
1521 split_restack(first, other, mode);
1522 split_stacking(first, &bottom, &top);
1527 split_restack(second, other, mode);
1530 void split_restack(WSplit *split, Window other, int mode)
1532 CALL_DYN(split_restack, split, (split, other, mode));
1536 static void splitregion_map(WSplitRegion *split)
1538 if(split->reg!=NULL)
1539 region_map(split->reg);
1542 static void splitinner_map(WSplitInner *split)
1544 splitinner_forall(split, split_map);
1547 void split_map(WSplit *split)
1549 CALL_DYN(split_map, split, (split));
1553 static void splitregion_unmap(WSplitRegion *split)
1555 if(split->reg!=NULL)
1556 region_unmap(split->reg);
1559 static void splitinner_unmap(WSplitInner *split)
1561 splitinner_forall(split, split_unmap);
1564 void split_unmap(WSplit *split)
1566 CALL_DYN(split_unmap, split, (split));
1570 static void splitregion_reparent(WSplitRegion *split, WWindow *wwin)
1572 if(split->reg!=NULL){
1573 WRectangle g=split->split.geom;
1574 region_reparent(split->reg, wwin, &g, REGION_FIT_EXACT);
1579 static void splitsplit_reparent(WSplitSplit *split, WWindow *wwin)
1581 if(split->current==SPLIT_CURRENT_TL){
1582 split_reparent(split->br, wwin);
1583 split_reparent(split->tl, wwin);
1585 split_reparent(split->tl, wwin);
1586 split_reparent(split->br, wwin);
1591 void split_reparent(WSplit *split, WWindow *wwin)
1593 CALL_DYN(split_reparent, split, (split, wwin));
1600 /*{{{ Transpose, flip, rotate */
1603 void splitsplit_flip_default(WSplitSplit *split)
1605 WRectangle tlng, brng;
1606 WRectangle *sg=&((WSplit*)split)->geom;
1609 assert(split->tl!=NULL && split->br!=NULL);
1611 split_update_bounds((WSplit*)split, TRUE);
1613 tlng=split->tl->geom;
1614 brng=split->br->geom;
1616 if(split->dir==SPLIT_HORIZONTAL){
1618 tlng.x=sg->x+sg->w-tlng.w;
1621 tlng.y=sg->y+sg->h-tlng.h;
1625 split->tl=split->br;
1627 split->current=(split->current==SPLIT_CURRENT_TL
1629 : SPLIT_CURRENT_TL);
1631 split_do_resize(split->tl, &brng, PRIMN_ANY, PRIMN_ANY, FALSE);
1632 split_do_resize(split->br, &tlng, PRIMN_ANY, PRIMN_ANY, FALSE);
1636 static void splitsplit_flip_(WSplitSplit *split)
1638 CALL_DYN(splitsplit_flip, split, (split));
1643 * Flip contents of \var{node}.
1646 void splitsplit_flip(WSplitSplit *split)
1648 splittree_begin_resize();
1650 if(!move_stdisp_out_of_way((WSplit*)split))
1653 splitsplit_flip_(split);
1655 splittree_end_resize();
1666 static FlipDir flipdir=FLIP_VERTICAL;
1669 static void do_flip(WSplit *split)
1671 WSplitSplit *ss=OBJ_CAST(split, WSplitSplit);
1674 if((flipdir==FLIP_ANY
1675 || (ss->dir==SPLIT_VERTICAL && flipdir==FLIP_VERTICAL)
1676 || (ss->dir==SPLIT_HORIZONTAL && flipdir==FLIP_HORIZONTAL))
1677 && !OBJ_IS(ss->tl, WSplitST)
1678 && !OBJ_IS(ss->br, WSplitST)){
1679 splitsplit_flip_(ss);
1683 if(OBJ_IS(ss, WSplitInner))
1684 splitinner_forall((WSplitInner*)ss, do_flip);
1688 static void splittree_flip_dir(WSplit *splittree, FlipDir dir)
1690 /* todo stdisp outta way */
1691 if(OBJ_IS(splittree, WSplitInner)){
1693 splitinner_forall((WSplitInner*)splittree, do_flip);
1698 static bool split_fliptrans_to(WSplit *node, const WRectangle *geom,
1699 bool trans, FlipDir flip)
1704 splittree_begin_resize();
1706 /* split_do_resize can do things right if 'node' has stdisp as child,
1707 * but otherwise transpose will put the stdisp in a bad split
1708 * configuration if it is contained within 'node', so we must
1709 * first move it and its fixed parent split below node. For correct
1710 * geometry calculation we move it immediately below node, and
1711 * resize stdisp's fixed parent node instead.
1713 node2=move_stdisp_out_of_way(node);
1718 split_update_bounds(node2, TRUE);
1720 split_do_rqgeom_(node2, geom, PRIMN_ANY, PRIMN_ANY, &rg, FALSE);
1722 split_do_resize(node2, &rg, PRIMN_ANY, PRIMN_ANY, trans);
1725 splittree_flip_dir(node2, flip);
1727 splittree_end_resize();
1733 bool split_transpose_to(WSplit *node, const WRectangle *geom)
1735 return split_fliptrans_to(node, geom, TRUE, FLIP_ANY);
1740 * Transpose contents of \var{node}.
1743 void split_transpose(WSplit *node)
1745 WRectangle g=node->geom;
1747 split_transpose_to(node, &g);
1751 bool split_rotate_to(WSplit *node, const WRectangle *geom, int rotation)
1753 FlipDir flip=FLIP_NONE;
1756 if(rotation==SCREEN_ROTATION_90){
1757 flip=FLIP_HORIZONTAL;
1759 }else if(rotation==SCREEN_ROTATION_180){
1761 }else if(rotation==SCREEN_ROTATION_270){
1766 return split_fliptrans_to(node, geom, trans, flip);
1776 * Return parent split for \var{split}.
1780 WSplitInner *split_parent(WSplit *split)
1782 return split->parent;
1787 * Returns the area of workspace used by the regions under \var{split}.
1791 ExtlTab split_geom(WSplit *split)
1793 return extl_table_from_rectangle(&(split->geom));
1798 * Returns the top or left child node of \var{split} depending
1799 * on the direction of the split.
1803 WSplit *splitsplit_tl(WSplitSplit *split)
1810 * Returns the bottom or right child node of \var{split} depending
1811 * on the direction of the split.
1815 WSplit *splitsplit_br(WSplitSplit *split)
1821 * Returns the direction of \var{split}; either ''vertical'' or
1826 const char *splitsplit_dir(WSplitSplit *split)
1828 return (split->dir==SPLIT_VERTICAL ? "vertical" : "horizontal");
1833 * Returns the region contained in \var{node}.
1837 WRegion *splitregion_reg(WSplitRegion *node)
1846 /*{{{ Save support */
1849 ExtlTab split_base_config(WSplit *node)
1851 ExtlTab t=extl_create_table();
1852 extl_table_sets_s(t, "type", OBJ_TYPESTR(node));
1857 static bool splitregion_get_config(WSplitRegion *node, ExtlTab *ret)
1864 if(!region_supports_save(node->reg)){
1865 warn(TR("Unable to get configuration for %s."),
1866 region_name(node->reg));
1870 rt=region_get_configuration(node->reg);
1871 t=split_base_config(&(node->split));
1872 extl_table_sets_t(t, "regparams", rt);
1873 extl_unref_table(rt);
1880 static bool splitst_get_config(WSplitST *node, ExtlTab *ret)
1882 *ret=split_base_config((WSplit*)node);
1887 static bool splitsplit_get_config(WSplitSplit *node, ExtlTab *ret)
1889 ExtlTab tab, tltab, brtab;
1892 if(!split_get_config(node->tl, &tltab))
1893 return split_get_config(node->br, ret);
1895 if(!split_get_config(node->br, &brtab)){
1900 tab=split_base_config((WSplit*)node);
1902 tls=split_size(node->tl, node->dir);
1903 brs=split_size(node->br, node->dir);
1905 extl_table_sets_s(tab, "dir", (node->dir==SPLIT_VERTICAL
1906 ? "vertical" : "horizontal"));
1908 extl_table_sets_i(tab, "tls", tls);
1909 extl_table_sets_t(tab, "tl", tltab);
1910 extl_unref_table(tltab);
1912 extl_table_sets_i(tab, "brs", brs);
1913 extl_table_sets_t(tab, "br", brtab);
1914 extl_unref_table(brtab);
1922 bool split_get_config(WSplit *node, ExtlTab *tabret)
1925 CALL_DYN_RET(ret, bool, split_get_config, node, (node, tabret));
1933 /*{{{ The classes */
1936 static DynFunTab split_dynfuntab[]={
1937 {split_do_resize, split_do_resize_default},
1938 {(DynFun*)split_current_todir, (DynFun*)split_current_todir_default},
1942 static DynFunTab splitinner_dynfuntab[]={
1943 {splitinner_mark_current, splitinner_mark_current_default},
1944 {split_map, splitinner_map},
1945 {split_unmap, splitinner_unmap},
1949 static DynFunTab splitsplit_dynfuntab[]={
1950 {split_update_bounds, splitsplit_update_bounds},
1951 {split_do_resize, splitsplit_do_resize},
1952 {splitinner_do_rqsize, splitsplit_do_rqsize},
1953 {splitinner_replace, splitsplit_replace},
1954 {splitinner_remove, splitsplit_remove},
1955 {(DynFun*)split_current_todir, (DynFun*)splitsplit_current_todir},
1956 {(DynFun*)splitinner_current, (DynFun*)splitsplit_current},
1957 {(DynFun*)splitinner_nextto, (DynFun*)splitsplit_nextto},
1958 {splitinner_mark_current, splitsplit_mark_current},
1959 {(DynFun*)split_get_config, (DynFun*)splitsplit_get_config},
1960 {splitinner_forall, splitsplit_forall},
1961 {split_restack, splitsplit_restack},
1962 {split_stacking, splitsplit_stacking},
1963 {split_reparent, splitsplit_reparent},
1964 {splitsplit_flip, splitsplit_flip_default},
1968 static DynFunTab splitregion_dynfuntab[]={
1969 {split_update_bounds, splitregion_update_bounds},
1970 {split_do_resize, splitregion_do_resize},
1971 {(DynFun*)split_get_config, (DynFun*)splitregion_get_config},
1972 {split_map, splitregion_map},
1973 {split_unmap, splitregion_unmap},
1974 {split_restack, splitregion_restack},
1975 {split_stacking, splitregion_stacking},
1976 {split_reparent, splitregion_reparent},
1980 static DynFunTab splitst_dynfuntab[]={
1981 {split_update_bounds, splitst_update_bounds},
1982 {split_do_resize, splitst_do_resize},
1983 {(DynFun*)split_get_config, (DynFun*)splitst_get_config},
1989 IMPLCLASS(WSplit, Obj, split_deinit, split_dynfuntab);
1992 IMPLCLASS(WSplitInner, WSplit, splitinner_deinit, splitinner_dynfuntab);
1995 IMPLCLASS(WSplitSplit, WSplitInner, splitsplit_deinit, splitsplit_dynfuntab);
1998 IMPLCLASS(WSplitRegion, WSplit, splitregion_deinit, splitregion_dynfuntab);
2001 IMPLCLASS(WSplitST, WSplitRegion, splitst_deinit, splitst_dynfuntab);