summaryrefslogtreecommitdiffstats
path: root/app/display/gimpnavigationeditor.c
blob: a518882bd7aac62c7a22603c14d62d059100a253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpnavigationeditor.c
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
 *
 * partly based on app/nav_window
 * Copyright (C) 1999 Andy Thomas <alt@gimp.org>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "display-types.h"

#include "config/gimpdisplayconfig.h"

#include "core/gimp.h"
#include "core/gimpcontext.h"
#include "core/gimpimage.h"
#include "core/gimpimageproxy.h"

#include "widgets/gimpdocked.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpmenufactory.h"
#include "widgets/gimpnavigationview.h"
#include "widgets/gimpuimanager.h"
#include "widgets/gimpviewrenderer.h"

#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-appearance.h"
#include "gimpdisplayshell-scale.h"
#include "gimpdisplayshell-scroll.h"
#include "gimpdisplayshell-transform.h"
#include "gimpnavigationeditor.h"

#include "gimp-intl.h"


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


static void        gimp_navigation_editor_docked_iface_init            (GimpDockedInterface  *iface);

static void        gimp_navigation_editor_dispose                      (GObject              *object);

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,
                                                                        gdouble               center_x,
                                                                        gdouble               center_y,
                                                                        gdouble               width,
                                                                        gdouble               height,
                                                                        GimpNavigationEditor *editor);
static void        gimp_navigation_editor_zoom                         (GimpNavigationView   *view,
                                                                        GimpZoomType          direction,
                                                                        GimpNavigationEditor *editor);
static void        gimp_navigation_editor_scroll                       (GimpNavigationView   *view,
                                                                        GdkScrollDirection    direction,
                                                                        GimpNavigationEditor *editor);

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

static void        gimp_navigation_editor_shell_infinite_canvas_notify (GimpDisplayShell     *shell,
                                                                        const GParamSpec     *pspec,
                                                                        GimpNavigationEditor *editor);
static void        gimp_navigation_editor_shell_scaled                 (GimpDisplayShell     *shell,
                                                                        GimpNavigationEditor *editor);
static void        gimp_navigation_editor_shell_scrolled               (GimpDisplayShell     *shell,
                                                                        GimpNavigationEditor *editor);
static void        gimp_navigation_editor_shell_rotated                (GimpDisplayShell     *shell,
                                                                        GimpNavigationEditor *editor);
static void        gimp_navigation_editor_shell_reconnect              (GimpDisplayShell     *shell,
                                                                        GimpNavigationEditor *editor);

static void        gimp_navigation_editor_viewable_size_changed        (GimpViewable         *viewable,
                                                                        GimpNavigationEditor *editor);

static void        gimp_navigation_editor_options_show_canvas_notify   (GimpDisplayOptions   *options,
                                                                        const GParamSpec     *pspec,
                                                                        GimpNavigationEditor *editor);

static void        gimp_navigation_editor_update_marker                (GimpNavigationEditor *editor);


G_DEFINE_TYPE_WITH_CODE (GimpNavigationEditor, gimp_navigation_editor,
                         GIMP_TYPE_EDITOR,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,
                                                gimp_navigation_editor_docked_iface_init))

#define parent_class gimp_navigation_editor_parent_class


static void
gimp_navigation_editor_class_init (GimpNavigationEditorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = gimp_navigation_editor_dispose;
}

static void
gimp_navigation_editor_docked_iface_init (GimpDockedInterface *iface)
{
  iface->set_context = gimp_navigation_editor_set_context;
}

static void
gimp_navigation_editor_init (GimpNavigationEditor *editor)
{
  GtkWidget *frame;

  editor->context       = NULL;
  editor->shell         = NULL;
  editor->scale_timeout = 0;

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (editor), frame, TRUE, TRUE, 0);
  gtk_widget_show (frame);

  editor->view = gimp_view_new_by_types (NULL,
                                         GIMP_TYPE_NAVIGATION_VIEW,
                                         GIMP_TYPE_IMAGE_PROXY,
                                         GIMP_VIEW_SIZE_MEDIUM, 0, TRUE);
  gtk_container_add (GTK_CONTAINER (frame), editor->view);
  gtk_widget_show (editor->view);

  g_signal_connect (editor->view, "marker-changed",
                    G_CALLBACK (gimp_navigation_editor_marker_changed),
                    editor);
  g_signal_connect (editor->view, "zoom",
                    G_CALLBACK (gimp_navigation_editor_zoom),
                    editor);
  g_signal_connect (editor->view, "scroll",
                    G_CALLBACK (gimp_navigation_editor_scroll),
                    editor);

  gtk_widget_set_sensitive (GTK_WIDGET (editor), FALSE);
}

static void
gimp_navigation_editor_dispose (GObject *object)
{
  GimpNavigationEditor *editor = GIMP_NAVIGATION_EDITOR (object);

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

  if (editor->scale_timeout)
    {
      g_source_remove (editor->scale_timeout);
      editor->scale_timeout = 0;
    }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gimp_navigation_editor_display_changed (GimpContext          *context,
                                        GimpDisplay          *display,
                                        GimpNavigationEditor *editor)
{
  GimpDisplayShell *shell = NULL;

  if (display && gimp_display_get_image (display))
    shell = gimp_display_get_shell (display);

  gimp_navigation_editor_set_shell (editor, shell);
}

static void
gimp_navigation_editor_image_chaged (GimpContext          *context,
                                     GimpImage            *image,
                                     GimpNavigationEditor *editor)
{
  GimpDisplay      *display = gimp_context_get_display (context);
  GimpDisplayShell *shell   = NULL;

  if (display && image)
    shell = gimp_display_get_shell (display);

  gimp_navigation_editor_set_shell (editor, shell);
}

static void
gimp_navigation_editor_set_context (GimpDocked  *docked,
                                    GimpContext *context)
{
  GimpNavigationEditor *editor  = GIMP_NAVIGATION_EDITOR (docked);
  GimpDisplay          *display = NULL;

  if (editor->context)
    {
      g_signal_handlers_disconnect_by_func (editor->context,
                                            gimp_navigation_editor_display_changed,
                                            editor);
      g_signal_handlers_disconnect_by_func (editor->context,
                                            gimp_navigation_editor_image_chaged,
                                            editor);
    }

  editor->context = context;

  if (editor->context)
    {
      g_signal_connect (context, "display-changed",
                        G_CALLBACK (gimp_navigation_editor_display_changed),
                        editor);
      /* make sure to also call gimp_navigation_editor_set_shell() when the
       * last image is closed, even though the display isn't changed, so that
       * the editor is properly cleared.
       */
      g_signal_connect (context, "image-changed",
                        G_CALLBACK (gimp_navigation_editor_image_chaged),
                        editor);

      display = gimp_context_get_display (context);
    }

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

  gimp_navigation_editor_display_changed (editor->context,
                                          display,
                                          editor);
}


/*  public functions  */

GtkWidget *
gimp_navigation_editor_new (GimpMenuFactory *menu_factory)
{
  return gimp_navigation_editor_new_private (menu_factory, NULL);
}

void
gimp_navigation_editor_popup (GimpDisplayShell *shell,
                              GtkWidget        *widget,
                              gint              click_x,
                              gint              click_y)
{
  GtkStyle             *style = gtk_widget_get_style (widget);
  GimpNavigationEditor *editor;
  GimpNavigationView   *view;
  GdkScreen            *screen;
  gint                  x, y;
  gint                  view_marker_center_x, view_marker_center_y;
  gint                  view_marker_width, view_marker_height;

  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
  g_return_if_fail (GTK_IS_WIDGET (widget));

  if (! shell->nav_popup)
    {
      GtkWidget *frame;

      shell->nav_popup = gtk_window_new (GTK_WINDOW_POPUP);

      frame = gtk_frame_new (NULL);
      gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
      gtk_container_add (GTK_CONTAINER (shell->nav_popup), frame);
      gtk_widget_show (frame);

      editor =
        GIMP_NAVIGATION_EDITOR (gimp_navigation_editor_new_private (NULL,
                                                                    shell));
      gtk_container_add (GTK_CONTAINER (frame), GTK_WIDGET (editor));
      gtk_widget_show (GTK_WIDGET (editor));

      g_signal_connect (editor->view, "button-release-event",
                        G_CALLBACK (gimp_navigation_editor_button_release),
                        shell);
    }
  else
    {
      GtkWidget *bin = gtk_bin_get_child (GTK_BIN (shell->nav_popup));

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

  view = GIMP_NAVIGATION_VIEW (editor->view);

  /* Set poup screen */
  screen = gtk_widget_get_screen (widget);
  gtk_window_set_screen (GTK_WINDOW (shell->nav_popup), screen);

  gimp_navigation_view_get_local_marker (view,
                                         &view_marker_center_x,
                                         &view_marker_center_y,
                                         &view_marker_width,
                                         &view_marker_height);
  /* Position the popup */
  {
    gint x_origin, y_origin;
    gint popup_width, popup_height;
    gint border_width, border_height;
    gint screen_click_x, screen_click_y;

    gdk_window_get_origin (gtk_widget_get_window (widget),
                           &x_origin, &y_origin);

    screen_click_x = x_origin + click_x;
    screen_click_y = y_origin + click_y;
    border_width   = 2 * style->xthickness;
    border_height  = 2 * style->ythickness;
    popup_width    = GIMP_VIEW (view)->renderer->width  - 2 * border_width;
    popup_height   = GIMP_VIEW (view)->renderer->height - 2 * border_height;

    x = screen_click_x -
        border_width -
        view_marker_center_x;

    y = screen_click_y -
        border_height -
        view_marker_center_y;

    /* 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);

    /* 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 ...
     */
    x = CLAMP (x, 0, gdk_screen_get_width (screen)  - popup_width);
    y = CLAMP (y, 0, gdk_screen_get_height (screen) - popup_height);

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

  gtk_widget_show (shell->nav_popup);
  gdk_flush ();

  /* fill in then grab pointer */
  gimp_navigation_view_set_motion_offset (view, 0, 0);
  gimp_navigation_view_grab_pointer (view);
}


/*  private functions  */

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

  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);

  if (shell)
    {
      Gimp              *gimp   = shell->display->gimp;
      GimpDisplayConfig *config = shell->display->config;
      GimpView          *view;

      editor = g_object_new (GIMP_TYPE_NAVIGATION_EDITOR, NULL);

      view = GIMP_VIEW (editor->view);

      gimp_view_renderer_set_size (view->renderer,
                                   config->nav_preview_size * 3,
                                   view->renderer->border_width);
      gimp_view_renderer_set_context (view->renderer,
                                      gimp_get_user_context (gimp));
      gimp_view_renderer_set_color_config (view->renderer,
                                           gimp_display_shell_get_color_config (shell));

      gimp_navigation_editor_set_shell (editor, shell);

    }
  else
    {
      GtkWidget *hscale;
      GtkWidget *hbox;

      editor = g_object_new (GIMP_TYPE_NAVIGATION_EDITOR,
                             "menu-factory",    menu_factory,
                             "menu-identifier", "<NavigationEditor>",
                             NULL);

      gtk_widget_set_size_request (editor->view,
                                   GIMP_VIEW_SIZE_HUGE,
                                   GIMP_VIEW_SIZE_HUGE);
      gimp_view_set_expand (GIMP_VIEW (editor->view), TRUE);

      /* the editor buttons */

      editor->zoom_out_button =
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-out", NULL);

      editor->zoom_in_button =
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-in", NULL);

      editor->zoom_100_button =
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-1-1", NULL);

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

      editor->zoom_fill_button =
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-zoom-fill", NULL);

      editor->shrink_wrap_button =
        gimp_editor_add_action_button (GIMP_EDITOR (editor), "view",
                                       "view-shrink-wrap", NULL);

      /* the zoom scale */

      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
      gtk_box_pack_end (GTK_BOX (editor), hbox, FALSE, FALSE, 0);
      gtk_widget_show (hbox);

      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);

      hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL,
                              editor->zoom_adjustment);
      gtk_scale_set_draw_value (GTK_SCALE (hscale), FALSE);
      gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);
      gtk_widget_show (hscale);

      /* 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);
    }

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

  return GTK_WIDGET (editor);
}

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_infinite_canvas_notify,
                                            editor);
      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);
      g_signal_handlers_disconnect_by_func (editor->shell,
                                            gimp_navigation_editor_shell_rotated,
                                            editor);
      g_signal_handlers_disconnect_by_func (editor->shell,
                                            gimp_navigation_editor_shell_reconnect,
                                            editor);

      g_signal_handlers_disconnect_by_func (editor->shell->options,
                                            gimp_navigation_editor_options_show_canvas_notify,
                                            editor);
      g_signal_handlers_disconnect_by_func (editor->shell->fullscreen_options,
                                            gimp_navigation_editor_options_show_canvas_notify,
                                            editor);
    }
  else if (shell)
    {
      gtk_widget_set_sensitive (GTK_WIDGET (editor), TRUE);
    }

  editor->shell = shell;

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

      g_clear_object (&editor->image_proxy);

      if (image)
        {
          editor->image_proxy = gimp_image_proxy_new (image);

          g_signal_connect (
            editor->image_proxy, "size-changed",
            G_CALLBACK (gimp_navigation_editor_viewable_size_changed),
            editor);
        }

      gimp_view_set_viewable (GIMP_VIEW (editor->view),
                              GIMP_VIEWABLE (editor->image_proxy));

      g_signal_connect (editor->shell, "notify::infinite-canvas",
                        G_CALLBACK (gimp_navigation_editor_shell_infinite_canvas_notify),
                        editor);
      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);
      g_signal_connect (editor->shell, "rotated",
                        G_CALLBACK (gimp_navigation_editor_shell_rotated),
                        editor);
      g_signal_connect (editor->shell, "reconnect",
                        G_CALLBACK (gimp_navigation_editor_shell_reconnect),
                        editor);

      g_signal_connect (editor->shell->options, "notify::show-canvas-boundary",
                        G_CALLBACK (gimp_navigation_editor_options_show_canvas_notify),
                        editor);
      g_signal_connect (editor->shell->fullscreen_options, "notify::show-canvas-boundary",
                        G_CALLBACK (gimp_navigation_editor_options_show_canvas_notify),
                        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);

      g_clear_object (&editor->image_proxy);
    }

  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)));
}

static gboolean
gimp_navigation_editor_button_release (GtkWidget        *widget,
                                       GdkEventButton   *bevent,
                                       GimpDisplayShell *shell)
{
  if (bevent->button == 1)
    {
      gtk_widget_hide (shell->nav_popup);
    }

  return FALSE;
}

static void
gimp_navigation_editor_marker_changed (GimpNavigationView   *view,
                                       gdouble               center_x,
                                       gdouble               center_y,
                                       gdouble               width,
                                       gdouble               height,
                                       GimpNavigationEditor *editor)
{
  GimpViewRenderer *renderer = GIMP_VIEW (editor->view)->renderer;

  if (editor->shell)
    {
      if (gimp_display_get_image (editor->shell->display))
        {
          GeglRectangle bounding_box;

          bounding_box = gimp_image_proxy_get_bounding_box (
            GIMP_IMAGE_PROXY (renderer->viewable));

          center_x += bounding_box.x;
          center_y += bounding_box.y;

          gimp_display_shell_scroll_center_image_xy (editor->shell,
                                                     center_x, center_y);
        }
    }
}

static void
gimp_navigation_editor_zoom (GimpNavigationView   *view,
                             GimpZoomType          direction,
                             GimpNavigationEditor *editor)
{
  g_return_if_fail (direction != GIMP_ZOOM_TO);

  if (editor->shell)
    {
      if (gimp_display_get_image (editor->shell->display))
        gimp_display_shell_scale (editor->shell,
                                  direction,
                                  0.0,
                                  GIMP_ZOOM_FOCUS_BEST_GUESS);
    }
}

static void
gimp_navigation_editor_scroll (GimpNavigationView   *view,
                               GdkScrollDirection    direction,
                               GimpNavigationEditor *editor)
{
  if (editor->shell)
    {
      GtkAdjustment *adj = NULL;
      gdouble        value;

      switch (direction)
        {
        case GDK_SCROLL_LEFT:
        case GDK_SCROLL_RIGHT:
          adj = editor->shell->hsbdata;
          break;

        case GDK_SCROLL_UP:
        case GDK_SCROLL_DOWN:
          adj = editor->shell->vsbdata;
          break;
        }

      gimp_assert (adj != NULL);

      value = gtk_adjustment_get_value (adj);

      switch (direction)
        {
        case GDK_SCROLL_LEFT:
        case GDK_SCROLL_UP:
          value -= gtk_adjustment_get_page_increment (adj) / 2;
          break;

        case GDK_SCROLL_RIGHT:
        case GDK_SCROLL_DOWN:
          value += gtk_adjustment_get_page_increment (adj) / 2;
          break;
        }

      value = CLAMP (value,
                     gtk_adjustment_get_lower (adj),
                     gtk_adjustment_get_upper (adj) -
                     gtk_adjustment_get_page_size (adj));

      gtk_adjustment_set_value (adj, value);
    }
}

static gboolean
gimp_navigation_editor_zoom_adj_changed_timeout (gpointer data)
{
  GimpNavigationEditor *editor = GIMP_NAVIGATION_EDITOR (data);
  GtkAdjustment        *adj    = editor->zoom_adjustment;

  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);

  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);
}

static void
gimp_navigation_editor_shell_infinite_canvas_notify (GimpDisplayShell     *shell,
                                                     const GParamSpec     *pspec,
                                                     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)));
}

static void
gimp_navigation_editor_shell_scaled (GimpDisplayShell     *shell,
                                     GimpNavigationEditor *editor)
{
  if (editor->zoom_label)
    {
      gchar *str;

      g_object_get (shell->zoom,
                    "percentage", &str,
                    NULL);
      gtk_label_set_text (GTK_LABEL (editor->zoom_label), str);
      g_free (str);
    }

  if (editor->zoom_adjustment)
    {
      gdouble val;

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

      g_signal_handlers_block_by_func (editor->zoom_adjustment,
                                       gimp_navigation_editor_zoom_adj_changed,
                                       editor);

      gtk_adjustment_set_value (editor->zoom_adjustment, val);

      g_signal_handlers_unblock_by_func (editor->zoom_adjustment,
                                         gimp_navigation_editor_zoom_adj_changed,
                                         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)));
}

static void
gimp_navigation_editor_shell_scrolled (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)));
}

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)));
}

static void
gimp_navigation_editor_viewable_size_changed (GimpViewable         *viewable,
                                              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)));
}

static void
gimp_navigation_editor_options_show_canvas_notify (GimpDisplayOptions   *options,
                                                   const GParamSpec     *pspec,
                                                   GimpNavigationEditor *editor)
{
  gimp_navigation_editor_update_marker (editor);
}

static void
gimp_navigation_editor_shell_reconnect (GimpDisplayShell     *shell,
                                        GimpNavigationEditor *editor)
{
  GimpImage *image = gimp_display_get_image (shell->display);

  g_clear_object (&editor->image_proxy);

  if (image)
    {
      editor->image_proxy = gimp_image_proxy_new (image);

      g_signal_connect (
        editor->image_proxy, "size-changed",
        G_CALLBACK (gimp_navigation_editor_viewable_size_changed),
        editor);
    }

  gimp_view_set_viewable (GIMP_VIEW (editor->view),
                          GIMP_VIEWABLE (editor->image_proxy));

  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)));
}

static void
gimp_navigation_editor_update_marker (GimpNavigationEditor *editor)
{
  GimpViewRenderer *renderer = GIMP_VIEW (editor->view)->renderer;
  GimpDisplayShell *shell    = editor->shell;

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

  if (renderer->viewable)
    {
      GimpNavigationView *view = GIMP_NAVIGATION_VIEW (editor->view);
      GimpImage          *image;
      GeglRectangle       bounding_box;
      gdouble             x, y;
      gdouble             w, h;

      image = gimp_image_proxy_get_image (
        GIMP_IMAGE_PROXY (renderer->viewable));

      gimp_image_proxy_set_show_all (
        GIMP_IMAGE_PROXY (renderer->viewable),
        gimp_display_shell_get_infinite_canvas (shell));

      bounding_box = gimp_image_proxy_get_bounding_box (
        GIMP_IMAGE_PROXY (renderer->viewable));

      gimp_display_shell_scroll_get_viewport (shell, &x, &y, &w, &h);
      gimp_display_shell_untransform_xy_f (shell,
                                           shell->disp_width  / 2,
                                           shell->disp_height / 2,
                                           &x, &y);

      x -= bounding_box.x;
      y -= bounding_box.y;

      gimp_navigation_view_set_marker (view,
                                       x, y, w, h,
                                       shell->flip_horizontally,
                                       shell->flip_vertically,
                                       shell->rotate_angle);

      gimp_navigation_view_set_canvas (
        view,
        gimp_display_shell_get_infinite_canvas (shell) &&
        gimp_display_shell_get_show_canvas (shell),
        -bounding_box.x,              -bounding_box.y,
        gimp_image_get_width (image), gimp_image_get_height (image));
    }
}