]> git.decadent.org.uk Git - ion3.git/blob - mod_tiling/tiling.c
[svn-upgrade] Integrating new upstream version, ion3 (20071130)
[ion3.git] / mod_tiling / tiling.c
1 /*
2  * ion/mod_tiling/tiling.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <string.h>
10
11 #include <libtu/objp.h>
12 #include <libtu/minmax.h>
13 #include <libtu/ptrlist.h>
14 #include <libmainloop/defer.h>
15 #include <libmainloop/signal.h>
16
17 #include <ioncore/common.h>
18 #include <ioncore/rootwin.h>
19 #include <ioncore/focus.h>
20 #include <ioncore/global.h>
21 #include <ioncore/region.h>
22 #include <ioncore/manage.h>
23 #include <ioncore/screen.h>
24 #include <ioncore/names.h>
25 #include <ioncore/saveload.h>
26 #include <ioncore/attach.h>
27 #include <ioncore/resize.h>
28 #include <libextl/extl.h>
29 #include <ioncore/regbind.h>
30 #include <ioncore/extlconv.h>
31 #include <ioncore/xwindow.h>
32 #include <ioncore/navi.h>
33 #include "placement.h"
34 #include "tiling.h"
35 #include "split.h"
36 #include "splitfloat.h"
37 #include "split-stdisp.h"
38 #include "main.h"
39
40
41
42 static WTilingIterTmp tiling_iter_default_tmp;
43
44
45 /*{{{ Some helper routines */
46
47
48 static WSplitRegion *get_node_check(WTiling *ws, WRegion *reg)
49 {
50     WSplitRegion *node;
51
52     if(reg==NULL)
53         return NULL;
54     
55     node=splittree_node_of(reg);
56     
57     if(node==NULL || REGION_MANAGER(reg)!=(WRegion*)ws)
58         return NULL;
59     
60     return node;
61 }
62
63
64 static bool check_node(WTiling *ws, WSplit *split)
65 {
66     if(split->parent)
67         return check_node(ws, (WSplit*)split->parent);
68     
69     if((split->ws_if_root!=(void*)ws)){
70         warn(TR("Split not on workspace."));
71         return FALSE;
72     }
73     return TRUE;
74 }
75
76
77 /*}}}*/
78
79
80 /*{{{ Dynfun implementations */
81
82
83 static void reparent_mgd(WRegion *sub, WWindow *par)
84 {
85     WFitParams subfp;
86     subfp.g=REGION_GEOM(sub);
87     subfp.mode=REGION_FIT_EXACT;
88     if(!region_fitrep(sub, par, &subfp)){
89         warn(TR("Error reparenting %s."), region_name(sub));
90         region_detach_manager(sub);
91     }
92 }
93
94
95 bool tiling_fitrep(WTiling *ws, WWindow *par, const WFitParams *fp)
96 {
97     WTilingIterTmp tmp;
98     bool ok=FALSE;
99     
100     if(par!=NULL){
101         if(!region_same_rootwin((WRegion*)ws, (WRegion*)par))
102             return FALSE;
103     
104         region_unset_parent((WRegion*)ws);
105         
106         XReparentWindow(ioncore_g.dpy, ws->dummywin, 
107                         par->win, fp->g.x, fp->g.y);
108         
109         region_set_parent((WRegion*)ws, par);
110     
111         if(ws->split_tree!=NULL)
112             split_reparent(ws->split_tree, par);
113     }
114     
115     REGION_GEOM(ws)=fp->g;
116     
117     if(ws->split_tree!=NULL){
118         bool done=FALSE;
119         if(fp->mode&REGION_FIT_ROTATE)
120             ok=split_rotate_to(ws->split_tree, &(fp->g), fp->rotation);
121         if(!ok)
122             split_resize(ws->split_tree, &(fp->g), PRIMN_ANY, PRIMN_ANY);
123     }
124     
125     return TRUE;
126 }
127
128
129 void tiling_managed_rqgeom(WTiling *ws, WRegion *mgd, 
130                            const WRQGeomParams *rq,
131                            WRectangle *geomret)
132 {
133     WSplitRegion *node=get_node_check(ws, mgd);
134     if(node!=NULL && ws->split_tree!=NULL)
135         splittree_rqgeom((WSplit*)node, rq->flags, &rq->geom, geomret);
136 }
137
138
139 void tiling_map(WTiling *ws)
140 {
141     REGION_MARK_MAPPED(ws);
142     XMapWindow(ioncore_g.dpy, ws->dummywin);
143     
144     if(ws->split_tree!=NULL)
145         split_map(ws->split_tree);
146 }
147
148
149 void tiling_unmap(WTiling *ws)
150 {
151     REGION_MARK_UNMAPPED(ws);
152     XUnmapWindow(ioncore_g.dpy, ws->dummywin);
153
154     if(ws->split_tree!=NULL)
155         split_unmap(ws->split_tree);
156 }
157
158
159 void tiling_fallback_focus(WTiling *ws, bool warp)
160 {
161     region_finalise_focusing((WRegion*)ws, ws->dummywin, warp);
162 }
163
164
165 void tiling_do_set_focus(WTiling *ws, bool warp)
166 {
167     WRegion *sub=tiling_current(ws);
168     
169     if(sub==NULL){
170         tiling_fallback_focus(ws, warp);
171         return;
172     }
173
174     region_do_set_focus(sub, warp);
175 }
176
177
178 static WTimer *restack_timer=NULL;
179
180
181 static void restack_handler(WTimer *tmr, Obj *obj)
182 {
183     if(obj!=NULL){
184         WTiling *ws=(WTiling*)obj;
185         split_restack(ws->split_tree, ws->dummywin, Above);
186     }
187 }
188
189
190 bool tiling_managed_prepare_focus(WTiling *ws, WRegion *reg, 
191                                  int flags, WPrepareFocusResult *res)
192 {
193     WSplitRegion *node; 
194
195     if(!region_prepare_focus((WRegion*)ws, flags, res))
196         return FALSE;
197     
198     node=get_node_check(ws, reg);
199     
200     if(node!=NULL && node->split.parent!=NULL)
201         splitinner_mark_current(node->split.parent, &(node->split));
202         
203     /* WSplitSplit uses activity based stacking as required on WAutoWS,
204      * so we must restack here.
205      */
206     if(ws->split_tree!=NULL){
207         int rd=mod_tiling_raise_delay;
208         bool use_timer=rd>0 && flags&REGION_GOTO_ENTERWINDOW;
209         
210         if(use_timer){
211             if(restack_timer!=NULL){
212                 Obj *obj=restack_timer->objwatch.obj;
213                 if(obj!=(Obj*)ws){
214                     timer_reset(restack_timer);
215                     restack_handler(restack_timer, obj);
216                 }
217             }else{
218                 restack_timer=create_timer();
219             }
220         }
221         
222         if(use_timer && restack_timer!=NULL){
223             timer_set(restack_timer, rd, restack_handler, (Obj*)ws);
224         }else{
225             split_restack(ws->split_tree, ws->dummywin, Above);
226         }
227     }
228
229     res->reg=reg;
230     res->flags=flags;
231     return TRUE;
232 }
233
234
235
236 void tiling_restack(WTiling *ws, Window other, int mode)
237 {
238     xwindow_restack(ws->dummywin, other, mode);
239     if(ws->split_tree!=NULL)
240         split_restack(ws->split_tree, ws->dummywin, Above);
241 }
242
243
244 void tiling_stacking(WTiling *ws, Window *bottomret, Window *topret)
245 {
246     Window sbottom=None, stop=None;
247     
248     if(ws->split_tree!=None)
249         split_stacking(ws->split_tree, &sbottom, &stop);
250     
251     *bottomret=ws->dummywin;
252     *topret=(stop!=None ? stop : ws->dummywin);
253 }
254
255
256 Window tiling_xwindow(const WTiling *ws)
257 {
258     return ws->dummywin;
259 }
260
261
262 /*}}}*/
263
264
265 /*{{{ Status display support code */
266
267
268 static bool regnodefilter(WSplit *split)
269 {
270     return OBJ_IS(split, WSplitRegion);
271 }
272
273
274 void tiling_unmanage_stdisp(WTiling *ws, bool permanent, bool nofocus)
275 {
276     WSplitRegion *tofocus=NULL;
277     bool setfocus=FALSE;
278     WRegion *od;
279     
280     if(ws->stdispnode==NULL)
281         return;
282     
283     od=ws->stdispnode->regnode.reg;
284     
285     if(od!=NULL){
286         if(!nofocus && REGION_IS_ACTIVE(od) && 
287            region_may_control_focus((WRegion*)ws)){
288             setfocus=TRUE;
289             tofocus=(WSplitRegion*)split_nextto((WSplit*)(ws->stdispnode), 
290                                                 PRIMN_ANY, PRIMN_ANY,
291                                                 regnodefilter);
292         }
293         /* Reset node_of info here so tiling_managed_remove will not
294          * remove the node.
295          */
296         splittree_set_node_of(od, NULL);
297         tiling_do_managed_remove(ws, od);
298     }
299     
300     if(permanent){
301         WSplit *node=(WSplit*)ws->stdispnode;
302         ws->stdispnode=NULL;
303         splittree_remove(node, TRUE);
304     }
305     
306     if(setfocus){
307         if(tofocus!=NULL)
308             region_set_focus(tofocus->reg);
309         else
310             tiling_fallback_focus(ws, FALSE);
311     }
312 }
313
314
315 static void tiling_create_stdispnode(WTiling *ws, WRegion *stdisp, 
316                                     int corner, int orientation, 
317                                     bool fullsize)
318 {
319     int flags=REGION_RQGEOM_WEAK_X|REGION_RQGEOM_WEAK_Y;
320     WRectangle *wg=&REGION_GEOM(ws), dg;
321     WSplitST *stdispnode;
322     WSplitSplit *split;
323
324     assert(ws->split_tree!=NULL);
325     
326     if(orientation==REGION_ORIENTATION_HORIZONTAL){
327         dg.x=wg->x;
328         dg.w=wg->w;
329         dg.h=0;
330         dg.y=((corner==MPLEX_STDISP_BL || corner==MPLEX_STDISP_BR)
331               ? wg->y+wg->h
332               : 0);
333     }else{
334         dg.y=wg->y;
335         dg.h=wg->h;
336         dg.w=0;
337         dg.x=((corner==MPLEX_STDISP_TR || corner==MPLEX_STDISP_BR)
338               ? wg->x+wg->w
339               : 0);
340     }
341             
342     stdispnode=create_splitst(&dg, stdisp);
343     
344     if(stdispnode==NULL){
345         warn(TR("Unable to create a node for status display."));
346         return;
347     }
348     
349     stdispnode->corner=corner;
350     stdispnode->orientation=orientation;
351     stdispnode->fullsize=fullsize;
352     
353     split=create_splitsplit(wg, (orientation==REGION_ORIENTATION_HORIZONTAL 
354                                  ? SPLIT_VERTICAL
355                                  : SPLIT_HORIZONTAL));
356
357     if(split==NULL){
358         warn(TR("Unable to create new split for status display."));
359         stdispnode->regnode.reg=NULL;
360         destroy_obj((Obj*)stdispnode);
361         return;
362     }
363
364     /* Set up new split tree */
365     ((WSplit*)stdispnode)->parent=(WSplitInner*)split;
366     ws->split_tree->parent=(WSplitInner*)split;
367     ws->split_tree->ws_if_root=NULL;
368     
369     if((orientation==REGION_ORIENTATION_HORIZONTAL && 
370         (corner==MPLEX_STDISP_BL || corner==MPLEX_STDISP_BR)) ||
371        (orientation==REGION_ORIENTATION_VERTICAL && 
372         (corner==MPLEX_STDISP_TR || corner==MPLEX_STDISP_BR))){
373         split->tl=ws->split_tree;
374         split->br=(WSplit*)stdispnode;
375         split->current=SPLIT_CURRENT_TL;
376     }else{
377         split->tl=(WSplit*)stdispnode;
378         split->br=ws->split_tree;
379         split->current=SPLIT_CURRENT_BR;
380     }
381
382     ws->split_tree=(WSplit*)split;
383     ((WSplit*)split)->ws_if_root=ws;
384     ws->stdispnode=stdispnode;
385 }
386
387
388 void tiling_manage_stdisp(WTiling *ws, WRegion *stdisp, 
389                          const WMPlexSTDispInfo *di)
390 {
391     bool mcf=region_may_control_focus((WRegion*)ws);
392     int flags=REGION_RQGEOM_WEAK_X|REGION_RQGEOM_WEAK_Y;
393     int orientation=region_orientation(stdisp);
394     bool act=FALSE;
395     WRectangle dg, *stdg;
396     
397     if(orientation!=REGION_ORIENTATION_VERTICAL /*&&
398        orientation!=REGION_ORIENTATION_HORIZONTAL*/){
399         orientation=REGION_ORIENTATION_HORIZONTAL;
400     }
401     
402     if(ws->stdispnode==NULL || ws->stdispnode->regnode.reg!=stdisp)
403         region_detach_manager(stdisp);
404
405     /* Remove old stdisp if corner and orientation don't match.
406      */
407     if(ws->stdispnode!=NULL && (di->pos!=ws->stdispnode->corner ||
408                                 orientation!=ws->stdispnode->orientation)){
409         tiling_unmanage_stdisp(ws, TRUE, TRUE);
410     }
411
412     if(ws->stdispnode==NULL){
413         tiling_create_stdispnode(ws, stdisp, di->pos, orientation, 
414                                 di->fullsize);
415         if(ws->stdispnode==NULL)
416             return;
417     }else{
418         WRegion *od=ws->stdispnode->regnode.reg;
419         if(od!=NULL){
420             act=REGION_IS_ACTIVE(od);
421             splittree_set_node_of(od, NULL);
422             tiling_managed_remove(ws, od);
423             assert(ws->stdispnode->regnode.reg==NULL);
424         }
425         
426         ws->stdispnode->fullsize=di->fullsize;
427         ws->stdispnode->regnode.reg=stdisp;
428         splittree_set_node_of(stdisp, &(ws->stdispnode->regnode));
429     }
430     
431     if(!tiling_managed_add(ws, stdisp)){
432         tiling_unmanage_stdisp(ws, TRUE, TRUE);
433         return;
434     }
435
436     stdisp->flags|=REGION_SKIP_FOCUS;
437     
438     dg=((WSplit*)(ws->stdispnode))->geom;
439     
440     dg.h=stdisp_recommended_h(ws->stdispnode);
441     dg.w=stdisp_recommended_w(ws->stdispnode);
442     
443     splittree_rqgeom((WSplit*)(ws->stdispnode), flags, &dg, FALSE);
444     
445     stdg=&(((WSplit*)ws->stdispnode)->geom);
446     
447     if(stdisp->geom.x!=stdg->x || stdisp->geom.y!=stdg->y ||
448        stdisp->geom.w!=stdg->w || stdisp->geom.h!=stdg->h){
449         region_fit(stdisp, stdg, REGION_FIT_EXACT);
450     }
451
452     /* Restack to ensure the split tree is stacked in the expected order. */
453     if(ws->split_tree!=NULL)
454         split_restack(ws->split_tree, ws->dummywin, Above);
455     
456     if(mcf && act)
457         region_set_focus(stdisp);
458 }
459
460
461 /*}}}*/
462
463
464 /*{{{ Create/destroy */
465
466
467 bool tiling_managed_add_default(WTiling *ws, WRegion *reg)
468 {
469     Window bottom=None, top=None;
470     WFrame *frame;
471     
472     if(TILING_STDISP_OF(ws)!=reg){
473         if(!ptrlist_insert_last(&(ws->managed_list), reg))
474             return FALSE;
475     }
476     
477     region_set_manager(reg, (WRegion*)ws);
478     
479     frame=OBJ_CAST(reg, WFrame);
480     if(frame!=NULL){
481         if(framemode_unalt(frame_mode(frame))==FRAME_MODE_TILED)
482             frame_set_mode(frame, FRAME_MODE_TILED);
483     }
484     
485     if(REGION_IS_MAPPED(ws))
486         region_map(reg);
487     
488     if(region_may_control_focus((WRegion*)ws)){
489         WRegion *curr=tiling_current(ws);
490         if(curr==NULL || !REGION_IS_ACTIVE(curr))
491             region_warp(reg);
492     }
493     
494     return TRUE;
495 }
496
497
498 bool tiling_managed_add(WTiling *ws, WRegion *reg)
499 {
500     bool ret=FALSE;
501     CALL_DYN_RET(ret, bool, tiling_managed_add, ws, (ws, reg));
502     return ret;
503 }
504
505
506 bool tiling_do_attach_initial(WTiling *ws, WRegion *reg)
507 {
508     assert(ws->split_tree==NULL);
509     
510     ws->split_tree=(WSplit*)create_splitregion(&REGION_GEOM(reg), reg);
511     if(ws->split_tree==NULL)
512         return FALSE;
513     
514     ws->split_tree->ws_if_root=ws;
515     
516     if(!tiling_managed_add(ws, reg)){
517         destroy_obj((Obj*)ws->split_tree);
518         ws->split_tree=NULL;
519         return FALSE;
520     }
521     
522     return TRUE;
523 }
524                                       
525
526 static WRegion *create_frame_tiling(WWindow *parent, const WFitParams *fp)
527 {
528     return (WRegion*)create_frame(parent, fp, FRAME_MODE_TILED);
529 }
530
531
532 bool tiling_init(WTiling *ws, WWindow *parent, const WFitParams *fp, 
533                 WRegionSimpleCreateFn *create_frame_fn, bool ci)
534 {
535     ws->split_tree=NULL;
536     ws->create_frame_fn=(create_frame_fn 
537                          ? create_frame_fn
538                          : create_frame_tiling);
539     ws->stdispnode=NULL;
540     ws->managed_list=NULL;
541     ws->batchop=FALSE;
542     
543     ws->dummywin=XCreateWindow(ioncore_g.dpy, parent->win,
544                                 fp->g.x, fp->g.y, 1, 1, 0,
545                                 CopyFromParent, InputOnly,
546                                 CopyFromParent, 0, NULL);
547     if(ws->dummywin==None)
548         return FALSE;
549
550     region_init(&(ws->reg), parent, fp);
551
552     ws->reg.flags|=(REGION_GRAB_ON_PARENT|
553                     REGION_PLEASE_WARP);
554     
555     if(ci){
556         WRegionAttachData data;
557         WRegion *res;
558         
559         data.type=REGION_ATTACH_NEW;
560         data.u.n.fn=(WRegionCreateFn*)ws->create_frame_fn;
561         data.u.n.param=NULL;
562     
563         res=region_attach_helper((WRegion*)ws, parent, fp,
564                                  (WRegionDoAttachFn*)tiling_do_attach_initial, 
565                                  NULL, &data);
566
567         if(res==NULL){
568             XDestroyWindow(ioncore_g.dpy, ws->dummywin);
569             return FALSE;
570         }
571     }
572     
573     XSelectInput(ioncore_g.dpy, ws->dummywin,
574                  FocusChangeMask|KeyPressMask|KeyReleaseMask|
575                  ButtonPressMask|ButtonReleaseMask);
576     XSaveContext(ioncore_g.dpy, ws->dummywin, ioncore_g.win_context,
577                  (XPointer)ws);
578     
579     region_register(&(ws->reg));
580     region_add_bindmap((WRegion*)ws, mod_tiling_tiling_bindmap);
581     
582     return TRUE;
583 }
584
585
586 WTiling *create_tiling(WWindow *parent, const WFitParams *fp, 
587                      WRegionSimpleCreateFn *create_frame_fn, bool ci)
588 {
589     CREATEOBJ_IMPL(WTiling, tiling, (p, parent, fp, create_frame_fn, ci));
590 }
591
592
593 WTiling *create_tiling_simple(WWindow *parent, const WFitParams *fp)
594 {
595     return create_tiling(parent, fp, NULL, TRUE);
596 }
597
598
599 void tiling_deinit(WTiling *ws)
600 {
601     WRegion *reg;
602     WTilingIterTmp tmp;
603     WMPlex *remanage_mplex=NULL;
604     
605     tiling_unmanage_stdisp(ws, FALSE, TRUE);
606
607     FOR_ALL_MANAGED_BY_TILING(reg, ws, tmp){
608         destroy_obj((Obj*)reg);
609     }
610
611     FOR_ALL_MANAGED_BY_TILING(reg, ws, tmp){
612         assert(FALSE);
613     }
614     
615     if(ws->split_tree!=NULL)
616         destroy_obj((Obj*)(ws->split_tree));
617
618     XDeleteContext(ioncore_g.dpy, ws->dummywin, ioncore_g.win_context);
619     XDestroyWindow(ioncore_g.dpy, ws->dummywin);
620     ws->dummywin=None;
621
622     region_deinit(&(ws->reg));
623 }
624
625
626 WRegion *tiling_managed_disposeroot(WTiling *ws, WRegion *reg)
627 {
628     WTilingIterTmp tmp;
629     WRegion *mgd;
630     
631     if(ws->batchop)
632         return reg;
633     
634     FOR_ALL_MANAGED_BY_TILING(mgd, ws, tmp){
635         if(mgd!=TILING_STDISP_OF(ws) && mgd!=reg)
636             return reg;
637     }
638     
639     return region_disposeroot((WRegion*)ws);
640 }
641
642
643 bool tiling_rescue_clientwins(WTiling *ws, WRescueInfo *info)
644 {
645     WTilingIterTmp tmp;
646     
647     ptrlist_iter_init(&tmp, ws->managed_list);
648     
649     return region_rescue_some_clientwins((WRegion*)ws, info,
650                                          (WRegionIterator*)ptrlist_iter, 
651                                          &tmp);
652 }
653
654
655 void tiling_do_managed_remove(WTiling *ws, WRegion *reg)
656 {
657     if(TILING_STDISP_OF(ws)==reg){
658         ws->stdispnode->regnode.reg=NULL;
659     }else{
660         ptrlist_remove(&(ws->managed_list), reg);
661     }
662
663     region_unset_manager(reg, (WRegion*)ws);
664     splittree_set_node_of(reg, NULL);
665 }
666
667
668 static bool nostdispfilter(WSplit *node)
669 {
670     return (OBJ_IS(node, WSplitRegion) && !OBJ_IS(node, WSplitST));
671 }
672
673
674 void tiling_managed_remove(WTiling *ws, WRegion *reg)
675 {
676     bool act=REGION_IS_ACTIVE(reg);
677     bool mcf=region_may_control_focus((WRegion*)ws);
678     WSplitRegion *node=get_node_check(ws, reg);
679     bool norestore=(OBJ_IS_BEING_DESTROYED(ws) || ws->batchop);
680     WRegion *other=NULL;
681
682     if(!norestore)
683         other=tiling_do_navi_next(ws, reg, REGION_NAVI_ANY, TRUE, FALSE);
684     
685     tiling_do_managed_remove(ws, reg);
686
687     if(node==(WSplitRegion*)(ws->stdispnode))
688         ws->stdispnode=NULL;
689     
690     if(node!=NULL){
691         bool reused=FALSE;
692         
693         if(other==NULL && !norestore){
694             WWindow *par=REGION_PARENT(ws);
695             WFitParams fp;
696             
697             assert(par!=NULL);
698             
699             fp.g=node->split.geom;
700             fp.mode=REGION_FIT_EXACT;
701             
702             other=(ws->create_frame_fn)(par, &fp);
703             
704             if(other!=NULL){
705                 node->reg=other;
706                 splittree_set_node_of(other, node);
707                 tiling_managed_add(ws, other);
708                 reused=TRUE;
709             }else{
710                 warn(TR("Tiling in useless state."));
711             }
712         }
713         
714         if(!reused)
715             splittree_remove((WSplit*)node, (!norestore && other!=NULL));
716     }
717     
718     if(!norestore && other!=NULL && act && mcf)
719         region_warp(other);
720 }
721
722
723 static bool mplexfilter(WSplit *node)
724 {
725     WSplitRegion *regnode=OBJ_CAST(node, WSplitRegion);
726     
727     return (regnode!=NULL && regnode->reg!=NULL &&
728             OBJ_IS(regnode->reg, WMPlex));
729 }
730
731
732 static WPHolder *find_ph_result=NULL;
733 static WRegion *find_ph_param=NULL;
734
735
736 static bool find_ph(WSplit *split)
737 {
738     WSplitRegion *sr=OBJ_CAST(split, WSplitRegion);
739
740     assert(find_ph_result==NULL);
741     
742     if(sr==NULL || sr->reg==NULL)
743         return FALSE;
744     
745     find_ph_result=region_get_rescue_pholder_for(sr->reg, find_ph_param);
746
747     return (find_ph_result!=NULL);
748 }
749
750
751 WPHolder *tiling_get_rescue_pholder_for(WTiling *ws, WRegion *mgd)
752 {
753     WSplit *node=(WSplit*)get_node_check(ws, mgd);
754     WPHolder *ph;
755     
756     find_ph_result=NULL;
757     find_ph_param=mgd;
758     
759     if(node==NULL){
760         if(ws->split_tree!=NULL){
761             split_current_todir(ws->split_tree, PRIMN_ANY, PRIMN_ANY, 
762                                 find_ph);
763         }
764     }else{
765         while(node!=NULL){
766             split_nextto(node, PRIMN_ANY, PRIMN_ANY, find_ph);
767             if(find_ph_result!=NULL)
768                 break;
769             node=(WSplit*)node->parent;
770         }
771     }
772     
773     ph=find_ph_result;
774     find_ph_result=NULL;
775     find_ph_param=NULL;
776      
777     return ph;
778 }
779
780
781 /*}}}*/
782
783
784 /*{{{ Navigation */
785
786
787 static void navi_to_primn(WRegionNavi nh, WPrimn *hprimn, WPrimn *vprimn, 
788                           WPrimn choice)
789 {
790     /* choice should be PRIMN_ANY or PRIMN_NONE */
791     
792     switch(nh){
793     case REGION_NAVI_BEG:
794         *vprimn=PRIMN_TL;
795         *hprimn=PRIMN_TL;
796         break;
797         
798     case REGION_NAVI_END:
799         *vprimn=PRIMN_BR;
800         *hprimn=PRIMN_BR;
801         break;
802         
803     case REGION_NAVI_LEFT:
804         *hprimn=PRIMN_TL;
805         *vprimn=choice;
806         break;
807         
808     case REGION_NAVI_RIGHT:
809         *hprimn=PRIMN_BR;
810         *vprimn=choice;
811         break;
812         
813     case REGION_NAVI_TOP:
814         *hprimn=choice;
815         *vprimn=PRIMN_TL;
816         break;
817         
818     case REGION_NAVI_BOTTOM:
819         *hprimn=choice;
820         *vprimn=PRIMN_BR;
821         break;
822         
823     default:
824     case REGION_NAVI_ANY:
825         *hprimn=PRIMN_ANY;
826         *vprimn=PRIMN_ANY;
827         break;
828     }
829 }
830
831
832 static WRegion *node_reg(WSplit *node)
833 {
834     WSplitRegion *rnode=OBJ_CAST(node, WSplitRegion);
835     return (rnode!=NULL ? rnode->reg : NULL);
836 }
837
838     
839 WRegion *tiling_do_navi_next(WTiling *ws, WRegion *reg, 
840                              WRegionNavi nh, bool nowrap,
841                              bool any)
842 {
843     WSplitFilter *filter=(any ? NULL : nostdispfilter);
844     WPrimn hprimn, vprimn;
845     WRegion *nxt=NULL;
846
847     navi_to_primn(nh, &hprimn, &vprimn, PRIMN_NONE);
848     
849     if(reg==NULL)
850         reg=tiling_current(ws);
851     
852     if(reg!=NULL){
853         WSplitRegion *node=get_node_check(ws, reg);
854         if(node!=NULL){
855             nxt=node_reg(split_nextto((WSplit*)node, hprimn, vprimn, 
856                                       filter));
857         }
858     }
859     
860     if(nxt==NULL && !nowrap){
861         nxt=node_reg(split_current_todir(ws->split_tree, 
862                                          primn_none2any(primn_invert(hprimn)),
863                                          primn_none2any(primn_invert(vprimn)),
864                                          filter));
865     }
866     
867     return nxt;
868 }
869
870
871 WRegion *tiling_do_navi_first(WTiling *ws, WRegionNavi nh, bool any)
872 {
873     WSplitFilter *filter=(any ? NULL : nostdispfilter);
874     WPrimn hprimn, vprimn;
875     
876     navi_to_primn(nh, &hprimn, &vprimn, PRIMN_ANY);
877
878     return node_reg(split_current_todir(ws->split_tree, 
879                                         hprimn, vprimn, filter));
880 }
881
882
883 WRegion *tiling_navi_next(WTiling *ws, WRegion *reg, 
884                           WRegionNavi nh, WRegionNaviData *data)
885 {
886     WRegion *nxt=tiling_do_navi_next(ws, reg, nh, TRUE, FALSE);
887     
888     return region_navi_cont(&ws->reg, nxt, data);
889 }
890
891
892 WRegion *tiling_navi_first(WTiling *ws, WRegionNavi nh,
893                            WRegionNaviData *data)
894 {
895     WRegion *reg=tiling_do_navi_first(ws, nh, FALSE);
896     
897     return region_navi_cont(&ws->reg, reg, data);
898 }
899
900
901 /*}}}*/
902
903
904 /*{{{ Split/unsplit */
905
906
907 static bool get_split_dir_primn(const char *str, int *dir, int *primn)
908 {
909     WPrimn hprimn, vprimn;
910     WRegionNavi nh;
911
912     if(!ioncore_string_to_navi(str, &nh))
913         return FALSE;
914     
915     navi_to_primn(nh, &hprimn, &vprimn, PRIMN_NONE);
916     
917     if(hprimn==PRIMN_NONE){
918         *dir=SPLIT_VERTICAL;
919         *primn=vprimn;
920     }else if(vprimn==PRIMN_NONE){
921         *dir=SPLIT_HORIZONTAL;
922         *primn=hprimn;
923     }else{
924         warn(TR("Invalid direction"));
925         return FALSE;
926     }
927     
928     return TRUE;
929 }
930
931
932 static bool get_split_dir_primn_float(const char *str, int *dir, int *primn,
933                                       bool *floating)
934 {
935     if(strncmp(str, "floating:", 9)==0){
936         *floating=TRUE;
937         return get_split_dir_primn(str+9, dir, primn);
938     }else{
939         *floating=FALSE;
940         return get_split_dir_primn(str, dir, primn);
941     }
942 }
943
944
945 #define SPLIT_MINS 16 /* totally arbitrary */
946
947
948 static WFrame *tiling_do_split(WTiling *ws, WSplit *node, 
949                               const char *dirstr, int minw, int minh)
950 {
951     int dir, primn, mins;
952     bool floating=FALSE;
953     WFrame *newframe;
954     WSplitRegion *nnode;
955     
956     if(node==NULL || ws->split_tree==NULL){
957         warn(TR("Invalid node."));
958         return NULL;
959     }
960     
961     if(!get_split_dir_primn_float(dirstr, &dir, &primn, &floating))
962         return NULL;
963     
964     mins=(dir==SPLIT_VERTICAL ? minh : minw);
965
966     if(!floating){
967         nnode=splittree_split(node, dir, primn, mins, 
968                               ws->create_frame_fn, 
969                               REGION_PARENT(ws));
970     }else{
971         nnode=splittree_split_floating(node, dir, primn, mins,
972                                        ws->create_frame_fn, ws);
973     }
974     
975     if(nnode==NULL){
976         warn(TR("Unable to split."));
977         return NULL;
978     }
979
980     /* We must restack here to ensure the split tree is stacked in the
981      * expected order.
982      */
983     if(ws->split_tree!=NULL)
984         split_restack(ws->split_tree, ws->dummywin, Above);
985
986     newframe=OBJ_CAST(nnode->reg, WFrame);
987     assert(newframe!=NULL);
988
989     if(!tiling_managed_add(ws, nnode->reg)){
990         nnode->reg=NULL;
991         destroy_obj((Obj*)nnode);
992         destroy_obj((Obj*)newframe);
993         return NULL;
994     }
995     
996     return newframe;
997 }
998
999
1000 /*EXTL_DOC
1001  * Create a new frame on \var{ws} \codestr{above}, \codestr{below}
1002  * \codestr{left} of, or \codestr{right} of \var{node} as indicated
1003  *  by \var{dirstr}. If \var{dirstr} is  prefixed with 
1004  * \codestr{floating:} a floating split is created.
1005  */
1006 EXTL_EXPORT_MEMBER
1007 WFrame *tiling_split(WTiling *ws, WSplit *node, const char *dirstr)
1008 {
1009     if(!check_node(ws, node))
1010         return NULL;
1011     
1012     return tiling_do_split(ws, node, dirstr,
1013                           SPLIT_MINS, SPLIT_MINS);
1014 }
1015
1016
1017 /*EXTL_DOC
1018  * Same as \fnref{WTiling.split} at the root of the split tree.
1019  */
1020 EXTL_EXPORT_MEMBER
1021 WFrame *tiling_split_top(WTiling *ws, const char *dirstr)
1022 {
1023     return tiling_do_split(ws, ws->split_tree, dirstr, 
1024                           SPLIT_MINS, SPLIT_MINS);
1025 }
1026
1027
1028 /*EXTL_DOC
1029  * Split \var{frame} creating a new frame to direction \var{dirstr}
1030  * (one of \codestr{left}, \codestr{right}, \codestr{top} or 
1031  * \codestr{bottom}) of \var{frame}.
1032  * If \var{attach_current} is set, the region currently displayed in
1033  * \var{frame}, if any, is moved to thenew frame.
1034  * If \var{dirstr} is prefixed with \codestr{floating:}, a floating
1035  * split is created.
1036  */
1037 EXTL_EXPORT_MEMBER
1038 WFrame *tiling_split_at(WTiling *ws, WFrame *frame, const char *dirstr, 
1039                        bool attach_current)
1040 {
1041     WRegion *curr;
1042     WSplitRegion *node;
1043     WFrame *newframe;
1044     
1045     if(frame==NULL)
1046         return NULL;
1047     
1048     node=get_node_check(ws, (WRegion*)frame);
1049
1050     newframe=tiling_do_split(ws, (WSplit*)node, dirstr, 
1051                             region_min_w((WRegion*)frame),
1052                             region_min_h((WRegion*)frame));
1053
1054     if(newframe==NULL)
1055         return NULL;
1056
1057     curr=mplex_mx_current(&(frame->mplex));
1058     
1059     if(attach_current && curr!=NULL)
1060         mplex_attach_simple(&(newframe->mplex), curr, MPLEX_ATTACH_SWITCHTO);
1061     
1062     if(region_may_control_focus((WRegion*)frame))
1063         region_goto((WRegion*)newframe);
1064
1065     return newframe;
1066 }
1067
1068
1069 /*EXTL_DOC
1070  * Try to relocate regions managed by \var{reg} to another frame
1071  * and, if possible, destroy it.
1072  */
1073 EXTL_EXPORT_MEMBER
1074 void tiling_unsplit_at(WTiling *ws, WRegion *reg)
1075 {
1076     WPHolder *ph;
1077     
1078     if(reg==NULL || REGION_MANAGER(reg)!=(WRegion*)ws)
1079         return;
1080     
1081     ph=region_get_rescue_pholder_for((WRegion*)ws, reg);
1082     
1083     if(ph!=NULL){
1084         region_rescue(reg, ph, REGION_RESCUE_NODEEP|REGION_RESCUE_PHFLAGS_OK);
1085         destroy_obj((Obj*)ph);
1086     }
1087     
1088     region_defer_rqdispose(reg);
1089 }
1090
1091
1092 /*}}}*/
1093
1094
1095 /*{{{ Navigation etc. exports */
1096
1097
1098 WRegion *tiling_current(WTiling *ws)
1099 {
1100     WSplitRegion *node=NULL;
1101     if(ws->split_tree!=NULL){
1102         node=(WSplitRegion*)split_current_todir(ws->split_tree, 
1103                                                 PRIMN_ANY, PRIMN_ANY, NULL);
1104     }
1105     return (node ? node->reg : NULL);
1106 }
1107
1108
1109 /*EXTL_DOC
1110  * Iterate over managed regions of \var{ws} until \var{iterfn} returns
1111  * \code{false}.
1112  * The function itself returns \code{true} if it reaches the end of list
1113  * without this happening.
1114  */
1115 EXTL_SAFE
1116 EXTL_EXPORT_MEMBER
1117 bool tiling_managed_i(WTiling *ws, ExtlFn iterfn)
1118 {
1119     PtrListIterTmp tmp;
1120     
1121     ptrlist_iter_init(&tmp, ws->managed_list);
1122     
1123     return extl_iter_objlist_(iterfn, (ObjIterator*)ptrlist_iter, &tmp);
1124 }
1125
1126
1127 /*EXTL_DOC
1128  * Returns the root of the split tree.
1129  */
1130 EXTL_SAFE
1131 EXTL_EXPORT_MEMBER
1132 WSplit *tiling_split_tree(WTiling *ws)
1133 {
1134     return ws->split_tree;
1135 }
1136
1137
1138 /*EXTL_DOC
1139  * Return the most previously active region next to \var{reg} in
1140  * direction \var{dirstr} (\codestr{left}, \codestr{right}, \codestr{up},
1141  * or \codestr{down}). The region \var{reg}
1142  * must be managed by \var{ws}. If \var{any} is not set, the status display
1143  * is not considered.
1144  */
1145 EXTL_SAFE
1146 EXTL_EXPORT_MEMBER
1147 WRegion *tiling_nextto(WTiling *ws, WRegion *reg, const char *dirstr,
1148                        bool any)
1149 {
1150     WRegionNavi nh;
1151     
1152     if(!ioncore_string_to_navi(dirstr, &nh))
1153         return NULL;
1154     
1155     return tiling_do_navi_next(ws, reg, nh, FALSE, any);
1156 }
1157
1158
1159 /*EXTL_DOC
1160  * Return the most previously active region on \var{ws} with no
1161  * other regions next to it in  direction \var{dirstr} 
1162  * (\codestr{left}, \codestr{right}, \codestr{up}, or \codestr{down}). 
1163  * If \var{any} is not set, the status display is not considered.
1164  */
1165 EXTL_SAFE
1166 EXTL_EXPORT_MEMBER
1167 WRegion *tiling_farthest(WTiling *ws, const char *dirstr, bool any)
1168 {
1169     WRegionNavi nh;
1170     
1171     if(!ioncore_string_to_navi(dirstr, &nh))
1172         return NULL;
1173     
1174     return tiling_do_navi_first(ws, nh, any);
1175 }
1176
1177
1178 /*EXTL_DOC
1179  * For region \var{reg} managed by \var{ws} return the \type{WSplit}
1180  * a leaf of which \var{reg} is.
1181  */
1182 EXTL_SAFE
1183 EXTL_EXPORT_MEMBER
1184 WSplitRegion *tiling_node_of(WTiling *ws, WRegion *reg)
1185 {
1186     if(reg==NULL){
1187         warn(TR("Nil parameter."));
1188         return NULL;
1189     }
1190     
1191     if(REGION_MANAGER(reg)!=(WRegion*)ws){
1192         warn(TR("Manager doesn't match."));
1193         return NULL;
1194     }
1195     
1196     return splittree_node_of(reg);
1197 }
1198
1199
1200 /*}}}*/
1201
1202
1203 /*{{{ Flip and transpose */
1204
1205
1206 static WSplitSplit *get_at_split(WTiling *ws, WRegion *reg)
1207 {
1208     WSplit *node;
1209     WSplitSplit *split;
1210     
1211     if(reg==NULL){
1212         split=OBJ_CAST(ws->split_tree, WSplitSplit);
1213         if(split==NULL)
1214             return NULL;
1215         else if(split->br==(WSplit*)ws->stdispnode)
1216             return OBJ_CAST(split->tl, WSplitSplit);
1217         else if(split->tl==(WSplit*)ws->stdispnode)
1218             return OBJ_CAST(split->br, WSplitSplit);
1219         else
1220             return split;
1221     }
1222     
1223     node=(WSplit*)get_node_check(ws, reg);
1224     
1225     if(node==NULL)
1226         return NULL;
1227     
1228     if(node==(WSplit*)ws->stdispnode){
1229         warn(TR("The status display is not a valid parameter for "
1230                 "this routine."));
1231         return NULL;
1232     }
1233         
1234     split=OBJ_CAST(node->parent, WSplitSplit);
1235     
1236     if(split!=NULL && (split->tl==(WSplit*)ws->stdispnode ||
1237                        split->br==(WSplit*)ws->stdispnode)){
1238         split=OBJ_CAST(((WSplit*)split)->parent, WSplitSplit);
1239     }
1240        
1241     return split;
1242 }
1243
1244
1245 /*EXTL_DOC
1246  * Flip \var{ws} at \var{reg} or root if nil.
1247  */
1248 EXTL_EXPORT_MEMBER
1249 bool iowns_flip_at(WTiling *ws, WRegion *reg)
1250 {
1251     WSplitSplit *split=get_at_split(ws, reg);
1252     
1253     if(split==NULL){
1254         return FALSE;
1255     }else{
1256         splitsplit_flip(split);
1257         return TRUE;
1258     }
1259 }
1260
1261
1262 /*EXTL_DOC
1263  * Transpose \var{ws} at \var{reg} or root if nil.
1264  */
1265 EXTL_EXPORT_MEMBER
1266 bool iowns_transpose_at(WTiling *ws, WRegion *reg)
1267 {
1268     WSplitSplit *split=get_at_split(ws, reg);
1269     
1270     if(split==NULL){
1271         return FALSE;
1272     }else{
1273         split_transpose((WSplit*)split);
1274         return TRUE;
1275     }
1276 }
1277
1278
1279 /*}}}*/
1280
1281
1282 /*{{{ Floating toggle */
1283
1284
1285 static void replace(WSplitSplit *split, WSplitSplit *nsplit)
1286 {
1287     WSplitInner *psplit=split->isplit.split.parent;
1288
1289     nsplit->tl=split->tl;
1290     split->tl=NULL;
1291     nsplit->tl->parent=(WSplitInner*)nsplit;
1292     
1293     nsplit->br=split->br;
1294     split->br=NULL;
1295     nsplit->br->parent=(WSplitInner*)nsplit;
1296     
1297     if(psplit!=NULL){
1298         splitinner_replace((WSplitInner*)psplit, (WSplit*)split, 
1299                            (WSplit*)nsplit);
1300     }else{
1301         splittree_changeroot((WSplit*)split, (WSplit*)nsplit);
1302     }
1303 }
1304
1305
1306 WSplitSplit *tiling_set_floating(WTiling *ws, WSplitSplit *split, int sp)
1307 {
1308     bool set=OBJ_IS(split, WSplitFloat);
1309     bool nset=libtu_do_setparam(sp, set);
1310     const WRectangle *g=&((WSplit*)split)->geom;
1311     WSplitSplit *ns;
1312     
1313     if(!XOR(nset, set))
1314         return split;
1315     
1316     if(nset){
1317         ns=(WSplitSplit*)create_splitfloat(g, ws, split->dir);
1318     }else{
1319         if(OBJ_IS(split->tl, WSplitST) || OBJ_IS(split->br, WSplitST)){
1320             warn(TR("Refusing to float split directly containing the "
1321                     "status display."));
1322             return NULL;
1323         }
1324         ns=create_splitsplit(g, split->dir);
1325     }
1326
1327     if(ns!=NULL){
1328         replace(split, ns);
1329         split_resize((WSplit*)ns, g, PRIMN_ANY, PRIMN_ANY);
1330         mainloop_defer_destroy((Obj*)split);
1331     }
1332     
1333     return ns;
1334 }
1335
1336
1337 /*EXTL_DOC
1338  * Toggle floating of a split's sides at \var{split} as indicated by the 
1339  * parameter \var{how} (\codestr{set}, \codestr{unset}, or \codestr{toggle}).
1340  * A split of the appropriate is returned, if there was a change.
1341  */
1342 EXTL_EXPORT_AS(WTiling, set_floating)
1343 WSplitSplit *tiling_set_floating_extl(WTiling *ws, WSplitSplit *split, 
1344                                      const char *how)
1345 {
1346     if(!check_node(ws, (WSplit*)split))
1347         return NULL;
1348     return tiling_set_floating(ws, split, libtu_string_to_setparam(how));
1349 }
1350
1351
1352 /*EXTL_DOC
1353  * Toggle floating of the sides of a split containin \var{reg} as indicated 
1354  * by the parameters \var{how} (\codestr{set}, \codestr{unset}, or 
1355  * \codestr{toggle}) and \var{dirstr} (\codestr{left}, \codestr{right}, 
1356  * \codestr{up}, or \codestr{down}). The new status is returned 
1357  * (and \code{false} also on error).
1358  */
1359 EXTL_EXPORT_AS(WTiling, set_floating_at)
1360 bool tiling_set_floating_at_extl(WTiling *ws, WRegion *reg, const char *how,
1361                                 const char *dirstr)
1362 {
1363     WPrimn hprimn=PRIMN_ANY, vprimn=PRIMN_ANY;
1364     WSplitSplit *split, *nsplit;
1365     WSplit *node;
1366     
1367     node=(WSplit*)get_node_check(ws, reg);
1368     if(node==NULL)
1369         return FALSE;
1370     
1371     
1372     if(dirstr!=NULL){
1373         WRegionNavi nh;
1374         
1375         if(!ioncore_string_to_navi(dirstr, &nh))
1376             return FALSE;
1377     
1378         navi_to_primn(nh, &hprimn, &vprimn, PRIMN_NONE);
1379     }
1380
1381     while(TRUE){
1382         split=OBJ_CAST(node->parent, WSplitSplit);
1383         if(split==NULL){
1384             warn(TR("No suitable split here."));
1385             return FALSE;
1386         }
1387
1388         if(!OBJ_IS(split->tl, WSplitST) && !OBJ_IS(split->br, WSplitST)){
1389             WPrimn tmp=(split->dir==SPLIT_VERTICAL ? vprimn : hprimn);
1390             if(tmp==PRIMN_ANY 
1391                || (node==split->tl && tmp==PRIMN_BR)
1392                || (node==split->br && tmp==PRIMN_TL)){
1393                 break;
1394             }
1395         }
1396         
1397         node=(WSplit*)split;
1398     }
1399     
1400     nsplit=tiling_set_floating(ws, split, libtu_string_to_setparam(how));
1401     
1402     return OBJ_IS((Obj*)(nsplit==NULL ? split : nsplit), WSplitFloat);
1403 }
1404
1405
1406 /*}}}*/
1407
1408
1409 /*{{{ Save */
1410
1411
1412 ExtlTab tiling_get_configuration(WTiling *ws)
1413 {
1414     ExtlTab tab, split_tree=extl_table_none();
1415     
1416     tab=region_get_base_configuration((WRegion*)ws);
1417     
1418     if(ws->split_tree!=NULL){
1419         if(!split_get_config(ws->split_tree, &split_tree))
1420             warn(TR("Could not get split tree."));
1421     }
1422     
1423     extl_table_sets_t(tab, "split_tree", split_tree);
1424     extl_unref_table(split_tree);
1425     
1426     return tab;
1427 }
1428
1429
1430 /*}}}*/
1431
1432
1433 /*{{{ Load */
1434
1435
1436 WSplit *load_splitst(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1437 {
1438     WSplitST *st;
1439
1440     if(ws->stdispnode!=NULL){
1441         warn(TR("Workspace already has a status display node."));
1442         return NULL;
1443     }
1444
1445     st=create_splitst(geom, NULL);
1446     ws->stdispnode=st;
1447     return (WSplit*)st;
1448 }
1449
1450
1451 static bool do_attach(WTiling *ws, WRegion *reg, void *p)
1452 {
1453     WSplitRegion *node=create_splitregion(&REGION_GEOM(reg), reg);
1454     
1455     if(node==NULL)
1456         return FALSE;
1457             
1458     if(!tiling_managed_add(ws, reg)){
1459         node->reg=NULL;
1460         destroy_obj((Obj*)node);
1461         return FALSE;
1462     }
1463     
1464     *(WSplitRegion**)p=node;
1465     
1466     return TRUE;
1467 }
1468
1469
1470 WSplit *load_splitregion(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1471 {
1472     WWindow *par=REGION_PARENT(ws);
1473     WRegionAttachData data;
1474     WSplit *node=NULL;
1475     WFitParams fp;
1476     ExtlTab rt;
1477     
1478     if(!extl_table_gets_t(tab, "regparams", &rt)){
1479         warn(TR("Missing region parameters."));
1480         return NULL;
1481     }
1482     
1483     data.type=REGION_ATTACH_LOAD;
1484     data.u.tab=rt;
1485     
1486     assert(par!=NULL);
1487     fp.g=*geom;
1488     fp.mode=REGION_FIT_EXACT;
1489     
1490     region_attach_helper((WRegion*)ws, par, &fp, 
1491                          (WRegionDoAttachFn*)do_attach, &node, &data);
1492
1493     extl_unref_table(rt);
1494     
1495     return node;
1496 }
1497
1498
1499 #define MINS 1
1500
1501 WSplit *load_splitsplit(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1502 {
1503     WSplit *tl=NULL, *br=NULL;
1504     WSplitSplit *split;
1505     char *dir_str;
1506     int dir, brs, tls;
1507     ExtlTab subtab;
1508     WRectangle geom2;
1509     int set=0;
1510
1511     set+=(extl_table_gets_i(tab, "tls", &tls)==TRUE);
1512     set+=(extl_table_gets_i(tab, "brs", &brs)==TRUE);
1513     set+=(extl_table_gets_s(tab, "dir", &dir_str)==TRUE);
1514     
1515     if(set!=3)
1516         return NULL;
1517     
1518     if(strcmp(dir_str, "vertical")==0){
1519         dir=SPLIT_VERTICAL;
1520     }else if(strcmp(dir_str, "horizontal")==0){
1521         dir=SPLIT_HORIZONTAL;
1522     }else{
1523         warn(TR("Invalid direction."));
1524         free(dir_str);
1525         return NULL;
1526     }
1527     free(dir_str);
1528
1529     split=create_splitsplit(geom, dir);
1530     if(split==NULL)
1531         return NULL;
1532
1533     tls=maxof(tls, MINS);
1534     brs=maxof(brs, MINS);
1535         
1536     geom2=*geom;
1537     if(dir==SPLIT_HORIZONTAL){
1538         tls=maxof(0, geom->w)*tls/(tls+brs);
1539         geom2.w=tls;
1540     }else{
1541         tls=maxof(0, geom->h)*tls/(tls+brs);
1542         geom2.h=tls;
1543     }
1544     
1545     if(extl_table_gets_t(tab, "tl", &subtab)){
1546         tl=tiling_load_node(ws, &geom2, subtab);
1547         extl_unref_table(subtab);
1548     }
1549
1550     geom2=*geom;
1551     if(dir==SPLIT_HORIZONTAL){
1552         geom2.w-=tls;
1553         geom2.x+=tls;
1554     }else{
1555         geom2.h-=tls;
1556         geom2.y+=tls;
1557     }
1558             
1559     if(extl_table_gets_t(tab, "br", &subtab)){
1560         br=tiling_load_node(ws, &geom2, subtab);
1561         extl_unref_table(subtab);
1562     }
1563     
1564     if(tl==NULL || br==NULL){
1565         /* PRIMN_TL/BR instead of ANY because of stdisp. */
1566         destroy_obj((Obj*)split);
1567         if(tl!=NULL){
1568             split_do_resize(tl, geom, PRIMN_BR, PRIMN_BR, FALSE);
1569             return tl;
1570         }
1571         if(br!=NULL){
1572             split_do_resize(br, geom, PRIMN_TL, PRIMN_TL, FALSE);
1573             return br;
1574         }
1575         return NULL;
1576     }
1577     
1578     tl->parent=(WSplitInner*)split;
1579     br->parent=(WSplitInner*)split;
1580
1581     /*split->tmpsize=tls;*/
1582     split->tl=tl;
1583     split->br=br;
1584     
1585     return (WSplit*)split;
1586 }
1587
1588
1589 WSplit *tiling_load_node_default(WTiling *ws, const WRectangle *geom, 
1590                                 ExtlTab tab)
1591 {
1592     char *typestr=NULL;
1593     WSplit *node=NULL;
1594
1595     extl_table_gets_s(tab, "type", &typestr);
1596     
1597     if(typestr==NULL){
1598         warn(TR("No split type given."));
1599         return NULL;
1600     }
1601     
1602     if(strcmp(typestr, "WSplitRegion")==0)
1603         node=load_splitregion(ws, geom, tab);
1604     else if(strcmp(typestr, "WSplitSplit")==0)
1605         node=load_splitsplit(ws, geom, tab);
1606     else if(strcmp(typestr, "WSplitFloat")==0)
1607         node=load_splitfloat(ws, geom, tab);
1608     else if(strcmp(typestr, "WSplitST")==0)
1609         node=NULL;/*load_splitst(ws, geom, tab);*/
1610     else
1611         warn(TR("Unknown split type."));
1612     
1613     free(typestr);
1614     
1615     return node;
1616 }
1617
1618
1619 WSplit *tiling_load_node(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1620 {
1621     WSplit *ret=NULL;
1622     CALL_DYN_RET(ret, WSplit*, tiling_load_node, ws, (ws, geom, tab));
1623     return ret;
1624 }
1625
1626
1627
1628 WRegion *tiling_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
1629 {
1630     WTiling *ws;
1631     ExtlTab treetab;
1632     bool ci=TRUE;
1633
1634     if(extl_table_gets_t(tab, "split_tree", &treetab))
1635         ci=FALSE;
1636     
1637     ws=create_tiling(par, fp, NULL, ci);
1638     
1639     if(ws==NULL){
1640         if(!ci)
1641             extl_unref_table(treetab);
1642         return NULL;
1643     }
1644
1645     if(!ci){
1646         ws->split_tree=tiling_load_node(ws, &REGION_GEOM(ws), treetab);
1647         extl_unref_table(treetab);
1648     }
1649     
1650     if(ws->split_tree==NULL){
1651         warn(TR("The workspace is empty."));
1652         destroy_obj((Obj*)ws);
1653         return NULL;
1654     }
1655     
1656     ws->split_tree->ws_if_root=ws;
1657     split_restack(ws->split_tree, ws->dummywin, Above);
1658     
1659     return (WRegion*)ws;
1660 }
1661
1662
1663 /*}}}*/
1664
1665
1666 /*{{{ Dynamic function table and class implementation */
1667
1668
1669 static DynFunTab tiling_dynfuntab[]={
1670     {region_map, 
1671      tiling_map},
1672     
1673     {region_unmap, 
1674      tiling_unmap},
1675     
1676     {region_do_set_focus, 
1677      tiling_do_set_focus},
1678     
1679     {(DynFun*)region_fitrep,
1680      (DynFun*)tiling_fitrep},
1681     
1682     {region_managed_rqgeom, 
1683      tiling_managed_rqgeom},
1684     
1685     {region_managed_remove, 
1686      tiling_managed_remove},
1687     
1688     {(DynFun*)region_managed_prepare_focus,
1689      (DynFun*)tiling_managed_prepare_focus},
1690     
1691     {(DynFun*)region_prepare_manage, 
1692      (DynFun*)tiling_prepare_manage},
1693     
1694     {(DynFun*)region_rescue_clientwins,
1695      (DynFun*)tiling_rescue_clientwins},
1696
1697     {(DynFun*)region_get_rescue_pholder_for,
1698      (DynFun*)tiling_get_rescue_pholder_for},
1699     
1700     {(DynFun*)region_get_configuration,
1701      (DynFun*)tiling_get_configuration},
1702
1703     {(DynFun*)region_managed_disposeroot,
1704      (DynFun*)tiling_managed_disposeroot},
1705
1706     {(DynFun*)region_current,
1707      (DynFun*)tiling_current},
1708
1709     {(DynFun*)tiling_managed_add,
1710      (DynFun*)tiling_managed_add_default},
1711     
1712     {region_manage_stdisp,
1713      tiling_manage_stdisp},
1714
1715     {region_unmanage_stdisp,
1716      tiling_unmanage_stdisp},
1717     
1718     {(DynFun*)tiling_load_node,
1719      (DynFun*)tiling_load_node_default},
1720             
1721     {region_restack,
1722      tiling_restack},
1723
1724     {region_stacking,
1725      tiling_stacking},
1726     
1727     {(DynFun*)region_navi_first,
1728      (DynFun*)tiling_navi_first},
1729     
1730     {(DynFun*)region_navi_next,
1731      (DynFun*)tiling_navi_next},
1732     
1733     {(DynFun*)region_xwindow,
1734      (DynFun*)tiling_xwindow},
1735     
1736     END_DYNFUNTAB
1737 };
1738
1739
1740 EXTL_EXPORT
1741 IMPLCLASS(WTiling, WRegion, tiling_deinit, tiling_dynfuntab);
1742
1743     
1744 /*}}}*/
1745