Mercurial > audlegacy
annotate src/audacious/dock.c @ 3121:3b6d316f8b09 trunk
GPL3 relicensing.
author | William Pitcock <nenolod@atheme-project.org> |
---|---|
date | Fri, 20 Jul 2007 08:59:47 -0500 |
parents | 3149d4b1a9a9 |
children | f1c756f39e6c |
rev | line source |
---|---|
2313 | 1 /* Audacious - Cross-platform multimedia player |
2 * Copyright (C) 2005-2007 Audacious development team | |
3 * | |
4 * Based on BMP: | |
5 * Copyright (C) 2003-2004 BMP development team. | |
6 * | |
7 * Based on XMMS: | |
8 * Copyright (C) 1998-2003 XMMS development team. | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2313
diff
changeset
|
12 * the Free Software Foundation; under version 3 of the License. |
2313 | 13 * |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2313
diff
changeset
|
20 * along with this program. If not, see <http://www.gnu.org/licenses>. |
2313 | 21 */ |
22 | |
23 #include "dock.h" | |
24 | |
25 #include <gdk/gdk.h> | |
26 #include <stdlib.h> | |
27 #include "main.h" | |
28 | |
29 #include "platform/smartinclude.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 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_move_press(GList * window_list, GtkWindow * w, | |
462 GdkEventButton * event, gboolean move_list) | |
463 { | |
464 gint mx, my; | |
465 DockedWindow *dwin; | |
466 | |
467 if (cfg.show_wm_decorations) | |
468 return; | |
469 | |
470 gtk_window_present(w); | |
471 gdk_window_get_pointer(GTK_WIDGET(w)->window, &mx, &my, NULL); | |
472 gtk_object_set_data(GTK_OBJECT(w), "move_offset_x", GINT_TO_POINTER(mx)); | |
473 gtk_object_set_data(GTK_OBJECT(w), "move_offset_y", GINT_TO_POINTER(my)); | |
474 if (move_list) | |
475 gtk_object_set_data(GTK_OBJECT(w), "docked_list", | |
476 get_docked_list(NULL, window_list, w, 0, 0)); | |
477 else { | |
478 dwin = g_new0(DockedWindow, 1); | |
479 dwin->w = w; | |
480 gtk_object_set_data(GTK_OBJECT(w), "docked_list", | |
481 g_list_append(NULL, dwin)); | |
482 } | |
483 gtk_object_set_data(GTK_OBJECT(w), "window_list", window_list); | |
484 gtk_object_set_data(GTK_OBJECT(w), "is_moving", GINT_TO_POINTER(1)); | |
485 } | |
486 | |
487 void | |
488 dock_move_motion(GtkWindow * w, GdkEventMotion * event) | |
489 { | |
490 gint offset_x, offset_y, win_x, win_y, x, y, mx, my; | |
491 GList *dlist; | |
492 GList *window_list; | |
493 | |
494 gdk_flush(); | |
495 | |
496 if (!gtk_object_get_data(GTK_OBJECT(w), "is_moving")) | |
497 return; | |
498 | |
499 offset_x = | |
500 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_x")); | |
501 offset_y = | |
502 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_y")); | |
503 dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list"); | |
504 window_list = gtk_object_get_data(GTK_OBJECT(w), "window_list"); | |
505 | |
506 gtk_window_get_position(w, &win_x, &win_y); | |
507 | |
508 gdk_window_get_pointer(NULL, &mx, &my, NULL); | |
509 | |
510 x = mx - offset_x; | |
511 y = my - offset_y; | |
512 | |
513 calc_snap_offset(dlist, window_list, x, y, &offset_x, &offset_y); | |
514 x += offset_x; | |
515 y += offset_y; | |
516 | |
517 docked_list_move(dlist, x, y); | |
518 } | |
519 | |
520 void | |
521 dock_move_release(GtkWindow * w) | |
522 { | |
523 GList *dlist; | |
524 gtk_object_remove_data(GTK_OBJECT(w), "is_moving"); | |
525 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_x"); | |
526 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_y"); | |
527 if ((dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list")) != NULL) | |
528 free_docked_list(dlist); | |
529 gtk_object_remove_data(GTK_OBJECT(w), "docked_list"); | |
530 gtk_object_remove_data(GTK_OBJECT(w), "window_list"); | |
531 } | |
532 | |
533 gboolean | |
534 dock_is_moving(GtkWindow * w) | |
535 { | |
536 if (gtk_object_get_data(GTK_OBJECT(w), "is_moving")) | |
537 return TRUE; | |
538 return FALSE; | |
539 } | |
540 | |
541 GList * | |
542 dock_add_window(GList * list, GtkWindow * window) | |
543 { | |
544 return g_list_append(list, window); | |
545 } | |
546 | |
547 GList * | |
548 dock_remove_window(GList * list, GtkWindow * window) | |
549 { | |
550 return g_list_remove(list, window); | |
551 } | |
552 | |
553 GList * | |
554 dock_window_set_decorated(GList * list, GtkWindow * window, | |
555 gboolean decorated) | |
556 { | |
557 if (gtk_window_get_decorated(window) == decorated) | |
558 return list; | |
559 | |
560 if (decorated) | |
561 list = dock_remove_window(list, window); | |
562 else | |
563 list = dock_add_window(list, window); | |
564 | |
565 gtk_window_set_decorated(window, decorated); | |
566 | |
567 return list; | |
568 } |