comparison src/skins/ui_skinned_playlist_slider.c @ 2620:2d6e08c81c09

add playlistwindow
author Tomasz Mon <desowin@gmail.com>
date Wed, 21 May 2008 21:35:11 +0200
parents
children 32e99af83a3e
comparison
equal deleted inserted replaced
2619:a066a10b2398 2620:2d6e08c81c09
1 /*
2 * Audacious - a cross-platform multimedia player
3 * Copyright (c) 2007 Tomasz Moń
4 *
5 * Based on:
6 * BMP - Cross-platform multimedia player
7 * Copyright (C) 2003-2004 BMP development team.
8 * XMMS:
9 * Copyright (C) 1998-2003 XMMS development team.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; under version 3 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses>.
22 *
23 * The Audacious team does not consider modular code linking to
24 * Audacious or using our public API to be a derived work.
25 */
26
27 #include "ui_skin.h"
28 #include "ui_skinned_playlist_slider.h"
29 #include "ui_playlist.h"
30
31 #define UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), ui_skinned_playlist_slider_get_type(), UiSkinnedPlaylistSliderPrivate))
32 typedef struct _UiSkinnedPlaylistSliderPrivate UiSkinnedPlaylistSliderPrivate;
33
34 enum {
35 REDRAW,
36 LAST_SIGNAL
37 };
38
39 struct _UiSkinnedPlaylistSliderPrivate {
40 SkinPixmapId skin_index;
41 gint width, height;
42
43 gint resize_height;
44 gint move_x;
45 gint prev_y;
46 gint drag_y;
47 };
48
49 static void ui_skinned_playlist_slider_class_init (UiSkinnedPlaylistSliderClass *klass);
50 static void ui_skinned_playlist_slider_init (UiSkinnedPlaylistSlider *playlist_slider);
51 static void ui_skinned_playlist_slider_destroy (GtkObject *object);
52 static void ui_skinned_playlist_slider_realize (GtkWidget *widget);
53 static void ui_skinned_playlist_slider_size_request (GtkWidget *widget, GtkRequisition *requisition);
54 static void ui_skinned_playlist_slider_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
55 static gboolean ui_skinned_playlist_slider_expose (GtkWidget *widget, GdkEventExpose *event);
56 static void ui_skinned_playlist_slider_set_position (GtkWidget *widget, gint y);
57 static gboolean ui_skinned_playlist_slider_button_press (GtkWidget *widget, GdkEventButton *event);
58 static gboolean ui_skinned_playlist_slider_button_release (GtkWidget *widget, GdkEventButton *event);
59 static gboolean ui_skinned_playlist_slider_motion_notify (GtkWidget *widget, GdkEventMotion *event);
60 static void ui_skinned_playlist_slider_redraw (UiSkinnedPlaylistSlider *playlist_slider);
61
62 static GtkWidgetClass *parent_class = NULL;
63 static guint playlist_slider_signals[LAST_SIGNAL] = { 0 };
64
65 GType ui_skinned_playlist_slider_get_type() {
66 static GType playlist_slider_type = 0;
67 if (!playlist_slider_type) {
68 static const GTypeInfo playlist_slider_info = {
69 sizeof (UiSkinnedPlaylistSliderClass),
70 NULL,
71 NULL,
72 (GClassInitFunc) ui_skinned_playlist_slider_class_init,
73 NULL,
74 NULL,
75 sizeof (UiSkinnedPlaylistSlider),
76 0,
77 (GInstanceInitFunc) ui_skinned_playlist_slider_init,
78 };
79 playlist_slider_type = g_type_register_static (GTK_TYPE_WIDGET, "UiSkinnedPlaylistSlider", &playlist_slider_info, 0);
80 }
81
82 return playlist_slider_type;
83 }
84
85 static void ui_skinned_playlist_slider_class_init(UiSkinnedPlaylistSliderClass *klass) {
86 GObjectClass *gobject_class;
87 GtkObjectClass *object_class;
88 GtkWidgetClass *widget_class;
89
90 gobject_class = G_OBJECT_CLASS(klass);
91 object_class = (GtkObjectClass*) klass;
92 widget_class = (GtkWidgetClass*) klass;
93 parent_class = gtk_type_class (gtk_widget_get_type ());
94
95 object_class->destroy = ui_skinned_playlist_slider_destroy;
96
97 widget_class->realize = ui_skinned_playlist_slider_realize;
98 widget_class->expose_event = ui_skinned_playlist_slider_expose;
99 widget_class->size_request = ui_skinned_playlist_slider_size_request;
100 widget_class->size_allocate = ui_skinned_playlist_slider_size_allocate;
101 widget_class->button_press_event = ui_skinned_playlist_slider_button_press;
102 widget_class->button_release_event = ui_skinned_playlist_slider_button_release;
103 widget_class->motion_notify_event = ui_skinned_playlist_slider_motion_notify;
104
105 klass->redraw = ui_skinned_playlist_slider_redraw;
106
107 playlist_slider_signals[REDRAW] =
108 g_signal_new ("redraw", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
109 G_STRUCT_OFFSET (UiSkinnedPlaylistSliderClass, redraw), NULL, NULL,
110 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0);
111
112 g_type_class_add_private (gobject_class, sizeof (UiSkinnedPlaylistSliderPrivate));
113 }
114
115 static void ui_skinned_playlist_slider_init(UiSkinnedPlaylistSlider *playlist_slider) {
116 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(playlist_slider);
117 playlist_slider->pressed = FALSE;
118 priv->resize_height = 0;
119 priv->move_x = 0;
120 priv->drag_y = 0;
121 priv->prev_y = 0;
122 }
123
124 GtkWidget* ui_skinned_playlist_slider_new(GtkWidget *fixed, gint x, gint y, gint h) {
125
126 UiSkinnedPlaylistSlider *hs = g_object_new (ui_skinned_playlist_slider_get_type (), NULL);
127 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(hs);
128
129 hs->x = x;
130 hs->y = y;
131 priv->width = 8;
132 priv->height = h;
133 priv->skin_index = SKIN_PLEDIT;
134
135 gtk_fixed_put(GTK_FIXED(fixed), GTK_WIDGET(hs), hs->x, hs->y);
136
137 return GTK_WIDGET(hs);
138 }
139
140 static void ui_skinned_playlist_slider_destroy(GtkObject *object) {
141 UiSkinnedPlaylistSlider *playlist_slider;
142
143 g_return_if_fail (object != NULL);
144 g_return_if_fail (UI_SKINNED_IS_PLAYLIST_SLIDER (object));
145
146 playlist_slider = UI_SKINNED_PLAYLIST_SLIDER (object);
147
148 if (GTK_OBJECT_CLASS (parent_class)->destroy)
149 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
150 }
151
152 static void ui_skinned_playlist_slider_realize(GtkWidget *widget) {
153 UiSkinnedPlaylistSlider *playlist_slider;
154 GdkWindowAttr attributes;
155 gint attributes_mask;
156
157 g_return_if_fail (widget != NULL);
158 g_return_if_fail (UI_SKINNED_IS_PLAYLIST_SLIDER(widget));
159
160 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
161 playlist_slider = UI_SKINNED_PLAYLIST_SLIDER(widget);
162
163 attributes.x = widget->allocation.x;
164 attributes.y = widget->allocation.y;
165 attributes.width = widget->allocation.width;
166 attributes.height = widget->allocation.height;
167 attributes.wclass = GDK_INPUT_OUTPUT;
168 attributes.window_type = GDK_WINDOW_CHILD;
169 attributes.event_mask = gtk_widget_get_events(widget);
170 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
171 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK;
172 attributes.visual = gtk_widget_get_visual(widget);
173 attributes.colormap = gtk_widget_get_colormap(widget);
174
175 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
176 widget->window = gdk_window_new(widget->parent->window, &attributes, attributes_mask);
177
178 widget->style = gtk_style_attach(widget->style, widget->window);
179 gdk_window_set_user_data(widget->window, widget);
180 }
181
182 static void ui_skinned_playlist_slider_size_request(GtkWidget *widget, GtkRequisition *requisition) {
183 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(widget);
184
185 requisition->width = priv->width;
186 requisition->height = priv->height;
187 }
188
189 static void ui_skinned_playlist_slider_size_allocate(GtkWidget *widget, GtkAllocation *allocation) {
190 UiSkinnedPlaylistSlider *playlist_slider = UI_SKINNED_PLAYLIST_SLIDER (widget);
191 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(playlist_slider);
192
193 widget->allocation = *allocation;
194 if (GTK_WIDGET_REALIZED (widget))
195 gdk_window_move_resize(widget->window, widget->allocation.x, widget->allocation.y, allocation->width, allocation->height);
196
197 if (playlist_slider->x + priv->move_x == widget->allocation.x)
198 priv->move_x = 0;
199 playlist_slider->x = widget->allocation.x;
200 playlist_slider->y = widget->allocation.y;
201
202 if (priv->height != widget->allocation.height) {
203 priv->height = priv->height + priv->resize_height;
204 priv->resize_height = 0;
205 gtk_widget_queue_draw(widget);
206 }
207 }
208
209 static gboolean ui_skinned_playlist_slider_expose(GtkWidget *widget, GdkEventExpose *event) {
210 g_return_val_if_fail (widget != NULL, FALSE);
211 g_return_val_if_fail (UI_SKINNED_IS_PLAYLIST_SLIDER (widget), FALSE);
212 g_return_val_if_fail (event != NULL, FALSE);
213
214 UiSkinnedPlaylistSlider *ps = UI_SKINNED_PLAYLIST_SLIDER (widget);
215 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(ps);
216 g_return_val_if_fail (priv->width > 0 && priv->height > 0, FALSE);
217
218 GdkPixbuf *obj = NULL;
219 obj = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, priv->width, priv->height);
220
221 gint num_visible;
222 num_visible = playlistwin_list_get_visible_count();
223
224
225 Playlist *playlist = aud_playlist_get_active();
226
227 gint y;
228 if (aud_playlist_get_length(playlist) > num_visible)
229 y = (playlistwin_list_get_first() * (priv->height - 19)) /
230 (aud_playlist_get_length(playlist) - num_visible);
231 else
232 y = 0;
233
234 if (y < 0) y=0;
235 if (y > priv->height - 19) y = priv->height - 19;
236
237 priv->prev_y = y;
238
239 /* FIXME: uses aud_active_skin->pixmaps directly and may need calibration */
240 /* drawing background */
241 gint c;
242 for (c = 0; c < priv->height / 29; c++) {
243 gdk_pixbuf_copy_area(aud_active_skin->pixmaps[SKIN_PLEDIT].pixbuf,
244 36, 42, priv->width, 29, obj, 0, c*29);
245 }
246
247 /* drawing knob */
248 skin_draw_pixbuf(widget, aud_active_skin, obj, priv->skin_index, ps->pressed ? 61 : 52, 53, 0, y, priv->width, 18);
249
250 ui_skinned_widget_draw(widget, obj, priv->width, priv->height, FALSE);
251
252 g_object_unref(obj);
253
254 return FALSE;
255 }
256
257 static void ui_skinned_playlist_slider_set_position(GtkWidget *widget, gint y) {
258 gint pos;
259 Playlist *playlist = aud_playlist_get_active();
260 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(widget);
261
262 y = CLAMP(y, 0, priv->height - 19);
263
264 pos = (y * (aud_playlist_get_length(playlist) - playlistwin_list_get_visible_count())) / (priv->height - 19);
265 playlistwin_set_toprow(pos);
266
267 gtk_widget_queue_draw(widget);
268 }
269
270 static gboolean ui_skinned_playlist_slider_button_press(GtkWidget *widget, GdkEventButton *event) {
271 UiSkinnedPlaylistSlider *ps = UI_SKINNED_PLAYLIST_SLIDER (widget);
272 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(widget);
273
274 if (event->button != 1 && event->button != 2)
275 return TRUE;
276
277 gint y = event->y;
278 if (event->type == GDK_BUTTON_PRESS) {
279 ps->pressed = TRUE;
280 if ((y >= priv->prev_y && y < priv->prev_y + 18)) {
281 priv->drag_y = y - priv->prev_y;
282 } else if (event->button == 2) {
283 ui_skinned_playlist_slider_set_position(widget, y);
284 priv->drag_y = 0;
285 } else {
286 gint n = playlistwin_list_get_visible_count() / 2;
287 if (y < priv->prev_y)
288 n *= -1;
289 playlistwin_scroll(n);
290 }
291 gtk_widget_queue_draw(widget);
292 }
293 return TRUE;
294 }
295
296 static gboolean ui_skinned_playlist_slider_button_release(GtkWidget *widget, GdkEventButton *event) {
297 UiSkinnedPlaylistSlider *ps = UI_SKINNED_PLAYLIST_SLIDER(widget);
298
299 if (event->button == 1 || event->button == 2) {
300 ps->pressed = FALSE;
301 gtk_widget_queue_draw(widget);
302 }
303 return TRUE;
304 }
305
306 static gboolean ui_skinned_playlist_slider_motion_notify(GtkWidget *widget, GdkEventMotion *event) {
307 UiSkinnedPlaylistSlider *ps = UI_SKINNED_PLAYLIST_SLIDER(widget);
308 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(widget);
309
310 if (ps->pressed) {
311 gint y = event->y - priv->drag_y;
312 ui_skinned_playlist_slider_set_position(widget, y);
313 }
314 return TRUE;
315 }
316
317 static void ui_skinned_playlist_slider_redraw(UiSkinnedPlaylistSlider *playlist_slider) {
318 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(playlist_slider);
319
320 if (priv->resize_height)
321 gtk_widget_set_size_request(GTK_WIDGET(playlist_slider), priv->width, priv->height+priv->resize_height);
322 if (priv->move_x)
323 gtk_fixed_move(GTK_FIXED(gtk_widget_get_parent(GTK_WIDGET(playlist_slider))), GTK_WIDGET(playlist_slider),
324 playlist_slider->x+priv->move_x, playlist_slider->y);
325
326 gtk_widget_queue_draw(GTK_WIDGET(playlist_slider));
327 }
328
329 void ui_skinned_playlist_slider_move_relative(GtkWidget *widget, gint x) {
330 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(widget);
331 priv->move_x += x;
332 }
333
334 void ui_skinned_playlist_slider_resize_relative(GtkWidget *widget, gint h) {
335 UiSkinnedPlaylistSliderPrivate *priv = UI_SKINNED_PLAYLIST_SLIDER_GET_PRIVATE(widget);
336 priv->resize_height += h;
337 }