-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
imageop.c
4007 lines (3454 loc) · 126 KB
/
imageop.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) 2009-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/>.
*/
#include "develop/imageop.h"
#include "bauhaus/bauhaus.h"
#include "common/collection.h"
#include "common/debug.h"
#include "common/dtpthread.h"
#include "common/exif.h"
#include "common/history.h"
#include "common/imagebuf.h"
#include "common/interpolation.h"
#include "common/iop_group.h"
#include "common/module.h"
#include "common/opencl.h"
#include "common/presets.h"
#include "common/usermanual_url.h"
#include "control/control.h"
#include "develop/blend.h"
#include "develop/develop.h"
#include "develop/format.h"
#include "develop/masks.h"
#include "develop/tiling.h"
#include "dtgtk/button.h"
#include "dtgtk/expander.h"
#include "dtgtk/gradientslider.h"
#include "dtgtk/icon.h"
#include "gui/accelerators.h"
#include "gui/color_picker_proxy.h"
#include "gui/drag_and_drop.h"
#include "gui/gtk.h"
#include "gui/guides.h"
#include "gui/presets.h"
#include "imageio/imageio_rawspeed.h"
#include "libs/modulegroups.h"
#ifdef GDK_WINDOWING_QUARTZ
#include "osx/osx.h"
#endif
#include <assert.h>
#include <gmodule.h>
#include <math.h>
#include <complex.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
typedef struct dt_iop_gui_simple_callback_t
{
dt_iop_module_t *self;
int index;
} dt_iop_gui_simple_callback_t;
typedef struct dt_iop_gui_multi_show_t
{
gboolean close, up, down, new;
} dt_iop_gui_multi_show_t;
void dt_iop_load_default_params(dt_iop_module_t *module)
{
memcpy(module->params, module->default_params, module->params_size);
dt_develop_blend_colorspace_t cst =
dt_develop_blend_default_module_blend_colorspace(module);
dt_develop_blend_init_blend_parameters(module->default_blendop_params, cst);
dt_iop_commit_blend_params(module, module->default_blendop_params);
dt_iop_gui_blending_reload_defaults(module);
}
static void _iop_modify_roi_in(dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
const dt_iop_roi_t *roi_out,
dt_iop_roi_t *roi_in)
{
*roi_in = *roi_out;
}
static void _iop_modify_roi_out(dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
dt_iop_roi_t *roi_out,
const dt_iop_roi_t *roi_in)
{
*roi_out = *roi_in;
}
/* default group for modules which do not implement the
* default_group() function */
static int default_default_group(void)
{
return IOP_GROUP_BASIC;
}
/* default flags for modules which does not implement the flags()
* function */
static int default_flags(void)
{
return 0;
}
/* default operation tags for modules which does not implement the
* flags() function */
static int default_operation_tags(void)
{
return 0;
}
/* default operation tags filter for modules which does not implement
* the flags() function */
static int default_operation_tags_filter(void)
{
return 0;
}
static const char **default_description(dt_iop_module_t *self)
{
return NULL;
}
static const char *default_aliases(void)
{
return "";
}
static const char *default_deprecated_msg(void)
{
return NULL;
}
static void default_commit_params(dt_iop_module_t *self,
dt_iop_params_t *params,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
memcpy(piece->data, params, self->params_size);
}
static void default_init_pipe(dt_iop_module_t *self,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
piece->data = calloc(1,self->params_size);
}
static void default_cleanup_pipe(dt_iop_module_t *self,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
free(piece->data);
}
static void default_gui_cleanup(dt_iop_module_t *self)
{
IOP_GUI_FREE;
}
static void default_cleanup(dt_iop_module_t *module)
{
g_free(module->params);
module->params = NULL;
free(module->default_params);
module->default_params = NULL;
}
static gboolean default_distort_transform(dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
float *points,
size_t points_count)
{
return TRUE;
}
static gboolean default_distort_backtransform(dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
float *points,
size_t points_count)
{
return TRUE;
}
static void default_process(dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
const void *const i,
void *const o,
const dt_iop_roi_t *const roi_in,
const dt_iop_roi_t *const roi_out)
{
if(roi_in->width <= 1
|| roi_in->height <= 1
|| roi_out->width <= 1
|| roi_out->height <= 1)
return;
if(self->process_plain)
self->process_plain(self, piece, i, o, roi_in, roi_out);
else
dt_unreachable_codepath_with_desc(self->op);
}
static dt_introspection_field_t *default_get_introspection_linear(void)
{
return NULL;
}
static dt_introspection_t *default_get_introspection(void)
{
return NULL;
}
static void *default_get_p(const void *param, const char *name)
{
return NULL;
}
static dt_introspection_field_t *default_get_f(const char *name)
{
return NULL;
}
void dt_iop_default_init(dt_iop_module_t *module)
{
size_t param_size = module->so->get_introspection()->size;
module->params_size = param_size;
module->params = calloc(1, param_size);
module->default_params = calloc(1, param_size);
module->default_enabled = FALSE;
module->has_trouble = FALSE;
module->gui_data = NULL;
dt_introspection_field_t *i = module->so->get_introspection_linear();
while(i->header.type != DT_INTROSPECTION_TYPE_NONE)
{
switch(i->header.type)
{
case DT_INTROSPECTION_TYPE_FLOATCOMPLEX:
*(float complex*)((uint8_t *)module->default_params + i->header.offset) =
i->FloatComplex.Default;
break;
case DT_INTROSPECTION_TYPE_FLOAT:
*(float*)((uint8_t *)module->default_params + i->header.offset) = i->Float.Default;
break;
case DT_INTROSPECTION_TYPE_INT:
*(int*)((uint8_t *)module->default_params + i->header.offset) = i->Int.Default;
break;
case DT_INTROSPECTION_TYPE_UINT:
*(unsigned int*)((uint8_t *)module->default_params + i->header.offset) =
i->UInt.Default;
break;
case DT_INTROSPECTION_TYPE_USHORT:
*(unsigned short*)((uint8_t *)module->default_params + i->header.offset) =
i->UShort.Default;
break;
case DT_INTROSPECTION_TYPE_INT8:
*(short*)((uint8_t *)module->default_params + i->header.offset) = i->Int8.Default;
break;
case DT_INTROSPECTION_TYPE_ENUM:
*(int*)((uint8_t *)module->default_params + i->header.offset) = i->Enum.Default;
break;
case DT_INTROSPECTION_TYPE_BOOL:
*(gboolean*)((uint8_t *)module->default_params + i->header.offset) = i->Bool.Default;
break;
case DT_INTROSPECTION_TYPE_CHAR:
*(char*)((uint8_t *)module->default_params + i->header.offset) = i->Char.Default;
break;
case DT_INTROSPECTION_TYPE_OPAQUE:
memset((uint8_t *)module->default_params + i->header.offset, 0, i->header.size);
break;
case DT_INTROSPECTION_TYPE_ARRAY:
{
if(i->Array.type == DT_INTROSPECTION_TYPE_CHAR) break;
size_t element_size = i->Array.field->header.size;
if(element_size % sizeof(int))
{
int8_t *p = (int8_t *)module->default_params + i->header.offset;
for(size_t c = element_size; c < i->header.size; c++, p++)
p[element_size] = *p;
}
else
{
element_size /= sizeof(int);
const size_t num_ints = i->header.size / sizeof(int);
int *p = (int *)((uint8_t *)module->default_params + i->header.offset);
for(size_t c = element_size; c < num_ints; c++, p++)
p[element_size] = *p;
}
}
break;
case DT_INTROSPECTION_TYPE_STRUCT:
// ignore STRUCT; nothing to do
break;
default:
dt_print(DT_DEBUG_PARAMS,
"[dt_iop_default_init] in `%s' unsupported introspection"
" type \"%s\" encountered"
" in (field %s)",
module->op, i->header.type_name, i->header.field_name);
break;
}
i++;
}
}
int dt_iop_load_module_so(void *m, const char *libname, const char *module_name)
{
dt_iop_module_so_t *module = (dt_iop_module_so_t *)m;
g_strlcpy(module->op, module_name, sizeof(module->op));
#define INCLUDE_API_FROM_MODULE_LOAD "iop_load_module"
#include "iop/iop_api.h"
if(!module->init)
module->init = dt_iop_default_init;
if(!module->modify_roi_in)
module->modify_roi_in = _iop_modify_roi_in;
if(!module->modify_roi_out)
module->modify_roi_out = _iop_modify_roi_out;
module->process_plain = module->process;
module->process = default_process;
module->data = NULL;
// the introspection api
module->have_introspection = FALSE;
if(module->introspection_init)
{
if(!module->introspection_init(module, DT_INTROSPECTION_VERSION))
{
// set the introspection related fields in module
module->have_introspection = TRUE;
if(module->get_p == default_get_p
|| module->get_f == default_get_f
|| module->get_introspection_linear == default_get_introspection_linear
|| module->get_introspection == default_get_introspection)
goto api_h_error;
}
else
dt_print(DT_DEBUG_ALWAYS,
"[iop_load_module] failed to initialize introspection for operation `%s'",
module_name);
}
if(module->init_global)
module->init_global(module);
return 0;
}
gboolean dt_iop_load_module_by_so(dt_iop_module_t *module,
dt_iop_module_so_t *so,
dt_develop_t *dev)
{
module->actions = DT_ACTION_TYPE_IOP_INSTANCE;
module->dev = dev;
module->widget = NULL;
module->header = NULL;
module->off = NULL;
module->hide_enable_button = FALSE;
module->has_trouble = FALSE;
module->request_color_pick = DT_REQUEST_COLORPICK_OFF;
module->request_histogram = DT_REQUEST_ONLY_IN_GUI;
module->histogram_stats.bins_count = 0;
module->histogram_stats.pixels = 0;
module->multi_priority = 0;
module->multi_name_hand_edited = FALSE;
module->iop_order = 0;
for(int k = 0; k < 3; k++)
{
module->picked_color[k] = module->picked_output_color[k] = 0.0f;
module->picked_color_min[k] = module->picked_output_color_min[k] = 666.0f;
module->picked_color_max[k] = module->picked_output_color_max[k] = -666.0f;
}
module->histogram_cst = IOP_CS_NONE;
module->histogram = NULL;
module->histogram_max[0] = module->histogram_max[1] =
module->histogram_max[2] = module->histogram_max[3] = 0;
module->histogram_middle_grey = FALSE;
module->request_mask_display = DT_DEV_PIXELPIPE_DISPLAY_NONE;
module->suppress_mask = FALSE;
module->enabled = module->default_enabled = FALSE; // all modules disabled by default.
g_strlcpy(module->op, so->op, sizeof(module->op));
module->raster_mask.source.users = g_hash_table_new(NULL, NULL);
module->raster_mask.source.masks =
g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
module->raster_mask.sink.source = NULL;
module->raster_mask.sink.id = INVALID_MASKID;
// only reference cached results of dlopen:
module->module = so->module;
module->so = so;
#define INCLUDE_API_FROM_MODULE_LOAD_BY_SO
#include "iop/iop_api.h"
module->version = so->version;
module->process_plain = so->process_plain;
module->have_introspection = so->have_introspection;
module->reset_button = NULL;
module->presets_button = NULL;
module->fusion_slider = NULL;
if(module->dev && module->dev->gui_attached)
{
/* set button state */
char option[1024];
snprintf(option, sizeof(option), "plugins/darkroom/%s/visible", module->op);
dt_iop_module_state_t state = IOP_STATE_HIDDEN;
if(dt_conf_get_bool(option))
{
state = IOP_STATE_ACTIVE;
snprintf(option, sizeof(option), "plugins/darkroom/%s/favorite", module->op);
if(dt_conf_get_bool(option)) state = IOP_STATE_FAVORITE;
}
dt_iop_gui_set_state(module, state);
}
module->global_data = so->data;
// now init the instance:
module->init(module);
/* initialize blendop params and default values */
module->blend_params = calloc(1, sizeof(dt_develop_blend_params_t));
module->default_blendop_params = calloc(1, sizeof(dt_develop_blend_params_t));
dt_develop_blend_colorspace_t cst =
dt_develop_blend_default_module_blend_colorspace(module);
dt_develop_blend_init_blend_parameters(module->default_blendop_params, cst);
dt_iop_commit_blend_params(module, module->default_blendop_params);
if(module->params_size == 0)
{
dt_print(DT_DEBUG_ALWAYS,
"[iop_load_module] `%s' needs to have a params size > 0!", so->op);
return TRUE; // empty params hurt us in many places, just add a dummy value
}
module->enabled = module->default_enabled; // apply (possibly new) default.
return FALSE;
}
void dt_iop_init_pipe(dt_iop_module_t *module,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
module->init_pipe(module, pipe, piece);
piece->blendop_data = calloc(1, sizeof(dt_develop_blend_params_t));
}
static gboolean _header_enter_notify_callback(GtkWidget *eventbox,
GdkEventCrossing *event,
gpointer user_data)
{
darktable.control->element = GPOINTER_TO_INT(user_data);
return FALSE;
}
static gboolean _header_motion_notify_show_callback(GtkWidget *eventbox,
GdkEventCrossing *event,
dt_iop_module_t *module)
{
darktable.control->element = DT_ACTION_ELEMENT_SHOW;
return dt_iop_show_hide_header_buttons(module, event, TRUE, FALSE);
}
static gboolean _header_motion_notify_hide_callback(GtkWidget *eventbox,
GdkEventCrossing *event,
dt_iop_module_t *module)
{
return dt_iop_show_hide_header_buttons(module, event, FALSE, FALSE);
}
static void _header_menu_deactivate_callback(GtkMenuShell *menushell,
dt_iop_module_t *module)
{
dt_iop_show_hide_header_buttons(module, NULL, FALSE, FALSE);
}
static void _gui_delete_callback(GtkButton *button, dt_iop_module_t *module)
{
dt_develop_t *dev = module->dev;
// we search another module with the same base
// we want the next module if any or the previous one
GList *modules = module->dev->iop;
dt_iop_module_t *next = NULL;
gboolean find = FALSE;
while(modules)
{
dt_iop_module_t *mod = modules->data;
if(mod == module)
{
find = TRUE;
if(next) break;
}
else if(mod->instance == module->instance)
{
next = mod;
if(find) break;
}
modules = g_list_next(modules);
}
if(!next) return; // what happened ???
if(dev->gui_attached)
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_DEVELOP_HISTORY_WILL_CHANGE);
// we must pay attention if priority is 0
const gboolean is_zero = (module->multi_priority == 0);
// we set the focus to the other instance
dt_iop_gui_set_expanded(next, TRUE, FALSE);
dt_iop_request_focus(next);
++darktable.gui->reset;
// we remove the plugin effectively
if(!dt_iop_is_hidden(module))
{
dt_iop_gui_cleanup_module(module);
gtk_widget_grab_focus(dt_ui_center(darktable.gui->ui));
}
// we remove all references in the history stack and dev->iop
// this will inform that a module has been removed from history
// we do it here so we have the multi_priorities to reconstruct
// de deleted module if the user undo it
dt_dev_module_remove(dev, module);
// if module was priority 0, then we set next to priority 0
if(is_zero)
{
// we want the first one in history
dt_iop_module_t *first = NULL;
GList *history = dev->history;
while(history)
{
dt_dev_history_item_t *hist = history->data;
if(hist->module->instance == module->instance && hist->module != module)
{
first = hist->module;
break;
}
history = g_list_next(history);
}
if(first == NULL) first = next;
// we set priority of first to 0
dt_iop_update_multi_priority(first, 0);
// we change this in the history stack too
for(history = dev->history; history; history = g_list_next(history))
{
dt_dev_history_item_t *hist = history->data;
if(hist->module == first) hist->multi_priority = 0;
}
}
// we save the current state of history (with the new multi_priorities)
if(dev->gui_attached)
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_DEVELOP_HISTORY_CHANGE);
// rebuild the accelerators (to point to an extant module)
dt_iop_connect_accels_multi(module->so);
dt_action_cleanup_instance_iop(module);
// don't delete the module, a pipe may still need it
dev->alliop = g_list_append(dev->alliop, module);
dt_dev_pixelpipe_rebuild(dev);
/* redraw */
dt_control_queue_redraw_center();
--darktable.gui->reset;
}
dt_iop_module_t *dt_iop_gui_get_previous_visible_module(const dt_iop_module_t *module)
{
dt_iop_module_t *prev = NULL;
for(GList *modules = module->dev->iop;
modules;
modules = g_list_next(modules))
{
dt_iop_module_t *mod = modules->data;
if(mod == module)
{
break;
}
else
{
// only for visible modules
GtkWidget *expander = mod->expander;
if(expander && gtk_widget_is_visible(expander))
{
prev = mod;
}
}
}
return prev;
}
dt_iop_module_t *dt_iop_gui_get_next_visible_module(const dt_iop_module_t *module)
{
dt_iop_module_t *next = NULL;
for(const GList *modules = g_list_last(module->dev->iop);
modules;
modules = g_list_previous(modules))
{
dt_iop_module_t *mod = modules->data;
if(mod == module)
{
break;
}
else
{
// only for visible modules
GtkWidget *expander = mod->expander;
if(expander && gtk_widget_is_visible(expander))
{
next = mod;
}
}
}
return next;
}
static void _gui_movedown_callback(GtkButton *button, dt_iop_module_t *module)
{
dt_ioppr_check_iop_order(module->dev, 0, "dt_iop_gui_movedown_callback begin");
// we need to place this module right before the previous
dt_iop_module_t *prev = dt_iop_gui_get_previous_visible_module(module);
if(!prev) return;
const int moved = dt_ioppr_move_iop_before(module->dev, module, prev);
if(!moved) return;
// we move the headers
GValue gv = { 0, { { 0 } } };
g_value_init(&gv, G_TYPE_INT);
gtk_container_child_get_property(
GTK_CONTAINER(dt_ui_get_container(darktable.gui->ui,
DT_UI_CONTAINER_PANEL_RIGHT_CENTER)),
prev->expander,
"position", &gv);
gtk_box_reorder_child(dt_ui_get_container(darktable.gui->ui,
DT_UI_CONTAINER_PANEL_RIGHT_CENTER),
module->expander, g_value_get_int(&gv));
dt_dev_add_history_item(prev->dev, module, TRUE);
dt_ioppr_check_iop_order(module->dev, 0, "dt_iop_gui_movedown_callback end");
// rebuild the accelerators
dt_iop_connect_accels_multi(module->so);
dt_dev_pixelpipe_rebuild(module->dev);
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_DEVELOP_MODULE_MOVED);
}
static void _gui_moveup_callback(GtkButton *button, dt_iop_module_t *module)
{
dt_ioppr_check_iop_order(module->dev, 0, "dt_iop_gui_moveup_callback begin");
// we need to place this module right after the next one
dt_iop_module_t *next = dt_iop_gui_get_next_visible_module(module);
if(!next) return;
const int moved = dt_ioppr_move_iop_after(module->dev, module, next);
if(!moved) return;
// we move the headers
GValue gv = { 0, { { 0 } } };
g_value_init(&gv, G_TYPE_INT);
gtk_container_child_get_property(
GTK_CONTAINER(dt_ui_get_container(darktable.gui->ui,
DT_UI_CONTAINER_PANEL_RIGHT_CENTER)),
next->expander,
"position", &gv);
gtk_box_reorder_child(dt_ui_get_container(darktable.gui->ui,
DT_UI_CONTAINER_PANEL_RIGHT_CENTER),
module->expander, g_value_get_int(&gv));
dt_dev_add_history_item(next->dev, module, TRUE);
dt_ioppr_check_iop_order(module->dev, 0, "dt_iop_gui_moveup_callback end");
// rebuild the accelerators
dt_iop_connect_accels_multi(module->so);
dt_dev_pixelpipe_rebuild(next->dev);
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_DEVELOP_MODULE_MOVED);
}
dt_iop_module_t *dt_iop_gui_duplicate(dt_iop_module_t *base,
const gboolean copy_params)
{
// make sure the duplicated module appears in the history
dt_dev_add_history_item(base->dev, base, FALSE);
// first we create the new module
++darktable.gui->reset;
dt_iop_module_t *module = dt_dev_module_duplicate(base->dev, base);
--darktable.gui->reset;
if(!module) return NULL;
// what is the position of the module in the pipe ?
GList *modules = module->dev->iop;
int pos_module = 0;
int pos_base = 0;
int pos = 0;
while(modules)
{
dt_iop_module_t *mod = modules->data;
if(mod == module)
pos_module = pos;
else if(mod == base)
pos_base = pos;
modules = g_list_next(modules);
pos++;
}
// we set the gui part of it
/* initialize gui if iop have one defined */
if(!dt_iop_is_hidden(module))
{
// make sure gui_init and reload defaults is called safely
dt_iop_gui_init(module);
/* add module to right panel */
dt_iop_gui_set_expander(module);
GValue gv = { 0, { { 0 } } };
g_value_init(&gv, G_TYPE_INT);
gtk_container_child_get_property(
GTK_CONTAINER(dt_ui_get_container(darktable.gui->ui,
DT_UI_CONTAINER_PANEL_RIGHT_CENTER)),
base->expander, "position", &gv);
gtk_box_reorder_child(dt_ui_get_container(darktable.gui->ui,
DT_UI_CONTAINER_PANEL_RIGHT_CENTER),
module->expander,
g_value_get_int(&gv) + pos_base - pos_module + 1);
dt_iop_gui_set_expanded(module, TRUE, FALSE);
dt_iop_reload_defaults(module); // some modules like profiled
// denoise update the gui in
// reload_defaults
if(copy_params)
{
memcpy(module->params, base->params, module->params_size);
if(module->flags() & IOP_FLAGS_SUPPORTS_BLENDING)
{
dt_iop_commit_blend_params(module, base->blend_params);
if(dt_is_valid_maskid(base->blend_params->mask_id))
{
module->blend_params->mask_id = NO_MASKID;
dt_masks_iop_use_same_as(module, base);
}
}
}
// we save the new instance creation
dt_dev_add_history_item(module->dev, module, TRUE);
dt_iop_gui_update_blending(module);
}
if(dt_conf_get_bool("darkroom/ui/single_module"))
{
dt_iop_gui_set_expanded(base, FALSE, TRUE);
dt_iop_gui_set_expanded(module, TRUE, TRUE);
}
// and we refresh the pipe
dt_iop_request_focus(module);
if(module->dev->gui_attached)
{
dt_dev_pixelpipe_rebuild(module->dev);
}
/* update ui to new parameters */
dt_iop_gui_update(module);
dt_dev_modulegroups_update_visibility(darktable.develop);
return module;
}
static void _gui_copy_callback(GtkButton *button, dt_iop_module_t *base)
{
dt_iop_module_t *module = dt_iop_gui_duplicate(base, FALSE);
/* setup key accelerators */
dt_iop_connect_accels_multi(base->so);
if(dt_conf_get_bool("darkroom/ui/rename_new_instance"))
dt_iop_gui_rename_module(module);
}
static void _gui_duplicate_callback(GtkButton *button, dt_iop_module_t *base)
{
dt_iop_module_t *module = dt_iop_gui_duplicate(base, TRUE);
/* setup key accelerators */
dt_iop_connect_accels_multi(base->so);
if(dt_conf_get_bool("darkroom/ui/rename_new_instance"))
dt_iop_gui_rename_module(module);
}
static gboolean _rename_module_key_press(GtkWidget *entry,
GdkEventKey *event,
dt_iop_module_t *module)
{
gboolean ended = FALSE;
if(event->type == GDK_FOCUS_CHANGE
|| event->keyval == GDK_KEY_Return
|| event->keyval == GDK_KEY_KP_Enter)
{
if(gtk_entry_get_text_length(GTK_ENTRY(entry)) > 0)
{
// name is not empty, set new multi_name
const gchar *name = gtk_entry_get_text(GTK_ENTRY(entry));
if(g_strcmp0(module->multi_name, name) != 0)
{
g_strlcpy(module->multi_name, name, sizeof(module->multi_name));
// this has been hand edited, the name should not be changed when
// applying a preset or a style.
module->multi_name_hand_edited = TRUE;
dt_dev_add_history_item(module->dev, module, TRUE);
}
}
else
{
// clear out multi-name (set 1st char to 0)
module->multi_name[0] = 0;
module->multi_name_hand_edited = FALSE;
dt_dev_add_history_item(module->dev, module, FALSE);
}
// make sure we write history & xmp to ensure that the new module name
// gets recorded into the XMP and won't be lost in case of crash.
dt_dev_write_history(darktable.develop);
dt_image_synch_xmp(darktable.develop->image_storage.id);
ended = TRUE;
}
else if(event->keyval == GDK_KEY_Escape)
{
ended = TRUE;
}
if(ended)
{
gtk_widget_show(module->instance_name);
g_signal_handlers_disconnect_by_func(entry,
G_CALLBACK(_rename_module_key_press),
module);
gtk_widget_destroy(entry);
dt_iop_show_hide_header_buttons(module, NULL, TRUE, FALSE); // after removing entry
dt_iop_gui_update_header(module);
dt_masks_group_update_name(module);
return TRUE;
}
return FALSE; /* event not handled */
}
static gboolean _rename_module_resize(GtkWidget *entry,
GdkEventKey *event,
dt_iop_module_t *module)
{
int width = 0;
GtkBorder padding;
pango_layout_get_pixel_size(gtk_entry_get_layout(GTK_ENTRY(entry)), &width, NULL);
gtk_style_context_get_padding(gtk_widget_get_style_context (entry),
gtk_widget_get_state_flags (entry),
&padding);
gtk_widget_set_size_request(entry, width + padding.left + padding.right + 1, -1);
return TRUE;
}
void dt_iop_gui_rename_module(dt_iop_module_t *module)
{
GtkWidget *focused = gtk_container_get_focus_child(GTK_CONTAINER(module->header));
if(focused && GTK_IS_ENTRY(focused)) return;
GtkWidget *entry = gtk_entry_new();
gtk_widget_set_name(entry, "iop-panel-label");
gtk_entry_set_width_chars(GTK_ENTRY(entry), 0);
gtk_entry_set_max_length(GTK_ENTRY(entry), sizeof(module->multi_name) - 1);
gtk_entry_set_text(GTK_ENTRY(entry),
strcmp(module->multi_name, "0")
|| module->multi_priority > 0
|| module->multi_name_hand_edited
? module->multi_name
: "");
// hide module instance name as we need the space for the entry
gtk_widget_hide(module->instance_name);
gtk_widget_add_events(entry, GDK_FOCUS_CHANGE_MASK);
g_signal_connect(entry, "key-press-event",
G_CALLBACK(_rename_module_key_press), module);
g_signal_connect(entry, "focus-out-event",
G_CALLBACK(_rename_module_key_press), module);
g_signal_connect(entry, "style-updated",
G_CALLBACK(_rename_module_resize), module);
g_signal_connect(entry, "changed",
G_CALLBACK(_rename_module_resize), module);
g_signal_connect(entry, "enter-notify-event",
G_CALLBACK(_header_enter_notify_callback),
GINT_TO_POINTER(DT_ACTION_ELEMENT_SHOW));
dt_iop_show_hide_header_buttons(module, NULL, FALSE, TRUE); // before adding entry
gtk_box_pack_start(GTK_BOX(module->header), entry, TRUE, TRUE, 0);
gtk_widget_show(entry);
gtk_widget_grab_focus(entry);
}
static void _gui_rename_callback(GtkButton *button,
dt_iop_module_t *module)
{
dt_iop_gui_rename_module(module);
}
void _get_multi_show(dt_iop_module_t *module,
dt_iop_gui_multi_show_t *multi_show)
{
dt_develop_t *dev = darktable.develop;
// We count the number of other instances
int nb_instances = 0;
for(GList *modules = dev->iop; modules; modules = g_list_next(modules))
{
dt_iop_module_t *mod = modules->data;
if(mod->instance == module->instance) nb_instances++;
}
dt_iop_module_t *mod_prev = dt_iop_gui_get_previous_visible_module(module);
dt_iop_module_t *mod_next = dt_iop_gui_get_next_visible_module(module);
const gboolean move_next =
(mod_next && mod_next->iop_order != INT_MAX)
? dt_ioppr_check_can_move_after_iop(dev->iop, module, mod_next)
: -1.0;
const gboolean move_prev =
(mod_prev && mod_prev->iop_order != INT_MAX)
? dt_ioppr_check_can_move_before_iop(dev->iop, module, mod_prev)
: -1.0;
multi_show->new = !(module->flags() & IOP_FLAGS_ONE_INSTANCE);
multi_show->close = (nb_instances > 1);
if(mod_next)
multi_show->up = move_next;
else
multi_show->up = 0;
if(mod_prev)
multi_show->down = move_prev;
else
multi_show->down = 0;
}
static gboolean _gui_multiinstance_callback(GtkButton *button,
GdkEventButton *event,
dt_iop_module_t *module)
{
if(event && event->button == 3)