added some netwm support in WINGs
[wmaker-crm.git] / WINGs / wwindow.c
blob74de385e95b898c856a919f17a0a7fdbd2439f4e
2 #include <X11/Xmd.h>
4 #include "WINGsP.h"
6 #include <X11/Xatom.h>
9 typedef struct W_Window {
10 W_Class widgetClass;
11 W_View *view;
13 struct W_Window *nextPtr; /* next in the window list */
15 struct W_Window *owner;
17 char *title;
19 WMPixmap *miniImage; /* miniwindow */
20 char *miniTitle;
22 char *wname;
24 WMSize resizeIncrement;
25 WMSize baseSize;
26 WMSize minSize;
27 WMSize maxSize;
28 WMPoint minAspect;
29 WMPoint maxAspect;
31 WMPoint upos;
32 WMPoint ppos;
34 WMAction *closeAction;
35 void *closeData;
37 int level;
39 struct {
40 unsigned style:4;
41 unsigned configured:1;
42 unsigned documentEdited:1;
44 unsigned setUPos:1;
45 unsigned setPPos:1;
46 unsigned setAspect:1;
47 } flags;
48 } _Window;
52 typedef struct {
53 CARD32 flags;
54 CARD32 window_style;
55 CARD32 window_level;
56 CARD32 reserved;
57 Pixmap miniaturize_pixmap; /* pixmap for miniaturize button */
58 Pixmap close_pixmap; /* pixmap for close button */
59 Pixmap miniaturize_mask; /* miniaturize pixmap mask */
60 Pixmap close_mask; /* close pixmap mask */
61 CARD32 extra_flags;
62 } GNUstepWMAttributes;
64 #define GSWindowStyleAttr (1<<0)
65 #define GSWindowLevelAttr (1<<1)
66 #define GSMiniaturizePixmapAttr (1<<3)
67 #define GSClosePixmapAttr (1<<4)
68 #define GSMiniaturizeMaskAttr (1<<5)
69 #define GSCloseMaskAttr (1<<6)
70 #define GSExtraFlagsAttr (1<<7)
72 /* extra flags */
73 #define GSDocumentEditedFlag (1<<0)
74 #define GSNoApplicationIconFlag (1<<5)
76 #define WMFHideOtherApplications 10
77 #define WMFHideApplication 12
81 static void willResizeWindow(W_ViewDelegate *, WMView *, unsigned*, unsigned*);
83 struct W_ViewDelegate _WindowViewDelegate = {
84 NULL,
85 NULL,
86 NULL,
87 NULL,
88 willResizeWindow
92 #define DEFAULT_WIDTH 400
93 #define DEFAULT_HEIGHT 180
94 #define DEFAULT_TITLE ""
97 static void destroyWindow(_Window *win);
99 static void handleEvents();
101 static void realizeWindow();
103 static void
104 realizeObserver(void *self, WMNotification *not)
106 realizeWindow(self);
110 WMWindow*
111 WMCreatePanelWithStyleForWindow(WMWindow *owner, char *name, int style)
113 WMWindow *win;
115 win = WMCreateWindowWithStyle(owner->view->screen, name, style);
116 win->owner = owner;
118 return win;
123 WMWindow*
124 WMCreatePanelForWindow(WMWindow *owner, char *name)
126 return WMCreatePanelWithStyleForWindow(owner, name,
127 WMTitledWindowMask
128 |WMClosableWindowMask
129 |WMResizableWindowMask);
133 void
134 WMChangePanelOwner(WMWindow *win, WMWindow *newOwner)
136 win->owner = newOwner;
138 if (win->view->flags.realized && newOwner) {
139 XSetTransientForHint(win->view->screen->display, win->view->window,
140 newOwner->view->window);
146 WMWindow*
147 WMCreateWindow(WMScreen *screen, char *name)
149 return WMCreateWindowWithStyle(screen, name, WMTitledWindowMask
150 |WMClosableWindowMask
151 |WMMiniaturizableWindowMask
152 |WMResizableWindowMask);
157 WMWindow*
158 WMCreateWindowWithStyle(WMScreen *screen, char *name, int style)
160 _Window *win;
162 win = wmalloc(sizeof(_Window));
163 memset(win, 0, sizeof(_Window));
165 win->widgetClass = WC_Window;
167 win->view = W_CreateTopView(screen);
168 if (!win->view) {
169 wfree(win);
170 return NULL;
172 win->view->self = win;
174 win->view->delegate = &_WindowViewDelegate;
176 win->wname = wstrdup(name);
178 /* add to the window list of the screen (application) */
179 win->nextPtr = screen->windowList;
180 screen->windowList = win;
182 WMCreateEventHandler(win->view, ExposureMask|StructureNotifyMask
183 |ClientMessageMask|FocusChangeMask,
184 handleEvents, win);
186 W_ResizeView(win->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
188 WMAddNotificationObserver(realizeObserver, win,
189 WMViewRealizedNotification, win->view);
191 win->flags.style = style;
193 win->level = WMNormalWindowLevel;
195 /* kluge. Find a better solution */
196 W_SetFocusOfTopLevel(win->view, win->view);
198 return win;
203 static void
204 setWindowTitle(WMWindow *win, const char *title)
206 WMScreen *scr= win->view->screen;
207 XTextProperty property;
208 int result;
210 result = XmbTextListToTextProperty(scr->display,
211 (char**)&title, 1, XStdICCTextStyle,
212 &property);
213 if (result == XNoMemory || result == XLocaleNotSupported) {
214 wwarning("window title conversion error... using STRING encoding");
215 XStoreName(scr->display, win->view->window, title);
216 } else {
217 XSetWMName(scr->display, win->view->window, &property);
218 if (property.value)
219 XFree(property.value);
222 XChangeProperty(scr->display, win->view->window,
223 scr->netwmName, scr->utf8String, 8,
224 PropModeReplace, (unsigned char *)title, strlen(title));
228 static void
229 setMiniwindowTitle(WMWindow *win, const char *title)
231 WMScreen *scr= win->view->screen;
232 XTextProperty property;
233 int result;
235 result = XmbTextListToTextProperty(scr->display,
236 (char**)&title, 1, XStdICCTextStyle,
237 &property);
238 if (result == XNoMemory || result == XLocaleNotSupported) {
239 wwarning("icon title conversion error..using STRING encoding");
240 XSetIconName(scr->display, win->view->window, title);
241 } else {
242 XSetWMIconName(scr->display, win->view->window, &property);
243 if (property.value)
244 XFree(property.value);
247 XChangeProperty(scr->display, win->view->window,
248 scr->netwmIconName, scr->utf8String, 8,
249 PropModeReplace, (unsigned char *)title, strlen(title));
253 static void
254 setMiniwindow(WMWindow *win, RImage *image)
256 WMScreen *scr= win->view->screen;
257 CARD32 *data;
258 int x, y;
259 int o;
261 if (!image)
262 return;
264 data= malloc((image->width * image->height + 2) * sizeof(CARD32));
265 if (!data)
266 return;
268 o= 0;
269 data[o++] = image->width;
270 data[o++] = image->height;
272 for (y= 0; y < image->height; y++) {
273 for (x= 0; x < image->width; x++) {
274 CARD32 pixel;
275 int offs= (x+y*image->width);
277 if (image->format == RRGBFormat)
278 pixel= image->data[offs*3]<<16 | image->data[offs*3+1]<<8 | image->data[offs*3+2];
279 else
280 pixel= image->data[offs*4]<<16 | image->data[offs*4+1]<<8 | image->data[offs*4+2] | image->data[offs*4+3] << 24;
282 data[o++]= pixel;
286 XChangeProperty(scr->display, win->view->window,
287 scr->netwmIcon, XA_CARDINAL, 32,
288 PropModeReplace,
289 (unsigned char *)data,
290 (image->width * image->height + 2) * sizeof(CARD32));
294 void
295 WMSetWindowTitle(WMWindow *win, char *title)
297 if (win->title!=NULL)
298 wfree(win->title);
299 if (title!=NULL)
300 win->title = wstrdup(title);
301 else
302 win->title = NULL;
304 if (win->view->flags.realized) {
305 setWindowTitle(win, title);
312 void
313 WMSetWindowCloseAction(WMWindow *win, WMAction *action, void *clientData)
315 Atom *atoms = NULL;
316 Atom *newAtoms;
317 int count;
318 WMScreen *scr = win->view->screen;
320 if (win->view->flags.realized) {
321 if (action && !win->closeAction) {
322 if (!XGetWMProtocols(scr->display, win->view->window, &atoms,
323 &count)) {
324 count = 0;
326 newAtoms = wmalloc((count+1)*sizeof(Atom));
327 if (count > 0)
328 memcpy(newAtoms, atoms, count*sizeof(Atom));
329 newAtoms[count++] = scr->deleteWindowAtom;
330 XSetWMProtocols(scr->display, win->view->window, newAtoms, count);
331 if (atoms)
332 XFree(atoms);
333 wfree(newAtoms);
334 } else if (!action && win->closeAction) {
335 int i, ncount;
337 if (XGetWMProtocols(scr->display, win->view->window, &atoms,
338 &count) && count>0) {
339 newAtoms = wmalloc((count-1)*sizeof(Atom));
340 ncount = 0;
341 for (i=0; i < count; i++) {
342 if (atoms[i]!=scr->deleteWindowAtom) {
343 newAtoms[i] = atoms[i];
344 ncount++;
347 XSetWMProtocols(scr->display, win->view->window, newAtoms,
348 ncount);
349 if (atoms)
350 XFree(atoms);
351 wfree(newAtoms);
355 win->closeAction = action;
356 win->closeData = clientData;
361 static void
362 willResizeWindow(W_ViewDelegate *self, WMView *view,
363 unsigned *width, unsigned *height)
365 WMWindow *win = (WMWindow*)view->self;
367 if (win->minSize.width > 0 && win->minSize.height > 0) {
368 if (*width < win->minSize.width)
369 *width = win->minSize.width;
370 if (*height < win->minSize.height)
371 *height = win->minSize.height;
374 if (win->maxSize.width > 0 && win->maxSize.height > 0) {
375 if (*width > win->maxSize.width)
376 *width = win->maxSize.width;
377 if (*height > win->maxSize.height)
378 *height = win->maxSize.height;
383 static void
384 setSizeHints(WMWindow *win)
386 XSizeHints *hints;
388 hints = XAllocSizeHints();
389 if (!hints) {
390 wwarning("could not allocate memory for window size hints");
391 return;
394 hints->flags = 0;
396 if (win->flags.setPPos) {
397 hints->flags |= PPosition;
398 hints->x = win->ppos.x;
399 hints->y = win->ppos.y;
401 if (win->flags.setUPos) {
402 hints->flags |= USPosition;
403 hints->x = win->upos.x;
404 hints->y = win->upos.y;
406 if (win->minSize.width>0 && win->minSize.height>0) {
407 hints->flags |= PMinSize;
408 hints->min_width = win->minSize.width;
409 hints->min_height = win->minSize.height;
411 if (win->maxSize.width>0 && win->maxSize.height>0) {
412 hints->flags |= PMaxSize;
413 hints->max_width = win->maxSize.width;
414 hints->max_height = win->maxSize.height;
416 if (win->baseSize.width>0 && win->baseSize.height>0) {
417 hints->flags |= PBaseSize;
418 hints->base_width = win->baseSize.width;
419 hints->base_height = win->baseSize.height;
421 if (win->resizeIncrement.width>0 && win->resizeIncrement.height>0) {
422 hints->flags |= PResizeInc;
423 hints->width_inc = win->resizeIncrement.width;
424 hints->height_inc = win->resizeIncrement.height;
426 if (win->flags.setAspect) {
427 hints->flags |= PAspect;
428 hints->min_aspect.x = win->minAspect.x;
429 hints->min_aspect.y = win->minAspect.y;
430 hints->max_aspect.x = win->maxAspect.x;
431 hints->max_aspect.y = win->maxAspect.y;
435 if (hints->flags) {
436 XSetWMNormalHints(win->view->screen->display, win->view->window, hints);
438 XFree(hints);
443 static void
444 writeGNUstepWMAttr(WMScreen *scr, Window window, GNUstepWMAttributes *attr)
446 unsigned long data[9];
448 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
449 data[0] = attr->flags;
450 data[1] = attr->window_style;
451 data[2] = attr->window_level;
452 data[3] = 0; /* reserved */
453 /* The X protocol says XIDs are 32bit */
454 data[4] = attr->miniaturize_pixmap;
455 data[5] = attr->close_pixmap;
456 data[6] = attr->miniaturize_mask;
457 data[7] = attr->close_mask;
458 data[8] = attr->extra_flags;
459 XChangeProperty(scr->display, window, scr->attribsAtom, scr->attribsAtom,
460 32, PropModeReplace, (unsigned char *)data, 9);
464 static void
465 setWindowMakerHints(WMWindow *win)
467 GNUstepWMAttributes attribs;
468 WMScreen *scr = WMWidgetScreen(win);
470 memset(&attribs, 0, sizeof(GNUstepWMAttributes));
471 attribs.flags = GSWindowStyleAttr|GSWindowLevelAttr|GSExtraFlagsAttr;
472 attribs.window_style = win->flags.style;
473 attribs.window_level = win->level;
474 if (win->flags.documentEdited)
475 attribs.extra_flags = GSDocumentEditedFlag;
476 else
477 attribs.extra_flags = 0;
479 writeGNUstepWMAttr(scr, win->view->window, &attribs);
483 static void
484 realizeWindow(WMWindow *win)
486 XWMHints *hints;
487 XClassHint *classHint;
488 WMScreen *scr = win->view->screen;
489 Atom atoms[4];
490 int count;
492 classHint = XAllocClassHint();
493 classHint->res_name = win->wname;
494 classHint->res_class = WMGetApplicationName();
495 XSetClassHint(scr->display, win->view->window, classHint);
496 XFree(classHint);
498 hints = XAllocWMHints();
499 hints->flags = 0;
500 if (!scr->aflags.simpleApplication) {
501 hints->flags |= WindowGroupHint;
502 hints->window_group = scr->groupLeader;
504 if (win->miniImage) {
505 hints->flags |= IconPixmapHint;
506 hints->icon_pixmap = WMGetPixmapXID(win->miniImage);
507 hints->icon_mask = WMGetPixmapMaskXID(win->miniImage);
508 if (hints->icon_mask != None) {
509 hints->flags |= IconMaskHint;
512 if (hints->flags != 0)
513 XSetWMHints(scr->display, win->view->window, hints);
514 XFree(hints);
516 count = 0;
517 if (win->closeAction) {
518 atoms[count++] = scr->deleteWindowAtom;
521 if (count>0)
522 XSetWMProtocols(scr->display, win->view->window, atoms, count);
524 if (win->title || win->miniTitle)
525 XmbSetWMProperties(scr->display, win->view->window, win->title,
526 win->miniTitle, NULL, 0, NULL, NULL, NULL);
528 setWindowMakerHints(win);
530 setSizeHints(win);
532 if (win->owner) {
533 XSetTransientForHint(scr->display, win->view->window,
534 win->owner->view->window);
537 if (win->title)
538 setWindowTitle(win, win->title);
543 void
544 WMSetWindowAspectRatio(WMWindow *win, int minX, int minY,
545 int maxX, int maxY)
547 win->flags.setAspect = 1;
548 win->minAspect.x = minX;
549 win->minAspect.y = minY;
550 win->maxAspect.x = maxX;
551 win->maxAspect.y = maxY;
552 if (win->view->flags.realized)
553 setSizeHints(win);
558 void
559 WMSetWindowInitialPosition(WMWindow *win, int x, int y)
561 win->flags.setPPos = 1;
562 win->ppos.x = x;
563 win->ppos.y = y;
564 if (win->view->flags.realized)
565 setSizeHints(win);
566 WMMoveWidget(win, x, y);
571 void
572 WMSetWindowUserPosition(WMWindow *win, int x, int y)
574 win->flags.setUPos = 1;
575 win->upos.x = x;
576 win->upos.y = y;
577 if (win->view->flags.realized)
578 setSizeHints(win);
579 WMMoveWidget(win, x, y);
585 void
586 WMSetWindowMinSize(WMWindow *win, unsigned width, unsigned height)
588 win->minSize.width = width;
589 win->minSize.height = height;
590 if (win->view->flags.realized)
591 setSizeHints(win);
596 void
597 WMSetWindowMaxSize(WMWindow *win, unsigned width, unsigned height)
599 win->maxSize.width = width;
600 win->maxSize.height = height;
601 if (win->view->flags.realized)
602 setSizeHints(win);
606 void
607 WMSetWindowBaseSize(WMWindow *win, unsigned width, unsigned height)
609 /* TODO: validate sizes */
610 win->baseSize.width = width;
611 win->baseSize.height = height;
612 if (win->view->flags.realized)
613 setSizeHints(win);
617 void
618 WMSetWindowResizeIncrements(WMWindow *win, unsigned wIncr, unsigned hIncr)
620 win->resizeIncrement.width = wIncr;
621 win->resizeIncrement.height = hIncr;
622 if (win->view->flags.realized)
623 setSizeHints(win);
627 void
628 WMSetWindowLevel(WMWindow *win, int level)
630 win->level = level;
631 if (win->view->flags.realized)
632 setWindowMakerHints(win);
636 void
637 WMSetWindowDocumentEdited(WMWindow *win, Bool flag)
639 flag = ((flag==0) ? 0 : 1);
640 if (win->flags.documentEdited != flag) {
641 win->flags.documentEdited = flag;
642 if (win->view->flags.realized)
643 setWindowMakerHints(win);
648 void
649 WMSetWindowMiniwindowImage(WMWindow *win, RImage *image)
651 if (win->view->flags.realized)
652 setMiniwindow(win, image);
656 void
657 WMSetWindowMiniwindowPixmap(WMWindow *win, WMPixmap *pixmap)
659 if ((win->miniImage && !pixmap) || (!win->miniImage && pixmap)) {
660 if (win->miniImage)
661 WMReleasePixmap(win->miniImage);
663 if (pixmap)
664 win->miniImage = WMRetainPixmap(pixmap);
665 else
666 win->miniImage = NULL;
668 if (win->view->flags.realized) {
669 XWMHints *hints;
671 hints = XGetWMHints(win->view->screen->display, win->view->window);
672 if (!hints) {
673 hints = XAllocWMHints();
674 if (!hints) {
675 wwarning("could not allocate memory for WM hints");
676 return;
678 hints->flags = 0;
680 if (pixmap) {
681 hints->flags |= IconPixmapHint;
682 hints->icon_pixmap = WMGetPixmapXID(pixmap);
683 hints->icon_mask = WMGetPixmapMaskXID(pixmap);
684 if (hints->icon_mask != None) {
685 hints->flags |= IconMaskHint;
688 XSetWMHints(win->view->screen->display, win->view->window, hints);
689 XFree(hints);
695 void
696 WMSetWindowMiniwindowTitle(WMWindow *win, char *title)
698 if ((win->miniTitle && !title) || (!win->miniTitle && title)
699 || (title && win->miniTitle && strcoll(title, win->miniTitle)!=0)) {
700 if (win->miniTitle)
701 wfree(win->miniTitle);
703 if (title)
704 win->miniTitle = wstrdup(title);
705 else
706 win->miniTitle = NULL;
708 if (win->view->flags.realized) {
709 setMiniwindowTitle(win, title);
715 void
716 WMCloseWindow(WMWindow *win)
718 WMUnmapWidget(win);
719 /* withdraw the window */
720 if (win->view->flags.realized)
721 XWithdrawWindow(win->view->screen->display, win->view->window,
722 win->view->screen->screen);
726 static void
727 handleEvents(XEvent *event, void *clientData)
729 _Window *win = (_Window*)clientData;
730 W_View *view = win->view;
733 switch (event->type) {
734 case ClientMessage:
735 if (event->xclient.message_type == win->view->screen->protocolsAtom
736 && event->xclient.format == 32
737 && event->xclient.data.l[0]==win->view->screen->deleteWindowAtom) {
739 if (win->closeAction) {
740 (*win->closeAction)(win, win->closeData);
743 break;
745 * was causing windows to ignore commands like closeWindow
746 * after the windows is iconized/restored or a workspace change
747 * if this is really needed, put the MapNotify portion too and
748 * fix the restack bug in wmaker
749 case UnmapNotify:
750 WMUnmapWidget(win);
751 break;
753 case MapNotify:
754 WMMapWidget(win);
755 break;
758 case DestroyNotify:
759 destroyWindow(win);
760 break;
762 case ConfigureNotify:
763 if (event->xconfigure.width != view->size.width
764 || event->xconfigure.height != view->size.height) {
766 view->size.width = event->xconfigure.width;
767 view->size.height = event->xconfigure.height;
769 if (view->flags.notifySizeChanged) {
770 WMPostNotificationName(WMViewSizeDidChangeNotification,
771 view, NULL);
774 if (event->xconfigure.x != view->pos.x
775 || event->xconfigure.y != view->pos.y) {
777 if (event->xconfigure.send_event) {
778 view->pos.x = event->xconfigure.x;
779 view->pos.y = event->xconfigure.y;
780 } else {
781 Window foo;
783 XTranslateCoordinates(view->screen->display,
784 view->window, view->screen->rootWin,
785 event->xconfigure.x, event->xconfigure.y,
786 &view->pos.x, &view->pos.y, &foo);
789 break;
796 static void
797 destroyWindow(_Window *win)
799 WMScreen *scr = win->view->screen;
801 WMRemoveNotificationObserver(win);
803 if (scr->windowList == win) {
804 scr->windowList = scr->windowList->nextPtr;
805 } else {
806 WMWindow *ptr;
807 ptr = scr->windowList;
809 if (ptr) {
810 while (ptr->nextPtr) {
811 if (ptr->nextPtr==win) {
812 ptr->nextPtr = ptr->nextPtr->nextPtr;
813 break;
815 ptr = ptr->nextPtr;
820 if (win->title) {
821 wfree(win->title);
824 if (win->miniTitle) {
825 wfree(win->miniTitle);
828 if (win->miniImage) {
829 WMReleasePixmap(win->miniImage);
832 if (win->wname)
833 wfree(win->wname);
835 wfree(win);