WPrefs: link against math library because it is used in a few places
[wmaker-crm.git] / WINGs / wview.c
blob20662447858f806146acb8e2a17d432153a34e8e
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 const 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 */
36 static XContext ViewContext = 0; /* context for views */
38 W_View *W_GetViewForXWindow(Display * display, Window window)
40 W_View *view;
42 if (XFindContext(display, window, ViewContext, (XPointer *) & view) == 0) {
43 return view;
45 return NULL;
48 static void unparentView(W_View * view)
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;
63 ptr = ptr->nextSister;
67 view->parent = NULL;
70 static void adoptChildView(W_View * view, W_View * child)
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;
85 child->parent = view;
88 static W_View *createView(W_Screen * screen, W_View * parent)
90 W_View *view;
92 if (ViewContext == 0)
93 ViewContext = XUniqueContext();
95 view = wmalloc(sizeof(W_View));
96 view->screen = screen;
98 if (parent != NULL) {
99 /* attributes are not valid for root window */
100 view->attribFlags = CWEventMask | CWBitGravity;
101 view->attribs = defAtts;
103 view->attribFlags |= CWBackPixel | CWColormap | CWBorderPixel;
104 view->attribs.background_pixel = W_PIXEL(screen->gray);
105 view->attribs.border_pixel = W_PIXEL(screen->black);
106 view->attribs.colormap = screen->colormap;
108 view->backColor = WMRetainColor(screen->gray);
110 adoptChildView(parent, view);
113 view->xic = 0;
115 view->refCount = 1;
117 view->eventHandlers = WMCreateArrayWithDestructor(4, wfree);
119 return view;
122 W_View *W_CreateView(W_View * parent)
124 return createView(parent->screen, parent);
127 W_View *W_CreateRootView(W_Screen * screen)
129 W_View *view;
131 view = createView(screen, NULL);
133 view->window = screen->rootWin;
135 view->flags.realized = 1;
136 view->flags.mapped = 1;
137 view->flags.root = 1;
139 view->size.width = WidthOfScreen(ScreenOfDisplay(screen->display, screen->screen));
140 view->size.height = HeightOfScreen(ScreenOfDisplay(screen->display, screen->screen));
142 return view;
145 W_View *W_CreateTopView(W_Screen * screen)
147 W_View *view;
149 view = createView(screen, screen->rootView);
150 if (!view)
151 return NULL;
153 view->flags.topLevel = 1;
154 view->attribs.event_mask |= StructureNotifyMask;
156 return view;
159 W_View *W_CreateUnmanagedTopView(W_Screen * screen)
161 W_View *view;
163 view = createView(screen, screen->rootView);
164 if (!view)
165 return NULL;
167 view->flags.topLevel = 1;
168 view->attribs.event_mask |= StructureNotifyMask;
170 view->attribFlags |= CWOverrideRedirect;
171 view->attribs.override_redirect = True;
173 return view;
176 void W_RealizeView(W_View * view)
178 Window parentWID;
179 Display *dpy = view->screen->display;
180 W_View *ptr;
182 assert(view->size.width > 0);
183 assert(view->size.height > 0);
185 if (view->parent && !view->parent->flags.realized) {
186 wwarning("trying to realize widget of unrealized parent");
187 return;
190 if (!view->flags.realized) {
192 if (view->parent == NULL) {
193 wwarning("trying to realize widget without parent");
194 return;
197 parentWID = view->parent->window;
198 view->window = XCreateWindow(dpy, parentWID, view->pos.x, view->pos.y,
199 view->size.width, view->size.height, 0,
200 view->screen->depth, InputOutput,
201 view->screen->visual, view->attribFlags, &view->attribs);
203 XSaveContext(dpy, view->window, ViewContext, (XPointer) view);
205 view->flags.realized = 1;
207 if (view->flags.mapWhenRealized) {
208 W_MapView(view);
209 view->flags.mapWhenRealized = 0;
212 WMPostNotificationName(WMViewRealizedNotification, view, NULL);
215 /* realize children */
216 ptr = view->childrenList;
217 while (ptr != NULL) {
218 W_RealizeView(ptr);
220 ptr = ptr->nextSister;
224 void W_ReparentView(W_View * view, W_View * newParent, int x, int y)
226 Display *dpy = view->screen->display;
228 assert(!view->flags.topLevel);
230 unparentView(view);
231 adoptChildView(newParent, view);
233 if (view->flags.realized) {
234 if (newParent->flags.realized) {
235 XReparentWindow(dpy, view->window, newParent->window, x, y);
236 } else {
237 wwarning("trying to reparent realized view to unrealized parent");
238 return;
242 view->pos.x = x;
243 view->pos.y = y;
246 void W_RaiseView(W_View * view)
248 if (W_VIEW_REALIZED(view))
249 XRaiseWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
252 void W_LowerView(W_View * view)
254 if (W_VIEW_REALIZED(view))
255 XLowerWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
258 void W_MapView(W_View * view)
260 if (!view->flags.mapped) {
261 if (view->flags.realized) {
262 XMapRaised(view->screen->display, view->window);
263 XFlush(view->screen->display);
264 view->flags.mapped = 1;
265 } else {
266 view->flags.mapWhenRealized = 1;
272 * W_MapSubviews-
273 * maps all children of the current view that where already realized.
275 void W_MapSubviews(W_View * view)
277 XMapSubwindows(view->screen->display, view->window);
278 XFlush(view->screen->display);
280 view = view->childrenList;
281 while (view) {
282 view->flags.mapped = 1;
283 view->flags.mapWhenRealized = 0;
284 view = view->nextSister;
288 void W_UnmapSubviews(W_View * view)
290 XUnmapSubwindows(view->screen->display, view->window);
291 XFlush(view->screen->display);
293 view = view->childrenList;
294 while (view) {
295 view->flags.mapped = 0;
296 view->flags.mapWhenRealized = 0;
297 view = view->nextSister;
301 void W_UnmapView(W_View * view)
303 view->flags.mapWhenRealized = 0;
304 if (!view->flags.mapped)
305 return;
307 XUnmapWindow(view->screen->display, view->window);
308 XFlush(view->screen->display);
310 view->flags.mapped = 0;
313 W_View *W_TopLevelOfView(W_View * view)
315 W_View *toplevel;
317 for (toplevel = view; toplevel && !toplevel->flags.topLevel; toplevel = toplevel->parent) ;
319 return toplevel;
322 static void destroyView(W_View * view)
324 W_View *ptr;
326 if (view->flags.alreadyDead)
327 return;
328 view->flags.alreadyDead = 1;
330 /* delete the balloon text for the view, if there's any */
331 WMSetBalloonTextForView(NULL, view);
333 if (view->nextFocusChain)
334 view->nextFocusChain->prevFocusChain = view->prevFocusChain;
335 if (view->prevFocusChain)
336 view->prevFocusChain->nextFocusChain = view->nextFocusChain;
338 /* Do not leave focus in a inexisting control */
339 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view)) == view)
340 W_SetFocusOfTopLevel(W_TopLevelOfView(view), NULL);
342 if (view->flags.topLevel) {
343 W_FocusInfo *info = view->screen->focusInfo;
344 /* remove focus information associated to this toplevel */
346 if (info) {
347 if (info->toplevel == view) {
348 view->screen->focusInfo = info->next;
349 wfree(info);
350 } else {
351 while (info->next) {
352 if (info->next->toplevel == view)
353 break;
354 info = info->next;
356 if (info->next) {
357 W_FocusInfo *next = info->next->next;
358 wfree(info->next);
359 info->next = next;
361 /* else the toplevel did not have any focused subview */
366 /* destroy children recursively */
367 while (view->childrenList != NULL) {
368 ptr = view->childrenList;
369 ptr->flags.parentDying = 1;
371 W_DestroyView(ptr);
373 if (ptr == view->childrenList) {
374 view->childrenList = ptr->nextSister;
375 ptr->parent = NULL;
379 W_CallDestroyHandlers(view);
381 if (view->flags.realized) {
382 XDeleteContext(view->screen->display, view->window, ViewContext);
384 /* if parent is being destroyed, it will die naturaly */
385 if (!view->flags.parentDying || view->flags.topLevel)
386 XDestroyWindow(view->screen->display, view->window);
389 /* remove self from parent's children list */
390 unparentView(view);
392 /* the array has a wfree() destructor that will be called automatically */
393 WMFreeArray(view->eventHandlers);
394 view->eventHandlers = NULL;
396 WMRemoveNotificationObserver(view);
398 W_FreeViewXdndPart(view);
400 if (view->backColor)
401 WMReleaseColor(view->backColor);
403 wfree(view);
406 void W_DestroyView(W_View * view)
408 view->refCount--;
410 if (view->refCount < 1) {
411 destroyView(view);
415 void W_MoveView(W_View * view, int x, int y)
417 assert(view->flags.root == 0);
419 if (view->delegate && view->delegate->willMove) {
420 (*view->delegate->willMove) (view->delegate, view, &x, &y);
423 if (view->pos.x == x && view->pos.y == y)
424 return;
426 if (view->flags.realized) {
427 XMoveWindow(view->screen->display, view->window, x, y);
429 view->pos.x = x;
430 view->pos.y = y;
432 if (view->delegate && view->delegate->didMove) {
433 (*view->delegate->didMove) (view->delegate, view);
437 void W_ResizeView(W_View * view, unsigned int width, unsigned int height)
439 /*int shrinked; */
441 if (view->delegate && view->delegate->willResize) {
442 (*view->delegate->willResize) (view->delegate, view, &width, &height);
445 assert(width > 0);
446 assert(height > 0);
448 if (view->size.width == width && view->size.height == height)
449 return;
451 /*shrinked = width < view->size.width || height < view->size.height; */
453 if (view->flags.realized) {
454 XResizeWindow(view->screen->display, view->window, width, height);
456 view->size.width = width;
457 view->size.height = height;
459 if (view->delegate && view->delegate->didResize) {
460 (*view->delegate->didResize) (view->delegate, view);
463 /* // TODO. replace in WINGs code, with the didResize delegate */
464 if (view->flags.notifySizeChanged)
465 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
468 void W_RedisplayView(W_View * view)
470 XEvent ev;
472 if (!view->flags.mapped)
473 return;
475 ev.xexpose.type = Expose;
476 ev.xexpose.display = view->screen->display;
477 ev.xexpose.window = view->window;
478 ev.xexpose.count = 0;
480 ev.xexpose.serial = 0;
482 WMHandleEvent(&ev);
485 void W_SetViewBackgroundColor(W_View * view, WMColor * color)
487 if (view->backColor)
488 WMReleaseColor(view->backColor);
489 view->backColor = WMRetainColor(color);
491 view->attribFlags |= CWBackPixel;
492 view->attribs.background_pixel = W_PIXEL(color);
493 if (view->flags.realized) {
494 XSetWindowBackground(view->screen->display, view->window, W_PIXEL(color));
495 XClearWindow(view->screen->display, view->window);
499 void W_SetViewCursor(W_View * view, Cursor cursor)
501 view->cursor = cursor;
502 if (W_VIEW_REALIZED(view)) {
503 XDefineCursor(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view), cursor);
504 } else {
505 view->attribFlags |= CWCursor;
506 view->attribs.cursor = cursor;
510 W_View *W_FocusedViewOfToplevel(W_View * view)
512 WMScreen *scr = view->screen;
513 W_FocusInfo *info;
515 for (info = scr->focusInfo; info != NULL; info = info->next)
516 if (view == info->toplevel)
517 break;
519 if (!info)
520 return NULL;
522 return info->focused;
525 void W_SetFocusOfTopLevel(W_View * toplevel, W_View * view)
527 WMScreen *scr = toplevel->screen;
528 XEvent event;
529 W_FocusInfo *info;
531 for (info = scr->focusInfo; info != NULL; info = info->next)
532 if (toplevel == info->toplevel)
533 break;
535 if (!info) {
536 info = wmalloc(sizeof(W_FocusInfo));
537 info->toplevel = toplevel;
538 info->focused = view;
539 info->next = scr->focusInfo;
540 scr->focusInfo = info;
541 } else {
542 event.xfocus.mode = NotifyNormal;
543 event.xfocus.detail = NotifyDetailNone;
544 if (info->focused) {
545 /* simulate FocusOut event */
546 event.xfocus.type = FocusOut;
547 W_DispatchMessage(info->focused, &event);
549 info->focused = view;
551 if (view) {
552 /* simulate FocusIn event */
553 event.xfocus.type = FocusIn;
554 W_DispatchMessage(view, &event);
558 void W_BroadcastMessage(W_View * targetParent, XEvent * event)
560 W_View *target;
562 target = targetParent->childrenList;
563 while (target != NULL) {
564 W_DispatchMessage(target, event);
566 target = target->nextSister;
570 void W_DispatchMessage(W_View * target, XEvent * event)
572 if (target->window == None)
573 return;
574 event->xclient.window = target->window;
575 event->xclient.display = target->screen->display;
577 WMHandleEvent(event);
579 XSendEvent(target->screen->display, target->window, False,
580 SubstructureNotifyMask, event);
584 WMView *W_RetainView(WMView * view)
586 view->refCount++;
588 return view;
591 void W_ReleaseView(WMView * view)
593 view->refCount--;
595 if (view->refCount < 1) {
596 destroyView(view);
600 WMWidget *WMWidgetOfView(WMView * view)
602 return view->self;
605 WMSize WMGetViewSize(WMView * view)
607 return view->size;
610 WMPoint WMGetViewPosition(WMView * view)
612 return view->pos;
615 void WMSetViewNotifySizeChanges(WMView * view, Bool flag)
617 view->flags.notifySizeChanged = ((flag == 0) ? 0 : 1);
620 Window WMViewXID(WMView * view)
622 return view->window;
625 WMPoint WMGetViewScreenPosition(WMView * view)
627 WMScreen *scr = W_VIEW_SCREEN(view);
628 Window foo;
629 int x, y, topX, topY;
630 unsigned int bar;
631 WMView *topView;
633 topView = view;
634 while (topView->parent && topView->parent != scr->rootView)
635 topView = topView->parent;
637 if (!XGetGeometry(scr->display, W_VIEW_DRAWABLE(topView), &foo, &topX, &topY, &bar, &bar, &bar, &bar)) {
638 topX = topY = 0;
641 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(view), scr->rootWin, 0, 0, &x, &y, &foo);
643 return wmkpoint(x - topX, y - topY);
646 static void resizedParent(void *self, WMNotification * notif)
648 WMSize size = WMGetViewSize((WMView *) WMGetNotificationObject(notif));
649 WMView *view = (WMView *) self;
651 W_MoveView(view, view->leftOffs, view->topOffs);
652 W_ResizeView(view, size.width - (view->leftOffs + view->rightOffs),
653 size.height - (view->topOffs + view->bottomOffs));
656 void WMSetViewExpandsToParent(WMView * view, int leftOffs, int topOffs, int rightOffs, int bottomOffs)
658 WMSize size = view->parent->size;
660 view->topOffs = topOffs;
661 view->bottomOffs = bottomOffs;
662 view->leftOffs = leftOffs;
663 view->rightOffs = rightOffs;
665 WMAddNotificationObserver(resizedParent, view, WMViewSizeDidChangeNotification, view->parent);
666 WMSetViewNotifySizeChanges(view->parent, True);
668 W_MoveView(view, leftOffs, topOffs);
669 W_ResizeView(view, size.width - (leftOffs + rightOffs), size.height - (topOffs + bottomOffs));