started Appearance update in WPrefs
[wmaker-crm.git] / WINGs / wview.c
blobb90d249d92e011feeb04d49c6a99b38c240b5364
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->pos.x == x && view->pos.y == y)
443 return;
445 if (view->flags.realized) {
446 XMoveWindow(view->screen->display, view->window, x, y);
448 view->pos.x = x;
449 view->pos.y = y;
453 void
454 W_ResizeView(W_View *view, unsigned int width, unsigned int height)
456 int shrinked;
458 assert(width > 0);
459 assert(height > 0);
461 if (view->size.width == width && view->size.height == height)
462 return;
464 shrinked = width < view->size.width || height < view->size.height;
466 if (view->flags.realized) {
467 XResizeWindow(view->screen->display, view->window, width, height);
469 view->size.width = width;
470 view->size.height = height;
472 if (view->flags.notifySizeChanged)
473 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
477 void
478 W_RedisplayView(W_View *view)
480 XEvent ev;
482 if (!view->flags.mapped)
483 return;
485 ev.xexpose.type = Expose;
486 ev.xexpose.display = view->screen->display;
487 ev.xexpose.window = view->window;
488 ev.xexpose.count = 0;
490 WMHandleEvent(&ev);
494 void
495 W_SetViewBackgroundColor(W_View *view, WMColor *color)
497 if (view->backColor)
498 WMReleaseColor(view->backColor);
499 view->backColor = WMRetainColor(color);
501 view->attribFlags |= CWBackPixel;
502 view->attribs.background_pixel = color->color.pixel;
503 if (view->flags.realized) {
504 XSetWindowBackground(view->screen->display, view->window,
505 color->color.pixel);
506 XClearWindow(view->screen->display, view->window);
512 W_View*
513 W_FocusedViewOfToplevel(W_View *view)
515 WMScreen *scr = view->screen;
516 W_FocusInfo *info;
518 for (info = scr->focusInfo; info!=NULL; info = info->next)
519 if (view == info->toplevel)
520 break;
522 if (!info)
523 return NULL;
525 return info->focused;
529 void
530 W_SetFocusOfTopLevel(W_View *toplevel, W_View *view)
532 WMScreen *scr = toplevel->screen;
533 XEvent event;
534 W_FocusInfo *info;
536 for (info = scr->focusInfo; info!=NULL; info = info->next)
537 if (toplevel == info->toplevel)
538 break;
540 if (!info) {
541 info = wmalloc(sizeof(W_FocusInfo));
542 info->toplevel = toplevel;
543 info->focused = view;
544 info->next = scr->focusInfo;
545 scr->focusInfo = info;
546 } else {
547 event.xfocus.mode = NotifyNormal;
548 event.xfocus.detail = NotifyDetailNone;
549 if (info->focused) {
550 /* simulate FocusOut event */
551 event.xfocus.type = FocusOut;
552 W_DispatchMessage(info->focused, &event);
554 info->focused = view;
556 if (view) {
557 /* simulate FocusIn event */
558 event.xfocus.type = FocusIn;
559 W_DispatchMessage(view, &event);
564 void
565 W_BroadcastMessage(W_View *targetParent, XEvent *event)
567 W_View *target;
569 target = targetParent->childrenList;
570 while (target!=NULL) {
571 W_DispatchMessage(target, event);
573 target = target->nextSister;
578 void
579 W_DispatchMessage(W_View *target, XEvent *event)
581 if (target->window==None)
582 return;
583 event->xclient.window = target->window;
584 event->xclient.display = target->screen->display;
586 WMHandleEvent(event);
588 XSendEvent(target->screen->display, target->window, False,
589 SubstructureNotifyMask, event);
595 WMView*
596 W_RetainView(WMView *view)
598 view->refCount++;
599 return view;
604 void
605 W_ReleaseView(WMView *view)
607 view->refCount--;
608 if (view->refCount < 1) {
609 destroyView(view);
614 WMWidget*
615 WMWidgetOfView(WMView *view)
617 return view->self;
621 WMSize
622 WMGetViewSize(WMView *view)
624 return view->size;
627 WMPoint
628 WMGetViewPosition(WMView *view)
630 return view->pos;
634 void
635 WMSetViewNotifySizeChanges(WMView *view, Bool flag)
637 view->flags.notifySizeChanged = flag;
640 Window
641 WMViewXID(WMView *view)
643 return view->window;