gimpnavigationeditor.c 25.9 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimpnavigationeditor.c
Michael Natterer's avatar
Michael Natterer committed
5 6 7 8 9
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
 *
 * partly based on app/nav_window
 * Copyright (C) 1999 Andy Thomas <alt@gimp.org>
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13 14 15 16 17 18 19 20
 * (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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23

24 25
#include "config.h"

26
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
27
#include <gtk/gtk.h>
28

29
#include "libgimpmath/gimpmath.h"
30 31
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
32
#include "display-types.h"
Michael Natterer's avatar
Michael Natterer committed
33

34
#include "config/gimpdisplayconfig.h"
35

36
#include "core/gimp.h"
37
#include "core/gimpcontext.h"
38
#include "core/gimpimage.h"
39

40
#include "widgets/gimpdocked.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "widgets/gimphelp-ids.h"
42
#include "widgets/gimpmenufactory.h"
43
#include "widgets/gimpnavigationview.h"
44
#include "widgets/gimpuimanager.h"
45
#include "widgets/gimpviewrenderer.h"
46

Michael Natterer's avatar
Michael Natterer committed
47 48 49 50
#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-scale.h"
#include "gimpdisplayshell-scroll.h"
51
#include "gimpdisplayshell-transform.h"
52
#include "gimpnavigationeditor.h"
Michael Natterer's avatar
Michael Natterer committed
53

54
#include "gimp-intl.h"
55

56

57 58 59
#define UPDATE_DELAY 300 /* From GtkRange in GTK+ 2.22 */


60 61
static void        gimp_navigation_editor_docked_iface_init (GimpDockedInterface  *iface);

62
static void        gimp_navigation_editor_dispose           (GObject              *object);
63 64 65 66 67 68 69 70 71 72 73 74 75

static void        gimp_navigation_editor_set_context       (GimpDocked           *docked,
                                                             GimpContext          *context);

static GtkWidget * gimp_navigation_editor_new_private       (GimpMenuFactory      *menu_factory,
                                                             GimpDisplayShell     *shell);

static void        gimp_navigation_editor_set_shell         (GimpNavigationEditor *editor,
                                                             GimpDisplayShell     *shell);
static gboolean    gimp_navigation_editor_button_release    (GtkWidget            *widget,
                                                             GdkEventButton       *bevent,
                                                             GimpDisplayShell     *shell);
static void        gimp_navigation_editor_marker_changed    (GimpNavigationView   *view,
76 77
                                                             gdouble               center_x,
                                                             gdouble               center_y,
78 79 80 81 82
                                                             gdouble               width,
                                                             gdouble               height,
                                                             GimpNavigationEditor *editor);
static void        gimp_navigation_editor_zoom              (GimpNavigationView   *view,
                                                             GimpZoomType          direction,
83
                                                             gdouble               delta,
84 85
                                                             GimpNavigationEditor *editor);
static void        gimp_navigation_editor_scroll            (GimpNavigationView   *view,
86
                                                             GdkEventScroll       *sevent,
87 88 89 90 91 92 93 94 95
                                                             GimpNavigationEditor *editor);

static void        gimp_navigation_editor_zoom_adj_changed  (GtkAdjustment        *adj,
                                                             GimpNavigationEditor *editor);

static void        gimp_navigation_editor_shell_scaled      (GimpDisplayShell     *shell,
                                                             GimpNavigationEditor *editor);
static void        gimp_navigation_editor_shell_scrolled    (GimpDisplayShell     *shell,
                                                             GimpNavigationEditor *editor);
96 97
static void        gimp_navigation_editor_shell_rotated     (GimpDisplayShell     *shell,
                                                             GimpNavigationEditor *editor);
98 99 100
static void        gimp_navigation_editor_shell_reconnect   (GimpDisplayShell     *shell,
                                                             GimpNavigationEditor *editor);
static void        gimp_navigation_editor_update_marker     (GimpNavigationEditor *editor);
101

BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
102

103 104 105
G_DEFINE_TYPE_WITH_CODE (GimpNavigationEditor, gimp_navigation_editor,
                         GIMP_TYPE_EDITOR,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,
106
                                                gimp_navigation_editor_docked_iface_init))
107

108
#define parent_class gimp_navigation_editor_parent_class
109

110

Michael Natterer's avatar
Michael Natterer committed
111
static void
112
gimp_navigation_editor_class_init (GimpNavigationEditorClass *klass)
Michael Natterer's avatar
Michael Natterer committed
113
{
114
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
115

116
  object_class->dispose = gimp_navigation_editor_dispose;
Michael Natterer's avatar
Michael Natterer committed
117 118
}

119 120 121 122 123 124
static void
gimp_navigation_editor_docked_iface_init (GimpDockedInterface *iface)
{
  iface->set_context = gimp_navigation_editor_set_context;
}

Michael Natterer's avatar
Michael Natterer committed
125
static void
126
gimp_navigation_editor_init (GimpNavigationEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
127 128 129
{
  GtkWidget *frame;

130 131 132
  editor->context       = NULL;
  editor->shell         = NULL;
  editor->scale_timeout = 0;
133

134 135
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
136
  gtk_box_pack_start (GTK_BOX (editor), frame, TRUE, TRUE, 0);
137 138
  gtk_widget_show (frame);

139 140
  editor->view = gimp_view_new_by_types (NULL,
                                         GIMP_TYPE_NAVIGATION_VIEW,
141
                                         GIMP_TYPE_IMAGE,
142
                                         GIMP_VIEW_SIZE_MEDIUM, 0, TRUE);
143 144
  gtk_container_add (GTK_CONTAINER (frame), editor->view);
  gtk_widget_show (editor->view);
145

146
  g_signal_connect (editor->view, "marker-changed",
147 148
                    G_CALLBACK (gimp_navigation_editor_marker_changed),
                    editor);
149
  g_signal_connect (editor->view, "zoom",
150 151
                    G_CALLBACK (gimp_navigation_editor_zoom),
                    editor);
152
  g_signal_connect (editor->view, "scroll",
153 154 155 156
                    G_CALLBACK (gimp_navigation_editor_scroll),
                    editor);

  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
Michael Natterer's avatar
Michael Natterer committed
157
}
158

159
static void
160
gimp_navigation_editor_dispose (GObject *object)
161
{
162 163 164 165 166
  GimpNavigationEditor *editor = GIMP_NAVIGATION_EDITOR (object);

  if (editor->shell)
    gimp_navigation_editor_set_shell (editor, NULL);

167 168 169 170 171 172
  if (editor->scale_timeout)
    {
      g_source_remove (editor->scale_timeout);
      editor->scale_timeout = 0;
    }

173
  G_OBJECT_CLASS (parent_class)->dispose (object);
174 175 176
}

static void
177
gimp_navigation_editor_display_changed (GimpContext          *context,
178
                                        GimpDisplay          *display,
179
                                        GimpNavigationEditor *editor)
180 181 182
{
  GimpDisplayShell *shell = NULL;

183
  if (display)
184
    shell = gimp_display_get_shell (display);
185

186
  gimp_navigation_editor_set_shell (editor, shell);
187 188 189
}

static void
190 191
gimp_navigation_editor_set_context (GimpDocked  *docked,
                                    GimpContext *context)
192
{
193
  GimpNavigationEditor *editor  = GIMP_NAVIGATION_EDITOR (docked);
194
  GimpDisplay          *display = NULL;
195

196
  if (editor->context)
197
    {
198
      g_signal_handlers_disconnect_by_func (editor->context,
199
                                            gimp_navigation_editor_display_changed,
200
                                            editor);
201 202
    }

203
  editor->context = context;
204

205
  if (editor->context)
206
    {
207
      g_signal_connect (context, "display-changed",
208
                        G_CALLBACK (gimp_navigation_editor_display_changed),
209
                        editor);
210

211
      display = gimp_context_get_display (context);
212 213
    }

214 215 216
  gimp_view_renderer_set_context (GIMP_VIEW (editor->view)->renderer,
                                  context);

217 218 219
  gimp_navigation_editor_display_changed (editor->context,
                                          display,
                                          editor);
220 221
}

222

Michael Natterer's avatar
Michael Natterer committed
223
/*  public functions  */
224

Michael Natterer's avatar
Michael Natterer committed
225
GtkWidget *
226
gimp_navigation_editor_new (GimpMenuFactory *menu_factory)
Michael Natterer's avatar
Michael Natterer committed
227
{
228
  return gimp_navigation_editor_new_private (menu_factory, NULL);
229 230 231
}

void
232 233
gimp_navigation_editor_popup (GimpDisplayShell *shell,
                              GtkWidget        *widget,
234
                              GdkEvent         *event,
235 236
                              gint              click_x,
                              gint              click_y)
237
{
238
  GtkStyleContext      *style = gtk_widget_get_style_context (widget);
239 240 241
  GimpNavigationEditor *editor;
  GimpNavigationView   *view;
  gint                  x, y;
242
  gint                  view_marker_center_x, view_marker_center_y;
243
  gint                  view_marker_width, view_marker_height;
244

Michael Natterer's avatar
Michael Natterer committed
245 246 247
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
  g_return_if_fail (GTK_IS_WIDGET (widget));

248
  if (! shell->nav_popup)
249
    {
250
      GtkWidget *frame;
251

Michael Natterer's avatar
Michael Natterer committed
252
      shell->nav_popup = gtk_window_new (GTK_WINDOW_POPUP);
253 254 255

      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
Michael Natterer's avatar
Michael Natterer committed
256
      gtk_container_add (GTK_CONTAINER (shell->nav_popup), frame);
257 258
      gtk_widget_show (frame);

259
      editor =
260 261
        GIMP_NAVIGATION_EDITOR (gimp_navigation_editor_new_private (NULL,
                                                                    shell));
262 263
      gtk_container_add (GTK_CONTAINER (frame), GTK_WIDGET (editor));
      gtk_widget_show (GTK_WIDGET (editor));
264

265
      g_signal_connect (editor->view, "button-release-event",
266
                        G_CALLBACK (gimp_navigation_editor_button_release),
Michael Natterer's avatar
Michael Natterer committed
267
                        shell);
268
    }
269 270
  else
    {
Sven Neumann's avatar
Sven Neumann committed
271 272 273
      GtkWidget *bin = gtk_bin_get_child (GTK_BIN (shell->nav_popup));

      editor = GIMP_NAVIGATION_EDITOR (gtk_bin_get_child (GTK_BIN (bin)));
274 275
    }

276
  view = GIMP_NAVIGATION_VIEW (editor->view);
277

278
  /* Set poup screen */
279 280
  gtk_window_set_screen (GTK_WINDOW (shell->nav_popup),
                         gtk_widget_get_screen (widget));
281

282
  gimp_navigation_view_get_local_marker (view,
283 284
                                         &view_marker_center_x,
                                         &view_marker_center_y,
285 286
                                         &view_marker_width,
                                         &view_marker_height);
287 288
  /* Position the popup */
  {
289 290 291 292 293 294 295 296 297 298
    GdkMonitor   *monitor;
    GdkRectangle  workarea;
    GtkBorder     border;
    gint          x_origin, y_origin;
    gint          popup_width, popup_height;
    gint          border_width, border_height;
    gint          screen_click_x, screen_click_y;

    monitor = gimp_widget_get_monitor (widget);
    gdk_monitor_get_workarea (monitor, &workarea);
Sven Neumann's avatar
Sven Neumann committed
299

300 301
    gdk_window_get_origin (gtk_widget_get_window (widget),
                           &x_origin, &y_origin);
302

303 304 305
    gtk_style_context_get_border (style, gtk_widget_get_state_flags (widget),
                                  &border);

306 307
    screen_click_x = x_origin + click_x;
    screen_click_y = y_origin + click_y;
308 309
    border_width   = 2 * border.left;
    border_height  = 2 * border.top;
310 311
    popup_width    = GIMP_VIEW (view)->renderer->width  - 2 * border_width;
    popup_height   = GIMP_VIEW (view)->renderer->height - 2 * border_height;
312 313 314

    x = screen_click_x -
        border_width -
315
        view_marker_center_x;
316 317 318

    y = screen_click_y -
        border_height -
319
        view_marker_center_y;
320

321 322 323 324 325 326 327
    /* When the image is zoomed out and overscrolled, the above
     * calculation risks positioning the popup far far away from the
     * click coordinate. We don't want that, so perform some clamping.
     */
    x = CLAMP (x, screen_click_x - popup_width,  screen_click_x);
    y = CLAMP (y, screen_click_y - popup_height, screen_click_y);

328 329 330 331 332 333 334 335 336
    /* If the popup doesn't fit into the screen, we have a problem.
     * We move the popup onscreen and risk that the pointer is not
     * in the square representing the viewable area anymore. Moving
     * the pointer will make the image scroll by a large amount,
     * but then it works as usual. Probably better than a popup that
     * is completely unusable in the lower right of the screen.
     *
     * Warping the pointer would be another solution ...
     */
337 338
    x = CLAMP (x, workarea.x, workarea.x + workarea.width  - popup_width);
    y = CLAMP (y, workarea.y, workarea.y + workarea.height - popup_height);
339 340 341

    gtk_window_move (GTK_WINDOW (shell->nav_popup), x, y);
  }
342

Michael Natterer's avatar
Michael Natterer committed
343
  gtk_widget_show (shell->nav_popup);
344
  gdk_display_flush (gtk_widget_get_display (shell->nav_popup));
345 346

  /* fill in then grab pointer */
347
  gimp_navigation_view_set_motion_offset (view, 0, 0);
348
  gimp_navigation_view_grab_pointer (view, event);
349 350
}

351

Michael Natterer's avatar
Michael Natterer committed
352
/*  private functions  */
353

Michael Natterer's avatar
Michael Natterer committed
354
static GtkWidget *
355 356
gimp_navigation_editor_new_private (GimpMenuFactory  *menu_factory,
                                    GimpDisplayShell *shell)
Michael Natterer's avatar
Michael Natterer committed
357
{
358
  GimpNavigationEditor *editor;
359

360 361 362 363
  g_return_val_if_fail (menu_factory == NULL ||
                        GIMP_IS_MENU_FACTORY (menu_factory), NULL);
  g_return_val_if_fail (shell == NULL || GIMP_IS_DISPLAY_SHELL (shell), NULL);
  g_return_val_if_fail (menu_factory || shell, NULL);
364

365
  if (shell)
366
    {
367
      Gimp              *gimp   = shell->display->gimp;
368
      GimpDisplayConfig *config = shell->display->config;
369 370 371 372
      GimpView          *view;

      editor = g_object_new (GIMP_TYPE_NAVIGATION_EDITOR, NULL);

373
      view = GIMP_VIEW (editor->view);
374

375 376 377
      gimp_view_renderer_set_size (view->renderer,
                                   config->nav_preview_size * 3,
                                   view->renderer->border_width);
378
      gimp_view_renderer_set_context (view->renderer,
379
                                      gimp_get_user_context (gimp));
380 381
      gimp_view_renderer_set_color_config (view->renderer,
                                           gimp_display_shell_get_color_config (shell));
382 383

      gimp_navigation_editor_set_shell (editor, shell);
384

385 386
    }
  else
Michael Natterer's avatar
Michael Natterer committed
387 388
    {
      GtkWidget *hscale;
389
      GtkWidget *hbox;
Michael Natterer's avatar
Michael Natterer committed
390

391 392 393 394 395
      editor = g_object_new (GIMP_TYPE_NAVIGATION_EDITOR,
                             "menu-factory",    menu_factory,
                             "menu-identifier", "<NavigationEditor>",
                             NULL);

396
      gtk_widget_set_size_request (editor->view,
397 398
                                   GIMP_VIEW_SIZE_HUGE,
                                   GIMP_VIEW_SIZE_HUGE);
399
      gimp_view_set_expand (GIMP_VIEW (editor->view), TRUE);
400

Michael Natterer's avatar
Michael Natterer committed
401 402
      /* the editor buttons */

403
      editor->zoom_out_button =
404 405
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-out", NULL);
Michael Natterer's avatar
Michael Natterer committed
406

407
      editor->zoom_in_button =
408 409
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-in", NULL);
Michael Natterer's avatar
Michael Natterer committed
410

411
      editor->zoom_100_button =
412 413
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-1-1", NULL);
Michael Natterer's avatar
Michael Natterer committed
414

415
      editor->zoom_fit_in_button =
416 417
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-fit-in", NULL);
418

419
      editor->zoom_fill_button =
420
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
421
                                       "view-zoom-fill", NULL);
Michael Natterer's avatar
Michael Natterer committed
422

423
      editor->shrink_wrap_button =
424 425
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-shrink-wrap", NULL);
Michael Natterer's avatar
Michael Natterer committed
426 427 428

      /* the zoom scale */

Michael Natterer's avatar
Michael Natterer committed
429
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
430 431
      gtk_box_pack_end (GTK_BOX (editor), hbox, FALSE, FALSE, 0);
      gtk_widget_show (hbox);
Michael Natterer's avatar
Michael Natterer committed
432

433 434 435 436 437 438 439
      editor->zoom_adjustment =
        GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -8.0, 8.0, 0.5, 1.0, 0.0));

      g_signal_connect (editor->zoom_adjustment, "value-changed",
                        G_CALLBACK (gimp_navigation_editor_zoom_adj_changed),
                        editor);

440 441
      hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL,
                              editor->zoom_adjustment);
442 443 444 445
      gtk_scale_set_draw_value (GTK_SCALE (hscale), FALSE);
      gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);
      gtk_widget_show (hscale);

446 447 448 449 450 451
      /* the zoom label */

      editor->zoom_label = gtk_label_new ("100%");
      gtk_label_set_width_chars (GTK_LABEL (editor->zoom_label), 7);
      gtk_box_pack_start (GTK_BOX (hbox), editor->zoom_label, FALSE, FALSE, 0);
      gtk_widget_show (editor->zoom_label);
452
    }
453

454
  gimp_view_renderer_set_background (GIMP_VIEW (editor->view)->renderer,
455
                                     GIMP_ICON_TEXTURE);
456

457
  return GTK_WIDGET (editor);
458
}
459

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static void
gimp_navigation_editor_set_shell (GimpNavigationEditor *editor,
                                  GimpDisplayShell     *shell)
{
  g_return_if_fail (GIMP_IS_NAVIGATION_EDITOR (editor));
  g_return_if_fail (! shell || GIMP_IS_DISPLAY_SHELL (shell));

  if (shell == editor->shell)
    return;

  if (editor->shell)
    {
      g_signal_handlers_disconnect_by_func (editor->shell,
                                            gimp_navigation_editor_shell_scaled,
                                            editor);
      g_signal_handlers_disconnect_by_func (editor->shell,
                                            gimp_navigation_editor_shell_scrolled,
                                            editor);
478 479 480
      g_signal_handlers_disconnect_by_func (editor->shell,
                                            gimp_navigation_editor_shell_rotated,
                                            editor);
481 482 483 484 485 486 487 488 489 490 491 492 493
      g_signal_handlers_disconnect_by_func (editor->shell,
                                            gimp_navigation_editor_shell_reconnect,
                                            editor);
    }
  else if (shell)
    {
      gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
    }

  editor->shell = shell;

  if (editor->shell)
    {
494 495
      GimpImage *image = gimp_display_get_image (shell->display);

496
      gimp_view_set_viewable (GIMP_VIEW (editor->view),
497
                              GIMP_VIEWABLE (image));
498 499 500 501 502 503 504

      g_signal_connect (editor->shell, "scaled",
                        G_CALLBACK (gimp_navigation_editor_shell_scaled),
                        editor);
      g_signal_connect (editor->shell, "scrolled",
                        G_CALLBACK (gimp_navigation_editor_shell_scrolled),
                        editor);
505 506 507
      g_signal_connect (editor->shell, "rotated",
                        G_CALLBACK (gimp_navigation_editor_shell_rotated),
                        editor);
508 509 510 511 512 513 514 515 516 517 518 519
      g_signal_connect (editor->shell, "reconnect",
                        G_CALLBACK (gimp_navigation_editor_shell_reconnect),
                        editor);

      gimp_navigation_editor_shell_scaled (editor->shell, editor);
    }
  else
    {
      gimp_view_set_viewable (GIMP_VIEW (editor->view), NULL);
      gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
    }

520 521 522
  if (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)))
    gimp_ui_manager_update (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)),
                            gimp_editor_get_popup_data (GIMP_EDITOR (editor)));
523 524
}

Michael Natterer's avatar
Michael Natterer committed
525
static gboolean
526 527 528
gimp_navigation_editor_button_release (GtkWidget        *widget,
                                       GdkEventButton   *bevent,
                                       GimpDisplayShell *shell)
529
{
Michael Natterer's avatar
Michael Natterer committed
530
  if (bevent->button == 1)
531
    {
Michael Natterer's avatar
Michael Natterer committed
532
      gtk_widget_hide (shell->nav_popup);
533
    }
534

Michael Natterer's avatar
Michael Natterer committed
535
  return FALSE;
536 537
}

Michael Natterer's avatar
Michael Natterer committed
538
static void
539
gimp_navigation_editor_marker_changed (GimpNavigationView   *view,
540 541
                                       gdouble               center_x,
                                       gdouble               center_y,
542 543
                                       gdouble               width,
                                       gdouble               height,
544
                                       GimpNavigationEditor *editor)
545
{
546
  if (editor->shell)
547
    {
548
      if (gimp_display_get_image (editor->shell->display))
549 550
        gimp_display_shell_scroll_center_image_xy (editor->shell,
                                                   center_x, center_y);
551
    }
Michael Natterer's avatar
Michael Natterer committed
552
}
553

Michael Natterer's avatar
Michael Natterer committed
554
static void
555 556
gimp_navigation_editor_zoom (GimpNavigationView   *view,
                             GimpZoomType          direction,
557
                             gdouble               delta,
558
                             GimpNavigationEditor *editor)
Michael Natterer's avatar
Michael Natterer committed
559
{
560 561
  g_return_if_fail (direction != GIMP_ZOOM_TO);

562
  if (editor->shell)
563
    {
564 565 566
      if (gimp_display_get_image (editor->shell->display))
        gimp_display_shell_scale (editor->shell,
                                  direction,
567
                                  delta,
568
                                  GIMP_ZOOM_FOCUS_BEST_GUESS);
569
    }
570 571
}

Michael Natterer's avatar
Michael Natterer committed
572
static void
573
gimp_navigation_editor_scroll (GimpNavigationView   *view,
574
                               GdkEventScroll       *sevent,
575
                               GimpNavigationEditor *editor)
576
{
577
  if (editor->shell)
Michael Natterer's avatar
Michael Natterer committed
578
    {
579 580 581 582 583 584 585 586 587 588
      gdouble value_x;
      gdouble value_y;

      gimp_scroll_adjustment_values (sevent,
                                     editor->shell->hsbdata,
                                     editor->shell->vsbdata,
                                     &value_x, &value_y);

      gtk_adjustment_set_value (editor->shell->hsbdata, value_x);
      gtk_adjustment_set_value (editor->shell->vsbdata, value_y);
Michael Natterer's avatar
Michael Natterer committed
589
    }
590
}
591

592 593
static gboolean
gimp_navigation_editor_zoom_adj_changed_timeout (gpointer data)
594
{
595 596 597
  GimpNavigationEditor *editor = GIMP_NAVIGATION_EDITOR (data);
  GtkAdjustment        *adj    = editor->zoom_adjustment;

598 599 600 601 602
  if (gimp_display_get_image (editor->shell->display))
    gimp_display_shell_scale (editor->shell,
                              GIMP_ZOOM_TO,
                              pow (2.0, gtk_adjustment_get_value (adj)),
                              GIMP_ZOOM_FOCUS_BEST_GUESS);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

  editor->scale_timeout = 0;

  return FALSE;
}

static void
gimp_navigation_editor_zoom_adj_changed (GtkAdjustment        *adj,
                                         GimpNavigationEditor *editor)
{
  if (editor->scale_timeout)
    g_source_remove (editor->scale_timeout);

  editor->scale_timeout =
    g_timeout_add (UPDATE_DELAY,
                   gimp_navigation_editor_zoom_adj_changed_timeout,
                   editor);
620
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
621

622
static void
623 624
gimp_navigation_editor_shell_scaled (GimpDisplayShell     *shell,
                                     GimpNavigationEditor *editor)
625
{
626
  if (editor->zoom_label)
627
    {
628
      gchar *str;
629

630 631 632 633 634
      g_object_get (shell->zoom,
                    "percentage", &str,
                    NULL);
      gtk_label_set_text (GTK_LABEL (editor->zoom_label), str);
      g_free (str);
635 636
    }

637
  if (editor->zoom_adjustment)
Michael Natterer's avatar
Michael Natterer committed
638
    {
639
      gdouble val;
640

641
      val = log (gimp_zoom_model_get_factor (shell->zoom)) / G_LN2;
642

643 644 645
      g_signal_handlers_block_by_func (editor->zoom_adjustment,
                                       gimp_navigation_editor_zoom_adj_changed,
                                       editor);
646

647
      gtk_adjustment_set_value (editor->zoom_adjustment, val);
648

649 650 651
      g_signal_handlers_unblock_by_func (editor->zoom_adjustment,
                                         gimp_navigation_editor_zoom_adj_changed,
                                         editor);
Michael Natterer's avatar
Michael Natterer committed
652
    }
653

654
  gimp_navigation_editor_update_marker (editor);
655

656 657 658
  if (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)))
    gimp_ui_manager_update (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)),
                            gimp_editor_get_popup_data (GIMP_EDITOR (editor)));
659 660
}

661
static void
662 663
gimp_navigation_editor_shell_scrolled (GimpDisplayShell     *shell,
                                       GimpNavigationEditor *editor)
664
{
665
  gimp_navigation_editor_update_marker (editor);
666

667 668 669
  if (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)))
    gimp_ui_manager_update (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)),
                            gimp_editor_get_popup_data (GIMP_EDITOR (editor)));
670 671
}

672 673 674 675 676 677 678 679 680 681 682
static void
gimp_navigation_editor_shell_rotated (GimpDisplayShell     *shell,
                                      GimpNavigationEditor *editor)
{
  gimp_navigation_editor_update_marker (editor);

  if (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)))
    gimp_ui_manager_update (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)),
                            gimp_editor_get_popup_data (GIMP_EDITOR (editor)));
}

683
static void
684 685
gimp_navigation_editor_shell_reconnect (GimpDisplayShell     *shell,
                                        GimpNavigationEditor *editor)
686
{
687 688
  GimpImage *image = gimp_display_get_image (shell->display);

689
  gimp_view_set_viewable (GIMP_VIEW (editor->view),
690
                          GIMP_VIEWABLE (image));
691

692 693 694
  if (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)))
    gimp_ui_manager_update (gimp_editor_get_ui_manager (GIMP_EDITOR (editor)),
                            gimp_editor_get_popup_data (GIMP_EDITOR (editor)));
695 696
}

697
static void
698
gimp_navigation_editor_update_marker (GimpNavigationEditor *editor)
699
{
700 701
  GimpViewRenderer *renderer = GIMP_VIEW (editor->view)->renderer;
  GimpDisplayShell *shell    = editor->shell;
702

703 704
  if (renderer->dot_for_dot != shell->dot_for_dot)
    gimp_view_renderer_set_dot_for_dot (renderer, shell->dot_for_dot);
705

706
  if (renderer->viewable)
707 708 709 710 711
    {
      GimpNavigationView *view = GIMP_NAVIGATION_VIEW (editor->view);
      gdouble             x, y;
      gdouble             w, h;

712
      gimp_display_shell_scroll_get_viewport (shell, &x, &y, &w, &h);
713 714 715 716
      gimp_display_shell_untransform_xy_f (shell,
                                           shell->disp_width  / 2,
                                           shell->disp_height / 2,
                                           &x, &y);
717

718 719 720 721 722
      gimp_navigation_view_set_marker (view,
                                       x, y, w, h,
                                       shell->flip_horizontally,
                                       shell->flip_vertically,
                                       shell->rotate_angle);
723
    }
724
}