9 typedef struct W_Window
{
13 struct W_Window
*nextPtr
; /* next in the window list */
15 struct W_Window
*owner
;
19 WMPixmap
*miniImage
; /* miniwindow */
24 WMSize resizeIncrement
;
32 WMAction
*closeAction
;
39 unsigned configured
:1;
40 unsigned documentEdited
:1;
53 Pixmap miniaturize_pixmap
; /* pixmap for miniaturize button */
54 Pixmap close_pixmap
; /* pixmap for close button */
55 Pixmap miniaturize_mask
; /* miniaturize pixmap mask */
56 Pixmap close_mask
; /* close pixmap mask */
58 } GNUstepWMAttributes
;
60 #define GSWindowStyleAttr (1<<0)
61 #define GSWindowLevelAttr (1<<1)
62 #define GSMiniaturizePixmapAttr (1<<3)
63 #define GSClosePixmapAttr (1<<4)
64 #define GSMiniaturizeMaskAttr (1<<5)
65 #define GSCloseMaskAttr (1<<6)
66 #define GSExtraFlagsAttr (1<<7)
69 #define GSDocumentEditedFlag (1<<0)
70 #define GSNoApplicationIconFlag (1<<5)
72 #define WMFHideOtherApplications 10
73 #define WMFHideApplication 12
77 static void resizeWindow(WMWidget
*, unsigned, unsigned);
79 struct W_ViewProcedureTable _WindowViewProcedures
= {
86 #define DEFAULT_WIDTH 400
87 #define DEFAULT_HEIGHT 180
88 #define DEFAULT_TITLE ""
91 static void destroyWindow(_Window
*win
);
93 static void handleEvents();
95 static void realizeWindow();
98 realizeObserver(void *self
, WMNotification
*not)
105 WMCreatePanelWithStyleForWindow(WMWindow
*owner
, char *name
, int style
)
109 win
= WMCreateWindowWithStyle(owner
->view
->screen
, name
, style
);
118 WMCreatePanelForWindow(WMWindow
*owner
, char *name
)
120 return WMCreatePanelWithStyleForWindow(owner
, name
,
122 |WMClosableWindowMask
123 |WMResizableWindowMask
);
128 WMChangePanelOwner(WMWindow
*win
, WMWindow
*newOwner
)
130 win
->owner
= newOwner
;
132 if (win
->view
->flags
.realized
&& newOwner
) {
133 XSetTransientForHint(win
->view
->screen
->display
, win
->view
->window
,
134 newOwner
->view
->window
);
141 WMCreateWindow(WMScreen
*screen
, char *name
)
143 return WMCreateWindowWithStyle(screen
, name
, WMTitledWindowMask
144 |WMClosableWindowMask
145 |WMMiniaturizableWindowMask
146 |WMResizableWindowMask
);
152 WMCreateWindowWithStyle(WMScreen
*screen
, char *name
, int style
)
156 win
= wmalloc(sizeof(_Window
));
157 memset(win
, 0, sizeof(_Window
));
159 win
->widgetClass
= WC_Window
;
161 win
->view
= W_CreateTopView(screen
);
166 win
->view
->self
= win
;
168 win
->wname
= wstrdup(name
);
170 /* add to the window list of the screen (application) */
171 win
->nextPtr
= screen
->windowList
;
172 screen
->windowList
= win
;
174 WMCreateEventHandler(win
->view
, ExposureMask
|StructureNotifyMask
175 |ClientMessageMask
|FocusChangeMask
,
178 W_ResizeView(win
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
180 WMAddNotificationObserver(realizeObserver
, win
,
181 WMViewRealizedNotification
, win
->view
);
183 win
->flags
.style
= style
;
185 win
->level
= WMNormalWindowLevel
;
187 /* kluge. Find a better solution */
188 W_SetFocusOfTopLevel(win
->view
, win
->view
);
195 WMSetWindowTitle(WMWindow
*win
, char *title
)
197 XTextProperty property
;
200 if (win
->title
!=NULL
)
203 win
->title
= wstrdup(title
);
207 if (win
->view
->flags
.realized
) {
208 result
= XmbTextListToTextProperty (win
->view
->screen
->display
,
209 &title
, 1, XStdICCTextStyle
,
211 if (result
== XNoMemory
|| result
== XLocaleNotSupported
) {
212 wwarning("window title conversion error... using STRING encoding");
213 XStoreName(win
->view
->screen
->display
, win
->view
->window
, title
);
215 XSetWMName(win
->view
->screen
->display
, win
->view
->window
, &property
);
217 XFree(property
.value
);
226 WMSetWindowCloseAction(WMWindow
*win
, WMAction
*action
, void *clientData
)
231 WMScreen
*scr
= win
->view
->screen
;
233 if (win
->view
->flags
.realized
) {
234 if (action
&& !win
->closeAction
) {
235 if (!XGetWMProtocols(scr
->display
, win
->view
->window
, &atoms
,
239 newAtoms
= wmalloc((count
+1)*sizeof(Atom
));
241 memcpy(newAtoms
, atoms
, count
*sizeof(Atom
));
242 newAtoms
[count
++] = scr
->deleteWindowAtom
;
243 XSetWMProtocols(scr
->display
, win
->view
->window
, newAtoms
, count
);
247 } else if (!action
&& win
->closeAction
) {
250 if (XGetWMProtocols(scr
->display
, win
->view
->window
, &atoms
,
251 &count
) && count
>0) {
252 newAtoms
= wmalloc((count
-1)*sizeof(Atom
));
254 for (i
=0; i
< count
; i
++) {
255 if (atoms
[i
]!=scr
->deleteWindowAtom
) {
256 newAtoms
[i
] = atoms
[i
];
260 XSetWMProtocols(scr
->display
, win
->view
->window
, newAtoms
,
268 win
->closeAction
= action
;
269 win
->closeData
= clientData
;
275 resizeWindow(WMWidget
*w
, unsigned width
, unsigned height
)
277 WMWindow
*win
= (WMWindow
*)w
;
279 if (win
->minSize
.width
> 0 && win
->minSize
.height
> 0) {
280 if (width
< win
->minSize
.width
)
281 width
= win
->minSize
.width
;
282 if (height
< win
->minSize
.height
)
283 height
= win
->minSize
.height
;
286 if (win
->maxSize
.width
> 0 && win
->maxSize
.height
> 0) {
287 if (width
> win
->maxSize
.width
)
288 width
= win
->maxSize
.width
;
289 if (height
> win
->maxSize
.height
)
290 height
= win
->maxSize
.height
;
293 W_ResizeView(win
->view
, width
, height
);
298 setSizeHints(WMWindow
*win
)
302 hints
= XAllocSizeHints();
304 wwarning("could not allocate memory for window size hints");
310 if (win
->flags
.upos_set
) {
311 hints
->flags
|= USPosition
;
312 hints
->x
= win
->upos
.x
;
313 hints
->y
= win
->upos
.y
;
315 if (win
->usize
.width
>0 && win
->usize
.height
>0) {
316 hints
->flags
|= USSize
;
317 hints
->width
= win
->usize
.width
;
318 hints
->height
= win
->usize
.height
;
320 if (win
->minSize
.width
>0 && win
->minSize
.height
>0) {
321 hints
->flags
|= PMinSize
;
322 hints
->min_width
= win
->minSize
.width
;
323 hints
->min_height
= win
->minSize
.height
;
325 if (win
->maxSize
.width
>0 && win
->maxSize
.height
>0) {
326 hints
->flags
|= PMaxSize
;
327 hints
->max_width
= win
->maxSize
.width
;
328 hints
->max_height
= win
->maxSize
.height
;
330 if (win
->baseSize
.width
>0 && win
->baseSize
.height
>0) {
331 hints
->flags
|= PBaseSize
;
332 hints
->base_width
= win
->baseSize
.width
;
333 hints
->base_height
= win
->baseSize
.height
;
335 if (win
->resizeIncrement
.width
>0 && win
->resizeIncrement
.height
>0) {
336 hints
->flags
|= PResizeInc
;
337 hints
->width_inc
= win
->resizeIncrement
.width
;
338 hints
->height_inc
= win
->resizeIncrement
.height
;
342 XSetWMNormalHints(win
->view
->screen
->display
, win
->view
->window
, hints
);
350 writeGNUstepWMAttr(WMScreen
*scr
, Window window
, GNUstepWMAttributes
*attr
)
352 unsigned long data
[9];
354 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
355 data
[0] = attr
->flags
;
356 data
[1] = attr
->window_style
;
357 data
[2] = attr
->window_level
;
358 data
[3] = 0; /* reserved */
359 /* The X protocol says XIDs are 32bit */
360 data
[4] = attr
->miniaturize_pixmap
;
361 data
[5] = attr
->close_pixmap
;
362 data
[6] = attr
->miniaturize_mask
;
363 data
[7] = attr
->close_mask
;
364 data
[8] = attr
->extra_flags
;
365 XChangeProperty(scr
->display
, window
, scr
->attribsAtom
, scr
->attribsAtom
,
366 32, PropModeReplace
, (unsigned char *)data
, 9);
371 setWindowMakerHints(WMWindow
*win
)
373 GNUstepWMAttributes attribs
;
374 WMScreen
*scr
= WMWidgetScreen(win
);
376 memset(&attribs
, 0, sizeof(GNUstepWMAttributes
));
377 attribs
.flags
= GSWindowStyleAttr
|GSWindowLevelAttr
|GSExtraFlagsAttr
;
378 attribs
.window_style
= win
->flags
.style
;
379 attribs
.window_level
= win
->level
;
380 if (win
->flags
.documentEdited
)
381 attribs
.extra_flags
= GSDocumentEditedFlag
;
383 attribs
.extra_flags
= 0;
385 writeGNUstepWMAttr(scr
, win
->view
->window
, &attribs
);
390 realizeWindow(WMWindow
*win
)
393 XClassHint
*classHint
;
394 WMScreen
*scr
= win
->view
->screen
;
398 classHint
= XAllocClassHint();
399 classHint
->res_name
= win
->wname
;
400 classHint
->res_class
= WMGetApplicationName();
401 XSetClassHint(scr
->display
, win
->view
->window
, classHint
);
404 hints
= XAllocWMHints();
406 if (!scr
->aflags
.simpleApplication
) {
407 hints
->flags
|= WindowGroupHint
;
408 hints
->window_group
= scr
->groupLeader
;
410 if (win
->miniImage
) {
411 hints
->flags
|= IconPixmapHint
;
412 hints
->icon_pixmap
= WMGetPixmapXID(win
->miniImage
);
413 hints
->icon_mask
= WMGetPixmapMaskXID(win
->miniImage
);
414 if (hints
->icon_mask
!= None
) {
415 hints
->flags
|= IconMaskHint
;
418 if (hints
->flags
!= 0)
419 XSetWMHints(scr
->display
, win
->view
->window
, hints
);
423 if (win
->closeAction
) {
424 atoms
[count
++] = scr
->deleteWindowAtom
;
428 XSetWMProtocols(scr
->display
, win
->view
->window
, atoms
, count
);
430 if (win
->title
|| win
->miniTitle
)
431 XmbSetWMProperties(scr
->display
, win
->view
->window
, win
->title
,
432 win
->miniTitle
, NULL
, 0, NULL
, NULL
, NULL
);
434 setWindowMakerHints(win
);
439 XSetTransientForHint(scr
->display
, win
->view
->window
,
440 win
->owner
->view
->window
);
447 WMSetWindowUPosition(WMWindow
*win
, int x
, int y
)
449 win
->flags
.upos_set
= 1;
452 if (win
->view
->flags
.realized
)
459 WMSetWindowUSize(WMWindow
*win
, unsigned width
, unsigned height
)
461 win
->usize
.width
= width
;
462 win
->usize
.height
= height
;
463 if (win
->view
->flags
.realized
)
469 WMSetWindowMinSize(WMWindow
*win
, unsigned width
, unsigned height
)
471 win
->minSize
.width
= width
;
472 win
->minSize
.height
= height
;
473 if (win
->view
->flags
.realized
)
480 WMSetWindowMaxSize(WMWindow
*win
, unsigned width
, unsigned height
)
482 win
->maxSize
.width
= width
;
483 win
->maxSize
.height
= height
;
484 if (win
->view
->flags
.realized
)
490 WMSetWindowBaseSize(WMWindow
*win
, unsigned width
, unsigned height
)
492 /* TODO: validate sizes */
493 win
->baseSize
.width
= width
;
494 win
->baseSize
.height
= height
;
495 if (win
->view
->flags
.realized
)
501 WMSetWindowResizeIncrements(WMWindow
*win
, unsigned wIncr
, unsigned hIncr
)
503 win
->resizeIncrement
.width
= wIncr
;
504 win
->resizeIncrement
.height
= hIncr
;
505 if (win
->view
->flags
.realized
)
511 WMSetWindowLevel(WMWindow
*win
, int level
)
514 if (win
->view
->flags
.realized
)
515 setWindowMakerHints(win
);
520 WMSetWindowDocumentEdited(WMWindow
*win
, Bool flag
)
522 if (win
->flags
.documentEdited
!= flag
) {
523 win
->flags
.documentEdited
= flag
;
524 if (win
->view
->flags
.realized
)
525 setWindowMakerHints(win
);
531 WMSetWindowMiniwindowImage(WMWindow
*win
, WMPixmap
*pixmap
)
533 if ((win
->miniImage
&& !pixmap
) || (!win
->miniImage
&& pixmap
)) {
535 WMReleasePixmap(win
->miniImage
);
538 win
->miniImage
= WMRetainPixmap(pixmap
);
540 win
->miniImage
= NULL
;
542 if (win
->view
->flags
.realized
) {
545 hints
= XGetWMHints(win
->view
->screen
->display
, win
->view
->window
);
547 hints
= XAllocWMHints();
549 wwarning("could not allocate memory for WM hints");
555 hints
->flags
|= IconPixmapHint
;
556 hints
->icon_pixmap
= WMGetPixmapXID(pixmap
);
557 hints
->icon_mask
= WMGetPixmapMaskXID(pixmap
);
558 if (hints
->icon_mask
!= None
) {
559 hints
->flags
|= IconMaskHint
;
562 XSetWMHints(win
->view
->screen
->display
, win
->view
->window
, hints
);
570 WMSetWindowMiniwindowTitle(WMWindow
*win
, char *title
)
572 XTextProperty property
;
575 if ((win
->miniTitle
&& !title
) || (!win
->miniTitle
&& title
)
576 || (title
&& win
->miniTitle
&& strcoll(title
, win
->miniTitle
)!=0)) {
578 free(win
->miniTitle
);
581 win
->miniTitle
= wstrdup(title
);
583 win
->miniTitle
= NULL
;
585 if (win
->view
->flags
.realized
) {
586 result
= XmbTextListToTextProperty (win
->view
->screen
->display
,
587 &title
, 1, XStdICCTextStyle
,
589 if (result
== XNoMemory
|| result
== XLocaleNotSupported
) {
590 wwarning("icon title conversion error..using STRING encoding");
591 XSetIconName(win
->view
->screen
->display
, win
->view
->window
,
594 XSetWMIconName(win
->view
->screen
->display
, win
->view
->window
,
597 XFree(property
.value
);
605 WMCloseWindow(WMWindow
*win
)
608 /* withdraw the window */
609 if (win
->view
->flags
.realized
)
610 XWithdrawWindow(win
->view
->screen
->display
, win
->view
->window
,
611 win
->view
->screen
->screen
);
616 handleEvents(XEvent
*event
, void *clientData
)
618 _Window
*win
= (_Window
*)clientData
;
619 W_View
*view
= win
->view
;
622 switch (event
->type
) {
624 if (event
->xclient
.message_type
== win
->view
->screen
->protocolsAtom
625 && event
->xclient
.format
== 32
626 && event
->xclient
.data
.l
[0]==win
->view
->screen
->deleteWindowAtom
) {
628 if (win
->closeAction
) {
629 (*win
->closeAction
)(win
, win
->closeData
);
642 case ConfigureNotify
:
643 if (event
->xconfigure
.width
!= view
->size
.width
644 || event
->xconfigure
.height
!= view
->size
.height
) {
646 view
->size
.width
= event
->xconfigure
.width
;
647 view
->size
.height
= event
->xconfigure
.height
;
649 if (view
->flags
.notifySizeChanged
) {
650 WMPostNotificationName(WMViewSizeDidChangeNotification
,
654 if (event
->xconfigure
.x
!= view
->pos
.x
655 || event
->xconfigure
.y
!= view
->pos
.y
) {
657 if (event
->xconfigure
.send_event
) {
658 view
->pos
.x
= event
->xconfigure
.x
;
659 view
->pos
.y
= event
->xconfigure
.y
;
663 XTranslateCoordinates(view
->screen
->display
,
664 view
->window
, view
->screen
->rootWin
,
665 event
->xconfigure
.x
, event
->xconfigure
.y
,
666 &view
->pos
.x
, &view
->pos
.y
, &foo
);
677 destroyWindow(_Window
*win
)
679 WMScreen
*scr
= win
->view
->screen
;
681 WMRemoveNotificationObserver(win
);
683 if (scr
->windowList
== win
) {
684 scr
->windowList
= scr
->windowList
->nextPtr
;
687 ptr
= scr
->windowList
;
689 while (ptr
->nextPtr
) {
690 if (ptr
->nextPtr
==win
) {
691 ptr
->nextPtr
= ptr
->nextPtr
->nextPtr
;
702 if (win
->miniTitle
) {
703 free(win
->miniTitle
);
706 if (win
->miniImage
) {
707 WMReleasePixmap(win
->miniImage
);