]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/snap.c
snap window-to-window
[mikachu/openbox.git] / openbox / snap.c
1 #include "client.h"
2 #include "frame.h"
3 #include "stacking.h"
4 #include "screen.h"
5
6 static int resistance = 10;
7 static gboolean edge_resistance = TRUE; /* window-to-edge */
8 static gboolean window_resistance = TRUE; /* window-to-window */
9
10 void snap_move(Client *c, int *x, int *y, int w, int h)
11 {
12     GList *it;
13     Rect *area;
14     int l, t, r, b; /* requested edges */
15     int al, at, ar, ab; /* screen area edges */
16     int cl, ct, cr, cb; /* current edges */
17     gboolean snapx = FALSE, snapy = FALSE;
18
19     if (!edge_resistance) return;
20
21     /* add the frame to the dimensions */
22     l = *x;
23     t = *y;
24     r = l + w - 1;
25     b = t + h - 1;
26
27     cl = c->frame->area.x;
28     ct = c->frame->area.y;
29     cr = cl + c->frame->area.width - 1;
30     cb = ct + c->frame->area.height - 1;
31     
32     /* snap to other clients */
33     if (window_resistance)
34         for (it = stacking_list; it != NULL; it = it->next) {
35             Client *target;
36             int tl, tt, tr, tb; /* 1 past the target's edges on each side */
37
38             target = it->data;
39
40             tl = target->frame->area.x - 1;
41             tt = target->frame->area.y - 1;
42             tr = tl + target->frame->area.width + 1;
43             tb = tt + target->frame->area.height + 1;
44
45             /* snapx and snapy ensure that the window snaps to the top-most
46                window edge available, without going all the way from
47                bottom-to-top in the stacking list
48             */
49             if (!snapx && cl >= tr && l < tr && l >= tr - resistance)
50                 *x = tr, snapx = TRUE;
51             else if (!snapx && cr <= tl && r > tl && r <= tl + resistance)
52                 *x = tl - w + 1, snapx = TRUE;
53             else if (!snapy && ct >= tb && t < tb && t >= tb - resistance)
54                 *y = tb, snapy = TRUE;
55             else if (!snapy && cb <= tt && b > tt && b <= tt + resistance)
56                 *y = tt - h + 1, snapy = TRUE;
57
58             if (snapx && snapy) break;
59         }
60
61     /* get the screen boundaries */
62     area = screen_area(c->desktop);
63     al = area->x;
64     at = area->y;
65     ar = al + area->width - 1;
66     ab = at + area->height - 1;
67
68     /* snap to screen edges */
69     if (cl >= al && l < al && l >= al - resistance)
70         *x = al;
71     else if (cr <= ar && r > ar && r <= ar + resistance)
72             *x = ar - w + 1;
73     if (ct >= at && t < at && t >= at - resistance)
74         *y = at;
75     else if (cb <= ab && b > ab && b < ab + resistance)
76         *y = ab - h + 1;
77 }