Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / examples / dtwsm / occupy.c
1 /* $XConsortium: occupy.c /main/3 1995/10/27 10:41:19 rswiston $ */
2 /*
3  *  (c) Copyright 1993, 1994 Hewlett-Packard Company    
4  *  (c) Copyright 1993, 1994 International Business Machines Corp.
5  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
6  *  (c) Copyright 1993, 1994 Novell, Inc.
7  */
8
9
10 /* Demonstrate the DtWsm Workspace API. */
11
12 /*  
13  * include files  
14  */
15
16 #include <stdio.h>
17 #include <Xm/XmAll.h>
18 #include <Dt/Wsm.h>
19
20
21 /*  
22  * functions
23  */
24
25 void main();
26 static void quitCB(); 
27 static void setCB(); 
28 static void showCB(); 
29 static void wsCB(); 
30 static void allWsCB(); 
31 static void SetupWorkspaceButtons ();
32 static void ShowWorkspaceOccupancy();
33 static void SetWorkspaceOccupancy();
34
35 /*  
36  * global variables  
37  */
38
39 static XmString xms;
40 static Widget toplevel;
41 static Widget wChildren [20];
42 static Widget *wWs;
43 static Atom *paWsSet = NULL;
44 static Boolean *pbOccupy;
45 static unsigned long numWorkspaces;
46 static Atom *paWs;
47
48 /* 
49  * button assignments 
50  */
51 #define QUIT_BUTTON     (wChildren[0])
52 #define SEPARATOR       (wChildren[1])
53 #define SET_BUTTON      (wChildren[2])
54 #define SHOW_BUTTON     (wChildren[3])
55 #define ALL_WS_BUTTON   (wChildren[4])
56 #define NUM_CHILDREN    5
57
58 /*
59  * main - main logic for program
60  */
61
62 void main (argc,argv)
63     int argc;
64     char **argv;
65 {
66     Widget frame;
67     Widget form;
68     Widget  top;
69     XtAppContext app_context;
70     Arg args[10];
71     int n;
72     int i;
73
74     /*
75      * initialize toolkit
76      */
77     n = 0;
78     XtSetArg (args[n], XmNallowShellResize, True);  n++;
79     toplevel = XtAppInitialize (&app_context, "Dtoccupy", NULL, 0, &argc, argv,
80                                                                  NULL, args, n);
81
82     n = 0;
83     XtSetArg (args[n], XmNmarginWidth, 10);  n++;
84     XtSetArg (args[n], XmNmarginHeight, 10);  n++;
85     frame = XmCreateFrame (toplevel, "frame", args, n);
86     XtManageChild (frame);
87
88     /*
89      *  manager widget
90      */
91     n = 0;
92     form = XmCreateForm (frame, "form", args, n);
93     XtManageChild (form);
94
95     /*
96      * create compound string for the button text
97      */
98     xms = XmStringCreateLocalized("Quit");
99
100     /*
101      * set up arglist
102      */
103     n = 0;
104     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
105     XtSetArg (args[n], XmNlabelString, xms);  n++;
106     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
107     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
108
109     /*
110      * create button
111      */
112     top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
113                                                                 form, args, n);
114
115     XmStringFree(xms);
116     /*
117      * add callback
118      */
119     XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
120
121     /*  
122      * create compound string for the button text  
123      */
124     xms = XmStringCreateLocalized("Set Occupancy");
125
126     /*  
127      * set up arglist  
128      */
129     n = 0;
130     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
131     XtSetArg (args[n], XmNlabelString, xms);  n++;
132     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
133     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
134     XtSetArg (args[n], XmNtopWidget, top); n++;
135
136     /*  
137      * create button  
138      */
139     top = SET_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
140           form, args, n);
141
142     XmStringFree(xms);
143     /*  
144      * add callback  
145      */
146     XtAddCallback (SET_BUTTON, XmNactivateCallback, setCB, NULL);
147
148     /*  
149      * create compound string for the button text  
150      */
151     xms = XmStringCreateLocalized("Occupy All Workspaces");
152
153     /*  
154      * set up arglist  
155      */
156     n = 0;
157     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
158     XtSetArg (args[n], XmNlabelString, xms);  n++;
159     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
160     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
161     XtSetArg (args[n], XmNtopWidget, top); n++;
162     /*  
163      * create button  
164      */
165     top = ALL_WS_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
166           form, args, n);
167
168     XmStringFree(xms);
169
170     /*  
171      * add callback  
172      */
173     XtAddCallback (ALL_WS_BUTTON, XmNactivateCallback, allWsCB, NULL);
174
175     /*  
176      * create compound string for the button text  
177      */
178     xms = XmStringCreateLocalized("Show Occupancy");
179
180     /*  
181      * set up arglist  
182      */
183     n = 0;
184     XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
185     XtSetArg (args[n], XmNlabelString, xms);  n++;
186     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
187     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
188     XtSetArg (args[n], XmNtopWidget, top); n++;
189     /*   
190      * create button  
191      */
192     top = SHOW_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
193       form, args, n);
194
195     XmStringFree(xms);
196     /*  
197      * add callback  
198      */
199     XtAddCallback (SHOW_BUTTON, XmNactivateCallback, showCB, NULL);
200
201     /*  
202      * set up arglist  
203      */
204     n = 0;
205     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
206     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
207     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
208     XtSetArg (args[n], XmNtopWidget, top); n++;
209     /*  
210      * create separator  
211      */
212     top = SEPARATOR = (Widget) 
213             XmCreateSeparatorGadget (form, "separator", args, n);
214
215     /* 
216      * set up workspace buttons 
217      */
218     SetupWorkspaceButtons (form, top);
219
220     /*  
221      * manage children
222      */
223     XtManageChildren (wChildren, NUM_CHILDREN);
224     XtManageChildren (wWs, numWorkspaces);
225
226     /*  
227      * realize widgets  
228      */
229     XtRealizeWidget (toplevel);
230
231     /* 
232      * set up initial workspace occupancy info 
233      */
234     pbOccupy = (Boolean *) XtMalloc (numWorkspaces * sizeof (Boolean));
235     for (i=0; i<numWorkspaces; i++)
236     {
237         pbOccupy[i] = False;
238     }
239     ShowWorkspaceOccupancy ();
240
241     /*  
242      * process events  
243      */
244     XtAppMainLoop (app_context);
245 }
246
247 /*-------------------------------------------------------------
248  *      Create a set of buttons for the current set of workspaces
249  */
250 static void SetupWorkspaceButtons (form, top)
251 Widget form;
252 Widget top;
253 {
254     Window root;
255     Arg args[10]; /*  arg list          */
256     register int n;     /*  arg count           */
257     register int i;
258     DtWsmWorkspaceInfo *pWsInfo;
259     char *pchWs;
260     Widget left = None;
261     Widget *pWid;
262
263     /*  
264      * get workspace information 
265      */
266     root = XRootWindowOfScreen(XtScreen(form));
267     if (DtWsmGetWorkspaceList (XtDisplay(form), root, 
268                     &paWs, (int *)&numWorkspaces) == Success)
269     {
270         wWs = (Widget *) XtMalloc (numWorkspaces * sizeof (Widget));
271         pWid = wWs;
272         for (i=0; i<numWorkspaces; i++, pWid++)
273         {
274             DtWsmGetWorkspaceInfo (XtDisplay(form), root, paWs[i],
275                 &pWsInfo);
276
277             pchWs = (char *) XmGetAtomName (XtDisplay(form), 
278                                             pWsInfo->workspace);
279             /*  
280              * create compound string for the button text  
281              */
282             xms = XmStringCreateLocalized(pchWs);
283
284             /*  
285              * set up arglist  
286              */
287             n = 0;
288             XtSetArg (args[n], XmNlabelType, XmSTRING);  n++;
289             XtSetArg (args[n], XmNlabelString, xms);  n++;
290             XtSetArg (args[n], XmNindicatorOn, True);  n++;
291             XtSetArg (args[n], XmNindicatorType, XmN_OF_MANY);  n++;
292             if (left == None)
293             {
294                 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
295             }
296             else
297             {
298                 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
299                 XtSetArg (args[n], XmNleftWidget, left); n++;
300             }
301             XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
302             XtSetArg (args[n], XmNtopWidget, top); n++;
303             /*  
304              * create button  
305              */
306             *pWid = XtCreateWidget ("button", xmToggleButtonWidgetClass,
307               form, args, n);
308
309             XmStringFree(xms);
310             /*  
311              * add callback  
312              */
313             XtAddCallback (*pWid, XmNvalueChangedCallback, wsCB, 
314                                     (XtPointer) i);
315             left = *pWid;
316         }
317     }
318     else
319     {
320         fprintf(stderr, "Failed to get workspace info -- dtwm not running!\n");
321         exit (1);
322     }
323 }
324
325 /*-------------------------------------------------------------
326  *      Update the workspace buttons with where we are now.
327  */
328 static void ShowWorkspaceOccupancy ()
329 {
330     Arg args[10]; /*  arg list          */
331     register int n;     /*  arg count           */
332     int i,j;
333     Atom *paWsIn = NULL;
334     unsigned long numWsIn;
335     int rval;
336
337     rval = DtWsmGetWorkspacesOccupied(XtDisplay(toplevel), 
338                 XtWindow(toplevel), &paWsIn, &numWsIn );
339
340     if (rval == Success)
341     {
342
343         for (i = 0; i<numWorkspaces; i++)
344         {
345             pbOccupy[i] = False;
346             for (j=0; j<numWsIn; j++)
347             {
348                 if (paWs[i] == paWsIn[j])
349                 {
350                     pbOccupy[i] = True;
351                     break;
352                 }
353             }
354         }
355
356         for (i = 0; i<numWorkspaces; i++)
357         {
358             n = 0;
359             if (pbOccupy[i])
360             {
361                 XtSetArg (args[n], XmNset, True);  n++;
362             }
363             else
364             {
365                 XtSetArg (args[n], XmNset, False);  n++;
366             }
367             XtSetValues (wWs[i], args, n);
368         }
369
370         if (paWsIn) XFree ((char *) paWsIn);
371     }
372 }
373
374 /*-------------------------------------------------------------
375  *      quitCB - callback for button
376  */
377 static void quitCB (w, client_data, call_data) 
378 Widget  w;              /*  widget id           */
379 XtPointer       client_data;    /*  data from application   */
380 XtPointer       call_data;      /*  data from widget class  */
381 {
382     exit (0);
383 }
384
385
386 /*-------------------------------------------------------------
387  *      showCB - callback for show occupancy button
388  */
389 static void showCB (w, client_data, call_data) 
390 Widget  w;              /*  widget id           */
391 XtPointer       client_data;    /*  data from application   */
392 XtPointer       call_data;      /*  data from widget class  */
393 {
394
395     ShowWorkspaceOccupancy();
396
397 }
398
399 /*-------------------------------------------------------------
400  *      setCB - callback for set occupancy button
401  */
402 static void setCB (w, client_data, call_data) 
403 Widget  w;              /*  widget id           */
404 XtPointer       client_data;    /*  data from application   */
405 XtPointer       call_data;      /*  data from widget class  */
406 {
407     int numSet;
408     int i;
409
410     if (!paWsSet)
411     {
412         paWsSet = (Atom *) XtMalloc (numWorkspaces * sizeof(Atom));
413     }
414
415     for (i=0, numSet=0; i<numWorkspaces; i++)
416     {
417         if (pbOccupy[i])
418         {
419             paWsSet[numSet++] = paWs[i];
420         }
421     }
422
423     DtWsmSetWorkspacesOccupied (XtDisplay(toplevel),
424                             XtWindow(toplevel), paWsSet, numSet);
425
426 }
427
428 /*-------------------------------------------------------------
429  *      wsCB - callback for set occupancy button
430  */
431 static void wsCB (w, client_data, call_data) 
432 Widget  w;              /*  widget id           */
433 XtPointer       client_data;    /*  data from application   */
434 XtPointer       call_data;      /*  data from widget class  */
435 {
436     int i = (Atom) client_data; /* ws array index */
437     XmToggleButtonCallbackStruct *ptb = 
438                 (XmToggleButtonCallbackStruct *) call_data;
439
440     if (ptb->reason == XmCR_VALUE_CHANGED)
441     {
442         if (ptb->set)
443         {
444             pbOccupy[i] = True;
445         }
446         else
447         {
448             pbOccupy[i] = False;
449         }
450     }
451 }
452
453 /*-------------------------------------------------------------
454  *      allWsCB - callback for occupy all button
455  */
456 static void allWsCB (w, client_data, call_data) 
457 Widget  w;              /*  widget id           */
458 XtPointer       client_data;    /*  data from application   */
459 XtPointer       call_data;      /*  data from widget class  */
460 {
461     DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
462 }
463