1 /* $XConsortium: occupy.c /main/3 1995/10/27 10:41:19 rswiston $ */
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.
10 /* Demonstrate the DtWsm Workspace API. */
30 static void allWsCB();
31 static void SetupWorkspaceButtons ();
32 static void ShowWorkspaceOccupancy();
33 static void SetWorkspaceOccupancy();
40 static Widget toplevel;
41 static Widget wChildren [20];
43 static Atom *paWsSet = NULL;
44 static Boolean *pbOccupy;
45 static unsigned long numWorkspaces;
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
59 * main - main logic for program
69 XtAppContext app_context;
78 XtSetArg (args[n], XmNallowShellResize, True); n++;
79 toplevel = XtAppInitialize (&app_context, "Dtoccupy", NULL, 0, &argc, argv,
83 XtSetArg (args[n], XmNmarginWidth, 10); n++;
84 XtSetArg (args[n], XmNmarginHeight, 10); n++;
85 frame = XmCreateFrame (toplevel, "frame", args, n);
86 XtManageChild (frame);
92 form = XmCreateForm (frame, "form", args, n);
96 * create compound string for the button text
98 xms = XmStringCreateLocalized("Quit");
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++;
112 top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
119 XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
122 * create compound string for the button text
124 xms = XmStringCreateLocalized("Set Occupancy");
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++;
139 top = SET_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
146 XtAddCallback (SET_BUTTON, XmNactivateCallback, setCB, NULL);
149 * create compound string for the button text
151 xms = XmStringCreateLocalized("Occupy All Workspaces");
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++;
165 top = ALL_WS_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
173 XtAddCallback (ALL_WS_BUTTON, XmNactivateCallback, allWsCB, NULL);
176 * create compound string for the button text
178 xms = XmStringCreateLocalized("Show Occupancy");
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++;
192 top = SHOW_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
199 XtAddCallback (SHOW_BUTTON, XmNactivateCallback, showCB, NULL);
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++;
212 top = SEPARATOR = (Widget)
213 XmCreateSeparatorGadget (form, "separator", args, n);
216 * set up workspace buttons
218 SetupWorkspaceButtons (form, top);
223 XtManageChildren (wChildren, NUM_CHILDREN);
224 XtManageChildren (wWs, numWorkspaces);
229 XtRealizeWidget (toplevel);
232 * set up initial workspace occupancy info
234 pbOccupy = (Boolean *) XtMalloc (numWorkspaces * sizeof (Boolean));
235 for (i=0; i<numWorkspaces; i++)
239 ShowWorkspaceOccupancy ();
244 XtAppMainLoop (app_context);
247 /*-------------------------------------------------------------
248 * Create a set of buttons for the current set of workspaces
250 static void SetupWorkspaceButtons (form, top)
255 Arg args[10]; /* arg list */
256 register int n; /* arg count */
258 DtWsmWorkspaceInfo *pWsInfo;
264 * get workspace information
266 root = XRootWindowOfScreen(XtScreen(form));
267 if (DtWsmGetWorkspaceList (XtDisplay(form), root,
268 &paWs, (int *)&numWorkspaces) == Success)
270 wWs = (Widget *) XtMalloc (numWorkspaces * sizeof (Widget));
272 for (i=0; i<numWorkspaces; i++, pWid++)
274 DtWsmGetWorkspaceInfo (XtDisplay(form), root, paWs[i],
277 pchWs = (char *) XmGetAtomName (XtDisplay(form),
280 * create compound string for the button text
282 xms = XmStringCreateLocalized(pchWs);
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++;
294 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
298 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
299 XtSetArg (args[n], XmNleftWidget, left); n++;
301 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
302 XtSetArg (args[n], XmNtopWidget, top); n++;
306 *pWid = XtCreateWidget ("button", xmToggleButtonWidgetClass,
313 XtAddCallback (*pWid, XmNvalueChangedCallback, wsCB,
320 fprintf(stderr, "Failed to get workspace info -- dtwm not running!\n");
325 /*-------------------------------------------------------------
326 * Update the workspace buttons with where we are now.
328 static void ShowWorkspaceOccupancy ()
330 Arg args[10]; /* arg list */
331 register int n; /* arg count */
334 unsigned long numWsIn;
337 rval = DtWsmGetWorkspacesOccupied(XtDisplay(toplevel),
338 XtWindow(toplevel), &paWsIn, &numWsIn );
343 for (i = 0; i<numWorkspaces; i++)
346 for (j=0; j<numWsIn; j++)
348 if (paWs[i] == paWsIn[j])
356 for (i = 0; i<numWorkspaces; i++)
361 XtSetArg (args[n], XmNset, True); n++;
365 XtSetArg (args[n], XmNset, False); n++;
367 XtSetValues (wWs[i], args, n);
370 if (paWsIn) XFree ((char *) paWsIn);
374 /*-------------------------------------------------------------
375 * quitCB - callback for button
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 */
386 /*-------------------------------------------------------------
387 * showCB - callback for show occupancy button
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 */
395 ShowWorkspaceOccupancy();
399 /*-------------------------------------------------------------
400 * setCB - callback for set occupancy button
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 */
412 paWsSet = (Atom *) XtMalloc (numWorkspaces * sizeof(Atom));
415 for (i=0, numSet=0; i<numWorkspaces; i++)
419 paWsSet[numSet++] = paWs[i];
423 DtWsmSetWorkspacesOccupied (XtDisplay(toplevel),
424 XtWindow(toplevel), paWsSet, numSet);
428 /*-------------------------------------------------------------
429 * wsCB - callback for set occupancy button
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 */
436 int i = (Atom) client_data; /* ws array index */
437 XmToggleButtonCallbackStruct *ptb =
438 (XmToggleButtonCallbackStruct *) call_data;
440 if (ptb->reason == XmCR_VALUE_CHANGED)
453 /*-------------------------------------------------------------
454 * allWsCB - callback for occupy all button
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 */
461 DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));