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