8 typedef struct W_Window
{
12 struct W_Window
*nextPtr
; /* next in the window list */
14 struct W_Window
*owner
;
18 WMPixmap
*miniImage
; /* miniwindow */
23 WMSize resizeIncrement
;
33 WMAction
*closeAction
;
40 unsigned configured
:1;
41 unsigned documentEdited
:1;
54 Pixmap miniaturize_pixmap
; /* pixmap for miniaturize button */
55 Pixmap close_pixmap
; /* pixmap for close button */
56 Pixmap miniaturize_mask
; /* miniaturize pixmap mask */
57 Pixmap close_mask
; /* close pixmap mask */
59 } GNUstepWMAttributes
;
61 #define GSWindowStyleAttr (1<<0)
62 #define GSWindowLevelAttr (1<<1)
63 #define GSMiniaturizePixmapAttr (1<<3)
64 #define GSClosePixmapAttr (1<<4)
65 #define GSMiniaturizeMaskAttr (1<<5)
66 #define GSCloseMaskAttr (1<<6)
67 #define GSExtraFlagsAttr (1<<7)
70 #define GSDocumentEditedFlag (1<<0)
71 #define GSNoApplicationIconFlag (1<<5)
73 #define WMFHideOtherApplications 10
74 #define WMFHideApplication 12
76 static void willResizeWindow(W_ViewDelegate
*, WMView
*, unsigned *, unsigned *);
78 struct W_ViewDelegate _WindowViewDelegate
= {
86 #define DEFAULT_WIDTH 400
87 #define DEFAULT_HEIGHT 180
88 #define DEFAULT_TITLE ""
90 static void destroyWindow(_Window
* win
);
92 static void handleEvents();
94 static void realizeWindow();
96 static void realizeObserver(void *self
, WMNotification
* not)
101 WMWindow
*WMCreatePanelWithStyleForWindow(WMWindow
* owner
, char *name
, int style
)
105 win
= WMCreateWindowWithStyle(owner
->view
->screen
, name
, style
);
111 WMWindow
*WMCreatePanelForWindow(WMWindow
* owner
, char *name
)
113 return WMCreatePanelWithStyleForWindow(owner
, name
,
114 WMTitledWindowMask
| WMClosableWindowMask
| WMResizableWindowMask
);
117 void WMChangePanelOwner(WMWindow
* win
, WMWindow
* newOwner
)
119 win
->owner
= newOwner
;
121 if (win
->view
->flags
.realized
&& newOwner
) {
122 XSetTransientForHint(win
->view
->screen
->display
, win
->view
->window
, newOwner
->view
->window
);
126 WMWindow
*WMCreateWindow(WMScreen
* screen
, char *name
)
128 return WMCreateWindowWithStyle(screen
, name
, WMTitledWindowMask
129 | WMClosableWindowMask
130 | WMMiniaturizableWindowMask
| WMResizableWindowMask
);
133 WMWindow
*WMCreateWindowWithStyle(WMScreen
* screen
, char *name
, int style
)
137 win
= wmalloc(sizeof(_Window
));
138 win
->widgetClass
= WC_Window
;
140 win
->view
= W_CreateTopView(screen
);
145 win
->view
->self
= win
;
147 win
->view
->delegate
= &_WindowViewDelegate
;
149 win
->wname
= wstrdup(name
);
151 /* add to the window list of the screen (application) */
152 win
->nextPtr
= screen
->windowList
;
153 screen
->windowList
= win
;
155 WMCreateEventHandler(win
->view
, ExposureMask
| StructureNotifyMask
156 | ClientMessageMask
| FocusChangeMask
, handleEvents
, win
);
158 W_ResizeView(win
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
160 WMAddNotificationObserver(realizeObserver
, win
, WMViewRealizedNotification
, win
->view
);
162 win
->flags
.style
= style
;
164 win
->level
= WMNormalWindowLevel
;
166 /* kluge. Find a better solution */
167 W_SetFocusOfTopLevel(win
->view
, win
->view
);
172 static void setWindowTitle(WMWindow
* win
, const char *title
)
174 WMScreen
*scr
= win
->view
->screen
;
175 XTextProperty property
;
178 result
= XmbTextListToTextProperty(scr
->display
, (char **)&title
, 1, XStdICCTextStyle
, &property
);
179 if (result
== XNoMemory
|| result
== XLocaleNotSupported
) {
180 wwarning("window title conversion error... using STRING encoding");
181 XStoreName(scr
->display
, win
->view
->window
, title
);
183 XSetWMName(scr
->display
, win
->view
->window
, &property
);
185 XFree(property
.value
);
188 XChangeProperty(scr
->display
, win
->view
->window
,
189 scr
->netwmName
, scr
->utf8String
, 8,
190 PropModeReplace
, (unsigned char *)title
, strlen(title
));
193 static void setMiniwindowTitle(WMWindow
* win
, const char *title
)
195 WMScreen
*scr
= win
->view
->screen
;
196 XTextProperty property
;
199 result
= XmbTextListToTextProperty(scr
->display
, (char **)&title
, 1, XStdICCTextStyle
, &property
);
200 if (result
== XNoMemory
|| result
== XLocaleNotSupported
) {
201 wwarning("icon title conversion error..using STRING encoding");
202 XSetIconName(scr
->display
, win
->view
->window
, title
);
204 XSetWMIconName(scr
->display
, win
->view
->window
, &property
);
206 XFree(property
.value
);
209 XChangeProperty(scr
->display
, win
->view
->window
,
210 scr
->netwmIconName
, scr
->utf8String
, 8,
211 PropModeReplace
, (unsigned char *)title
, strlen(title
));
214 static void setMiniwindow(WMWindow
*win
, RImage
*image
)
216 WMScreen
*scr
= win
->view
->screen
;
224 data
= wmalloc((image
->width
* image
->height
+ 2) * sizeof(long));
227 data
[o
++] = image
->width
;
228 data
[o
++] = image
->height
;
230 for (y
= 0; y
< image
->height
; y
++) {
231 for (x
= 0; x
< image
->width
; x
++) {
233 int offs
= (x
+ y
* image
->width
);
235 if (image
->format
== RRGBFormat
)
236 pixel
= image
->data
[offs
* 3] << 16 | image
->data
[offs
* 3 + 1] << 8
237 | image
->data
[offs
* 3 + 2];
239 pixel
= image
->data
[offs
* 4] << 16 | image
->data
[offs
* 4 + 1] << 8
240 | image
->data
[offs
* 4 + 2] | image
->data
[offs
* 4 + 3] << 24;
246 XChangeProperty(scr
->display
, win
->view
->window
, scr
->netwmIcon
,
247 XA_CARDINAL
, 32, PropModeReplace
,
248 (unsigned char *)data
, (image
->width
* image
->height
+ 2));
253 void WMSetWindowTitle(WMWindow
* win
, char *title
)
255 if (win
->title
!= NULL
)
258 win
->title
= wstrdup(title
);
262 if (win
->view
->flags
.realized
) {
263 setWindowTitle(win
, title
);
267 void WMSetWindowCloseAction(WMWindow
* win
, WMAction
* action
, void *clientData
)
272 WMScreen
*scr
= win
->view
->screen
;
274 if (win
->view
->flags
.realized
) {
275 if (action
&& !win
->closeAction
) {
276 if (!XGetWMProtocols(scr
->display
, win
->view
->window
, &atoms
, &count
)) {
279 newAtoms
= wmalloc((count
+ 1) * sizeof(Atom
));
281 memcpy(newAtoms
, atoms
, count
* sizeof(Atom
));
282 newAtoms
[count
++] = scr
->deleteWindowAtom
;
283 XSetWMProtocols(scr
->display
, win
->view
->window
, newAtoms
, count
);
287 } else if (!action
&& win
->closeAction
) {
290 if (XGetWMProtocols(scr
->display
, win
->view
->window
, &atoms
, &count
) && count
> 0) {
291 newAtoms
= wmalloc((count
- 1) * sizeof(Atom
));
293 for (i
= 0; i
< count
; i
++) {
294 if (atoms
[i
] != scr
->deleteWindowAtom
) {
295 newAtoms
[i
] = atoms
[i
];
299 XSetWMProtocols(scr
->display
, win
->view
->window
, newAtoms
, ncount
);
306 win
->closeAction
= action
;
307 win
->closeData
= clientData
;
310 static void willResizeWindow(W_ViewDelegate
* self
, WMView
* view
, unsigned *width
, unsigned *height
)
312 WMWindow
*win
= (WMWindow
*) view
->self
;
314 if (win
->minSize
.width
> 0 && win
->minSize
.height
> 0) {
315 if (*width
< win
->minSize
.width
)
316 *width
= win
->minSize
.width
;
317 if (*height
< win
->minSize
.height
)
318 *height
= win
->minSize
.height
;
321 if (win
->maxSize
.width
> 0 && win
->maxSize
.height
> 0) {
322 if (*width
> win
->maxSize
.width
)
323 *width
= win
->maxSize
.width
;
324 if (*height
> win
->maxSize
.height
)
325 *height
= win
->maxSize
.height
;
329 static void setSizeHints(WMWindow
* win
)
333 hints
= XAllocSizeHints();
335 wwarning("could not allocate memory for window size hints");
341 if (win
->flags
.setPPos
) {
342 hints
->flags
|= PPosition
;
343 hints
->x
= win
->ppos
.x
;
344 hints
->y
= win
->ppos
.y
;
346 if (win
->flags
.setUPos
) {
347 hints
->flags
|= USPosition
;
348 hints
->x
= win
->upos
.x
;
349 hints
->y
= win
->upos
.y
;
351 if (win
->minSize
.width
> 0 && win
->minSize
.height
> 0) {
352 hints
->flags
|= PMinSize
;
353 hints
->min_width
= win
->minSize
.width
;
354 hints
->min_height
= win
->minSize
.height
;
356 if (win
->maxSize
.width
> 0 && win
->maxSize
.height
> 0) {
357 hints
->flags
|= PMaxSize
;
358 hints
->max_width
= win
->maxSize
.width
;
359 hints
->max_height
= win
->maxSize
.height
;
361 if (win
->baseSize
.width
> 0 && win
->baseSize
.height
> 0) {
362 hints
->flags
|= PBaseSize
;
363 hints
->base_width
= win
->baseSize
.width
;
364 hints
->base_height
= win
->baseSize
.height
;
366 if (win
->resizeIncrement
.width
> 0 && win
->resizeIncrement
.height
> 0) {
367 hints
->flags
|= PResizeInc
;
368 hints
->width_inc
= win
->resizeIncrement
.width
;
369 hints
->height_inc
= win
->resizeIncrement
.height
;
371 if (win
->flags
.setAspect
) {
372 hints
->flags
|= PAspect
;
373 hints
->min_aspect
.x
= win
->minAspect
.x
;
374 hints
->min_aspect
.y
= win
->minAspect
.y
;
375 hints
->max_aspect
.x
= win
->maxAspect
.x
;
376 hints
->max_aspect
.y
= win
->maxAspect
.y
;
380 XSetWMNormalHints(win
->view
->screen
->display
, win
->view
->window
, hints
);
385 static void writeGNUstepWMAttr(WMScreen
* scr
, Window window
, GNUstepWMAttributes
* attr
)
387 unsigned long data
[9];
389 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
390 data
[0] = attr
->flags
;
391 data
[1] = attr
->window_style
;
392 data
[2] = attr
->window_level
;
393 data
[3] = 0; /* reserved */
394 /* The X protocol says XIDs are 32bit */
395 data
[4] = attr
->miniaturize_pixmap
;
396 data
[5] = attr
->close_pixmap
;
397 data
[6] = attr
->miniaturize_mask
;
398 data
[7] = attr
->close_mask
;
399 data
[8] = attr
->extra_flags
;
400 XChangeProperty(scr
->display
, window
, scr
->attribsAtom
, scr
->attribsAtom
,
401 32, PropModeReplace
, (unsigned char *)data
, 9);
404 static void setWindowMakerHints(WMWindow
* win
)
406 GNUstepWMAttributes attribs
;
407 WMScreen
*scr
= WMWidgetScreen(win
);
409 memset(&attribs
, 0, sizeof(GNUstepWMAttributes
));
410 attribs
.flags
= GSWindowStyleAttr
| GSWindowLevelAttr
| GSExtraFlagsAttr
;
411 attribs
.window_style
= win
->flags
.style
;
412 attribs
.window_level
= win
->level
;
413 if (win
->flags
.documentEdited
)
414 attribs
.extra_flags
= GSDocumentEditedFlag
;
416 attribs
.extra_flags
= 0;
418 writeGNUstepWMAttr(scr
, win
->view
->window
, &attribs
);
421 static void realizeWindow(WMWindow
* win
)
424 XClassHint
*classHint
;
425 WMScreen
*scr
= win
->view
->screen
;
429 classHint
= XAllocClassHint();
430 classHint
->res_name
= win
->wname
;
431 classHint
->res_class
= WMGetApplicationName();
432 XSetClassHint(scr
->display
, win
->view
->window
, classHint
);
435 hints
= XAllocWMHints();
437 if (!scr
->aflags
.simpleApplication
) {
438 hints
->flags
|= WindowGroupHint
;
439 hints
->window_group
= scr
->groupLeader
;
441 if (win
->miniImage
) {
442 hints
->flags
|= IconPixmapHint
;
443 hints
->icon_pixmap
= WMGetPixmapXID(win
->miniImage
);
444 hints
->icon_mask
= WMGetPixmapMaskXID(win
->miniImage
);
445 if (hints
->icon_mask
!= None
) {
446 hints
->flags
|= IconMaskHint
;
449 if (hints
->flags
!= 0)
450 XSetWMHints(scr
->display
, win
->view
->window
, hints
);
454 if (win
->closeAction
) {
455 atoms
[count
++] = scr
->deleteWindowAtom
;
459 XSetWMProtocols(scr
->display
, win
->view
->window
, atoms
, count
);
461 if (win
->title
|| win
->miniTitle
)
462 XmbSetWMProperties(scr
->display
, win
->view
->window
, win
->title
,
463 win
->miniTitle
, NULL
, 0, NULL
, NULL
, NULL
);
465 setWindowMakerHints(win
);
470 XSetTransientForHint(scr
->display
, win
->view
->window
, win
->owner
->view
->window
);
474 setWindowTitle(win
, win
->title
);
477 void WMSetWindowAspectRatio(WMWindow
* win
, int minX
, int minY
, int maxX
, int maxY
)
479 win
->flags
.setAspect
= 1;
480 win
->minAspect
.x
= minX
;
481 win
->minAspect
.y
= minY
;
482 win
->maxAspect
.x
= maxX
;
483 win
->maxAspect
.y
= maxY
;
484 if (win
->view
->flags
.realized
)
488 void WMSetWindowInitialPosition(WMWindow
* win
, int x
, int y
)
490 win
->flags
.setPPos
= 1;
493 if (win
->view
->flags
.realized
)
495 WMMoveWidget(win
, x
, y
);
498 void WMSetWindowUserPosition(WMWindow
* win
, int x
, int y
)
500 win
->flags
.setUPos
= 1;
503 if (win
->view
->flags
.realized
)
505 WMMoveWidget(win
, x
, y
);
508 void WMSetWindowMinSize(WMWindow
* win
, unsigned width
, unsigned height
)
510 win
->minSize
.width
= width
;
511 win
->minSize
.height
= height
;
512 if (win
->view
->flags
.realized
)
516 void WMSetWindowMaxSize(WMWindow
* win
, unsigned width
, unsigned height
)
518 win
->maxSize
.width
= width
;
519 win
->maxSize
.height
= height
;
520 if (win
->view
->flags
.realized
)
524 void WMSetWindowBaseSize(WMWindow
* win
, unsigned width
, unsigned height
)
526 /* TODO: validate sizes */
527 win
->baseSize
.width
= width
;
528 win
->baseSize
.height
= height
;
529 if (win
->view
->flags
.realized
)
533 void WMSetWindowResizeIncrements(WMWindow
* win
, unsigned wIncr
, unsigned hIncr
)
535 win
->resizeIncrement
.width
= wIncr
;
536 win
->resizeIncrement
.height
= hIncr
;
537 if (win
->view
->flags
.realized
)
541 void WMSetWindowLevel(WMWindow
* win
, int level
)
544 if (win
->view
->flags
.realized
)
545 setWindowMakerHints(win
);
548 void WMSetWindowDocumentEdited(WMWindow
* win
, Bool flag
)
550 flag
= ((flag
== 0) ? 0 : 1);
551 if (win
->flags
.documentEdited
!= flag
) {
552 win
->flags
.documentEdited
= flag
;
553 if (win
->view
->flags
.realized
)
554 setWindowMakerHints(win
);
558 void WMSetWindowMiniwindowImage(WMWindow
* win
, RImage
* image
)
560 if (win
->view
->flags
.realized
)
561 setMiniwindow(win
, image
);
564 void WMSetWindowMiniwindowPixmap(WMWindow
* win
, WMPixmap
* pixmap
)
566 if ((win
->miniImage
&& !pixmap
) || (!win
->miniImage
&& pixmap
)) {
568 WMReleasePixmap(win
->miniImage
);
571 win
->miniImage
= WMRetainPixmap(pixmap
);
573 win
->miniImage
= NULL
;
575 if (win
->view
->flags
.realized
) {
578 hints
= XGetWMHints(win
->view
->screen
->display
, win
->view
->window
);
580 hints
= XAllocWMHints();
582 wwarning("could not allocate memory for WM hints");
588 hints
->flags
|= IconPixmapHint
;
589 hints
->icon_pixmap
= WMGetPixmapXID(pixmap
);
590 hints
->icon_mask
= WMGetPixmapMaskXID(pixmap
);
591 if (hints
->icon_mask
!= None
) {
592 hints
->flags
|= IconMaskHint
;
595 XSetWMHints(win
->view
->screen
->display
, win
->view
->window
, hints
);
601 void WMSetWindowMiniwindowTitle(WMWindow
* win
, char *title
)
603 if ((win
->miniTitle
&& !title
) || (!win
->miniTitle
&& title
)
604 || (title
&& win
->miniTitle
&& strcoll(title
, win
->miniTitle
) != 0)) {
606 wfree(win
->miniTitle
);
609 win
->miniTitle
= wstrdup(title
);
611 win
->miniTitle
= NULL
;
613 if (win
->view
->flags
.realized
) {
614 setMiniwindowTitle(win
, title
);
619 void WMCloseWindow(WMWindow
* win
)
622 /* withdraw the window */
623 if (win
->view
->flags
.realized
)
624 XWithdrawWindow(win
->view
->screen
->display
, win
->view
->window
, win
->view
->screen
->screen
);
627 static void handleEvents(XEvent
* event
, void *clientData
)
629 _Window
*win
= (_Window
*) clientData
;
630 W_View
*view
= win
->view
;
632 switch (event
->type
) {
634 if (event
->xclient
.message_type
== win
->view
->screen
->protocolsAtom
635 && event
->xclient
.format
== 32
636 && event
->xclient
.data
.l
[0] == win
->view
->screen
->deleteWindowAtom
) {
638 if (win
->closeAction
) {
639 (*win
->closeAction
) (win
, win
->closeData
);
644 * was causing windows to ignore commands like closeWindow
645 * after the windows is iconized/restored or a workspace change
646 * if this is really needed, put the MapNotify portion too and
647 * fix the restack bug in wmaker
661 case ConfigureNotify
:
662 if (event
->xconfigure
.width
!= view
->size
.width
|| event
->xconfigure
.height
!= view
->size
.height
) {
664 view
->size
.width
= event
->xconfigure
.width
;
665 view
->size
.height
= event
->xconfigure
.height
;
667 if (view
->flags
.notifySizeChanged
) {
668 WMPostNotificationName(WMViewSizeDidChangeNotification
, view
, NULL
);
671 if (event
->xconfigure
.x
!= view
->pos
.x
|| event
->xconfigure
.y
!= view
->pos
.y
) {
673 if (event
->xconfigure
.send_event
) {
674 view
->pos
.x
= event
->xconfigure
.x
;
675 view
->pos
.y
= event
->xconfigure
.y
;
679 XTranslateCoordinates(view
->screen
->display
,
680 view
->window
, view
->screen
->rootWin
,
681 event
->xconfigure
.x
, event
->xconfigure
.y
,
682 &view
->pos
.x
, &view
->pos
.y
, &foo
);
689 static void destroyWindow(_Window
* win
)
691 WMScreen
*scr
= win
->view
->screen
;
693 WMRemoveNotificationObserver(win
);
695 if (scr
->windowList
== win
) {
696 scr
->windowList
= scr
->windowList
->nextPtr
;
699 ptr
= scr
->windowList
;
702 while (ptr
->nextPtr
) {
703 if (ptr
->nextPtr
== win
) {
704 ptr
->nextPtr
= ptr
->nextPtr
->nextPtr
;
716 if (win
->miniTitle
) {
717 wfree(win
->miniTitle
);
720 if (win
->miniImage
) {
721 WMReleasePixmap(win
->miniImage
);