]> icculus.org git repositories - dana/openbox.git/blob - openbox/moveresize.c
missing break would be bad in future if added more cases
[dana/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 "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     corner = cnr;
76     button = b;
77
78     /*
79       have to change start_cx and start_cy if going to do this..
80     if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
81         corner == prop_atoms.net_wm_moveresize_size_keyboard)
82         XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
83                      c->area.width / 2, c->area.height / 2);
84     */
85
86     if (corner == prop_atoms.net_wm_moveresize_move ||
87         corner == prop_atoms.net_wm_moveresize_move_keyboard) {
88         cur_x = start_cx;
89         cur_y = start_cy;
90         moving = TRUE;
91     } else {
92         cur_x = start_cw;
93         cur_y = start_ch;
94         moving = FALSE;
95     }
96
97     moveresize_in_progress = TRUE;
98
99     if (corner == prop_atoms.net_wm_moveresize_size_topleft)
100         cur = OB_CURSOR_NORTHWEST;
101     else if (corner == prop_atoms.net_wm_moveresize_size_top)
102         cur = OB_CURSOR_NORTH;
103     else if (corner == prop_atoms.net_wm_moveresize_size_topright)
104         cur = OB_CURSOR_NORTHEAST;
105     else if (corner == prop_atoms.net_wm_moveresize_size_right)
106         cur = OB_CURSOR_EAST;
107     else if (corner == prop_atoms.net_wm_moveresize_size_bottomright)
108         cur = OB_CURSOR_SOUTHEAST;
109     else if (corner == prop_atoms.net_wm_moveresize_size_bottom)
110         cur = OB_CURSOR_SOUTH;
111     else if (corner == prop_atoms.net_wm_moveresize_size_bottomleft)
112         cur = OB_CURSOR_SOUTHWEST;
113     else if (corner == prop_atoms.net_wm_moveresize_size_left)
114         cur = OB_CURSOR_WEST;
115     else if (corner == prop_atoms.net_wm_moveresize_size_keyboard)
116         cur = OB_CURSOR_SOUTHEAST;
117     else if (corner == prop_atoms.net_wm_moveresize_move)
118         cur = OB_CURSOR_MOVE;
119     else if (corner == prop_atoms.net_wm_moveresize_move_keyboard)
120         cur = OB_CURSOR_MOVE;
121     else
122         g_assert_not_reached();
123
124     grab_pointer(TRUE, cur);
125     grab_keyboard(TRUE);
126 }
127
128 void moveresize_end(gboolean cancel)
129 {
130     grab_keyboard(FALSE);
131     grab_pointer(FALSE, None);
132
133     popup_hide(popup);
134
135     if (moving) {
136         client_configure(moveresize_client, OB_CORNER_TOPLEFT,
137                          (cancel ? start_cx : cur_x),
138                          (cancel ? start_cy : cur_y),
139                          start_cw, start_ch, TRUE, TRUE);
140     } else {
141         client_configure(moveresize_client, lockcorner,
142                          moveresize_client->area.x,
143                          moveresize_client->area.y,
144                          (cancel ? start_cw : cur_x),
145                          (cancel ? start_ch : cur_y), TRUE, TRUE);
146     }
147
148     moveresize_in_progress = FALSE;
149     moveresize_client = NULL;
150 }
151
152 static void do_move(gboolean resist)
153 {
154     Rect *a;
155
156     if (resist)
157         resist_move(moveresize_client, &cur_x, &cur_y);
158
159     /* get where the client should be */
160     frame_frame_gravity(moveresize_client->frame, &cur_x, &cur_y);
161     client_configure(moveresize_client, OB_CORNER_TOPLEFT, cur_x, cur_y,
162                      start_cw, start_ch, TRUE, FALSE);
163
164     /* this would be better with a fixed width font ... XXX can do it better
165        if there are 2 text boxes */
166     a = screen_area(screen_desktop);
167     popup_coords("X:  %4d  Y:  %4d",
168                  moveresize_client->frame->area.x - a->x,
169                  moveresize_client->frame->area.y - a->y);
170 }
171
172 static void do_resize(gboolean resist)
173 {
174     if (resist) {
175         /* resist_size needs the frame size */
176         cur_x += moveresize_client->frame->size.left +
177             moveresize_client->frame->size.right;
178         cur_y += moveresize_client->frame->size.top +
179             moveresize_client->frame->size.bottom;
180
181         resist_size(moveresize_client, &cur_x, &cur_y, lockcorner);
182
183         cur_x -= moveresize_client->frame->size.left +
184             moveresize_client->frame->size.right;
185         cur_y -= moveresize_client->frame->size.top +
186             moveresize_client->frame->size.bottom;
187     }
188     
189     client_configure(moveresize_client, lockcorner, 
190                      moveresize_client->area.x, moveresize_client->area.y,
191                      cur_x, cur_y, TRUE, FALSE);
192
193     /* this would be better with a fixed width font ... XXX can do it better
194        if there are 2 text boxes */
195     popup_coords("W:  %4d  H:  %4d", moveresize_client->logical_size.width,
196                  moveresize_client->logical_size.height);
197 }
198
199 void moveresize_event(XEvent *e)
200 {
201     g_assert(moveresize_in_progress);
202
203     if (e->type == ButtonPress) {
204         if (!button) {
205             start_x = e->xbutton.x_root;
206             start_y = e->xbutton.y_root;
207             button = e->xbutton.button; /* this will end it now */
208         }
209     } else if (e->type == ButtonRelease) {
210         if (!button || e->xbutton.button == button) {
211             moveresize_end(FALSE);
212         }
213     } else if (e->type == MotionNotify) {
214         if (moving) {
215             cur_x = start_cx + e->xmotion.x_root - start_x;
216             cur_y = start_cy + e->xmotion.y_root - start_y;
217             do_move(TRUE);
218         } else {
219             if (corner == prop_atoms.net_wm_moveresize_size_topleft) {
220                 cur_x = start_cw - (e->xmotion.x_root - start_x);
221                 cur_y = start_ch - (e->xmotion.y_root - start_y);
222                 lockcorner = OB_CORNER_BOTTOMRIGHT;
223             } else if (corner == prop_atoms.net_wm_moveresize_size_top) {
224                 cur_x = start_cw;
225                 cur_y = start_ch - (e->xmotion.y_root - start_y);
226                 lockcorner = OB_CORNER_BOTTOMRIGHT;
227             } else if (corner == prop_atoms.net_wm_moveresize_size_topright) {
228                 cur_x = start_cw + (e->xmotion.x_root - start_x);
229                 cur_y = start_ch - (e->xmotion.y_root - start_y);
230                 lockcorner = OB_CORNER_BOTTOMLEFT;
231             } else if (corner == prop_atoms.net_wm_moveresize_size_right) { 
232                 cur_x = start_cw + (e->xmotion.x_root - start_x);
233                 cur_y = start_ch;
234                 lockcorner = OB_CORNER_BOTTOMLEFT;
235             } else if (corner ==
236                        prop_atoms.net_wm_moveresize_size_bottomright) {
237                 cur_x = start_cw + (e->xmotion.x_root - start_x);
238                 cur_y = start_ch + (e->xmotion.y_root - start_y);
239                 lockcorner = OB_CORNER_TOPLEFT;
240             } else if (corner == prop_atoms.net_wm_moveresize_size_bottom) {
241                 cur_x = start_cw;
242                 cur_y = start_ch + (e->xmotion.y_root - start_y);
243                 lockcorner = OB_CORNER_TOPLEFT;
244             } else if (corner ==
245                        prop_atoms.net_wm_moveresize_size_bottomleft) {
246                 cur_x = start_cw - (e->xmotion.x_root - start_x);
247                 cur_y = start_ch + (e->xmotion.y_root - start_y);
248                 lockcorner = OB_CORNER_TOPRIGHT;
249             } else if (corner == prop_atoms.net_wm_moveresize_size_left) {
250                 cur_x = start_cw - (e->xmotion.x_root - start_x);
251                 cur_y = start_ch;
252                 lockcorner = OB_CORNER_TOPRIGHT;
253             } else if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
254                 cur_x = start_cw + (e->xmotion.x_root - start_x);
255                 cur_y = start_ch + (e->xmotion.y_root - start_y);
256                 lockcorner = OB_CORNER_TOPLEFT;
257             } else
258                 g_assert_not_reached();
259
260             do_resize(TRUE);
261         }
262     } else if (e->type == KeyPress) {
263         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
264             moveresize_end(TRUE);
265         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
266             moveresize_end(FALSE);
267         else {
268             if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
269                 int dx = 0, dy = 0;
270
271                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
272                     dx = MAX(4, moveresize_client->size_inc.width);
273                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
274                     dx = -MAX(4, moveresize_client->size_inc.width);
275                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
276                     dy = MAX(4, moveresize_client->size_inc.height);
277                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
278                     dy = -MAX(4, moveresize_client->size_inc.height);
279                 else
280                     return;
281
282                 cur_x += dx;
283                 cur_y += dy;
284                 XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
285
286                 do_resize(FALSE);
287             } else if (corner == prop_atoms.net_wm_moveresize_move_keyboard) {
288                 int dx = 0, dy = 0;
289
290                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
291                     dx = 4;
292                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
293                     dx = -4;
294                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
295                     dy = 4;
296                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
297                     dy = -4;
298                 else
299                     return;
300
301                 cur_x += dx;
302                 cur_y += dy;
303                 XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
304
305                 do_move(FALSE);
306             }
307         }
308     }
309 }