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 view
->backColor
= WMRetainColor(screen
->gray
);
132 adoptChildView(parent
, view
);
141 W_CreateView(W_View
*parent
)
143 return createView(parent
->screen
, parent
);
148 W_CreateRootView(W_Screen
*screen
)
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;
161 WidthOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
163 HeightOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
170 W_CreateTopView(W_Screen
*screen
)
174 view
= createView(screen
, screen
->rootView
);
178 view
->flags
.topLevel
= 1;
179 view
->attribs
.event_mask
|= StructureNotifyMask
;
187 W_RealizeView(W_View
*view
)
190 Display
*dpy
= view
->screen
->display
;
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");
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
,
210 XSaveContext(dpy
, view
->window
, ViewContext
, (XPointer
)view
);
212 view
->flags
.realized
= 1;
214 if (view
->flags
.mapWhenRealized
) {
216 view
->flags
.mapWhenRealized
= 0;
219 WMPostNotificationName(WMViewRealizedNotification
, view
, NULL
);
222 /* realize children */
223 ptr
= view
->childrenList
;
227 ptr
= ptr
->nextSister
;
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
)
245 W_ReparentView(W_View
*view
, W_View
*newParent
, int x
, int y
)
247 Display
*dpy
= view
->screen
->display
;
249 assert(!view
->flags
.topLevel
);
252 adoptChildView(newParent
, view
);
254 if (view
->flags
.realized
) {
255 if (newParent
->flags
.realized
) {
256 XReparentWindow(dpy
, view
->window
, newParent
->window
, x
, y
);
258 wwarning("trying to reparent realized view to unrealized parent");
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;
278 view
->flags
.mapWhenRealized
= 1;
286 * maps all children of the current view that where already realized.
289 W_MapSubviews(W_View
*view
)
291 XMapSubwindows(view
->screen
->display
, view
->window
);
292 XFlush(view
->screen
->display
);
294 view
= view
->childrenList
;
296 view
->flags
.mapped
= 1;
297 view
->flags
.mapWhenRealized
= 0;
298 view
= view
->nextSister
;
305 W_UnmapSubviews(W_View
*view
)
307 XUnmapSubwindows(view
->screen
->display
, view
->window
);
308 XFlush(view
->screen
->display
);
310 view
= view
->childrenList
;
312 view
->flags
.mapped
= 0;
313 view
->flags
.mapWhenRealized
= 0;
314 view
= view
->nextSister
;
321 W_UnmapView(W_View
*view
)
323 view
->flags
.mapWhenRealized
= 0;
324 if (!view
->flags
.mapped
)
327 XUnmapWindow(view
->screen
->display
, view
->window
);
328 XFlush(view
->screen
->display
);
330 view
->flags
.mapped
= 0;
335 W_TopLevelOfView(W_View
*view
)
339 for (toplevel
=view
; !toplevel
->flags
.topLevel
; toplevel
=toplevel
->parent
);
346 destroyView(W_View
*view
)
350 if (view
->flags
.alreadyDead
)
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 */
371 if (info
->toplevel
==view
) {
372 view
->screen
->focusInfo
= info
->next
;
376 if (info
->next
->toplevel
== view
)
381 W_FocusInfo
*next
= 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;
397 if (ptr
== view
->childrenList
) {
398 view
->childrenList
= ptr
->nextSister
;
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 */
416 W_CleanUpEvents(view
);
418 if (view
->dragSourceProcs
)
419 free(view
->dragSourceProcs
);
421 if (view
->dragDestinationProcs
)
422 free(view
->dragDestinationProcs
);
430 W_DestroyView(W_View
*view
)
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
)
449 if (view
->flags
.realized
) {
450 XMoveWindow(view
->screen
->display
, view
->window
, x
, y
);
455 if (view
->delegate
&& view
->delegate
->didMove
) {
456 (*view
->delegate
->didMove
)(view
->delegate
, view
);
462 W_ResizeView(W_View
*view
, unsigned int width
, unsigned int height
)
466 if (view
->delegate
&& view
->delegate
->willResize
) {
467 (*view
->delegate
->willResize
)(view
->delegate
, view
, &width
, &height
);
473 if (view
->size
.width
== width
&& view
->size
.height
== height
)
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
);
494 W_RedisplayView(W_View
*view
)
498 if (!view
->flags
.mapped
)
501 ev
.xexpose
.type
= Expose
;
502 ev
.xexpose
.display
= view
->screen
->display
;
503 ev
.xexpose
.window
= view
->window
;
504 ev
.xexpose
.count
= 0;
511 W_SetViewBackgroundColor(W_View
*view
, WMColor
*color
)
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
,
522 XClearWindow(view
->screen
->display
, view
->window
);
529 W_FocusedViewOfToplevel(W_View
*view
)
531 WMScreen
*scr
= view
->screen
;
534 for (info
= scr
->focusInfo
; info
!=NULL
; info
= info
->next
)
535 if (view
== info
->toplevel
)
541 return info
->focused
;
546 W_SetFocusOfTopLevel(W_View
*toplevel
, W_View
*view
)
548 WMScreen
*scr
= toplevel
->screen
;
552 for (info
= scr
->focusInfo
; info
!=NULL
; info
= info
->next
)
553 if (toplevel
== info
->toplevel
)
557 info
= wmalloc(sizeof(W_FocusInfo
));
558 info
->toplevel
= toplevel
;
559 info
->focused
= view
;
560 info
->next
= scr
->focusInfo
;
561 scr
->focusInfo
= info
;
563 event
.xfocus
.mode
= NotifyNormal
;
564 event
.xfocus
.detail
= NotifyDetailNone
;
566 /* simulate FocusOut event */
567 event
.xfocus
.type
= FocusOut
;
568 W_DispatchMessage(info
->focused
, &event
);
570 info
->focused
= view
;
573 /* simulate FocusIn event */
574 event
.xfocus
.type
= FocusIn
;
575 W_DispatchMessage(view
, &event
);
581 W_BroadcastMessage(W_View
*targetParent
, XEvent
*event
)
585 target
= targetParent
->childrenList
;
586 while (target
!=NULL
) {
587 W_DispatchMessage(target
, event
);
589 target
= target
->nextSister
;
595 W_DispatchMessage(W_View
*target
, XEvent
*event
)
597 if (target
->window
==None
)
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);
612 W_RetainView(WMView
*view
)
621 W_ReleaseView(WMView
*view
)
624 if (view
->refCount
< 1) {
631 WMWidgetOfView(WMView
*view
)
638 WMGetViewSize(WMView
*view
)
644 WMGetViewPosition(WMView
*view
)
651 WMSetViewNotifySizeChanges(WMView
*view
, Bool flag
)
653 view
->flags
.notifySizeChanged
= flag
;
657 WMViewXID(WMView
*view
)