]> git.decadent.org.uk Git - ion3.git/blob - mod_dock/dock.c
63e9117c13443a0b5357e8330d46f674681ca549
[ion3.git] / mod_dock / dock.c
1 /*
2  * Ion dock module
3  * Copyright (C) 2003 Tom Payne
4  * Copyright (C) 2003 Per Olofsson
5  * Copyright (C) 2004-2007 Tuomo Valkonen
6  *
7  * by Tom Payne <ion@tompayne.org>
8  * based on code by Per Olofsson <pelle@dsv.su.se>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * $Header: /home/twp/cvsroot/twp/ion/ion-devel-dock/dock.c,v 1.17 2003/12/21 11:59:48 twp Exp $
25  *
26  */
27
28
29 /*{{{ Includes */
30
31 #include <limits.h>
32 #include <string.h>
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xutil.h>
36 #include <X11/extensions/shape.h>
37 #include <X11/extensions/Xext.h>
38
39 #include <libtu/objp.h>
40 #include <libtu/map.h>
41 #include <libtu/minmax.h>
42 #include <libextl/extl.h>
43 #include <libextl/readconfig.h>
44 #include <libmainloop/defer.h>
45
46 #include <ioncore/common.h>
47 #include <ioncore/clientwin.h>
48 #include <ioncore/eventh.h>
49 #include <ioncore/global.h>
50 #include <ioncore/manage.h>
51 #include <ioncore/names.h>
52 #include <ioncore/property.h>
53 #include <ioncore/resize.h>
54 #include <ioncore/window.h>
55 #include <ioncore/mplex.h>
56 #include <ioncore/saveload.h>
57 #include <ioncore/bindmaps.h>
58 #include <ioncore/regbind.h>
59 #include <ioncore/extlconv.h>
60 #include <ioncore/event.h>
61 #include <ioncore/resize.h>
62 #include <ioncore/sizehint.h>
63 #include <ioncore/basicpholder.h>
64
65 #include "exports.h"
66
67 /*}}}*/
68
69
70 /*{{{ Macros */
71
72 #ifdef __GNUC__
73 #define UNUSED __attribute__ ((unused))
74 #else
75 #define UNUSED
76 #endif
77
78 /*}}}*/
79
80
81 /*{{{ Variables */
82
83 #include "../version.h"
84
85 static const char *modname="dock";
86 const char mod_dock_ion_api_version[]=ION_API_VERSION;
87
88 static bool shape_extension=FALSE;
89 static int shape_event_basep=0;
90 static int shape_error_basep=0;
91
92 static WBindmap *dock_bindmap=NULL;
93
94 /*}}}*/
95
96
97 /*{{{ Classes */
98
99 INTRSTRUCT(WDockParam);
100 INTRSTRUCT(WDockApp);
101 INTRCLASS(WDock);
102
103 DECLSTRUCT(WDockParam){
104     const char *key;
105     const char *desc;
106     const StringIntMap *map;
107     int dflt;
108 };
109
110 DECLSTRUCT(WDockApp){
111     WDockApp *next, *prev;
112     WRegion *reg;
113     int pos;
114     bool draw_border;
115     bool tile;
116     WRectangle geom;
117     WRectangle tile_geom;
118     WRectangle border_geom;
119 };
120
121 DECLCLASS(WDock){
122     WWindow win;
123     WDock *dock_next, *dock_prev;
124     int pos, grow;
125     bool is_auto;
126     GrBrush *brush;
127     WDockApp *dockapps;
128     
129     int min_w, min_h;
130     int max_w, max_h;
131     
132     bool arrange_called;
133     bool save;
134 };
135
136 static WDock *docks=NULL;
137
138 /*}}}*/
139
140
141 /*{{{ Parameter conversion */
142
143 static void dock_param_extl_table_get(const WDockParam *param, 
144                                       ExtlTab conftab, int value)
145 {
146     const char *s;
147
148     s=stringintmap_key(param->map, value, NULL);
149     if(s){
150         extl_table_sets_s(conftab, param->key, s);
151     }
152
153 }
154
155
156 static bool dock_param_do_set(const WDockParam *param, char *s,
157                               int *ret)
158 {
159     bool changed=FALSE;
160     int i=stringintmap_value(param->map, s, -1);
161     if(i<0){
162         warn_obj(modname, "Invalid %s \"%s\"", param->desc, s);
163     }else{
164         if(*ret!=i){
165             changed=TRUE;
166         }
167         *ret=i;
168     }
169     free(s);
170
171     return changed;
172
173 }
174
175
176 static bool dock_param_extl_table_set(const WDockParam *param, ExtlTab conftab,
177                                       int *ret)
178 {
179     char *s;
180
181     if(extl_table_gets_s(conftab, param->key, &s))
182         return dock_param_do_set(param, s, ret);
183
184     return FALSE;
185
186 }
187
188
189 static bool dock_param_brush_set(const WDockParam *param, GrBrush *brush,
190                                  int *ret)
191 {
192     char *s;
193
194     if(grbrush_get_extra(brush, param->key, 's', &s))
195         return dock_param_do_set(param, s, ret);
196
197     return FALSE;
198
199 }
200
201 /*}}}*/
202
203
204 /*{{{ Parameter descriptions */
205
206 static const WDockParam dock_param_name={
207     "name",
208     "name",
209     NULL,
210     0
211 };
212
213
214 #define DOCK_HPOS_MASK   0x000f
215 #define DOCK_HPOS_LEFT   0x0000
216 #define DOCK_HPOS_CENTER 0x0001
217 #define DOCK_HPOS_RIGHT  0x0002
218 #define DOCK_VPOS_MASK   0x00f0
219 #define DOCK_VPOS_TOP    0x0000
220 #define DOCK_VPOS_MIDDLE 0x0010
221 #define DOCK_VPOS_BOTTOM 0x0020
222
223
224 static StringIntMap dock_pos_map[]={
225     {"tl", DOCK_VPOS_TOP|DOCK_HPOS_LEFT},
226     {"tc", DOCK_VPOS_TOP|DOCK_HPOS_CENTER},
227     {"tr", DOCK_VPOS_TOP|DOCK_HPOS_RIGHT},
228     {"ml", DOCK_VPOS_MIDDLE|DOCK_HPOS_LEFT},
229     {"mc", DOCK_VPOS_MIDDLE|DOCK_HPOS_CENTER},
230     {"mr", DOCK_VPOS_MIDDLE|DOCK_HPOS_RIGHT},
231     {"bl", DOCK_VPOS_BOTTOM|DOCK_HPOS_LEFT},
232     {"bc", DOCK_VPOS_BOTTOM|DOCK_HPOS_CENTER},
233     {"br", DOCK_VPOS_BOTTOM|DOCK_HPOS_RIGHT},
234     END_STRINGINTMAP
235 };
236
237 static WDockParam dock_param_pos={
238     "pos",
239     "dock position",
240     dock_pos_map,
241     DOCK_HPOS_LEFT|DOCK_VPOS_BOTTOM
242 };
243
244
245 enum WDockGrow{
246     DOCK_GROW_UP,
247     DOCK_GROW_DOWN,
248     DOCK_GROW_LEFT,
249     DOCK_GROW_RIGHT
250 };
251
252 static StringIntMap dock_grow_map[]={
253     {"up", DOCK_GROW_UP},
254     {"down", DOCK_GROW_DOWN},
255     {"left", DOCK_GROW_LEFT},
256     {"right", DOCK_GROW_RIGHT},
257     END_STRINGINTMAP
258 };
259
260 WDockParam dock_param_grow={
261     "grow",
262     "growth direction",
263     dock_grow_map,
264     DOCK_GROW_RIGHT
265 };
266
267
268 static const WDockParam dock_param_is_auto={
269     "is_auto",
270     "is automatic",
271     NULL,
272     TRUE
273 };
274
275
276 enum WDockOutlineStyle{
277     DOCK_OUTLINE_STYLE_NONE,
278     DOCK_OUTLINE_STYLE_ALL,
279     DOCK_OUTLINE_STYLE_EACH
280 };
281
282 static StringIntMap dock_outline_style_map[]={
283     {"none", DOCK_OUTLINE_STYLE_NONE},
284     {"all", DOCK_OUTLINE_STYLE_ALL},
285     {"each", DOCK_OUTLINE_STYLE_EACH},
286     END_STRINGINTMAP
287 };
288
289 WDockParam dock_param_outline_style={
290     "outline_style",
291     "outline style",
292     dock_outline_style_map,
293     DOCK_OUTLINE_STYLE_ALL
294 };
295
296
297 static const WDockParam dock_param_tile_width={
298     "width",
299     "tile width",
300     NULL,
301     64
302 };
303
304 static const WDockParam dock_param_tile_height={
305     "height",
306     "tile height",
307     NULL,
308     64
309 };
310
311
312 /*}}}*/
313
314
315 /*{{{ Misc. */
316
317 #define CLIENTWIN_WINPROP_POSITION "dockposition"
318 #define CLIENTWIN_WINPROP_BORDER "dockborder"
319
320 static WDockApp *dock_find_dockapp(WDock *dock, WRegion *reg)
321 {
322     WDockApp *dockapp;
323
324     for(dockapp=dock->dockapps; dockapp!=NULL; dockapp=dockapp->next){
325         if(dockapp->reg==reg){
326             return dockapp;
327         }
328     }
329
330     return NULL;
331
332 }
333
334
335 static void dock_get_outline_style(WDock *dock, int *ret)
336 {
337
338     *ret=dock_param_outline_style.dflt;
339     if(dock->brush!=NULL)
340         dock_param_brush_set(&dock_param_outline_style, dock->brush, ret);
341
342 }
343
344 /*}}}*/
345
346
347 /*{{{ Size calculation */
348
349
350 static void dock_get_tile_size(WDock *dock, WRectangle *ret)
351 {
352     ExtlTab tile_size_table;
353
354     ret->x=0;
355     ret->y=0;
356     ret->w=dock_param_tile_width.dflt;
357     ret->h=dock_param_tile_height.dflt;
358     if(dock->brush==NULL)
359         return;
360     if(grbrush_get_extra(dock->brush, "tile_size", 't', &tile_size_table)){
361         extl_table_gets_i(tile_size_table, dock_param_tile_width.key, &ret->w);
362         extl_table_gets_i(tile_size_table, dock_param_tile_height.key, &ret->h);
363         extl_unref_table(tile_size_table);
364     }
365
366 }
367
368
369 static void dock_get_pos_grow(WDock *dock, int *pos, int *grow)
370 {
371     WMPlex *mplex=OBJ_CAST(REGION_PARENT(dock), WMPlex);
372     WRegion *mplex_stdisp;
373     WMPlexSTDispInfo din;
374     
375     if(mplex!=NULL){
376         mplex_get_stdisp(mplex, &mplex_stdisp, &din);
377         if(mplex_stdisp==(WRegion*)dock){
378             /* Ok, we're assigned as a status display for mplex, so
379              * get parameters from there.
380              */
381             *pos=((din.pos==MPLEX_STDISP_TL || din.pos==MPLEX_STDISP_BL)
382                   ? DOCK_HPOS_LEFT
383                   : DOCK_HPOS_RIGHT) 
384                 | ((din.pos==MPLEX_STDISP_TL || din.pos==MPLEX_STDISP_TR)
385                    ? DOCK_VPOS_TOP
386                    : DOCK_VPOS_BOTTOM);
387             *grow=dock->grow;
388             return;
389         }
390     }
391     
392     *grow=dock->grow;
393     *pos=dock->pos;
394 }
395
396
397
398 static void dock_reshape(WDock *dock)
399 {
400     int outline_style;
401     
402     if(!shape_extension){
403         return;
404     }
405     
406     dock_get_outline_style(dock, &outline_style);
407     
408     switch(outline_style){
409     case DOCK_OUTLINE_STYLE_NONE:
410     case DOCK_OUTLINE_STYLE_EACH:
411         {
412             WDockApp *dockapp;
413             
414             /* Start with an empty set */
415             XShapeCombineRectangles(ioncore_g.dpy, ((WWindow*)dock)->win,
416                                     ShapeBounding, 0, 0, NULL, 0, ShapeSet, 0);
417             
418             /* Union with dockapp shapes */
419             for(dockapp=dock->dockapps; dockapp!=NULL; dockapp=dockapp->next){
420                 WClientWin *cwin=OBJ_CAST(dockapp->reg, WClientWin);
421                 if(outline_style==DOCK_OUTLINE_STYLE_EACH
422                    && dockapp->draw_border){
423                     /* Union with border shape */
424                     XRectangle tile_rect;
425                     
426                     tile_rect.x=dockapp->border_geom.x;
427                     tile_rect.y=dockapp->border_geom.y;
428                     tile_rect.width=dockapp->border_geom.w;
429                     tile_rect.height=dockapp->border_geom.h;
430                     XShapeCombineRectangles(ioncore_g.dpy, ((WWindow*)dock)->win,
431                                             ShapeBounding, 0, 0, &tile_rect, 1,
432                                             ShapeUnion, 0);
433                 }else if(cwin!=NULL){
434                     /* Union with dockapp shape */
435                     int count;
436                     int ordering;
437                     
438                     XRectangle *rects=XShapeGetRectangles(ioncore_g.dpy, cwin->win,
439                                                           ShapeBounding, &count,
440                                                           &ordering);
441                     if(rects!=NULL){
442                         WRectangle dockapp_geom=REGION_GEOM(cwin);
443                         XShapeCombineRectangles(ioncore_g.dpy, ((WWindow*)dock)->win,
444                                                 ShapeBounding,
445                                                 dockapp_geom.x, dockapp_geom.y,
446                                                 rects, count, ShapeUnion, ordering);
447                         XFree(rects);
448                     }
449                 }
450             }
451         }
452         break;
453         
454     case DOCK_OUTLINE_STYLE_ALL:
455         {
456             WRectangle geom;
457             XRectangle rect;
458             
459             geom=REGION_GEOM(dock);
460             rect.x=0;
461             rect.y=0;
462             rect.width=geom.w;
463             rect.height=geom.h;
464             XShapeCombineRectangles(ioncore_g.dpy, ((WWindow*)dock)->win,
465                                     ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
466         }
467         break;
468     }
469     
470 }
471
472
473 static void dock_arrange_dockapps(WDock *dock, const WRectangle *bd_dockg, 
474                                   const WDockApp *replace_this, 
475                                   WDockApp *with_this)
476 {
477     GrBorderWidths dock_bdw, dockapp_bdw;
478     WDockApp dummy_copy, *dockapp;
479     int pos, grow, cur_coord=0;
480     WRectangle dock_geom;
481
482     dock->arrange_called=TRUE;
483     
484     dock_get_pos_grow(dock, &pos, &grow);
485
486     /* Determine dock and dockapp border widths */
487     memset(&dock_bdw, 0, sizeof(GrBorderWidths));
488     memset(&dockapp_bdw, 0, sizeof(GrBorderWidths));
489     
490     if(dock->brush){
491         int outline_style;
492
493         dock_get_outline_style(dock, &outline_style);
494         switch(outline_style){
495         case DOCK_OUTLINE_STYLE_NONE:
496             break;
497         case DOCK_OUTLINE_STYLE_ALL:
498             grbrush_get_border_widths(dock->brush, &dock_bdw);
499             dockapp_bdw.spacing=dock_bdw.spacing;
500             break;
501         case DOCK_OUTLINE_STYLE_EACH:
502             grbrush_get_border_widths(dock->brush, &dockapp_bdw);
503             break;
504         }
505     }
506     
507     dock_geom.w=bd_dockg->w-dock_bdw.left-dock_bdw.right;
508     dock_geom.h=bd_dockg->h-dock_bdw.top-dock_bdw.bottom;
509
510     /* Calculate initial co-ordinate for layout algorithm */
511     switch(grow){
512     case DOCK_GROW_UP:
513         cur_coord=dock_bdw.top+dock_geom.h;
514         break;
515     case DOCK_GROW_DOWN:
516         cur_coord=dock_bdw.top;
517         break;
518     case DOCK_GROW_LEFT:
519         cur_coord=dock_bdw.left+dock_geom.w;
520         break;
521     case DOCK_GROW_RIGHT:
522         cur_coord=dock_bdw.left;
523         break;
524     }
525
526     /* Arrange dockapps */
527     for(dockapp=dock->dockapps; dockapp!=NULL; dockapp=dockapp->next){
528         WDockApp *da=dockapp;
529         
530         if(replace_this!=NULL){
531             if(replace_this==dockapp){
532                 da=with_this;
533             }else{
534                 dummy_copy=*dockapp;
535                 da=&dummy_copy;
536             }
537         }
538             
539         /* Calculate first co-ordinate */
540         switch(grow){
541         case DOCK_GROW_UP:
542         case DOCK_GROW_DOWN:
543             switch(pos&DOCK_HPOS_MASK){
544             case DOCK_HPOS_LEFT:
545                 da->border_geom.x=0;
546                 break;
547             case DOCK_HPOS_CENTER:
548                 da->border_geom.x=(dock_geom.w-da->border_geom.w)/2;
549                 break;
550             case DOCK_HPOS_RIGHT:
551                 da->border_geom.x=dock_geom.w-da->border_geom.w;
552                 break;
553             }
554             da->border_geom.x+=dock_bdw.left;
555             break;
556         case DOCK_GROW_LEFT:
557         case DOCK_GROW_RIGHT:
558             switch(pos&DOCK_VPOS_MASK){
559             case DOCK_VPOS_TOP:
560                 da->border_geom.y=0;
561                 break;
562             case DOCK_VPOS_MIDDLE:
563                 da->border_geom.y=(dock_geom.h-da->border_geom.h)/2;
564                 break;
565             case DOCK_VPOS_BOTTOM:
566                 da->border_geom.y=dock_geom.h-da->border_geom.h;
567                 break;
568             }
569             da->border_geom.y+=dock_bdw.top;
570             break;
571         }
572
573         /* Calculate second co-ordinate */
574         switch(grow){
575         case DOCK_GROW_UP:
576             cur_coord-=da->border_geom.h;
577             da->border_geom.y=cur_coord;
578             cur_coord-=dockapp_bdw.spacing;
579             break;
580         case DOCK_GROW_DOWN:
581             da->border_geom.y=cur_coord;
582             cur_coord+=da->border_geom.h+dockapp_bdw.spacing;
583             break;
584         case DOCK_GROW_LEFT:
585             cur_coord-=da->border_geom.w;
586             da->border_geom.x=cur_coord;
587             cur_coord-=dockapp_bdw.spacing;
588             break;
589         case DOCK_GROW_RIGHT:
590             da->border_geom.x=cur_coord;
591             cur_coord+=da->border_geom.w+dockapp_bdw.spacing;
592             break;
593         }
594
595         /* Calculate tile geom */
596         da->tile_geom.x=da->border_geom.x+dockapp_bdw.left;
597         da->tile_geom.y=da->border_geom.y+dockapp_bdw.top;
598
599         /* Calculate dockapp geom */
600         if(da->tile){
601             da->geom.x=da->tile_geom.x+(da->tile_geom.w-da->geom.w)/2;
602             da->geom.y=da->tile_geom.y+(da->tile_geom.h-da->geom.h)/2;
603         }else{
604             da->geom.x=da->tile_geom.x;
605             da->geom.y=da->tile_geom.y;
606         }
607         
608         if(replace_this==NULL)
609             region_fit(da->reg, &(da->geom), REGION_FIT_BOUNDS);
610     }
611 }
612
613
614 static void dock_set_minmax(WDock *dock, int grow, const WRectangle *g)
615 {
616     dock->min_w=g->w;
617     dock->min_h=g->h;
618     if(grow==DOCK_GROW_UP || grow==DOCK_GROW_DOWN){
619         dock->max_w=g->w;
620         dock->max_h=INT_MAX;
621     }else{
622         dock->max_w=INT_MAX;
623         dock->max_h=g->h;
624     }
625 }
626
627
628 static void dockapp_calc_preferred_size(WDock *dock, int grow, 
629                                         const WRectangle *tile_size,
630                                         WDockApp *da)
631 {
632     int w=da->geom.w, h=da->geom.h;
633     
634     if(grow==DOCK_GROW_UP || grow==DOCK_GROW_DOWN){
635         da->geom.w=minof(w, tile_size->w);
636         da->geom.h=h;
637     }else{
638         da->geom.w=w;
639         da->geom.h=minof(h, tile_size->h);
640     }
641     
642     region_size_hints_correct(da->reg, &(da->geom.w), &(da->geom.h), TRUE);
643 }
644
645
646
647 static void dock_managed_rqgeom_(WDock *dock, WRegion *reg, int flags,
648                                  const WRectangle *geom, WRectangle *geomret,
649                                  bool just_update_minmax)
650 {
651     WDockApp *dockapp=NULL, *thisdockapp=NULL, thisdockapp_copy;
652     WRectangle parent_geom, dock_geom, border_dock_geom;
653     GrBorderWidths dock_bdw, dockapp_bdw;
654     int n_dockapps=0, max_w=1, max_h=1, total_w=0, total_h=0;
655     int pos, grow;
656     WRectangle tile_size;
657     WWindow *par=REGION_PARENT(dock);
658     
659     /* dock_resize calls with NULL parameters. */
660     assert(reg!=NULL || (geomret==NULL && !(flags&REGION_RQGEOM_TRYONLY)));
661     
662     dock_get_pos_grow(dock, &pos, &grow);
663
664     /* Determine parent and tile geoms */
665     parent_geom.x=0;
666     parent_geom.y=0;
667     if(par!=NULL){
668         parent_geom.w=REGION_GEOM(par).w;
669         parent_geom.h=REGION_GEOM(par).h;
670     }else{
671         /* Should not happen in normal operation. */
672         parent_geom.w=1;
673         parent_geom.h=1;
674     }
675     
676     dock_get_tile_size(dock, &tile_size);
677
678     /* Determine dock and dockapp border widths */
679     memset(&dock_bdw, 0, sizeof(GrBorderWidths));
680     memset(&dockapp_bdw, 0, sizeof(GrBorderWidths));
681     
682     if(dock->brush){
683         int outline_style;
684
685         dock_get_outline_style(dock, &outline_style);
686         switch(outline_style){
687         case DOCK_OUTLINE_STYLE_NONE:
688             break;
689         case DOCK_OUTLINE_STYLE_ALL:
690             grbrush_get_border_widths(dock->brush, &dock_bdw);
691             dockapp_bdw.spacing=dock_bdw.spacing;
692             break;
693         case DOCK_OUTLINE_STYLE_EACH:
694             grbrush_get_border_widths(dock->brush, &dockapp_bdw);
695             break;
696         }
697     }
698
699     /* Calculate widths and heights */
700     for(dockapp=dock->dockapps; dockapp!=NULL; dockapp=dockapp->next){
701         WDockApp *da=dockapp;
702         bool update=!(flags&REGION_RQGEOM_TRYONLY);
703         if(dockapp->reg==reg){
704             thisdockapp=dockapp;
705             if(flags&REGION_RQGEOM_TRYONLY){
706                 thisdockapp_copy=*dockapp;
707                 thisdockapp_copy.geom=*geom;
708                 da=&thisdockapp_copy;
709                 update=TRUE;
710             }
711             da->geom=*geom;
712         }
713         
714         if(update){
715             /* Calculcate preferred size */
716             dockapp_calc_preferred_size(dock, grow, &tile_size, da);
717                         
718             /* Determine whether dockapp should be placed on a tile */
719             da->tile=da->geom.w<=tile_size.w && da->geom.h<=tile_size.h;
720             
721             /* Calculate width and height */
722             if(da->tile){
723                 da->tile_geom.w=tile_size.w;
724                 da->tile_geom.h=tile_size.h;
725             }else{
726                 da->tile_geom.w=da->geom.w;
727                 da->tile_geom.h=da->geom.h;
728             }
729
730             /* Calculate border width and height */
731             da->border_geom.w=dockapp_bdw.left+da->tile_geom.w+dockapp_bdw.right;
732             da->border_geom.h=dockapp_bdw.top+da->tile_geom.h+dockapp_bdw.right;
733         }
734         
735         /* Calculate maximum and accumulated widths and heights */
736         if(da->border_geom.w>max_w)
737             max_w=da->border_geom.w;
738         total_w+=da->border_geom.w+(n_dockapps ? dockapp_bdw.spacing : 0);
739         
740         if(da->border_geom.h>max_h)
741             max_h=da->border_geom.h;
742         total_h+=da->border_geom.h+(n_dockapps ? dockapp_bdw.spacing : 0);
743         
744         /* Count dockapps */
745         ++n_dockapps;
746     }
747     
748     if(thisdockapp==NULL && reg!=NULL){
749         warn("Requesting dockapp not found.");
750         if(geomret)
751             *geomret=REGION_GEOM(reg);
752         return;
753     }
754     
755     /* Calculate width and height of dock */
756     if(n_dockapps){
757         switch(grow){
758         case DOCK_GROW_LEFT:
759         case DOCK_GROW_RIGHT:
760             dock_geom.w=total_w;
761             dock_geom.h=max_h;
762             break;
763         case DOCK_GROW_UP:
764         case DOCK_GROW_DOWN:
765         default:
766             dock_geom.w=max_w;
767             dock_geom.h=total_h;
768             break;
769         }
770     }else{
771         dock_geom.w=tile_size.w;
772         dock_geom.h=tile_size.h;
773     }
774     
775     border_dock_geom.x=REGION_GEOM(dock).x;
776     border_dock_geom.y=REGION_GEOM(dock).y;
777     border_dock_geom.w=dock_bdw.left+dock_geom.w+dock_bdw.right;
778     border_dock_geom.h=dock_bdw.top+dock_geom.h+dock_bdw.bottom;
779     
780     /* Fit dock to new geom if required */
781     if(!(flags&REGION_RQGEOM_TRYONLY)){
782         WRQGeomParams rq=RQGEOMPARAMS_INIT;
783         
784         dock_set_minmax(dock, grow, &border_dock_geom);
785         
786         if(just_update_minmax)
787             return;
788         
789         rq.flags=REGION_RQGEOM_WEAK_X|REGION_RQGEOM_WEAK_Y;
790         rq.geom=border_dock_geom;
791
792         dock->arrange_called=FALSE;
793         
794         region_rqgeom((WRegion*)dock, &rq, NULL);
795         
796         if(!dock->arrange_called)
797             dock_arrange_dockapps(dock, &REGION_GEOM(dock), NULL, NULL);
798         
799         if(thisdockapp!=NULL && geomret!=NULL)
800             *geomret=thisdockapp->geom;
801     }else{
802         if(thisdockapp!=NULL && geomret!=NULL){
803             dock_arrange_dockapps(dock, &REGION_GEOM(dock), 
804                                   thisdockapp, &thisdockapp_copy);
805             *geomret=thisdockapp_copy.geom;
806         }
807     }
808 }
809
810 static void dock_managed_rqgeom(WDock *dock, WRegion *reg, 
811                                 const WRQGeomParams *rq, 
812                                 WRectangle *geomret)
813 {
814     dock_managed_rqgeom_(dock, reg, rq->flags, &rq->geom, geomret, FALSE);
815 }
816
817
818 void dock_size_hints(WDock *dock, WSizeHints *hints)
819 {
820     hints->min_set=TRUE;
821     hints->min_width=dock->min_w;
822     hints->min_height=dock->min_h;
823     
824     hints->max_set=TRUE;
825     hints->max_width=dock->max_w;
826     hints->max_height=dock->max_h;
827 }
828
829
830 static bool dock_fitrep(WDock *dock, WWindow *parent, const WFitParams *fp)
831 {
832     if(!window_fitrep(&(dock->win), parent, fp))
833         return FALSE;
834
835     dock_arrange_dockapps(dock, &(fp->g), NULL, NULL);
836     
837     if(shape_extension)
838         dock_reshape(dock);
839     
840     return TRUE;
841 }
842
843
844 static int dock_orientation(WDock *dock)
845 {
846     return ((dock->grow==DOCK_GROW_LEFT || dock->grow==DOCK_GROW_RIGHT)
847             ? REGION_ORIENTATION_HORIZONTAL
848             : REGION_ORIENTATION_VERTICAL);
849 }
850
851
852 /*}}}*/
853
854
855 /*{{{ Drawing */
856
857
858 static void dock_draw(WDock *dock, bool complete)
859 {
860     int outline_style;
861     WRectangle g;
862     
863     if(dock->brush==NULL)
864         return;
865     
866     g.x=0;
867     g.y=0;
868     g.w=REGION_GEOM(dock).w;
869     g.h=REGION_GEOM(dock).h;
870     
871     grbrush_begin(dock->brush, &g, (complete ? 0 : GRBRUSH_NO_CLEAR_OK));
872     
873     dock_get_outline_style(dock, &outline_style);
874     switch(outline_style){
875     case DOCK_OUTLINE_STYLE_NONE:
876         break;
877     case DOCK_OUTLINE_STYLE_ALL:
878         {
879             WRectangle geom=REGION_GEOM(dock);
880             geom.x=geom.y=0;
881             grbrush_draw_border(dock->brush, &geom);
882         }
883         break;
884     case DOCK_OUTLINE_STYLE_EACH:
885         {
886             WDockApp *dockapp;
887             for(dockapp=dock->dockapps; dockapp!=NULL;
888                 dockapp=dockapp->next){
889                 grbrush_draw_border(dock->brush, &dockapp->tile_geom);
890             }
891         }
892         break;
893     }
894     
895     grbrush_end(dock->brush);
896 }
897
898
899 /*EXTL_DOC
900  * Resizes and refreshes \var{dock}.
901  */
902 EXTL_EXPORT_MEMBER
903 void dock_resize(WDock *dock)
904 {
905     dock_managed_rqgeom_(dock, NULL, 0, NULL, NULL, FALSE);
906     dock_draw(dock, TRUE);
907     
908 }
909
910 static void dock_brush_release(WDock *dock)
911 {
912     
913     if(dock->brush){
914         grbrush_release(dock->brush);
915         dock->brush=NULL;
916     }
917
918 }
919
920
921 static void dock_brush_get(WDock *dock)
922 {
923
924     dock_brush_release(dock);
925     dock->brush=gr_get_brush(((WWindow*)dock)->win, 
926                              region_rootwin_of((WRegion*)dock), 
927                              "stdisp-dock");
928 }
929
930
931 static void dock_updategr(WDock *dock)
932 {
933     dock_brush_get(dock);
934     dock_resize(dock);
935 }
936
937 /*}}}*/
938
939
940 /*{{{ Set/get */
941
942
943 static void mplexpos(int pos, int *mpos)
944 {
945     int hp=pos&DOCK_HPOS_MASK, vp=pos&DOCK_VPOS_MASK;
946     int p;
947     
948     p=(vp!=DOCK_VPOS_MIDDLE
949        ? (vp==DOCK_VPOS_TOP
950           ? (hp!=DOCK_HPOS_CENTER
951              ? (hp==DOCK_HPOS_RIGHT
952                 ? MPLEX_STDISP_TR
953                 : MPLEX_STDISP_TL)
954              : -1)
955           : (hp!=DOCK_HPOS_CENTER
956              ? (hp==DOCK_HPOS_RIGHT
957                 ? MPLEX_STDISP_BR
958                 : MPLEX_STDISP_BL)
959              : -1))
960        : -1);
961     
962     if(p==-1)
963         warn("Invalid dock position while as stdisp.");
964     else
965         *mpos=p;
966 }
967
968
969 static void mplexszplcy(int pos, WSizePolicy *szplcy)
970 {
971     int hp=pos&DOCK_HPOS_MASK, vp=pos&DOCK_VPOS_MASK;
972     WSizePolicy p;
973     
974     p=(vp!=DOCK_VPOS_MIDDLE
975        ? (vp==DOCK_VPOS_TOP
976           ? (hp!=DOCK_HPOS_CENTER
977              ? (hp==DOCK_HPOS_RIGHT
978                 ? SIZEPOLICY_GRAVITY_NORTHEAST
979                 : SIZEPOLICY_GRAVITY_NORTHWEST)
980              : SIZEPOLICY_GRAVITY_NORTH)
981           : (hp!=DOCK_HPOS_CENTER
982              ? (hp==DOCK_HPOS_RIGHT
983                 ? SIZEPOLICY_GRAVITY_SOUTHEAST
984                 : SIZEPOLICY_GRAVITY_SOUTHWEST)
985              : SIZEPOLICY_GRAVITY_SOUTH))
986        : SIZEPOLICY_GRAVITY_CENTER);
987     
988     *szplcy=p;
989 }
990     
991
992 static void dock_do_set(WDock *dock, ExtlTab conftab, bool resize)
993 {
994     char *s;
995     bool b;
996     bool growset=FALSE;
997     bool posset=FALSE;
998     bool save=FALSE;
999     
1000     if(extl_table_gets_s(conftab, dock_param_name.key, &s)){
1001         if(!region_set_name((WRegion*)dock, s)){
1002             warn_obj(modname, "Can't set name to \"%s\"", s);
1003         }
1004         free(s);
1005     }
1006
1007     if(extl_table_gets_b(conftab, "save", &save))
1008         dock->save=save;
1009     
1010     if(dock_param_extl_table_set(&dock_param_pos, conftab, &dock->pos))
1011         posset=TRUE;
1012
1013     if(dock_param_extl_table_set(&dock_param_grow, conftab, &dock->grow))
1014         growset=TRUE;
1015     
1016     if(extl_table_gets_b(conftab, dock_param_is_auto.key, &b))
1017         dock->is_auto=b;
1018
1019     if(resize && (growset || posset)){
1020         WMPlex *par=OBJ_CAST(REGION_PARENT(dock), WMPlex);
1021         WRegion *stdisp=NULL;
1022         WMPlexSTDispInfo din;
1023         
1024         if(par!=NULL){
1025             mplex_get_stdisp(par, &stdisp, &din);
1026             din.fullsize=FALSE; /* not supported. */
1027             if(stdisp==(WRegion*)dock){
1028                 if(posset)
1029                     mplexpos(dock->pos, &din.pos);
1030                 if(growset){
1031                     /* Update min/max first */
1032                     dock_managed_rqgeom_(dock, NULL, 0, NULL, NULL, TRUE);
1033                 }
1034                 mplex_set_stdisp(par, (WRegion*)dock, &din);
1035             }else if((WRegion*)par==REGION_MANAGER(dock)){
1036                 WSizePolicy szplcy;
1037                 mplexszplcy(dock->pos, &szplcy);
1038                 mplex_set_szplcy(par, (WRegion*)dock, szplcy);
1039             }
1040         }
1041         
1042         dock_resize(dock);
1043     }
1044 }
1045
1046
1047 /*EXTL_DOC
1048  * Configure \var{dock}. \var{conftab} is a table of key/value pairs:
1049  *
1050  * \begin{tabularx}{\linewidth}{llX}
1051  *  \tabhead{Key & Values & Description}
1052  *  \var{name} & string & Name of dock \\
1053  *  \var{pos} & string in $\{t,m,b\}\times\{t,c,b\}$ & Dock position. 
1054  *       Can only be used in floating mode. \\
1055  *  \var{grow} & up/down/left/right & 
1056  *       Growth direction where new dockapps are added. Also
1057  *       sets orientation for dock when working as WMPlex status
1058  *       display (see \fnref{WMPlex.set_stdisp}). \\
1059  *  \var{is_auto} & bool & 
1060  *       Should \var{dock} automatically manage new dockapps? \\
1061  * \end{tabularx}
1062  *
1063  * Any parameters not explicitly set in \var{conftab} will be left unchanged.
1064  */
1065 EXTL_EXPORT_MEMBER
1066 void dock_set(WDock *dock, ExtlTab conftab)
1067 {
1068     dock_do_set(dock, conftab, TRUE);
1069 }
1070
1071
1072 static void dock_do_get(WDock *dock, ExtlTab conftab)
1073 {
1074     extl_table_sets_s(conftab, dock_param_name.key,
1075                       region_name((WRegion*)dock));
1076     dock_param_extl_table_get(&dock_param_pos, conftab, dock->pos);
1077     dock_param_extl_table_get(&dock_param_grow, conftab, dock->grow);
1078     extl_table_sets_b(conftab, dock_param_is_auto.key, dock->is_auto);
1079     extl_table_sets_b(conftab, "save", dock->save);
1080 }
1081
1082
1083 /*EXTL_DOC
1084  * Get \var{dock}'s configuration table. See \fnref{WDock.set} for a
1085  * description of the table.
1086  */
1087 EXTL_SAFE
1088 EXTL_EXPORT_MEMBER
1089 ExtlTab dock_get(WDock *dock)
1090 {
1091     ExtlTab conftab;
1092
1093     conftab=extl_create_table();
1094     dock_do_get(dock, conftab);
1095     return conftab;
1096 }
1097
1098
1099 /*}}}*/
1100
1101
1102 /*{{{ Init/deinit */
1103
1104
1105 static bool dock_init(WDock *dock, WWindow *parent, const WFitParams *fp)
1106 {
1107     WFitParams fp2=*fp;
1108     
1109     dock->pos=dock_param_pos.dflt;
1110     dock->grow=dock_param_grow.dflt;
1111     dock->is_auto=dock_param_is_auto.dflt;
1112     dock->brush=NULL;
1113     dock->dockapps=NULL;
1114     dock->min_w=1;
1115     dock->min_h=1;
1116     dock->max_w=1;
1117     dock->max_h=1;
1118     dock->arrange_called=FALSE;
1119     dock->save=TRUE;
1120
1121     
1122     if(!window_init((WWindow*)dock, parent, &fp2))
1123         return FALSE;
1124     
1125     region_add_bindmap((WRegion*)dock, dock_bindmap);
1126
1127     window_select_input(&(dock->win), IONCORE_EVENTMASK_CWINMGR);
1128
1129     dock_brush_get(dock);
1130
1131     LINK_ITEM(docks, dock, dock_next, dock_prev);
1132     
1133     return TRUE;
1134 }
1135
1136
1137 static WDock *create_dock(WWindow *parent, const WFitParams *fp)
1138 {
1139     CREATEOBJ_IMPL(WDock, dock, (p, parent, fp));
1140 }
1141
1142
1143 static void dock_deinit(WDock *dock)
1144 {
1145     while(dock->dockapps!=NULL)
1146         destroy_obj((Obj*)dock->dockapps->reg);
1147
1148     UNLINK_ITEM(docks, dock, dock_next, dock_prev);
1149
1150     dock_brush_release(dock);
1151
1152     window_deinit((WWindow*) dock);
1153 }
1154
1155
1156 EXTL_EXPORT
1157 WDock *mod_dock_create(ExtlTab tab)
1158 {
1159     char *mode=NULL;
1160     bool floating=FALSE;
1161     int screenid=0;
1162     WScreen *screen=NULL;
1163     WDock *dock=NULL;
1164     WRegion *stdisp=NULL;
1165     WMPlexSTDispInfo din;
1166     WFitParams fp;
1167     
1168     if(extl_table_gets_s(tab, "mode", &mode)){
1169         if(strcmp(mode, "floating")==0){
1170             floating=TRUE;
1171         }else if(strcmp(mode, "embedded")!=0){
1172             warn("Invalid dock mode.");
1173             free(mode);
1174             return NULL;
1175         }
1176         free(mode);
1177     }
1178     
1179     extl_table_gets_i(tab, "screen", &screenid);
1180     screen=ioncore_find_screen_id(screenid);
1181     if(screen==NULL){
1182         warn("Screen %d does not exist.", screenid);
1183         return NULL;
1184     }
1185     
1186     for(dock=docks; dock; dock=dock->dock_next){
1187         if(region_screen_of((WRegion*)dock)==screen){
1188             warn("Screen %d already has a dock. Refusing to create another.",
1189                  screenid);
1190             return NULL;
1191         }
1192     }
1193
1194     if(!floating){
1195         mplex_get_stdisp((WMPlex*)screen, &stdisp, &din);
1196         if(stdisp!=NULL && !extl_table_is_bool_set(tab, "force")){
1197             warn("Screen %d already has an stdisp. Refusing to add embedded "
1198                  "dock.", screenid);
1199             return NULL;
1200         }
1201     }
1202
1203     /* Create the dock */
1204     fp.mode=REGION_FIT_BOUNDS|REGION_FIT_WHATEVER;
1205     fp.g.x=0;
1206     fp.g.y=0;
1207     fp.g.w=1;
1208     fp.g.h=1;
1209         
1210     dock=create_dock((WWindow*)screen, &fp);
1211
1212     if(dock==NULL){
1213         warn("Failed to create dock.");
1214         return NULL;
1215     }
1216     
1217         
1218     /* Get parameters */
1219     dock->save=FALSE;
1220     dock_do_set(dock, tab, FALSE);
1221     
1222     /* Calculate min/max size */
1223     dock_managed_rqgeom_(dock, NULL, 0, NULL, NULL, TRUE);
1224
1225     /* Final setup */    
1226     if(floating){
1227         const WRectangle *pg=&REGION_GEOM(screen);
1228         WMPlexAttachParams par=MPLEXATTACHPARAMS_INIT;
1229         WRegionAttachData data;
1230         
1231         par.flags=(MPLEX_ATTACH_UNNUMBERED
1232                    |MPLEX_ATTACH_SIZEPOLICY
1233                    |MPLEX_ATTACH_GEOM
1234                    |MPLEX_ATTACH_PASSIVE);
1235         
1236         par.geom.w=dock->min_w;
1237         par.geom.h=dock->min_h;
1238         par.geom.x=0;
1239         par.geom.y=0;
1240         
1241         mplexszplcy(dock->pos, &par.szplcy);
1242         
1243         if(extl_table_is_bool_set(tab, "floating_hidden"))
1244             par.flags|=MPLEX_ATTACH_HIDDEN;
1245         
1246         data.type=REGION_ATTACH_REPARENT;
1247         data.u.reg=(WRegion*)dock;
1248         
1249         if(mplex_do_attach((WMPlex*)screen, &par, &data))
1250             return dock;
1251     }else{
1252         mplexpos(dock->pos, &din.pos);
1253         din.fullsize=FALSE; /* not supported */
1254         if(mplex_set_stdisp((WMPlex*)screen, (WRegion*)dock, &din))
1255             return dock;
1256     }
1257
1258     /* Failed to attach. */
1259     warn("Failed to attach dock to screen.");
1260     destroy_obj((Obj*)dock);
1261     return NULL;
1262 }
1263
1264
1265 /*}}}*/
1266
1267
1268 /*{{{ Toggle */
1269
1270
1271 /*EXTL_DOC
1272  * Toggle floating docks on \var{mplex}.
1273  */
1274 EXTL_EXPORT
1275 void mod_dock_set_floating_shown_on(WMPlex *mplex, const char *how)
1276 {
1277     int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
1278     WDock *dock;
1279     
1280     for(dock=docks; dock; dock=dock->dock_next){
1281         if(REGION_MANAGER(dock)==(WRegion*)mplex)
1282             mplex_set_hidden(mplex, (WRegion*)dock, setpar);
1283     }
1284 }
1285
1286
1287 /*}}}*/
1288
1289
1290 /*{{{ Save/load */
1291
1292
1293 ExtlTab dock_get_configuration(WDock *dock)
1294 {
1295     ExtlTab tab;
1296     
1297     if(dock->save==FALSE)
1298         return extl_table_none();
1299     
1300     tab=region_get_base_configuration((WRegion*)dock);
1301     dock_do_get(dock, tab);
1302
1303     return tab;
1304 }
1305
1306
1307 WRegion *dock_load(WWindow *par, const WFitParams *fp, ExtlTab tab)
1308 {
1309     WDock *dock=create_dock(par, fp);
1310     if(dock!=NULL){
1311         dock_set(dock, tab);
1312         dock_fitrep(dock, NULL, fp);
1313     }
1314
1315     return (WRegion*)dock;
1316 }
1317
1318
1319 /*}}}*/
1320
1321
1322 /*{{{ Client window management setup */
1323
1324
1325 static bool dock_do_attach_final(WDock *dock, WRegion *reg, void *unused)
1326 {
1327     WDockApp *dockapp, *before_dockapp;
1328     WRectangle geom;
1329     WFitParams fp;
1330     bool draw_border=TRUE;
1331     int pos=INT_MAX;
1332      
1333     /* Create and initialise a new WDockApp struct */
1334     dockapp=ALLOC(WDockApp);
1335     
1336     if(dockapp==NULL)
1337         return FALSE;
1338     
1339     if(OBJ_IS(reg, WClientWin)){
1340         ExtlTab proptab=((WClientWin*)reg)->proptab;
1341         extl_table_gets_b(proptab, CLIENTWIN_WINPROP_BORDER, &draw_border);
1342         extl_table_gets_i(proptab, CLIENTWIN_WINPROP_POSITION, &pos);
1343     }
1344
1345     dockapp->reg=reg;
1346     dockapp->draw_border=draw_border;
1347     dockapp->pos=pos;
1348     dockapp->tile=FALSE;
1349
1350     /* Insert the dockapp at the correct relative position */
1351     before_dockapp=dock->dockapps;
1352     for(before_dockapp=dock->dockapps;
1353         before_dockapp!=NULL && dockapp->pos>=before_dockapp->pos;
1354         before_dockapp=before_dockapp->next){
1355     }
1356     
1357     if(before_dockapp!=NULL){
1358         LINK_ITEM_BEFORE(dock->dockapps, before_dockapp, dockapp, next, prev);
1359     }else{
1360         LINK_ITEM(dock->dockapps, dockapp, next, prev);
1361     }
1362
1363     region_set_manager(reg, (WRegion*)dock);
1364     
1365     geom=REGION_GEOM(reg);
1366     dock_managed_rqgeom_(dock, reg, 
1367                          REGION_RQGEOM_WEAK_X|REGION_RQGEOM_WEAK_Y,
1368                          &geom, NULL, FALSE);
1369     
1370     region_map(reg);
1371
1372     return TRUE;
1373 }
1374
1375
1376
1377 static WRegion *dock_do_attach(WDock *dock, WRegionAttachData *data)
1378 {
1379     WFitParams fp;
1380     dock_get_tile_size(dock, &(fp.g));
1381     fp.g.x=0;
1382     fp.g.y=0;
1383     fp.mode=REGION_FIT_WHATEVER|REGION_FIT_BOUNDS;
1384     
1385     return region_attach_helper((WRegion*)dock, (WWindow*)dock, &fp,
1386                                 (WRegionDoAttachFn*)dock_do_attach_final, 
1387                                 NULL, data);
1388 }
1389
1390
1391 /*EXTL_DOC
1392  * Attach \var{reg} to \var{dock}.
1393  */
1394 EXTL_EXPORT_MEMBER
1395 bool dock_attach(WDock *dock, WRegion *reg)
1396 {
1397     WRegionAttachData data;
1398     WFitParams fp;
1399     
1400     data.type=REGION_ATTACH_REPARENT;
1401     data.u.reg=reg;
1402
1403     return (dock_do_attach(dock, &data)!=NULL);
1404 }
1405
1406
1407 static bool dock_handle_drop(WDock *dock, int x, int y,
1408                              WRegion *dropped)
1409 {
1410     return dock_attach(dock, dropped);
1411 }
1412
1413
1414 static WRegion *dock_ph_handler(WDock *dock, int flags, WRegionAttachData *data)
1415 {
1416     return dock_do_attach(dock, data);
1417 }
1418
1419     
1420 static WPHolder *dock_managed_get_pholder(WDock *dock, WRegion *mgd)
1421 {
1422     return (WPHolder*)create_basicpholder((WRegion*)dock,
1423                                           ((WBasicPHolderHandler*)
1424                                            dock_ph_handler));
1425 }
1426
1427
1428 static WPHolder *dock_prepare_manage(WDock *dock, const WClientWin *cwin,
1429                                      const WManageParams *param UNUSED,
1430                                      int priority)
1431 {
1432     if(!MANAGE_PRIORITY_OK(priority, MANAGE_PRIORITY_LOW))
1433         return NULL;
1434
1435     return (WPHolder*)create_basicpholder((WRegion*)dock,
1436                                           ((WBasicPHolderHandler*)
1437                                            dock_ph_handler));
1438 }
1439
1440
1441 static void dock_managed_remove(WDock *dock, WRegion *reg)
1442 {
1443
1444     WDockApp *dockapp=dock_find_dockapp(dock, reg);
1445     
1446     if(dockapp==NULL)
1447         return;
1448     
1449     UNLINK_ITEM(dock->dockapps, dockapp, next, prev);
1450     free(dockapp);
1451
1452     region_unset_manager(reg, (WRegion*)dock);
1453
1454     dock_resize(dock);
1455 }
1456
1457
1458 static bool dock_clientwin_is_dockapp(WClientWin *cwin,
1459                                       const WManageParams *param)
1460 {
1461     bool is_dockapp=FALSE;
1462
1463     /* First, inspect the WManageParams.dockapp parameter */
1464     if(param->dockapp){
1465         is_dockapp=TRUE;
1466     }
1467
1468     /* Second, inspect the _NET_WM_WINDOW_TYPE property */
1469     if(!is_dockapp){
1470         static Atom atom__net_wm_window_type=None;
1471         static Atom atom__net_wm_window_type_dock=None;
1472         Atom actual_type=None;
1473         int actual_format;
1474         unsigned long nitems;
1475         unsigned long bytes_after;
1476         unsigned char *prop;
1477
1478         if(atom__net_wm_window_type==None){
1479             atom__net_wm_window_type=XInternAtom(ioncore_g.dpy,
1480                                                  "_NET_WM_WINDOW_TYPE",
1481                                                  False);
1482         }
1483         if(atom__net_wm_window_type_dock==None){
1484             atom__net_wm_window_type_dock=XInternAtom(ioncore_g.dpy,
1485                                                      "_NET_WM_WINDOW_TYPE_DOCK",
1486                                                       False);
1487         }
1488         if(XGetWindowProperty(ioncore_g.dpy, cwin->win, atom__net_wm_window_type,
1489                               0, sizeof(Atom), False, XA_ATOM, &actual_type,
1490                               &actual_format, &nitems, &bytes_after, &prop)
1491            ==Success){
1492             if(actual_type==XA_ATOM && nitems>=1
1493                && *(Atom*)prop==atom__net_wm_window_type_dock){
1494                 is_dockapp=TRUE;
1495             }
1496             XFree(prop);
1497         }
1498     }
1499
1500     /* Third, inspect the WM_CLASS property */
1501     if(!is_dockapp){
1502         char **p;
1503         int n;
1504
1505         p=xwindow_get_text_property(cwin->win, XA_WM_CLASS, &n);
1506         if(p!=NULL){
1507             if(n>=2 && strcmp(p[1], "DockApp")==0){
1508                 is_dockapp=TRUE;
1509             }
1510             XFreeStringList(p);
1511         }
1512     }
1513
1514     /* Fourth, inspect the _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR property */
1515     if(!is_dockapp){
1516         static Atom atom__kde_net_wm_system_tray_window_for=None;
1517         Atom actual_type=None;
1518         int actual_format;
1519         unsigned long nitems;
1520         unsigned long bytes_after;
1521         unsigned char *prop;
1522
1523         if(atom__kde_net_wm_system_tray_window_for==None){
1524             atom__kde_net_wm_system_tray_window_for=XInternAtom(ioncore_g.dpy,
1525                                                                 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
1526                                                                 False);
1527         }
1528         if(XGetWindowProperty(ioncore_g.dpy, cwin->win,
1529                               atom__kde_net_wm_system_tray_window_for, 0,
1530                               sizeof(Atom), False, AnyPropertyType, 
1531                               &actual_type, &actual_format, &nitems,
1532                               &bytes_after, &prop)==Success){
1533             if(actual_type!=None){
1534                 is_dockapp=TRUE;
1535             }
1536             XFree(prop);
1537         }
1538     }
1539
1540     return is_dockapp;
1541
1542 }
1543
1544
1545 static WDock *dock_find_suitable_dock(WClientWin *cwin,
1546                                       const WManageParams *param)
1547 {
1548     WDock *dock;
1549
1550     for(dock=docks; dock; dock=dock->dock_next){
1551         if(!dock->is_auto)
1552             continue;
1553         if(!region_same_rootwin((WRegion*)dock, (WRegion*)cwin))
1554             continue;
1555         break;
1556     }
1557
1558     return dock;
1559 }
1560
1561
1562 static bool clientwin_do_manage_hook(WClientWin *cwin, const WManageParams *param)
1563 {
1564     WDock *dock;
1565
1566     if(!dock_clientwin_is_dockapp(cwin, param)){
1567         return FALSE;
1568     }
1569
1570     dock=dock_find_suitable_dock(cwin, param);
1571     if(!dock){
1572         return FALSE;
1573     }
1574
1575     return region_manage_clientwin((WRegion*)dock, cwin, param,
1576                                    MANAGE_PRIORITY_NONE);
1577 }
1578
1579
1580 /*}}}*/
1581
1582
1583 /*{{{ Module init/deinit */
1584
1585
1586 bool mod_dock_init()
1587 {
1588
1589     if(XShapeQueryExtension(ioncore_g.dpy, &shape_event_basep,
1590                             &shape_error_basep)){
1591         shape_extension=TRUE;
1592     }else{
1593         XMissingExtension(ioncore_g.dpy, "SHAPE");
1594     }
1595
1596     if(!ioncore_register_regclass(&CLASSDESCR(WDock), 
1597                                   (WRegionLoadCreateFn*)dock_load)){
1598         return FALSE;
1599     }
1600
1601     if(!mod_dock_register_exports()){
1602         ioncore_unregister_regclass(&CLASSDESCR(WDock));
1603         return FALSE;
1604     }
1605
1606     dock_bindmap=ioncore_alloc_bindmap("WDock", NULL);
1607     if(dock_bindmap==NULL){
1608         warn("Unable to allocate dock bindmap.");
1609         mod_dock_unregister_exports();
1610         ioncore_unregister_regclass(&CLASSDESCR(WDock));
1611     }
1612
1613     extl_read_config("cfg_dock", NULL, TRUE);
1614
1615     hook_add(clientwin_do_manage_alt, 
1616              (WHookDummy*)clientwin_do_manage_hook);
1617
1618     return TRUE;
1619
1620 }
1621
1622
1623 void mod_dock_deinit()
1624 {
1625     WDock *dock;
1626
1627     ioncore_unregister_regclass(&CLASSDESCR(WDock));
1628
1629     hook_remove(clientwin_do_manage_alt, 
1630                 (WHookDummy*)clientwin_do_manage_hook);
1631
1632     dock=docks;
1633     while(dock!=NULL){
1634         WDock *next=dock->dock_next;
1635         destroy_obj((Obj*)dock);
1636         dock=next;
1637     }
1638
1639     mod_dock_unregister_exports();
1640     
1641     if(dock_bindmap!=NULL){
1642         ioncore_free_bindmap("WDock", dock_bindmap);
1643         dock_bindmap=NULL;
1644     }
1645 }
1646
1647
1648 /*}}}*/
1649
1650
1651 /*{{{ WDock class description and dynfun list */
1652
1653
1654 static DynFunTab dock_dynfuntab[]={
1655     {window_draw, dock_draw},
1656     {region_updategr, dock_updategr},
1657     {region_managed_rqgeom, dock_managed_rqgeom},
1658     {(DynFun*)region_prepare_manage, (DynFun*)dock_prepare_manage},
1659     {region_managed_remove, dock_managed_remove},
1660     {(DynFun*)region_get_configuration, (DynFun*)dock_get_configuration},
1661     {region_size_hints, dock_size_hints},
1662     {(DynFun*)region_fitrep, (DynFun*)dock_fitrep},
1663     {(DynFun*)region_orientation, (DynFun*)dock_orientation},
1664     {(DynFun*)region_handle_drop, (DynFun*)dock_handle_drop},
1665
1666     {(DynFun*)region_managed_get_pholder,
1667      (DynFun*)dock_managed_get_pholder},
1668     END_DYNFUNTAB
1669 };
1670
1671
1672 EXTL_EXPORT
1673 IMPLCLASS(WDock, WWindow, dock_deinit, dock_dynfuntab);
1674
1675
1676 /*}}}*/
1677