1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpscalebutton.c
5 * Copyright (C) 2008 Sven Neumann <sven@gimp.org>
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
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <gtk/gtk.h>
24
25 #include "widgets-types.h"
26
27 #include "gimpscalebutton.h"
28
29
30 static void gimp_scale_button_value_changed (GtkScaleButton *button,
31 gdouble value);
32 static void gimp_scale_button_update_tooltip (GimpScaleButton *button);
33 static gboolean gimp_scale_button_image_expose (GtkWidget *widget,
34 GdkEventExpose *event,
35 GimpScaleButton *button);
36
37
G_DEFINE_TYPE(GimpScaleButton,gimp_scale_button,GTK_TYPE_SCALE_BUTTON)38 G_DEFINE_TYPE (GimpScaleButton, gimp_scale_button, GTK_TYPE_SCALE_BUTTON)
39
40 #define parent_class gimp_scale_button_parent_class
41
42
43 static void
44 gimp_scale_button_class_init (GimpScaleButtonClass *klass)
45 {
46 GtkScaleButtonClass *button_class = GTK_SCALE_BUTTON_CLASS (klass);
47
48 button_class->value_changed = gimp_scale_button_value_changed;
49 }
50
51 static void
gimp_scale_button_init(GimpScaleButton * button)52 gimp_scale_button_init (GimpScaleButton *button)
53 {
54 GtkWidget *image = gtk_bin_get_child (GTK_BIN (button));
55 GtkWidget *plusminus;
56
57 plusminus = gtk_scale_button_get_plus_button (GTK_SCALE_BUTTON (button));
58 gtk_widget_hide (plusminus);
59 gtk_widget_set_no_show_all (plusminus, TRUE);
60
61 plusminus = gtk_scale_button_get_minus_button (GTK_SCALE_BUTTON (button));
62 gtk_widget_hide (plusminus);
63 gtk_widget_set_no_show_all (plusminus, TRUE);
64
65 g_signal_connect (image, "expose-event",
66 G_CALLBACK (gimp_scale_button_image_expose),
67 button);
68
69 /* GtkScaleButton doesn't emit "value-changed" when the adjustment changes */
70 g_signal_connect (button, "notify::adjustment",
71 G_CALLBACK (gimp_scale_button_update_tooltip),
72 NULL);
73
74 gimp_scale_button_update_tooltip (button);
75 }
76
77 static void
gimp_scale_button_value_changed(GtkScaleButton * button,gdouble value)78 gimp_scale_button_value_changed (GtkScaleButton *button,
79 gdouble value)
80 {
81 if (GTK_SCALE_BUTTON_CLASS (parent_class)->value_changed)
82 GTK_SCALE_BUTTON_CLASS (parent_class)->value_changed (button, value);
83
84 gimp_scale_button_update_tooltip (GIMP_SCALE_BUTTON (button));
85 }
86
87 static void
gimp_scale_button_update_tooltip(GimpScaleButton * button)88 gimp_scale_button_update_tooltip (GimpScaleButton *button)
89 {
90 GtkAdjustment *adj;
91 gchar *text;
92 gdouble value;
93 gdouble lower;
94 gdouble upper;
95
96 adj = gtk_scale_button_get_adjustment (GTK_SCALE_BUTTON (button));
97
98 value = gtk_adjustment_get_value (adj);
99 lower = gtk_adjustment_get_lower (adj);
100 upper = gtk_adjustment_get_upper (adj);
101
102 /* use U+2009 THIN SPACE to separate the percent sign from the number */
103
104 text = g_strdup_printf ("%d\342\200\211%%",
105 (gint) (0.5 + ((value - lower) * 100.0 /
106 (upper - lower))));
107
108 gtk_widget_set_tooltip_text (GTK_WIDGET (button), text);
109 g_free (text);
110 }
111
112 static gboolean
gimp_scale_button_image_expose(GtkWidget * widget,GdkEventExpose * event,GimpScaleButton * button)113 gimp_scale_button_image_expose (GtkWidget *widget,
114 GdkEventExpose *event,
115 GimpScaleButton *button)
116 {
117 GtkStyle *style = gtk_widget_get_style (widget);
118 GtkAllocation allocation;
119 GtkAdjustment *adj;
120 cairo_t *cr;
121 gint value;
122 gint steps;
123 gint i;
124
125 gtk_widget_get_allocation (widget, &allocation);
126
127 steps = MIN (allocation.width, allocation.height) / 2;
128
129 adj = gtk_scale_button_get_adjustment (GTK_SCALE_BUTTON (button));
130
131 if (steps < 1)
132 return TRUE;
133
134 value = 0.5 + ((gtk_adjustment_get_value (adj) -
135 gtk_adjustment_get_lower (adj)) * (gdouble) steps /
136 (gtk_adjustment_get_upper (adj) -
137 gtk_adjustment_get_lower (adj)));
138
139 cr = gdk_cairo_create (gtk_widget_get_window (widget));
140
141 gdk_cairo_rectangle (cr, &event->area);
142 cairo_clip (cr);
143
144 cairo_set_line_width (cr, 0.5);
145
146 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
147 {
148 cairo_translate (cr,
149 allocation.x + allocation.width - 0.5,
150 allocation.y + allocation.height);
151 cairo_scale (cr, -2.0, -2.0);
152 }
153 else
154 {
155 cairo_translate (cr,
156 allocation.x + 0.5,
157 allocation.y + allocation.height);
158 cairo_scale (cr, 2.0, -2.0);
159 }
160
161 for (i = 0; i < value; i++)
162 {
163 cairo_move_to (cr, i, 0);
164 cairo_line_to (cr, i, i + 0.5);
165 }
166
167 gdk_cairo_set_source_color (cr, &style->fg[gtk_widget_get_state (widget)]);
168 cairo_stroke (cr);
169
170 for ( ; i < steps; i++)
171 {
172 cairo_move_to (cr, i, 0);
173 cairo_line_to (cr, i, i + 0.5);
174 }
175
176 gdk_cairo_set_source_color (cr, &style->fg[GTK_STATE_INSENSITIVE]);
177 cairo_stroke (cr);
178
179 cairo_destroy (cr);
180
181 return TRUE;
182 }
183
184 GtkWidget *
gimp_scale_button_new(gdouble value,gdouble min,gdouble max)185 gimp_scale_button_new (gdouble value,
186 gdouble min,
187 gdouble max)
188 {
189 GtkObject *adj;
190 gdouble step;
191
192 g_return_val_if_fail (value >= min && value <= max, NULL);
193
194 step = (max - min) / 10.0;
195 adj = gtk_adjustment_new (value, min, max, step, step, 0);
196
197 return g_object_new (GIMP_TYPE_SCALE_BUTTON,
198 "orientation", GTK_ORIENTATION_HORIZONTAL,
199 "adjustment", adj,
200 "size", GTK_ICON_SIZE_MENU,
201 NULL);
202 }
203