4 #include <X11/Xresource.h>
6 /* the notifications about views */
8 char *WMViewSizeDidChangeNotification
= "WMViewSizeDidChangeNotification";
9 char *WMViewFocusDidChangeNotification
= "WMViewFocusDidChangeNotification";
10 char *WMViewRealizedNotification
= "WMViewRealizedNotification";
13 KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask| \
14 EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask| \
15 VisibilityChangeMask|FocusChangeMask|PropertyChangeMask|\
16 SubstructureNotifyMask|SubstructureRedirectMask
18 static XSetWindowAttributes defAtts
= {
19 None
, /* background_pixmap */
20 0, /* background_pixel */
21 CopyFromParent
, /* border_pixmap */
23 ForgetGravity
, /* bit_gravity */
24 ForgetGravity
, /* win_gravity */
25 NotUseful
, /* backing_store */
26 (unsigned)~0, /* backing_planes */
27 0, /* backing_pixel */
28 False
, /* save_under */
29 EVENT_MASK
, /* event_mask */
30 0, /* do_not_propagate_mask */
31 False
, /* override_redirect */
36 static XContext ViewContext
= 0; /* context for views */
38 W_View
*W_GetViewForXWindow(Display
* display
, Window window
)
42 if (XFindContext(display
, window
, ViewContext
, (XPointer
*) & view
) == 0) {
48 static void unparentView(W_View
* view
)
50 /* remove from parent's children list */
51 if (view
->parent
!= NULL
) {
54 ptr
= view
->parent
->childrenList
;
56 view
->parent
->childrenList
= view
->nextSister
;
59 if (ptr
->nextSister
== view
) {
60 ptr
->nextSister
= view
->nextSister
;
63 ptr
= ptr
->nextSister
;
70 static void adoptChildView(W_View
* view
, W_View
* child
)
72 child
->nextSister
= NULL
;
74 /* add to end of children list of parent */
75 if (view
->childrenList
== NULL
) {
76 view
->childrenList
= child
;
80 ptr
= view
->childrenList
;
81 while (ptr
->nextSister
!= NULL
)
82 ptr
= ptr
->nextSister
;
83 ptr
->nextSister
= child
;
88 static W_View
*createView(W_Screen
* screen
, W_View
* parent
)
93 ViewContext
= XUniqueContext();
95 view
= wmalloc(sizeof(W_View
));
96 memset(view
, 0, sizeof(W_View
));
98 view
->screen
= screen
;
100 if (parent
!= NULL
) {
101 /* attributes are not valid for root window */
102 view
->attribFlags
= CWEventMask
| CWBitGravity
;
103 view
->attribs
= defAtts
;
105 view
->attribFlags
|= CWBackPixel
| CWColormap
| CWBorderPixel
;
106 view
->attribs
.background_pixel
= W_PIXEL(screen
->gray
);
107 view
->attribs
.border_pixel
= W_PIXEL(screen
->black
);
108 view
->attribs
.colormap
= screen
->colormap
;
110 view
->backColor
= WMRetainColor(screen
->gray
);
112 adoptChildView(parent
, view
);
119 view
->eventHandlers
= WMCreateArrayWithDestructor(4, wfree
);
124 W_View
*W_CreateView(W_View
* parent
)
126 return createView(parent
->screen
, parent
);
129 W_View
*W_CreateRootView(W_Screen
* screen
)
133 view
= createView(screen
, NULL
);
135 view
->window
= screen
->rootWin
;
137 view
->flags
.realized
= 1;
138 view
->flags
.mapped
= 1;
139 view
->flags
.root
= 1;
141 view
->size
.width
= WidthOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
142 view
->size
.height
= HeightOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
147 W_View
*W_CreateTopView(W_Screen
* screen
)
151 view
= createView(screen
, screen
->rootView
);
155 view
->flags
.topLevel
= 1;
156 view
->attribs
.event_mask
|= StructureNotifyMask
;
161 W_View
*W_CreateUnmanagedTopView(W_Screen
* screen
)
165 view
= createView(screen
, screen
->rootView
);
169 view
->flags
.topLevel
= 1;
170 view
->attribs
.event_mask
|= StructureNotifyMask
;
172 view
->attribFlags
|= CWOverrideRedirect
;
173 view
->attribs
.override_redirect
= True
;
178 void W_RealizeView(W_View
* view
)
181 Display
*dpy
= view
->screen
->display
;
184 assert(view
->size
.width
> 0);
185 assert(view
->size
.height
> 0);
187 if (view
->parent
&& !view
->parent
->flags
.realized
) {
188 wwarning("trying to realize widget of unrealized parent");
192 if (!view
->flags
.realized
) {
193 parentWID
= view
->parent
->window
;
194 view
->window
= XCreateWindow(dpy
, parentWID
, view
->pos
.x
, view
->pos
.y
,
195 view
->size
.width
, view
->size
.height
, 0,
196 view
->screen
->depth
, InputOutput
,
197 view
->screen
->visual
, view
->attribFlags
, &view
->attribs
);
199 XSaveContext(dpy
, view
->window
, ViewContext
, (XPointer
) view
);
201 view
->flags
.realized
= 1;
203 if (view
->flags
.mapWhenRealized
) {
205 view
->flags
.mapWhenRealized
= 0;
208 WMPostNotificationName(WMViewRealizedNotification
, view
, NULL
);
211 /* realize children */
212 ptr
= view
->childrenList
;
213 while (ptr
!= NULL
) {
216 ptr
= ptr
->nextSister
;
220 void W_ReparentView(W_View
* view
, W_View
* newParent
, int x
, int y
)
222 Display
*dpy
= view
->screen
->display
;
224 assert(!view
->flags
.topLevel
);
227 adoptChildView(newParent
, view
);
229 if (view
->flags
.realized
) {
230 if (newParent
->flags
.realized
) {
231 XReparentWindow(dpy
, view
->window
, newParent
->window
, x
, y
);
233 wwarning("trying to reparent realized view to unrealized parent");
242 void W_RaiseView(W_View
* view
)
244 if (W_VIEW_REALIZED(view
))
245 XRaiseWindow(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
));
248 void W_LowerView(W_View
* view
)
250 if (W_VIEW_REALIZED(view
))
251 XLowerWindow(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
));
254 void W_MapView(W_View
* view
)
256 if (!view
->flags
.mapped
) {
257 if (view
->flags
.realized
) {
258 XMapRaised(view
->screen
->display
, view
->window
);
259 XFlush(view
->screen
->display
);
260 view
->flags
.mapped
= 1;
262 view
->flags
.mapWhenRealized
= 1;
269 * maps all children of the current view that where already realized.
271 void W_MapSubviews(W_View
* view
)
273 XMapSubwindows(view
->screen
->display
, view
->window
);
274 XFlush(view
->screen
->display
);
276 view
= view
->childrenList
;
278 view
->flags
.mapped
= 1;
279 view
->flags
.mapWhenRealized
= 0;
280 view
= view
->nextSister
;
284 void W_UnmapSubviews(W_View
* view
)
286 XUnmapSubwindows(view
->screen
->display
, view
->window
);
287 XFlush(view
->screen
->display
);
289 view
= view
->childrenList
;
291 view
->flags
.mapped
= 0;
292 view
->flags
.mapWhenRealized
= 0;
293 view
= view
->nextSister
;
297 void W_UnmapView(W_View
* view
)
299 view
->flags
.mapWhenRealized
= 0;
300 if (!view
->flags
.mapped
)
303 XUnmapWindow(view
->screen
->display
, view
->window
);
304 XFlush(view
->screen
->display
);
306 view
->flags
.mapped
= 0;
309 W_View
*W_TopLevelOfView(W_View
* view
)
313 for (toplevel
= view
; toplevel
&& !toplevel
->flags
.topLevel
; toplevel
= toplevel
->parent
) ;
318 static void destroyView(W_View
* view
)
322 if (view
->flags
.alreadyDead
)
324 view
->flags
.alreadyDead
= 1;
326 /* delete the balloon text for the view, if there's any */
327 WMSetBalloonTextForView(NULL
, view
);
329 if (view
->nextFocusChain
)
330 view
->nextFocusChain
->prevFocusChain
= view
->prevFocusChain
;
331 if (view
->prevFocusChain
)
332 view
->prevFocusChain
->nextFocusChain
= view
->nextFocusChain
;
334 /* Do not leave focus in a inexisting control */
335 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view
)) == view
)
336 W_SetFocusOfTopLevel(W_TopLevelOfView(view
), NULL
);
338 if (view
->flags
.topLevel
) {
339 W_FocusInfo
*info
= view
->screen
->focusInfo
;
340 /* remove focus information associated to this toplevel */
343 if (info
->toplevel
== view
) {
344 view
->screen
->focusInfo
= info
->next
;
348 if (info
->next
->toplevel
== view
)
353 W_FocusInfo
*next
= info
->next
->next
;
357 /* else the toplevel did not have any focused subview */
362 /* destroy children recursively */
363 while (view
->childrenList
!= NULL
) {
364 ptr
= view
->childrenList
;
365 ptr
->flags
.parentDying
= 1;
369 if (ptr
== view
->childrenList
) {
370 view
->childrenList
= ptr
->nextSister
;
375 W_CallDestroyHandlers(view
);
377 if (view
->flags
.realized
) {
378 XDeleteContext(view
->screen
->display
, view
->window
, ViewContext
);
380 /* if parent is being destroyed, it will die naturaly */
381 if (!view
->flags
.parentDying
|| view
->flags
.topLevel
)
382 XDestroyWindow(view
->screen
->display
, view
->window
);
385 /* remove self from parent's children list */
388 /* the array has a wfree() destructor that will be called automatically */
389 WMFreeArray(view
->eventHandlers
);
390 view
->eventHandlers
= NULL
;
392 WMRemoveNotificationObserver(view
);
394 W_FreeViewXdndPart(view
);
397 WMReleaseColor(view
->backColor
);
402 void W_DestroyView(W_View
* view
)
406 if (view
->refCount
< 1) {
411 void W_MoveView(W_View
* view
, int x
, int y
)
413 assert(view
->flags
.root
== 0);
415 if (view
->delegate
&& view
->delegate
->willMove
) {
416 (*view
->delegate
->willMove
) (view
->delegate
, view
, &x
, &y
);
419 if (view
->pos
.x
== x
&& view
->pos
.y
== y
)
422 if (view
->flags
.realized
) {
423 XMoveWindow(view
->screen
->display
, view
->window
, x
, y
);
428 if (view
->delegate
&& view
->delegate
->didMove
) {
429 (*view
->delegate
->didMove
) (view
->delegate
, view
);
433 void W_ResizeView(W_View
* view
, unsigned int width
, unsigned int height
)
437 if (view
->delegate
&& view
->delegate
->willResize
) {
438 (*view
->delegate
->willResize
) (view
->delegate
, view
, &width
, &height
);
444 if (view
->size
.width
== width
&& view
->size
.height
== height
)
447 /*shrinked = width < view->size.width || height < view->size.height; */
449 if (view
->flags
.realized
) {
450 XResizeWindow(view
->screen
->display
, view
->window
, width
, height
);
452 view
->size
.width
= width
;
453 view
->size
.height
= height
;
455 if (view
->delegate
&& view
->delegate
->didResize
) {
456 (*view
->delegate
->didResize
) (view
->delegate
, view
);
459 /* // TODO. replace in WINGs code, with the didResize delegate */
460 if (view
->flags
.notifySizeChanged
)
461 WMPostNotificationName(WMViewSizeDidChangeNotification
, view
, NULL
);
464 void W_RedisplayView(W_View
* view
)
468 if (!view
->flags
.mapped
)
471 ev
.xexpose
.type
= Expose
;
472 ev
.xexpose
.display
= view
->screen
->display
;
473 ev
.xexpose
.window
= view
->window
;
474 ev
.xexpose
.count
= 0;
476 ev
.xexpose
.serial
= 0;
481 void W_SetViewBackgroundColor(W_View
* view
, WMColor
* color
)
484 WMReleaseColor(view
->backColor
);
485 view
->backColor
= WMRetainColor(color
);
487 view
->attribFlags
|= CWBackPixel
;
488 view
->attribs
.background_pixel
= W_PIXEL(color
);
489 if (view
->flags
.realized
) {
490 XSetWindowBackground(view
->screen
->display
, view
->window
, W_PIXEL(color
));
491 XClearWindow(view
->screen
->display
, view
->window
);
495 void W_SetViewCursor(W_View
* view
, Cursor cursor
)
497 view
->cursor
= cursor
;
498 if (W_VIEW_REALIZED(view
)) {
499 XDefineCursor(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
), cursor
);
501 view
->attribFlags
|= CWCursor
;
502 view
->attribs
.cursor
= cursor
;
506 W_View
*W_FocusedViewOfToplevel(W_View
* view
)
508 WMScreen
*scr
= view
->screen
;
511 for (info
= scr
->focusInfo
; info
!= NULL
; info
= info
->next
)
512 if (view
== info
->toplevel
)
518 return info
->focused
;
521 void 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
);
554 void W_BroadcastMessage(W_View
* targetParent
, XEvent
* event
)
558 target
= targetParent
->childrenList
;
559 while (target
!= NULL
) {
560 W_DispatchMessage(target
, event
);
562 target
= target
->nextSister
;
566 void W_DispatchMessage(W_View
* target
, XEvent
* event
)
568 if (target
->window
== None
)
570 event
->xclient
.window
= target
->window
;
571 event
->xclient
.display
= target
->screen
->display
;
573 WMHandleEvent(event
);
575 XSendEvent(target->screen->display, target->window, False,
576 SubstructureNotifyMask, event);
580 WMView
*W_RetainView(WMView
* view
)
587 void W_ReleaseView(WMView
* view
)
591 if (view
->refCount
< 1) {
596 WMWidget
*WMWidgetOfView(WMView
* view
)
601 WMSize
WMGetViewSize(WMView
* view
)
606 WMPoint
WMGetViewPosition(WMView
* view
)
611 void WMSetViewNotifySizeChanges(WMView
* view
, Bool flag
)
613 view
->flags
.notifySizeChanged
= ((flag
== 0) ? 0 : 1);
616 Window
WMViewXID(WMView
* view
)
621 WMPoint
WMGetViewScreenPosition(WMView
* view
)
623 WMScreen
*scr
= W_VIEW_SCREEN(view
);
625 int x
, y
, topX
, topY
;
630 while (topView
->parent
&& topView
->parent
!= scr
->rootView
)
631 topView
= topView
->parent
;
633 if (!XGetGeometry(scr
->display
, W_VIEW_DRAWABLE(topView
), &foo
, &topX
, &topY
, &bar
, &bar
, &bar
, &bar
)) {
637 XTranslateCoordinates(scr
->display
, W_VIEW_DRAWABLE(view
), scr
->rootWin
, 0, 0, &x
, &y
, &foo
);
639 return wmkpoint(x
- topX
, y
- topY
);
642 static void resizedParent(void *self
, WMNotification
* notif
)
644 WMSize size
= WMGetViewSize((WMView
*) WMGetNotificationObject(notif
));
645 WMView
*view
= (WMView
*) self
;
647 W_MoveView(view
, view
->leftOffs
, view
->topOffs
);
648 W_ResizeView(view
, size
.width
- (view
->leftOffs
+ view
->rightOffs
),
649 size
.height
- (view
->topOffs
+ view
->bottomOffs
));
652 void WMSetViewExpandsToParent(WMView
* view
, int leftOffs
, int topOffs
, int rightOffs
, int bottomOffs
)
654 WMSize size
= view
->parent
->size
;
656 view
->topOffs
= topOffs
;
657 view
->bottomOffs
= bottomOffs
;
658 view
->leftOffs
= leftOffs
;
659 view
->rightOffs
= rightOffs
;
661 WMAddNotificationObserver(resizedParent
, view
, WMViewSizeDidChangeNotification
, view
->parent
);
662 WMSetViewNotifySizeChanges(view
->parent
, True
);
664 W_MoveView(view
, leftOffs
, topOffs
);
665 W_ResizeView(view
, size
.width
- (leftOffs
+ rightOffs
), size
.height
- (topOffs
+ bottomOffs
));