reverted to KeepInsideScreen
[wmaker-crm.git] / WINGs / wview.c
blob0e2be5b6d7fe51c3a6550b8f2a50440e373ba954
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->refCount = 1;
118 view->screen = screen;
120 if (parent!=NULL) {
121 /* attributes are not valid for root window */
122 view->attribFlags = CWEventMask|CWBitGravity;
123 view->attribs = defAtts;
125 view->attribFlags |= CWBackPixel|CWColormap|CWBorderPixel;
126 view->attribs.background_pixel = W_PIXEL(screen->gray);
127 view->attribs.border_pixel = W_PIXEL(screen->black);
128 view->attribs.colormap = screen->colormap;
130 view->backColor = WMRetainColor(screen->gray);
132 adoptChildView(parent, view);
135 return view;
140 W_View*
141 W_CreateView(W_View *parent)
143 return createView(parent->screen, parent);
147 W_View*
148 W_CreateRootView(W_Screen *screen)
150 W_View *view;
152 view = createView(screen, NULL);
154 view->window = screen->rootWin;
156 view->flags.realized = 1;
157 view->flags.mapped = 1;
158 view->flags.root = 1;
160 view->size.width =
161 WidthOfScreen(ScreenOfDisplay(screen->display, screen->screen));
162 view->size.height =
163 HeightOfScreen(ScreenOfDisplay(screen->display, screen->screen));
165 return view;
169 W_View*
170 W_CreateTopView(W_Screen *screen)
172 W_View *view;
174 view = createView(screen, screen->rootView);
175 if (!view)
176 return NULL;
178 view->flags.topLevel = 1;
179 view->attribs.event_mask |= StructureNotifyMask;
181 return view;
186 void
187 W_RealizeView(W_View *view)
189 Window parentWID;
190 Display *dpy = view->screen->display;
191 W_View *ptr;
193 assert(view->size.width > 0);
194 assert(view->size.height > 0);
197 if (view->parent && !view->parent->flags.realized) {
198 wwarning("trying to realize widget of unrealized parent");
199 return;
202 if (!view->flags.realized) {
203 parentWID = view->parent->window;
204 view->window = XCreateWindow(dpy, parentWID, view->pos.x, view->pos.y,
205 view->size.width, view->size.height, 0,
206 view->screen->depth, InputOutput,
207 view->screen->visual, view->attribFlags,
208 &view->attribs);
210 XSaveContext(dpy, view->window, ViewContext, (XPointer)view);
212 view->flags.realized = 1;
214 if (view->flags.mapWhenRealized) {
215 W_MapView(view);
216 view->flags.mapWhenRealized = 0;
219 WMPostNotificationName(WMViewRealizedNotification, view, NULL);
222 /* realize children */
223 ptr = view->childrenList;
224 while (ptr!=NULL) {
225 W_RealizeView(ptr);
227 ptr = ptr->nextSister;
233 Bool
234 W_CheckInternalMessage(W_Screen *scr, XClientMessageEvent *cev, int event)
236 if (cev->message_type == scr->internalMessage
237 && cev->format == 32 && cev->data.l[1] == event)
238 return True;
239 else
240 return False;
244 void
245 W_ReparentView(W_View *view, W_View *newParent, int x, int y)
247 Display *dpy = view->screen->display;
249 assert(!view->flags.topLevel);
251 unparentView(view);
252 adoptChildView(newParent, view);
254 if (view->flags.realized) {
255 if (newParent->flags.realized) {
256 XReparentWindow(dpy, view->window, newParent->window, x, y);
257 } else {
258 wwarning("trying to reparent realized view to unrealized parent");
259 return;
263 view->pos.x = x;
264 view->pos.y = y;
269 void
270 W_MapView(W_View *view)
272 if (!view->flags.mapped) {
273 if (view->flags.realized) {
274 XMapRaised(view->screen->display, view->window);
275 XFlush(view->screen->display);
276 view->flags.mapped = 1;
277 } else {
278 view->flags.mapWhenRealized = 1;
285 * W_MapSubviews-
286 * maps all children of the current view that where already realized.
288 void
289 W_MapSubviews(W_View *view)
291 XMapSubwindows(view->screen->display, view->window);
292 XFlush(view->screen->display);
294 view = view->childrenList;
295 while (view) {
296 view->flags.mapped = 1;
297 view->flags.mapWhenRealized = 0;
298 view = view->nextSister;
304 void
305 W_UnmapSubviews(W_View *view)
307 XUnmapSubwindows(view->screen->display, view->window);
308 XFlush(view->screen->display);
310 view = view->childrenList;
311 while (view) {
312 view->flags.mapped = 0;
313 view->flags.mapWhenRealized = 0;
314 view = view->nextSister;
320 void
321 W_UnmapView(W_View *view)
323 view->flags.mapWhenRealized = 0;
324 if (!view->flags.mapped)
325 return;
327 XUnmapWindow(view->screen->display, view->window);
328 XFlush(view->screen->display);
330 view->flags.mapped = 0;
334 W_View*
335 W_TopLevelOfView(W_View *view)
337 W_View *toplevel;
339 for (toplevel=view; !toplevel->flags.topLevel; toplevel=toplevel->parent);
341 return toplevel;
345 static void
346 destroyView(W_View *view)
348 W_View *ptr;
350 if (view->flags.alreadyDead)
351 return;
352 view->flags.alreadyDead = 1;
354 /* delete the balloon text for the view, if there's any */
355 WMSetBalloonTextForView(NULL, view);
357 if (view->nextFocusChain)
358 view->nextFocusChain->prevFocusChain = view->prevFocusChain;
359 if (view->prevFocusChain)
360 view->prevFocusChain->nextFocusChain = view->nextFocusChain;
362 /* Do not leave focus in a inexisting control */
363 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view))==view)
364 W_SetFocusOfTopLevel(W_TopLevelOfView(view), NULL);
366 if (view->flags.topLevel) {
367 W_FocusInfo *info = view->screen->focusInfo;
368 /* remove focus information associated to this toplevel */
370 if (info) {
371 if (info->toplevel==view) {
372 view->screen->focusInfo = info->next;
373 free(info);
374 } else {
375 while (info->next) {
376 if (info->next->toplevel == view)
377 break;
378 info = info->next;
380 if (info->next) {
381 W_FocusInfo *next = info->next->next;
382 free(info->next);
383 info->next = next;
385 /* else the toplevel did not have any focused subview */
390 /* destroy children recursively */
391 while (view->childrenList!=NULL) {
392 ptr = view->childrenList;
393 ptr->flags.parentDying = 1;
395 W_DestroyView(ptr);
397 if (ptr == view->childrenList) {
398 view->childrenList = ptr->nextSister;
399 ptr->parent = NULL;
403 W_CallDestroyHandlers(view);
405 if (view->flags.realized) {
406 XDeleteContext(view->screen->display, view->window, ViewContext);
408 /* if parent is being destroyed, it will die naturaly */
409 if (!view->flags.parentDying || view->flags.topLevel)
410 XDestroyWindow(view->screen->display, view->window);
413 /* remove self from parent's children list */
414 unparentView(view);
416 W_CleanUpEvents(view);
417 #if 0
418 if (view->dragSourceProcs)
419 free(view->dragSourceProcs);
421 if (view->dragDestinationProcs)
422 free(view->dragDestinationProcs);
423 #endif
424 free(view);
429 void
430 W_DestroyView(W_View *view)
432 W_ReleaseView(view);
437 void
438 W_MoveView(W_View *view, int x, int y)
440 assert(view->flags.root==0);
442 if (view->delegate && view->delegate->willMove) {
443 (*view->delegate->willMove)(view->delegate, view, &x, &y);
446 if (view->pos.x == x && view->pos.y == y)
447 return;
449 if (view->flags.realized) {
450 XMoveWindow(view->screen->display, view->window, x, y);
452 view->pos.x = x;
453 view->pos.y = y;
455 if (view->delegate && view->delegate->didMove) {
456 (*view->delegate->didMove)(view->delegate, view);
461 void
462 W_ResizeView(W_View *view, unsigned int width, unsigned int height)
464 int shrinked;
466 if (view->delegate && view->delegate->willResize) {
467 (*view->delegate->willResize)(view->delegate, view, &width, &height);
470 assert(width > 0);
471 assert(height > 0);
473 if (view->size.width == width && view->size.height == height)
474 return;
476 shrinked = width < view->size.width || height < view->size.height;
478 if (view->flags.realized) {
479 XResizeWindow(view->screen->display, view->window, width, height);
481 view->size.width = width;
482 view->size.height = height;
484 if (view->delegate && view->delegate->didResize) {
485 (*view->delegate->didResize)(view->delegate, view);
488 if (view->flags.notifySizeChanged)
489 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
493 void
494 W_RedisplayView(W_View *view)
496 XEvent ev;
498 if (!view->flags.mapped)
499 return;
501 ev.xexpose.type = Expose;
502 ev.xexpose.display = view->screen->display;
503 ev.xexpose.window = view->window;
504 ev.xexpose.count = 0;
506 WMHandleEvent(&ev);
510 void
511 W_SetViewBackgroundColor(W_View *view, WMColor *color)
513 if (view->backColor)
514 WMReleaseColor(view->backColor);
515 view->backColor = WMRetainColor(color);
517 view->attribFlags |= CWBackPixel;
518 view->attribs.background_pixel = color->color.pixel;
519 if (view->flags.realized) {
520 XSetWindowBackground(view->screen->display, view->window,
521 color->color.pixel);
522 XClearWindow(view->screen->display, view->window);
528 W_View*
529 W_FocusedViewOfToplevel(W_View *view)
531 WMScreen *scr = view->screen;
532 W_FocusInfo *info;
534 for (info = scr->focusInfo; info!=NULL; info = info->next)
535 if (view == info->toplevel)
536 break;
538 if (!info)
539 return NULL;
541 return info->focused;
545 void
546 W_SetFocusOfTopLevel(W_View *toplevel, W_View *view)
548 WMScreen *scr = toplevel->screen;
549 XEvent event;
550 W_FocusInfo *info;
552 for (info = scr->focusInfo; info!=NULL; info = info->next)
553 if (toplevel == info->toplevel)
554 break;
556 if (!info) {
557 info = wmalloc(sizeof(W_FocusInfo));
558 info->toplevel = toplevel;
559 info->focused = view;
560 info->next = scr->focusInfo;
561 scr->focusInfo = info;
562 } else {
563 event.xfocus.mode = NotifyNormal;
564 event.xfocus.detail = NotifyDetailNone;
565 if (info->focused) {
566 /* simulate FocusOut event */
567 event.xfocus.type = FocusOut;
568 W_DispatchMessage(info->focused, &event);
570 info->focused = view;
572 if (view) {
573 /* simulate FocusIn event */
574 event.xfocus.type = FocusIn;
575 W_DispatchMessage(view, &event);
580 void
581 W_BroadcastMessage(W_View *targetParent, XEvent *event)
583 W_View *target;
585 target = targetParent->childrenList;
586 while (target!=NULL) {
587 W_DispatchMessage(target, event);
589 target = target->nextSister;
594 void
595 W_DispatchMessage(W_View *target, XEvent *event)
597 if (target->window==None)
598 return;
599 event->xclient.window = target->window;
600 event->xclient.display = target->screen->display;
602 WMHandleEvent(event);
604 XSendEvent(target->screen->display, target->window, False,
605 SubstructureNotifyMask, event);
611 WMView*
612 W_RetainView(WMView *view)
614 view->refCount++;
615 return view;
620 void
621 W_ReleaseView(WMView *view)
623 view->refCount--;
624 if (view->refCount < 1) {
625 destroyView(view);
630 WMWidget*
631 WMWidgetOfView(WMView *view)
633 return view->self;
637 WMSize
638 WMGetViewSize(WMView *view)
640 return view->size;
643 WMPoint
644 WMGetViewPosition(WMView *view)
646 return view->pos;
650 void
651 WMSetViewNotifySizeChanges(WMView *view, Bool flag)
653 view->flags.notifySizeChanged = flag;
656 Window
657 WMViewXID(WMView *view)
659 return view->window;