Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wview.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
9d2e6ef9 scottc1998-09-29 22:36:29 +00002#include "WINGsP.h"
3
4#include <X11/Xresource.h>
5
9d2e6ef9 scottc1998-09-29 22:36:29 +00006/* the notifications about views */
7
8char *WMViewSizeDidChangeNotification = "WMViewSizeDidChangeNotification";
9char *WMViewFocusDidChangeNotification = "WMViewFocusDidChangeNotification";
10char *WMViewRealizedNotification = "WMViewRealizedNotification";
11
9d2e6ef9 scottc1998-09-29 22:36:29 +000012#define EVENT_MASK \
e7d0c5d9 dan2004-04-07 02:50:52 +000013 KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask| \
14 EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask| \
15 VisibilityChangeMask|FocusChangeMask|PropertyChangeMask|\
16 SubstructureNotifyMask|SubstructureRedirectMask
9d2e6ef9 scottc1998-09-29 22:36:29 +000017
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020018static XSetWindowAttributes defAtts = {
19 None, /* background_pixmap */
20 0, /* background_pixel */
21 CopyFromParent, /* border_pixmap */
22 0, /* border_pixel */
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 */
32 None, /* colormap */
33 None /* cursor */
9d2e6ef9 scottc1998-09-29 22:36:29 +000034};
35
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036static XContext ViewContext = 0; /* context for views */
9d2e6ef9 scottc1998-09-29 22:36:29 +000037
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020038W_View *W_GetViewForXWindow(Display * display, Window window)
9d2e6ef9 scottc1998-09-29 22:36:29 +000039{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020040 W_View *view;
e7d0c5d9 dan2004-04-07 02:50:52 +000041
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020042 if (XFindContext(display, window, ViewContext, (XPointer *) & view) == 0) {
43 return view;
44 }
45 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000046}
47
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020048static void unparentView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +000049{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020050 /* remove from parent's children list */
51 if (view->parent != NULL) {
52 W_View *ptr;
e7d0c5d9 dan2004-04-07 02:50:52 +000053
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020054 ptr = view->parent->childrenList;
55 if (ptr == view) {
56 view->parent->childrenList = view->nextSister;
57 } else {
58 while (ptr != NULL) {
59 if (ptr->nextSister == view) {
60 ptr->nextSister = view->nextSister;
61 break;
62 }
63 ptr = ptr->nextSister;
64 }
65 }
66 }
67 view->parent = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000068}
69
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020070static void adoptChildView(W_View * view, W_View * child)
9d2e6ef9 scottc1998-09-29 22:36:29 +000071{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020072 child->nextSister = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000073
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020074 /* add to end of children list of parent */
75 if (view->childrenList == NULL) {
76 view->childrenList = child;
77 } else {
78 W_View *ptr;
e7d0c5d9 dan2004-04-07 02:50:52 +000079
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020080 ptr = view->childrenList;
81 while (ptr->nextSister != NULL)
82 ptr = ptr->nextSister;
83 ptr->nextSister = child;
84 }
85 child->parent = view;
9d2e6ef9 scottc1998-09-29 22:36:29 +000086}
87
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088static W_View *createView(W_Screen * screen, W_View * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +000089{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020090 W_View *view;
9d2e6ef9 scottc1998-09-29 22:36:29 +000091
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020092 if (ViewContext == 0)
93 ViewContext = XUniqueContext();
e7d0c5d9 dan2004-04-07 02:50:52 +000094
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020095 view = wmalloc(sizeof(W_View));
96 memset(view, 0, sizeof(W_View));
e29fce43 kojima2000-07-10 22:37:39 +000097
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020098 view->screen = screen;
9d2e6ef9 scottc1998-09-29 22:36:29 +000099
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200100 if (parent != NULL) {
101 /* attributes are not valid for root window */
102 view->attribFlags = CWEventMask | CWBitGravity;
103 view->attribs = defAtts;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000104
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105 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;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000109
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200110 view->backColor = WMRetainColor(screen->gray);
dd2d71fc kojima1999-05-15 17:38:05 +0000111
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200112 adoptChildView(parent, view);
113 }
9093cde8 dan2001-01-01 08:39:26 +0000114
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200115 view->xic = 0;
ca616755 dan2004-10-28 04:17:18 +0000116
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200117 view->refCount = 1;
9093cde8 dan2001-01-01 08:39:26 +0000118
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200119 view->eventHandlers = WMCreateArrayWithDestructor(4, wfree);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000120
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200121 return view;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000122}
123
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200124W_View *W_CreateView(W_View * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000125{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200126 return createView(parent->screen, parent);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000127}
128
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200129W_View *W_CreateRootView(W_Screen * screen)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000130{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200131 W_View *view;
e7d0c5d9 dan2004-04-07 02:50:52 +0000132
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200133 view = createView(screen, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000134
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200135 view->window = screen->rootWin;
e7d0c5d9 dan2004-04-07 02:50:52 +0000136
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 view->flags.realized = 1;
138 view->flags.mapped = 1;
139 view->flags.root = 1;
e7d0c5d9 dan2004-04-07 02:50:52 +0000140
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200141 view->size.width = WidthOfScreen(ScreenOfDisplay(screen->display, screen->screen));
142 view->size.height = HeightOfScreen(ScreenOfDisplay(screen->display, screen->screen));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000143
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200144 return view;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000145}
146
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200147W_View *W_CreateTopView(W_Screen * screen)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000148{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200149 W_View *view;
e7d0c5d9 dan2004-04-07 02:50:52 +0000150
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200151 view = createView(screen, screen->rootView);
152 if (!view)
153 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000154
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200155 view->flags.topLevel = 1;
156 view->attribs.event_mask |= StructureNotifyMask;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000157
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200158 return view;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000159}
160
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161W_View *W_CreateUnmanagedTopView(W_Screen * screen)
e29fce43 kojima2000-07-10 22:37:39 +0000162{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200163 W_View *view;
e7d0c5d9 dan2004-04-07 02:50:52 +0000164
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200165 view = createView(screen, screen->rootView);
166 if (!view)
167 return NULL;
e29fce43 kojima2000-07-10 22:37:39 +0000168
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200169 view->flags.topLevel = 1;
170 view->attribs.event_mask |= StructureNotifyMask;
e29fce43 kojima2000-07-10 22:37:39 +0000171
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200172 view->attribFlags |= CWOverrideRedirect;
173 view->attribs.override_redirect = True;
e29fce43 kojima2000-07-10 22:37:39 +0000174
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200175 return view;
e29fce43 kojima2000-07-10 22:37:39 +0000176}
177
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200178void W_RealizeView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000179{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200180 Window parentWID;
181 Display *dpy = view->screen->display;
182 W_View *ptr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000183
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200184 assert(view->size.width > 0);
185 assert(view->size.height > 0);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000186
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200187 if (view->parent && !view->parent->flags.realized) {
188 wwarning("trying to realize widget of unrealized parent");
189 return;
190 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000191
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200192 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);
1e922744 dan2002-10-16 04:05:45 +0000198
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199 XSaveContext(dpy, view->window, ViewContext, (XPointer) view);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000200
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200201 view->flags.realized = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000202
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200203 if (view->flags.mapWhenRealized) {
204 W_MapView(view);
205 view->flags.mapWhenRealized = 0;
206 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000207
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200208 WMPostNotificationName(WMViewRealizedNotification, view, NULL);
209 }
e7d0c5d9 dan2004-04-07 02:50:52 +0000210
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200211 /* realize children */
212 ptr = view->childrenList;
213 while (ptr != NULL) {
214 W_RealizeView(ptr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000215
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200216 ptr = ptr->nextSister;
217 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000218}
219
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200220void W_ReparentView(W_View * view, W_View * newParent, int x, int y)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000221{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200222 Display *dpy = view->screen->display;
1e922744 dan2002-10-16 04:05:45 +0000223
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200224 assert(!view->flags.topLevel);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000225
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200226 unparentView(view);
227 adoptChildView(newParent, view);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000228
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200229 if (view->flags.realized) {
230 if (newParent->flags.realized) {
231 XReparentWindow(dpy, view->window, newParent->window, x, y);
232 } else {
233 wwarning("trying to reparent realized view to unrealized parent");
234 return;
235 }
236 }
e7d0c5d9 dan2004-04-07 02:50:52 +0000237
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200238 view->pos.x = x;
239 view->pos.y = y;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000240}
241
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200242void W_RaiseView(W_View * view)
e29fce43 kojima2000-07-10 22:37:39 +0000243{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200244 if (W_VIEW_REALIZED(view))
245 XRaiseWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
e29fce43 kojima2000-07-10 22:37:39 +0000246}
247
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200248void W_LowerView(W_View * view)
e29fce43 kojima2000-07-10 22:37:39 +0000249{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200250 if (W_VIEW_REALIZED(view))
251 XLowerWindow(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view));
e29fce43 kojima2000-07-10 22:37:39 +0000252}
253
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200254void W_MapView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000255{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200256 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;
261 } else {
262 view->flags.mapWhenRealized = 1;
263 }
264 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000265}
266
9d2e6ef9 scottc1998-09-29 22:36:29 +0000267/*
268 * W_MapSubviews-
269 * maps all children of the current view that where already realized.
270 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200271void W_MapSubviews(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000272{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200273 XMapSubwindows(view->screen->display, view->window);
274 XFlush(view->screen->display);
e7d0c5d9 dan2004-04-07 02:50:52 +0000275
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200276 view = view->childrenList;
277 while (view) {
278 view->flags.mapped = 1;
279 view->flags.mapWhenRealized = 0;
280 view = view->nextSister;
281 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000282}
283
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200284void W_UnmapSubviews(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000285{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200286 XUnmapSubwindows(view->screen->display, view->window);
287 XFlush(view->screen->display);
e7d0c5d9 dan2004-04-07 02:50:52 +0000288
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200289 view = view->childrenList;
290 while (view) {
291 view->flags.mapped = 0;
292 view->flags.mapWhenRealized = 0;
293 view = view->nextSister;
294 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000295}
296
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200297void W_UnmapView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000298{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200299 view->flags.mapWhenRealized = 0;
300 if (!view->flags.mapped)
301 return;
bba6dd16 dan2000-10-03 22:20:00 +0000302
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200303 XUnmapWindow(view->screen->display, view->window);
304 XFlush(view->screen->display);
bba6dd16 dan2000-10-03 22:20:00 +0000305
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200306 view->flags.mapped = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000307}
308
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200309W_View *W_TopLevelOfView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000310{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200311 W_View *toplevel;
e7d0c5d9 dan2004-04-07 02:50:52 +0000312
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200313 for (toplevel = view; toplevel && !toplevel->flags.topLevel; toplevel = toplevel->parent) ;
e7d0c5d9 dan2004-04-07 02:50:52 +0000314
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200315 return toplevel;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000316}
317
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200318static void destroyView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000319{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200320 W_View *ptr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000321
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200322 if (view->flags.alreadyDead)
323 return;
324 view->flags.alreadyDead = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000325
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200326 /* delete the balloon text for the view, if there's any */
327 WMSetBalloonTextForView(NULL, view);
d3609d34 kojima1999-04-25 21:43:50 +0000328
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200329 if (view->nextFocusChain)
330 view->nextFocusChain->prevFocusChain = view->prevFocusChain;
331 if (view->prevFocusChain)
332 view->prevFocusChain->nextFocusChain = view->nextFocusChain;
e7495baf dan1999-02-17 11:06:40 +0000333
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200334 /* Do not leave focus in a inexisting control */
335 if (W_FocusedViewOfToplevel(W_TopLevelOfView(view)) == view)
336 W_SetFocusOfTopLevel(W_TopLevelOfView(view), NULL);
e7d0c5d9 dan2004-04-07 02:50:52 +0000337
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200338 if (view->flags.topLevel) {
339 W_FocusInfo *info = view->screen->focusInfo;
340 /* remove focus information associated to this toplevel */
e7d0c5d9 dan2004-04-07 02:50:52 +0000341
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200342 if (info) {
343 if (info->toplevel == view) {
344 view->screen->focusInfo = info->next;
345 wfree(info);
346 } else {
347 while (info->next) {
348 if (info->next->toplevel == view)
349 break;
350 info = info->next;
351 }
352 if (info->next) {
353 W_FocusInfo *next = info->next->next;
354 wfree(info->next);
355 info->next = next;
356 }
357 /* else the toplevel did not have any focused subview */
358 }
359 }
360 }
e29fce43 kojima2000-07-10 22:37:39 +0000361
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200362 /* destroy children recursively */
363 while (view->childrenList != NULL) {
364 ptr = view->childrenList;
365 ptr->flags.parentDying = 1;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000366
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200367 W_DestroyView(ptr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000368
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200369 if (ptr == view->childrenList) {
370 view->childrenList = ptr->nextSister;
371 ptr->parent = NULL;
372 }
373 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000374
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200375 W_CallDestroyHandlers(view);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000376
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200377 if (view->flags.realized) {
378 XDeleteContext(view->screen->display, view->window, ViewContext);
e7d0c5d9 dan2004-04-07 02:50:52 +0000379
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200380 /* if parent is being destroyed, it will die naturaly */
381 if (!view->flags.parentDying || view->flags.topLevel)
382 XDestroyWindow(view->screen->display, view->window);
383 }
e7d0c5d9 dan2004-04-07 02:50:52 +0000384
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200385 /* remove self from parent's children list */
386 unparentView(view);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000387
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200388 /* the array has a wfree() destructor that will be called automatically */
389 WMFreeArray(view->eventHandlers);
390 view->eventHandlers = NULL;
e7d0c5d9 dan2004-04-07 02:50:52 +0000391
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200392 WMRemoveNotificationObserver(view);
e7d0c5d9 dan2004-04-07 02:50:52 +0000393
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200394 W_FreeViewXdndPart(view);
e7d0c5d9 dan2004-04-07 02:50:52 +0000395
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200396 if (view->backColor)
397 WMReleaseColor(view->backColor);
bacf8229 dan2006-01-22 18:19:09 +0000398
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200399 wfree(view);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000400}
401
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200402void W_DestroyView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000403{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200404 view->refCount--;
9093cde8 dan2001-01-01 08:39:26 +0000405
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200406 if (view->refCount < 1) {
407 destroyView(view);
408 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000409}
410
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200411void W_MoveView(W_View * view, int x, int y)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000412{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200413 assert(view->flags.root == 0);
c2ec1cfe dan2002-11-13 15:13:48 +0000414
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200415 if (view->delegate && view->delegate->willMove) {
416 (*view->delegate->willMove) (view->delegate, view, &x, &y);
417 }
5e4625da kojima1999-05-29 21:41:25 +0000418
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200419 if (view->pos.x == x && view->pos.y == y)
420 return;
5e4625da kojima1999-05-29 21:41:25 +0000421
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200422 if (view->flags.realized) {
423 XMoveWindow(view->screen->display, view->window, x, y);
424 }
425 view->pos.x = x;
426 view->pos.y = y;
5e4625da kojima1999-05-29 21:41:25 +0000427
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200428 if (view->delegate && view->delegate->didMove) {
429 (*view->delegate->didMove) (view->delegate, view);
430 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000431}
432
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200433void W_ResizeView(W_View * view, unsigned int width, unsigned int height)
1e922744 dan2002-10-16 04:05:45 +0000434{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200435 /*int shrinked; */
c2ec1cfe dan2002-11-13 15:13:48 +0000436
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200437 if (view->delegate && view->delegate->willResize) {
438 (*view->delegate->willResize) (view->delegate, view, &width, &height);
439 }
5e4625da kojima1999-05-29 21:41:25 +0000440
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200441 assert(width > 0);
442 assert(height > 0);
5e4625da kojima1999-05-29 21:41:25 +0000443
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200444 if (view->size.width == width && view->size.height == height)
445 return;
c2ec1cfe dan2002-11-13 15:13:48 +0000446
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200447 /*shrinked = width < view->size.width || height < view->size.height; */
c2ec1cfe dan2002-11-13 15:13:48 +0000448
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200449 if (view->flags.realized) {
450 XResizeWindow(view->screen->display, view->window, width, height);
451 }
452 view->size.width = width;
453 view->size.height = height;
5e4625da kojima1999-05-29 21:41:25 +0000454
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200455 if (view->delegate && view->delegate->didResize) {
456 (*view->delegate->didResize) (view->delegate, view);
457 }
5e4625da kojima1999-05-29 21:41:25 +0000458
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200459 /* // TODO. replace in WINGs code, with the didResize delegate */
460 if (view->flags.notifySizeChanged)
461 WMPostNotificationName(WMViewSizeDidChangeNotification, view, NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000462}
463
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200464void W_RedisplayView(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000465{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200466 XEvent ev;
e7d0c5d9 dan2004-04-07 02:50:52 +0000467
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200468 if (!view->flags.mapped)
469 return;
e7d0c5d9 dan2004-04-07 02:50:52 +0000470
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200471 ev.xexpose.type = Expose;
472 ev.xexpose.display = view->screen->display;
473 ev.xexpose.window = view->window;
474 ev.xexpose.count = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000475
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200476 ev.xexpose.serial = 0;
e99511b0 kojima2001-01-02 14:17:26 +0000477
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200478 WMHandleEvent(&ev);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000479}
480
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200481void W_SetViewBackgroundColor(W_View * view, WMColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000482{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200483 if (view->backColor)
484 WMReleaseColor(view->backColor);
485 view->backColor = WMRetainColor(color);
dd2d71fc kojima1999-05-15 17:38:05 +0000486
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200487 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);
492 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000493}
494
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200495void W_SetViewCursor(W_View * view, Cursor cursor)
52308a0a kojima2000-11-11 18:08:10 +0000496{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200497 view->cursor = cursor;
498 if (W_VIEW_REALIZED(view)) {
499 XDefineCursor(W_VIEW_DISPLAY(view), W_VIEW_DRAWABLE(view), cursor);
500 } else {
501 view->attribFlags |= CWCursor;
502 view->attribs.cursor = cursor;
503 }
52308a0a kojima2000-11-11 18:08:10 +0000504}
505
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200506W_View *W_FocusedViewOfToplevel(W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000507{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200508 WMScreen *scr = view->screen;
509 W_FocusInfo *info;
e7d0c5d9 dan2004-04-07 02:50:52 +0000510
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200511 for (info = scr->focusInfo; info != NULL; info = info->next)
512 if (view == info->toplevel)
513 break;
e7d0c5d9 dan2004-04-07 02:50:52 +0000514
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200515 if (!info)
516 return NULL;
e7d0c5d9 dan2004-04-07 02:50:52 +0000517
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200518 return info->focused;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000519}
520
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200521void W_SetFocusOfTopLevel(W_View * toplevel, W_View * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000522{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200523 WMScreen *scr = toplevel->screen;
524 XEvent event;
525 W_FocusInfo *info;
e7d0c5d9 dan2004-04-07 02:50:52 +0000526
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200527 for (info = scr->focusInfo; info != NULL; info = info->next)
528 if (toplevel == info->toplevel)
529 break;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000530
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200531 if (!info) {
532 info = wmalloc(sizeof(W_FocusInfo));
533 info->toplevel = toplevel;
534 info->focused = view;
535 info->next = scr->focusInfo;
536 scr->focusInfo = info;
537 } else {
538 event.xfocus.mode = NotifyNormal;
539 event.xfocus.detail = NotifyDetailNone;
540 if (info->focused) {
541 /* simulate FocusOut event */
542 event.xfocus.type = FocusOut;
543 W_DispatchMessage(info->focused, &event);
544 }
545 info->focused = view;
546 }
547 if (view) {
548 /* simulate FocusIn event */
549 event.xfocus.type = FocusIn;
550 W_DispatchMessage(view, &event);
551 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000552}
553
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200554void W_BroadcastMessage(W_View * targetParent, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000555{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200556 W_View *target;
e7d0c5d9 dan2004-04-07 02:50:52 +0000557
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200558 target = targetParent->childrenList;
559 while (target != NULL) {
560 W_DispatchMessage(target, event);
e7d0c5d9 dan2004-04-07 02:50:52 +0000561
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200562 target = target->nextSister;
563 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000564}
565
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200566void W_DispatchMessage(W_View * target, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000567{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200568 if (target->window == None)
569 return;
570 event->xclient.window = target->window;
571 event->xclient.display = target->screen->display;
e7d0c5d9 dan2004-04-07 02:50:52 +0000572
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200573 WMHandleEvent(event);
574 /*
575 XSendEvent(target->screen->display, target->window, False,
576 SubstructureNotifyMask, event);
577 */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000578}
579
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200580WMView *W_RetainView(WMView * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000581{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200582 view->refCount++;
e29fce43 kojima2000-07-10 22:37:39 +0000583
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200584 return view;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000585}
586
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200587void W_ReleaseView(WMView * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000588{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200589 view->refCount--;
e29fce43 kojima2000-07-10 22:37:39 +0000590
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200591 if (view->refCount < 1) {
592 destroyView(view);
593 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000594}
595
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200596WMWidget *WMWidgetOfView(WMView * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000597{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200598 return view->self;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000599}
600
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200601WMSize WMGetViewSize(WMView * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000602{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200603 return view->size;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000604}
605
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200606WMPoint WMGetViewPosition(WMView * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000607{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200608 return view->pos;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000609}
610
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200611void WMSetViewNotifySizeChanges(WMView * view, Bool flag)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000612{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200613 view->flags.notifySizeChanged = ((flag == 0) ? 0 : 1);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000614}
615
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200616Window WMViewXID(WMView * view)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000617{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200618 return view->window;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000619}
620
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200621WMPoint WMGetViewScreenPosition(WMView * view)
6672180d kojima2000-03-28 02:30:13 +0000622{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200623 WMScreen *scr = W_VIEW_SCREEN(view);
624 Window foo;
625 int x, y, topX, topY;
626 unsigned int bar;
627 WMView *topView;
84fa789c dan2002-03-04 08:52:49 +0000628
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200629 topView = view;
630 while (topView->parent && topView->parent != scr->rootView)
631 topView = topView->parent;
84fa789c dan2002-03-04 08:52:49 +0000632
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200633 if (!XGetGeometry(scr->display, W_VIEW_DRAWABLE(topView), &foo, &topX, &topY, &bar, &bar, &bar, &bar)) {
634 topX = topY = 0;
635 }
84fa789c dan2002-03-04 08:52:49 +0000636
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200637 XTranslateCoordinates(scr->display, W_VIEW_DRAWABLE(view), scr->rootWin, 0, 0, &x, &y, &foo);
84fa789c dan2002-03-04 08:52:49 +0000638
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200639 return wmkpoint(x - topX, y - topY);
6672180d kojima2000-03-28 02:30:13 +0000640}
641
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200642static void resizedParent(void *self, WMNotification * notif)
affcc3ba kojima2001-02-20 23:38:36 +0000643{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200644 WMSize size = WMGetViewSize((WMView *) WMGetNotificationObject(notif));
645 WMView *view = (WMView *) self;
affcc3ba kojima2001-02-20 23:38:36 +0000646
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200647 W_MoveView(view, view->leftOffs, view->topOffs);
648 W_ResizeView(view, size.width - (view->leftOffs + view->rightOffs),
649 size.height - (view->topOffs + view->bottomOffs));
affcc3ba kojima2001-02-20 23:38:36 +0000650}
651
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200652void WMSetViewExpandsToParent(WMView * view, int leftOffs, int topOffs, int rightOffs, int bottomOffs)
affcc3ba kojima2001-02-20 23:38:36 +0000653{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200654 WMSize size = view->parent->size;
affcc3ba kojima2001-02-20 23:38:36 +0000655
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200656 view->topOffs = topOffs;
657 view->bottomOffs = bottomOffs;
658 view->leftOffs = leftOffs;
659 view->rightOffs = rightOffs;
e7d0c5d9 dan2004-04-07 02:50:52 +0000660
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200661 WMAddNotificationObserver(resizedParent, view, WMViewSizeDidChangeNotification, view->parent);
662 WMSetViewNotifySizeChanges(view->parent, True);
affcc3ba kojima2001-02-20 23:38:36 +0000663
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200664 W_MoveView(view, leftOffs, topOffs);
665 W_ResizeView(view, size.width - (leftOffs + rightOffs), size.height - (topOffs + bottomOffs));
affcc3ba kojima2001-02-20 23:38:36 +0000666}