2
* nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
5
* Copyright (C) 2004 NVIDIA Corporation.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of Version 2 of the GNU General Public
9
* License as published by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See Version 2
14
* of the GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the:
19
* Free Software Foundation, Inc.
20
* 59 Temple Place - Suite 330
21
* Boston, MA 02111-1307, USA
26
#include <gdk/gdkkeysyms.h>
38
GType ctk_scale_get_type(
42
static GType ctk_scale_type = 0;
44
if (!ctk_scale_type) {
45
static const GTypeInfo ctk_scale_info = {
46
sizeof (CtkScaleClass),
48
NULL, /* base_finalize */
49
NULL, /* class_init, */
50
NULL, /* class_finalize */
51
NULL, /* class_data */
54
NULL, /* instance_init */
57
ctk_scale_type = g_type_register_static (GTK_TYPE_VBOX,
58
"CtkScale", &ctk_scale_info, 0);
61
return ctk_scale_type;
67
* ctk_scale_key_event() - GTK's default handling of the up/down keys
68
* for hscale widgets is odd, so override it: the up key and the page
69
* up key increase the adjustment value; similarly, the down key and
70
* the page down key decrease the adjustment value.
73
static gboolean ctk_scale_key_event(GtkWidget *widget, GdkEvent *event,
76
CtkScale *ctk_scale = CTK_SCALE(user_data);
77
GdkEventKey *key_event = (GdkEventKey *) event;
78
GtkAdjustment *adjustment = GTK_ADJUSTMENT(ctk_scale->gtk_adjustment);
81
switch (key_event->keyval) {
87
newval = adjustment->value - adjustment->step_increment;
94
newval = adjustment->value + adjustment->step_increment;
98
case GDK_KP_Page_Down:
99
newval = adjustment->value - adjustment->page_increment;
104
newval = adjustment->value + adjustment->page_increment;
111
gtk_adjustment_set_value(adjustment, newval);
117
static void adjustment_value_changed(
118
GtkAdjustment *adjustment,
122
CtkScale *ctk_scale = CTK_SCALE(user_data);
125
switch (ctk_scale->value_type) {
127
g_snprintf(text, 6, "%d", (gint) adjustment->value);
131
g_snprintf(text, 6, "%2.3f", adjustment->value);
135
gtk_entry_set_text(GTK_ENTRY(ctk_scale->text_entry), text);
138
static void text_entry_activate(
143
CtkScale *ctk_scale = CTK_SCALE(user_data);
144
gdouble newval = g_strtod(gtk_entry_get_text(entry), NULL);
145
gtk_adjustment_set_value(ctk_scale->gtk_adjustment, newval);
151
* text_entry_toggled() -
154
static void text_entry_toggled(CtkConfig *ctk_config, gpointer user_data)
156
CtkScale *ctk_scale = CTK_SCALE(user_data);
158
if (ctk_config_slider_text_entry_shown(ctk_config)) {
159
if (!ctk_scale->text_entry_packed) {
160
gtk_container_add(GTK_CONTAINER(ctk_scale->text_entry_container),
161
ctk_scale->text_entry);
163
gtk_widget_show(ctk_scale->text_entry);
164
ctk_scale->text_entry_packed = TRUE;
166
if (ctk_scale->text_entry_packed) {
168
(GTK_CONTAINER(ctk_scale->text_entry_container),
169
ctk_scale->text_entry);
171
gtk_widget_hide(ctk_scale->text_entry);
172
ctk_scale->text_entry_packed = FALSE;
174
} /* text_entry_toggled() */
179
* ctk_scale_new() - constructor for the Scale widget
182
GtkWidget* ctk_scale_new(GtkAdjustment *gtk_adjustment,
183
const gchar *label_text,
184
CtkConfig *ctk_config,
193
g_return_val_if_fail(GTK_IS_ADJUSTMENT(gtk_adjustment), NULL);
194
g_return_val_if_fail(label_text != NULL, NULL);
196
/* create and initialize the object */
198
object = g_object_new(CTK_TYPE_SCALE, NULL);
200
ctk_scale = CTK_SCALE(object);
202
ctk_scale->gtk_adjustment = gtk_adjustment;
203
ctk_scale->label = label_text;
204
ctk_scale->ctk_config = ctk_config;
205
ctk_scale->value_type = value_type;
207
gtk_box_set_spacing (GTK_BOX (object), 2);
211
label = gtk_label_new(ctk_scale->label);
212
gtk_box_pack_start(GTK_BOX (object), label, FALSE, FALSE, 0);
214
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
216
/* frame around slider and text box */
218
frame = gtk_frame_new(NULL);
219
gtk_box_pack_start(GTK_BOX(object), frame, TRUE, TRUE, 0);
221
/* event box (for tooltips) */
223
ctk_scale->tooltip_widget = gtk_event_box_new();
224
gtk_container_add(GTK_CONTAINER(frame), ctk_scale->tooltip_widget);
226
/* hbox to contain slider and text box */
228
hbox = gtk_hbox_new(FALSE, 0);
229
gtk_container_add(GTK_CONTAINER(ctk_scale->tooltip_widget), hbox);
233
ctk_scale->text_entry = gtk_entry_new_with_max_length(6);
234
gtk_entry_set_width_chars(GTK_ENTRY(ctk_scale->text_entry), 6);
236
/* text entry container */
238
ctk_scale->text_entry_container = gtk_frame_new(NULL);
239
gtk_frame_set_shadow_type(GTK_FRAME(ctk_scale->text_entry_container),
241
gtk_container_set_border_width
242
(GTK_CONTAINER(ctk_scale->text_entry_container), 0);
245
gtk_container_add(GTK_CONTAINER(ctk_scale->text_entry_container),
246
ctk_scale->text_entry);
247
ctk_scale->text_entry_packed = TRUE;
248
g_object_ref(G_OBJECT(ctk_scale->text_entry));
250
gtk_box_pack_start(GTK_BOX(hbox),
251
ctk_scale->text_entry_container, FALSE, FALSE, 0);
253
text_entry_toggled(ctk_scale->ctk_config, (gpointer) ctk_scale);
255
/* wire up the adjustment events */
257
adjustment_value_changed(ctk_scale->gtk_adjustment, G_OBJECT(ctk_scale));
259
g_signal_connect(G_OBJECT(ctk_scale->gtk_adjustment), "value_changed",
260
G_CALLBACK(adjustment_value_changed),
261
(gpointer) ctk_scale);
263
g_signal_connect(G_OBJECT(ctk_scale->text_entry), "activate",
264
G_CALLBACK(text_entry_activate),
265
(gpointer) ctk_scale);
267
g_signal_connect(G_OBJECT(ctk_config), "slider_text_entry_toggled",
268
G_CALLBACK(text_entry_toggled),
269
(gpointer) ctk_scale);
273
ctk_scale->gtk_scale =
274
gtk_hscale_new(GTK_ADJUSTMENT(ctk_scale->gtk_adjustment));
276
gtk_scale_set_draw_value(GTK_SCALE(ctk_scale->gtk_scale), FALSE);
278
gtk_scale_set_digits(GTK_SCALE(ctk_scale->gtk_scale), 0);
281
gtk_box_pack_start(GTK_BOX(hbox), ctk_scale->gtk_scale, TRUE, TRUE, 3);
283
g_signal_connect(ctk_scale->gtk_scale, "key_press_event",
284
G_CALLBACK(ctk_scale_key_event), G_OBJECT(ctk_scale));
286
return GTK_WIDGET (object);
288
} /* ctk_scale_new() */