File: ShellUtil.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (316 lines) | stat: -rw-r--r-- 7,480 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.1.1.1  2001/07/18 11:06:03  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <Xmt/Xmt.h>
#include <Xmt/ConvertersP.h>
#include <X11/IntrinsicP.h>  /* for faster subclass checking */
#include <Xm/AtomMgr.h>
#include <Xm/Protocols.h>

#if NeedFunctionPrototypes
Widget XmtGetApplicationShell(Widget w)
#else
Widget XmtGetApplicationShell(w)
Widget w;
#endif
{
    if (w == NULL) return NULL;
    while(XtParent(w)) w = XtParent(w);
    return w;
}

#if NeedFunctionPrototypes
Widget XmtGetTopLevelShell(Widget w)
#else
Widget XmtGetTopLevelShell(w)
Widget w;
#endif
{
    if (w == NULL) return NULL;
    while(!XtIsTopLevelShell(w)) w = XtParent(w);
    return w;
}

#if NeedFunctionPrototypes
Widget XmtGetShell(Widget w)
#else
Widget XmtGetShell(w)
Widget w;
#endif
{
    /*
     * XXX  what happens when we go up from a menu button?
     * do we find the menu shell or the main shell?  This is
     * important for callback functions from menu buttons
     */
    if (w == NULL) return NULL;
    while (!XtIsShell(w)) w = XtParent(w);
    return w;
}

#if NeedFunctionPrototypes
void XmtAddDeleteCallback(Widget shell, int response,
			  XtCallbackProc proc, XtPointer data)
#else
void XmtAddDeleteCallback(shell, response, proc, data)
Widget shell;
int response;
XtCallbackProc proc;
XtPointer data;
#endif
{
    static Atom wm_delete_window = 0;

    if (!wm_delete_window) {
	wm_delete_window = XmInternAtom(XtDisplay(shell),
					"WM_DELETE_WINDOW", False);
    }

    XtVaSetValues(shell, XmNdeleteResponse, response, NULL);
    XmAddWMProtocolCallback(shell, wm_delete_window, proc, data);
}

#if NeedFunctionPrototypes
void XmtAddSaveYourselfCallback(Widget shell,
				XtCallbackProc proc, XtPointer data)
#else
void XmtAddSaveYourselfCallback(shell, proc, data)
Widget shell;
XtCallbackProc proc;
XtPointer data;
#endif
{
    static Atom wm_save_yourself;

    if (!XtIsApplicationShell(shell)) {
	XmtWarningMsg("XmtAddSaveYourselfCallback", "badShell",
		      "specified shell '%s' is not an ApplicationShell.",
		      XtName(shell));
	return;
    }

    if (!wm_save_yourself) {
	wm_save_yourself = XmInternAtom(XtDisplay(shell),
					"WM_SAVE_YOURSELF", False);
    }
    else {
	XmtWarningMsg("XmtAddSaveYourselfCallback", "multiple",
		      "only one callback may be registered per process.");
	return;
    }

    XmAddWMProtocolCallback(shell, wm_save_yourself, proc, data);
}

#if NeedFunctionPrototypes
void XmtIconifyShell(Widget w)
#else
void XmtIconifyShell(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);

    if (!XtIsTopLevelShell(shell)) {
	XmtWarningMsg("XmtIconifyShell", "badShell",
		      "Shell widget '%s' is not a TopLevelShell.",
		      XtName(shell));
	return;
    }
    XtVaSetValues(shell, XtNiconic, True, NULL);
}

#if NeedFunctionPrototypes
void XmtDeiconifyShell(Widget w)
#else
void XmtDeiconifyShell(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);

    if (!XtIsTopLevelShell(shell)) {
	XmtWarningMsg("XmtDeiconifyShell", "badShell",
		      "Shell widget '%s' is not a TopLevelShell.",
		      XtName(shell));
	return;
    }
    XtVaSetValues(shell, XtNiconic, False, NULL);
    if (!XtIsRealized(shell)) return;
    XMapWindow(XtDisplay(shell), XtWindow(shell));
}

#if NeedFunctionPrototypes
void XmtRaiseShell(Widget w)
#else
void XmtRaiseShell(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);

    if (!XtIsRealized(shell)) return;
    XmtDeiconifyShell(shell);
    XRaiseWindow(XtDisplay(shell), XtWindow(shell));
}

#if NeedFunctionPrototypes
void XmtLowerShell(Widget w)
#else
void XmtLowerShell(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);

    if (!XtIsRealized(shell)) return;
    XLowerWindow(XtDisplay(shell), XtWindow(shell));
}


#if NeedFunctionPrototypes
void XmtSetFocusToShell(Widget w)
#else
void XmtSetFocusToShell(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);

    if (!XtIsRealized(shell)) return;
    XmtRaiseShell(shell);
    XSetInputFocus(XtDisplay(shell), XtWindow(shell), RevertToParent,
		   XtLastTimestampProcessed(XtDisplay(shell)));
}    

#if NeedFunctionPrototypes
void XmtWarpToShell(Widget w)
#else
void XmtWarpToShell(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);
    int width, height;

    if (!XtIsRealized(shell)) return;
    XmtRaiseShell(shell);
    XtVaGetValues(shell, XtNwidth, &width, XtNheight, &height, NULL);
    XWarpPointer(XtDisplay(shell), None, XtWindow(shell),
		 0,0,0,0, width/2, height/2);
    XSetInputFocus(XtDisplay(shell), XtWindow(shell), RevertToParent,
		   XtLastTimestampProcessed(XtDisplay(shell)));
}

#if NeedFunctionPrototypes
void XmtMoveShellToPointer(Widget w)
#else
void XmtMoveShellToPointer(w)
Widget w;
#endif
{
    Widget shell = XmtGetShell(w);
    Display *dpy = XtDisplay(shell);
    int width, height;
    int x,y;
    int screennum = XScreenNumberOfScreen(XtScreen(shell));
    int rootwidth = DisplayWidth(dpy, screennum);
    int rootheight = DisplayHeight(dpy, screennum);
    Window root, child;
    int dummyx, dummyy;
    unsigned mask;

    XmtRaiseShell(shell);
    XtVaGetValues(shell, XtNwidth, &width, XtNheight, &height, NULL);
    XQueryPointer(dpy, XtWindow(shell), &root, &child, &x, &y,
		  &dummyx, &dummyy, &mask);
    x -= width/2;
    y -= height/2;
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    if (x > rootwidth - width) x = rootwidth - width;
    if (y > rootheight - height) y = rootheight - height;

    XtVaSetValues(shell, XtNx, x, XtNy, y, NULL);
    XSetInputFocus(XtDisplay(shell), XtWindow(shell), RevertToParent,
		   XtLastTimestampProcessed(XtDisplay(shell)));
}


typedef enum {
    FocusNone,
    FocusSetInput,
    FocusWarp,
    FocusMove,
    FocusStyleUnset  /* a invalid default value */
} FocusStyle;

#define XmtNfocusStyle "focusStyle"
#define XmtCFocusStyle "FocusStyle"
#define XmtRFocusStyle "FocusStyle"

static String focus_names[] = { "focus", "move", "none", "warp"};
static Cardinal focus_values[] = {FocusSetInput, FocusMove, FocusNone, FocusWarp};

static XtResource focus_resources[] = {
  {XmtNfocusStyle, XmtCFocusStyle, XmtRFocusStyle,
       sizeof(FocusStyle), 0, XmtRFocusStyle, (XtPointer) FocusStyleUnset}
};
    

#if NeedFunctionPrototypes
void XmtFocusShell(Widget w)
#else
void XmtFocusShell(w)
Widget w;
#endif
{
    static Boolean got_focus_style;
    static FocusStyle focus_style;
    Widget shell = XmtGetShell(w);

    if (!got_focus_style) {
	XmtRegisterEnumConverter(XmtRFocusStyle, focus_names, focus_values,
				 XtNumber(focus_names), NULL);
	XtGetApplicationResources(shell, (XtPointer)&focus_style,
				  focus_resources, 1, NULL, 0);
	if (focus_style == FocusStyleUnset){
	    if (XmIsMotifWMRunning(shell)) focus_style = FocusSetInput;
	    else focus_style = FocusNone;
	}
	got_focus_style = True;
    }

    switch(focus_style) {
    case FocusNone:
    default:
	XmtRaiseShell(shell);
	break;
    case FocusSetInput:
	XmtSetFocusToShell(shell);
	break;
    case FocusWarp:
	XmtWarpToShell(shell);
	break;
    case FocusMove:
	XmtMoveShellToPointer(shell);
	break;
    }
}