]> git.decadent.org.uk Git - ion3.git/blob - mod_tiling/tiling.c
[svn-upgrade] Integrating new upstream version, ion3 (20070720)
[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         WFrameMode mode=frame_mode(frame);
482         if(mode!=FRAME_MODE_TILED && mode!=FRAME_MODE_TILED_ALT)
483             frame_set_mode(frame, FRAME_MODE_TILED);
484     }
485     
486     if(REGION_IS_MAPPED(ws))
487         region_map(reg);
488     
489     if(region_may_control_focus((WRegion*)ws)){
490         WRegion *curr=tiling_current(ws);
491         if(curr==NULL || !REGION_IS_ACTIVE(curr))
492             region_warp(reg);
493     }
494     
495     return TRUE;
496 }
497
498
499 bool tiling_managed_add(WTiling *ws, WRegion *reg)
500 {
501     bool ret=FALSE;
502     CALL_DYN_RET(ret, bool, tiling_managed_add, ws, (ws, reg));
503     return ret;
504 }
505
506
507 bool tiling_do_attach_initial(WTiling *ws, WRegion *reg)
508 {
509     assert(ws->split_tree==NULL);
510     
511     ws->split_tree=(WSplit*)create_splitregion(&REGION_GEOM(reg), reg);
512     if(ws->split_tree==NULL)
513         return FALSE;
514     
515     ws->split_tree->ws_if_root=ws;
516     
517     if(!tiling_managed_add(ws, reg)){
518         destroy_obj((Obj*)ws->split_tree);
519         ws->split_tree=NULL;
520         return FALSE;
521     }
522     
523     return TRUE;
524 }
525                                       
526
527 static WRegion *create_frame_tiling(WWindow *parent, const WFitParams *fp)
528 {
529     return (WRegion*)create_frame(parent, fp, FRAME_MODE_TILED);
530 }
531
532
533 bool tiling_init(WTiling *ws, WWindow *parent, const WFitParams *fp, 
534                 WRegionSimpleCreateFn *create_frame_fn, bool ci)
535 {
536     ws->split_tree=NULL;
537     ws->create_frame_fn=(create_frame_fn 
538                          ? create_frame_fn
539                          : create_frame_tiling);
540     ws->stdispnode=NULL;
541     ws->managed_list=NULL;
542     ws->batchop=FALSE;
543     
544     ws->dummywin=XCreateWindow(ioncore_g.dpy, parent->win,
545                                 fp->g.x, fp->g.y, 1, 1, 0,
546                                 CopyFromParent, InputOnly,
547                                 CopyFromParent, 0, NULL);
548     if(ws->dummywin==None)
549         return FALSE;
550
551     region_init(&(ws->reg), parent, fp);
552
553     ws->reg.flags|=(REGION_GRAB_ON_PARENT|
554                     REGION_PLEASE_WARP);
555     
556     if(ci){
557         WRegionAttachData data;
558         WRegion *res;
559         
560         data.type=REGION_ATTACH_NEW;
561         data.u.n.fn=(WRegionCreateFn*)ws->create_frame_fn;
562         data.u.n.param=NULL;
563     
564         res=region_attach_helper((WRegion*)ws, parent, fp,
565                                  (WRegionDoAttachFn*)tiling_do_attach_initial, 
566                                  NULL, &data);
567
568         if(res==NULL){
569             XDestroyWindow(ioncore_g.dpy, ws->dummywin);
570             return FALSE;
571         }
572     }
573     
574     XSelectInput(ioncore_g.dpy, ws->dummywin,
575                  FocusChangeMask|KeyPressMask|KeyReleaseMask|
576                  ButtonPressMask|ButtonReleaseMask);
577     XSaveContext(ioncore_g.dpy, ws->dummywin, ioncore_g.win_context,
578                  (XPointer)ws);
579     
580     region_register(&(ws->reg));
581     region_add_bindmap((WRegion*)ws, mod_tiling_tiling_bindmap);
582     
583     return TRUE;
584 }
585
586
587 WTiling *create_tiling(WWindow *parent, const WFitParams *fp, 
588                      WRegionSimpleCreateFn *create_frame_fn, bool ci)
589 {
590     CREATEOBJ_IMPL(WTiling, tiling, (p, parent, fp, create_frame_fn, ci));
591 }
592
593
594 WTiling *create_tiling_simple(WWindow *parent, const WFitParams *fp)
595 {
596     return create_tiling(parent, fp, NULL, TRUE);
597 }
598
599
600 void tiling_deinit(WTiling *ws)
601 {
602     WRegion *reg;
603     WTilingIterTmp tmp;
604     WMPlex *remanage_mplex=NULL;
605     
606     tiling_unmanage_stdisp(ws, FALSE, TRUE);
607
608     FOR_ALL_MANAGED_BY_TILING(reg, ws, tmp){
609         destroy_obj((Obj*)reg);
610     }
611
612     FOR_ALL_MANAGED_BY_TILING(reg, ws, tmp){
613         assert(FALSE);
614     }
615     
616     if(ws->split_tree!=NULL)
617         destroy_obj((Obj*)(ws->split_tree));
618
619     XDeleteContext(ioncore_g.dpy, ws->dummywin, ioncore_g.win_context);
620     XDestroyWindow(ioncore_g.dpy, ws->dummywin);
621     ws->dummywin=None;
622
623     region_deinit(&(ws->reg));
624 }
625
626
627 WRegion *tiling_managed_disposeroot(WTiling *ws, WRegion *reg)
628 {
629     WTilingIterTmp tmp;
630     WRegion *mgd;
631     
632     if(ws->batchop)
633         return reg;
634     
635     FOR_ALL_MANAGED_BY_TILING(mgd, ws, tmp){
636         if(mgd!=TILING_STDISP_OF(ws) && mgd!=reg)
637             return reg;
638     }
639     
640     return region_disposeroot((WRegion*)ws);
641 }
642
643
644 bool tiling_rescue_clientwins(WTiling *ws, WRescueInfo *info)
645 {
646     WTilingIterTmp tmp;
647     
648     ptrlist_iter_init(&tmp, ws->managed_list);
649     
650     return region_rescue_some_clientwins((WRegion*)ws, info,
651                                          (WRegionIterator*)ptrlist_iter, 
652                                          &tmp);
653 }
654
655
656 void tiling_do_managed_remove(WTiling *ws, WRegion *reg)
657 {
658     if(TILING_STDISP_OF(ws)==reg){
659         ws->stdispnode->regnode.reg=NULL;
660     }else{
661         ptrlist_remove(&(ws->managed_list), reg);
662     }
663
664     region_unset_manager(reg, (WRegion*)ws);
665     splittree_set_node_of(reg, NULL);
666 }
667
668
669 static bool nostdispfilter(WSplit *node)
670 {
671     return (OBJ_IS(node, WSplitRegion) && !OBJ_IS(node, WSplitST));
672 }
673
674
675 void tiling_managed_remove(WTiling *ws, WRegion *reg)
676 {
677     bool act=REGION_IS_ACTIVE(reg);
678     bool mcf=region_may_control_focus((WRegion*)ws);
679     WSplitRegion *node=get_node_check(ws, reg);
680     bool norestore=(OBJ_IS_BEING_DESTROYED(ws) || ws->batchop);
681     WRegion *other=NULL;
682
683     if(!norestore)
684         other=tiling_do_navi_next(ws, reg, REGION_NAVI_ANY, TRUE, FALSE);
685     
686     tiling_do_managed_remove(ws, reg);
687
688     if(node==(WSplitRegion*)(ws->stdispnode))
689         ws->stdispnode=NULL;
690     
691     if(node!=NULL){
692         bool reused=FALSE;
693         
694         if(other==NULL && !norestore){
695             WWindow *par=REGION_PARENT(ws);
696             WFitParams fp;
697             
698             assert(par!=NULL);
699             
700             fp.g=node->split.geom;
701             fp.mode=REGION_FIT_EXACT;
702             
703             other=(ws->create_frame_fn)(par, &fp);
704             
705             if(other!=NULL){
706                 node->reg=other;
707                 splittree_set_node_of(other, node);
708                 tiling_managed_add(ws, other);
709                 reused=TRUE;
710             }else{
711                 warn(TR("Tiling in useless state."));
712             }
713         }
714         
715         if(!reused)
716             splittree_remove((WSplit*)node, (!norestore && other!=NULL));
717     }
718     
719     if(!norestore && other!=NULL && act && mcf)
720         region_warp(other);
721 }
722
723
724 static bool mplexfilter(WSplit *node)
725 {
726     WSplitRegion *regnode=OBJ_CAST(node, WSplitRegion);
727     
728     return (regnode!=NULL && regnode->reg!=NULL &&
729             OBJ_IS(regnode->reg, WMPlex));
730 }
731
732
733 static WPHolder *find_ph_result=NULL;
734 static WRegion *find_ph_param=NULL;
735
736
737 static bool find_ph(WSplit *split)
738 {
739     WSplitRegion *sr=OBJ_CAST(split, WSplitRegion);
740
741     assert(find_ph_result==NULL);
742     
743     if(sr==NULL || sr->reg==NULL)
744         return FALSE;
745     
746     find_ph_result=region_get_rescue_pholder_for(sr->reg, find_ph_param);
747
748     return (find_ph_result!=NULL);
749 }
750
751
752 WPHolder *tiling_get_rescue_pholder_for(WTiling *ws, WRegion *mgd)
753 {
754     WSplit *node=(WSplit*)get_node_check(ws, mgd);
755     WPHolder *ph;
756     
757     find_ph_result=NULL;
758     find_ph_param=mgd;
759     
760     if(node==NULL){
761         if(ws->split_tree!=NULL){
762             split_current_todir(ws->split_tree, PRIMN_ANY, PRIMN_ANY, 
763                                 find_ph);
764         }
765     }else{
766         while(node!=NULL){
767             split_nextto(node, PRIMN_ANY, PRIMN_ANY, find_ph);
768             if(find_ph_result!=NULL)
769                 break;
770             node=(WSplit*)node->parent;
771         }
772     }
773     
774     ph=find_ph_result;
775     find_ph_result=NULL;
776     find_ph_param=NULL;
777      
778     return ph;
779 }
780
781
782 /*}}}*/
783
784
785 /*{{{ Navigation */
786
787
788 static void navi_to_primn(WRegionNavi nh, WPrimn *hprimn, WPrimn *vprimn, 
789                           WPrimn choice)
790 {
791     /* choice should be PRIMN_ANY or PRIMN_NONE */
792     
793     switch(nh){
794     case REGION_NAVI_BEG:
795         *vprimn=PRIMN_TL;
796         *hprimn=PRIMN_TL;
797         break;
798         
799     case REGION_NAVI_END:
800         *vprimn=PRIMN_BR;
801         *hprimn=PRIMN_BR;
802         break;
803         
804     case REGION_NAVI_LEFT:
805         *hprimn=PRIMN_TL;
806         *vprimn=choice;
807         break;
808         
809     case REGION_NAVI_RIGHT:
810         *hprimn=PRIMN_BR;
811         *vprimn=choice;
812         break;
813         
814     case REGION_NAVI_TOP:
815         *hprimn=choice;
816         *vprimn=PRIMN_TL;
817         break;
818         
819     case REGION_NAVI_BOTTOM:
820         *hprimn=choice;
821         *vprimn=PRIMN_BR;
822         break;
823         
824     default:
825     case REGION_NAVI_ANY:
826         *hprimn=PRIMN_ANY;
827         *vprimn=PRIMN_ANY;
828         break;
829     }
830 }
831
832
833 static WRegion *node_reg(WSplit *node)
834 {
835     WSplitRegion *rnode=OBJ_CAST(node, WSplitRegion);
836     return (rnode!=NULL ? rnode->reg : NULL);
837 }
838
839     
840 WRegion *tiling_do_navi_next(WTiling *ws, WRegion *reg, 
841                              WRegionNavi nh, bool nowrap,
842                              bool any)
843 {
844     WSplitFilter *filter=(any ? NULL : nostdispfilter);
845     WPrimn hprimn, vprimn;
846     WRegion *nxt=NULL;
847
848     navi_to_primn(nh, &hprimn, &vprimn, PRIMN_NONE);
849     
850     if(reg==NULL)
851         reg=tiling_current(ws);
852     
853     if(reg!=NULL){
854         WSplitRegion *node=get_node_check(ws, reg);
855         if(node!=NULL){
856             nxt=node_reg(split_nextto((WSplit*)node, hprimn, vprimn, 
857                                       filter));
858         }
859     }
860     
861     if(nxt==NULL && !nowrap){
862         nxt=node_reg(split_current_todir(ws->split_tree, 
863                                          primn_none2any(primn_invert(hprimn)),
864                                          primn_none2any(primn_invert(vprimn)),
865                                          filter));
866     }
867     
868     return nxt;
869 }
870
871
872 WRegion *tiling_do_navi_first(WTiling *ws, WRegionNavi nh, bool any)
873 {
874     WSplitFilter *filter=(any ? NULL : nostdispfilter);
875     WPrimn hprimn, vprimn;
876     
877     navi_to_primn(nh, &hprimn, &vprimn, PRIMN_ANY);
878
879     return node_reg(split_current_todir(ws->split_tree, 
880                                         hprimn, vprimn, filter));
881 }
882
883
884 WRegion *tiling_navi_next(WTiling *ws, WRegion *reg, 
885                           WRegionNavi nh, WRegionNaviData *data)
886 {
887     WRegion *nxt=tiling_do_navi_next(ws, reg, nh, TRUE, FALSE);
888     
889     return region_navi_cont(&ws->reg, nxt, data);
890 }
891
892
893 WRegion *tiling_navi_first(WTiling *ws, WRegionNavi nh,
894                            WRegionNaviData *data)
895 {
896     WRegion *reg=tiling_do_navi_first(ws, nh, FALSE);
897     
898     return region_navi_cont(&ws->reg, reg, data);
899 }
900
901
902 /*}}}*/
903
904
905 /*{{{ Split/unsplit */
906
907
908 static bool get_split_dir_primn(const char *str, int *dir, int *primn)
909 {
910     WPrimn hprimn, vprimn;
911     WRegionNavi nh;
912
913     if(!ioncore_string_to_navi(str, &nh))
914         return FALSE;
915     
916     navi_to_primn(nh, &hprimn, &vprimn, PRIMN_NONE);
917     
918     if(hprimn==PRIMN_NONE){
919         *dir=SPLIT_VERTICAL;
920         *primn=vprimn;
921     }else if(vprimn==PRIMN_NONE){
922         *dir=SPLIT_HORIZONTAL;
923         *primn=hprimn;
924     }else{
925         warn(TR("Invalid direction"));
926         return FALSE;
927     }
928     
929     return TRUE;
930 }
931
932
933 static bool get_split_dir_primn_float(const char *str, int *dir, int *primn,
934                                       bool *floating)
935 {
936     if(strncmp(str, "floating:", 9)==0){
937         *floating=TRUE;
938         return get_split_dir_primn(str+9, dir, primn);
939     }else{
940         *floating=FALSE;
941         return get_split_dir_primn(str, dir, primn);
942     }
943 }
944
945
946 #define SPLIT_MINS 16 /* totally arbitrary */
947
948
949 static WFrame *tiling_do_split(WTiling *ws, WSplit *node, 
950                               const char *dirstr, int minw, int minh)
951 {
952     int dir, primn, mins;
953     bool floating=FALSE;
954     WFrame *newframe;
955     WSplitRegion *nnode;
956     
957     if(node==NULL || ws->split_tree==NULL){
958         warn(TR("Invalid node."));
959         return NULL;
960     }
961     
962     if(!get_split_dir_primn_float(dirstr, &dir, &primn, &floating))
963         return NULL;
964     
965     mins=(dir==SPLIT_VERTICAL ? minh : minw);
966
967     if(!floating){
968         nnode=splittree_split(node, dir, primn, mins, 
969                               ws->create_frame_fn, 
970                               REGION_PARENT(ws));
971     }else{
972         nnode=splittree_split_floating(node, dir, primn, mins,
973                                        ws->create_frame_fn, ws);
974     }
975     
976     if(nnode==NULL){
977         warn(TR("Unable to split."));
978         return NULL;
979     }
980
981     /* We must restack here to ensure the split tree is stacked in the
982      * expected order.
983      */
984     if(ws->split_tree!=NULL)
985         split_restack(ws->split_tree, ws->dummywin, Above);
986
987     newframe=OBJ_CAST(nnode->reg, WFrame);
988     assert(newframe!=NULL);
989
990     if(!tiling_managed_add(ws, nnode->reg)){
991         nnode->reg=NULL;
992         destroy_obj((Obj*)nnode);
993         destroy_obj((Obj*)newframe);
994         return NULL;
995     }
996
997     /* Restack */
998     if(ws->split_tree!=NULL)
999         split_restack(ws->split_tree, ws->dummywin, Above);
1000     
1001     return newframe;
1002 }
1003
1004
1005 /*EXTL_DOC
1006  * Create a new frame on \var{ws} \codestr{above}, \codestr{below}
1007  * \codestr{left} of, or \codestr{right} of \var{node} as indicated
1008  *  by \var{dirstr}. If \var{dirstr} is  prefixed with 
1009  * \codestr{floating:} a floating split is created.
1010  */
1011 EXTL_EXPORT_MEMBER
1012 WFrame *tiling_split(WTiling *ws, WSplit *node, const char *dirstr)
1013 {
1014     if(!check_node(ws, node))
1015         return NULL;
1016     
1017     return tiling_do_split(ws, node, dirstr,
1018                           SPLIT_MINS, SPLIT_MINS);
1019 }
1020
1021
1022 /*EXTL_DOC
1023  * Same as \fnref{WTiling.split} at the root of the split tree.
1024  */
1025 EXTL_EXPORT_MEMBER
1026 WFrame *tiling_split_top(WTiling *ws, const char *dirstr)
1027 {
1028     return tiling_do_split(ws, ws->split_tree, dirstr, 
1029                           SPLIT_MINS, SPLIT_MINS);
1030 }
1031
1032
1033 /*EXTL_DOC
1034  * Split \var{frame} creating a new frame to direction \var{dirstr}
1035  * (one of \codestr{left}, \codestr{right}, \codestr{top} or 
1036  * \codestr{bottom}) of \var{frame}.
1037  * If \var{attach_current} is set, the region currently displayed in
1038  * \var{frame}, if any, is moved to thenew frame.
1039  * If \var{dirstr} is prefixed with \codestr{floating:}, a floating
1040  * split is created.
1041  */
1042 EXTL_EXPORT_MEMBER
1043 WFrame *tiling_split_at(WTiling *ws, WFrame *frame, const char *dirstr, 
1044                        bool attach_current)
1045 {
1046     WRegion *curr;
1047     WSplitRegion *node;
1048     WFrame *newframe;
1049     
1050     if(frame==NULL)
1051         return NULL;
1052     
1053     node=get_node_check(ws, (WRegion*)frame);
1054
1055     newframe=tiling_do_split(ws, (WSplit*)node, dirstr, 
1056                             region_min_w((WRegion*)frame),
1057                             region_min_h((WRegion*)frame));
1058
1059     if(newframe==NULL)
1060         return NULL;
1061
1062     curr=mplex_mx_current(&(frame->mplex));
1063     
1064     if(attach_current && curr!=NULL)
1065         mplex_attach_simple(&(newframe->mplex), curr, MPLEX_ATTACH_SWITCHTO);
1066     
1067     if(region_may_control_focus((WRegion*)frame))
1068         region_goto((WRegion*)newframe);
1069
1070     return newframe;
1071 }
1072
1073
1074 /*EXTL_DOC
1075  * Try to relocate regions managed by \var{reg} to another frame
1076  * and, if possible, destroy it.
1077  */
1078 EXTL_EXPORT_MEMBER
1079 void tiling_unsplit_at(WTiling *ws, WRegion *reg)
1080 {
1081     WPHolder *ph;
1082     
1083     if(reg==NULL || REGION_MANAGER(reg)!=(WRegion*)ws)
1084         return;
1085     
1086     ph=region_get_rescue_pholder_for((WRegion*)ws, reg);
1087     
1088     if(ph!=NULL){
1089         region_rescue(reg, ph);
1090         destroy_obj((Obj*)ph);
1091     }
1092     
1093     region_defer_rqdispose(reg);
1094 }
1095
1096
1097 /*}}}*/
1098
1099
1100 /*{{{ Navigation etc. exports */
1101
1102
1103 WRegion *tiling_current(WTiling *ws)
1104 {
1105     WSplitRegion *node=NULL;
1106     if(ws->split_tree!=NULL){
1107         node=(WSplitRegion*)split_current_todir(ws->split_tree, 
1108                                                 PRIMN_ANY, PRIMN_ANY, NULL);
1109     }
1110     return (node ? node->reg : NULL);
1111 }
1112
1113
1114 /*EXTL_DOC
1115  * Iterate over managed regions of \var{ws} until \var{iterfn} returns
1116  * \code{false}.
1117  * The function itself returns \code{true} if it reaches the end of list
1118  * without this happening.
1119  */
1120 EXTL_SAFE
1121 EXTL_EXPORT_MEMBER
1122 bool tiling_managed_i(WTiling *ws, ExtlFn iterfn)
1123 {
1124     PtrListIterTmp tmp;
1125     
1126     ptrlist_iter_init(&tmp, ws->managed_list);
1127     
1128     return extl_iter_objlist_(iterfn, (ObjIterator*)ptrlist_iter, &tmp);
1129 }
1130
1131
1132 /*EXTL_DOC
1133  * Returns the root of the split tree.
1134  */
1135 EXTL_SAFE
1136 EXTL_EXPORT_MEMBER
1137 WSplit *tiling_split_tree(WTiling *ws)
1138 {
1139     return ws->split_tree;
1140 }
1141
1142
1143 /*EXTL_DOC
1144  * Return the most previously active region next to \var{reg} in
1145  * direction \var{dirstr} (\codestr{left}, \codestr{right}, \codestr{up},
1146  * or \codestr{down}). The region \var{reg}
1147  * must be managed by \var{ws}. If \var{any} is not set, the status display
1148  * is not considered.
1149  */
1150 EXTL_SAFE
1151 EXTL_EXPORT_MEMBER
1152 WRegion *tiling_nextto(WTiling *ws, WRegion *reg, const char *dirstr,
1153                        bool any)
1154 {
1155     WRegionNavi nh;
1156     
1157     if(!ioncore_string_to_navi(dirstr, &nh))
1158         return NULL;
1159     
1160     return tiling_do_navi_next(ws, reg, nh, FALSE, any);
1161 }
1162
1163
1164 /*EXTL_DOC
1165  * Return the most previously active region on \var{ws} with no
1166  * other regions next to it in  direction \var{dirstr} 
1167  * (\codestr{left}, \codestr{right}, \codestr{up}, or \codestr{down}). 
1168  * If \var{any} is not set, the status display is not considered.
1169  */
1170 EXTL_SAFE
1171 EXTL_EXPORT_MEMBER
1172 WRegion *tiling_farthest(WTiling *ws, const char *dirstr, bool any)
1173 {
1174     WRegionNavi nh;
1175     
1176     if(!ioncore_string_to_navi(dirstr, &nh))
1177         return NULL;
1178     
1179     return tiling_do_navi_first(ws, nh, any);
1180 }
1181
1182
1183 /*EXTL_DOC
1184  * For region \var{reg} managed by \var{ws} return the \type{WSplit}
1185  * a leaf of which \var{reg} is.
1186  */
1187 EXTL_SAFE
1188 EXTL_EXPORT_MEMBER
1189 WSplitRegion *tiling_node_of(WTiling *ws, WRegion *reg)
1190 {
1191     if(reg==NULL){
1192         warn(TR("Nil parameter."));
1193         return NULL;
1194     }
1195     
1196     if(REGION_MANAGER(reg)!=(WRegion*)ws){
1197         warn(TR("Manager doesn't match."));
1198         return NULL;
1199     }
1200     
1201     return splittree_node_of(reg);
1202 }
1203
1204
1205 /*}}}*/
1206
1207
1208 /*{{{ Flip and transpose */
1209
1210
1211 static WSplitSplit *get_at_split(WTiling *ws, WRegion *reg)
1212 {
1213     WSplit *node;
1214     WSplitSplit *split;
1215     
1216     if(reg==NULL){
1217         split=OBJ_CAST(ws->split_tree, WSplitSplit);
1218         if(split==NULL)
1219             return NULL;
1220         else if(split->br==(WSplit*)ws->stdispnode)
1221             return OBJ_CAST(split->tl, WSplitSplit);
1222         else if(split->tl==(WSplit*)ws->stdispnode)
1223             return OBJ_CAST(split->br, WSplitSplit);
1224         else
1225             return split;
1226     }
1227     
1228     node=(WSplit*)get_node_check(ws, reg);
1229     
1230     if(node==NULL)
1231         return NULL;
1232     
1233     if(node==(WSplit*)ws->stdispnode){
1234         warn(TR("The status display is not a valid parameter for "
1235                 "this routine."));
1236         return NULL;
1237     }
1238         
1239     split=OBJ_CAST(node->parent, WSplitSplit);
1240     
1241     if(split!=NULL && (split->tl==(WSplit*)ws->stdispnode ||
1242                        split->br==(WSplit*)ws->stdispnode)){
1243         split=OBJ_CAST(((WSplit*)split)->parent, WSplitSplit);
1244     }
1245        
1246     return split;
1247 }
1248
1249
1250 /*EXTL_DOC
1251  * Flip \var{ws} at \var{reg} or root if nil.
1252  */
1253 EXTL_EXPORT_MEMBER
1254 bool iowns_flip_at(WTiling *ws, WRegion *reg)
1255 {
1256     WSplitSplit *split=get_at_split(ws, reg);
1257     
1258     if(split==NULL){
1259         return FALSE;
1260     }else{
1261         splitsplit_flip(split);
1262         return TRUE;
1263     }
1264 }
1265
1266
1267 /*EXTL_DOC
1268  * Transpose \var{ws} at \var{reg} or root if nil.
1269  */
1270 EXTL_EXPORT_MEMBER
1271 bool iowns_transpose_at(WTiling *ws, WRegion *reg)
1272 {
1273     WSplitSplit *split=get_at_split(ws, reg);
1274     
1275     if(split==NULL){
1276         return FALSE;
1277     }else{
1278         split_transpose((WSplit*)split);
1279         return TRUE;
1280     }
1281 }
1282
1283
1284 /*}}}*/
1285
1286
1287 /*{{{ Floating toggle */
1288
1289
1290 static void replace(WSplitSplit *split, WSplitSplit *nsplit)
1291 {
1292     WSplitInner *psplit=split->isplit.split.parent;
1293
1294     nsplit->tl=split->tl;
1295     split->tl=NULL;
1296     nsplit->tl->parent=(WSplitInner*)nsplit;
1297     
1298     nsplit->br=split->br;
1299     split->br=NULL;
1300     nsplit->br->parent=(WSplitInner*)nsplit;
1301     
1302     if(psplit!=NULL){
1303         splitinner_replace((WSplitInner*)psplit, (WSplit*)split, 
1304                            (WSplit*)nsplit);
1305     }else{
1306         splittree_changeroot((WSplit*)split, (WSplit*)nsplit);
1307     }
1308 }
1309
1310
1311 WSplitSplit *tiling_set_floating(WTiling *ws, WSplitSplit *split, int sp)
1312 {
1313     bool set=OBJ_IS(split, WSplitFloat);
1314     bool nset=libtu_do_setparam(sp, set);
1315     const WRectangle *g=&((WSplit*)split)->geom;
1316     WSplitSplit *ns;
1317     
1318     if(!XOR(nset, set))
1319         return split;
1320     
1321     if(nset){
1322         ns=(WSplitSplit*)create_splitfloat(g, ws, split->dir);
1323     }else{
1324         if(OBJ_IS(split->tl, WSplitST) || OBJ_IS(split->br, WSplitST)){
1325             warn(TR("Refusing to float split directly containing the "
1326                     "status display."));
1327             return NULL;
1328         }
1329         ns=create_splitsplit(g, split->dir);
1330     }
1331
1332     if(ns!=NULL){
1333         replace(split, ns);
1334         split_resize((WSplit*)ns, g, PRIMN_ANY, PRIMN_ANY);
1335         mainloop_defer_destroy((Obj*)split);
1336     }
1337     
1338     return ns;
1339 }
1340
1341
1342 /*EXTL_DOC
1343  * Toggle floating of a split's sides at \var{split} as indicated by the 
1344  * parameter \var{how} (\codestr{set}, \codestr{unset}, or \codestr{toggle}).
1345  * A split of the appropriate is returned, if there was a change.
1346  */
1347 EXTL_EXPORT_AS(WTiling, set_floating)
1348 WSplitSplit *tiling_set_floating_extl(WTiling *ws, WSplitSplit *split, 
1349                                      const char *how)
1350 {
1351     if(!check_node(ws, (WSplit*)split))
1352         return NULL;
1353     return tiling_set_floating(ws, split, libtu_string_to_setparam(how));
1354 }
1355
1356
1357 /*EXTL_DOC
1358  * Toggle floating of the sides of a split containin \var{reg} as indicated 
1359  * by the parameters \var{how} (\codestr{set}, \codestr{unset}, or 
1360  * \codestr{toggle}) and \var{dirstr} (\codestr{left}, \codestr{right}, 
1361  * \codestr{up}, or \codestr{down}). The new status is returned 
1362  * (and \code{false} also on error).
1363  */
1364 EXTL_EXPORT_AS(WTiling, set_floating_at)
1365 bool tiling_set_floating_at_extl(WTiling *ws, WRegion *reg, const char *how,
1366                                 const char *dirstr)
1367 {
1368     WPrimn hprimn=PRIMN_ANY, vprimn=PRIMN_ANY;
1369     WSplitSplit *split, *nsplit;
1370     WSplit *node;
1371     
1372     node=(WSplit*)get_node_check(ws, reg);
1373     if(node==NULL)
1374         return FALSE;
1375     
1376     
1377     if(dirstr!=NULL){
1378         WRegionNavi nh;
1379         
1380         if(!ioncore_string_to_navi(dirstr, &nh))
1381             return FALSE;
1382     
1383         navi_to_primn(nh, &hprimn, &vprimn, PRIMN_NONE);
1384     }
1385
1386     while(TRUE){
1387         split=OBJ_CAST(node->parent, WSplitSplit);
1388         if(split==NULL){
1389             warn(TR("No suitable split here."));
1390             return FALSE;
1391         }
1392
1393         if(!OBJ_IS(split->tl, WSplitST) && !OBJ_IS(split->br, WSplitST)){
1394             WPrimn tmp=(split->dir==SPLIT_VERTICAL ? vprimn : hprimn);
1395             if(tmp==PRIMN_ANY 
1396                || (node==split->tl && tmp==PRIMN_BR)
1397                || (node==split->br && tmp==PRIMN_TL)){
1398                 break;
1399             }
1400         }
1401         
1402         node=(WSplit*)split;
1403     }
1404     
1405     nsplit=tiling_set_floating(ws, split, libtu_string_to_setparam(how));
1406     
1407     return OBJ_IS((Obj*)(nsplit==NULL ? split : nsplit), WSplitFloat);
1408 }
1409
1410
1411 /*}}}*/
1412
1413
1414 /*{{{ Save */
1415
1416
1417 ExtlTab tiling_get_configuration(WTiling *ws)
1418 {
1419     ExtlTab tab, split_tree=extl_table_none();
1420     
1421     tab=region_get_base_configuration((WRegion*)ws);
1422     
1423     if(ws->split_tree!=NULL){
1424         if(!split_get_config(ws->split_tree, &split_tree))
1425             warn(TR("Could not get split tree."));
1426     }
1427     
1428     extl_table_sets_t(tab, "split_tree", split_tree);
1429     extl_unref_table(split_tree);
1430     
1431     return tab;
1432 }
1433
1434
1435 /*}}}*/
1436
1437
1438 /*{{{ Load */
1439
1440
1441 WSplit *load_splitst(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1442 {
1443     WSplitST *st;
1444
1445     if(ws->stdispnode!=NULL){
1446         warn(TR("Workspace already has a status display node."));
1447         return NULL;
1448     }
1449
1450     st=create_splitst(geom, NULL);
1451     ws->stdispnode=st;
1452     return (WSplit*)st;
1453 }
1454
1455
1456 static bool do_attach(WTiling *ws, WRegion *reg, void *p)
1457 {
1458     WSplitRegion *node=create_splitregion(&REGION_GEOM(reg), reg);
1459     
1460     if(node==NULL)
1461         return FALSE;
1462             
1463     if(!tiling_managed_add(ws, reg)){
1464         node->reg=NULL;
1465         destroy_obj((Obj*)node);
1466         return FALSE;
1467     }
1468     
1469     *(WSplitRegion**)p=node;
1470     
1471     return TRUE;
1472 }
1473
1474
1475 WSplit *load_splitregion(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1476 {
1477     WWindow *par=REGION_PARENT(ws);
1478     WRegionAttachData data;
1479     WSplit *node=NULL;
1480     WFitParams fp;
1481     ExtlTab rt;
1482     
1483     if(!extl_table_gets_t(tab, "regparams", &rt)){
1484         warn(TR("Missing region parameters."));
1485         return NULL;
1486     }
1487     
1488     data.type=REGION_ATTACH_LOAD;
1489     data.u.tab=rt;
1490     
1491     assert(par!=NULL);
1492     fp.g=*geom;
1493     fp.mode=REGION_FIT_EXACT;
1494     
1495     region_attach_helper((WRegion*)ws, par, &fp, 
1496                          (WRegionDoAttachFn*)do_attach, &node, &data);
1497
1498     extl_unref_table(rt);
1499     
1500     return node;
1501 }
1502
1503
1504 #define MINS 1
1505
1506 WSplit *load_splitsplit(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1507 {
1508     WSplit *tl=NULL, *br=NULL;
1509     WSplitSplit *split;
1510     char *dir_str;
1511     int dir, brs, tls;
1512     ExtlTab subtab;
1513     WRectangle geom2;
1514     int set=0;
1515
1516     set+=(extl_table_gets_i(tab, "tls", &tls)==TRUE);
1517     set+=(extl_table_gets_i(tab, "brs", &brs)==TRUE);
1518     set+=(extl_table_gets_s(tab, "dir", &dir_str)==TRUE);
1519     
1520     if(set!=3)
1521         return NULL;
1522     
1523     if(strcmp(dir_str, "vertical")==0){
1524         dir=SPLIT_VERTICAL;
1525     }else if(strcmp(dir_str, "horizontal")==0){
1526         dir=SPLIT_HORIZONTAL;
1527     }else{
1528         warn(TR("Invalid direction."));
1529         free(dir_str);
1530         return NULL;
1531     }
1532     free(dir_str);
1533
1534     split=create_splitsplit(geom, dir);
1535     if(split==NULL)
1536         return NULL;
1537
1538     tls=maxof(tls, MINS);
1539     brs=maxof(brs, MINS);
1540         
1541     geom2=*geom;
1542     if(dir==SPLIT_HORIZONTAL){
1543         tls=maxof(0, geom->w)*tls/(tls+brs);
1544         geom2.w=tls;
1545     }else{
1546         tls=maxof(0, geom->h)*tls/(tls+brs);
1547         geom2.h=tls;
1548     }
1549     
1550     if(extl_table_gets_t(tab, "tl", &subtab)){
1551         tl=tiling_load_node(ws, &geom2, subtab);
1552         extl_unref_table(subtab);
1553     }
1554
1555     geom2=*geom;
1556     if(dir==SPLIT_HORIZONTAL){
1557         geom2.w-=tls;
1558         geom2.x+=tls;
1559     }else{
1560         geom2.h-=tls;
1561         geom2.y+=tls;
1562     }
1563             
1564     if(extl_table_gets_t(tab, "br", &subtab)){
1565         br=tiling_load_node(ws, &geom2, subtab);
1566         extl_unref_table(subtab);
1567     }
1568     
1569     if(tl==NULL || br==NULL){
1570         /* PRIMN_TL/BR instead of ANY because of stdisp. */
1571         destroy_obj((Obj*)split);
1572         if(tl!=NULL){
1573             split_do_resize(tl, geom, PRIMN_BR, PRIMN_BR, FALSE);
1574             return tl;
1575         }
1576         if(br!=NULL){
1577             split_do_resize(br, geom, PRIMN_TL, PRIMN_TL, FALSE);
1578             return br;
1579         }
1580         return NULL;
1581     }
1582     
1583     tl->parent=(WSplitInner*)split;
1584     br->parent=(WSplitInner*)split;
1585
1586     /*split->tmpsize=tls;*/
1587     split->tl=tl;
1588     split->br=br;
1589     
1590     return (WSplit*)split;
1591 }
1592
1593
1594 WSplit *tiling_load_node_default(WTiling *ws, const WRectangle *geom, 
1595                                 ExtlTab tab)
1596 {
1597     char *typestr=NULL;
1598     WSplit *node=NULL;
1599
1600     extl_table_gets_s(tab, "type", &typestr);
1601     
1602     if(typestr==NULL){
1603         warn(TR("No split type given."));
1604         return NULL;
1605     }
1606     
1607     if(strcmp(typestr, "WSplitRegion")==0)
1608         node=load_splitregion(ws, geom, tab);
1609     else if(strcmp(typestr, "WSplitSplit")==0)
1610         node=load_splitsplit(ws, geom, tab);
1611     else if(strcmp(typestr, "WSplitFloat")==0)
1612         node=load_splitfloat(ws, geom, tab);
1613     else if(strcmp(typestr, "WSplitST")==0)
1614         node=NULL;/*load_splitst(ws, geom, tab);*/
1615     else
1616         warn(TR("Unknown split type."));
1617     
1618     free(typestr);
1619     
1620     return node;
1621 }
1622
1623
1624 WSplit *tiling_load_node(WTiling *ws, const WRectangle *geom, ExtlTab tab)
1625 {
1626     WSplit *ret=NULL;
1627     CALL_DYN_RET(ret, WSplit*, tiling_load_node, ws, (ws, geom, tab));
1628     return ret;
1629 }
1630
1631
1632
1633 WRegion *tiling_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
1634 {
1635     WTiling *ws;
1636     ExtlTab treetab;
1637     bool ci=TRUE;
1638
1639     if(extl_table_gets_t(tab, "split_tree", &treetab))
1640         ci=FALSE;
1641     
1642     ws=create_tiling(par, fp, NULL, ci);
1643     
1644     if(ws==NULL){
1645         if(!ci)
1646             extl_unref_table(treetab);
1647         return NULL;
1648     }
1649
1650     if(!ci){
1651         ws->split_tree=tiling_load_node(ws, &REGION_GEOM(ws), treetab);
1652         extl_unref_table(treetab);
1653     }
1654     
1655     if(ws->split_tree==NULL){
1656         warn(TR("The workspace is empty."));
1657         destroy_obj((Obj*)ws);
1658         return NULL;
1659     }
1660     
1661     ws->split_tree->ws_if_root=ws;
1662     split_restack(ws->split_tree, ws->dummywin, Above);
1663     
1664     return (WRegion*)ws;
1665 }
1666
1667
1668 /*}}}*/
1669
1670
1671 /*{{{ Dynamic function table and class implementation */
1672
1673
1674 static DynFunTab tiling_dynfuntab[]={
1675     {region_map, 
1676      tiling_map},
1677     
1678     {region_unmap, 
1679      tiling_unmap},
1680     
1681     {region_do_set_focus, 
1682      tiling_do_set_focus},
1683     
1684     {(DynFun*)region_fitrep,
1685      (DynFun*)tiling_fitrep},
1686     
1687     {region_managed_rqgeom, 
1688      tiling_managed_rqgeom},
1689     
1690     {region_managed_remove, 
1691      tiling_managed_remove},
1692     
1693     {(DynFun*)region_managed_prepare_focus,
1694      (DynFun*)tiling_managed_prepare_focus},
1695     
1696     {(DynFun*)region_prepare_manage, 
1697      (DynFun*)tiling_prepare_manage},
1698     
1699     {(DynFun*)region_rescue_clientwins,
1700      (DynFun*)tiling_rescue_clientwins},
1701
1702     {(DynFun*)region_get_rescue_pholder_for,
1703      (DynFun*)tiling_get_rescue_pholder_for},
1704     
1705     {(DynFun*)region_get_configuration,
1706      (DynFun*)tiling_get_configuration},
1707
1708     {(DynFun*)region_managed_disposeroot,
1709      (DynFun*)tiling_managed_disposeroot},
1710
1711     {(DynFun*)region_current,
1712      (DynFun*)tiling_current},
1713
1714     {(DynFun*)tiling_managed_add,
1715      (DynFun*)tiling_managed_add_default},
1716     
1717     {region_manage_stdisp,
1718      tiling_manage_stdisp},
1719
1720     {region_unmanage_stdisp,
1721      tiling_unmanage_stdisp},
1722     
1723     {(DynFun*)tiling_load_node,
1724      (DynFun*)tiling_load_node_default},
1725             
1726     {region_restack,
1727      tiling_restack},
1728
1729     {region_stacking,
1730      tiling_stacking},
1731     
1732     {(DynFun*)region_navi_first,
1733      (DynFun*)tiling_navi_first},
1734     
1735     {(DynFun*)region_navi_next,
1736      (DynFun*)tiling_navi_next},
1737     
1738     {(DynFun*)region_xwindow,
1739      (DynFun*)tiling_xwindow},
1740     
1741     END_DYNFUNTAB
1742 };
1743
1744
1745 EXTL_EXPORT
1746 IMPLCLASS(WTiling, WRegion, tiling_deinit, tiling_dynfuntab);
1747
1748     
1749 /*}}}*/
1750