free
[wmaker-crm.git] / WINGs / wwindow.c
blob0eecf416ecf326f6f5753083d6093519fcdeb444
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));
292 free(data);
296 void
297 WMSetWindowTitle(WMWindow *win, char *title)
299 if (win->title!=NULL)
300 wfree(win->title);
301 if (title!=NULL)
302 win->title = wstrdup(title);
303 else
304 win->title = NULL;
306 if (win->view->flags.realized) {
307 setWindowTitle(win, title);
314 void
315 WMSetWindowCloseAction(WMWindow *win, WMAction *action, void *clientData)
317 Atom *atoms = NULL;
318 Atom *newAtoms;
319 int count;
320 WMScreen *scr = win->view->screen;
322 if (win->view->flags.realized) {
323 if (action && !win->closeAction) {
324 if (!XGetWMProtocols(scr->display, win->view->window, &atoms,
325 &count)) {
326 count = 0;
328 newAtoms = wmalloc((count+1)*sizeof(Atom));
329 if (count > 0)
330 memcpy(newAtoms, atoms, count*sizeof(Atom));
331 newAtoms[count++] = scr->deleteWindowAtom;
332 XSetWMProtocols(scr->display, win->view->window, newAtoms, count);
333 if (atoms)
334 XFree(atoms);
335 wfree(newAtoms);
336 } else if (!action && win->closeAction) {
337 int i, ncount;
339 if (XGetWMProtocols(scr->display, win->view->window, &atoms,
340 &count) && count>0) {
341 newAtoms = wmalloc((count-1)*sizeof(Atom));
342 ncount = 0;
343 for (i=0; i < count; i++) {
344 if (atoms[i]!=scr->deleteWindowAtom) {
345 newAtoms[i] = atoms[i];
346 ncount++;
349 XSetWMProtocols(scr->display, win->view->window, newAtoms,
350 ncount);
351 if (atoms)
352 XFree(atoms);
353 wfree(newAtoms);
357 win->closeAction = action;
358 win->closeData = clientData;
363 static void
364 willResizeWindow(W_ViewDelegate *self, WMView *view,
365 unsigned *width, unsigned *height)
367 WMWindow *win = (WMWindow*)view->self;
369 if (win->minSize.width > 0 && win->minSize.height > 0) {
370 if (*width < win->minSize.width)
371 *width = win->minSize.width;
372 if (*height < win->minSize.height)
373 *height = win->minSize.height;
376 if (win->maxSize.width > 0 && win->maxSize.height > 0) {
377 if (*width > win->maxSize.width)
378 *width = win->maxSize.width;
379 if (*height > win->maxSize.height)
380 *height = win->maxSize.height;
385 static void
386 setSizeHints(WMWindow *win)
388 XSizeHints *hints;
390 hints = XAllocSizeHints();
391 if (!hints) {
392 wwarning("could not allocate memory for window size hints");
393 return;
396 hints->flags = 0;
398 if (win->flags.setPPos) {
399 hints->flags |= PPosition;
400 hints->x = win->ppos.x;
401 hints->y = win->ppos.y;
403 if (win->flags.setUPos) {
404 hints->flags |= USPosition;
405 hints->x = win->upos.x;
406 hints->y = win->upos.y;
408 if (win->minSize.width>0 && win->minSize.height>0) {
409 hints->flags |= PMinSize;
410 hints->min_width = win->minSize.width;
411 hints->min_height = win->minSize.height;
413 if (win->maxSize.width>0 && win->maxSize.height>0) {
414 hints->flags |= PMaxSize;
415 hints->max_width = win->maxSize.width;
416 hints->max_height = win->maxSize.height;
418 if (win->baseSize.width>0 && win->baseSize.height>0) {
419 hints->flags |= PBaseSize;
420 hints->base_width = win->baseSize.width;
421 hints->base_height = win->baseSize.height;
423 if (win->resizeIncrement.width>0 && win->resizeIncrement.height>0) {
424 hints->flags |= PResizeInc;
425 hints->width_inc = win->resizeIncrement.width;
426 hints->height_inc = win->resizeIncrement.height;
428 if (win->flags.setAspect) {
429 hints->flags |= PAspect;
430 hints->min_aspect.x = win->minAspect.x;
431 hints->min_aspect.y = win->minAspect.y;
432 hints->max_aspect.x = win->maxAspect.x;
433 hints->max_aspect.y = win->maxAspect.y;
437 if (hints->flags) {
438 XSetWMNormalHints(win->view->screen->display, win->view->window, hints);
440 XFree(hints);
445 static void
446 writeGNUstepWMAttr(WMScreen *scr, Window window, GNUstepWMAttributes *attr)
448 unsigned long data[9];
450 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
451 data[0] = attr->flags;
452 data[1] = attr->window_style;
453 data[2] = attr->window_level;
454 data[3] = 0; /* reserved */
455 /* The X protocol says XIDs are 32bit */
456 data[4] = attr->miniaturize_pixmap;
457 data[5] = attr->close_pixmap;
458 data[6] = attr->miniaturize_mask;
459 data[7] = attr->close_mask;
460 data[8] = attr->extra_flags;
461 XChangeProperty(scr->display, window, scr->attribsAtom, scr->attribsAtom,
462 32, PropModeReplace, (unsigned char *)data, 9);
466 static void
467 setWindowMakerHints(WMWindow *win)
469 GNUstepWMAttributes attribs;
470 WMScreen *scr = WMWidgetScreen(win);
472 memset(&attribs, 0, sizeof(GNUstepWMAttributes));
473 attribs.flags = GSWindowStyleAttr|GSWindowLevelAttr|GSExtraFlagsAttr;
474 attribs.window_style = win->flags.style;
475 attribs.window_level = win->level;
476 if (win->flags.documentEdited)
477 attribs.extra_flags = GSDocumentEditedFlag;
478 else
479 attribs.extra_flags = 0;
481 writeGNUstepWMAttr(scr, win->view->window, &attribs);
485 static void
486 realizeWindow(WMWindow *win)
488 XWMHints *hints;
489 XClassHint *classHint;
490 WMScreen *scr = win->view->screen;
491 Atom atoms[4];
492 int count;
494 classHint = XAllocClassHint();
495 classHint->res_name = win->wname;
496 classHint->res_class = WMGetApplicationName();
497 XSetClassHint(scr->display, win->view->window, classHint);
498 XFree(classHint);
500 hints = XAllocWMHints();
501 hints->flags = 0;
502 if (!scr->aflags.simpleApplication) {
503 hints->flags |= WindowGroupHint;
504 hints->window_group = scr->groupLeader;
506 if (win->miniImage) {
507 hints->flags |= IconPixmapHint;
508 hints->icon_pixmap = WMGetPixmapXID(win->miniImage);
509 hints->icon_mask = WMGetPixmapMaskXID(win->miniImage);
510 if (hints->icon_mask != None) {
511 hints->flags |= IconMaskHint;
514 if (hints->flags != 0)
515 XSetWMHints(scr->display, win->view->window, hints);
516 XFree(hints);
518 count = 0;
519 if (win->closeAction) {
520 atoms[count++] = scr->deleteWindowAtom;
523 if (count>0)
524 XSetWMProtocols(scr->display, win->view->window, atoms, count);
526 if (win->title || win->miniTitle)
527 XmbSetWMProperties(scr->display, win->view->window, win->title,
528 win->miniTitle, NULL, 0, NULL, NULL, NULL);
530 setWindowMakerHints(win);
532 setSizeHints(win);
534 if (win->owner) {
535 XSetTransientForHint(scr->display, win->view->window,
536 win->owner->view->window);
539 if (win->title)
540 setWindowTitle(win, win->title);
545 void
546 WMSetWindowAspectRatio(WMWindow *win, int minX, int minY,
547 int maxX, int maxY)
549 win->flags.setAspect = 1;
550 win->minAspect.x = minX;
551 win->minAspect.y = minY;
552 win->maxAspect.x = maxX;
553 win->maxAspect.y = maxY;
554 if (win->view->flags.realized)
555 setSizeHints(win);
560 void
561 WMSetWindowInitialPosition(WMWindow *win, int x, int y)
563 win->flags.setPPos = 1;
564 win->ppos.x = x;
565 win->ppos.y = y;
566 if (win->view->flags.realized)
567 setSizeHints(win);
568 WMMoveWidget(win, x, y);
573 void
574 WMSetWindowUserPosition(WMWindow *win, int x, int y)
576 win->flags.setUPos = 1;
577 win->upos.x = x;
578 win->upos.y = y;
579 if (win->view->flags.realized)
580 setSizeHints(win);
581 WMMoveWidget(win, x, y);
587 void
588 WMSetWindowMinSize(WMWindow *win, unsigned width, unsigned height)
590 win->minSize.width = width;
591 win->minSize.height = height;
592 if (win->view->flags.realized)
593 setSizeHints(win);
598 void
599 WMSetWindowMaxSize(WMWindow *win, unsigned width, unsigned height)
601 win->maxSize.width = width;
602 win->maxSize.height = height;
603 if (win->view->flags.realized)
604 setSizeHints(win);
608 void
609 WMSetWindowBaseSize(WMWindow *win, unsigned width, unsigned height)
611 /* TODO: validate sizes */
612 win->baseSize.width = width;
613 win->baseSize.height = height;
614 if (win->view->flags.realized)
615 setSizeHints(win);
619 void
620 WMSetWindowResizeIncrements(WMWindow *win, unsigned wIncr, unsigned hIncr)
622 win->resizeIncrement.width = wIncr;
623 win->resizeIncrement.height = hIncr;
624 if (win->view->flags.realized)
625 setSizeHints(win);
629 void
630 WMSetWindowLevel(WMWindow *win, int level)
632 win->level = level;
633 if (win->view->flags.realized)
634 setWindowMakerHints(win);
638 void
639 WMSetWindowDocumentEdited(WMWindow *win, Bool flag)
641 flag = ((flag==0) ? 0 : 1);
642 if (win->flags.documentEdited != flag) {
643 win->flags.documentEdited = flag;
644 if (win->view->flags.realized)
645 setWindowMakerHints(win);
650 void
651 WMSetWindowMiniwindowImage(WMWindow *win, RImage *image)
653 if (win->view->flags.realized)
654 setMiniwindow(win, image);
658 void
659 WMSetWindowMiniwindowPixmap(WMWindow *win, WMPixmap *pixmap)
661 if ((win->miniImage && !pixmap) || (!win->miniImage && pixmap)) {
662 if (win->miniImage)
663 WMReleasePixmap(win->miniImage);
665 if (pixmap)
666 win->miniImage = WMRetainPixmap(pixmap);
667 else
668 win->miniImage = NULL;
670 if (win->view->flags.realized) {
671 XWMHints *hints;
673 hints = XGetWMHints(win->view->screen->display, win->view->window);
674 if (!hints) {
675 hints = XAllocWMHints();
676 if (!hints) {
677 wwarning("could not allocate memory for WM hints");
678 return;
680 hints->flags = 0;
682 if (pixmap) {
683 hints->flags |= IconPixmapHint;
684 hints->icon_pixmap = WMGetPixmapXID(pixmap);
685 hints->icon_mask = WMGetPixmapMaskXID(pixmap);
686 if (hints->icon_mask != None) {
687 hints->flags |= IconMaskHint;
690 XSetWMHints(win->view->screen->display, win->view->window, hints);
691 XFree(hints);
697 void
698 WMSetWindowMiniwindowTitle(WMWindow *win, char *title)
700 if ((win->miniTitle && !title) || (!win->miniTitle && title)
701 || (title && win->miniTitle && strcoll(title, win->miniTitle)!=0)) {
702 if (win->miniTitle)
703 wfree(win->miniTitle);
705 if (title)
706 win->miniTitle = wstrdup(title);
707 else
708 win->miniTitle = NULL;
710 if (win->view->flags.realized) {
711 setMiniwindowTitle(win, title);
717 void
718 WMCloseWindow(WMWindow *win)
720 WMUnmapWidget(win);
721 /* withdraw the window */
722 if (win->view->flags.realized)
723 XWithdrawWindow(win->view->screen->display, win->view->window,
724 win->view->screen->screen);
728 static void
729 handleEvents(XEvent *event, void *clientData)
731 _Window *win = (_Window*)clientData;
732 W_View *view = win->view;
735 switch (event->type) {
736 case ClientMessage:
737 if (event->xclient.message_type == win->view->screen->protocolsAtom
738 && event->xclient.format == 32
739 && event->xclient.data.l[0]==win->view->screen->deleteWindowAtom) {
741 if (win->closeAction) {
742 (*win->closeAction)(win, win->closeData);
745 break;
747 * was causing windows to ignore commands like closeWindow
748 * after the windows is iconized/restored or a workspace change
749 * if this is really needed, put the MapNotify portion too and
750 * fix the restack bug in wmaker
751 case UnmapNotify:
752 WMUnmapWidget(win);
753 break;
755 case MapNotify:
756 WMMapWidget(win);
757 break;
760 case DestroyNotify:
761 destroyWindow(win);
762 break;
764 case ConfigureNotify:
765 if (event->xconfigure.width != view->size.width
766 || event->xconfigure.height != view->size.height) {
768 view->size.width = event->xconfigure.width;
769 view->size.height = event->xconfigure.height;
771 if (view->flags.notifySizeChanged) {
772 WMPostNotificationName(WMViewSizeDidChangeNotification,
773 view, NULL);
776 if (event->xconfigure.x != view->pos.x
777 || event->xconfigure.y != view->pos.y) {
779 if (event->xconfigure.send_event) {
780 view->pos.x = event->xconfigure.x;
781 view->pos.y = event->xconfigure.y;
782 } else {
783 Window foo;
785 XTranslateCoordinates(view->screen->display,
786 view->window, view->screen->rootWin,
787 event->xconfigure.x, event->xconfigure.y,
788 &view->pos.x, &view->pos.y, &foo);
791 break;
798 static void
799 destroyWindow(_Window *win)
801 WMScreen *scr = win->view->screen;
803 WMRemoveNotificationObserver(win);
805 if (scr->windowList == win) {
806 scr->windowList = scr->windowList->nextPtr;
807 } else {
808 WMWindow *ptr;
809 ptr = scr->windowList;
811 if (ptr) {
812 while (ptr->nextPtr) {
813 if (ptr->nextPtr==win) {
814 ptr->nextPtr = ptr->nextPtr->nextPtr;
815 break;
817 ptr = ptr->nextPtr;
822 if (win->title) {
823 wfree(win->title);
826 if (win->miniTitle) {
827 wfree(win->miniTitle);
830 if (win->miniImage) {
831 WMReleasePixmap(win->miniImage);
834 if (win->wname)
835 wfree(win->wname);
837 wfree(win);