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 const 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 view
->screen
= screen
;
99 /* attributes are not valid for root window */
100 view
->attribFlags
= CWEventMask
| CWBitGravity
;
101 view
->attribs
= defAtts
;
103 view
->attribFlags
|= CWBackPixel
| CWColormap
| CWBorderPixel
;
104 view
->attribs
.background_pixel
= W_PIXEL(screen
->gray
);
105 view
->attribs
.border_pixel
= W_PIXEL(screen
->black
);
106 view
->attribs
.colormap
= screen
->colormap
;
108 view
->backColor
= WMRetainColor(screen
->gray
);
110 adoptChildView(parent
, view
);
117 view
->eventHandlers
= WMCreateArrayWithDestructor(4, wfree
);
122 W_View
*W_CreateView(W_View
* parent
)
124 return createView(parent
->screen
, parent
);
127 W_View
*W_CreateRootView(W_Screen
* screen
)
131 view
= createView(screen
, NULL
);
133 view
->window
= screen
->rootWin
;
135 view
->flags
.realized
= 1;
136 view
->flags
.mapped
= 1;
137 view
->flags
.root
= 1;
139 view
->size
.width
= WidthOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
140 view
->size
.height
= HeightOfScreen(ScreenOfDisplay(screen
->display
, screen
->screen
));
145 W_View
*W_CreateTopView(W_Screen
* screen
)
149 view
= createView(screen
, screen
->rootView
);
153 view
->flags
.topLevel
= 1;
154 view
->attribs
.event_mask
|= StructureNotifyMask
;
159 W_View
*W_CreateUnmanagedTopView(W_Screen
* screen
)
163 view
= createView(screen
, screen
->rootView
);
167 view
->flags
.topLevel
= 1;
168 view
->attribs
.event_mask
|= StructureNotifyMask
;
170 view
->attribFlags
|= CWOverrideRedirect
;
171 view
->attribs
.override_redirect
= True
;
176 void W_RealizeView(W_View
* view
)
179 Display
*dpy
= view
->screen
->display
;
182 assert(view
->size
.width
> 0);
183 assert(view
->size
.height
> 0);
185 if (view
->parent
&& !view
->parent
->flags
.realized
) {
186 wwarning("trying to realize widget of unrealized parent");
190 if (!view
->flags
.realized
) {
192 if (view
->parent
== NULL
) {
193 wwarning("trying to realize widget without parent");
197 parentWID
= view
->parent
->window
;
198 view
->window
= XCreateWindow(dpy
, parentWID
, view
->pos
.x
, view
->pos
.y
,
199 view
->size
.width
, view
->size
.height
, 0,
200 view
->screen
->depth
, InputOutput
,
201 view
->screen
->visual
, view
->attribFlags
, &view
->attribs
);
203 XSaveContext(dpy
, view
->window
, ViewContext
, (XPointer
) view
);
205 view
->flags
.realized
= 1;
207 if (view
->flags
.mapWhenRealized
) {
209 view
->flags
.mapWhenRealized
= 0;
212 WMPostNotificationName(WMViewRealizedNotification
, view
, NULL
);
215 /* realize children */
216 ptr
= view
->childrenList
;
217 while (ptr
!= NULL
) {
220 ptr
= ptr
->nextSister
;
224 void W_ReparentView(W_View
* view
, W_View
* newParent
, int x
, int y
)
226 Display
*dpy
= view
->screen
->display
;
228 assert(!view
->flags
.topLevel
);
231 adoptChildView(newParent
, view
);
233 if (view
->flags
.realized
) {
234 if (newParent
->flags
.realized
) {
235 XReparentWindow(dpy
, view
->window
, newParent
->window
, x
, y
);
237 wwarning("trying to reparent realized view to unrealized parent");
246 void W_RaiseView(W_View
* view
)
248 if (W_VIEW_REALIZED(view
))
249 XRaiseWindow(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
));
252 void W_LowerView(W_View
* view
)
254 if (W_VIEW_REALIZED(view
))
255 XLowerWindow(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
));
258 void W_MapView(W_View
* view
)
260 if (!view
->flags
.mapped
) {
261 if (view
->flags
.realized
) {
262 XMapRaised(view
->screen
->display
, view
->window
);
263 XFlush(view
->screen
->display
);
264 view
->flags
.mapped
= 1;
266 view
->flags
.mapWhenRealized
= 1;
273 * maps all children of the current view that where already realized.
275 void W_MapSubviews(W_View
* view
)
277 XMapSubwindows(view
->screen
->display
, view
->window
);
278 XFlush(view
->screen
->display
);
280 view
= view
->childrenList
;
282 view
->flags
.mapped
= 1;
283 view
->flags
.mapWhenRealized
= 0;
284 view
= view
->nextSister
;
288 void W_UnmapSubviews(W_View
* view
)
290 XUnmapSubwindows(view
->screen
->display
, view
->window
);
291 XFlush(view
->screen
->display
);
293 view
= view
->childrenList
;
295 view
->flags
.mapped
= 0;
296 view
->flags
.mapWhenRealized
= 0;
297 view
= view
->nextSister
;
301 void W_UnmapView(W_View
* view
)
303 view
->flags
.mapWhenRealized
= 0;
304 if (!view
->flags
.mapped
)
307 XUnmapWindow(view
->screen
->display
, view
->window
);
308 XFlush(view
->screen
->display
);
310 view
->flags
.mapped
= 0;
313 W_View
*W_TopLevelOfView(W_View
* view
)
317 for (toplevel
= view
; toplevel
&& !toplevel
->flags
.topLevel
; toplevel
= toplevel
->parent
) ;
322 static void destroyView(W_View
* view
)
326 if (view
->flags
.alreadyDead
)
328 view
->flags
.alreadyDead
= 1;
330 /* delete the balloon text for the view, if there's any */
331 WMSetBalloonTextForView(NULL
, view
);
333 if (view
->nextFocusChain
)
334 view
->nextFocusChain
->prevFocusChain
= view
->prevFocusChain
;
335 if (view
->prevFocusChain
)
336 view
->prevFocusChain
->nextFocusChain
= view
->nextFocusChain
;
338 /* Do not leave focus in a inexisting control */
339 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view
)) == view
)
340 W_SetFocusOfTopLevel(W_TopLevelOfView(view
), NULL
);
342 if (view
->flags
.topLevel
) {
343 W_FocusInfo
*info
= view
->screen
->focusInfo
;
344 /* remove focus information associated to this toplevel */
347 if (info
->toplevel
== view
) {
348 view
->screen
->focusInfo
= info
->next
;
352 if (info
->next
->toplevel
== view
)
357 W_FocusInfo
*next
= info
->next
->next
;
361 /* else the toplevel did not have any focused subview */
366 /* destroy children recursively */
367 while (view
->childrenList
!= NULL
) {
368 ptr
= view
->childrenList
;
369 ptr
->flags
.parentDying
= 1;
373 if (ptr
== view
->childrenList
) {
374 view
->childrenList
= ptr
->nextSister
;
379 W_CallDestroyHandlers(view
);
381 if (view
->flags
.realized
) {
382 XDeleteContext(view
->screen
->display
, view
->window
, ViewContext
);
384 /* if parent is being destroyed, it will die naturaly */
385 if (!view
->flags
.parentDying
|| view
->flags
.topLevel
)
386 XDestroyWindow(view
->screen
->display
, view
->window
);
389 /* remove self from parent's children list */
392 /* the array has a wfree() destructor that will be called automatically */
393 WMFreeArray(view
->eventHandlers
);
394 view
->eventHandlers
= NULL
;
396 WMRemoveNotificationObserver(view
);
398 W_FreeViewXdndPart(view
);
401 WMReleaseColor(view
->backColor
);
406 void W_DestroyView(W_View
* view
)
410 if (view
->refCount
< 1) {
415 void W_MoveView(W_View
* view
, int x
, int y
)
417 assert(view
->flags
.root
== 0);
419 if (view
->delegate
&& view
->delegate
->willMove
) {
420 (*view
->delegate
->willMove
) (view
->delegate
, view
, &x
, &y
);
423 if (view
->pos
.x
== x
&& view
->pos
.y
== y
)
426 if (view
->flags
.realized
) {
427 XMoveWindow(view
->screen
->display
, view
->window
, x
, y
);
432 if (view
->delegate
&& view
->delegate
->didMove
) {
433 (*view
->delegate
->didMove
) (view
->delegate
, view
);
437 void W_ResizeView(W_View
* view
, unsigned int width
, unsigned int height
)
441 if (view
->delegate
&& view
->delegate
->willResize
) {
442 (*view
->delegate
->willResize
) (view
->delegate
, view
, &width
, &height
);
448 if (view
->size
.width
== width
&& view
->size
.height
== height
)
451 /*shrinked = width < view->size.width || height < view->size.height; */
453 if (view
->flags
.realized
) {
454 XResizeWindow(view
->screen
->display
, view
->window
, width
, height
);
456 view
->size
.width
= width
;
457 view
->size
.height
= height
;
459 if (view
->delegate
&& view
->delegate
->didResize
) {
460 (*view
->delegate
->didResize
) (view
->delegate
, view
);
463 /* // TODO. replace in WINGs code, with the didResize delegate */
464 if (view
->flags
.notifySizeChanged
)
465 WMPostNotificationName(WMViewSizeDidChangeNotification
, view
, NULL
);
468 void W_RedisplayView(W_View
* view
)
472 if (!view
->flags
.mapped
)
475 ev
.xexpose
.type
= Expose
;
476 ev
.xexpose
.display
= view
->screen
->display
;
477 ev
.xexpose
.window
= view
->window
;
478 ev
.xexpose
.count
= 0;
480 ev
.xexpose
.serial
= 0;
485 void W_SetViewBackgroundColor(W_View
* view
, WMColor
* color
)
488 WMReleaseColor(view
->backColor
);
489 view
->backColor
= WMRetainColor(color
);
491 view
->attribFlags
|= CWBackPixel
;
492 view
->attribs
.background_pixel
= W_PIXEL(color
);
493 if (view
->flags
.realized
) {
494 XSetWindowBackground(view
->screen
->display
, view
->window
, W_PIXEL(color
));
495 XClearWindow(view
->screen
->display
, view
->window
);
499 void W_SetViewCursor(W_View
* view
, Cursor cursor
)
501 view
->cursor
= cursor
;
502 if (W_VIEW_REALIZED(view
)) {
503 XDefineCursor(W_VIEW_DISPLAY(view
), W_VIEW_DRAWABLE(view
), cursor
);
505 view
->attribFlags
|= CWCursor
;
506 view
->attribs
.cursor
= cursor
;
510 W_View
*W_FocusedViewOfToplevel(W_View
* view
)
512 WMScreen
*scr
= view
->screen
;
515 for (info
= scr
->focusInfo
; info
!= NULL
; info
= info
->next
)
516 if (view
== info
->toplevel
)
522 return info
->focused
;
525 void W_SetFocusOfTopLevel(W_View
* toplevel
, W_View
* view
)
527 WMScreen
*scr
= toplevel
->screen
;
531 for (info
= scr
->focusInfo
; info
!= NULL
; info
= info
->next
)
532 if (toplevel
== info
->toplevel
)
536 info
= wmalloc(sizeof(W_FocusInfo
));
537 info
->toplevel
= toplevel
;
538 info
->focused
= view
;
539 info
->next
= scr
->focusInfo
;
540 scr
->focusInfo
= info
;
542 event
.xfocus
.mode
= NotifyNormal
;
543 event
.xfocus
.detail
= NotifyDetailNone
;
545 /* simulate FocusOut event */
546 event
.xfocus
.type
= FocusOut
;
547 W_DispatchMessage(info
->focused
, &event
);
549 info
->focused
= view
;
552 /* simulate FocusIn event */
553 event
.xfocus
.type
= FocusIn
;
554 W_DispatchMessage(view
, &event
);
558 void W_BroadcastMessage(W_View
* targetParent
, XEvent
* event
)
562 target
= targetParent
->childrenList
;
563 while (target
!= NULL
) {
564 W_DispatchMessage(target
, event
);
566 target
= target
->nextSister
;
570 void 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);
584 WMView
*W_RetainView(WMView
* view
)
591 void W_ReleaseView(WMView
* view
)
595 if (view
->refCount
< 1) {
600 WMWidget
*WMWidgetOfView(WMView
* view
)
605 WMSize
WMGetViewSize(WMView
* view
)
610 WMPoint
WMGetViewPosition(WMView
* view
)
615 void WMSetViewNotifySizeChanges(WMView
* view
, Bool flag
)
617 view
->flags
.notifySizeChanged
= ((flag
== 0) ? 0 : 1);
620 Window
WMViewXID(WMView
* view
)
625 WMPoint
WMGetViewScreenPosition(WMView
* view
)
627 WMScreen
*scr
= W_VIEW_SCREEN(view
);
629 int x
, y
, topX
, topY
;
634 while (topView
->parent
&& topView
->parent
!= scr
->rootView
)
635 topView
= topView
->parent
;
637 if (!XGetGeometry(scr
->display
, W_VIEW_DRAWABLE(topView
), &foo
, &topX
, &topY
, &bar
, &bar
, &bar
, &bar
)) {
641 XTranslateCoordinates(scr
->display
, W_VIEW_DRAWABLE(view
), scr
->rootWin
, 0, 0, &x
, &y
, &foo
);
643 return wmkpoint(x
- topX
, y
- topY
);
646 static void resizedParent(void *self
, WMNotification
* notif
)
648 WMSize size
= WMGetViewSize((WMView
*) WMGetNotificationObject(notif
));
649 WMView
*view
= (WMView
*) self
;
651 W_MoveView(view
, view
->leftOffs
, view
->topOffs
);
652 W_ResizeView(view
, size
.width
- (view
->leftOffs
+ view
->rightOffs
),
653 size
.height
- (view
->topOffs
+ view
->bottomOffs
));
656 void WMSetViewExpandsToParent(WMView
* view
, int leftOffs
, int topOffs
, int rightOffs
, int bottomOffs
)
658 WMSize size
= view
->parent
->size
;
660 view
->topOffs
= topOffs
;
661 view
->bottomOffs
= bottomOffs
;
662 view
->leftOffs
= leftOffs
;
663 view
->rightOffs
= rightOffs
;
665 WMAddNotificationObserver(resizedParent
, view
, WMViewSizeDidChangeNotification
, view
->parent
);
666 WMSetViewNotifySizeChanges(view
->parent
, True
);
668 W_MoveView(view
, leftOffs
, topOffs
);
669 W_ResizeView(view
, size
.width
- (leftOffs
+ rightOffs
), size
.height
- (topOffs
+ bottomOffs
));