~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to libgimpwidgets/gimpcellrenderercolor.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LIBGIMP - The GIMP Library
 
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 
3
 *
 
4
 * gimpcellrenderercolor.c
 
5
 * Copyright (C) 2004  Sven Neuman <sven1@gimp.org>
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library 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 GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <gtk/gtk.h>
 
26
 
 
27
#include "libgimpcolor/gimpcolor.h"
 
28
 
 
29
#include "gimpwidgetstypes.h"
 
30
 
 
31
#include "gimpcolorarea.h"
 
32
#include "gimpcellrenderercolor.h"
 
33
 
 
34
 
 
35
#define DEFAULT_ICON_SIZE  GTK_ICON_SIZE_MENU
 
36
 
 
37
 
 
38
enum
 
39
{
 
40
  PROP_0,
 
41
  PROP_COLOR,
 
42
  PROP_OPAQUE,
 
43
  PROP_SIZE
 
44
};
 
45
 
 
46
 
 
47
static void gimp_cell_renderer_color_class_init (GimpCellRendererColorClass *klass);
 
48
static void gimp_cell_renderer_color_init       (GimpCellRendererColor      *cell);
 
49
 
 
50
static void gimp_cell_renderer_color_get_property (GObject         *object,
 
51
                                                   guint            param_id,
 
52
                                                   GValue          *value,
 
53
                                                   GParamSpec      *pspec);
 
54
static void gimp_cell_renderer_color_set_property (GObject         *object,
 
55
                                                   guint            param_id,
 
56
                                                   const GValue    *value,
 
57
                                                   GParamSpec      *pspec);
 
58
static void gimp_cell_renderer_color_get_size     (GtkCellRenderer *cell,
 
59
                                                   GtkWidget       *widget,
 
60
                                                   GdkRectangle    *rectangle,
 
61
                                                   gint            *x_offset,
 
62
                                                   gint            *y_offset,
 
63
                                                   gint            *width,
 
64
                                                   gint            *height);
 
65
static void gimp_cell_renderer_color_render       (GtkCellRenderer *cell,
 
66
                                                   GdkWindow       *window,
 
67
                                                   GtkWidget       *widget,
 
68
                                                   GdkRectangle    *background_area,
 
69
                                                   GdkRectangle    *cell_area,
 
70
                                                   GdkRectangle    *expose_area,
 
71
                                                   GtkCellRendererState flags);
 
72
 
 
73
 
 
74
static GtkCellRendererClass *parent_class = NULL;
 
75
 
 
76
 
 
77
GType
 
78
gimp_cell_renderer_color_get_type (void)
 
79
{
 
80
  static GType cell_type = 0;
 
81
 
 
82
  if (! cell_type)
 
83
    {
 
84
      static const GTypeInfo cell_info =
 
85
      {
 
86
        sizeof (GimpCellRendererColorClass),
 
87
        NULL,           /* base_init */
 
88
        NULL,           /* base_finalize */
 
89
        (GClassInitFunc) gimp_cell_renderer_color_class_init,
 
90
        NULL,           /* class_finalize */
 
91
        NULL,           /* class_data */
 
92
        sizeof (GimpCellRendererColor),
 
93
        0,              /* n_preallocs */
 
94
        (GInstanceInitFunc) gimp_cell_renderer_color_init,
 
95
      };
 
96
 
 
97
      cell_type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
 
98
                                          "GimpCellRendererColor",
 
99
                                          &cell_info, 0);
 
100
    }
 
101
 
 
102
  return cell_type;
 
103
}
 
104
 
 
105
static void
 
106
gimp_cell_renderer_color_class_init (GimpCellRendererColorClass *klass)
 
107
{
 
108
  GObjectClass         *object_class = G_OBJECT_CLASS (klass);
 
109
  GtkCellRendererClass *cell_class   = GTK_CELL_RENDERER_CLASS (klass);
 
110
 
 
111
  parent_class = g_type_class_peek_parent (klass);
 
112
 
 
113
  object_class->get_property = gimp_cell_renderer_color_get_property;
 
114
  object_class->set_property = gimp_cell_renderer_color_set_property;
 
115
 
 
116
  cell_class->get_size       = gimp_cell_renderer_color_get_size;
 
117
  cell_class->render         = gimp_cell_renderer_color_render;
 
118
 
 
119
  g_object_class_install_property (object_class,
 
120
                                   PROP_COLOR,
 
121
                                   g_param_spec_boxed ("color", NULL, NULL,
 
122
                                                       GIMP_TYPE_RGB,
 
123
                                                       G_PARAM_READWRITE));
 
124
  g_object_class_install_property (object_class,
 
125
                                   PROP_OPAQUE,
 
126
                                   g_param_spec_boolean ("opaque", NULL, NULL,
 
127
                                                         TRUE,
 
128
                                                         G_PARAM_READWRITE |
 
129
                                                         G_PARAM_CONSTRUCT));
 
130
  g_object_class_install_property (object_class,
 
131
                                   PROP_SIZE,
 
132
                                   g_param_spec_int ("icon_size", NULL, NULL,
 
133
                                                     0, G_MAXINT,
 
134
                                                     DEFAULT_ICON_SIZE,
 
135
                                                     G_PARAM_READWRITE |
 
136
                                                     G_PARAM_CONSTRUCT));
 
137
}
 
138
 
 
139
static void
 
140
gimp_cell_renderer_color_init (GimpCellRendererColor *cell)
 
141
{
 
142
  gimp_rgba_set (&cell->color, 0.0, 0.0, 0.0, 1.0);
 
143
}
 
144
 
 
145
static void
 
146
gimp_cell_renderer_color_get_property (GObject    *object,
 
147
                                       guint       param_id,
 
148
                                       GValue     *value,
 
149
                                       GParamSpec *pspec)
 
150
{
 
151
  GimpCellRendererColor *cell = GIMP_CELL_RENDERER_COLOR (object);
 
152
 
 
153
  switch (param_id)
 
154
    {
 
155
    case PROP_COLOR:
 
156
      g_value_set_boxed (value, &cell->color);
 
157
      break;
 
158
    case PROP_OPAQUE:
 
159
      g_value_set_boolean (value, cell->opaque);
 
160
      break;
 
161
    case PROP_SIZE:
 
162
      g_value_set_int (value, cell->size);
 
163
      break;
 
164
    default:
 
165
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
166
      break;
 
167
    }
 
168
}
 
169
 
 
170
static void
 
171
gimp_cell_renderer_color_set_property (GObject      *object,
 
172
                                       guint         param_id,
 
173
                                       const GValue *value,
 
174
                                       GParamSpec   *pspec)
 
175
{
 
176
  GimpCellRendererColor *cell = GIMP_CELL_RENDERER_COLOR (object);
 
177
  GimpRGB               *color;
 
178
 
 
179
  switch (param_id)
 
180
    {
 
181
    case PROP_COLOR:
 
182
      color = g_value_get_boxed (value);
 
183
      cell->color = *color;
 
184
      break;
 
185
    case PROP_OPAQUE:
 
186
      cell->opaque = g_value_get_boolean (value);
 
187
      break;
 
188
    case PROP_SIZE:
 
189
      cell->size = g_value_get_int (value);
 
190
      break;
 
191
    default:
 
192
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
193
      break;
 
194
    }
 
195
}
 
196
 
 
197
static void
 
198
gimp_cell_renderer_color_get_size (GtkCellRenderer *cell,
 
199
                                   GtkWidget       *widget,
 
200
                                   GdkRectangle    *cell_area,
 
201
                                   gint            *x_offset,
 
202
                                   gint            *y_offset,
 
203
                                   gint            *width,
 
204
                                   gint            *height)
 
205
{
 
206
  GimpCellRendererColor *color = GIMP_CELL_RENDERER_COLOR (cell);
 
207
  GtkSettings           *settings;
 
208
  gint                   calc_width;
 
209
  gint                   calc_height;
 
210
 
 
211
  settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
 
212
 
 
213
  gtk_icon_size_lookup_for_settings (settings,
 
214
                                     color->size, &calc_width, &calc_height);
 
215
 
 
216
  if (cell_area && calc_width > 0 && calc_height > 0)
 
217
    {
 
218
      if (x_offset)
 
219
        {
 
220
          *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
 
221
                        1.0 - cell->xalign : cell->xalign) *
 
222
                       (cell_area->width - calc_width));
 
223
          *x_offset = MAX (*x_offset, 0) + cell->xpad;
 
224
        }
 
225
      if (y_offset)
 
226
        {
 
227
          *y_offset = (cell->yalign *
 
228
                       (cell_area->height - calc_height));
 
229
          *y_offset = MAX (*y_offset, 0) + cell->ypad;
 
230
        }
 
231
    }
 
232
  else
 
233
    {
 
234
      if (x_offset)
 
235
        *x_offset = 0;
 
236
      if (y_offset)
 
237
        *y_offset = 0;
 
238
    }
 
239
 
 
240
  if (width)
 
241
    *width  = calc_width  + 2 * cell->xpad;
 
242
  if (height)
 
243
    *height = calc_height + 2 * cell->ypad;
 
244
}
 
245
 
 
246
static void
 
247
gimp_cell_renderer_color_render (GtkCellRenderer      *cell,
 
248
                                 GdkWindow            *window,
 
249
                                 GtkWidget            *widget,
 
250
                                 GdkRectangle         *background_area,
 
251
                                 GdkRectangle         *cell_area,
 
252
                                 GdkRectangle         *expose_area,
 
253
                                 GtkCellRendererState  flags)
 
254
{
 
255
  GimpCellRendererColor *color = GIMP_CELL_RENDERER_COLOR (cell);
 
256
  GdkRectangle           rect;
 
257
 
 
258
  gimp_cell_renderer_color_get_size (cell, widget, cell_area,
 
259
                                     &rect.x,
 
260
                                     &rect.y,
 
261
                                     &rect.width,
 
262
                                     &rect.height);
 
263
 
 
264
  rect.x      += cell_area->x + cell->xpad;
 
265
  rect.y      += cell_area->y + cell->ypad;
 
266
  rect.width  -= 2 * cell->xpad;
 
267
  rect.height -= 2 * cell->ypad;
 
268
 
 
269
  if (rect.width > 2 && rect.height > 2)
 
270
    {
 
271
      GtkStateType  state;
 
272
      guchar       *buf;
 
273
      guint         rowstride = 3 * (rect.width - 2);
 
274
 
 
275
      if (rowstride & 3)
 
276
        rowstride += 4 - (rowstride & 3);
 
277
 
 
278
      buf = g_alloca (rowstride * (rect.height - 2));
 
279
 
 
280
      _gimp_color_area_render_buf ((color->opaque ?
 
281
                                    GIMP_COLOR_AREA_FLAT :
 
282
                                    GIMP_COLOR_AREA_SMALL_CHECKS),
 
283
                                   buf,
 
284
                                   rect.width - 2, rect.height - 2, rowstride,
 
285
                                   &color->color);
 
286
 
 
287
      gdk_draw_rgb_image_dithalign (window,
 
288
                                    widget->style->black_gc,
 
289
                                    rect.x + 1, rect.y + 1,
 
290
                                    rect.width - 2, rect.height - 2,
 
291
                                    GDK_RGB_DITHER_MAX,
 
292
                                    buf, rowstride, rect.x, rect.y);
 
293
 
 
294
      state = (flags & GTK_CELL_RENDERER_SELECTED ?
 
295
               GTK_STATE_SELECTED : GTK_STATE_NORMAL);
 
296
 
 
297
      gdk_draw_rectangle (window,
 
298
                          widget->style->fg_gc[state],
 
299
                          FALSE,
 
300
                          rect.x, rect.y, rect.width - 1, rect.height - 1);
 
301
    }
 
302
}
 
303
 
 
304
/**
 
305
 * gimp_cell_renderer_color_new:
 
306
 *
 
307
 * Creates a #GtkCellRenderer that displays a color.
 
308
 *
 
309
 * Return value: a new #GimpCellRendererColor
 
310
 *
 
311
 * Since: GIMP 2.2
 
312
 **/
 
313
GtkCellRenderer *
 
314
gimp_cell_renderer_color_new (void)
 
315
{
 
316
  return g_object_new (GIMP_TYPE_CELL_RENDERER_COLOR, NULL);
 
317
}