]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/moveresize.c
kill non-opaque move/resize
[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     dispatch_move(moveresize_client, &cur_x, &cur_y);
151
152     /* get where the client should be */
153     frame_frame_gravity(moveresize_client->frame, &cur_x, &cur_y);
154     client_configure(moveresize_client, OB_CORNER_TOPLEFT, cur_x, cur_y,
155                      start_cw, start_ch, TRUE, FALSE);
156
157     /* this would be better with a fixed width font ... XXX can do it better
158        if there are 2 text boxes */
159     popup_coords("X:  %4d  Y:  %4d", moveresize_client->frame->area.x,
160                  moveresize_client->frame->area.y);
161 }
162
163 static void do_resize()
164 {
165     /* dispatch_resize needs the frame size */
166     cur_x += moveresize_client->frame->size.left +
167         moveresize_client->frame->size.right;
168     cur_y += moveresize_client->frame->size.top +
169         moveresize_client->frame->size.bottom;
170
171     dispatch_resize(moveresize_client, &cur_x, &cur_y, lockcorner);
172
173     cur_x -= moveresize_client->frame->size.left +
174         moveresize_client->frame->size.right;
175     cur_y -= moveresize_client->frame->size.top +
176         moveresize_client->frame->size.bottom;
177     
178     client_configure(moveresize_client, lockcorner, 
179                      moveresize_client->area.x, moveresize_client->area.y,
180                      cur_x, cur_y, TRUE, FALSE);
181
182     /* this would be better with a fixed width font ... XXX can do it better
183        if there are 2 text boxes */
184     popup_coords("W:  %4d  H:  %4d", moveresize_client->logical_size.width,
185                  moveresize_client->logical_size.height);
186 }
187
188 void moveresize_event(XEvent *e)
189 {
190     g_assert(moveresize_in_progress);
191
192     if (e->type == ButtonPress) {
193         if (!button) {
194             start_x = e->xbutton.x_root;
195             start_y = e->xbutton.y_root;
196             button = e->xbutton.button; /* this will end it now */
197         }
198     } else if (e->type == ButtonRelease) {
199         if (!button || e->xbutton.button == button) {
200             moveresize_end(FALSE);
201         }
202     } else if (e->type == MotionNotify) {
203         if (moving) {
204             cur_x = start_cx + e->xmotion.x_root - start_x;
205             cur_y = start_cy + e->xmotion.y_root - start_y;
206             do_move();
207         } else {
208             if (corner == prop_atoms.net_wm_moveresize_size_topleft) {
209                 cur_x = start_cw - (e->xmotion.x_root - start_x);
210                 cur_y = start_ch - (e->xmotion.y_root - start_y);
211                 lockcorner = OB_CORNER_BOTTOMRIGHT;
212             } else if (corner == prop_atoms.net_wm_moveresize_size_top) {
213                 cur_x = start_cw;
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_topright) {
217                 cur_x = start_cw + (e->xmotion.x_root - start_x);
218                 cur_y = start_ch - (e->xmotion.y_root - start_y);
219                 lockcorner = OB_CORNER_BOTTOMLEFT;
220             } else if (corner == prop_atoms.net_wm_moveresize_size_right) { 
221                 cur_x = start_cw + (e->xmotion.x_root - start_x);
222                 cur_y = start_ch;
223                 lockcorner = OB_CORNER_BOTTOMLEFT;
224             } else if (corner ==
225                        prop_atoms.net_wm_moveresize_size_bottomright) {
226                 cur_x = start_cw + (e->xmotion.x_root - start_x);
227                 cur_y = start_ch + (e->xmotion.y_root - start_y);
228                 lockcorner = OB_CORNER_TOPLEFT;
229             } else if (corner == prop_atoms.net_wm_moveresize_size_bottom) {
230                 cur_x = start_cw;
231                 cur_y = start_ch + (e->xmotion.y_root - start_y);
232                 lockcorner = OB_CORNER_TOPLEFT;
233             } else if (corner ==
234                        prop_atoms.net_wm_moveresize_size_bottomleft) {
235                 cur_x = start_cw - (e->xmotion.x_root - start_x);
236                 cur_y = start_ch + (e->xmotion.y_root - start_y);
237                 lockcorner = OB_CORNER_TOPRIGHT;
238             } else if (corner == prop_atoms.net_wm_moveresize_size_left) {
239                 cur_x = start_cw - (e->xmotion.x_root - start_x);
240                 cur_y = start_ch;
241                 lockcorner = OB_CORNER_TOPRIGHT;
242             } else if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
243                 cur_x = start_cw + (e->xmotion.x_root - start_x);
244                 cur_y = start_ch + (e->xmotion.y_root - start_y);
245                 lockcorner = OB_CORNER_TOPLEFT;
246             } else
247                 g_assert_not_reached();
248
249             do_resize();
250         }
251     } else if (e->type == KeyPress) {
252         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
253             moveresize_end(TRUE);
254         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
255             moveresize_end(FALSE);
256         else {
257             if (corner == prop_atoms.net_wm_moveresize_size_keyboard) {
258                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
259                     cur_x += MAX(4, moveresize_client->size_inc.width);
260                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
261                     cur_x -= MAX(4, moveresize_client->size_inc.width);
262                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
263                     cur_y += MAX(4, moveresize_client->size_inc.height);
264                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
265                     cur_y -= MAX(4, moveresize_client->size_inc.height);
266                 else
267                     return;
268                 do_resize();
269             } else if (corner == prop_atoms.net_wm_moveresize_move_keyboard) {
270                 if (e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
271                     cur_x += 4;
272                 else if (e->xkey.keycode == ob_keycode(OB_KEY_LEFT))
273                     cur_x -= 4;
274                 else if (e->xkey.keycode == ob_keycode(OB_KEY_DOWN))
275                     cur_y += 4;
276                 else if (e->xkey.keycode == ob_keycode(OB_KEY_UP))
277                     cur_y -= 4;
278                 else
279                     return;
280                 do_move();
281             }
282         }
283     }
284 }