]> git.decadent.org.uk Git - ion3.git/blob - ioncore/mplex.c
[svn-inject] Installing original source of ion3
[ion3.git] / ioncore / mplex.c
1 /*
2  * ion/ioncore/mplex.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2006. 
5  *
6  * Ion is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <limits.h>
13 #include <string.h>
14
15 #include <libtu/objp.h>
16 #include <libtu/minmax.h>
17 #include <libtu/rb.h>
18 #include <libextl/extl.h>
19 #include <libmainloop/defer.h>
20
21 #include "common.h"
22 #include "window.h"
23 #include "global.h"
24 #include "rootwin.h"
25 #include "focus.h"
26 #include "event.h"
27 #include "attach.h"
28 #include "manage.h"
29 #include "resize.h"
30 #include "tags.h"
31 #include "sizehint.h"
32 #include "extlconv.h"
33 #include "frame-pointer.h"
34 #include "bindmaps.h"
35 #include "regbind.h"
36 #include "saveload.h"
37 #include "xwindow.h"
38 #include "mplexpholder.h"
39 #include "llist.h"
40 #include "names.h"
41 #include "sizepolicy.h"
42 #include "stacking.h"
43 #include "group.h"
44 #include "navi.h"
45 #include "groupedpholder.h"
46
47
48 #define SUBS_MAY_BE_MAPPED(MPLEX) \
49     (REGION_IS_MAPPED(MPLEX) && !MPLEX_MGD_UNVIEWABLE(MPLEX))
50
51 #define PASSIVE(ST) ((ST)->level<=STACKING_LEVEL_MODAL1          \
52                      && ((ST)->reg==NULL                         \
53                          || (ST)->reg->flags&REGION_SKIP_FOCUS))
54
55 #define CAN_MANAGE_STDISP(REG) HAS_DYN(REG, region_manage_stdisp)
56
57
58 /*{{{ Stacking list stuff */
59
60
61 WStacking *mplex_get_stacking(WMPlex *mplex)
62 {
63     return window_get_stacking(&mplex->win);
64 }
65
66
67 WStacking **mplex_get_stackingp(WMPlex *mplex)
68 {
69     return window_get_stackingp(&mplex->win);
70 }
71
72
73 void mplex_iter_init(WMPlexIterTmp *tmp, WMPlex *mplex)
74 {
75     stacking_iter_mgr_init(tmp, mplex->mgd, NULL, mplex);
76 }
77
78
79 WRegion *mplex_iter(WMPlexIterTmp *tmp)
80 {
81     return stacking_iter_mgr(tmp);
82 }
83
84
85 WStacking *mplex_iter_nodes(WMPlexIterTmp *tmp)
86 {
87     return stacking_iter_mgr_nodes(tmp);
88 }
89
90
91 /*}}}*/
92
93
94 /*{{{ Destroy/create mplex */
95
96
97 bool mplex_do_init(WMPlex *mplex, WWindow *parent, Window win,
98                    const WFitParams *fp, bool create)
99 {
100     mplex->flags=0;
101     
102     mplex->mx_list=NULL;
103     mplex->mx_current=NULL;
104     mplex->mx_phs=NULL;
105     mplex->mx_count=0;
106     
107     mplex->mgd=NULL;
108     
109     watch_init(&(mplex->stdispwatch));
110     mplex->stdispinfo.pos=MPLEX_STDISP_BL;
111     mplex->stdispinfo.fullsize=FALSE;
112     
113     if(create){
114         if(!window_init((WWindow*)mplex, parent, fp))
115             return FALSE;
116     }else{
117         if(!window_do_init((WWindow*)mplex, parent, win, fp))
118             return FALSE;
119     }
120     
121     mplex->win.region.flags|=REGION_BINDINGS_ARE_GRABBED;
122     
123     window_select_input(&(mplex->win), IONCORE_EVENTMASK_CWINMGR);
124     
125     region_add_bindmap((WRegion*)mplex, ioncore_mplex_bindmap);
126     region_add_bindmap((WRegion*)mplex, ioncore_mplex_toplevel_bindmap);
127     
128     region_register((WRegion*)mplex);
129     
130     /* Call this to set MPLEX_MANAGED_UNVIEWABLE if necessary. */
131     mplex_fit_managed(mplex);
132     
133     return TRUE;
134 }
135
136
137 bool mplex_init(WMPlex *mplex, WWindow *parent, const WFitParams *fp)
138 {
139     return mplex_do_init(mplex, parent, None, fp, TRUE);
140 }
141
142
143 WMPlex *create_mplex(WWindow *parent, const WFitParams *fp)
144 {
145     CREATEOBJ_IMPL(WMPlex, mplex, (p, parent, fp));
146 }
147
148
149 void mplex_deinit(WMPlex *mplex)
150 {
151     WMPlexIterTmp tmp;
152     WRegion *reg;
153     
154     FOR_ALL_MANAGED_BY_MPLEX(mplex, reg, tmp){
155         destroy_obj((Obj*)reg);
156     }
157     
158     assert(mplex->mgd==NULL);
159     assert(mplex->mx_list==NULL);
160
161     while(mplex->mx_phs!=NULL){
162         assert(mplexpholder_move(mplex->mx_phs, NULL, NULL, NULL));
163     }
164     
165     window_deinit((WWindow*)mplex);
166 }
167
168
169 bool mplex_may_destroy(WMPlex *mplex)
170 {
171     if(mplex->mgd!=NULL){
172         warn(TR("Refusing to destroy - not empty."));
173         return FALSE;
174     }
175     
176     return TRUE;
177 }
178
179
180 /*}}}*/
181
182
183 /*{{{ Node lookup etc. */
184
185
186 WStacking *mplex_find_stacking(WMPlex *mplex, WRegion *reg)
187 {
188     WStacking *st;
189     
190     /* Some routines that call us expect us to this check. */
191     if(reg==NULL || REGION_MANAGER(reg)!=(WRegion*)mplex)
192         return NULL;
193     
194     st=ioncore_find_stacking(reg);
195
196     assert(st==NULL || st->mgr_prev!=NULL);
197     
198     return st;
199 }
200
201
202 WStacking *mplex_current_node(WMPlex *mplex)
203 {
204     WStacking *st=NULL;
205     WRegion *reg;
206     
207     reg=REGION_ACTIVE_SUB(mplex);
208     reg=region_managed_within((WRegion*)mplex, reg);
209     if(reg!=NULL)
210         st=mplex_find_stacking(mplex, reg);
211
212     if(st!=NULL)
213         return st;
214     else
215         return (mplex->mx_current!=NULL ? mplex->mx_current->st : NULL);
216 }
217
218
219 WRegion *mplex_current(WMPlex *mplex)
220 {
221     WStacking *node=mplex_current_node(mplex);
222     return (node==NULL ? NULL : node->reg);
223 }
224
225
226 /*}}}*/
227
228
229 /*{{{ Exclusive list management and exports */
230
231 /*EXTL_DOC
232  * Returns the number of objects on the mutually exclusive list of \var{mplex}.
233  */
234 EXTL_SAFE
235 EXTL_EXPORT_MEMBER
236 int mplex_mx_count(WMPlex *mplex)
237 {
238     return mplex->mx_count;
239 }
240
241
242 /*EXTL_DOC
243  * Returns the managed object currently active within the mutually exclusive
244  * list of \var{mplex}.
245  */
246 EXTL_SAFE
247 EXTL_EXPORT_MEMBER
248 WRegion *mplex_mx_current(WMPlex *mplex)
249 {
250     WLListNode *lnode=mplex->mx_current;
251     return (lnode==NULL ? NULL : lnode->st->reg);
252 }
253
254
255 /*EXTL_DOC
256  * Returns the \var{n}:th object managed by \var{mplex} on the
257  * \var{l}:th layer.
258  */
259 EXTL_SAFE
260 EXTL_EXPORT_MEMBER
261 WRegion *mplex_mx_nth(WMPlex *mplex, uint n)
262 {
263     WLListNode *lnode=llist_nth_node(mplex->mx_list, n);
264     return (lnode==NULL ? NULL : lnode->st->reg);
265 }
266
267
268 /*EXTL_DOC
269  * Returns a list of regions on the numbered/mutually exclusive list of 
270  * \var{mplex}.
271  */
272 EXTL_SAFE
273 EXTL_EXPORT_MEMBER
274 ExtlTab mplex_mx_list(WMPlex *mplex)
275 {
276     WLListIterTmp tmp;
277     llist_iter_init(&tmp, mplex->mx_list);
278     
279     return extl_obj_iterable_to_table((ObjIterator*)llist_iter_regions, &tmp);
280 }
281
282
283 /*EXTL_DOC
284  * Returns a list of all regions managed by \var{mplex}.
285  */
286 EXTL_SAFE
287 EXTL_EXPORT_MEMBER
288 ExtlTab mplex_managed_list(WMPlex *mplex)
289 {
290     WMPlexIterTmp tmp;
291     mplex_iter_init(&tmp, mplex);
292     
293     return extl_obj_iterable_to_table((ObjIterator*)mplex_iter, &tmp);
294 }
295
296
297 /*EXTL_DOC
298  * Set index of \var{reg} within the multiplexer to \var{index} within 
299  * the mutually exclusive list. Special values for \var{index} are:
300  * \begin{tabularx}{\linewidth}{lX}
301  *   $-1$ & After \fnref{WMPlex.mx_current}. \\
302  *   $-2$ & Last. \\
303  * \end{tabularx}
304  */
305 EXTL_EXPORT_MEMBER
306 void mplex_set_index(WMPlex *mplex, WRegion *reg, int index)
307 {
308     WLListNode *lnode, *after;
309     WStacking *node;
310     
311     node=mplex_find_stacking(mplex, reg);
312
313     if(node==NULL)
314         return;
315     
316     lnode=node->lnode;
317     
318     if(lnode==NULL){
319         lnode=ALLOC(WLListNode);
320         if(lnode==NULL)
321             return;
322         lnode->next=NULL;
323         lnode->prev=NULL;
324         lnode->phs=NULL;
325         lnode->st=node;
326         node->lnode=lnode;
327         mplex->mx_count++;
328     }else{
329         mplex_move_phs_before(mplex, lnode);
330         llist_unlink(&(mplex->mx_list), lnode);
331     }
332     
333     /* TODO: Support remove? */
334     
335     after=llist_index_to_after(mplex->mx_list, mplex->mx_current, index);
336     llist_link_after(&(mplex->mx_list), after, lnode);
337     mplex_managed_changed(mplex, MPLEX_CHANGE_REORDER, FALSE, reg);
338 }
339
340
341 /*EXTL_DOC
342  * Get index of \var{reg} within the multiplexer on list 1. The first region 
343  * managed by \var{mplex} has index zero. If \var{reg} is not managed by 
344  * \var{mplex}, -1 is returned.
345  */
346 EXTL_SAFE
347 EXTL_EXPORT_MEMBER
348 int mplex_get_index(WMPlex *mplex, WRegion *reg)
349 {
350     WLListIterTmp tmp;
351     WLListNode *lnode;
352     int index=0;
353     
354     FOR_ALL_NODES_ON_LLIST(lnode, mplex->mx_list, tmp){
355         if(reg==lnode->st->reg)
356             return index;
357         index++;
358     }
359     
360     return -1;
361 }
362
363
364 /*EXTL_DOC
365  * Move \var{r} ''right'' within objects managed by \var{mplex} on list 1.
366  */
367 EXTL_EXPORT_MEMBER
368 void mplex_inc_index(WMPlex *mplex, WRegion *r)
369 {
370     if(r==NULL)
371         r=mplex_mx_current(mplex);
372     if(r!=NULL)
373         mplex_set_index(mplex, r, mplex_get_index(mplex, r)+1);
374 }
375
376
377 /*EXTL_DOC
378  * Move \var{r} ''right'' within objects managed by \var{mplex} on list 1.
379  */
380 EXTL_EXPORT_MEMBER
381 void mplex_dec_index(WMPlex *mplex, WRegion *r)
382 {
383     if(r==NULL)
384         r=mplex_mx_current(mplex);
385     if(r!=NULL)
386         mplex_set_index(mplex, r, mplex_get_index(mplex, r)-1);
387 }
388
389
390 /*}}}*/
391
392
393 /*{{{ Mapping */
394
395
396 static void mplex_map_mgd(WMPlex *mplex)
397 {
398     WMPlexIterTmp tmp;
399     WStacking *node;
400
401     FOR_ALL_NODES_IN_MPLEX(mplex, node, tmp){
402         if(!STACKING_IS_HIDDEN(node))
403             region_map(node->reg);
404     }
405 }
406
407
408 static void mplex_unmap_mgd(WMPlex *mplex)
409 {
410     WMPlexIterTmp tmp;
411     WStacking *node;
412
413     FOR_ALL_NODES_IN_MPLEX(mplex, node, tmp){
414         if(!STACKING_IS_HIDDEN(node))
415             region_unmap(node->reg);
416     }
417 }
418
419
420
421 void mplex_map(WMPlex *mplex)
422 {
423     window_map((WWindow*)mplex);
424     /* A lame requirement of the ICCCM is that client windows should be
425      * unmapped if the parent is unmapped.
426      */
427     if(!MPLEX_MGD_UNVIEWABLE(mplex))
428         mplex_map_mgd(mplex);
429 }
430
431
432 void mplex_unmap(WMPlex *mplex)
433 {
434     window_unmap((WWindow*)mplex);
435     /* A lame requirement of the ICCCM is that client windows should be
436      * unmapped if the parent is unmapped.
437      */
438     if(!MPLEX_MGD_UNVIEWABLE(mplex))
439         mplex_unmap_mgd(mplex);
440 }
441
442
443 /*}}}*/
444
445
446 /*{{{ Resize and reparent */
447
448
449 bool mplex_fitrep(WMPlex *mplex, WWindow *par, const WFitParams *fp)
450 {
451     bool wchg=(REGION_GEOM(mplex).w!=fp->g.w);
452     bool hchg=(REGION_GEOM(mplex).h!=fp->g.h);
453     
454     window_do_fitrep(&(mplex->win), par, &(fp->g));
455     
456     if(wchg || hchg){
457         mplex_fit_managed(mplex);
458         mplex_size_changed(mplex, wchg, hchg);
459     }
460     
461     return TRUE;
462 }
463
464
465 void mplex_do_fit_managed(WMPlex *mplex, WFitParams *fp)
466 {
467     WRectangle geom;
468     WMPlexIterTmp tmp;
469     WStacking *node;
470     WFitParams fp2;
471     
472     if(!MPLEX_MGD_UNVIEWABLE(mplex) && (fp->g.w<=1 || fp->g.h<=1)){
473         mplex->flags|=MPLEX_MANAGED_UNVIEWABLE;
474         if(REGION_IS_MAPPED(mplex))
475             mplex_unmap_mgd(mplex);
476     }else if(MPLEX_MGD_UNVIEWABLE(mplex) && !(fp->g.w<=1 || fp->g.h<=1)){
477         mplex->flags&=~MPLEX_MANAGED_UNVIEWABLE;
478         if(REGION_IS_MAPPED(mplex))
479             mplex_map_mgd(mplex);
480     }
481     
482     if(!MPLEX_MGD_UNVIEWABLE(mplex)){
483         FOR_ALL_NODES_IN_MPLEX(mplex, node, tmp){
484             fp2=*fp;
485             sizepolicy(&node->szplcy, node->reg, NULL, 0, &fp2);
486             region_fitrep(node->reg, NULL, &fp2);
487         }
488     }
489 }
490
491
492 void mplex_fit_managed(WMPlex *mplex)
493 {
494     WFitParams fp;
495     
496     fp.mode=REGION_FIT_EXACT;
497     mplex_managed_geom(mplex, &(fp.g));
498
499     mplex_do_fit_managed(mplex, &fp);
500 }
501
502
503 static void mplex_managed_rqgeom(WMPlex *mplex, WRegion *sub,
504                                  const WRQGeomParams *rq,
505                                  WRectangle *geomret)
506 {
507     WRectangle rg;
508     WFitParams fp;
509     WStacking *node;
510
511     node=mplex_find_stacking(mplex, sub);
512     
513     assert(node!=NULL);
514
515     mplex_managed_geom(mplex, &fp.g);
516
517     sizepolicy(&node->szplcy, sub, &rq->geom, &rq->flags, &fp);
518     
519     if(geomret!=NULL)
520         *geomret=fp.g;
521     
522     if(!(rq->flags&REGION_RQGEOM_TRYONLY))
523         region_fitrep(sub, NULL, &fp);
524 }
525
526
527 /*}}}*/
528
529
530 /*{{{ Focus  */
531
532
533 static WRegion *mplex_do_to_focus(WMPlex *mplex, WStacking *to_try)
534 {
535     WStacking *stacking=mplex_get_stacking(mplex);
536     WStacking *st=NULL;
537     
538     if(stacking==NULL)
539         return NULL;
540
541     if(to_try!=NULL && (to_try->reg==NULL || !REGION_IS_MAPPED(to_try->reg)))
542         to_try=NULL;
543
544     st=stacking_find_to_focus_mapped(stacking, to_try, NULL);
545     
546     if(st!=NULL)
547         return st->reg;
548     else if(mplex->mx_current!=NULL)
549         return mplex->mx_current->st->reg;
550     else
551         return NULL;
552 }
553
554
555 WRegion *mplex_to_focus(WMPlex *mplex)
556 {
557     WRegion *reg=REGION_ACTIVE_SUB(mplex);
558     WStacking *to_try=NULL;
559     
560     if(reg!=NULL)
561         to_try=ioncore_find_stacking(reg);
562
563     return mplex_do_to_focus(mplex, to_try);
564 }
565
566
567 static WRegion *mplex_do_to_focus_on(WMPlex *mplex, WStacking *node,
568                                      WStacking *to_try)
569 {
570     WStacking *stacking=mplex_get_stacking(mplex);
571     WStacking *st=NULL;
572     
573     if(stacking==NULL)
574         return NULL;
575
576     if(to_try!=NULL && (to_try->reg==NULL || !REGION_IS_MAPPED(to_try->reg)))
577         to_try=NULL;
578     
579     st=stacking_find_to_focus_mapped(stacking, to_try, node->reg);
580     
581     return (st!=NULL ? st->reg : NULL);
582 }
583
584
585 static WRegion *mplex_to_focus_on(WMPlex *mplex, WStacking *node,
586                                   WStacking *to_try)
587 {
588     WRegion *reg;
589     WGroup *grp=OBJ_CAST(node->reg, WGroup);
590     
591     if(grp!=NULL){
592         if(to_try==NULL)
593             to_try=grp->current_managed;
594         reg=mplex_do_to_focus_on(mplex, node, to_try);
595         if(reg!=NULL || to_try!=NULL)
596             return reg;
597         /* We don't know whether something is blocking focus here,
598          * or if there was nothing to focus (as node->reg itself
599          * isn't on the stacking list).
600          */
601     }
602     
603     reg=mplex_do_to_focus(mplex, node);
604     return (reg==node->reg ? reg : NULL);
605 }
606
607
608 void mplex_do_set_focus(WMPlex *mplex, bool warp)
609 {
610     if(!MPLEX_MGD_UNVIEWABLE(mplex)){
611         WRegion *reg=mplex_to_focus(mplex);
612         
613         if(reg!=NULL){
614             region_do_set_focus(reg, warp);
615             return;
616         }
617     }
618     
619     window_do_set_focus((WWindow*)mplex, warp);
620 }
621
622
623 /*}}}*/
624
625
626 /*{{{ Switch */
627
628
629 static void mplex_do_remanage_stdisp(WMPlex *mplex, WRegion *sub)
630 {
631     WRegion *stdisp=(WRegion*)(mplex->stdispwatch.obj);
632
633     /* Move stdisp */
634     if(sub!=NULL && CAN_MANAGE_STDISP(sub)){
635         if(stdisp!=NULL){
636             WRegion *mgrw=region_managed_within((WRegion*)mplex, stdisp);
637             if(mgrw!=sub){
638                 WRegion *mgr=REGION_MANAGER(stdisp);
639                 if(mgr!=NULL){
640                     if(CAN_MANAGE_STDISP(mgr))
641                         region_unmanage_stdisp(mgr, FALSE, FALSE);
642                     region_detach_manager(stdisp);
643                 }
644                 
645                 region_manage_stdisp(sub, stdisp, 
646                                      &(mplex->stdispinfo));
647             }
648         }else{
649             region_unmanage_stdisp(sub, TRUE, FALSE);
650         }
651     }/*else if(stdisp!=NULL){
652         region_detach_manager(stdisp);
653         region_unmap(stdisp);
654     }*/
655 }
656
657
658 void mplex_remanage_stdisp(WMPlex *mplex)
659 {
660     mplex_do_remanage_stdisp(mplex, (mplex->mx_current!=NULL
661                                      ? mplex->mx_current->st->reg
662                                      : NULL));
663 }
664
665
666 static void mplex_do_node_display(WMPlex *mplex, WStacking *node,
667                                   bool call_changed)
668 {
669     WRegion *sub=node->reg;
670     WLListNode *mxc=mplex->mx_current;
671
672     if(!STACKING_IS_HIDDEN(node))
673         return;
674     
675     if(node->lnode!=NULL && node->lnode!=mxc)
676         mplex_do_remanage_stdisp(mplex, sub);
677
678     node->hidden=FALSE;
679     
680     if(SUBS_MAY_BE_MAPPED(mplex))
681         region_map(sub);
682     else
683         region_unmap(sub);
684     
685     if(node->lnode!=NULL){
686         if(mxc!=NULL){
687             /* Hide current mx region. We do it after mapping the
688              * new one to avoid flicker.
689              */
690             if(REGION_IS_MAPPED(mplex))
691                 region_unmap(mxc->st->reg);
692             mxc->st->hidden=TRUE;
693         }
694         
695         mplex->mx_current=node->lnode;
696         
697         /* Ugly hack:
698          * Many programs will get upset if the visible, although only 
699          * such, client window is not the lowest window in the mplex. 
700          * xprop/xwininfo will return the information for the lowest 
701          * window. 'netscape -remote' will not work at all if there are
702          * no visible netscape windows.
703          */
704         {
705             #warning "TODO: less ugly hack"
706             WGroup *grp=(WGroup*)OBJ_CAST(sub, WGroupCW);
707             if(grp!=NULL && grp->bottom!=NULL){
708                 region_managed_rqorder((WRegion*)grp, grp->bottom->reg, 
709                                        REGION_ORDER_BACK);
710             }
711         }
712         
713         if(call_changed)
714             mplex_managed_changed(mplex, MPLEX_CHANGE_SWITCHONLY, TRUE, sub);
715     }
716 }
717
718
719 static bool mplex_refocus(WMPlex *mplex, WStacking *node, bool warp)
720 {
721     WRegion *foc=NULL;
722     bool ret=TRUE;
723     
724     if(node!=NULL){
725         foc=mplex_to_focus_on(mplex, node, NULL);
726         ret=(foc!=NULL);
727     }
728         
729     if(foc==NULL){
730         ret=FALSE;
731         foc=mplex_to_focus(mplex);
732     }
733     
734     if(foc!=NULL /* && !REGION_IS_ACTIVE(foc) */ )
735         region_maybewarp(foc, warp);
736     
737     return ret;
738 }
739
740
741 bool mplex_do_prepare_focus(WMPlex *mplex, WStacking *node,
742                             WStacking *sub, int flags, 
743                             WPrepareFocusResult *res)
744 {
745     WRegion *foc;
746     
747     if(sub==NULL && node==NULL)
748         return FALSE;
749     
750     /* Display the node in any case */
751     if(node!=NULL && !(flags&REGION_GOTO_ENTERWINDOW))
752         mplex_do_node_display(mplex, node, TRUE);
753     
754     if(!region_prepare_focus((WRegion*)mplex, flags, res))
755         return FALSE;
756
757     if(node!=NULL)
758         foc=mplex_to_focus_on(mplex, node, sub);
759     else
760         foc=mplex_do_to_focus(mplex, sub);
761
762     if(foc!=NULL){
763         res->reg=foc;
764         res->flags=flags;
765         
766         if(sub==NULL)
767             return (foc==node->reg);
768         else
769             return (foc==sub->reg);
770     }else{
771         return FALSE;
772     }
773 }
774
775
776 bool mplex_managed_prepare_focus(WMPlex *mplex, WRegion *disp, 
777                                  int flags, WPrepareFocusResult *res)
778 {
779     WStacking *node=mplex_find_stacking(mplex, disp);
780     
781     if(node==NULL)
782         return FALSE;
783     else
784         return mplex_do_prepare_focus(mplex, node, NULL, flags, res);
785 }
786
787
788 /*}}}*/
789
790
791 /*{{{ Switch exports */
792
793
794 static void do_switch(WMPlex *mplex, WLListNode *lnode)
795 {
796     WStacking *node=(lnode!=NULL ? lnode->st : NULL);
797     
798     if(node!=NULL){
799         bool mcf=region_may_control_focus((WRegion*)mplex);
800     
801         mplex_do_node_display(mplex, node, TRUE);
802         
803         if(mcf)
804             mplex_refocus(mplex, node, TRUE);
805     }
806 }
807
808
809 /*EXTL_DOC
810  * Have \var{mplex} display the \var{n}:th object managed by it.
811  */
812 EXTL_EXPORT_MEMBER
813 void mplex_switch_nth(WMPlex *mplex, uint n)
814 {
815     do_switch(mplex, llist_nth_node(mplex->mx_list, n));
816 }
817
818
819 /*EXTL_DOC
820  * Have \var{mplex} display next (wrt. currently selected) object managed 
821  * by it.
822  */
823 EXTL_EXPORT_MEMBER
824 void mplex_switch_next(WMPlex *mplex)
825 {
826     do_switch(mplex, LIST_NEXT_WRAP(mplex->mx_list, mplex->mx_current, 
827                                     next, prev));
828 }
829
830
831 /*EXTL_DOC
832  * Have \var{mplex} display previous (wrt. currently selected) object
833  * managed by it.
834  */
835 EXTL_EXPORT_MEMBER
836 void mplex_switch_prev(WMPlex *mplex)
837 {
838     do_switch(mplex, LIST_PREV_WRAP(mplex->mx_list, mplex->mx_current,
839                                     next, prev));
840 }
841
842
843 bool mplex_set_hidden(WMPlex *mplex, WRegion *reg, int sp)
844 {
845     bool mcf=region_may_control_focus((WRegion*)mplex);
846     WStacking *node=mplex_find_stacking(mplex, reg);
847     bool hidden, nhidden;
848     
849     if(node==NULL)
850         return FALSE;
851     
852     hidden=STACKING_IS_HIDDEN(node);
853     nhidden=libtu_do_setparam(sp, hidden);
854     
855     if(!hidden && nhidden){
856         node->hidden=TRUE;
857         
858         if(REGION_IS_MAPPED(mplex) && !MPLEX_MGD_UNVIEWABLE(mplex))
859             region_unmap(reg);
860         
861         /* lnode -> switch next? */
862     }else if(hidden && !nhidden){
863         mplex_do_node_display(mplex, node, TRUE);
864     }
865     
866     if(mcf && !PASSIVE(node))
867         mplex_refocus(mplex, (nhidden ? NULL : node), TRUE);
868     
869     return STACKING_IS_HIDDEN(node);
870 }
871
872
873 /*EXTL_DOC
874  * Set the visibility of the region \var{reg} on \var{mplex}
875  * as specified with the parameter \var{how} (set/unset/toggle).
876  * The resulting state is returned.
877  */
878 EXTL_EXPORT_AS(WMPlex, set_hidden)
879 bool mplex_set_hidden_extl(WMPlex *mplex, WRegion *reg, const char *how)
880 {
881     return mplex_set_hidden(mplex, reg, libtu_string_to_setparam(how));
882 }
883
884
885 /*EXTL_DOC
886  * Is \var{reg} on within \var{mplex} and hidden?
887  */
888 EXTL_SAFE
889 EXTL_EXPORT_MEMBER
890 bool mplex_is_hidden(WMPlex *mplex, WRegion *reg)
891 {
892     WStacking *node=mplex_find_stacking(mplex, reg);
893     
894     return (node!=NULL && STACKING_IS_HIDDEN(node));
895 }
896
897
898 /*}}}*/
899
900
901 /*{{{ Navigation */
902
903
904 static WStacking *mplex_nxt(WMPlex *mplex, WStacking *st, bool wrap)
905 {
906     return (st->mgr_next!=NULL 
907             ? st->mgr_next 
908             : (wrap ? mplex->mgd : NULL));
909 }
910
911
912 static WStacking *mplex_prv(WMPlex *mplex, WStacking *st, bool wrap)
913 {
914     return (st!=mplex->mgd
915             ? st->mgr_prev
916             : (wrap ? st->mgr_prev : NULL));
917 }
918
919
920 typedef WStacking *NxtFn(WMPlex *mplex, WStacking *st, bool wrap);
921
922
923 static WRegion *do_navi(WMPlex *mplex, WStacking *sti, 
924                         NxtFn *fn, WRegionNaviData *data, bool sti_ok)
925 {
926     WStacking *st, *stacking;
927     uint min_level=0;
928     bool wrap=FALSE;
929     
930     stacking=mplex_get_stacking(mplex);
931     
932     if(stacking!=NULL)
933         min_level=stacking_min_level_mapped(stacking);
934
935     st=sti;
936     while(1){
937         st=fn(mplex, st, wrap); 
938         
939         if(st==NULL || (st==sti && !sti_ok))
940             break;
941         
942         if(!st->hidden){
943             if(OBJ_IS(st->reg, WGroup)){
944                 /* WGroup navigation code should respect modal stuff. */
945                 WRegion *res=region_navi_cont((WRegion*)mplex, st->reg, data);
946                 if(res!=NULL){
947                     if(res!=st->reg){
948                         return res;
949                     }else{
950                         #warning "TODO: What to do?"
951                     }
952                 }
953             }else{
954                 if(st->level>=min_level && !PASSIVE(st))
955                     return region_navi_cont((WRegion*)mplex, st->reg, data);
956             }
957         }
958                 
959         if(st==sti)
960             break;
961     }
962     
963     return NULL;
964 }
965                         
966
967 WRegion *mplex_navi_first(WMPlex *mplex, WRegionNavi nh,
968                           WRegionNaviData *data)
969 {
970     WStacking *lst=mplex->mgd;
971
972     if(lst==NULL)
973         return region_navi_cont((WRegion*)mplex, NULL, data);
974     
975     if(nh==REGION_NAVI_ANY){
976         /* ? */
977     }
978     
979     if(nh==REGION_NAVI_ANY || nh==REGION_NAVI_END || 
980        nh==REGION_NAVI_BOTTOM || nh==REGION_NAVI_RIGHT){
981         return do_navi(mplex, lst, mplex_prv, data, TRUE);
982     }else{
983         return do_navi(mplex, lst->mgr_prev, mplex_nxt, data, TRUE);
984     }
985 }
986
987     
988 WRegion *mplex_navi_next(WMPlex *mplex, WRegion *rel, WRegionNavi nh,
989                          WRegionNaviData *data)
990 {
991     WStacking *st;
992     
993     if(rel!=NULL){
994         st=mplex_find_stacking(mplex, rel);
995         if(st==NULL)
996             return NULL;
997     }else if(mplex->mx_current!=NULL){
998         st=mplex->mx_current->st;
999     }else{
1000         return mplex_navi_first(mplex, nh, data);
1001     }
1002         
1003     if(nh==REGION_NAVI_ANY){
1004         /* ? */
1005     }
1006     
1007     if(nh==REGION_NAVI_ANY || nh==REGION_NAVI_END || 
1008        nh==REGION_NAVI_BOTTOM || nh==REGION_NAVI_RIGHT){
1009         return do_navi(mplex, st, mplex_nxt, data, FALSE);
1010     }else{
1011         return do_navi(mplex, st, mplex_prv, data, FALSE);
1012     }
1013 }
1014
1015
1016 /*}}}*/
1017
1018
1019 /*{{{ Stacking */
1020
1021
1022 bool mplex_managed_rqorder(WMPlex *mplex, WRegion *reg, WRegionOrder order)
1023 {
1024     WStacking **stackingp=mplex_get_stackingp(mplex);
1025     WStacking *st;
1026
1027     if(stackingp==NULL || *stackingp==NULL)
1028         return FALSE;
1029
1030     st=mplex_find_stacking(mplex, reg);
1031     
1032     if(st==NULL)
1033         return FALSE;
1034     
1035     stacking_restack(stackingp, st, None, NULL, NULL,
1036                      (order!=REGION_ORDER_FRONT));
1037     
1038     return TRUE;
1039 }
1040
1041
1042 /*}}}*/
1043
1044
1045 /*{{{ Attach */
1046
1047
1048 static bool mplex_stack(WMPlex *mplex, WStacking *st)
1049 {
1050     WStacking *tmp=NULL;
1051     Window bottom=None, top=None;
1052     WStacking **stackingp=mplex_get_stackingp(mplex);
1053     
1054     if(stackingp==NULL)
1055         return FALSE;
1056
1057     LINK_ITEM_FIRST(tmp, st, next, prev);
1058     stacking_weave(stackingp, &tmp, FALSE);
1059     assert(tmp==NULL);
1060     
1061     return TRUE;
1062 }
1063
1064
1065 static void mplex_unstack(WMPlex *mplex, WStacking *st)
1066 {
1067     WStacking *stacking;
1068     
1069     stacking=mplex_get_stacking(mplex);
1070     
1071     stacking_unstack(&mplex->win, st);
1072 }
1073
1074
1075 /* WMPlexWPHolder is used for position marking in order to allow
1076  * WLListNodes be safely removed in the attach handler hnd, that
1077  * could remove something this mplex is managing.
1078  */
1079 bool mplex_do_attach_final(WMPlex *mplex, WRegion *reg, WMPlexPHolder *ph)
1080 {
1081     WStacking *node=NULL;
1082     WLListNode *lnode=NULL;
1083     WMPlexAttachParams *param=&ph->param;
1084     bool mx_was_empty, sw, modal, mcf, hidden;
1085     uint level;
1086     
1087     mcf=region_may_control_focus((WRegion*)mplex);
1088     
1089     mx_was_empty=(mplex->mx_list==NULL);
1090     
1091     modal=(param->flags&MPLEX_ATTACH_LEVEL
1092            ? param->level>=STACKING_LEVEL_MODAL1
1093            : param->flags&MPLEX_ATTACH_MODAL);
1094     
1095     level=(param->flags&MPLEX_ATTACH_LEVEL
1096            ? param->level
1097            : (param->flags&MPLEX_ATTACH_MODAL
1098               ? STACKING_LEVEL_MODAL1
1099               : (param->flags&MPLEX_ATTACH_UNNUMBERED
1100                  ? STACKING_LEVEL_NORMAL
1101                  : STACKING_LEVEL_BOTTOM)));
1102     
1103     hidden=(param->flags&MPLEX_ATTACH_HIDDEN
1104             && (param->flags&MPLEX_ATTACH_UNNUMBERED
1105                 || !mx_was_empty));
1106     
1107     sw=(!hidden && (param->flags&MPLEX_ATTACH_SWITCHTO 
1108                     || (param->flags&MPLEX_ATTACH_UNNUMBERED
1109                         ? modal
1110                         : (mplex_current_node(mplex)==NULL))));
1111     
1112     hidden=(hidden || (!sw && !(param->flags&MPLEX_ATTACH_UNNUMBERED)));
1113     
1114     
1115     node=create_stacking();
1116     
1117     if(node==NULL)
1118         return FALSE;
1119     
1120     if(!(param->flags&MPLEX_ATTACH_UNNUMBERED)){
1121         lnode=ALLOC(WLListNode);
1122         if(lnode==NULL){
1123             stacking_free(node);
1124             return FALSE;
1125         }
1126         lnode->next=NULL;
1127         lnode->prev=NULL;
1128         lnode->phs=NULL;
1129         lnode->st=node;
1130         node->lnode=lnode;
1131     }
1132     
1133     if(!stacking_assoc(node, reg)){
1134         if(lnode!=NULL){
1135             node->lnode=NULL;
1136             free(lnode);
1137         }
1138         stacking_free(node);
1139         return FALSE;
1140     }
1141
1142     node->hidden=TRUE;
1143     node->szplcy=param->szplcy;
1144     node->level=level;
1145     
1146     if(lnode!=NULL){
1147         llist_link_after(&(mplex->mx_list), 
1148                          (ph!=NULL ? ph->after : NULL), 
1149                          lnode);
1150         mplex->mx_count++;
1151         
1152         /* Move following placeholders after new node */
1153         while(ph->next!=NULL)
1154             mplexpholder_move(ph->next, mplex, NULL, lnode);
1155     }
1156     
1157     LINK_ITEM(mplex->mgd, node, mgr_next, mgr_prev);
1158
1159     if(!OBJ_IS(reg, WGroup))
1160         mplex_stack(mplex, node);
1161     
1162     region_set_manager(reg, (WRegion*)mplex);
1163     
1164     if(!hidden)
1165         mplex_do_node_display(mplex, node, FALSE);
1166     else
1167         region_unmap(reg);
1168     
1169     if(sw && mcf)
1170         mplex_refocus(mplex, node, FALSE);
1171     
1172     if(lnode!=NULL)
1173         mplex_managed_changed(mplex, MPLEX_CHANGE_ADD, sw, reg);
1174     
1175     return TRUE;
1176 }
1177
1178
1179 WRegion *mplex_do_attach_pholder(WMPlex *mplex, WMPlexPHolder *ph,
1180                                  WRegionAttachData *data)
1181 {
1182     WMPlexAttachParams *param=&(ph->param);
1183     WSizePolicy szplcy=param->szplcy;
1184     WFitParams fp;
1185     WRegion *reg;
1186     
1187     param->szplcy=(param->flags&MPLEX_ATTACH_SIZEPOLICY &&
1188                    param->szplcy!=SIZEPOLICY_DEFAULT
1189                    ? param->szplcy
1190                    : (param->flags&MPLEX_ATTACH_UNNUMBERED
1191                       ? SIZEPOLICY_FULL_BOUNDS
1192                       : SIZEPOLICY_FULL_EXACT));
1193     
1194     mplex_managed_geom(mplex, &(fp.g));
1195     
1196     sizepolicy(&param->szplcy, NULL, 
1197                (param->flags&MPLEX_ATTACH_GEOM 
1198                 ? &(param->geom)
1199                 : NULL),
1200                0, &fp);
1201     
1202     if(param->flags&MPLEX_ATTACH_WHATEVER)
1203         fp.mode|=REGION_FIT_WHATEVER;
1204     
1205     reg=region_attach_helper((WRegion*)mplex, 
1206                              (WWindow*)mplex, &fp,
1207                              (WRegionDoAttachFn*)mplex_do_attach_final,
1208                              (void*)ph, data);
1209     
1210     /* restore */
1211     ph->param.szplcy=szplcy;
1212     
1213     return reg;
1214 }
1215
1216
1217 WRegion *mplex_do_attach(WMPlex *mplex, WMPlexAttachParams *param,
1218                          WRegionAttachData *data)
1219 {
1220     WMPlexPHolder *ph;
1221     WRegion *reg;
1222     
1223     ph=create_mplexpholder(mplex, NULL, param);
1224     
1225     if(ph==NULL)
1226         return NULL;
1227
1228     reg=mplex_do_attach_pholder(mplex, ph, data);
1229     
1230     destroy_obj((Obj*)ph);
1231     
1232     return reg;
1233 }
1234
1235
1236 WRegion *mplex_do_attach_new(WMPlex *mplex, WMPlexAttachParams *param,
1237                              WRegionCreateFn *fn, void *fn_param)
1238 {
1239     WRegionAttachData data;
1240     
1241     data.type=REGION_ATTACH_NEW;
1242     data.u.n.fn=fn;
1243     data.u.n.param=fn_param;
1244     
1245     return mplex_do_attach(mplex, param, &data);
1246 }
1247
1248
1249 #define MPLEX_ATTACH_SET_FLAGS (MPLEX_ATTACH_GEOM|       \
1250                                 MPLEX_ATTACH_SIZEPOLICY| \
1251                                 MPLEX_ATTACH_INDEX)
1252
1253
1254 WRegion *mplex_attach_simple(WMPlex *mplex, WRegion *reg, int flags)
1255 {
1256     WMPlexAttachParams param;
1257     WRegionAttachData data;
1258     
1259     param.flags=flags&~MPLEX_ATTACH_SET_FLAGS;
1260     
1261     data.type=REGION_ATTACH_REPARENT;
1262     data.u.reg=reg;
1263     
1264     return mplex_do_attach(mplex, &param, &data);
1265 }
1266
1267
1268 static void get_params(WMPlex *mplex, ExtlTab tab, WMPlexAttachParams *par)
1269 {
1270     int layer=1;
1271     int tmp;
1272     
1273     par->flags=0;
1274     
1275     if(extl_table_gets_i(tab, "layer", &tmp)){
1276         /* backwards compatibility */
1277         if(tmp==2){
1278             par->flags|=MPLEX_ATTACH_UNNUMBERED;
1279             if(!extl_table_is_bool_set(tab, "passive"))
1280                 par->flags|=MPLEX_ATTACH_MODAL;
1281         }
1282     }
1283
1284     if(extl_table_gets_i(tab, "level", &tmp)){
1285         if(tmp>=0){
1286             par->flags|=MPLEX_ATTACH_LEVEL;
1287             par->level=tmp;
1288         }
1289     }
1290     
1291     if(extl_table_is_bool_set(tab, "modal"))
1292         par->flags|=MPLEX_ATTACH_MODAL;
1293
1294     if(extl_table_is_bool_set(tab, "unnumbered"))
1295         par->flags|=MPLEX_ATTACH_UNNUMBERED;
1296     
1297     if(extl_table_is_bool_set(tab, "switchto"))
1298         par->flags|=MPLEX_ATTACH_SWITCHTO;
1299
1300     if(extl_table_is_bool_set(tab, "hidden"))
1301         par->flags|=MPLEX_ATTACH_HIDDEN;
1302
1303     if(extl_table_gets_i(tab, "index", &(par->index)))
1304         par->flags|=MPLEX_ATTACH_INDEX;
1305
1306     if(extl_table_gets_i(tab, "sizepolicy", &tmp)){
1307         par->flags|=MPLEX_ATTACH_SIZEPOLICY;
1308         par->szplcy=tmp;
1309     }
1310     
1311     if(extl_table_gets_rectangle(tab, "geom", &par->geom))
1312         par->flags|=MPLEX_ATTACH_GEOM;
1313 }
1314
1315
1316 /*EXTL_DOC
1317  * Attach and reparent existing region \var{reg} to \var{mplex}.
1318  * The table \var{param} may contain the fields \var{index} and
1319  * \var{switchto} that are interpreted as for \fnref{WMPlex.attach_new}.
1320  */
1321 EXTL_EXPORT_MEMBER
1322 WRegion *mplex_attach(WMPlex *mplex, WRegion *reg, ExtlTab param)
1323 {
1324     WMPlexAttachParams par;
1325     WRegionAttachData data;
1326
1327     if(reg==NULL)
1328         return NULL;
1329     
1330     get_params(mplex, param, &par);
1331     
1332     data.type=REGION_ATTACH_REPARENT;
1333     data.u.reg=reg;
1334     
1335     return mplex_do_attach(mplex, &par, &data);
1336 }
1337
1338
1339 /*EXTL_DOC
1340  * Create a new region to be managed by \var{mplex}. At least the following
1341  * fields in \var{param} are understood (all but \var{type} are optional).
1342  * 
1343  * \begin{tabularx}{\linewidth}{lX}
1344  *  \tabhead{Field & Description}
1345  *  \var{type} & (string) Class name (a string) of the object to be created. \\
1346  *  \var{name} & (string) Name of the object to be created (a string). \\
1347  *  \var{switchto} & (boolean) Should the region be switched to (boolean)? \\
1348  *  \var{unnumbered} & (boolean) Do not put on the numbered mutually 
1349  *                     exclusive list. \\
1350  *  \var{index} & (integer) Index on this list, same as for 
1351  *                \fnref{WMPlex.set_index}. \\
1352  *  \var{level} & (integer) Stacking level. \\
1353  *  \var{modal} & (boolean) Shortcut for modal stacking level. \\
1354  *  \var{hidden} & (boolean) Attach hidden, if not prevented
1355  *                  by e.g. the mutually exclusive list being empty.
1356  *                  This option overrides \var{switchto}. \\
1357  *  \var{sizepolicy} & (integer) Size policy.
1358  *                     (TODO: document them somewhere.) \\
1359  *  \var{geom} & (table) Geometry specification. \\
1360  * \end{tabularx}
1361  * 
1362  * In addition parameters to the region to be created are passed in this 
1363  * same table.
1364  */
1365 EXTL_EXPORT_MEMBER
1366 WRegion *mplex_attach_new(WMPlex *mplex, ExtlTab param)
1367 {
1368     WMPlexAttachParams par;
1369     WRegionAttachData data;
1370     
1371     get_params(mplex, param, &par);
1372     
1373     data.type=REGION_ATTACH_LOAD;
1374     data.u.tab=param;
1375     
1376     return mplex_do_attach(mplex, &par, &data);
1377 }
1378
1379
1380 /*EXTL_DOC
1381  * Attach all tagged regions to \var{mplex}.
1382  */
1383 EXTL_EXPORT_MEMBER
1384 void mplex_attach_tagged(WMPlex *mplex)
1385 {
1386     WRegion *reg;
1387     
1388     while((reg=ioncore_tags_take_first())!=NULL)
1389         mplex_attach_simple(mplex, reg, 0);
1390 }
1391
1392
1393 static bool mplex_handle_drop(WMPlex *mplex, int x, int y,
1394                               WRegion *dropped)
1395 {
1396     WRegion *curr=mplex_mx_current(mplex);
1397     
1398     /* This code should handle dropping tabs on floating workspaces. */
1399     if(curr && HAS_DYN(curr, region_handle_drop)){
1400         int rx, ry;
1401         region_rootpos(curr, &rx, &ry);
1402         if(rectangle_contains(&REGION_GEOM(curr), x-rx, y-ry)){
1403             if(region_handle_drop(curr, x, y, dropped))
1404                 return TRUE;
1405         }
1406     }
1407     
1408     return (NULL!=mplex_attach_simple(mplex, dropped, MPLEX_ATTACH_SWITCHTO));
1409 }
1410
1411
1412 WPHolder *mplex_prepare_manage(WMPlex *mplex, const WClientWin *cwin,
1413                                const WManageParams *param, int redir)
1414 {
1415     WMPlexAttachParams ap;
1416     WPHolder *ph=NULL;
1417     WMPlexPHolder *mph;
1418     WLListNode *after;
1419     
1420     if(redir==MANAGE_REDIR_STRICT_YES || redir==MANAGE_REDIR_PREFER_YES){
1421         WStacking *cur=mplex_current_node(mplex);
1422         
1423         if(cur!=NULL){
1424             ph=region_prepare_manage(cur->reg, cwin, param,
1425                                      MANAGE_REDIR_PREFER_YES);
1426             if(ph!=NULL)
1427                 return ph;
1428         }
1429         
1430         if(mplex->mx_current!=NULL && mplex->mx_current->st!=cur){
1431             ph=region_prepare_manage(mplex->mx_current->st->reg, 
1432                                      cwin, param, 
1433                                      MANAGE_REDIR_PREFER_YES);
1434             if(ph!=NULL)
1435                 return ph;
1436         }
1437     }
1438     
1439     if(redir==MANAGE_REDIR_STRICT_YES)
1440         return NULL;
1441     
1442     ap.flags=((param->switchto ? MPLEX_ATTACH_SWITCHTO : 0)
1443               |MPLEX_ATTACH_SIZEPOLICY);
1444     ap.szplcy=SIZEPOLICY_FULL_BOUNDS;
1445     
1446     mph=create_mplexpholder(mplex, NULL, &ap);
1447     
1448     if(mph!=NULL){
1449         WGroupedPHolder *gph=create_groupedpholder((WPHolder*)mph);
1450         if(gph!=NULL)
1451             return (WPHolder*)gph;
1452     }
1453     
1454     return (WPHolder*)mph;
1455 }
1456
1457
1458 /*}}}*/
1459
1460
1461 /*{{{ Remove */
1462
1463
1464 void mplex_managed_remove(WMPlex *mplex, WRegion *sub)
1465 {
1466     bool mx=FALSE, hadfocus=FALSE, mcf;
1467     WRegion *stdisp=(WRegion*)(mplex->stdispwatch.obj);
1468     WStacking *node, *next=NULL;
1469  
1470     mcf=region_may_control_focus((WRegion*)mplex);
1471     
1472     if(stdisp!=NULL){
1473         if(CAN_MANAGE_STDISP(sub) && 
1474            region_managed_within((WRegion*)mplex, stdisp)==sub){
1475             region_unmanage_stdisp(sub, TRUE, TRUE);
1476             region_detach_manager(stdisp);
1477         }
1478     }
1479     
1480     node=mplex_find_stacking(mplex, sub);
1481     
1482     if(node==NULL)
1483         return;
1484     
1485     hadfocus=(mplex_current_node(mplex)==node);
1486     
1487     if(node->lnode!=NULL){
1488         if(mplex->mx_current==node->lnode){
1489             WLListNode *lnext;
1490             
1491             mplex->mx_current=NULL;
1492             lnext=LIST_PREV(mplex->mx_list, node->lnode, next, prev);
1493             if(lnext==NULL){
1494                 lnext=LIST_NEXT(mplex->mx_list, node->lnode, next, prev);
1495                 if(lnext==node->lnode)
1496                     lnext=NULL;
1497             }
1498             if(lnext!=NULL)
1499                 next=lnext->st;
1500         }
1501         
1502         mplex_move_phs_before(mplex, node->lnode);
1503         llist_unlink(&(mplex->mx_list), node->lnode);
1504         mplex->mx_count--;
1505
1506         free(node->lnode);
1507         node->lnode=NULL;
1508         mx=TRUE;
1509     }
1510     
1511     UNLINK_ITEM(mplex->mgd, node, mgr_next, mgr_prev);
1512     
1513     mplex_unstack(mplex, node);
1514     
1515     stacking_unassoc(node);
1516     stacking_free(node);
1517
1518     region_unset_manager(sub, (WRegion*)mplex);
1519     
1520     if(OBJ_IS_BEING_DESTROYED(mplex))
1521         return;
1522     
1523     if(next!=NULL)
1524         mplex_do_node_display(mplex, next, FALSE);
1525
1526     if(hadfocus && mcf)
1527         mplex_refocus(mplex, next, FALSE);
1528     
1529     if(mx)
1530         mplex_managed_changed(mplex, MPLEX_CHANGE_REMOVE, next!=NULL, sub);
1531 }
1532
1533
1534 bool mplex_rescue_clientwins(WMPlex *mplex, WPHolder *ph)
1535 {
1536     bool ret1, ret2;
1537     WMPlexIterTmp tmp;
1538     
1539     mplex_iter_init(&tmp, mplex);
1540     ret1=region_rescue_some_clientwins((WRegion*)mplex, ph,
1541                                        (WRegionIterator*)mplex_iter,
1542                                        &tmp);
1543     
1544     ret2=region_rescue_child_clientwins((WRegion*)mplex, ph);
1545     
1546     return (ret1 && ret2);
1547 }
1548
1549
1550
1551 void mplex_child_removed(WMPlex *mplex, WRegion *sub)
1552 {
1553     if(sub!=NULL && sub==(WRegion*)(mplex->stdispwatch.obj)){
1554         watch_reset(&(mplex->stdispwatch));
1555         mplex_set_stdisp(mplex, NULL, NULL);
1556     }
1557 }
1558
1559
1560 /*}}}*/
1561
1562
1563 /*{{{ Status display support */
1564
1565 #ifndef offsetof
1566 # define offsetof(T,F) ((size_t)((char*)&((T*)0L)->F-(char*)0L))
1567 #endif
1568
1569 #define STRUCTOF(T, F, FADDR) \
1570         ((T*)((char*)(FADDR)-offsetof(T, F)))
1571
1572
1573 static void stdisp_watch_handler(Watch *watch, Obj *obj)
1574 {
1575     /*WMPlex *mplex=STRUCTOF(WMPlex, stdispinfo, 
1576      STRUCTOF(WMPlexSTDispInfo, regwatch, watch));
1577      WMPlexSTDispInfo *di=&(mplex->stdispinfo);
1578      WGenWS *ws=OBJ_CAST(REGION_MANAGER(obj), WGenWS);
1579      * 
1580      if(ioncore_g.opmode!=IONCORE_OPMODE_DEINIT && ws!=NULL)
1581      genws_unmanage_stdisp(ws, TRUE, FALSE);*/
1582 }
1583
1584
1585 bool mplex_set_stdisp(WMPlex *mplex, WRegion *reg, 
1586                       const WMPlexSTDispInfo *din)
1587 {
1588     WRegion *oldstdisp=(WRegion*)(mplex->stdispwatch.obj);
1589     WRegion *mgr=NULL;
1590     
1591     assert(reg==NULL || (reg==oldstdisp) ||
1592            (REGION_MANAGER(reg)==NULL && 
1593             REGION_PARENT(reg)==(WWindow*)mplex));
1594     
1595     if(oldstdisp!=NULL){
1596         mgr=region_managed_within((WRegion*)mplex, oldstdisp);
1597         
1598         if(!CAN_MANAGE_STDISP(mgr))
1599             mgr=NULL;
1600         
1601         if(oldstdisp!=reg){
1602             mainloop_defer_destroy((Obj*)oldstdisp);
1603             watch_reset(&(mplex->stdispwatch));
1604         }
1605     }
1606     
1607     if(din!=NULL)
1608         mplex->stdispinfo=*din;
1609     
1610     if(reg==NULL){
1611         if(mgr!=NULL){
1612             region_unmanage_stdisp(mgr, TRUE, FALSE);
1613             if(oldstdisp!=NULL)
1614                 region_detach_manager(oldstdisp);
1615         }
1616     }else{
1617         watch_setup(&(mplex->stdispwatch), (Obj*)reg, stdisp_watch_handler);
1618         
1619         mplex_remanage_stdisp(mplex);
1620     }
1621     
1622     return TRUE;
1623 }
1624
1625
1626 void mplex_get_stdisp(WMPlex *mplex, WRegion **reg, WMPlexSTDispInfo *di)
1627 {
1628     *di=mplex->stdispinfo;
1629     *reg=(WRegion*)mplex->stdispwatch.obj;
1630 }
1631
1632
1633 static StringIntMap pos_map[]={
1634     {"tl", MPLEX_STDISP_TL},
1635     {"tr", MPLEX_STDISP_TR},
1636     {"bl", MPLEX_STDISP_BL},
1637     {"br", MPLEX_STDISP_BR},
1638     {NULL, 0}
1639 };
1640
1641
1642 static bool do_attach_stdisp(WRegion *mplex, WRegion *reg, void *unused)
1643 {
1644     /* We do not actually manage the stdisp. */
1645     return TRUE;
1646 }
1647
1648
1649 /*EXTL_DOC
1650  * Set/create status display for \var{mplex}. Table is a standard
1651  * description of the object to be created (as passed to e.g. 
1652  * \fnref{WMPlex.attach_new}). In addition, the following fields are
1653  * recognised:
1654  * 
1655  * \begin{tabularx}{\linewidth}{lX}
1656  *   \tabhead{Field & Description}
1657  *   \var{pos} & The corner of the screen to place the status display
1658  *               in. One of \code{tl}, \code{tr}, \var{bl} or \var{br}. \\
1659  *   \var{action} & If this field is set to \code{keep}, \var{corner}
1660  *                  and \var{orientation} are changed for the existing
1661  *                  status display. If this field is set to \var{remove},
1662  *                  the existing status display is removed. If this
1663  *                  field is not set or is set to \code{replace}, a 
1664  *                  new status display is created and the old, if any,
1665  *                  removed. \\
1666  * \end{tabularx}
1667  */
1668 EXTL_EXPORT_AS(WMPlex, set_stdisp)
1669 WRegion *mplex_set_stdisp_extl(WMPlex *mplex, ExtlTab t)
1670 {
1671     WRegion *stdisp=NULL;
1672     WMPlexSTDispInfo din=mplex->stdispinfo;
1673     char *s;
1674     
1675     if(extl_table_gets_s(t, "pos", &s)){
1676         din.pos=stringintmap_value(pos_map, s, -1);
1677         if(din.pos<0){
1678             warn(TR("Invalid position setting."));
1679             return NULL;
1680         }
1681     }
1682     
1683     extl_table_gets_b(t, "fullsize", &(din.fullsize));
1684     
1685     s=NULL;
1686     extl_table_gets_s(t, "action", &s);
1687     
1688     if(s==NULL || strcmp(s, "replace")==0){
1689         WRegionAttachData data;
1690         WFitParams fp;
1691         int o2;
1692         
1693         fp.g.x=0;
1694         fp.g.y=0;
1695         fp.g.w=REGION_GEOM(mplex).w;
1696         fp.g.h=REGION_GEOM(mplex).h;
1697         fp.mode=REGION_FIT_BOUNDS|REGION_FIT_WHATEVER;
1698         
1699         /* Full mplex size is stupid so use saved geometry initially
1700          * if there's one.
1701          */
1702         extl_table_gets_rectangle(t, "geom", &(fp.g));
1703         
1704         data.type=REGION_ATTACH_LOAD;
1705         data.u.tab=t;
1706             
1707         stdisp=region_attach_helper((WRegion*)mplex, 
1708                                     (WWindow*)mplex, &fp,
1709                                     do_attach_stdisp, NULL,
1710                                     &data);
1711         
1712         if(stdisp==NULL)
1713             return NULL;
1714         
1715     }else if(strcmp(s, "keep")==0){
1716         stdisp=(WRegion*)(mplex->stdispwatch.obj);
1717     }else if(strcmp(s, "remove")!=0){
1718         warn(TR("Invalid action setting."));
1719         return FALSE;
1720     }
1721     
1722     if(!mplex_set_stdisp(mplex, stdisp, &din)){
1723         destroy_obj((Obj*)stdisp);
1724         return NULL;
1725     }
1726     
1727     return stdisp;
1728 }
1729
1730
1731 static ExtlTab mplex_do_get_stdisp_extl(WMPlex *mplex, bool fullconfig)
1732 {
1733     WRegion *reg=(WRegion*)mplex->stdispwatch.obj;
1734     ExtlTab t;
1735     
1736     if(reg==NULL)
1737         return extl_table_none();
1738     
1739     if(fullconfig){
1740         t=region_get_configuration(reg);
1741         extl_table_sets_rectangle(t, "geom", &REGION_GEOM(reg));
1742     }else{
1743         t=extl_create_table();
1744         extl_table_sets_o(t, "reg", (Obj*)reg);
1745     }
1746     
1747     if(t!=extl_table_none()){
1748         WMPlexSTDispInfo *di=&(mplex->stdispinfo);
1749         extl_table_sets_s(t, "pos", stringintmap_key(pos_map, di->pos, NULL));
1750         extl_table_sets_b(t, "fullsize", di->fullsize);
1751     }
1752     return t;
1753 }
1754
1755
1756 /*EXTL_DOC
1757  * Get status display information. See \fnref{WMPlex.get_stdisp} for
1758  * information on the fields.
1759  */
1760 EXTL_SAFE
1761 EXTL_EXPORT_AS(WMPlex, get_stdisp)
1762 ExtlTab mplex_get_stdisp_extl(WMPlex *mplex)
1763 {
1764     return mplex_do_get_stdisp_extl(mplex, FALSE);
1765 }
1766
1767
1768 /*}}}*/
1769
1770
1771 /*{{{ Dynfuns */
1772
1773
1774 void mplex_managed_geom_default(const WMPlex *mplex, WRectangle *geom)
1775 {
1776     geom->x=0;
1777     geom->y=0;
1778     geom->w=REGION_GEOM(mplex).w;
1779     geom->h=REGION_GEOM(mplex).h;
1780 }
1781
1782
1783 void mplex_managed_geom(const WMPlex *mplex, WRectangle *geom)
1784 {
1785     CALL_DYN(mplex_managed_geom, mplex, (mplex, geom));
1786 }
1787
1788
1789 void mplex_size_changed(WMPlex *mplex, bool wchg, bool hchg)
1790 {
1791     CALL_DYN(mplex_size_changed, mplex, (mplex, wchg, hchg));
1792 }
1793
1794
1795 void mplex_managed_changed(WMPlex *mplex, int mode, bool sw, WRegion *mgd)
1796 {
1797     CALL_DYN(mplex_managed_changed, mplex, (mplex, mode, sw, mgd));
1798 }
1799
1800
1801 int mplex_default_index(WMPlex *mplex)
1802 {
1803     int idx=LLIST_INDEX_LAST;
1804     CALL_DYN_RET(idx, int, mplex_default_index, mplex, (mplex));
1805     return idx;
1806 }
1807
1808
1809 /* For regions managing stdisps */
1810
1811 void region_manage_stdisp(WRegion *reg, WRegion *stdisp, 
1812                           const WMPlexSTDispInfo *info)
1813 {
1814     CALL_DYN(region_manage_stdisp, reg, (reg, stdisp, info));
1815 }
1816
1817
1818 void region_unmanage_stdisp(WRegion *reg, bool permanent, bool nofocus)
1819 {
1820     CALL_DYN(region_unmanage_stdisp, reg, (reg, permanent, nofocus));
1821 }
1822
1823
1824 /*}}}*/
1825
1826
1827 /*{{{ Changed hook helper */
1828
1829
1830 static const char *mode2str(int mode)
1831 {
1832     if(mode==MPLEX_CHANGE_SWITCHONLY)
1833         return "switchonly";
1834     else if(mode==MPLEX_CHANGE_REORDER)
1835         return "reorder";
1836     else if(mode==MPLEX_CHANGE_ADD)
1837         return "add";
1838     else if(mode==MPLEX_CHANGE_REMOVE)
1839         return "remove";
1840     return NULL;
1841 }
1842     
1843
1844 static bool mrsh_chg(ExtlFn fn, WMPlexChangedParams *p)
1845 {
1846     ExtlTab t=extl_create_table();
1847     bool ret;
1848     
1849     extl_table_sets_o(t, "reg", (Obj*)p->reg);
1850     extl_table_sets_s(t, "mode", mode2str(p->mode));
1851     extl_table_sets_b(t, "sw", p->sw);
1852     extl_table_sets_o(t, "sub", (Obj*)p->sub);
1853     
1854     extl_protect(NULL);
1855     ret=extl_call(fn, "t", NULL, t);
1856     extl_unprotect(NULL);
1857     
1858     extl_unref_table(t);
1859     
1860     return ret;
1861 }
1862
1863
1864 void mplex_call_changed_hook(WMPlex *mplex, WHook *hook, 
1865                              int mode, bool sw, WRegion *reg)
1866 {
1867     WMPlexChangedParams p;
1868     
1869     p.reg=mplex;
1870     p.mode=mode;
1871     p.sw=sw;
1872     p.sub=reg;
1873
1874     hook_call_p(hook, &p, (WHookMarshallExtl*)mrsh_chg);
1875 }
1876
1877
1878 /*}}} */
1879
1880
1881 /*{{{ Save/load */
1882
1883
1884 static void save_node(WMPlex *mplex, ExtlTab subs, int *n, 
1885                       WStacking *node, bool unnumbered)
1886 {
1887     ExtlTab st, g;
1888     
1889     st=region_get_configuration(node->reg);
1890     
1891     if(st!=extl_table_none()){
1892         /*"TODO: better switchto saving? */
1893         if(mplex->mx_current!=NULL && node==mplex->mx_current->st)
1894             extl_table_sets_b(st, "switchto", TRUE);
1895         extl_table_sets_i(st, "sizepolicy", node->szplcy);
1896         extl_table_sets_i(st, "level", node->level);
1897         g=extl_table_from_rectangle(&REGION_GEOM(node->reg));
1898         extl_table_sets_t(st, "geom", g);
1899         extl_unref_table(g);
1900         if(STACKING_IS_HIDDEN(node))
1901             extl_table_sets_b(st, "hidden", TRUE);
1902         if(unnumbered)
1903             extl_table_sets_b(st, "unnumbered", TRUE);
1904         
1905         extl_table_seti_t(subs, ++(*n), st);
1906         extl_unref_table(st);
1907     }
1908 }
1909
1910
1911 ExtlTab mplex_get_configuration(WMPlex *mplex)
1912 {
1913     ExtlTab tab, subs, stdisptab;
1914     WMPlexIterTmp tmp;
1915     WLListIterTmp ltmp;
1916     WLListNode *lnode;
1917     WStacking *node;
1918     int n=0;
1919     
1920     tab=region_get_base_configuration((WRegion*)mplex);
1921     
1922     subs=extl_create_table();
1923     extl_table_sets_t(tab, "subs", subs);
1924     
1925     /* First the numbered/mutually exclusive nodes */
1926     FOR_ALL_NODES_ON_LLIST(lnode, mplex->mx_list, ltmp){
1927         save_node(mplex, subs, &n, lnode->st, FALSE);
1928     }
1929
1930     FOR_ALL_NODES_IN_MPLEX(mplex, node, tmp){
1931         if(node->lnode==NULL)
1932             save_node(mplex, subs, &n, node, TRUE);
1933     }
1934
1935     extl_unref_table(subs);
1936     
1937     /*stdisptab=mplex_do_get_stdisp_extl(mplex, TRUE);
1938     if(stdisptab!=extl_table_none()){
1939         extl_table_sets_t(tab, "stdisp", stdisptab);
1940         extl_unref_table(stdisptab);
1941     }*/
1942     
1943     return tab;
1944 }
1945
1946
1947 static WMPlex *tmp_mplex=NULL;
1948 static WMPlexAttachParams *tmp_par=NULL;
1949
1950 static WPHolder *pholder_callback()
1951 {
1952     assert(tmp_mplex!=NULL);
1953     return (WPHolder*)create_mplexpholder(tmp_mplex, NULL, tmp_par);
1954 }
1955
1956
1957 void mplex_load_contents(WMPlex *mplex, ExtlTab tab)
1958 {
1959     ExtlTab substab, subtab;
1960     int n, i;
1961
1962     /*if(extl_table_gets_t(tab, "stdisp", &subtab)){
1963         mplex_set_stdisp_extl(mplex, subtab);
1964         extl_unref_table(subtab);
1965     }*/
1966     
1967     if(extl_table_gets_t(tab, "subs", &substab)){
1968         n=extl_table_get_n(substab);
1969         for(i=1; i<=n; i++){
1970             if(extl_table_geti_t(substab, i, &subtab)){
1971                 /*mplex_attach_new(mplex, subtab);*/
1972                 WMPlexAttachParams par;
1973                 WRegionAttachData data;
1974                 char *tmp=NULL;
1975                 
1976                 get_params(mplex, subtab, &par);
1977                 
1978                 par.flags|=MPLEX_ATTACH_INDEX;
1979                 par.index=LLIST_INDEX_LAST;
1980                 
1981                 tmp_par=&par;
1982                 tmp_mplex=mplex;
1983                 
1984                 data.type=REGION_ATTACH_LOAD;
1985                 data.u.tab=subtab;
1986                 
1987                 ioncore_set_sm_pholder_callback(pholder_callback);
1988     
1989                 mplex_do_attach(mplex, &par, &data);
1990
1991                 tmp_mplex=NULL;
1992                 
1993                 extl_unref_table(subtab);
1994             }
1995         }
1996         extl_unref_table(substab);
1997     }
1998 }
1999
2000
2001 WRegion *mplex_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
2002 {
2003     WMPlex *mplex=create_mplex(par, fp);
2004     if(mplex!=NULL)
2005         mplex_load_contents(mplex, tab);
2006     return (WRegion*)mplex;
2007 }
2008
2009
2010 /*}}}*/
2011
2012
2013 /*{{{ Dynfuntab and class info */
2014
2015
2016 static DynFunTab mplex_dynfuntab[]={
2017     {region_do_set_focus, 
2018      mplex_do_set_focus},
2019     
2020     {region_managed_remove, 
2021      mplex_managed_remove},
2022     
2023     {region_managed_rqgeom,
2024      mplex_managed_rqgeom},
2025     
2026     {(DynFun*)region_managed_prepare_focus,
2027      (DynFun*)mplex_managed_prepare_focus},
2028     
2029     {(DynFun*)region_handle_drop, 
2030      (DynFun*)mplex_handle_drop},
2031     
2032     {region_map, mplex_map},
2033     {region_unmap, mplex_unmap},
2034     
2035     {(DynFun*)region_prepare_manage,
2036      (DynFun*)mplex_prepare_manage},
2037     
2038     {(DynFun*)region_current,
2039      (DynFun*)mplex_current},
2040     
2041     {(DynFun*)region_rescue_clientwins,
2042      (DynFun*)mplex_rescue_clientwins},
2043     
2044     {(DynFun*)region_get_configuration,
2045      (DynFun*)mplex_get_configuration},
2046     
2047     {mplex_managed_geom, 
2048      mplex_managed_geom_default},
2049     
2050     {(DynFun*)region_fitrep,
2051      (DynFun*)mplex_fitrep},
2052     
2053     {region_child_removed,
2054      mplex_child_removed},
2055     
2056     {(DynFun*)region_managed_get_pholder,
2057      (DynFun*)mplex_managed_get_pholder},
2058
2059     {(DynFun*)region_get_rescue_pholder_for,
2060      (DynFun*)mplex_get_rescue_pholder_for},
2061
2062     {(DynFun*)region_may_destroy,
2063      (DynFun*)mplex_may_destroy},
2064     
2065     {(DynFun*)region_navi_first,
2066      (DynFun*)mplex_navi_first},
2067     
2068     {(DynFun*)region_navi_next,
2069      (DynFun*)mplex_navi_next},
2070     
2071     {(DynFun*)region_managed_rqorder,
2072      (DynFun*)mplex_managed_rqorder},
2073     
2074     END_DYNFUNTAB
2075 };
2076
2077
2078 EXTL_EXPORT
2079 IMPLCLASS(WMPlex, WWindow, mplex_deinit, mplex_dynfuntab);
2080
2081
2082 /*}}}*/
2083