~ubuntu-branches/ubuntu/vivid/gimp/vivid

« back to all changes in this revision

Viewing changes to app/tools/gimpmagnifytool.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-08 18:50:03 UTC
  • mto: (1.1.26) (0.5.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: package-import@ubuntu.com-20120508185003-tltkvbaysf8d2426
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* GIMP - The GNU Image Manipulation Program
2
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
3
 *
4
 
 * This program is free software; you can redistribute it and/or modify
 
4
 * This program is free software: you can redistribute it and/or modify
5
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
 
6
 * the Free Software Foundation; either version 3 of the License, or
7
7
 * (at your option) any later version.
8
8
 *
9
9
 * This program is distributed in the hope that it will be useful,
12
12
 * GNU General Public License for more details.
13
13
 *
14
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.
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
16
 */
18
17
 
19
18
#include "config.h"
20
19
 
 
20
#include <gegl.h>
21
21
#include <gtk/gtk.h>
22
22
 
23
23
#include "libgimpmath/gimpmath.h"
28
28
#include "core/gimpimage.h"
29
29
 
30
30
#include "widgets/gimphelp-ids.h"
 
31
#include "widgets/gimpwidgets-utils.h"
31
32
 
 
33
#include "display/gimpcanvasrectangle.h"
32
34
#include "display/gimpdisplay.h"
33
35
#include "display/gimpdisplayshell.h"
34
36
#include "display/gimpdisplayshell-scale.h"
41
43
 
42
44
 
43
45
static void   gimp_magnify_tool_button_press   (GimpTool              *tool,
44
 
                                                GimpCoords            *coords,
 
46
                                                const GimpCoords      *coords,
45
47
                                                guint32                time,
46
48
                                                GdkModifierType        state,
 
49
                                                GimpButtonPressType    press_type,
47
50
                                                GimpDisplay           *display);
48
51
static void   gimp_magnify_tool_button_release (GimpTool              *tool,
49
 
                                                GimpCoords            *coords,
 
52
                                                const GimpCoords      *coords,
50
53
                                                guint32                time,
51
54
                                                GdkModifierType        state,
52
55
                                                GimpButtonReleaseType  release_type,
53
56
                                                GimpDisplay           *display);
54
57
static void   gimp_magnify_tool_motion         (GimpTool              *tool,
55
 
                                                GimpCoords            *coords,
 
58
                                                const GimpCoords      *coords,
56
59
                                                guint32                time,
57
60
                                                GdkModifierType        state,
58
61
                                                GimpDisplay           *display);
62
65
                                                GdkModifierType        state,
63
66
                                                GimpDisplay           *display);
64
67
static void   gimp_magnify_tool_cursor_update  (GimpTool              *tool,
65
 
                                                GimpCoords            *coords,
 
68
                                                const GimpCoords      *coords,
66
69
                                                GdkModifierType        state,
67
70
                                                GimpDisplay           *display);
68
71
 
69
72
static void   gimp_magnify_tool_draw           (GimpDrawTool          *draw_tool);
70
73
 
 
74
static void   gimp_magnify_tool_update_items   (GimpMagnifyTool       *magnify);
 
75
 
71
76
 
72
77
G_DEFINE_TYPE (GimpMagnifyTool, gimp_magnify_tool, GIMP_TYPE_DRAW_TOOL)
73
78
 
85
90
                "gimp-zoom-tool",
86
91
                _("Zoom"),
87
92
                _("Zoom Tool: Adjust the zoom level"),
88
 
                N_("tool|_Zoom"), "Z",
 
93
                N_("_Zoom"), "Z",
89
94
                NULL, GIMP_HELP_TOOL_ZOOM,
90
95
                GIMP_STOCK_TOOL_ZOOM,
91
96
                data);
111
116
{
112
117
  GimpTool *tool = GIMP_TOOL (magnify_tool);
113
118
 
114
 
  magnify_tool->x = 0;
115
 
  magnify_tool->y = 0;
116
 
  magnify_tool->w = 0;
117
 
  magnify_tool->h = 0;
118
 
 
119
119
  gimp_tool_control_set_scroll_lock            (tool->control, TRUE);
120
120
  gimp_tool_control_set_handle_empty_image     (tool->control, TRUE);
121
121
  gimp_tool_control_set_wants_click            (tool->control, TRUE);
122
122
  gimp_tool_control_set_snap_to                (tool->control, FALSE);
123
123
 
124
 
  gimp_tool_control_set_cursor                 (tool->control,
125
 
                                                GIMP_CURSOR_MOUSE);
126
124
  gimp_tool_control_set_tool_cursor            (tool->control,
127
125
                                                GIMP_TOOL_CURSOR_ZOOM);
128
126
  gimp_tool_control_set_cursor_modifier        (tool->control,
129
127
                                                GIMP_CURSOR_MODIFIER_PLUS);
130
128
  gimp_tool_control_set_toggle_cursor_modifier (tool->control,
131
129
                                                GIMP_CURSOR_MODIFIER_MINUS);
 
130
 
 
131
  magnify_tool->x = 0;
 
132
  magnify_tool->y = 0;
 
133
  magnify_tool->w = 0;
 
134
  magnify_tool->h = 0;
132
135
}
133
136
 
134
137
static void
135
 
gimp_magnify_tool_button_press (GimpTool        *tool,
136
 
                                GimpCoords      *coords,
137
 
                                guint32          time,
138
 
                                GdkModifierType  state,
139
 
                                GimpDisplay     *display)
 
138
gimp_magnify_tool_button_press (GimpTool            *tool,
 
139
                                const GimpCoords    *coords,
 
140
                                guint32              time,
 
141
                                GdkModifierType      state,
 
142
                                GimpButtonPressType  press_type,
 
143
                                GimpDisplay         *display)
140
144
{
141
145
  GimpMagnifyTool *magnify = GIMP_MAGNIFY_TOOL (tool);
142
146
 
153
157
 
154
158
static void
155
159
gimp_magnify_tool_button_release (GimpTool              *tool,
156
 
                                  GimpCoords            *coords,
 
160
                                  const GimpCoords      *coords,
157
161
                                  guint32                time,
158
162
                                  GdkModifierType        state,
159
163
                                  GimpButtonReleaseType  release_type,
161
165
{
162
166
  GimpMagnifyTool    *magnify = GIMP_MAGNIFY_TOOL (tool);
163
167
  GimpMagnifyOptions *options = GIMP_MAGNIFY_TOOL_GET_OPTIONS (tool);
164
 
  GimpDisplayShell   *shell   = GIMP_DISPLAY_SHELL (tool->display->shell);
 
168
  GimpDisplayShell   *shell   = gimp_display_get_shell (tool->display);
165
169
 
166
170
  gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
167
171
 
168
172
  gimp_tool_control_halt (tool->control);
169
173
 
170
 
  if (release_type != GIMP_BUTTON_RELEASE_CANCEL)
 
174
  switch (release_type)
171
175
    {
172
 
      gdouble x1, y1, x2, y2;
173
 
      gdouble width, height;
174
 
      gdouble current_scale;
175
 
      gdouble new_scale;
176
 
 
177
 
      x1     = (magnify->w < 0) ?  magnify->x + magnify->w : magnify->x;
178
 
      y1     = (magnify->h < 0) ?  magnify->y + magnify->h : magnify->y;
179
 
      width  = (magnify->w < 0) ? -magnify->w : magnify->w;
180
 
      height = (magnify->h < 0) ? -magnify->h : magnify->h;
181
 
      x2     = x1 + width;
182
 
      y2     = y1 + height;
183
 
 
184
 
      width  = MAX (1.0, width);
185
 
      height = MAX (1.0, height);
186
 
 
187
 
      current_scale = gimp_zoom_model_get_factor (shell->zoom);
188
 
 
189
 
      if (release_type == GIMP_BUTTON_RELEASE_CLICK ||
190
 
          release_type == GIMP_BUTTON_RELEASE_NO_MOTION)
191
 
        {
192
 
          gimp_display_shell_scale (shell,
193
 
                                    options->zoom_type,
194
 
                                    0.0,
195
 
                                    GIMP_ZOOM_FOCUS_BEST_GUESS);
196
 
        }
197
 
      else
198
 
        {
199
 
          gdouble display_width;
200
 
          gdouble display_height;
201
 
          gdouble factor = 1.0;
202
 
 
203
 
          display_width  = FUNSCALEX (shell, shell->disp_width);
204
 
          display_height = FUNSCALEY (shell, shell->disp_height);
205
 
 
206
 
          switch (options->zoom_type)
207
 
            {
208
 
            case GIMP_ZOOM_IN:
209
 
              factor = MIN ((display_width  / width),
210
 
                            (display_height / height));
211
 
              break;
212
 
 
213
 
            case GIMP_ZOOM_OUT:
214
 
              factor = MAX ((width  / display_width),
215
 
                            (height / display_height));
216
 
              break;
217
 
 
218
 
            default:
219
 
              break;
220
 
            }
221
 
 
222
 
          new_scale = current_scale * factor;
223
 
 
224
 
          if (new_scale != current_scale)
225
 
            {
226
 
              gdouble xres;
227
 
              gdouble yres;
228
 
              gint    offset_x = 0;
229
 
              gint    offset_y = 0;
230
 
 
231
 
              gimp_image_get_resolution (display->image, &xres, &yres);
232
 
 
233
 
              switch (options->zoom_type)
234
 
                {
235
 
                case GIMP_ZOOM_IN:
236
 
                  /*  move the center of the rectangle to the center of the
237
 
                   *  viewport:
238
 
                   *
239
 
                   *  new_offset = center of rectangle in new scale screen coords
240
 
                   *               including offset
241
 
                   *               -
242
 
                   *               center of viewport in screen coords without
243
 
                   *               offset
244
 
                   */
245
 
                  offset_x = RINT (new_scale * ((x1 + x2) / 2.0) *
246
 
                                   SCREEN_XRES (shell) / xres -
247
 
                                   (shell->disp_width / 2.0));
248
 
 
249
 
                  offset_y = RINT (new_scale * ((y1 + y2) / 2.0) *
250
 
                                   SCREEN_YRES (shell) / yres -
251
 
                                   (shell->disp_height / 2.0));
252
 
                  break;
253
 
 
254
 
                case GIMP_ZOOM_OUT:
255
 
                  /*  move the center of the viewport to the center of the
256
 
                   *  rectangle:
257
 
                   *
258
 
                   *  new_offset = center of viewport in new scale screen coords
259
 
                   *               including offset
260
 
                   *               -
261
 
                   *               center of rectangle in screen coords without
262
 
                   *               offset
263
 
                   */
264
 
                  offset_x = RINT (new_scale * UNSCALEX (shell,
265
 
                                                         shell->offset_x +
266
 
                                                         shell->disp_width / 2.0) *
267
 
                                   SCREEN_XRES (shell) / xres -
268
 
                                   (SCALEX (shell, (x1 + x2) / 2.0) -
269
 
                                    shell->offset_x));
270
 
 
271
 
                  offset_y = RINT (new_scale * UNSCALEY (shell,
272
 
                                                         shell->offset_y +
273
 
                                                         shell->disp_height / 2.0) *
274
 
                                   SCREEN_YRES (shell) / yres -
275
 
                                   (SCALEY (shell, (y1 + y2) / 2.0) -
276
 
                                    shell->offset_y));
277
 
                  break;
278
 
 
279
 
                default:
280
 
                  break;
281
 
                }
282
 
 
283
 
              gimp_display_shell_scale_by_values (shell,
284
 
                                                  new_scale,
285
 
                                                  offset_x, offset_y,
286
 
                                                  options->auto_resize);
287
 
            }
288
 
        }
 
176
    case GIMP_BUTTON_RELEASE_CLICK:
 
177
    case GIMP_BUTTON_RELEASE_NO_MOTION:
 
178
      gimp_display_shell_scale (shell,
 
179
                                options->zoom_type,
 
180
                                0.0,
 
181
                                GIMP_ZOOM_FOCUS_POINTER);
 
182
      break;
 
183
 
 
184
    case GIMP_BUTTON_RELEASE_NORMAL:
 
185
      {
 
186
        gdouble x1, y1, x2, y2;
 
187
        gdouble width, height;
 
188
        gdouble current_scale;
 
189
        gdouble new_scale;
 
190
        gdouble display_width;
 
191
        gdouble display_height;
 
192
        gdouble factor = 1.0;
 
193
 
 
194
        x1     = (magnify->w < 0) ?  magnify->x + magnify->w : magnify->x;
 
195
        y1     = (magnify->h < 0) ?  magnify->y + magnify->h : magnify->y;
 
196
        width  = (magnify->w < 0) ? -magnify->w : magnify->w;
 
197
        height = (magnify->h < 0) ? -magnify->h : magnify->h;
 
198
        x2     = x1 + width;
 
199
        y2     = y1 + height;
 
200
 
 
201
        width  = MAX (1.0, width);
 
202
        height = MAX (1.0, height);
 
203
 
 
204
        current_scale = gimp_zoom_model_get_factor (shell->zoom);
 
205
 
 
206
        display_width  = FUNSCALEX (shell, shell->disp_width);
 
207
        display_height = FUNSCALEY (shell, shell->disp_height);
 
208
 
 
209
        switch (options->zoom_type)
 
210
          {
 
211
          case GIMP_ZOOM_IN:
 
212
            factor = MIN ((display_width  / width),
 
213
                          (display_height / height));
 
214
            break;
 
215
 
 
216
          case GIMP_ZOOM_OUT:
 
217
            factor = MAX ((width  / display_width),
 
218
                          (height / display_height));
 
219
            break;
 
220
 
 
221
          default:
 
222
            break;
 
223
          }
 
224
 
 
225
        new_scale = current_scale * factor;
 
226
 
 
227
        if (new_scale != current_scale)
 
228
          {
 
229
            gint    offset_x = 0;
 
230
            gint    offset_y = 0;
 
231
            gdouble xres;
 
232
            gdouble yres;
 
233
            gdouble screen_xres;
 
234
            gdouble screen_yres;
 
235
 
 
236
            gimp_image_get_resolution (gimp_display_get_image (display),
 
237
                                       &xres, &yres);
 
238
            gimp_display_shell_get_screen_resolution (shell,
 
239
                                                      &screen_xres, &screen_yres);
 
240
 
 
241
            switch (options->zoom_type)
 
242
              {
 
243
              case GIMP_ZOOM_IN:
 
244
                /*  move the center of the rectangle to the center of the
 
245
                 *  viewport:
 
246
                 *
 
247
                 *  new_offset = center of rectangle in new scale screen coords
 
248
                 *               including offset
 
249
                 *               -
 
250
                 *               center of viewport in screen coords without
 
251
                 *               offset
 
252
                 */
 
253
                offset_x = RINT (new_scale * ((x1 + x2) / 2.0) *
 
254
                                 screen_xres / xres -
 
255
                                 (shell->disp_width / 2.0));
 
256
 
 
257
                offset_y = RINT (new_scale * ((y1 + y2) / 2.0) *
 
258
                                 screen_yres / yres -
 
259
                                 (shell->disp_height / 2.0));
 
260
                break;
 
261
 
 
262
              case GIMP_ZOOM_OUT:
 
263
                /*  move the center of the viewport to the center of the
 
264
                 *  rectangle:
 
265
                 *
 
266
                 *  new_offset = center of viewport in new scale screen coords
 
267
                 *               including offset
 
268
                 *               -
 
269
                 *               center of rectangle in screen coords without
 
270
                 *               offset
 
271
                 */
 
272
                offset_x = RINT (new_scale * UNSCALEX (shell,
 
273
                                                       shell->offset_x +
 
274
                                                       shell->disp_width / 2.0) *
 
275
                                 screen_xres / xres -
 
276
                                 (SCALEX (shell, (x1 + x2) / 2.0) -
 
277
                                  shell->offset_x));
 
278
 
 
279
                offset_y = RINT (new_scale * UNSCALEY (shell,
 
280
                                                       shell->offset_y +
 
281
                                                       shell->disp_height / 2.0) *
 
282
                                 screen_yres / yres -
 
283
                                 (SCALEY (shell, (y1 + y2) / 2.0) -
 
284
                                  shell->offset_y));
 
285
                break;
 
286
 
 
287
              default:
 
288
                break;
 
289
              }
 
290
 
 
291
            gimp_display_shell_scale_by_values (shell,
 
292
                                                new_scale,
 
293
                                                offset_x, offset_y,
 
294
                                                options->auto_resize);
 
295
          }
 
296
      }
 
297
      break;
 
298
 
 
299
    default:
 
300
      break;
289
301
    }
290
302
}
291
303
 
292
304
static void
293
 
gimp_magnify_tool_motion (GimpTool        *tool,
294
 
                          GimpCoords      *coords,
295
 
                          guint32          time,
296
 
                          GdkModifierType  state,
297
 
                          GimpDisplay     *display)
 
305
gimp_magnify_tool_motion (GimpTool         *tool,
 
306
                          const GimpCoords *coords,
 
307
                          guint32           time,
 
308
                          GdkModifierType   state,
 
309
                          GimpDisplay      *display)
298
310
{
299
311
  GimpMagnifyTool *magnify = GIMP_MAGNIFY_TOOL (tool);
300
312
 
301
 
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
302
 
 
303
313
  magnify->w = coords->x - magnify->x;
304
314
  magnify->h = coords->y - magnify->y;
305
315
 
306
 
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
 
316
  gimp_magnify_tool_update_items (magnify);
307
317
}
308
318
 
309
319
static void
315
325
{
316
326
  GimpMagnifyOptions *options = GIMP_MAGNIFY_TOOL_GET_OPTIONS (tool);
317
327
 
318
 
  if (key == GDK_CONTROL_MASK)
 
328
  if (key == gimp_get_toggle_behavior_mask ())
319
329
    {
320
330
      switch (options->zoom_type)
321
331
        {
334
344
}
335
345
 
336
346
static void
337
 
gimp_magnify_tool_cursor_update (GimpTool        *tool,
338
 
                                 GimpCoords      *coords,
339
 
                                 GdkModifierType  state,
340
 
                                 GimpDisplay     *display)
 
347
gimp_magnify_tool_cursor_update (GimpTool         *tool,
 
348
                                 const GimpCoords *coords,
 
349
                                 GdkModifierType   state,
 
350
                                 GimpDisplay      *display)
341
351
{
342
352
  GimpMagnifyOptions *options = GIMP_MAGNIFY_TOOL_GET_OPTIONS (tool);
343
353
 
352
362
{
353
363
  GimpMagnifyTool *magnify = GIMP_MAGNIFY_TOOL (draw_tool);
354
364
 
355
 
  gimp_draw_tool_draw_rectangle (draw_tool,
356
 
                                 FALSE,
 
365
  magnify->rectangle =
 
366
    gimp_draw_tool_add_rectangle (draw_tool, FALSE,
 
367
                                  magnify->x,
 
368
                                  magnify->y,
 
369
                                  magnify->w,
 
370
                                  magnify->h);
 
371
}
 
372
 
 
373
static void
 
374
gimp_magnify_tool_update_items (GimpMagnifyTool *magnify)
 
375
{
 
376
  if (gimp_draw_tool_is_active (GIMP_DRAW_TOOL (magnify)))
 
377
    {
 
378
      gimp_canvas_rectangle_set (magnify->rectangle,
357
379
                                 magnify->x,
358
380
                                 magnify->y,
359
381
                                 magnify->w,
360
 
                                 magnify->h,
361
 
                                 FALSE);
 
382
                                 magnify->h);
 
383
    }
362
384
}