~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/widgets/gimpfgbgview.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpfgbgview.c
 
5
 * Copyright (C) 2005  Sven Neumann <sven@gimp.org>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program 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
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <string.h>
 
25
 
 
26
#include <gtk/gtk.h>
 
27
 
 
28
#include "libgimpcolor/gimpcolor.h"
 
29
#include "libgimpwidgets/gimpwidgets.h"
 
30
 
 
31
#include "widgets-types.h"
 
32
 
 
33
#include "core/gimpcontext.h"
 
34
#include "core/gimpmarshal.h"
 
35
 
 
36
#include "gimpdnd.h"
 
37
#include "gimpfgbgview.h"
 
38
 
 
39
 
 
40
enum
 
41
{
 
42
  PROP_0,
 
43
  PROP_CONTEXT
 
44
};
 
45
 
 
46
 
 
47
static void     gimp_fg_bg_view_set_property (GObject        *object,
 
48
                                              guint           property_id,
 
49
                                              const GValue   *value,
 
50
                                              GParamSpec     *pspec);
 
51
static void     gimp_fg_bg_view_get_property (GObject        *object,
 
52
                                              guint           property_id,
 
53
                                              GValue         *value,
 
54
                                              GParamSpec     *pspec);
 
55
 
 
56
static void     gimp_fg_bg_view_destroy      (GtkObject      *object);
 
57
static gboolean gimp_fg_bg_view_expose       (GtkWidget      *widget,
 
58
                                              GdkEventExpose *eevent);
 
59
 
 
60
 
 
61
G_DEFINE_TYPE (GimpFgBgView, gimp_fg_bg_view, GTK_TYPE_DRAWING_AREA)
 
62
 
 
63
#define parent_class gimp_fg_bg_view_parent_class
 
64
 
 
65
 
 
66
static void
 
67
gimp_fg_bg_view_class_init (GimpFgBgViewClass *klass)
 
68
{
 
69
  GObjectClass   *object_class     = G_OBJECT_CLASS (klass);
 
70
  GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (klass);
 
71
  GtkWidgetClass *widget_class     = GTK_WIDGET_CLASS (klass);
 
72
 
 
73
  object_class->set_property = gimp_fg_bg_view_set_property;
 
74
  object_class->get_property = gimp_fg_bg_view_get_property;
 
75
 
 
76
  gtk_object_class->destroy  = gimp_fg_bg_view_destroy;
 
77
 
 
78
  widget_class->expose_event = gimp_fg_bg_view_expose;
 
79
 
 
80
  g_object_class_install_property (object_class, PROP_CONTEXT,
 
81
                                   g_param_spec_object ("context",
 
82
                                                        NULL, NULL,
 
83
                                                        GIMP_TYPE_CONTEXT,
 
84
                                                        GIMP_PARAM_READWRITE));
 
85
}
 
86
 
 
87
static void
 
88
gimp_fg_bg_view_init (GimpFgBgView *view)
 
89
{
 
90
  view->context = NULL;
 
91
}
 
92
 
 
93
static void
 
94
gimp_fg_bg_view_set_property (GObject      *object,
 
95
                              guint         property_id,
 
96
                              const GValue *value,
 
97
                              GParamSpec   *pspec)
 
98
{
 
99
  GimpFgBgView *view = GIMP_FG_BG_VIEW (object);
 
100
 
 
101
  switch (property_id)
 
102
    {
 
103
    case PROP_CONTEXT:
 
104
      gimp_fg_bg_view_set_context (view, g_value_get_object (value));
 
105
      break;
 
106
    default:
 
107
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
108
      break;
 
109
    }
 
110
}
 
111
 
 
112
static void
 
113
gimp_fg_bg_view_get_property (GObject    *object,
 
114
                              guint       property_id,
 
115
                              GValue     *value,
 
116
                              GParamSpec *pspec)
 
117
{
 
118
  GimpFgBgView *view = GIMP_FG_BG_VIEW (object);
 
119
 
 
120
  switch (property_id)
 
121
    {
 
122
    case PROP_CONTEXT:
 
123
      g_value_set_object (value, view->context);
 
124
      break;
 
125
    default:
 
126
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
127
      break;
 
128
    }
 
129
}
 
130
 
 
131
static void
 
132
gimp_fg_bg_view_destroy (GtkObject *object)
 
133
{
 
134
  GimpFgBgView *view = GIMP_FG_BG_VIEW (object);
 
135
 
 
136
  if (view->context)
 
137
    gimp_fg_bg_view_set_context (view, NULL);
 
138
 
 
139
  if (view->render_buf)
 
140
    {
 
141
      g_free (view->render_buf);
 
142
      view->render_buf = NULL;
 
143
      view->render_buf_size = 0;
 
144
    }
 
145
 
 
146
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
 
147
}
 
148
 
 
149
static void
 
150
gimp_fg_bg_view_draw_rect (GimpFgBgView  *view,
 
151
                           GdkDrawable   *drawable,
 
152
                           GdkGC         *gc,
 
153
                           gint           x,
 
154
                           gint           y,
 
155
                           gint           width,
 
156
                           gint           height,
 
157
                           const GimpRGB *color)
 
158
{
 
159
  gint    rowstride;
 
160
  guchar  r, g, b;
 
161
  gint    xx, yy;
 
162
  guchar *bp;
 
163
 
 
164
  if (! (width > 0 && height > 0))
 
165
    return;
 
166
 
 
167
  gimp_rgb_get_uchar (color, &r, &g, &b);
 
168
 
 
169
  rowstride = 3 * ((width + 3) & -4);
 
170
 
 
171
  if (! view->render_buf || view->render_buf_size < height * rowstride)
 
172
    {
 
173
      view->render_buf_size = rowstride * height;
 
174
 
 
175
      g_free (view->render_buf);
 
176
      view->render_buf = g_malloc (view->render_buf_size);
 
177
    }
 
178
 
 
179
  bp = view->render_buf;
 
180
  for (xx = 0; xx < width; xx++)
 
181
    {
 
182
      *bp++ = r;
 
183
      *bp++ = g;
 
184
      *bp++ = b;
 
185
    }
 
186
 
 
187
  bp = view->render_buf;
 
188
 
 
189
  for (yy = 1; yy < height; yy++)
 
190
    {
 
191
      bp += rowstride;
 
192
      memcpy (bp, view->render_buf, rowstride);
 
193
    }
 
194
 
 
195
  gdk_draw_rgb_image (drawable, gc, x, y, width, height,
 
196
                      GDK_RGB_DITHER_MAX,
 
197
                      view->render_buf,
 
198
                      rowstride);
 
199
}
 
200
 
 
201
static gboolean
 
202
gimp_fg_bg_view_expose (GtkWidget      *widget,
 
203
                        GdkEventExpose *eevent)
 
204
{
 
205
  GimpFgBgView *view = GIMP_FG_BG_VIEW (widget);
 
206
  gint          width, height;
 
207
  gint          rect_w, rect_h;
 
208
  GimpRGB       color;
 
209
 
 
210
  if (! GTK_WIDGET_DRAWABLE (widget))
 
211
    return FALSE;
 
212
 
 
213
  width  = widget->allocation.width;
 
214
  height = widget->allocation.height;
 
215
 
 
216
  rect_w = width  * 3 / 4;
 
217
  rect_h = height * 3 / 4;
 
218
 
 
219
  /*  draw the background area  */
 
220
 
 
221
  if (view->context)
 
222
    {
 
223
      gimp_context_get_background (view->context, &color);
 
224
      gimp_fg_bg_view_draw_rect (view,
 
225
                                 widget->window,
 
226
                                 widget->style->fg_gc[0],
 
227
                                 width - rect_w + 1, height - rect_h + 1,
 
228
                                 rect_w - 2, rect_h - 2,
 
229
                                 &color);
 
230
    }
 
231
 
 
232
  gtk_paint_shadow (widget->style, widget->window, GTK_STATE_NORMAL,
 
233
                    GTK_SHADOW_IN,
 
234
                    NULL, widget, NULL,
 
235
                    width - rect_w, height - rect_h, rect_w, rect_h);
 
236
 
 
237
  /*  draw the foreground area  */
 
238
 
 
239
  if (view->context)
 
240
    {
 
241
      gimp_context_get_foreground (view->context, &color);
 
242
      gimp_fg_bg_view_draw_rect (view,
 
243
                                 widget->window,
 
244
                                 widget->style->fg_gc[0],
 
245
                                 1, 1,
 
246
                                 rect_w - 2, rect_h - 2,
 
247
                                 &color);
 
248
    }
 
249
 
 
250
  gtk_paint_shadow (widget->style, widget->window, GTK_STATE_NORMAL,
 
251
                    GTK_SHADOW_OUT,
 
252
                    NULL, widget, NULL,
 
253
                    0, 0, rect_w, rect_h);
 
254
 
 
255
  return TRUE;
 
256
}
 
257
 
 
258
/*  public functions  */
 
259
 
 
260
GtkWidget *
 
261
gimp_fg_bg_view_new (GimpContext *context)
 
262
{
 
263
  g_return_val_if_fail (context == NULL || GIMP_IS_CONTEXT (context), NULL);
 
264
 
 
265
  return g_object_new (GIMP_TYPE_FG_BG_VIEW,
 
266
                       "context", context,
 
267
                       NULL);
 
268
}
 
269
 
 
270
void
 
271
gimp_fg_bg_view_set_context (GimpFgBgView *view,
 
272
                             GimpContext  *context)
 
273
{
 
274
  g_return_if_fail (GIMP_IS_FG_BG_VIEW (view));
 
275
  g_return_if_fail (context == NULL || GIMP_IS_CONTEXT (context));
 
276
 
 
277
  if (context == view->context)
 
278
    return;
 
279
 
 
280
  if (view->context)
 
281
    {
 
282
      g_signal_handlers_disconnect_by_func (view->context,
 
283
                                            gtk_widget_queue_draw,
 
284
                                            view);
 
285
      g_object_unref (view->context);
 
286
      view->context = NULL;
 
287
    }
 
288
 
 
289
  view->context = context;
 
290
 
 
291
  if (context)
 
292
    {
 
293
      g_object_ref (context);
 
294
 
 
295
      g_signal_connect_swapped (context, "foreground-changed",
 
296
                                G_CALLBACK (gtk_widget_queue_draw),
 
297
                                view);
 
298
      g_signal_connect_swapped (context, "background-changed",
 
299
                                G_CALLBACK (gtk_widget_queue_draw),
 
300
                                view);
 
301
    }
 
302
 
 
303
  g_object_notify (G_OBJECT (view), "context");
 
304
}