forked from baedert/gtkimageview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtkimageview.c
2527 lines (2123 loc) · 81 KB
/
gtkimageview.c
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2016 Timm Bäder
*
* GTK+ is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* GLib 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with GTK+; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:gtkimageview
* @Short_description: A widget for displaying content images to users
* @Title: GtkImageView
*
* #GtkImageView is a widget intended to be used to display "content images"
* to users. What we refer to as "content images" in the documentation could
* be characterized as "images the user is deeply interested in". You should
* use #GtkImageView whenever you want to actually present an image instead
* of just using an icon.
*
* Contrary to #GtkImage, #GtkImageView does not just display an image with
* a fixed size, but provides ways of rotating and scaling it, as well as
* built-in gestures (via #GtkGestureRotate and #GtkGestureZoom) to rotate
* and zoom the image.
*
*
* # Scale factor handling
*
* All the functions intended to set the image of a #GtkImageView instance take a
* "scale_factor" parameter (except for gtk_image_view_set_surface(), in which case
* the scale factor of the surface is taken instead). This scale factor can be interpreted
* the same as the #GtkWidget:scale-factor property of #GtkWidget, but for the given image.
*
*/
#include "gtkimageview.h"
#include <gtk/gtk.h>
#include <math.h>
#define DEG_TO_RAD(x) (((x) / 360.0) * (2 * M_PI))
#define RAD_TO_DEG(x) (((x) / (2.0 * M_PI) * 360.0))
#define TRANSITION_DURATION (150.0 * 1000.0)
#define ANGLE_TRANSITION_MIN_DELTA (1.0)
#define SCALE_TRANSITION_MIN_DELTA (0.01)
#define _PARAM_READABLE G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
#define _PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
typedef struct
{
double hupper;
double vupper;
double hvalue;
double vvalue;
double angle;
double scale;
} State;
struct _GtkImageViewPrivate
{
double scale;
double angle;
int scale_factor;
gboolean fit_allocation : 1;
gboolean scale_set : 1;
gboolean snap_angle : 1;
gboolean rotatable : 1;
gboolean zoomable : 1;
gboolean in_rotate : 1;
gboolean in_zoom : 1;
gboolean size_valid : 1;
gboolean transitions_enabled : 1;
gboolean in_angle_transition : 1;
gboolean in_scale_transition : 1;
GtkGesture *rotate_gesture;
double gesture_start_angle;
double visible_angle;
GtkGesture *zoom_gesture;
double gesture_start_scale;
double visible_scale;
/* Current anchor point, or -1/-1.
* In widget coordinates. */
double anchor_x;
double anchor_y;
GdkWindow *event_window;
/* GtkScrollable stuff */
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
GtkScrollablePolicy hscroll_policy : 1;
GtkScrollablePolicy vscroll_policy : 1;
gboolean is_animation;
GdkPixbufAnimation *source_animation;
GdkPixbufAnimationIter *source_animation_iter;
cairo_surface_t *image_surface;
int animation_timeout;
/* Transitions */
double transition_start_angle;
gint64 angle_transition_start;
guint angle_transition_id;
double transition_start_scale;
gint64 scale_transition_start;
guint scale_transition_id;
/* We cache the bounding box size so we don't have to
* recompute it at every draw() */
double cached_width;
double cached_height;
double cached_scale;
};
enum
{
PROP_SCALE = 1,
PROP_SCALE_SET,
PROP_ANGLE,
PROP_ROTATABLE,
PROP_ZOOMABLE,
PROP_SNAP_ANGLE,
PROP_FIT_ALLOCATION,
PROP_TRANSITIONS_ENABLED,
LAST_WIDGET_PROPERTY,
PROP_HADJUSTMENT,
PROP_VADJUSTMENT,
PROP_HSCROLL_POLICY,
PROP_VSCROLL_POLICY,
LAST_PROPERTY
};
static GParamSpec *widget_props[LAST_WIDGET_PROPERTY] = { NULL, };
G_DEFINE_TYPE_WITH_CODE (GtkImageView, gtk_image_view, GTK_TYPE_WIDGET,
G_ADD_PRIVATE (GtkImageView)
G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
typedef struct _LoadTaskData LoadTaskData;
struct _LoadTaskData
{
int scale_factor;
gpointer source;
};
static void gtk_image_view_update_surface (GtkImageView *image_view,
const GdkPixbuf *frame,
int scale_factor);
static void adjustment_value_changed_cb (GtkAdjustment *adjustment,
gpointer user_data);
static void gtk_image_view_update_adjustments (GtkImageView *image_view);
static void gtk_image_view_compute_bounding_box (GtkImageView *image_view,
double *width,
double *height,
double *scale_out);
static void gtk_image_view_ensure_gestures (GtkImageView *image_view);
static inline void gtk_image_view_restrict_adjustment (GtkAdjustment *adjustment);
static void gtk_image_view_fix_anchor (GtkImageView *image_view,
double anchor_x,
double anchor_y,
State *old_state);
static inline double
gtk_image_view_get_real_scale (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->in_zoom || priv->in_scale_transition)
return priv->visible_scale;
else
return priv->scale;
}
static inline double
gtk_image_view_get_real_angle (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->in_rotate || priv->in_angle_transition)
return priv->visible_angle;
else
return priv->angle;
}
static inline double
gtk_image_view_clamp_angle (double angle)
{
double new_angle = angle;
if (angle > 360.0)
new_angle -= (int)(angle / 360.0) * 360;
else if (angle < 0.0)
new_angle += 360 - ((int)(angle /360) * 360.0);
g_assert (new_angle >= 0.0);
g_assert (new_angle <= 360.0);
return new_angle;
}
static inline int
gtk_image_view_get_snapped_angle (double angle)
{
return (int) ((angle + 45.0) / 90.0) * 90;
}
static void
gtk_image_view_get_current_state (GtkImageView *image_view,
State *state)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->hadjustment != NULL && priv->vadjustment != NULL)
{
state->hvalue = gtk_adjustment_get_value (priv->hadjustment);
state->vvalue = gtk_adjustment_get_value (priv->vadjustment);
state->hupper = gtk_adjustment_get_upper (priv->hadjustment);
state->vupper = gtk_adjustment_get_upper (priv->vadjustment);
}
state->angle = gtk_image_view_get_real_angle (image_view);
state->scale = gtk_image_view_get_real_scale (image_view);
}
static gboolean
gtk_image_view_transitions_enabled (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
gboolean animations_enabled;
g_object_get (gtk_widget_get_settings (GTK_WIDGET (image_view)),
"gtk-enable-animations", &animations_enabled,
NULL);
return priv->transitions_enabled && animations_enabled && priv->image_surface;
}
static gboolean
scale_frameclock_cb (GtkWidget *widget,
GdkFrameClock *frame_clock,
gpointer user_data)
{
GtkImageView *image_view = GTK_IMAGE_VIEW (widget);
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
State state;
gint64 now = gdk_frame_clock_get_frame_time (frame_clock);
double t = (now - priv->scale_transition_start) / TRANSITION_DURATION;
double new_scale = (priv->scale - priv->transition_start_scale) * t;
gtk_image_view_get_current_state (image_view, &state);
priv->visible_scale = priv->transition_start_scale + new_scale;
priv->size_valid = FALSE;
if (t >= 1.0)
priv->in_scale_transition = FALSE;
if (priv->hadjustment && priv->vadjustment)
{
GtkAllocation allocation;
gtk_widget_get_allocation (widget, &allocation);
gtk_image_view_update_adjustments (image_view);
gtk_image_view_fix_anchor (image_view,
allocation.width / 2,
allocation.height / 2,
&state);
}
if (priv->fit_allocation)
gtk_widget_queue_draw (widget);
else
gtk_widget_queue_resize (widget);
if (t >= 1.0)
{
priv->scale_transition_id = 0;
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
static void
gtk_image_view_animate_to_scale (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->scale_transition_id != 0)
gtk_widget_remove_tick_callback (GTK_WIDGET (image_view), priv->scale_transition_id);
/* Target scale is priv->scale */
priv->in_scale_transition = TRUE;
priv->visible_scale = priv->scale;
priv->transition_start_scale = priv->scale;
priv->scale_transition_start = gdk_frame_clock_get_frame_time (gtk_widget_get_frame_clock (GTK_WIDGET (image_view)));
priv->scale_transition_id = gtk_widget_add_tick_callback (GTK_WIDGET (image_view),
scale_frameclock_cb,
NULL, NULL);
}
static gboolean
angle_frameclock_cb (GtkWidget *widget,
GdkFrameClock *frame_clock,
gpointer user_data)
{
GtkImageView *image_view = GTK_IMAGE_VIEW (widget);
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
int direction = GPOINTER_TO_INT (user_data);
gint64 now = gdk_frame_clock_get_frame_time (frame_clock);
State state;
double target_angle = priv->angle;
if (direction == 1 && target_angle < priv->transition_start_angle)
target_angle += 360.0;
else if (direction == 0 && target_angle > priv->transition_start_angle)
target_angle -= 360.0;
double t = (now - priv->angle_transition_start) / TRANSITION_DURATION;
double new_angle = (target_angle - priv->transition_start_angle) * t;
gtk_image_view_get_current_state (image_view, &state);
priv->visible_angle = priv->transition_start_angle + new_angle;
priv->size_valid = FALSE;
if (t >= 1.0)
priv->in_angle_transition = FALSE;
if (priv->hadjustment && priv->vadjustment)
{
GtkAllocation allocation;
gtk_widget_get_allocation (widget, &allocation);
gtk_image_view_update_adjustments (image_view);
gtk_image_view_fix_anchor (image_view,
allocation.width / 2,
allocation.height / 2,
&state);
}
if (priv->fit_allocation)
gtk_widget_queue_draw (widget);
else
gtk_widget_queue_resize (widget);
if (t >= 1.0)
{
priv->angle_transition_id = 0;
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
static void
gtk_image_view_animate_to_angle (GtkImageView *image_view,
int direction)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->angle_transition_id != 0)
{
gtk_widget_remove_tick_callback (GTK_WIDGET (image_view), priv->angle_transition_id);
priv->angle_transition_id = 0;
}
/* Target angle is priv->angle */
priv->in_angle_transition = TRUE;
priv->visible_angle = priv->angle;
priv->transition_start_angle = priv->angle;
priv->angle_transition_start = gdk_frame_clock_get_frame_time (gtk_widget_get_frame_clock (GTK_WIDGET (image_view)));
priv->angle_transition_id = gtk_widget_add_tick_callback (GTK_WIDGET (image_view),
angle_frameclock_cb,
GINT_TO_POINTER (direction),
NULL);
}
static void
gtk_image_view_do_snapping (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
double new_angle = gtk_image_view_get_snapped_angle (priv->angle);
g_assert (priv->snap_angle);
if (gtk_image_view_transitions_enabled (image_view))
gtk_image_view_animate_to_angle (image_view, new_angle > priv->angle);
priv->angle = new_angle;
/* Don't notify! */
}
static void
free_load_task_data (LoadTaskData *data)
{
g_clear_object (&data->source);
g_slice_free (LoadTaskData, data);
}
static void
to_rotate_coords (GtkImageView *image_view,
State *state,
double in_x, double in_y,
double *out_x, double *out_y)
{
double cx = state->hupper / 2.0 - state->hvalue;
double cy = state->vupper / 2.0 - state->vvalue;
*out_x = in_x - cx;
*out_y = in_y - cy;
}
static void
gtk_image_view_fix_anchor (GtkImageView *image_view,
double anchor_x,
double anchor_y,
State *old_state)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
double hupper_delta = gtk_adjustment_get_upper (priv->hadjustment)
- old_state->hupper;
double vupper_delta = gtk_adjustment_get_upper (priv->vadjustment)
- old_state->vupper;
double hupper_delta_scale, vupper_delta_scale;
double hupper_delta_angle, vupper_delta_angle;
double cur_scale = gtk_image_view_get_real_scale (image_view);
g_assert (old_state->hupper > 0);
g_assert (old_state->vupper > 0);
g_assert (priv->hadjustment);
g_assert (priv->vadjustment);
g_assert (priv->size_valid);
g_assert (anchor_x >= 0);
g_assert (anchor_y >= 0);
g_assert (anchor_x < gtk_widget_get_allocated_width (GTK_WIDGET (image_view)));
g_assert (anchor_y < gtk_widget_get_allocated_height (GTK_WIDGET (image_view)));
/* Amount of upper change caused by scale */
hupper_delta_scale = ((old_state->hupper / old_state->scale) * cur_scale)
- old_state->hupper;
vupper_delta_scale = ((old_state->vupper / old_state->scale) * cur_scale)
- old_state->vupper;
/* Amount of upper change caused by angle */
hupper_delta_angle = hupper_delta - hupper_delta_scale;
vupper_delta_angle = vupper_delta - vupper_delta_scale;
/* As a first step, fix the anchor point with regard to the
* updated scale
*/
{
double hvalue = gtk_adjustment_get_value (priv->hadjustment);
double vvalue = gtk_adjustment_get_value (priv->vadjustment);
double px = anchor_x + hvalue;
double py = anchor_y + vvalue;
double px_after = (px / old_state->scale) * cur_scale;
double py_after = (py / old_state->scale) * cur_scale;
gtk_adjustment_set_value (priv->hadjustment,
hvalue + px_after - px);
gtk_adjustment_set_value (priv->vadjustment,
vvalue + py_after - py);
}
gtk_adjustment_set_value (priv->hadjustment,
gtk_adjustment_get_value (priv->hadjustment) + hupper_delta_angle / 2.0);
gtk_adjustment_set_value (priv->vadjustment,
gtk_adjustment_get_value (priv->vadjustment) + vupper_delta_angle / 2.0);
{
double rotate_anchor_x = 0;
double rotate_anchor_y = 0;
double anchor_angle;
double anchor_length;
double new_anchor_x, new_anchor_y;
double delta_x, delta_y;
/* Calculate the angle of the given anchor point relative to the
* bounding box center and the OLD state */
to_rotate_coords (image_view, old_state,
anchor_x, anchor_y,
&rotate_anchor_x, &rotate_anchor_y);
anchor_angle = atan2 (rotate_anchor_y, rotate_anchor_x);
anchor_length = sqrt ((rotate_anchor_x * rotate_anchor_x) +
(rotate_anchor_y * rotate_anchor_y));
/* The angle of the anchor point NOW is the old angle plus
* the difference between old surface angle and new surface angle */
anchor_angle += DEG_TO_RAD (gtk_image_view_get_real_angle (image_view)
- old_state->angle);
/* Calculate the position of the new anchor point, relative
* to the bounding box center */
new_anchor_x = cos (anchor_angle) * anchor_length;
new_anchor_y = sin (anchor_angle) * anchor_length;
/* The difference between old anchor and new anchor
* is what we care about... */
delta_x = rotate_anchor_x - new_anchor_x;
delta_y = rotate_anchor_y - new_anchor_y;
/* At last, make the old anchor match the new anchor */
gtk_adjustment_set_value (priv->hadjustment,
gtk_adjustment_get_value (priv->hadjustment) - delta_x);
gtk_adjustment_set_value (priv->vadjustment,
gtk_adjustment_get_value (priv->vadjustment) - delta_y);
}
gtk_widget_queue_draw (GTK_WIDGET (image_view));
}
static void
gtk_image_view_compute_bounding_box (GtkImageView *image_view,
double *width,
double *height,
double *scale_out)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
GtkAllocation alloc;
double image_width;
double image_height;
double bb_width = 0;
double bb_height = 0;
double upper_right_degrees;
double upper_left_degrees;
double r;
double upper_right_x, upper_right_y;
double upper_left_x, upper_left_y;
double scale;
double angle;
if (priv->size_valid)
{
*width = priv->cached_width;
*height = priv->cached_height;
if (scale_out)
*scale_out = priv->cached_scale;
return;
}
if (!priv->image_surface)
{
*width = 0;
*height = 0;
return;
}
gtk_widget_get_allocation (GTK_WIDGET (image_view), &alloc);
angle = gtk_image_view_get_real_angle (image_view);
image_width = cairo_image_surface_get_width (priv->image_surface) / priv->scale_factor;
image_height = cairo_image_surface_get_height (priv->image_surface) / priv->scale_factor;
upper_right_degrees = DEG_TO_RAD (angle) + atan (image_height / image_width);
upper_left_degrees = DEG_TO_RAD (angle) + atan (image_height / -image_width);
r = sqrt ((image_width / 2.0) * (image_width / 2.0) + (image_height / 2.0) * (image_height / 2.0));
upper_right_x = r * cos (upper_right_degrees);
upper_right_y = r * sin (upper_right_degrees);
upper_left_x = r * cos (upper_left_degrees);
upper_left_y = r * sin (upper_left_degrees);
bb_width = round (MAX (fabs (upper_right_x), fabs (upper_left_x)) * 2.0);
bb_height = round (MAX (fabs (upper_right_y), fabs (upper_left_y)) * 2.0);
if (priv->fit_allocation)
{
double scale_x = (double)alloc.width / (double)bb_width;
double scale_y = (double)alloc.height / (double)bb_height;
scale = MIN (MIN (scale_x, scale_y), 1.0);
}
else
{
scale = gtk_image_view_get_real_scale (image_view);
}
priv->cached_scale = scale;
if (scale_out)
*scale_out = scale;
if (priv->fit_allocation)
{
g_assert (!priv->scale_set);
priv->scale = scale;
g_object_notify_by_pspec (G_OBJECT (image_view),
widget_props[PROP_SCALE]);
}
*width = priv->cached_width = bb_width * scale;
*height = priv->cached_height = bb_height * scale;
priv->size_valid = TRUE;
}
static inline void
gtk_image_view_restrict_adjustment (GtkAdjustment *adjustment)
{
double value = gtk_adjustment_get_value (adjustment);
double upper = gtk_adjustment_get_upper (adjustment);
double page_size = gtk_adjustment_get_page_size (adjustment);
value = gtk_adjustment_get_value (adjustment);
upper = gtk_adjustment_get_upper (adjustment);
if (value > upper - page_size)
gtk_adjustment_set_value (adjustment, upper - page_size);
else if (value < 0)
gtk_adjustment_set_value (adjustment, 0);
}
static void
gtk_image_view_update_adjustments (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
double width, height;
int widget_width = gtk_widget_get_allocated_width (GTK_WIDGET (image_view));
int widget_height = gtk_widget_get_allocated_height (GTK_WIDGET (image_view));
if (!priv->hadjustment && !priv->vadjustment)
return;
if (!priv->image_surface)
{
if (priv->hadjustment)
gtk_adjustment_configure (priv->hadjustment, 0, 0, 1, 0, 0, 1);
if (priv->vadjustment)
gtk_adjustment_configure (priv->vadjustment, 0, 0, 1, 0, 0, 1);
return;
}
gtk_image_view_compute_bounding_box (image_view,
&width,
&height,
NULL);
/* compute_bounding_box makes sure that the bounding box is never bigger than
* the widget allocation if fit-allocation is set */
if (priv->hadjustment)
{
gtk_adjustment_set_upper (priv->hadjustment, MAX (width, widget_width));
gtk_adjustment_set_page_size (priv->hadjustment, widget_width);
gtk_image_view_restrict_adjustment (priv->hadjustment);
}
if (priv->vadjustment)
{
gtk_adjustment_set_upper (priv->vadjustment, MAX (height, widget_height));
gtk_adjustment_set_page_size (priv->vadjustment, widget_height);
gtk_image_view_restrict_adjustment (priv->vadjustment);
}
}
static void
gtk_image_view_set_scale_internal (GtkImageView *image_view,
double scale)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
scale = MAX (0, scale);
priv->scale = scale;
priv->size_valid = FALSE;
g_object_notify_by_pspec (G_OBJECT (image_view),
widget_props[PROP_SCALE]);
if (priv->scale_set)
{
priv->scale_set = FALSE;
g_object_notify_by_pspec (G_OBJECT (image_view),
widget_props[PROP_SCALE_SET]);
}
if (priv->fit_allocation)
{
priv->fit_allocation = FALSE;
g_object_notify_by_pspec (G_OBJECT (image_view),
widget_props[PROP_FIT_ALLOCATION]);
}
gtk_image_view_update_adjustments (image_view);
gtk_widget_queue_resize (GTK_WIDGET (image_view));
}
static void
gesture_zoom_begin_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer user_data)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (user_data);
if (!priv->zoomable ||
!priv->image_surface)
{
gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_DENIED);
return;
}
if (priv->anchor_x == -1 && priv->anchor_y == -1)
{
gtk_gesture_get_bounding_box_center (gesture,
&priv->anchor_x,
&priv->anchor_y);
}
}
static void
gesture_zoom_end_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
gtk_image_view_set_scale_internal (image_view, priv->visible_scale);
priv->in_zoom = FALSE;
priv->anchor_x = -1;
priv->anchor_y = -1;
}
static void
gesture_zoom_cancel_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer user_data)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (user_data);
if (priv->in_zoom)
gtk_image_view_set_scale (user_data, priv->gesture_start_scale);
priv->in_zoom = FALSE;
priv->anchor_x = -1;
priv->anchor_y = -1;
}
static void
gesture_zoom_changed_cb (GtkGestureZoom *gesture,
double delta,
GtkWidget *widget)
{
GtkImageView *image_view = GTK_IMAGE_VIEW (widget);
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
State state;
double new_scale;
if (!priv->in_zoom)
{
priv->in_zoom = TRUE;
priv->gesture_start_scale = priv->scale;
}
if (priv->fit_allocation)
{
priv->fit_allocation = FALSE;
g_object_notify_by_pspec (G_OBJECT (widget),
widget_props[PROP_FIT_ALLOCATION]);
}
new_scale = priv->gesture_start_scale * delta;
gtk_image_view_get_current_state (image_view, &state);
priv->visible_scale = new_scale;
priv->size_valid = FALSE;
gtk_image_view_update_adjustments (image_view);
if (priv->hadjustment != NULL && priv->vadjustment != NULL)
{
gtk_image_view_fix_anchor (image_view,
priv->anchor_x,
priv->anchor_y,
&state);
}
gtk_widget_queue_resize (GTK_WIDGET (image_view));
}
static void
gesture_rotate_begin_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer user_data)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (user_data);
if (!priv->rotatable ||
!priv->image_surface)
{
gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_DENIED);
return;
}
if (priv->anchor_x == -1 && priv->anchor_y == -1)
{
gtk_gesture_get_bounding_box_center (gesture,
&priv->anchor_x,
&priv->anchor_y);
}
}
static void
gesture_rotate_end_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
priv->angle = gtk_image_view_clamp_angle (priv->visible_angle);
if (priv->snap_angle)
{
/* Will update priv->angle */
gtk_image_view_do_snapping (image_view);
}
g_object_notify_by_pspec (image_view,
widget_props[PROP_ANGLE]);
priv->in_rotate = FALSE;
priv->anchor_x = -1;
priv->anchor_y = -1;
}
static void
gesture_rotate_cancel_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
gpointer image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
priv->size_valid = FALSE;
gtk_image_view_update_adjustments (image_view);
priv->in_rotate = FALSE;
priv->anchor_x = -1;
priv->anchor_y = -1;
}
static void
gesture_rotate_changed_cb (GtkGestureRotate *gesture,
double angle,
double delta,
GtkWidget *widget)
{
GtkImageView *image_view = GTK_IMAGE_VIEW (widget);
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
State old_state;
double new_angle;
if (!priv->in_rotate)
{
priv->in_rotate = TRUE;
priv->gesture_start_angle = priv->angle;
}
new_angle = priv->gesture_start_angle + RAD_TO_DEG (delta);
gtk_image_view_get_current_state (image_view, &old_state);
priv->visible_angle = new_angle;
priv->size_valid = FALSE;
gtk_image_view_update_adjustments (image_view);
if (priv->hadjustment && priv->vadjustment && !priv->fit_allocation)
gtk_image_view_fix_anchor (image_view,
priv->anchor_x,
priv->anchor_y,
&old_state);
if (priv->fit_allocation)
gtk_widget_queue_draw (widget);
else
gtk_widget_queue_resize (widget);
}
static void
gtk_image_view_ensure_gestures (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->zoomable && priv->zoom_gesture == NULL)
{
priv->zoom_gesture = gtk_gesture_zoom_new (GTK_WIDGET (image_view));
g_signal_connect (priv->zoom_gesture, "scale-changed",
(GCallback)gesture_zoom_changed_cb, image_view);
g_signal_connect (priv->zoom_gesture, "begin",
(GCallback)gesture_zoom_begin_cb, image_view);
g_signal_connect (priv->zoom_gesture, "end",
(GCallback)gesture_zoom_end_cb, image_view);
g_signal_connect (priv->zoom_gesture, "cancel",
(GCallback)gesture_zoom_cancel_cb, image_view);
}
else if (!priv->zoomable && priv->zoom_gesture != NULL)
{
g_clear_object (&priv->zoom_gesture);
}
if (priv->rotatable && priv->rotate_gesture == NULL)
{
priv->rotate_gesture = gtk_gesture_rotate_new (GTK_WIDGET (image_view));
g_signal_connect (priv->rotate_gesture, "angle-changed", (GCallback)gesture_rotate_changed_cb, image_view);
g_signal_connect (priv->rotate_gesture, "begin", (GCallback)gesture_rotate_begin_cb, image_view);
g_signal_connect (priv->rotate_gesture, "end", (GCallback)gesture_rotate_end_cb, image_view);
g_signal_connect (priv->rotate_gesture, "cancel", (GCallback)gesture_rotate_cancel_cb, image_view);
}
else if (!priv->rotatable && priv->rotate_gesture != NULL)
{
g_clear_object (&priv->rotate_gesture);
}
if (priv->zoom_gesture && priv->rotate_gesture)
gtk_gesture_group (priv->zoom_gesture,
priv->rotate_gesture);
}
static void
gtk_image_view_init (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
GtkWidget *widget = GTK_WIDGET (image_view);
gtk_widget_set_can_focus (widget, TRUE);
gtk_widget_set_has_window (widget, FALSE);
priv->scale = 1.0;
priv->angle = 0.0;
priv->visible_scale = 1.0;
priv->visible_angle = 0.0;
priv->snap_angle = FALSE;
priv->fit_allocation = FALSE;
priv->scale_set = FALSE;
priv->size_valid = FALSE;
priv->anchor_x = -1;
priv->anchor_y = -1;
priv->rotatable = TRUE;
priv->zoomable = TRUE;
priv->transitions_enabled = TRUE;
priv->angle_transition_id = 0;
priv->scale_transition_id = 0;
gtk_image_view_ensure_gestures (image_view);
}
static GdkPixbuf *
gtk_image_view_get_current_frame (GtkImageView *image_view)
{
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
g_assert (priv->source_animation);
if (priv->is_animation)
return gdk_pixbuf_animation_iter_get_pixbuf (priv->source_animation_iter);
else
return gdk_pixbuf_animation_get_static_image (priv->source_animation);
}
static gboolean
gtk_image_view_update_animation (gpointer user_data)
{
GtkImageView *image_view = user_data;
GtkImageViewPrivate *priv = gtk_image_view_get_instance_private (image_view);
if (priv->is_animation)
{