]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/moveresize.c
make all the masks unsigned chars for consistancy woot
[mikachu/openbox.git] / openbox / moveresize.c
1 #include "grab.h"
2 #include "framerender.h"
3 #include "screen.h"
4 #include "prop.h"
5 #include "client.h"
6 #include "frame.h"
7 #include "dispatch.h"
8 #include "openbox.h"
9 #include "popup.h"
10 #include "config.h"
11 #include "render/render.h"
12 #include "render/theme.h"
13
14 #include <X11/Xlib.h>
15 #include <glib.h>
16
17 gboolean moveresize_in_progress = FALSE;
18 ObClient *moveresize_client = NULL;
19
20 static gboolean moving = FALSE; /* TRUE - moving, FALSE - resizing */
21
22 static int start_x, start_y, start_cx, start_cy, start_cw, start_ch;
23 static int cur_x, cur_y;
24 static guint button;
25 static guint32 corner;
26 static ObCorner lockcorner;
27
28 static Popup *popup = NULL;
29
30 #define POPUP_X (10)
31 #define POPUP_Y (10)
32
33 void moveresize_startup()
34 {
35     XSetWindowAttributes attrib;
36
37     popup = popup_new(FALSE);
38     popup_size_to_string(popup, "W:  0000  W:  0000");
39
40     attrib.save_under = True;
41 }
42
43 void moveresize_shutdown()
44 {
45     popup_free(popup);
46     popup = NULL;
47 }
48
49 static void popup_coords(char *format, int a, int b)
50 {
51     char *text;
52     Rect *area;
53
54     text = g_strdup_printf(format, a, b);
55     area = screen_physical_area_monitor(0);
56     popup_position(popup, NorthWestGravity,
57                    POPUP_X + area->x, POPUP_Y + area->y);
58     popup_show(popup, text, NULL);
59     g_free(text);
60 }
61
62 void moveresize_start(ObClient *c, int x, int y, guint b, guint32 cnr)
63 {
64     ObCursor cur;
65
66     g_assert(!moveresize_in_progress);
67
68     moveresize_client = c;
69     start_cx = c->frame->area.x;
70     start_cy = c->frame->area.y;
71     start_cw = c->area.width;
72     start_ch = c->area.height;
73     start_x = x;
74     start_y = y;
75     if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
76         corner == prop_atoms.net_wm_moveresize_size_keyboard)
77         button = 0; /* mouse can't end it without being pressed first */
78     else
79         button = b;
80     corner = cnr;
81
82     if (corner == prop_atoms.net_wm_moveresize_move ||
83         corner == prop_atoms.net_wm_moveresize_move_keyboard) {
84         cur_x = start_cx;
85         cur_y = start_cy;
86         moving = TRUE;
87     } else {
88         cur_x = start_cw;
89         cur_y = start_ch;
90         moving = FALSE;
91     }
92
93     moveresize_in_progress = TRUE;
94
95     if (corner == prop_atoms.net_wm_moveresize_size_topleft)
96         cur = OB_CURSOR_NORTHWEST;
97     else if (corner == prop_atoms.net_wm_moveresize_size_top)
98         cur = OB_CURSOR_NORTH;
99     else if (corner == prop_atoms.net_wm_moveresize_size_topright)
100         cur = OB_CURSOR_NORTHEAST;
101     else if (corner == prop_atoms.net_wm_moveresize_size_right)
102         cur = OB_CURSOR_EAST;
103     else if (corner == prop_atoms.net_wm_moveresize_size_bottomright)
104         cur = OB_CURSOR_SOUTHEAST;
105     else if (corner == prop_atoms.net_wm_moveresize_size_bottom)
106         cur = OB_CURSOR_SOUTH;
107     else if (corner == prop_atoms.net_wm_moveresize_size_bottomleft)
108         cur = OB_CURSOR_SOUTHWEST;
109     else if (corner == prop_atoms.net_wm_moveresize_size_left)
110         cur = OB_CURSOR_WEST;
111     else if (corner == prop_atoms.net_wm_moveresize_size_keyboard)
112         cur = OB_CURSOR_SOUTHEAST;
113     else if (corner == prop_atoms.net_wm_moveresize_move)
114         cur = OB_CURSOR_MOVE;
115     else if (corner == prop_atoms.net_wm_moveresize_move_keyboard)
116         cur = OB_CURSOR_MOVE;
117     else
118         g_assert_not_reached();
119
120     grab_pointer(TRUE, cur);
121     grab_keyboard(TRUE);
122 }
123
124 void moveresize_end(gboolean cancel)
125 {
126     grab_keyboard(FALSE);
127     grab_pointer(FALSE, None);
128
129     popup_hide(popup);
130
131     if (moving) {
132         client_configure(moveresize_client, OB_CORNER_TOPLEFT,
133                          (cancel ? start_cx : cur_x),
134                          (cancel ? start_cy : cur_y),
135                          start_cw, start_ch, TRUE, TRUE);
136     } else {
137         client_configure(moveresize_client, lockcorner,
138                          moveresize_client->area.x,
139                          moveresize_client->area.y,
140                          (cancel ? start_cw : cur_x),
141                          (cancel ? start_ch : cur_y), TRUE, TRUE);
142     }
143
144     moveresize_in_progress = FALSE;
145     moveresize_client = NULL;
146 }
147
148 static void do_move()
149 {
150     Rect *a;
151
152     dispatch_move(moveresize_client, &cur_x, &cur_y);
153
154     /* get where the client should be */
155     frame_frame_gravity(moveresize_client->frame, &cur_x, &cur_y);
156     client_configure(moveresize_client, OB_CORNER_TOPLEFT, cur_x, cur_y,
157                      start_cw, start_ch, TRUE, FALSE);
158
159     /* this would be better with a fixed width font ... XXX can do it better
160        if there are 2 text boxes */
161     a = screen_area(screen_desktop);
162     popup_coords("X:  %4d  Y:  %4d",
163                  moveresize_client->frame->area.x - a->x,
164                  moveresize_client->frame->area.y - a->y);
165 }
166
167 static void do_resize()
168 {
169     /* dispatch_resize needs the frame size */
170     cur_x += moveresize_client->frame->size.left +
171         moveresize_client->frame->size.right;
172     cur_y += moveresize_client->frame->size.top +
173         moveresize_client->frame->size.bottom;
174
175     dispatch_resize(moveresize_client, &cur_x, &cur_y, lockcorner);
176
177     cur_x -= moveresize_client->frame->size.left +
178         moveresize_client->frame->size.right;
179     cur_y -= moveresize_client->frame->size.top +
180         moveresize_client->frame->size.bottom;
181     
182     client_configure(moveresize_client, lockcorner, 
183                      moveresize_client->area.x, moveresize_client->area.y,
184                      cur_x, cur_y, TRUE, FALSE);
185
186     /* this would be better with a fixed width font ... XXX can do it better
187        if there are 2 text boxes */
188     popup_coords("W:  %4d  H:  %4d", moveresize_client->logical_size.width,
189                  moveresize_client->logical_size.height);
190 }
191
192 void moveresize_event(XEvent *e)
193 {
194     g_assert(moveresize_in_progress);
195
196     if (e->type == ButtonPress) {
197         if (!button) {
198             start_x = e->xbutton.x_root;
199             start_y = e->xbutton.y_root;
200             button = e->xbutton.button; /* this will end it now */
201         }
202     } else if (e->type == ButtonRelease) {
203         if (!button || e->xbutton.button == button) {
204             moveresize_end(FALSE);
205         }
206     } else if (e->type == MotionNotify) {
207         if (moving) {
208             cur_x = start_cx + e->xmotion.x_root - start_x;
209             cur_y = start_cy + e->xmotion.y_root - start_y;
210             do_move();
211         } else {
212             if (corner == prop_atoms.net_wm_moveresize_size_topleft) {
213                 cur_x = start_cw - (e->xmotion.x_root - start_x);
214                 cur_y = start_ch - (e->xmotion.y_root - start_y);
215                 lockcorner = OB_CORNER_BOTTOMRIGHT;
216             } else if (corner == prop_atoms.net_wm_moveresize_size_top) {
217                 cur_x = start_cw;
218                 cur_y = start_ch - (e->xmotion.y_root - start_y);
219                 lockcorner = OB_CORNER_BOTTOMRIGHT;
220             } else if (corner == prop_atoms.net_wm_moveresize_size_topright) {
221                 cur_x = start_cw + (e->xmotion.x_root - start_x);
222                 cur_y = start_ch - (e->xmotion.y_root - start_y);
223                 lockcorner = OB_CORNER_BOTTOMLEFT;
224             } else if (corner == prop_atoms.net_wm_moveresize_size_right) { 
225                 cur_x = start_cw + (e->xmotion.x_root - start_x);
226                 cur_y = start_ch;
227                 lockcorner = OB_CORNER_BOTTOMLEFT;
228             } else if (corner ==
229                        prop_atoms.net_wm_moveresize_size_bottomright) {
230                 cur_x = start_cw + (e->xmotion.x_root - start_x);
231                 cur_y = start_ch + (e->xmotion.y_root - start_y);
232                 lockcorner = OB_CORNER_TOPLEFT;
233             } else if (corner == prop_atoms.net_wm_moveresize_size_bottom) {
234                 cur_x = start_cw;
235                 cur_y = start_ch + (e->xmotion.y_root - start_y);
236                 lockcorner = OB_CORNER_TOPLEFT;
237             } else if (corner ==
238                        prop_atoms.net_wm_moveresize_size_bottomleft) {
239                 cur_x = start_cw - (e->xmotion.x_root - start_x);
240                 cur_y = start_ch + (e->xmotion.y_root - start_y);
241                 lockcorner = OB_CORNER_TOPRIGHT;
242             } else if (corner == prop_atoms.net_wm_moveresize_size_left) {
243                 cur_x = start_cw - (e->xmotion.x_root - start_x);
244                 cur_y = start_ch;
245                 lockcorner = OB_CORNER_TOPRIGHT;
246             } else if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
247                 cur_x = start_cw + (e->xmotion.x_root - start_x);
248                 cur_y = start_ch + (e->xmotion.y_root - start_y);
249                 lockcorner = OB_CORNER_TOPLEFT;
250             } else
251                 g_assert_not_reached();
252
253             do_resize();
254         }
255     } else if (e->type == KeyPress) {
256         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
257             moveresize_end(TRUE);
258         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
259             moveresize_end(FALSE);
260         else {
261             if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
262                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
263                     cur_x += MAX(4, moveresize_client->size_inc.width);
264                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
265                     cur_x -= MAX(4, moveresize_client->size_inc.width);
266                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
267                     cur_y += MAX(4, moveresize_client->size_inc.height);
268                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
269                     cur_y -= MAX(4, moveresize_client->size_inc.height);
270                 else
271                     return;
272                 do_resize();
273             } else if (corner == prop_atoms.net_wm_moveresize_move_keyboard) {
274                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
275                     cur_x += 4;
276                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
277                     cur_x -= 4;
278                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
279                     cur_y += 4;
280                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
281                     cur_y -= 4;
282                 else
283                     return;
284                 do_move();
285             }
286         }
287     }
288 }