Mercurial > audlegacy
annotate src/audacious/dock.c @ 3468:440877c9360e trunk
Changed PLAYLIST_{UN}LOCK() macros to use playlist itself as argument, not
the mutex.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 09 Sep 2007 22:05:24 +0300 |
parents | f1c756f39e6c |
children | 9049fae12a72 0dd74d0da472 |
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>. |
3123
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
21 * |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
22 * The Audacious team does not consider modular code linking to |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
23 * Audacious or using our public API to be a derived work. |
2313 | 24 */ |
25 | |
26 #include "dock.h" | |
27 | |
28 #include <gdk/gdk.h> | |
29 #include <stdlib.h> | |
30 #include "main.h" | |
31 | |
32 #include "platform/smartinclude.h" | |
33 | |
34 struct _DockedWindow { | |
35 GtkWindow *w; | |
36 gint offset_x, offset_y; | |
37 }; | |
38 | |
39 typedef struct _DockedWindow DockedWindow; | |
40 | |
41 | |
42 static gint | |
43 docked_list_compare(DockedWindow * a, DockedWindow * b) | |
44 { | |
45 if (a->w == b->w) | |
46 return 0; | |
47 return 1; | |
48 } | |
49 | |
50 static void | |
51 snap_edge(gint * x, gint * y, gint w, gint h, gint bx, gint by, | |
52 gint bw, gint bh) | |
53 { | |
54 gint sd = cfg.snap_distance; | |
55 | |
56 if ((*x + w > bx - sd) && (*x + w < bx + sd) && | |
57 (*y > by - h - sd) && (*y < by + bh + sd)) { | |
58 *x = bx - w; | |
59 if ((*y > by - sd) && (*y < by + sd)) | |
60 *y = by; | |
61 if ((*y + h > by + bh - sd) && (*y + h < by + bh + sd)) | |
62 *y = by + bh - h; | |
63 } | |
64 if ((*x > bx + bw - sd) && (*x < bx + bw + sd) && | |
65 (*y > by - h - sd) && (*y < by + bh + sd)) { | |
66 *x = bx + bw; | |
67 if ((*y > by - sd) && (*y < by + sd)) | |
68 *y = by; | |
69 if ((*y + h > by + bh - sd) && (*y + h < by + bh + sd)) | |
70 *y = by + bh - h; | |
71 } | |
72 } | |
73 | |
74 static void | |
75 snap(gint * x, gint * y, gint w, gint h, gint bx, gint by, gint bw, gint bh) | |
76 { | |
77 snap_edge(x, y, w, h, bx, by, bw, bh); | |
78 snap_edge(y, x, h, w, by, bx, bh, bw); | |
79 } | |
80 | |
81 static void | |
82 calc_snap_offset(GList * dlist, GList * wlist, gint x, gint y, | |
83 gint * off_x, gint * off_y) | |
84 { | |
85 gint nx, ny, nw, nh, sx, sy, sw, sh; | |
86 GtkWindow *w; | |
87 GList *dnode, *wnode; | |
88 DockedWindow temp, *dw; | |
89 | |
90 | |
91 *off_x = 0; | |
92 *off_y = 0; | |
93 | |
94 if (!cfg.snap_windows) | |
95 return; | |
96 | |
97 /* | |
98 * FIXME: Why not break out of the loop when we find someting | |
99 * to snap to? | |
100 */ | |
101 for (dnode = dlist; dnode; dnode = g_list_next(dnode)) { | |
102 dw = dnode->data; | |
103 gtk_window_get_size(dw->w, &nw, &nh); | |
104 | |
105 nx = dw->offset_x + *off_x + x; | |
106 ny = dw->offset_y + *off_y + y; | |
107 | |
108 /* Snap to screen edges */ | |
109 if (abs(nx) < cfg.snap_distance) | |
110 *off_x -= nx; | |
111 if (abs(ny) < cfg.snap_distance) | |
112 *off_y -= ny; | |
113 if (abs(nx + nw - gdk_screen_width()) < cfg.snap_distance) | |
114 *off_x -= nx + nw - gdk_screen_width(); | |
115 if (abs(ny + nh - gdk_screen_height()) < cfg.snap_distance) | |
116 *off_y -= ny + nh - gdk_screen_height(); | |
117 | |
118 /* Snap to other windows */ | |
119 for (wnode = wlist; wnode; wnode = g_list_next(wnode)) { | |
120 temp.w = wnode->data; | |
121 if (g_list_find_custom | |
122 (dlist, &temp, (GCompareFunc) docked_list_compare)) | |
123 /* These windows are already docked */ | |
124 continue; | |
125 | |
126 w = GTK_WINDOW(wnode->data); | |
127 gtk_window_get_position(w, &sx, &sy); | |
128 gtk_window_get_size(w, &sw, &sh); | |
129 | |
130 nx = dw->offset_x + *off_x + x; | |
131 ny = dw->offset_y + *off_y + y; | |
132 | |
133 snap(&nx, &ny, nw, nh, sx, sy, sw, sh); | |
134 | |
135 *off_x += nx - (dw->offset_x + *off_x + x); | |
136 *off_y += ny - (dw->offset_y + *off_y + y); | |
137 } | |
138 } | |
139 } | |
140 | |
141 | |
142 static gboolean | |
143 is_docked(gint a_x, gint a_y, gint a_w, gint a_h, | |
144 gint b_x, gint b_y, gint b_w, gint b_h) | |
145 { | |
146 if (((a_x == b_x + b_w) || (a_x + a_w == b_x)) && | |
147 (b_y + b_h >= a_y) && (b_y <= a_y + a_h)) | |
148 return TRUE; | |
149 | |
150 if (((a_y == b_y + b_h) || (a_y + a_h == b_y)) && | |
151 (b_x >= a_x - b_w) && (b_x <= a_x + a_w)) | |
152 return TRUE; | |
153 | |
154 return FALSE; | |
155 } | |
156 | |
157 /* | |
158 * Builds a list of all windows that are docked to the window "w". | |
159 * Recursively adds all windows that are docked to the windows that are | |
160 * docked to "w" and so on... | |
161 * FIXME: init_off_? ? | |
162 */ | |
163 | |
164 static GList * | |
165 get_docked_list(GList * dlist, GList * wlist, GtkWindow * w, | |
166 gint init_off_x, gint init_off_y) | |
167 { | |
168 GList *node; | |
169 DockedWindow *dwin, temp; | |
170 gint w_x, w_y, w_width, w_height; | |
171 gint t_x, t_y, t_width, t_height; | |
172 | |
173 | |
174 gtk_window_get_position(w, &w_x, &w_y); | |
175 gtk_window_get_size(w, &w_width, &w_height); | |
176 if (!dlist) { | |
177 dwin = g_new0(DockedWindow, 1); | |
178 dwin->w = w; | |
179 dlist = g_list_append(dlist, dwin); | |
180 } | |
181 | |
182 for (node = wlist; node; node = g_list_next(node)) { | |
183 temp.w = node->data; | |
184 if (g_list_find_custom | |
185 (dlist, &temp, (GCompareFunc) docked_list_compare)) | |
186 continue; | |
187 | |
188 gtk_window_get_position(GTK_WINDOW(node->data), &t_x, &t_y); | |
189 gtk_window_get_size(GTK_WINDOW(node->data), &t_width, &t_height); | |
190 if (is_docked | |
191 (w_x, w_y, w_width, w_height, t_x, t_y, t_width, t_height)) { | |
192 dwin = g_new0(DockedWindow, 1); | |
193 dwin->w = node->data; | |
194 | |
195 dwin->offset_x = t_x - w_x + init_off_x; | |
196 dwin->offset_y = t_y - w_y + init_off_y; | |
197 | |
198 dlist = g_list_append(dlist, dwin); | |
199 | |
200 dlist = | |
201 get_docked_list(dlist, wlist, dwin->w, dwin->offset_x, | |
202 dwin->offset_y); | |
203 } | |
204 } | |
205 return dlist; | |
206 } | |
207 | |
208 static void | |
209 free_docked_list(GList * dlist) | |
210 { | |
211 GList *node; | |
212 | |
213 for (node = dlist; node; node = g_list_next(node)) | |
214 g_free(node->data); | |
215 g_list_free(dlist); | |
216 } | |
217 | |
218 static void | |
219 docked_list_move(GList * list, gint x, gint y) | |
220 { | |
221 GList *node; | |
222 DockedWindow *dw; | |
223 | |
224 for (node = list; node; node = g_list_next(node)) { | |
225 dw = node->data; | |
226 gtk_window_move(dw->w, x + dw->offset_x, y + dw->offset_y); | |
227 gdk_flush(); | |
228 } | |
229 } | |
230 | |
231 static GList * | |
232 shade_move_list(GList * list, GtkWindow * widget, gint offset) | |
233 { | |
234 gint x, y, w, h; | |
235 GList *node; | |
236 DockedWindow *dw; | |
237 | |
238 gtk_window_get_position(widget, &x, &y); | |
239 gtk_window_get_size(widget, &w, &h); | |
240 | |
241 | |
242 for (node = list; node;) { | |
243 gint dx, dy, dwidth, dheight; | |
244 | |
245 dw = node->data; | |
246 gtk_window_get_position(dw->w, &dx, &dy); | |
247 gtk_window_get_size(dw->w, &dwidth, &dheight); | |
248 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) && | |
249 ((dx + dwidth) > x && dx < (x + w))) { | |
250 list = g_list_remove_link(list, node); | |
251 g_list_free_1(node); | |
252 | |
253 node = list = shade_move_list(list, dw->w, offset); | |
254 } | |
255 else | |
256 node = g_list_next(node); | |
257 } | |
258 gtk_window_move(widget, x, y + offset); | |
259 return list; | |
260 } | |
261 | |
262 /* | |
263 * Builds a list of the windows in the list of DockedWindows "winlist" | |
264 * that are docked to the top or bottom of the window, and recursively | |
265 * adds all windows that are docked to the top or bottom of that window, | |
266 * and so on... | |
267 * Note: The data in "winlist" is not copied. | |
268 */ | |
269 static GList * | |
270 find_shade_list(GtkWindow * widget, GList * winlist, GList * shade_list) | |
271 { | |
272 gint x, y, w, h; | |
273 gint dx, dy, dwidth, dheight; | |
274 GList *node; | |
275 | |
276 gtk_window_get_position(widget, &x, &y); | |
277 gtk_window_get_size(widget, &w, &h); | |
278 for (node = winlist; node; node = g_list_next(node)) { | |
279 DockedWindow *dw = node->data; | |
280 if (g_list_find_custom | |
281 (shade_list, dw, (GCompareFunc) docked_list_compare)) | |
282 continue; | |
283 gtk_window_get_position(dw->w, &dx, &dy); | |
284 gtk_window_get_size(dw->w, &dwidth, &dheight); | |
285 | |
286 /* FIXME. Is the is_docked() necessary? */ | |
287 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) && | |
288 ((dx + dwidth) > x && dx < (x + w))) { | |
289 shade_list = g_list_append(shade_list, dw); | |
290 shade_list = find_shade_list(dw->w, winlist, shade_list); | |
291 } | |
292 } | |
293 return shade_list; | |
294 } | |
295 | |
296 void | |
297 dock_window_resize(GtkWindow * widget, gint new_w, gint new_h, gint w, gint h) | |
298 { | |
299 gdk_window_set_hints(GTK_WIDGET(widget)->window, 0, 0, MIN(w, new_w), | |
300 MIN(h, new_h), MAX(w, new_w), MAX(h, new_h), | |
301 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE); | |
302 gdk_window_resize(GTK_WIDGET(widget)->window, new_w, new_h); | |
303 gdk_window_set_hints(GTK_WIDGET(widget)->window, 0, 0, new_w, new_h, | |
304 new_w, new_h, GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE); | |
305 } | |
306 | |
307 void | |
308 dock_shade(GList * window_list, GtkWindow * widget, gint new_h) | |
309 { | |
310 gint x, y, w, h, off_y, orig_off_y; | |
311 GList *node, *docked_list, *slist; | |
312 DockedWindow *dw; | |
313 | |
314 gtk_window_get_position(widget, &x, &y); | |
315 gtk_window_get_size(widget, &w, &h); | |
316 | |
317 if (cfg.show_wm_decorations) { | |
318 dock_window_resize(widget, w, new_h, w, h); | |
319 return; | |
320 } | |
321 | |
322 docked_list = get_docked_list(NULL, window_list, widget, 0, 0); | |
323 slist = find_shade_list(widget, docked_list, NULL); | |
324 | |
325 off_y = new_h - h; | |
326 do { | |
327 orig_off_y = off_y; | |
328 for (node = slist; node; node = g_list_next(node)) { | |
329 gint dx, dy, dwidth, dheight; | |
330 | |
331 dw = node->data; | |
332 if (dw->w == widget) | |
333 continue; | |
334 gtk_window_get_position(dw->w, &dx, &dy); | |
335 gtk_window_get_size(dw->w, &dwidth, &dheight); | |
336 if ((dy >= y) && ((dy + off_y + dheight) > gdk_screen_height())) | |
337 off_y -= (dy + off_y + dheight) - gdk_screen_height(); | |
338 else if ((dy >= y) && ((dy + dheight) == gdk_screen_height())) | |
339 off_y = 0; | |
340 | |
341 if (((dy >= y) && ((dy + off_y) < 0))) | |
342 off_y -= dy + off_y; | |
343 if ((dy < y) && ((dy + (off_y - (new_h - h))) < 0)) | |
344 off_y -= dy + (off_y - (new_h - h)); | |
345 } | |
346 } while (orig_off_y != off_y); | |
347 if (slist) { | |
348 GList *mlist = g_list_copy(slist); | |
349 | |
350 /* Remove this widget from the list */ | |
351 for (node = mlist; node; node = g_list_next(node)) { | |
352 dw = node->data; | |
353 if (dw->w == widget) { | |
354 mlist = g_list_remove_link(mlist, node); | |
355 g_list_free_1(node); | |
356 break; | |
357 } | |
358 } | |
359 for (node = mlist; node;) { | |
360 GList *temp; | |
361 gint dx, dy, dwidth, dheight; | |
362 | |
363 dw = node->data; | |
364 | |
365 gtk_window_get_position(dw->w, &dx, &dy); | |
366 gtk_window_get_size(dw->w, &dwidth, &dheight); | |
367 /* | |
368 * Find windows that are directly docked to this window, | |
369 * move it, and any windows docked to that window again | |
370 */ | |
371 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight) && | |
372 ((dx + dwidth) > x && dx < (x + w))) { | |
373 mlist = g_list_remove_link(mlist, node); | |
374 g_list_free_1(node); | |
375 if (dy > y) | |
376 temp = shade_move_list(mlist, dw->w, off_y); | |
377 else if (off_y - (new_h - h) != 0) | |
378 temp = shade_move_list(mlist, dw->w, off_y - (new_h - h)); | |
379 else | |
380 temp = mlist; | |
381 node = mlist = temp; | |
382 } | |
383 else | |
384 node = g_list_next(node); | |
385 } | |
386 g_list_free(mlist); | |
387 } | |
388 g_list_free(slist); | |
389 free_docked_list(docked_list); | |
390 gtk_window_move(widget, x, y + off_y - (new_h - h)); | |
391 dock_window_resize(widget, w, new_h, w, h); | |
392 } | |
393 | |
394 static GList * | |
395 resize_move_list(GList * list, GtkWindow * widget, | |
396 gint offset_x, gint offset_y) | |
397 { | |
398 gint x, y, w, h; | |
399 GList *node; | |
400 DockedWindow *dw; | |
401 | |
402 gtk_window_get_position(widget, &x, &y); | |
403 gtk_window_get_size(widget, &w, &h); | |
404 | |
405 | |
406 for (node = list; node;) { | |
407 gint dx, dy, dwidth, dheight; | |
408 dw = node->data; | |
409 gtk_window_get_position(dw->w, &dx, &dy); | |
410 gtk_window_get_size(dw->w, &dwidth, &dheight); | |
411 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight)) { | |
412 | |
413 list = g_list_remove_link(list, node); | |
414 g_list_free_1(node); | |
415 node = list = resize_move_list(list, dw->w, offset_x, offset_y); | |
416 } | |
417 else | |
418 node = g_list_next(node); | |
419 } | |
420 gtk_window_move(widget, x + offset_x, y + offset_y); | |
421 return list; | |
422 } | |
423 | |
424 static GList * | |
425 resize_calc_offset(GList * list, GtkWindow * widget, | |
426 gint offset_x, gint offset_y, | |
427 gint * goffset_x, gint * goffset_y) | |
428 { | |
429 gint x, y, w, h; | |
430 GList *node; | |
431 DockedWindow *dw; | |
432 | |
433 gtk_window_get_position(widget, &x, &y); | |
434 gtk_window_get_size(widget, &w, &h); | |
435 | |
436 | |
437 for (node = list; node;) { | |
438 gint dx, dy, dwidth, dheight; | |
439 dw = node->data; | |
440 gtk_window_get_position(dw->w, &dx, &dy); | |
441 gtk_window_get_size(dw->w, &dwidth, &dheight); | |
442 if (is_docked(x, y, w, h, dx, dy, dwidth, dheight)) { | |
443 if (dx + offset_x + dwidth > gdk_screen_width()) { | |
444 offset_x -= dx + offset_x + dwidth - gdk_screen_width(); | |
445 (*goffset_x) -= dx + offset_x + dwidth - gdk_screen_width(); | |
446 } | |
447 if (dy + offset_y + dheight > gdk_screen_height()) { | |
448 offset_y -= dy + offset_y + dheight - gdk_screen_height(); | |
449 (*goffset_y) -= dy + offset_y + dheight - gdk_screen_height(); | |
450 } | |
451 list = g_list_remove_link(list, node); | |
452 g_list_free_1(node); | |
453 node = list = | |
454 resize_calc_offset(list, dw->w, offset_x, offset_y, | |
455 goffset_x, goffset_y); | |
456 } | |
457 else | |
458 node = g_list_next(node); | |
459 } | |
460 return list; | |
461 } | |
462 | |
463 void | |
464 dock_move_press(GList * window_list, GtkWindow * w, | |
465 GdkEventButton * event, gboolean move_list) | |
466 { | |
467 gint mx, my; | |
468 DockedWindow *dwin; | |
469 | |
470 if (cfg.show_wm_decorations) | |
471 return; | |
472 | |
473 gtk_window_present(w); | |
474 gdk_window_get_pointer(GTK_WIDGET(w)->window, &mx, &my, NULL); | |
475 gtk_object_set_data(GTK_OBJECT(w), "move_offset_x", GINT_TO_POINTER(mx)); | |
476 gtk_object_set_data(GTK_OBJECT(w), "move_offset_y", GINT_TO_POINTER(my)); | |
477 if (move_list) | |
478 gtk_object_set_data(GTK_OBJECT(w), "docked_list", | |
479 get_docked_list(NULL, window_list, w, 0, 0)); | |
480 else { | |
481 dwin = g_new0(DockedWindow, 1); | |
482 dwin->w = w; | |
483 gtk_object_set_data(GTK_OBJECT(w), "docked_list", | |
484 g_list_append(NULL, dwin)); | |
485 } | |
486 gtk_object_set_data(GTK_OBJECT(w), "window_list", window_list); | |
487 gtk_object_set_data(GTK_OBJECT(w), "is_moving", GINT_TO_POINTER(1)); | |
488 } | |
489 | |
490 void | |
491 dock_move_motion(GtkWindow * w, GdkEventMotion * event) | |
492 { | |
493 gint offset_x, offset_y, win_x, win_y, x, y, mx, my; | |
494 GList *dlist; | |
495 GList *window_list; | |
496 | |
497 gdk_flush(); | |
498 | |
499 if (!gtk_object_get_data(GTK_OBJECT(w), "is_moving")) | |
500 return; | |
501 | |
502 offset_x = | |
503 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_x")); | |
504 offset_y = | |
505 GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(w), "move_offset_y")); | |
506 dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list"); | |
507 window_list = gtk_object_get_data(GTK_OBJECT(w), "window_list"); | |
508 | |
509 gtk_window_get_position(w, &win_x, &win_y); | |
510 | |
511 gdk_window_get_pointer(NULL, &mx, &my, NULL); | |
512 | |
513 x = mx - offset_x; | |
514 y = my - offset_y; | |
515 | |
516 calc_snap_offset(dlist, window_list, x, y, &offset_x, &offset_y); | |
517 x += offset_x; | |
518 y += offset_y; | |
519 | |
520 docked_list_move(dlist, x, y); | |
521 } | |
522 | |
523 void | |
524 dock_move_release(GtkWindow * w) | |
525 { | |
526 GList *dlist; | |
527 gtk_object_remove_data(GTK_OBJECT(w), "is_moving"); | |
528 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_x"); | |
529 gtk_object_remove_data(GTK_OBJECT(w), "move_offset_y"); | |
530 if ((dlist = gtk_object_get_data(GTK_OBJECT(w), "docked_list")) != NULL) | |
531 free_docked_list(dlist); | |
532 gtk_object_remove_data(GTK_OBJECT(w), "docked_list"); | |
533 gtk_object_remove_data(GTK_OBJECT(w), "window_list"); | |
534 } | |
535 | |
536 gboolean | |
537 dock_is_moving(GtkWindow * w) | |
538 { | |
539 if (gtk_object_get_data(GTK_OBJECT(w), "is_moving")) | |
540 return TRUE; | |
541 return FALSE; | |
542 } | |
543 | |
544 GList * | |
545 dock_add_window(GList * list, GtkWindow * window) | |
546 { | |
547 return g_list_append(list, window); | |
548 } | |
549 | |
550 GList * | |
551 dock_remove_window(GList * list, GtkWindow * window) | |
552 { | |
553 return g_list_remove(list, window); | |
554 } | |
555 | |
556 GList * | |
557 dock_window_set_decorated(GList * list, GtkWindow * window, | |
558 gboolean decorated) | |
559 { | |
560 if (gtk_window_get_decorated(window) == decorated) | |
561 return list; | |
562 | |
563 if (decorated) | |
564 list = dock_remove_window(list, window); | |
565 else | |
566 list = dock_add_window(list, window); | |
567 | |
568 gtk_window_set_decorated(window, decorated); | |
569 | |
570 return list; | |
571 } |