-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy paththumbtable.c
3353 lines (3022 loc) · 111 KB
/
thumbtable.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
/*
This file is part of darktable,
Copyright (C) 2024 darktable developers.
darktable 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.
darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
*/
/** a class to manage a table of thumbnail for lighttable and filmstrip. */
#include "dtgtk/thumbtable.h"
#include "common/darktable.h"
#include "common/collection.h"
#include "common/colorlabels.h"
#include "common/debug.h"
#include "common/history.h"
#include "common/image_cache.h"
#include "common/ratings.h"
#include "common/selection.h"
#include "common/undo.h"
#include "control/control.h"
#include "gui/accelerators.h"
#include "gui/drag_and_drop.h"
#include "views/view.h"
#include "bauhaus/bauhaus.h"
#ifdef GDK_WINDOWING_QUARTZ
#include "osx/osx.h"
#endif
static void _list_remove_thumb(gpointer user_data)
{
dt_thumbnail_t *thumb = (dt_thumbnail_t *)user_data;
gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(thumb->w_main)), thumb->w_main);
dt_thumbnail_destroy(thumb);
}
// get the class name associated with the overlays mode
static gchar *_thumbs_get_overlays_class(dt_thumbnail_overlay_t over)
{
switch(over)
{
case DT_THUMBNAIL_OVERLAYS_NONE:
return g_strdup("dt_overlays_none");
case DT_THUMBNAIL_OVERLAYS_HOVER_EXTENDED:
return g_strdup("dt_overlays_hover_extended");
case DT_THUMBNAIL_OVERLAYS_ALWAYS_NORMAL:
return g_strdup("dt_overlays_always");
case DT_THUMBNAIL_OVERLAYS_ALWAYS_EXTENDED:
return g_strdup("dt_overlays_always_extended");
case DT_THUMBNAIL_OVERLAYS_MIXED:
return g_strdup("dt_overlays_mixed");
case DT_THUMBNAIL_OVERLAYS_HOVER_BLOCK:
return g_strdup("dt_overlays_hover_block");
default:
return g_strdup("dt_overlays_hover");
}
}
// get the size category, depending on the thumb size
static int _thumbs_get_prefs_size(dt_thumbtable_t *table)
{
// we get the size delimitations to differentiate sizes categories
// one we set as many categories as we want (this can be useful if
// we want to finetune css very precisely)
const char *txt = dt_conf_get_string_const("plugins/lighttable/thumbnail_sizes");
gchar **ts = g_strsplit(txt, "|", -1);
int i = 0;
while(ts[i])
{
const int s = g_ascii_strtoll(ts[i], NULL, 10);
if(table->thumb_size < s)
break;
i++;
}
g_strfreev(ts);
return i;
}
// update thumbtable class and overlays mode, depending on size category
static void _thumbs_update_overlays_mode(dt_thumbtable_t *table)
{
int ns = _thumbs_get_prefs_size(table);
// we change the class that indicate the thumb size
gchar *c0 = g_strdup_printf("dt_thumbnails_%d", table->prefs_size);
gchar *c1 = g_strdup_printf("dt_thumbnails_%d", ns);
dt_gui_remove_class(table->widget, c0);
dt_gui_add_class(table->widget, c1);
g_free(c0);
g_free(c1);
table->prefs_size = ns;
// we change the overlay mode
gchar *txt = g_strdup_printf("plugins/lighttable/overlays/%d/%d", table->mode, ns);
dt_thumbnail_overlay_t over = dt_conf_get_int(txt);
g_free(txt);
txt = g_strdup_printf("plugins/lighttable/tooltips/%d/%d", table->mode, ns);
table->show_tooltips = dt_conf_get_bool(txt);
g_free(txt);
dt_thumbtable_set_overlays_mode(table, over);
}
// change the type of overlays that should be shown
void dt_thumbtable_set_overlays_mode(dt_thumbtable_t *table,
const dt_thumbnail_overlay_t over)
{
if(!table)
return;
// we ensure the tooltips change in any cases
gchar *txt = g_strdup_printf("plugins/lighttable/tooltips/%d/%d",
table->mode, table->prefs_size);
dt_conf_set_bool(txt, table->show_tooltips);
g_free(txt);
int timeout = 2;
if(over != table->overlays)
{
// if the overlay change
txt = g_strdup_printf("plugins/lighttable/overlays/%d/%d",
table->mode, table->prefs_size);
dt_conf_set_int(txt, over);
g_free(txt);
gchar *cl0 = _thumbs_get_overlays_class(table->overlays);
gchar *cl1 = _thumbs_get_overlays_class(over);
dt_gui_remove_class(table->widget, cl0);
dt_gui_add_class(table->widget, cl1);
g_free(cl0);
g_free(cl1);
txt = g_strdup_printf("plugins/lighttable/overlays_block_timeout/%d/%d",
table->mode, table->prefs_size);
if(!dt_conf_key_exists(txt))
timeout = dt_conf_get_int("plugins/lighttable/overlay_timeout");
else
timeout = dt_conf_get_int(txt);
g_free(txt);
}
for(const GList *l = table->list; l; l = g_list_next(l))
{
dt_thumbnail_t *th = l->data;
// in any cases, we update the tooltip
th->tooltip = table->show_tooltips;
if(over != table->overlays)
{
// we need to change the overlay content if we pass from normal to
// extended overlays this is not done on the fly with css to avoid
// computing extended msg for nothing and to reserve space if needed
dt_thumbnail_set_overlay(th, over, timeout);
// and we resize the bottom area
dt_thumbnail_resize(th, th->width, th->height, TRUE, IMG_TO_FIT);
}
else
{
dt_thumbnail_update_infos(th);
}
}
table->overlays = over;
table->overlays_block_timeout = timeout;
}
// change the type of overlays that should be shown
void dt_thumbtable_set_overlays_block_timeout(dt_thumbtable_t *table,
const int timeout)
{
if(!table)
return;
gchar *txt = g_strdup_printf("plugins/lighttable/overlays_block_timeout/%d/%d",
table->mode, table->prefs_size);
dt_conf_set_int(txt, timeout);
g_free(txt);
table->overlays_block_timeout = timeout;
// we need to change the overlay timeout for each thumbnails
for(const GList *l = table->list; l; l = g_list_next(l))
{
dt_thumbnail_t *th = l->data;
th->overlay_timeout_duration = timeout;
}
}
// get the thumb at specific position
static dt_thumbnail_t *_thumb_get_at_pos(dt_thumbtable_t *table, int x, int y)
{
for(const GList *l = table->list; l; l = g_list_next(l))
{
dt_thumbnail_t *th = l->data;
if(th->x <= x
&& th->x + th->width > x
&& th->y <= y
&& th->y + th->height > y)
return th;
}
return NULL;
}
// get the thumb which is currently under mouse cursor
static dt_thumbnail_t *_thumb_get_under_mouse(dt_thumbtable_t *table)
{
if(!table->mouse_inside)
return NULL;
int x = -1;
int y = -1;
gdk_window_get_origin(gtk_widget_get_window(table->widget), &x, &y);
x = table->last_x - x;
y = table->last_y - y;
return _thumb_get_at_pos(table, x, y);
}
// get imgid from rowid
static dt_imgid_t _thumb_get_imgid(int rowid)
{
dt_imgid_t id = NO_IMGID;
sqlite3_stmt *stmt;
gchar *query = g_strdup_printf
("SELECT imgid FROM memory.collected_images WHERE rowid=%d", rowid);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
id = sqlite3_column_int(stmt, 0);
}
g_free(query);
sqlite3_finalize(stmt);
return id;
}
// get rowid from imgid
static int _thumb_get_rowid(dt_imgid_t imgid)
{
int id = -1;
sqlite3_stmt *stmt;
gchar *query = g_strdup_printf
("SELECT rowid FROM memory.collected_images WHERE imgid=%d", imgid);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
id = sqlite3_column_int(stmt, 0);
}
g_free(query);
sqlite3_finalize(stmt);
return id;
}
// get the coordinate of the rectangular area used by all the loaded thumbs
static void _pos_compute_area(dt_thumbtable_t *table)
{
if(table->list)
{
int x1 = INT_MAX;
int y1 = INT_MAX;
int x2 = INT_MIN;
int y2 = INT_MIN;
for(const GList *l = table->list; l; l = g_list_next(l))
{
const dt_thumbnail_t *th = (const dt_thumbnail_t *)l->data;
x1 = MIN(x1, th->x);
y1 = MIN(y1, th->y);
x2 = MAX(x2, th->x);
y2 = MAX(y2, th->y);
}
table->thumbs_area.x = x1;
table->thumbs_area.y = y1;
table->thumbs_area.width = x2 + table->thumb_size - x1;
table->thumbs_area.height = y2 + table->thumb_size - y1;
}
else
{
table->thumbs_area.x = 0;
table->thumbs_area.y = 0;
table->thumbs_area.width = 0;
table->thumbs_area.height = 0;
}
}
// get the position of the next image after the one at (x,y)
static void _pos_get_next(dt_thumbtable_t *table, int *x, int *y)
{
if(table->mode == DT_THUMBTABLE_MODE_FILEMANAGER)
{
*x += table->thumb_size;
if(*x + table->thumb_size > table->view_width)
{
*x = table->center_offset;
*y += table->thumb_size;
}
}
else if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
{
*x += table->thumb_size;
}
else if(table->mode == DT_THUMBTABLE_MODE_ZOOM)
{
*x += table->thumb_size;
if(*x + table->thumb_size >
table->thumbs_area.x + table->thumbs_per_row * table->thumb_size)
{
*x = table->thumbs_area.x;
*y += table->thumb_size;
}
}
}
// get the position of the previous image after the one at (x,y)
static void _pos_get_previous(dt_thumbtable_t *table, int *x, int *y)
{
if(table->mode == DT_THUMBTABLE_MODE_FILEMANAGER)
{
*x -= table->thumb_size;
if(*x < 0)
{
*x = (table->thumbs_per_row - 1) * table->thumb_size + table->center_offset;
*y -= table->thumb_size;
}
}
else if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
{
*x -= table->thumb_size;
}
else if(table->mode == DT_THUMBTABLE_MODE_ZOOM)
{
*x -= table->thumb_size;
if(*x < table->thumbs_area.x)
{
*x = table->thumbs_area.x + (table->thumbs_per_row - 1) * table->thumb_size;
*y -= table->thumb_size;
}
}
}
// compute thumb_size, thumbs_per_row and rows for the current widget size
// return TRUE if something as changed (or forced) FALSE otherwise
static gboolean _compute_sizes(dt_thumbtable_t *table, gboolean force)
{
gboolean ret = FALSE; // return value to show if something as changed
GtkAllocation allocation;
gtk_widget_get_allocation(table->widget, &allocation);
if(allocation.width <= 20 || allocation.height <= 20)
{
table->view_width = allocation.width;
table->view_height = allocation.height;
return FALSE;
}
int old_size = table->thumb_size;
if(table->mode == DT_THUMBTABLE_MODE_FILEMANAGER)
{
const int npr = dt_view_lighttable_get_zoom(darktable.view_manager);
if(force
|| allocation.width != table->view_width
|| allocation.height != table->view_height
|| npr != table->thumbs_per_row)
{
table->thumbs_per_row = npr;
table->view_width = allocation.width;
table->view_height = allocation.height;
table->thumb_size = MIN(table->view_width / table->thumbs_per_row,
table->view_height);
table->rows = table->view_height / table->thumb_size + 1;
table->center_offset =
(table->view_width - table->thumbs_per_row * table->thumb_size) / 2;
ret = TRUE;
}
}
else if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
{
if(force
|| allocation.width != table->view_width
|| allocation.height != table->view_height)
{
table->thumbs_per_row = 1;
table->view_width = allocation.width;
table->view_height = allocation.height;
table->thumb_size = table->view_height;
table->rows = table->view_width / table->thumb_size;
table->center_offset = 0;
if(table->rows % 2)
table->rows += 2;
else
table->rows += 1;
ret = TRUE;
}
}
else if(table->mode == DT_THUMBTABLE_MODE_ZOOM)
{
const int npr = dt_view_lighttable_get_zoom(darktable.view_manager);
if(force
|| allocation.width != table->view_width
|| allocation.height != table->view_height)
{
table->thumbs_per_row = DT_ZOOMABLE_NB_PER_ROW;
table->view_width = allocation.width;
table->view_height = allocation.height;
table->thumb_size = table->view_width / npr;
table->rows = (table->view_height - table->thumbs_area.y) / table->thumb_size + 1;
table->center_offset = 0;
ret = TRUE;
}
}
// if the thumb size has changed, we need to set overlays, etc... correctly
if(table->thumb_size != old_size)
{
_thumbs_update_overlays_mode(table);
}
return ret;
}
// update scrollbars positions and visibility
// return their visibility state
static gboolean _thumbtable_update_scrollbars(dt_thumbtable_t *table)
{
if(table->mode != DT_THUMBTABLE_MODE_FILEMANAGER)
return FALSE;
if(!table->scrollbars)
return FALSE;
table->code_scrolling = TRUE;
// get the total number of images
const uint32_t nbid = MAX(1, dt_collection_get_collected_count());
// so the total number of lines is :
const uint32_t nblines = ceilf(nbid / (float)table->thumbs_per_row);
// now we have a space under last images (when the first shown line is fully shown)
const float pagesize = table->view_height / (float)table->thumb_size;
const float maxvalue = nblines + (pagesize - floorf(pagesize));
// the number of line before
float lbefore = (table->offset - 1) / table->thumbs_per_row;
if((table->offset - 1) % table->thumbs_per_row)
lbefore++;
// if scrollbars are used, we can have partial row shown
if(table->thumbs_area.y != 0)
{
lbefore += -table->thumbs_area.y / (float)table->thumb_size;
}
// if the scrollbar is currently visible and we want to hide it we
// first ensure that with the width without the scrollbar, we won't
// need a scrollbar
const int bar = gtk_widget_get_allocated_width(darktable.gui->scrollbars.vscrollbar);
if(gtk_widget_get_visible(darktable.gui->scrollbars.vscrollbar)
&& nblines <= table->rows - 1)
{
const int nw = table->view_width + bar;
if(nblines * nw / table->thumbs_per_row >= table->view_height)
{
dt_view_set_scrollbar(darktable.view_manager->current_view,
0, 0, 0, 0, lbefore, 0, maxvalue + 1, pagesize);
table->code_scrolling = FALSE;
return TRUE;
}
}
/* In filemanager, no horizontal bar, and vertical bar reference is 1 thumb.
We make sure to show a scrollbar - and thus keep thumbs slightly smaller -
if not showing it required to avoid an unstable state.
*/
const float thresh = (float)(table->view_width / table->thumbs_per_row)
/ (float)((table->view_width - bar) / table->thumbs_per_row) - 1.0f;
dt_view_set_scrollbar(darktable.view_manager->current_view,
0, 0, 0, 0, lbefore, 0, maxvalue - thresh, pagesize);
table->code_scrolling = FALSE;
return (lbefore >= maxvalue);
}
// remove all unneeded thumbnails from the list and the widget
// unneeded == completely hidden
static int _thumbs_remove_unneeded(dt_thumbtable_t *table,
GList **th_invalid)
{
if(!th_invalid)
return 0;
int changed = 0;
for(const GList *l = *th_invalid; l; l = g_list_next(l))
{
dt_thumbnail_t *th = l->data;
gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(th->w_main)), th->w_main);
dt_thumbnail_destroy(th);
changed++;
}
g_list_free(*th_invalid);
return changed;
}
static void _thumb_move_or_create(dt_thumbtable_t *table,
GList **th_invalid,
const int imgid,
const int rowid,
const int posx,
const int posy,
const gboolean top,
const dt_thumbnail_selection_t sel)
{
if(!th_invalid || *th_invalid==NULL)
{
// no widgets available. We need to create one
dt_thumbnail_t *thumb = dt_thumbnail_new(table->thumb_size,
table->thumb_size,
IMG_TO_FIT,
imgid,
rowid,
table->overlays,
DT_THUMBNAIL_CONTAINER_LIGHTTABLE,
table->show_tooltips,
sel);
if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
{
thumb->single_click = TRUE;
thumb->sel_mode = DT_THUMBNAIL_SEL_MODE_MOD_ONLY;
}
thumb->x = posx;
thumb->y = posy;
if(top)
table->list = g_list_prepend(table->list, thumb);
else
table->list = g_list_append(table->list, thumb);
// we remember image margins for new thumbs (this limit flickering)
dt_thumbnail_t *first = table->list->data;
gtk_widget_set_margin_start(thumb->w_image_box, gtk_widget_get_margin_start(first->w_image_box));
gtk_widget_set_margin_top(thumb->w_image_box, gtk_widget_get_margin_top(first->w_image_box));
gtk_layout_put(GTK_LAYOUT(table->widget), thumb->w_main, posx, posy);
}
else
{
// let's reuse a now unaffected widget
dt_thumbnail_t *thumb = (*th_invalid)->data;
thumb->imgid = imgid;
thumb->rowid = rowid;
thumb->x = posx;
thumb->y = posy;
dt_thumbnail_reload_infos(thumb);
dt_thumbnail_surface_destroy(thumb);
thumb->img_surf_preview = FALSE;
gtk_layout_move(GTK_LAYOUT(table->widget), thumb->w_main, thumb->x, thumb->y);
*th_invalid = g_list_delete_link(*th_invalid, *th_invalid);
// insert the thumb at the right place in the table->list
if(top)
table->list = g_list_prepend(table->list, thumb);
else
table->list = g_list_append(table->list, thumb);
// eventually update the selected state
dt_thumbnail_set_selection(thumb, sel);
}
}
// load all needed thumbnails in the list and the widget
// needed == that should appear in the current view (possibly not entirely)
static int _thumbs_load_needed(dt_thumbtable_t *table,
GList **th_invalid,
dt_thumbnail_t *first,
dt_thumbnail_t *last)
{
if(!first || !last) return 0;
sqlite3_stmt *stmt;
int changed = 0;
// we save here the last image value as they may change if the last image is invalid
const int last_y = last->y;
const int last_x = last->x;
const int last_rowid = last->rowid;
// we load image at the beginning
if(first->rowid > 1
&& (((table->mode == DT_THUMBTABLE_MODE_FILEMANAGER
|| table->mode == DT_THUMBTABLE_MODE_ZOOM)
&& first->y > 0)
|| (table->mode == DT_THUMBTABLE_MODE_FILMSTRIP
&& first->x > 0)))
{
int space = first->y;
if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
space = first->x;
const int nb_to_load = space / table->thumb_size + (space % table->thumb_size != 0);
// clang-format off
gchar *query = g_strdup_printf(
"SELECT mi.rowid, mi.imgid, si.imgid"
" FROM memory.collected_images AS mi"
" LEFT JOIN main.selected_images AS si"
" ON mi.imgid = si.imgid"
" WHERE mi.rowid<%d"
" ORDER BY mi.rowid DESC LIMIT %d",
first->rowid, nb_to_load * table->thumbs_per_row);
// clang-format on
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
int posx = first->x;
int posy = first->y;
_pos_get_previous(table, &posx, &posy);
while(sqlite3_step(stmt) == SQLITE_ROW)
{
if(posy < table->view_height) // we don't load invisible thumbs
{
const dt_imgid_t imgid = sqlite3_column_int(stmt, 1);
const int rowid = sqlite3_column_int(stmt, 0);
const gboolean selected = (imgid == sqlite3_column_int(stmt, 2));
_thumb_move_or_create(table,
th_invalid,
imgid,
rowid,
posx, posy,
TRUE,
selected);
changed++;
}
_pos_get_previous(table, &posx, &posy);
}
g_free(query);
sqlite3_finalize(stmt);
}
// we load images at the end
// if there's space under the last image, we have rows to load
// if the last line is not full, we have already reached the end of the collection
if((table->mode == DT_THUMBTABLE_MODE_FILEMANAGER
&& last_y + table->thumb_size < table->view_height
&& last_x >= table->thumb_size * (table->thumbs_per_row - 1))
|| (table->mode == DT_THUMBTABLE_MODE_FILMSTRIP
&& last_x + table->thumb_size < table->view_width)
|| (table->mode == DT_THUMBTABLE_MODE_ZOOM
&& last_y + table->thumb_size < table->view_height))
{
int space = table->view_height - (last_y + table->thumb_size);
if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
space = table->view_width - (last_x + table->thumb_size);
const int nb_to_load = space / table->thumb_size + (space % table->thumb_size != 0);
// clang-format off
gchar *query = g_strdup_printf(
"SELECT mi.rowid, mi.imgid, si.imgid"
" FROM memory.collected_images AS mi"
" LEFT JOIN main.selected_images AS si"
" ON mi.imgid = si.imgid"
" WHERE rowid>%d"
" ORDER BY rowid LIMIT %d",
last_rowid, nb_to_load * table->thumbs_per_row);
// clang-format on
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), query, -1, &stmt, NULL);
int posx = last_x;
int posy = last_y;
_pos_get_next(table, &posx, &posy);
while(sqlite3_step(stmt) == SQLITE_ROW)
{
if(posy + table->thumb_size > 0) // we don't load invisible thumbs
{
const dt_imgid_t imgid = sqlite3_column_int(stmt, 1);
const int rowid = sqlite3_column_int(stmt, 0);
const gboolean selected = (imgid == sqlite3_column_int(stmt, 2));
_thumb_move_or_create(table,
th_invalid,
imgid,
rowid,
posx, posy,
FALSE,
selected);
changed++;
}
_pos_get_next(table, &posx, &posy);
}
g_free(query);
sqlite3_finalize(stmt);
}
return changed;
}
// move all thumbs from the table.
// if clamp, we verify that the move is allowed (collection bounds, etc...)
static gboolean _move(dt_thumbtable_t *table,
const int x,
const int y,
const gboolean clamp)
{
if(!table->list)
return FALSE;
int posx = x;
int posy = y;
if(clamp)
{
// we check bounds to allow or not the move
if(table->mode == DT_THUMBTABLE_MODE_FILEMANAGER)
{
posx = 0; // to be sure, we don't want horizontal move
if(posy == 0) return FALSE;
dt_thumbnail_t *first = table->list->data;
// clamp the movement to ensure we don't go before the first image or after last one
const int max_up = ((first->rowid-1) / table->thumbs_per_row) * table->thumb_size - table->thumbs_area.y;
posy = MIN(posy, max_up);
// nb of line of the full collection
const uint32_t nblines =
ceilf(MAX(1,
dt_collection_get_collected_count()) / (float)table->thumbs_per_row);
// max first line on screen to ensure we don't go to far
const int max_line = nblines - table->view_height / table->thumb_size;
// limit of the movement
const int max_down = MAX(0, max_line * table->thumb_size - max_up);
posy = MAX(posy, -max_down);
}
else if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
{
posy = 0; // to be sure, we don't want vertical move
if(posx == 0) return FALSE;
// we stop when first or last rowid image is fully shown
const dt_thumbnail_t *first = table->list->data;
const dt_thumbnail_t *last = g_list_last(table->list)->data;
const int middle = (table->view_width - table->thumb_size) / 2;
posx = CLAMP(posx, middle - last->x, middle - first->x);
}
else if(table->mode == DT_THUMBTABLE_MODE_ZOOM)
{
// we stop before thumb area completely disappear from screen
const int space = table->thumb_size * 0.5; // we want at least
// 1/2 thumb to stay
// visible
posy = MIN(table->view_height - space - table->thumbs_area.y, posy);
posy = MAX(space - table->thumbs_area.y - table->thumbs_area.height, posy);
posx = MIN(table->view_width - space - table->thumbs_area.x, posx);
posx = MAX(space - table->thumbs_area.x - table->thumbs_area.width, posx);
}
}
if(posy == 0 && posx == 0)
return FALSE;
GList *th_invalid = NULL;
// we move all current thumbs
dt_thumbnail_t *first = NULL;
dt_thumbnail_t *last = NULL;
GList *l = table->list;
while(l)
{
dt_thumbnail_t *th = l->data;
if(!first)
first = th;
last = th;
th->y += posy;
th->x += posx;
if(th->y + table->thumb_size <= 0 || th->y > table->view_height
|| (table->mode == DT_THUMBTABLE_MODE_FILMSTRIP
&& (th->x + table->thumb_size <= 0 || th->x > table->view_width)))
{
th_invalid = g_list_prepend(th_invalid, th);
GList *ll = l;
l = g_list_next(l);
table->list = g_list_delete_link(table->list, ll);
if(table->drag_thumb == th)
table->drag_thumb = NULL;
}
else
{
gtk_layout_move(GTK_LAYOUT(table->widget), th->w_main, th->x, th->y);
l = g_list_next(l);
}
}
// we update the thumbs_area
const int old_areay = table->thumbs_area.y;
table->thumbs_area.x += posx;
table->thumbs_area.y += posy;
// we load all needed thumbs
int changed = _thumbs_load_needed(table, &th_invalid, first, last);
// we remove the images not visible on screen
changed += _thumbs_remove_unneeded(table, &th_invalid);
// if there has been changed, we recompute thumbs area
if(changed > 0)
_pos_compute_area(table);
// we update the offset
if(table->mode == DT_THUMBTABLE_MODE_FILEMANAGER)
{
// we need to take account of the previous area move if needed
table->offset =
MAX(1,
table->offset - (ceilf((posy + old_areay) / (float)table->thumb_size) * table->thumbs_per_row));
table->offset_imgid = _thumb_get_imgid(table->offset);
}
else if(table->mode == DT_THUMBTABLE_MODE_FILMSTRIP)
{
table->offset = MAX(1, table->offset - posx / table->thumb_size);
table->offset_imgid = _thumb_get_imgid(table->offset);
}
else if(table->mode == DT_THUMBTABLE_MODE_ZOOM)
{
const dt_thumbnail_t *nfirst = table->list->data;
table->offset = nfirst->rowid;
table->offset_imgid = nfirst->imgid;
}
// and we store it
dt_conf_set_int("plugins/lighttable/collect/history_pos0", table->offset);
if(table->mode == DT_THUMBTABLE_MODE_ZOOM)
{
dt_conf_set_int("lighttable/zoomable/last_offset", table->offset);
}
// update scrollbars
_thumbtable_update_scrollbars(table);
return TRUE;
}
static dt_thumbnail_t *_thumbtable_get_thumb(dt_thumbtable_t *table,
const dt_imgid_t imgid)
{
if(!dt_is_valid_imgid(imgid))
return NULL;
for(const GList *l = table->list; l; l = g_list_next(l))
{
dt_thumbnail_t *th = l->data;
if(th->imgid == imgid)
return th;
}
return NULL;
}
// change zoom value for the zoomable thumbtable
static void _zoomable_zoom(dt_thumbtable_t *table,
const int oldzoom,
const int newzoom)
{
// nothing to do if thumbtable is empty
if(!table->list)
return;
// determine the center of the zoom
int x = 0;
int y = 0;
if(table->mouse_inside)
{
// if the mouse is inside the table, let's use its position
gdk_window_get_origin(gtk_widget_get_window(table->widget), &x, &y);
x = table->last_x - x;
y = table->last_y - y;
}
else
{
// otherwise we use the center of the view
x = table->view_width / 2;
y = table->view_height / 2;
}
const int new_size = table->view_width / newzoom;
const double ratio = (double)new_size / (double)table->thumb_size;
// we get row/column numbers of the image under cursor
const int anchor_x = (x - table->thumbs_area.x) / table->thumb_size;
const int anchor_y = (y - table->thumbs_area.y) / table->thumb_size;
// we compute the new position of this image. This will be our
// reference to compute sizes of other thumbs
const int anchor_posx =
x - (x - anchor_x * table->thumb_size - table->thumbs_area.x) * ratio;
const int anchor_posy =
y - (y - anchor_y * table->thumb_size - table->thumbs_area.y) * ratio;
// we move and resize each thumbs
dt_thumbnail_t *first = NULL;
dt_thumbnail_t *last = NULL;
GList *l = table->list;
while(l)
{
dt_thumbnail_t *th = l->data;
if(!first) first = th;
last = th;
// we get row/column numbers
const int posx = (th->x - table->thumbs_area.x) / table->thumb_size;
const int posy = (th->y - table->thumbs_area.y) / table->thumb_size;
// we compute new position taking anchor image as reference
th->x = anchor_posx - (anchor_x - posx) * new_size;
th->y = anchor_posy - (anchor_y - posy) * new_size;
// we move the thumbnail to its new position.
// in some case the thumbnail may be out of sight. This will be handled later.
gtk_layout_move(GTK_LAYOUT(table->widget), th->w_main, th->x, th->y);
l = g_list_next(l);
dt_thumbnail_resize(th, new_size, new_size, FALSE, IMG_TO_FIT);
}
// we update table values
table->thumb_size = new_size;
_pos_compute_area(table);
// we ensure there's still some visible thumbnails
const int space = new_size * 0.5; // we want at least 1/2 thumb to stay visible
int posy = MIN(table->view_height - space - table->thumbs_area.y, 0);
posy = MAX(space - table->thumbs_area.y - table->thumbs_area.height, posy);
int posx = MIN(table->view_width - space - table->thumbs_area.x, 0);
posx = MAX(space - table->thumbs_area.x - table->thumbs_area.width, posx);
if(posx != 0 || posy != 0)
_move(table, posx, posy, FALSE);
// now we search for thumbnails out of sight
GList *th_invalid = NULL;
l = table->list;
while(l)
{
dt_thumbnail_t *th = l->data;
if(th->y + table->thumb_size <= 0 || th->y > table->view_height)
{
th_invalid = g_list_prepend(th_invalid, th);
GList *ll = l;
l = g_list_next(l);
table->list = g_list_delete_link(table->list, ll);
if(table->drag_thumb == th)
table->drag_thumb = NULL;
}
else
{
l = g_list_next(l);
}
}
// and we load/unload thumbs if needed
int changed = _thumbs_load_needed(table, &th_invalid, first, last);
changed += _thumbs_remove_unneeded(table, &th_invalid);
if(changed > 0)
_pos_compute_area(table);
// we update all the values
const dt_thumbnail_t *nfirst = table->list->data;
table->offset = nfirst->rowid;
table->offset_imgid = nfirst->imgid;
dt_conf_set_int("plugins/lighttable/collect/history_pos0", table->offset);
dt_conf_set_int("lighttable/zoomable/last_offset", table->offset);
dt_conf_set_int("lighttable/zoomable/last_pos_x", table->thumbs_area.x);
dt_conf_set_int("lighttable/zoomable/last_pos_y", table->thumbs_area.y);
dt_view_lighttable_set_zoom(darktable.view_manager, newzoom);
gtk_widget_queue_draw(table->widget);
}
// change zoom value for the classic thumbtable
static void _filemanager_zoom(dt_thumbtable_t *table,
const int oldzoom,
const int newzoom)
{
// nothing to do if thumbtable is empty
if(!table->list)
return;
// we are looking for the image to zoom around
int x = 0;
int y = 0;
dt_thumbnail_t *thumb = NULL;
if(table->mouse_inside)
{
// if the mouse is inside the table, let's use its position
gdk_window_get_origin(gtk_widget_get_window(table->widget), &x, &y);
x = table->last_x - x;
y = table->last_y - y;
thumb = _thumb_get_at_pos(table, x, y);
}
if(!thumb)
{
// otherwise we use the classic retrieving method
const dt_imgid_t id = dt_act_on_get_main_image();
thumb = _thumbtable_get_thumb(table, id);
if(thumb)
{
// and we take the center of the thumb
x = thumb->x + thumb->width / 2;
y = thumb->y + thumb->height / 2;
}
else