]> git.decadent.org.uk Git - ion3.git/blob - mod_sp/main.c
[svn-upgrade] Integrating new upstream version, ion3 (20070506)
[ion3.git] / mod_sp / main.c
1 /*
2  * ion/mod_sp/main.c
3  *
4  * Copyright (c) Tuomo Valkonen 2004-2007. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <string.h>
10
11 #include <libtu/map.h>
12 #include <libtu/minmax.h>
13 #include <libextl/readconfig.h>
14 #include <libmainloop/hooks.h>
15
16 #include <ioncore/saveload.h>
17 #include <ioncore/screen.h>
18 #include <ioncore/mplex.h>
19 #include <ioncore/stacking.h>
20 #include <ioncore/ioncore.h>
21 #include <ioncore/global.h>
22 #include <ioncore/framep.h>
23 #include <ioncore/frame.h>
24 #include <ioncore/names.h>
25 #include <ioncore/group.h>
26 #include <ioncore/group-ws.h>
27
28 #include "main.h"
29 #include "exports.h"
30
31
32 /*{{{ Module information */
33
34 #include "../version.h"
35
36 char mod_sp_ion_api_version[]=ION_API_VERSION;
37
38
39 /*}}}*/
40
41
42 /*{{{ Bindmaps, config, etc. */
43
44
45 #define SP_NAME  "*scratchpad*"
46 #define SPWS_NAME  "*scratchws*"
47
48
49 /*}}}*/
50
51
52 /*{{{ Exports */
53
54
55 static WRegion *create_frame_scratchpad(WWindow *parent, const WFitParams *fp,
56                                         void *unused)
57 {
58     return (WRegion*)create_frame(parent, fp, FRAME_MODE_UNKNOWN);
59 }
60
61
62 static WRegion *create_scratchws(WWindow *parent, const WFitParams *fp, 
63                                  void *unused)
64 {
65     WRegion *reg;
66     WRegionAttachData data;
67     WGroupAttachParams par;
68     WGroupWS *ws;
69     
70     ws=create_groupws(parent, fp);
71     
72     if(ws==NULL)
73         return NULL;
74         
75     region_set_name((WRegion*)ws, SPWS_NAME);
76     
77     data.type=REGION_ATTACH_NEW;
78     data.u.n.fn=create_frame_scratchpad;
79     data.u.n.param=NULL;
80     
81     par.szplcy_set=TRUE;
82     par.szplcy=SIZEPOLICY_FREE_GLUE;
83     
84     par.geom_set=TRUE;
85     par.geom.w=minof(fp->g.w, CF_SCRATCHPAD_DEFAULT_W);
86     par.geom.h=minof(fp->g.h, CF_SCRATCHPAD_DEFAULT_H);
87     par.geom.x=(fp->g.w-par.geom.w)/2;
88     par.geom.y=(fp->g.h-par.geom.h)/2;
89     
90     par.level_set=TRUE;
91     par.level=STACKING_LEVEL_MODAL1;
92     
93     par.bottom=TRUE;
94     
95     reg=group_do_attach(&ws->grp, &par, &data);
96
97     if(reg==NULL){
98         destroy_obj((Obj*)ws);
99         return NULL;
100     }
101      
102     region_set_name((WRegion*)reg, SP_NAME);
103     
104     return (WRegion*)ws;
105 }
106
107
108 static WRegion *create(WMPlex *mplex, int flags)
109 {
110     WRegion *sp;
111     WMPlexAttachParams par;
112
113     par.flags=(flags
114                |MPLEX_ATTACH_UNNUMBERED
115                |MPLEX_ATTACH_SIZEPOLICY
116                |MPLEX_ATTACH_PSEUDOMODAL);
117     par.szplcy=SIZEPOLICY_FULL_EXACT;
118
119     sp=mplex_do_attach_new((WMPlex*)mplex, &par,
120                            create_scratchws,
121                            NULL);
122     
123     if(sp==NULL)
124         warn(TR("Unable to create scratchpad."));
125     
126     return sp;
127 }
128
129
130 static bool is_scratchpad(WRegion *reg)
131 {
132     char *nm=reg->ni.name;
133     int inst_off=reg->ni.inst_off;
134     
135     if(nm==NULL)
136         return FALSE;
137     
138     return (inst_off<0
139             ? (strcmp(nm, SP_NAME)==0 || 
140                strcmp(nm, SPWS_NAME)==0)
141             : (strncmp(nm, SP_NAME, inst_off)==0 ||
142                strncmp(nm, SPWS_NAME, inst_off)==0));
143 }
144
145
146 /*EXTL_DOC
147  * Change displayed status of some scratchpad on \var{mplex} if one is 
148  * found. The parameter \var{how} is one of 
149  * \codestr{set}, \codestr{unset}, or \codestr{toggle}.
150  * The resulting status is returned.
151  */
152 EXTL_EXPORT
153 bool mod_sp_set_shown_on(WMPlex *mplex, const char *how)
154 {
155     int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
156     WMPlexIterTmp tmp;
157     WRegion *reg;
158     bool found=FALSE;
159     
160     FOR_ALL_MANAGED_BY_MPLEX(mplex, reg, tmp){
161         if(is_scratchpad(reg)){
162             mplex_set_hidden(mplex, reg, setpar);
163             found=TRUE;
164         }
165     }
166
167     if(!found){
168         int sp=libtu_string_to_setparam(how);
169         if(sp==SETPARAM_SET || sp==SETPARAM_TOGGLE)
170             found=(create(mplex, 0)!=NULL);
171     }
172     
173     return found;
174 }
175
176
177 /*EXTL_DOC
178  * Toggle displayed status of \var{sp}.
179  * The parameter \var{how} is one of 
180  * \codestr{set}, \codestr{unset}, or \codestr{toggle}.
181  * The resulting status is returned.
182  */
183 EXTL_EXPORT
184 bool mod_sp_set_shown(WFrame *sp, const char *how)
185 {
186     if(sp!=NULL){
187         int setpar=libtu_setparam_invert(libtu_string_to_setparam(how));
188         WMPlex *mplex=OBJ_CAST(REGION_MANAGER(sp), WMPlex);
189         if(mplex!=NULL)
190             return mplex_set_hidden(mplex, (WRegion*)sp, setpar);
191     }
192     
193     return FALSE;
194 }
195
196
197 /*}}}*/
198
199
200 /*{{{ Init & deinit */
201
202
203 void mod_sp_deinit()
204 {
205     mod_sp_unregister_exports();
206 }
207
208
209 static void check_and_create()
210 {
211     WMPlexIterTmp tmp;
212     WScreen *scr;
213     WRegion *reg;
214     
215     /* No longer needed, free the memory the list uses. */
216     hook_remove(ioncore_post_layout_setup_hook, check_and_create);
217     
218     FOR_ALL_SCREENS(scr){
219         FOR_ALL_MANAGED_BY_MPLEX((WMPlex*)scr, reg, tmp){
220             if(is_scratchpad(reg))
221                 return;
222         }
223         
224         create(&scr->mplex, MPLEX_ATTACH_HIDDEN);
225     }
226 }
227     
228
229 bool mod_sp_init()
230 {
231     if(!mod_sp_register_exports())
232         return FALSE;
233
234     extl_read_config("cfg_sp", NULL, FALSE);
235     
236     if(ioncore_g.opmode==IONCORE_OPMODE_INIT){
237         hook_add(ioncore_post_layout_setup_hook, check_and_create);
238     }else{
239         check_and_create();
240     }
241     
242     return TRUE;
243 }
244
245
246 /*}}}*/
247