]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/gfx/mw/v2_0/src/demos/nanowm/actions.c
Initial revision
[karo-tx-redboot.git] / packages / services / gfx / mw / v2_0 / src / demos / nanowm / actions.c
1 /*
2  * NanoWM - Window Manager for Nano-X
3  *
4  * Copyright (C) 2000 Greg Haerr <greg@censoft.com>
5  * Copyright (C) 2000 Alex Holden <alex@linuxhacker.org>
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #define MWINCLUDECOLORS
10 #include "nano-X.h"
11 #include "nxdraw.h"
12 /* Uncomment this if you want debugging output from this file */
13 /*#define DEBUG*/
14
15 #include "nanowm.h"
16
17 /* uncomment this line to perform outline move operations*/
18 /*#define OUTLINE_MOVE*/
19
20 void redraw_ncarea(win *window)
21 {
22         GR_WINDOW_INFO info;
23         GR_WM_PROPERTIES props;
24         GR_BOOL active;
25
26         Dprintf("container_exposure window %d\n", window->wid);
27
28         GrGetWindowInfo(window->wid, &info);
29
30         GrGetWMProperties(window->clientid, &props);
31
32         /*
33          * Check for invalid window.  This will be the
34          * case if the client exited, and we're just
35          * getting the paint notification for our parent.
36          */
37         if (props.flags == 0)
38                 return;
39
40         active = (window->clientid == GrGetFocus());
41         nxPaintNCArea(window->wid, info.width, info.height, props.title,
42                 active, props.props);
43 }
44
45 void container_exposure(win *window, GR_EVENT_EXPOSURE *event)
46 {
47         Dprintf("container_exposure window %d\n", window->wid);
48
49         redraw_ncarea(window);
50 }
51
52 static GR_BOOL
53 PtInRect(GR_RECT *prc, GR_SIZE x, GR_SIZE y)
54 {
55         return (x >= prc->x && x < (prc->x+prc->width) &&
56                 y >= prc->y && y < (prc->y+prc->height));
57 }
58
59 void container_buttondown(win *window, GR_EVENT_BUTTON *event)
60 {
61         struct pos_size *pos;
62         GR_RECT         r;
63         GR_COORD        cxborder = 0, cyborder = 0;
64         GR_WINDOW_INFO  info;
65         GR_GC_ID        gc;
66         Dprintf("container_buttondown window %d\n", window->wid);
67
68         if(window->active) return;
69
70         GrGetWindowInfo(window->wid, &info);
71
72         /* calc border sizes*/
73         if (info.props & GR_WM_PROPS_BORDER) {
74                 cxborder = 1;
75                 cyborder = 1;
76         }
77         if (info.props & GR_WM_PROPS_APPFRAME) {
78                 cxborder = CXBORDER;
79                 cyborder = CYBORDER;
80         }
81
82         /* Check for close box press*/
83         if ((info.props & (GR_WM_PROPS_CAPTION|GR_WM_PROPS_CLOSEBOX)) ==
84             (GR_WM_PROPS_CAPTION|GR_WM_PROPS_CLOSEBOX)) {
85
86                 /* Get close box rect*/
87                 r.x = info.width - CXCLOSEBOX - cxborder - 2;
88                 r.y = cyborder + 2;
89                 r.width = CXCLOSEBOX;
90                 r.height = CYCLOSEBOX;
91
92                 /* Check mousedn in close box*/
93                 if (PtInRect(&r, event->x, event->y)) {
94                         /* this may or not close the window...*/
95                         GrCloseWindow(window->clientid);
96                         return;
97                 }
98         }
99
100         /* Set focus on button down*/
101         GrSetFocus(window->clientid);
102 /*
103  * Note: Resize seems to cause lots of trouble since the resize "handle"
104  * does not seem to be visible/advertized.  Thus at any touch, the window
105  * may get resized and it is often impossible to recover
106  */
107
108         /* check for corner resize */
109         r.x = info.width - 5;
110         r.y = info.height - 5;
111         r.width = 5;
112         r.height = 5;
113
114         if(PtInRect(&r,event->x, event->y)) {
115
116           struct pos_size * pos;
117
118           if(!window->data)
119             if(!(window->data = malloc(sizeof(struct pos_size)))) return;
120
121           window->sizing = GR_TRUE;
122           pos = (struct pos_size*)window->data;
123           
124           /* save off the width/height offset from the window manager */
125           GrGetWindowInfo(window->clientid,&info);
126           pos->xoff = -info.width;
127           pos->yoff = -info.height;
128
129           GrGetWindowInfo(window->wid,&info);
130           pos->xoff += info.width;
131           pos->yoff += info.height;
132
133           gc = GrNewGC();
134           GrSetGCMode(gc, GR_MODE_XOR|GR_MODE_EXCLUDECHILDREN);
135           GrRect(GR_ROOT_WINDOW_ID,gc,info.x, info.y, info.width, info.height);
136           GrDestroyGC(gc);
137
138           /* save this rectangle's width/height so we can erase it later */
139           pos->width = info.width;
140           pos->height = info.height;
141
142           return;
143         }
144
145         /* if not in caption, return (FIXME, not calc'd exactly)*/
146         if (!(info.props & GR_WM_PROPS_CAPTION))
147                 return;
148
149         /* Get caption box rect*/
150         r.x = cxborder;
151         r.y = cyborder;
152         r.width = info.width - cxborder*2;
153         r.height = CYCAPTION;
154
155         /* Check for mousedn in caption box*/
156         if (!PtInRect(&r, event->x, event->y))
157                 return;
158
159         /* Raise window if mouse down and allowed*/
160         if (!(info.props & GR_WM_PROPS_NORAISE))
161                 GrRaiseWindow(window->wid);
162
163         /* Don't allow window move if NOMOVE property set*/
164         if (info.props & GR_WM_PROPS_NOMOVE)
165                 return;
166
167         if(!window->data)
168                 if(!(window->data = malloc(sizeof(struct pos_size)))) return;
169
170         pos = (struct pos_size *) window->data;
171
172         GrGetWindowInfo(window->wid,&info);
173         
174         pos->xoff = event->x;
175         pos->yoff = event->y;
176
177 #ifdef OUTLINE_MOVE
178         pos->xorig = info.x;
179         pos->yorig = info.y;
180         pos->width = info.width;
181         pos->height = info.height;
182
183         gc = GrNewGC();
184         GrSetGCMode(gc, GR_MODE_XOR|GR_MODE_EXCLUDECHILDREN);
185         GrRect(GR_ROOT_WINDOW_ID,gc,info.x, info.y, info.width, info.height);
186         GrDestroyGC(gc);
187 #endif  
188         window->active = GR_TRUE;
189 }
190
191 void container_buttonup(win *window, GR_EVENT_BUTTON *event)
192 {
193         Dprintf("container_buttonup window %d\n", window->wid);
194
195         if(window->active) {
196           struct pos_size * pos = (struct pos_size *)window->data;
197 #ifdef OUTLINE_MOVE
198           GR_GC_ID gc;    
199           gc = GrNewGC();
200           GrSetGCMode(gc, GR_MODE_XOR|GR_MODE_EXCLUDECHILDREN);
201           GrRect(GR_ROOT_WINDOW_ID,gc,pos->xorig, pos->yorig, pos->width, pos->height);
202
203           GrMoveWindow(window->wid, pos->xorig, pos->yorig);
204
205 #endif
206           free(pos);
207           window->active = GR_FALSE;
208           window->data = 0;
209         }
210         
211         if(window->sizing) {
212           GR_WINDOW_INFO info;
213           GR_GC_ID gc;
214
215           struct pos_size * pos = (struct pos_size *)window->data;
216
217           gc = GrNewGC();
218           GrSetGCMode(gc, GR_MODE_XOR|GR_MODE_EXCLUDECHILDREN);
219
220           GrGetWindowInfo(window->wid, &info);
221
222           GrRect(GR_ROOT_WINDOW_ID,gc,info.x, info.y, pos->width, pos->height);
223
224           GrResizeWindow(window->wid,event->rootx - info.x, event->rooty - info.y);
225           GrResizeWindow(window->clientid,event->rootx - info.x - pos->xoff, 
226                          event->rooty - info.y - pos->yoff);
227           GrDestroyGC(gc);
228           free(window->data);
229           window->sizing = GR_FALSE;
230           window->data = 0;
231         }
232 }
233
234 void container_mousemoved(win *window, GR_EVENT_MOUSE *event)
235 {
236         struct pos_size *pos;
237         GR_WINDOW_INFO info;
238         GR_GC_ID gc;
239
240         Dprintf("container_mousemoved window %d\n", window->wid);
241
242         if(window->sizing) {
243
244           struct pos_size * pos = (struct pos_size*)window->data;
245           GrGetWindowInfo(window->wid, &info);
246
247           gc = GrNewGC();
248           GrSetGCMode(gc, GR_MODE_XOR|GR_MODE_EXCLUDECHILDREN);
249
250           /* erase old rectangle */
251           GrRect(GR_ROOT_WINDOW_ID,gc,info.x, info.y, pos->width, pos->height);
252           /* draw new one */
253           GrRect(GR_ROOT_WINDOW_ID,gc,info.x, info.y, 
254                  event->rootx - info.x, event->rooty - info.y);
255           GrDestroyGC(gc);
256
257           /* save this new rectangle's width, height */
258           /* I know, this shouldn't be stored in x/y, but... */
259           pos->width = event->rootx - info.x;
260           pos->height = event->rooty - info.y;
261
262           return;
263         }
264
265         if(!window->active) return;
266
267         pos = (struct pos_size *) window->data;
268
269 #ifdef OUTLINE_MOVE
270         gc = GrNewGC();
271         GrSetGCMode(gc, GR_MODE_XOR|GR_MODE_EXCLUDECHILDREN);
272         GrRect(GR_ROOT_WINDOW_ID,gc,pos->xorig, pos->yorig, pos->width, pos->height);
273         GrRect(GR_ROOT_WINDOW_ID,gc,event->rootx - pos->xoff, event->rooty - pos->yoff,
274                pos->width, pos->height);
275
276         pos->xorig = event->rootx - pos->xoff;
277         pos->yorig = event->rooty - pos->yoff;
278         
279         GrDestroyGC(gc);
280 #else   
281         GrMoveWindow(window->wid, event->rootx - pos->xoff,
282                 event->rooty - pos->yoff);
283 #endif
284 }
285
286 #if 0000
287 void topbar_exposure(win *window, GR_EVENT_EXPOSURE *event)
288 {
289         win *pwin = find_window(window->pid);
290         struct clientinfo *ci = pwin->data;
291         GR_WM_PROPERTIES prop;
292
293         Dprintf("topbar_exposure window %d\n", window->wid);
294
295         GrGetWMProperties(ci->cid, &prop);
296         if (prop.title)
297                 GrText(window->wid, buttonsgc, 0, 0, prop.title, -1,
298                         GR_TFASCII|GR_TFTOP);
299 }
300
301 void closebutton_exposure(win *window, GR_EVENT_EXPOSURE *event)
302 {
303         Dprintf("closebutton_exposure window %d\n", window->wid);
304
305         GrBitmap(window->wid, buttonsgc, 0, 0, TITLE_BAR_HEIGHT,
306                 TITLE_BAR_HEIGHT, window->active ? closebutton_pressed :
307                                                 closebutton_notpressed);
308 }
309
310 void topbar_buttondown(win *window, GR_EVENT_BUTTON *event)
311 {
312         struct position *pos;
313
314         Dprintf("topbar_buttondown window %d\n", window->wid);
315
316         GrRaiseWindow(window->pid);
317
318         if(window->active) return;
319
320         if(!window->data)
321                 if(!(window->data = malloc(sizeof(struct position)))) return;
322
323         pos = (struct position *) window->data;
324
325         pos->x = event->x + TITLE_BAR_HEIGHT;   /* actually width*/
326         pos->y = event->y;
327
328         window->active = GR_TRUE;
329 }
330
331 void resizebar_buttondown(win *window, GR_EVENT_BUTTON *event)
332 {
333         GR_WINDOW_INFO wi;
334         struct pos_size *pos;
335
336         Dprintf("resizebar_buttondown window %d\n", window->wid);
337
338         GrRaiseWindow(window->pid);
339
340         if(window->active) return;
341
342         if(!window->data)
343                 if(!(window->data = malloc(sizeof(struct pos_size)))) return;
344
345         pos = (struct pos_size *) window->data;
346
347         GrGetWindowInfo(window->pid, &wi);
348
349         pos->xoff = event->x;
350         pos->yoff = event->y;
351         pos->xorig = wi.x;
352         pos->yorig = wi.y;
353         pos->width = wi.width;
354         pos->height = wi.height;
355
356         window->active = GR_TRUE;
357 }
358
359 void closebutton_buttondown(win *window, GR_EVENT_BUTTON *event)
360 {
361         Dprintf("closebutton_buttondown window %d\n", window->wid);
362
363         GrRaiseWindow(window->pid);
364
365         window->active = GR_TRUE;
366         closebutton_exposure(window, NULL);
367 }
368
369
370 void topbar_buttonup(win *window, GR_EVENT_BUTTON *event)
371 {
372         Dprintf("topbar_buttonup window %d\n", window->wid);
373
374         window->active = GR_FALSE;
375 }
376
377 void closebutton_buttonup(win *window, GR_EVENT_BUTTON *event)
378 {
379         win *pwin = find_window(window->pid);
380         struct clientinfo *ci = pwin->data;
381
382         Dprintf("closebutton_buttonup window %d\n", window->wid);
383
384         window->active = GR_FALSE;
385         closebutton_exposure(window, NULL);
386
387         GrCloseWindow(ci->cid);
388 }
389
390 void topbar_mousemoved(win *window, GR_EVENT_MOUSE *event)
391 {
392         struct position *pos;
393         GR_WM_PROPERTIES props;
394
395         Dprintf("topbar_mousemoved window %d\n", window->wid);
396
397         if(!window->active) return;
398
399         pos = (struct position *) window->data;
400
401         /* turn off background erase draw while moving*/
402         GrGetWMProperties(window->pid, &props);
403         props.flags = GR_WM_FLAGS_PROPS;
404         props.props |= GR_WM_PROPS_NOBACKGROUND;
405         GrSetWMProperties(window->pid, &props);
406
407         GrMoveWindow(window->pid, event->rootx - pos->x,
408                         event->rooty - pos->y);
409
410         props.props &= ~GR_WM_PROPS_NOBACKGROUND;
411         GrSetWMProperties(window->pid, &props);
412 }
413
414 void leftbar_mousemoved(win *window, GR_EVENT_MOUSE *event)
415 {
416         GR_COORD newx;
417         GR_SIZE newwidth;
418         struct pos_size *pos;
419
420         Dprintf("leftbar_mousemoved window %d\n", window->wid);
421
422         if(!window->active) return;
423
424         pos = (struct pos_size *) window->data;
425
426         newx = event->rootx - pos->xoff;
427         newwidth = pos->width + pos->xorig - event->rootx - pos->xoff;
428
429         GrMoveWindow(window->pid, newx, pos->yorig);
430         GrResizeWindow(window->pid, newwidth, pos->height);
431 }
432
433 void leftresize_mousemoved(win *window, GR_EVENT_MOUSE *event)
434 {
435         GR_COORD newx;
436         GR_SIZE newwidth, newheight;
437         struct pos_size *pos;
438
439         Dprintf("leftresize_mousemoved window %d\n", window->wid);
440
441         if(!window->active) return;
442
443         pos = (struct pos_size *) window->data;
444
445         newx = event->rootx - pos->xoff;
446         newheight = event->rooty - pos->yorig;
447         newwidth = pos->width + pos->xorig - event->rootx - pos->xoff;
448
449         GrMoveWindow(window->pid, newx, pos->yorig);
450         GrResizeWindow(window->pid, newwidth, newheight);
451 }
452
453 void bottombar_mousemoved(win *window, GR_EVENT_MOUSE *event)
454 {
455         GR_SIZE newheight;
456         struct pos_size *pos;
457
458         Dprintf("bottombar_mousemoved window %d\n", window->wid);
459
460         if(!window->active) return;
461
462         pos = (struct pos_size *) window->data;
463
464         newheight = event->rooty - pos->yorig;
465
466         GrResizeWindow(window->pid, pos->width, newheight);
467 }
468
469 void rightresize_mousemoved(win *window, GR_EVENT_MOUSE *event)
470 {
471         GR_SIZE newwidth, newheight;
472         struct pos_size *pos;
473
474         Dprintf("rightresize_mousemoved window %d\n", window->wid);
475
476         if(!window->active) return;
477
478         pos = (struct pos_size *) window->data;
479
480         newheight = event->rooty - pos->yorig;
481         newwidth = event->rootx - pos->xorig;
482
483         GrResizeWindow(window->pid, newwidth, newheight);
484 }
485
486 void rightbar_mousemoved(win *window, GR_EVENT_MOUSE *event)
487 {
488         GR_SIZE newwidth;
489         struct pos_size *pos;
490
491         Dprintf("rightbar_mousemoved window %d\n", window->wid);
492
493         if(!window->active) return;
494
495         pos = (struct pos_size *) window->data;
496
497         newwidth = event->rootx - pos->xorig;
498
499         GrResizeWindow(window->pid, newwidth, pos->height);
500
501 }
502 #endif /* 0000*/