comparison audacious/dock.c @ 1064:13d721835794 trunk

[svn] - revert back to dock.c 2/2 (hope it works)
author nenolod
date Tue, 16 May 2006 17:12:36 -0700
parents
children 2f22435c21b7
comparison
equal deleted inserted replaced
1063:73be9df33f30 1064:13d721835794
1 /* BMP - Cross-platform multimedia player
2 * Copyright (C) 2003-2004 BMP development team.
3 *
4 * Based on XMMS:
5 * Copyright (C) 1998-2003 XMMS development team.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #include "dock.h"
23
24 #include <gdk/gdk.h>
25 #include <stdlib.h>
26 #include "main.h"
27
28 #include <gdk/gdkx.h>
29 #include <X11/Xlib.h>
30
31 struct _DockedWindow {
32 GtkWindow *w;
33 gint offset_x, offset_y;
34 };
35
36 typedef struct _DockedWindow DockedWindow;
37
38
39 static gint
40 docked_list_compare(DockedWindow * a, DockedWindow * b)
41 {
42 if (a->w == b->w)
43 return 0;
44 return 1;
45 }
46
47 static void
48 snap_edge(gint * x, gint * y, gint w, gint h, gint bx, gint by,
49 gint bw, gint bh)
50 {
51 gint sd = cfg.snap_distance;
52
53 if ((*x + w > bx - sd) && (*x + w < bx + sd) &&
54 (*y > by - h - sd) && (*y < by + bh + sd)) {
55 *x = bx - w;
56 if ((*y > by - sd) && (*y < by + sd))
57 *y = by;
58 if ((*y + h > by + bh - sd) && (*y + h < by + bh + sd))
59 *y = by + bh - h;
60 }
61 if ((*x > bx + bw - sd) && (*x < bx + bw + sd) &&
62 (*y > by - h - sd) && (*y < by + bh + sd)) {
63 *x = bx + bw;
64 if ((*y > by - sd) && (*y < by + sd))
65 *y = by;
66 if ((*y + h > by + bh - sd) && (*y + h < by + bh + sd))
67 *y = by + bh - h;
68 }
69 }
70
71 static void
72 snap(gint * x, gint * y, gint w, gint h, gint bx, gint by, gint bw, gint bh)
73 {
74 snap_edge(x, y, w, h, bx, by, bw, bh);
75 snap_edge(y, x, h, w, by, bx, bh, bw);
76 }
77
78 static void
79 calc_snap_offset(GList * dlist, GList * wlist, gint x, gint y,
80 gint * off_x, gint * off_y)
81 {
82 gint nx, ny, nw, nh, sx, sy, sw, sh;
83 GtkWindow *w;
84 GList *dnode, *wnode;
85 DockedWindow temp, *dw;
86
87
88 *off_x = 0;
89 *off_y = 0;
90
91 if (!cfg.snap_windows)
92 return;
93
94 /*
95 * FIXME: Why not break out of the loop when we find someting
96 * to snap to?
97 */
98 for (dnode = dlist; dnode; dnode = g_list_next(dnode)) {
99 dw = dnode->data;
100 gtk_window_get_size(dw->w, &nw, &nh);
101
102 nx = dw->offset_x + *off_x + x;
103 ny = dw->offset_y + *off_y + y;
104
105 /* Snap to screen edges */
106 if (abs(nx) < cfg.snap_distance)
107 *off_x -= nx;
108 if (abs(ny) < cfg.snap_distance)
109 *off_y -= ny;
110 if (abs(nx + nw - gdk_screen_width()) < cfg.snap_distance)
111 *off_x -= nx + nw - gdk_screen_width();
112 if (abs(ny + nh - gdk_screen_height()) < cfg.snap_distance)
113 *off_y -= ny + nh - gdk_screen_height();
114
115 /* Snap to other windows */
116 for (wnode = wlist; wnode; wnode = g_list_next(wnode)) {
117 temp.w = wnode->data;
118 if (g_list_find_custom
119 (dlist, &temp, (GCompareFunc) docked_list_compare))
120 /* These windows are already docked */
121 continue;
122
123 w = GTK_WINDOW(wnode->data);
124 gtk_window_get_position(w, &sx, &sy);
125 gtk_window_get_size(w, &sw, &sh);
126
127 nx = dw->offset_x + *off_x + x;
128 ny = dw->offset_y + *off_y + y;
129
130 snap(&nx, &ny, nw, nh, sx, sy, sw, sh);
131
132 *off_x += nx - (dw->offset_x + *off_x + x);
133 *off_y += ny - (dw->offset_y + *off_y + y);
134 }
135 }
136 }
137
138
139 static gboolean
140 is_docked(gint a_x, gint a_y, gint a_w, gint a_h,
141 gint b_x, gint b_y, gint b_w, gint b_h)
142 {
143 if (((a_x == b_x + b_w) || (a_x + a_w == b_x)) &&
144 (b_y + b_h >= a_y) && (b_y <= a_y + a_h))
145 return TRUE;
146
147 if (((a_y == b_y + b_h) || (a_y + a_h == b_y)) &&
148 (b_x >= a_x - b_w) && (b_x <= a_x + a_w))
149 return TRUE;
150
151 return FALSE;
152 }
153
154 /*
155 * Builds a list of all windows that are docked to the window "w".
156 * Recursively adds all windows that are docked to the windows that are
157 * docked to "w" and so on...
158 * FIXME: init_off_? ?
159 */
160
161 static GList *
162 get_docked_list(GList * dlist, GList * wlist, GtkWindow * w,
163 gint init_off_x, gint init_off_y)
164 {
165 GList *node;
166 DockedWindow *dwin, temp;
167 gint w_x, w_y, w_width, w_height;
168 gint t_x, t_y, t_width, t_height;
169
170
171 gtk_window_get_position(w, &w_x, &w_y);
172 gtk_window_get_size(w, &w_width, &w_height);
173 if (!dlist) {
174 dwin = g_new0(DockedWindow, 1);
175 dwin->w = w;
176 dlist = g_list_append(dlist, dwin);
177 }
178
179 for (node = wlist; node; node = g_list_next(node)) {
180 temp.w = node->data;
181 if (g_list_find_custom
182 (dlist, &temp, (GCompareFunc) docked_list_compare))
183 continue;
184
185 gtk_window_get_position(GTK_WINDOW(node->data), &t_x, &t_y);
186 gtk_window_get_size(GTK_WINDOW(node->data), &t_width, &t_height);
187 if (is_docked
188 (w_x, w_y, w_width, w_height, t_x, t_y, t_width, t_height)) {
189 dwin = g_new0(DockedWindow, 1);
190 dwin->w = node->data;
191
192 dwin->offset_x = t_x - w_x + init_off_x;
193 dwin->offset_y = t_y - w_y + init_off_y;
194
195 dlist = g_list_append(dlist, dwin);
196
197 dlist =
198 get_docked_list(dlist, wlist, dwin->w, dwin->offset_x,
199 dwin->offset_y);
200 }
201 }
202 return dlist;
203 }
204
205 static void
206 free_docked_list(GList * dlist)
207 {
208 GList *node;
209
210 for (node = dlist; node; node = g_list_next(node))
211 g_free(node->data);
212 g_list_free(dlist);
213 }
214
215 static void
216 docked_list_move(GList * list, gint x, gint y)
217 {
218 GList *node;
219 DockedWindow *dw;
220
221 for (node = list; node; node = g_list_next(node)) {
222 dw = node->data;
223 gtk_window_move(dw->w, x + dw->offset_x, y + dw->offset_y);
224 gdk_flush();
225 }
226 }
227
228 static GList *
229 shade_move_list(GList * list, GtkWindow * widget, gint offset)
230 {
231 gint x, y, w, h;
232 GList *node;
233 DockedWindow *dw;
234
235 gtk_window_get_position(widget, &x, &y);
236 gtk_window_get_size(widget, &w, &h);
237
238
239 for (node = list; node;) {
240 gint dx, dy, dwidth, dheight;
241
242 dw = node->data;
243 gtk_window_get_position(dw->w, &dx, &dy);
244 gtk_window_get_size(dw->w, &dwidth, &dheight);
245 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) &&
246 ((dx + dwidth) > x && dx < (x + w))) {
247 list = g_list_remove_link(list, node);
248 g_list_free_1(node);
249
250 node = list = shade_move_list(list, dw->w, offset);
251 }
252 else
253 node = g_list_next(node);
254 }
255 gtk_window_move(widget, x, y + offset);
256 return list;
257 }
258
259 /*
260 * Builds a list of the windows in the list of DockedWindows "winlist"
261 * that are docked to the top or bottom of the window, and recursively
262 * adds all windows that are docked to the top or bottom of that window,
263 * and so on...
264 * Note: The data in "winlist" is not copied.
265 */
266 static GList *
267 find_shade_list(GtkWindow * widget, GList * winlist, GList * shade_list)
268 {
269 gint x, y, w, h;
270 gint dx, dy, dwidth, dheight;
271 GList *node;
272
273 gtk_window_get_position(widget, &x, &y);
274 gtk_window_get_size(widget, &w, &h);
275 for (node = winlist; node; node = g_list_next(node)) {
276 DockedWindow *dw = node->data;
277 if (g_list_find_custom
278 (shade_list, dw, (GCompareFunc) docked_list_compare))
279 continue;
280 gtk_window_get_position(dw->w, &dx, &dy);
281 gtk_window_get_size(dw->w, &dwidth, &dheight);
282
283 /* FIXME. Is the is_docked() necessary? */
284 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) &&
285 ((dx + dwidth) > x && dx < (x + w))) {
286 shade_list = g_list_append(shade_list, dw);
287 shade_list = find_shade_list(dw->w, winlist, shade_list);
288 }
289 }
290 return shade_list;
291 }
292
293 static void
294 dock_window_resize(GtkWindow * widget, gint new_w, gint new_h, gint w, gint h)
295 {
296 gdk_window_set_hints(GTK_WIDGET(widget)->window, 0, 0, MIN(w, new_w),
297 MIN(h, new_h), MAX(w, new_w), MAX(h, new_h),
298 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
299 gdk_window_resize(GTK_WIDGET(widget)->window, new_w, new_h);
300 gdk_window_set_hints(GTK_WIDGET(widget)->window, 0, 0, new_w, new_h,
301 new_w, new_h, GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
302 }
303
304 void
305 dock_shade(GList * window_list, GtkWindow * widget, gint new_h)
306 {
307 gint x, y, w, h, off_y, orig_off_y;
308 GList *node, *docked_list, *slist;
309 DockedWindow *dw;
310
311 gtk_window_get_position(widget, &x, &y);
312 gtk_window_get_size(widget, &w, &h);
313
314 if (cfg.show_wm_decorations) {
315 dock_window_resize(widget, w, new_h, w, h);
316 return;
317 }
318
319 docked_list = get_docked_list(NULL, window_list, widget, 0, 0);
320 slist = find_shade_list(widget, docked_list, NULL);
321
322 off_y = new_h - h;
323 do {
324 orig_off_y = off_y;
325 for (node = slist; node; node = g_list_next(node)) {
326 gint dx, dy, dwidth, dheight;
327
328 dw = node->data;
329 if (dw->w == widget)
330 continue;
331 gtk_window_get_position(dw->w, &dx, &dy);
332 gtk_window_get_size(dw->w, &dwidth, &dheight);
333 if ((dy >= y) && ((dy + off_y + dheight) > gdk_screen_height()))
334 off_y -= (dy + off_y + dheight) - gdk_screen_height();
335 else if ((dy >= y) && ((dy + dheight) == gdk_screen_height()))
336 off_y = 0;
337
338 if (((dy >= y) && ((dy + off_y) < 0)))
339 off_y -= dy + off_y;
340 if ((dy < y) && ((dy + (off_y - (new_h - h))) < 0))
341 off_y -= dy + (off_y - (new_h - h));
342 }
343 } while (orig_off_y != off_y);
344 if (slist) {
345 GList *mlist = g_list_copy(slist);
346
347 /* Remove this widget from the list */
348 for (node = mlist; node; node = g_list_next(node)) {
349 dw = node->data;
350 if (dw->w == widget) {
351 mlist = g_list_remove_link(mlist, node);
352 g_list_free_1(node);
353 break;
354 }
355 }
356 for (node = mlist; node;) {
357 GList *temp;
358 gint dx, dy, dwidth, dheight;
359
360 dw = node->data;
361
362 gtk_window_get_position(dw->w, &dx, &dy);
363 gtk_window_get_size(dw->w, &dwidth, &dheight);
364 /*
365 * Find windows that are directly docked to this window,
366 * move it, and any windows docked to that window again
367 */
368 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) &&
369 ((dx + dwidth) > x && dx < (x + w))) {
370 mlist = g_list_remove_link(mlist, node);
371 g_list_free_1(node);
372 if (dy > y)
373 temp = shade_move_list(mlist, dw->w, off_y);
374 else if (off_y - (new_h - h) != 0)
375 temp = shade_move_list(mlist, dw->w, off_y - (new_h - h));
376 else
377 temp = mlist;
378 node = mlist = temp;
379 }
380 else
381 node = g_list_next(node);
382 }
383 g_list_free(mlist);
384 }
385 g_list_free(slist);
386 free_docked_list(docked_list);
387 gtk_window_move(widget, x, y + off_y - (new_h - h));
388 dock_window_resize(widget, w, new_h, w, h);
389 }
390
391 static GList *
392 resize_move_list(GList * list, GtkWindow * widget,
393 gint offset_x, gint offset_y)
394 {
395 gint x, y, w, h;
396 GList *node;
397 DockedWindow *dw;
398
399 gtk_window_get_position(widget, &x, &y);
400 gtk_window_get_size(widget, &w, &h);
401
402
403 for (node = list; node;) {
404 gint dx, dy, dwidth, dheight;
405 dw = node->data;
406 gtk_window_get_position(dw->w, &dx, &dy);
407 gtk_window_get_size(dw->w, &dwidth, &dheight);
408 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight)) {
409
410 list = g_list_remove_link(list, node);
411 g_list_free_1(node);
412 node = list = resize_move_list(list, dw->w, offset_x, offset_y);
413 }
414 else
415 node = g_list_next(node);
416 }
417 gtk_window_move(widget, x + offset_x, y + offset_y);
418 return list;
419 }
420
421 static GList *
422 resize_calc_offset(GList * list, GtkWindow * widget,
423 gint offset_x, gint offset_y,
424 gint * goffset_x, gint * goffset_y)
425 {
426 gint x, y, w, h;
427 GList *node;
428 DockedWindow *dw;
429
430 gtk_window_get_position(widget, &x, &y);
431 gtk_window_get_size(widget, &w, &h);
432
433
434 for (node = list; node;) {
435 gint dx, dy, dwidth, dheight;
436 dw = node->data;
437 gtk_window_get_position(dw->w, &dx, &dy);
438 gtk_window_get_size(dw->w, &dwidth, &dheight);
439 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight)) {
440 if (dx + offset_x + dwidth > gdk_screen_width()) {
441 offset_x -= dx + offset_x + dwidth - gdk_screen_width();
442 (*goffset_x) -= dx + offset_x + dwidth - gdk_screen_width();
443 }
444 if (dy + offset_y + dheight > gdk_screen_height()) {
445 offset_y -= dy + offset_y + dheight - gdk_screen_height();
446 (*goffset_y) -= dy + offset_y + dheight - gdk_screen_height();
447 }
448 list = g_list_remove_link(list, node);
449 g_list_free_1(node);
450 node = list =
451 resize_calc_offset(list, dw->w, offset_x, offset_y,
452 goffset_x, goffset_y);
453 }
454 else
455 node = g_list_next(node);
456 }
457 return list;
458 }
459
460 void
461 dock_resize(GList * window_list, GtkWindow * widget, gint new_w, gint new_h)
462 {
463 gint x, y, w, h;
464 gint dx, dy, dwidth, dheight;
465 gint off_x, off_y;
466 GList *list, *dlist = NULL, *tlist = NULL, *mlist = NULL, *node;
467 DockedWindow *dw;
468
469 gtk_window_get_position(widget, &x, &y);
470 gtk_window_get_size(widget, &w, &h);
471 if (cfg.show_wm_decorations) {
472 dock_window_resize(widget, new_w, new_h, w, h);
473 return;
474 }
475
476 list = get_docked_list(NULL, window_list, widget, 0, 0);
477
478 off_x = 0;
479 off_y = 0;
480
481 for (node = list; node; node = g_list_next(node)) {
482 dw = node->data;
483 if (dw->w != widget) {
484 gtk_window_get_position(dw->w, &dx, &dy);
485 gtk_window_get_size(dw->w, &dwidth, &dheight);
486 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight))
487 dlist = g_list_append(dlist, dw);
488 else
489 mlist = g_list_append(mlist, dw);
490 }
491 }
492 tlist = g_list_copy(mlist);
493 for (node = dlist; node; node = g_list_next(node)) {
494 gint doff_x, doff_y;
495 dw = node->data;
496 gtk_window_get_position(dw->w, &dx, &dy);
497 gtk_window_get_size(dw->w, &dwidth, &dheight);
498 if (dx - x - w == 0)
499 doff_x = (x + off_x + new_w) - dx;
500 else
501 doff_x = (x + off_x + (dx - x)) - dx;
502
503 if (dy - y - h == 0)
504 doff_y = (y + off_y + new_h) - dy;
505 else
506 doff_y = (y + off_y + (dy - y)) - dy;
507
508 if (dx + doff_x + dwidth > gdk_screen_width()) {
509 off_x -= dx + doff_x + dwidth - gdk_screen_width();
510 doff_x -= dx + doff_x + dwidth - gdk_screen_width();
511 }
512 if (dy + doff_y + dheight > gdk_screen_height()) {
513 off_y -= dy + doff_y + dheight - gdk_screen_height();
514 doff_y -= dy + doff_y + dheight - gdk_screen_height();
515 }
516 tlist =
517 resize_calc_offset(tlist, dw->w, doff_x, doff_y, &off_x, &off_y);
518 }
519 if ((x + off_x + new_w) > gdk_screen_width())
520 off_x -= x + off_x + new_w - gdk_screen_width();
521 if ((y + off_y + new_h) > gdk_screen_height())
522 off_y -= y + off_y + new_h - gdk_screen_height();
523
524 g_list_free(tlist);
525 for (node = dlist; node; node = g_list_next(node)) {
526 gint doff_x, doff_y;
527 dw = node->data;
528 gtk_window_get_position(dw->w, &dx, &dy);
529 if (dx - x - w == 0)
530 doff_x = (x + off_x + new_w) - dx;
531 else
532 doff_x = (x + off_x + (dx - x)) - dx;
533
534 if (dy - y - h == 0)
535 doff_y = (y + off_y + new_h) - dy;
536 else
537 doff_y = (y + off_y + (dy - y)) - dy;
538 mlist = resize_move_list(mlist, dw->w, doff_x, doff_y);
539 gtk_window_move(GTK_WINDOW(dw->w), dx + doff_x, dy + doff_y);
540 }
541
542
543 gtk_window_move(widget, x + off_x, y + off_y);
544 dock_window_resize(widget, new_w, new_h, w, h);
545 }
546
547 void
548 dock_move_press(GList * window_list, GtkWindow * w,
549 GdkEventButton * event, gboolean move_list)
550 {
551 gint mx, my;
552 DockedWindow *dwin;
553
554 if (cfg.show_wm_decorations)
555 return;
556
557 gtk_window_present(w);
558 gdk_window_get_pointer(GTK_WIDGET(w)->window, &mx, &my, NULL);
559 gtk_object_set_data(GTK_OBJECT(w), "move_offset_x", GINT_TO_POINTER(mx));
560 gtk_object_set_data(GTK_OBJECT(w), "move_offset_y", GINT_TO_POINTER(my));
561 if (move_list)
562 gtk_object_set_data(GTK_OBJECT(w), "docked_list",
563 get_docked_list(NULL, window_list, w, 0, 0));
564 else {
565 dwin = g_new0(DockedWindow, 1);
566 dwin->w = w;
567 gtk_object_set_data(GTK_OBJECT(w), "docked_list",
568 g_list_append(NULL, dwin));
569 }
570 gtk_object_set_data(GTK_OBJECT(w), "window_list", window_list);
571 gtk_object_set_data(GTK_OBJECT(w), "is_moving", GINT_TO_POINTER(1));
572 }
573
574 void
575 dock_move_motion(GtkWindow * w, GdkEventMotion * event)
576 {
577 gint offset_x, offset_y, win_x, win_y, x, y, mx, my;
578 GList *dlist;
579 GList *window_list;
580
581 gdk_flush();
582
583 if (!gtk_object_get_data(GTK_OBJECT(w), "is_moving"))
584 return;
585
586 offset_x =
587 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_x"));
588 offset_y =
589 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_y"));
590 dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list");
591 window_list = gtk_object_get_data(GTK_OBJECT(w), "window_list");
592
593 gtk_window_get_position(w, &win_x, &win_y);
594
595 gdk_window_get_pointer(NULL, &mx, &my, NULL);
596
597 x = mx - offset_x;
598 y = my - offset_y;
599
600 calc_snap_offset(dlist, window_list, x, y, &offset_x, &offset_y);
601 x += offset_x;
602 y += offset_y;
603
604 docked_list_move(dlist, x, y);
605 }
606
607 void
608 dock_move_release(GtkWindow * w)
609 {
610 GList *dlist;
611 gtk_object_remove_data(GTK_OBJECT(w), "is_moving");
612 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_x");
613 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_y");
614 if ((dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list")) != NULL)
615 free_docked_list(dlist);
616 gtk_object_remove_data(GTK_OBJECT(w), "docked_list");
617 gtk_object_remove_data(GTK_OBJECT(w), "window_list");
618 }
619
620 gboolean
621 dock_is_moving(GtkWindow * w)
622 {
623 if (gtk_object_get_data(GTK_OBJECT(w), "is_moving"))
624 return TRUE;
625 return FALSE;
626 }
627
628 GList *
629 dock_add_window(GList * list, GtkWindow * window)
630 {
631 return g_list_append(list, window);
632 }
633
634 GList *
635 dock_remove_window(GList * list, GtkWindow * window)
636 {
637 return g_list_remove(list, window);
638 }
639
640 GList *
641 dock_window_set_decorated(GList * list, GtkWindow * window,
642 gboolean decorated)
643 {
644 if (gtk_window_get_decorated(window) == decorated)
645 return list;
646
647 if (decorated)
648 list = dock_remove_window(list, window);
649 else
650 list = dock_add_window(list, window);
651
652 gtk_window_set_decorated(window, decorated);
653
654 return list;
655 }
656 /* BMP - Cross-platform multimedia player
657 * Copyright (C) 2003-2004 BMP development team.
658 *
659 * Based on XMMS:
660 * Copyright (C) 1998-2003 XMMS development team.
661 *
662 * This program is free software; you can redistribute it and/or modify
663 * it under the terms of the GNU General Public License as published by
664 * the Free Software Foundation; either version 2 of the License, or
665 * (at your option) any later version.
666 *
667 * This program is distributed in the hope that it will be useful,
668 * but WITHOUT ANY WARRANTY; without even the implied warranty of
669 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
670 * GNU General Public License for more details.
671 *
672 * You should have received a copy of the GNU General Public License
673 * along with this program; if not, write to the Free Software
674 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
675 */
676
677 #include "dock.h"
678
679 #include <gdk/gdk.h>
680 #include <stdlib.h>
681 #include "main.h"
682
683 #include <gdk/gdkx.h>
684 #include <X11/Xlib.h>
685
686 struct _DockedWindow {
687 GtkWindow *w;
688 gint offset_x, offset_y;
689 };
690
691 typedef struct _DockedWindow DockedWindow;
692
693
694 static gint
695 docked_list_compare(DockedWindow * a, DockedWindow * b)
696 {
697 if (a->w == b->w)
698 return 0;
699 return 1;
700 }
701
702 static void
703 snap_edge(gint * x, gint * y, gint w, gint h, gint bx, gint by,
704 gint bw, gint bh)
705 {
706 gint sd = cfg.snap_distance;
707
708 if ((*x + w > bx - sd) && (*x + w < bx + sd) &&
709 (*y > by - h - sd) && (*y < by + bh + sd)) {
710 *x = bx - w;
711 if ((*y > by - sd) && (*y < by + sd))
712 *y = by;
713 if ((*y + h > by + bh - sd) && (*y + h < by + bh + sd))
714 *y = by + bh - h;
715 }
716 if ((*x > bx + bw - sd) && (*x < bx + bw + sd) &&
717 (*y > by - h - sd) && (*y < by + bh + sd)) {
718 *x = bx + bw;
719 if ((*y > by - sd) && (*y < by + sd))
720 *y = by;
721 if ((*y + h > by + bh - sd) && (*y + h < by + bh + sd))
722 *y = by + bh - h;
723 }
724 }
725
726 static void
727 snap(gint * x, gint * y, gint w, gint h, gint bx, gint by, gint bw, gint bh)
728 {
729 snap_edge(x, y, w, h, bx, by, bw, bh);
730 snap_edge(y, x, h, w, by, bx, bh, bw);
731 }
732
733 static void
734 calc_snap_offset(GList * dlist, GList * wlist, gint x, gint y,
735 gint * off_x, gint * off_y)
736 {
737 gint nx, ny, nw, nh, sx, sy, sw, sh;
738 GtkWindow *w;
739 GList *dnode, *wnode;
740 DockedWindow temp, *dw;
741
742
743 *off_x = 0;
744 *off_y = 0;
745
746 if (!cfg.snap_windows)
747 return;
748
749 /*
750 * FIXME: Why not break out of the loop when we find someting
751 * to snap to?
752 */
753 for (dnode = dlist; dnode; dnode = g_list_next(dnode)) {
754 dw = dnode->data;
755 gtk_window_get_size(dw->w, &nw, &nh);
756
757 nx = dw->offset_x + *off_x + x;
758 ny = dw->offset_y + *off_y + y;
759
760 /* Snap to screen edges */
761 if (abs(nx) < cfg.snap_distance)
762 *off_x -= nx;
763 if (abs(ny) < cfg.snap_distance)
764 *off_y -= ny;
765 if (abs(nx + nw - gdk_screen_width()) < cfg.snap_distance)
766 *off_x -= nx + nw - gdk_screen_width();
767 if (abs(ny + nh - gdk_screen_height()) < cfg.snap_distance)
768 *off_y -= ny + nh - gdk_screen_height();
769
770 /* Snap to other windows */
771 for (wnode = wlist; wnode; wnode = g_list_next(wnode)) {
772 temp.w = wnode->data;
773 if (g_list_find_custom
774 (dlist, &temp, (GCompareFunc) docked_list_compare))
775 /* These windows are already docked */
776 continue;
777
778 w = GTK_WINDOW(wnode->data);
779 gtk_window_get_position(w, &sx, &sy);
780 gtk_window_get_size(w, &sw, &sh);
781
782 nx = dw->offset_x + *off_x + x;
783 ny = dw->offset_y + *off_y + y;
784
785 snap(&nx, &ny, nw, nh, sx, sy, sw, sh);
786
787 *off_x += nx - (dw->offset_x + *off_x + x);
788 *off_y += ny - (dw->offset_y + *off_y + y);
789 }
790 }
791 }
792
793
794 static gboolean
795 is_docked(gint a_x, gint a_y, gint a_w, gint a_h,
796 gint b_x, gint b_y, gint b_w, gint b_h)
797 {
798 if (((a_x == b_x + b_w) || (a_x + a_w == b_x)) &&
799 (b_y + b_h >= a_y) && (b_y <= a_y + a_h))
800 return TRUE;
801
802 if (((a_y == b_y + b_h) || (a_y + a_h == b_y)) &&
803 (b_x >= a_x - b_w) && (b_x <= a_x + a_w))
804 return TRUE;
805
806 return FALSE;
807 }
808
809 /*
810 * Builds a list of all windows that are docked to the window "w".
811 * Recursively adds all windows that are docked to the windows that are
812 * docked to "w" and so on...
813 * FIXME: init_off_? ?
814 */
815
816 static GList *
817 get_docked_list(GList * dlist, GList * wlist, GtkWindow * w,
818 gint init_off_x, gint init_off_y)
819 {
820 GList *node;
821 DockedWindow *dwin, temp;
822 gint w_x, w_y, w_width, w_height;
823 gint t_x, t_y, t_width, t_height;
824
825
826 gtk_window_get_position(w, &w_x, &w_y);
827 gtk_window_get_size(w, &w_width, &w_height);
828 if (!dlist) {
829 dwin = g_new0(DockedWindow, 1);
830 dwin->w = w;
831 dlist = g_list_append(dlist, dwin);
832 }
833
834 for (node = wlist; node; node = g_list_next(node)) {
835 temp.w = node->data;
836 if (g_list_find_custom
837 (dlist, &temp, (GCompareFunc) docked_list_compare))
838 continue;
839
840 gtk_window_get_position(GTK_WINDOW(node->data), &t_x, &t_y);
841 gtk_window_get_size(GTK_WINDOW(node->data), &t_width, &t_height);
842 if (is_docked
843 (w_x, w_y, w_width, w_height, t_x, t_y, t_width, t_height)) {
844 dwin = g_new0(DockedWindow, 1);
845 dwin->w = node->data;
846
847 dwin->offset_x = t_x - w_x + init_off_x;
848 dwin->offset_y = t_y - w_y + init_off_y;
849
850 dlist = g_list_append(dlist, dwin);
851
852 dlist =
853 get_docked_list(dlist, wlist, dwin->w, dwin->offset_x,
854 dwin->offset_y);
855 }
856 }
857 return dlist;
858 }
859
860 static void
861 free_docked_list(GList * dlist)
862 {
863 GList *node;
864
865 for (node = dlist; node; node = g_list_next(node))
866 g_free(node->data);
867 g_list_free(dlist);
868 }
869
870 static void
871 docked_list_move(GList * list, gint x, gint y)
872 {
873 GList *node;
874 DockedWindow *dw;
875
876 for (node = list; node; node = g_list_next(node)) {
877 dw = node->data;
878 gtk_window_move(dw->w, x + dw->offset_x, y + dw->offset_y);
879 gdk_flush();
880 }
881 }
882
883 static GList *
884 shade_move_list(GList * list, GtkWindow * widget, gint offset)
885 {
886 gint x, y, w, h;
887 GList *node;
888 DockedWindow *dw;
889
890 gtk_window_get_position(widget, &x, &y);
891 gtk_window_get_size(widget, &w, &h);
892
893
894 for (node = list; node;) {
895 gint dx, dy, dwidth, dheight;
896
897 dw = node->data;
898 gtk_window_get_position(dw->w, &dx, &dy);
899 gtk_window_get_size(dw->w, &dwidth, &dheight);
900 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) &&
901 ((dx + dwidth) > x && dx < (x + w))) {
902 list = g_list_remove_link(list, node);
903 g_list_free_1(node);
904
905 node = list = shade_move_list(list, dw->w, offset);
906 }
907 else
908 node = g_list_next(node);
909 }
910 gtk_window_move(widget, x, y + offset);
911 return list;
912 }
913
914 /*
915 * Builds a list of the windows in the list of DockedWindows "winlist"
916 * that are docked to the top or bottom of the window, and recursively
917 * adds all windows that are docked to the top or bottom of that window,
918 * and so on...
919 * Note: The data in "winlist" is not copied.
920 */
921 static GList *
922 find_shade_list(GtkWindow * widget, GList * winlist, GList * shade_list)
923 {
924 gint x, y, w, h;
925 gint dx, dy, dwidth, dheight;
926 GList *node;
927
928 gtk_window_get_position(widget, &x, &y);
929 gtk_window_get_size(widget, &w, &h);
930 for (node = winlist; node; node = g_list_next(node)) {
931 DockedWindow *dw = node->data;
932 if (g_list_find_custom
933 (shade_list, dw, (GCompareFunc) docked_list_compare))
934 continue;
935 gtk_window_get_position(dw->w, &dx, &dy);
936 gtk_window_get_size(dw->w, &dwidth, &dheight);
937
938 /* FIXME. Is the is_docked() necessary? */
939 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) &&
940 ((dx + dwidth) > x && dx < (x + w))) {
941 shade_list = g_list_append(shade_list, dw);
942 shade_list = find_shade_list(dw->w, winlist, shade_list);
943 }
944 }
945 return shade_list;
946 }
947
948 static void
949 dock_window_resize(GtkWindow * widget, gint new_w, gint new_h, gint w, gint h)
950 {
951 gdk_window_set_hints(GTK_WIDGET(widget)->window, 0, 0, MIN(w, new_w),
952 MIN(h, new_h), MAX(w, new_w), MAX(h, new_h),
953 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
954 gdk_window_resize(GTK_WIDGET(widget)->window, new_w, new_h);
955 gdk_window_set_hints(GTK_WIDGET(widget)->window, 0, 0, new_w, new_h,
956 new_w, new_h, GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
957 }
958
959 void
960 dock_shade(GList * window_list, GtkWindow * widget, gint new_h)
961 {
962 gint x, y, w, h, off_y, orig_off_y;
963 GList *node, *docked_list, *slist;
964 DockedWindow *dw;
965
966 gtk_window_get_position(widget, &x, &y);
967 gtk_window_get_size(widget, &w, &h);
968
969 if (cfg.show_wm_decorations) {
970 dock_window_resize(widget, w, new_h, w, h);
971 return;
972 }
973
974 docked_list = get_docked_list(NULL, window_list, widget, 0, 0);
975 slist = find_shade_list(widget, docked_list, NULL);
976
977 off_y = new_h - h;
978 do {
979 orig_off_y = off_y;
980 for (node = slist; node; node = g_list_next(node)) {
981 gint dx, dy, dwidth, dheight;
982
983 dw = node->data;
984 if (dw->w == widget)
985 continue;
986 gtk_window_get_position(dw->w, &dx, &dy);
987 gtk_window_get_size(dw->w, &dwidth, &dheight);
988 if ((dy >= y) && ((dy + off_y + dheight) > gdk_screen_height()))
989 off_y -= (dy + off_y + dheight) - gdk_screen_height();
990 else if ((dy >= y) && ((dy + dheight) == gdk_screen_height()))
991 off_y = 0;
992
993 if (((dy >= y) && ((dy + off_y) < 0)))
994 off_y -= dy + off_y;
995 if ((dy < y) && ((dy + (off_y - (new_h - h))) < 0))
996 off_y -= dy + (off_y - (new_h - h));
997 }
998 } while (orig_off_y != off_y);
999 if (slist) {
1000 GList *mlist = g_list_copy(slist);
1001
1002 /* Remove this widget from the list */
1003 for (node = mlist; node; node = g_list_next(node)) {
1004 dw = node->data;
1005 if (dw->w == widget) {
1006 mlist = g_list_remove_link(mlist, node);
1007 g_list_free_1(node);
1008 break;
1009 }
1010 }
1011 for (node = mlist; node;) {
1012 GList *temp;
1013 gint dx, dy, dwidth, dheight;
1014
1015 dw = node->data;
1016
1017 gtk_window_get_position(dw->w, &dx, &dy);
1018 gtk_window_get_size(dw->w, &dwidth, &dheight);
1019 /*
1020 * Find windows that are directly docked to this window,
1021 * move it, and any windows docked to that window again
1022 */
1023 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) &&
1024 ((dx + dwidth) > x && dx < (x + w))) {
1025 mlist = g_list_remove_link(mlist, node);
1026 g_list_free_1(node);
1027 if (dy > y)
1028 temp = shade_move_list(mlist, dw->w, off_y);
1029 else if (off_y - (new_h - h) != 0)
1030 temp = shade_move_list(mlist, dw->w, off_y - (new_h - h));
1031 else
1032 temp = mlist;
1033 node = mlist = temp;
1034 }
1035 else
1036 node = g_list_next(node);
1037 }
1038 g_list_free(mlist);
1039 }
1040 g_list_free(slist);
1041 free_docked_list(docked_list);
1042 gtk_window_move(widget, x, y + off_y - (new_h - h));
1043 dock_window_resize(widget, w, new_h, w, h);
1044 }
1045
1046 static GList *
1047 resize_move_list(GList * list, GtkWindow * widget,
1048 gint offset_x, gint offset_y)
1049 {
1050 gint x, y, w, h;
1051 GList *node;
1052 DockedWindow *dw;
1053
1054 gtk_window_get_position(widget, &x, &y);
1055 gtk_window_get_size(widget, &w, &h);
1056
1057
1058 for (node = list; node;) {
1059 gint dx, dy, dwidth, dheight;
1060 dw = node->data;
1061 gtk_window_get_position(dw->w, &dx, &dy);
1062 gtk_window_get_size(dw->w, &dwidth, &dheight);
1063 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight)) {
1064
1065 list = g_list_remove_link(list, node);
1066 g_list_free_1(node);
1067 node = list = resize_move_list(list, dw->w, offset_x, offset_y);
1068 }
1069 else
1070 node = g_list_next(node);
1071 }
1072 gtk_window_move(widget, x + offset_x, y + offset_y);
1073 return list;
1074 }
1075
1076 static GList *
1077 resize_calc_offset(GList * list, GtkWindow * widget,
1078 gint offset_x, gint offset_y,
1079 gint * goffset_x, gint * goffset_y)
1080 {
1081 gint x, y, w, h;
1082 GList *node;
1083 DockedWindow *dw;
1084
1085 gtk_window_get_position(widget, &x, &y);
1086 gtk_window_get_size(widget, &w, &h);
1087
1088
1089 for (node = list; node;) {
1090 gint dx, dy, dwidth, dheight;
1091 dw = node->data;
1092 gtk_window_get_position(dw->w, &dx, &dy);
1093 gtk_window_get_size(dw->w, &dwidth, &dheight);
1094 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight)) {
1095 if (dx + offset_x + dwidth > gdk_screen_width()) {
1096 offset_x -= dx + offset_x + dwidth - gdk_screen_width();
1097 (*goffset_x) -= dx + offset_x + dwidth - gdk_screen_width();
1098 }
1099 if (dy + offset_y + dheight > gdk_screen_height()) {
1100 offset_y -= dy + offset_y + dheight - gdk_screen_height();
1101 (*goffset_y) -= dy + offset_y + dheight - gdk_screen_height();
1102 }
1103 list = g_list_remove_link(list, node);
1104 g_list_free_1(node);
1105 node = list =
1106 resize_calc_offset(list, dw->w, offset_x, offset_y,
1107 goffset_x, goffset_y);
1108 }
1109 else
1110 node = g_list_next(node);
1111 }
1112 return list;
1113 }
1114
1115 void
1116 dock_resize(GList * window_list, GtkWindow * widget, gint new_w, gint new_h)
1117 {
1118 gint x, y, w, h;
1119 gint dx, dy, dwidth, dheight;
1120 gint off_x, off_y;
1121 GList *list, *dlist = NULL, *tlist = NULL, *mlist = NULL, *node;
1122 DockedWindow *dw;
1123
1124 gtk_window_get_position(widget, &x, &y);
1125 gtk_window_get_size(widget, &w, &h);
1126 if (cfg.show_wm_decorations) {
1127 dock_window_resize(widget, new_w, new_h, w, h);
1128 return;
1129 }
1130
1131 list = get_docked_list(NULL, window_list, widget, 0, 0);
1132
1133 off_x = 0;
1134 off_y = 0;
1135
1136 for (node = list; node; node = g_list_next(node)) {
1137 dw = node->data;
1138 if (dw->w != widget) {
1139 gtk_window_get_position(dw->w, &dx, &dy);
1140 gtk_window_get_size(dw->w, &dwidth, &dheight);
1141 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight))
1142 dlist = g_list_append(dlist, dw);
1143 else
1144 mlist = g_list_append(mlist, dw);
1145 }
1146 }
1147 tlist = g_list_copy(mlist);
1148 for (node = dlist; node; node = g_list_next(node)) {
1149 gint doff_x, doff_y;
1150 dw = node->data;
1151 gtk_window_get_position(dw->w, &dx, &dy);
1152 gtk_window_get_size(dw->w, &dwidth, &dheight);
1153 if (dx - x - w == 0)
1154 doff_x = (x + off_x + new_w) - dx;
1155 else
1156 doff_x = (x + off_x + (dx - x)) - dx;
1157
1158 if (dy - y - h == 0)
1159 doff_y = (y + off_y + new_h) - dy;
1160 else
1161 doff_y = (y + off_y + (dy - y)) - dy;
1162
1163 if (dx + doff_x + dwidth > gdk_screen_width()) {
1164 off_x -= dx + doff_x + dwidth - gdk_screen_width();
1165 doff_x -= dx + doff_x + dwidth - gdk_screen_width();
1166 }
1167 if (dy + doff_y + dheight > gdk_screen_height()) {
1168 off_y -= dy + doff_y + dheight - gdk_screen_height();
1169 doff_y -= dy + doff_y + dheight - gdk_screen_height();
1170 }
1171 tlist =
1172 resize_calc_offset(tlist, dw->w, doff_x, doff_y, &off_x, &off_y);
1173 }
1174 if ((x + off_x + new_w) > gdk_screen_width())
1175 off_x -= x + off_x + new_w - gdk_screen_width();
1176 if ((y + off_y + new_h) > gdk_screen_height())
1177 off_y -= y + off_y + new_h - gdk_screen_height();
1178
1179 g_list_free(tlist);
1180 for (node = dlist; node; node = g_list_next(node)) {
1181 gint doff_x, doff_y;
1182 dw = node->data;
1183 gtk_window_get_position(dw->w, &dx, &dy);
1184 if (dx - x - w == 0)
1185 doff_x = (x + off_x + new_w) - dx;
1186 else
1187 doff_x = (x + off_x + (dx - x)) - dx;
1188
1189 if (dy - y - h == 0)
1190 doff_y = (y + off_y + new_h) - dy;
1191 else
1192 doff_y = (y + off_y + (dy - y)) - dy;
1193 mlist = resize_move_list(mlist, dw->w, doff_x, doff_y);
1194 gtk_window_move(GTK_WINDOW(dw->w), dx + doff_x, dy + doff_y);
1195 }
1196
1197
1198 gtk_window_move(widget, x + off_x, y + off_y);
1199 dock_window_resize(widget, new_w, new_h, w, h);
1200 }
1201
1202 void
1203 dock_move_press(GList * window_list, GtkWindow * w,
1204 GdkEventButton * event, gboolean move_list)
1205 {
1206 gint mx, my;
1207 DockedWindow *dwin;
1208
1209 if (cfg.show_wm_decorations)
1210 return;
1211
1212 gtk_window_present(w);
1213 gdk_window_get_pointer(GTK_WIDGET(w)->window, &mx, &my, NULL);
1214 gtk_object_set_data(GTK_OBJECT(w), "move_offset_x", GINT_TO_POINTER(mx));
1215 gtk_object_set_data(GTK_OBJECT(w), "move_offset_y", GINT_TO_POINTER(my));
1216 if (move_list)
1217 gtk_object_set_data(GTK_OBJECT(w), "docked_list",
1218 get_docked_list(NULL, window_list, w, 0, 0));
1219 else {
1220 dwin = g_new0(DockedWindow, 1);
1221 dwin->w = w;
1222 gtk_object_set_data(GTK_OBJECT(w), "docked_list",
1223 g_list_append(NULL, dwin));
1224 }
1225 gtk_object_set_data(GTK_OBJECT(w), "window_list", window_list);
1226 gtk_object_set_data(GTK_OBJECT(w), "is_moving", GINT_TO_POINTER(1));
1227 }
1228
1229 void
1230 dock_move_motion(GtkWindow * w, GdkEventMotion * event)
1231 {
1232 gint offset_x, offset_y, win_x, win_y, x, y, mx, my;
1233 GList *dlist;
1234 GList *window_list;
1235
1236 gdk_flush();
1237
1238 if (!gtk_object_get_data(GTK_OBJECT(w), "is_moving"))
1239 return;
1240
1241 offset_x =
1242 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_x"));
1243 offset_y =
1244 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_y"));
1245 dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list");
1246 window_list = gtk_object_get_data(GTK_OBJECT(w), "window_list");
1247
1248 gtk_window_get_position(w, &win_x, &win_y);
1249
1250 gdk_window_get_pointer(NULL, &mx, &my, NULL);
1251
1252 x = mx - offset_x;
1253 y = my - offset_y;
1254
1255 calc_snap_offset(dlist, window_list, x, y, &offset_x, &offset_y);
1256 x += offset_x;
1257 y += offset_y;
1258
1259 docked_list_move(dlist, x, y);
1260 }
1261
1262 void
1263 dock_move_release(GtkWindow * w)
1264 {
1265 GList *dlist;
1266 gtk_object_remove_data(GTK_OBJECT(w), "is_moving");
1267 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_x");
1268 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_y");
1269 if ((dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list")) != NULL)
1270 free_docked_list(dlist);
1271 gtk_object_remove_data(GTK_OBJECT(w), "docked_list");
1272 gtk_object_remove_data(GTK_OBJECT(w), "window_list");
1273 }
1274
1275 gboolean
1276 dock_is_moving(GtkWindow * w)
1277 {
1278 if (gtk_object_get_data(GTK_OBJECT(w), "is_moving"))
1279 return TRUE;
1280 return FALSE;
1281 }
1282
1283 GList *
1284 dock_add_window(GList * list, GtkWindow * window)
1285 {
1286 return g_list_append(list, window);
1287 }
1288
1289 GList *
1290 dock_remove_window(GList * list, GtkWindow * window)
1291 {
1292 return g_list_remove(list, window);
1293 }
1294
1295 GList *
1296 dock_window_set_decorated(GList * list, GtkWindow * window,
1297 gboolean decorated)
1298 {
1299 if (gtk_window_get_decorated(window) == decorated)
1300 return list;
1301
1302 if (decorated)
1303 list = dock_remove_window(list, window);
1304 else
1305 list = dock_add_window(list, window);
1306
1307 gtk_window_set_decorated(window, decorated);
1308
1309 return list;
1310 }