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

« back to all changes in this revision

Viewing changes to nvidia-settings-1.0/src/gtk+-2.x/ctkcurve.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 <string.h>
 
27
#include "NvCtrlAttributes.h"
 
28
 
 
29
#include "ctkcurve.h"
 
30
 
 
31
 
 
32
#define REQUESTED_WIDTH 94
 
33
#define REQUESTED_HEIGHT 94
 
34
 
 
35
static void
 
36
ctk_curve_class_init    (CtkCurveClass *);
 
37
 
 
38
static void
 
39
ctk_curve_finalize      (GObject *);
 
40
 
 
41
static gboolean
 
42
ctk_curve_expose_event  (GtkWidget *, GdkEventExpose *);
 
43
 
 
44
static void
 
45
ctk_curve_size_request  (GtkWidget *, GtkRequisition *);
 
46
 
 
47
static gboolean
 
48
ctk_curve_configure_event(GtkWidget *, GdkEventConfigure *);
 
49
 
 
50
static void
 
51
plot_color_ramp         (GdkPixmap *, GdkGC *, gushort *, gint, gint, gint);
 
52
 
 
53
 
 
54
static void draw(CtkCurve *ctk_curve);
 
55
 
 
56
static GObjectClass *parent_class;
 
57
 
 
58
 
 
59
GType ctk_curve_get_type(
 
60
    void
 
61
)
 
62
{
 
63
    static GType ctk_curve_type = 0;
 
64
 
 
65
    if (!ctk_curve_type) {
 
66
        static const GTypeInfo ctk_curve_info = {
 
67
            sizeof (CtkCurveClass),
 
68
            NULL, /* base_init */
 
69
            NULL, /* base_finalize */
 
70
            (GClassInitFunc) ctk_curve_class_init,
 
71
            NULL, /* class_finalize */
 
72
            NULL, /* class_data */
 
73
            sizeof (CtkCurve),
 
74
            0, /* n_preallocs */
 
75
            NULL, /* instance_init */
 
76
        };
 
77
 
 
78
        ctk_curve_type = g_type_register_static (GTK_TYPE_DRAWING_AREA,
 
79
                        "CtkCurve", &ctk_curve_info, 0);
 
80
    }
 
81
 
 
82
    return ctk_curve_type;
 
83
}
 
84
 
 
85
static void ctk_curve_class_init(
 
86
    CtkCurveClass *ctk_curve_class
 
87
)
 
88
{
 
89
    GObjectClass *gobject_class;
 
90
    GtkWidgetClass *widget_class;
 
91
 
 
92
    widget_class = (GtkWidgetClass *) ctk_curve_class;
 
93
    gobject_class = (GObjectClass *) ctk_curve_class;
 
94
 
 
95
    parent_class = g_type_class_peek_parent(ctk_curve_class);
 
96
 
 
97
    gobject_class->finalize = ctk_curve_finalize;
 
98
 
 
99
    widget_class->expose_event = ctk_curve_expose_event;
 
100
    widget_class->size_request = ctk_curve_size_request;
 
101
    widget_class->configure_event = ctk_curve_configure_event;
 
102
}
 
103
 
 
104
 
 
105
 
 
106
static void ctk_curve_finalize(
 
107
    GObject *object
 
108
)
 
109
{
 
110
    CtkCurve *ctk_curve;
 
111
    GdkColormap *gdk_colormap;
 
112
    GdkColor *gdk_color;
 
113
 
 
114
    ctk_curve = CTK_CURVE(object);
 
115
 
 
116
    gdk_colormap = ctk_curve->gdk_colormap;
 
117
 
 
118
    gdk_color = &ctk_curve->gdk_color_red;
 
119
    gdk_colormap_free_colors(gdk_colormap, gdk_color, 1);
 
120
 
 
121
    gdk_color = &ctk_curve->gdk_color_green;
 
122
    gdk_colormap_free_colors(gdk_colormap, gdk_color, 1);
 
123
 
 
124
    gdk_color = &ctk_curve->gdk_color_blue;
 
125
    gdk_colormap_free_colors(gdk_colormap, gdk_color, 1);
 
126
 
 
127
    g_object_unref(gdk_colormap);
 
128
}
 
129
 
 
130
static gboolean ctk_curve_expose_event(
 
131
    GtkWidget *widget,
 
132
    GdkEventExpose *event
 
133
)
 
134
{
 
135
    gint width, height;
 
136
    CtkCurve *ctk_curve;
 
137
 
 
138
    ctk_curve = CTK_CURVE(widget);
 
139
 
 
140
    width  = widget->allocation.width  - 2 * widget->style->xthickness;
 
141
    height = widget->allocation.height - 2 * widget->style->ythickness;
 
142
 
 
143
    gtk_paint_shadow(widget->style, widget->window,
 
144
                     GTK_STATE_NORMAL, GTK_SHADOW_IN,
 
145
                     &event->area, widget, "ctk_curve", 0, 0,
 
146
                     widget->allocation.width, widget->allocation.height);
 
147
 
 
148
    gdk_gc_set_function(ctk_curve->gdk_gc, GDK_COPY);
 
149
    
 
150
    gdk_draw_drawable(widget->window, ctk_curve->gdk_gc, ctk_curve->gdk_pixmap,
 
151
                      0, 0, widget->style->xthickness,
 
152
                      widget->style->ythickness,
 
153
                      width, height);
 
154
    return FALSE;
 
155
}
 
156
 
 
157
static gboolean ctk_curve_configure_event
 
158
(
 
159
 GtkWidget *widget,
 
160
 GdkEventConfigure *event
 
161
 )
 
162
{
 
163
    CtkCurve *ctk_curve = CTK_CURVE(widget);
 
164
 
 
165
    ctk_curve->width = event->width;
 
166
    ctk_curve->height = event->height;
 
167
    
 
168
    if (ctk_curve->gdk_pixmap) g_object_unref(ctk_curve->gdk_pixmap);
 
169
    if (ctk_curve->gdk_gc) g_object_unref(ctk_curve->gdk_gc);
 
170
 
 
171
    ctk_curve->gdk_pixmap = gdk_pixmap_new(widget->window, ctk_curve->width,
 
172
                                           ctk_curve->height, -1);
 
173
    ctk_curve->gdk_gc = gdk_gc_new(ctk_curve->gdk_pixmap);
 
174
 
 
175
    draw(ctk_curve);
 
176
 
 
177
    return FALSE;
 
178
}
 
179
 
 
180
 
 
181
static void plot_color_ramp(
 
182
    GdkPixmap *gdk_pixmap,
 
183
    GdkGC *gdk_gc,
 
184
    gushort *color_ramp,
 
185
    gint n_color_ramp_entries,
 
186
    gint width,
 
187
    gint height
 
188
)
 
189
{
 
190
    gfloat x, dx, y;
 
191
    GdkPoint *gdk_points;
 
192
    gint i;
 
193
 
 
194
    gdk_points = g_malloc(width * sizeof(GdkPoint));
 
195
 
 
196
    x = 0;
 
197
    dx = (n_color_ramp_entries - 1.0) / (width - 1.0);
 
198
 
 
199
    for (i = 0; i < width; i++, x += dx) {
 
200
        y = (gfloat) color_ramp[(int) (x + 0.5)];
 
201
        gdk_points[i].x = i;
 
202
        gdk_points[i].y = height - ((height - 1) * (y / 65535) + 0.5);
 
203
    }
 
204
 
 
205
    gdk_draw_lines(gdk_pixmap, gdk_gc, gdk_points, width);
 
206
 
 
207
    g_free(gdk_points);
 
208
}
 
209
 
 
210
static void ctk_curve_size_request(
 
211
    GtkWidget *widget,
 
212
    GtkRequisition *requisition
 
213
)
 
214
{
 
215
    requisition->width  = REQUESTED_WIDTH;
 
216
    requisition->height = REQUESTED_HEIGHT;
 
217
}
 
218
 
 
219
static void color_changed(
 
220
    GtkWidget *widget
 
221
)
 
222
{
 
223
    GdkRectangle rectangle;
 
224
 
 
225
    rectangle.x = widget->style->xthickness;
 
226
    rectangle.y = widget->style->ythickness;
 
227
 
 
228
    rectangle.width  = widget->allocation.width  - 2 * rectangle.x;
 
229
    rectangle.height = widget->allocation.height - 2 * rectangle.y;
 
230
 
 
231
    if (GTK_WIDGET_DRAWABLE(widget)) {
 
232
        draw(CTK_CURVE(widget)); /* only draw when visible */
 
233
        gdk_window_invalidate_rect(widget->window, &rectangle, FALSE);
 
234
    }
 
235
}
 
236
 
 
237
GtkWidget* ctk_curve_new(NvCtrlAttributeHandle *handle, GtkWidget *color)
 
238
{
 
239
    GObject *object;
 
240
    CtkCurve *ctk_curve;
 
241
    GdkColormap *gdk_colormap;
 
242
    GdkColor *gdk_color;
 
243
 
 
244
    object = g_object_new(CTK_TYPE_CURVE, NULL);
 
245
 
 
246
    ctk_curve = CTK_CURVE(object);
 
247
 
 
248
    ctk_curve->handle = handle;
 
249
    ctk_curve->color = color;
 
250
 
 
251
    ctk_curve->gdk_pixmap = NULL;
 
252
    ctk_curve->gdk_gc = NULL;
 
253
    
 
254
    ctk_curve->gdk_colormap = gdk_colormap = gdk_colormap_get_system();
 
255
 
 
256
    gdk_color = &ctk_curve->gdk_color_red;
 
257
    memset(gdk_color, 0, sizeof(GdkColor));
 
258
    gdk_color->red = 65535;
 
259
    gdk_colormap_alloc_color(gdk_colormap, gdk_color, FALSE, TRUE);
 
260
 
 
261
    gdk_color = &ctk_curve->gdk_color_green;
 
262
    memset(gdk_color, 0, sizeof(GdkColor));
 
263
    gdk_color->green = 65535;
 
264
    gdk_colormap_alloc_color(gdk_colormap, gdk_color, FALSE, TRUE);
 
265
 
 
266
    gdk_color = &ctk_curve->gdk_color_blue;
 
267
    memset(gdk_color, 0, sizeof(GdkColor));
 
268
    gdk_color->blue = 65535;
 
269
    gdk_colormap_alloc_color(gdk_colormap, gdk_color, FALSE, TRUE);
 
270
 
 
271
 
 
272
    g_signal_connect_swapped(G_OBJECT(ctk_curve->color), "changed",
 
273
                             G_CALLBACK(color_changed), (gpointer) ctk_curve);
 
274
 
 
275
    return GTK_WIDGET(object);
 
276
}
 
277
 
 
278
 
 
279
 
 
280
static void draw(CtkCurve *ctk_curve)
 
281
{
 
282
    GtkWidget *widget = GTK_WIDGET(ctk_curve);
 
283
 
 
284
    gushort *lut;
 
285
    gint n_lut_entries;
 
286
 
 
287
    gdk_gc_set_function(ctk_curve->gdk_gc, GDK_COPY);
 
288
    
 
289
    gdk_draw_rectangle(ctk_curve->gdk_pixmap, widget->style->black_gc,
 
290
                       TRUE, 0, 0, ctk_curve->width, ctk_curve->height);
 
291
 
 
292
    gdk_gc_set_function(ctk_curve->gdk_gc, GDK_XOR);
 
293
 
 
294
    gdk_gc_set_foreground(ctk_curve->gdk_gc, &ctk_curve->gdk_color_red);
 
295
    NvCtrlGetColorRamp(ctk_curve->handle, RED_CHANNEL, &lut, &n_lut_entries);
 
296
    plot_color_ramp(ctk_curve->gdk_pixmap, ctk_curve->gdk_gc,
 
297
                    lut, n_lut_entries, ctk_curve->width, ctk_curve->height);
 
298
    
 
299
    gdk_gc_set_foreground(ctk_curve->gdk_gc, &ctk_curve->gdk_color_green);
 
300
    NvCtrlGetColorRamp(ctk_curve->handle, GREEN_CHANNEL, &lut, &n_lut_entries);
 
301
    plot_color_ramp(ctk_curve->gdk_pixmap, ctk_curve->gdk_gc,
 
302
                    lut, n_lut_entries, ctk_curve->width, ctk_curve->height);
 
303
 
 
304
    gdk_gc_set_foreground(ctk_curve->gdk_gc, &ctk_curve->gdk_color_blue);
 
305
    NvCtrlGetColorRamp(ctk_curve->handle, BLUE_CHANNEL, &lut, &n_lut_entries);
 
306
    plot_color_ramp(ctk_curve->gdk_pixmap, ctk_curve->gdk_gc,
 
307
                    lut, n_lut_entries, ctk_curve->width, ctk_curve->height);
 
308
    
 
309
}