fixed bug with zombies after wmaker crashed
[wmaker-crm.git] / WINGs / wview.c
blob376854412a4bf18f2ec474f5146ac6c14e164223
3 #include "WINGsP.h"
5 #include <X11/Xresource.h>
9 /* the notifications about views */
11 char *WMViewSizeDidChangeNotification = "WMViewSizeDidChangeNotification";
12 char *WMViewFocusDidChangeNotification = "WMViewFocusDidChangeNotification";
13 char *WMViewRealizedNotification = "WMViewRealizedNotification";
16 #define EVENT_MASK \
17 KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask| \
18 EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask| \
19 VisibilityChangeMask|FocusChangeMask|PropertyChangeMask|\
20 SubstructureNotifyMask|SubstructureRedirectMask
23 static XSetWindowAttributes defAtts= {
24 None, /* background_pixmap */
25 0, /* background_pixel */
26 CopyFromParent, /* border_pixmap */
27 0, /* border_pixel */
28 ForgetGravity, /* bit_gravity */
29 ForgetGravity, /* win_gravity */
30 NotUseful, /* backing_store */
31 (unsigned) ~0, /* backing_planes */
32 0, /* backing_pixel */
33 False, /* save_under */
34 EVENT_MASK, /* event_mask */
35 0, /* do_not_propagate_mask */
36 False, /* override_redirect */
37 None, /* colormap */
38 None /* cursor */
43 static XContext ViewContext=0; /* context for views */
48 W_View*
49 W_GetViewForXWindow(Display *display, Window window)
51 W_View *view;
53 if (XFindContext(display, window, ViewContext, (XPointer*)&view)==0) {
54 return view;
56 return NULL;
61 static void
62 unparentView(W_View *view)
64 /* remove from parent's children list */
65 if (view->parent!=NULL) {
66 W_View *ptr;
68 ptr = view->parent->childrenList;
69 if (ptr == view) {
70 view->parent->childrenList = view->nextSister;
71 } else {
72 while (ptr!=NULL) {
73 if (ptr->nextSister == view) {
74 ptr->nextSister = view->nextSister;
75 break;
77 ptr = ptr->nextSister;
81 view->parent = NULL;
85 static void
86 adoptChildView(W_View *view, W_View *child)
88 child->nextSister = NULL;
90 /* add to end of children list of parent */
91 if (view->childrenList == NULL) {
92 view->childrenList = child;
93 } else {
94 W_View *ptr;
96 ptr = view->childrenList;
97 while (ptr->nextSister!=NULL)
98 ptr = ptr->nextSister;
99 ptr->nextSister = child;
101 child->parent = view;
105 static W_View*
106 createView(W_Screen *screen, W_View *parent)
108 W_View *view;
110 if (ViewContext==0)
111 ViewContext = XUniqueContext();
113 view = wmalloc(sizeof(W_View));
114 memset(view, 0, sizeof(W_View));
116 view->screen = screen;
118 if (parent!=NULL) {
119 /* attributes are not valid for root window */
120 view->attribFlags = CWEventMask|CWBitGravity;
121 view->attribs = defAtts;
123 view->attribFlags |= CWBackPixel|CWColormap|CWBorderPixel;
124 view->attribs.background_pixel = W_PIXEL(screen->gray);
125 view->attribs.border_pixel = W_PIXEL(screen->black);
126 view->attribs.colormap = screen->colormap;
128 view->backColor = WMRetainColor(screen->gray);
130 adoptChildView(parent, view);
133 view->refCount = 1;
135 view->eventHandlers = WMCreateArrayWithDestructor(4, wfree);
137 return view;
142 W_View*
143 W_CreateView(W_View *parent)
145 return createView(parent->screen, parent);
149 W_View*
150 W_CreateRootView(W_Screen *screen)
152 W_View *view;
154 view = createView(screen, NULL);
156 view->window = screen->rootWin;
158 view->flags.realized = 1;
159 view->flags.mapped = 1;
160 view->flags.root = 1;
162 view->size.width =
163 WidthOfScreen(ScreenOfDisplay(screen->display, screen->screen));
164 view->size.height =
165 HeightOfScreen(ScreenOfDisplay(screen->display, screen->screen));
167 return view;
171 W_View*
172 W_CreateTopView(W_Screen *screen)
174 W_View *view;
176 view = createView(screen, screen->rootView);
177 if (!view)
178 return NULL;
180 view->flags.topLevel = 1;
181 view->attribs.event_mask |= StructureNotifyMask;
183 return view;
187 W_View*
188 W_CreateUnmanagedTopView(W_Screen *screen)
190 W_View *view;
192 view = createView(screen, screen->rootView);
193 if (!view)
194 return NULL;
196 view->flags.topLevel = 1;
197 view->attribs.event_mask |= StructureNotifyMask;
199 view->attribFlags |= CWOverrideRedirect;
200 view->attribs.override_redirect = True;
202 return view;
206 void
207 W_RealizeView(W_View *view)
209 Window parentWID;
210 Display *dpy = view->screen->display;
211 W_View *ptr;
213 assert(view->size.width > 0);
214 assert(view->size.height > 0);
217 if (view->parent && !view->parent->flags.realized) {
218 wwarning("trying to realize widget of unrealized parent");
219 return;
222 if (!view->flags.realized) {
223 parentWID = view->parent->window;
224 view->window = XCreateWindow(dpy, parentWID, view->pos.x, view->pos.y,
225 view->size.width, view->size.height, 0,
226 view->screen->depth, InputOutput,
227 view->screen->visual, view->attribFlags,
228 &view->attribs);
230 XSaveContext(dpy, view->window, ViewContext, (XPointer)view);
232 view->flags.realized = 1;
234 if (view->flags.mapWhenRealized) {
235 W_MapView(view);
236 view->flags.mapWhenRealized = 0;
239 WMPostNotificationName(WMViewRealizedNotification, view, NULL);
242 /* realize children */
243 ptr = view->childrenList;
244 while (ptr!=NULL) {
245 W_RealizeView(ptr);
247 ptr = ptr->nextSister;
252 void
253 W_ReparentView(W_View *view, W_View *newParent, int x, int y)
255 Display *dpy = view->screen->display;
257 assert(!view->flags.topLevel);
259 unparentView(view);
260 adoptChildView(newParent, view);
262 if (view->flags.realized) {
263 if (newParent->flags.realized) {
264 XReparentWindow(dpy, view->window, newParent->window, x, y);
265 } else {
266 wwarning("trying to reparent realized view to unrealized parent");
267 return;
271 view->pos.x = x;
272 view->pos.y = y;
276 void
277 W_RaiseView(W_View *view)
279 if (W_VIEW_REALIZED(view))
280 XRaiseWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
285 void
286 W_LowerView(W_View *view)
288 if (W_VIEW_REALIZED(view))
289 XLowerWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
294 void
295 W_MapView(W_View *view)
297 if (!view->flags.mapped) {
298 if (view->flags.realized) {
299 XMapRaised(view->screen->display, view->window);
300 XFlush(view->screen->display);
301 view->flags.mapped = 1;
302 } else {
303 view->flags.mapWhenRealized = 1;
310 * W_MapSubviews-
311 * maps all children of the current view that where already realized.
313 void
314 W_MapSubviews(W_View *view)
316 XMapSubwindows(view->screen->display, view->window);
317 XFlush(view->screen->display);
319 view = view->childrenList;
320 while (view) {
321 view->flags.mapped = 1;
322 view->flags.mapWhenRealized = 0;
323 view = view->nextSister;
329 void
330 W_UnmapSubviews(W_View *view)
332 XUnmapSubwindows(view->screen->display, view->window);
333 XFlush(view->screen->display);
335 view = view->childrenList;
336 while (view) {
337 view->flags.mapped = 0;
338 view->flags.mapWhenRealized = 0;
339 view = view->nextSister;
345 void
346 W_UnmapView(W_View *view)
348 view->flags.mapWhenRealized = 0;
349 if (!view->flags.mapped)
350 return;
352 XUnmapWindow(view->screen->display, view->window);
353 XFlush(view->screen->display);
355 view->flags.mapped = 0;
359 W_View*
360 W_TopLevelOfView(W_View *view)
362 W_View *toplevel;
364 for (toplevel=view;
365 toplevel && !toplevel->flags.topLevel;
366 toplevel=toplevel->parent);
368 return toplevel;
372 static void
373 destroyView(W_View *view)
375 W_View *ptr;
377 if (view->flags.alreadyDead)
378 return;
379 view->flags.alreadyDead = 1;
381 /* delete the balloon text for the view, if there's any */
382 WMSetBalloonTextForView(NULL, view);
384 if (view->nextFocusChain)
385 view->nextFocusChain->prevFocusChain = view->prevFocusChain;
386 if (view->prevFocusChain)
387 view->prevFocusChain->nextFocusChain = view->nextFocusChain;
389 /* Do not leave focus in a inexisting control */
390 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view))==view)
391 W_SetFocusOfTopLevel(W_TopLevelOfView(view), NULL);
393 if (view->flags.topLevel) {
394 W_FocusInfo *info = view->screen->focusInfo;
395 /* remove focus information associated to this toplevel */
397 if (info) {
398 if (info->toplevel==view) {
399 view->screen->focusInfo = info->next;
400 wfree(info);
401 } else {
402 while (info->next) {
403 if (info->next->toplevel == view)
404 break;
405 info = info->next;
407 if (info->next) {
408 W_FocusInfo *next = info->next->next;
409 wfree(info->next);
410 info->next = next;
412 /* else the toplevel did not have any focused subview */
417 /* destroy children recursively */
418 while (view->childrenList!=NULL) {
419 ptr = view->childrenList;
420 ptr->flags.parentDying = 1;
422 W_DestroyView(ptr);
424 if (ptr == view->childrenList) {
425 view->childrenList = ptr->nextSister;
426 ptr->parent = NULL;
430 W_CallDestroyHandlers(view);
432 if (view->flags.realized) {
433 XDeleteContext(view->screen->display,
434 view->window, ViewContext);
436 /* if parent is being destroyed, it will die naturaly */
437 if (!view->flags.parentDying || view->flags.topLevel)
438 XDestroyWindow(view->screen->display, view->window);
441 /* remove self from parent's children list */
442 unparentView(view);
444 /* the array has a wfree() destructor that will be called automatically */
445 WMFreeArray(view->eventHandlers);
446 view->eventHandlers = NULL;
448 WMRemoveNotificationObserver(view);
450 W_FreeViewXdndPart(view);
452 wfree(view);
457 void
458 W_DestroyView(W_View *view)
460 view->refCount--;
462 if (view->refCount < 1) {
463 destroyView(view);
469 void
470 W_MoveView(W_View *view, int x, int y)
472 assert(view->flags.root==0);
474 if (view->delegate && view->delegate->willMove) {
475 (*view->delegate->willMove)(view->delegate, view, &x, &y);
478 if (view->pos.x == x && view->pos.y == y)
479 return;
481 if (view->flags.realized) {
482 XMoveWindow(view->screen->display, view->window, x, y);
484 view->pos.x = x;
485 view->pos.y = y;
487 if (view->delegate && view->delegate->didMove) {
488 (*view->delegate->didMove)(view->delegate, view);
493 void
494 W_ResizeView(W_View *view, unsigned int width, unsigned int height)
496 /*int shrinked;*/
498 if (view->delegate && view->delegate->willResize) {
499 (*view->delegate->willResize)(view->delegate, view, &width, &height);
502 assert(width > 0);
503 assert(height > 0);
505 if (view->size.width == width && view->size.height == height)
506 return;
508 /*shrinked = width < view->size.width || height < view->size.height;*/
510 if (view->flags.realized) {
511 XResizeWindow(view->screen->display, view->window, width, height);
513 view->size.width = width;
514 view->size.height = height;
516 if (view->delegate && view->delegate->didResize) {
517 (*view->delegate->didResize)(view->delegate, view);
520 /* // TODO. replace in WINGs code, with the didResize delegate */
521 if (view->flags.notifySizeChanged)
522 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
526 void
527 W_RedisplayView(W_View *view)
529 XEvent ev;
531 if (!view->flags.mapped)
532 return;
534 ev.xexpose.type = Expose;
535 ev.xexpose.display = view->screen->display;
536 ev.xexpose.window = view->window;
537 ev.xexpose.count = 0;
539 ev.xexpose.serial = 0;
541 WMHandleEvent(&ev);
545 void
546 W_SetViewBackgroundColor(W_View *view, WMColor *color)
548 if (view->backColor)
549 WMReleaseColor(view->backColor);
550 view->backColor = WMRetainColor(color);
552 view->attribFlags |= CWBackPixel;
553 view->attribs.background_pixel = W_PIXEL(color);
554 if (view->flags.realized) {
555 XSetWindowBackground(view->screen->display, view->window,
556 W_PIXEL(color));
557 XClearWindow(view->screen->display, view->window);
562 void
563 W_SetViewCursor(W_View *view, Cursor cursor)
565 view->cursor = cursor;
566 if (W_VIEW_REALIZED(view)) {
567 XDefineCursor(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view), cursor);
568 } else {
569 view->attribFlags |= CWCursor;
570 view->attribs.cursor = cursor;
575 W_View*
576 W_FocusedViewOfToplevel(W_View *view)
578 WMScreen *scr = view->screen;
579 W_FocusInfo *info;
581 for (info = scr->focusInfo; info!=NULL; info = info->next)
582 if (view == info->toplevel)
583 break;
585 if (!info)
586 return NULL;
588 return info->focused;
592 void
593 W_SetFocusOfTopLevel(W_View *toplevel, W_View *view)
595 WMScreen *scr = toplevel->screen;
596 XEvent event;
597 W_FocusInfo *info;
599 for (info = scr->focusInfo; info!=NULL; info = info->next)
600 if (toplevel == info->toplevel)
601 break;
603 if (!info) {
604 info = wmalloc(sizeof(W_FocusInfo));
605 info->toplevel = toplevel;
606 info->focused = view;
607 info->next = scr->focusInfo;
608 scr->focusInfo = info;
609 } else {
610 event.xfocus.mode = NotifyNormal;
611 event.xfocus.detail = NotifyDetailNone;
612 if (info->focused) {
613 /* simulate FocusOut event */
614 event.xfocus.type = FocusOut;
615 W_DispatchMessage(info->focused, &event);
617 info->focused = view;
619 if (view) {
620 /* simulate FocusIn event */
621 event.xfocus.type = FocusIn;
622 W_DispatchMessage(view, &event);
627 void
628 W_BroadcastMessage(W_View *targetParent, XEvent *event)
630 W_View *target;
632 target = targetParent->childrenList;
633 while (target!=NULL) {
634 W_DispatchMessage(target, event);
636 target = target->nextSister;
641 void
642 W_DispatchMessage(W_View *target, XEvent *event)
644 if (target->window==None)
645 return;
646 event->xclient.window = target->window;
647 event->xclient.display = target->screen->display;
649 WMHandleEvent(event);
651 XSendEvent(target->screen->display, target->window, False,
652 SubstructureNotifyMask, event);
658 WMView*
659 W_RetainView(WMView *view)
661 view->refCount++;
663 return view;
667 void
668 W_ReleaseView(WMView *view)
670 view->refCount--;
672 if (view->refCount < 1) {
673 destroyView(view);
678 WMWidget*
679 WMWidgetOfView(WMView *view)
681 return view->self;
685 WMSize
686 WMGetViewSize(WMView *view)
688 return view->size;
691 WMPoint
692 WMGetViewPosition(WMView *view)
694 return view->pos;
698 void
699 WMSetViewNotifySizeChanges(WMView *view, Bool flag)
701 view->flags.notifySizeChanged = ((flag==0) ? 0 : 1);
705 Window
706 WMViewXID(WMView *view)
708 return view->window;
712 WMPoint
713 WMGetViewScreenPosition(WMView *view)
715 WMScreen *scr = W_VIEW_SCREEN(view);
716 Window foo;
717 int x, y, topX, topY, bar;
718 WMView *topView;
720 topView = view;
721 while (topView->parent && topView->parent!=scr->rootView)
722 topView = topView->parent;
724 if (!XGetGeometry(scr->display, W_VIEW_DRAWABLE(topView), &foo, &topX,
725 &topY, &bar, &bar, &bar, &bar)) {
726 topX = topY = 0;
729 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(view),
730 scr->rootWin, 0, 0, &x, &y, &foo);
732 return wmkpoint(x-topX, y-topY);
736 static void
737 resizedParent(void *self, WMNotification *notif)
739 WMSize size = WMGetViewSize((WMView*)WMGetNotificationObject(notif));
740 WMView *view = (WMView*)self;
742 W_MoveView(view, view->leftOffs, view->topOffs);
743 W_ResizeView(view, size.width - (view->leftOffs + view->rightOffs),
744 size.height - (view->topOffs + view->bottomOffs));
748 void
749 WMSetViewExpandsToParent(WMView *view, int leftOffs, int topOffs,
750 int rightOffs, int bottomOffs)
752 WMSize size = view->parent->size;
754 view->topOffs = topOffs;
755 view->bottomOffs = bottomOffs;
756 view->leftOffs = leftOffs;
757 view->rightOffs = rightOffs;
759 WMAddNotificationObserver(resizedParent, view,
760 WMViewSizeDidChangeNotification,
761 view->parent);
762 WMSetViewNotifySizeChanges(view->parent, True);
764 W_MoveView(view, leftOffs, topOffs);
765 W_ResizeView(view, size.width - (leftOffs + rightOffs),
766 size.height - (topOffs + bottomOffs));