changed panel to use boxes
[wmaker-crm.git] / WINGs / wwindow.c
blob60e130c77da11946dbdcf9b12a0516cc15d61faa
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;
202 void
203 WMSetWindowTitle(WMWindow *win, char *title)
205 XTextProperty property;
206 int result;
208 if (win->title!=NULL)
209 wfree(win->title);
210 if (title!=NULL)
211 win->title = wstrdup(title);
212 else
213 win->title = NULL;
215 if (win->view->flags.realized) {
216 result = XmbTextListToTextProperty (win->view->screen->display,
217 &title, 1, XStdICCTextStyle,
218 &property);
219 if (result == XNoMemory || result == XLocaleNotSupported) {
220 wwarning("window title conversion error... using STRING encoding");
221 XStoreName(win->view->screen->display, win->view->window, title);
222 } else {
223 XSetWMName(win->view->screen->display, win->view->window, &property);
224 if (property.value)
225 XFree(property.value);
233 void
234 WMSetWindowCloseAction(WMWindow *win, WMAction *action, void *clientData)
236 Atom *atoms = NULL;
237 Atom *newAtoms;
238 int count;
239 WMScreen *scr = win->view->screen;
241 if (win->view->flags.realized) {
242 if (action && !win->closeAction) {
243 if (!XGetWMProtocols(scr->display, win->view->window, &atoms,
244 &count)) {
245 count = 0;
247 newAtoms = wmalloc((count+1)*sizeof(Atom));
248 if (count > 0)
249 memcpy(newAtoms, atoms, count*sizeof(Atom));
250 newAtoms[count++] = scr->deleteWindowAtom;
251 XSetWMProtocols(scr->display, win->view->window, newAtoms, count);
252 if (atoms)
253 XFree(atoms);
254 wfree(newAtoms);
255 } else if (!action && win->closeAction) {
256 int i, ncount;
258 if (XGetWMProtocols(scr->display, win->view->window, &atoms,
259 &count) && count>0) {
260 newAtoms = wmalloc((count-1)*sizeof(Atom));
261 ncount = 0;
262 for (i=0; i < count; i++) {
263 if (atoms[i]!=scr->deleteWindowAtom) {
264 newAtoms[i] = atoms[i];
265 ncount++;
268 XSetWMProtocols(scr->display, win->view->window, newAtoms,
269 ncount);
270 if (atoms)
271 XFree(atoms);
272 wfree(newAtoms);
276 win->closeAction = action;
277 win->closeData = clientData;
282 static void
283 willResizeWindow(W_ViewDelegate *self, WMView *view,
284 unsigned *width, unsigned *height)
286 WMWindow *win = (WMWindow*)view->self;
288 if (win->minSize.width > 0 && win->minSize.height > 0) {
289 if (*width < win->minSize.width)
290 *width = win->minSize.width;
291 if (*height < win->minSize.height)
292 *height = win->minSize.height;
295 if (win->maxSize.width > 0 && win->maxSize.height > 0) {
296 if (*width > win->maxSize.width)
297 *width = win->maxSize.width;
298 if (*height > win->maxSize.height)
299 *height = win->maxSize.height;
304 static void
305 setSizeHints(WMWindow *win)
307 XSizeHints *hints;
309 hints = XAllocSizeHints();
310 if (!hints) {
311 wwarning("could not allocate memory for window size hints");
312 return;
315 hints->flags = 0;
317 if (win->flags.setPPos) {
318 hints->flags |= PPosition;
319 hints->x = win->ppos.x;
320 hints->y = win->ppos.y;
322 if (win->flags.setUPos) {
323 hints->flags |= USPosition;
324 hints->x = win->upos.x;
325 hints->y = win->upos.y;
327 if (win->minSize.width>0 && win->minSize.height>0) {
328 hints->flags |= PMinSize;
329 hints->min_width = win->minSize.width;
330 hints->min_height = win->minSize.height;
332 if (win->maxSize.width>0 && win->maxSize.height>0) {
333 hints->flags |= PMaxSize;
334 hints->max_width = win->maxSize.width;
335 hints->max_height = win->maxSize.height;
337 if (win->baseSize.width>0 && win->baseSize.height>0) {
338 hints->flags |= PBaseSize;
339 hints->base_width = win->baseSize.width;
340 hints->base_height = win->baseSize.height;
342 if (win->resizeIncrement.width>0 && win->resizeIncrement.height>0) {
343 hints->flags |= PResizeInc;
344 hints->width_inc = win->resizeIncrement.width;
345 hints->height_inc = win->resizeIncrement.height;
347 if (win->flags.setAspect) {
348 hints->flags |= PAspect;
349 hints->min_aspect.x = win->minAspect.x;
350 hints->min_aspect.y = win->minAspect.y;
351 hints->max_aspect.x = win->maxAspect.x;
352 hints->max_aspect.y = win->maxAspect.y;
356 if (hints->flags) {
357 XSetWMNormalHints(win->view->screen->display, win->view->window, hints);
359 XFree(hints);
364 static void
365 writeGNUstepWMAttr(WMScreen *scr, Window window, GNUstepWMAttributes *attr)
367 unsigned long data[9];
369 /* handle idiot compilers where array of CARD32 != struct of CARD32 */
370 data[0] = attr->flags;
371 data[1] = attr->window_style;
372 data[2] = attr->window_level;
373 data[3] = 0; /* reserved */
374 /* The X protocol says XIDs are 32bit */
375 data[4] = attr->miniaturize_pixmap;
376 data[5] = attr->close_pixmap;
377 data[6] = attr->miniaturize_mask;
378 data[7] = attr->close_mask;
379 data[8] = attr->extra_flags;
380 XChangeProperty(scr->display, window, scr->attribsAtom, scr->attribsAtom,
381 32, PropModeReplace, (unsigned char *)data, 9);
385 static void
386 setWindowMakerHints(WMWindow *win)
388 GNUstepWMAttributes attribs;
389 WMScreen *scr = WMWidgetScreen(win);
391 memset(&attribs, 0, sizeof(GNUstepWMAttributes));
392 attribs.flags = GSWindowStyleAttr|GSWindowLevelAttr|GSExtraFlagsAttr;
393 attribs.window_style = win->flags.style;
394 attribs.window_level = win->level;
395 if (win->flags.documentEdited)
396 attribs.extra_flags = GSDocumentEditedFlag;
397 else
398 attribs.extra_flags = 0;
400 writeGNUstepWMAttr(scr, win->view->window, &attribs);
404 static void
405 realizeWindow(WMWindow *win)
407 XWMHints *hints;
408 XClassHint *classHint;
409 WMScreen *scr = win->view->screen;
410 Atom atoms[4];
411 int count;
413 classHint = XAllocClassHint();
414 classHint->res_name = win->wname;
415 classHint->res_class = WMGetApplicationName();
416 XSetClassHint(scr->display, win->view->window, classHint);
417 XFree(classHint);
419 hints = XAllocWMHints();
420 hints->flags = 0;
421 if (!scr->aflags.simpleApplication) {
422 hints->flags |= WindowGroupHint;
423 hints->window_group = scr->groupLeader;
425 if (win->miniImage) {
426 hints->flags |= IconPixmapHint;
427 hints->icon_pixmap = WMGetPixmapXID(win->miniImage);
428 hints->icon_mask = WMGetPixmapMaskXID(win->miniImage);
429 if (hints->icon_mask != None) {
430 hints->flags |= IconMaskHint;
433 if (hints->flags != 0)
434 XSetWMHints(scr->display, win->view->window, hints);
435 XFree(hints);
437 count = 0;
438 if (win->closeAction) {
439 atoms[count++] = scr->deleteWindowAtom;
442 if (count>0)
443 XSetWMProtocols(scr->display, win->view->window, atoms, count);
445 if (win->title || win->miniTitle)
446 XmbSetWMProperties(scr->display, win->view->window, win->title,
447 win->miniTitle, NULL, 0, NULL, NULL, NULL);
449 setWindowMakerHints(win);
451 setSizeHints(win);
453 if (win->owner) {
454 XSetTransientForHint(scr->display, win->view->window,
455 win->owner->view->window);
461 void
462 WMSetWindowAspectRatio(WMWindow *win, int minX, int minY,
463 int maxX, int maxY)
465 win->flags.setAspect = 1;
466 win->minAspect.x = minX;
467 win->minAspect.y = minY;
468 win->maxAspect.x = maxX;
469 win->maxAspect.y = maxY;
470 if (win->view->flags.realized)
471 setSizeHints(win);
476 void
477 WMSetWindowInitialPosition(WMWindow *win, int x, int y)
479 win->flags.setPPos = 1;
480 win->ppos.x = x;
481 win->ppos.y = y;
482 if (win->view->flags.realized)
483 setSizeHints(win);
484 WMMoveWidget(win, x, y);
489 void
490 WMSetWindowUserPosition(WMWindow *win, int x, int y)
492 win->flags.setUPos = 1;
493 win->upos.x = x;
494 win->upos.y = y;
495 if (win->view->flags.realized)
496 setSizeHints(win);
497 WMMoveWidget(win, x, y);
503 void
504 WMSetWindowMinSize(WMWindow *win, unsigned width, unsigned height)
506 win->minSize.width = width;
507 win->minSize.height = height;
508 if (win->view->flags.realized)
509 setSizeHints(win);
514 void
515 WMSetWindowMaxSize(WMWindow *win, unsigned width, unsigned height)
517 win->maxSize.width = width;
518 win->maxSize.height = height;
519 if (win->view->flags.realized)
520 setSizeHints(win);
524 void
525 WMSetWindowBaseSize(WMWindow *win, unsigned width, unsigned height)
527 /* TODO: validate sizes */
528 win->baseSize.width = width;
529 win->baseSize.height = height;
530 if (win->view->flags.realized)
531 setSizeHints(win);
535 void
536 WMSetWindowResizeIncrements(WMWindow *win, unsigned wIncr, unsigned hIncr)
538 win->resizeIncrement.width = wIncr;
539 win->resizeIncrement.height = hIncr;
540 if (win->view->flags.realized)
541 setSizeHints(win);
545 void
546 WMSetWindowLevel(WMWindow *win, int level)
548 win->level = level;
549 if (win->view->flags.realized)
550 setWindowMakerHints(win);
554 void
555 WMSetWindowDocumentEdited(WMWindow *win, Bool flag)
557 if (win->flags.documentEdited != flag) {
558 win->flags.documentEdited = flag;
559 if (win->view->flags.realized)
560 setWindowMakerHints(win);
565 void
566 WMSetWindowMiniwindowImage(WMWindow *win, WMPixmap *pixmap)
568 if ((win->miniImage && !pixmap) || (!win->miniImage && pixmap)) {
569 if (win->miniImage)
570 WMReleasePixmap(win->miniImage);
572 if (pixmap)
573 win->miniImage = WMRetainPixmap(pixmap);
574 else
575 win->miniImage = NULL;
577 if (win->view->flags.realized) {
578 XWMHints *hints;
580 hints = XGetWMHints(win->view->screen->display, win->view->window);
581 if (!hints) {
582 hints = XAllocWMHints();
583 if (!hints) {
584 wwarning("could not allocate memory for WM hints");
585 return;
587 hints->flags = 0;
589 if (pixmap) {
590 hints->flags |= IconPixmapHint;
591 hints->icon_pixmap = WMGetPixmapXID(pixmap);
592 hints->icon_mask = WMGetPixmapMaskXID(pixmap);
593 if (hints->icon_mask != None) {
594 hints->flags |= IconMaskHint;
597 XSetWMHints(win->view->screen->display, win->view->window, hints);
598 XFree(hints);
604 void
605 WMSetWindowMiniwindowTitle(WMWindow *win, char *title)
607 XTextProperty property;
608 int result;
610 if ((win->miniTitle && !title) || (!win->miniTitle && title)
611 || (title && win->miniTitle && strcoll(title, win->miniTitle)!=0)) {
612 if (win->miniTitle)
613 wfree(win->miniTitle);
615 if (title)
616 win->miniTitle = wstrdup(title);
617 else
618 win->miniTitle = NULL;
620 if (win->view->flags.realized) {
621 result = XmbTextListToTextProperty (win->view->screen->display,
622 &title, 1, XStdICCTextStyle,
623 &property);
624 if (result == XNoMemory || result == XLocaleNotSupported) {
625 wwarning("icon title conversion error..using STRING encoding");
626 XSetIconName(win->view->screen->display, win->view->window,
627 title);
628 } else {
629 XSetWMIconName(win->view->screen->display, win->view->window,
630 &property);
631 if (property.value)
632 XFree(property.value);
639 void
640 WMCloseWindow(WMWindow *win)
642 WMUnmapWidget(win);
643 /* withdraw the window */
644 if (win->view->flags.realized)
645 XWithdrawWindow(win->view->screen->display, win->view->window,
646 win->view->screen->screen);
650 static void
651 handleEvents(XEvent *event, void *clientData)
653 _Window *win = (_Window*)clientData;
654 W_View *view = win->view;
657 switch (event->type) {
658 case ClientMessage:
659 if (event->xclient.message_type == win->view->screen->protocolsAtom
660 && event->xclient.format == 32
661 && event->xclient.data.l[0]==win->view->screen->deleteWindowAtom) {
663 if (win->closeAction) {
664 (*win->closeAction)(win, win->closeData);
667 break;
669 // was causing windows to ignore commands like closeWindow
670 * // after the windows is iconized/restored or a workspace change
671 * // if this is really needed, put the MapNotify portion too and
672 * // fix the restack bug in wmaker
673 case UnmapNotify:
674 WMUnmapWidget(win);
675 break;
677 case MapNotify:
678 WMMapWidget(win);
679 break;
682 case DestroyNotify:
683 destroyWindow(win);
684 break;
686 case ConfigureNotify:
687 if (event->xconfigure.width != view->size.width
688 || event->xconfigure.height != view->size.height) {
690 view->size.width = event->xconfigure.width;
691 view->size.height = event->xconfigure.height;
693 if (view->flags.notifySizeChanged) {
694 WMPostNotificationName(WMViewSizeDidChangeNotification,
695 view, NULL);
698 if (event->xconfigure.x != view->pos.x
699 || event->xconfigure.y != view->pos.y) {
701 if (event->xconfigure.send_event) {
702 view->pos.x = event->xconfigure.x;
703 view->pos.y = event->xconfigure.y;
704 } else {
705 Window foo;
707 XTranslateCoordinates(view->screen->display,
708 view->window, view->screen->rootWin,
709 event->xconfigure.x, event->xconfigure.y,
710 &view->pos.x, &view->pos.y, &foo);
713 break;
720 static void
721 destroyWindow(_Window *win)
723 WMScreen *scr = win->view->screen;
725 WMRemoveNotificationObserver(win);
727 if (scr->windowList == win) {
728 scr->windowList = scr->windowList->nextPtr;
729 } else {
730 WMWindow *ptr;
731 ptr = scr->windowList;
733 if (ptr) {
734 while (ptr->nextPtr) {
735 if (ptr->nextPtr==win) {
736 ptr->nextPtr = ptr->nextPtr->nextPtr;
737 break;
739 ptr = ptr->nextPtr;
744 if (win->title) {
745 wfree(win->title);
748 if (win->miniTitle) {
749 wfree(win->miniTitle);
752 if (win->miniImage) {
753 WMReleasePixmap(win->miniImage);
756 if (win->wname)
757 wfree(win->wname);
759 wfree(win);