wmshutdown: Bump to version 1.6
[dockapps.git] / wmisdn / regions.cc
blob6fae139fd37105510c3ab7fcebeab37a8f209d2d
1 #include <string.h>
2 #include <X11/Xlib.h>
3 #include "regions.h"
5 struct region
7 int id;
8 int x, y, w, h;
9 bool enabled;
11 void (*mouse_in)( int );
12 void (*mouse_out)( int );
13 void (*mouse_click)( int, unsigned int );
15 struct region *next;
18 struct regioned_window
20 Window win;
21 struct region *first_reg;
22 struct regioned_window *next;
26 static struct regioned_window *regioned_windows=NULL;
27 static Display *dpy=NULL;
30 static struct region *region_locate( Window win, int x, int y );
31 static struct region *region_find( Window win, int id );
32 static struct regioned_window *region_get_win( Window win );
34 void region_init( Display *_dpy )
36 dpy = _dpy;
39 void region_add( Window win, int id, int x, int y, int w, int h,
40 void (*mouse_in)(int), void (*mouse_out)(int), void (*mouse_click)(int, unsigned int) )
42 /* cerate the region and set its fields */
43 struct region *reg = new region;
44 reg->id = id;
45 reg->x = x;
46 reg->y = y;
47 reg->w = w;
48 reg->h = h;
49 reg->enabled = true;
50 reg->mouse_in = mouse_in;
51 reg->mouse_out = mouse_out;
52 reg->mouse_click = mouse_click;
54 /* find the regioned_win and insert the new region into its list of regions */
55 struct regioned_window *reg_win = region_get_win(win);
56 reg->next = reg_win->first_reg;
57 reg_win->first_reg = reg;
60 void region_enable( Window win, int id )
62 struct region *reg = region_find( win, id );
63 if( reg != NULL )
64 reg->enabled = true;
67 void region_disable( Window win, int id )
69 struct region *reg = region_find( win, id );
70 if( reg != NULL )
71 reg->enabled = false;
75 bool region_in( Window win, int x, int y )
77 return (region_locate(win,x,y) != NULL);
80 void region_mouse_motion( Window win, int x, int y )
82 struct region *reg;
83 static struct region *last_active_reg=NULL;
84 reg = region_locate( win, x, y );
85 if( reg == last_active_reg )
86 return;
87 if( last_active_reg != NULL )
89 last_active_reg->mouse_out( last_active_reg->id );
90 XUngrabPointer( dpy, CurrentTime );
92 if( reg != NULL )
94 reg->mouse_in( reg->id) ;
95 XGrabPointer( dpy, win, true, PointerMotionMask|ButtonPress, GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
97 last_active_reg = reg;
100 void region_mouse_click( Window win, int x, int y, unsigned int button )
102 struct region *reg;
103 region_mouse_motion( win, x, y );
104 if( (reg=region_locate(win,x,y)) != NULL )
105 reg->mouse_click(reg->id, button);
108 static struct region *region_locate( Window win, int x, int y )
110 struct region *reg = region_get_win(win)->first_reg;
112 while( reg != NULL )
114 if( (x >= reg->x) && (x <= reg->x+reg->w) &&
115 (y >= reg->y) && (y <= reg->y+reg->h ) &&
116 reg->enabled )
117 return reg;
118 reg = reg->next;
120 return NULL;
123 static struct region *region_find( Window win, int id )
125 struct region *reg = region_get_win(win)->first_reg;
127 while( reg != NULL )
129 if( reg->id == id )
130 return reg;
131 reg = reg->next;
133 return NULL;
136 static struct regioned_window *region_get_win( Window win )
138 struct regioned_window *reg_win = regioned_windows;
140 while( reg_win != NULL )
142 if( reg_win->win == win )
143 return reg_win;
144 reg_win = reg_win->next;
147 reg_win = new regioned_window;
148 reg_win->win = win;
149 reg_win->first_reg = NULL;
150 reg_win->next = regioned_windows;
151 regioned_windows = reg_win;
152 return reg_win;