Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wview.c
blobda7329e1de3fb288adc4d79877031067b7c60aa5
2 #include "WINGsP.h"
4 #include <X11/Xresource.h>
6 /* the notifications about views */
8 char *WMViewSizeDidChangeNotification = "WMViewSizeDidChangeNotification";
9 char *WMViewFocusDidChangeNotification = "WMViewFocusDidChangeNotification";
10 char *WMViewRealizedNotification = "WMViewRealizedNotification";
12 #define EVENT_MASK \
13 KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask| \
14 EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask| \
15 VisibilityChangeMask|FocusChangeMask|PropertyChangeMask|\
16 SubstructureNotifyMask|SubstructureRedirectMask
18 static XSetWindowAttributes defAtts = {
19 None, /* background_pixmap */
20 0, /* background_pixel */
21 CopyFromParent, /* border_pixmap */
22 0, /* border_pixel */
23 ForgetGravity, /* bit_gravity */
24 ForgetGravity, /* win_gravity */
25 NotUseful, /* backing_store */
26 (unsigned)~0, /* backing_planes */
27 0, /* backing_pixel */
28 False, /* save_under */
29 EVENT_MASK, /* event_mask */
30 0, /* do_not_propagate_mask */
31 False, /* override_redirect */
32 None, /* colormap */
33 None /* cursor */
34 };
36 static XContext ViewContext = 0; /* context for views */
38 W_View *W_GetViewForXWindow(Display * display, Window window)
39 {
40 W_View *view;
42 if (XFindContext(display, window, ViewContext, (XPointer *) & view) == 0) {
43 return view;
44 }
45 return NULL;
46 }
48 static void unparentView(W_View * view)
49 {
50 /* remove from parent's children list */
51 if (view->parent != NULL) {
52 W_View *ptr;
54 ptr = view->parent->childrenList;
55 if (ptr == view) {
56 view->parent->childrenList = view->nextSister;
57 } else {
58 while (ptr != NULL) {
59 if (ptr->nextSister == view) {
60 ptr->nextSister = view->nextSister;
61 break;
62 }
63 ptr = ptr->nextSister;
64 }
65 }
66 }
67 view->parent = NULL;
68 }
70 static void adoptChildView(W_View * view, W_View * child)
71 {
72 child->nextSister = NULL;
74 /* add to end of children list of parent */
75 if (view->childrenList == NULL) {
76 view->childrenList = child;
77 } else {
78 W_View *ptr;
80 ptr = view->childrenList;
81 while (ptr->nextSister != NULL)
82 ptr = ptr->nextSister;
83 ptr->nextSister = child;
84 }
85 child->parent = view;
86 }
88 static W_View *createView(W_Screen * screen, W_View * parent)
89 {
90 W_View *view;
92 if (ViewContext == 0)
93 ViewContext = XUniqueContext();
95 view = wmalloc(sizeof(W_View));
96 memset(view, 0, sizeof(W_View));
98 view->screen = screen;
100 if (parent != NULL) {
101 /* attributes are not valid for root window */
102 view->attribFlags = CWEventMask | CWBitGravity;
103 view->attribs = defAtts;
105 view->attribFlags |= CWBackPixel | CWColormap | CWBorderPixel;
106 view->attribs.background_pixel = W_PIXEL(screen->gray);
107 view->attribs.border_pixel = W_PIXEL(screen->black);
108 view->attribs.colormap = screen->colormap;
110 view->backColor = WMRetainColor(screen->gray);
112 adoptChildView(parent, view);
115 view->xic = 0;
117 view->refCount = 1;
119 view->eventHandlers = WMCreateArrayWithDestructor(4, wfree);
121 return view;
124 W_View *W_CreateView(W_View * parent)
126 return createView(parent->screen, parent);
129 W_View *W_CreateRootView(W_Screen * screen)
131 W_View *view;
133 view = createView(screen, NULL);
135 view->window = screen->rootWin;
137 view->flags.realized = 1;
138 view->flags.mapped = 1;
139 view->flags.root = 1;
141 view->size.width = WidthOfScreen(ScreenOfDisplay(screen->display, screen->screen));
142 view->size.height = HeightOfScreen(ScreenOfDisplay(screen->display, screen->screen));
144 return view;
147 W_View *W_CreateTopView(W_Screen * screen)
149 W_View *view;
151 view = createView(screen, screen->rootView);
152 if (!view)
153 return NULL;
155 view->flags.topLevel = 1;
156 view->attribs.event_mask |= StructureNotifyMask;
158 return view;
161 W_View *W_CreateUnmanagedTopView(W_Screen * screen)
163 W_View *view;
165 view = createView(screen, screen->rootView);
166 if (!view)
167 return NULL;
169 view->flags.topLevel = 1;
170 view->attribs.event_mask |= StructureNotifyMask;
172 view->attribFlags |= CWOverrideRedirect;
173 view->attribs.override_redirect = True;
175 return view;
178 void W_RealizeView(W_View * view)
180 Window parentWID;
181 Display *dpy = view->screen->display;
182 W_View *ptr;
184 assert(view->size.width > 0);
185 assert(view->size.height > 0);
187 if (view->parent && !view->parent->flags.realized) {
188 wwarning("trying to realize widget of unrealized parent");
189 return;
192 if (!view->flags.realized) {
193 parentWID = view->parent->window;
194 view->window = XCreateWindow(dpy, parentWID, view->pos.x, view->pos.y,
195 view->size.width, view->size.height, 0,
196 view->screen->depth, InputOutput,
197 view->screen->visual, view->attribFlags, &view->attribs);
199 XSaveContext(dpy, view->window, ViewContext, (XPointer) view);
201 view->flags.realized = 1;
203 if (view->flags.mapWhenRealized) {
204 W_MapView(view);
205 view->flags.mapWhenRealized = 0;
208 WMPostNotificationName(WMViewRealizedNotification, view, NULL);
211 /* realize children */
212 ptr = view->childrenList;
213 while (ptr != NULL) {
214 W_RealizeView(ptr);
216 ptr = ptr->nextSister;
220 void W_ReparentView(W_View * view, W_View * newParent, int x, int y)
222 Display *dpy = view->screen->display;
224 assert(!view->flags.topLevel);
226 unparentView(view);
227 adoptChildView(newParent, view);
229 if (view->flags.realized) {
230 if (newParent->flags.realized) {
231 XReparentWindow(dpy, view->window, newParent->window, x, y);
232 } else {
233 wwarning("trying to reparent realized view to unrealized parent");
234 return;
238 view->pos.x = x;
239 view->pos.y = y;
242 void W_RaiseView(W_View * view)
244 if (W_VIEW_REALIZED(view))
245 XRaiseWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
248 void W_LowerView(W_View * view)
250 if (W_VIEW_REALIZED(view))
251 XLowerWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
254 void W_MapView(W_View * view)
256 if (!view->flags.mapped) {
257 if (view->flags.realized) {
258 XMapRaised(view->screen->display, view->window);
259 XFlush(view->screen->display);
260 view->flags.mapped = 1;
261 } else {
262 view->flags.mapWhenRealized = 1;
267 /*
268 * W_MapSubviews-
269 * maps all children of the current view that where already realized.
270 */
271 void W_MapSubviews(W_View * view)
273 XMapSubwindows(view->screen->display, view->window);
274 XFlush(view->screen->display);
276 view = view->childrenList;
277 while (view) {
278 view->flags.mapped = 1;
279 view->flags.mapWhenRealized = 0;
280 view = view->nextSister;
284 void W_UnmapSubviews(W_View * view)
286 XUnmapSubwindows(view->screen->display, view->window);
287 XFlush(view->screen->display);
289 view = view->childrenList;
290 while (view) {
291 view->flags.mapped = 0;
292 view->flags.mapWhenRealized = 0;
293 view = view->nextSister;
297 void W_UnmapView(W_View * view)
299 view->flags.mapWhenRealized = 0;
300 if (!view->flags.mapped)
301 return;
303 XUnmapWindow(view->screen->display, view->window);
304 XFlush(view->screen->display);
306 view->flags.mapped = 0;
309 W_View *W_TopLevelOfView(W_View * view)
311 W_View *toplevel;
313 for (toplevel = view; toplevel && !toplevel->flags.topLevel; toplevel = toplevel->parent) ;
315 return toplevel;
318 static void destroyView(W_View * view)
320 W_View *ptr;
322 if (view->flags.alreadyDead)
323 return;
324 view->flags.alreadyDead = 1;
326 /* delete the balloon text for the view, if there's any */
327 WMSetBalloonTextForView(NULL, view);
329 if (view->nextFocusChain)
330 view->nextFocusChain->prevFocusChain = view->prevFocusChain;
331 if (view->prevFocusChain)
332 view->prevFocusChain->nextFocusChain = view->nextFocusChain;
334 /* Do not leave focus in a inexisting control */
335 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view)) == view)
336 W_SetFocusOfTopLevel(W_TopLevelOfView(view), NULL);
338 if (view->flags.topLevel) {
339 W_FocusInfo *info = view->screen->focusInfo;
340 /* remove focus information associated to this toplevel */
342 if (info) {
343 if (info->toplevel == view) {
344 view->screen->focusInfo = info->next;
345 wfree(info);
346 } else {
347 while (info->next) {
348 if (info->next->toplevel == view)
349 break;
350 info = info->next;
352 if (info->next) {
353 W_FocusInfo *next = info->next->next;
354 wfree(info->next);
355 info->next = next;
357 /* else the toplevel did not have any focused subview */
362 /* destroy children recursively */
363 while (view->childrenList != NULL) {
364 ptr = view->childrenList;
365 ptr->flags.parentDying = 1;
367 W_DestroyView(ptr);
369 if (ptr == view->childrenList) {
370 view->childrenList = ptr->nextSister;
371 ptr->parent = NULL;
375 W_CallDestroyHandlers(view);
377 if (view->flags.realized) {
378 XDeleteContext(view->screen->display, view->window, ViewContext);
380 /* if parent is being destroyed, it will die naturaly */
381 if (!view->flags.parentDying || view->flags.topLevel)
382 XDestroyWindow(view->screen->display, view->window);
385 /* remove self from parent's children list */
386 unparentView(view);
388 /* the array has a wfree() destructor that will be called automatically */
389 WMFreeArray(view->eventHandlers);
390 view->eventHandlers = NULL;
392 WMRemoveNotificationObserver(view);
394 W_FreeViewXdndPart(view);
396 if (view->backColor)
397 WMReleaseColor(view->backColor);
399 wfree(view);
402 void W_DestroyView(W_View * view)
404 view->refCount--;
406 if (view->refCount < 1) {
407 destroyView(view);
411 void W_MoveView(W_View * view, int x, int y)
413 assert(view->flags.root == 0);
415 if (view->delegate && view->delegate->willMove) {
416 (*view->delegate->willMove) (view->delegate, view, &x, &y);
419 if (view->pos.x == x && view->pos.y == y)
420 return;
422 if (view->flags.realized) {
423 XMoveWindow(view->screen->display, view->window, x, y);
425 view->pos.x = x;
426 view->pos.y = y;
428 if (view->delegate && view->delegate->didMove) {
429 (*view->delegate->didMove) (view->delegate, view);
433 void W_ResizeView(W_View * view, unsigned int width, unsigned int height)
435 /*int shrinked; */
437 if (view->delegate && view->delegate->willResize) {
438 (*view->delegate->willResize) (view->delegate, view, &width, &height);
441 assert(width > 0);
442 assert(height > 0);
444 if (view->size.width == width && view->size.height == height)
445 return;
447 /*shrinked = width < view->size.width || height < view->size.height; */
449 if (view->flags.realized) {
450 XResizeWindow(view->screen->display, view->window, width, height);
452 view->size.width = width;
453 view->size.height = height;
455 if (view->delegate && view->delegate->didResize) {
456 (*view->delegate->didResize) (view->delegate, view);
459 /* // TODO. replace in WINGs code, with the didResize delegate */
460 if (view->flags.notifySizeChanged)
461 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
464 void W_RedisplayView(W_View * view)
466 XEvent ev;
468 if (!view->flags.mapped)
469 return;
471 ev.xexpose.type = Expose;
472 ev.xexpose.display = view->screen->display;
473 ev.xexpose.window = view->window;
474 ev.xexpose.count = 0;
476 ev.xexpose.serial = 0;
478 WMHandleEvent(&ev);
481 void W_SetViewBackgroundColor(W_View * view, WMColor * color)
483 if (view->backColor)
484 WMReleaseColor(view->backColor);
485 view->backColor = WMRetainColor(color);
487 view->attribFlags |= CWBackPixel;
488 view->attribs.background_pixel = W_PIXEL(color);
489 if (view->flags.realized) {
490 XSetWindowBackground(view->screen->display, view->window, W_PIXEL(color));
491 XClearWindow(view->screen->display, view->window);
495 void W_SetViewCursor(W_View * view, Cursor cursor)
497 view->cursor = cursor;
498 if (W_VIEW_REALIZED(view)) {
499 XDefineCursor(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view), cursor);
500 } else {
501 view->attribFlags |= CWCursor;
502 view->attribs.cursor = cursor;
506 W_View *W_FocusedViewOfToplevel(W_View * view)
508 WMScreen *scr = view->screen;
509 W_FocusInfo *info;
511 for (info = scr->focusInfo; info != NULL; info = info->next)
512 if (view == info->toplevel)
513 break;
515 if (!info)
516 return NULL;
518 return info->focused;
521 void W_SetFocusOfTopLevel(W_View * toplevel, W_View * view)
523 WMScreen *scr = toplevel->screen;
524 XEvent event;
525 W_FocusInfo *info;
527 for (info = scr->focusInfo; info != NULL; info = info->next)
528 if (toplevel == info->toplevel)
529 break;
531 if (!info) {
532 info = wmalloc(sizeof(W_FocusInfo));
533 info->toplevel = toplevel;
534 info->focused = view;
535 info->next = scr->focusInfo;
536 scr->focusInfo = info;
537 } else {
538 event.xfocus.mode = NotifyNormal;
539 event.xfocus.detail = NotifyDetailNone;
540 if (info->focused) {
541 /* simulate FocusOut event */
542 event.xfocus.type = FocusOut;
543 W_DispatchMessage(info->focused, &event);
545 info->focused = view;
547 if (view) {
548 /* simulate FocusIn event */
549 event.xfocus.type = FocusIn;
550 W_DispatchMessage(view, &event);
554 void W_BroadcastMessage(W_View * targetParent, XEvent * event)
556 W_View *target;
558 target = targetParent->childrenList;
559 while (target != NULL) {
560 W_DispatchMessage(target, event);
562 target = target->nextSister;
566 void W_DispatchMessage(W_View * target, XEvent * event)
568 if (target->window == None)
569 return;
570 event->xclient.window = target->window;
571 event->xclient.display = target->screen->display;
573 WMHandleEvent(event);
574 /*
575 XSendEvent(target->screen->display, target->window, False,
576 SubstructureNotifyMask, event);
577 */
580 WMView *W_RetainView(WMView * view)
582 view->refCount++;
584 return view;
587 void W_ReleaseView(WMView * view)
589 view->refCount--;
591 if (view->refCount < 1) {
592 destroyView(view);
596 WMWidget *WMWidgetOfView(WMView * view)
598 return view->self;
601 WMSize WMGetViewSize(WMView * view)
603 return view->size;
606 WMPoint WMGetViewPosition(WMView * view)
608 return view->pos;
611 void WMSetViewNotifySizeChanges(WMView * view, Bool flag)
613 view->flags.notifySizeChanged = ((flag == 0) ? 0 : 1);
616 Window WMViewXID(WMView * view)
618 return view->window;
621 WMPoint WMGetViewScreenPosition(WMView * view)
623 WMScreen *scr = W_VIEW_SCREEN(view);
624 Window foo;
625 int x, y, topX, topY;
626 unsigned int bar;
627 WMView *topView;
629 topView = view;
630 while (topView->parent && topView->parent != scr->rootView)
631 topView = topView->parent;
633 if (!XGetGeometry(scr->display, W_VIEW_DRAWABLE(topView), &foo, &topX, &topY, &bar, &bar, &bar, &bar)) {
634 topX = topY = 0;
637 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(view), scr->rootWin, 0, 0, &x, &y, &foo);
639 return wmkpoint(x - topX, y - topY);
642 static void resizedParent(void *self, WMNotification * notif)
644 WMSize size = WMGetViewSize((WMView *) WMGetNotificationObject(notif));
645 WMView *view = (WMView *) self;
647 W_MoveView(view, view->leftOffs, view->topOffs);
648 W_ResizeView(view, size.width - (view->leftOffs + view->rightOffs),
649 size.height - (view->topOffs + view->bottomOffs));
652 void WMSetViewExpandsToParent(WMView * view, int leftOffs, int topOffs, int rightOffs, int bottomOffs)
654 WMSize size = view->parent->size;
656 view->topOffs = topOffs;
657 view->bottomOffs = bottomOffs;
658 view->leftOffs = leftOffs;
659 view->rightOffs = rightOffs;
661 WMAddNotificationObserver(resizedParent, view, WMViewSizeDidChangeNotification, view->parent);
662 WMSetViewNotifySizeChanges(view->parent, True);
664 W_MoveView(view, leftOffs, topOffs);
665 W_ResizeView(view, size.width - (leftOffs + rightOffs), size.height - (topOffs + bottomOffs));