~ubuntu-branches/ubuntu/precise/nvidia-settings/precise-proposed

« back to all changes in this revision

Viewing changes to src/gtk+-2.x/ctkscale.c

  • Committer: Bazaar Package Importer
  • Author(s): Randall Donald
  • Date: 2004-07-03 19:09:17 UTC
  • Revision ID: james.westby@ubuntu.com-20040703190917-rqkze2s58ux5pamy
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
 
3
 * and Linux systems.
 
4
 *
 
5
 * Copyright (C) 2004 NVIDIA Corporation.
 
6
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the:
 
18
 *
 
19
 *           Free Software Foundation, Inc.
 
20
 *           59 Temple Place - Suite 330
 
21
 *           Boston, MA 02111-1307, USA
 
22
 *
 
23
 */
 
24
 
 
25
#include <gtk/gtk.h>
 
26
#include <gdk/gdkkeysyms.h>
 
27
 
 
28
#include "ctkscale.h"
 
29
#include <stdio.h>
 
30
 
 
31
 
 
32
enum {
 
33
    PROP_0,
 
34
    PROP_GTK_ADJUSTMENT,
 
35
    PROP_LABEL
 
36
};
 
37
 
 
38
GType ctk_scale_get_type(
 
39
    void
 
40
)
 
41
{
 
42
    static GType ctk_scale_type = 0;
 
43
 
 
44
    if (!ctk_scale_type) {
 
45
        static const GTypeInfo ctk_scale_info = {
 
46
            sizeof (CtkScaleClass),
 
47
            NULL, /* base_init */
 
48
            NULL, /* base_finalize */
 
49
            NULL, /* class_init, */
 
50
            NULL, /* class_finalize */
 
51
            NULL, /* class_data */
 
52
            sizeof (CtkScale),
 
53
            0, /* n_preallocs */
 
54
            NULL, /* instance_init */
 
55
        };
 
56
 
 
57
        ctk_scale_type = g_type_register_static (GTK_TYPE_VBOX,
 
58
                "CtkScale", &ctk_scale_info, 0);
 
59
    }
 
60
 
 
61
    return ctk_scale_type;
 
62
}
 
63
 
 
64
 
 
65
 
 
66
/*
 
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.
 
71
 */
 
72
 
 
73
static gboolean ctk_scale_key_event(GtkWidget *widget, GdkEvent *event, 
 
74
                                    gpointer user_data)
 
75
{
 
76
    CtkScale *ctk_scale = CTK_SCALE(user_data);
 
77
    GdkEventKey *key_event = (GdkEventKey *) event;
 
78
    GtkAdjustment *adjustment = GTK_ADJUSTMENT(ctk_scale->gtk_adjustment);
 
79
    gdouble newval;
 
80
    
 
81
    switch (key_event->keyval) {
 
82
 
 
83
    case GDK_Left:
 
84
    case GDK_KP_Left:
 
85
    case GDK_Down:
 
86
    case GDK_KP_Down:
 
87
        newval = adjustment->value - adjustment->step_increment;
 
88
        break;
 
89
        
 
90
    case GDK_Right:
 
91
    case GDK_KP_Right:
 
92
    case GDK_Up:
 
93
    case GDK_KP_Up:
 
94
        newval = adjustment->value + adjustment->step_increment;
 
95
        break;
 
96
 
 
97
    case GDK_Page_Down:
 
98
    case GDK_KP_Page_Down:
 
99
        newval = adjustment->value - adjustment->page_increment;
 
100
        break;
 
101
 
 
102
    case GDK_Page_Up:
 
103
    case GDK_KP_Page_Up:
 
104
        newval = adjustment->value + adjustment->page_increment;
 
105
        break;
 
106
 
 
107
    default:
 
108
        return FALSE;
 
109
    }
 
110
    
 
111
    gtk_adjustment_set_value(adjustment, newval);
 
112
    
 
113
    return TRUE;
 
114
}
 
115
 
 
116
 
 
117
static void adjustment_value_changed(
 
118
    GtkAdjustment *adjustment,
 
119
    gpointer user_data
 
120
)
 
121
{
 
122
    CtkScale *ctk_scale = CTK_SCALE(user_data);
 
123
    gchar text[7];
 
124
    
 
125
    switch (ctk_scale->value_type) {
 
126
    case G_TYPE_INT:
 
127
        g_snprintf(text, 6, "%d", (gint) adjustment->value);
 
128
        break;
 
129
    case G_TYPE_DOUBLE:
 
130
    default:
 
131
        g_snprintf(text, 6, "%2.3f", adjustment->value);
 
132
        break;
 
133
    }
 
134
    
 
135
    gtk_entry_set_text(GTK_ENTRY(ctk_scale->text_entry), text);
 
136
}
 
137
 
 
138
static void text_entry_activate(
 
139
    GtkEntry *entry,
 
140
    gpointer user_data
 
141
)
 
142
{
 
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);
 
146
}
 
147
 
 
148
 
 
149
 
 
150
/*
 
151
 * text_entry_toggled() - 
 
152
 */
 
153
 
 
154
static void text_entry_toggled(CtkConfig *ctk_config, gpointer user_data)
 
155
{
 
156
    CtkScale *ctk_scale = CTK_SCALE(user_data);
 
157
 
 
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);
 
162
        }
 
163
        gtk_widget_show(ctk_scale->text_entry);
 
164
        ctk_scale->text_entry_packed = TRUE;
 
165
    } else {
 
166
        if (ctk_scale->text_entry_packed) {
 
167
            gtk_container_remove
 
168
                (GTK_CONTAINER(ctk_scale->text_entry_container),
 
169
                 ctk_scale->text_entry);
 
170
        }
 
171
        gtk_widget_hide(ctk_scale->text_entry);
 
172
        ctk_scale->text_entry_packed = FALSE;
 
173
    }
 
174
} /* text_entry_toggled() */
 
175
 
 
176
 
 
177
 
 
178
/*
 
179
 * ctk_scale_new() - constructor for the Scale widget
 
180
 */
 
181
 
 
182
GtkWidget* ctk_scale_new(GtkAdjustment *gtk_adjustment,
 
183
                         const gchar *label_text,
 
184
                         CtkConfig *ctk_config,
 
185
                         gint value_type)
 
186
{
 
187
    GObject *object;
 
188
    CtkScale *ctk_scale;
 
189
    GtkWidget *label;
 
190
    GtkWidget *frame;
 
191
    GtkWidget *hbox;
 
192
    
 
193
    g_return_val_if_fail(GTK_IS_ADJUSTMENT(gtk_adjustment), NULL);
 
194
    g_return_val_if_fail(label_text != NULL, NULL);
 
195
    
 
196
    /* create and initialize the object */
 
197
 
 
198
    object = g_object_new(CTK_TYPE_SCALE, NULL);
 
199
    
 
200
    ctk_scale = CTK_SCALE(object);
 
201
 
 
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;
 
206
    
 
207
    gtk_box_set_spacing (GTK_BOX (object), 2);
 
208
    
 
209
    /* scale label */
 
210
    
 
211
    label = gtk_label_new(ctk_scale->label);
 
212
    gtk_box_pack_start(GTK_BOX (object), label, FALSE, FALSE, 0);
 
213
    
 
214
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
 
215
    
 
216
    /* frame around slider and text box */
 
217
    
 
218
    frame = gtk_frame_new(NULL);
 
219
    gtk_box_pack_start(GTK_BOX(object), frame, TRUE, TRUE, 0);
 
220
    
 
221
    /* event box (for tooltips) */
 
222
 
 
223
    ctk_scale->tooltip_widget = gtk_event_box_new();
 
224
    gtk_container_add(GTK_CONTAINER(frame), ctk_scale->tooltip_widget);
 
225
    
 
226
    /* hbox to contain slider and text box */
 
227
 
 
228
    hbox = gtk_hbox_new(FALSE, 0);
 
229
    gtk_container_add(GTK_CONTAINER(ctk_scale->tooltip_widget), hbox);
 
230
 
 
231
    /* text entry */
 
232
    
 
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);
 
235
    
 
236
    /* text entry container */
 
237
 
 
238
    ctk_scale->text_entry_container = gtk_frame_new(NULL);
 
239
    gtk_frame_set_shadow_type(GTK_FRAME(ctk_scale->text_entry_container),
 
240
                              GTK_SHADOW_NONE);
 
241
    gtk_container_set_border_width
 
242
        (GTK_CONTAINER(ctk_scale->text_entry_container), 0);
 
243
                                    
 
244
 
 
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));
 
249
    
 
250
    gtk_box_pack_start(GTK_BOX(hbox),
 
251
                       ctk_scale->text_entry_container, FALSE, FALSE, 0);
 
252
    
 
253
    text_entry_toggled(ctk_scale->ctk_config, (gpointer) ctk_scale);
 
254
 
 
255
    /* wire up the adjustment events */
 
256
 
 
257
    adjustment_value_changed(ctk_scale->gtk_adjustment, G_OBJECT(ctk_scale));
 
258
 
 
259
    g_signal_connect(G_OBJECT(ctk_scale->gtk_adjustment), "value_changed",
 
260
                     G_CALLBACK(adjustment_value_changed),
 
261
                     (gpointer) ctk_scale);
 
262
 
 
263
    g_signal_connect(G_OBJECT(ctk_scale->text_entry), "activate",
 
264
                     G_CALLBACK(text_entry_activate),
 
265
                     (gpointer) ctk_scale);
 
266
                     
 
267
    g_signal_connect(G_OBJECT(ctk_config), "slider_text_entry_toggled",
 
268
                     G_CALLBACK(text_entry_toggled),
 
269
                     (gpointer) ctk_scale);
 
270
 
 
271
    /* the slider */
 
272
 
 
273
    ctk_scale->gtk_scale =
 
274
        gtk_hscale_new(GTK_ADJUSTMENT(ctk_scale->gtk_adjustment));
 
275
    
 
276
    gtk_scale_set_draw_value(GTK_SCALE(ctk_scale->gtk_scale), FALSE);
 
277
    
 
278
    gtk_scale_set_digits(GTK_SCALE(ctk_scale->gtk_scale), 0);
 
279
 
 
280
 
 
281
    gtk_box_pack_start(GTK_BOX(hbox), ctk_scale->gtk_scale, TRUE, TRUE, 3);
 
282
    
 
283
    g_signal_connect(ctk_scale->gtk_scale, "key_press_event",
 
284
                     G_CALLBACK(ctk_scale_key_event), G_OBJECT(ctk_scale));
 
285
    
 
286
    return GTK_WIDGET (object);
 
287
    
 
288
} /* ctk_scale_new() */