gimpdisplayshell-cursor.c 9.11 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
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 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <gegl.h>
21 22 23 24
#include <gtk/gtk.h>

#include "display-types.h"

25
#include "config/gimpguiconfig.h"
26 27 28 29

#include "core/gimpimage.h"

#include "widgets/gimpcursor.h"
30
#include "widgets/gimpdialogfactory.h"
31
#include "widgets/gimpdockcontainer.h"
32
#include "widgets/gimpsessioninfo.h"
33

34
#include "gimpcanvascursor.h"
35
#include "gimpdisplay.h"
36
#include "gimpcursorview.h"
37 38 39 40 41 42 43
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-cursor.h"
#include "gimpdisplayshell-transform.h"
#include "gimpstatusbar.h"


static void  gimp_display_shell_real_set_cursor (GimpDisplayShell   *shell,
44 45 46 47
                                                 GimpCursorType      cursor_type,
                                                 GimpToolCursorType  tool_cursor,
                                                 GimpCursorModifier  modifier,
                                                 gboolean            always_install);
48 49


50 51
/*  public functions  */

52 53
void
gimp_display_shell_set_cursor (GimpDisplayShell   *shell,
54
                               GimpCursorType      cursor_type,
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                               GimpToolCursorType  tool_cursor,
                               GimpCursorModifier  modifier)
{
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

  if (! shell->using_override_cursor)
    {
      gimp_display_shell_real_set_cursor (shell,
                                          cursor_type,
                                          tool_cursor,
                                          modifier,
                                          FALSE);
    }
}

70 71 72 73 74 75 76 77 78 79 80 81
void
gimp_display_shell_unset_cursor (GimpDisplayShell *shell)
{
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

  if (! shell->using_override_cursor)
    {
      gimp_display_shell_real_set_cursor (shell,
                                          (GimpCursorType) -1, 0, 0, FALSE);
    }
}

82 83
void
gimp_display_shell_set_override_cursor (GimpDisplayShell *shell,
84
                                        GimpCursorType    cursor_type)
85 86 87 88 89 90 91 92 93 94
{
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

  if (! shell->using_override_cursor ||
      (shell->using_override_cursor &&
       shell->override_cursor != cursor_type))
    {
      shell->override_cursor       = cursor_type;
      shell->using_override_cursor = TRUE;

95
      gimp_cursor_set (shell->canvas,
96
                       shell->cursor_handedness,
97 98 99
                       cursor_type,
                       GIMP_TOOL_CURSOR_NONE,
                       GIMP_CURSOR_MODIFIER_NONE);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }
}

void
gimp_display_shell_unset_override_cursor (GimpDisplayShell *shell)
{
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

  if (shell->using_override_cursor)
    {
      shell->using_override_cursor = FALSE;

      gimp_display_shell_real_set_cursor (shell,
                                          shell->current_cursor,
                                          shell->tool_cursor,
                                          shell->cursor_modifier,
                                          TRUE);
    }
}

void
121 122 123 124 125 126
gimp_display_shell_update_software_cursor (GimpDisplayShell    *shell,
                                           GimpCursorPrecision  precision,
                                           gint                 display_x,
                                           gint                 display_y,
                                           gdouble              image_x,
                                           gdouble              image_y)
127
{
128 129 130 131 132
  GimpImageWindow   *image_window;
  GimpDialogFactory *factory;
  GimpStatusbar     *statusbar;
  GtkWidget         *widget;
  GimpImage         *image;
133

134 135
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

136
  image = gimp_display_get_image (shell->display);
137

138 139 140 141
  if (shell->draw_cursor &&
      shell->proximity   &&
      display_x >= 0     &&
      display_y >= 0)
142
    {
143
      gimp_canvas_item_begin_change (shell->cursor);
144

145 146 147
      gimp_canvas_cursor_set (shell->cursor,
                              display_x,
                              display_y);
148
      gimp_canvas_item_set_visible (shell->cursor, TRUE);
149

150 151 152
      gimp_canvas_item_end_change (shell->cursor);
    }
  else
153
    {
154
      gimp_canvas_item_set_visible (shell->cursor, FALSE);
155 156
    }

157 158 159
  /*  use the passed image_coords for the statusbar because they are
   *  possibly snapped...
   */
160
  statusbar = gimp_display_shell_get_statusbar (shell);
161

162
  gimp_statusbar_update_cursor (statusbar, precision, image_x, image_y);
163

164 165 166 167 168
  image_window = gimp_display_shell_get_window (shell);
  factory = gimp_dock_container_get_dialog_factory (GIMP_DOCK_CONTAINER (image_window));

  widget = gimp_dialog_factory_find_widget (factory, "gimp-cursor-view");

169
  if (widget)
170
    {
171
      GtkWidget *cursor_view = gtk_bin_get_child (GTK_BIN (widget));
172 173 174 175 176

      if (cursor_view)
        {
          gint t_x = -1;
          gint t_y = -1;
177

178 179 180
          /*  ...but use the unsnapped display_coords for the info window  */
          if (display_x >= 0 && display_y >= 0)
            gimp_display_shell_untransform_xy (shell, display_x, display_y,
181
                                               &t_x, &t_y, FALSE);
182 183

          gimp_cursor_view_update_cursor (GIMP_CURSOR_VIEW (cursor_view),
184
                                          image, shell->unit,
185 186 187
                                          t_x, t_y);
        }
    }
188 189
}

190
void
191
gimp_display_shell_clear_software_cursor (GimpDisplayShell *shell)
192
{
193 194 195 196
  GimpImageWindow   *image_window;
  GimpDialogFactory *factory;
  GimpStatusbar     *statusbar;
  GtkWidget         *widget;
197

198 199
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

200 201
  gimp_canvas_item_set_visible (shell->cursor, FALSE);

202
  statusbar = gimp_display_shell_get_statusbar (shell);
203

204
  gimp_statusbar_clear_cursor (statusbar);
205

206 207 208 209 210
  image_window = gimp_display_shell_get_window (shell);
  factory = gimp_dock_container_get_dialog_factory (GIMP_DOCK_CONTAINER (image_window));

  widget = gimp_dialog_factory_find_widget (factory, "gimp-cursor-view");

211
  if (widget)
212
    {
213
      GtkWidget *cursor_view = gtk_bin_get_child (GTK_BIN (widget));
214 215 216 217

      if (cursor_view)
        gimp_cursor_view_clear_cursor (GIMP_CURSOR_VIEW (cursor_view));
    }
218 219
}

220 221 222

/*  private functions  */

223 224
static void
gimp_display_shell_real_set_cursor (GimpDisplayShell   *shell,
225
                                    GimpCursorType      cursor_type,
226 227 228 229
                                    GimpToolCursorType  tool_cursor,
                                    GimpCursorModifier  modifier,
                                    gboolean            always_install)
{
230
  GimpHandedness cursor_handedness;
231 232 233

  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));

234 235 236 237
  if (cursor_type == (GimpCursorType) -1)
    {
      shell->current_cursor = cursor_type;

238
      if (gtk_widget_is_drawable (shell->canvas))
239
        gdk_window_set_cursor (gtk_widget_get_window (shell->canvas), NULL);
240 241 242 243

      return;
    }

244 245
  if (cursor_type != GIMP_CURSOR_NONE &&
      cursor_type != GIMP_CURSOR_BAD)
246
    {
247
      switch (shell->display->config->cursor_mode)
248 249 250 251 252
        {
        case GIMP_CURSOR_MODE_TOOL_ICON:
          break;

        case GIMP_CURSOR_MODE_TOOL_CROSSHAIR:
253 254
          if (cursor_type < GIMP_CURSOR_CORNER_TOP ||
              cursor_type > GIMP_CURSOR_SIDE_TOP_LEFT)
255 256 257 258 259 260
            {
              /* the corner and side cursors count as crosshair, so leave
               * them and override everything else
               */
              cursor_type = GIMP_CURSOR_CROSSHAIR_SMALL;
            }
261 262 263 264 265
          break;

        case GIMP_CURSOR_MODE_CROSSHAIR:
          cursor_type = GIMP_CURSOR_CROSSHAIR;
          tool_cursor = GIMP_TOOL_CURSOR_NONE;
266 267 268 269 270 271

          if (modifier != GIMP_CURSOR_MODIFIER_BAD)
            {
              /* the bad modifier is always shown */
              modifier = GIMP_CURSOR_MODIFIER_NONE;
            }
272 273
          break;
        }
274 275
    }

276 277
  cursor_type = gimp_cursor_rotate (cursor_type, shell->rotate_angle);

278
  cursor_handedness = GIMP_GUI_CONFIG (shell->display->config)->cursor_handedness;
279

280
  if (shell->cursor_handedness != cursor_handedness ||
281 282 283
      shell->current_cursor    != cursor_type       ||
      shell->tool_cursor       != tool_cursor       ||
      shell->cursor_modifier   != modifier          ||
284 285
      always_install)
    {
286 287 288 289
      shell->cursor_handedness = cursor_handedness;
      shell->current_cursor    = cursor_type;
      shell->tool_cursor       = tool_cursor;
      shell->cursor_modifier   = modifier;
290

291
      gimp_cursor_set (shell->canvas,
292
                       cursor_handedness,
293
                       cursor_type, tool_cursor, modifier);
294 295
    }
}