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