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