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

« back to all changes in this revision

Viewing changes to app/tools/gimpbucketfilltool.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
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <gtk/gtk.h>
 
22
#include <gdk/gdkkeysyms.h>
 
23
 
 
24
#include "libgimpwidgets/gimpwidgets.h"
 
25
 
 
26
#include "tools-types.h"
 
27
 
 
28
#include "config/gimpguiconfig.h"
 
29
 
 
30
#include "core/gimp.h"
 
31
#include "core/gimpchannel.h"
 
32
#include "core/gimpdrawable-bucket-fill.h"
 
33
#include "core/gimpimage.h"
 
34
#include "core/gimptoolinfo.h"
 
35
 
 
36
#include "widgets/gimphelp-ids.h"
 
37
 
 
38
#include "display/gimpdisplay.h"
 
39
 
 
40
#include "gimpbucketfilloptions.h"
 
41
#include "gimpbucketfilltool.h"
 
42
#include "gimptoolcontrol.h"
 
43
 
 
44
#include "gimp-intl.h"
 
45
 
 
46
 
 
47
static GimpToolClass *parent_class = NULL;
 
48
 
 
49
 
 
50
/*  local function prototypes  */
 
51
 
 
52
static void   gimp_bucket_fill_tool_class_init (GimpBucketFillToolClass *klass);
 
53
static void   gimp_bucket_fill_tool_init       (GimpBucketFillTool      *bucket_fill_tool);
 
54
 
 
55
static void   gimp_bucket_fill_tool_button_press    (GimpTool        *tool,
 
56
                                                     GimpCoords      *coords,
 
57
                                                     guint32          time,
 
58
                                                     GdkModifierType  state,
 
59
                                                     GimpDisplay     *gdisp);
 
60
static void   gimp_bucket_fill_tool_button_release  (GimpTool        *tool,
 
61
                                                     GimpCoords      *coords,
 
62
                                                     guint32          time,
 
63
                                                     GdkModifierType  state,
 
64
                                                     GimpDisplay     *gdisp);
 
65
static void   gimp_bucket_fill_tool_modifier_key    (GimpTool        *tool,
 
66
                                                     GdkModifierType  key,
 
67
                                                     gboolean         press,
 
68
                                                     GdkModifierType  state,
 
69
                                                     GimpDisplay     *gdisp);
 
70
static void   gimp_bucket_fill_tool_cursor_update   (GimpTool        *tool,
 
71
                                                     GimpCoords      *coords,
 
72
                                                     GdkModifierType  state,
 
73
                                                     GimpDisplay     *gdisp);
 
74
 
 
75
 
 
76
/*  public functions  */
 
77
 
 
78
void
 
79
gimp_bucket_fill_tool_register (GimpToolRegisterCallback  callback,
 
80
                                gpointer                  data)
 
81
{
 
82
  (* callback) (GIMP_TYPE_BUCKET_FILL_TOOL,
 
83
                GIMP_TYPE_BUCKET_FILL_OPTIONS,
 
84
                gimp_bucket_fill_options_gui,
 
85
                GIMP_CONTEXT_FOREGROUND_MASK |
 
86
                GIMP_CONTEXT_BACKGROUND_MASK |
 
87
                GIMP_CONTEXT_OPACITY_MASK    |
 
88
                GIMP_CONTEXT_PAINT_MODE_MASK |
 
89
                GIMP_CONTEXT_PATTERN_MASK,
 
90
                "gimp-bucket-fill-tool",
 
91
                _("Bucket Fill"),
 
92
                _("Fill with a color or pattern"),
 
93
                N_("_Bucket Fill"), "<shift>B",
 
94
                NULL, GIMP_HELP_TOOL_BUCKET_FILL,
 
95
                GIMP_STOCK_TOOL_BUCKET_FILL,
 
96
                data);
 
97
}
 
98
 
 
99
GType
 
100
gimp_bucket_fill_tool_get_type (void)
 
101
{
 
102
  static GType tool_type = 0;
 
103
 
 
104
  if (! tool_type)
 
105
    {
 
106
      static const GTypeInfo tool_info =
 
107
      {
 
108
        sizeof (GimpBucketFillToolClass),
 
109
        (GBaseInitFunc) NULL,
 
110
        (GBaseFinalizeFunc) NULL,
 
111
        (GClassInitFunc) gimp_bucket_fill_tool_class_init,
 
112
        NULL,           /* class_finalize */
 
113
        NULL,           /* class_data     */
 
114
        sizeof (GimpBucketFillTool),
 
115
        0,              /* n_preallocs    */
 
116
        (GInstanceInitFunc) gimp_bucket_fill_tool_init,
 
117
      };
 
118
 
 
119
      tool_type = g_type_register_static (GIMP_TYPE_TOOL,
 
120
                                          "GimpBucketFillTool",
 
121
                                          &tool_info, 0);
 
122
    }
 
123
 
 
124
  return tool_type;
 
125
}
 
126
 
 
127
 
 
128
/*  private functions  */
 
129
 
 
130
static void
 
131
gimp_bucket_fill_tool_class_init (GimpBucketFillToolClass *klass)
 
132
{
 
133
  GimpToolClass *tool_class;
 
134
 
 
135
  tool_class = GIMP_TOOL_CLASS (klass);
 
136
 
 
137
  parent_class = g_type_class_peek_parent (klass);
 
138
 
 
139
  tool_class->button_press   = gimp_bucket_fill_tool_button_press;
 
140
  tool_class->button_release = gimp_bucket_fill_tool_button_release;
 
141
  tool_class->modifier_key   = gimp_bucket_fill_tool_modifier_key;
 
142
  tool_class->cursor_update  = gimp_bucket_fill_tool_cursor_update;
 
143
}
 
144
 
 
145
static void
 
146
gimp_bucket_fill_tool_init (GimpBucketFillTool *bucket_fill_tool)
 
147
{
 
148
  GimpTool *tool;
 
149
 
 
150
  tool = GIMP_TOOL (bucket_fill_tool);
 
151
 
 
152
  gimp_tool_control_set_scroll_lock (tool->control, TRUE);
 
153
  gimp_tool_control_set_tool_cursor (tool->control,
 
154
                                     GIMP_TOOL_CURSOR_BUCKET_FILL);
 
155
}
 
156
 
 
157
static void
 
158
gimp_bucket_fill_tool_button_press (GimpTool        *tool,
 
159
                                    GimpCoords      *coords,
 
160
                                    guint32          time,
 
161
                                    GdkModifierType  state,
 
162
                                    GimpDisplay     *gdisp)
 
163
{
 
164
  GimpBucketFillTool    *bucket_tool;
 
165
  GimpBucketFillOptions *options;
 
166
 
 
167
  bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
 
168
  options     = GIMP_BUCKET_FILL_OPTIONS (tool->tool_info->tool_options);
 
169
 
 
170
  bucket_tool->target_x = coords->x;
 
171
  bucket_tool->target_y = coords->y;
 
172
 
 
173
  if (! options->sample_merged)
 
174
    {
 
175
      gint off_x, off_y;
 
176
 
 
177
      gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (gdisp->gimage)),
 
178
                         &off_x, &off_y);
 
179
 
 
180
      bucket_tool->target_x -= off_x;
 
181
      bucket_tool->target_y -= off_y;
 
182
    }
 
183
 
 
184
  tool->gdisp = gdisp;
 
185
  gimp_tool_control_activate (tool->control);
 
186
}
 
187
 
 
188
static void
 
189
gimp_bucket_fill_tool_button_release (GimpTool        *tool,
 
190
                                      GimpCoords      *coords,
 
191
                                      guint32          time,
 
192
                                      GdkModifierType  state,
 
193
                                      GimpDisplay     *gdisp)
 
194
{
 
195
  GimpBucketFillTool    *bucket_tool;
 
196
  GimpBucketFillOptions *options;
 
197
  GimpContext           *context;
 
198
 
 
199
  bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
 
200
  options     = GIMP_BUCKET_FILL_OPTIONS (tool->tool_info->tool_options);
 
201
  context     = GIMP_CONTEXT (options);
 
202
 
 
203
  /*  if the 3rd button isn't pressed, fill the selected region  */
 
204
  if (! (state & GDK_BUTTON3_MASK))
 
205
    {
 
206
      gimp_drawable_bucket_fill (gimp_image_active_drawable (gdisp->gimage),
 
207
                                 context,
 
208
                                 options->fill_mode,
 
209
                                 gimp_context_get_paint_mode (context),
 
210
                                 gimp_context_get_opacity (context),
 
211
                                 ! options->fill_selection,
 
212
                                 options->fill_transparent,
 
213
                                 options->threshold,
 
214
                                 options->sample_merged,
 
215
                                 bucket_tool->target_x,
 
216
                                 bucket_tool->target_y);
 
217
 
 
218
      gimp_image_flush (gdisp->gimage);
 
219
    }
 
220
 
 
221
  gimp_tool_control_halt (tool->control);
 
222
}
 
223
 
 
224
static void
 
225
gimp_bucket_fill_tool_modifier_key (GimpTool        *tool,
 
226
                                    GdkModifierType  key,
 
227
                                    gboolean         press,
 
228
                                    GdkModifierType  state,
 
229
                                    GimpDisplay     *gdisp)
 
230
{
 
231
  GimpBucketFillOptions *options;
 
232
 
 
233
  options = GIMP_BUCKET_FILL_OPTIONS (tool->tool_info->tool_options);
 
234
 
 
235
  if (key == GDK_CONTROL_MASK)
 
236
    {
 
237
      switch (options->fill_mode)
 
238
        {
 
239
        case GIMP_FG_BUCKET_FILL:
 
240
          g_object_set (options, "fill-mode", GIMP_BG_BUCKET_FILL, NULL);
 
241
          break;
 
242
 
 
243
        case GIMP_BG_BUCKET_FILL:
 
244
          g_object_set (options, "fill-mode", GIMP_FG_BUCKET_FILL, NULL);
 
245
          break;
 
246
 
 
247
        default:
 
248
          break;
 
249
        }
 
250
    }
 
251
  else if (key == GDK_SHIFT_MASK)
 
252
    {
 
253
      g_object_set (options, "fill-selection", ! options->fill_selection, NULL);
 
254
    }
 
255
}
 
256
 
 
257
static void
 
258
gimp_bucket_fill_tool_cursor_update (GimpTool        *tool,
 
259
                                     GimpCoords      *coords,
 
260
                                     GdkModifierType  state,
 
261
                                     GimpDisplay     *gdisp)
 
262
{
 
263
  GimpBucketFillOptions *options;
 
264
  GimpCursorModifier     cmodifier = GIMP_CURSOR_MODIFIER_NONE;
 
265
 
 
266
  options = GIMP_BUCKET_FILL_OPTIONS (tool->tool_info->tool_options);
 
267
 
 
268
  if (gimp_image_coords_in_active_drawable (gdisp->gimage, coords))
 
269
    {
 
270
      GimpChannel *selection = gimp_image_get_mask (gdisp->gimage);
 
271
 
 
272
      /*  One more test--is there a selected region?
 
273
       *  if so, is cursor inside?
 
274
       */
 
275
      if (gimp_channel_is_empty (selection) ||
 
276
          gimp_channel_value (selection, coords->x, coords->y))
 
277
        {
 
278
          switch (options->fill_mode)
 
279
            {
 
280
            case GIMP_FG_BUCKET_FILL:
 
281
              cmodifier = GIMP_CURSOR_MODIFIER_FOREGROUND;
 
282
              break;
 
283
 
 
284
            case GIMP_BG_BUCKET_FILL:
 
285
              cmodifier = GIMP_CURSOR_MODIFIER_BACKGROUND;
 
286
              break;
 
287
 
 
288
            case GIMP_PATTERN_BUCKET_FILL:
 
289
              cmodifier = GIMP_CURSOR_MODIFIER_PATTERN;
 
290
              break;
 
291
            }
 
292
        }
 
293
    }
 
294
 
 
295
  gimp_tool_control_set_cursor_modifier (tool->control, cmodifier);
 
296
 
 
297
  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, gdisp);
 
298
}