gimpbucketfilltool.c 8.88 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19
#include "config.h"
Michael Natterer's avatar
Michael Natterer committed
20

Sven Neumann's avatar
Sven Neumann committed
21
#include <gtk/gtk.h>
Michael Natterer's avatar
Michael Natterer committed
22 23
#include <gdk/gdkkeysyms.h>

24 25
#include "libgimpwidgets/gimpwidgets.h"

26
#include "tools-types.h"
Sven Neumann's avatar
Sven Neumann committed
27

28 29
#include "config/gimpguiconfig.h"

Michael Natterer's avatar
Michael Natterer committed
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/gimppickable.h"
35

36 37
#include "widgets/gimphelp-ids.h"

38 39
#include "display/gimpdisplay.h"

40
#include "gimpbucketfilloptions.h"
41
#include "gimpbucketfilltool.h"
42
#include "gimptoolcontrol.h"
43

44
#include "gimp-intl.h"
45

Elliot Lee's avatar
Elliot Lee committed
46

47
/*  local function prototypes  */
48

49 50 51 52
static void   gimp_bucket_fill_tool_button_press   (GimpTool        *tool,
                                                    GimpCoords      *coords,
                                                    guint32          time,
                                                    GdkModifierType  state,
53
                                                    GimpDisplay     *display);
54 55 56 57
static void   gimp_bucket_fill_tool_button_release (GimpTool        *tool,
                                                    GimpCoords      *coords,
                                                    guint32          time,
                                                    GdkModifierType  state,
58
                                                    GimpDisplay     *display);
59 60 61 62
static void   gimp_bucket_fill_tool_modifier_key   (GimpTool        *tool,
                                                    GdkModifierType  key,
                                                    gboolean         press,
                                                    GdkModifierType  state,
63
                                                    GimpDisplay     *display);
64 65 66
static void   gimp_bucket_fill_tool_cursor_update  (GimpTool        *tool,
                                                    GimpCoords      *coords,
                                                    GdkModifierType  state,
67
                                                    GimpDisplay     *display);
68

Michael Natterer's avatar
Michael Natterer committed
69

70
G_DEFINE_TYPE (GimpBucketFillTool, gimp_bucket_fill_tool, GIMP_TYPE_TOOL)
71 72 73

#define parent_class gimp_bucket_fill_tool_parent_class

Elliot Lee's avatar
Elliot Lee committed
74

75
void
Nate Summers's avatar
Nate Summers committed
76
gimp_bucket_fill_tool_register (GimpToolRegisterCallback  callback,
77
                                gpointer                  data)
78
{
Nate Summers's avatar
Nate Summers committed
79
  (* callback) (GIMP_TYPE_BUCKET_FILL_TOOL,
80 81
                GIMP_TYPE_BUCKET_FILL_OPTIONS,
                gimp_bucket_fill_options_gui,
82 83
                GIMP_CONTEXT_FOREGROUND_MASK |
                GIMP_CONTEXT_BACKGROUND_MASK |
84 85 86
                GIMP_CONTEXT_OPACITY_MASK    |
                GIMP_CONTEXT_PAINT_MODE_MASK |
                GIMP_CONTEXT_PATTERN_MASK,
87
                "gimp-bucket-fill-tool",
88 89
                _("Bucket Fill"),
                _("Fill with a color or pattern"),
90
                N_("_Bucket Fill"), "<shift>B",
91
                NULL, GIMP_HELP_TOOL_BUCKET_FILL,
Nate Summers's avatar
Nate Summers committed
92
                GIMP_STOCK_TOOL_BUCKET_FILL,
93
                data);
94 95 96 97 98
}

static void
gimp_bucket_fill_tool_class_init (GimpBucketFillToolClass *klass)
{
Michael Natterer's avatar
Michael Natterer committed
99
  GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
100 101 102 103 104 105 106 107 108 109

  tool_class->button_press   = gimp_bucket_fill_tool_button_press;
  tool_class->button_release = gimp_bucket_fill_tool_button_release;
  tool_class->modifier_key   = gimp_bucket_fill_tool_modifier_key;
  tool_class->cursor_update  = gimp_bucket_fill_tool_cursor_update;
}

static void
gimp_bucket_fill_tool_init (GimpBucketFillTool *bucket_fill_tool)
{
Michael Natterer's avatar
Michael Natterer committed
110
  GimpTool *tool = GIMP_TOOL (bucket_fill_tool);
111

112 113 114 115 116 117 118
  gimp_tool_control_set_scroll_lock     (tool->control, TRUE);
  gimp_tool_control_set_tool_cursor     (tool->control,
                                         GIMP_TOOL_CURSOR_BUCKET_FILL);
  gimp_tool_control_set_action_value_1  (tool->control,
                                         "context/context-opacity-set");
  gimp_tool_control_set_action_object_1 (tool->control,
                                         "context/context-pattern-select-set");
119 120
}

121
static void
122 123 124
gimp_bucket_fill_tool_button_press (GimpTool        *tool,
                                    GimpCoords      *coords,
                                    guint32          time,
Michael Natterer's avatar
Michael Natterer committed
125
                                    GdkModifierType  state,
126
                                    GimpDisplay     *display)
Elliot Lee's avatar
Elliot Lee committed
127
{
Michael Natterer's avatar
Michael Natterer committed
128
  GimpBucketFillTool    *bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
129
  GimpBucketFillOptions *options     = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
130

131 132
  bucket_tool->target_x = coords->x;
  bucket_tool->target_y = coords->y;
Elliot Lee's avatar
Elliot Lee committed
133

134
  if (! options->sample_merged)
135 136 137
    {
      gint off_x, off_y;

138
      gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (display->image)),
139
                         &off_x, &off_y);
140 141 142 143

      bucket_tool->target_x -= off_x;
      bucket_tool->target_y -= off_y;
    }
Elliot Lee's avatar
Elliot Lee committed
144

145
  tool->display = display;
146
  gimp_tool_control_activate (tool->control);
Elliot Lee's avatar
Elliot Lee committed
147 148
}

149
static void
150 151 152
gimp_bucket_fill_tool_button_release (GimpTool        *tool,
                                      GimpCoords      *coords,
                                      guint32          time,
Michael Natterer's avatar
Michael Natterer committed
153
                                      GdkModifierType  state,
154
                                      GimpDisplay     *display)
Elliot Lee's avatar
Elliot Lee committed
155
{
Michael Natterer's avatar
Michael Natterer committed
156
  GimpBucketFillTool    *bucket_tool = GIMP_BUCKET_FILL_TOOL (tool);
157 158
  GimpBucketFillOptions *options     = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
  GimpContext           *context     = GIMP_CONTEXT (options);
159

Elliot Lee's avatar
Elliot Lee committed
160
  /*  if the 3rd button isn't pressed, fill the selected region  */
161
  if (! (state & GDK_BUTTON3_MASK))
Elliot Lee's avatar
Elliot Lee committed
162
    {
163
      gimp_drawable_bucket_fill (gimp_image_active_drawable (display->image),
164
                                 context,
165 166 167
                                 options->fill_mode,
                                 gimp_context_get_paint_mode (context),
                                 gimp_context_get_opacity (context),
168
                                 ! options->fill_selection,
169
                                 options->fill_transparent,
170
                                 options->fill_criterion,
171 172 173 174 175
                                 options->threshold,
                                 options->sample_merged,
                                 bucket_tool->target_x,
                                 bucket_tool->target_y);

176
      gimp_image_flush (display->image);
Elliot Lee's avatar
Elliot Lee committed
177 178
    }

179
  gimp_tool_control_halt (tool->control);
Elliot Lee's avatar
Elliot Lee committed
180 181 182
}

static void
183 184 185 186
gimp_bucket_fill_tool_modifier_key (GimpTool        *tool,
                                    GdkModifierType  key,
                                    gboolean         press,
                                    GdkModifierType  state,
187
                                    GimpDisplay     *display)
188
{
189
  GimpBucketFillOptions *options = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
190

191 192
  if (key == GDK_CONTROL_MASK)
    {
193
      switch (options->fill_mode)
194
        {
195
        case GIMP_FG_BUCKET_FILL:
196
          g_object_set (options, "fill-mode", GIMP_BG_BUCKET_FILL, NULL);
197
          break;
198

199
        case GIMP_BG_BUCKET_FILL:
200
          g_object_set (options, "fill-mode", GIMP_FG_BUCKET_FILL, NULL);
201
          break;
202

203 204 205 206
        default:
          break;
        }
    }
207 208 209 210
  else if (key == GDK_SHIFT_MASK)
    {
      g_object_set (options, "fill-selection", ! options->fill_selection, NULL);
    }
211 212 213 214 215
}

static void
gimp_bucket_fill_tool_cursor_update (GimpTool        *tool,
                                     GimpCoords      *coords,
Michael Natterer's avatar
Michael Natterer committed
216
                                     GdkModifierType  state,
217
                                     GimpDisplay     *display)
Elliot Lee's avatar
Elliot Lee committed
218
{
219
  GimpBucketFillOptions *options  = GIMP_BUCKET_FILL_TOOL_GET_OPTIONS (tool);
220
  GimpCursorModifier     modifier = GIMP_CURSOR_MODIFIER_BAD;
Elliot Lee's avatar
Elliot Lee committed
221

222 223
  if (gimp_image_coords_in_active_pickable (display->image, coords,
                                            options->sample_merged, TRUE))
224
    {
225
      switch (options->fill_mode)
226
        {
227 228 229 230 231 232 233 234 235 236 237
        case GIMP_FG_BUCKET_FILL:
          modifier = GIMP_CURSOR_MODIFIER_FOREGROUND;
          break;

        case GIMP_BG_BUCKET_FILL:
          modifier = GIMP_CURSOR_MODIFIER_BACKGROUND;
          break;

        case GIMP_PATTERN_BUCKET_FILL:
          modifier = GIMP_CURSOR_MODIFIER_PATTERN;
          break;
238
        }
239
    }
240

241
  gimp_tool_control_set_cursor_modifier (tool->control, modifier);
242

243
  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
Elliot Lee's avatar
Elliot Lee committed
244
}