2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: wsinfo.c /main/3 1996/11/05 12:17:59 mustafa $ */
24 /***********************************************************************
25 * CDE Developer's Conference Sample Program
30 * Demonstrate the CDE Workspace API. This file uses:
32 * DtWsmGetWorkspaceInfo
33 * DtWsmFreeWorkspaceInfo
34 * DtWsmGetCurrentWorkspace
35 * DtWsmAddCurrentWorkspaceCallback
36 * DtWsmOccupyAllWorkspaces
38 * (c) Copyright 1993, 1994 Hewlett-Packard Company
39 * (c) Copyright 1993, 1994 International Business Machines Corp.
40 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
41 * (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
43 ***********************************************************************/
50 #include <X11/Intrinsic.h>
52 #include <Xm/AtomMgr.h>
61 * functions defined in this program
66 void ShowCurrentWorkspaceInfo ();
67 DtWsmWsChangeProc wschangecb();
68 void InstallXErrorHandler();
73 Widget toplevel; /* Shell widget */
74 Widget wChildren [20];
79 #define QUIT_BUTTON (wChildren[0])
80 #define UPDATE_BUTTON (wChildren[1])
81 #define SEPARATOR (wChildren[2])
82 #define WS_LABEL (wChildren[3])
83 #define WS_VALUE (wChildren[4])
84 #define TITLE_LABEL (wChildren[5])
85 #define TITLE_VALUE (wChildren[6])
86 #define BACKDROP_LABEL (wChildren[7])
87 #define BACKDROP_VALUE (wChildren[8])
88 #define COLORSET_LABEL (wChildren[9])
89 #define COLORSET_VALUE (wChildren[10])
90 #define SEPARATOR2 (wChildren[11])
91 #define NUM_CHILDREN 12
93 /*-------------------------------------------------------------
94 * main - main logic for program
102 XtAppContext app_context;
106 XmString xms, xmsBlank;
112 XtSetArg (args[n], XmNallowShellResize, True); n++;
113 toplevel = XtAppInitialize (&app_context, "XMDemos", NULL, 0,
114 &argc, argv, NULL, args, n);
117 * Set up X error handling
119 InstallXErrorHandler ();
125 form = XmCreateForm (toplevel, "form", args, n);
126 XtManageChild (form);
129 * create compound string for blank labels
131 xmsBlank = XmStringCreateLocalized(" ");
133 /*******************************************
135 ******************************************/
138 * create compound string for the button text
140 xms = XmStringCreateLocalized("Quit");
146 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
147 XtSetArg (args[n], XmNlabelString, xms); n++;
148 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
149 XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
153 top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
160 XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
162 /*******************************************
163 * UPDATE WORKSPACE INFO BUTTON
164 ******************************************/
167 * create compound string for the button text
169 xms = XmStringCreateLocalized("Get workspace info");
175 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
176 XtSetArg (args[n], XmNlabelString, xms); n++;
177 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
178 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
179 XtSetArg (args[n], XmNtopWidget, top); n++;
183 top = UPDATE_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
190 XtAddCallback (UPDATE_BUTTON, XmNactivateCallback, updateCB, NULL);
192 /*******************************************
194 ******************************************/
200 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
201 XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
202 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
203 XtSetArg (args[n], XmNtopWidget, top); n++;
207 top = SEPARATOR = (Widget)
208 XmCreateSeparatorGadget (form, "separator", args, n);
210 /*******************************************
211 * LABEL FOR CURRENT WORKSPACE
212 ******************************************/
215 * NOTE: We assume this is the longest label and lay out the
216 * other fields based on this assumption.
220 * create compound string for the label text
222 xms = XmStringCreateLocalized("Current workspace: ");
228 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
229 XtSetArg (args[n], XmNlabelString, xms); n++;
230 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
231 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
232 XtSetArg (args[n], XmNtopWidget, top); n++;
236 WS_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
240 /*******************************************
241 * VALUE FOR CURRENT WORKSPACE
242 ******************************************/
248 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
249 XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
250 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
251 XtSetArg (args[n], XmNleftWidget, WS_LABEL); n++;
252 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
253 XtSetArg (args[n], XmNtopWidget, top); n++;
257 top = WS_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
260 /*******************************************
261 * LABEL FOR WORKSPACE TITLE
262 ******************************************/
265 * create compound string for the label text
267 xms = XmStringCreateLocalized(" title: ");
273 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
274 XtSetArg (args[n], XmNlabelString, xms); n++;
275 XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
276 XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
277 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
278 XtSetArg (args[n], XmNtopWidget, top); n++;
282 TITLE_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
286 /*******************************************
287 * VALUE FOR WORKSPACE TITLE
288 ******************************************/
294 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
295 XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
296 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
297 XtSetArg (args[n], XmNleftWidget, TITLE_LABEL); n++;
298 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
299 XtSetArg (args[n], XmNtopWidget, top); n++;
303 top = TITLE_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
306 /*******************************************
307 * LABEL FOR WORKSPACE BACKDROP
308 ******************************************/
311 * create compound string for the label text
313 xms = XmStringCreateLocalized(" backdrop: ");
319 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
320 XtSetArg (args[n], XmNlabelString, xms); n++;
321 XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
322 XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
323 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
324 XtSetArg (args[n], XmNtopWidget, top); n++;
328 BACKDROP_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
332 /*******************************************
333 * VALUE FOR WORKSPACE BACKDROP
334 ******************************************/
340 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
341 XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
342 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
343 XtSetArg (args[n], XmNleftWidget, BACKDROP_LABEL); n++;
344 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
345 XtSetArg (args[n], XmNtopWidget, top); n++;
349 top = BACKDROP_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
352 /*******************************************
353 * LABEL FOR WORKSPACE COLORSET ID
354 ******************************************/
357 * create compound string for the label text
359 xms = XmStringCreateLocalized(" color set id: ");
365 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
366 XtSetArg (args[n], XmNlabelString, xms); n++;
367 XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
368 XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
369 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
370 XtSetArg (args[n], XmNtopWidget, top); n++;
374 COLORSET_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
378 /*******************************************
379 * VALUE FOR WORKSPACE COLORSET ID
380 ******************************************/
386 XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
387 XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
388 XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
389 XtSetArg (args[n], XmNleftWidget, COLORSET_LABEL); n++;
390 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
391 XtSetArg (args[n], XmNtopWidget, top); n++;
395 top = COLORSET_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
398 /*******************************************
400 ******************************************/
406 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
407 XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
408 XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
409 XtSetArg (args[n], XmNtopWidget, top); n++;
413 top = SEPARATOR2 = (Widget)
414 XmCreateSeparatorGadget (form, "separator", args, n);
419 XtManageChildren (wChildren, NUM_CHILDREN);
422 * Show info on current workspace
424 ShowCurrentWorkspaceInfo ();
429 XtRealizeWidget (toplevel);
432 * Do these calls after the shell is realized.
433 * They need a window created for the top level shell.
435 * HINT: See XtSetMappedWhenManaged(1) if you want to
436 * realize a widget to get a window, but need to
437 * control when the map of the window occurs.
439 (void) DtWsmAddCurrentWorkspaceCallback (toplevel,
440 (DtWsmWsChangeProc) wschangecb, NULL);
442 DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
447 XtAppMainLoop (app_context);
451 /*-------------------------------------------------------------
452 * Update the information for the current workspace
454 void ShowCurrentWorkspaceInfo ()
456 Arg args[10]; /* arg list */
457 register int n; /* arg count */
462 DtWsmWorkspaceInfo *pWsInfo;
467 dpy = XtDisplay(toplevel);
468 root = XDefaultRootWindow(dpy);
470 rval = DtWsmGetCurrentWorkspace(dpy, root, &aWs);
474 rval = DtWsmGetWorkspaceInfo (dpy, root, aWs, &pWsInfo);
479 pchName = XmGetAtomName (dpy, pWsInfo->workspace);
480 xms = XmStringCreateLocalized(pchName);
483 XtSetArg (args[n], XmNlabelString, xms); n++;
484 XtSetValues (WS_VALUE, args, n);
489 /* workspace title */
490 xms = XmStringCreateLocalized(pWsInfo->pchTitle);
493 XtSetArg (args[n], XmNlabelString, xms); n++;
494 XtSetValues (TITLE_VALUE, args, n);
498 /* workspace backdrop name */
499 if (pWsInfo->backdropName != None)
500 pchName = XmGetAtomName (dpy, pWsInfo->backdropName);
502 pchName = "<failed to load!>";
503 xms = XmStringCreateLocalized(pchName);
506 XtSetArg (args[n], XmNlabelString, xms); n++;
507 XtSetValues (BACKDROP_VALUE, args, n);
510 if (pWsInfo->backdropName != None)
513 /* workspace colorset id */
514 sprintf (pch, "%d", pWsInfo->colorSetId);
515 xms = XmStringCreateLocalized(pch);
518 XtSetArg (args[n], XmNlabelString, xms); n++;
519 XtSetValues (COLORSET_VALUE, args, n);
523 DtWsmFreeWorkspaceInfo (pWsInfo);
529 fprintf (stderr, "Failed to get workspace information.\n");
530 fprintf (stderr, "Is dtwm running?\n");
535 /*-------------------------------------------------------------
536 * quitCB - callback for button
538 void quitCB (w, client_data, call_data)
539 Widget w; /* widget id */
540 caddr_t client_data; /* data from application */
541 caddr_t call_data; /* data from widget class */
547 /*-------------------------------------------------------------
548 * updateCB - callback for show current workspace info
550 void updateCB (w, client_data, call_data)
551 Widget w; /* widget id */
552 caddr_t client_data; /* data from application */
553 caddr_t call_data; /* data from widget class */
556 ShowCurrentWorkspaceInfo ();
560 /*-------------------------------------------------------------
561 ** wschangecb - callback for workspace switch
564 wschangecb (w, atom, client_data)
567 XtPointer client_data;
569 ShowCurrentWorkspaceInfo ();
574 /*-------------------------------------------------------------
575 ** data for X error handler
577 #define E_MAJOR_CODE 0
578 #define E_MINOR_CODE 1
579 #define E_RESOURCE_ID 2
580 #define E_ERROR_SERIAL 3
581 #define E_CURRENT_SERIAL 4
583 #define NUM_E_STRINGS 5
585 static char *pchErrorFormatNames [NUM_E_STRINGS] = {
593 static char *pchErrorFormat [NUM_E_STRINGS];
596 /*-------------------------------------------------------------
597 ** HandleXErrors - catch X protocol errors
600 _HandleXErrors (display, errorEvent)
602 XErrorEvent *errorEvent;
605 char message[BUFSIZ];
607 XGetErrorText (display, errorEvent->error_code, buffer, BUFSIZ);
608 fprintf (stderr, "\n*** X error occurred ***\n");
609 fprintf (stderr, "Description = '%s'\n ", buffer);
611 fprintf (stderr, pchErrorFormat[E_MAJOR_CODE], errorEvent->request_code);
612 sprintf(message, "%d", errorEvent->request_code);
613 XGetErrorDatabaseText (display, "XRequest", message, " ", buffer, BUFSIZ);
614 fprintf (stderr, " (%s)\n ", buffer);
615 fprintf (stderr, pchErrorFormat[E_MINOR_CODE], errorEvent->minor_code);
616 fprintf (stderr, "\n ");
617 fprintf (stderr, pchErrorFormat[E_RESOURCE_ID], errorEvent->resourceid);
618 fprintf (stderr, "\n ");
619 fprintf (stderr, pchErrorFormat[E_ERROR_SERIAL], errorEvent->serial);
620 fprintf (stderr, "\n ");
621 fprintf (stderr, pchErrorFormat[E_CURRENT_SERIAL],
622 LastKnownRequestProcessed(display));
623 fprintf (stderr, "\n");
628 /*-------------------------------------------------------------
629 ** Setup the X error handler
632 InstallXErrorHandler ( display )
639 * Fetch the X error format strings from XErrorDB
641 for (i = 0; i< NUM_E_STRINGS; i++)
643 XGetErrorDatabaseText (display, "XlibMessage",
644 pchErrorFormatNames[i], " %d ", buffer, BUFSIZ);
646 pchErrorFormat[i] = (char *) XtMalloc (1+strlen(buffer));
648 strcpy(pchErrorFormat[i], buffer);
651 XSetErrorHandler (_HandleXErrors);