/
bauhaus.c
3613 lines (3121 loc) · 125 KB
/
bauhaus.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) 2012-2022 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 "bauhaus/bauhaus.h"
#include "common/calculator.h"
#include "common/darktable.h"
#include "control/conf.h"
#include "develop/develop.h"
#include "develop/imageop.h"
#include "gui/accelerators.h"
#include "gui/color_picker_proxy.h"
#include "gui/gtk.h"
#ifdef GDK_WINDOWING_QUARTZ
#include "osx/osx.h"
#endif
#include <math.h>
#include <strings.h>
#include <pango/pangocairo.h>
G_DEFINE_TYPE(DtBauhausWidget, dt_bh, GTK_TYPE_DRAWING_AREA)
enum
{
// Sliders
DT_ACTION_ELEMENT_VALUE = 0,
DT_ACTION_ELEMENT_BUTTON = 1,
DT_ACTION_ELEMENT_FORCE = 2,
DT_ACTION_ELEMENT_ZOOM = 3,
// Combos
DT_ACTION_ELEMENT_SELECTION = 0,
//DT_ACTION_ELEMENT_BUTTON = 1,
};
static const dt_action_def_t _action_def_slider, _action_def_combo,
_action_def_focus_slider, _action_def_focus_combo, _action_def_focus_button;
// INNER_PADDING is the horizontal space between slider and quad
// and vertical space between labels and slider baseline
static const double INNER_PADDING = 4.0;
// fwd declare
static gboolean dt_bauhaus_popup_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data);
static gboolean dt_bauhaus_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
static gboolean _widget_draw(GtkWidget *widget, cairo_t *crf);
static gboolean _widget_scroll(GtkWidget *widget, GdkEventScroll *event);
static gboolean _widget_key_press(GtkWidget *widget, GdkEventKey *event);
static void _get_preferred_width(GtkWidget *widget, gint *minimum_size, gint *natural_size);
static void _get_preferred_height(GtkWidget *widget, gint *minimum_height, gint *natural_height);
static void dt_bauhaus_widget_accept(dt_bauhaus_widget_t *w);
static void dt_bauhaus_widget_reject(dt_bauhaus_widget_t *w);
static void _bauhaus_combobox_set(dt_bauhaus_widget_t *w, const int pos, const gboolean mute);
static void bauhaus_request_focus(dt_bauhaus_widget_t *w)
{
if(w->module && w->module->type == DT_ACTION_TYPE_IOP_INSTANCE)
dt_iop_request_focus((dt_iop_module_t *)w->module);
gtk_widget_set_state_flags(GTK_WIDGET(w), GTK_STATE_FLAG_FOCUSED, FALSE);
}
static float _widget_get_quad_width(dt_bauhaus_widget_t *w)
{
if(w->show_quad)
return darktable.bauhaus->quad_width + INNER_PADDING;
else
return .0f;
}
static void _combobox_next_sensitive(dt_bauhaus_widget_t *w, int delta, const gboolean mute)
{
dt_bauhaus_combobox_data_t *d = &w->data.combobox;
int new_pos = d->active;
int step = delta > 0 ? 1 : -1;
int cur = new_pos + step;
while(delta && cur >= 0 && cur < d->entries->len)
{
dt_bauhaus_combobox_entry_t *entry = g_ptr_array_index(d->entries, cur);
if(entry->sensitive)
{
new_pos = cur;
delta -= step;
}
cur += step;
}
_bauhaus_combobox_set(w, new_pos, mute);
}
static dt_bauhaus_combobox_entry_t *new_combobox_entry(const char *label, dt_bauhaus_combobox_alignment_t alignment,
gboolean sensitive, void *data, void (*free_func)(void *))
{
dt_bauhaus_combobox_entry_t *entry = (dt_bauhaus_combobox_entry_t *)calloc(1, sizeof(dt_bauhaus_combobox_entry_t));
entry->label = g_strdup(label);
entry->alignment = alignment;
entry->sensitive = sensitive;
entry->data = data;
entry->free_func = free_func;
return entry;
}
static void free_combobox_entry(gpointer data)
{
dt_bauhaus_combobox_entry_t *entry = (dt_bauhaus_combobox_entry_t *)data;
g_free(entry->label);
if(entry->free_func)
entry->free_func(entry->data);
free(entry);
}
static GdkRGBA * default_color_assign()
{
// helper to initialize a color pointer with red color as a default
GdkRGBA color = {.red = 1.0f, .green = 0.0f, .blue = 0.0f, .alpha = 1.0f};
return gdk_rgba_copy(&color);
}
static void _margins_retrieve(dt_bauhaus_widget_t *w)
{
GtkStyleContext *context = gtk_widget_get_style_context(GTK_WIDGET(w));
const GtkStateFlags state = gtk_widget_get_state_flags(GTK_WIDGET(w));
gtk_style_context_get_margin(context, state, &w->margin);
gtk_style_context_get_padding(context, state, &w->padding);
}
void dt_bauhaus_widget_set_section(GtkWidget *widget, const gboolean is_section)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
w->is_section = is_section;
}
static int show_pango_text(dt_bauhaus_widget_t *w, GtkStyleContext *context, cairo_t *cr, const char *text,
float x_pos, float y_pos, float max_width, gboolean right_aligned, gboolean calc_only,
PangoEllipsizeMode ellipsize, gboolean is_markup, gboolean is_label, float *width,
float *height)
{
PangoLayout *layout = pango_cairo_create_layout(cr);
if(max_width > 0)
{
pango_layout_set_ellipsize(layout, ellipsize);
pango_layout_set_width(layout, (int)(PANGO_SCALE * max_width + 0.5f));
}
if(text)
{
if(is_markup)
pango_layout_set_markup(layout, text, -1);
else
pango_layout_set_text(layout, text, -1);
}
else
{
// length of -1 is not allowed with NULL string (wtf)
pango_layout_set_text(layout, NULL, 0);
}
PangoFontDescription *font_desc = 0;
gtk_style_context_get(context, gtk_widget_get_state_flags(GTK_WIDGET(w)), "font", &font_desc, NULL);
pango_layout_set_font_description(layout, font_desc);
PangoAttrList *attrlist = pango_attr_list_new();
PangoAttribute *attr = pango_attr_font_features_new("tnum");
pango_attr_list_insert(attrlist, attr);
pango_layout_set_attributes(layout, attrlist);
pango_attr_list_unref(attrlist);
pango_cairo_context_set_resolution(pango_layout_get_context(layout), darktable.gui->dpi);
int pango_width, pango_height;
pango_layout_get_size(layout, &pango_width, &pango_height);
const float text_width = ((double)pango_width/PANGO_SCALE);
if(calc_only && width && height)
{
*width = text_width;
*height = ((double)pango_height / PANGO_SCALE);
}
if(right_aligned) x_pos -= text_width;
if(!calc_only)
{
cairo_move_to(cr, x_pos, y_pos);
pango_cairo_show_layout(cr, layout);
}
pango_font_description_free(font_desc);
g_object_unref(layout);
return text_width;
}
// -------------------------------
static gboolean _cursor_timeout_callback(gpointer user_data)
{
if(darktable.bauhaus->cursor_blink_counter > 0) darktable.bauhaus->cursor_blink_counter--;
darktable.bauhaus->cursor_visible = !darktable.bauhaus->cursor_visible;
gtk_widget_queue_draw(darktable.bauhaus->popup_area);
// this can be >0 when we haven't reached the desired number or -1 when blinking forever
if(darktable.bauhaus->cursor_blink_counter != 0)
return TRUE;
darktable.bauhaus->cursor_timeout = 0; // otherwise the cursor won't come up when starting to type
return FALSE;
}
static void _start_cursor(int max_blinks)
{
darktable.bauhaus->cursor_blink_counter = max_blinks;
darktable.bauhaus->cursor_visible = FALSE;
if(darktable.bauhaus->cursor_timeout == 0)
darktable.bauhaus->cursor_timeout = g_timeout_add(500, _cursor_timeout_callback, NULL);
}
static void _stop_cursor()
{
if(darktable.bauhaus->cursor_timeout > 0)
{
g_source_remove(darktable.bauhaus->cursor_timeout);
darktable.bauhaus->cursor_timeout = 0;
darktable.bauhaus->cursor_visible = FALSE;
}
}
// -------------------------------
static void dt_bauhaus_slider_set_normalized(dt_bauhaus_widget_t *w, float pos);
static float slider_right_pos(float width, dt_bauhaus_widget_t *w)
{
// relative position (in widget) of the right bound of the slider corrected with the inner padding
return 1.0f - _widget_get_quad_width(w) / width;
}
static float slider_coordinate(const float abs_position, const float width, dt_bauhaus_widget_t *w)
{
// Translates an horizontal position relative to the slider
// in an horizontal position relative to the widget
const float left_bound = 0.0f;
const float right_bound = slider_right_pos(width, w); // exclude the quad area on the right
return (left_bound + abs_position * (right_bound - left_bound)) * width;
}
static float get_slider_line_offset(float pos, float scale, float x, float y, float ht, const int width,
dt_bauhaus_widget_t *w)
{
// ht is in [0,1] scale here
const float l = 0.0f;
const float r = slider_right_pos(width, w);
float offset = 0.0f;
// handle linear startup and rescale y to fit the whole range again
if(y < ht)
{
offset = (x - l) / (r - l) - pos;
}
else
{
y -= ht;
y /= (1.0f - ht);
offset = (x - y * y * .5f - (1.0f - y * y) * (l + pos * (r - l)))
/ (.5f * y * y / scale + (1.0f - y * y) * (r - l));
}
// clamp to result in a [0,1] range:
if(pos + offset > 1.0f) offset = 1.0f - pos;
if(pos + offset < 0.0f) offset = -pos;
return offset;
}
// draw a loupe guideline for the quadratic zoom in in the slider interface:
static void draw_slider_line(cairo_t *cr, float pos, float off, float scale, const int width, const int height,
const int ht, dt_bauhaus_widget_t *w)
{
// pos is normalized position [0,1], offset is on that scale.
// ht is in pixels here
const float r = slider_right_pos(width, w);
const int steps = 64;
cairo_move_to(cr, width * (pos + off) * r, ht * .7f);
cairo_line_to(cr, width * (pos + off) * r, ht);
for(int j = 1; j < steps; j++)
{
const float y = j / (steps - 1.0f);
const float x = y * y * .5f * (1.f + off / scale) + (1.0f - y * y) * (pos + off) * r;
cairo_line_to(cr, x * width, ht + y * (height - ht));
}
}
// -------------------------------
static void combobox_popup_scroll(int amt)
{
const dt_bauhaus_combobox_data_t *d = &darktable.bauhaus->current->data.combobox;
int old_value = d->active;
_combobox_next_sensitive(darktable.bauhaus->current, amt, d->mute_scrolling);
gint wx = 0, wy = 0;
const int skip = darktable.bauhaus->line_height;
GdkWindow *w = gtk_widget_get_window(darktable.bauhaus->popup_window);
gdk_window_get_origin(w, &wx, &wy);
gdk_window_move(w, wx, wy - skip * (d->active - old_value));
// make sure highlighted entry is updated:
darktable.bauhaus->mouse_x = 0;
darktable.bauhaus->mouse_y = d->active * skip + skip / 2;
gtk_widget_queue_draw(darktable.bauhaus->popup_area);
}
static void _slider_zoom_range(dt_bauhaus_widget_t *w, float zoom)
{
dt_bauhaus_slider_data_t *d = &w->data.slider;
const float value = dt_bauhaus_slider_get(GTK_WIDGET(w));
if(!zoom)
{
d->min = d->soft_min;
d->max = d->soft_max;
dt_bauhaus_slider_set(GTK_WIDGET(w), value); // restore value (and move min/max again if needed)
return;
}
// make sure current value still in zoomed range
const float min_visible = powf(10.0f, -d->digits) / d->factor;
const float multiplier = powf(2.0f, zoom/2);
const float new_min = value - multiplier * (value - d->min);
const float new_max = value + multiplier * (d->max - value);
if(new_min >= d->hard_min
&& new_max <= d->hard_max
&& new_max - new_min >= min_visible * 10)
{
d->min = new_min;
d->max = new_max;
}
gtk_widget_queue_draw(GTK_WIDGET(w));
}
static void _slider_zoom_toast(dt_bauhaus_widget_t *w)
{
dt_bauhaus_slider_data_t *d = &w->data.slider;
gchar *min_text = dt_bauhaus_slider_get_text(GTK_WIDGET(w), d->factor > 0 ? d->min : d->max);
gchar *max_text = dt_bauhaus_slider_get_text(GTK_WIDGET(w), d->factor > 0 ? d->max : d->min);
dt_action_widget_toast(w->module, GTK_WIDGET(w), "\n[%s , %s]", min_text, max_text);
g_free(min_text);
g_free(max_text);
}
static gboolean dt_bauhaus_popup_scroll(GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
{
int delta_y = 0;
if(dt_gui_get_scroll_unit_deltas(event, NULL, &delta_y))
{
if(darktable.bauhaus->current->type == DT_BAUHAUS_COMBOBOX)
combobox_popup_scroll(delta_y);
else
{
_slider_zoom_range(darktable.bauhaus->current, delta_y);
gtk_widget_queue_draw(widget);
}
}
return TRUE;
}
static gboolean dt_bauhaus_popup_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
{
gtk_widget_queue_draw(darktable.bauhaus->popup_area);
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
const GtkBorder *padding = darktable.bauhaus->popup_padding;
const int width = allocation.width - padding->left - padding->right;
const int height = allocation.height - padding->top - padding->bottom;
const int ht = darktable.bauhaus->line_height + INNER_PADDING * 2.0f;
gint wx, wy;
GdkWindow *window = gtk_widget_get_window(darktable.bauhaus->popup_window);
gdk_window_get_origin(window, &wx, &wy);
const float tol = 50;
if(event->x_root > wx + allocation.width + tol || event->y_root > wy + allocation.height + tol
|| event->x_root < (int)wx - tol || event->y_root < (int)wy - tol)
{
dt_bauhaus_widget_reject(darktable.bauhaus->current);
dt_bauhaus_hide_popup();
return TRUE;
}
const float ex = event->x_root - wx - padding->left;
const float ey = event->y_root - wy - padding->top;
if(darktable.bauhaus->keys_cnt == 0) _stop_cursor();
dt_bauhaus_widget_t *w = darktable.bauhaus->current;
switch(w->type)
{
case DT_BAUHAUS_COMBOBOX:
{
GdkRectangle workarea;
gdk_monitor_get_workarea(gdk_display_get_monitor_at_window(gdk_window_get_display(window), window), &workarea);
const gint workarea_bottom = workarea.y + workarea.height;
float dy = 0;
const float move = darktable.bauhaus->mouse_y - ey;
if(move > 0 && wy < workarea.y)
{
dy = (workarea.y - wy);
if(event->y_root >= workarea.y)
dy *= move / (darktable.bauhaus->mouse_y + wy + padding->top - workarea.y);
}
if(move < 0 && wy + allocation.height > workarea_bottom)
{
dy = (workarea_bottom - wy - allocation.height);
if(event->y_root <= workarea_bottom)
dy *= move / (darktable.bauhaus->mouse_y + wy + padding->top - workarea_bottom);
}
darktable.bauhaus->mouse_x = ex;
darktable.bauhaus->mouse_y = ey - dy;
gdk_window_move(window, wx, wy + dy);
break;
}
case DT_BAUHAUS_SLIDER:
{
const dt_bauhaus_slider_data_t *d = &w->data.slider;
const float mouse_off
= get_slider_line_offset(d->oldpos, 5.0 * powf(10.0f, -d->digits) / (d->max - d->min) / d->factor,
ex / width, ey / height, ht / (float)height, allocation.width, w);
if(!darktable.bauhaus->change_active)
{
if((darktable.bauhaus->mouse_line_distance < 0 && mouse_off >= 0)
|| (darktable.bauhaus->mouse_line_distance > 0 && mouse_off <= 0))
darktable.bauhaus->change_active = 1;
darktable.bauhaus->mouse_line_distance = mouse_off;
}
if(darktable.bauhaus->change_active)
{
// remember mouse position for motion effects in draw
darktable.bauhaus->mouse_x = ex;
darktable.bauhaus->mouse_y = ey;
dt_bauhaus_slider_set_normalized(w, d->oldpos + mouse_off);
}
break;
}
default:
break;
}
return TRUE;
}
static gboolean dt_bauhaus_popup_leave_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
{
gtk_widget_set_state_flags(widget, GTK_STATE_FLAG_NORMAL, TRUE);
return TRUE;
}
static gboolean dt_bauhaus_popup_button_release(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
int delay = 0;
g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &delay, NULL);
if(darktable.bauhaus->current && (darktable.bauhaus->current->type == DT_BAUHAUS_COMBOBOX)
&& (event->button == 1) && (event->time >= darktable.bauhaus->opentime + delay) && !darktable.bauhaus->hiding)
{
gtk_widget_set_state_flags(widget, GTK_STATE_FLAG_ACTIVE, TRUE);
// event might be in wrong system, transform ourselves:
gint wx, wy, x, y;
gdk_window_get_origin(gtk_widget_get_window(darktable.bauhaus->popup_window), &wx, &wy);
gdk_device_get_position(
gdk_seat_get_pointer(gdk_display_get_default_seat(gtk_widget_get_display(widget))), 0, &x, &y);
darktable.bauhaus->end_mouse_x = x - wx;
darktable.bauhaus->end_mouse_y = y - wy;
const dt_bauhaus_combobox_data_t *d = &darktable.bauhaus->current->data.combobox;
if(!d->mute_scrolling)
dt_bauhaus_widget_accept(darktable.bauhaus->current);
dt_bauhaus_hide_popup();
}
else if(darktable.bauhaus->hiding)
{
dt_bauhaus_hide_popup();
}
return TRUE;
}
static gboolean dt_bauhaus_popup_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
if(event->window != gtk_widget_get_window(widget))
{
dt_bauhaus_widget_reject(darktable.bauhaus->current);
dt_bauhaus_hide_popup();
return TRUE;
}
int delay = 0;
g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &delay, NULL);
if(event->button == 1)
{
if(darktable.bauhaus->current->type == DT_BAUHAUS_COMBOBOX
&& event->time < darktable.bauhaus->opentime + delay)
{
// counts as double click, reset:
const dt_bauhaus_combobox_data_t *d = &darktable.bauhaus->current->data.combobox;
dt_bauhaus_combobox_set(GTK_WIDGET(darktable.bauhaus->current), d->defpos);
dt_bauhaus_widget_reject(darktable.bauhaus->current);
gtk_widget_set_state_flags(GTK_WIDGET(darktable.bauhaus->current),
GTK_STATE_FLAG_FOCUSED, FALSE);
}
else
{
// only accept left mouse click
const GtkBorder *padding = darktable.bauhaus->popup_padding;
darktable.bauhaus->end_mouse_x = event->x - padding->left;
darktable.bauhaus->end_mouse_y = event->y - padding->top;
dt_bauhaus_widget_accept(darktable.bauhaus->current);
gtk_widget_set_state_flags(GTK_WIDGET(darktable.bauhaus->current),
GTK_STATE_FLAG_FOCUSED, FALSE);
}
darktable.bauhaus->hiding = TRUE;
}
else if(event->button == 2 && darktable.bauhaus->current->type == DT_BAUHAUS_SLIDER)
{
_slider_zoom_range(darktable.bauhaus->current, 0);
gtk_widget_queue_draw(widget);
}
else
{
dt_bauhaus_widget_reject(darktable.bauhaus->current);
darktable.bauhaus->hiding = TRUE;
}
return TRUE;
}
static void dt_bauhaus_window_show(GtkWidget *w, gpointer user_data)
{
gtk_grab_add(GTK_WIDGET(user_data));
}
static void dt_bh_init(DtBauhausWidget *class)
{
// not sure if we want to use this instead of our code in *_new()
// TODO: the common code from bauhaus_widget_init() could go here.
}
static gboolean _enter_leave(GtkWidget *widget, GdkEventCrossing *event)
{
if(event->type == GDK_ENTER_NOTIFY)
gtk_widget_set_state_flags(widget, GTK_STATE_FLAG_PRELIGHT, FALSE);
else
gtk_widget_unset_state_flags(widget, GTK_STATE_FLAG_PRELIGHT);
gtk_widget_queue_draw(widget);
return FALSE;
}
static void _widget_finalize(GObject *widget)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
if(w->type == DT_BAUHAUS_SLIDER)
{
dt_bauhaus_slider_data_t *d = &w->data.slider;
if(d->timeout_handle) g_source_remove(d->timeout_handle);
free(d->grad_col);
free(d->grad_pos);
}
else
{
dt_bauhaus_combobox_data_t *d = &w->data.combobox;
g_ptr_array_free(d->entries, TRUE);
free(d->text);
}
g_free(w->section);
G_OBJECT_CLASS(dt_bh_parent_class)->finalize(widget);
}
static void dt_bh_class_init(DtBauhausWidgetClass *class)
{
darktable.bauhaus->signals[DT_BAUHAUS_VALUE_CHANGED_SIGNAL]
= g_signal_new("value-changed", G_TYPE_FROM_CLASS(class), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
darktable.bauhaus->signals[DT_BAUHAUS_QUAD_PRESSED_SIGNAL]
= g_signal_new("quad-pressed", G_TYPE_FROM_CLASS(class), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
widget_class->draw = _widget_draw;
widget_class->scroll_event = _widget_scroll;
widget_class->key_press_event = _widget_key_press;
widget_class->get_preferred_width = _get_preferred_width;
widget_class->get_preferred_height = _get_preferred_height;
widget_class->enter_notify_event = _enter_leave;
widget_class->leave_notify_event = _enter_leave;
G_OBJECT_CLASS(class)->finalize = _widget_finalize;
}
void dt_bauhaus_load_theme()
{
darktable.bauhaus->line_height = 9;
darktable.bauhaus->marker_size = 0.25f;
GtkWidget *root_window = dt_ui_main_window(darktable.gui->ui);
GtkStyleContext *ctx = gtk_style_context_new();
GtkWidgetPath *path = gtk_widget_path_new();
const int pos = gtk_widget_path_append_type(path, GTK_TYPE_WIDGET);
gtk_widget_path_iter_add_class(path, pos, "dt_bauhaus");
gtk_style_context_set_path(ctx, path);
gtk_style_context_set_screen (ctx, gtk_widget_get_screen(root_window));
gtk_style_context_lookup_color(ctx, "bauhaus_fg", &darktable.bauhaus->color_fg);
gtk_style_context_lookup_color(ctx, "bauhaus_fg_insensitive", &darktable.bauhaus->color_fg_insensitive);
gtk_style_context_lookup_color(ctx, "bauhaus_bg", &darktable.bauhaus->color_bg);
gtk_style_context_lookup_color(ctx, "bauhaus_border", &darktable.bauhaus->color_border);
gtk_style_context_lookup_color(ctx, "bauhaus_fill", &darktable.bauhaus->color_fill);
gtk_style_context_lookup_color(ctx, "bauhaus_indicator_border", &darktable.bauhaus->indicator_border);
gtk_style_context_lookup_color(ctx, "graph_bg", &darktable.bauhaus->graph_bg);
gtk_style_context_lookup_color(ctx, "graph_exterior", &darktable.bauhaus->graph_exterior);
gtk_style_context_lookup_color(ctx, "graph_border", &darktable.bauhaus->graph_border);
gtk_style_context_lookup_color(ctx, "graph_grid", &darktable.bauhaus->graph_grid);
gtk_style_context_lookup_color(ctx, "graph_fg", &darktable.bauhaus->graph_fg);
gtk_style_context_lookup_color(ctx, "graph_fg_active", &darktable.bauhaus->graph_fg_active);
gtk_style_context_lookup_color(ctx, "graph_overlay", &darktable.bauhaus->graph_overlay);
gtk_style_context_lookup_color(ctx, "inset_histogram", &darktable.bauhaus->inset_histogram);
gtk_style_context_lookup_color(ctx, "graph_red", &darktable.bauhaus->graph_colors[0]);
gtk_style_context_lookup_color(ctx, "graph_green", &darktable.bauhaus->graph_colors[1]);
gtk_style_context_lookup_color(ctx, "graph_blue", &darktable.bauhaus->graph_colors[2]);
gtk_style_context_lookup_color(ctx, "colorlabel_red",
&darktable.bauhaus->colorlabels[DT_COLORLABELS_RED]);
gtk_style_context_lookup_color(ctx, "colorlabel_yellow",
&darktable.bauhaus->colorlabels[DT_COLORLABELS_YELLOW]);
gtk_style_context_lookup_color(ctx, "colorlabel_green",
&darktable.bauhaus->colorlabels[DT_COLORLABELS_GREEN]);
gtk_style_context_lookup_color(ctx, "colorlabel_blue",
&darktable.bauhaus->colorlabels[DT_COLORLABELS_BLUE]);
gtk_style_context_lookup_color(ctx, "colorlabel_purple",
&darktable.bauhaus->colorlabels[DT_COLORLABELS_PURPLE]);
PangoFontDescription *pfont = 0;
gtk_style_context_get(ctx, GTK_STATE_FLAG_NORMAL, "font", &pfont, NULL);
// make sure we release previously loaded font
if(darktable.bauhaus->pango_font_desc)
pango_font_description_free(darktable.bauhaus->pango_font_desc);
darktable.bauhaus->pango_font_desc = pfont;
if(darktable.bauhaus->pango_sec_font_desc)
pango_font_description_free(darktable.bauhaus->pango_sec_font_desc);
// now get the font for the section labels
gtk_widget_path_iter_add_class(path, pos, "dt_section_label");
gtk_style_context_set_path(ctx, path);
gtk_style_context_get(ctx, GTK_STATE_FLAG_NORMAL, "font", &pfont, NULL);
darktable.bauhaus->pango_sec_font_desc = pfont;
gtk_widget_path_free(path);
cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 128, 128);
cairo_t *cr = cairo_create(cst);
PangoLayout *layout = pango_cairo_create_layout(cr);
pango_layout_set_text(layout, "m", -1);
pango_layout_set_font_description(layout, darktable.bauhaus->pango_font_desc);
pango_cairo_context_set_resolution(pango_layout_get_context(layout), darktable.gui->dpi);
int pango_width, pango_height;
pango_layout_get_size(layout, &pango_width, &pango_height);
g_object_unref(layout);
cairo_destroy(cr);
cairo_surface_destroy(cst);
darktable.bauhaus->line_height = pango_height / PANGO_SCALE;
darktable.bauhaus->quad_width = darktable.bauhaus->line_height;
darktable.bauhaus->baseline_size = darktable.bauhaus->line_height / 2.5f; // absolute size in Cairo unit
darktable.bauhaus->border_width = 2.0f; // absolute size in Cairo unit
darktable.bauhaus->marker_size = (darktable.bauhaus->baseline_size + darktable.bauhaus->border_width) * 0.9f;
}
void dt_bauhaus_init()
{
darktable.bauhaus = (dt_bauhaus_t *)calloc(1, sizeof(dt_bauhaus_t));
darktable.bauhaus->keys_cnt = 0;
darktable.bauhaus->current = NULL;
darktable.bauhaus->popup_area = gtk_drawing_area_new();
darktable.bauhaus->pango_font_desc = NULL;
dt_bauhaus_load_theme();
darktable.bauhaus->skip_accel = 1;
// this easily gets keyboard input:
// darktable.bauhaus->popup_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
// but this doesn't flicker, and the above hack with key input seems to work well.
darktable.bauhaus->popup_window = gtk_window_new(GTK_WINDOW_POPUP);
#ifdef GDK_WINDOWING_QUARTZ
dt_osx_disallow_fullscreen(darktable.bauhaus->popup_window);
#endif
// this is needed for popup, not for toplevel.
// since popup_area gets the focus if we show the window, this is all
// we need.
gtk_window_set_resizable(GTK_WINDOW(darktable.bauhaus->popup_window), FALSE);
gtk_window_set_default_size(GTK_WINDOW(darktable.bauhaus->popup_window), 260, 260);
gtk_window_set_transient_for(GTK_WINDOW(darktable.bauhaus->popup_window),
GTK_WINDOW(dt_ui_main_window(darktable.gui->ui)));
// gtk_window_set_modal(GTK_WINDOW(c->popup_window), TRUE);
// gtk_window_set_decorated(GTK_WINDOW(c->popup_window), FALSE);
// for pie menu:
// gtk_window_set_position(GTK_WINDOW(c->popup_window), GTK_WIN_POS_MOUSE);// | GTK_WIN_POS_CENTER);
// needed on macOS to avoid fullscreening the popup with newer GTK
gtk_window_set_type_hint(GTK_WINDOW(darktable.bauhaus->popup_window), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
gtk_container_add(GTK_CONTAINER(darktable.bauhaus->popup_window), darktable.bauhaus->popup_area);
gtk_widget_set_hexpand(darktable.bauhaus->popup_area, TRUE);
gtk_widget_set_vexpand(darktable.bauhaus->popup_area, TRUE);
// gtk_window_set_title(GTK_WINDOW(c->popup_window), _("dtgtk control popup"));
gtk_window_set_keep_above(GTK_WINDOW(darktable.bauhaus->popup_window), TRUE);
gtk_window_set_gravity(GTK_WINDOW(darktable.bauhaus->popup_window), GDK_GRAVITY_STATIC);
gtk_widget_set_can_focus(darktable.bauhaus->popup_area, TRUE);
gtk_widget_add_events(darktable.bauhaus->popup_area, GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
| GDK_KEY_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK
| darktable.gui->scroll_mask);
GObject *window = G_OBJECT(darktable.bauhaus->popup_window), *area = G_OBJECT(darktable.bauhaus->popup_area);
g_signal_connect(window, "show", G_CALLBACK(dt_bauhaus_window_show), area);
g_signal_connect(area, "draw", G_CALLBACK(dt_bauhaus_popup_draw), NULL);
g_signal_connect(area, "motion-notify-event", G_CALLBACK(dt_bauhaus_popup_motion_notify), NULL);
g_signal_connect(area, "leave-notify-event", G_CALLBACK(dt_bauhaus_popup_leave_notify), NULL);
g_signal_connect(area, "button-press-event", G_CALLBACK(dt_bauhaus_popup_button_press), NULL);
g_signal_connect(area, "button-release-event", G_CALLBACK (dt_bauhaus_popup_button_release), NULL);
g_signal_connect(area, "key-press-event", G_CALLBACK(dt_bauhaus_popup_key_press), NULL);
g_signal_connect(area, "scroll-event", G_CALLBACK(dt_bauhaus_popup_scroll), NULL);
dt_action_define(&darktable.control->actions_focus, NULL, N_("sliders"), NULL, &_action_def_focus_slider);
dt_action_define(&darktable.control->actions_focus, NULL, N_("dropdowns"), NULL, &_action_def_focus_combo);
dt_action_define(&darktable.control->actions_focus, NULL, N_("buttons"), NULL, &_action_def_focus_button);
}
void dt_bauhaus_cleanup()
{
}
// fwd declare a few callbacks
static gboolean dt_bauhaus_slider_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
static gboolean dt_bauhaus_slider_button_release(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
static gboolean dt_bauhaus_slider_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data);
static gboolean dt_bauhaus_combobox_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
static gboolean dt_bauhaus_combobox_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data);
// static gboolean
// dt_bauhaus_button_release(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
// end static init/cleanup
// =================================================
// common initialization
static void _bauhaus_widget_init(dt_bauhaus_widget_t *w, dt_iop_module_t *self)
{
w->module = DT_ACTION(self);
w->field = NULL;
w->section = NULL;
// no quad icon and no toggle button:
w->quad_paint = 0;
w->quad_paint_data = NULL;
w->quad_toggle = 0;
w->show_quad = TRUE;
w->show_label = TRUE;
gtk_widget_add_events(GTK_WIDGET(w), GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
| GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
| GDK_FOCUS_CHANGE_MASK
| darktable.gui->scroll_mask);
gtk_widget_set_can_focus(GTK_WIDGET(w), TRUE);
dt_gui_add_class(GTK_WIDGET(w), "dt_bauhaus");
}
void dt_bauhaus_combobox_set_default(GtkWidget *widget, int def)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
dt_bauhaus_combobox_data_t *d = &w->data.combobox;
d->defpos = def;
}
int dt_bauhaus_combobox_get_default(GtkWidget *widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
const dt_bauhaus_combobox_data_t *d = &w->data.combobox;
return d->defpos;
}
void dt_bauhaus_slider_set_hard_min(GtkWidget* widget, float val)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
dt_bauhaus_slider_data_t *d = &w->data.slider;
float pos = dt_bauhaus_slider_get(widget);
d->hard_min = val;
d->min = MAX(d->min, d->hard_min);
d->soft_min = MAX(d->soft_min, d->hard_min);
if(val > d->hard_max) dt_bauhaus_slider_set_hard_max(widget,val);
if(pos < val)
{
dt_bauhaus_slider_set(widget,val);
}
else
{
dt_bauhaus_slider_set(widget,pos);
}
}
float dt_bauhaus_slider_get_hard_min(GtkWidget* widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
const dt_bauhaus_slider_data_t *d = &w->data.slider;
return d->hard_min;
}
void dt_bauhaus_slider_set_hard_max(GtkWidget* widget, float val)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
dt_bauhaus_slider_data_t *d = &w->data.slider;
float pos = dt_bauhaus_slider_get(widget);
d->hard_max = val;
d->max = MIN(d->max, d->hard_max);
d->soft_max = MIN(d->soft_max, d->hard_max);
if(val < d->hard_min) dt_bauhaus_slider_set_hard_min(widget,val);
if(pos > val)
{
dt_bauhaus_slider_set(widget,val);
}
else
{
dt_bauhaus_slider_set(widget,pos);
}
}
float dt_bauhaus_slider_get_hard_max(GtkWidget* widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
const dt_bauhaus_slider_data_t *d = &w->data.slider;
return d->hard_max;
}
void dt_bauhaus_slider_set_soft_min(GtkWidget* widget, float val)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
dt_bauhaus_slider_data_t *d = &w->data.slider;
float oldval = dt_bauhaus_slider_get(widget);
d->min = d->soft_min = CLAMP(val,d->hard_min,d->hard_max);
dt_bauhaus_slider_set(widget,oldval);
}
float dt_bauhaus_slider_get_soft_min(GtkWidget* widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
const dt_bauhaus_slider_data_t *d = &w->data.slider;
return d->soft_min;
}
void dt_bauhaus_slider_set_soft_max(GtkWidget* widget, float val)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
dt_bauhaus_slider_data_t *d = &w->data.slider;
float oldval = dt_bauhaus_slider_get(widget);
d->max = d->soft_max = CLAMP(val,d->hard_min,d->hard_max);
dt_bauhaus_slider_set(widget,oldval);
}
float dt_bauhaus_slider_get_soft_max(GtkWidget* widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
const dt_bauhaus_slider_data_t *d = &w->data.slider;
return d->soft_max;
}
void dt_bauhaus_slider_set_default(GtkWidget *widget, float def)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
dt_bauhaus_slider_data_t *d = &w->data.slider;
d->defpos = def;
}
void dt_bauhaus_slider_set_soft_range(GtkWidget *widget, float soft_min, float soft_max)
{
dt_bauhaus_slider_set_soft_min(widget,soft_min);
dt_bauhaus_slider_set_soft_max(widget,soft_max);
}
float dt_bauhaus_slider_get_default(GtkWidget *widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
const dt_bauhaus_slider_data_t *d = &w->data.slider;
return d->defpos;
}
dt_action_t *dt_bauhaus_widget_set_label(GtkWidget *widget, const char *section, const char *label)
{
dt_action_t *ac = NULL;
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
memset(w->label, 0, sizeof(w->label)); // keep valgrind happy
if(label) g_strlcpy(w->label, Q_(label), sizeof(w->label));
if(section) w->section = g_strdup(Q_(section));
if(w->module)
{
if(!darktable.bauhaus->skip_accel || w->module->type != DT_ACTION_TYPE_IOP_INSTANCE)
{
ac = dt_action_define(w->module, section, label, widget,
w->type == DT_BAUHAUS_SLIDER ? &_action_def_slider : &_action_def_combo);
if(w->module->type != DT_ACTION_TYPE_IOP_INSTANCE) w->module = ac;
}
// if new bauhaus widget added to front of widget_list; move it to the back
dt_iop_module_t *m = (dt_iop_module_t *)w->module;
if(w->module->type == DT_ACTION_TYPE_IOP_INSTANCE &&
w->field && m->widget_list && ((dt_action_target_t *)m->widget_list->data)->target == (gpointer)widget)
{
if(!m->widget_list_bh)
{
m->widget_list_bh = m->widget_list;
if(m->widget_list->next)
{
GSList *last = g_slist_last(m->widget_list);
last->next = m->widget_list;
m->widget_list = m->widget_list->next;
last->next->next = NULL;
}
}
else
{
GSList *first = m->widget_list->next;
m->widget_list->next = m->widget_list_bh->next;
m->widget_list_bh->next = m->widget_list;
m->widget_list = first;
}
}
gtk_widget_queue_draw(GTK_WIDGET(w));
}
return ac;
}
const char* dt_bauhaus_widget_get_label(GtkWidget *widget)
{
const dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
return w->label;
}
void dt_bauhaus_widget_set_quad_paint(GtkWidget *widget, dt_bauhaus_quad_paint_f f, int paint_flags, void *paint_data)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
w->quad_paint = f;
w->quad_paint_flags = paint_flags;
w->quad_paint_data = paint_data;
}
void dt_bauhaus_widget_set_field(GtkWidget *widget, gpointer field, dt_introspection_type_t field_type)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
if(*w->label)
fprintf(stderr, "[dt_bauhaus_widget_set_field] bauhaus label '%s' set before field (needs to be after)\n", w->label);