1 /*
2 * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "GxPlayHead.h"
20
21 #define P_(s) (s) // FIXME -> gettext
22
23 static gboolean gx_play_head_draw (GtkWidget *widget, cairo_t *cr);
24 static void gx_play_head_get_preferred_width (GtkWidget *widget, gint *min_width, gint *natural_width);
25 static void gx_play_head_get_preferred_height (GtkWidget *widget, gint *min_height, gint *natural_height);
26 static void gx_play_head_size_request (GtkWidget *widget, gint *width, gint *height);
27 static void gx_play_head_render_pixbuf (GtkWidget *widget);
28
G_DEFINE_TYPE(GxPlayHead,gx_play_head,GX_TYPE_REGLER)29 G_DEFINE_TYPE(GxPlayHead, gx_play_head, GX_TYPE_REGLER)
30
31 #define get_stock_id(widget) (GX_PLAYHEAD_CLASS(GTK_WIDGET_GET_CLASS(widget))->stock_id)
32
33 static void gx_play_head_class_init(GxPlayHeadClass *klass)
34 {
35 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
36
37 widget_class->draw = gx_play_head_draw;
38 widget_class->get_preferred_width = gx_play_head_get_preferred_width;
39 widget_class->get_preferred_height = gx_play_head_get_preferred_height;
40 widget_class->button_press_event = NULL;
41 widget_class->button_release_event = NULL;
42 widget_class->motion_notify_event = NULL;
43 widget_class->enter_notify_event = NULL;
44 widget_class->leave_notify_event = NULL;
45 klass->stock_id = "playhead";
46
47 gtk_widget_class_set_css_name(widget_class, "gx-play-head");
48
49 gtk_widget_class_install_style_property(
50 widget_class,
51 g_param_spec_int("phead-width",P_("size of phead"),
52 P_("Width of movable part of playhead"),
53 0, 100, 20, GParamFlags(G_PARAM_READABLE|G_PARAM_STATIC_STRINGS)));
54 }
55
gx_play_head_get_preferred_width(GtkWidget * widget,gint * min_width,gint * natural_width)56 static void gx_play_head_get_preferred_width (GtkWidget *widget, gint *min_width, gint *natural_width)
57 {
58 gint width, height;
59 gx_play_head_size_request(widget, &width, &height);
60
61 if (min_width) {
62 *min_width = width;
63 }
64 if (natural_width) {
65 *natural_width = width;
66 }
67 }
68
gx_play_head_get_preferred_height(GtkWidget * widget,gint * min_height,gint * natural_height)69 static void gx_play_head_get_preferred_height (GtkWidget *widget, gint *min_height, gint *natural_height)
70 {
71 gint width, height;
72 gx_play_head_size_request(widget, &width, &height);
73
74 if (min_height) {
75 *min_height = height;
76 }
77 if (natural_height) {
78 *natural_height = height;
79 }
80 }
81
gx_play_head_size_request(GtkWidget * widget,gint * width,gint * height)82 static void gx_play_head_size_request (GtkWidget *widget, gint *width, gint *height)
83 {
84 g_assert(GX_IS_PLAYHEAD(widget));
85 GxPlayHead *phead = GX_PLAYHEAD(widget);
86 *width = phead->width;
87 *height = phead->height;
88 _gx_regler_calc_size_request(GX_REGLER(widget), width, height, TRUE);
89 }
90
gx_play_head_draw(GtkWidget * widget,cairo_t * cr)91 static gboolean gx_play_head_draw(GtkWidget *widget, cairo_t *cr)
92 {
93 g_assert(GX_IS_PLAYHEAD(widget));
94 GxPlayHead *phead = GX_PLAYHEAD(widget);
95 GtkAllocation allocation;
96 gtk_widget_get_allocation(widget, &allocation);
97 int x = 0;
98 int y = 0;
99 int rect_width = allocation.width;
100 phead->image_rect.x = phead->image_rect.y = 0;
101 gdouble slstate = _gx_regler_get_step_pos(GX_REGLER(widget), rect_width - (phead->height*5));
102 // background
103 phead->scaled_image = gdk_pixbuf_scale_simple(
104 phead->image, rect_width*3, phead->height, GDK_INTERP_NEAREST);
105 gdk_cairo_set_source_pixbuf (cr, phead->scaled_image, x-20, y);
106 cairo_rectangle(cr, x, y, rect_width, phead->height);
107 cairo_fill(cr);
108 //phead
109 gdk_cairo_set_source_pixbuf (cr, phead->image, x -(phead->width/2 + phead->height) + slstate, y);
110 cairo_rectangle(cr, x + slstate, y, (phead->height*3), phead->height);
111 cairo_fill(cr);
112 g_object_unref(phead->scaled_image);
113 return FALSE;
114 }
115
gx_play_head_init(GxPlayHead * playhead)116 static void gx_play_head_init(GxPlayHead *playhead)
117 {
118 GtkWidget *widget = GTK_WIDGET(playhead);
119 gx_play_head_render_pixbuf(widget);
120 g_signal_connect (widget, "style-updated",
121 G_CALLBACK (gx_play_head_render_pixbuf), NULL);
122 }
123
gx_play_head_render_pixbuf(GtkWidget * widget)124 static void gx_play_head_render_pixbuf (GtkWidget *widget)
125 {
126 GxPlayHead *playhead = GX_PLAYHEAD(widget);
127 gtk_widget_style_get(widget, "phead-width", &playhead->phead_width, NULL);
128 playhead->image = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
129 get_stock_id(widget), -1,
130 GTK_ICON_LOOKUP_GENERIC_FALLBACK, nullptr);
131 playhead->height = gdk_pixbuf_get_height(playhead->image);
132 playhead->width = gdk_pixbuf_get_width(playhead->image) - playhead->height*2;
133 GdkRectangle rect;
134 rect.width = playhead->width;
135 rect.height = playhead->height;
136 playhead->image_rect = rect;
137 }
138