- made deiconification not automatically focus window in sloppy focus
[wmaker-crm.git] / WINGs / wview.c
blob325a34886c41a09d9d68bc633a6874ea88dd85dc
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 adoptChildView(parent, view);
133 return view;
138 W_View*
139 W_CreateView(W_View *parent)
141 return createView(parent->screen, parent);
145 W_View*
146 W_CreateRootView(W_Screen *screen)
148 W_View *view;
150 view = createView(screen, NULL);
152 view->window = screen->rootWin;
154 view->flags.realized = 1;
155 view->flags.mapped = 1;
156 view->flags.root = 1;
158 view->size.width =
159 WidthOfScreen(ScreenOfDisplay(screen->display, screen->screen));
160 view->size.height =
161 HeightOfScreen(ScreenOfDisplay(screen->display, screen->screen));
163 return view;
167 W_View*
168 W_CreateTopView(W_Screen *screen)
170 W_View *view;
172 view = createView(screen, screen->rootView);
173 if (!view)
174 return NULL;
176 view->flags.topLevel = 1;
177 view->attribs.event_mask |= StructureNotifyMask;
179 return view;
184 void
185 W_RealizeView(W_View *view)
187 Window parentWID;
188 Display *dpy = view->screen->display;
189 W_View *ptr;
191 assert(view->size.width > 0);
192 assert(view->size.height > 0);
195 if (view->parent && !view->parent->flags.realized) {
196 wwarning("trying to realize widget of unrealized parent");
197 return;
200 if (!view->flags.realized) {
201 parentWID = view->parent->window;
202 view->window = XCreateWindow(dpy, parentWID, view->pos.x, view->pos.y,
203 view->size.width, view->size.height, 0,
204 view->screen->depth, InputOutput,
205 view->screen->visual, view->attribFlags,
206 &view->attribs);
208 XSaveContext(dpy, view->window, ViewContext, (XPointer)view);
210 view->flags.realized = 1;
212 if (view->flags.mapWhenRealized) {
213 W_MapView(view);
214 view->flags.mapWhenRealized = 0;
217 WMPostNotificationName(WMViewRealizedNotification, view, NULL);
220 /* realize children */
221 ptr = view->childrenList;
222 while (ptr!=NULL) {
223 W_RealizeView(ptr);
225 ptr = ptr->nextSister;
231 Bool
232 W_CheckInternalMessage(W_Screen *scr, XClientMessageEvent *cev, int event)
234 if (cev->message_type == scr->internalMessage
235 && cev->format == 32 && cev->data.l[1] == event)
236 return True;
237 else
238 return False;
242 void
243 W_ReparentView(W_View *view, W_View *newParent, int x, int y)
245 Display *dpy = view->screen->display;
247 assert(!view->flags.topLevel);
249 unparentView(view);
250 adoptChildView(newParent, view);
252 if (view->flags.realized) {
253 if (newParent->flags.realized) {
254 XReparentWindow(dpy, view->window, newParent->window, x, y);
255 } else {
256 wwarning("trying to reparent realized view to unrealized parent");
257 return;
261 view->pos.x = x;
262 view->pos.y = y;
267 void
268 W_MapView(W_View *view)
270 if (!view->flags.mapped) {
271 if (view->flags.realized) {
272 XMapRaised(view->screen->display, view->window);
273 XFlush(view->screen->display);
274 view->flags.mapped = 1;
275 } else {
276 view->flags.mapWhenRealized = 1;
283 * W_MapSubviews-
284 * maps all children of the current view that where already realized.
286 void
287 W_MapSubviews(W_View *view)
289 XMapSubwindows(view->screen->display, view->window);
290 XFlush(view->screen->display);
292 view = view->childrenList;
293 while (view) {
294 view->flags.mapped = 1;
295 view->flags.mapWhenRealized = 0;
296 view = view->nextSister;
302 void
303 W_UnmapSubviews(W_View *view)
305 XUnmapSubwindows(view->screen->display, view->window);
306 XFlush(view->screen->display);
308 view = view->childrenList;
309 while (view) {
310 view->flags.mapped = 0;
311 view->flags.mapWhenRealized = 0;
312 view = view->nextSister;
318 void
319 W_UnmapView(W_View *view)
321 view->flags.mapWhenRealized = 0;
322 if (!view->flags.mapped)
323 return;
325 XUnmapWindow(view->screen->display, view->window);
326 XFlush(view->screen->display);
328 view->flags.mapped = 0;
332 W_View*
333 W_TopLevelOfView(W_View *view)
335 W_View *toplevel;
337 for (toplevel=view; !toplevel->flags.topLevel; toplevel=toplevel->parent);
339 return toplevel;
343 static void
344 destroyView(W_View *view)
346 W_View *ptr;
348 if (view->flags.alreadyDead)
349 return;
350 view->flags.alreadyDead = 1;
352 if (view->nextFocusChain)
353 view->nextFocusChain->prevFocusChain = view->prevFocusChain;
354 if (view->prevFocusChain)
355 view->prevFocusChain->nextFocusChain = view->nextFocusChain;
357 /* Do not leave focus in a inexisting control */
358 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view))==view)
359 W_SetFocusOfTopLevel(W_TopLevelOfView(view), NULL);
361 if (view->flags.topLevel) {
362 W_FocusInfo *info = view->screen->focusInfo;
363 /* remove focus information associated to this toplevel */
365 if (info) {
366 if (info->toplevel==view) {
367 view->screen->focusInfo = info->next;
368 free(info);
369 } else {
370 while (info->next) {
371 if (info->next->toplevel == view)
372 break;
373 info = info->next;
375 if (info->next) {
376 W_FocusInfo *next = info->next->next;
377 free(info->next);
378 info->next = next;
380 /* else the toplevel did not have any focused subview */
385 /* destroy children recursively */
386 while (view->childrenList!=NULL) {
387 ptr = view->childrenList;
388 ptr->flags.parentDying = 1;
390 W_DestroyView(ptr);
392 if (ptr == view->childrenList) {
393 view->childrenList = ptr->nextSister;
394 ptr->parent = NULL;
398 W_CallDestroyHandlers(view);
400 if (view->flags.realized) {
401 XDeleteContext(view->screen->display, view->window, ViewContext);
403 /* if parent is being destroyed, it will die naturaly */
404 if (!view->flags.parentDying || view->flags.topLevel)
405 XDestroyWindow(view->screen->display, view->window);
408 /* remove self from parent's children list */
409 unparentView(view);
411 W_CleanUpEvents(view);
412 #if 0
413 if (view->dragSourceProcs)
414 free(view->dragSourceProcs);
416 if (view->dragDestinationProcs)
417 free(view->dragDestinationProcs);
418 #endif
419 free(view);
424 void
425 W_DestroyView(W_View *view)
427 W_ReleaseView(view);
432 void
433 W_MoveView(W_View *view, int x, int y)
435 assert(view->flags.root==0);
437 if (view->pos.x == x && view->pos.y == y)
438 return;
440 if (view->flags.realized) {
441 XMoveWindow(view->screen->display, view->window, x, y);
443 view->pos.x = x;
444 view->pos.y = y;
448 void
449 W_ResizeView(W_View *view, unsigned int width, unsigned int height)
451 int shrinked;
453 assert(width > 0);
454 assert(height > 0);
456 if (view->size.width == width && view->size.height == height)
457 return;
459 shrinked = width < view->size.width || height < view->size.height;
461 if (view->flags.realized) {
462 XResizeWindow(view->screen->display, view->window, width, height);
464 view->size.width = width;
465 view->size.height = height;
467 if (view->flags.notifySizeChanged)
468 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
472 void
473 W_RedisplayView(W_View *view)
475 XEvent ev;
477 if (!view->flags.mapped)
478 return;
480 ev.xexpose.type = Expose;
481 ev.xexpose.display = view->screen->display;
482 ev.xexpose.window = view->window;
483 ev.xexpose.count = 0;
485 WMHandleEvent(&ev);
489 void
490 W_SetViewBackgroundColor(W_View *view, WMColor *color)
492 view->attribFlags |= CWBackPixel;
493 view->attribs.background_pixel = color->color.pixel;
494 if (view->flags.realized) {
495 XSetWindowBackground(view->screen->display, view->window,
496 color->color.pixel);
497 XClearWindow(view->screen->display, view->window);
503 W_View*
504 W_FocusedViewOfToplevel(W_View *view)
506 WMScreen *scr = view->screen;
507 W_FocusInfo *info;
509 for (info = scr->focusInfo; info!=NULL; info = info->next)
510 if (view == info->toplevel)
511 break;
513 if (!info)
514 return NULL;
516 return info->focused;
520 void
521 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);
555 void
556 W_BroadcastMessage(W_View *targetParent, XEvent *event)
558 W_View *target;
560 target = targetParent->childrenList;
561 while (target!=NULL) {
562 W_DispatchMessage(target, event);
564 target = target->nextSister;
569 void
570 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);
586 WMView*
587 W_RetainView(WMView *view)
589 view->refCount++;
590 return view;
595 void
596 W_ReleaseView(WMView *view)
598 view->refCount--;
599 if (view->refCount < 1) {
600 destroyView(view);
605 WMWidget*
606 WMWidgetOfView(WMView *view)
608 return view->self;
612 WMSize
613 WMGetViewSize(WMView *view)
615 return view->size;
618 WMPoint
619 WMGetViewPosition(WMView *view)
621 return view->pos;
625 void
626 WMSetViewNotifySizeChanges(WMView *view, Bool flag)
628 view->flags.notifySizeChanged = flag;
631 Window
632 WMViewXID(WMView *view)
634 return view->window;