propagate from branch 'im.pidgin.pidgin.mxit' (head 2629bf405a36a13177423301925120fe8...
[pidgin-git.git] / finch / libgnt / gntslider.c
blob63e5d694bf18d410b02fc3de3ee01491a59c2c5f
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntinternal.h"
24 #include "gntcolors.h"
25 #include "gntkeys.h"
26 #include "gntslider.h"
27 #include "gntstyle.h"
29 enum
31 SIG_VALUE_CHANGED,
32 SIGS,
35 static guint signals[SIGS] = { 0 };
37 static GntWidgetClass *parent_class = NULL;
39 /* returns TRUE if the value was changed */
40 static gboolean
41 sanitize_value(GntSlider *slider)
43 if (slider->current < slider->min)
44 slider->current = slider->min;
45 else if (slider->current > slider->max)
46 slider->current = slider->max;
47 else
48 return FALSE;
49 return TRUE;
52 static void
53 redraw_slider(GntSlider *slider)
55 GntWidget *widget = GNT_WIDGET(slider);
56 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_MAPPED))
57 gnt_widget_draw(widget);
60 static void
61 slider_value_changed(GntSlider *slider)
63 g_signal_emit(slider, signals[SIG_VALUE_CHANGED], 0, slider->current);
66 static void
67 gnt_slider_draw(GntWidget *widget)
69 GntSlider *slider = GNT_SLIDER(widget);
70 int attr = 0;
71 int position, size = 0;
73 if (slider->vertical)
74 size = widget->priv.height;
75 else
76 size = widget->priv.width;
78 if (gnt_widget_has_focus(widget))
79 attr |= GNT_COLOR_HIGHLIGHT;
80 else
81 attr |= GNT_COLOR_HIGHLIGHT_D;
83 if (slider->max != slider->min)
84 position = ((size - 1) * (slider->current - slider->min)) / (slider->max - slider->min);
85 else
86 position = 0;
87 if (slider->vertical) {
88 mvwvline(widget->window, size-position, 0, ACS_VLINE | gnt_color_pair(GNT_COLOR_NORMAL) | A_BOLD,
89 position);
90 mvwvline(widget->window, 0, 0, ACS_VLINE | gnt_color_pair(GNT_COLOR_NORMAL),
91 size-position);
92 } else {
93 mvwhline(widget->window, 0, 0, ACS_HLINE | gnt_color_pair(GNT_COLOR_NORMAL) | A_BOLD,
94 position);
95 mvwhline(widget->window, 0, position, ACS_HLINE | gnt_color_pair(GNT_COLOR_NORMAL),
96 size - position);
99 mvwaddch(widget->window,
100 slider->vertical ? (size - position - 1) : 0,
101 slider->vertical ? 0 : position,
102 ACS_CKBOARD | gnt_color_pair(attr));
105 static void
106 gnt_slider_size_request(GntWidget *widget)
108 if (GNT_SLIDER(widget)->vertical) {
109 widget->priv.width = 1;
110 widget->priv.height = 5;
111 } else {
112 widget->priv.width = 5;
113 widget->priv.height = 1;
117 static void
118 gnt_slider_map(GntWidget *widget)
120 if (widget->priv.width == 0 || widget->priv.height == 0)
121 gnt_widget_size_request(widget);
122 GNTDEBUG;
125 static gboolean
126 step_back(GntBindable *bindable, GList *null)
128 GntSlider *slider = GNT_SLIDER(bindable);
129 gnt_slider_advance_step(slider, -1);
130 return TRUE;
133 static gboolean
134 small_step_back(GntBindable *bindable, GList *null)
136 GntSlider *slider = GNT_SLIDER(bindable);
137 gnt_slider_set_value(slider, slider->current - slider->smallstep);
138 return TRUE;
141 static gboolean
142 large_step_back(GntBindable *bindable, GList *null)
144 GntSlider *slider = GNT_SLIDER(bindable);
145 gnt_slider_set_value(slider, slider->current - slider->largestep);
146 return TRUE;
149 static gboolean
150 step_forward(GntBindable *bindable, GList *list)
152 GntSlider *slider = GNT_SLIDER(bindable);
153 gnt_slider_advance_step(slider, 1);
154 return TRUE;
157 static gboolean
158 small_step_forward(GntBindable *bindable, GList *null)
160 GntSlider *slider = GNT_SLIDER(bindable);
161 gnt_slider_set_value(slider, slider->current + slider->smallstep);
162 return TRUE;
165 static gboolean
166 large_step_forward(GntBindable *bindable, GList *null)
168 GntSlider *slider = GNT_SLIDER(bindable);
169 gnt_slider_set_value(slider, slider->current + slider->largestep);
170 return TRUE;
173 static gboolean
174 move_min_value(GntBindable *bindable, GList *null)
176 GntSlider *slider = GNT_SLIDER(bindable);
177 gnt_slider_set_value(slider, slider->min);
178 return TRUE;
181 static gboolean
182 move_max_value(GntBindable *bindable, GList *null)
184 GntSlider *slider = GNT_SLIDER(bindable);
185 gnt_slider_set_value(slider, slider->max);
186 return TRUE;
189 static void
190 gnt_slider_class_init(GntSliderClass *klass)
192 GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
193 parent_class = GNT_WIDGET_CLASS(klass);
194 parent_class->draw = gnt_slider_draw;
195 parent_class->map = gnt_slider_map;
196 parent_class->size_request = gnt_slider_size_request;
198 klass->changed = NULL;
200 signals[SIG_VALUE_CHANGED] =
201 g_signal_new("changed",
202 G_TYPE_FROM_CLASS(klass),
203 G_SIGNAL_RUN_LAST,
204 G_STRUCT_OFFSET(GntSliderClass, changed),
205 NULL, NULL,
206 g_cclosure_marshal_VOID__INT,
207 G_TYPE_NONE, 1, G_TYPE_INT);
209 gnt_bindable_class_register_action(bindable, "step-backward", step_back, GNT_KEY_LEFT, NULL);
210 gnt_bindable_register_binding(bindable, "step-backward", GNT_KEY_DOWN, NULL);
211 gnt_bindable_class_register_action(bindable, "step-forward", step_forward, GNT_KEY_RIGHT, NULL);
212 gnt_bindable_register_binding(bindable, "step-forward", GNT_KEY_UP, NULL);
213 gnt_bindable_class_register_action(bindable, "small-step-backward", small_step_back, GNT_KEY_CTRL_LEFT, NULL);
214 gnt_bindable_register_binding(bindable, "small-step-backward", GNT_KEY_CTRL_DOWN, NULL);
215 gnt_bindable_class_register_action(bindable, "small-step-forward", small_step_forward, GNT_KEY_CTRL_RIGHT, NULL);
216 gnt_bindable_register_binding(bindable, "small-step-forward", GNT_KEY_CTRL_UP, NULL);
217 gnt_bindable_class_register_action(bindable, "large-step-backward", large_step_back, GNT_KEY_PGDOWN, NULL);
218 gnt_bindable_class_register_action(bindable, "large-step-forward", large_step_forward, GNT_KEY_PGUP, NULL);
219 gnt_bindable_class_register_action(bindable, "min-value", move_min_value, GNT_KEY_HOME, NULL);
220 gnt_bindable_class_register_action(bindable, "max-value", move_max_value, GNT_KEY_END, NULL);
222 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), GNT_BINDABLE_CLASS(klass));
225 static void
226 gnt_slider_init(GTypeInstance *instance, gpointer class)
228 GntWidget *widget = GNT_WIDGET(instance);
229 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_SHADOW | GNT_WIDGET_NO_BORDER | GNT_WIDGET_CAN_TAKE_FOCUS);
230 widget->priv.minw = 1;
231 widget->priv.minh = 1;
232 GNTDEBUG;
235 /******************************************************************************
236 * GntSlider API
237 *****************************************************************************/
238 GType
239 gnt_slider_get_gtype(void)
241 static GType type = 0;
243 if(type == 0)
245 static const GTypeInfo info = {
246 sizeof(GntSliderClass),
247 NULL, /* base_init */
248 NULL, /* base_finalize */
249 (GClassInitFunc)gnt_slider_class_init,
250 NULL, /* class_finalize */
251 NULL, /* class_data */
252 sizeof(GntSlider),
253 0, /* n_preallocs */
254 gnt_slider_init, /* instance_init */
255 NULL /* value_table */
258 type = g_type_register_static(GNT_TYPE_WIDGET,
259 "GntSlider",
260 &info, 0);
263 return type;
266 GntWidget *gnt_slider_new(gboolean vertical, int max, int min)
268 GntWidget *widget = g_object_new(GNT_TYPE_SLIDER, NULL);
269 GntSlider *slider = GNT_SLIDER(widget);
271 slider->vertical = vertical;
273 if (vertical) {
274 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_Y);
275 } else {
276 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X);
279 gnt_slider_set_range(slider, max, min);
280 slider->step = 1;
282 return widget;
285 void gnt_slider_set_value(GntSlider *slider, int value)
287 int old;
288 if (slider->current == value)
289 return;
290 old = slider->current;
291 slider->current = value;
292 sanitize_value(slider);
293 if (old == slider->current)
294 return;
295 redraw_slider(slider);
296 slider_value_changed(slider);
299 int gnt_slider_get_value(GntSlider *slider)
301 return slider->current;
304 int gnt_slider_advance_step(GntSlider *slider, int steps)
306 gnt_slider_set_value(slider, slider->current + steps * slider->step);
307 return slider->current;
310 void gnt_slider_set_step(GntSlider *slider, int step)
312 slider->step = step;
315 void gnt_slider_set_small_step(GntSlider *slider, int step)
317 slider->smallstep = step;
320 void gnt_slider_set_large_step(GntSlider *slider, int step)
322 slider->largestep = step;
325 void gnt_slider_set_range(GntSlider *slider, int max, int min)
327 slider->max = MAX(max, min);
328 slider->min = MIN(max, min);
329 sanitize_value(slider);
332 static void
333 update_label(GntSlider *slider, int current_value, GntLabel *label)
335 char value[256];
336 g_snprintf(value, sizeof(value), "%d/%d", current_value, slider->max);
337 gnt_label_set_text(label, value);
340 void gnt_slider_reflect_label(GntSlider *slider, GntLabel *label)
342 g_signal_connect(G_OBJECT(slider), "changed", G_CALLBACK(update_label), label);