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
));
116 view
->screen
= screen
;
119 /* attributes are not valid for root window */
120 view
->attribFlags
= CWEventMask
|CWBitGravity
;
121 view
->attribs
= defAtts
;
123 view
->attribFlags
|= CWBackPixel
|CWColormap
|CWBorderPixel
;
124 view
->attribs
.background_pixel
= W_PIXEL(screen
->gray
);
125 view
->attribs
.border_pixel
= W_PIXEL(screen
->black
);
126 view
->attribs
.colormap
= screen
->colormap
;
128 view
->backColor
= WMRetainColor(screen
->gray
);
130 adoptChildView(parent
, view
);
135 view
->eventHandlers
= WMCreateBag(4);
143 W_CreateView(W_View
*parent
)
145 return createView(parent
->screen
, parent
);
150 W_CreateRootView(W_Screen
*screen
)
154 view
= createView(screen
, NULL
);
156 view
->window
= screen
->rootWin
;
158 view
->flags
.realized
= 1;
159 view
->flags
.mapped
= 1;
160 view
->flags
.root
= 1;
163 WidthOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
165 HeightOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
172 W_CreateTopView(W_Screen
*screen
)
176 view
= createView(screen
, screen
->rootView
);
180 view
->flags
.topLevel
= 1;
181 view
->attribs
.event_mask
|= StructureNotifyMask
;
188 W_CreateUnmanagedTopView(W_Screen
*screen
)
192 view
= createView(screen
, screen
->rootView
);
196 view
->flags
.topLevel
= 1;
197 view
->attribs
.event_mask
|= StructureNotifyMask
;
199 view
->attribFlags
|= CWOverrideRedirect
;
200 view
->attribs
.override_redirect
= True
;
207 W_RealizeView(W_View
*view
)
210 Display
*dpy
= view
->screen
->display
;
213 assert(view
->size
.width
> 0);
214 assert(view
->size
.height
> 0);
217 if (view
->parent
&& !view
->parent
->flags
.realized
) {
218 wwarning("trying to realize widget of unrealized parent");
222 if (!view
->flags
.realized
) {
223 parentWID
= view
->parent
->window
;
224 view
->window
= XCreateWindow(dpy
, parentWID
, view
->pos
.x
, view
->pos
.y
,
225 view
->size
.width
, view
->size
.height
, 0,
226 view
->screen
->depth
, InputOutput
,
227 view
->screen
->visual
, view
->attribFlags
,
230 XSaveContext(dpy
, view
->window
, ViewContext
, (XPointer
)view
);
232 view
->flags
.realized
= 1;
234 if (view
->flags
.mapWhenRealized
) {
236 view
->flags
.mapWhenRealized
= 0;
239 WMPostNotificationName(WMViewRealizedNotification
, view
, NULL
);
242 /* realize children */
243 ptr
= view
->childrenList
;
247 ptr
= ptr
->nextSister
;
253 W_ReparentView(W_View
*view
, W_View
*newParent
, int x
, int y
)
255 Display
*dpy
= view
->screen
->display
;
257 assert(!view
->flags
.topLevel
);
260 adoptChildView(newParent
, view
);
262 if (view
->flags
.realized
) {
263 if (newParent
->flags
.realized
) {
264 XReparentWindow(dpy
, view
->window
, newParent
->window
, x
, y
);
266 wwarning("trying to reparent realized view to unrealized parent");
277 W_RaiseView(W_View
*view
)
279 if (W_VIEW_REALIZED(view
))
280 XRaiseWindow(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
));
286 W_LowerView(W_View
*view
)
288 if (W_VIEW_REALIZED(view
))
289 XLowerWindow(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
));
295 W_MapView(W_View
*view
)
297 if (!view
->flags
.mapped
) {
298 if (view
->flags
.realized
) {
299 XMapRaised(view
->screen
->display
, view
->window
);
300 XFlush(view
->screen
->display
);
301 view
->flags
.mapped
= 1;
303 view
->flags
.mapWhenRealized
= 1;
311 * maps all children of the current view that where already realized.
314 W_MapSubviews(W_View
*view
)
316 XMapSubwindows(view
->screen
->display
, view
->window
);
317 XFlush(view
->screen
->display
);
319 view
= view
->childrenList
;
321 view
->flags
.mapped
= 1;
322 view
->flags
.mapWhenRealized
= 0;
323 view
= view
->nextSister
;
330 W_UnmapSubviews(W_View
*view
)
332 XUnmapSubwindows(view
->screen
->display
, view
->window
);
333 XFlush(view
->screen
->display
);
335 view
= view
->childrenList
;
337 view
->flags
.mapped
= 0;
338 view
->flags
.mapWhenRealized
= 0;
339 view
= view
->nextSister
;
346 W_UnmapView(W_View
*view
)
348 view
->flags
.mapWhenRealized
= 0;
349 if (!view
->flags
.mapped
)
352 XUnmapWindow(view
->screen
->display
, view
->window
);
353 XFlush(view
->screen
->display
);
355 view
->flags
.mapped
= 0;
360 W_TopLevelOfView(W_View
*view
)
365 toplevel
&& !toplevel
->flags
.topLevel
;
366 toplevel
=toplevel
->parent
);
373 destroyView(W_View
*view
)
377 if (view
->flags
.alreadyDead
)
379 view
->flags
.alreadyDead
= 1;
381 /* delete the balloon text for the view, if there's any */
382 WMSetBalloonTextForView(NULL
, view
);
384 if (view
->nextFocusChain
)
385 view
->nextFocusChain
->prevFocusChain
= view
->prevFocusChain
;
386 if (view
->prevFocusChain
)
387 view
->prevFocusChain
->nextFocusChain
= view
->nextFocusChain
;
389 /* Do not leave focus in a inexisting control */
390 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view
))==view
)
391 W_SetFocusOfTopLevel(W_TopLevelOfView(view
), NULL
);
393 if (view
->flags
.topLevel
) {
394 W_FocusInfo
*info
= view
->screen
->focusInfo
;
395 /* remove focus information associated to this toplevel */
398 if (info
->toplevel
==view
) {
399 view
->screen
->focusInfo
= info
->next
;
403 if (info
->next
->toplevel
== view
)
408 W_FocusInfo
*next
= info
->next
->next
;
412 /* else the toplevel did not have any focused subview */
417 /* destroy children recursively */
418 while (view
->childrenList
!=NULL
) {
419 ptr
= view
->childrenList
;
420 ptr
->flags
.parentDying
= 1;
424 if (ptr
== view
->childrenList
) {
425 view
->childrenList
= ptr
->nextSister
;
430 W_CallDestroyHandlers(view
);
432 if (view
->flags
.realized
) {
433 XDeleteContext(view
->screen
->display
,
434 view
->window
, ViewContext
);
436 /* if parent is being destroyed, it will die naturaly */
437 if (!view
->flags
.parentDying
|| view
->flags
.topLevel
)
438 XDestroyWindow(view
->screen
->display
, view
->window
);
441 /* remove self from parent's children list */
444 W_CleanUpEvents(view
);
446 WMFreeBag(view
->eventHandlers
);
447 view
->eventHandlers
= NULL
;
449 WMUnregisterViewDraggedTypes(view
);
452 if (view
->dragSourceProcs
)
453 wfree(view
->dragSourceProcs
);
455 if (view
->dragDestinationProcs
)
456 wfree(view
->dragDestinationProcs
);
458 if (scr
->dragInfo
.destView
== view
) {
459 scr
->dragInfo
.destView
= NULL
;
468 W_DestroyView(W_View
*view
)
472 if (view
->refCount
< 1) {
480 W_MoveView(W_View
*view
, int x
, int y
)
482 assert(view
->flags
.root
==0);
484 if (view
->delegate
&& view
->delegate
->willMove
) {
485 (*view
->delegate
->willMove
)(view
->delegate
, view
, &x
, &y
);
488 if (view
->pos
.x
== x
&& view
->pos
.y
== y
)
491 if (view
->flags
.realized
) {
492 XMoveWindow(view
->screen
->display
, view
->window
, x
, y
);
497 if (view
->delegate
&& view
->delegate
->didMove
) {
498 (*view
->delegate
->didMove
)(view
->delegate
, view
);
504 W_ResizeView(W_View
*view
, unsigned int width
, unsigned int height
)
508 if (view
->delegate
&& view
->delegate
->willResize
) {
509 (*view
->delegate
->willResize
)(view
->delegate
, view
, &width
, &height
);
515 if (view
->size
.width
== width
&& view
->size
.height
== height
)
518 shrinked
= width
< view
->size
.width
|| height
< view
->size
.height
;
520 if (view
->flags
.realized
) {
521 XResizeWindow(view
->screen
->display
, view
->window
, width
, height
);
523 view
->size
.width
= width
;
524 view
->size
.height
= height
;
526 if (view
->delegate
&& view
->delegate
->didResize
) {
527 (*view
->delegate
->didResize
)(view
->delegate
, view
);
530 if (view
->flags
.notifySizeChanged
)
531 WMPostNotificationName(WMViewSizeDidChangeNotification
, view
, NULL
);
536 W_RedisplayView(W_View
*view
)
540 if (!view
->flags
.mapped
)
543 ev
.xexpose
.type
= Expose
;
544 ev
.xexpose
.display
= view
->screen
->display
;
545 ev
.xexpose
.window
= view
->window
;
546 ev
.xexpose
.count
= 0;
548 ev
.xexpose
.serial
= 0;
555 W_SetViewBackgroundColor(W_View
*view
, WMColor
*color
)
558 WMReleaseColor(view
->backColor
);
559 view
->backColor
= WMRetainColor(color
);
561 view
->attribFlags
|= CWBackPixel
;
562 view
->attribs
.background_pixel
= color
->color
.pixel
;
563 if (view
->flags
.realized
) {
564 XSetWindowBackground(view
->screen
->display
, view
->window
,
566 XClearWindow(view
->screen
->display
, view
->window
);
572 W_SetViewCursor(W_View
*view
, Cursor cursor
)
574 view
->cursor
= cursor
;
575 if (W_VIEW_REALIZED(view
)) {
576 XDefineCursor(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
), cursor
);
578 view
->attribFlags
|= CWCursor
;
579 view
->attribs
.cursor
= cursor
;
585 W_FocusedViewOfToplevel(W_View
*view
)
587 WMScreen
*scr
= view
->screen
;
590 for (info
= scr
->focusInfo
; info
!=NULL
; info
= info
->next
)
591 if (view
== info
->toplevel
)
597 return info
->focused
;
602 W_SetFocusOfTopLevel(W_View
*toplevel
, W_View
*view
)
604 WMScreen
*scr
= toplevel
->screen
;
608 for (info
= scr
->focusInfo
; info
!=NULL
; info
= info
->next
)
609 if (toplevel
== info
->toplevel
)
613 info
= wmalloc(sizeof(W_FocusInfo
));
614 info
->toplevel
= toplevel
;
615 info
->focused
= view
;
616 info
->next
= scr
->focusInfo
;
617 scr
->focusInfo
= info
;
619 event
.xfocus
.mode
= NotifyNormal
;
620 event
.xfocus
.detail
= NotifyDetailNone
;
622 /* simulate FocusOut event */
623 event
.xfocus
.type
= FocusOut
;
624 W_DispatchMessage(info
->focused
, &event
);
626 info
->focused
= view
;
629 /* simulate FocusIn event */
630 event
.xfocus
.type
= FocusIn
;
631 W_DispatchMessage(view
, &event
);
637 W_BroadcastMessage(W_View
*targetParent
, XEvent
*event
)
641 target
= targetParent
->childrenList
;
642 while (target
!=NULL
) {
643 W_DispatchMessage(target
, event
);
645 target
= target
->nextSister
;
651 W_DispatchMessage(W_View
*target
, XEvent
*event
)
653 if (target
->window
==None
)
655 event
->xclient
.window
= target
->window
;
656 event
->xclient
.display
= target
->screen
->display
;
658 WMHandleEvent(event
);
660 XSendEvent(target->screen->display, target->window, False,
661 SubstructureNotifyMask, event);
668 W_RetainView(WMView
*view
)
677 W_ReleaseView(WMView
*view
)
681 if (view
->refCount
< 1) {
688 WMWidgetOfView(WMView
*view
)
695 WMGetViewSize(WMView
*view
)
701 WMGetViewPosition(WMView
*view
)
708 WMSetViewNotifySizeChanges(WMView
*view
, Bool flag
)
710 view
->flags
.notifySizeChanged
= flag
;
715 WMViewXID(WMView
*view
)
722 WMGetViewScreenPosition(WMView
*view
)
724 WMScreen
*scr
= W_VIEW_SCREEN(view
);
728 XTranslateCoordinates(scr
->display
, W_VIEW_DRAWABLE(view
),
729 scr
->rootWin
, 0, 0, &x
, &y
, &foo
);
731 return wmkpoint(x
, y
);