Debian Codegraph @

xo/lwindow.cxx

raw
1 /* lwindow.cxx
2 $Id: lwindow.cxx,v 1.20 1998/10/15 04:11:51 elf Exp $
3
4 written by Marc Singer
5 8 May 1997
6
7 This file is part of the project XO. See the file README for
8 more information.
9
10 Copyright (C) 1997 Marc Singer
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 in a file called COPYING along with this program; if not, write to
24 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 02139, USA.
26
27 -----------
28 DESCRIPTION
29 -----------
30
31 */
32
33 #include "standard.h"
34 #include "ldisplay.h"
35 #include "lwindow.h"
36
37 /* LWindow::create
38
39 creates the X server window object. As a side effect this LWindow
40 structure is linked into the hierarchy of windows.
41
42 */
43
44 bool LWindow::create (LWindow* pWindowParent)
45 {
46 Window wndParent = (pWindowParent
47 ? pWindowParent->window ()
48 : XDefaultRootWindow (m_pDisplay->display ()));
49 XSetWindowAttributes attr = m_attr;
50 unsigned long mask = m_maskAttr;
51
52 // if (!(mask & CWBackPixel)) {
53 // mask |= CWBackPixel;
54 // attr.background_pixel = 19;
55 // }
56 // if (!(mask & CWBorderPixel)) {
57 // mask |= CWBorderPixel;
58 // attributes.border_pixel = 1;
59 // }
60 // if (!(mask & CWEventMask) || event_mask) {
61 // mask |= CWEventMask;
62 // attributes.event_mask = event_mask;
63 // }
64 // m_pEventHandlerMap = pEventHandlerMap;
65 mask |= CWColormap;
66 attr.colormap = m_pDisplay->colormap ();
67
68 mask |= CWEventMask;
69
70 m_window = XCreateWindow (xdisplay (),
71 wndParent,
72 m_x, m_y, m_width, m_height, 0,
73 m_pDisplay->depth (),
74 InputOutput, m_pDisplay->visual (),
75 mask, &attr);
76 m_pDisplay->flush (); // Helps notify semantics, really? **FIXME**
77
78 // m_event_mask = attr.event_mask;
79
80 if (m_window) {
81 // Properties
82 if (m_szXName) {
83 XChangeProperty (xdisplay (), m_window, XA_WM_NAME, XA_STRING,
84 sizeof (char)*8, PropModeReplace,
85 (unsigned char*) m_szXName, strlen (m_szXName));
86 XChangeProperty (xdisplay (), m_window, XA_WM_ICON_NAME, XA_STRING,
87 sizeof (char)*8, PropModeReplace,
88 (unsigned char*) m_szXName, strlen (m_szXName));
89 }
90
91 if (m_szXClass) {
92 int cch;
93 for (cch = 0; m_szXClass[cch];
94 cch += 1 + strlen (m_szXClass + cch));
95 XChangeProperty (xdisplay (), m_window, XA_WM_CLASS, XA_STRING,
96 sizeof (char)*8, PropModeReplace,
97 (unsigned char*) m_szXClass, cch);
98 }
99 {
100 XSizeHints hints;
101 memset (&hints, 0, sizeof (hints));
102 hints.flags |= (m_fOrigin ? USPosition : PPosition);
103 hints.flags |= (m_fExtent ? USSize : PSize);
104 // hints.flags |= PMinSize | PMaxSize | PAspect;
105 hints.x = m_x;
106 hints.y = m_y;
107 hints.width = m_width;
108 hints.height = m_height;
109 hints.min_width = 32;
110 hints.min_height = 32;
111 hints.max_width = 1024;
112 hints.max_height = 1024;
113 hints.min_aspect.x = 1;
114 hints.min_aspect.y = 1;
115 hints.max_aspect.x = 1;
116 hints.max_aspect.y = 1;
117 XSetWMNormalHints (xdisplay (), m_window, &hints);
118 }
119 {
120 XWMHints hints;
121 memset (&hints, 0, sizeof (hints));
122 hints.flags = InputHint | StateHint;
123 hints.input = True; // We default to a Passive input model
124 hints.initial_state = NormalState;
125 XSetWMHints (xdisplay (), m_window, &hints);
126 }
127 {
128 char sz[256];
129 gethostname (sz, sizeof (sz));
130 XChangeProperty (xdisplay (), m_window, XA_WM_CLIENT_MACHINE, XA_STRING,
131 sizeof (char)*8, PropModeReplace,
132 (unsigned char*) sz, strlen (sz));
133 }
134
135 m_pDisplay->hash_window (this);
136 m_pWindowParent = pWindowParent;
137
138 // Link
139 LWindow** ppWindowHere = (pWindowParent
140 ? &pWindowParent->m_pWindowChild
141 : m_pDisplay->root_windows ());
142 m_pWindowSibling = *ppWindowHere;
143 *ppWindowHere = this;
144
145 // Give due notice
146 XoEvent _event;
147 memset (&_event, 0, sizeof (_event));
148 XoCreateSelfWindowEvent& event = _event.xcreatewindow;
149 event.type = CreateSelfNotify;
150 event.display = m_pDisplay->display ();
151 event.window = m_window;
152 event.parent = wndParent;
153 event.x = m_x;
154 event.y = m_y;
155 event.width = m_width;
156 event.height = m_height;
157 m_pDisplay->dispatch ((XEvent*) &_event);
158 }
159
160 return m_window != 0;
161 }
162
163
164 LWindow* LWindow::find_sibling (int id)
165 {
166 for (LWindow* pWindow = this; pWindow; pWindow = pWindow->m_pWindowSibling)
167 if (pWindow->id () == id)
168 return pWindow;
169 return NULL;
170 }
171
172 PFNEvent LWindow::find_event (int event_type, XEvent* event)
173 {
174 for (EventMap* pMap = event_map ();
175 pMap && pMap->pfn; ++pMap)
176 if (pMap->event_type == event_type)
177 return pMap->pfn;
178
179 // This is the new, improved implementation of event handling. We
180 // simply call the virtual function and let the implementor take
181 // care of it. We still return a NULL pointer so that the old code
182 // works.
183
184 switch (event_type) {
185 case Expose:
186 handle_expose ((XExposeEvent*) event);
187 break;
188 case ButtonPress:
189 handle_buttonpress ((XButtonEvent*) event);
190 break;
191 case ConfigureNotify:
192 handle_configure ((XConfigureEvent*) event);
193 break;
194 case ResizeRequest:
195 handle_resizerequest ((XResizeRequestEvent*) event);
196 break;
197 default:
198 // printf ("event %d\n", event_type);
199 break;
200 }
201
202 return (PFNEvent) NULL;
203 }
204
205
206 void LWindow::geometry (const char* sz)
207 {
208 if (!sz || !* sz)
209 return;
210
211 int x = m_x;
212 int y = m_y;
213 unsigned int width = m_width;
214 unsigned int height = m_height;
215 int flags = XParseGeometry (sz, &x, &y, &width, &height);
216 if (flags & XNegative)
217 x = m_pDisplay->width () - width;
218 if (flags & YNegative)
219 y = m_pDisplay->height () - height;
220 if (flags & XValue) {
221 m_x = x;
222 m_fOrigin = True;
223 }
224 if (flags & YValue) {
225 m_y = y;
226 m_fOrigin = True;
227 }
228 if (flags & WidthValue) {
229 m_width = width;
230 m_fExtent = True;
231 }
232 if (flags & HeightValue) {
233 m_height = height;
234 m_fExtent = True;
235 }
236 }
237
238
239 /* LWindow::init
240
241 initializes an LWindow instance from an existing LWindow instance.
242 This can be though of as a clone operation since everything except
243 for the X-Windows window handle is duplicated. (FXIME, really?)
244
245 */
246
247 void LWindow::init (LWindow* pWindow)
248 {
249 // Locality
250 m_pDisplay = pWindow->m_pDisplay;
251 // Appearance
252 m_maskAttr = pWindow->m_maskAttr;
253 m_attr = pWindow->m_attr;
254 m_x = pWindow->m_x;
255 m_y = pWindow->m_y;
256 m_width = pWindow->m_width;
257 m_height = pWindow->m_height;
258
259 // Personality
260 m_cbInstance = pWindow->m_cbInstance;
261 m_pEventMap = pWindow->m_pEventMap;
262 m_pfnNotify = pWindow->m_pfnNotify;
263
264 memcpy (this + 1, pWindow + 1, m_cbInstance);// Duplicate instance data
265 }
266
267
268 void LWindow::lower (void)
269 {
270 if (!m_window)
271 return;
272 XLowerWindow (m_pDisplay->display (), m_window);
273 }
274
275 void LWindow::map (void)
276 {
277 if (!m_window)
278 return;
279 XMapWindow (m_pDisplay->display (), m_window);
280 }
281
282
283 void LWindow::notify (int child_event, int cArgs, int* rgArgs)
284 {
285 // Some windows don't notify
286 if (!m_pWindowOwner || !m_pWindowOwner->m_pfnNotify)
287 return;
288
289 XoEvent _event;
290 memset (&_event, 0, sizeof (_event));
291
292 XoChildNotifyEvent& event = _event.xchildnotify;
293 event.type = ChildNotify;
294 event.display = m_pDisplay->display ();
295 event.window = m_pWindowOwner->window ();
296 event.pWindowChild = this;
297 event.pvParentData = m_pvOwner;
298 event.child_type = child_event;
299 for (int i = 0; i < cArgs && rgArgs
300 && i < int (sizeof (event.rgData)/sizeof (int)); ++i)
301 event.rgData[i] = rgArgs[i];
302 (m_pWindowOwner->*m_pfnNotify) ((XEvent*) &event);
303 // m_pDisplay->dispatch ((XEvent*) &event);
304 }
305
306
307 void LWindow::release_this (void)
308 {
309 if (m_szName) {
310 free ((void*) m_szName);
311 m_szName = NULL;
312 }
313 if (m_szXName) {
314 free ((void*) m_szXName);
315 m_szXName = NULL;
316 }
317 if (m_szXClass) {
318 free ((void*) m_szXClass);
319 m_szXClass = NULL;
320 }
321 if (m_window) {
322 m_pDisplay->unhash_window (this);
323 XDestroyWindow (m_pDisplay->display (), m_window);
324 m_window = 0;
325 }
326 }
327
328
329 /* LWindow::qualify
330
331 is a first attempt to provide information that Window Managers can
332 use to recognize us.
333
334 */
335
336 void LWindow::qualify (const char* szXName, const char* szXClass)
337 {
338 if (m_szXName) {
339 free ((void*) m_szXName);
340 m_szXName = NULL;
341 }
342 if (szXName && *szXName) {
343 m_szXName = (char*) malloc (strlen (szXName) + 1);
344 memcpy ((void*) m_szXName, szXName, strlen (szXName) + 1);
345 }
346 if (m_szXClass) {
347 free ((void*) m_szXClass);
348 m_szXClass = NULL;
349 }
350 if (szXClass && *szXClass) {
351 int cch = 0;
352 while (szXClass[cch])
353 cch += 1 + strlen (szXClass + cch);
354 // fprintf (stderr, "class is %d long\n", cch);
355 m_szXClass = (char*) malloc (cch + 1);
356 memcpy ((void*) m_szXClass, szXClass, cch + 1);
357 }
358 }
359
360 void LWindow::text (const char* sz)
361 {
362 if (m_szName) {
363 free ((void*) m_szName);
364 m_szName = NULL;
365 }
366 if (!sz || !*sz)
367 return;
368
369 m_szName = (char*) malloc (strlen (sz) + 1);
370 memcpy ((void*) m_szName, sz, strlen (sz) + 1);
371 }
372
373 void LWindow::transient_for (LWindow* pWindow)
374 {
375 XSetTransientForHint (m_pDisplay->display (),
376 pWindow ? pWindow->m_window
377 : XDefaultRootWindow (m_pDisplay->display ()),
378 m_window);
379 }
380
381 void LWindow::net_window_type (int type)
382 {
383 const char* sz = NULL;
384 switch (type) {
385 default:
386 case windowTypeNormal:
387 sz = "_NET_WM_WINDOW_TYPE_NORMAL";
388 break;
389 case windowTypeDesktop:
390 sz = "_NET_WM_WINDOW_TYPE_DESKTOP";
391 break;
392 case windowTypeDock:
393 sz = "_NET_WM_WINDOW_TYPE_DOCK";
394 break;
395 case windowTypeToolbar:
396 sz = "_NET_WM_WINDOW_TYPE_TOOLBAR";
397 break;
398 case windowTypeMenu:
399 sz = "_NET_WM_WINDOW_TYPE_MENU";
400 break;
401 case windowTypeUtility:
402 sz = "_NET_WM_WINDOW_TYPE_UTILITY";
403 break;
404 case windowTypeSplash:
405 sz = "_NET_WM_WINDOW_TYPE_SPLASH";
406 break;
407 case windowTypeDialog:
408 sz = "_NET_WM_WINDOW_TYPE_DIALOG";
409 break;
410 }
411
412 Atom a = XInternAtom (xdisplay (), "_NET_WM_WINDOW_TYPE", true);
413 Atom b = XInternAtom (xdisplay (), sz, true);
414
415 XChangeProperty (xdisplay (), m_window, a, XA_ATOM, sizeof (Atom)*8,
416 PropModeReplace, (unsigned char*) &b, 1);
417 }