fixed some stuffs
[wmaker-crm.git] / WINGs / wview.c
blob6d42a89438ae8877aa595c6c0e3851161e05fafa
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 = WMCreateBag(4);
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 W_CleanUpEvents(view);
446 WMFreeBag(view->eventHandlers);
447 view->eventHandlers = NULL;
449 WMUnregisterViewDraggedTypes(view);
451 #if 0
452 if (view->dragSourceProcs)
453 wfree(view->dragSourceProcs);
455 if (view->dragDestinationProcs)
456 wfree(view->dragDestinationProcs);
458 if (scr->dragInfo.destView == view) {
459 scr->dragInfo.destView = NULL;
461 #endif
462 wfree(view);
467 void
468 W_DestroyView(W_View *view)
470 view->refCount--;
472 if (view->refCount < 1) {
473 destroyView(view);
479 void
480 W_MoveView(W_View *view, int x, int y)
482 assert(view->flags.root==0);
484 if (view->delegate && view->delegate->willMove) {
485 (*view->delegate->willMove)(view->delegate, view, &x, &y);
488 if (view->pos.x == x && view->pos.y == y)
489 return;
491 if (view->flags.realized) {
492 XMoveWindow(view->screen->display, view->window, x, y);
494 view->pos.x = x;
495 view->pos.y = y;
497 if (view->delegate && view->delegate->didMove) {
498 (*view->delegate->didMove)(view->delegate, view);
503 void
504 W_ResizeView(W_View *view, unsigned int width, unsigned int height)
506 int shrinked;
508 if (view->delegate && view->delegate->willResize) {
509 (*view->delegate->willResize)(view->delegate, view, &width, &height);
512 assert(width > 0);
513 assert(height > 0);
515 if (view->size.width == width && view->size.height == height)
516 return;
518 shrinked = width < view->size.width || height < view->size.height;
520 if (view->flags.realized) {
521 XResizeWindow(view->screen->display, view->window, width, height);
523 view->size.width = width;
524 view->size.height = height;
526 if (view->delegate && view->delegate->didResize) {
527 (*view->delegate->didResize)(view->delegate, view);
530 if (view->flags.notifySizeChanged)
531 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
535 void
536 W_RedisplayView(W_View *view)
538 XEvent ev;
540 if (!view->flags.mapped)
541 return;
543 ev.xexpose.type = Expose;
544 ev.xexpose.display = view->screen->display;
545 ev.xexpose.window = view->window;
546 ev.xexpose.count = 0;
548 ev.xexpose.serial = 0;
550 WMHandleEvent(&ev);
554 void
555 W_SetViewBackgroundColor(W_View *view, WMColor *color)
557 if (view->backColor)
558 WMReleaseColor(view->backColor);
559 view->backColor = WMRetainColor(color);
561 view->attribFlags |= CWBackPixel;
562 view->attribs.background_pixel = color->color.pixel;
563 if (view->flags.realized) {
564 XSetWindowBackground(view->screen->display, view->window,
565 color->color.pixel);
566 XClearWindow(view->screen->display, view->window);
571 void
572 W_SetViewCursor(W_View *view, Cursor cursor)
574 view->cursor = cursor;
575 if (W_VIEW_REALIZED(view)) {
576 XDefineCursor(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view), cursor);
577 } else {
578 view->attribFlags |= CWCursor;
579 view->attribs.cursor = cursor;
584 W_View*
585 W_FocusedViewOfToplevel(W_View *view)
587 WMScreen *scr = view->screen;
588 W_FocusInfo *info;
590 for (info = scr->focusInfo; info!=NULL; info = info->next)
591 if (view == info->toplevel)
592 break;
594 if (!info)
595 return NULL;
597 return info->focused;
601 void
602 W_SetFocusOfTopLevel(W_View *toplevel, W_View *view)
604 WMScreen *scr = toplevel->screen;
605 XEvent event;
606 W_FocusInfo *info;
608 for (info = scr->focusInfo; info!=NULL; info = info->next)
609 if (toplevel == info->toplevel)
610 break;
612 if (!info) {
613 info = wmalloc(sizeof(W_FocusInfo));
614 info->toplevel = toplevel;
615 info->focused = view;
616 info->next = scr->focusInfo;
617 scr->focusInfo = info;
618 } else {
619 event.xfocus.mode = NotifyNormal;
620 event.xfocus.detail = NotifyDetailNone;
621 if (info->focused) {
622 /* simulate FocusOut event */
623 event.xfocus.type = FocusOut;
624 W_DispatchMessage(info->focused, &event);
626 info->focused = view;
628 if (view) {
629 /* simulate FocusIn event */
630 event.xfocus.type = FocusIn;
631 W_DispatchMessage(view, &event);
636 void
637 W_BroadcastMessage(W_View *targetParent, XEvent *event)
639 W_View *target;
641 target = targetParent->childrenList;
642 while (target!=NULL) {
643 W_DispatchMessage(target, event);
645 target = target->nextSister;
650 void
651 W_DispatchMessage(W_View *target, XEvent *event)
653 if (target->window==None)
654 return;
655 event->xclient.window = target->window;
656 event->xclient.display = target->screen->display;
658 WMHandleEvent(event);
660 XSendEvent(target->screen->display, target->window, False,
661 SubstructureNotifyMask, event);
667 WMView*
668 W_RetainView(WMView *view)
670 view->refCount++;
672 return view;
676 void
677 W_ReleaseView(WMView *view)
679 view->refCount--;
681 if (view->refCount < 1) {
682 destroyView(view);
687 WMWidget*
688 WMWidgetOfView(WMView *view)
690 return view->self;
694 WMSize
695 WMGetViewSize(WMView *view)
697 return view->size;
700 WMPoint
701 WMGetViewPosition(WMView *view)
703 return view->pos;
707 void
708 WMSetViewNotifySizeChanges(WMView *view, Bool flag)
710 view->flags.notifySizeChanged = flag;
714 Window
715 WMViewXID(WMView *view)
717 return view->window;
721 WMPoint
722 WMGetViewScreenPosition(WMView *view)
724 WMScreen *scr = W_VIEW_SCREEN(view);
725 Window foo;
726 int x, y;
728 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(view),
729 scr->rootWin, 0, 0, &x, &y, &foo);
731 return wmkpoint(x, y);