5 #include <X11/Xresource.h>
9 /* the notifications about views */
11 char *WMViewSizeDidChangeNotification
= "WMViewSizeDidChangeNotification";
12 char *WMViewFocusDidChangeNotification
= "WMViewFocusDidChangeNotification";
13 char *WMViewRealizedNotification
= "WMViewRealizedNotification";
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 */
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 */
43 static XContext ViewContext
=0; /* context for views */
49 W_GetViewForXWindow(Display
*display
, Window window
)
53 if (XFindContext(display
, window
, ViewContext
, (XPointer
*)&view
)==0) {
62 unparentView(W_View
*view
)
64 /* remove from parent's children list */
65 if (view
->parent
!=NULL
) {
68 ptr
= view
->parent
->childrenList
;
70 view
->parent
->childrenList
= view
->nextSister
;
73 if (ptr
->nextSister
== view
) {
74 ptr
->nextSister
= view
->nextSister
;
77 ptr
= ptr
->nextSister
;
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
;
96 ptr
= view
->childrenList
;
97 while (ptr
->nextSister
!=NULL
)
98 ptr
= ptr
->nextSister
;
99 ptr
->nextSister
= child
;
101 child
->parent
= view
;
106 createView(W_Screen
*screen
, W_View
*parent
)
111 ViewContext
= XUniqueContext();
113 view
= wmalloc(sizeof(W_View
));
114 memset(view
, 0, sizeof(W_View
));
118 view
->screen
= screen
;
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
);
139 W_CreateView(W_View
*parent
)
141 return createView(parent
->screen
, parent
);
146 W_CreateRootView(W_Screen
*screen
)
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;
159 WidthOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
161 HeightOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
168 W_CreateTopView(W_Screen
*screen
)
172 view
= createView(screen
, screen
->rootView
);
176 view
->flags
.topLevel
= 1;
177 view
->attribs
.event_mask
|= StructureNotifyMask
;
185 W_RealizeView(W_View
*view
)
188 Display
*dpy
= view
->screen
->display
;
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");
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
,
208 XSaveContext(dpy
, view
->window
, ViewContext
, (XPointer
)view
);
210 view
->flags
.realized
= 1;
212 if (view
->flags
.mapWhenRealized
) {
214 view
->flags
.mapWhenRealized
= 0;
217 WMPostNotificationName(WMViewRealizedNotification
, view
, NULL
);
220 /* realize children */
221 ptr
= view
->childrenList
;
225 ptr
= ptr
->nextSister
;
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
)
243 W_ReparentView(W_View
*view
, W_View
*newParent
, int x
, int y
)
245 Display
*dpy
= view
->screen
->display
;
247 assert(!view
->flags
.topLevel
);
250 adoptChildView(newParent
, view
);
252 if (view
->flags
.realized
) {
253 if (newParent
->flags
.realized
) {
254 XReparentWindow(dpy
, view
->window
, newParent
->window
, x
, y
);
256 wwarning("trying to reparent realized view to unrealized parent");
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;
276 view
->flags
.mapWhenRealized
= 1;
284 * maps all children of the current view that where already realized.
287 W_MapSubviews(W_View
*view
)
289 XMapSubwindows(view
->screen
->display
, view
->window
);
290 XFlush(view
->screen
->display
);
292 view
= view
->childrenList
;
294 view
->flags
.mapped
= 1;
295 view
->flags
.mapWhenRealized
= 0;
296 view
= view
->nextSister
;
303 W_UnmapSubviews(W_View
*view
)
305 XUnmapSubwindows(view
->screen
->display
, view
->window
);
306 XFlush(view
->screen
->display
);
308 view
= view
->childrenList
;
310 view
->flags
.mapped
= 0;
311 view
->flags
.mapWhenRealized
= 0;
312 view
= view
->nextSister
;
319 W_UnmapView(W_View
*view
)
321 view
->flags
.mapWhenRealized
= 0;
322 if (!view
->flags
.mapped
)
325 XUnmapWindow(view
->screen
->display
, view
->window
);
326 XFlush(view
->screen
->display
);
328 view
->flags
.mapped
= 0;
333 W_TopLevelOfView(W_View
*view
)
337 for (toplevel
=view
; !toplevel
->flags
.topLevel
; toplevel
=toplevel
->parent
);
344 destroyView(W_View
*view
)
348 if (view
->flags
.alreadyDead
)
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 */
366 if (info
->toplevel
==view
) {
367 view
->screen
->focusInfo
= info
->next
;
371 if (info
->next
->toplevel
== view
)
376 W_FocusInfo
*next
= 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;
392 if (ptr
== view
->childrenList
) {
393 view
->childrenList
= ptr
->nextSister
;
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 */
411 W_CleanUpEvents(view
);
413 if (view
->dragSourceProcs
)
414 free(view
->dragSourceProcs
);
416 if (view
->dragDestinationProcs
)
417 free(view
->dragDestinationProcs
);
425 W_DestroyView(W_View
*view
)
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
)
440 if (view
->flags
.realized
) {
441 XMoveWindow(view
->screen
->display
, view
->window
, x
, y
);
449 W_ResizeView(W_View
*view
, unsigned int width
, unsigned int height
)
456 if (view
->size
.width
== width
&& view
->size
.height
== height
)
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
);
473 W_RedisplayView(W_View
*view
)
477 if (!view
->flags
.mapped
)
480 ev
.xexpose
.type
= Expose
;
481 ev
.xexpose
.display
= view
->screen
->display
;
482 ev
.xexpose
.window
= view
->window
;
483 ev
.xexpose
.count
= 0;
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
,
497 XClearWindow(view
->screen
->display
, view
->window
);
504 W_FocusedViewOfToplevel(W_View
*view
)
506 WMScreen
*scr
= view
->screen
;
509 for (info
= scr
->focusInfo
; info
!=NULL
; info
= info
->next
)
510 if (view
== info
->toplevel
)
516 return info
->focused
;
521 W_SetFocusOfTopLevel(W_View
*toplevel
, W_View
*view
)
523 WMScreen
*scr
= toplevel
->screen
;
527 for (info
= scr
->focusInfo
; info
!=NULL
; info
= info
->next
)
528 if (toplevel
== info
->toplevel
)
532 info
= wmalloc(sizeof(W_FocusInfo
));
533 info
->toplevel
= toplevel
;
534 info
->focused
= view
;
535 info
->next
= scr
->focusInfo
;
536 scr
->focusInfo
= info
;
538 event
.xfocus
.mode
= NotifyNormal
;
539 event
.xfocus
.detail
= NotifyDetailNone
;
541 /* simulate FocusOut event */
542 event
.xfocus
.type
= FocusOut
;
543 W_DispatchMessage(info
->focused
, &event
);
545 info
->focused
= view
;
548 /* simulate FocusIn event */
549 event
.xfocus
.type
= FocusIn
;
550 W_DispatchMessage(view
, &event
);
556 W_BroadcastMessage(W_View
*targetParent
, XEvent
*event
)
560 target
= targetParent
->childrenList
;
561 while (target
!=NULL
) {
562 W_DispatchMessage(target
, event
);
564 target
= target
->nextSister
;
570 W_DispatchMessage(W_View
*target
, XEvent
*event
)
572 if (target
->window
==None
)
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);
587 W_RetainView(WMView
*view
)
596 W_ReleaseView(WMView
*view
)
599 if (view
->refCount
< 1) {
606 WMWidgetOfView(WMView
*view
)
613 WMGetViewSize(WMView
*view
)
619 WMGetViewPosition(WMView
*view
)
626 WMSetViewNotifySizeChanges(WMView
*view
, Bool flag
)
628 view
->flags
.notifySizeChanged
= flag
;
632 WMViewXID(WMView
*view
)