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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
|
//
// fiv-view.c: image viewing widget
//
// Copyright (c) 2021 - 2022, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
#include "config.h"
#include "fiv-io.h"
#include "fiv-view.h"
#include <math.h>
#include <stdbool.h>
#include <gtk/gtk.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif // GDK_WINDOWING_X11
#ifdef GDK_WINDOWING_QUARTZ
#include <gdk/gdkquartz.h>
#endif // GDK_WINDOWING_QUARTZ
GType
fiv_view_command_get_type(void)
{
static gsize guard;
if (g_once_init_enter(&guard)) {
#define XX(constant, name) {constant, #constant, name},
static const GEnumValue values[] = {FIV_VIEW_COMMANDS(XX) {}};
#undef XX
GType type = g_enum_register_static(
g_intern_static_string("FivViewCommand"), values);
g_once_init_leave(&guard, type);
}
return guard;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct _FivView {
GtkWidget parent_instance;
GtkAdjustment *hadjustment; ///< GtkScrollable boilerplate
GtkAdjustment *vadjustment; ///< GtkScrollable boilerplate
GtkScrollablePolicy hscroll_policy; ///< GtkScrollable boilerplate
GtkScrollablePolicy vscroll_policy; ///< GtkScrollable boilerplate
gchar *messages; ///< Image load information
gchar *uri; ///< Path to the current image (if any)
cairo_surface_t *image; ///< The loaded image (sequence)
cairo_surface_t *page; ///< Current page within image, weak
cairo_surface_t *page_scaled; ///< Current page within image, scaled
cairo_surface_t *frame; ///< Current frame within page, weak
FivIoOrientation orientation; ///< Current page orientation
bool enable_cms : 1; ///< Smooth scaling toggle
bool filter : 1; ///< Smooth scaling toggle
bool checkerboard : 1; ///< Show checkerboard background
bool enhance : 1; ///< Try to enhance picture data
bool scale_to_fit : 1; ///< Image no larger than the allocation
bool fixate : 1; ///< Keep zoom and position
double scale; ///< Scaling factor
double drag_start[2]; ///< Adjustment values for drag origin
cairo_surface_t *enhance_swap; ///< Quick swap in/out
FivIoProfile screen_cms_profile; ///< Target colour profile for widget
int remaining_loops; ///< Greater than zero if limited
gint64 frame_time; ///< Current frame's start, µs precision
gulong frame_update_connection; ///< GdkFrameClock::update
};
G_DEFINE_TYPE_EXTENDED(FivView, fiv_view, GTK_TYPE_WIDGET, 0,
G_IMPLEMENT_INTERFACE(GTK_TYPE_SCROLLABLE, NULL))
typedef struct _Dimensions {
double width, height;
} Dimensions;
static FivIoOrientation view_left[9] = {
[FivIoOrientationUnknown] = FivIoOrientationUnknown,
[FivIoOrientation0] = FivIoOrientation270,
[FivIoOrientationMirror0] = FivIoOrientationMirror270,
[FivIoOrientation180] = FivIoOrientation90,
[FivIoOrientationMirror180] = FivIoOrientationMirror90,
[FivIoOrientationMirror270] = FivIoOrientationMirror180,
[FivIoOrientation90] = FivIoOrientation0,
[FivIoOrientationMirror90] = FivIoOrientationMirror0,
[FivIoOrientation270] = FivIoOrientation180,
};
static FivIoOrientation view_mirror[9] = {
[FivIoOrientationUnknown] = FivIoOrientationUnknown,
[FivIoOrientation0] = FivIoOrientationMirror0,
[FivIoOrientationMirror0] = FivIoOrientation0,
[FivIoOrientation180] = FivIoOrientationMirror180,
[FivIoOrientationMirror180] = FivIoOrientation180,
[FivIoOrientationMirror270] = FivIoOrientation270,
[FivIoOrientation90] = FivIoOrientationMirror90,
[FivIoOrientationMirror90] = FivIoOrientation90,
[FivIoOrientation270] = FivIoOrientationMirror270,
};
static FivIoOrientation view_right[9] = {
[FivIoOrientationUnknown] = FivIoOrientationUnknown,
[FivIoOrientation0] = FivIoOrientation90,
[FivIoOrientationMirror0] = FivIoOrientationMirror90,
[FivIoOrientation180] = FivIoOrientation270,
[FivIoOrientationMirror180] = FivIoOrientationMirror270,
[FivIoOrientationMirror270] = FivIoOrientationMirror0,
[FivIoOrientation90] = FivIoOrientation180,
[FivIoOrientationMirror90] = FivIoOrientationMirror180,
[FivIoOrientation270] = FivIoOrientation0,
};
enum {
PROP_MESSAGES = 1,
PROP_SCALE,
PROP_SCALE_TO_FIT,
PROP_FIXATE,
PROP_ENABLE_CMS,
PROP_FILTER,
PROP_CHECKERBOARD,
PROP_ENHANCE,
PROP_PLAYING,
PROP_HAS_IMAGE,
PROP_CAN_ANIMATE,
PROP_HAS_PREVIOUS_PAGE,
PROP_HAS_NEXT_PAGE,
N_PROPERTIES,
// These are overriden, we do not register them.
PROP_HADJUSTMENT,
PROP_VADJUSTMENT,
PROP_HSCROLL_POLICY,
PROP_VSCROLL_POLICY,
};
static GParamSpec *view_properties[N_PROPERTIES];
enum {
COMMAND,
LAST_SIGNAL
};
// Globals are, sadly, the canonical way of storing signal numbers.
static guint view_signals[LAST_SIGNAL];
static void
on_adjustment_value_changed(
G_GNUC_UNUSED GtkAdjustment *adjustment, gpointer user_data)
{
gtk_widget_queue_draw(GTK_WIDGET(user_data));
}
static Dimensions
get_surface_dimensions(FivView *self)
{
if (!self->image)
return (Dimensions) {};
Dimensions dimensions = {};
fiv_io_orientation_dimensions(
self->page, self->orientation, &dimensions.width, &dimensions.height);
return dimensions;
}
static void
get_display_dimensions(FivView *self, int *width, int *height)
{
Dimensions surface_dimensions = get_surface_dimensions(self);
*width = ceil(surface_dimensions.width * self->scale);
*height = ceil(surface_dimensions.height * self->scale);
}
static void
update_adjustments(FivView *self)
{
int w = 0, h = 0;
get_display_dimensions(self, &w, &h);
GtkAllocation alloc;
gtk_widget_get_allocation(GTK_WIDGET(self), &alloc);
if (self->hadjustment) {
gtk_adjustment_configure(self->hadjustment,
gtk_adjustment_get_value(self->hadjustment), 0, w,
alloc.width * 0.1, alloc.width * 0.9, alloc.width);
}
if (self->vadjustment) {
gtk_adjustment_configure(self->vadjustment,
gtk_adjustment_get_value(self->vadjustment), 0, h,
alloc.height * 0.1, alloc.height * 0.9, alloc.height);
}
}
static gboolean
replace_adjustment(
FivView *self, GtkAdjustment **adjustment, GtkAdjustment *replacement)
{
if (*adjustment == replacement)
return FALSE;
if (*adjustment) {
g_signal_handlers_disconnect_by_func(
*adjustment, on_adjustment_value_changed, self);
g_clear_object(adjustment);
}
if (replacement) {
*adjustment = g_object_ref(replacement);
g_signal_connect(*adjustment, "value-changed",
G_CALLBACK(on_adjustment_value_changed), self);
update_adjustments(self);
}
return TRUE;
}
static void
fiv_view_finalize(GObject *gobject)
{
FivView *self = FIV_VIEW(gobject);
g_clear_pointer(&self->screen_cms_profile, fiv_io_profile_free);
g_clear_pointer(&self->enhance_swap, cairo_surface_destroy);
g_clear_pointer(&self->image, cairo_surface_destroy);
g_clear_pointer(&self->page_scaled, cairo_surface_destroy);
g_free(self->uri);
g_free(self->messages);
replace_adjustment(self, &self->hadjustment, NULL);
replace_adjustment(self, &self->vadjustment, NULL);
G_OBJECT_CLASS(fiv_view_parent_class)->finalize(gobject);
}
static void
fiv_view_get_property(
GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
FivView *self = FIV_VIEW(object);
switch (property_id) {
case PROP_MESSAGES:
g_value_set_string(value, self->messages);
break;
case PROP_SCALE:
g_value_set_double(value, self->scale);
break;
case PROP_SCALE_TO_FIT:
g_value_set_boolean(value, self->scale_to_fit);
break;
case PROP_FIXATE:
g_value_set_boolean(value, self->fixate);
break;
case PROP_ENABLE_CMS:
g_value_set_boolean(value, self->enable_cms);
break;
case PROP_FILTER:
g_value_set_boolean(value, self->filter);
break;
case PROP_CHECKERBOARD:
g_value_set_boolean(value, self->checkerboard);
break;
case PROP_ENHANCE:
g_value_set_boolean(value, self->enhance);
break;
case PROP_PLAYING:
g_value_set_boolean(value, !!self->frame_update_connection);
break;
case PROP_HAS_IMAGE:
g_value_set_boolean(value, !!self->image);
break;
case PROP_CAN_ANIMATE:
g_value_set_boolean(value, self->page &&
cairo_surface_get_user_data(self->page, &fiv_io_key_frame_next));
break;
case PROP_HAS_PREVIOUS_PAGE:
g_value_set_boolean(value, self->image && self->page != self->image);
break;
case PROP_HAS_NEXT_PAGE:
g_value_set_boolean(value, self->page &&
cairo_surface_get_user_data(self->page, &fiv_io_key_page_next));
break;
case PROP_HADJUSTMENT:
g_value_set_object(value, self->hadjustment);
break;
case PROP_VADJUSTMENT:
g_value_set_object(value, self->vadjustment);
break;
case PROP_HSCROLL_POLICY:
g_value_set_enum(value, self->hscroll_policy);
break;
case PROP_VSCROLL_POLICY:
g_value_set_enum(value, self->vscroll_policy);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
fiv_view_set_property(
GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
FivView *self = FIV_VIEW(object);
switch (property_id) {
case PROP_SCALE_TO_FIT:
if (self->scale_to_fit != g_value_get_boolean(value))
fiv_view_command(self, FIV_VIEW_COMMAND_TOGGLE_SCALE_TO_FIT);
break;
case PROP_FIXATE:
if (self->fixate != g_value_get_boolean(value))
fiv_view_command(self, FIV_VIEW_COMMAND_TOGGLE_FIXATE);
break;
case PROP_ENABLE_CMS:
if (self->enable_cms != g_value_get_boolean(value))
fiv_view_command(self, FIV_VIEW_COMMAND_TOGGLE_CMS);
break;
case PROP_FILTER:
if (self->filter != g_value_get_boolean(value))
fiv_view_command(self, FIV_VIEW_COMMAND_TOGGLE_FILTER);
break;
case PROP_CHECKERBOARD:
if (self->checkerboard != g_value_get_boolean(value))
fiv_view_command(self, FIV_VIEW_COMMAND_TOGGLE_CHECKERBOARD);
break;
case PROP_ENHANCE:
if (self->enhance != g_value_get_boolean(value))
fiv_view_command(self, FIV_VIEW_COMMAND_TOGGLE_ENHANCE);
break;
case PROP_HADJUSTMENT:
if (replace_adjustment(
self, &self->hadjustment, g_value_get_object(value)))
g_object_notify_by_pspec(object, pspec);
break;
case PROP_VADJUSTMENT:
if (replace_adjustment(
self, &self->vadjustment, g_value_get_object(value)))
g_object_notify_by_pspec(object, pspec);
break;
case PROP_HSCROLL_POLICY:
if ((gint) self->hscroll_policy != g_value_get_enum(value)) {
self->hscroll_policy = g_value_get_enum(value);
gtk_widget_queue_resize(GTK_WIDGET(self));
g_object_notify_by_pspec(object, pspec);
}
break;
case PROP_VSCROLL_POLICY:
if ((gint) self->vscroll_policy != g_value_get_enum(value)) {
self->vscroll_policy = g_value_get_enum(value);
gtk_widget_queue_resize(GTK_WIDGET(self));
g_object_notify_by_pspec(object, pspec);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
fiv_view_get_preferred_height(GtkWidget *widget, gint *minimum, gint *natural)
{
FivView *self = FIV_VIEW(widget);
if (self->scale_to_fit) {
*minimum = 1;
*natural = MAX(*minimum, ceil(get_surface_dimensions(self).height));
} else {
int dw, dh;
get_display_dimensions(self, &dw, &dh);
*minimum = *natural = dh;
}
}
static void
fiv_view_get_preferred_width(GtkWidget *widget, gint *minimum, gint *natural)
{
FivView *self = FIV_VIEW(widget);
if (self->scale_to_fit) {
*minimum = 1;
*natural = MAX(*minimum, ceil(get_surface_dimensions(self).width));
} else {
int dw, dh;
get_display_dimensions(self, &dw, &dh);
*minimum = *natural = dw;
}
}
static void
prescale_page(FivView *self)
{
FivIoRenderClosure *closure = NULL;
if (!self->image || !(closure =
cairo_surface_get_user_data(self->page, &fiv_io_key_render)))
return;
// TODO(p): Restart the animation. No vector formats currently animate.
g_return_if_fail(!self->frame_update_connection);
// If it fails, the previous frame pointer may become invalid.
g_clear_pointer(&self->page_scaled, cairo_surface_destroy);
self->frame = self->page_scaled = closure->render(closure, self->scale);
if (!self->page_scaled)
self->frame = self->page;
}
static void
fiv_view_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
GTK_WIDGET_CLASS(fiv_view_parent_class)->size_allocate(widget, allocation);
FivView *self = FIV_VIEW(widget);
if (!self->image || !self->scale_to_fit)
goto out;
Dimensions surface_dimensions = get_surface_dimensions(self);
double scale = 1;
if (ceil(surface_dimensions.width * scale) > allocation->width)
scale = allocation->width / surface_dimensions.width;
if (ceil(surface_dimensions.height * scale) > allocation->height)
scale = allocation->height / surface_dimensions.height;
if (self->scale != scale) {
self->scale = scale;
g_object_notify_by_pspec(G_OBJECT(widget), view_properties[PROP_SCALE]);
prescale_page(self);
}
out:
update_adjustments(self);
}
// https://www.freedesktop.org/wiki/OpenIcc/ICC_Profiles_in_X_Specification_0.4
// has disappeared, but you can use the wayback machine.
//
// Note that Wayland does not have any appropriate protocol, as of writing:
// https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/14
static void
reload_screen_cms_profile(FivView *self, GdkWindow *window)
{
g_clear_pointer(&self->screen_cms_profile, fiv_io_profile_free);
GdkDisplay *display = gdk_window_get_display(window);
GdkMonitor *monitor = gdk_display_get_monitor_at_window(display, window);
int num = -1;
for (int i = gdk_display_get_n_monitors(display); num < 0 && i--; )
if (gdk_display_get_monitor(display, i) == monitor)
num = i;
if (num < 0)
goto out;
char atom[32] = "";
g_snprintf(atom, sizeof atom, "_ICC_PROFILE%c%d", num ? '_' : '\0', num);
// Sadly, there is no nice GTK+/GDK mechanism to watch this for changes.
int format = 0, length = 0;
GdkAtom type = GDK_NONE;
guchar *data = NULL;
GdkWindow *root = gdk_screen_get_root_window(gdk_window_get_screen(window));
if (gdk_property_get(root, gdk_atom_intern(atom, FALSE), GDK_NONE, 0,
8 << 20 /* MiB */, FALSE, &type, &format, &length, &data)) {
if (format == 8 && length > 0)
self->screen_cms_profile = fiv_io_profile_new(data, length);
g_free(data);
}
out:
if (!self->screen_cms_profile)
self->screen_cms_profile = fiv_io_profile_new_sRGB();
}
static void
fiv_view_realize(GtkWidget *widget)
{
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
GdkWindowAttr attributes = {
.window_type = GDK_WINDOW_CHILD,
.x = allocation.x,
.y = allocation.y,
.width = allocation.width,
.height = allocation.height,
// Input-only would presumably also work (as in GtkPathBar, e.g.),
// but it merely seems to involve more work.
.wclass = GDK_INPUT_OUTPUT,
// Assuming here that we can't ask for a higher-precision Visual
// than what we get automatically.
.visual = gtk_widget_get_visual(widget),
// Pointer motion/release enables GtkGestureDrag.
.event_mask = gtk_widget_get_events(widget) | GDK_KEY_PRESS_MASK |
GDK_SCROLL_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK,
};
// We need this window to receive input events at all.
GdkWindow *window = gdk_window_new(gtk_widget_get_parent_window(widget),
&attributes, GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL);
// Without the following call, or the rendering mode set to "recording",
// RGB30 degrades to RGB24, because gdk_window_begin_paint_internal()
// creates backing stores using cairo_content_t constants.
//
// It completely breaks the Quartz backend, so limit it to X11.
#ifdef GDK_WINDOWING_X11
// Note that this disables double buffering, and sometimes causes artefacts,
// see: https://gitlab.gnome.org/GNOME/gtk/-/issues/2560
//
// If GTK+'s OpenGL integration fails to deliver, we need to use the window
// directly, sidestepping the toolkit entirely.
if (GDK_IS_X11_WINDOW(window))
gdk_window_ensure_native(window);
#endif // GDK_WINDOWING_X11
gtk_widget_register_window(widget, window);
gtk_widget_set_window(widget, window);
gtk_widget_set_realized(widget, TRUE);
reload_screen_cms_profile(FIV_VIEW(widget), window);
}
static gboolean
fiv_view_draw(GtkWidget *widget, cairo_t *cr)
{
// Placed here due to our using a native GdkWindow on X11,
// which makes the widget have no double buffering or default background.
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
GtkStyleContext *style = gtk_widget_get_style_context(widget);
gtk_render_background(style, cr, 0, 0, allocation.width, allocation.height);
FivView *self = FIV_VIEW(widget);
if (!self->image ||
!gtk_cairo_should_draw_window(cr, gtk_widget_get_window(widget)))
return TRUE;
int w, h;
get_display_dimensions(self, &w, &h);
double x = 0;
double y = 0;
if (self->hadjustment)
x = -floor(gtk_adjustment_get_value(self->hadjustment));
if (self->vadjustment)
y = -floor(gtk_adjustment_get_value(self->vadjustment));
if (w < allocation.width)
x = round((allocation.width - w) / 2.);
if (h < allocation.height)
y = round((allocation.height - h) / 2.);
Dimensions surface_dimensions = {};
cairo_matrix_t matrix = fiv_io_orientation_apply(
self->page_scaled ? self->page_scaled : self->page, self->orientation,
&surface_dimensions.width, &surface_dimensions.height);
cairo_translate(cr, x, y);
if (self->checkerboard) {
gtk_style_context_save(style);
gtk_style_context_add_class(style, "checkerboard");
gtk_render_background(style, cr, 0, 0, w, h);
gtk_style_context_restore(style);
}
// Then all frames are pre-scaled.
if (self->page_scaled) {
cairo_set_source_surface(cr, self->frame, 0, 0);
cairo_pattern_set_matrix(cairo_get_source(cr), &matrix);
cairo_paint(cr);
return TRUE;
}
// FIXME: Recording surfaces do not work well with CAIRO_SURFACE_TYPE_XLIB,
// we always get a shitty pixmap, where transparency contains junk.
if (cairo_surface_get_type(self->frame) == CAIRO_SURFACE_TYPE_RECORDING) {
cairo_surface_t *image =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
cairo_t *tcr = cairo_create(image);
cairo_scale(tcr, self->scale, self->scale);
cairo_set_source_surface(tcr, self->frame, 0, 0);
cairo_pattern_set_matrix(cairo_get_source(tcr), &matrix);
cairo_paint(tcr);
cairo_destroy(tcr);
cairo_set_source_surface(cr, image, 0, 0);
cairo_paint(cr);
cairo_surface_destroy(image);
return TRUE;
}
// XXX: The rounding together with padding may result in up to
// a pixel's worth of made-up picture data.
cairo_rectangle(cr, 0, 0, w, h);
cairo_clip(cr);
cairo_scale(cr, self->scale, self->scale);
cairo_set_source_surface(cr, self->frame, 0, 0);
cairo_pattern_t *pattern = cairo_get_source(cr);
cairo_pattern_set_matrix(pattern, &matrix);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
// TODO(p): Prescale it ourselves to an off-screen bitmap, gamma-correctly.
if (self->filter)
cairo_pattern_set_filter(pattern, CAIRO_FILTER_GOOD);
else
cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
#ifdef GDK_WINDOWING_QUARTZ
// Not supported there. Acts a bit like repeating, but weirdly offset.
if (GDK_IS_QUARTZ_WINDOW(gtk_widget_get_window(widget)))
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_NONE);
#endif // GDK_WINDOWING_QUARTZ
cairo_paint(cr);
return TRUE;
}
static gboolean
fiv_view_button_press_event(GtkWidget *widget, GdkEventButton *event)
{
// XXX: Return value?
GTK_WIDGET_CLASS(fiv_view_parent_class)->button_press_event(widget, event);
if (event->button == GDK_BUTTON_PRIMARY &&
gtk_widget_get_focus_on_click(widget))
gtk_widget_grab_focus(widget);
return FALSE;
}
#define SCALE_STEP 1.25
static gboolean
set_scale_to_fit(FivView *self, bool scale_to_fit)
{
if (self->scale_to_fit != scale_to_fit) {
if ((self->scale_to_fit = scale_to_fit)) {
self->fixate = false;
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_FIXATE]);
}
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_SCALE_TO_FIT]);
gtk_widget_queue_resize(GTK_WIDGET(self));
}
return TRUE;
}
static gboolean
set_scale(FivView *self, double scale, const GdkEvent *event)
{
// FIXME: Zooming to exactly 1:1 breaks rendering with some images
// when using a native X11 Window. This is a silly workaround.
GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(self));
if (window && gdk_window_has_native(window) && scale == 1)
scale = 0.999999999999999;
if (self->scale == scale)
goto out;
GtkAllocation allocation;
gtk_widget_get_allocation(GTK_WIDGET(self), &allocation);
double focus_x = 0, focus_y = 0, surface_x = 0, surface_y = 0;
if (!event || !gdk_event_get_coords(event, &focus_x, &focus_y)) {
focus_x = 0.5 * allocation.width;
focus_y = 0.5 * allocation.height;
}
if (self->hadjustment && self->vadjustment) {
int w, h;
get_display_dimensions(self, &w, &h);
surface_x = (focus_x + (w < allocation.width
? -round((allocation.width - w) / 2.)
: +floor(gtk_adjustment_get_value(self->hadjustment))))
/ self->scale;
surface_y = (focus_y + (h < allocation.height
? -round((allocation.height - h) / 2.)
: +floor(gtk_adjustment_get_value(self->vadjustment))))
/ self->scale;
}
self->scale = scale;
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_SCALE]);
prescale_page(self);
if (self->hadjustment && self->vadjustment) {
update_adjustments(self);
Dimensions surface_dimensions = get_surface_dimensions(self);
if (surface_dimensions.width * self->scale > allocation.width)
gtk_adjustment_set_value(
self->hadjustment, surface_x * self->scale - focus_x);
if (surface_dimensions.height * self->scale > allocation.height)
gtk_adjustment_set_value(
self->vadjustment, surface_y * self->scale - focus_y);
}
gtk_widget_queue_resize(GTK_WIDGET(self));
out:
return set_scale_to_fit(self, false);
}
static void
set_scale_to_fit_width(FivView *self)
{
double w = get_surface_dimensions(self).width;
int allocated = gtk_widget_get_allocated_width(GTK_WIDGET(self));
if (ceil(w * self->scale) > allocated)
set_scale(self, allocated / w, NULL);
}
static void
set_scale_to_fit_height(FivView *self)
{
double h = get_surface_dimensions(self).height;
int allocated = gtk_widget_get_allocated_height(GTK_WIDGET(self));
if (ceil(h * self->scale) > allocated)
set_scale(self, allocated / h, NULL);
}
static gboolean
fiv_view_scroll_event(GtkWidget *widget, GdkEventScroll *event)
{
FivView *self = FIV_VIEW(widget);
if (!self->image)
return FALSE;
if (event->state & gtk_accelerator_get_default_mod_mask())
return FALSE;
switch (event->direction) {
case GDK_SCROLL_UP:
return set_scale(self, self->scale * SCALE_STEP, (GdkEvent *) event);
case GDK_SCROLL_DOWN:
return set_scale(self, self->scale / SCALE_STEP, (GdkEvent *) event);
default:
// For some reason, native GdkWindows may also get GDK_SCROLL_SMOOTH.
// Left/right are good to steal from GtkScrolledWindow for consistency.
return TRUE;
}
}
static void
stop_animating(FivView *self)
{
GdkFrameClock *clock = gtk_widget_get_frame_clock(GTK_WIDGET(self));
if (!clock || !self->frame_update_connection)
return;
g_signal_handler_disconnect(clock, self->frame_update_connection);
gdk_frame_clock_end_updating(clock);
self->frame_time = 0;
self->frame_update_connection = 0;
self->remaining_loops = 0;
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_PLAYING]);
}
static gboolean
advance_frame(FivView *self)
{
cairo_surface_t *next =
cairo_surface_get_user_data(self->frame, &fiv_io_key_frame_next);
if (next) {
self->frame = next;
} else {
if (self->remaining_loops && !--self->remaining_loops)
return FALSE;
self->frame = self->page;
}
return TRUE;
}
static gboolean
advance_animation(FivView *self, GdkFrameClock *clock)
{
gint64 now = gdk_frame_clock_get_frame_time(clock);
while (true) {
// TODO(p): See if infinite frames can actually happen, and how.
intptr_t duration = (intptr_t) cairo_surface_get_user_data(
self->frame, &fiv_io_key_frame_duration);
if (duration < 0)
return FALSE;
// Do not busy loop. GIF timings are given in hundredths of a second.
// Note that browsers seem to do [< 10] => 100:
// https://bugs.webkit.org/show_bug.cgi?id=36082
if (duration == 0)
duration = gdk_frame_timings_get_refresh_interval(
gdk_frame_clock_get_current_timings(clock)) / 1000;
if (duration == 0)
duration = 1;
gint64 then = self->frame_time + duration * 1000;
if (then > now)
return TRUE;
if (!advance_frame(self))
return FALSE;
self->frame_time = then;
gtk_widget_queue_draw(GTK_WIDGET(self));
}
}
static void
on_frame_clock_update(GdkFrameClock *clock, gpointer user_data)
{
FivView *self = FIV_VIEW(user_data);
if (!advance_animation(self, clock))
stop_animating(self);
}
static void
start_animating(FivView *self)
{
stop_animating(self);
GdkFrameClock *clock = gtk_widget_get_frame_clock(GTK_WIDGET(self));
if (!clock || !self->image ||
!cairo_surface_get_user_data(self->page, &fiv_io_key_frame_next))
return;
self->frame_time = gdk_frame_clock_get_frame_time(clock);
self->frame_update_connection = g_signal_connect(
clock, "update", G_CALLBACK(on_frame_clock_update), self);
self->remaining_loops =
(uintptr_t) cairo_surface_get_user_data(self->page, &fiv_io_key_loops);
gdk_frame_clock_begin_updating(clock);
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_PLAYING]);
}
static void
switch_page(FivView *self, cairo_surface_t *page)
{
g_clear_pointer(&self->page_scaled, cairo_surface_destroy);
self->frame = self->page = page;
prescale_page(self);
if (!self->page ||
(self->orientation = (uintptr_t) cairo_surface_get_user_data(
self->page, &fiv_io_key_orientation)) == FivIoOrientationUnknown)
self->orientation = FivIoOrientation0;
start_animating(self);
gtk_widget_queue_resize(GTK_WIDGET(self));
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_CAN_ANIMATE]);
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_HAS_PREVIOUS_PAGE]);
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_HAS_NEXT_PAGE]);
}
static void
fiv_view_map(GtkWidget *widget)
{
GTK_WIDGET_CLASS(fiv_view_parent_class)->map(widget);
// Loading before mapping will fail to obtain a GdkFrameClock.
start_animating(FIV_VIEW(widget));
}
void
fiv_view_unmap(GtkWidget *widget)
{
stop_animating(FIV_VIEW(widget));
GTK_WIDGET_CLASS(fiv_view_parent_class)->unmap(widget);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
on_drag_begin(GtkGestureDrag *drag, G_GNUC_UNUSED gdouble start_x,
G_GNUC_UNUSED gdouble start_y, gpointer user_data)
{
GtkGesture *gesture = GTK_GESTURE(drag);
GdkEventSequence *sequence = gtk_gesture_get_last_updated_sequence(gesture);
GdkModifierType state = 0;
gdk_event_get_state(gtk_gesture_get_last_event(gesture, sequence), &state);
if (state & gtk_accelerator_get_default_mod_mask()) {
gtk_gesture_set_state(gesture, GTK_EVENT_SEQUENCE_DENIED);
return;
}
// Since we set this up as a pointer-only gesture, there is only the NULL
// sequence, so gtk_gesture_set_sequence_state() is completely unneeded.
gtk_gesture_set_state(gesture, GTK_EVENT_SEQUENCE_CLAIMED);
GdkWindow *window = gtk_widget_get_window(
gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(drag)));
GdkCursor *cursor =
gdk_cursor_new_from_name(gdk_window_get_display(window), "grabbing");
gdk_window_set_cursor(window, cursor);
g_object_unref(cursor);
FivView *self = FIV_VIEW(user_data);
self->drag_start[0] = self->hadjustment ?
gtk_adjustment_get_value(self->hadjustment) : 0;
self->drag_start[1] = self->vadjustment ?
gtk_adjustment_get_value(self->vadjustment) : 0;
}
static void
on_drag_update(G_GNUC_UNUSED GtkGestureDrag *drag, gdouble offset_x,
gdouble offset_y, gpointer user_data)
{
FivView *self = FIV_VIEW(user_data);
if (self->hadjustment) {
gtk_adjustment_set_value(
self->hadjustment, self->drag_start[0] - offset_x);
}
if (self->vadjustment) {
gtk_adjustment_set_value(
self->vadjustment, self->drag_start[1] - offset_y);
}
}
static void
on_drag_end(GtkGestureDrag *drag, G_GNUC_UNUSED gdouble start_x,
G_GNUC_UNUSED gdouble start_y, G_GNUC_UNUSED gpointer user_data)
{
GdkWindow *window = gtk_widget_get_window(
gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(drag)));
gdk_window_set_cursor(window, NULL);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
show_error_dialog(GtkWindow *parent, GError *error)
{
GtkWidget *dialog = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_error_free(error);
}
static GtkWindow *
get_toplevel(GtkWidget *widget)
{
if (GTK_IS_WINDOW((widget = gtk_widget_get_toplevel(widget))))
return GTK_WINDOW(widget);
return NULL;
}
static void
on_draw_page(G_GNUC_UNUSED GtkPrintOperation *operation,
GtkPrintContext *context, G_GNUC_UNUSED int page_nr, FivView *self)
{
// Any DPI will be wrong, unless we import that information from the image.
double scale = 1 / 96.;
Dimensions surface_dimensions = {};
cairo_matrix_t matrix =
fiv_io_orientation_apply(self->page, self->orientation,
&surface_dimensions.width, &surface_dimensions.height);
double w = surface_dimensions.width * scale;
double h = surface_dimensions.height * scale;
// Scale down to fit the print area, taking care to not divide by zero.
double areaw = gtk_print_context_get_width(context);
double areah = gtk_print_context_get_height(context);
scale *= fmin((areaw < w) ? areaw / w : 1, (areah < h) ? areah / h : 1);
cairo_t *cr = gtk_print_context_get_cairo_context(context);
cairo_scale(cr, scale, scale);
cairo_set_source_surface(cr, self->frame, 0, 0);
cairo_pattern_set_matrix(cairo_get_source(cr), &matrix);
cairo_paint(cr);
}
static void
print(FivView *self)
{
GtkPrintOperation *print = gtk_print_operation_new();
gtk_print_operation_set_n_pages(print, 1);
gtk_print_operation_set_embed_page_setup(print, TRUE);
gtk_print_operation_set_unit(print, GTK_UNIT_INCH);
gtk_print_operation_set_job_name(print, "Image");
g_signal_connect(print, "draw-page", G_CALLBACK(on_draw_page), self);
static GtkPrintSettings *settings = NULL;
if (settings != NULL)
gtk_print_operation_set_print_settings(print, settings);
GError *error = NULL;
GtkWindow *window = get_toplevel(GTK_WIDGET(self));
GtkPrintOperationResult res = gtk_print_operation_run(
print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, window, &error);
if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
if (settings != NULL)
g_object_unref(settings);
settings = g_object_ref(gtk_print_operation_get_print_settings(print));
}
if (error)
show_error_dialog(window, error);
g_object_unref(print);
}
static gboolean
save_as(FivView *self, cairo_surface_t *frame)
{
GtkWindow *window = get_toplevel(GTK_WIDGET(self));
FivIoProfile target = NULL;
if (self->enable_cms && (target = self->screen_cms_profile)) {
GtkWidget *dialog = gtk_message_dialog_new(window, GTK_DIALOG_MODAL,
GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, "%s",
"Color management overrides attached color profiles.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
GtkWidget *dialog = gtk_file_chooser_dialog_new(
frame ? "Save frame as" : "Save page as",
window, GTK_FILE_CHOOSER_ACTION_SAVE,
"_Cancel", GTK_RESPONSE_CANCEL, "_Save", GTK_RESPONSE_ACCEPT, NULL);
GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);
GFile *file = g_file_new_for_uri(self->uri);
const gchar *path = g_file_peek_path(file);
// TODO(p): Use g_file_info_get_display_name().
gchar *basename = g_filename_display_basename(path ? path : self->uri);
// Note that GTK+'s save dialog is too stupid to automatically change
// the extension when user changes the filter. Presumably,
// gtk_file_chooser_set_extra_widget() can be used to circumvent this.
gchar *name =
g_strdup_printf(frame ? "%s.frame.webp" : "%s.webp", basename);
gtk_file_chooser_set_current_name(chooser, name);
g_free(name);
if (path) {
gchar *dirname = g_path_get_dirname(path);
gtk_file_chooser_set_current_folder(chooser, dirname);
g_free(dirname);
}
g_free(basename);
g_object_unref(file);
// This is the best general format: supports lossless encoding, animations,
// alpha channel, and Exif and ICC profile metadata.
// PNG is another viable option, but sPNG can't do APNG, Wuffs can't save,
// and libpng is a pain in the arse.
GtkFileFilter *webp_filter = gtk_file_filter_new();
gtk_file_filter_add_mime_type(webp_filter, "image/webp");
gtk_file_filter_add_pattern(webp_filter, "*.webp");
gtk_file_filter_set_name(webp_filter, "Lossless WebP (*.webp)");
gtk_file_chooser_add_filter(chooser, webp_filter);
// The format is supported by Exiv2 and ExifTool.
// This is mostly a developer tool.
GtkFileFilter *exv_filter = gtk_file_filter_new();
gtk_file_filter_add_mime_type(exv_filter, "image/x-exv");
gtk_file_filter_add_pattern(exv_filter, "*.exv");
gtk_file_filter_set_name(exv_filter, "Exiv2 metadata (*.exv)");
gtk_file_chooser_add_filter(chooser, exv_filter);
GError *error = NULL;
switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
gchar *path;
case GTK_RESPONSE_ACCEPT:
path = gtk_file_chooser_get_filename(chooser);
if (!(gtk_file_chooser_get_filter(chooser) == webp_filter
? fiv_io_save(self->page, frame, target, path, &error)
: fiv_io_save_metadata(self->page, path, &error)))
show_error_dialog(window, error);
g_free(path);
// Fall-through.
default:
gtk_widget_destroy(dialog);
// Fall-through.
case GTK_RESPONSE_NONE:
return TRUE;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static GtkWidget *
info_start_group(GtkWidget *vbox, const char *group)
{
GtkWidget *label = gtk_label_new(group);
gtk_widget_set_hexpand(label, TRUE);
gtk_widget_set_halign(label, GTK_ALIGN_FILL);
PangoAttrList *attrs = pango_attr_list_new();
pango_attr_list_insert(attrs, pango_attr_weight_new(PANGO_WEIGHT_BOLD));
gtk_label_set_attributes(GTK_LABEL(label), attrs);
pango_attr_list_unref(attrs);
GtkWidget *grid = gtk_grid_new();
GtkWidget *expander = gtk_expander_new(NULL);
gtk_expander_set_label_widget(GTK_EXPANDER(expander), label);
gtk_expander_set_expanded(GTK_EXPANDER(expander), TRUE);
gtk_container_add(GTK_CONTAINER(expander), grid);
gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
gtk_box_pack_start(GTK_BOX(vbox), expander, FALSE, FALSE, 0);
return grid;
}
static GtkWidget *
info_parse(char *tsv)
{
GtkSizeGroup *sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
const char *last_group = NULL;
GtkWidget *grid = NULL;
int line = 1, row = 0;
for (char *nl; (nl = strchr(tsv, '\n')); line++, tsv = ++nl) {
*nl = 0;
if (nl > tsv && nl[-1] == '\r')
nl[-1] = 0;
char *group = tsv, *tag = strchr(group, '\t');
if (!tag) {
g_warning("ExifTool parse error on line %d", line);
continue;
}
*tag++ = 0;
for (char *p = group; *p; p++)
if (*p == '_')
*p = ' ';
char *value = strchr(tag, '\t');
if (!value) {
g_warning("ExifTool parse error on line %d", line);
continue;
}
*value++ = 0;
if (!last_group || strcmp(last_group, group)) {
grid = info_start_group(vbox, (last_group = group));
row = 0;
}
GtkWidget *a = gtk_label_new(tag);
gtk_size_group_add_widget(sg, a);
gtk_label_set_selectable(GTK_LABEL(a), TRUE);
gtk_label_set_xalign(GTK_LABEL(a), 0.);
gtk_grid_attach(GTK_GRID(grid), a, 0, row, 1, 1);
GtkWidget *b = gtk_label_new(value);
gtk_label_set_selectable(GTK_LABEL(b), TRUE);
gtk_label_set_xalign(GTK_LABEL(b), 0.);
gtk_label_set_line_wrap(GTK_LABEL(b), TRUE);
gtk_widget_set_hexpand(b, TRUE);
gtk_grid_attach(GTK_GRID(grid), b, 1, row, 1, 1);
row++;
}
g_object_unref(sg);
return vbox;
}
static void
info(FivView *self)
{
// TODO(p): Add a fallback to internal capabilities.
// The simplest is to specify the filename and the resolution.
GtkWindow *window = get_toplevel(GTK_WIDGET(self));
int flags = G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE;
GError *error = NULL;
gchar *path = g_filename_from_uri(self->uri, NULL, &error);
if (!path) {
show_error_dialog(window, error);
return;
}
GSubprocess *subprocess = g_subprocess_new(flags, &error, "exiftool",
"-tab", "-groupNames", "-duplicates", "-extractEmbedded", "--binary",
"-quiet", "--", path, NULL);
g_free(path);
if (error) {
show_error_dialog(window, error);
return;
}
gchar *out = NULL, *err = NULL;
if (!g_subprocess_communicate_utf8(
subprocess, NULL, NULL, &out, &err, &error)) {
show_error_dialog(window, error);
return;
}
GtkWidget *dialog = gtk_widget_new(GTK_TYPE_DIALOG,
"use-header-bar", TRUE,
"title", "Information",
"transient-for", window,
"destroy-with-parent", TRUE, NULL);
GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
if (*err) {
GtkWidget *info = gtk_info_bar_new();
GtkInfoBar *info_bar = GTK_INFO_BAR(info);
gtk_info_bar_set_message_type(info_bar, GTK_MESSAGE_WARNING);
GtkWidget *info_area = gtk_info_bar_get_content_area(info_bar);
GtkWidget *label = gtk_label_new(g_strstrip(err));
gtk_container_add(GTK_CONTAINER(info_area), label);
gtk_container_add(GTK_CONTAINER(content_area), info);
}
GtkWidget *scroller = gtk_scrolled_window_new(NULL, NULL);
GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW(scroller);
gtk_scrolled_window_set_max_content_width(sw, 600);
gtk_scrolled_window_set_max_content_height(sw, 800);
gtk_scrolled_window_set_propagate_natural_width(sw, TRUE);
gtk_scrolled_window_set_propagate_natural_height(sw, TRUE);
GtkWidget *info = info_parse(out);
gtk_widget_set_hexpand(info, TRUE);
gtk_widget_set_vexpand(info, TRUE);
gtk_style_context_add_class(
gtk_widget_get_style_context(GTK_WIDGET(info)), "fiv-information");
gtk_container_add(GTK_CONTAINER(scroller), info);
gtk_container_add(GTK_CONTAINER(content_area), scroller);
g_free(out);
g_free(err);
g_object_unref(subprocess);
gtk_widget_show_all(dialog);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static gboolean
fiv_view_key_press_event(GtkWidget *widget, GdkEventKey *event)
{
FivView *self = FIV_VIEW(widget);
// So far, our commands cannot accept arguments, so these few are hardcoded.
if (self->image &&
!(event->state & gtk_accelerator_get_default_mod_mask()) &&
event->keyval >= GDK_KEY_1 && event->keyval <= GDK_KEY_9)
return set_scale(self, event->keyval - GDK_KEY_0, NULL);
return GTK_WIDGET_CLASS(fiv_view_parent_class)
->key_press_event(widget, event);
}
static void
bind(GtkBindingSet *bs, guint keyval, GdkModifierType modifiers,
FivViewCommand command)
{
gtk_binding_entry_add_signal(
bs, keyval, modifiers, "command", 1, FIV_TYPE_VIEW_COMMAND, command);
}
static void
fiv_view_class_init(FivViewClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = fiv_view_finalize;
object_class->get_property = fiv_view_get_property;
object_class->set_property = fiv_view_set_property;
view_properties[PROP_MESSAGES] = g_param_spec_string(
"messages", "Messages", "Informative messages from the last image load",
NULL, G_PARAM_READABLE);
view_properties[PROP_SCALE] = g_param_spec_double(
"scale", "Scale", "Zoom level",
0, G_MAXDOUBLE, 1.0, G_PARAM_READABLE);
view_properties[PROP_SCALE_TO_FIT] = g_param_spec_boolean(
"scale-to-fit", "Scale to fit", "Scale images down to fit the window",
TRUE, G_PARAM_READWRITE);
view_properties[PROP_FIXATE] = g_param_spec_boolean(
"fixate", "Fixate", "Keep zoom and position",
FALSE, G_PARAM_READWRITE);
view_properties[PROP_ENABLE_CMS] = g_param_spec_boolean(
"enable-cms", "Enable CMS", "Enable color management",
TRUE, G_PARAM_READWRITE);
view_properties[PROP_FILTER] = g_param_spec_boolean(
"filter", "Use filtering", "Scale images smoothly",
TRUE, G_PARAM_READWRITE);
view_properties[PROP_CHECKERBOARD] = g_param_spec_boolean(
"checkerboard", "Show checkerboard", "Highlight transparent background",
FALSE, G_PARAM_READWRITE);
view_properties[PROP_ENHANCE] = g_param_spec_boolean(
"enhance", "Enhance JPEG", "Enhance low-quality JPEG",
FALSE, G_PARAM_READWRITE);
view_properties[PROP_PLAYING] = g_param_spec_boolean(
"playing", "Playing animation", "An animation is running",
FALSE, G_PARAM_READABLE);
view_properties[PROP_HAS_IMAGE] = g_param_spec_boolean(
"has-image", "Has an image", "An image is loaded",
FALSE, G_PARAM_READABLE);
view_properties[PROP_CAN_ANIMATE] = g_param_spec_boolean(
"can-animate", "Can animate", "An animation is loaded",
FALSE, G_PARAM_READABLE);
view_properties[PROP_HAS_PREVIOUS_PAGE] = g_param_spec_boolean(
"has-previous-page", "Has a previous page", "Preceding pages exist",
FALSE, G_PARAM_READABLE);
view_properties[PROP_HAS_NEXT_PAGE] = g_param_spec_boolean(
"has-next-page", "Has a next page", "Following pages exist",
FALSE, G_PARAM_READABLE);
g_object_class_install_properties(
object_class, N_PROPERTIES, view_properties);
g_object_class_override_property(
object_class, PROP_HADJUSTMENT, "hadjustment");
g_object_class_override_property(
object_class, PROP_VADJUSTMENT, "vadjustment");
g_object_class_override_property(
object_class, PROP_HSCROLL_POLICY, "hscroll-policy");
g_object_class_override_property(
object_class, PROP_VSCROLL_POLICY, "vscroll-policy");
view_signals[COMMAND] =
g_signal_new_class_handler("command", G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_CALLBACK(fiv_view_command),
NULL, NULL, NULL, G_TYPE_NONE, 1, FIV_TYPE_VIEW_COMMAND);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
widget_class->get_preferred_height = fiv_view_get_preferred_height;
widget_class->get_preferred_width = fiv_view_get_preferred_width;
widget_class->size_allocate = fiv_view_size_allocate;
widget_class->map = fiv_view_map;
widget_class->unmap = fiv_view_unmap;
widget_class->realize = fiv_view_realize;
widget_class->draw = fiv_view_draw;
widget_class->button_press_event = fiv_view_button_press_event;
widget_class->scroll_event = fiv_view_scroll_event;
widget_class->key_press_event = fiv_view_key_press_event;
GtkBindingSet *bs = gtk_binding_set_by_class(klass);
// First, the standard, intuitive bindings.
bind(bs, GDK_KEY_0, GDK_CONTROL_MASK, FIV_VIEW_COMMAND_ZOOM_1);
bind(bs, GDK_KEY_plus, GDK_CONTROL_MASK, FIV_VIEW_COMMAND_ZOOM_IN);
bind(bs, GDK_KEY_minus, GDK_CONTROL_MASK, FIV_VIEW_COMMAND_ZOOM_OUT);
bind(bs, GDK_KEY_p, GDK_CONTROL_MASK, FIV_VIEW_COMMAND_PRINT);
bind(bs, GDK_KEY_r, GDK_CONTROL_MASK, FIV_VIEW_COMMAND_RELOAD);
bind(bs, GDK_KEY_s, GDK_CONTROL_MASK, FIV_VIEW_COMMAND_SAVE_PAGE);
bind(bs, GDK_KEY_s, GDK_MOD1_MASK, FIV_VIEW_COMMAND_SAVE_FRAME);
bind(bs, GDK_KEY_Return, GDK_MOD1_MASK, FIV_VIEW_COMMAND_INFO);
// The scale-to-fit binding is from gThumb, which has more such modes.
bind(bs, GDK_KEY_F5, 0, FIV_VIEW_COMMAND_RELOAD);
bind(bs, GDK_KEY_r, 0, FIV_VIEW_COMMAND_RELOAD);
bind(bs, GDK_KEY_plus, 0, FIV_VIEW_COMMAND_ZOOM_IN);
bind(bs, GDK_KEY_minus, 0, FIV_VIEW_COMMAND_ZOOM_OUT);
bind(bs, GDK_KEY_w, 0, FIV_VIEW_COMMAND_FIT_WIDTH);
bind(bs, GDK_KEY_h, 0, FIV_VIEW_COMMAND_FIT_HEIGHT);
bind(bs, GDK_KEY_k, 0, FIV_VIEW_COMMAND_TOGGLE_FIXATE);
bind(bs, GDK_KEY_x, 0, FIV_VIEW_COMMAND_TOGGLE_SCALE_TO_FIT);
bind(bs, GDK_KEY_c, 0, FIV_VIEW_COMMAND_TOGGLE_CMS);
bind(bs, GDK_KEY_i, 0, FIV_VIEW_COMMAND_TOGGLE_FILTER);
bind(bs, GDK_KEY_t, 0, FIV_VIEW_COMMAND_TOGGLE_CHECKERBOARD);
bind(bs, GDK_KEY_e, 0, FIV_VIEW_COMMAND_TOGGLE_ENHANCE);
bind(bs, GDK_KEY_less, 0, FIV_VIEW_COMMAND_ROTATE_LEFT);
bind(bs, GDK_KEY_equal, 0, FIV_VIEW_COMMAND_MIRROR);
bind(bs, GDK_KEY_greater, 0, FIV_VIEW_COMMAND_ROTATE_RIGHT);
bind(bs, GDK_KEY_bracketleft, 0, FIV_VIEW_COMMAND_PAGE_PREVIOUS);
bind(bs, GDK_KEY_bracketright, 0, FIV_VIEW_COMMAND_PAGE_NEXT);
bind(bs, GDK_KEY_braceleft, 0, FIV_VIEW_COMMAND_FRAME_PREVIOUS);
bind(bs, GDK_KEY_braceright, 0, FIV_VIEW_COMMAND_FRAME_NEXT);
bind(bs, GDK_KEY_space, 0, FIV_VIEW_COMMAND_TOGGLE_PLAYBACK);
// TODO(p): Later override "screen_changed", recreate Pango layouts there,
// if we get to have any, or otherwise reflect DPI changes.
gtk_widget_class_set_css_name(widget_class, "fiv-view");
}
static void
fiv_view_init(FivView *self)
{
gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
self->enable_cms = true;
self->filter = true;
self->checkerboard = false;
self->scale = 1.0;
GtkGesture *drag = gtk_gesture_drag_new(GTK_WIDGET(self));
g_object_set_data_full(
G_OBJECT(self), "fiv-view-drag-gesture", drag, g_object_unref);
gtk_event_controller_set_propagation_phase(
GTK_EVENT_CONTROLLER(drag), GTK_PHASE_BUBBLE);
// GtkScrolledWindow's internal GtkGestureDrag is set to only look for
// touch events (and its "event_controllers" are perfectly private,
// so we can't change this), hopefully this is mutually exclusive with that.
// Though note that the GdkWindow doesn't register for touch events now.
gtk_gesture_single_set_exclusive(GTK_GESTURE_SINGLE(drag), TRUE);
g_signal_connect(drag, "drag-begin",
G_CALLBACK(on_drag_begin), self);
g_signal_connect(drag, "drag-update",
G_CALLBACK(on_drag_update), self);
g_signal_connect(drag, "drag-end",
G_CALLBACK(on_drag_end), self);
}
// --- Public interface --------------------------------------------------------
static cairo_surface_t *
open_without_swapping_in(FivView *self, const char *uri)
{
FivIoOpenContext ctx = {
.uri = uri,
.screen_profile = self->enable_cms ? self->screen_cms_profile : NULL,
.screen_dpi = 96, // TODO(p): Try to retrieve it from the screen.
.enhance = self->enhance,
.warnings = g_ptr_array_new_with_free_func(g_free),
};
GError *error = NULL;
cairo_surface_t *surface = fiv_io_open(&ctx, &error);
if (error) {
g_ptr_array_add(ctx.warnings, g_strdup(error->message));
g_error_free(error);
}
g_clear_pointer(&self->messages, g_free);
if (ctx.warnings->len) {
g_ptr_array_add(ctx.warnings, NULL);
self->messages = g_strjoinv("\n", (gchar **) ctx.warnings->pdata);
}
g_ptr_array_free(ctx.warnings, TRUE);
return surface;
}
// TODO(p): Progressive picture loading, or at least async/cancellable.
gboolean
fiv_view_set_uri(FivView *self, const char *uri)
{
// This is extremely expensive, and only works sometimes.
g_clear_pointer(&self->enhance_swap, cairo_surface_destroy);
if (self->enhance) {
self->enhance = FALSE;
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_ENHANCE]);
}
cairo_surface_t *surface = open_without_swapping_in(self, uri);
g_clear_pointer(&self->image, cairo_surface_destroy);
self->frame = self->page = NULL;
self->image = surface;
switch_page(self, self->image);
// Otherwise, adjustment values and zoom are retained implicitly.
if (!self->fixate)
set_scale_to_fit(self, true);
g_free(self->uri);
self->uri = g_strdup(uri);
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_MESSAGES]);
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_HAS_IMAGE]);
return surface != NULL;
}
static void
page_step(FivView *self, int step)
{
cairo_user_data_key_t *key =
step < 0 ? &fiv_io_key_page_previous : &fiv_io_key_page_next;
cairo_surface_t *page = cairo_surface_get_user_data(self->page, key);
if (page)
switch_page(self, page);
}
static void
frame_step(FivView *self, int step)
{
stop_animating(self);
cairo_user_data_key_t *key =
step < 0 ? &fiv_io_key_frame_previous : &fiv_io_key_frame_next;
if (!step || !(self->frame = cairo_surface_get_user_data(self->frame, key)))
self->frame = self->page;
gtk_widget_queue_draw(GTK_WIDGET(self));
}
static gboolean
reload(FivView *self)
{
cairo_surface_t *surface = open_without_swapping_in(self, self->uri);
g_object_notify_by_pspec(G_OBJECT(self), view_properties[PROP_MESSAGES]);
if (!surface)
return FALSE;
g_clear_pointer(&self->image, cairo_surface_destroy);
g_clear_pointer(&self->enhance_swap, cairo_surface_destroy);
switch_page(self, (self->image = surface));
return TRUE;
}
static void
swap_enhanced_image(FivView *self)
{
cairo_surface_t *saved = self->image;
self->image = self->page = self->frame = NULL;
if (self->enhance_swap) {
switch_page(self, (self->image = self->enhance_swap));
self->enhance_swap = saved;
} else if (reload(self)) {
self->enhance_swap = saved;
} else {
switch_page(self, (self->image = saved));
}
}
void
fiv_view_command(FivView *self, FivViewCommand command)
{
g_return_if_fail(FIV_IS_VIEW(self));
GtkWidget *widget = GTK_WIDGET(self);
if (!self->image)
return;
switch (command) {
break; case FIV_VIEW_COMMAND_RELOAD:
reload(self);
break; case FIV_VIEW_COMMAND_ROTATE_LEFT:
self->orientation = view_left[self->orientation];
gtk_widget_queue_resize(widget);
break; case FIV_VIEW_COMMAND_MIRROR:
self->orientation = view_mirror[self->orientation];
gtk_widget_queue_resize(widget);
break; case FIV_VIEW_COMMAND_ROTATE_RIGHT:
self->orientation = view_right[self->orientation];
gtk_widget_queue_resize(widget);
break; case FIV_VIEW_COMMAND_PAGE_FIRST:
switch_page(self, self->image);
break; case FIV_VIEW_COMMAND_PAGE_PREVIOUS:
page_step(self, -1);
break; case FIV_VIEW_COMMAND_PAGE_NEXT:
page_step(self, +1);
break; case FIV_VIEW_COMMAND_PAGE_LAST:
for (cairo_surface_t *s = self->page;
(s = cairo_surface_get_user_data(s, &fiv_io_key_page_next)); )
self->page = s;
switch_page(self, self->page);
break; case FIV_VIEW_COMMAND_FRAME_FIRST:
frame_step(self, 0);
break; case FIV_VIEW_COMMAND_FRAME_PREVIOUS:
frame_step(self, -1);
break; case FIV_VIEW_COMMAND_FRAME_NEXT:
frame_step(self, +1);
break; case FIV_VIEW_COMMAND_TOGGLE_PLAYBACK:
self->frame_update_connection
? stop_animating(self)
: start_animating(self);
break; case FIV_VIEW_COMMAND_TOGGLE_CMS:
self->enable_cms = !self->enable_cms;
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_ENABLE_CMS]);
reload(self);
break; case FIV_VIEW_COMMAND_TOGGLE_FILTER:
self->filter = !self->filter;
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_FILTER]);
gtk_widget_queue_draw(widget);
break; case FIV_VIEW_COMMAND_TOGGLE_CHECKERBOARD:
self->checkerboard = !self->checkerboard;
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_CHECKERBOARD]);
gtk_widget_queue_draw(widget);
break; case FIV_VIEW_COMMAND_TOGGLE_ENHANCE:
self->enhance = !self->enhance;
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_ENHANCE]);
swap_enhanced_image(self);
break; case FIV_VIEW_COMMAND_PRINT:
print(self);
break; case FIV_VIEW_COMMAND_SAVE_PAGE:
save_as(self, NULL);
break; case FIV_VIEW_COMMAND_SAVE_FRAME:
save_as(self, self->frame);
break; case FIV_VIEW_COMMAND_INFO:
info(self);
break; case FIV_VIEW_COMMAND_ZOOM_IN:
set_scale(self, self->scale * SCALE_STEP, NULL);
break; case FIV_VIEW_COMMAND_ZOOM_OUT:
set_scale(self, self->scale / SCALE_STEP, NULL);
break; case FIV_VIEW_COMMAND_ZOOM_1:
set_scale(self, 1.0, NULL);
break; case FIV_VIEW_COMMAND_FIT_WIDTH:
set_scale_to_fit_width(self);
break; case FIV_VIEW_COMMAND_FIT_HEIGHT:
set_scale_to_fit_height(self);
break; case FIV_VIEW_COMMAND_TOGGLE_SCALE_TO_FIT:
set_scale_to_fit(self, !self->scale_to_fit);
break; case FIV_VIEW_COMMAND_TOGGLE_FIXATE:
if ((self->fixate = !self->fixate))
set_scale_to_fit(self, false);
g_object_notify_by_pspec(
G_OBJECT(self), view_properties[PROP_FIXATE]);
}
}
|