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