Blender V5.3
interface_handlers.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cctype>
11#include <cfloat>
12#include <cmath>
13#include <cstdlib>
14#include <cstring>
15#include <optional>
16#include <variant>
17
18#include "MEM_guardedalloc.h"
19
20#include "DNA_brush_types.h"
22#include "DNA_scene_types.h"
23#include "DNA_screen_types.h"
24
25#include "BLI_array.hh"
26#include "BLI_array_utils_c.hh"
27#include "BLI_linklist.hh"
28#include "BLI_listbase.hh"
29#include "BLI_math_geom_c.hh"
30#include "BLI_rect.hh"
31#include "BLI_sort_utils.hh"
32#include "BLI_string.hh"
34#include "BLI_string_utf8.hh"
35#include "BLI_time.hh"
36#include "BLI_utildefines.hh"
37
38#include "BKE_animsys.hh"
39#include "BKE_blender_undo.hh"
40#include "BKE_brush.hh"
41#include "BKE_colorband.hh"
42#include "BKE_colortools.hh"
43#include "BKE_context.hh"
44#include "BKE_curveprofile.h"
45#include "BKE_global.hh"
46#include "BKE_main.hh"
47#include "BKE_movieclip.hh"
48#include "BKE_paint.hh"
49#include "BKE_paint_types.hh"
50#include "BKE_report.hh"
51#include "BKE_scene.hh"
52#include "BKE_screen.hh"
53#include "BKE_tracking.hh"
54#include "BKE_unit.hh"
55
56#include "BLT_translation.hh"
57
58#include "GHOST_ISystem.hh"
59
61
62#include "ED_numinput.hh"
63#include "ED_screen.hh"
64#include "ED_undo.hh"
65
66#include "UI_abstract_view.hh"
67#include "UI_interface.hh"
68#include "UI_interface_c.hh"
69#include "UI_string_search.hh"
70
71#include "BLF_api.hh"
72
73#include "BPY_extern_run.hh"
74
76#include "interface_intern.hh"
77
78#include "RNA_access.hh"
79#include "RNA_prototypes.hh"
80
81#include "CLG_log.h"
82
83#include "WM_api.hh"
84#include "WM_types.hh"
85#include "wm_event_system.hh"
86
87namespace blender::ui {
88
89static CLG_LogRef LOG = {"ui.handler"};
90
91/* -------------------------------------------------------------------- */
99
101#define USE_CONT_MOUSE_CORRECT
103#define USE_DRAG_TOGGLE
104
106#define USE_DRAG_MULTINUM
107
109#define USE_ALLSELECT
110
115#define USE_KEYNAV_LIMIT
116
118#define USE_DRAG_POPUP
119
121
122/* -------------------------------------------------------------------- */
125
136#define UI_PROP_SCALE_LOG_MIN 0.5e-8f
142#define UI_PROP_SCALE_LOG_SNAP_OFFSET 0.03f
143
155#define UI_DRAG_MAP_SOFT_RANGE_PIXEL_MAX 1000
156
158
159/* -------------------------------------------------------------------- */
162
163struct BlockInteraction_Handle;
164struct TextEdit;
165
166static int do_but_EXIT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event);
167static bool but_find_select_in_enum__cmp(const Button *but_a, const Button *but_b);
168static void textedit_string_set(Button *but, TextEdit &text_edit, const char *str);
169static void button_tooltip_timer_reset(bContext *C, Button *but);
170
171static void block_interaction_begin_ensure(bContext *C,
172 Block *block,
173 HandleButtonData *data,
174 const bool is_click);
175static BlockInteraction_Handle *block_interaction_begin(bContext *C,
176 Block *block,
177 const bool is_click);
178static void block_interaction_end(bContext *C,
179 BlockInteraction_CallbackData *callbacks,
180 BlockInteraction_Handle *interaction);
181static void block_interaction_update(bContext *C,
182 BlockInteraction_CallbackData *callbacks,
183 BlockInteraction_Handle *interaction);
184
185#ifdef USE_KEYNAV_LIMIT
186static void mouse_motion_keynav_init(KeyNavLock *keynav, const wmEvent *event);
187static bool mouse_motion_keynav_test(KeyNavLock *keynav, const wmEvent *event);
188#endif
189
190static void with_but_active_as_semi_modal(bContext *C,
191 ARegion *region,
192 Button *but,
193 FunctionRef<void()> fn);
194static int handle_region_semi_modal_buttons(bContext *C, const wmEvent *event, ARegion *region);
195
197
198/* -------------------------------------------------------------------- */
201
202#define BUTTON_FLASH_DELAY 0.020
203#define MENU_SCROLL_INTERVAL 0.1
204#define PIE_MENU_INTERVAL 0.01
205#define BUTTON_AUTO_OPEN_THRESH 0.2
206#define BUTTON_MOUSE_TOWARDS_THRESH 1.0
208#define BUTTON_KEYNAV_PX_LIMIT 8
209
211#define MENU_TOWARDS_MARGIN 20
213#define MENU_TOWARDS_WIGGLE_ROOM 64
214
215static constexpr double menu_keep_open_duration = 0.4f;
216
224
250
257
270
271#ifdef USE_ALLSELECT
272
273/* Unfortunately there's no good way handle more generally:
274 * (propagate single clicks on layer buttons to other objects) */
275# define USE_ALLSELECT_LAYER_HACK
276
279 union {
280 bool val_b;
281 int val_i;
282 float val_f;
283 };
284};
285
288 bool do_free = false;
289 bool is_enabled = false;
290 /* When set, simply copy values (don't apply difference).
291 * Rules are:
292 * - dragging numbers uses delta.
293 * - typing in values will assign to all. */
294 bool is_copy = false;
295};
296
297static bool selectcontext_begin(bContext *C, Button *but, uiSelectContextStore *selctx_data);
298static void selectcontext_end(Button *but, uiSelectContextStore *selctx_data);
299static void selectcontext_apply(bContext *C,
300 Button *but,
301 uiSelectContextStore *selctx_data,
302 const double value,
303 const double value_orig);
304
311# define IS_ALLSELECT_EVENT(event) (((event)->modifier & KM_ALT) != 0)
312
314# define BUT_IS_SELECT_CONTEXT BUT_NODE_ACTIVE
315
316#endif /* USE_ALLSELECT */
317
318#ifdef USE_DRAG_MULTINUM
319
323# define DRAG_MULTINUM_THRESHOLD_DRAG_X (UI_UNIT_Y / 4)
324
330# define DRAG_MULTINUM_THRESHOLD_DRAG_Y (UI_UNIT_Y / 4)
331
340# define DRAG_MULTINUM_THRESHOLD_VERTICAL (0.75f)
341
342/* a simple version of HandleButtonData when accessing multiple buttons */
344 double origvalue = 0.0f;
345 Button *but = nullptr;
346
347# ifdef USE_ALLSELECT
349# endif
350};
351
353 enum {
363
364 bool has_mbuts = false; /* any buttons flagged BUT_DRAG_MULTI */
365 LinkNode *mbuts = nullptr;
366 ButStore *bs_mbuts = nullptr;
367
368 bool is_proportional = false;
369
370 /* In some cases we directly apply the changes to multiple buttons,
371 * so we don't want to do it twice. */
372 bool skip = false;
373
374 /* before activating, we need to check gesture direction accumulate signed cursor movement
375 * here so we can tell if this is a vertical motion or not. */
376 float drag_dir[2] = {0.0f, 0.0f};
377
378 /* Previous mouse position for accumulating drag_dir. */
379 int drag_dir_prev[2] = {0, 0};
380
381 /* values copied direct from event->xy
382 * used to detect buttons between the current and initial mouse position */
383 int drag_start[2] = {0, 0};
384
385 /* store x location once INIT_SETUP is set,
386 * moving outside this sets INIT_ENABLE */
387 int drag_lock_x = 0;
388};
389
390#endif /* USE_DRAG_MULTINUM */
391
395struct TextEdit {
397 char *edit_string = nullptr;
398 /* Maximum string size the button accepts, and as such the maximum size for #edit_string
399 * (including terminator). */
401 /* Allow reallocating #edit_string and using #max_string_size to track alloc size (maxlen + 1) */
402 bool is_str_dynamic = false;
403 char *original_string = nullptr;
404
405 /* Button text selection:
406 * extension direction, selextend, inside do_but_TEX */
408
409 /* Text field undo. */
411};
412
414 wmWindowManager *wm = nullptr;
415 wmWindow *window = nullptr;
416 ScrArea *area = nullptr;
417 ARegion *region = nullptr;
418
419 bool interactive = false;
420
421 /* overall state */
423 int retval = 0;
424 /* booleans (could be made into flags) */
425 bool cancel = false;
426 bool escapecancel = false;
427 bool applied = false;
429 /* Button is being applied through an extra icon. */
431 bool changed_cursor = false;
433 wmTimer *flashtimer = nullptr;
434
438
440 double text_select_auto_scroll_last_time = std::numeric_limits<double>::lowest();
441
442 double value = 0.0f;
443 double origvalue = 0.0f;
444 double startvalue = 0.0f;
445 float vec[3], origvec[3];
446 ColorBand *coba = nullptr;
447
448 /* True when alt is held and the preference for displaying tooltips should be ignored. */
449 bool tooltip_force = false;
455 bool disable_force = false;
456
462 bool is_semi_modal = false;
463
464 /* auto open */
465 bool used_mouse = false;
467
468 /* auto open (hold) */
470
471 /* number editing / dragging */
472 /* coords are Window/Block relative (depends on the button) */
473 int draglastx = 0;
474 int draglasty = 0;
475 int dragstartx = 0;
476 int dragstarty = 0;
477 float2 relative_drag = {0.0f, 0.0f};
478 bool dragchange = false;
479 bool draglock = false;
480 int dragsel = 0;
481 float dragf = 0.0f;
482 float dragfstart = 0.0f;
483 CBData *dragcbd = nullptr;
484
486 float drag_map_soft_min = 0.0f;
487 float drag_map_soft_max = 0.0f;
488
489#ifdef USE_CONT_MOUSE_CORRECT
490 /* when ungrabbing buttons which are #but_is_cursor_warp(),
491 * we may want to position them.
492 * FLT_MAX signifies do-nothing, use #block_to_window_fl()
493 * to get this into a usable space. */
494 float ungrab_mval[2];
495#endif
496
497 /* Menu open, see: #UI_screen_free_active_but_highlight. */
499
500 /* Search box see: #UI_screen_free_active_but_highlight. */
501 ARegion *searchbox = nullptr;
502#ifdef USE_KEYNAV_LIMIT
504#endif
505
506#ifdef USE_DRAG_MULTINUM
507 /* Multi-buttons will be updated in unison with the active button. */
509#endif
510
511#ifdef USE_ALLSELECT
513#endif
514
516
517 /* post activate */
519 Button *postbut = nullptr;
520};
521
568
569static void button_activate_init(bContext *C,
570 ARegion *region,
571 Button *but,
572 ButtonActivateType type);
574static void button_activate_exit(
575 bContext *C, Button *but, HandleButtonData *data, const bool mousemove, const bool onfree);
576static int handler_region_menu(bContext *C, const wmEvent *event, void *userdata);
578 ARegion *region,
579 Button *but,
580 ButtonActivateType type);
582 Button *but,
584 const wmEvent *event);
587 const wmEvent *event);
589
590#ifdef USE_DRAG_MULTINUM
591static void multibut_restore(bContext *C, HandleButtonData *data, Block *block);
593#endif
594
595/* buttons clipboard */
598static bool but_copypaste_curve_alive = false;
600static bool but_copypaste_profile_alive = false;
601
603
604/* -------------------------------------------------------------------- */
607
609{
610 MEM_delete(*data);
611 *data = nullptr;
612}
613
615
616/* -------------------------------------------------------------------- */
619
620bool button_is_editing(const Button *but)
621{
622 const HandleButtonData *data = but->active;
624}
625
626void pan_to_scroll(const wmEvent *event, int *type, int *val)
627{
628 static int lastdy = 0;
629 const int dy = WM_event_absolute_delta_y(event);
630
631 /* This event should be originally from event->type,
632 * converting wrong event into wheel is bad, see #33803. */
633 BLI_assert(*type == MOUSEPAN);
634
635 /* sign differs, reset */
636 if ((dy > 0 && lastdy < 0) || (dy < 0 && lastdy > 0)) {
637 lastdy = dy;
638 }
639 else {
640 lastdy += dy;
641
642 if (abs(lastdy) > int(UI_UNIT_Y)) {
643 *val = KM_PRESS;
644
645 if (dy > 0) {
646 *type = WHEELUPMOUSE;
647 }
648 else {
649 *type = WHEELDOWNMOUSE;
650 }
651
652 lastdy = 0;
653 }
654 }
655}
656
657static bool but_find_select_in_enum__cmp(const Button *but_a, const Button *but_b)
658{
659 return ((but_a->type == but_b->type) && (but_a->alignnr == but_b->alignnr) &&
660 (but_a->poin == but_b->poin) && (but_a->rnapoin.type == but_b->rnapoin.type) &&
661 (but_a->rnaprop == but_b->rnaprop));
662}
663
665{
666 Block *block = but->block;
667 int i = block->but_index(but);
668 Button *but_found = nullptr;
669 BLI_assert(ELEM(direction, -1, 1));
670
671 while (i > 0 && but_find_select_in_enum__cmp(block->buttons_ptrs[i - 1].get(), but)) {
672 i--;
673 }
674
675 while (i < block->buttons_ptrs.size() &&
676 but_find_select_in_enum__cmp(block->buttons_ptrs[i].get(), but))
677 {
678 if (block->buttons_ptrs[i]->flag & UI_SELECT) {
679 but_found = block->buttons_ptrs[i].get();
680 if (direction == 1) {
681 break;
682 }
683 }
684 i++;
685 }
686
687 return but_found;
688}
689
690static float mouse_scale_warp_factor(const bool shift)
691{
692 return shift ? 0.05f : 1.0f;
693}
694
696 const float mx,
697 const float my,
698 float *r_mx,
699 float *r_my,
700 const bool shift)
701{
702 const float fac = mouse_scale_warp_factor(shift);
703
704 /* slow down the mouse, this is fairly picky */
705 *r_mx = (data->dragstartx * (1.0f - fac) + mx * fac);
706 *r_my = (data->dragstarty * (1.0f - fac) + my * fac);
707}
708
710
711/* -------------------------------------------------------------------- */
714
715#ifdef USE_DRAG_MULTINUM
716static bool multibut_drag_wait(const HandleButtonMulti &multi_data)
717{
718 const bool initializing = ELEM(
720 const bool vertical_dragging = abs(multi_data.drag_dir[1]) > abs(multi_data.drag_dir[0]);
721
722 /* Continue waiting if we are dragging vertically but have not yet detected gesture. */
723 return (initializing && vertical_dragging);
724}
725#endif /* USE_DRAG_MULTINUM */
726
731 int mx,
732 FunctionRef<int()> drag_threshold_fn)
733{
734 if (data->draglock) {
735 const int threshold = drag_threshold_fn();
736 if (abs(mx - data->dragstartx) < threshold) {
737 return false;
738 }
739#ifdef USE_DRAG_MULTINUM
740 /* Continue to wait for multibut drag initialization if dragging vertically. */
741 if (multibut_drag_wait(data->multi_data)) {
742 return false;
743 }
744#endif
745 data->draglock = false;
746 data->dragstartx = mx; /* ignore mouse movement within drag-lock */
747 }
748
749 return true;
750}
751
752static bool rna_is_userdef(PointerRNA *ptr, PropertyRNA *prop)
753{
754 /* Not very elegant, but ensures preference changes force re-save. */
755
756 if (!prop) {
757 return false;
758 }
760 return false;
761 }
762
763 if (RNA_struct_is_a(ptr->type, RNA_ProjectAssetLibrary)) {
764 return false;
765 }
766
767 StructRNA *base = RNA_struct_base(ptr->type);
768 if (base == nullptr) {
769 base = ptr->type;
770 }
771
772 bool is_userdef = false;
773 if (ELEM(base,
774 RNA_AddonPreferences,
775 RNA_KeyConfigPreferences,
776 RNA_KeyMapItem,
777 RNA_UserAssetLibrary))
778 {
779 is_userdef = true;
780 }
781 else if (ptr->owner_id) {
782 switch (ptr->owner_id->id_type()) {
783 case ID_WM: {
784 for (const AncestorPointerRNA &ancestor : ptr->ancestors) {
785 if (RNA_struct_is_a(ancestor.type, RNA_KeyConfigPreferences)) {
786 is_userdef = true;
787 break;
788 }
789 }
790 break;
791 }
792 default: {
793 break;
794 }
795 }
796 }
797 else if (ptr->owner_id == nullptr) {
798 for (const AncestorPointerRNA &ancestor : ptr->ancestors) {
799 if (RNA_struct_is_a(ancestor.type, RNA_AddonPreferences)) {
800 is_userdef = true;
801 break;
802 }
803 }
804 }
805
806 return is_userdef;
807}
808
809bool button_is_userdef(const Button *but)
810{
811 /* This is read-only, RNA API isn't using const when it could. */
812 return rna_is_userdef(const_cast<PointerRNA *>(&but->rnapoin), but->rnaprop);
813}
814
816{
817 if (rna_is_userdef(ptr, prop)) {
818 U.runtime.is_dirty = true;
820 }
821}
822
827
832
834
835/* -------------------------------------------------------------------- */
838
844
845static eSnapType event_to_snap(const wmEvent *event)
846{
847 return (event->modifier & KM_CTRL) ? (event->modifier & KM_SHIFT) ? SNAP_ON_SMALL : SNAP_ON :
848 SNAP_OFF;
849}
850
851static bool event_is_snap(const wmEvent *event)
852{
853 return (ELEM(event->type, EVT_LEFTCTRLKEY, EVT_RIGHTCTRLKEY) ||
855}
856
857static void color_snap_hue(const eSnapType snap, float *r_hue)
858{
859 const float snap_increment = (snap == SNAP_ON_SMALL) ? 24 : 12;
861 *r_hue = roundf((*r_hue) * snap_increment) / snap_increment;
862}
863
865
866/* -------------------------------------------------------------------- */
869
870static ListBaseT<AfterFunc> UIAfterFuncs = {nullptr, nullptr};
871
873{
874 AfterFunc *after = MEM_new<AfterFunc>(__func__);
875 /* Safety asserts to check if members were 0 initialized properly. */
876 BLI_assert(after->next == nullptr && after->prev == nullptr);
877 BLI_assert(after->undostr[0] == '\0');
878
879 BLI_addtail(&UIAfterFuncs, after);
880
881 return after;
882}
883
895 PointerRNA **properties,
896 wm::OpCallContext opcontext,
897 const Button *context_but)
898{
899 AfterFunc *after = afterfunc_new();
900
901 after->optype = ot;
902 after->opcontext = opcontext;
903 if (properties) {
904 after->opptr = *properties;
905 *properties = nullptr;
906 }
907
908 if (context_but && context_but->context) {
909 after->context = *context_but->context;
910 }
911 if (context_but && context_but->block->handle) {
912 after->region_popup = context_but->block->handle->ctx_region_popup;
913 }
914
915 if (context_but) {
916 after->drawstr = button_drawstr_without_sep_char(context_but);
917 }
918}
919
921{
922 handle_afterfunc_add_operator_ex(ot, nullptr, opcontext, nullptr);
923}
924
926{
927 if (op && op->type->check) {
928 op->type->check(C, op);
929 }
930}
931
935static bool afterfunc_check(const Block *block, const Button *but)
936{
937 auto *text_button = but->type == ButtonType::Text ? static_cast<const ButtonText *>(but) :
938 nullptr;
939 return (but->func || but->apply_func || but->funcN ||
940 (text_button && (text_button->rename_func || text_button->rename_full_func)) ||
941 but->optype || but->rnaprop || block->handle_func ||
942 (block->handle && block->handle->popup_op));
943}
944
950static void apply_but_func(bContext *C, Button *but)
951{
952 Block *block = but->block;
953 if (!afterfunc_check(block, but)) {
954 return;
955 }
956
957 AfterFunc *after = afterfunc_new();
958
959 if (but->func && ELEM(but, but->func_arg1, but->func_arg2)) {
960 /* exception, this will crash due to removed button otherwise */
961 but->func(C, but->func_arg1, but->func_arg2);
962 }
963 else {
964 after->func = but->func;
965 }
966
967 after->func_arg1 = but->func_arg1;
968 after->func_arg2 = but->func_arg2;
969
970 after->apply_func = but->apply_func;
971
972 after->funcN = but->funcN;
973 after->func_argN = (but->func_argN) ? but->func_argN_copy_fn(but->func_argN) : nullptr;
975 /* but->func_argN_copy_fn is not needed for #uiAfterFunc. */
976
977 if (but->type == ButtonType::Text) {
978 auto *text_button = static_cast<ButtonText *>(but);
979
980 after->rename_func = std::move(text_button->rename_func);
981 after->rename_orig = text_button->rename_orig; /* needs free! */
982
983 after->rename_full_func = std::move(text_button->rename_full_func);
984 after->rename_full_new = std::move(text_button->rename_full_new);
985 text_button->rename_full_new = "";
986 }
987
988 after->handle_func = block->handle_func;
989 after->handle_func_arg = block->handle_func_arg;
990 after->retval = but->retval;
991
992 if (block->handle) {
993 after->popup_op = block->handle->popup_op;
994 }
995
996 if (!but->operator_never_call) {
997 after->optype = but->optype;
998 after->opcontext = but->opcontext;
999 after->opptr = but->opptr;
1000
1001 but->optype = nullptr;
1002 but->opcontext = wm::OpCallContext(0);
1003 but->opptr = nullptr;
1004 }
1005
1006 after->rnapoin = but->rnapoin;
1007 after->rnaprop = but->rnaprop;
1008
1009 if (but->type == ButtonType::SearchMenu) {
1010 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
1011 after->search_arg = search_but->arg;
1012 }
1013
1014 if (but->active != nullptr) {
1016 if (data->custom_interaction_handle != nullptr) {
1018 after->custom_interaction_handle = data->custom_interaction_handle;
1019
1020 /* Ensure this callback runs once and last. */
1021 AfterFunc *after_prev = after->prev;
1022 if (after_prev && (after_prev->custom_interaction_handle == data->custom_interaction_handle))
1023 {
1024 after_prev->custom_interaction_handle = nullptr;
1025 memset(&after_prev->custom_interaction_callbacks,
1026 0x0,
1027 sizeof(after_prev->custom_interaction_callbacks));
1028 }
1029 else {
1031 }
1032 }
1033 }
1034
1035 if (but->context) {
1036 after->context = *but->context;
1037 }
1038 if (but->block->handle) {
1039 after->region_popup = but->block->handle->ctx_region_popup;
1040 }
1041
1043}
1044
1045/* typically call apply_but_undo(), apply_but_autokey() */
1046static void apply_but_undo(Button *but, bool use_undo_grouped = false)
1047{
1048 if (!(but->flag & BUT_UNDO)) {
1049 return;
1050 }
1051
1052 /* Skip undo push for buttons in redo panel, see: #134505. */
1053 const ARegion *region = CTX_wm_region(static_cast<bContext *>(but->block->evil_C));
1054 if (region && region->regiontype == RGN_TYPE_HUD) {
1055 return;
1056 }
1057
1058 std::optional<StringRef> str;
1059 size_t str_len_clip = SIZE_MAX - 1;
1060
1061 /* define which string to use for undo */
1062 if (but->type == ButtonType::Menu) {
1063 if (!but->drawstr.empty()) {
1064 str = but->drawstr;
1065 }
1066 str_len_clip = button_drawstr_len_without_sep_char(but);
1067 }
1068 else if (!but->drawstr.empty()) {
1069 str = but->drawstr;
1070 str_len_clip = button_drawstr_len_without_sep_char(but);
1071 }
1072 else {
1073 str = but->tip;
1074 str_len_clip = button_tip_len_only_first_line(but);
1075 }
1076
1077 /* fallback, else we don't get an undo! */
1078 if (!str || str->is_empty() || str_len_clip == 0) {
1079 str = "Unknown Action";
1080 str_len_clip = str->size();
1081 }
1082
1083 /* Optionally override undo when undo system doesn't support storing properties. */
1084 std::optional<UndoEncodeHints> undo_hints_or_none = UndoEncodeHints::None;
1085 if (but->rnapoin.owner_id) {
1086 /* Exception for renaming ID data, we always need undo pushes in this case,
1087 * because undo systems track data by their ID, see: #67002. */
1088 /* Exception for active shape-key, since changing this in edit-mode updates
1089 * the shape key from object mode data. */
1090 if (ELEM(but->rnaprop, &rna_ID_name, &rna_Object_active_shape_key_index)) {
1091 /* pass */
1092 }
1093 else if (but->rnaprop) {
1094 ID *id = but->rnapoin.owner_id;
1095 undo_hints_or_none = ED_undo_is_legacy_compatible_for_property(
1096 static_cast<bContext *>(but->block->evil_C), id, but->rnapoin, *but->rnaprop);
1097 }
1098 }
1099
1100 if (undo_hints_or_none.has_value()) {
1101 /* XXX: disable all undo pushes from UI changes from sculpt mode as they cause memfile undo
1102 * steps to be written which cause lag: #71434. */
1103 if (BKE_paintmode_get_active_from_context(static_cast<bContext *>(but->block->evil_C)) ==
1105 {
1106 undo_hints_or_none = std::nullopt;
1107 }
1108 }
1109
1110 if (!undo_hints_or_none.has_value()) {
1111 str = "";
1112 }
1113
1114 /* Delayed, after all other functions run, popups are closed, etc. */
1115 AfterFunc *after = afterfunc_new();
1116 str->copy_utf8_truncated(after->undostr, min_zz(str_len_clip + 1, sizeof(after->undostr)));
1117 after->use_undo_grouped = use_undo_grouped;
1118 after->undo_hints = undo_hints_or_none.value_or(UndoEncodeHints::None);
1119}
1120
1122{
1123 Scene *scene = CTX_data_scene(C);
1124
1125 /* try autokey */
1126 button_anim_autokey(C, but, scene, BKE_scene_frame_get(scene));
1127
1128 if (!but->rnaprop) {
1129 return;
1130 }
1131
1133 return;
1134 }
1135
1136 /* make a little report about what we've done! */
1137 std::optional<const std::string> str = WM_prop_pystring_assign(
1138 C, &but->rnapoin, but->rnaprop, but->rnaindex);
1139 if (!str.has_value()) {
1140 return;
1141 }
1142 BKE_report(CTX_wm_reports(C), RPT_PROPERTY, str.value().c_str());
1144}
1145
1147{
1148 /* Copy to avoid recursive calls. */
1149 ListBaseT<AfterFunc> funcs = UIAfterFuncs;
1150 UIAfterFuncs.clear_no_delete();
1151
1152 for (AfterFunc &afterf : funcs.items_mutable()) {
1153 AfterFunc after = afterf; /* Copy to avoid memory leak on exit(). */
1154 BLI_remlink(&funcs, &afterf);
1155 MEM_delete(&afterf);
1156
1157 if (after.context) {
1158 CTX_store_set(C, &after.context.value());
1159 }
1160
1161 /* Set the popup this button's popup was opened from (a context menu in a popover)
1162 * so operators can access the popup's active button, see: #151170.
1163 * That popup may have been closed along with the menu, so check it still exists. */
1164 ARegion *region_popup_prev = nullptr;
1165 if (after.region_popup) {
1166 if (BLI_findindex(&CTX_wm_screen(C)->regionbase, after.region_popup) != -1) {
1167 region_popup_prev = CTX_wm_region_popup(C);
1169 }
1170 else {
1171 after.region_popup = nullptr;
1172 }
1173 }
1174
1175 if (after.popup_op) {
1176 popup_check(C, after.popup_op);
1177 }
1178
1179 PointerRNA opptr;
1180 if (after.opptr) {
1181 /* free in advance to avoid leak on exit */
1182 opptr = *after.opptr;
1183 MEM_delete(after.opptr);
1184 }
1185
1186 if (after.optype) {
1188 after.optype,
1189 after.opcontext,
1190 (after.opptr) ? &opptr : nullptr,
1191 nullptr,
1192 after.drawstr);
1193 }
1194
1195 if (after.opptr) {
1197 }
1198
1199 if (after.rnapoin) {
1200 RNA_property_update(C, &after.rnapoin, after.rnaprop);
1201 }
1202
1203 if (after.context) {
1204 CTX_store_set(C, nullptr);
1205 }
1206
1207 if (after.region_popup) {
1208 /* The operator may have freed the popup, for example by loading a file. */
1209 bScreen *screen = CTX_wm_screen(C);
1210 if (region_popup_prev &&
1211 !(screen && BLI_findindex(&screen->regionbase, region_popup_prev) != -1))
1212 {
1213 region_popup_prev = nullptr;
1214 }
1215 CTX_wm_region_popup_set(C, region_popup_prev);
1216 }
1217
1218 if (after.rename_full_func) {
1219 BLI_assert(!after.rename_func);
1220 after.rename_full_func(after.rename_full_new);
1221 }
1222
1223 if (after.func) {
1224 after.func(C, after.func_arg1, after.func_arg2);
1225 }
1226 if (after.apply_func) {
1227 after.apply_func(*C);
1228 }
1229 if (after.funcN) {
1230 after.funcN(C, after.func_argN, after.func_arg2);
1231 }
1232 if (after.func_argN) {
1233 after.func_argN_free_fn(after.func_argN);
1234 }
1235
1236 if (after.handle_func) {
1237 after.handle_func(C, after.handle_func_arg, after.retval);
1238 }
1239
1240 if (after.rename_func) {
1241 after.rename_func(*C, after.rename_orig);
1242 }
1243 if (after.rename_orig) {
1244 MEM_delete(after.rename_orig);
1245 }
1246
1247 if (after.custom_interaction_handle != nullptr) {
1250 if (after.custom_interaction_handle->user_count == 0) {
1255 }
1256 after.custom_interaction_handle = nullptr;
1257 }
1258
1260
1261 if (after.undostr[0]) {
1262 /* Remove "Adjust Last Operation" HUD. Using it would revert this undo push which isn't
1263 * obvious, see #78171. */
1265 if (after.use_undo_grouped) {
1267 }
1268 else {
1269 ED_undo_push(C, after.undostr, after.undo_hints);
1270 }
1271 }
1272 }
1273}
1274
1276{
1277 apply_but_func(C, but);
1278
1279 data->retval = but->retval;
1280 data->applied = true;
1281}
1282
1284{
1285 button_value_set(but, double(but->retval));
1286 apply_but_func(C, but);
1287
1288 data->retval = but->retval;
1289 data->applied = true;
1290}
1291
1293{
1294 if (but->type == ButtonType::Menu) {
1295 button_value_set(but, data->value);
1296 }
1297
1299 apply_but_func(C, but);
1300 data->retval = but->retval;
1301 data->applied = true;
1302}
1303
1305{
1306 const double value = button_value_get(but);
1307 int value_toggle;
1308 if (but->bit) {
1309 value_toggle = UI_BITBUT_VALUE_TOGGLED(int(value), but->bitnr);
1310 }
1311 else {
1312 value_toggle = (value == 0.0);
1313 }
1314
1315 button_value_set(but, double(value_toggle));
1318 }
1319
1320 apply_but_func(C, but);
1321
1322 data->retval = but->retval;
1323 data->applied = true;
1324}
1325
1327{
1328 button_value_set(but, but->hardmax);
1329
1330 apply_but_func(C, but);
1331
1332 /* states of other row buttons */
1333 for (Button &bt : block->buttons()) {
1334 if (&bt != but && bt.poin == but->poin && ELEM(bt.type, ButtonType::Row, ButtonType::ListRow))
1335 {
1337 }
1338 }
1339
1340 data->retval = but->retval;
1341 data->applied = true;
1342}
1343
1345{
1346 if (data->apply_through_extra_icon) {
1347 /* Don't apply this, it would cause unintended tree-row toggling when clicking on extra icons.
1348 */
1349 return;
1350 }
1351 apply_but_ROW(C, block, but, data);
1352}
1353
1355{
1356 if (!data->text_edit.edit_string) {
1357 return;
1358 }
1359
1360 button_string_set(C, but, data->text_edit.edit_string);
1362
1363 ButtonText *text_button = but->type == ButtonType::Text ? static_cast<ButtonText *>(but) :
1364 nullptr;
1365 /* only if there are afterfuncs, otherwise 'rename_orig' isn't freed */
1366 if (text_button && afterfunc_check(but->block, but)) {
1367 /* give butfunc a copy of the original text too.
1368 * feature used for bone renaming, channels, etc.
1369 * afterfunc frees rename_orig */
1370 if (data->text_edit.original_string && (but->flag & BUT_TEXTEDIT_UPDATE)) {
1371 /* In this case, we need to keep `original_string` available,
1372 * to restore real org string in case we cancel after having typed something already. */
1373 text_button->rename_orig = BLI_strdup(data->text_edit.original_string);
1374 }
1375 else {
1376 text_button->rename_orig = data->text_edit.original_string;
1377 data->text_edit.original_string = nullptr;
1378 }
1379 }
1380
1381 void *orig_arg2 = but->func_arg2;
1382
1383 /* If arg2 isn't in use already, pass the active search item through it. */
1384 if ((but->func_arg2 == nullptr) && (but->type == ButtonType::SearchMenu)) {
1385 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
1386 but->func_arg2 = search_but->item_active;
1387 if ((U.flag & USER_FLAG_RECENT_SEARCHES_DISABLE) == 0) {
1389 }
1390 }
1391
1392 apply_but_func(C, but);
1393
1394 but->func_arg2 = orig_arg2;
1395
1396 data->retval = but->retval;
1397 data->applied = true;
1398}
1399
1401{
1402 if (data->text_edit.edit_string) {
1403 button_string_set(C, but, data->text_edit.edit_string);
1405 }
1406 else {
1407 button_value_set(but, but->hardmax);
1408 apply_but_func(C, but);
1409 }
1410
1411 data->retval = but->retval;
1412 data->applied = true;
1413}
1414
1416{
1417 if (data->text_edit.edit_string) {
1418 /* This is intended to avoid unnecessary updates when the value stays the same, however there
1419 * are issues with the current implementation. It does not work with multi-button editing
1420 * (#89996) or operator popups where a number button requires an update even if the value is
1421 * unchanged (#89996).
1422 *
1423 * Trying to detect changes at this level is not reliable. Instead it could be done at the
1424 * level of RNA update/set, skipping RNA update if RNA set did not change anything, instead
1425 * of skipping all button updates. */
1426#if 0
1427 double value;
1428 /* Check if the string value is a number and cancel if it's equal to the startvalue. */
1429 if (button_string_eval_number(C, but, data->str, &value) && (value == data->startvalue)) {
1430 data->cancel = true;
1431 return;
1432 }
1433#endif
1434
1435 if (button_string_set(C, but, data->text_edit.edit_string)) {
1436 data->value = button_value_get(but);
1437 }
1438 else {
1439 data->cancel = true;
1440 return;
1441 }
1442 }
1443 else {
1444 button_value_set(but, data->value);
1445 }
1446
1448 apply_but_func(C, but);
1449
1450 data->retval = but->retval;
1451 data->applied = true;
1452}
1453
1455{
1456 button_v3_set(but, data->vec);
1458 apply_but_func(C, but);
1459
1460 data->retval = but->retval;
1461 data->applied = true;
1462}
1463
1465{
1466 apply_but_func(C, but);
1467 data->retval = but->retval;
1468 data->applied = true;
1469}
1470
1472{
1473 apply_but_func(C, but);
1474 data->retval = but->retval;
1475 data->applied = true;
1476}
1477
1479{
1480 apply_but_func(C, but);
1481 data->retval = but->retval;
1482 data->applied = true;
1483}
1484
1486
1487/* -------------------------------------------------------------------- */
1490
1491#ifdef USE_DRAG_MULTINUM
1492
1493/* Small multi-but API. */
1495{
1497 BLI_assert(data->multi_data.has_mbuts);
1498
1499 ButtonMultiState *mbut_state = MEM_new<ButtonMultiState>(__func__);
1500 mbut_state->but = but;
1501 mbut_state->origvalue = button_value_get(but);
1502# ifdef USE_ALLSELECT
1503 mbut_state->select_others.is_copy = data->select_others.is_copy;
1504# endif
1505
1506 BLI_linklist_prepend(&data->multi_data.mbuts, mbut_state);
1507
1508 butstore_register(data->multi_data.bs_mbuts, &mbut_state->but);
1509}
1510
1512{
1513 for (LinkNode *l = data->multi_data.mbuts; l; l = l->next) {
1514 ButtonMultiState *mbut_state = static_cast<ButtonMultiState *>(l->link);
1515
1516 if (mbut_state->but == but) {
1517 return mbut_state;
1518 }
1519 }
1520
1521 return nullptr;
1522}
1523
1525{
1526 for (Button &but : block->buttons()) {
1527 if (but.flag & BUT_DRAG_MULTI) {
1528 ButtonMultiState *mbut_state = multibut_lookup(data, &but);
1529 if (mbut_state) {
1530 button_value_set(&but, mbut_state->origvalue);
1531
1532# ifdef USE_ALLSELECT
1533 if (!mbut_state->select_others.elems.is_empty()) {
1535 C, &but, &mbut_state->select_others, mbut_state->origvalue, mbut_state->origvalue);
1536 }
1537# else
1538 UNUSED_VARS(C);
1539# endif
1540 }
1541 }
1542 }
1543}
1544
1546{
1547# ifdef USE_ALLSELECT
1548 if (data->multi_data.mbuts) {
1549 LinkNode *list = data->multi_data.mbuts;
1550 while (list) {
1551 LinkNode *next = list->next;
1552 ButtonMultiState *mbut_state = static_cast<ButtonMultiState *>(list->link);
1553 MEM_delete(mbut_state);
1554 MEM_delete(list);
1555 list = next;
1556 }
1557 }
1558# else
1559 BLI_linklist_freeN(data->multi_data.mbuts);
1560# endif
1561
1562 data->multi_data.mbuts = nullptr;
1563
1564 if (data->multi_data.bs_mbuts) {
1565 butstore_free(block, data->multi_data.bs_mbuts);
1566 data->multi_data.bs_mbuts = nullptr;
1567 }
1568}
1569
1570static bool multibut_states_tag(Button *but_active, HandleButtonData *data, const wmEvent *event)
1571{
1572 float seg[2][2];
1573 bool changed = false;
1574
1575 seg[0][0] = data->multi_data.drag_start[0];
1576 seg[0][1] = data->multi_data.drag_start[1];
1577
1578 seg[1][0] = event->xy[0];
1579 seg[1][1] = event->xy[1];
1580
1581 BLI_assert(data->multi_data.init == HandleButtonMulti::INIT_SETUP);
1582
1583 window_to_block_fl(data->region, but_active->block, &seg[0][0], &seg[0][1]);
1584 window_to_block_fl(data->region, but_active->block, &seg[1][0], &seg[1][1]);
1585
1586 data->multi_data.has_mbuts = false;
1587
1588 /* follow but_find_mouse_over_ex logic */
1589 for (Button &but : but_active->block->buttons()) {
1590 bool drag_prev = false;
1591 bool drag_curr = false;
1592
1593 /* re-set each time */
1594 if (but.flag & BUT_DRAG_MULTI) {
1595 but.flag &= ~BUT_DRAG_MULTI;
1596 drag_prev = true;
1597 }
1598
1599 if (button_is_interactive(&but, false)) {
1600
1601 /* drag checks */
1602 if (but_active != &but) {
1603 if (button_is_compatible(but_active, &but)) {
1604
1605 BLI_assert(but.active == nullptr);
1606
1607 /* finally check for overlap */
1608 if (BLI_rctf_isect_segment(&but.rect, seg[0], seg[1])) {
1609
1610 but.flag |= BUT_DRAG_MULTI;
1611 data->multi_data.has_mbuts = true;
1612 drag_curr = true;
1613 }
1614 }
1615 }
1616 }
1617
1618 changed |= (drag_prev != drag_curr);
1619 }
1620
1621 return changed;
1622}
1623
1625{
1626 BLI_assert(data->multi_data.init == HandleButtonMulti::INIT_SETUP);
1627 BLI_assert(data->multi_data.has_mbuts);
1628
1629 data->multi_data.bs_mbuts = butstore_create(but_active->block);
1630
1631 for (Button &but : but_active->block->buttons()) {
1632 if (but.flag & BUT_DRAG_MULTI) {
1633 multibut_add(data, &but);
1634 }
1635 }
1636
1637 /* Edit buttons proportionally to each other.
1638 * NOTE: if we mix buttons which are proportional and others which are not,
1639 * this may work a bit strangely. */
1640 if ((but_active->rnaprop && (RNA_property_flag(but_active->rnaprop) & PROP_PROPORTIONAL)) ||
1642 {
1643 if (data->origvalue != 0.0) {
1644 data->multi_data.is_proportional = true;
1645 }
1646 }
1647}
1648
1650{
1651 ARegion *region = data->region;
1652 const double value_delta = data->value - data->origvalue;
1653 const double value_scale = data->multi_data.is_proportional ? (data->value / data->origvalue) :
1654 0.0;
1655
1656 BLI_assert(data->multi_data.init == HandleButtonMulti::INIT_ENABLE);
1657 BLI_assert(data->multi_data.skip == false);
1658
1659 for (Button &but : block->buttons()) {
1660 if (!(but.flag & BUT_DRAG_MULTI)) {
1661 continue;
1662 }
1663
1664 ButtonMultiState *mbut_state = multibut_lookup(data, &but);
1665
1666 if (mbut_state == nullptr) {
1667 /* Highly unlikely. */
1668 CLOG_WARN(&LOG, "%s: Can't find button", __func__);
1669 /* While this avoids crashing, multi-button dragging will fail,
1670 * which is still a bug from the user perspective. See #83651. */
1671 continue;
1672 }
1673
1674 void *active_back;
1675 button_execute_begin(C, region, &but, &active_back);
1676
1677# ifdef USE_ALLSELECT
1678 if (data->select_others.is_enabled) {
1679 /* init once! */
1680 if (mbut_state->select_others.elems.is_empty()) {
1681 selectcontext_begin(C, &but, &mbut_state->select_others);
1682 }
1683 if (mbut_state->select_others.elems.is_empty()) {
1684 mbut_state->select_others.elems.clear();
1685 }
1686 }
1687
1688 /* Needed so we apply the right deltas. */
1689 but.active->origvalue = mbut_state->origvalue;
1690 but.active->select_others = mbut_state->select_others;
1691 but.active->select_others.do_free = false;
1692# endif
1693
1694 BLI_assert(active_back == nullptr);
1695 /* No need to check 'data->state' here. */
1696 if (data->text_edit.edit_string) {
1697 /* Entering text (set all). */
1698 but.active->value = data->value;
1699 button_string_set(C, &but, data->text_edit.edit_string);
1700 }
1701 else {
1702 /* Dragging (use delta). */
1703 if (data->multi_data.is_proportional) {
1704 but.active->value = mbut_state->origvalue * value_scale;
1705 }
1706 else {
1707 but.active->value = mbut_state->origvalue + value_delta;
1708 }
1709
1710 /* Clamp based on soft limits, see #40154. */
1711 CLAMP(but.active->value, double(but.softmin), double(but.softmax));
1712 }
1713
1714 button_execute_end(C, region, &but, active_back);
1715 }
1716}
1717
1718#endif /* USE_DRAG_MULTINUM */
1719
1721
1722/* -------------------------------------------------------------------- */
1725
1726#ifdef USE_DRAG_TOGGLE
1727
1728/* Helpers that wrap boolean functions, to support different kinds of buttons. */
1729
1731{
1732 if (but->flag & BUT_DISABLED) {
1733 return false;
1734 }
1735 if (button_is_bool(but)) {
1736 return true;
1737 }
1738 if (button_is_decorator(but)) {
1739 const auto *but_decorate = static_cast<const ButtonDecorator *>(but);
1740 return but_decorate->toggle_keyframe_on_click;
1741 }
1742 return false;
1743}
1744
1749{
1751 if (button_is_decorator(but)) {
1752 return button_anim_decorate_pushed_state(static_cast<ButtonDecorator *>(but));
1753 }
1754 if (button_is_bool(but)) {
1755 return button_is_pushed(but);
1756 }
1758 return false;
1759}
1760
1762 /* init */
1765
1767 bool xy_lock[2];
1768
1769 int xy_init[2];
1770 int xy_last[2];
1771};
1772
1774 ARegion *region,
1775 const bool pushed_state,
1776 const int xy_src[2],
1777 const int xy_dst[2],
1778 const bool drag_lock[2])
1779{
1780 /* popups such as layers won't re-evaluate on redraw */
1781 const bool do_check = (region->regiontype == RGN_TYPE_TEMPORARY);
1782 bool changed = false;
1783
1784 for (Block &block : region->runtime->uiblocks) {
1785 float xy_a_block[2] = {float(xy_src[0]), float(xy_src[1])};
1786 float xy_b_block[2] = {float(xy_dst[0]), float(xy_dst[1])};
1787
1788 window_to_block_fl(region, &block, &xy_a_block[0], &xy_a_block[1]);
1789 window_to_block_fl(region, &block, &xy_b_block[0], &xy_b_block[1]);
1790
1791 for (Button &but : block.buttons()) {
1792 /* NOTE: ctrl is always true here because (at least for now)
1793 * we always want to consider text control in this case, even when not embossed. */
1794
1795 if (!button_is_interactive(&but, true)) {
1796 continue;
1797 }
1798 /* Needs to match fixed lock direction. */
1799 if (((but.flag & BUT_DRAG_LOCK_X) == BUT_DRAG_LOCK_X) && !drag_lock[0]) {
1800 continue;
1801 }
1802 if (!BLI_rctf_isect_segment(&but.rect, xy_a_block, xy_b_block)) {
1803 continue;
1804 }
1805 if (!drag_toggle_but_is_supported(&but)) {
1806 continue;
1807 }
1808 /* is it pressed? */
1809 const bool pushed_state_but = drag_toggle_but_pushed_state(&but);
1810 if (pushed_state_but == pushed_state) {
1811 continue;
1812 }
1813
1814 /* execute the button */
1815 button_execute(C, region, &but);
1816 if (do_check) {
1818 }
1819 if (U.runtime.is_dirty == false) {
1821 }
1822 changed = true;
1823 }
1824 }
1825
1826 if (changed) {
1827 /* apply now, not on release (or if handlers are canceled for whatever reason) */
1829 }
1830
1831 return changed;
1832}
1833
1834static void drag_toggle_set(bContext *C, uiDragToggleHandle *drag_info, const int xy_input[2])
1835{
1836 ARegion *region = CTX_wm_region(C);
1837 bool do_draw = false;
1838
1845 if (drag_info->is_xy_lock_init == false) {
1846 /* first store the buttons original coords */
1847 Button *but = button_find_mouse_over_ex(region, xy_input, true, false, nullptr, nullptr);
1848
1849 if (but) {
1850 if (but->flag & BUT_DRAG_LOCK) {
1851 const float but_cent_new[2] = {
1852 BLI_rctf_cent_x(&but->rect),
1853 BLI_rctf_cent_y(&but->rect),
1854 };
1855
1856 /* check if this is a different button,
1857 * chances are high the button won't move about :) */
1858 if (len_manhattan_v2v2(drag_info->but_cent_start, but_cent_new) > 1.0f) {
1859 if (fabsf(drag_info->but_cent_start[0] - but_cent_new[0]) <
1860 fabsf(drag_info->but_cent_start[1] - but_cent_new[1]))
1861 {
1862 drag_info->xy_lock[0] = true;
1863 }
1864 else {
1865 drag_info->xy_lock[1] = true;
1866 }
1867 drag_info->is_xy_lock_init = true;
1868 }
1869 }
1870 else {
1871 drag_info->is_xy_lock_init = true;
1872 }
1873 }
1874 }
1875 /* done with axis locking */
1876
1877 int xy[2];
1878 xy[0] = (drag_info->xy_lock[0] == false) ? xy_input[0] : drag_info->xy_last[0];
1879 xy[1] = (drag_info->xy_lock[1] == false) ? xy_input[1] : drag_info->xy_last[1];
1880
1881 /* touch all buttons between last mouse coord and this one */
1882 do_draw = drag_toggle_set_xy_xy(
1883 C, region, drag_info->pushed_state, drag_info->xy_last, xy, drag_info->xy_lock);
1884
1885 if (do_draw) {
1886 ED_region_tag_redraw(region);
1887 }
1888
1889 copy_v2_v2_int(drag_info->xy_last, xy);
1890}
1891
1892static void handler_region_drag_toggle_remove(bContext * /*C*/, void *userdata)
1893{
1894 uiDragToggleHandle *drag_info = static_cast<uiDragToggleHandle *>(userdata);
1895 MEM_delete(drag_info);
1896}
1897
1898static int handler_region_drag_toggle(bContext *C, const wmEvent *event, void *userdata)
1899{
1900 uiDragToggleHandle *drag_info = static_cast<uiDragToggleHandle *>(userdata);
1901 bool done = false;
1902
1903 switch (event->type) {
1904 case LEFTMOUSE: {
1905 if (event->val == KM_RELEASE) {
1906 done = true;
1907 }
1908 break;
1909 }
1910 case MOUSEMOVE: {
1911 drag_toggle_set(C, drag_info, event->xy);
1912 break;
1913 }
1914 default: {
1915 break;
1916 }
1917 }
1918
1919 if (done) {
1920 wmWindow *win = CTX_wm_window(C);
1921 const ARegion *region = CTX_wm_region(C);
1923 region, drag_info->xy_init, true, false, nullptr, nullptr);
1924
1925 if (but) {
1926 apply_but_undo(but);
1927 }
1928
1932 drag_info,
1933 false);
1935
1937 return WM_UI_HANDLER_BREAK;
1938 }
1940}
1941
1942static bool but_is_drag_toggle(const Button *but)
1943{
1944 return ((drag_toggle_but_is_supported(but) == true) &&
1945 /* Menu check is important so the button dragged over isn't removed instantly. */
1946 (block_is_menu(but->block) == false));
1947}
1948
1949#endif /* USE_DRAG_TOGGLE */
1950
1951#ifdef USE_ALLSELECT
1952
1954{
1955 PointerRNA lptr;
1956 PropertyRNA *lprop;
1957 bool success = false;
1958
1960
1961 PointerRNA ptr = but->rnapoin;
1962 PropertyRNA *prop = but->rnaprop;
1963 const int index = but->rnaindex;
1964
1965 /* for now don't support whole colors */
1966 if (index == -1) {
1967 return false;
1968 }
1969
1970 /* if there is a valid property that is editable... */
1971 if (ptr && prop) {
1972 bool use_path_from_id;
1973
1974 /* some facts we want to know */
1975 const bool is_array = RNA_property_array_check(prop);
1976 const int rna_type = RNA_property_type(prop);
1977
1978 std::optional<std::string> path;
1979 if (context_copy_to_selected_list(C, &ptr, prop, &lb, &use_path_from_id, &path) &&
1980 !lb.is_empty())
1981 {
1982 selctx_data->elems.resize(lb.size());
1983
1984 int i;
1985 PointerRNA *link;
1986 for (i = 0, link = lb.data(); i < selctx_data->elems.size(); i++, link++) {
1988 link,
1989 prop,
1990 path.has_value() ? path->c_str() : nullptr,
1991 use_path_from_id,
1992 &lptr,
1993 &lprop))
1994 {
1995 selctx_data->elems.pop_last();
1996 i -= 1;
1997 continue;
1998 }
1999
2000 uiSelectContextElem *other = &selctx_data->elems[i];
2001 other->ptr = lptr;
2002 if (is_array) {
2003 if (rna_type == PROP_FLOAT) {
2004 other->val_f = RNA_property_float_get_index(&lptr, lprop, index);
2005 }
2006 else if (rna_type == PROP_INT) {
2007 other->val_i = RNA_property_int_get_index(&lptr, lprop, index);
2008 }
2009 /* ignored for now */
2010# if 0
2011 else if (rna_type == PROP_BOOLEAN) {
2012 other->val_b = RNA_property_boolean_get_index(&lptr, lprop, index);
2013 }
2014# endif
2015 }
2016 else {
2017 if (rna_type == PROP_FLOAT) {
2018 other->val_f = RNA_property_float_get(&lptr, lprop);
2019 }
2020 else if (rna_type == PROP_INT) {
2021 other->val_i = RNA_property_int_get(&lptr, lprop);
2022 }
2023 /* ignored for now */
2024# if 0
2025 else if (rna_type == PROP_BOOLEAN) {
2026 other->val_b = RNA_property_boolean_get(&lptr, lprop);
2027 }
2028 else if (rna_type == PROP_ENUM) {
2029 other->val_i = RNA_property_enum_get(&lptr, lprop);
2030 }
2031# endif
2032 }
2033 }
2034 success = !selctx_data->elems.is_empty();
2035 }
2036 }
2037
2038 /* caller can clear */
2039 selctx_data->do_free = true;
2040
2041 if (success) {
2043 }
2044
2045 return success;
2046}
2047
2048static void selectcontext_end(Button *but, uiSelectContextStore *selctx_data)
2049{
2050 if (selctx_data->do_free) {
2051 selctx_data->elems.clear();
2052 }
2053
2055}
2056
2058 Button *but,
2059 uiSelectContextStore *selctx_data,
2060 const double value,
2061 const double value_orig)
2062{
2063 if (!selctx_data->elems.is_empty()) {
2064 PropertyRNA *prop = but->rnaprop;
2065 PropertyRNA *lprop = but->rnaprop;
2066 const int index = but->rnaindex;
2067 const bool use_delta = (selctx_data->is_copy == false);
2068
2069 std::variant<bool, int, float, std::string, PointerRNA> delta, min, max;
2070
2071 const bool is_array = RNA_property_array_check(prop);
2072 const int rna_type = RNA_property_type(prop);
2073
2074 if (rna_type == PROP_FLOAT) {
2075 delta.emplace<float>(use_delta ? (value - value_orig) : value);
2076 float min_v, max_v;
2077 RNA_property_float_range(&but->rnapoin, prop, &min_v, &max_v);
2078 min.emplace<float>(min_v);
2079 max.emplace<float>(max_v);
2080 }
2081 else if (rna_type == PROP_INT) {
2082 delta.emplace<int>(int(use_delta ? (value - value_orig) : value));
2083 int min_v, max_v;
2084 RNA_property_int_range(&but->rnapoin, prop, &min_v, &max_v);
2085 min.emplace<int>(min_v);
2086 max.emplace<int>(max_v);
2087 }
2088 else if (rna_type == PROP_ENUM) {
2089 /* Not a delta in fact. */
2090 delta.emplace<int>(RNA_property_enum_get(&but->rnapoin, prop));
2091 }
2092 else if (rna_type == PROP_BOOLEAN) {
2093 if (is_array) {
2094 /* Not a delta in fact. */
2095 delta.emplace<bool>(RNA_property_boolean_get_index(&but->rnapoin, prop, index));
2096 }
2097 else {
2098 /* Not a delta in fact. */
2099 delta.emplace<bool>(RNA_property_boolean_get(&but->rnapoin, prop));
2100 }
2101 }
2102 else if (rna_type == PROP_POINTER) {
2103 /* Not a delta in fact. */
2104 delta.emplace<PointerRNA>(RNA_property_pointer_get(&but->rnapoin, prop));
2105 }
2106 else if (rna_type == PROP_STRING) {
2107 /* Not a delta in fact. */
2108 delta.emplace<std::string>(RNA_property_string_get(&but->rnapoin, prop));
2109 }
2110
2111# ifdef USE_ALLSELECT_LAYER_HACK
2112 /* make up for not having 'handle_layer_buttons' */
2113 {
2114 const PropertySubType subtype = RNA_property_subtype(prop);
2115
2116 if ((rna_type == PROP_BOOLEAN) && ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER) && is_array &&
2117 /* could check for 'handle_layer_buttons' */
2118 but->func)
2119 {
2120 wmWindow *win = CTX_wm_window(C);
2121 if ((win->runtime->eventstate->modifier & KM_SHIFT) == 0) {
2122 const int len = RNA_property_array_length(&but->rnapoin, prop);
2123 bool *tmparray = MEM_new_array_zeroed<bool>(len, __func__);
2124
2125 tmparray[index] = true;
2126
2127 for (uiSelectContextElem &other : selctx_data->elems) {
2128 PointerRNA lptr = other.ptr;
2129 RNA_property_boolean_set_array(&lptr, lprop, tmparray);
2130 RNA_property_update(C, &lptr, lprop);
2131 }
2132
2133 MEM_delete(tmparray);
2134
2135 return;
2136 }
2137 }
2138 }
2139# endif
2140
2141 for (uiSelectContextElem &other : selctx_data->elems) {
2142 PointerRNA lptr = other.ptr;
2143
2144 if (rna_type == PROP_FLOAT) {
2145 float other_value = std::get<float>(delta) + (use_delta ? other.val_f : 0.0f);
2146 CLAMP(other_value, std::get<float>(min), std::get<float>(max));
2147 if (is_array) {
2148 RNA_property_float_set_index(&lptr, lprop, index, other_value);
2149 }
2150 else {
2151 RNA_property_float_set(&lptr, lprop, other_value);
2152 }
2153 }
2154 else if (rna_type == PROP_INT) {
2155 int other_value = std::get<int>(delta) + (use_delta ? other.val_i : 0);
2156 CLAMP(other_value, std::get<int>(min), std::get<int>(max));
2157 if (is_array) {
2158 RNA_property_int_set_index(&lptr, lprop, index, other_value);
2159 }
2160 else {
2161 RNA_property_int_set(&lptr, lprop, other_value);
2162 }
2163 }
2164 else if (rna_type == PROP_BOOLEAN) {
2165 const bool other_value = std::get<bool>(delta);
2166 if (is_array) {
2167 RNA_property_boolean_set_index(&lptr, lprop, index, other_value);
2168 }
2169 else {
2170 RNA_property_boolean_set(&lptr, lprop, other_value);
2171 }
2172 }
2173 else if (rna_type == PROP_ENUM) {
2174 const int other_value = std::get<int>(delta);
2175 BLI_assert(!is_array);
2176 RNA_property_enum_set(&lptr, lprop, other_value);
2177 }
2178 else if (rna_type == PROP_POINTER) {
2179 const PointerRNA &other_value = std::get<PointerRNA>(delta);
2180 RNA_property_pointer_set(&lptr, lprop, other_value, nullptr);
2181 }
2182 else if (rna_type == PROP_STRING) {
2183 const std::string &other_value = std::get<std::string>(delta);
2184 RNA_property_string_set(&lptr, lprop, other_value.c_str());
2185 }
2186
2187 RNA_property_update(C, &lptr, prop);
2188 }
2189 }
2190}
2191
2192#endif /* USE_ALLSELECT */
2193
2195
2196/* -------------------------------------------------------------------- */
2199
2200static bool but_drag_init(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
2201{
2202 /* Prevent #TIMER events from initializing drag events which can overlap with #KM_PRESS_DRAG
2203 * events, see #161044. */
2204 if (ISTIMER(event->type)) {
2205 return false;
2206 }
2207
2208 /* prevent other WM gestures to start while we try to drag */
2210
2211 /* Clamp the maximum to half the UI unit size so a high user preference
2212 * doesn't require the user to drag more than half the default button height. */
2213 const int drag_threshold = min_ii(
2215 int((UI_UNIT_Y / 2) * block_to_window_scale(data->region, but->block)));
2216
2217 if (abs(data->dragstartx - event->xy[0]) + abs(data->dragstarty - event->xy[1]) > drag_threshold)
2218 {
2220 data->cancel = true;
2221#ifdef USE_DRAG_TOGGLE
2224 ARegion *region_prev;
2225
2226 /* call here because regular mouse-up event won't run,
2227 * typically 'button_activate_exit()' handles this */
2228 apply_but_autokey(C, but);
2229
2231 drag_info->but_cent_start[0] = BLI_rctf_cent_x(&but->rect);
2232 drag_info->but_cent_start[1] = BLI_rctf_cent_y(&but->rect);
2233 if ((but->flag & BUT_DRAG_LOCK_X) == BUT_DRAG_LOCK_X) {
2234 drag_info->xy_lock[0] = true;
2235 }
2236 copy_v2_v2_int(drag_info->xy_init, event->xy);
2237 copy_v2_v2_int(drag_info->xy_last, event->xy);
2238
2239 /* needed for toggle drag on popups */
2240 region_prev = CTX_wm_region(C);
2241 CTX_wm_region_set(C, data->region);
2242
2244 &data->window->runtime->modalhandlers,
2247 drag_info,
2249
2250 CTX_wm_region_set(C, region_prev);
2251
2252 /* Initialize alignment for single row/column regions,
2253 * otherwise we use the relative position of the first other button dragged over. */
2254 if (ELEM(data->region->regiontype,
2260 {
2261 const int region_alignment = RGN_ALIGN_ENUM_FROM_MASK(data->region->alignment);
2262 int lock_axis = -1;
2263
2264 if (ELEM(region_alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
2265 lock_axis = 0;
2266 }
2267 else if (ELEM(region_alignment, RGN_ALIGN_TOP, RGN_ALIGN_BOTTOM)) {
2268 lock_axis = 1;
2269 }
2270 if (lock_axis != -1) {
2271 drag_info->xy_lock[lock_axis] = true;
2272 drag_info->is_xy_lock_init = true;
2273 }
2274 }
2275 }
2276 else
2277#endif
2278 if (but->type == ButtonType::Color)
2279 {
2280 bool valid = false;
2281 DragColorHandle *drag_info = MEM_new_zeroed<DragColorHandle>(__func__);
2282
2283 drag_info->has_alpha = button_color_has_alpha(but);
2284
2285 /* TODO: support more button pointer types. */
2287 button_v4_get(but, drag_info->color);
2288 drag_info->gamma_corrected = true;
2289 valid = true;
2290 }
2291 else if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR) {
2292 button_v4_get(but, drag_info->color);
2293 drag_info->gamma_corrected = false;
2294 valid = true;
2295 }
2297 button_v4_get(but, drag_info->color);
2298 copy_v4_v4(drag_info->color, reinterpret_cast<float *>(but->poin));
2299 drag_info->gamma_corrected = false;
2300 valid = true;
2301 }
2302
2303 if (valid) {
2304 WM_event_start_drag(C, ICON_COLOR, WM_DRAG_COLOR, drag_info, WM_DRAG_FREE_DATA);
2305 }
2306 else {
2307 MEM_delete(drag_info);
2308 return false;
2309 }
2310 }
2311 else if (but->type == ButtonType::ViewItem) {
2312 const auto *view_item_but = static_cast<ButtonViewItem *>(but);
2313 if (view_item_but->view_item) {
2314 return view_item_drag_start(*C, *view_item_but->view_item);
2315 }
2316 }
2317 else {
2318 button_drag_start(C, but);
2319 }
2320 return true;
2321 }
2322
2323 return false;
2324}
2325
2327
2328/* -------------------------------------------------------------------- */
2331
2333{
2334 apply_but_func(C, but);
2335 data->retval = but->retval;
2336 data->applied = true;
2337}
2338
2340{
2341 apply_but_func(C, but);
2342 data->retval = but->retval;
2343 data->applied = true;
2344}
2345
2347{
2348 apply_but_func(C, but);
2349 data->retval = but->retval;
2350 data->applied = true;
2351}
2352
2354{
2355 apply_but_func(C, but);
2356 data->retval = but->retval;
2357 data->applied = true;
2358}
2359
2360static void apply_but(
2361 bContext *C, Block *block, Button *but, HandleButtonData *data, const bool interactive)
2362{
2363 const ButtonType but_type = but->type; /* Store as const to quiet maybe uninitialized warning. */
2364
2365 data->retval = 0;
2366
2367 /* if we cancel and have not applied yet, there is nothing to do,
2368 * otherwise we have to restore the original value again */
2369 if (data->cancel) {
2370 if (!data->applied) {
2371 return;
2372 }
2373
2374 if (data->text_edit.edit_string) {
2375 MEM_delete(data->text_edit.edit_string);
2376 }
2377 data->text_edit.edit_string = data->text_edit.original_string;
2378 data->text_edit.original_string = nullptr;
2379 data->value = data->origvalue;
2380 copy_v3_v3(data->vec, data->origvec);
2381 /* postpone clearing origdata */
2382 }
2383 else {
2384 /* We avoid applying interactive edits a second time
2385 * at the end with the #HandleButtonData.applied_interactive flag. */
2386 if (interactive) {
2387 data->applied_interactive = true;
2388 }
2389 else if (data->applied_interactive) {
2390 return;
2391 }
2392
2393#ifdef USE_ALLSELECT
2394# ifdef USE_DRAG_MULTINUM
2395 if (but->flag & BUT_DRAG_MULTI) {
2396 /* pass */
2397 }
2398 else
2399# endif
2400 if (data->select_others.elems.is_empty())
2401 {
2402 wmWindow *win = CTX_wm_window(C);
2403 const wmEvent *event = win->runtime->eventstate;
2404 /* May have been enabled before activating, don't do for array pasting. */
2405 if (data->select_others.is_enabled || IS_ALLSELECT_EVENT(event)) {
2406 /* See comment for #IS_ALLSELECT_EVENT why this needs to be filtered here. */
2407 const bool is_array_paste = (event->val == KM_PRESS) &&
2408 (event->modifier & (KM_CTRL | KM_OSKEY)) &&
2409 (event->modifier & KM_SHIFT) == 0 && (event->type == EVT_VKEY);
2410 if (!is_array_paste) {
2411 selectcontext_begin(C, but, &data->select_others);
2412 data->select_others.is_enabled = true;
2413 }
2414 }
2415 }
2416 if (data->select_others.elems.is_empty()) {
2417 /* Don't check again. */
2418 data->select_others.elems.clear();
2419 }
2420#endif
2421 }
2422
2423 /* ensures we are writing actual values */
2424 char *editstr = but->editstr;
2425 double *editval = but->editval;
2426 float *editvec = but->editvec;
2427 ColorBand *editcoba;
2428 CurveMapping *editcumap;
2429 CurveProfile *editprofile;
2430 if (but_type == ButtonType::ColorBand) {
2431 ButtonColorBand *but_coba = static_cast<ButtonColorBand *>(but);
2432 editcoba = but_coba->edit_coba;
2433 }
2434 else if (but_type == ButtonType::Curve) {
2435 ButtonCurveMapping *but_cumap = static_cast<ButtonCurveMapping *>(but);
2436 editcumap = but_cumap->edit_cumap;
2437 }
2438 else if (but_type == ButtonType::CurveProfile) {
2439 ButtonCurveProfile *but_profile = static_cast<ButtonCurveProfile *>(but);
2440 editprofile = but_profile->edit_profile;
2441 }
2442 but->editstr = nullptr;
2443 but->editval = nullptr;
2444 but->editvec = nullptr;
2445 if (but_type == ButtonType::ColorBand) {
2446 ButtonColorBand *but_coba = static_cast<ButtonColorBand *>(but);
2447 but_coba->edit_coba = nullptr;
2448 }
2449 else if (but_type == ButtonType::Curve) {
2450 ButtonCurveMapping *but_cumap = static_cast<ButtonCurveMapping *>(but);
2451 but_cumap->edit_cumap = nullptr;
2452 }
2453 else if (but_type == ButtonType::CurveProfile) {
2454 ButtonCurveProfile *but_profile = static_cast<ButtonCurveProfile *>(but);
2455 but_profile->edit_profile = nullptr;
2456 }
2457
2458 /* handle different types */
2459 switch (but_type) {
2460 case ButtonType::But:
2463 apply_but_BUT(C, but, data);
2464 break;
2465 case ButtonType::Text:
2468 apply_but_TEX(C, but, data);
2469 break;
2471 case ButtonType::Toggle:
2477 apply_but_TOG(C, but, data);
2478 break;
2480 case ButtonType::Row:
2481 apply_but_ROW(C, block, but, data);
2482 break;
2484 apply_but_VIEW_ITEM(C, block, but, data);
2485 break;
2486 case ButtonType::Tab:
2487 apply_but_TAB(C, but, data);
2488 break;
2489 case ButtonType::Scroll:
2490 case ButtonType::Grip:
2491 case ButtonType::Num:
2493 apply_but_NUM(C, but, data);
2494 break;
2495 case ButtonType::Menu:
2496 case ButtonType::Block:
2498 apply_but_BLOCK(C, but, data);
2499 break;
2500 case ButtonType::Color:
2501 if (data->cancel) {
2502 apply_but_VEC(C, but, data);
2503 }
2504 else {
2505 apply_but_BLOCK(C, but, data);
2506 }
2507 break;
2509 apply_but_BUTM(C, but, data);
2510 break;
2514 apply_but_VEC(C, but, data);
2515 break;
2517 apply_but_COLORBAND(C, but, data);
2518 break;
2519 case ButtonType::Curve:
2520 apply_but_CURVE(C, but, data);
2521 break;
2524 break;
2527 apply_but_BUT(C, but, data);
2528 break;
2529 case ButtonType::Image:
2530 apply_but_IMAGE(C, but, data);
2531 break;
2533 apply_but_HISTOGRAM(C, but, data);
2534 break;
2536 apply_but_WAVEFORM(C, but, data);
2537 break;
2540 break;
2541 default:
2542 break;
2543 }
2544
2545#ifdef USE_DRAG_MULTINUM
2546 if (data->multi_data.has_mbuts) {
2547 if ((data->multi_data.init == HandleButtonMulti::INIT_ENABLE) &&
2548 (data->multi_data.skip == false))
2549 {
2550 if (data->cancel) {
2551 multibut_restore(C, data, block);
2552 }
2553 else {
2554 multibut_states_apply(C, data, block);
2555 }
2556 }
2557 }
2558#endif
2559
2560#ifdef USE_ALLSELECT
2561 selectcontext_apply(C, but, &data->select_others, data->value, data->origvalue);
2562#endif
2563
2564 if (data->cancel) {
2565 data->origvalue = 0.0;
2566 zero_v3(data->origvec);
2567 }
2568
2569 but->editstr = editstr;
2570 but->editval = editval;
2571 but->editvec = editvec;
2572 if (but_type == ButtonType::ColorBand) {
2573 ButtonColorBand *but_coba = static_cast<ButtonColorBand *>(but);
2574 but_coba->edit_coba = editcoba;
2575 }
2576 else if (but_type == ButtonType::Curve) {
2577 ButtonCurveMapping *but_cumap = static_cast<ButtonCurveMapping *>(but);
2578 but_cumap->edit_cumap = editcumap;
2579 }
2580 else if (but_type == ButtonType::CurveProfile) {
2581 ButtonCurveProfile *but_profile = static_cast<ButtonCurveProfile *>(but);
2582 but_profile->edit_profile = editprofile;
2583 }
2584
2585 if (data->custom_interaction_handle != nullptr) {
2587 C, &block->custom_interaction_callbacks, data->custom_interaction_handle);
2588 }
2589}
2590
2592
2593/* -------------------------------------------------------------------- */
2596
2597static void but_get_pasted_text_from_clipboard(const bool ensure_utf8,
2598 char **r_buf_paste,
2599 int *r_buf_len,
2600 bool paste_all_lines)
2601{
2602 /* get only first line even if the clipboard contains multiple lines */
2603 int length;
2604 char *text = paste_all_lines ? WM_clipboard_text_get(false, ensure_utf8, &length) :
2605 WM_clipboard_text_get_firstline(false, ensure_utf8, &length);
2606
2607 if (text) {
2608 *r_buf_paste = text;
2609 *r_buf_len = length;
2610 }
2611 else {
2612 *r_buf_paste = MEM_new_zeroed<char>(__func__);
2613 *r_buf_len = 0;
2614 }
2615}
2616
2618{
2619 return RNA_property_array_length(&but->rnapoin, but->rnaprop);
2620}
2621
2623 bContext *C, Button *but, HandleButtonData *data, const float *values, const int values_len)
2624{
2626
2627 RNA_property_float_set_array_at_most(&but->rnapoin, but->rnaprop, values, values_len);
2628 if (data) {
2629 if (but->type == ButtonType::Unitvec) {
2630 BLI_assert(values_len == 3);
2631 copy_v3_v3(data->vec, values);
2632 }
2633 else {
2634 data->value = values[but->rnaindex];
2635 }
2636 }
2637
2639}
2640
2641static void float_array_to_string(const float *values,
2642 const int values_len,
2643 char *output,
2644 int output_maxncpy)
2645{
2646 const int values_end = values_len - 1;
2647 int ofs = 0;
2648 output[ofs++] = '[';
2649 for (int i = 0; i < values_len; i++) {
2651 output + ofs, output_maxncpy - ofs, (i != values_end) ? "%f, " : "%f]", values[i]);
2652 }
2653}
2654
2655static void but_copy_numeric_array(Button *but, char *output, int output_maxncpy)
2656{
2657 const int values_len = get_but_property_array_length(but);
2658 Array<float, 16> values(values_len);
2659 RNA_property_float_get_array(&but->rnapoin, but->rnaprop, values.data());
2660 float_array_to_string(values.data(), values_len, output, output_maxncpy);
2661}
2662
2663static bool parse_float_array(char *text, float *values, int values_len_expected)
2664{
2665 /* can parse max 4 floats for now */
2666 BLI_assert(0 <= values_len_expected && values_len_expected <= 4);
2667
2668 float v[5];
2669 const int values_len_actual = sscanf(
2670 text, "[%f, %f, %f, %f, %f]", &v[0], &v[1], &v[2], &v[3], &v[4]);
2671
2672 if (values_len_actual == values_len_expected) {
2673 memcpy(values, v, sizeof(float) * values_len_expected);
2674 return true;
2675 }
2676 return false;
2677}
2678
2680 Button *but,
2682 char *buf_paste)
2683{
2684 const int values_len = get_but_property_array_length(but);
2685 if (values_len > 4) {
2686 /* not supported for now */
2687 return;
2688 }
2689
2690 Array<float, 16> values(values_len);
2691
2692 if (parse_float_array(buf_paste, values.data(), values_len)) {
2693 but_set_float_array(C, but, data, values.data(), values_len);
2694 }
2695 else {
2696 WM_global_report(RPT_ERROR, "Expected an array of numbers: [n, n, ...]");
2697 }
2698}
2699
2700static void but_copy_numeric_value(Button *but, char *output, int output_maxncpy)
2701{
2702 /* Get many decimal places, then strip trailing zeros.
2703 * NOTE: too high values start to give strange results. */
2704 button_string_get_ex(but, output, output_maxncpy, UI_PRECISION_FLOAT_MAX, false, nullptr);
2706}
2707
2709 Button *but,
2711 char *buf_paste)
2712{
2713 double value;
2714 if (button_string_eval_number(C, but, buf_paste, &value)) {
2716 data->value = value;
2717 button_string_set(C, but, buf_paste);
2719 }
2720 else {
2721 WM_global_report(RPT_ERROR, "Expected a number");
2722 }
2723}
2724
2726 Button *but,
2728 char *buf_paste)
2729{
2730 float xyz[3];
2731 if (parse_float_array(buf_paste, xyz, 3)) {
2732 if (normalize_v3(xyz) == 0.0f) {
2733 /* better set Z up then have a zero vector */
2734 xyz[2] = 1.0;
2735 }
2736 but_set_float_array(C, but, data, xyz, 3);
2737 }
2738 else {
2739 WM_global_report(RPT_ERROR, "Paste expected 3 numbers, formatted: '[n, n, n]'");
2740 }
2741}
2742
2743static void but_copy_color(Button *but, char *output, int output_maxncpy)
2744{
2745 float rgba[4];
2746
2747 if (but->rnaprop && get_but_property_array_length(but) >= 4) {
2748 rgba[3] = RNA_property_float_get_index(&but->rnapoin, but->rnaprop, 3);
2749 }
2750 else {
2751 rgba[3] = 1.0f;
2752 }
2753
2754 button_v3_get(but, rgba);
2755
2756 /* convert to linear color to do compatible copy between gamma and non-gamma */
2758 srgb_to_linearrgb_v3_v3(rgba, rgba);
2759 }
2760
2761 float_array_to_string(rgba, 4, output, output_maxncpy);
2762}
2763
2764static void but_paste_color(bContext *C, Button *but, char *buf_paste)
2765{
2766 float rgba[4] = {0.0, 0.0, 0.0, 1.0};
2767 bool is_parsed = false;
2768
2769 if (parse_float_array(buf_paste, rgba, 4)) {
2770 is_parsed = true;
2771 }
2772 else if (hex_to_rgba(buf_paste, &rgba[0], &rgba[1], &rgba[2], &rgba[3])) {
2774 is_parsed = true;
2775 }
2776
2777 if (is_parsed) {
2778 if (but->rnaprop) {
2779 /* Assume linear colors in buffer. */
2781 linearrgb_to_srgb_v3_v3(rgba, rgba);
2782 }
2783
2784 /* Some color properties are RGB, not RGBA. */
2785 const int array_len = get_but_property_array_length(but);
2786 but_set_float_array(C, but, nullptr, rgba, std::min(array_len, int(ARRAY_SIZE(rgba))));
2787 }
2788 }
2789 else {
2790 WM_global_report(RPT_ERROR, "Paste expected hex code or 4 numbers, formatted: '[n, n, n, n]'");
2791 }
2792}
2793
2794static void but_copy_text(Button *but, char *output, int output_maxncpy)
2795{
2796 button_string_get(but, output, output_maxncpy);
2797}
2798
2799static void but_paste_text(bContext *C, Button *but, HandleButtonData *data, char *buf_paste)
2800{
2801 BLI_assert(but->active == data);
2803 button_set_string_interactive(C, but, buf_paste);
2804}
2805
2807{
2808 if (but->poin != nullptr) {
2809 memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
2810 }
2811}
2812
2814{
2815 if (but_copypaste_coba.tot != 0 && but->poin != nullptr) {
2817 memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand));
2819 }
2820}
2821
2823{
2824 if (but->poin != nullptr) {
2828 }
2829}
2830
2832{
2833 if (but_copypaste_curve_alive && but->poin != nullptr) {
2835
2836 CurveMapping *dest = reinterpret_cast<CurveMapping *>(but->poin);
2837
2838 int source_channels = BKE_curvemapping_num_channels(&but_copypaste_curve);
2839 int dest_channels = BKE_curvemapping_num_channels(dest);
2840
2841 if (source_channels == dest_channels) {
2844 }
2845 else {
2848 dest, &but_copypaste_curve, dest->cur, but_copypaste_curve.cur, true);
2849 }
2850
2852 }
2853}
2854
2856{
2857 if (but->poin != nullptr) {
2861 reinterpret_cast<CurveProfile *>(but->poin));
2862 }
2863}
2864
2866{
2867 if (but_copypaste_profile_alive && but->poin != nullptr) {
2869 BKE_curveprofile_free_data(reinterpret_cast<CurveProfile *>(but->poin));
2870 BKE_curveprofile_copy_data(reinterpret_cast<CurveProfile *>(but->poin),
2873 }
2874}
2875
2876static void but_copy_operator(bContext *C, Button *but, char *output, int output_maxncpy)
2877{
2879
2880 std::string str = WM_operator_pystring_ex(C, nullptr, false, true, but->optype, opptr);
2881 BLI_strncpy_utf8(output, str.c_str(), output_maxncpy);
2882}
2883
2884static bool but_copy_menu(bContext *C, Button *but, char *output, int output_maxncpy)
2885{
2886 MenuType *mt = button_menutype_get(but);
2887 if (mt) {
2888 BLI_snprintf_utf8(output, output_maxncpy, "bpy.ops.wm.call_menu(name=\"%s\")", mt->idname);
2889 return true;
2890 }
2891
2892 PropertyRNA *prop = but->rnaprop;
2893 if (prop == nullptr) {
2894 return false;
2895 }
2896
2897 const int type = RNA_property_type(prop);
2898 if (type != PROP_ENUM) {
2899 return false;
2900 }
2901
2902 const int active_item = RNA_property_enum_get(&but->rnapoin, prop);
2903 EnumPropertyItem item;
2904 const bool found = RNA_property_enum_item_from_value(C, &but->rnapoin, prop, active_item, &item);
2905 if (!found) {
2906 return false;
2907 }
2908
2909 if (item.identifier == nullptr) {
2910 return false;
2911 }
2912
2913 if (item.identifier == StringRef("")) {
2914 return false;
2915 }
2916
2917 BLI_strncpy_utf8(output, item.identifier, output_maxncpy);
2918 return true;
2919}
2920
2921static void but_paste_menu(bContext *C, Button *but, const StringRef value)
2922{
2923 if (button_menutype_get(but) != nullptr) {
2924 return;
2925 }
2926
2927 if (value.is_empty()) {
2928 return;
2929 }
2930
2931 PropertyRNA *prop = but->rnaprop;
2932 if (prop == nullptr) {
2933 return;
2934 }
2935
2936 const int type = RNA_property_type(prop);
2937 if (type != PROP_ENUM) {
2938 return;
2939 }
2940
2941 int item_index;
2942 const bool found = RNA_property_enum_value(
2943 C, &but->rnapoin, prop, std::string(value).c_str(), &item_index);
2944 if (!found) {
2946 RPT_ERROR, "Paste of \"%s\": cannot assign value to property", std::string(value).c_str());
2947 return;
2948 }
2949 RNA_property_enum_set(&but->rnapoin, prop, item_index);
2950 RNA_property_update(C, &but->rnapoin, prop);
2951}
2952
2953static bool but_copy_popover(Button *but, char *output, int output_maxncpy)
2954{
2956 if (pt) {
2957 BLI_snprintf_utf8(output, output_maxncpy, "bpy.ops.wm.call_panel(name=\"%s\")", pt->idname);
2958 return true;
2959 }
2960 return false;
2961}
2962
2964static bool but_copy(bContext *C, Button *but, const bool copy_array)
2965{
2966 if (but_contains_password(but)) {
2967 return false;
2968 }
2969
2970 /* Arbitrary large value (allow for paths: 'PATH_MAX') */
2971 char buf[4096] = {0};
2972 const int buf_maxncpy = sizeof(buf);
2973
2974 /* Left false for copying internal data (color-band for eg). */
2975 bool is_buf_set = false;
2976
2977 const bool has_required_data = but->poin || but->rnapoin;
2978
2979 switch (but->type) {
2980 case ButtonType::Num:
2982 if (!has_required_data) {
2983 break;
2984 }
2985 if (copy_array && button_has_array_value(but)) {
2986 but_copy_numeric_array(but, buf, buf_maxncpy);
2987 }
2988 else {
2989 but_copy_numeric_value(but, buf, buf_maxncpy);
2990 }
2991 is_buf_set = true;
2992 break;
2993
2995 if (!has_required_data) {
2996 break;
2997 }
2998 but_copy_numeric_array(but, buf, buf_maxncpy);
2999 is_buf_set = true;
3000 break;
3001
3002 case ButtonType::Color:
3003 if (!has_required_data) {
3004 break;
3005 }
3006 but_copy_color(but, buf, buf_maxncpy);
3007 is_buf_set = true;
3008 break;
3009
3010 case ButtonType::Text:
3013 if (!has_required_data) {
3014 break;
3015 }
3016 but_copy_text(but, buf, buf_maxncpy);
3017 is_buf_set = true;
3018 break;
3019
3021 but_copy_colorband(but);
3022 break;
3023
3024 case ButtonType::Curve:
3026 break;
3027
3030 break;
3031
3032 case ButtonType::But:
3033 if (!but->optype) {
3034 break;
3035 }
3036 but_copy_operator(C, but, buf, buf_maxncpy);
3037 is_buf_set = true;
3038 break;
3039
3040 case ButtonType::Row:
3041 case ButtonType::Menu:
3043 if (but_copy_menu(C, but, buf, buf_maxncpy)) {
3044 is_buf_set = true;
3045 }
3046 break;
3048 if (but_copy_popover(but, buf, buf_maxncpy)) {
3049 is_buf_set = true;
3050 }
3051 break;
3052 case ButtonType::ViewItem: {
3053 ButtonViewItem *viewitem_but = static_cast<ButtonViewItem *>(but);
3054 const StringRef name = viewitem_but->view_item->get_rename_string();
3055 name.copy_utf8_truncated(buf);
3056 is_buf_set = true;
3057 break;
3058 }
3059 default:
3060 break;
3061 }
3062
3063 if (is_buf_set) {
3064 WM_clipboard_text_set(buf, false);
3065 }
3066 return is_buf_set;
3067}
3068
3069static void but_paste(bContext *C, Button *but, HandleButtonData *data, const bool paste_array)
3070{
3071 BLI_assert((but->flag & BUT_DISABLED) == 0); /* caller should check */
3072
3073 int buf_paste_len = 0;
3074 char *buf_paste;
3076 but_is_utf8(but), &buf_paste, &buf_paste_len, but->type == ButtonType::TextBox);
3077
3078 const bool has_required_data = but->poin || but->rnapoin;
3079
3080 switch (but->type) {
3081 case ButtonType::Num:
3083 if (!has_required_data) {
3084 break;
3085 }
3086 if (paste_array && button_has_array_value(but)) {
3087 but_paste_numeric_array(C, but, data, buf_paste);
3088 }
3089 else {
3090 but_paste_numeric_value(C, but, data, buf_paste);
3091 }
3092 break;
3093
3095 if (!has_required_data) {
3096 break;
3097 }
3098 but_paste_normalized_vector(C, but, data, buf_paste);
3099 break;
3100
3101 case ButtonType::Color:
3102 if (!has_required_data) {
3103 break;
3104 }
3105 but_paste_color(C, but, buf_paste);
3106 break;
3107
3108 case ButtonType::Text:
3111 if (!has_required_data) {
3112 break;
3113 }
3114 but_paste_text(C, but, data, buf_paste);
3115 break;
3116
3118 but_paste_colorband(C, but, data);
3119 break;
3120
3121 case ButtonType::Curve:
3123 break;
3124
3127 break;
3128
3129 case ButtonType::Row:
3130 case ButtonType::Menu:
3131 but_paste_menu(C, but, {buf_paste, buf_paste_len});
3132 break;
3133
3134 case ButtonType::ViewItem: {
3135 ButtonViewItem *viewitem_but = static_cast<ButtonViewItem *>(but);
3136 viewitem_but->view_item->rename(*C, buf_paste);
3138 break;
3139 }
3140 default:
3141 break;
3142 }
3143
3144 MEM_delete(buf_paste);
3145}
3146
3152
3154
3155/* -------------------------------------------------------------------- */
3167
3168static int text_position_from_hidden(Button *but, int pos)
3169{
3170 const char *butstr = (but->editstr) ? but->editstr : but->drawstr.c_str();
3171 const char *strpos = butstr;
3172 const char *str_end = butstr + strlen(butstr);
3173 for (int i = 0; i < pos; i++) {
3174 strpos = BLI_str_find_next_char_utf8(strpos, str_end);
3175 }
3176
3177 return (strpos - butstr);
3178}
3179
3180static int text_position_to_hidden(Button *but, int pos)
3181{
3182 const char *butstr = (but->editstr) ? but->editstr : but->drawstr.c_str();
3183 return BLI_strnlen_utf8(butstr, pos);
3184}
3185
3186void button_text_password_hide(std::string &password_str, Button *but, const bool restore)
3187{
3188 if (!(but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_PASSWORD)) {
3189 return;
3190 }
3191
3192 char *butstr = (but->editstr) ? but->editstr : but->drawstr.data();
3193
3194 if (restore) {
3195 /* restore original string */
3196 memcpy(butstr, password_str.data(), password_str.length() + 1);
3197
3198 /* remap cursor positions */
3199 if (but->pos >= 0) {
3200 but->pos = text_position_from_hidden(but, but->pos);
3201 but->selsta = text_position_from_hidden(but, but->selsta);
3202 but->selend = text_position_from_hidden(but, but->selend);
3203 }
3204 }
3205 else {
3206 /* convert text to hidden text using asterisks (e.g. pass -> ****) */
3207 const size_t len = BLI_strlen_utf8(butstr);
3208
3209 /* remap cursor positions */
3210 if (but->pos >= 0) {
3211 but->pos = text_position_to_hidden(but, but->pos);
3212 but->selsta = text_position_to_hidden(but, but->selsta);
3213 but->selend = text_position_to_hidden(but, but->selend);
3214 }
3215
3216 /* save original string */
3217 password_str = butstr;
3218 memset(butstr, '*', len);
3219 butstr[len] = '\0';
3220 }
3221}
3222
3224
3225/* -------------------------------------------------------------------- */
3228
3229void button_set_string_interactive(bContext *C, Button *but, const char *value)
3230{
3231 /* Caller should check. */
3232 BLI_assert((but->flag & BUT_DISABLED) == 0);
3233
3235 textedit_string_set(but, but->active->text_edit, value);
3236
3237 if (but->type == ButtonType::SearchMenu && but->active) {
3238 but->changed = true;
3239 searchbox_update(C, but->active->searchbox, but, true);
3240 }
3241
3243}
3244
3246{
3247 if (!but->active) {
3248 return;
3249 }
3250
3251 /* most likely nullptr, but let's check, and give it temp zero string */
3252 if (!but->active->text_edit.edit_string) {
3254 }
3255 but->active->text_edit.edit_string[0] = 0;
3256
3257 apply_but_TEX(C, but, but->active);
3259}
3260
3261static void textedit_string_ensure_max_length(Button *but, TextEdit &text_edit, int str_maxncpy)
3262{
3263 BLI_assert(text_edit.is_str_dynamic);
3264 BLI_assert(text_edit.edit_string == but->editstr);
3265
3266 if (str_maxncpy > text_edit.max_string_size) {
3267 text_edit.edit_string = but->editstr = static_cast<char *>(
3268 MEM_realloc_uninitialized(text_edit.edit_string, sizeof(char) * str_maxncpy));
3269 text_edit.max_string_size = str_maxncpy;
3270 }
3271}
3272
3273static void textedit_string_set(Button *but, TextEdit &text_edit, const char *str)
3274{
3275 if (text_edit.is_str_dynamic) {
3276 textedit_string_ensure_max_length(but, text_edit, strlen(str) + 1);
3277 }
3278
3279 if (but_is_utf8(but)) {
3280 BLI_strncpy_utf8(text_edit.edit_string, str, text_edit.max_string_size);
3281 }
3282 else {
3283 BLI_strncpy(text_edit.edit_string, str, text_edit.max_string_size);
3284 }
3285}
3286
3287static bool textedit_delete_selection(Button *but, TextEdit &text_edit)
3288{
3289 char *str = text_edit.edit_string;
3290 const int len = strlen(str);
3291 bool changed = false;
3292 if (but->selsta != but->selend && len) {
3293 memmove(str + but->selsta, str + but->selend, (len - but->selend) + 1);
3294 changed = true;
3295 }
3296
3297 if (but->ofs > but->selsta) {
3298 /* Decrease the offset by the amount of the selection that is hidden. Without
3299 * this adjustment, pasting text that doesn't fit in the text field would leave
3300 * the pasted text scrolled out of the view (to the left), see: #134491. */
3301 but->ofs -= (but->ofs - but->selsta);
3302 }
3303
3304 but->pos = but->selend = but->selsta;
3305 return changed;
3306}
3307
3313static void textedit_set_cursor_pos(Button *but, const ARegion *region, const float2 xy)
3314{
3315 if (but->type == ButtonType::TextBox) {
3316 textbox_textedit_set_cursor_pos(static_cast<ButtonTextBox *>(but), region, xy);
3317 return;
3318 }
3319 /* XXX pass on as arg. */
3320 uiFontStyle fstyle = style_get()->widget;
3321 const float aspect = but->block->aspect;
3322
3323 float startx = but->rect.xmin;
3324 float endx = but->rect.xmax;
3325 float starty_dummy = 0.0f;
3326 std::string password_str;
3327 /* treat 'str_last' as null terminator for str, no need to modify in-place */
3328 const char *str = but->editstr, *str_last;
3329
3330 /* Compute padding in block space. */
3331 bool right_aligned = !(but->drawflag & BUT_TEXT_LEFT) && but->drawflag & BUT_TEXT_RIGHT;
3332
3334 if (but->flag & UI_HAS_ICON) {
3335 startx += UI_ICON_SIZE;
3336 }
3337 }
3338 if (!(but->drawflag & BUT_NO_TEXT_PADDING)) {
3339 if (right_aligned) {
3340 startx += U.pixelsize;
3341 endx -= UI_TEXT_MARGIN_X * U.widget_unit;
3342 }
3343 else {
3344 startx += UI_TEXT_MARGIN_X * U.widget_unit;
3345 endx -= U.pixelsize;
3346 }
3347 }
3348 else if (right_aligned) {
3349 endx -= U.pixelsize;
3350 }
3351 else {
3352 startx += U.pixelsize;
3353 }
3354
3355 /* Transform startx and endx to screen space. */
3356 block_to_window_fl(region, but->block, &startx, &starty_dummy);
3357 block_to_window_fl(region, but->block, &endx, &starty_dummy);
3358
3359 fontscale(&fstyle.points, aspect);
3360
3361 fontstyle_set(&fstyle);
3362
3363 button_text_password_hide(password_str, but, false);
3364
3365 /* Compute shift due to right-alignment after password filter. */
3366 if (right_aligned) {
3367 int width = BLF_width(fstyle.uifont_id, str + but->ofs, strlen(str + but->ofs));
3368 const float align_x_ofs = endx - startx - width;
3369 startx += max_ff(0.0f, align_x_ofs);
3370 }
3371
3372 /* mouse dragged outside the widget to the left */
3373 if (xy.x < startx) {
3374 int i = but->ofs;
3375
3376 str_last = &str[but->ofs];
3377
3378 while (i > 0) {
3379 if (BLI_str_cursor_step_prev_utf8(str, but->ofs, &i)) {
3380 /* 0.25 == scale factor for less sensitivity */
3381 if (BLF_width(fstyle.uifont_id, str + i, (str_last - str) - i) > (startx - xy.x) * 0.25f) {
3382 break;
3383 }
3384 }
3385 else {
3386 break; /* unlikely but possible */
3387 }
3388 }
3389 but->ofs = i;
3390 but->pos = but->ofs;
3391 }
3392 /* mouse inside the widget, mouse coords mapped in widget space */
3393 else {
3394 but->pos = but->ofs +
3396 fstyle.uifont_id, str + but->ofs, strlen(str + but->ofs), int(xy.x - startx));
3397 }
3398
3399 button_text_password_hide(password_str, but, true);
3400}
3401
3403{
3404 textedit_set_cursor_pos(but, data->region, xy);
3405
3406 but->selsta = but->pos;
3407 but->selend = data->text_edit.sel_pos_init;
3408 if (but->selend < but->selsta) {
3409 std::swap(but->selsta, but->selend);
3410 }
3411
3412 button_update(but);
3413}
3414
3420static bool textedit_insert_buf(Button *but, TextEdit &text_edit, const char *buf, int buf_len)
3421{
3422 int len = strlen(text_edit.edit_string);
3423 const int str_maxncpy_new = len - (but->selend - but->selsta) + 1;
3424 bool changed = false;
3425
3426 if (text_edit.is_str_dynamic) {
3427 textedit_string_ensure_max_length(but, text_edit, str_maxncpy_new + buf_len);
3428 }
3429
3430 if (str_maxncpy_new <= text_edit.max_string_size) {
3431 char *str = text_edit.edit_string;
3432 size_t step = buf_len;
3433
3434 /* type over the current selection */
3435 if ((but->selend - but->selsta) > 0) {
3436 changed = textedit_delete_selection(but, text_edit);
3437 len = strlen(str);
3438 }
3439
3440 if ((len + step >= text_edit.max_string_size) && (text_edit.max_string_size - (len + 1) > 0)) {
3441 if (but_is_utf8(but)) {
3442 /* Shorten 'step' to a UTF8 aligned size that fits. */
3443 BLI_strnlen_utf8_ex(buf, text_edit.max_string_size - (len + 1), &step);
3444 }
3445 else {
3446 step = text_edit.max_string_size - (len + 1);
3447 }
3448 }
3449
3450 if (step && (len + step < text_edit.max_string_size)) {
3451 memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
3452 memcpy(&str[but->pos], buf, step * sizeof(char));
3453 but->pos += step;
3454 changed = true;
3455 }
3456 }
3457
3458 return changed;
3459}
3460
3461#ifdef WITH_INPUT_IME
3462static bool textedit_insert_ascii(Button *but, HandleButtonData *data, const char ascii)
3463{
3464 BLI_assert(isascii(ascii));
3465 const char buf[2] = {ascii, '\0'};
3466 return textedit_insert_buf(but, data->text_edit, buf, sizeof(buf) - 1);
3467}
3468#endif
3469
3470static void textedit_move(Button *but,
3471 TextEdit &text_edit,
3472 eStrCursorJumpDirection direction,
3473 const bool select,
3475 bool jump_all_multiline = false)
3476{
3477 Vector<StringRef> lines = {text_edit.edit_string};
3478 if (but->type == ButtonType::TextBox && jump == STRCUR_JUMP_ALL && !jump_all_multiline) {
3479 lines = textbox_wrap_lines(static_cast<ButtonTextBox *>(but));
3480 }
3481 const char *str = lines.first().begin();
3482 const StringRef line_cursor =
3484 const int pos_prev = but->pos;
3485 const bool has_sel = (but->selend - but->selsta) > 0;
3486
3487 button_update(but);
3488
3489 /* special case, quit selection and set cursor */
3490 if (has_sel && !select) {
3491 if (jump == STRCUR_JUMP_ALL) {
3492 but->selsta = but->selend = but->pos = (direction ? line_cursor.end() :
3493 line_cursor.begin()) -
3494 str;
3495 }
3496 else {
3497 if (direction) {
3498 but->selsta = but->pos = but->selend;
3499 }
3500 else {
3501 but->pos = but->selend = but->selsta;
3502 }
3503 }
3504 text_edit.sel_pos_init = but->pos;
3505 }
3506 else {
3507 int pos_i = but->pos - (line_cursor.data() - str);
3509 line_cursor.data(), line_cursor.size(), &pos_i, direction, jump, true);
3510 but->pos = pos_i + (line_cursor.data() - str);
3511
3512 if (select) {
3513 if (has_sel == false) {
3514 /* Holding shift but with no previous selection. */
3515 but->selsta = but->pos;
3516 but->selend = pos_prev;
3517 }
3518 else if (but->selsta == pos_prev) {
3519 /* Previous selection, extending start position. */
3520 but->selsta = but->pos;
3521 }
3522 else {
3523 /* Previous selection, extending end position. */
3524 but->selend = but->pos;
3525 }
3526 }
3527 if (but->selend < but->selsta) {
3528 std::swap(but->selsta, but->selend);
3529 }
3530 }
3531}
3532
3533static bool textedit_delete(Button *but,
3534 TextEdit &text_edit,
3535 eStrCursorJumpDirection direction,
3537{
3538 char *str = text_edit.edit_string;
3539 const int len = strlen(str);
3540
3541 bool changed = false;
3542
3543 if (jump == STRCUR_JUMP_ALL) {
3544 if (len) {
3545 changed = true;
3546 }
3547 str[0] = '\0';
3548 but->pos = 0;
3549 }
3550 else if (direction) { /* delete */
3551 if ((but->selend - but->selsta) > 0) {
3552 changed = textedit_delete_selection(but, text_edit);
3553 }
3554 else if (but->pos >= 0 && but->pos < len) {
3555 int pos = but->pos;
3556 int step;
3557 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump, true);
3558 step = pos - but->pos;
3559 memmove(&str[but->pos], &str[but->pos + step], (len + 1) - (but->pos + step));
3560 changed = true;
3561 }
3562 }
3563 else { /* backspace */
3564 if (len != 0) {
3565 if ((but->selend - but->selsta) > 0) {
3566 changed = textedit_delete_selection(but, text_edit);
3567 }
3568 else if (but->pos > 0) {
3569 int pos = but->pos;
3570 int step;
3571
3572 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump, true);
3573 step = but->pos - pos;
3574 memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
3575 but->pos -= step;
3576 changed = true;
3577 }
3578 }
3579 }
3580
3581 return changed;
3582}
3583
3585{
3586 char *str = data->text_edit.edit_string;
3587
3588 int changed;
3589 if (data->searchbox) {
3590 changed = searchbox_autocomplete(C, data->searchbox, but, data->text_edit.edit_string);
3591 }
3592 else {
3593 changed = but->autocomplete_func(C, str, but->autofunc_arg);
3594 }
3595
3596 but->pos = strlen(str);
3597 but->selsta = but->selend = but->pos;
3598
3599 return changed;
3600}
3601
3602/* mode for textedit_copypaste() */
3603enum {
3607};
3608
3609static bool textedit_copypaste(Button *but, TextEdit &text_edit, const int mode)
3610{
3611 bool changed = false;
3612
3613 /* paste */
3614 if (mode == UI_TEXTEDIT_PASTE) {
3615 /* extract the first line from the clipboard */
3616 int buf_len;
3617 char *pbuf = but->type != ButtonType::TextBox ?
3618 WM_clipboard_text_get_firstline(false, but_is_utf8(but), &buf_len) :
3619 WM_clipboard_text_get(false, but_is_utf8(but), &buf_len);
3620
3621 if (pbuf) {
3622 textedit_insert_buf(but, text_edit, pbuf, buf_len);
3623
3624 changed = true;
3625
3626 MEM_delete(pbuf);
3627 }
3628 }
3629 /* cut & copy */
3630 else if (ELEM(mode, UI_TEXTEDIT_COPY, UI_TEXTEDIT_CUT)) {
3631 /* copy the contents to the copypaste buffer */
3632 const int sellen = but->selend - but->selsta;
3633 char *buf = MEM_new_array_uninitialized<char>((sellen + 1), __func__);
3634
3635 memcpy(buf, text_edit.edit_string + but->selsta, sellen);
3636 buf[sellen] = '\0';
3637
3638 WM_clipboard_text_set(buf, false);
3639 MEM_delete(buf);
3640
3641 /* for cut only, delete the selection afterwards */
3642 if (mode == UI_TEXTEDIT_CUT) {
3643 if ((but->selend - but->selsta) > 0) {
3644 changed = textedit_delete_selection(but, text_edit);
3645 }
3646 }
3647 }
3648
3649 return changed;
3650}
3651
3652#ifdef WITH_INPUT_IME
3653/* Enable IME, and setup #Button IME data. */
3654static void textedit_ime_begin(wmWindow *win, Button *but)
3655{
3656 if (ELEM(but->type, ButtonType::Num, ButtonType::NumSlider)) {
3657 return;
3658 }
3659
3660 /* XXX Is this really needed? */
3661 int x, y;
3662
3663 /* If we were in an IME elsewhere, end it so we can start fresh. */
3664 WM_window_IME_end(win);
3665
3666 /* enable IME and position to cursor, it's a trick */
3667 x = win->runtime->eventstate->xy[0];
3668 /* flip y and move down a bit, prevent the IME panel cover the edit button */
3669 y = win->runtime->eventstate->xy[1] - 12;
3670
3671 WM_window_IME_begin(win, x, y, 0, 0, bke::wmIMEOwnerType::Button);
3672}
3673
3674/* Disable IME, and clear #Button IME data. */
3675static void textedit_ime_end(wmWindow *win, Button *but)
3676{
3677 if (ELEM(but->type, ButtonType::Num, ButtonType::NumSlider)) {
3678 return;
3679 }
3680 WM_window_IME_end(win);
3681}
3682
3683void button_ime_reposition(Button *but, int x, int y)
3684{
3685 if (ELEM(but->type, ButtonType::Num, ButtonType::NumSlider)) {
3686 return;
3687 }
3688 BLI_assert(but->active || but->semi_modal_state);
3689 HandleButtonData *data = but->semi_modal_state ? but->semi_modal_state : but->active;
3690
3691 region_to_window(data->region, &x, &y);
3692 y -= 4;
3693 wmWindow *win = data->window;
3694 if (win->runtime->ime_owner == bke::wmIMEOwnerType::Button) {
3695 WM_window_IME_reposition(win, x, y, 0, 0);
3696 }
3697 else {
3698 /* The session ended or a region took it while editing, e.g. because a popup opened.
3699 * Begin again for IME to recover, ending first as #textedit_ime_begin does. */
3700 WM_window_IME_end(win);
3701 WM_window_IME_begin(win, x, y, 0, 0, bke::wmIMEOwnerType::Button);
3702 }
3703}
3704
3705const wmIMEData *button_ime_data_get(Button *but)
3706{
3707 HandleButtonData *data = but->semi_modal_state ? but->semi_modal_state : but->active;
3708
3709 /* The IME state is per-window, so don't use a composition a region owns,
3710 * see #wmIMEOwnerType. */
3711 if (data && data->window && data->window->runtime->ime_data_is_composing &&
3712 (data->window->runtime->ime_owner == bke::wmIMEOwnerType::Button))
3713 {
3714 return data->window->runtime->ime_data;
3715 }
3716 return nullptr;
3717}
3718#endif /* WITH_INPUT_IME */
3719
3720std::optional<StringRef> button_edit_unit_hint_get(const Button &but)
3721{
3723 if (data == nullptr || data->text_edit_unit_hint.empty()) {
3724 return std::nullopt;
3725 }
3726 return data->text_edit_unit_hint;
3727}
3728
3729/* Currently there are property subtypes that are not considered "units", so handle them
3730 * separately here. */
3731static std::optional<StringRef> button_edit_unit_hint_get_from_prop_subtype(
3732 const PropertySubType subtype)
3733{
3734 switch (subtype) {
3735 case PROP_PIXEL:
3736 return "px";
3737 case PROP_PERCENTAGE:
3738 return "%";
3739 default:
3740 return {};
3741 }
3742 return {};
3743}
3744
3746{
3747 /* Unit completion (hint) is only done for buttons with a unit or with a property such as
3748 * percentage or pixel for e.g. For everything else, we reset the completion to an empty string.
3749 */
3750 const PropertySubType subtype = but->rnaprop ? RNA_property_subtype(but->rnaprop) : PROP_NONE;
3751 const std::optional<StringRef> subtype_hint = button_edit_unit_hint_get_from_prop_subtype(
3752 subtype);
3753 if (!button_is_unit(but) && !subtype_hint.has_value()) {
3754 data->text_edit_unit_hint.clear();
3755 return;
3756 }
3757
3758 /* Set the completion text (hint) to the unit that is used by this value. */
3759 std::string name_short;
3760 const int unit_type = RNA_SUBTYPE_UNIT_VALUE(button_unit_type_get(but));
3761 if (unit_type != PROP_NONE) {
3762 /* If the string contains the unit already, don't add it as a hint. */
3763 if (BKE_unit_string_contains_unit(data->text_edit.edit_string, unit_type)) {
3764 data->text_edit_unit_hint.clear();
3765 return;
3766 }
3767
3768 /* If the expression we're entering is not valid, don't show the hint. */
3769 if (!BPY_string_compile_check(data->text_edit.edit_string)) {
3770 data->text_edit_unit_hint.clear();
3771 return;
3772 }
3773
3774 const void *usys;
3775 int len;
3776 UnitSettings &unit_settings = CTX_data_scene(C)->unit;
3777 BKE_unit_system_get(unit_settings.system, unit_type, &usys, &len);
3778 const int unit_index = BKE_preffered_unit_of_type_or_base_get(unit_settings, unit_type);
3779 name_short = BKE_unit_display_name_short_get(usys, unit_index);
3780 BLI_assert(!name_short.empty());
3781 }
3782 else if (subtype_hint) {
3783 /* Special handling for some subtypes. */
3784 name_short = *subtype_hint;
3785 /* If the string contains the unit already, don't add it as a hint.
3786 * Note: This is a simple sub-string check and may fail at times. */
3787 const StringRefNull str(data->text_edit.edit_string);
3788 BLI_assert(!name_short.empty());
3789 if (str.find(name_short) != StringRef::not_found) {
3790 data->text_edit_unit_hint.clear();
3791 return;
3792 }
3793
3794 /* If the expression we're entering is not valid, don't show the hint. */
3795 if (!BPY_string_compile_check(data->text_edit.edit_string)) {
3796 data->text_edit_unit_hint.clear();
3797 return;
3798 }
3799 }
3800
3801 if (name_short.empty()) {
3802 data->text_edit_unit_hint.clear();
3803 return;
3804 }
3805
3806 /* Add a space before the short unit name. */
3807 data->text_edit_unit_hint = " " + name_short;
3808}
3809
3811{
3812 TextEdit &text_edit = data->text_edit;
3813 wmWindow *win = data->window;
3814 const bool is_num_but = ELEM(but->type, ButtonType::Num, ButtonType::NumSlider);
3815 const bool is_textbox = ELEM(but->type, ButtonType::TextBox);
3816
3817 bool no_zero_strip = false;
3818
3819 MEM_SAFE_DELETE(text_edit.edit_string);
3820
3821 WorkspaceStatus status(C);
3822
3823#if defined(__APPLE__)
3824 const int ctrl_icon = ICON_KEY_COMMAND;
3825#else
3826 const int ctrl_icon = ICON_EVENT_CTRL;
3827#endif
3828
3829 status.item(IFACE_("Confirm"), ICON_EVENT_RETURN);
3830 if (is_textbox) {
3831 status.item(IFACE_("New Line"), ICON_EVENT_SHIFT, ICON_EVENT_RETURN);
3832 }
3833
3834 status.item(IFACE_("Cancel"), ICON_EVENT_ESC);
3835
3836 if (!is_num_but) {
3837 status.item(IFACE_("Select All"), ctrl_icon, ICON_EVENT_A);
3838 status.item(IFACE_("Copy"), ctrl_icon, ICON_EVENT_C);
3839 status.item(IFACE_("Paste"), ctrl_icon, ICON_EVENT_V);
3840 }
3841
3842 if (but->autocomplete_func || data->searchbox) {
3843 status.item(IFACE_("Autocomplete"), ICON_EVENT_TAB);
3844 }
3845
3846#ifdef USE_DRAG_MULTINUM
3847 /* this can happen from multi-drag */
3848 if (data->applied_interactive) {
3849 /* remove any small changes so canceling edit doesn't restore invalid value: #40538 */
3850 data->cancel = true;
3851 apply_but(C, but->block, but, data, true);
3852 data->cancel = false;
3853
3854 data->applied_interactive = false;
3855 }
3856#endif
3857
3858#ifdef USE_ALLSELECT
3859 if (is_num_but) {
3861 data->select_others.is_enabled = true;
3862 data->select_others.is_copy = true;
3863 }
3864 }
3865#endif
3866
3867 /* retrieve string */
3869 if (text_edit.max_string_size != 0) {
3870 text_edit.edit_string = MEM_new_array_zeroed<char>(text_edit.max_string_size, "textedit str");
3871 /* We do not want to truncate precision to default here, it's nice to show value,
3872 * not to edit it - way too much precision is lost then. */
3874 text_edit.edit_string,
3875 text_edit.max_string_size,
3877 true,
3878 &no_zero_strip);
3879 }
3880 else {
3881 text_edit.is_str_dynamic = true;
3882 text_edit.edit_string = button_string_get_dynamic(but, &text_edit.max_string_size);
3883 }
3884
3885 if (button_is_float(but) && !button_is_unit(but) &&
3886 !button_anim_expression_get(but, nullptr, 0) && !no_zero_strip)
3887 {
3888 BLI_str_rstrip_float_zero(text_edit.edit_string, '\0');
3889 }
3890
3891 if (is_num_but) {
3892 BLI_assert(text_edit.is_str_dynamic == false);
3894
3896 }
3897
3898 /* won't change from now on */
3899 const int len = strlen(text_edit.edit_string);
3900
3901 text_edit.original_string = BLI_strdupn(text_edit.edit_string, len);
3902 text_edit.sel_pos_init = 0;
3903
3904 /* set cursor pos to the end of the text */
3905 but->editstr = text_edit.edit_string;
3906 but->pos = len;
3907 if (bool(but->flag & BUT_ACTIVATE_ON_INIT_NO_SELECT)) {
3908 but->selsta = len;
3909 }
3910 else {
3911 but->selsta = 0;
3912 }
3913 but->selend = len;
3914
3915 /* Initialize undo history tracking. */
3917 textedit_undo_push(text_edit.undo_stack_text, but->editstr, but->pos);
3918
3919 /* optional searchbox */
3920 if (but->type == ButtonType::SearchMenu) {
3921 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
3922
3923 data->searchbox = search_but->popup_create_fn(C, data->region, search_but);
3924 searchbox_update(C, data->searchbox, but, true); /* true = reset */
3925 }
3926
3927 /* reset alert flag (avoid confusion, will refresh on exit) */
3928 but->flag &= ~BUT_REDALERT;
3929
3930 button_update(but);
3931
3932 /* Set the edit unit hint if needed. */
3934
3935 /* Make sure the edited button is in view. */
3936 if (data->searchbox) {
3937 /* Popup blocks don't support moving after creation, so don't change the view for them. */
3938 }
3939 else if (block_layout_needs_resolving(but->block)) {
3940 /* Layout isn't resolved yet (may happen when activating while drawing through
3941 * #button_active_only()), so can't move it into view yet. This causes
3942 * #but_update_view_for_active() to run after the layout is resolved. */
3943 but->changed = true;
3944 }
3945 else if ((but->block->flag & BLOCK_CLIP_EVENTS) == 0) {
3946 /* Blocks with BLOCK_CLIP_EVENTS are overlapping their region, so scrolling
3947 * that region to ensure it is in view can't work and causes issues. #97530 */
3948 but_ensure_in_view(C, data->region, but);
3949 }
3950 /* Text buttons already shows text exit cursor. */
3951 if (but->type != ButtonType::TextBox) {
3953 }
3954
3955 /* Temporarily turn off window auto-focus on platforms that support it. */
3956 GHOST_ISystem *ghost_system = GHOST_ISystem::getSystem();
3957 ghost_system->setAutoFocus(false);
3958
3959#ifdef WITH_INPUT_IME
3960 if (!is_num_but) {
3961 textedit_ime_begin(win, but);
3962 }
3963#endif
3964}
3965
3967{
3968 TextEdit &text_edit = data->text_edit;
3969 wmWindow *win = data->window;
3970
3971 ED_workspace_status_text(C, nullptr);
3972
3973 if (but) {
3974 if (but_is_utf8(but)) {
3975 const int strip = BLI_str_utf8_invalid_strip(but->editstr, strlen(but->editstr));
3976 /* Strip non-UTF8 characters unless buttons support this.
3977 * This should never happen as all text input should be valid UTF8,
3978 * there is a small chance existing data contains invalid sequences.
3979 * This could check could be made into an assertion if `but->editstr`
3980 * is valid UTF8 when #textedit_begin assigns the string. */
3981 if (strip) {
3982 CLOG_INFO_NOCHECK(&LOG, "%s: invalid utf8 - stripped chars %d", __func__, strip);
3983 }
3984 }
3985
3986 if (data->searchbox) {
3987 if (data->cancel == false) {
3989 ButtonSearch *but_search = static_cast<ButtonSearch *>(but);
3990
3991 if ((searchbox_apply(but, data->searchbox) == false) &&
3992 (searchbox_find_index(data->searchbox, but->editstr) == -1) &&
3993 !but_search->results_are_suggestions)
3994 {
3995
3996 if (but->flag & BUT_VALUE_CLEAR) {
3997 /* It is valid for _VALUE_CLEAR flavor to have no active element
3998 * (it's a valid way to unlink). */
3999 but->editstr[0] = '\0';
4000 }
4001 data->cancel = true;
4002
4003 /* ensure menu (popup) too is closed! */
4004 data->escapecancel = true;
4005
4006 if (but->editstr[0]) {
4007 WM_global_reportf(RPT_ERROR, "Failed to find '%s'", but->editstr);
4009 }
4010 }
4011 }
4012
4013 searchbox_free(C, data->searchbox);
4014 data->searchbox = nullptr;
4015 }
4016
4017 but->editstr = nullptr;
4018 but->pos = -1;
4019 }
4020
4022
4023 /* Turn back on the auto-focusing of windows. */
4024 GHOST_ISystem *ghost_system = GHOST_ISystem::getSystem();
4025 ghost_system->setAutoFocus(true);
4026
4027 /* Free text undo history text blocks. */
4029 text_edit.undo_stack_text = nullptr;
4030
4031#ifdef WITH_INPUT_IME
4032 textedit_ime_end(win, but);
4033#endif
4034}
4035
4037{
4038 /* Label and round-box can overlap real buttons (backdrops...). */
4039 if (ELEM(actbut->type,
4045 {
4046 return;
4047 }
4048
4049 for (Button &but : block->buttons() | std::views::drop(block->but_index(actbut) + 1)) {
4050 if (button_is_editable_as_text(&but)) {
4051 if (!(but.flag & (BUT_DISABLED | UI_HIDDEN))) {
4052 data->postbut = &but;
4054 return;
4055 }
4056 }
4057 }
4058 for (Button &but : block->buttons()) {
4059 if (&but == actbut) {
4060 break;
4061 }
4062 if (button_is_editable_as_text(&but)) {
4063 if (!(but.flag & (BUT_DISABLED | UI_HIDDEN))) {
4064 data->postbut = &but;
4066 return;
4067 }
4068 }
4069 }
4070}
4071
4073{
4074 /* Label and round-box can overlap real buttons (backdrops...). */
4075 if (ELEM(actbut->type,
4081 {
4082 return;
4083 }
4084
4085 for (Button &but :
4086 block->buttons() | std::views::take(block->but_index(actbut)) | std::views::reverse)
4087 {
4088 if (button_is_editable_as_text(&but)) {
4089 if (!(but.flag & (BUT_DISABLED | UI_HIDDEN))) {
4090 data->postbut = &but;
4092 return;
4093 }
4094 }
4095 }
4096 for (Button &but : block->buttons() | std::views::reverse) {
4097 if (&but == actbut) {
4098 break;
4099 }
4100 if (button_is_editable_as_text(&but)) {
4101 if (!(but.flag & (BUT_DISABLED | UI_HIDDEN))) {
4102 data->postbut = &but;
4104 return;
4105 }
4106 }
4107 }
4108}
4109
4114{
4115#if defined(__APPLE__)
4116 if (event->modifier & KM_OSKEY) {
4117 return STRCUR_JUMP_ALL;
4118 }
4119 if (event->modifier & KM_ALT) {
4120 return STRCUR_JUMP_DELIM;
4121 }
4122#else
4123 if (event->modifier & KM_CTRL) {
4124 return STRCUR_JUMP_DELIM;
4125 }
4126#endif
4127 return STRCUR_JUMP_NONE;
4128}
4129
4131 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
4132{
4133 TextEdit &text_edit = data->text_edit;
4134 int retval = WM_UI_HANDLER_CONTINUE;
4135 bool changed = false, inbox = false, update = false, skip_undo_push = false;
4136
4137#ifdef WITH_INPUT_IME
4138 wmWindow *win = CTX_wm_window(C);
4139 const wmIMEData *ime_data = win->runtime->ime_data;
4140 const bool is_ime_composing = ime_data && win->runtime->ime_data_is_composing;
4141#else
4142 const bool is_ime_composing = false;
4143#endif
4144 ButtonTextBox *textbox = but->type == ButtonType::TextBox ? static_cast<ButtonTextBox *>(but) :
4145 nullptr;
4146 int prev_pos = but->pos;
4147 switch (event->type) {
4148 case MOUSEMOVE:
4149 case MOUSEPAN:
4150 if (data->searchbox) {
4151#ifdef USE_KEYNAV_LIMIT
4152 if ((event->type == MOUSEMOVE) &&
4153 mouse_motion_keynav_test(&data->searchbox_keynav_state, event))
4154 {
4155 /* pass */
4156 }
4157 else {
4158 searchbox_event(C, data->searchbox, but, data->region, event);
4159 }
4160#else
4161 searchbox_event(C, data->searchbox, but, data->region, event);
4162#endif
4163 }
4165
4166 break;
4167 case RIGHTMOUSE:
4168 case EVT_ESCKEY:
4169 /* Don't consume cancel events (would usually end text editing), let menu code handle it. */
4170 if (data->is_semi_modal) {
4171 break;
4172 }
4173 if (event->val == KM_PRESS) {
4174 /* Support search context menu. */
4175 if (event->type == RIGHTMOUSE) {
4176 if (data->searchbox) {
4177 if (searchbox_event(C, data->searchbox, but, data->region, event)) {
4178 /* Only break if the event was handled. */
4179 break;
4180 }
4181 }
4182 }
4183
4184#ifdef WITH_INPUT_IME
4185 /* skips button handling since it is not wanted */
4186 if (is_ime_composing) {
4187 break;
4188 }
4189#endif
4190 data->cancel = true;
4191 data->escapecancel = true;
4193 retval = WM_UI_HANDLER_BREAK;
4194 }
4195 break;
4196 case LEFTMOUSE: {
4197 /* Allow clicks on extra icons while editing. */
4198 if (do_but_extra_operator_icon(C, but, data, event)) {
4199 break;
4200 }
4201
4202 const bool had_selection = but->selsta != but->selend;
4203
4204 /* exit on LMB only on RELEASE for searchbox, to mimic other popups,
4205 * and allow multiple menu levels */
4206 if (data->searchbox) {
4207 inbox = searchbox_inside(data->searchbox, event->xy);
4208 }
4209
4210 bool is_press_in_button = false;
4211 if (ELEM(event->val, KM_PRESS, KM_DBL_CLICK)) {
4212 float mx = event->xy[0];
4213 float my = event->xy[1];
4214 window_to_block_fl(data->region, block, &mx, &my);
4215
4216 if (button_contains_pt(but, mx, my)) {
4217 is_press_in_button = true;
4218 }
4219 }
4220
4221 /* for double click: we do a press again for when you first click on button
4222 * (selects all text, no cursor pos) */
4223 if (ELEM(event->val, KM_PRESS, KM_DBL_CLICK)) {
4224 if (is_press_in_button) {
4225 /* Extend text selection when holding shift. */
4226 if (event->modifier & KM_SHIFT) {
4227 text_edit.sel_pos_init = but->pos == but->selsta ? but->selend : but->selsta;
4229 }
4230 else {
4231 textedit_set_cursor_pos(but, data->region, float2(event->xy));
4232 but->selsta = but->selend = but->pos;
4233 text_edit.sel_pos_init = but->pos;
4234 }
4235
4237 retval = WM_UI_HANDLER_BREAK;
4238 }
4239 else if (inbox == false && !data->is_semi_modal) {
4240 /* if searchbox, click outside will cancel */
4241 if (data->searchbox) {
4242 data->cancel = data->escapecancel = true;
4243 }
4244#ifdef WITH_INPUT_IME
4245 else if (is_ime_composing && !ime_data->composite.empty() &&
4246 but->type == ButtonType::Text)
4247 {
4249 but, text_edit, ime_data->composite.c_str(), ime_data->composite.size());
4250 }
4251#endif
4253 retval = WM_UI_HANDLER_BREAK;
4254 }
4255 }
4256
4257 /* only select a word in button if there was no selection before */
4258 if (event->val == KM_DBL_CLICK && had_selection == false) {
4259 if (is_press_in_button) {
4260 const int str_len = strlen(text_edit.edit_string);
4261 /* This may not be necessary, additional check to ensure `pos` is never out of range,
4262 * since negative values aren't acceptable, see: #113154. */
4263 CLAMP(but->pos, 0, str_len);
4264
4265 int selsta, selend;
4267 text_edit.edit_string, str_len, but->pos, &selsta, &selend);
4268 but->pos = selend;
4269 but->selsta = selsta;
4270 but->selend = selend;
4271 /* Anchor selection to the left side unless the last word. */
4272 text_edit.sel_pos_init = ((selend == str_len) && (selsta != 0)) ? selend : selsta;
4273 retval = WM_UI_HANDLER_BREAK;
4274 changed = true;
4275 }
4276 }
4277 else if (inbox && !data->is_semi_modal) {
4278 /* if we allow activation on key press,
4279 * it gives problems launching operators #35713. */
4280 if (event->val == KM_RELEASE) {
4282 retval = WM_UI_HANDLER_BREAK;
4283 }
4284 }
4285 break;
4286 }
4287 default: {
4288 break;
4289 }
4290 }
4291
4292 if (event->val == KM_PRESS && !is_ime_composing) {
4293 switch (event->type) {
4294 case EVT_VKEY:
4295 case EVT_XKEY:
4296 case EVT_CKEY:
4297#if defined(__APPLE__)
4298 if (ELEM(event->modifier, KM_OSKEY, KM_CTRL))
4299#else
4300 if (event->modifier == KM_CTRL)
4301#endif
4302 {
4303 if (event->type == EVT_VKEY) {
4304 changed = textedit_copypaste(but, text_edit, UI_TEXTEDIT_PASTE);
4305 }
4306 else if (event->type == EVT_CKEY) {
4307 changed = textedit_copypaste(but, text_edit, UI_TEXTEDIT_COPY);
4308 }
4309 else if (event->type == EVT_XKEY) {
4310 changed = textedit_copypaste(but, text_edit, UI_TEXTEDIT_CUT);
4311 }
4312
4313 retval = WM_UI_HANDLER_BREAK;
4314 }
4315 break;
4316 case EVT_RIGHTARROWKEY:
4317 case EVT_LEFTARROWKEY: {
4318 const eStrCursorJumpDirection direction = (event->type == EVT_RIGHTARROWKEY) ?
4322 textedit_move(but, text_edit, direction, event->modifier & KM_SHIFT, jump);
4323 retval = WM_UI_HANDLER_BREAK;
4324 break;
4325 }
4326 case WHEELDOWNMOUSE:
4327 case EVT_DOWNARROWKEY:
4328 if (data->searchbox) {
4329#ifdef USE_KEYNAV_LIMIT
4330 mouse_motion_keynav_init(&data->searchbox_keynav_state, event);
4331#endif
4332 searchbox_event(C, data->searchbox, but, data->region, event);
4333 break;
4334 }
4335 if (textbox && event->type == EVT_DOWNARROWKEY) {
4337 retval = WM_UI_HANDLER_BREAK;
4338 break;
4339 }
4340 if (event->type == WHEELDOWNMOUSE) {
4341 break;
4342 }
4344 case EVT_ENDKEY:
4345 textedit_move(but,
4346 text_edit,
4348 event->modifier & KM_SHIFT,
4350 event->modifier & KM_CTRL);
4351 retval = WM_UI_HANDLER_BREAK;
4352 break;
4353 case WHEELUPMOUSE:
4354 case EVT_UPARROWKEY:
4355 if (data->searchbox) {
4356#ifdef USE_KEYNAV_LIMIT
4357 mouse_motion_keynav_init(&data->searchbox_keynav_state, event);
4358#endif
4359 searchbox_event(C, data->searchbox, but, data->region, event);
4360 break;
4361 }
4362 if (textbox && event->type == EVT_UPARROWKEY) {
4364 retval = WM_UI_HANDLER_BREAK;
4365 break;
4366 }
4367 if (event->type == WHEELUPMOUSE) {
4368 break;
4369 }
4371 case EVT_HOMEKEY:
4372 textedit_move(but,
4373 text_edit,
4375 event->modifier & KM_SHIFT,
4377 event->modifier & KM_CTRL);
4378 retval = WM_UI_HANDLER_BREAK;
4379 break;
4380 case EVT_PADENTER:
4381 case EVT_RETKEY:
4382 if (but->type == ButtonType::TextBox && event->modifier & KM_SHIFT) {
4383 char utf8_buf[2] = "\n";
4384 textedit_insert_buf(but, text_edit, utf8_buf, 1);
4385 but->selsta = but->selend = but->pos;
4386 changed = true;
4387 }
4388 else {
4390 }
4391 retval = WM_UI_HANDLER_BREAK;
4392 break;
4393 case EVT_DELKEY:
4394 case EVT_BACKSPACEKEY: {
4395 const eStrCursorJumpDirection direction = (event->type == EVT_DELKEY) ? STRCUR_DIR_NEXT :
4398 changed = textedit_delete(but, text_edit, direction, jump);
4399 retval = WM_UI_HANDLER_BREAK;
4400 break;
4401 }
4402
4403 case EVT_AKEY:
4404
4405 /* Ctrl-A: Select all. */
4406#if defined(__APPLE__)
4407 /* OSX uses Command-A system-wide, so add it. */
4408 if (ELEM(event->modifier, KM_OSKEY, KM_CTRL))
4409#else
4410 if (event->modifier == KM_CTRL)
4411#endif
4412 {
4413 textedit_move(but, text_edit, STRCUR_DIR_PREV, false, STRCUR_JUMP_ALL, true);
4414 textedit_move(but, text_edit, STRCUR_DIR_NEXT, true, STRCUR_JUMP_ALL, true);
4415 retval = WM_UI_HANDLER_BREAK;
4416 }
4417 break;
4418
4419 case EVT_TABKEY:
4420 /* There is a key conflict here, we can't tab with auto-complete. */
4421 if (but->autocomplete_func || data->searchbox) {
4422 const int autocomplete = textedit_autocomplete(C, but, data);
4423 changed = autocomplete != AUTOCOMPLETE_NO_MATCH;
4424
4425 if (autocomplete == AUTOCOMPLETE_FULL_MATCH) {
4427 /* Exit to apply, then re-activate (as with Tab cycling between text fields),
4428 * so this only runs when Tab is pressed, see: #150689. */
4430 data->postbut = but;
4432 }
4434 }
4435 }
4436 else if ((event->modifier & ~KM_SHIFT) == 0) {
4437 /* Use standard keys for cycling through buttons Tab, Shift-Tab to reverse. */
4438 if (event->modifier & KM_SHIFT) {
4439 textedit_prev_but(block, but, data);
4440 }
4441 else {
4442 textedit_next_but(block, but, data);
4443 }
4445 }
4446 retval = WM_UI_HANDLER_BREAK;
4447 break;
4448 case EVT_ZKEY: {
4449 /* Ctrl-Z or Ctrl-Shift-Z: Undo/Redo (allowing for OS-Key on Apple). */
4450
4451 const bool is_redo = (event->modifier & KM_SHIFT);
4452 if (
4453#if defined(__APPLE__)
4454 ((event->modifier & KM_OSKEY) && ((event->modifier & (KM_ALT | KM_CTRL)) == 0)) ||
4455#endif
4456 ((event->modifier & KM_CTRL) && ((event->modifier & (KM_ALT | KM_OSKEY)) == 0)))
4457 {
4458 int undo_pos;
4459 const char *undo_str = textedit_undo(
4460 text_edit.undo_stack_text, is_redo ? 1 : -1, &undo_pos);
4461 if (undo_str != nullptr) {
4462 textedit_string_set(but, text_edit, undo_str);
4463
4464 /* Set the cursor & clear selection. */
4465 but->pos = undo_pos;
4466 but->selsta = but->pos;
4467 but->selend = but->pos;
4468 changed = true;
4469 }
4470 retval = WM_UI_HANDLER_BREAK;
4471 skip_undo_push = true;
4472 }
4473 break;
4474 }
4475 default: {
4476 break;
4477 }
4478 }
4479
4480 if ((event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)
4481#ifdef WITH_INPUT_IME
4482 && !is_ime_composing && !WM_event_is_ime_switch(event)
4483#endif
4484 )
4485 {
4486 char utf8_buf_override[2] = {'\0', '\0'};
4487 const char *utf8_buf = event->utf8_buf;
4488
4489 /* Exception that's useful for number buttons, some keyboard
4490 * numpads have a comma instead of a period. */
4491 if (ELEM(but->type, ButtonType::Num, ButtonType::NumSlider)) { /* Could use `data->min`. */
4492 if ((event->type == EVT_PADPERIOD) && (utf8_buf[0] == ',')) {
4493 utf8_buf_override[0] = '.';
4494 utf8_buf = utf8_buf_override;
4495 }
4496 }
4497
4498 if (utf8_buf[0]) {
4499 const int utf8_buf_len = BLI_str_utf8_size_or_error(utf8_buf);
4500 BLI_assert(utf8_buf_len != -1);
4501 changed = textedit_insert_buf(but, text_edit, utf8_buf, utf8_buf_len);
4502 }
4503
4504 retval = WM_UI_HANDLER_BREAK;
4505 }
4506 /* textbutton with this flag: do live update (e.g. for search buttons) */
4507 if (but->flag & BUT_TEXTEDIT_UPDATE) {
4508 update = true;
4509 }
4510 }
4511
4512#ifdef WITH_INPUT_IME
4513 if (event->type == WM_IME_COMPOSITE_START) {
4514 changed = true;
4515 if (but->selend > but->selsta) {
4516 textedit_delete_selection(but, text_edit);
4517 }
4518 }
4519 else if (event->type == WM_IME_COMPOSITE_EVENT) {
4520 changed = true;
4521 if (!ime_data->result.empty()) {
4523 STREQ(ime_data->result.c_str(), "\xE3\x80\x82"))
4524 {
4525 /* Convert Ideographic Full Stop (U+3002) to decimal point when entering numbers. */
4526 textedit_insert_ascii(but, data, '.');
4527 }
4528 else {
4529 textedit_insert_buf(but, text_edit, ime_data->result.c_str(), ime_data->result.size());
4530 }
4531 }
4532 }
4533 else if (event->type == WM_IME_COMPOSITE_END) {
4534 changed = true;
4535 }
4536#endif
4537 if (textbox && changed) {
4538 /* Text changed, invalidate cache now. */
4539 textbox->wrap_cache.reset();
4540 }
4541 if (textbox && (changed || prev_pos != but->pos) && data->state != BUTTON_STATE_EXIT) {
4542 textbox_scroll_to_cursor(textbox);
4543 }
4544 if (changed) {
4545 /* The undo stack may be nullptr if an event exits editing. */
4546 if ((skip_undo_push == false) && (text_edit.undo_stack_text != nullptr)) {
4547 textedit_undo_push(text_edit.undo_stack_text, text_edit.edit_string, but->pos);
4548 }
4549
4551
4552 /* only do live update when but flag request it (BUT_TEXTEDIT_UPDATE). */
4553 if (update && data->interactive) {
4554 apply_but(C, block, but, data, true);
4555 }
4556 else {
4558 }
4559 but->changed = true;
4560
4561 if (data->searchbox) {
4562 searchbox_update(C, data->searchbox, but, true); /* true = reset */
4563 }
4564 }
4565
4566 if (changed || (retval == WM_UI_HANDLER_BREAK)) {
4567 ED_region_tag_redraw(data->region);
4568 if (!data->searchbox) {
4569 /* In case of popup regions, tag for popup refreshing too (contents may have changed). Not
4570 * done for search-boxes, since they have their own update handling. */
4572 }
4573 }
4574
4575 /* Swallow all events unless semi-modal handling is requested. */
4576 return data->is_semi_modal ? retval : WM_UI_HANDLER_BREAK;
4577}
4578
4580 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
4581{
4582 int retval = WM_UI_HANDLER_CONTINUE;
4583 ButtonTextBox *textbox = but->type == ButtonType::TextBox ? static_cast<ButtonTextBox *>(but) :
4584 nullptr;
4585
4586 switch (event->type) {
4587 case TIMER: {
4588 if (!textbox || event->customdata != data->text_select_auto_scroll) {
4589 break;
4590 }
4591
4592 const wmTimer *timer = static_cast<const wmTimer *>(event->customdata);
4593 if (timer->time_duration == data->text_select_auto_scroll_last_time) {
4594 retval = WM_UI_HANDLER_BREAK;
4595 break;
4596 }
4597 data->text_select_auto_scroll_last_time = timer->time_duration;
4598
4599 rctf rect;
4600 block_to_window_rctf(data->region, block, &rect, &but->rect);
4601
4602 rect.ymax -= textbox_vertical_padding() / block->aspect;
4603 rect.ymin += textbox_vertical_padding() / block->aspect;
4604
4605 if (BLI_rctf_isect_y(&rect, event->xy[1])) {
4606 break;
4607 }
4608 retval = WM_UI_HANDLER_BREAK;
4609 textbox_add_scroll(textbox, (rect.ymax < event->xy[1] ? -1 : 1));
4611 break;
4612 }
4613 case WHEELUPMOUSE:
4614 case WHEELDOWNMOUSE: {
4615 if (!textbox) {
4616 break;
4617 }
4618 textbox_add_scroll(textbox, (event->type == WHEELUPMOUSE ? -1 : 1));
4620 retval = WM_UI_HANDLER_BREAK;
4621 break;
4622 }
4623 case MOUSEMOVE: {
4625 retval = WM_UI_HANDLER_BREAK;
4626 break;
4627 }
4628 case LEFTMOUSE:
4629 if (event->val == KM_RELEASE) {
4630 if (textbox) {
4631 textbox_scroll_to_cursor(textbox);
4632 }
4634 }
4635 retval = WM_UI_HANDLER_BREAK;
4636 break;
4637 default: {
4638 break;
4639 }
4640 }
4641
4642 if (retval == WM_UI_HANDLER_BREAK) {
4643 button_update(but);
4644 ED_region_tag_redraw(data->region);
4645 }
4646
4647 return retval;
4648}
4649
4651
4652/* -------------------------------------------------------------------- */
4655
4657{
4658 data->startvalue = button_value_get(but);
4659 data->origvalue = data->startvalue;
4660 data->value = data->origvalue;
4661}
4662
4664{
4665#ifdef WITH_INPUT_IME
4666 WM_window_IME_end(data->window);
4667#endif
4668
4669 if (but->type == ButtonType::Curve) {
4670 ButtonCurveMapping *but_cumap = static_cast<ButtonCurveMapping *>(but);
4671 but_cumap->edit_cumap = reinterpret_cast<CurveMapping *>(but->poin);
4672 }
4673 else if (but->type == ButtonType::CurveProfile) {
4674 ButtonCurveProfile *but_profile = static_cast<ButtonCurveProfile *>(but);
4675 but_profile->edit_profile = reinterpret_cast<CurveProfile *>(but->poin);
4676 }
4677 else if (but->type == ButtonType::ColorBand) {
4678 ButtonColorBand *but_coba = static_cast<ButtonColorBand *>(but);
4679 data->coba = reinterpret_cast<ColorBand *>(but->poin);
4680 but_coba->edit_coba = data->coba;
4681 }
4682 else if (ELEM(but->type,
4687 {
4688 button_v3_get(but, data->origvec);
4689 copy_v3_v3(data->vec, data->origvec);
4690 but->editvec = data->vec;
4691 }
4692 else {
4694 but->editval = &data->value;
4695
4696 float softmin = but->softmin;
4697 float softmax = but->softmax;
4698 float softrange = softmax - softmin;
4699 const PropertyScaleType scale_type = button_scale_type(but);
4700
4701 float log_min = (scale_type == PROP_SCALE_LOG) ? max_ff(softmin, UI_PROP_SCALE_LOG_MIN) : 0.0f;
4702
4703 if ((but->type == ButtonType::Num) && (button_is_cursor_warp(but) == false)) {
4704 ButtonNumber *number_but = static_cast<ButtonNumber *>(but);
4705
4706 if (scale_type == PROP_SCALE_LOG) {
4707 log_min = max_ff(log_min, powf(10, -number_but->precision) * 0.5f);
4708 }
4709 /* Use a minimum so we have a predictable range,
4710 * otherwise some float buttons get a large range. */
4711 const float value_step_float_min = 0.1f;
4712 const bool is_float = button_is_float(but);
4713 const double value_step = is_float ?
4714 double(number_but->step_size * UI_PRECISION_FLOAT_SCALE) :
4715 int(number_but->step_size);
4716 const float drag_map_softrange_max = UI_DRAG_MAP_SOFT_RANGE_PIXEL_MAX * UI_SCALE_FAC;
4717 const float softrange_max = min_ff(
4718 softrange,
4719 2 * (is_float ? min_ff(value_step, value_step_float_min) *
4720 (drag_map_softrange_max / value_step_float_min) :
4721 drag_map_softrange_max));
4722
4723 if (softrange > softrange_max) {
4724 /* Center around the value, keeping in the real soft min/max range. */
4725 softmin = data->origvalue - (softrange_max / 2);
4726 softmax = data->origvalue + (softrange_max / 2);
4727 if (!isfinite(softmin)) {
4728 softmin = (data->origvalue > 0.0f ? FLT_MAX : -FLT_MAX);
4729 }
4730 if (!isfinite(softmax)) {
4731 softmax = (data->origvalue > 0.0f ? FLT_MAX : -FLT_MAX);
4732 }
4733
4734 if (softmin < but->softmin) {
4735 softmin = but->softmin;
4736 softmax = softmin + softrange_max;
4737 }
4738 else if (softmax > but->softmax) {
4739 softmax = but->softmax;
4740 softmin = softmax - softrange_max;
4741 }
4742
4743 /* Can happen at extreme values. */
4744 if (softmin == softmax) [[unlikely]] {
4745 if (data->origvalue > 0.0) {
4746 softmin = nextafterf(softmin, -FLT_MAX);
4747 }
4748 else {
4749 softmax = nextafterf(softmax, FLT_MAX);
4750 }
4751 }
4752
4753 softrange = softmax - softmin;
4754 }
4755 }
4756
4757 if (softrange == 0.0f) {
4758 data->dragfstart = 0.0f;
4759 }
4760 else {
4761 switch (scale_type) {
4762 case PROP_SCALE_LINEAR: {
4763 data->dragfstart = (float(data->value) - softmin) / softrange;
4764 break;
4765 }
4766 case PROP_SCALE_LOG: {
4767 BLI_assert(log_min != 0.0f);
4768 const float base = softmax / log_min;
4769 data->dragfstart = logf(float(data->value) / log_min) / logf(base);
4770 break;
4771 }
4772 case PROP_SCALE_CUBIC: {
4773 const float cubic_min = cube_f(softmin);
4774 const float cubic_max = cube_f(softmax);
4775 const float cubic_range = cubic_max - cubic_min;
4776 const float f = (float(data->value) - softmin) * cubic_range / softrange + cubic_min;
4777 data->dragfstart = (cbrtf(f) - softmin) / softrange;
4778 break;
4779 }
4780 }
4781 }
4782 data->dragf = data->dragfstart;
4783
4784 data->drag_map_soft_min = softmin;
4785 data->drag_map_soft_max = softmax;
4786 }
4787
4788 data->dragchange = false;
4789 data->draglock = true;
4790}
4791
4793{
4794 but->editval = nullptr;
4795 but->editvec = nullptr;
4796 if (but->type == ButtonType::ColorBand) {
4797 ButtonColorBand *but_coba = static_cast<ButtonColorBand *>(but);
4798 but_coba->edit_coba = nullptr;
4799 }
4800 else if (but->type == ButtonType::Curve) {
4801 ButtonCurveMapping *but_cumap = static_cast<ButtonCurveMapping *>(but);
4802 but_cumap->edit_cumap = nullptr;
4803 }
4804 else if (but->type == ButtonType::CurveProfile) {
4805 ButtonCurveProfile *but_profile = static_cast<ButtonCurveProfile *>(but);
4806 but_profile->edit_profile = nullptr;
4807 }
4808 data->dragstartx = 0;
4809 data->draglastx = 0;
4810 data->dragchange = false;
4811 data->dragcbd = nullptr;
4812 data->dragsel = 0;
4813}
4814
4816{
4817 if (data->interactive) {
4818 apply_but(C, block, but, data, true);
4819 }
4820 else {
4821 button_update(but);
4822 }
4823
4824 ED_region_tag_redraw(data->region);
4825}
4826
4828{
4829 but->active->apply_through_extra_icon = true;
4830
4831 if (but->active->interactive) {
4832 apply_but(C, but->block, but, but->active, true);
4833 }
4836 op_icon->optype_params->optype,
4837 op_icon->optype_params->opcontext,
4838 op_icon->optype_params->opptr,
4839 nullptr,
4840 "");
4841
4842 /* Force recreation of extra operator icons (pseudo update). */
4844
4846}
4847
4849
4850/* -------------------------------------------------------------------- */
4853
4855{
4856 BlockCreateFunc func = nullptr;
4857 BlockHandleCreateFunc handlefunc = nullptr;
4858 MenuCreateFunc menufunc = nullptr;
4859 MenuCreateFunc popoverfunc = nullptr;
4860 /* The checks for the panel type being null are for exceptional cases where script
4861 * authors intentionally unregister built-in panels for example.
4862 * While this should only ever happen rarely, it shouldn't crash, see #144716. */
4863 PanelType *popover_panel_type = nullptr;
4864 void *arg = nullptr;
4865
4866 if (but->type != ButtonType::Pulldown) {
4867 /* Clear the status bar. */
4868 WorkspaceStatus status(C);
4869 status.item(" ", ICON_NONE);
4870 }
4871
4872 switch (but->type) {
4873 case ButtonType::Block:
4875 if (but->menu_create_func) {
4876 menufunc = but->menu_create_func;
4877 arg = but->poin;
4878 }
4879 else {
4880 func = but->block_create_func;
4881 arg = but->poin ? but->poin : but->func_argN;
4882 }
4883 break;
4884 case ButtonType::Menu:
4886 if (button_menu_draw_as_popover(but)) {
4887 const char *idname = static_cast<const char *>(but->func_argN);
4888 popover_panel_type = WM_paneltype_find(idname, false);
4889 }
4890
4891 if (popover_panel_type) {
4892 popoverfunc = but->menu_create_func;
4893 }
4894 else {
4895 menufunc = but->menu_create_func;
4896 arg = but->poin;
4897 }
4898 break;
4901 popoverfunc = but->menu_create_func;
4902 popover_panel_type = reinterpret_cast<PanelType *>(but->poin);
4903 break;
4904 case ButtonType::Color:
4905 button_v3_get(but, data->origvec);
4906 copy_v3_v3(data->vec, data->origvec);
4907 but->editvec = data->vec;
4908
4909 if (button_menu_draw_as_popover(but)) {
4910 const char *idname = static_cast<const char *>(but->func_argN);
4911 popover_panel_type = WM_paneltype_find(idname, false);
4912 }
4913
4914 if (popover_panel_type) {
4915 popoverfunc = but->menu_create_func;
4916 }
4917 else {
4918 handlefunc = block_func_COLOR;
4919 }
4920 arg = but;
4921 break;
4922
4923 /* quiet warnings for unhandled types */
4924 default:
4925 break;
4926 }
4927
4928 if (func || handlefunc) {
4929 data->menu = popup_block_create(C, data->region, but, func, handlefunc, arg, nullptr, false);
4930 if (but->block->handle) {
4931 data->menu->popup = but->block->handle->popup;
4932 }
4933 }
4934 else if (menufunc) {
4935 data->menu = popup_menu_create(
4936 C,
4937 data->region,
4938 but,
4939 menufunc,
4940 arg,
4941 /* Inherit the `can_refresh` flag from the parent menu, if any. */
4942 but->block->handle ? but->block->handle->can_refresh : false);
4943 if (MenuType *mt = button_menutype_get(but)) {
4944 STRNCPY_UTF8(data->menu->menu_idname, mt->idname);
4945 }
4946 if (but->block->handle) {
4947 data->menu->popup = but->block->handle->popup;
4948 }
4949 }
4950 else if (popoverfunc) {
4951 data->menu = popover_panel_create(C, data->region, but, popoverfunc, popover_panel_type);
4952 if (but->block->handle) {
4953 data->menu->popup = but->block->handle->popup;
4954 }
4955 }
4956
4957#ifdef USE_ALLSELECT
4958 {
4959 if (IS_ALLSELECT_EVENT(data->window->runtime->eventstate)) {
4960 data->select_others.is_enabled = true;
4961 }
4962 }
4963#endif
4964
4965 /* Force new region handler to run, in case that needs to activate some state (e.g. to handle
4966 * #BUT_FORCE_SEMI_MODAL_ACTIVE). */
4968
4969 /* this makes adjacent blocks auto open from now on */
4970 // if (but->block->auto_open == 0) {
4971 // but->block->auto_open = 1;
4972 //}
4973}
4974
4976{
4977 if (but) {
4978 but->editval = nullptr;
4979 but->editvec = nullptr;
4980
4982 }
4983
4984 ED_workspace_status_text(C, nullptr);
4985
4986 if (data->menu) {
4987 popup_block_free(C, data->menu);
4988 data->menu = nullptr;
4989 }
4990}
4991
4993{
4995
4996 if (data && data->menu) {
4997 return data->menu->direction;
4998 }
4999
5000 return 0;
5001}
5002
5008 Button *but,
5010 const wmEvent *event,
5011 ButtonActivateType activate_type)
5012{
5013 ARegion *region = data->region;
5014 Button *labelbut = button_find_mouse_over_ex(region, event->xy, true, false, nullptr, nullptr);
5015
5016 if (labelbut && labelbut->type == ButtonType::Text && !(labelbut->flag & BUT_DISABLED)) {
5017 /* Exit list-row. */
5018 data->cancel = true;
5019 button_activate_exit(C, but, data, false, false);
5020
5021 /* Activate the text button. */
5022 button_activate_init(C, region, labelbut, activate_type);
5023
5024 return labelbut;
5025 }
5026 return nullptr;
5027}
5028
5030
5031/* -------------------------------------------------------------------- */
5034
5036 ARegion *region,
5037 const wmEvent *event)
5038{
5039 if (but->extra_op_icons.is_empty()) {
5040 return nullptr;
5041 }
5042
5043 int x = event->xy[0], y = event->xy[1];
5044 window_to_block(region, but->block, &x, &y);
5045 if (!BLI_rctf_isect_pt(&but->rect, x, y)) {
5046 return nullptr;
5047 }
5048
5049 const float icon_size = 0.8f * BLI_rctf_size_y(&but->rect); /* ICON_SIZE_FROM_BUTRECT */
5050 float xmax = but->rect.xmax;
5051 /* Same as in 'widget_draw_extra_icons', icon padding from the right edge. */
5052 xmax -= 0.2 * icon_size;
5053
5054 /* Handle the padding space from the right edge as the last button. */
5055 if (x > xmax) {
5056 return but->extra_op_icons.last();
5057 }
5058
5059 /* Inverse order, from right to left. */
5060 for (ButtonExtraOpIcon &op_icon : but->extra_op_icons.items_reversed()) {
5061 if ((x > (xmax - icon_size)) && x <= xmax) {
5062 return &op_icon;
5063 }
5064 xmax -= icon_size;
5065 }
5066
5067 return nullptr;
5068}
5069
5071 Button *but,
5073 const wmEvent *event)
5074{
5075 ButtonExtraOpIcon *op_icon = but_extra_operator_icon_mouse_over_get(but, data->region, event);
5076
5077 if (!op_icon) {
5078 return false;
5079 }
5080
5081 /* Only act on release, avoids some glitches. */
5082 if (event->val != KM_RELEASE) {
5083 /* Still swallow events on the icon. */
5084 return true;
5085 }
5086 if (event->type == event->prev_press_type) {
5087 /* Release should be close to the press. #151371. */
5088 const float icon_size = 0.8f * BLI_rctf_size_y(&but->rect);
5089 if (abs(event->prev_press_xy[0] - event->xy[0]) > icon_size) {
5090 return true;
5091 }
5092 }
5093
5094 ED_region_tag_redraw(data->region);
5096
5097 but_extra_operator_icon_apply(C, but, op_icon);
5098 /* NOTE: 'but', 'data' may now be freed, don't access. */
5099
5100 return true;
5101}
5102
5105 const wmEvent *event)
5106{
5107 ButtonExtraOpIcon *old_highlighted = nullptr;
5108
5109 /* Unset highlighting of all first. */
5110 for (ButtonExtraOpIcon &op_icon : but->extra_op_icons) {
5111 if (op_icon.highlighted) {
5112 old_highlighted = &op_icon;
5113 }
5114 op_icon.highlighted = false;
5115 }
5116
5117 ButtonExtraOpIcon *hovered = but_extra_operator_icon_mouse_over_get(but, data->region, event);
5118
5119 if (hovered) {
5120 hovered->highlighted = true;
5121 }
5122
5123 if (old_highlighted != hovered) {
5125 }
5126}
5127
5128#ifdef USE_DRAG_TOGGLE
5129/* Shared by any button that supports drag-toggle. */
5131 bContext *C, Button *but, HandleButtonData *data, const wmEvent *event, int *r_retval)
5132{
5133 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5134 if (event->type == LEFTMOUSE && event->val == KM_PRESS && but_is_drag_toggle(but)) {
5135 apply_but(C, but->block, but, data, true);
5137 data->dragstartx = event->xy[0];
5138 data->dragstarty = event->xy[1];
5139 *r_retval = WM_UI_HANDLER_BREAK;
5140 return true;
5141 }
5142 }
5143 else if (data->state == BUTTON_STATE_WAIT_DRAG) {
5144 /* NOTE: the 'BUTTON_STATE_WAIT_DRAG' part of 'do_but_EXIT' could be refactored into
5145 * its own function */
5146 data->applied = false;
5147 *r_retval = do_but_EXIT(C, but, data, event);
5148 return true;
5149 }
5150 return false;
5151}
5152#endif /* USE_DRAG_TOGGLE */
5153
5154static int do_but_BUT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
5155{
5156#ifdef USE_DRAG_TOGGLE
5157 {
5158 int retval;
5159 if (do_but_ANY_drag_toggle(C, but, data, event, &retval)) {
5160 return retval;
5161 }
5162 }
5163#endif
5164 if (button_draw_as_link(but)) {
5165 if (data->window->cursor != WM_CURSOR_HAND_POINT) {
5167 }
5168 data->changed_cursor = true;
5169 }
5170 if (button_opens_link(but) && !data->changed_wokspace_status) {
5171 WorkspaceStatus status(C);
5172 status.item(button_get_link(but, C), ICON_NONE);
5173 data->changed_wokspace_status = true;
5174 }
5175 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5176 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
5178 return WM_UI_HANDLER_BREAK;
5179 }
5180 if (event->type == LEFTMOUSE && event->val == KM_RELEASE && but->block->handle) {
5181 /* regular buttons will be 'UI_SELECT', menu items 'UI_HOVER' */
5182 if (!(but->flag & (UI_SELECT | UI_HOVER))) {
5183 data->cancel = true;
5184 }
5186 return WM_UI_HANDLER_BREAK;
5187 }
5188 if (ELEM(event->type, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
5190 return WM_UI_HANDLER_BREAK;
5191 }
5192 }
5193 else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
5194 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
5195 if (!(but->flag & UI_SELECT)) {
5196 data->cancel = true;
5197 }
5199 return WM_UI_HANDLER_BREAK;
5200 }
5201 }
5202
5204}
5205
5207{
5208 ButtonHotkeyEvent *hotkey_but = static_cast<ButtonHotkeyEvent *>(but);
5210
5211 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5213 (event->val == KM_PRESS))
5214 {
5215 but->drawstr.clear();
5216 hotkey_but->modifier_key = wmEventModifierFlag(0);
5218 return WM_UI_HANDLER_BREAK;
5219 }
5220 }
5221 else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
5222 if (ISMOUSE_MOTION(event->type)) {
5224 }
5225 if (event->type == EVT_UNKNOWNKEY) {
5226 WM_global_report(RPT_WARNING, "Unsupported key: Unknown");
5228 }
5229 if (event->type == EVT_CAPSLOCKKEY) {
5230 WM_global_report(RPT_WARNING, "Unsupported key: CapsLock");
5232 }
5233
5234 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
5235 /* only cancel if click outside the button */
5236 if (button_contains_point_px(but, but->active->region, event->xy) == false) {
5237 data->cancel = true;
5238 /* Close the containing popup (if any). */
5239 data->escapecancel = true;
5241 return WM_UI_HANDLER_BREAK;
5242 }
5243 }
5244
5245 /* always set */
5246 hotkey_but->modifier_key = event->modifier;
5247
5248 button_update(but);
5249 ED_region_tag_redraw(data->region);
5250
5251 if (event->val == KM_PRESS) {
5252 if (ISHOTKEY(event->type) && (event->type != EVT_ESCKEY)) {
5253 if (WM_key_event_string(event->type, false)[0]) {
5254 button_value_set(but, event->type);
5255 }
5256 else {
5257 data->cancel = true;
5258 }
5259
5261 return WM_UI_HANDLER_BREAK;
5262 }
5263 if (event->type == EVT_ESCKEY) {
5264 if (event->val == KM_PRESS) {
5265 data->cancel = true;
5266 data->escapecancel = true;
5268 }
5269 }
5270 }
5271 }
5272
5274}
5275
5276static int do_but_KEYEVT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
5277{
5278 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5279 if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
5281 return WM_UI_HANDLER_BREAK;
5282 }
5283 }
5284 else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
5285 if (ISMOUSE_MOTION(event->type)) {
5287 }
5288
5289 if (event->val == KM_PRESS) {
5290 if (WM_key_event_string(event->type, false)[0]) {
5291 button_value_set(but, event->type);
5292 }
5293 else {
5294 data->cancel = true;
5295 }
5296
5298 }
5299 }
5300
5302}
5303
5304static int do_but_TAB(
5305 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
5306{
5307 const bool is_property = (but->rnaprop != nullptr);
5308
5309#ifdef USE_DRAG_TOGGLE
5310 if (is_property) {
5311 int retval;
5312 if (do_but_ANY_drag_toggle(C, but, data, event, &retval)) {
5313 return retval;
5314 }
5315 }
5316#endif
5317
5318 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5319 const int rna_type = but->rnaprop ? RNA_property_type(but->rnaprop) : 0;
5320
5321 if (is_property && ELEM(rna_type, PROP_POINTER, PROP_STRING) &&
5322 (but->custom_data != nullptr) && (event->type == LEFTMOUSE) &&
5323 ((event->val == KM_DBL_CLICK) || (event->modifier & KM_CTRL)))
5324 {
5326 return WM_UI_HANDLER_BREAK;
5327 }
5328 if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY)) {
5329 const int event_val = (is_property) ? KM_PRESS : KM_CLICK;
5330 if (event->val == event_val) {
5332 return WM_UI_HANDLER_BREAK;
5333 }
5334 }
5335 }
5336 else if (data->state == BUTTON_STATE_TEXT_EDITING) {
5337 do_but_textedit(C, block, but, data, event);
5338 return WM_UI_HANDLER_BREAK;
5339 }
5340 else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
5341 do_but_textedit_select(C, block, but, data, event);
5342 return WM_UI_HANDLER_BREAK;
5343 }
5344
5346}
5347
5353 Button *but,
5355 const int inc_value)
5356{
5357 /* The allocated string only increases in length by 1,
5358 * only support incrementing by one. */
5359 BLI_assert(ELEM(inc_value, -1, 1));
5360
5361 if (data->state != BUTTON_STATE_HIGHLIGHT) {
5362 /* This function assumes the mouse is only hovering over the input. */
5364 }
5365
5366 /* Retrieve the string. */
5367 char *but_string;
5368 int str_maxncpy = button_string_get_maxncpy(but);
5369 bool no_zero_strip = false;
5370 if (str_maxncpy != 0) {
5371 but_string = MEM_new_array_zeroed<char>(str_maxncpy, __func__);
5373 but, but_string, str_maxncpy, UI_PRECISION_FLOAT_MAX, true, &no_zero_strip);
5374 }
5375 else {
5376 but_string = button_string_get_dynamic(but, &str_maxncpy);
5377 }
5378
5379 if (but_string[0] == '\0') {
5380 /* Don't append a number to an empty string. */
5381 MEM_delete(but_string);
5383 }
5384
5385 /* More space needed for an added digit. */
5386 str_maxncpy += 1;
5387 char *head = MEM_new_array_zeroed<char>(str_maxncpy, __func__);
5388 char *tail = MEM_new_array_zeroed<char>(str_maxncpy, __func__);
5389 ushort digits;
5390
5391 /* Decode the string, parsing head, digits, tail. */
5392 int num = BLI_path_sequence_decode(but_string, head, str_maxncpy, tail, str_maxncpy, &digits);
5393 MEM_delete(but_string);
5394 if (num == 0 && digits == 0) {
5396 }
5397
5398 /* Increase or decrease the value. */
5399 num += inc_value;
5400
5401 /* Encode the new string with the changed value. */
5402 char *string = MEM_new_array_zeroed<char>(str_maxncpy, __func__);
5403 BLI_path_sequence_encode(string, str_maxncpy, head, tail, digits, num);
5404
5405 /* Save this new string to the button. */
5406 button_set_string_interactive(C, but, string);
5407
5408 /* Free the strings. */
5409 MEM_delete(string);
5410 MEM_delete(head);
5411 MEM_delete(tail);
5412
5413 return WM_UI_HANDLER_BREAK;
5414}
5415
5416static int do_but_TEX(
5417 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
5418{
5419 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5421 event->val == KM_PRESS)
5422 {
5423 if (ELEM(event->type, EVT_PADENTER, EVT_RETKEY) && !but_is_utf8(but)) {
5424 /* Pass, allow file-selector, enter to execute. */
5425 }
5427 (event->modifier != KM_CTRL))
5428 {
5429 /* Pass. */
5430 }
5431 else {
5432 if (!but_extra_operator_icon_mouse_over_get(but, data->region, event)) {
5435 if (event->type == LEFTMOUSE && but->type == ButtonType::TextBox) {
5436 /* Text-box buttons allows to scroll its content even when they are not in text-edit
5437 * state, let the user to place the text cursor under the mouse and to immediately
5438 * start selecting text without requiring to activate the text-box with an extra click.
5439 */
5440 textedit_set_cursor_pos(but, data->region, float2(event->xy));
5441 but->selsta = but->selend = data->text_edit.sel_pos_init = but->pos;
5443 }
5444 }
5445 return WM_UI_HANDLER_BREAK;
5446 }
5447 }
5448 else if (event->type == LEFTMOUSE && event->val == KM_DBL_CLICK &&
5449 but->type == ButtonType::Text && static_cast<ButtonText *>(but)->use_label_style)
5450 {
5452 return WM_UI_HANDLER_BREAK;
5453 }
5454 else if (ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE) && (event->modifier & KM_CTRL)) {
5455 const int inc_value = (event->type == WHEELUPMOUSE) ? 1 : -1;
5456 return do_but_text_value_cycle(C, but, data, inc_value);
5457 }
5458 }
5459 else if (data->state == BUTTON_STATE_TEXT_EDITING) {
5460 return do_but_textedit(C, block, but, data, event);
5461 }
5462 else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
5463 return do_but_textedit_select(C, block, but, data, event);
5464 }
5465
5467}
5468
5470 Block *block,
5471 ButtonTextBox *textbox,
5473 const wmEvent *event)
5474{
5475 wmWindow *win = CTX_wm_window(C);
5476 const bool is_disabled = textbox->flag & BUT_DISABLED || data->disable_force;
5477
5478 switch (data->state) {
5481 if (event->type == MOUSEPAN && textbox->last_total_lines > textbox->visible_lines()) {
5482 int type = event->type;
5483 int value = event->val;
5484
5485 pan_to_scroll(event, &type, &value);
5486 int scroll_dir = 1;
5487 if (event->flag & WM_EVENT_SCROLL_INVERT) {
5488 scroll_dir = -1;
5489 }
5490 if (type != MOUSEPAN) {
5491 textbox_add_scroll(textbox, (type == WHEELUPMOUSE ? -1 : 1) * scroll_dir);
5492 ED_region_tag_redraw(data->region);
5493 }
5494 return WM_UI_HANDLER_BREAK;
5495 }
5496 if (ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
5497 if (textbox->last_total_lines > textbox->visible_lines()) {
5498 textbox_add_scroll(textbox, (event->type == WHEELUPMOUSE ? -1 : 1));
5499 ED_region_tag_redraw(data->region);
5500 return WM_UI_HANDLER_BREAK;
5501 }
5504 }
5505 rctf rect;
5506 block_to_window_rctf(data->region, block, &rect, &textbox->rect);
5507
5508 rctf scroll_rect = rect;
5509 scroll_rect.xmin = rect.xmax - button_text_padding(textbox);
5510 scroll_rect.ymin += textbox_grip_height() / block->aspect;
5511
5512 rctf grip_rect = rect;
5513 grip_rect.xmin = grip_rect.xmax - button_text_padding(textbox) - (2.0f / block->aspect);
5514 grip_rect.ymax = grip_rect.ymin + textbox_grip_height() / block->aspect;
5515
5516 /* Update mouse cursor on mouse move. */
5517 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
5518 if (BLI_rctf_isect_pt(&scroll_rect, UNPACK2(event->xy))) {
5519 if (textbox->last_total_lines > textbox->visible_lines()) {
5522 data->changed_cursor = false;
5523 break;
5524 }
5525 }
5526 if (BLI_rctf_isect_pt(&grip_rect, UNPACK2(event->xy))) {
5527 if (win->cursor != WM_CURSOR_NS_SCROLL) {
5529 data->changed_cursor = true;
5530 }
5531 break;
5532 }
5533 /* Set text-edit cursor only if the button is not disabled, otherwise reset the mouse
5534 * cursor. */
5535 if (is_disabled) {
5536 if (win->cursor != WM_CURSOR_DEFAULT) {
5539 data->changed_cursor = false;
5540 }
5541 }
5542 else if (win->cursor != WM_CURSOR_TEXT_EDIT) {
5544 data->changed_cursor = true;
5545 }
5546 break;
5547 }
5548
5549 if (!(event->val == KM_PRESS && event->type == LEFTMOUSE)) {
5550 break;
5551 }
5552 /* Try activate the text-box scroll-bar. */
5553 if (textbox->last_total_lines > textbox->visible_lines() &&
5554 BLI_rctf_isect_pt(&scroll_rect, UNPACK2(event->xy)))
5555 {
5558 data->changed_cursor = false;
5560 /* Scroll to mouse cursor with mouse move. */
5562 return WM_UI_HANDLER_BREAK;
5563 }
5564 /* Try activate the text-box grip button. */
5565 if (BLI_rctf_isect_pt(&grip_rect, UNPACK2(event->xy))) {
5568 data->changed_cursor = true;
5569 data->dragstarty = event->xy[1];
5570 data->origvalue = textbox->visible_lines();
5571 return WM_UI_HANDLER_BREAK;
5572 }
5573 break;
5574 }
5576 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
5579 return WM_UI_HANDLER_BREAK;
5580 }
5581 if (event->type == MOUSEMOVE) {
5582 int mx = event->xy[0];
5583 int my = event->xy[1];
5584 window_to_block(data->region, block, &mx, &my);
5585 const float ymin = textbox->rect.ymin + textbox_grip_height();
5586 const float range = textbox->rect.ymax - ymin;
5587 const int scroll = round_fl_to_int(
5588 (range - (my - ymin)) / range *
5589 (textbox->last_total_lines - textbox->state->visible_lines));
5590
5591 if (textbox->line_scroll() != scroll) {
5592 ED_region_tag_redraw(data->region);
5593 }
5594 textbox->line_scroll_set(scroll);
5595 return WM_UI_HANDLER_BREAK;
5596 }
5597 break;
5598 }
5600 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
5603 return WM_UI_HANDLER_BREAK;
5604 }
5605 if (event->type == MOUSEMOVE) {
5606 int visible_lines = data->origvalue +
5607 ((data->dragstarty - event->xy[1]) /
5609 visible_lines = std::max(textbox_minimum_visible_lines, visible_lines);
5610
5611 if (textbox->state->visible_lines != visible_lines) {
5612 ED_region_tag_redraw(data->region);
5613 if (block_is_popup_any(block)) {
5615 }
5616 }
5617 textbox->state->visible_lines = visible_lines;
5618 return WM_UI_HANDLER_BREAK;
5619 }
5620 break;
5621 }
5622 default:
5623 break;
5624 }
5625 /* When disabled text-box buttons only can be scrolled/resized. */
5626 if (is_disabled) {
5628 }
5629 /* Handle regular text buttons events. */
5630 return do_but_TEX(C, block, textbox, data, event);
5631}
5632
5634 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
5635{
5636 /* unlink icon is on right */
5638 /* doing this on KM_PRESS calls eyedropper after clicking unlink icon */
5639 if ((event->val == KM_RELEASE) && do_but_extra_operator_icon(C, but, data, event)) {
5640 return WM_UI_HANDLER_BREAK;
5641 }
5642 }
5643 return do_but_TEX(C, block, but, data, event);
5644}
5645
5646static int do_but_TOG(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
5647{
5648#ifdef USE_DRAG_TOGGLE
5649 {
5650 int retval;
5651 if (do_but_ANY_drag_toggle(C, but, data, event, &retval)) {
5652 return retval;
5653 }
5654 }
5655#endif
5656
5657 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5658 bool do_activate = false;
5659 if (ELEM(event->type, EVT_PADENTER, EVT_RETKEY)) {
5660 if (event->val == KM_PRESS) {
5661 do_activate = true;
5662 }
5663 }
5664 else if (event->type == LEFTMOUSE) {
5665 if (block_is_menu(but->block)) {
5666 /* Behave like other menu items. */
5667 do_activate = (event->val == KM_RELEASE);
5668 }
5669 else if (!do_but_extra_operator_icon(C, but, data, event)) {
5670 /* Also use double-clicks to prevent fast clicks to leak to other handlers (#76481). */
5671 do_activate = ELEM(event->val, KM_PRESS, KM_DBL_CLICK);
5672 }
5673 }
5674
5675 if (do_activate) {
5677 return WM_UI_HANDLER_BREAK;
5678 }
5679 if (ELEM(event->type, MOUSEPAN, WHEELDOWNMOUSE, WHEELUPMOUSE) && (event->modifier & KM_CTRL)) {
5680 if (ELEM(but->type,
5688 {
5689 /* Support Ctrl-Wheel to cycle toggles and check-boxes. */
5691 return WM_UI_HANDLER_BREAK;
5692 }
5693 if (but->type == ButtonType::Row) {
5694 /* Support Ctrl-Wheel to cycle values on expanded enum rows. */
5695 int type = event->type;
5696 int val = event->val;
5697
5698 /* Convert pan to scroll-wheel. */
5699 if (type == MOUSEPAN) {
5700 pan_to_scroll(event, &type, &val);
5701
5702 if (type == MOUSEPAN) {
5703 return WM_UI_HANDLER_BREAK;
5704 }
5705 }
5706
5707 const int direction = (type == WHEELDOWNMOUSE) ? -1 : 1;
5708 Button *but_select = button_find_select_in_enum(but, direction);
5709 if (but_select) {
5710 Button *but_other = (direction == -1) ? but_select->block->next_but(but_select) :
5711 but_select->block->prev_but(but_select);
5712 if (but_other && but_find_select_in_enum__cmp(but, but_other)) {
5713 ARegion *region = data->region;
5714
5715 data->cancel = true;
5716 button_activate_exit(C, but, data, false, false);
5717
5718 /* Activate the text button. */
5719 button_activate_init(C, region, but_other, BUTTON_ACTIVATE_OVER);
5720 data = but_other->active;
5721 if (data) {
5722 apply_but(C, but->block, but_other, but_other->active, true);
5723 button_activate_exit(C, but_other, data, false, false);
5724
5725 /* restore active button */
5727 }
5728 else {
5729 /* shouldn't happen */
5730 BLI_assert(0);
5731 }
5732 }
5733 }
5734 return WM_UI_HANDLER_BREAK;
5735 }
5736 }
5737 }
5739}
5740
5749 ARegion *region,
5750 ButtonViewItem *but,
5751 const bool close_popup = true)
5752{
5753
5754 /* For popups. Other abstract view instances correctly calls the select operator, see:
5755 * #141235. */
5756 if (but->context) {
5757 CTX_store_set(C, but->context);
5758 }
5759 but->view_item->activate(*C);
5760 CTX_store_set(C, nullptr);
5761
5764
5765 if (close_popup && !view_item_popup_keep_open(*but->view_item)) {
5767 }
5768}
5769
5771{
5772 ButtonViewItem *view_item_but = static_cast<ButtonViewItem *>(but);
5773 BLI_assert(view_item_but->type == ButtonType::ViewItem);
5774
5775 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5776 if (event->type == LEFTMOUSE) {
5777 switch (event->val) {
5778 case KM_PRESS:
5779 /* Extra icons have priority, don't mess with them. */
5780 if (but_extra_operator_icon_mouse_over_get(but, data->region, event)) {
5781 return WM_UI_HANDLER_BREAK;
5782 }
5783
5784 if (block_is_popup_any(but->block)) {
5785 /* TODO(!147047): This should be handled in selection operator. */
5786 force_activate_view_item_but(C, data->region, view_item_but, false);
5787 return WM_UI_HANDLER_BREAK;
5788 }
5789
5790 if (view_item_supports_drag(*view_item_but->view_item)) {
5792 data->dragstartx = event->xy[0];
5793 data->dragstarty = event->xy[1];
5794 }
5795
5796 /* Always continue for drag and drop handling. Also for cases where keymap items are
5797 * registered to add custom activate or drag operators (the pose library does this for
5798 * example). */
5800 case KM_DBL_CLICK:
5801 if (view_item_can_rename(*view_item_but->view_item)) {
5802 data->cancel = true;
5803 view_item_begin_rename(*view_item_but->view_item);
5804 ED_region_tag_redraw(data->region);
5806 return WM_UI_HANDLER_BREAK;
5807 }
5809 }
5810 }
5811 }
5812 else if (data->state == BUTTON_STATE_WAIT_DRAG) {
5813 /* Let "default" button handling take care of the drag logic. */
5814 return do_but_EXIT(C, but, data, event);
5815 }
5816
5818}
5819
5820static int do_but_EXIT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
5821{
5822 if (data->state == BUTTON_STATE_HIGHLIGHT) {
5823
5824 /* First handle click on icon-drag type button. */
5825 if ((event->type == LEFTMOUSE) && (event->val == KM_PRESS) && button_drag_is_draggable(but)) {
5826 if (but_contains_point_px_icon(but, data->region, event)) {
5827
5828 /* tell the button to wait and keep checking further events to
5829 * see if it should start dragging */
5831 data->dragstartx = event->xy[0];
5832 data->dragstarty = event->xy[1];
5834 }
5835 }
5836#ifdef USE_DRAG_TOGGLE
5837 if ((event->type == LEFTMOUSE) && (event->val == KM_PRESS) && but_is_drag_toggle(but)) {
5839 data->dragstartx = event->xy[0];
5840 data->dragstarty = event->xy[1];
5842 }
5843#endif
5844
5845 if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
5847 /* XXX: (a bit ugly) Special case handling for file-browser drag buttons (icon and filename
5848 * label). */
5849 if (button_drag_is_draggable(but) && but_contains_point_px_icon(but, data->region, event)) {
5851 }
5852 const Button *view_but = view_item_find_mouse_over(data->region, event->xy);
5853 if (view_but) {
5855 }
5857 return ret;
5858 }
5859 }
5860 else if (data->state == BUTTON_STATE_WAIT_DRAG) {
5861
5862 /* this function also ends state */
5863 if (but_drag_init(C, but, data, event)) {
5864 return WM_UI_HANDLER_BREAK;
5865 }
5866
5867 /* If the mouse has been pressed and released, getting to
5868 * this point without triggering a drag, then clear the
5869 * drag state for this button and continue to pass on the event */
5870 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
5873 }
5874
5875 /* while waiting for a drag to be triggered, always block
5876 * other events from getting handled */
5877 return WM_UI_HANDLER_BREAK;
5878 }
5879
5881}
5882
5883/* var names match numedit_but_NUM */
5885 Button *but, float tempf, float softmin, float softmax, const eSnapType snap)
5886{
5887 if (tempf == softmin || tempf == softmax || snap == SNAP_OFF) {
5888 /* pass */
5889 }
5890 else {
5891 const PropertyScaleType scale_type = button_scale_type(but);
5892 float softrange = softmax - softmin;
5893 float fac = 1.0f;
5894
5895 if (button_is_unit(but)) {
5896 const UnitSettings *unit = but->block->unit;
5897 const int unit_type = RNA_SUBTYPE_UNIT_VALUE(button_unit_type_get(but));
5898
5899 if (BKE_unit_is_valid(unit->system, unit_type)) {
5900 fac = float(BKE_unit_base_scalar(unit->system, unit_type));
5901 if (ELEM(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
5902 fac /= unit->scale_length;
5903 }
5904 }
5905 }
5906
5907 if (fac != 1.0f) {
5908 /* snap in unit-space */
5909 tempf /= fac;
5910 // softmin /= fac; /* UNUSED */
5911 // softmax /= fac; /* UNUSED */
5912 softrange /= fac;
5913 }
5914
5915 /* workaround, too high snapping values */
5916 /* snapping by 10's for float buttons is quite annoying (location, scale...),
5917 * but allow for rotations */
5918 if (softrange >= 21.0f) {
5919 const UnitSettings *unit = but->block->unit;
5920 const int unit_type = button_unit_type_get(but);
5921 if ((unit_type == PROP_UNIT_ROTATION) && (unit->system_rotation != USER_UNIT_ROT_RADIANS)) {
5922 /* Pass (degrees). */
5923 }
5924 else {
5925 softrange = 20.0f;
5926 }
5927 }
5928
5930 switch (scale_type) {
5931 case PROP_SCALE_LINEAR:
5932 case PROP_SCALE_CUBIC: {
5933 const float snap_fac = (snap == SNAP_ON_SMALL ? 0.1f : 1.0f);
5934 if (softrange < 2.10f) {
5935 tempf = roundf(tempf * 10.0f / snap_fac) * 0.1f * snap_fac;
5936 }
5937 else if (softrange < 21.0f) {
5938 tempf = roundf(tempf / snap_fac) * snap_fac;
5939 }
5940 else {
5941 tempf = roundf(tempf * 0.1f / snap_fac) * 10.0f * snap_fac;
5942 }
5943 break;
5944 }
5945 case PROP_SCALE_LOG: {
5946 const float snap_fac = powf(10.0f,
5947 roundf(log10f(tempf) + UI_PROP_SCALE_LOG_SNAP_OFFSET) -
5948 (snap == SNAP_ON_SMALL ? 2.0f : 1.0f));
5949 tempf = roundf(tempf / snap_fac) * snap_fac;
5950 break;
5951 }
5952 }
5953
5954 if (fac != 1.0f) {
5955 tempf *= fac;
5956 }
5957 }
5958
5959 return tempf;
5960}
5961
5962static float numedit_apply_snap(int temp, float softmin, float softmax, const eSnapType snap)
5963{
5964 if (ELEM(temp, softmin, softmax)) {
5965 return temp;
5966 }
5967
5968 switch (snap) {
5969 case SNAP_OFF:
5970 break;
5971 case SNAP_ON:
5972 temp = 10 * (temp / 10);
5973 break;
5974 case SNAP_ON_SMALL:
5975 temp = 100 * (temp / 100);
5976 break;
5977 }
5978
5979 return temp;
5980}
5981
5984 int mx,
5985 FunctionRef<int()> drag_threshold_fn,
5986 const bool is_motion,
5987 const eSnapType snap,
5988 float fac)
5989{
5990 float deler, tempf;
5991 int lvalue, temp;
5992 bool changed = false;
5993 const bool is_float = button_is_float(but);
5994 const PropertyScaleType scale_type = button_scale_type(but);
5995
5996 /* prevent unwanted drag adjustments, test motion so modifier keys refresh. */
5997 if ((is_motion || data->draglock) &&
5998 (but_dragedit_update_mval(data, mx, drag_threshold_fn) == false))
5999 {
6000 return changed;
6001 }
6002
6004 static_cast<bContext *>(but->block->evil_C), but->block, data, false);
6005
6006 if (button_is_cursor_warp(but)) {
6007 const float softmin = but->softmin;
6008 const float softmax = but->softmax;
6009 const float softrange = softmax - softmin;
6010
6011 const float log_min = (scale_type == PROP_SCALE_LOG) ?
6013 powf(10, -but->precision) * 0.5f) :
6014 0;
6015
6016 /* Mouse location isn't screen clamped to the screen so use a linear mapping
6017 * 2px == 1-int, or 1px == 1-ClickStep */
6018 if (is_float) {
6019 fac *= 0.01f * but->step_size;
6020 switch (scale_type) {
6021 case PROP_SCALE_LINEAR: {
6022 tempf = float(data->startvalue) + float(mx - data->dragstartx) * fac;
6023 break;
6024 }
6025 case PROP_SCALE_LOG: {
6026 const float startvalue = max_ff(float(data->startvalue), log_min);
6027 tempf = expf(float(mx - data->dragstartx) * fac) * startvalue;
6028 if (tempf <= log_min) {
6029 tempf = 0.0f;
6030 }
6031 break;
6032 }
6033 case PROP_SCALE_CUBIC: {
6034 tempf = cbrtf(float(data->startvalue)) + float(mx - data->dragstartx) * fac;
6035 tempf *= tempf * tempf;
6036 break;
6037 }
6038 }
6039
6040 tempf = numedit_apply_snapf(but, tempf, softmin, softmax, snap);
6041
6042#if 1 /* fake moving the click start, nicer for dragging back after passing the limit */
6043 switch (scale_type) {
6044 case PROP_SCALE_LINEAR: {
6045 if (tempf < softmin) {
6046 data->dragstartx -= (softmin - tempf) / fac;
6047 tempf = softmin;
6048 }
6049 else if (tempf > softmax) {
6050 data->dragstartx -= (softmax - tempf) / fac;
6051 tempf = softmax;
6052 }
6053 break;
6054 }
6055 case PROP_SCALE_LOG: {
6056 const float startvalue = max_ff(float(data->startvalue), log_min);
6057 if (tempf < log_min) {
6058 data->dragstartx -= logf(log_min / startvalue) / fac - float(mx - data->dragstartx);
6059 tempf = softmin;
6060 }
6061 else if (tempf > softmax) {
6062 data->dragstartx -= logf(softmax / startvalue) / fac - float(mx - data->dragstartx);
6063 tempf = softmax;
6064 }
6065 break;
6066 }
6067 case PROP_SCALE_CUBIC: {
6068 if (tempf < softmin) {
6069 data->dragstartx = mx - int((cbrtf(softmin) - cbrtf(float(data->startvalue))) / fac);
6070 tempf = softmin;
6071 }
6072 else if (tempf > softmax) {
6073 data->dragstartx = mx - int((cbrtf(softmax) - cbrtf(float(data->startvalue))) / fac);
6074 tempf = softmax;
6075 }
6076 break;
6077 }
6078 }
6079#else
6080 CLAMP(tempf, softmin, softmax);
6081#endif
6082
6083 if (tempf != float(data->value)) {
6084 data->dragchange = true;
6085 data->value = tempf;
6086 changed = true;
6087 }
6088 }
6089 else {
6090 if (softrange > 256) {
6091 fac = 1.0;
6092 } /* 1px == 1 */
6093 else if (softrange > 32) {
6094 fac = 1.0 / 2.0;
6095 } /* 2px == 1 */
6096 else {
6097 fac = 1.0 / 16.0;
6098 } /* 16px == 1? */
6099
6100 temp = data->startvalue + ((double(mx) - data->dragstartx) * double(fac));
6101 temp = numedit_apply_snap(temp, softmin, softmax, snap);
6102
6103#if 1 /* fake moving the click start, nicer for dragging back after passing the limit */
6104 if (temp < softmin) {
6105 data->dragstartx -= (softmin - temp) / fac;
6106 temp = softmin;
6107 }
6108 else if (temp > softmax) {
6109 data->dragstartx += (temp - softmax) / fac;
6110 temp = softmax;
6111 }
6112#else
6113 CLAMP(temp, softmin, softmax);
6114#endif
6115
6116 if (temp != data->value) {
6117 data->dragchange = true;
6118 data->value = temp;
6119 changed = true;
6120 }
6121 }
6122
6123 data->draglastx = mx;
6124 }
6125 else {
6126 /* Use 'but->softmin', 'but->softmax' when clamping values. */
6127 const float softmin = data->drag_map_soft_min;
6128 const float softmax = data->drag_map_soft_max;
6129 const float softrange = softmax - softmin;
6130
6131 float non_linear_range_limit;
6132 float non_linear_pixel_map;
6133 float non_linear_scale;
6134
6135 /* Use a non-linear mapping of the mouse drag especially for large floats
6136 * (normal behavior) */
6137 deler = 500;
6138 if (is_float) {
6139 /* not needed for smaller float buttons */
6140 non_linear_range_limit = 11.0f;
6141 non_linear_pixel_map = 500.0f;
6142 }
6143 else {
6144 /* only scale large int buttons */
6145 non_linear_range_limit = 129.0f;
6146 /* Larger for ints, we don't need to fine tune them. */
6147 non_linear_pixel_map = 250.0f;
6148
6149 /* prevent large ranges from getting too out of control */
6150 if (softrange > 600) {
6151 deler = powf(softrange, 0.75f);
6152 }
6153 else if (softrange < 25) {
6154 deler = 50.0;
6155 }
6156 else if (softrange < 100) {
6157 deler = 100.0;
6158 }
6159 }
6160 deler /= fac;
6161
6162 if (softrange > non_linear_range_limit) {
6163 non_linear_scale = float(abs(mx - data->dragstartx)) / non_linear_pixel_map;
6164 }
6165 else {
6166 non_linear_scale = 1.0f;
6167 }
6168
6169 if (is_float == false) {
6170 /* at minimum, moving cursor 2 pixels should change an int button. */
6171 CLAMP_MIN(non_linear_scale, 0.5f * UI_SCALE_FAC);
6172 }
6173
6174 data->dragf += (float(mx - data->draglastx) / deler) * non_linear_scale;
6175
6176 if (but->softmin == softmin) {
6177 CLAMP_MIN(data->dragf, 0.0f);
6178 }
6179 if (but->softmax == softmax) {
6180 CLAMP_MAX(data->dragf, 1.0f);
6181 }
6182
6183 data->draglastx = mx;
6184
6185 switch (scale_type) {
6186 case PROP_SCALE_LINEAR: {
6187 tempf = (softmin + data->dragf * softrange);
6188 break;
6189 }
6190 case PROP_SCALE_LOG: {
6191 const float log_min = max_ff(max_ff(softmin, UI_PROP_SCALE_LOG_MIN),
6192 powf(10.0f, -but->precision) * 0.5f);
6193 const float base = softmax / log_min;
6194 tempf = powf(base, data->dragf) * log_min;
6195 if (tempf <= log_min) {
6196 tempf = 0.0f;
6197 }
6198 break;
6199 }
6200 case PROP_SCALE_CUBIC: {
6201 tempf = (softmin + data->dragf * softrange);
6202 tempf *= tempf * tempf;
6203 float cubic_min = softmin * softmin * softmin;
6204 float cubic_max = softmax * softmax * softmax;
6205 tempf = (tempf - cubic_min) / (cubic_max - cubic_min) * softrange + softmin;
6206 break;
6207 }
6208 }
6209
6210 if (!is_float) {
6211 temp = round_fl_to_int(tempf);
6212
6213 temp = numedit_apply_snap(temp, but->softmin, but->softmax, snap);
6214
6215 CLAMP(temp, but->softmin, but->softmax);
6216 lvalue = int(data->value);
6217
6218 if (temp != lvalue) {
6219 data->dragchange = true;
6220 data->value = double(temp);
6221 changed = true;
6222 }
6223 }
6224 else {
6225 temp = 0;
6226 tempf = numedit_apply_snapf(but, tempf, but->softmin, but->softmax, snap);
6227
6228 CLAMP(tempf, but->softmin, but->softmax);
6229
6230 if (tempf != float(data->value)) {
6231 data->dragchange = true;
6232 data->value = tempf;
6233 changed = true;
6234 }
6235 }
6236 }
6237
6238 return changed;
6239}
6240
6242{
6243 const int oldflag = but->drawflag;
6245
6247 if (!data) {
6248 return;
6249 }
6250
6251 /* Ignore once we start dragging. */
6252 if (data->dragchange == false) {
6253 const float handle_width = min_ff(BLI_rctf_size_x(&but->rect) / 3,
6254 BLI_rctf_size_y(&but->rect) * 0.7f);
6255 /* we can click on the side arrows to increment/decrement,
6256 * or click inside to edit the value directly */
6257 int mx = data->window->runtime->eventstate->xy[0];
6258 int my = data->window->runtime->eventstate->xy[1];
6259 window_to_block(data->region, but->block, &mx, &my);
6260
6261 if (mx < (but->rect.xmin + handle_width)) {
6262 but->drawflag |= BUT_HOVER_LEFT;
6263 }
6264 else if (mx > (but->rect.xmax - handle_width)) {
6265 but->drawflag |= BUT_HOVER_RIGHT;
6266 }
6267 }
6268
6269 /* Don't change the cursor once pressed or if a modal operator is running.
6270 * If a modal operator is running, the number edit input will be ignored,
6271 * so do not try to change the cursor if this is the case.
6272 */
6273 if ((but->flag & UI_SELECT) == 0 && WM_cursor_modal_is_set_ok(data->window)) {
6274 if ((but->drawflag & BUT_HOVER_LEFT) || (but->drawflag & BUT_HOVER_RIGHT)) {
6275 if (data->changed_cursor) {
6277 data->changed_cursor = false;
6278 }
6279 }
6280 else {
6281 if (data->changed_cursor == false) {
6283 data->changed_cursor = true;
6284 }
6285 }
6286 }
6287
6288 if (but->drawflag != oldflag) {
6289 ED_region_tag_redraw(data->region);
6290 }
6291}
6292
6293static int do_but_NUM(
6294 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
6295{
6296 ButtonNumber *number_but = static_cast<ButtonNumber *>(but);
6297 int click = 0;
6298 int retval = WM_UI_HANDLER_CONTINUE;
6299
6300 /* mouse location scaled to fit the UI */
6301 int mx = event->xy[0];
6302 int my = event->xy[1];
6303 /* mouse location kept at screen pixel coords */
6304 const int screen_mx = event->xy[0];
6305
6306 /* Defer evaluation as it's rarely needed. */
6307 auto drag_threshold_fn = [event]() -> int { return WM_event_drag_threshold(event); };
6308
6310
6311 window_to_block(data->region, block, &mx, &my);
6312 numedit_set_active(but);
6313
6314 if (data->state == BUTTON_STATE_HIGHLIGHT) {
6315 int type = event->type, val = event->val;
6316
6317 if (type == MOUSEPAN) {
6318 pan_to_scroll(event, &type, &val);
6319 }
6320
6321 /* XXX hardcoded keymap check.... */
6322 if (type == MOUSEPAN && (event->modifier & KM_CTRL)) {
6323 /* allow accumulating values, otherwise scrolling gets preference */
6324 retval = WM_UI_HANDLER_BREAK;
6325 }
6326 else if (type == WHEELDOWNMOUSE && (event->modifier & KM_CTRL)) {
6327 mx = but->rect.xmin;
6328 but->drawflag &= ~BUT_HOVER_RIGHT;
6329 but->drawflag |= BUT_HOVER_LEFT;
6330 click = 1;
6331 }
6332 else if ((type == WHEELUPMOUSE) && (event->modifier & KM_CTRL)) {
6333 mx = but->rect.xmax;
6334 but->drawflag &= ~BUT_HOVER_LEFT;
6335 but->drawflag |= BUT_HOVER_RIGHT;
6336 click = 1;
6337 }
6338 else if (event->val == KM_PRESS) {
6339 if (ELEM(event->type, EVT_PADENTER, EVT_RETKEY) && (event->modifier & KM_CTRL)) {
6341 retval = WM_UI_HANDLER_BREAK;
6342 }
6343 else if (event->type == LEFTMOUSE) {
6344 data->dragstartx = data->draglastx = button_is_cursor_warp(but) ? screen_mx : mx;
6346 retval = WM_UI_HANDLER_BREAK;
6347 }
6348 else if (ELEM(event->type, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
6349 click = 1;
6350 }
6351 else if (event->type == EVT_BUT_OPEN) {
6352 /* Handle button_focus_on_enter_event. */
6354 retval = WM_UI_HANDLER_BREAK;
6355 }
6356 else if (event->type == EVT_MINUSKEY && event->val == KM_PRESS) {
6358 data->value = -data->value;
6360 retval = WM_UI_HANDLER_BREAK;
6361 }
6362
6363#ifdef USE_DRAG_MULTINUM
6364 copy_v2_v2_int(data->multi_data.drag_start, event->xy);
6365 data->multi_data.drag_dir_prev[0] = mx;
6366 data->multi_data.drag_dir_prev[1] = my;
6367#endif
6368 }
6369 }
6370 else if (data->state == BUTTON_STATE_NUM_EDITING) {
6371 if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
6372 if (event->val == KM_PRESS) {
6373 data->cancel = true;
6374 data->escapecancel = true;
6376 }
6377 }
6378 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
6379 if (data->dragchange) {
6380#ifdef USE_DRAG_MULTINUM
6381 /* If we started multi-button but didn't drag, then edit. */
6382 if (data->multi_data.init == HandleButtonMulti::INIT_SETUP) {
6383 click = 1;
6384 }
6385 else
6386#endif
6387 {
6389 }
6390 }
6391 else {
6392 click = 1;
6393 }
6394 }
6395 else if ((event->type == MOUSEMOVE) || event_is_snap(event)) {
6396 const bool is_motion = (event->type == MOUSEMOVE);
6397 const eSnapType snap = event_to_snap(event);
6398 float fac;
6399
6400#ifdef USE_DRAG_MULTINUM
6401 data->multi_data.drag_dir[0] += abs(data->multi_data.drag_dir_prev[0] - mx);
6402 data->multi_data.drag_dir[1] += abs(data->multi_data.drag_dir_prev[1] - my);
6403 data->multi_data.drag_dir_prev[0] = mx;
6404 data->multi_data.drag_dir_prev[1] = my;
6405#endif
6406
6407 fac = 1.0f;
6408 if (event->modifier & KM_SHIFT) {
6409 fac /= 10.0f;
6410 }
6411
6412 if (numedit_but_NUM(number_but,
6413 data,
6414 (button_is_cursor_warp(but) ? screen_mx : mx),
6415 drag_threshold_fn,
6416 is_motion,
6417 snap,
6418 fac))
6419 {
6420 numedit_apply(C, block, but, data);
6421 }
6422#ifdef USE_DRAG_MULTINUM
6423 else if (data->multi_data.has_mbuts) {
6424 if (data->multi_data.init == HandleButtonMulti::INIT_ENABLE) {
6425 multibut_states_apply(C, data, block);
6426 }
6427 }
6428#endif
6429 }
6430 retval = WM_UI_HANDLER_BREAK;
6431 }
6432 else if (data->state == BUTTON_STATE_TEXT_EDITING) {
6433 do_but_textedit(C, block, but, data, event);
6434 retval = WM_UI_HANDLER_BREAK;
6435 }
6436 else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
6437 do_but_textedit_select(C, block, but, data, event);
6438 retval = WM_UI_HANDLER_BREAK;
6439 }
6440
6441 if (click) {
6442 /* we can click on the side arrows to increment/decrement,
6443 * or click inside to edit the value directly */
6444
6445 if (!button_is_float(but)) {
6446 /* Integer Value. */
6447 if (but->drawflag & (BUT_HOVER_LEFT | BUT_HOVER_RIGHT)) {
6449
6450 const int step = number_but->step_size * (event->modifier & KM_SHIFT ? 0.1 : 1);
6451 const int value_step = std::max(1, step);
6452 BLI_assert(value_step > 0);
6453 const int softmin = round_fl_to_int_clamp(but->softmin);
6454 const int softmax = round_fl_to_int_clamp(but->softmax);
6455 const double value_test = (but->drawflag & BUT_HOVER_LEFT) ?
6456 double(max_ii(softmin, int(data->value) - value_step)) :
6457 double(min_ii(softmax, int(data->value) + value_step));
6458 if (value_test != data->value) {
6459 data->value = double(value_test);
6460 }
6461 else {
6462 data->cancel = true;
6463 }
6465 }
6466 else {
6468 }
6469 }
6470 else {
6471 /* Float Value. */
6472 if (but->drawflag & (BUT_HOVER_LEFT | BUT_HOVER_RIGHT)) {
6473 const PropertyScaleType scale_type = button_scale_type(but);
6474
6476
6477 double value_step;
6478 if (scale_type == PROP_SCALE_LOG) {
6479 double precision = (roundf(log10f(data->value) + UI_PROP_SCALE_LOG_SNAP_OFFSET) - 1.0f) +
6480 log10f(number_but->step_size);
6481 /* Non-finite when `data->value` is zero. */
6482 if (!isfinite(precision)) [[unlikely]] {
6483 precision = -FLT_MAX; /* Ignore this value. */
6484 }
6485 value_step = powf(10.0f, max_ff(precision, -number_but->precision));
6486 }
6487 else {
6488 value_step = double(number_but->step_size * UI_PRECISION_FLOAT_SCALE);
6489 }
6490
6491 const eSnapType snap = ISMOUSE_BUTTON(event->type) ? event_to_snap(event) : SNAP_OFF;
6492 value_step = (snap == SNAP_OFF) ? value_step : number_but->step_size;
6493 if (event->modifier & KM_SHIFT) {
6494 value_step *= 0.1;
6495 }
6496
6497 BLI_assert(value_step > 0.0f);
6498 const double value_test =
6499 (but->drawflag & BUT_HOVER_LEFT) ?
6500 double(max_ff(but->softmin, float(data->value - value_step))) :
6501 double(min_ff(but->softmax, float(data->value + value_step)));
6502 if (value_test != data->value) {
6503 data->value = numedit_apply_snapf(but, value_test, but->softmin, but->softmax, snap);
6504 }
6505 else {
6506 data->cancel = true;
6507 }
6509 }
6510 else {
6512 }
6513 }
6514
6515 retval = WM_UI_HANDLER_BREAK;
6516 }
6517
6518 data->draglastx = mx;
6519 data->draglasty = my;
6520
6521 return retval;
6522}
6523
6524static bool numedit_but_SLI(Button *but,
6526 int mx,
6527 FunctionRef<int()> drag_threshold_fn,
6528 const bool is_horizontal,
6529 const bool is_motion,
6530 const bool snap,
6531 const bool shift)
6532{
6533 float cursor_x_range, f, tempf, softmin, softmax, softrange;
6534 int temp, lvalue;
6535 bool changed = false;
6536 float mx_fl, my_fl;
6537
6538 /* prevent unwanted drag adjustments, test motion so modifier keys refresh. */
6539 if ((but->type != ButtonType::Scroll) && (is_motion || data->draglock) &&
6540 (but_dragedit_update_mval(data, mx, drag_threshold_fn) == false))
6541 {
6542 return changed;
6543 }
6544
6546 static_cast<bContext *>(but->block->evil_C), but->block, data, false);
6547
6548 const PropertyScaleType scale_type = button_scale_type(but);
6549
6550 softmin = but->softmin;
6551 softmax = but->softmax;
6552 softrange = softmax - softmin;
6553
6554 /* yes, 'mx' as both x/y is intentional */
6555 mouse_scale_warp(data, mx, mx, &mx_fl, &my_fl, shift);
6556
6557 if (but->type == ButtonType::NumSlider) {
6558 cursor_x_range = BLI_rctf_size_x(&but->rect);
6559 }
6560 else if (but->type == ButtonType::Scroll) {
6561 auto *scroll_but = reinterpret_cast<ButtonScrollBar *>(but);
6562 const float size = (is_horizontal) ? BLI_rctf_size_x(&but->rect) :
6563 -BLI_rctf_size_y(&but->rect);
6564 cursor_x_range = size * (but->softmax - but->softmin) /
6565 (but->softmax - but->softmin + scroll_but->visual_height);
6566 }
6567 else {
6568 const float ofs = (BLI_rctf_size_y(&but->rect) / 2.0f);
6569 cursor_x_range = (BLI_rctf_size_x(&but->rect) - ofs);
6570 }
6571
6572 f = (mx_fl - data->dragstartx) / cursor_x_range + data->dragfstart;
6573 CLAMP(f, 0.0f, 1.0f);
6574
6575 /* deal with mouse correction */
6576#ifdef USE_CONT_MOUSE_CORRECT
6577 if (button_is_cursor_warp(but)) {
6578 /* OK but can go outside bounds */
6579 if (is_horizontal) {
6580 data->ungrab_mval[0] = but->rect.xmin + (f * cursor_x_range);
6581 data->ungrab_mval[1] = BLI_rctf_cent_y(&but->rect);
6582 }
6583 else {
6584 data->ungrab_mval[1] = but->rect.ymin + (f * cursor_x_range);
6585 data->ungrab_mval[0] = BLI_rctf_cent_x(&but->rect);
6586 }
6587 BLI_rctf_clamp_pt_v(&but->rect, data->ungrab_mval);
6588 }
6589#endif
6590 /* done correcting mouse */
6591
6592 switch (scale_type) {
6593 case PROP_SCALE_LINEAR: {
6594 tempf = softmin + f * softrange;
6595 break;
6596 }
6597 case PROP_SCALE_LOG: {
6598 tempf = powf(softmax / softmin, f) * softmin;
6599 break;
6600 }
6601 case PROP_SCALE_CUBIC: {
6602 const float cubicmin = cube_f(softmin);
6603 const float cubicmax = cube_f(softmax);
6604 const float cubicrange = cubicmax - cubicmin;
6605 tempf = cube_f(softmin + f * softrange);
6606 tempf = (tempf - cubicmin) / cubicrange * softrange + softmin;
6607 break;
6608 }
6609 }
6610 temp = round_fl_to_int(tempf);
6611
6612 if (snap) {
6613 if (ELEM(tempf, softmin, softmax)) {
6614 /* pass */
6615 }
6616 else if (button_is_float(but)) {
6617
6618 if (shift) {
6619 if (ELEM(tempf, softmin, softmax)) {
6620 }
6621 else if (softrange < 2.10f) {
6622 tempf = roundf(tempf * 100.0f) * 0.01f;
6623 }
6624 else if (softrange < 21.0f) {
6625 tempf = roundf(tempf * 10.0f) * 0.1f;
6626 }
6627 else {
6628 tempf = roundf(tempf);
6629 }
6630 }
6631 else {
6632 if (softrange < 2.10f) {
6633 tempf = roundf(tempf * 10.0f) * 0.1f;
6634 }
6635 else if (softrange < 21.0f) {
6636 tempf = roundf(tempf);
6637 }
6638 else {
6639 tempf = roundf(tempf * 0.1f) * 10.0f;
6640 }
6641 }
6642 }
6643 else {
6644 temp = 10 * (temp / 10);
6645 tempf = temp;
6646 }
6647 }
6648
6649 if (!button_is_float(but)) {
6650 lvalue = round(data->value);
6651
6652 CLAMP(temp, softmin, softmax);
6653
6654 if (temp != lvalue) {
6655 data->value = temp;
6656 data->dragchange = true;
6657 changed = true;
6658 }
6659 }
6660 else {
6661 CLAMP(tempf, softmin, softmax);
6662
6663 if (tempf != float(data->value)) {
6664 data->value = tempf;
6665 data->dragchange = true;
6666 changed = true;
6667 }
6668 }
6669
6670 return changed;
6671}
6672
6673static int do_but_SLI(
6674 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
6675{
6676 int click = 0;
6677 int retval = WM_UI_HANDLER_CONTINUE;
6678
6679 int mx = event->xy[0];
6680 int my = event->xy[1];
6681 window_to_block(data->region, block, &mx, &my);
6682
6683 /* Defer evaluation as it's rarely needed. */
6684 auto drag_threshold_fn = [event]() -> int { return WM_event_drag_threshold(event); };
6685
6686 if (data->state == BUTTON_STATE_HIGHLIGHT) {
6687 int type = event->type, val = event->val;
6688
6689 if (type == MOUSEPAN) {
6690 pan_to_scroll(event, &type, &val);
6691 }
6692
6693 /* XXX hardcoded keymap check.... */
6694 if ((type == MOUSEPAN) && (event->modifier & KM_CTRL)) {
6695 /* allow accumulating values, otherwise scrolling gets preference */
6696 retval = WM_UI_HANDLER_BREAK;
6697 }
6698 else if ((type == WHEELDOWNMOUSE) && (event->modifier & KM_CTRL)) {
6699 mx = but->rect.xmin;
6700 click = 2;
6701 }
6702 else if ((type == WHEELUPMOUSE) && (event->modifier & KM_CTRL)) {
6703 mx = but->rect.xmax;
6704 click = 2;
6705 }
6706 else if (event->val == KM_PRESS) {
6707 if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && (event->modifier & KM_CTRL)) {
6709 retval = WM_UI_HANDLER_BREAK;
6710 }
6711#ifndef USE_ALLSELECT
6712 /* alt-click on sides to get "arrows" like in ButtonType::Num buttons,
6713 * and match wheel usage above */
6714 else if ((event->type == LEFTMOUSE) && (event->modifier & KM_ALT)) {
6715 int halfpos = BLI_rctf_cent_x(&but->rect);
6716 click = 2;
6717 if (mx < halfpos) {
6718 mx = but->rect.xmin;
6719 }
6720 else {
6721 mx = but->rect.xmax;
6722 }
6723 }
6724#endif
6725 else if (event->type == LEFTMOUSE) {
6726 data->dragstartx = mx;
6727 data->draglastx = mx;
6729 retval = WM_UI_HANDLER_BREAK;
6730 }
6731 else if (ELEM(event->type, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
6732 click = 1;
6733 }
6734 else if (event->type == EVT_MINUSKEY && event->val == KM_PRESS) {
6736 data->value = -data->value;
6738 retval = WM_UI_HANDLER_BREAK;
6739 }
6740 }
6741#ifdef USE_DRAG_MULTINUM
6742 copy_v2_v2_int(data->multi_data.drag_start, event->xy);
6743 data->multi_data.drag_dir_prev[0] = mx;
6744 data->multi_data.drag_dir_prev[1] = my;
6745#endif
6746 }
6747 else if (data->state == BUTTON_STATE_NUM_EDITING) {
6748 if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
6749 if (event->val == KM_PRESS) {
6750 data->cancel = true;
6751 data->escapecancel = true;
6753 }
6754 }
6755 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
6756 if (data->dragchange) {
6757#ifdef USE_DRAG_MULTINUM
6758 /* If we started multi-button but didn't drag, then edit. */
6759 if (data->multi_data.init == HandleButtonMulti::INIT_SETUP) {
6760 click = 1;
6761 }
6762 else
6763#endif
6764 {
6766 }
6767 }
6768 else {
6769#ifdef USE_CONT_MOUSE_CORRECT
6770 /* reset! */
6771 copy_v2_fl(data->ungrab_mval, FLT_MAX);
6772#endif
6773 click = 1;
6774 }
6775 }
6776 else if ((event->type == MOUSEMOVE) || event_is_snap(event)) {
6777 const bool is_motion = (event->type == MOUSEMOVE);
6778#ifdef USE_DRAG_MULTINUM
6779 data->multi_data.drag_dir[0] += abs(data->multi_data.drag_dir_prev[0] - mx);
6780 data->multi_data.drag_dir[1] += abs(data->multi_data.drag_dir_prev[1] - my);
6781 data->multi_data.drag_dir_prev[0] = mx;
6782 data->multi_data.drag_dir_prev[1] = my;
6783#endif
6784 if (numedit_but_SLI(but,
6785 data,
6786 mx,
6787 drag_threshold_fn,
6788 true,
6789 is_motion,
6790 event->modifier & KM_CTRL,
6791 event->modifier & KM_SHIFT))
6792 {
6793 numedit_apply(C, block, but, data);
6794 }
6795
6796#ifdef USE_DRAG_MULTINUM
6797 else if (data->multi_data.has_mbuts) {
6798 if (data->multi_data.init == HandleButtonMulti::INIT_ENABLE) {
6799 multibut_states_apply(C, data, block);
6800 }
6801 }
6802#endif
6803 }
6804 retval = WM_UI_HANDLER_BREAK;
6805 }
6806 else if (data->state == BUTTON_STATE_TEXT_EDITING) {
6807 do_but_textedit(C, block, but, data, event);
6808 retval = WM_UI_HANDLER_BREAK;
6809 }
6810 else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
6811 do_but_textedit_select(C, block, but, data, event);
6812 retval = WM_UI_HANDLER_BREAK;
6813 }
6814
6815 if (click) {
6816 if (click == 2) {
6817 const PropertyScaleType scale_type = button_scale_type(but);
6818
6819 /* nudge slider to the left or right */
6820 float f, tempf, softmin, softmax, softrange;
6821 int temp;
6822
6824
6825 softmin = but->softmin;
6826 softmax = but->softmax;
6827 softrange = softmax - softmin;
6828
6829 tempf = data->value;
6830 temp = int(data->value);
6831
6832#if 0
6833 if (but->type == SLI) {
6834 /* same as below */
6835 f = float(mx - but->rect.xmin) / (BLI_rctf_size_x(&but->rect));
6836 }
6837 else
6838#endif
6839 {
6840 f = float(mx - but->rect.xmin) / BLI_rctf_size_x(&but->rect);
6841 }
6842
6843 if (scale_type == PROP_SCALE_LOG) {
6844 f = powf(softmax / softmin, f) * softmin;
6845 }
6846 else {
6847 f = softmin + f * softrange;
6848 }
6849
6850 if (!button_is_float(but)) {
6851 int value_step = 1;
6852 if (f < temp) {
6853 temp -= value_step;
6854 }
6855 else {
6856 temp += value_step;
6857 }
6858
6859 if (temp >= softmin && temp <= softmax) {
6860 data->value = temp;
6861 }
6862 else {
6863 data->cancel = true;
6864 }
6865 }
6866 else {
6867 if (tempf >= softmin && tempf <= softmax) {
6868 float value_step;
6869 if (scale_type == PROP_SCALE_LOG) {
6870 value_step = powf(10.0f, roundf(log10f(tempf) + UI_PROP_SCALE_LOG_SNAP_OFFSET) - 1.0f);
6871 }
6872 else {
6873 value_step = 0.01f;
6874 }
6875
6876 if (f < tempf) {
6877 tempf -= value_step;
6878 }
6879 else {
6880 tempf += value_step;
6881 }
6882
6883 CLAMP(tempf, softmin, softmax);
6884 data->value = tempf;
6885 }
6886 else {
6887 data->cancel = true;
6888 }
6889 }
6890
6892 retval = WM_UI_HANDLER_BREAK;
6893 }
6894 else {
6895 /* edit the value directly */
6897 retval = WM_UI_HANDLER_BREAK;
6898 }
6899 }
6900
6901 data->draglastx = mx;
6902 data->draglasty = my;
6903
6904 return retval;
6905}
6906
6907static int do_but_SCROLL(
6908 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
6909{
6910 int retval = WM_UI_HANDLER_CONTINUE;
6911 const bool horizontal = (BLI_rctf_size_x(&but->rect) > BLI_rctf_size_y(&but->rect));
6912
6913 int mx = event->xy[0];
6914 int my = event->xy[1];
6915 window_to_block(data->region, block, &mx, &my);
6916
6917 /* Defer evaluation as it's rarely needed. */
6918 auto drag_threshold_fn = [event]() -> int { return WM_event_drag_threshold(event); };
6919
6920 if (data->state == BUTTON_STATE_HIGHLIGHT) {
6921 if (event->val == KM_PRESS) {
6922 if (event->type == LEFTMOUSE) {
6923 if (horizontal) {
6924 data->dragstartx = mx;
6925 data->draglastx = mx;
6926 }
6927 else {
6928 data->dragstartx = my;
6929 data->draglastx = my;
6930 }
6932 retval = WM_UI_HANDLER_BREAK;
6933 }
6934 }
6935 }
6936 else if (data->state == BUTTON_STATE_NUM_EDITING) {
6937 if (event->type == EVT_ESCKEY) {
6938 if (event->val == KM_PRESS) {
6939 data->cancel = true;
6940 data->escapecancel = true;
6942 }
6943 }
6944 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
6946 }
6947 else if (event->type == MOUSEMOVE) {
6948 const bool is_motion = true;
6949 if (numedit_but_SLI(but,
6950 data,
6951 (horizontal) ? mx : my,
6952 drag_threshold_fn,
6953 horizontal,
6954 is_motion,
6955 false,
6956 false))
6957 {
6958 /* Scroll-bars in popups need UI layout refresh to update the right items to show. */
6959 if (block_is_popup_any(but->block)) {
6961 }
6962 numedit_apply(C, block, but, data);
6963 }
6964 }
6965
6966 retval = WM_UI_HANDLER_BREAK;
6967 }
6968
6969 return retval;
6970}
6971
6972static int do_but_GRIP(
6973 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
6974{
6975 int retval = WM_UI_HANDLER_CONTINUE;
6976 const bool horizontal = (BLI_rctf_size_x(&but->rect) < BLI_rctf_size_y(&but->rect));
6977
6978 /* NOTE: Having to store org point in window space and recompute it to block "space" each time
6979 * is not ideal, but this is a way to hack around behavior of window_to_block(), which
6980 * returns different results when the block is inside a panel or not...
6981 * See #37739.
6982 */
6983
6984 int mx = event->xy[0];
6985 int my = event->xy[1];
6986 window_to_block(data->region, block, &mx, &my);
6987
6988 if (data->state == BUTTON_STATE_HIGHLIGHT) {
6989 if (event->val == KM_PRESS) {
6990 if (event->type == LEFTMOUSE) {
6991 data->dragstartx = event->xy[0];
6992 data->dragstarty = event->xy[1];
6994 retval = WM_UI_HANDLER_BREAK;
6995 }
6996 }
6997 }
6998 else if (data->state == BUTTON_STATE_NUM_EDITING) {
6999 if (event->type == EVT_ESCKEY) {
7000 if (event->val == KM_PRESS) {
7001 data->cancel = true;
7002 data->escapecancel = true;
7004 }
7005 }
7006 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
7008 }
7009 else if (event->type == MOUSEMOVE) {
7010 int dragstartx = data->dragstartx;
7011 int dragstarty = data->dragstarty;
7012 window_to_block(data->region, block, &dragstartx, &dragstarty);
7013 data->value = data->origvalue + (horizontal ? mx - dragstartx : dragstarty - my);
7014 numedit_apply(C, block, but, data);
7015 if (block_is_popup_any(block)) {
7017 }
7018 }
7019
7020 retval = WM_UI_HANDLER_BREAK;
7021 }
7022
7023 return retval;
7024}
7025
7026static int do_but_LISTROW(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
7027{
7028 if (data->state == BUTTON_STATE_HIGHLIGHT) {
7029 /* hack to pass on ctrl+click and double click to overlapping text
7030 * editing field for editing list item names
7031 */
7032 if ((ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && (event->val == KM_PRESS) &&
7033 (event->modifier & KM_CTRL)) ||
7034 (event->type == LEFTMOUSE && event->val == KM_DBL_CLICK))
7035 {
7037 C, but, data, event, BUTTON_ACTIVATE_TEXT_EDITING);
7038 if (labelbut) {
7039 /* Nothing else to do. */
7040 return WM_UI_HANDLER_BREAK;
7041 }
7042 }
7043 }
7044
7045 return do_but_EXIT(C, but, data, event);
7046}
7047
7048static int do_but_BLOCK(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
7049{
7050 if (data->state == BUTTON_STATE_HIGHLIGHT) {
7051
7052 /* First handle click on icon-drag type button. */
7053 if (event->type == LEFTMOUSE && button_drag_is_draggable(but) && event->val == KM_PRESS) {
7054 if (but_contains_point_px_icon(but, data->region, event)) {
7056 data->dragstartx = event->xy[0];
7057 data->dragstarty = event->xy[1];
7058 return WM_UI_HANDLER_BREAK;
7059 }
7060 }
7061#ifdef USE_DRAG_TOGGLE
7062 if (event->type == LEFTMOUSE && event->val == KM_PRESS && but_is_drag_toggle(but)) {
7064 data->dragstartx = event->xy[0];
7065 data->dragstarty = event->xy[1];
7066 return WM_UI_HANDLER_BREAK;
7067 }
7068#endif
7069 /* regular open menu */
7070 if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
7072 return WM_UI_HANDLER_BREAK;
7073 }
7074 if (button_supports_cycling(but)) {
7075 if (ELEM(event->type, MOUSEPAN, WHEELDOWNMOUSE, WHEELUPMOUSE) && (event->modifier & KM_CTRL))
7076 {
7077 int type = event->type;
7078 int val = event->val;
7079
7080 /* Convert pan to scroll-wheel. */
7081 if (type == MOUSEPAN) {
7082 pan_to_scroll(event, &type, &val);
7083
7084 if (type == MOUSEPAN) {
7085 return WM_UI_HANDLER_BREAK;
7086 }
7087 }
7088
7089 const int direction = (type == WHEELDOWNMOUSE) ? 1 : -1;
7090
7091 data->value = button_menu_step(but, direction);
7092
7094 apply_but(C, but->block, but, data, true);
7095
7096 /* Button's state need to be changed to EXIT so moving mouse away from this mouse
7097 * wouldn't lead to cancel changes made to this button, but changing state to EXIT also
7098 * makes no button active for a while which leads to triggering operator when doing fast
7099 * scrolling mouse wheel. using post activate stuff from button allows to make button be
7100 * active again after checking for all that mouse leave and cancel stuff, so quick
7101 * scroll wouldn't be an issue anymore. Same goes for scrolling wheel in another
7102 * direction below (sergey).
7103 */
7104 data->postbut = but;
7105 data->posttype = BUTTON_ACTIVATE_OVER;
7106
7107 /* without this, a new interface that draws as result of the menu change
7108 * won't register that the mouse is over it, eg:
7109 * Alt+MouseWheel over the render slots, without this,
7110 * the slot menu fails to switch a second time.
7111 *
7112 * The active state of the button could be maintained some other way
7113 * and remove this mouse-move event.
7114 */
7116
7117 return WM_UI_HANDLER_BREAK;
7118 }
7119 }
7120 }
7121 else if (data->state == BUTTON_STATE_WAIT_DRAG) {
7122
7123 /* this function also ends state */
7124 if (but_drag_init(C, but, data, event)) {
7125 return WM_UI_HANDLER_BREAK;
7126 }
7127
7128 /* outside icon quit, not needed if drag activated */
7129 if (0 == but_contains_point_px_icon(but, data->region, event)) {
7131 data->cancel = true;
7132 return WM_UI_HANDLER_BREAK;
7133 }
7134
7135 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
7137 return WM_UI_HANDLER_BREAK;
7138 }
7139 }
7140
7142}
7143
7145 Button *but, HandleButtonData *data, int mx, int my, const eSnapType snap)
7146{
7147 float mrad;
7148 bool changed = true;
7149
7150 /* button is presumed square */
7151 /* if mouse moves outside of sphere, it does negative normal */
7152
7153 /* note that both data->vec and data->origvec should be normalized
7154 * else we'll get a harmless but annoying jump when first clicking */
7155
7156 float *fp = data->origvec;
7157 const float rad = BLI_rctf_size_x(&but->rect);
7158 const float radsq = rad * rad;
7159
7160 int mdx, mdy;
7161 if (fp[2] > 0.0f) {
7162 mdx = (rad * fp[0]);
7163 mdy = (rad * fp[1]);
7164 }
7165 else if (fp[2] > -1.0f) {
7166 mrad = rad / sqrtf(fp[0] * fp[0] + fp[1] * fp[1]);
7167
7168 mdx = 2.0f * mrad * fp[0] - (rad * fp[0]);
7169 mdy = 2.0f * mrad * fp[1] - (rad * fp[1]);
7170 }
7171 else {
7172 mdx = mdy = 0;
7173 }
7174
7175 float dx = float(mx + mdx - data->dragstartx);
7176 float dy = float(my + mdy - data->dragstarty);
7177
7178 fp = data->vec;
7179 mrad = dx * dx + dy * dy;
7180 if (mrad < radsq) { /* inner circle */
7181 fp[0] = dx;
7182 fp[1] = dy;
7183 fp[2] = sqrtf(radsq - dx * dx - dy * dy);
7184 }
7185 else { /* outer circle */
7186
7187 mrad = rad / sqrtf(mrad); /* veclen */
7188
7189 dx *= (2.0f * mrad - 1.0f);
7190 dy *= (2.0f * mrad - 1.0f);
7191
7192 mrad = dx * dx + dy * dy;
7193 if (mrad < radsq) {
7194 fp[0] = dx;
7195 fp[1] = dy;
7196 fp[2] = -sqrtf(radsq - dx * dx - dy * dy);
7197 }
7198 }
7199 normalize_v3(fp);
7200
7201 if (snap != SNAP_OFF) {
7202 const int snap_steps = (snap == SNAP_ON) ? 4 : 12; /* 45 or 15 degree increments */
7203 const float snap_steps_angle = M_PI / snap_steps;
7204 float angle, angle_snap;
7205
7206 /* round each axis of 'fp' to the next increment
7207 * do this in "angle" space - this gives increments of same size */
7208 for (int i = 0; i < 3; i++) {
7209 angle = asinf(fp[i]);
7210 angle_snap = roundf(angle / snap_steps_angle) * snap_steps_angle;
7211 fp[i] = sinf(angle_snap);
7212 }
7213 normalize_v3(fp);
7214 changed = !compare_v3v3(fp, data->origvec, FLT_EPSILON);
7215 }
7216
7217 data->draglastx = mx;
7218 data->draglasty = my;
7219
7220 return changed;
7221}
7222
7223static void palette_set_active(ButtonColor *color_but)
7224{
7225 if (color_but->is_palette_color) {
7226 Palette *palette = id_cast<Palette *>(color_but->rnapoin.owner_id);
7227 const PaletteColor *color = static_cast<const PaletteColor *>(color_but->rnapoin.data);
7228 palette->active_color = BLI_findindex(&palette->colors, color);
7229 }
7230}
7231
7232static int do_but_COLOR(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
7233{
7235 ButtonColor *color_but = static_cast<ButtonColor *>(but);
7236
7237 if (data->state == BUTTON_STATE_HIGHLIGHT) {
7238 /* First handle click on icon-drag type button. */
7239 if (event->type == LEFTMOUSE && button_drag_is_draggable(but) && event->val == KM_PRESS) {
7240 palette_set_active(color_but);
7241 if (but_contains_point_px_icon(but, data->region, event)) {
7243 data->dragstartx = event->xy[0];
7244 data->dragstarty = event->xy[1];
7245 return WM_UI_HANDLER_BREAK;
7246 }
7247 }
7248#ifdef USE_DRAG_TOGGLE
7249 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
7250 palette_set_active(color_but);
7252 data->dragstartx = event->xy[0];
7253 data->dragstarty = event->xy[1];
7254 return WM_UI_HANDLER_BREAK;
7255 }
7256#endif
7257 /* regular open menu */
7258 if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) {
7259 palette_set_active(color_but);
7261 return WM_UI_HANDLER_BREAK;
7262 }
7263 if (ELEM(event->type, MOUSEPAN, WHEELDOWNMOUSE, WHEELUPMOUSE) && (event->modifier & KM_CTRL)) {
7264 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
7265 float hsv_static[3] = {0.0f};
7266 float *hsv = cpicker ? cpicker->hsv_perceptual : hsv_static;
7267 float col[3];
7268
7269 button_v3_get(but, col);
7270 rgb_to_hsv_compat_v(col, hsv);
7271
7272 if (event->type == WHEELDOWNMOUSE) {
7273 hsv[2] = clamp_f(hsv[2] - 0.05f, 0.0f, 1.0f);
7274 }
7275 else if (event->type == WHEELUPMOUSE) {
7276 hsv[2] = clamp_f(hsv[2] + 0.05f, 0.0f, 1.0f);
7277 }
7278 else {
7279 const float fac = 0.005 * (event->xy[1] - event->prev_xy[1]);
7280 hsv[2] = clamp_f(hsv[2] + fac, 0.0f, 1.0f);
7281 }
7282
7283 hsv_to_rgb_v(hsv, data->vec);
7284 button_v3_set(but, data->vec);
7285
7287 apply_but(C, but->block, but, data, true);
7288 return WM_UI_HANDLER_BREAK;
7289 }
7290 if (color_but->is_palette_color && (event->type == EVT_DELKEY) && (event->val == KM_PRESS)) {
7291 Palette *palette = id_cast<Palette *>(but->rnapoin.owner_id);
7292 PaletteColor *color = static_cast<PaletteColor *>(but->rnapoin.data);
7293
7295
7297
7298 /* this is risky. it works OK for now,
7299 * but if it gives trouble we should delay execution */
7300 but->rnapoin = {};
7301 but->rnaprop = nullptr;
7302
7303 return WM_UI_HANDLER_BREAK;
7304 }
7305 }
7306 else if (data->state == BUTTON_STATE_WAIT_DRAG) {
7307
7308 /* this function also ends state */
7309 if (but_drag_init(C, but, data, event)) {
7310 return WM_UI_HANDLER_BREAK;
7311 }
7312
7313 /* outside icon quit, not needed if drag activated */
7314 if (0 == but_contains_point_px_icon(but, data->region, event)) {
7316 data->cancel = true;
7317 return WM_UI_HANDLER_BREAK;
7318 }
7319
7320 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
7321 if (color_but->is_palette_color) {
7322 if ((event->modifier & KM_CTRL) == 0) {
7323 float color[3];
7325 if (paint != nullptr) {
7327
7328 if (brush == nullptr) {
7329 /* Pass. */
7330 }
7331 else if (brush->flag & BRUSH_USE_GRADIENT) {
7332 float *target = &brush->gradient->data[brush->gradient->cur].r;
7333
7337 }
7338 else if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR) {
7340 }
7342 }
7343 else {
7344 bool updated = false;
7345
7348 &but->rnapoin, but->rnaprop, color, ARRAY_SIZE(color));
7351 updated = true;
7352 }
7353 else if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR) {
7355 &but->rnapoin, but->rnaprop, color, ARRAY_SIZE(color));
7357 updated = true;
7358 }
7359
7360 if (updated) {
7361 PropertyRNA *brush_color_prop;
7362
7363 PointerRNA brush_ptr = RNA_id_pointer_create(&brush->id);
7364 brush_color_prop = RNA_struct_find_property(&brush_ptr, "color");
7365 RNA_property_update(C, &brush_ptr, brush_color_prop);
7366 }
7367 }
7368 }
7369
7371 }
7372 else {
7374 }
7375 }
7376 else {
7378 }
7379 return WM_UI_HANDLER_BREAK;
7380 }
7381 }
7382
7384}
7385
7387 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
7388{
7389 int mx = event->xy[0];
7390 int my = event->xy[1];
7391 window_to_block(data->region, block, &mx, &my);
7392
7393 if (data->state == BUTTON_STATE_HIGHLIGHT) {
7394 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
7395 const eSnapType snap = event_to_snap(event);
7396 data->dragstartx = mx;
7397 data->dragstarty = my;
7398 data->draglastx = mx;
7399 data->draglasty = my;
7401
7402 /* also do drag the first time */
7403 if (numedit_but_UNITVEC(but, data, mx, my, snap)) {
7404 numedit_apply(C, block, but, data);
7405 }
7406
7407 return WM_UI_HANDLER_BREAK;
7408 }
7409 }
7410 else if (data->state == BUTTON_STATE_NUM_EDITING) {
7411 if ((event->type == MOUSEMOVE) || event_is_snap(event)) {
7412 if (mx != data->draglastx || my != data->draglasty || event->type != MOUSEMOVE) {
7413 const eSnapType snap = event_to_snap(event);
7414 if (numedit_but_UNITVEC(but, data, mx, my, snap)) {
7415 numedit_apply(C, block, but, data);
7416 }
7417 }
7418 }
7419 else if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
7420 if (event->val == KM_PRESS) {
7421 data->cancel = true;
7422 data->escapecancel = true;
7424 }
7425 }
7426 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
7428 }
7429
7430 return WM_UI_HANDLER_BREAK;
7431 }
7432
7434}
7435
7436/* scales a vector so no axis exceeds max
7437 * (could become BLI_math func) */
7438static void clamp_axis_max_v3(float v[3], const float max)
7439{
7440 const float v_max = std::max({v[0], v[1], v[2]});
7441 if (v_max > max) {
7442 mul_v3_fl(v, max / v_max);
7443 v[0] = std::min(v[0], max);
7444 v[1] = std::min(v[1], max);
7445 v[2] = std::min(v[2], max);
7446 }
7447}
7448
7450 const float rgb[3],
7451 float hsv[3])
7452{
7453 if (hsv_but->gradient_type == GRAD_L_ALT) {
7454 rgb_to_hsl_compat_v(rgb, hsv);
7455 }
7456 else {
7457 rgb_to_hsv_compat_v(rgb, hsv);
7458 }
7459}
7460
7462 const float rgb[3],
7463 float hsv[3])
7464{
7465 if (hsv_but->gradient_type == GRAD_L_ALT) {
7466 rgb_to_hsl_v(rgb, hsv);
7467 }
7468 else {
7469 rgb_to_hsv_v(rgb, hsv);
7470 }
7471}
7472
7474 const float hsv[3],
7475 float rgb[3])
7476{
7477 if (hsv_but->gradient_type == GRAD_L_ALT) {
7478 hsl_to_rgb_v(hsv, rgb);
7479 }
7480 else {
7481 hsv_to_rgb_v(hsv, rgb);
7482 }
7483}
7484
7487 int mx,
7488 int my,
7489 const eSnapType snap,
7490 const bool shift,
7491 const bool use_continuous_grab,
7492 bool start_drag = false)
7493{
7494 const ButtonHSVCube *hsv_but = static_cast<ButtonHSVCube *>(but);
7495 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
7496 float *hsv = cpicker->hsv_perceptual;
7497 float rgb[3];
7498 float x, y;
7499 const bool changed = true;
7500 if (start_drag) {
7501 if (shift) {
7502 rcti rect_i;
7503 float xpos, ypos, hsvo[3];
7504
7505 BLI_rcti_rctf_copy(&rect_i, &but->rect);
7506 copy_v3_v3(rgb, data->vec);
7508 copy_v3_v3(hsvo, hsv);
7509
7510 rgb_to_color_picker_HSVCUBE_compat_v(hsv_but, rgb, hsvo);
7511
7512 /* Get original position. */
7513 hsvcube_pos_from_vals(hsv_but, &rect_i, hsvo, &xpos, &ypos);
7514 data->relative_drag = {xpos, ypos};
7515 }
7516 else {
7517 data->relative_drag = {float(mx), float(my)};
7518 }
7519 }
7520
7521 if (use_continuous_grab || shift) {
7522 rcti rect;
7523 BLI_rcti_rctf_copy(&rect, &but->rect);
7524
7525 const float fac = mouse_scale_warp_factor(shift);
7526 data->relative_drag.x += float(mx - data->draglastx) * fac;
7527 data->relative_drag.y += float(my - data->draglasty) * fac;
7528 BLI_rctf_clamp_pt_v(&but->rect, data->relative_drag);
7529 }
7530 else {
7531 data->relative_drag.x = mx;
7532 data->relative_drag.y = my;
7533 }
7534
7535#ifdef USE_CONT_MOUSE_CORRECT
7536 if (use_continuous_grab) {
7537 data->ungrab_mval[0] = data->relative_drag.x;
7538 data->ungrab_mval[1] = data->relative_drag.y;
7539 }
7540#endif
7541
7542 copy_v3_v3(rgb, data->origvec);
7544
7545 rgb_to_color_picker_HSVCUBE_compat_v(hsv_but, rgb, hsv);
7546
7547 /* relative position within box */
7548 x = (float(data->relative_drag.x) - but->rect.xmin) / BLI_rctf_size_x(&but->rect);
7549 y = (float(data->relative_drag.y) - but->rect.ymin) / BLI_rctf_size_y(&but->rect);
7550 CLAMP(x, 0.0f, 1.0f);
7551 CLAMP(y, 0.0f, 1.0f);
7552
7553 switch (hsv_but->gradient_type) {
7554 case GRAD_SV:
7555 hsv[1] = x;
7556 hsv[2] = y;
7557 break;
7558 case GRAD_HV:
7559 hsv[0] = x;
7560 hsv[2] = y;
7561 break;
7562 case GRAD_HS:
7563 hsv[0] = x;
7564 hsv[1] = y;
7565 break;
7566 case GRAD_H:
7567 hsv[0] = x;
7568 break;
7569 case GRAD_S:
7570 hsv[1] = x;
7571 break;
7572 case GRAD_V:
7573 hsv[2] = x;
7574 break;
7575 case GRAD_L_ALT:
7576 hsv[2] = y;
7577 break;
7578 case GRAD_V_ALT: {
7579 /* vertical 'value' strip */
7580 const float min = but->softmin, max = but->softmax;
7581 /* exception only for value strip - use the range set in but->min/max */
7582 hsv[2] = y * (max - min) + min;
7583 break;
7584 }
7585 default:
7586 BLI_assert(0);
7587 break;
7588 }
7589
7590 if (snap != SNAP_OFF) {
7591 if (ELEM(hsv_but->gradient_type, GRAD_HV, GRAD_HS, GRAD_H)) {
7592 color_snap_hue(snap, &hsv[0]);
7593 }
7594 }
7595
7596 color_picker_to_rgb_HSVCUBE_v(hsv_but, hsv, rgb);
7598
7599 /* clamp because with color conversion we can exceed range #34295. */
7600 if (hsv_but->gradient_type == GRAD_V_ALT) {
7601 clamp_axis_max_v3(rgb, but->softmax);
7602 }
7603
7604 copy_v3_v3(data->vec, rgb);
7605
7606 data->draglastx = mx;
7607 data->draglasty = my;
7608
7609 return changed;
7610}
7611
7612#ifdef WITH_INPUT_NDOF
7613static void ndofedit_but_HSVCUBE(ButtonHSVCube *hsv_but,
7614 HandleButtonData *data,
7615 const wmNDOFMotionData &ndof,
7616 const eSnapType snap,
7617 const bool shift)
7618{
7619 ColorPicker *cpicker = static_cast<ColorPicker *>(hsv_but->custom_data);
7620 float *hsv = cpicker->hsv_perceptual;
7621 const float hsv_v_max = max_ff(hsv[2], hsv_but->softmax);
7622 float rgb[3];
7623 const float sensitivity = (shift ? 0.15f : 0.3f) * ndof.time_delta;
7624
7625 button_v3_get(hsv_but, rgb);
7627 rgb_to_color_picker_HSVCUBE_compat_v(hsv_but, rgb, hsv);
7628
7629 switch (hsv_but->gradient_type) {
7630 case GRAD_SV:
7631 hsv[1] += ndof.rvec[2] * sensitivity;
7632 hsv[2] += ndof.rvec[0] * sensitivity;
7633 break;
7634 case GRAD_HV:
7635 hsv[0] += ndof.rvec[2] * sensitivity;
7636 hsv[2] += ndof.rvec[0] * sensitivity;
7637 break;
7638 case GRAD_HS:
7639 hsv[0] += ndof.rvec[2] * sensitivity;
7640 hsv[1] += ndof.rvec[0] * sensitivity;
7641 break;
7642 case GRAD_H:
7643 hsv[0] += ndof.rvec[2] * sensitivity;
7644 break;
7645 case GRAD_S:
7646 hsv[1] += ndof.rvec[2] * sensitivity;
7647 break;
7648 case GRAD_V:
7649 hsv[2] += ndof.rvec[2] * sensitivity;
7650 break;
7651 case GRAD_V_ALT:
7652 case GRAD_L_ALT:
7653 /* vertical 'value' strip */
7654
7655 /* exception only for value strip - use the range set in but->min/max */
7656 hsv[2] += ndof.rvec[0] * sensitivity;
7657
7658 CLAMP(hsv[2], hsv_but->softmin, hsv_but->softmax);
7659 break;
7660 default:
7661 BLI_assert_msg(0, "invalid hsv type");
7662 break;
7663 }
7664
7665 if (snap != SNAP_OFF) {
7666 if (ELEM(hsv_but->gradient_type, GRAD_HV, GRAD_HS, GRAD_H)) {
7667 color_snap_hue(snap, &hsv[0]);
7668 }
7669 }
7670
7671 /* ndof specific: the changes above aren't clamping */
7672 hsv_clamp_v(hsv, hsv_v_max);
7673
7674 color_picker_to_rgb_HSVCUBE_v(hsv_but, hsv, rgb);
7676
7677 copy_v3_v3(data->vec, rgb);
7678 button_v3_set(hsv_but, data->vec);
7679}
7680#endif /* WITH_INPUT_NDOF */
7681
7683 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
7684{
7685 ButtonHSVCube *hsv_but = static_cast<ButtonHSVCube *>(but);
7686 int mx = event->xy[0];
7687 int my = event->xy[1];
7688 window_to_block(data->region, block, &mx, &my);
7689
7690 if (data->state == BUTTON_STATE_HIGHLIGHT) {
7691 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
7692 const eSnapType snap = event_to_snap(event);
7693
7694 data->dragstartx = mx;
7695 data->dragstarty = my;
7696 data->draglastx = mx;
7697 data->draglasty = my;
7699
7700 /* also do drag the first time */
7701 const bool shift = event->modifier & KM_SHIFT;
7702 const bool use_continuous_grab = button_is_cursor_warp(but) &&
7703 event->tablet.active == EVT_TABLET_NONE;
7704 if (numedit_but_HSVCUBE(but, data, mx, my, snap, shift, use_continuous_grab, true)) {
7705 numedit_apply(C, block, but, data);
7706 }
7707
7708 return WM_UI_HANDLER_BREAK;
7709 }
7710#ifdef WITH_INPUT_NDOF
7711 if (event->type == NDOF_MOTION) {
7712 const wmNDOFMotionData &ndof = *static_cast<const wmNDOFMotionData *>(event->customdata);
7713 const eSnapType snap = event_to_snap(event);
7714
7715 ndofedit_but_HSVCUBE(hsv_but, data, ndof, snap, event->modifier & KM_SHIFT);
7716
7718 apply_but(C, but->block, but, data, true);
7719
7720 return WM_UI_HANDLER_BREAK;
7721 }
7722#endif /* WITH_INPUT_NDOF */
7723 /* XXX hardcoded keymap check.... */
7724 if (event->type == EVT_BACKSPACEKEY && event->val == KM_PRESS) {
7725 if (ELEM(hsv_but->gradient_type, GRAD_V_ALT, GRAD_L_ALT)) {
7726 int len;
7727
7728 /* reset only value */
7729
7731 if (ELEM(len, 3, 4)) {
7732 float rgb[3], def_hsv[3];
7733 float def[4];
7734 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
7735 float *hsv = cpicker->hsv_perceptual;
7736
7738 rgb_to_color_picker_HSVCUBE_v(hsv_but, def, def_hsv);
7739
7740 button_v3_get(but, rgb);
7741 rgb_to_color_picker_HSVCUBE_compat_v(hsv_but, rgb, hsv);
7742
7743 def_hsv[0] = hsv[0];
7744 def_hsv[1] = hsv[1];
7745
7746 color_picker_to_rgb_HSVCUBE_v(hsv_but, def_hsv, rgb);
7747 button_v3_set(but, rgb);
7748 apply_but_func(C, but);
7749
7750 RNA_property_update(C, &but->rnapoin, but->rnaprop);
7751 return WM_UI_HANDLER_BREAK;
7752 }
7753 }
7754 }
7755 }
7756 else if (data->state == BUTTON_STATE_NUM_EDITING) {
7757 if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
7758 if (event->val == KM_PRESS) {
7759 data->cancel = true;
7760 data->escapecancel = true;
7762 }
7763 }
7764 else if ((event->type == MOUSEMOVE) || event_is_snap(event)) {
7765 if (mx != data->draglastx || my != data->draglasty || event->type != MOUSEMOVE) {
7766 const eSnapType snap = event_to_snap(event);
7767
7768 const bool shift = event->modifier & KM_SHIFT;
7769 const bool use_continuous_grab = button_is_cursor_warp(but) &&
7770 event->tablet.active == EVT_TABLET_NONE;
7771 if (numedit_but_HSVCUBE(but, data, mx, my, snap, shift, use_continuous_grab)) {
7772 numedit_apply(C, block, but, data);
7773 }
7774 }
7775 }
7776 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
7778 }
7779
7780 return WM_UI_HANDLER_BREAK;
7781 }
7782
7784}
7785
7788 float mx,
7789 float my,
7790 const eSnapType snap,
7791 const bool shift,
7792 const bool use_continuous_grab,
7793 const bool start_drag = false)
7794{
7795 const bool changed = true;
7796 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
7797 float *hsv = cpicker->hsv_perceptual;
7798
7799 rcti rect;
7800 BLI_rcti_rctf_copy(&rect, &but->rect);
7801 if (start_drag) {
7802 /* Allow start editing the original color if shift is pressed at start. */
7803 if (shift) {
7804 float hsvo[3], rgbo[3];
7805
7806 /* Get original hsv. */
7807 copy_v3_v3(hsvo, hsv);
7808 copy_v3_v3(rgbo, data->origvec);
7811
7812 /* Get original color pick position within the circle. */
7814 cpicker, &rect, hsvo, &data->relative_drag.x, &data->relative_drag.y);
7815 }
7816 else {
7817 data->relative_drag.x = mx;
7818 data->relative_drag.y = my;
7819 }
7820 }
7821
7822 float rgb[3];
7823 copy_v3_v3(rgb, data->origvec);
7826
7827 if (use_continuous_grab || shift) {
7828 const float fac = mouse_scale_warp_factor(shift);
7829 data->relative_drag.x += (mx - float(data->draglastx)) * fac;
7830 data->relative_drag.y += (my - float(data->draglasty)) * fac;
7831
7832 const float radius = min_ff(BLI_rctf_size_x(&but->rect), BLI_rctf_size_y(&but->rect)) / 2.0f;
7833 const float cent[2] = {BLI_rctf_cent_x(&but->rect), BLI_rctf_cent_y(&but->rect)};
7834 const float len = len_v2v2(cent, data->relative_drag);
7835
7836 if (len > radius) {
7837 dist_ensure_v2_v2fl(data->relative_drag, cent, radius);
7838 }
7839 }
7840 else {
7841 data->relative_drag.x = mx;
7842 data->relative_drag.y = my;
7843 }
7844
7845#ifdef USE_CONT_MOUSE_CORRECT
7846 if (use_continuous_grab) {
7847 /* OK but can go outside bounds */
7848 data->ungrab_mval[0] = data->relative_drag.x;
7849 data->ungrab_mval[1] = data->relative_drag.y;
7850 }
7851#endif
7852 /* exception, when using color wheel in 'locked' value state:
7853 * allow choosing a hue for black values, by giving a tiny increment */
7854 if (cpicker->use_color_lock) {
7855 if (U.color_picker_type == USER_CP_CIRCLE_HSV) { /* lock */
7856 if (hsv[2] == 0.0f) {
7857 hsv[2] = 0.0001f;
7858 }
7859 }
7860 else {
7861 if (hsv[2] == 0.0f) {
7862 hsv[2] = 0.0001f;
7863 }
7864 hsv[2] = std::min(hsv[2], 0.9999f);
7865 }
7866 }
7867
7868 hsvcircle_vals_from_pos(&rect, data->relative_drag.x, data->relative_drag.y, hsv, hsv + 1);
7869
7870 if ((cpicker->use_color_cubic) && (U.color_picker_type == USER_CP_CIRCLE_HSV)) {
7871 hsv[1] = 1.0f - sqrt3f(1.0f - hsv[1]);
7872 }
7873
7874 if (snap != SNAP_OFF) {
7875 color_snap_hue(snap, &hsv[0]);
7876 }
7877
7878 color_picker_hsv_to_rgb(hsv, rgb);
7879
7880 if (cpicker->use_luminosity_lock) {
7881 if (!is_zero_v3(rgb)) {
7883 }
7884 }
7885
7887 button_v3_set(but, rgb);
7888
7889 data->draglastx = mx;
7890 data->draglasty = my;
7891
7892 return changed;
7893}
7894
7895#ifdef WITH_INPUT_NDOF
7896static void ndofedit_but_HSVCIRCLE(Button *but,
7897 HandleButtonData *data,
7898 const wmNDOFMotionData &ndof,
7899 const eSnapType snap,
7900 const bool shift)
7901{
7902 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
7903 float *hsv = cpicker->hsv_perceptual;
7904 float rgb[3];
7905 float phi, r, v[2];
7906 const float sensitivity = (shift ? 0.06f : 0.3f) * ndof.time_delta;
7907
7908 button_v3_get(but, rgb);
7911
7912 /* Convert current color on hue/sat disc to circular coordinates phi, r */
7913 phi = fmodf(hsv[0] + 0.25f, 1.0f) * -2.0f * float(M_PI);
7914 r = hsv[1];
7915 // const float sqr = r > 0.0f ? sqrtf(r) : 1; /* UNUSED */
7916
7917 /* Convert to 2d vectors */
7918 v[0] = r * cosf(phi);
7919 v[1] = r * sinf(phi);
7920
7921 /* Use ndof device y and x rotation to move the vector in 2d space */
7922 v[0] += ndof.rvec[2] * sensitivity;
7923 v[1] += ndof.rvec[0] * sensitivity;
7924
7925 /* convert back to polar coords on circle */
7926 phi = atan2f(v[0], v[1]) / (2.0f * float(M_PI)) + 0.5f;
7927
7928 /* use ndof Y rotation to additionally rotate hue */
7929 phi += ndof.rvec[1] * sensitivity * 0.5f;
7930 r = len_v2(v);
7931
7932 /* convert back to hsv values, in range [0,1] */
7933 hsv[0] = phi;
7934 hsv[1] = r;
7935
7936 /* exception, when using color wheel in 'locked' value state:
7937 * allow choosing a hue for black values, by giving a tiny increment */
7938 if (cpicker->use_color_lock) {
7939 if (U.color_picker_type == USER_CP_CIRCLE_HSV) { /* lock */
7940 if (hsv[2] == 0.0f) {
7941 hsv[2] = 0.0001f;
7942 }
7943 }
7944 else {
7945 if (hsv[2] == 0.0f) {
7946 hsv[2] = 0.0001f;
7947 }
7948 if (hsv[2] == 1.0f) {
7949 hsv[2] = 0.9999f;
7950 }
7951 }
7952 }
7953
7954 if (snap != SNAP_OFF) {
7955 color_snap_hue(snap, &hsv[0]);
7956 }
7957
7958 hsv_clamp_v(hsv, FLT_MAX);
7959
7960 color_picker_hsv_to_rgb(hsv, data->vec);
7961
7962 if (cpicker->use_luminosity_lock) {
7963 if (!is_zero_v3(data->vec)) {
7964 normalize_v3_length(data->vec, cpicker->luminosity_lock_value);
7965 }
7966 }
7967
7969 button_v3_set(but, data->vec);
7970}
7971#endif /* WITH_INPUT_NDOF */
7972
7974 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
7975{
7976 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
7977 float *hsv = cpicker->hsv_perceptual;
7978 int mx = event->xy[0];
7979 int my = event->xy[1];
7980 window_to_block(data->region, block, &mx, &my);
7981
7982 if (data->state == BUTTON_STATE_HIGHLIGHT) {
7983 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
7984 const eSnapType snap = event_to_snap(event);
7985 data->dragstartx = mx;
7986 data->dragstarty = my;
7987 data->draglastx = mx;
7988 data->draglasty = my;
7990 const bool shift = event->modifier & KM_SHIFT;
7991 const bool use_continuous_grab = button_is_cursor_warp(but) &&
7992 event->tablet.active == EVT_TABLET_NONE;
7993 /* also do drag the first time */
7994 if (numedit_but_HSVCIRCLE(but, data, mx, my, snap, shift, use_continuous_grab, true)) {
7995 numedit_apply(C, block, but, data);
7996 }
7997
7998 return WM_UI_HANDLER_BREAK;
7999 }
8000#ifdef WITH_INPUT_NDOF
8001 if (event->type == NDOF_MOTION) {
8002 const eSnapType snap = event_to_snap(event);
8003 const wmNDOFMotionData &ndof = *static_cast<const wmNDOFMotionData *>(event->customdata);
8004
8005 ndofedit_but_HSVCIRCLE(but, data, ndof, snap, event->modifier & KM_SHIFT);
8006
8008 apply_but(C, but->block, but, data, true);
8009
8010 return WM_UI_HANDLER_BREAK;
8011 }
8012#endif /* WITH_INPUT_NDOF */
8013 /* XXX hardcoded keymap check.... */
8014 if (event->type == EVT_BACKSPACEKEY && event->val == KM_PRESS) {
8015 int len;
8016
8017 /* reset only saturation */
8018
8020 if (len >= 3) {
8021 float rgb[3], def_hsv[3];
8022 float *def = MEM_new_array_zeroed<float>(len, __func__);
8023
8025 color_picker_hsv_to_rgb(def, def_hsv);
8026
8027 button_v3_get(but, rgb);
8029
8030 def_hsv[0] = hsv[0];
8031 def_hsv[2] = hsv[2];
8032
8033 hsv_to_rgb_v(def_hsv, rgb);
8034 button_v3_set(but, rgb);
8035 apply_but_func(C, but);
8036
8037 RNA_property_update(C, &but->rnapoin, but->rnaprop);
8038
8039 MEM_delete(def);
8040 }
8041 return WM_UI_HANDLER_BREAK;
8042 }
8043 }
8044 else if (data->state == BUTTON_STATE_NUM_EDITING) {
8045 if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
8046 if (event->val == KM_PRESS) {
8047 data->cancel = true;
8048 data->escapecancel = true;
8050 }
8051 }
8052 /* XXX hardcoded keymap check.... */
8053 else if (event->type == WHEELDOWNMOUSE) {
8054 hsv[2] = clamp_f(hsv[2] - 0.05f, 0.0f, 1.0f);
8055 button_hsv_set(but); /* converts to rgb */
8056 numedit_apply(C, block, but, data);
8057 }
8058 else if (event->type == WHEELUPMOUSE) {
8059 hsv[2] = clamp_f(hsv[2] + 0.05f, 0.0f, 1.0f);
8060 button_hsv_set(but); /* converts to rgb */
8061 numedit_apply(C, block, but, data);
8062 }
8063 else if ((event->type == MOUSEMOVE) || event_is_snap(event)) {
8064 if (mx != data->draglastx || my != data->draglasty || event->type != MOUSEMOVE) {
8065 const eSnapType snap = event_to_snap(event);
8066 const bool shift = event->modifier & KM_SHIFT;
8067 const bool use_continuous_grab = button_is_cursor_warp(but) &&
8068 event->tablet.active == EVT_TABLET_NONE;
8069 if (numedit_but_HSVCIRCLE(but, data, mx, my, snap, shift, use_continuous_grab)) {
8070 numedit_apply(C, block, but, data);
8071 }
8072 }
8073 }
8074 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
8076 }
8077 return WM_UI_HANDLER_BREAK;
8078 }
8079
8081}
8082
8084{
8085 bool changed = false;
8086
8087 if (data->draglastx == mx) {
8088 return changed;
8089 }
8090
8091 if (data->coba->tot == 0) {
8092 return changed;
8093 }
8094
8095 const float dx = float(mx - data->draglastx) / BLI_rctf_size_x(&but->rect);
8096 data->dragcbd->pos += dx;
8097 CLAMP(data->dragcbd->pos, 0.0f, 1.0f);
8098
8100 data->dragcbd = data->coba->data + data->coba->cur; /* because qsort */
8101
8102 data->draglastx = mx;
8103 changed = true;
8104
8105 return changed;
8106}
8107
8109 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
8110{
8111 int mx = event->xy[0];
8112 int my = event->xy[1];
8113 window_to_block(data->region, block, &mx, &my);
8114
8115 if (data->state == BUTTON_STATE_HIGHLIGHT) {
8116 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
8117 ColorBand *coba = reinterpret_cast<ColorBand *>(but->poin);
8118
8119 if (event->modifier & KM_CTRL) {
8120 /* insert new key on mouse location */
8121 const float pos = float(mx - but->rect.xmin) / BLI_rctf_size_x(&but->rect);
8122 BKE_colorband_element_add(coba, pos);
8124 }
8125 else {
8126 CBData *cbd;
8127 /* ignore zoom-level for mindist */
8128 int mindist = (50 * UI_SCALE_FAC) * block->aspect;
8129 int xco;
8130 data->dragstartx = mx;
8131 data->dragstarty = my;
8132 data->draglastx = mx;
8133 data->draglasty = my;
8134
8135 /* activate new key when mouse is close */
8136 int a;
8137 for (a = 0, cbd = coba->data; a < coba->tot; a++, cbd++) {
8138 xco = but->rect.xmin + (cbd->pos * BLI_rctf_size_x(&but->rect));
8139 xco = abs(xco - mx);
8140 if (a == coba->cur) {
8141 /* Selected one disadvantage. */
8142 xco += 5;
8143 }
8144 if (xco < mindist) {
8145 coba->cur = a;
8146 mindist = xco;
8147 }
8148 }
8149
8150 data->dragcbd = coba->data + coba->cur;
8151 data->dragfstart = data->dragcbd->pos;
8153 }
8154
8155 return WM_UI_HANDLER_BREAK;
8156 }
8157 }
8158 else if (data->state == BUTTON_STATE_NUM_EDITING) {
8159 if (event->type == MOUSEMOVE) {
8160 if (mx != data->draglastx || my != data->draglasty) {
8161 if (numedit_but_COLORBAND(but, data, mx)) {
8162 numedit_apply(C, block, but, data);
8163 }
8164 }
8165 }
8166 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
8168 }
8169 else if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
8170 if (event->val == KM_PRESS) {
8171 data->dragcbd->pos = data->dragfstart;
8173 data->cancel = true;
8174 data->escapecancel = true;
8176 }
8177 }
8178 return WM_UI_HANDLER_BREAK;
8179 }
8180
8182}
8183
8184static bool numedit_but_CURVE(Block *block,
8185 Button *but,
8187 int evtx,
8188 int evty,
8189 bool snap,
8190 const bool shift)
8191{
8192 CurveMapping *cumap = reinterpret_cast<CurveMapping *>(but->poin);
8193 CurveMap *cuma = cumap->cm + cumap->cur;
8194 CurveMapPoint *cmp = cuma->curve;
8195 bool changed = false;
8196
8197 /* evtx evty and drag coords are absolute mouse-coords,
8198 * prevents errors when editing when layout changes. */
8199 int mx = evtx;
8200 int my = evty;
8201 window_to_block(data->region, block, &mx, &my);
8202 int dragx = data->draglastx;
8203 int dragy = data->draglasty;
8204 window_to_block(data->region, block, &dragx, &dragy);
8205
8206 const float zoomx = BLI_rctf_size_x(&but->rect) / BLI_rctf_size_x(&cumap->curr);
8207 const float zoomy = BLI_rctf_size_y(&but->rect) / BLI_rctf_size_y(&cumap->curr);
8208
8209 if (snap) {
8210 float d[2];
8211
8212 d[0] = mx - data->dragstartx;
8213 d[1] = my - data->dragstarty;
8214
8215 if (len_squared_v2(d) < (3.0f * 3.0f)) {
8216 snap = false;
8217 }
8218 }
8219
8220 float fx = (mx - dragx) / zoomx;
8221 float fy = (my - dragy) / zoomy;
8222
8223 if (data->dragsel != -1) {
8224 CurveMapPoint *cmp_last = nullptr;
8225 const float mval_factor = mouse_scale_warp_factor(shift);
8226 bool moved_point = false; /* for ctrl grid, can't use orig coords because of sorting */
8227
8228 fx *= mval_factor;
8229 fy *= mval_factor;
8230
8231 for (int a = 0; a < cuma->totpoint; a++) {
8232 if (cmp[a].flag & CUMA_SELECT) {
8233 const float origx = cmp[a].x, origy = cmp[a].y;
8234 cmp[a].x += fx;
8235 cmp[a].y += fy;
8236 if (snap) {
8237 cmp[a].x = 0.125f * roundf(8.0f * cmp[a].x);
8238 cmp[a].y = 0.125f * roundf(8.0f * cmp[a].y);
8239 }
8240 if (cmp[a].x != origx || cmp[a].y != origy) {
8241 moved_point = true;
8242 }
8243
8244 cmp_last = &cmp[a];
8245 }
8246 }
8247
8248 BKE_curvemapping_changed(cumap, false);
8249
8250 if (moved_point) {
8251 data->draglastx = evtx;
8252 data->draglasty = evty;
8253 changed = true;
8254
8255#ifdef USE_CONT_MOUSE_CORRECT
8256 /* NOTE: using 'cmp_last' is weak since there may be multiple points selected,
8257 * but in practice this isn't really an issue */
8258 if (button_is_cursor_warp(but)) {
8259 /* OK but can go outside bounds */
8260 data->ungrab_mval[0] = but->rect.xmin + ((cmp_last->x - cumap->curr.xmin) * zoomx);
8261 data->ungrab_mval[1] = but->rect.ymin + ((cmp_last->y - cumap->curr.ymin) * zoomy);
8262 BLI_rctf_clamp_pt_v(&but->rect, data->ungrab_mval);
8263 }
8264#endif
8265 }
8266
8267 data->dragchange = true; /* mark for selection */
8268 }
8269 else {
8270 /* clamp for clip */
8271 if (cumap->flag & CUMA_DO_CLIP) {
8272 if (cumap->curr.xmin - fx < cumap->clipr.xmin) {
8273 fx = cumap->curr.xmin - cumap->clipr.xmin;
8274 }
8275 else if (cumap->curr.xmax - fx > cumap->clipr.xmax) {
8276 fx = cumap->curr.xmax - cumap->clipr.xmax;
8277 }
8278 if (cumap->curr.ymin - fy < cumap->clipr.ymin) {
8279 fy = cumap->curr.ymin - cumap->clipr.ymin;
8280 }
8281 else if (cumap->curr.ymax - fy > cumap->clipr.ymax) {
8282 fy = cumap->curr.ymax - cumap->clipr.ymax;
8283 }
8284 }
8285
8286 cumap->curr.xmin -= fx;
8287 cumap->curr.ymin -= fy;
8288 cumap->curr.xmax -= fx;
8289 cumap->curr.ymax -= fy;
8290
8291 data->draglastx = evtx;
8292 data->draglasty = evty;
8293
8294 changed = true;
8295 }
8296
8297 return changed;
8298}
8299
8300static int do_but_CURVE(
8301 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
8302{
8303 bool changed = false;
8304 Scene *scene = CTX_data_scene(C);
8305
8306 int mx = event->xy[0];
8307 int my = event->xy[1];
8308 window_to_block(data->region, block, &mx, &my);
8309
8310 if (data->state == BUTTON_STATE_HIGHLIGHT) {
8311 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
8312 CurveMapping *cumap = reinterpret_cast<CurveMapping *>(but->poin);
8313 CurveMap *cuma = cumap->cm + cumap->cur;
8314 const float m_xy[2] = {float(mx), float(my)};
8315 float dist_min_sq = square_f(UI_SCALE_FAC * 14.0f); /* 14 pixels radius */
8316 int sel = -1;
8317
8318 if (event->modifier & KM_CTRL) {
8319 float f_xy[2];
8320 BLI_rctf_transform_pt_v(&cumap->curr, &but->rect, f_xy, m_xy);
8321
8322 BKE_curvemap_insert(cuma, f_xy[0], f_xy[1]);
8323 BKE_curvemapping_changed(cumap, false);
8324 changed = true;
8325 }
8326
8327 /* check for selecting of a point */
8328 CurveMapPoint *cmp = cuma->curve; /* ctrl adds point, new malloc */
8329 for (int a = 0; a < cuma->totpoint; a++) {
8330 float f_xy[2];
8331 BLI_rctf_transform_pt_v(&but->rect, &cumap->curr, f_xy, &cmp[a].x);
8332 const float dist_sq = len_squared_v2v2(m_xy, f_xy);
8333 if (dist_sq < dist_min_sq) {
8334 sel = a;
8335 dist_min_sq = dist_sq;
8336 }
8337 }
8338
8339 if (sel == -1) {
8340 float f_xy[2], f_xy_prev[2];
8341
8342 /* if the click didn't select anything, check if it's clicked on the
8343 * curve itself, and if so, add a point */
8344 cmp = cuma->table;
8345
8346 BLI_rctf_transform_pt_v(&but->rect, &cumap->curr, f_xy, &cmp[0].x);
8347
8348 /* with 160px height 8px should translate to the old 0.05 coefficient at no zoom */
8349 dist_min_sq = square_f(UI_SCALE_FAC * 8.0f);
8350
8351 /* loop through the curve segment table and find what's near the mouse. */
8352 for (int i = 1; i <= CM_TABLE; i++) {
8353 copy_v2_v2(f_xy_prev, f_xy);
8354 BLI_rctf_transform_pt_v(&but->rect, &cumap->curr, f_xy, &cmp[i].x);
8355
8356 if (dist_squared_to_line_segment_v2(m_xy, f_xy_prev, f_xy) < dist_min_sq) {
8357 BLI_rctf_transform_pt_v(&cumap->curr, &but->rect, f_xy, m_xy);
8358
8359 CurveMapPoint *new_pt = BKE_curvemap_insert(cuma, f_xy[0], f_xy[1]);
8360 new_pt->flag &= ~CUMA_SELECT; /* deselect new point for now */
8361 BKE_curvemapping_changed(cumap, false);
8362
8363 changed = true;
8364
8365 /* reset cmp back to the curve points again,
8366 * rather than drawing segments */
8367 cmp = cuma->curve;
8368
8369 /* find newly added point and make it 'sel' */
8370 for (int a = 0; a < cuma->totpoint; a++) {
8371 if (cmp[a].x == f_xy[0]) {
8372 sel = a;
8373 }
8374 }
8375 break;
8376 }
8377 }
8378 }
8379
8380 cmp = cuma->curve;
8381 if (sel != -1) {
8382 /* ok, we move a point */
8383 /* deselect all if this one is deselect. except if we hold shift */
8384 if (event->modifier & KM_SHIFT) { /* If holding shift. */
8385 if (cmp[sel].flag & CUMA_SELECT) { /* if the current point is selected. */
8386 if (cmp[sel].flag & CUMA_ACTIVE) {
8388 }
8389 cmp[sel].flag &= ~(CUMA_SELECT | CUMA_ACTIVE);
8390 }
8391 else {
8392 for (int a = 0; a < cuma->totpoint; a++) {
8393 cmp[a].flag &= ~CUMA_ACTIVE;
8394 }
8395 cmp[sel].flag |= (CUMA_SELECT | CUMA_ACTIVE);
8396 }
8397 }
8398 else { /* If not holding shift. */
8399 for (int a = 0; a < cuma->totpoint; a++) {
8400 cmp[a].flag &= ~(CUMA_SELECT | CUMA_ACTIVE);
8401 }
8402 cmp[sel].flag |= (CUMA_SELECT | CUMA_ACTIVE);
8403 }
8404 }
8405 else {
8406 /* move the view */
8407 data->cancel = true;
8408 }
8409
8410 data->dragsel = sel;
8411
8412 data->dragstartx = event->xy[0];
8413 data->dragstarty = event->xy[1];
8414 data->draglastx = event->xy[0];
8415 data->draglasty = event->xy[1];
8416
8418 return WM_UI_HANDLER_BREAK;
8419 }
8420 }
8421 else if (data->state == BUTTON_STATE_NUM_EDITING) {
8422 if (event->type == MOUSEMOVE) {
8423 if (event->xy[0] != data->draglastx || event->xy[1] != data->draglasty) {
8424
8425 if (numedit_but_CURVE(block,
8426 but,
8427 data,
8428 event->xy[0],
8429 event->xy[1],
8430 event->modifier & KM_CTRL,
8431 event->modifier & KM_SHIFT))
8432 {
8433 numedit_apply(C, block, but, data);
8434 }
8435 }
8436 }
8437 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
8438 if (data->dragsel != -1) {
8439 CurveMapping *cumap = reinterpret_cast<CurveMapping *>(but->poin);
8440 CurveMap *cuma = cumap->cm + cumap->cur;
8441 CurveMapPoint *cmp = cuma->curve;
8442
8443 if (data->dragchange == false) {
8444 /* deselect all, select one */
8445 if ((event->modifier & KM_SHIFT) == 0) {
8446 for (int a = 0; a < cuma->totpoint; a++) {
8447 cmp[a].flag &= ~(CUMA_SELECT | CUMA_ACTIVE);
8448 }
8449 cmp[data->dragsel].flag |= (CUMA_SELECT | CUMA_ACTIVE);
8450 }
8451 }
8452 else {
8453 BKE_curvemapping_changed(cumap, true); /* remove doubles */
8455 }
8456 }
8457
8459 }
8460
8461 return WM_UI_HANDLER_BREAK;
8462 }
8463
8464 /* UNUSED but keep for now */
8465 (void)changed;
8466
8468}
8469
8470/* Same as numedit_but_CURVE with some smaller changes. */
8472 Button *but,
8474 int evtx,
8475 int evty,
8476 bool snap,
8477 const bool shift)
8478{
8479 CurveProfile *profile = reinterpret_cast<CurveProfile *>(but->poin);
8480 CurveProfilePoint *pts = profile->path;
8481 bool changed = false;
8482
8483 /* evtx evty and drag coords are absolute mouse-coords,
8484 * prevents errors when editing when layout changes. */
8485 int mx = evtx;
8486 int my = evty;
8487 window_to_block(data->region, block, &mx, &my);
8488 int dragx = data->draglastx;
8489 int dragy = data->draglasty;
8490 window_to_block(data->region, block, &dragx, &dragy);
8491
8492 const float zoomx = BLI_rctf_size_x(&but->rect) / BLI_rctf_size_x(&profile->view_rect);
8493 const float zoomy = BLI_rctf_size_y(&but->rect) / BLI_rctf_size_y(&profile->view_rect);
8494
8495 if (snap) {
8496 const float d[2] = {float(mx - data->dragstartx), float(data->dragstarty)};
8497 if (len_squared_v2(d) < (9.0f * UI_SCALE_FAC)) {
8498 snap = false;
8499 }
8500 }
8501
8502 float fx = (mx - dragx) / zoomx;
8503 float fy = (my - dragy) / zoomy;
8504
8505 if (data->dragsel != -1) {
8506 float last_x, last_y;
8507 const float mval_factor = mouse_scale_warp_factor(shift);
8508 bool moved_point = false; /* for ctrl grid, can't use orig coords because of sorting */
8509
8510 fx *= mval_factor;
8511 fy *= mval_factor;
8512
8513 /* Move all selected points. */
8514 const float delta[2] = {fx, fy};
8515 for (int a = 0; a < profile->path_len; a++) {
8516 /* Don't move the last and first control points. */
8517 if (pts[a].flag & PROF_SELECT) {
8518 moved_point |= BKE_curveprofile_move_point(profile, &pts[a], snap, delta);
8519 last_x = pts[a].x;
8520 last_y = pts[a].y;
8521 }
8522 else {
8523 /* Move handles when they're selected but the control point isn't. */
8524 if (ELEM(pts[a].h2, HD_FREE, HD_ALIGN) && (pts[a].flag & PROF_H1_SELECT)) {
8525 moved_point |= BKE_curveprofile_move_handle(&pts[a], true, snap, delta);
8526 last_x = pts[a].h1_loc[0];
8527 last_y = pts[a].h1_loc[1];
8528 }
8529 if (ELEM(pts[a].h2, HD_FREE, HD_ALIGN) && (pts[a].flag & PROF_H2_SELECT)) {
8530 moved_point |= BKE_curveprofile_move_handle(&pts[a], false, snap, delta);
8531 last_x = pts[a].h2_loc[0];
8532 last_y = pts[a].h2_loc[1];
8533 }
8534 }
8535 }
8536
8538
8539 if (moved_point) {
8540 data->draglastx = evtx;
8541 data->draglasty = evty;
8542 changed = true;
8543#ifdef USE_CONT_MOUSE_CORRECT
8544 /* NOTE: using 'cmp_last' is weak since there may be multiple points selected,
8545 * but in practice this isn't really an issue */
8546 if (button_is_cursor_warp(but)) {
8547 /* OK but can go outside bounds */
8548 data->ungrab_mval[0] = but->rect.xmin + ((last_x - profile->view_rect.xmin) * zoomx);
8549 data->ungrab_mval[1] = but->rect.ymin + ((last_y - profile->view_rect.ymin) * zoomy);
8550 BLI_rctf_clamp_pt_v(&but->rect, data->ungrab_mval);
8551 }
8552#endif
8553 }
8554 data->dragchange = true; /* mark for selection */
8555 }
8556 else {
8557 /* Clamp the view rect when clipping is on. */
8558 if (profile->flag & PROF_USE_CLIP) {
8559 if (profile->view_rect.xmin - fx < profile->clip_rect.xmin) {
8560 fx = profile->view_rect.xmin - profile->clip_rect.xmin;
8561 }
8562 else if (profile->view_rect.xmax - fx > profile->clip_rect.xmax) {
8563 fx = profile->view_rect.xmax - profile->clip_rect.xmax;
8564 }
8565 if (profile->view_rect.ymin - fy < profile->clip_rect.ymin) {
8566 fy = profile->view_rect.ymin - profile->clip_rect.ymin;
8567 }
8568 else if (profile->view_rect.ymax - fy > profile->clip_rect.ymax) {
8569 fy = profile->view_rect.ymax - profile->clip_rect.ymax;
8570 }
8571 }
8572
8573 profile->view_rect.xmin -= fx;
8574 profile->view_rect.ymin -= fy;
8575 profile->view_rect.xmax -= fx;
8576 profile->view_rect.ymax -= fy;
8577
8578 data->draglastx = evtx;
8579 data->draglasty = evty;
8580
8581 changed = true;
8582 }
8583
8584 return changed;
8585}
8586
8591{
8592 return (point->flag & PROF_SELECT &&
8593 (ELEM(point->h1, HD_FREE, HD_ALIGN) || ELEM(point->h2, HD_FREE, HD_ALIGN))) ||
8594 point->flag & PROF_H1_SELECT || point->flag & PROF_H2_SELECT;
8595}
8596
8598{
8599 /* Active flags are the select flags multiplied by #PROF_ACTIVE.
8600 * Static asserts ensure this relationship holds if flag values change. */
8601 static_assert(PROF_ACTIVE == PROF_SELECT * 8);
8602 static_assert(PROF_H1_ACTIVE == PROF_H1_SELECT * 8);
8603 static_assert(PROF_H2_ACTIVE == PROF_H2_SELECT * 8);
8604 return eCurveProfilePoint_Flag(int(selection_type) * int(PROF_ACTIVE));
8605}
8606
8612 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
8613{
8614 CurveProfile *profile = reinterpret_cast<CurveProfile *>(but->poin);
8615 int mx = event->xy[0];
8616 int my = event->xy[1];
8617
8618 window_to_block(data->region, block, &mx, &my);
8619
8620 /* Move selected control points. */
8621 if (event->type == EVT_GKEY && event->val == KM_RELEASE) {
8622 data->dragstartx = mx;
8623 data->dragstarty = my;
8624 data->draglastx = mx;
8625 data->draglasty = my;
8627 return WM_UI_HANDLER_BREAK;
8628 }
8629
8630 /* Delete selected control points. */
8631 if (event->type == EVT_XKEY && event->val == KM_RELEASE) {
8635 return WM_UI_HANDLER_BREAK;
8636 }
8637
8638 /* Selecting, adding, and starting point movements. */
8639 if (data->state == BUTTON_STATE_HIGHLIGHT) {
8640 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
8641 const float m_xy[2] = {float(mx), float(my)};
8642
8643 if (event->modifier & KM_CTRL) {
8644 float f_xy[2];
8645 BLI_rctf_transform_pt_v(&profile->view_rect, &but->rect, f_xy, m_xy);
8646
8647 BKE_curveprofile_insert(profile, f_xy[0], f_xy[1]);
8649 }
8650
8651 /* Check for selecting of a point by finding closest point in radius. */
8652 CurveProfilePoint *pts = profile->path;
8653 /* 14 pixels radius for selecting points. */
8654 float dist_min_sq = square_f(UI_SCALE_FAC * 14.0f);
8655 int i_selected = -1;
8656 eCurveProfilePoint_Flag selection_type = {}; /* For handle selection. */
8657 for (int i = 0; i < profile->path_len; i++) {
8658 float f_xy[2];
8659 BLI_rctf_transform_pt_v(&but->rect, &profile->view_rect, f_xy, &pts[i].x);
8660 float dist_sq = len_squared_v2v2(m_xy, f_xy);
8661 if (dist_sq < dist_min_sq) {
8662 i_selected = i;
8663 selection_type = PROF_SELECT;
8664 dist_min_sq = dist_sq;
8665 }
8666
8667 /* Also select handles if the point is selected and it has the right handle type. */
8668 if (point_draw_handles(&pts[i])) {
8669 if (ELEM(profile->path[i].h1, HD_FREE, HD_ALIGN)) {
8670 BLI_rctf_transform_pt_v(&but->rect, &profile->view_rect, f_xy, pts[i].h1_loc);
8671 dist_sq = len_squared_v2v2(m_xy, f_xy);
8672 if (dist_sq < dist_min_sq) {
8673 i_selected = i;
8674 selection_type = PROF_H1_SELECT;
8675 dist_min_sq = dist_sq;
8676 }
8677 }
8678 if (ELEM(profile->path[i].h2, HD_FREE, HD_ALIGN)) {
8679 BLI_rctf_transform_pt_v(&but->rect, &profile->view_rect, f_xy, pts[i].h2_loc);
8680 dist_sq = len_squared_v2v2(m_xy, f_xy);
8681 if (dist_sq < dist_min_sq) {
8682 i_selected = i;
8683 selection_type = PROF_H2_SELECT;
8684 dist_min_sq = dist_sq;
8685 }
8686 }
8687 }
8688 }
8689
8690 /* Add a point if the click was close to the path but not a control point or handle. */
8691 if (i_selected == -1) {
8692 float f_xy[2], f_xy_prev[2];
8693 CurveProfilePoint *table = profile->table;
8694 BLI_rctf_transform_pt_v(&but->rect, &profile->view_rect, f_xy, &table[0].x);
8695
8696 dist_min_sq = square_f(UI_SCALE_FAC * 8.0f); /* 8 pixel radius from each table point. */
8697
8698 /* Loop through the path's high resolution table and find what's near the click. */
8699 for (int i = 1; i <= BKE_curveprofile_table_size(profile); i++) {
8700 copy_v2_v2(f_xy_prev, f_xy);
8701 BLI_rctf_transform_pt_v(&but->rect, &profile->view_rect, f_xy, &table[i].x);
8702
8703 if (dist_squared_to_line_segment_v2(m_xy, f_xy_prev, f_xy) < dist_min_sq) {
8704 BLI_rctf_transform_pt_v(&profile->view_rect, &but->rect, f_xy, m_xy);
8705
8706 CurveProfilePoint *new_pt = BKE_curveprofile_insert(profile, f_xy[0], f_xy[1]);
8707 new_pt->flag &= ~PROF_SELECT; /* Deselect new point for now. */
8709
8710 /* Get the index of the newly added point. */
8711 i_selected = int(new_pt - profile->path);
8712 BLI_assert(i_selected >= 0 && i_selected <= profile->path_len);
8713 selection_type = PROF_SELECT;
8714 break;
8715 }
8716 }
8717 }
8718
8719 /* Change the flag for the point(s) if one was selected or added. */
8720 /* Offset the selection type to get the active type. */
8721 const eCurveProfilePoint_Flag active_type = profile_select_to_active(selection_type);
8722 pts = profile->path;
8723 if (i_selected != -1) {
8724 /* Deselect all if this one is deselected, except if we hold shift. */
8725 if (event->modifier & KM_SHIFT) {
8726 if (pts[i_selected].flag & selection_type) {
8727 /* If the current point or handle is selected. */
8728 pts[i_selected].flag ^= selection_type;
8729
8730 if (pts[i_selected].flag & active_type) {
8731 /* If the current point or handle is active. */
8732 pts[i_selected].flag &= ~(PROF_ACTIVE | PROF_H1_ACTIVE | PROF_H2_ACTIVE);
8733 if (pts[i_selected].flag & PROF_SELECT) {
8734 pts[i_selected].flag |= PROF_ACTIVE;
8735 }
8736 else if (pts[i_selected].flag & PROF_H1_SELECT) {
8737 pts[i_selected].flag |= PROF_H1_ACTIVE;
8738 }
8739 else if (pts[i_selected].flag & PROF_H2_SELECT) {
8740 pts[i_selected].flag |= PROF_H2_ACTIVE;
8741 }
8742 else {
8743 /* If the current point including its handles are d, activate the nearest
8744 * point. */
8745 BKE_curveprofile_activate_nearest_point(profile, i_selected);
8746 }
8747 }
8748 }
8749 else {
8750 for (int a = 0; a < profile->path_len; a++) {
8752 }
8753 pts[i_selected].flag |= (selection_type | active_type);
8754 }
8755 }
8756 else {
8757 for (int i = 0; i < profile->path_len; i++) {
8758 // pts[i].flag &= ~(PROF_SELECT | PROF_H1_SELECT | PROF_H2_SELECT);
8759 profile->path[i].flag &= ~(PROF_SELECT | PROF_H1_SELECT | PROF_H2_SELECT |
8761 }
8762 profile->path[i_selected].flag |= (selection_type | active_type);
8763 }
8764 }
8765 else {
8766 /* Move the view. */
8767 data->cancel = true;
8768 }
8769
8770 data->dragsel = i_selected;
8771
8772 data->dragstartx = mx;
8773 data->dragstarty = my;
8774 data->draglastx = mx;
8775 data->draglasty = my;
8776
8778 return WM_UI_HANDLER_BREAK;
8779 }
8780 }
8781 else if (data->state == BUTTON_STATE_NUM_EDITING) { /* Do control point movement. */
8782 if (event->type == MOUSEMOVE) {
8783 if (mx != data->draglastx || my != data->draglasty) {
8785 block, but, data, mx, my, event->modifier & KM_CTRL, event->modifier & KM_SHIFT))
8786 {
8787 numedit_apply(C, block, but, data);
8788 }
8789 }
8790 }
8791 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
8792 /* Finish move. */
8793 if (data->dragsel != -1) {
8794
8795 if (data->dragchange == false) {
8796 /* Deselect all, select one. */
8797 }
8798 else {
8799 /* Remove doubles, clip after move. */
8801 }
8802 }
8804 }
8805 return WM_UI_HANDLER_BREAK;
8806 }
8807
8809}
8810
8811static bool numedit_but_HISTOGRAM(Button *but, HandleButtonData *data, int mx, int my)
8812{
8813 Histogram *hist = reinterpret_cast<Histogram *>(but->poin);
8814 const bool changed = true;
8815 const float dy = my - data->draglasty;
8816
8817 /* scale histogram values (dy / 10 for better control) */
8818 const float yfac = min_ff(pow2f(hist->ymax), 1.0f) * 0.5f;
8819 hist->ymax += (dy * 0.1f) * yfac;
8820
8821 /* 0.1 allows us to see HDR colors up to 10 */
8822 CLAMP(hist->ymax, 0.1f, 100.0f);
8823
8824 data->draglastx = mx;
8825 data->draglasty = my;
8826
8827 return changed;
8828}
8829
8831 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
8832{
8833 int mx = event->xy[0];
8834 int my = event->xy[1];
8835 window_to_block(data->region, block, &mx, &my);
8836
8837 if (data->state == BUTTON_STATE_HIGHLIGHT) {
8838 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
8839 data->dragstartx = mx;
8840 data->dragstarty = my;
8841 data->draglastx = mx;
8842 data->draglasty = my;
8844
8845 /* also do drag the first time */
8846 if (numedit_but_HISTOGRAM(but, data, mx, my)) {
8847 numedit_apply(C, block, but, data);
8848 }
8849
8850 return WM_UI_HANDLER_BREAK;
8851 }
8852 /* XXX hardcoded keymap check.... */
8853 if (event->type == EVT_BACKSPACEKEY && event->val == KM_PRESS) {
8854 Histogram *hist = reinterpret_cast<Histogram *>(but->poin);
8855 hist->ymax = 1.0f;
8856
8858 return WM_UI_HANDLER_BREAK;
8859 }
8860 }
8861 else if (data->state == BUTTON_STATE_NUM_EDITING) {
8862 if (event->type == EVT_ESCKEY) {
8863 if (event->val == KM_PRESS) {
8864 data->cancel = true;
8865 data->escapecancel = true;
8867 }
8868 }
8869 else if (event->type == MOUSEMOVE) {
8870 if (mx != data->draglastx || my != data->draglasty) {
8871 if (numedit_but_HISTOGRAM(but, data, mx, my)) {
8872 numedit_apply(C, block, but, data);
8873 }
8874 }
8875 }
8876 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
8878 }
8879 return WM_UI_HANDLER_BREAK;
8880 }
8881
8883}
8884
8885static bool numedit_but_WAVEFORM(Button *but, HandleButtonData *data, int mx, int my)
8886{
8887 Scopes *scopes = reinterpret_cast<Scopes *>(but->poin);
8888 const bool changed = true;
8889
8890 const float dy = my - data->draglasty;
8891
8892 /* scale waveform values */
8893 scopes->wavefrm_yfac += dy / 200.0f;
8894
8895 CLAMP(scopes->wavefrm_yfac, 0.5f, 2.0f);
8896
8897 data->draglastx = mx;
8898 data->draglasty = my;
8899
8900 return changed;
8901}
8902
8904 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
8905{
8906 int mx = event->xy[0];
8907 int my = event->xy[1];
8908 window_to_block(data->region, block, &mx, &my);
8909
8910 if (data->state == BUTTON_STATE_HIGHLIGHT) {
8911 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
8912 data->dragstartx = mx;
8913 data->dragstarty = my;
8914 data->draglastx = mx;
8915 data->draglasty = my;
8917
8918 /* also do drag the first time */
8919 if (numedit_but_WAVEFORM(but, data, mx, my)) {
8920 numedit_apply(C, block, but, data);
8921 }
8922
8923 return WM_UI_HANDLER_BREAK;
8924 }
8925 /* XXX hardcoded keymap check.... */
8926 if (event->type == EVT_BACKSPACEKEY && event->val == KM_PRESS) {
8927 Scopes *scopes = reinterpret_cast<Scopes *>(but->poin);
8928 scopes->wavefrm_yfac = 1.0f;
8929
8931 return WM_UI_HANDLER_BREAK;
8932 }
8933 }
8934 else if (data->state == BUTTON_STATE_NUM_EDITING) {
8935 if (event->type == EVT_ESCKEY) {
8936 if (event->val == KM_PRESS) {
8937 data->cancel = true;
8938 data->escapecancel = true;
8940 }
8941 }
8942 else if (event->type == MOUSEMOVE) {
8943 if (mx != data->draglastx || my != data->draglasty) {
8944 if (numedit_but_WAVEFORM(but, data, mx, my)) {
8945 numedit_apply(C, block, but, data);
8946 }
8947 }
8948 }
8949 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
8951 }
8952 return WM_UI_HANDLER_BREAK;
8953 }
8954
8956}
8957
8959 bContext *C, Button *but, HandleButtonData *data, int mx, int my, const bool shift)
8960{
8961 MovieClipScopes *scopes = reinterpret_cast<MovieClipScopes *>(but->poin);
8962 const bool changed = true;
8963
8964 float dx = mx - data->draglastx;
8965 float dy = my - data->draglasty;
8966
8967 if (shift) {
8968 dx /= 5.0f;
8969 dy /= 5.0f;
8970 }
8971
8972 if (!scopes->track_locked) {
8973 const MovieClip *clip = CTX_data_edit_movieclip(C);
8974 const int clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, scopes->scene_framenr);
8975 if (scopes->marker->framenr != clip_framenr) {
8976 scopes->marker = BKE_tracking_marker_ensure(scopes->track, clip_framenr);
8977 }
8978
8979 scopes->marker->flag &= ~(MARKER_DISABLED | MARKER_TRACKED);
8980 scopes->marker->pos[0] += -dx * scopes->slide_scale[0] / BLI_rctf_size_x(&but->block->rect);
8981 scopes->marker->pos[1] += -dy * scopes->slide_scale[1] / BLI_rctf_size_y(&but->block->rect);
8982
8984 }
8985
8986 scopes->ok = 0;
8987
8988 data->draglastx = mx;
8989 data->draglasty = my;
8990
8991 return changed;
8992}
8993
8995 bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
8996{
8997 int mx = event->xy[0];
8998 int my = event->xy[1];
8999 window_to_block(data->region, block, &mx, &my);
9000
9001 if (data->state == BUTTON_STATE_HIGHLIGHT) {
9002 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
9003 data->dragstartx = mx;
9004 data->dragstarty = my;
9005 data->draglastx = mx;
9006 data->draglasty = my;
9008
9009 /* also do drag the first time */
9010 if (numedit_but_TRACKPREVIEW(C, but, data, mx, my, event->modifier & KM_SHIFT)) {
9011 numedit_apply(C, block, but, data);
9012 }
9013
9014 return WM_UI_HANDLER_BREAK;
9015 }
9016 }
9017 else if (data->state == BUTTON_STATE_NUM_EDITING) {
9018 if (event->type == EVT_ESCKEY) {
9019 if (event->val == KM_PRESS) {
9020 data->cancel = true;
9021 data->escapecancel = true;
9023 }
9024 }
9025 else if (event->type == MOUSEMOVE) {
9026 if (mx != data->draglastx || my != data->draglasty) {
9027 if (numedit_but_TRACKPREVIEW(C, but, data, mx, my, event->modifier & KM_SHIFT)) {
9028 numedit_apply(C, block, but, data);
9029 }
9030 }
9031 }
9032 else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
9034 }
9035 return WM_UI_HANDLER_BREAK;
9036 }
9037
9039}
9040
9041static int do_button(bContext *C, Block *block, Button *but, const wmEvent *event)
9042{
9044 int retval = WM_UI_HANDLER_CONTINUE;
9045
9046 const bool is_disabled = but->flag & BUT_DISABLED || data->disable_force;
9047
9048 /* If `but->pointype` is set, `but->poin` should be too. */
9049 BLI_assert(!bool(but->pointype) || but->poin);
9050
9051 /* Let disabled text-box buttons to be scrolled/resized. */
9052 if (is_disabled && but->type == ButtonType::TextBox) {
9053 if (do_but_TEXTBOX(C, block, static_cast<ButtonTextBox *>(but), data, event) ==
9055 {
9056 return WM_UI_HANDLER_BREAK;
9057 }
9058 }
9059
9060 /* Only hard-coded stuff here, button interactions with configurable
9061 * keymaps are handled using operators (see #keymap_ui). */
9062
9063 if (data->state == BUTTON_STATE_HIGHLIGHT) {
9064
9065 /* handle copy and paste */
9066 bool is_press_ctrl_but_no_shift = (event->val == KM_PRESS) &&
9067 (event->modifier & (KM_CTRL | KM_OSKEY)) &&
9068 (event->modifier & KM_SHIFT) == 0;
9069 const bool do_copy = event->type == EVT_CKEY && is_press_ctrl_but_no_shift;
9070 const bool do_paste = event->type == EVT_VKEY && is_press_ctrl_but_no_shift;
9071
9072 /* Specific handling for list-rows, we try to find their overlapping text button. */
9073 if ((do_copy || do_paste) && but->type == ButtonType::ListRow) {
9074 Button *labelbut = but_list_row_text_activate(C, but, data, event, BUTTON_ACTIVATE_OVER);
9075 if (labelbut) {
9076 but = labelbut;
9077 data = but->active;
9078 }
9079 }
9080
9081 /* When clicking on a disabled button on top of a list row, activate the list row below it.
9082 * See #150341. */
9083 if (is_disabled && (event->type == LEFTMOUSE) && (event->val == KM_PRESS)) {
9084 if (Button *listrow = listrow_find_mouse_over(data->region, event->xy)) {
9085 button_execute(C, data->region, listrow);
9086 return WM_UI_HANDLER_BREAK;
9087 }
9088 }
9089
9090 /* do copy first, because it is the only allowed operator when disabled */
9091 if (do_copy) {
9092 if (but_copy(C, but, event->modifier & KM_ALT)) {
9093 return WM_UI_HANDLER_BREAK;
9094 }
9095 }
9096
9097 /* handle menu */
9098
9099 if ((event->type == RIGHTMOUSE) && (event->modifier == 0) && (event->val == KM_PRESS)) {
9100 /* For some button types that are typically representing entire sets of data,
9101 * right-clicking to spawn the context menu should also activate the item. This makes it
9102 * clear which item will be operated on. Apply the button immediately, so context menu
9103 * polls get the right active item. */
9104 auto *clicked_view_item_but = static_cast<ButtonViewItem *>(
9105 but->type == ButtonType::ViewItem ? but :
9106 view_item_find_mouse_over(data->region, event->xy));
9107 if (clicked_view_item_but) {
9108 clicked_view_item_but->view_item->activate_for_context_menu(*C);
9110 }
9111
9112 /* RMB has two options now */
9113 if (popup_context_menu_for_button(C, but, event)) {
9115 return WM_UI_HANDLER_BREAK;
9116 }
9117 }
9118
9119 if (is_disabled) {
9121 }
9122
9123#ifdef WITH_INPUT_NDOF
9124 /* 2D view navigation conflicts with using NDOF to adjust colors,
9125 * especially in the node-editor, see: #105224. */
9126 if (event->type == NDOF_MOTION) {
9127 if (data->region->runtime->type->keymapflag & ED_KEYMAP_VIEW2D) {
9129 }
9130 }
9131#endif /* WITH_INPUT_NDOF */
9132
9133 if (do_paste) {
9134 but_paste(C, but, data, event->modifier & KM_ALT);
9135 return WM_UI_HANDLER_BREAK;
9136 }
9137
9138 if ((data->state == BUTTON_STATE_HIGHLIGHT) &&
9140 (event->val == KM_RELEASE) &&
9141 /* Only returns true if the event was handled. */
9142 do_but_extra_operator_icon(C, but, data, event))
9143 {
9144 return WM_UI_HANDLER_BREAK;
9145 }
9146 }
9147
9148 if (but->flag & BUT_DISABLED) {
9149 /* It's important to continue here instead of breaking since breaking causes the event to be
9150 * considered "handled", preventing further click/drag events from being generated.
9151 *
9152 * An example of where this is needed is dragging node-sockets, where dragging a node-socket
9153 * could exit the button before the drag threshold was reached, disable the button then break
9154 * handling of the #MOUSEMOVE event preventing the socket being dragged entirely, see: #96255.
9155 *
9156 * Region level event handling is responsible for preventing events being passed
9157 * through to parts of the UI that are logically behind this button, see: #92364. */
9159 }
9160
9161 switch (but->type) {
9162 case ButtonType::But:
9164 retval = do_but_BUT(C, but, data, event);
9165 break;
9167 retval = do_but_KEYEVT(C, but, data, event);
9168 break;
9170 retval = do_but_HOTKEYEVT(C, but, data, event);
9171 break;
9172 case ButtonType::Tab:
9173 retval = do_but_TAB(C, block, but, data, event);
9174 break;
9176 case ButtonType::Toggle:
9182 case ButtonType::Row:
9183 retval = do_but_TOG(C, but, data, event);
9184 break;
9186 retval = do_but_VIEW_ITEM(C, but, data, event);
9187 break;
9188 case ButtonType::Scroll:
9189 retval = do_but_SCROLL(C, block, but, data, event);
9190 break;
9191 case ButtonType::Grip:
9192 retval = do_but_GRIP(C, block, but, data, event);
9193 break;
9194 case ButtonType::Num:
9195 retval = do_but_NUM(C, block, but, data, event);
9196 break;
9198 retval = do_but_SLI(C, block, but, data, event);
9199 break;
9201 /* Nothing to do! */
9202 break;
9204 retval = do_but_LISTROW(C, but, data, event);
9205 break;
9207 case ButtonType::Label:
9208 case ButtonType::Image:
9212 retval = do_but_EXIT(C, but, data, event);
9213 break;
9215 retval = do_but_HISTOGRAM(C, block, but, data, event);
9216 break;
9218 retval = do_but_WAVEFORM(C, block, but, data, event);
9219 break;
9221 /* Nothing to do! */
9222 break;
9223 case ButtonType::Text:
9225 if ((but->type == ButtonType::SearchMenu) && (but->flag & BUT_VALUE_CLEAR)) {
9226 retval = do_but_SEARCH_UNLINK(C, block, but, data, event);
9227 if (retval & WM_UI_HANDLER_BREAK) {
9228 break;
9229 }
9230 }
9231 retval = do_but_TEX(C, block, but, data, event);
9232 break;
9234 retval = do_but_TEXTBOX(C, block, static_cast<ButtonTextBox *>(but), data, event);
9235 break;
9236
9237 case ButtonType::Menu:
9239 case ButtonType::Block:
9241 retval = do_but_BLOCK(C, but, data, event);
9242 break;
9244 retval = do_but_BUT(C, but, data, event);
9245 break;
9246 case ButtonType::Color:
9247 retval = do_but_COLOR(C, but, data, event);
9248 break;
9250 retval = do_but_UNITVEC(C, block, but, data, event);
9251 break;
9253 retval = do_but_COLORBAND(C, block, but, data, event);
9254 break;
9255 case ButtonType::Curve:
9256 retval = do_but_CURVE(C, block, but, data, event);
9257 break;
9259 retval = do_but_CURVEPROFILE(C, block, but, data, event);
9260 break;
9262 retval = do_but_HSVCUBE(C, block, but, data, event);
9263 break;
9265 retval = do_but_HSVCIRCLE(C, block, but, data, event);
9266 break;
9268 retval = do_but_TRACKPREVIEW(C, block, but, data, event);
9269 break;
9270
9271 /* quiet warnings for unhandled types */
9272 case ButtonType::Sepr:
9275 case ButtonType::Extra:
9276 break;
9277 }
9278
9279#ifdef USE_DRAG_MULTINUM
9280 data = but->active;
9281 if (data) {
9282 if (ISMOUSE_MOTION(event->type) ||
9283 /* if we started dragging, progress on any event */
9284 (data->multi_data.init == HandleButtonMulti::INIT_SETUP))
9285 {
9288 {
9289 /* initialize! */
9290 if (data->multi_data.init == HandleButtonMulti::INIT_UNSET) {
9291 /* --> (HandleButtonMulti::INIT_SETUP | HandleButtonMulti::INIT_DISABLE) */
9292
9293 const float margin_y = DRAG_MULTINUM_THRESHOLD_DRAG_Y / sqrtf(block->aspect);
9294
9295 /* check if we have a vertical gesture */
9296 if (len_squared_v2(data->multi_data.drag_dir) > (margin_y * margin_y)) {
9297 const float dir_nor_y[2] = {0.0, 1.0f};
9298 float dir_nor_drag[2];
9299
9300 normalize_v2_v2(dir_nor_drag, data->multi_data.drag_dir);
9301
9302 if (fabsf(dot_v2v2(dir_nor_drag, dir_nor_y)) > DRAG_MULTINUM_THRESHOLD_VERTICAL) {
9303 data->multi_data.init = HandleButtonMulti::INIT_SETUP;
9304 data->multi_data.drag_lock_x = event->xy[0];
9305 }
9306 else {
9307 data->multi_data.init = HandleButtonMulti::INIT_DISABLE;
9308 }
9309 }
9310 }
9311 else if (data->multi_data.init == HandleButtonMulti::INIT_SETUP) {
9312 /* --> (HandleButtonMulti::INIT_ENABLE) */
9313 const float margin_x = DRAG_MULTINUM_THRESHOLD_DRAG_X / sqrtf(block->aspect);
9314 /* Check if we're don't setting buttons. */
9315 if ((data->text_edit.edit_string &&
9317 ((abs(data->multi_data.drag_lock_x - event->xy[0]) > margin_x) &&
9318 /* Just to be sure, check we're dragging more horizontally then vertically. */
9319 abs(event->prev_xy[0] - event->xy[0]) > abs(event->prev_xy[1] - event->xy[1])))
9320 {
9321 if (data->multi_data.has_mbuts) {
9323 data->multi_data.init = HandleButtonMulti::INIT_ENABLE;
9324 }
9325 else {
9326 data->multi_data.init = HandleButtonMulti::INIT_DISABLE;
9327 }
9328 }
9329 }
9330
9331 if (data->multi_data.init == HandleButtonMulti::INIT_SETUP) {
9332 if (multibut_states_tag(but, data, event)) {
9333 ED_region_tag_redraw(data->region);
9334 }
9335 }
9336 }
9337 }
9338 }
9339#endif /* USE_DRAG_MULTINUM */
9340
9341 return retval;
9342}
9343
9345
9346/* -------------------------------------------------------------------- */
9349
9350static void blocks_set_tooltips(ARegion *region, const bool enable)
9351{
9352 if (!region) {
9353 return;
9354 }
9355
9356 /* We disabled buttons when they were already shown, and re-enable them on mouse move. */
9357 for (Block &block : region->runtime->uiblocks) {
9358 block.tooltipdisabled = !enable;
9359 }
9360}
9361
9363{
9365 if (data) {
9366 bScreen *screen = WM_window_get_active_screen(data->window);
9367 if (screen->tool_tip && screen->tool_tip->region) {
9368 WM_tooltip_refresh(C, data->window);
9369 }
9370 }
9371}
9372
9374{
9376 if (data) {
9377 if (data->autoopentimer) {
9378 WM_event_timer_remove(data->wm, data->window, data->autoopentimer);
9379 data->autoopentimer = nullptr;
9380 }
9381
9382 if (data->window) {
9383 WM_tooltip_clear(C, data->window);
9384 }
9385 }
9386}
9387
9389 bContext *C, ARegion *region, int *pass, double *r_pass_delay, bool *r_exit_on_event)
9390{
9391 bool is_quick_tip = false;
9392 if (*pass == 1) {
9393 is_quick_tip = true;
9394 (*pass)--;
9395 (*r_pass_delay) = UI_TOOLTIP_DELAY + UI_TOOLTIP_DELAY_QUICK;
9396 }
9397
9398 Button *but = region_active_but_get(region);
9399 *r_exit_on_event = false;
9400 if (but) {
9401 const wmWindow *win = CTX_wm_window(C);
9403 but, but->active ? but->active->region : region, win->runtime->eventstate);
9404
9405 return tooltip_create_from_button_or_extra_icon(C, region, but, extra_icon, is_quick_tip);
9406 }
9407 return nullptr;
9408}
9409
9411{
9414
9415 WM_tooltip_timer_clear(C, data->window);
9416
9417 if ((U.flag & USER_TOOLTIPS) || (data->tooltip_force)) {
9418 if (!but->block->tooltipdisabled) {
9419 if (!wm->runtime->drags.first_) {
9420 const bool is_quick_tip = but_has_quick_tooltip(but);
9421 const double delay = is_quick_tip ? UI_TOOLTIP_DELAY_QUICK : UI_TOOLTIP_DELAY;
9423 C, data->window, data->area, data->region, but_tooltip_init, delay);
9424 if (is_quick_tip) {
9425 bScreen *screen = WM_window_get_active_screen(data->window);
9426 if (screen->tool_tip) {
9427 screen->tool_tip->pass = screen->tool_tip->region ? 0 : 1;
9428 }
9429 }
9430 }
9431 }
9432 }
9433}
9434
9436
9437/* -------------------------------------------------------------------- */
9440
9453
9455{
9457 if (data->state == state) {
9458 return;
9459 }
9460
9461 /* Highlight has timers for tool-tips and auto open. */
9463 but->flag &= ~UI_SELECT;
9464
9466
9467 /* Automatic open pull-down block timer. */
9469 /* Menu button types may draw as popovers, check for this case
9470 * ignoring other kinds of menus (mainly enums). (see #66538). */
9471 ((but->type == ButtonType::Menu) &&
9473 {
9474 if (data->used_mouse && !data->autoopentimer) {
9475 int time;
9476
9477 if (but->block->auto_open == true) { /* test for toolbox */
9478 time = 1;
9479 }
9480 else if (but->block->flag & BLOCK_LOOP && but->type == ButtonType::Pulldown) {
9481 /* When auto open is disabled, open subpanel on hover but don't rely on sub level
9482 * threshold value, see: #153110 */
9483 time = (U.uiflag & USER_MENUOPENAUTO) ? 5 * U.menuthreshold2 : 10;
9484 }
9485 else if (U.uiflag & USER_MENUOPENAUTO) {
9486 time = 5 * U.menuthreshold1;
9487 }
9488 else {
9489 time = -1; /* do nothing */
9490 }
9491
9492 if (time >= 0) {
9493 data->autoopentimer = WM_event_timer_add(
9494 data->wm, data->window, TIMER, 0.02 * double(time));
9495 }
9496 }
9497 }
9498 }
9499 else {
9500 but->flag |= UI_SELECT;
9502 }
9503
9505 data->text_select_auto_scroll = WM_event_timer_add(data->wm, data->window, TIMER, 0.1f);
9506 }
9507 else if (state != BUTTON_STATE_TEXT_SELECTING && data->text_select_auto_scroll) {
9508 WM_event_timer_remove(data->wm, data->window, data->text_select_auto_scroll);
9509 data->text_select_auto_scroll = nullptr;
9510 }
9511
9512 /* Only Text-box buttons can set
9513 * #BUTTON_STATE_TEXTBOX_SCROLLING or #BUTTON_STATE_TEXTBOX_RESIZING as state. */
9515 but->type == ButtonType::TextBox);
9516
9517 /* text editing */
9519 }
9520 else if (state == BUTTON_STATE_TEXT_EDITING && !ELEM(data->state,
9524 {
9525 textedit_begin(C, but, data);
9526 }
9528 textedit_end(C, but, data);
9529 }
9530 else if ((data->state == BUTTON_STATE_TEXT_SELECTING ||
9532 but->editstr)) &&
9534 {
9535 textedit_end(C, but, data);
9536 }
9537
9538 /* number editing */
9540 if (button_is_cursor_warp(but)) {
9542 rctf rectf;
9543 block_to_window_rctf(data->region, but->block, &rectf, &but->rect);
9544 rcti bounds;
9545 BLI_rcti_rctf_copy(&bounds, &rectf);
9547 }
9548 else {
9550 }
9551 WorkspaceStatus status(C);
9552 status.item(IFACE_("Cancel"), ICON_EVENT_ESC);
9553#if defined(__APPLE__)
9554 status.item(IFACE_("Snap"), ICON_KEY_COMMAND);
9555#else
9556 status.item(IFACE_("Snap"), ICON_EVENT_CTRL);
9557#endif
9558 if (button_is_float(but)) {
9559 status.item(IFACE_("Precision"), ICON_EVENT_SHIFT);
9560 }
9561 }
9562 numedit_begin(but, data);
9563 }
9564 else if (data->state == BUTTON_STATE_NUM_EDITING) {
9565 numedit_end(but, data);
9566
9568 ED_workspace_status_text(C, nullptr);
9569 }
9570
9571 if (but->flag & BUT_DRIVEN) {
9572 /* Only warn when editing stepping/dragging the value.
9573 * No warnings should show for editing driver expressions though!
9574 */
9577 "Can't edit driven number value, see driver editor for the driver setup");
9578 }
9579 }
9580
9581 if (button_is_cursor_warp(but)) {
9582
9583#ifdef USE_CONT_MOUSE_CORRECT
9584 /* stereo3d has issues with changing cursor location so rather avoid */
9585 if (data->ungrab_mval[0] != FLT_MAX && !WM_stereo3d_enabled(data->window, false)) {
9586 int mouse_ungrab_xy[2];
9587 block_to_window_fl(data->region, but->block, &data->ungrab_mval[0], &data->ungrab_mval[1]);
9588 mouse_ungrab_xy[0] = data->ungrab_mval[0];
9589 mouse_ungrab_xy[1] = data->ungrab_mval[1];
9590
9591 WM_cursor_grab_disable(data->window, mouse_ungrab_xy);
9592 }
9593 else {
9594 WM_cursor_grab_disable(data->window, nullptr);
9595 }
9596#else
9597 WM_cursor_grab_disable(data->window, nullptr);
9598#endif
9599 }
9600 }
9601 /* menu open */
9603 block_open_begin(C, but, data);
9604 }
9605 else if (data->state == BUTTON_STATE_MENU_OPEN) {
9606 block_open_end(C, but, data);
9607 }
9608
9609 /* add a short delay before exiting, to ensure there is some feedback */
9611 data->flashtimer = WM_event_timer_add(data->wm, data->window, TIMER, BUTTON_FLASH_DELAY);
9612 }
9613 else if (data->flashtimer) {
9614 WM_event_timer_remove(data->wm, data->window, data->flashtimer);
9615 data->flashtimer = nullptr;
9616 }
9617
9618 /* add hold timer if it's used */
9619 if (state == BUTTON_STATE_WAIT_RELEASE && (but->hold_func != nullptr)) {
9620 data->hold_action_timer = WM_event_timer_add(
9621 data->wm, data->window, TIMER, BUTTON_AUTO_OPEN_THRESH);
9622 }
9623 else if (data->hold_action_timer) {
9624 WM_event_timer_remove(data->wm, data->window, data->hold_action_timer);
9625 data->hold_action_timer = nullptr;
9626 }
9627
9628 /* Add a blocking ui handler at the window handler for blocking, modal states
9629 * but not for popups, because we already have a window level handler. */
9630 if (!(but->block->handle && but->block->handle->popup)) {
9632 if (!button_modal_state(data->state)) {
9634 &data->window->runtime->modalhandlers,
9636 nullptr,
9637 data,
9639 }
9640 }
9641 else {
9642 if (button_modal_state(data->state)) {
9643 /* true = postpone free */
9645 &data->window->runtime->modalhandlers, handler_region_menu, nullptr, data, true);
9646 }
9647 }
9648 }
9649
9650 /* Wait for mouse-move to enable drag. */
9652 but->flag &= ~UI_SELECT;
9653 }
9654
9657 }
9658 else if (state == BUTTON_STATE_EXIT) {
9659 if (data->state == BUTTON_STATE_NUM_EDITING) {
9660 /* This happens on pasting values for example. */
9662 }
9663 }
9664
9665 if (but->type == ButtonType::TextBox &&
9667 {
9668 /* Text-box buttons allows to start text selection, or to use the handles for resize the
9669 * text-box or scroll the text content while in #BUTTON_STATE_TEXT_EDITING or
9670 * #BUTTON_STATE_HIGHLIGHT with left click, add mouse move event to update the mouse cursor so
9671 * it can properly hint what action it can perform with left click.
9672 */
9674 }
9675
9676 data->state = state;
9677
9678 if (state != BUTTON_STATE_EXIT) {
9679 /* When objects for eg. are removed, running but_update() can access
9680 * the removed data - so disable update on exit. Also in case of
9681 * highlight when not in a popup menu, we remove because data used in
9682 * button below popup might have been removed by action of popup. Needs
9683 * a more reliable solution... */
9684 if (state != BUTTON_STATE_HIGHLIGHT || (but->block->flag & BLOCK_LOOP)) {
9685 button_update(but);
9686 }
9687 }
9688
9689 /* redraw */
9691}
9692
9694 ARegion *region,
9695 Button *but,
9696 ButtonActivateType type)
9697{
9698 /* Don't activate semi-modal buttons the normal way, they have special activation handling. */
9699 if (but->semi_modal_state) {
9700 return;
9701 }
9702 /* Only ever one active button! */
9703 BLI_assert(region_find_active_but(region) == nullptr);
9704
9705 /* setup struct */
9706 HandleButtonData *data = MEM_new<HandleButtonData>(__func__);
9707 data->wm = CTX_wm_manager(C);
9708 data->window = CTX_wm_window(C);
9709 data->area = CTX_wm_area(C);
9710 BLI_assert(region != nullptr);
9711 data->region = region;
9712
9713#ifdef USE_CONT_MOUSE_CORRECT
9714 copy_v2_fl(data->ungrab_mval, FLT_MAX);
9715#endif
9716
9718 /* XXX curve is temp */
9719 }
9720 else {
9721 if ((but->flag & BUT_UPDATE_DELAY) == 0) {
9722 data->interactive = true;
9723 }
9724 }
9725
9726 data->state = BUTTON_STATE_INIT;
9727
9728 /* Activate button. Sets the hover flag to enable button highlights, usually the button is
9729 * initially activated because it's hovered. */
9730 but->flag |= UI_HOVER;
9731 but->active = data;
9732
9733 /* we disable auto_open in the block after a threshold, because we still
9734 * want to allow auto opening adjacent menus even if no button is activated
9735 * in between going over to the other button, but only for a short while */
9736 if (type == BUTTON_ACTIVATE_OVER && but->block->auto_open == true) {
9738 but->block->auto_open = false;
9739 }
9740 }
9741
9742 if (type == BUTTON_ACTIVATE_OVER) {
9743 data->used_mouse = true;
9744 }
9746
9747 if (type == BUTTON_ACTIVATE_OPEN) {
9749
9750 /* activate first button in submenu */
9751 if (data->menu && data->menu->region) {
9752 ARegion *subar = data->menu->region;
9753 Block *subblock = subar->runtime->uiblocks.first();
9754 Button *subbut;
9755
9756 if (subblock) {
9757 subbut = button_first(subblock);
9758
9759 if (subbut) {
9760 handle_button_activate(C, subar, subbut, BUTTON_ACTIVATE);
9761 }
9762 }
9763 }
9764 }
9765 else if (type == BUTTON_ACTIVATE_TEXT_EDITING) {
9767 }
9768 else if (type == BUTTON_ACTIVATE_APPLY) {
9770 }
9771
9772 if (but->type == ButtonType::Grip) {
9773 const bool horizontal = (BLI_rctf_size_x(&but->rect) < BLI_rctf_size_y(&but->rect));
9775 }
9776 else if (but->type == ButtonType::Num) {
9777 numedit_set_active(but);
9778 }
9779
9780 if (but_has_quick_tooltip(but)) {
9781 /* Show a label for this button. */
9782 bScreen *screen = WM_window_get_active_screen(data->window);
9783 if ((BLI_time_now_seconds() - WM_tooltip_time_closed()) < 0.1) {
9785 if (screen->tool_tip) {
9786 screen->tool_tip->pass = 1;
9787 }
9788 }
9789 }
9790}
9791
9792static int popup_handler(bContext *C, const wmEvent *event, void *userdata);
9793static void popup_handler_remove(bContext *C, void *userdata);
9794
9796 bContext *C, Button *but, HandleButtonData *data, const bool mousemove, const bool onfree)
9797{
9798 wmWindow *win = data->window;
9799 Block *block = but->block;
9800
9803 }
9804
9805 /* ensure we are in the exit state */
9806 if (data->state != BUTTON_STATE_EXIT) {
9808 }
9809
9810 /* apply the button action or value */
9811 if (!onfree) {
9812 apply_but(C, block, but, data, false);
9813 }
9814
9815#ifdef USE_DRAG_MULTINUM
9816 if (data->multi_data.has_mbuts) {
9817 for (Button &bt : block->buttons()) {
9818 if (bt.flag & BUT_DRAG_MULTI) {
9819 bt.flag &= ~BUT_DRAG_MULTI;
9820
9821 if (!data->cancel) {
9822 apply_but_autokey(C, &bt);
9823 }
9824 }
9825 }
9826
9827 multibut_free(data, block);
9828 }
9829#endif
9830
9831 /* if this button is in a menu, this will set the button return
9832 * value to the button value and the menu return value to ok, the
9833 * menu return value will be picked up and the menu will close */
9834 if (block->handle && !(block->flag & BLOCK_KEEP_OPEN)) {
9835 if (!data->cancel || data->escapecancel) {
9836 PopupBlockHandle *menu;
9837
9838 menu = block->handle;
9839 menu->menuretval = (data->cancel) ? RETURN_CANCEL : RETURN_OK;
9840 }
9841 }
9842
9843 if (!onfree && !data->cancel) {
9844 /* autokey & undo push */
9845 apply_but_undo(but);
9846 apply_but_autokey(C, but);
9847
9848#ifdef USE_ALLSELECT
9849 {
9850 /* only RNA from this button is used */
9851 Button but_temp = Button(*but);
9852 uiSelectContextStore *selctx_data = &data->select_others;
9853 for (uiSelectContextElem &other : selctx_data->elems) {
9854 but_temp.rnapoin = other.ptr;
9855 apply_but_autokey(C, &but_temp);
9856 }
9857 }
9858#endif
9859
9860 /* popup menu memory */
9861 if (block->flag & BLOCK_POPUP_MEMORY) {
9862 popup_menu_memory_set(block, but);
9863 }
9864
9865 if (U.runtime.is_dirty == false) {
9867 }
9868 }
9869
9870 /* Disable tool-tips until mouse-move + last active flag. */
9871 for (Block &block_iter : data->region->runtime->uiblocks) {
9872 for (Button &bt : block_iter.buttons()) {
9873 bt.flag &= ~BUT_LAST_ACTIVE;
9874 }
9875
9876 block_iter.tooltipdisabled = true;
9877 }
9878
9879 blocks_set_tooltips(data->region, false);
9880
9881 /* clean up */
9882 if (data->text_edit.edit_string) {
9883 MEM_delete(data->text_edit.edit_string);
9884 }
9885 if (data->text_edit.original_string) {
9886 MEM_delete(data->text_edit.original_string);
9887 }
9888
9889#ifdef USE_ALLSELECT
9890 selectcontext_end(but, &data->select_others);
9891#endif
9892
9893 if (data->changed_cursor) {
9894 if (but->type == ButtonType::TextBox || button_draw_as_link(but)) {
9896 }
9898 }
9899 if (data->changed_wokspace_status) {
9900 ED_workspace_status_text(C, nullptr);
9901 }
9902
9903 /* redraw and refresh (for popups) */
9906
9907 if ((but->flag & BUT_DRAG_MULTI) == 0) {
9908 if (data->custom_interaction_handle != nullptr) {
9909 /* Should only set when the button is modal. */
9910 BLI_assert(but->active != nullptr);
9911 data->custom_interaction_handle->user_count--;
9912
9913 BLI_assert(data->custom_interaction_handle->user_count >= 0);
9914 if (data->custom_interaction_handle->user_count == 0) {
9916 C, &but->block->custom_interaction_callbacks, data->custom_interaction_handle);
9917 }
9918 data->custom_interaction_handle = nullptr;
9919 }
9920 }
9921
9922 BLI_assert(!but->semi_modal_state || but->semi_modal_state == but->active);
9923 but->semi_modal_state = nullptr;
9924 /* clean up button */
9926
9927 but->flag &= ~(UI_HOVER | UI_SELECT);
9928 but->flag |= BUT_LAST_ACTIVE;
9929 if (!onfree) {
9930 button_update(but);
9931 }
9932
9933 /* Adds empty mouse-move in queue for re-initialize handler, in case mouse is
9934 * still over a button. We cannot just check for this ourselves because
9935 * at this point the mouse may be over a button in another region. */
9936 if (mousemove) {
9938 }
9939}
9940
9942{
9943 /* this gets called when the button somehow disappears while it is still
9944 * active, this is bad for user interaction, but we need to handle this
9945 * case cleanly anyway in case it happens */
9946 if (but->active) {
9948 data->cancel = true;
9949 button_activate_exit(const_cast<bContext *>(C), but, data, false, true);
9950 }
9951}
9952
9954{
9955 if (!but->semi_modal_state) {
9956 return;
9957 }
9958 /* Activate the button (using the semi modal state) and use the normal active button freeing. */
9961 but,
9962 [&]() { button_active_free(C, but); });
9963}
9964
9965/* returns the active button with an optional checking function */
9966static Button *context_button_active(const ARegion *region, bool (*but_check_cb)(const Button *))
9967{
9968 Button *but_found = nullptr;
9969
9970 while (region) {
9971 /* Follow this exact priority (from highest to lowest priority):
9972 * 1) Active-override button (#BUT_ACTIVE_OVERRIDE).
9973 * 2) The real active button.
9974 * 3) The previously active button (#BUT_LAST_ACTIVE).
9975 */
9976 Button *active_but_override = nullptr;
9977 Button *active_but_real = nullptr;
9978 Button *active_but_last = nullptr;
9979
9980 /* find active button */
9981 for (Block &block : region->runtime->uiblocks) {
9982 for (Button &but : block.buttons()) {
9983 if (but.flag & BUT_ACTIVE_OVERRIDE) {
9984 active_but_override = &but;
9985 }
9986 if (but.active) {
9987 active_but_real = &but;
9988 }
9989 if (but.flag & BUT_LAST_ACTIVE) {
9990 active_but_last = &but;
9991 }
9992 }
9993 }
9994
9995 Button *activebut = active_but_override;
9996 if (!activebut) {
9997 activebut = active_but_real;
9998 }
9999 if (!activebut) {
10000 activebut = active_but_last;
10001 }
10002
10003 if (activebut && (but_check_cb == nullptr || but_check_cb(activebut))) {
10004 HandleButtonData *data = activebut->active;
10005
10006 but_found = activebut;
10007
10008 /* Recurse into opened menu, like color-picker case. */
10009 if (data && data->menu && (region != data->menu->region)) {
10010 region = data->menu->region;
10011 }
10012 else {
10013 return but_found;
10014 }
10015 }
10016 else {
10017 /* no active button */
10018 return but_found;
10019 }
10020 }
10021
10022 return but_found;
10023}
10024
10026{
10027 return context_button_active(CTX_wm_region(C), nullptr);
10028}
10029
10031{
10032 ARegion *region_popup = CTX_wm_region_popup(C);
10033 return context_button_active(region_popup ? region_popup : CTX_wm_region(C), nullptr);
10034}
10035
10037{
10038 return context_button_active(region, nullptr);
10039}
10040
10041Button *region_but_find_rect_over(const ARegion *region, const rcti *rect_px)
10042{
10043 return button_find_rect_over(region, rect_px);
10044}
10045
10046Block *region_block_find_mouse_over(const ARegion *region, const int xy[2], bool only_clip)
10047{
10048 return block_find_mouse_over_ex(region, xy, only_clip);
10049}
10050
10052 PointerRNA *r_ptr,
10053 PropertyRNA **r_prop,
10054 int *r_index)
10055{
10056 Button *activebut = region_active_but_get(region);
10057
10058 if (activebut && activebut->rnapoin) {
10059 *r_ptr = activebut->rnapoin;
10060 *r_prop = activebut->rnaprop;
10061 *r_index = activebut->rnaindex;
10062 }
10063 else {
10064 *r_ptr = {};
10065 *r_prop = nullptr;
10066 *r_index = 0;
10067 }
10068
10069 return activebut;
10070}
10071
10073 PointerRNA *r_ptr,
10074 PropertyRNA **r_prop,
10075 int *r_index)
10076{
10077 ARegion *region_popup = CTX_wm_region_popup(C);
10079 region_popup ? region_popup : CTX_wm_region(C), r_ptr, r_prop, r_index);
10080}
10081
10082void context_active_but_prop_handle(bContext *C, const bool handle_undo)
10083{
10085 if (activebut) {
10086 /* TODO(@ideasman42): look into a better way to handle the button change
10087 * currently this is mainly so reset defaults works for the
10088 * operator redo panel. */
10089 Block *block = activebut->block;
10090
10091 /* There are various functions buttons may run on completion.
10092 * See #apply_but_funcs_after for reference, we could even call
10093 * this function however it may have unforeseen consequences,
10094 * see replies to: !134233. */
10095
10096 /* This may be needed to validate the value, see: #134101. */
10097 if (activebut->func) {
10098 activebut->func(C, activebut->func_arg1, activebut->func_arg2);
10099 }
10100 if (block->handle_func) {
10101 block->handle_func(C, block->handle_func_arg, activebut->retval);
10102 }
10103 if (handle_undo) {
10104 /* Update the button so the undo text uses the correct value. */
10105 button_update(activebut);
10106 apply_but_undo(activebut);
10107 }
10108 }
10109}
10110
10112{
10113 wm_event_handler_ui_cancel_ex(C, win, region, false);
10114}
10115
10117{
10118 ARegion *region_ctx = CTX_wm_region(C);
10119
10120 /* background mode */
10121 if (region_ctx == nullptr) {
10122 return nullptr;
10123 }
10124
10125 /* scan active regions ui */
10126 for (Block &block : region_ctx->runtime->uiblocks) {
10127 if (block.ui_operator) {
10128 return block.ui_operator;
10129 }
10130 }
10131
10132 /* scan popups */
10133 {
10134 bScreen *screen = CTX_wm_screen(C);
10135
10136 for (ARegion &region : screen->regionbase) {
10137 if (&region == region_ctx) {
10138 continue;
10139 }
10140 for (Block &block : region.runtime->uiblocks) {
10141 if (block.ui_operator) {
10142 return block.ui_operator;
10143 }
10144 }
10145 }
10146 }
10147
10148 return nullptr;
10149}
10150
10152{
10153 Button *but = region_active_but_get(button_region);
10154 return (but != nullptr) ? but->active->searchbox : nullptr;
10155}
10156
10158{
10159 Scene *scene = CTX_data_scene(C);
10160 ARegion *region_popup = CTX_wm_region_popup(C);
10161 ARegion *region = region_popup ? region_popup : CTX_wm_region(C);
10162 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
10164 depsgraph, (scene) ? BKE_scene_frame_get(scene) : 0.0f);
10165
10166 while (region) {
10167 /* find active button */
10168 Button *activebut = nullptr;
10169
10170 for (Block &block : region->runtime->uiblocks) {
10171 for (Button &but : block.buttons()) {
10172 button_anim_flag(&but, &anim_eval_context);
10174 if (button_is_decorator(&but)) {
10176 }
10177
10178 ED_region_tag_redraw(region);
10179
10180 if (but.active) {
10181 activebut = &but;
10182 }
10183 else if (!activebut && (but.flag & BUT_LAST_ACTIVE)) {
10184 activebut = &but;
10185 }
10186 }
10187 }
10188
10189 if (activebut) {
10190 /* Always recurse into opened menu, so all buttons update (like color-picker). */
10191 HandleButtonData *data = activebut->active;
10192 if (data && data->menu) {
10193 region = data->menu->region;
10194 }
10195 else {
10196 return;
10197 }
10198 }
10199 else {
10200 /* no active button */
10201 return;
10202 }
10203 }
10204}
10205
10207{
10208 Button *active_but = block_active_but_get(block);
10209 if (!active_but || !active_but->active || !active_but->changed || active_but->block != block) {
10210 return;
10211 }
10212 /* If there is a search popup attached to the button, don't change the view. The popups don't
10213 * support updating the position to the button position nicely. */
10214 HandleButtonData *data = active_but->active;
10215 if (data->searchbox) {
10216 return;
10217 }
10218
10219 but_ensure_in_view(C, active_but->active->region, active_but);
10220}
10221
10223
10224/* -------------------------------------------------------------------- */
10227
10228static Button *but_find_open_event(ARegion *region, const wmEvent *event)
10229{
10230 for (Block &block : region->runtime->uiblocks) {
10231 for (Button &but : block.buttons()) {
10232 if (&but == event->customdata) {
10233 return &but;
10234 }
10235 }
10236 }
10237 return nullptr;
10238}
10239
10240static int handle_button_over(bContext *C, const wmEvent *event, ARegion *region)
10241{
10242 if (event->type == MOUSEMOVE) {
10243 const bool labeledit = event->modifier & KM_CTRL;
10244 /* Allow buttons to be activated to show the tool-tip,
10245 * then force-disable them if they're not considered interactive
10246 * so they don't swallow events but can still display tips. */
10247 const bool for_tooltip = true;
10249 region, event->xy, labeledit, for_tooltip, nullptr, nullptr);
10250 if (but) {
10252
10253 if ((event->modifier & KM_ALT) && but->active) {
10254 /* Display tool-tips if holding Alt on mouse-over when tool-tips are disabled in the
10255 * preferences. */
10256 but->active->tooltip_force = true;
10257 }
10258
10259 if (but->active && !button_is_interactive(but, labeledit)) {
10260 but->active->disable_force = true;
10261 }
10262 }
10263 }
10264 else if (event->type == EVT_BUT_OPEN) {
10265 Button *but = but_find_open_event(region, event);
10266 if (but) {
10268 do_button(C, but->block, but, event);
10269 }
10270 }
10271
10273}
10274
10276{
10277 wmWindow *win = CTX_wm_window(C);
10278
10280
10281 wmEvent event;
10282 wm_event_init_from_window(win, &event);
10283 event.type = EVT_BUT_OPEN;
10284 event.val = KM_PRESS;
10285 event.flag = eWM_EventFlag(0);
10286 event.customdata = but;
10287 event.customdata_free = false;
10288
10289 do_button(C, but->block, but, &event);
10290}
10291
10293{
10295}
10296
10297void button_execute_begin(bContext * /*C*/, ARegion *region, Button *but, void **active_back)
10298{
10299 BLI_assert(region != nullptr);
10300 BLI_assert(BLI_findindex(&region->runtime->uiblocks, but->block) != -1);
10301 /* NOTE: ideally we would not have to change 'but->active' however
10302 * some functions we call don't use data (as they should be doing) */
10304 *active_back = but->active;
10305 data = MEM_new<HandleButtonData>(__func__);
10306 but->active = data;
10307 BLI_assert(region != nullptr);
10308 data->region = region;
10309}
10310
10311void button_execute_end(bContext *C, ARegion * /*region*/, Button *but, void *active_back)
10312{
10313 apply_but(C, but->block, but, but->active, true);
10314
10315 if ((but->flag & BUT_DRAG_MULTI) == 0) {
10316 apply_but_autokey(C, but);
10317 }
10318 /* use onfree event so undo is handled by caller and apply is already done above */
10319 button_activate_exit(C, but, but->active, false, true);
10320 but->active = static_cast<HandleButtonData *>(active_back);
10321}
10322
10324 ARegion *region,
10325 Button *but,
10326 ButtonActivateType type)
10327{
10328 Button *oldbut = region_find_active_but(region);
10329 if (oldbut) {
10330 HandleButtonData *data = oldbut->active;
10331 data->cancel = true;
10332 button_activate_exit(C, oldbut, data, false, false);
10333 }
10334
10335 button_activate_init(C, region, but, type);
10336}
10337
10342{
10344 /* mainly for operator buttons */
10346 }
10348 /* open sub-menus (like right arrow key) */
10350 }
10351 else if (but->type == ButtonType::Menu) {
10352 /* activate menu items */
10354 }
10355 else {
10356#ifndef NDEBUG
10357 CLOG_WARN(&LOG, "%s: error, unhandled type: %d", __func__, int(but->type));
10358#endif
10359 return false;
10360 }
10361 return true;
10362}
10363
10375 ARegion *region,
10376 Button *but,
10377 FunctionRef<void()> fn)
10378{
10379 BLI_assert(but->active == nullptr);
10380
10381 Button *prev_active_but = region_find_active_but(region);
10382 HandleButtonData *prev_active_data = prev_active_but ? prev_active_but->active : nullptr;
10383 if (prev_active_but) {
10384 prev_active_but->active = nullptr;
10385 }
10386
10387 /* Enforce the button to actually be active, using #Button.semi_modal_state to store its handling
10388 * state. */
10389 if (!but->semi_modal_state) {
10390 button_activate_event(C, region, but);
10391 but->semi_modal_state = but->active;
10392 but->semi_modal_state->is_semi_modal = true;
10393 }
10394
10395 /* Activate the button using the previously created/stored semi-modal state. */
10396 but->active = but->semi_modal_state;
10397 fn();
10398 but->active = nullptr;
10399
10400 if (prev_active_but) {
10401 prev_active_but->active = prev_active_data;
10402 }
10403}
10404
10411 ARegion *region,
10412 FunctionRef<void(Button *semi_modal_but)> fn)
10413{
10414 /* Might want to have some way to define which order these should be handled in - if there's
10415 * every actually a use-case for multiple semi-active buttons at the same time. */
10416
10417 for (Block &block : region->runtime->uiblocks) {
10418 for (Button &but : block.buttons()) {
10420 with_but_active_as_semi_modal(C, region, &but, [&]() { fn(&but); });
10421 }
10422 }
10423 }
10424}
10425
10427
10428/* -------------------------------------------------------------------- */
10431
10432static bool button_value_default(Button *but, double *r_value)
10433{
10434 if (but->rnaprop != nullptr && button_is_rna_valid(but)) {
10435 const int type = RNA_property_type(but->rnaprop);
10436 if (ELEM(type, PROP_FLOAT, PROP_INT)) {
10437 double default_value;
10438 switch (type) {
10439 case PROP_INT:
10441 default_value = double(
10443 }
10444 else {
10445 default_value = double(RNA_property_int_get_default(&but->rnapoin, but->rnaprop));
10446 }
10447 break;
10448 case PROP_FLOAT:
10450 default_value = double(
10452 }
10453 else {
10454 default_value = double(RNA_property_float_get_default(&but->rnapoin, but->rnaprop));
10455 }
10456 break;
10457 }
10458 *r_value = default_value;
10459 return true;
10460 }
10461 }
10462 return false;
10463}
10464
10465static int handle_button_event(bContext *C, const wmEvent *event, Button *but)
10466{
10468 const HandleButtonState state_orig = data->state;
10469
10470 Block *block = but->block;
10471 ARegion *region = data->region;
10472
10473 int retval = WM_UI_HANDLER_CONTINUE;
10474
10475 if (data->state == BUTTON_STATE_HIGHLIGHT) {
10476 switch (event->type) {
10477 case WINDEACTIVATE:
10478 case EVT_BUT_CANCEL:
10479 data->cancel = true;
10481 break;
10482#ifdef USE_UI_POPOVER_ONCE
10483 case LEFTMOUSE: {
10484 if (event->val == KM_RELEASE) {
10485 if (block->flag & BLOCK_POPOVER_ONCE) {
10486 if (!(but->flag & BUT_DISABLED)) {
10488 data->cancel = false;
10490 retval = WM_UI_HANDLER_BREAK;
10491 /* Cancel because this `but` handles all events and we don't want
10492 * the parent button's update function to do anything.
10493 *
10494 * Causes issues with buttons defined by #Layout::prop_with_popover.
10495 */
10497 }
10498 else if (button_is_editable_as_text(but)) {
10500 retval = WM_UI_HANDLER_BREAK;
10501 }
10502 }
10503 }
10504 }
10505 break;
10506 }
10507#endif
10508 case MOUSEMOVE: {
10509 Button *but_other = but_find_mouse_over(region, event);
10510 bool exit = false;
10511
10512 /* always deactivate button for pie menus,
10513 * else moving to blank space will leave activated */
10514 if ((!block_is_menu(block) || block_is_pie_menu(block)) &&
10515 !button_contains_point_px(but, region, event->xy))
10516 {
10517 exit = true;
10518 }
10519 else if (but_other && button_is_editable(but_other) && (but_other != but)) {
10520 exit = true;
10521 }
10522
10523 if (exit) {
10524 data->cancel = true;
10526 }
10527 else {
10528 /* While the pointer is moved around we need to reevaluate the
10529 * tooltips. If a tooltip is already showing we normally don't
10530 * want to reset it as this feels like the tooltip is chasing
10531 * the mouse pointer after a short delay. The exception is large
10532 * areas like thumbnails as we might want to dismiss them without
10533 * leaving the area or move to reposition to reveal obscured parts. */
10534 bool reenable_tooltip = true;
10535 bScreen *screen = CTX_wm_screen(C);
10536 if (screen && screen->tool_tip) {
10537 const int movement = len_manhattan_v2v2_int(event->xy, screen->tool_tip->event_xy);
10538 const int threshold = WM_event_drag_threshold(event);
10539 if (screen->tool_tip->region) {
10540 /* Tooltip is showing. Only reset with motion on large buttons.
10541 * Otherwise the tooltip follows the mouse while it is moved. */
10542 const bool large_button = but->type == ButtonType::Label &&
10544 reenable_tooltip = (large_button && movement > threshold);
10545 }
10546 else {
10547 /* Tooltip not yet showing. Allow some movement before resetting timer,
10548 * otherwise it is difficult to get tooltips with pens and touch. #153319. */
10549 reenable_tooltip = (movement > threshold);
10550 }
10551 }
10552
10553 if (reenable_tooltip) {
10554 blocks_set_tooltips(region, true);
10556 }
10557 }
10558
10559 /* Update extra icons states. */
10561
10562 break;
10563 }
10564 case TIMER: {
10565 /* Handle menu auto open timer. */
10566 if (event->customdata == data->autoopentimer) {
10567 WM_event_timer_remove(data->wm, data->window, data->autoopentimer);
10568 data->autoopentimer = nullptr;
10569 /* Do not open sub-menus while using an auto-scroll handler. */
10570 if ((block_is_pie_menu(block) || !block->handle || !block->handle->scrolltimer) &&
10571 (button_contains_point_px(but, region, event->xy) || but->active))
10572 {
10574 }
10575 }
10576
10577 break;
10578 }
10579 /* XXX hardcoded keymap check... but anyway,
10580 * while view changes, tool-tips should be removed */
10581 case WHEELUPMOUSE:
10582 case WHEELDOWNMOUSE:
10583 case MIDDLEMOUSE:
10584 case MOUSEPAN:
10587 default:
10588 break;
10589 }
10590
10591 /* handle button type specific events */
10592 retval = do_button(C, block, but, event);
10593 }
10594 else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
10595 switch (event->type) {
10596 case WINDEACTIVATE:
10597 data->cancel = true;
10599 break;
10600
10601 case TIMER: {
10602 if (event->customdata == data->hold_action_timer) {
10603 if (true) {
10604 data->cancel = true;
10606 }
10607 else {
10608 /* Do this so we can still mouse-up, closing the menu and running the button.
10609 * This is nice to support but there are times when the button gets left pressed.
10610 * Keep disabled for now. */
10611 WM_event_timer_remove(data->wm, data->window, data->hold_action_timer);
10612 data->hold_action_timer = nullptr;
10613 }
10614 retval = WM_UI_HANDLER_CONTINUE;
10615 but->hold_func(C, data->region, but);
10616 }
10617 break;
10618 }
10619 case MOUSEMOVE: {
10620 /* deselect the button when moving the mouse away */
10621 /* also de-activate for buttons that only show highlights */
10622 if (button_contains_point_px(but, region, event->xy)) {
10623
10624 /* Drag on a hold button (used in the toolbar) now opens it immediately. */
10625 if (data->hold_action_timer) {
10626 if (but->flag & UI_SELECT) {
10627 const int threshold = WM_event_drag_threshold(event);
10628 const int movement = len_manhattan_v2v2_int(event->xy, event->prev_press_xy);
10629 if (movement <= threshold) {
10630 /* pass */
10631 }
10632 else {
10633 WM_event_timer_remove(data->wm, data->window, data->hold_action_timer);
10634 data->hold_action_timer = WM_event_timer_add(data->wm, data->window, TIMER, 0.0f);
10635 }
10636 }
10637 }
10638
10639 if (!(but->flag & UI_SELECT)) {
10640 but->flag |= (UI_SELECT | UI_HOVER);
10641 data->cancel = false;
10643 }
10644 }
10645 else {
10646 if (but->flag & UI_SELECT) {
10647 but->flag &= ~(UI_SELECT | UI_HOVER);
10648 data->cancel = true;
10650 }
10651 }
10652 break;
10653 }
10654 default:
10655 /* otherwise catch mouse release event */
10656 do_button(C, block, but, event);
10657 break;
10658 }
10659
10660 retval = WM_UI_HANDLER_BREAK;
10661 }
10662 else if (data->state == BUTTON_STATE_WAIT_FLASH) {
10663 switch (event->type) {
10664 case TIMER: {
10665 if (event->customdata == data->flashtimer) {
10667 }
10668 break;
10669 }
10670 default: {
10671 break;
10672 }
10673 }
10674
10675 retval = WM_UI_HANDLER_CONTINUE;
10676 }
10677 else if (data->state == BUTTON_STATE_MENU_OPEN) {
10678 /* check for exit because of mouse-over another button */
10679 switch (event->type) {
10680 case MOUSEMOVE: {
10681 Button *bt;
10682
10683 if (data->menu && data->menu->region) {
10684 if (region_contains_point_px(data->menu->region, event->xy)) {
10685 break;
10686 }
10687 }
10688
10689 bt = but_find_mouse_over(region, event);
10690
10691 if (bt && bt->active != data) {
10692 if (but->type != ButtonType::Color) { /* exception */
10693 data->cancel = true;
10694 }
10696 }
10697 break;
10698 }
10699 case RIGHTMOUSE: {
10700 if (event->val == KM_PRESS) {
10701 Button *bt = but_find_mouse_over(region, event);
10702 if (bt && bt->active == data) {
10704 }
10705 }
10706 break;
10707 }
10708 default: {
10709 break;
10710 }
10711 }
10712
10713 do_button(C, block, but, event);
10714 retval = WM_UI_HANDLER_CONTINUE;
10715 }
10716 else {
10717 retval = do_button(C, block, but, event);
10718 // retval = WM_UI_HANDLER_BREAK; XXX why ?
10719 }
10720
10721 /* may have been re-allocated above (eyedropper for eg) */
10722 data = but->active;
10723 if (data && data->state == BUTTON_STATE_EXIT) {
10724 Button *post_but = data->postbut;
10725 const ButtonActivateType post_type = data->posttype;
10726
10727 /* Reset the button value when empty text is typed. */
10728 if ((data->cancel == false) && (data->text_edit.edit_string != nullptr) &&
10729 (data->text_edit.edit_string[0] == '\0') &&
10731 {
10732 MEM_SAFE_DELETE(data->text_edit.edit_string);
10733 button_value_default(but, &data->value);
10734
10735#ifdef USE_DRAG_MULTINUM
10736 if (data->multi_data.mbuts) {
10737 for (LinkNode *l = data->multi_data.mbuts; l; l = l->next) {
10738 ButtonMultiState *state = static_cast<ButtonMultiState *>(l->link);
10739 Button *but_iter = state->but;
10740 double default_value;
10741
10742 if (button_value_default(but_iter, &default_value)) {
10743 button_value_set(but_iter, default_value);
10744 }
10745 }
10746 }
10747 data->multi_data.skip = true;
10748#endif
10749 }
10750
10751 button_activate_exit(C, but, data, (post_but == nullptr), false);
10752
10753 /* for jumping to the next button with tab while text editing */
10754 if (post_but) {
10755 /* The post_but still has previous ranges (without the changes in active button considered),
10756 * needs refreshing the ranges. */
10757 button_range_set_soft(post_but);
10758 button_range_set_hard(post_but);
10759
10760 button_activate_init(C, region, post_but, post_type);
10761 }
10762 else if (!((event->type == EVT_BUT_CANCEL) && (event->val == 1))) {
10763 /* XXX issue is because WM_event_add_mousemove(wm) is a bad hack and not reliable,
10764 * if that gets coded better this bypass can go away too.
10765 *
10766 * This is needed to make sure if a button was active,
10767 * it stays active while the mouse is over it.
10768 * This avoids adding mouse-moves, see: #33466. */
10770 if (but_find_mouse_over(region, event) == but) {
10772 }
10773 }
10774 }
10775 }
10776
10777 return retval;
10778}
10779
10780static int uilist_get_increment(const uiList *list, const int type)
10781{
10782 /* Left or right in grid layouts or any direction in single column layouts increments by 1. */
10783 int increment = ELEM(type, EVT_UPARROWKEY, EVT_LEFTARROWKEY, WHEELUPMOUSE) ? -1 : 1;
10784
10785 if ((list->filter_sort_flag & UILST_FLT_SORT_REVERSE) != 0) {
10786 increment *= -1;
10787 }
10788
10789 return increment;
10790}
10791
10792static int handle_uilist_event(bContext *C, const wmEvent *event, ARegion *region, Button *listbox)
10793{
10794 int retval = WM_UI_HANDLER_CONTINUE;
10795 int type = event->type, val = event->val;
10796 int scroll_dir = 1;
10797 bool redraw = false;
10798
10799 uiList *ui_list = static_cast<uiList *>(listbox->custom_data);
10800 if (!ui_list || !ui_list->dyn_data) {
10801 return retval;
10802 }
10803 uiListDyn *dyn_data = ui_list->dyn_data;
10804
10805 int mx = event->xy[0];
10806 int my = event->xy[1];
10807 window_to_block(region, listbox->block, &mx, &my);
10808
10809 /* Convert pan to scroll-wheel. */
10810 if (type == MOUSEPAN) {
10811 pan_to_scroll(event, &type, &val);
10812
10813 /* 'pan_to_scroll' gives the absolute direction. */
10814 if (event->flag & WM_EVENT_SCROLL_INVERT) {
10815 scroll_dir = -1;
10816 }
10817
10818 /* If type still is mouse-pan, we call it handled, since delta-y accumulate. */
10819 /* also see `wm_event_system.cc` do_wheel_ui hack. */
10820 if (type == MOUSEPAN) {
10821 retval = WM_UI_HANDLER_BREAK;
10822 }
10823 }
10824
10825 if (val == KM_PRESS) {
10827 (event->modifier == 0)) ||
10828 (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE) && (event->modifier == KM_CTRL)))
10829 {
10830 const int value_orig = RNA_property_int_get(&listbox->rnapoin, listbox->rnaprop);
10831 int value, min, max;
10832
10833 value = value_orig;
10834 const int inc = uilist_get_increment(ui_list, type);
10835
10836 if (dyn_data->items_filter_neworder || dyn_data->items_filter_flags) {
10837 /* If we have a display order different from
10838 * collection order, we have some work! */
10839 int *org_order = MEM_new_array_uninitialized<int>(dyn_data->items_shown, __func__);
10840 const int *new_order = dyn_data->items_filter_neworder;
10841 int org_idx = -1, len = dyn_data->items_len;
10842 int current_idx = -1;
10843
10844 for (int i = 0; i < len; i++) {
10845 if (uilist_item_index_is_filtered_visible(ui_list, i)) {
10846 org_order[new_order ? new_order[++org_idx] : ++org_idx] = i;
10847 if (i == value) {
10848 current_idx = new_order ? new_order[org_idx] : org_idx;
10849 }
10850 }
10851 else if (i == value && org_idx >= 0) {
10852 current_idx = -(new_order ? new_order[org_idx] : org_idx) - 1;
10853 }
10854 }
10855 /* Now, org_order maps displayed indices to real indices,
10856 * and current_idx either contains the displayed index of active value (positive),
10857 * or its more-nearest one (negated).
10858 */
10859 if (current_idx < 0) {
10860 current_idx = (current_idx * -1) + (inc < 0 ? inc : inc - 1);
10861 }
10862 else {
10863 current_idx += inc;
10864 }
10865 CLAMP(current_idx, 0, dyn_data->items_shown - 1);
10866 value = org_order[current_idx];
10867 MEM_delete(org_order);
10868 }
10869 else {
10870 value += inc;
10871 }
10872
10873 CLAMP(value, 0, dyn_data->items_len - 1);
10874
10875 RNA_property_int_range(&listbox->rnapoin, listbox->rnaprop, &min, &max);
10876 CLAMP(value, min, max);
10877
10878 if (value != value_orig) {
10879 RNA_property_int_set(&listbox->rnapoin, listbox->rnaprop, value);
10880 RNA_property_update(C, &listbox->rnapoin, listbox->rnaprop);
10881
10882 Button *but = button_first(listbox->block);
10883 if (but && but->type == ButtonType::ListRow) {
10884 apply_but_undo(but, true);
10885 }
10886
10888 redraw = true;
10889 }
10890 retval = WM_UI_HANDLER_BREAK;
10891 }
10892 else if (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE) && (event->modifier & KM_SHIFT)) {
10893 /* We now have proper grip, but keep this anyway! */
10894 if (ui_list->list_grip < (dyn_data->visual_height_min - UI_LIST_AUTO_SIZE_THRESHOLD)) {
10895 ui_list->list_grip = dyn_data->visual_height;
10896 }
10897 ui_list->list_grip += (type == WHEELUPMOUSE) ? -1 : 1;
10898
10900
10901 redraw = true;
10902 retval = WM_UI_HANDLER_BREAK;
10903 }
10904 else if (ELEM(type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
10905 if (dyn_data->height > dyn_data->visual_height) {
10906 /* list template will clamp */
10907 ui_list->list_scroll += scroll_dir * ((type == WHEELUPMOUSE) ? -1 : 1);
10908
10909 redraw = true;
10910 retval = WM_UI_HANDLER_BREAK;
10911 }
10912 }
10913 }
10914
10915 if (redraw) {
10916 ED_region_tag_redraw(region);
10918 }
10919
10920 return retval;
10921}
10922
10923/* Handle mouse hover for Views and UiList rows. */
10924static int handle_viewlist_items_hover(const wmEvent *event, ARegion *region)
10925{
10926 const bool has_list = !region->ui_lists.is_empty();
10927 const bool has_view = [&]() {
10928 for (Block &block : region->runtime->uiblocks) {
10929 if (!block.views.is_empty()) {
10930 return true;
10931 }
10932 }
10933 return false;
10934 }();
10935
10936 if (!has_view && !has_list) {
10937 /* Avoid unnecessary lookup. */
10939 }
10940
10941 /* Always highlight the hovered view item, even if the mouse hovers another button inside. */
10942 Button *highlight_row_but = [&]() -> Button * {
10943 if (Button *but = view_item_find_search_highlight(region)) {
10944 return but;
10945 }
10946 if (Button *but = view_item_find_mouse_over(region, event->xy)) {
10947 return but;
10948 }
10949 if (Button *but = listrow_find_mouse_over(region, event->xy)) {
10950 return but;
10951 }
10952 return nullptr;
10953 }();
10954
10955 bool changed = false;
10956
10957 if (highlight_row_but && !(highlight_row_but->flag & UI_HOVER)) {
10958 highlight_row_but->flag |= UI_HOVER;
10959 changed = true;
10960 }
10961
10962 for (Block &block : region->runtime->uiblocks) {
10963 for (Button &but : block.buttons()) {
10964 if (&but == highlight_row_but) {
10965 continue;
10966 }
10968 continue;
10969 }
10970
10971 if (but.flag & UI_HOVER) {
10972 but.flag &= ~UI_HOVER;
10973 changed = true;
10974 }
10975 }
10976 }
10977
10978 if (changed) {
10980 }
10981
10983}
10984
10986 const wmEvent *event,
10987 Button *active_but,
10988 ARegion *region)
10989{
10990 switch (event->type) {
10991 case MOUSEMOVE:
10992 if (event->xy[0] != event->prev_xy[0] || event->xy[1] != event->prev_xy[1]) {
10994 }
10995 break;
10996 case LEFTMOUSE:
10997 if (event->modifier == 0) {
10998 /* Only bother finding the active view item button if the active button isn't already a
10999 * view item. */
11000 auto *view_but = static_cast<ButtonViewItem *>(
11001 (active_but && active_but->type == ButtonType::ViewItem) ?
11002 active_but :
11003 view_item_find_mouse_over(region, event->xy));
11004
11005 if (view_but) {
11006 if (view_item_supports_drag(*view_but->view_item)) {
11007 if (event->val != KM_CLICK) {
11008 break;
11009 }
11010 }
11011 else if (event->val != KM_PRESS) {
11012 break;
11013 }
11014
11015 /* Will free active button if there already is one. */
11016 /* Close the popup when clicking on the view item directly, not any overlapped button. */
11017 const bool close_popup = view_but == active_but;
11018 force_activate_view_item_but(C, region, view_but, close_popup);
11019 }
11020 }
11021 break;
11022 case EVT_RETKEY:
11023 case EVT_PADENTER:
11024 if (event->val == KM_PRESS) {
11025 if (auto *search_highlight_but = static_cast<ButtonViewItem *>(
11027 {
11028 force_activate_view_item_but(C, region, search_highlight_but);
11029 return WM_UI_HANDLER_BREAK;
11030 }
11031 }
11032 break;
11033 default:
11034 break;
11035 }
11036
11038}
11039
11040static void handle_button_return_submenu(bContext *C, const wmEvent *event, Button *but)
11041{
11043 PopupBlockHandle *menu = data->menu;
11044
11045 /* copy over return values from the closing menu */
11046 if ((menu->menuretval & RETURN_OK) || (menu->menuretval & RETURN_UPDATE)) {
11047 if (but->type == ButtonType::Color) {
11048 copy_v3_v3(data->vec, menu->retvec);
11049 }
11050 else if (but->type == ButtonType::Menu) {
11051 data->value = menu->retvalue;
11052 }
11053 }
11054
11055 if (menu->menuretval & RETURN_UPDATE) {
11056 if (data->interactive) {
11057 apply_but(C, but->block, but, data, true);
11058 }
11059 else {
11060 button_update(but);
11061 }
11062
11063 menu->menuretval = 0;
11064 }
11065
11066 /* now change button state or exit, which will close the submenu */
11067 if ((menu->menuretval & RETURN_OK) || (menu->menuretval & RETURN_CANCEL)) {
11068 if (menu->menuretval != RETURN_OK) {
11069 data->cancel = true;
11070 }
11071
11072 button_activate_exit(C, but, data, true, false);
11073 }
11074 else if (menu->menuretval & RETURN_OUT) {
11075 if (event->type == MOUSEMOVE && button_contains_point_px(but, data->region, event->xy)) {
11077 }
11078 else {
11079 if (ISKEYBOARD(event->type)) {
11080 /* keyboard menu hierarchy navigation, going back to previous level */
11081 but->active->used_mouse = false;
11083 }
11084 else {
11085 data->cancel = true;
11086 button_activate_exit(C, but, data, true, false);
11087 }
11088 }
11089 }
11090}
11091
11093
11094/* -------------------------------------------------------------------- */
11097
11107
11108static void mouse_motion_towards_init_ex(PopupBlockHandle *menu, const int xy[2], const bool force)
11109{
11110 BLI_assert(((Block *)menu->region->runtime->uiblocks.first_)->flag &
11112
11113 if (!menu->dotowards || force) {
11114 menu->dotowards = true;
11115 menu->towards_xy[0] = xy[0];
11116 menu->towards_xy[1] = xy[1];
11117
11118 if (force) {
11119 menu->towardstime = DBL_MAX; /* unlimited time */
11120 }
11121 else {
11123 }
11124 }
11125}
11126
11127static void mouse_motion_towards_init(PopupBlockHandle *menu, const int xy[2])
11128{
11129 mouse_motion_towards_init_ex(menu, xy, false);
11130}
11131
11132static void mouse_motion_towards_reinit(PopupBlockHandle *menu, const int xy[2])
11133{
11134 mouse_motion_towards_init_ex(menu, xy, true);
11135}
11136
11138 PopupBlockHandle *menu,
11139 const int xy[2],
11140 const bool use_wiggle_room)
11141{
11143
11144 /* annoying fix for #36269, this is a bit odd but in fact works quite well
11145 * don't mouse-out of a menu if another menu has been created after it.
11146 * if this causes problems we could remove it and check on a different fix - campbell */
11147 if (menu->region->next) {
11148 /* Test if this is the last menu. */
11149 ARegion *region = menu->region->next;
11150 do {
11151 Block *block_iter = region->runtime->uiblocks.first();
11152 if (block_iter && block_is_menu(block_iter)) {
11153 return true;
11154 }
11155 } while ((region = region->next));
11156 }
11157 /* annoying fix end! */
11158
11159 if (!menu->dotowards) {
11160 return false;
11161 }
11162
11163 float oldp[2] = {menu->towards_xy[0], menu->towards_xy[1]};
11164 const float newp[2] = {float(xy[0]), float(xy[1])};
11165 if (len_squared_v2v2(oldp, newp) < (4.0f * 4.0f)) {
11166 return menu->dotowards;
11167 }
11168
11169 /* verify that we are moving towards one of the edges of the
11170 * menu block, in other words, in the triangle formed by the
11171 * initial mouse location and two edge points. */
11172 rctf rect_px;
11173 block_to_window_rctf(menu->region, block, &rect_px, &block->rect);
11174
11175 const float margin = MENU_TOWARDS_MARGIN;
11176
11177 const float p1[2] = {rect_px.xmin - margin, rect_px.ymin - margin};
11178 const float p2[2] = {rect_px.xmax + margin, rect_px.ymin - margin};
11179 const float p3[2] = {rect_px.xmax + margin, rect_px.ymax + margin};
11180 const float p4[2] = {rect_px.xmin - margin, rect_px.ymax + margin};
11181
11182 /* allow for some wiggle room, if the user moves a few pixels away,
11183 * don't immediately quit (only for top level menus) */
11184 if (use_wiggle_room) {
11185 const float cent[2] = {BLI_rctf_cent_x(&rect_px), BLI_rctf_cent_y(&rect_px)};
11186 float delta[2];
11187
11188 sub_v2_v2v2(delta, oldp, cent);
11190 add_v2_v2(oldp, delta);
11191 }
11192
11193 bool closer = (isect_point_tri_v2(newp, oldp, p1, p2) ||
11194 isect_point_tri_v2(newp, oldp, p2, p3) ||
11195 isect_point_tri_v2(newp, oldp, p3, p4) || isect_point_tri_v2(newp, oldp, p4, p1));
11196
11197 if (!closer) {
11198 menu->dotowards = false;
11199 }
11200
11201 /* 1 second timer */
11203 menu->dotowards = false;
11204 }
11205
11206 return menu->dotowards;
11207}
11208
11209#ifdef USE_KEYNAV_LIMIT
11210static void mouse_motion_keynav_init(KeyNavLock *keynav, const wmEvent *event)
11211{
11212 keynav->is_keynav = true;
11213 copy_v2_v2_int(keynav->event_xy, event->xy);
11214}
11215
11219static bool mouse_motion_keynav_test(KeyNavLock *keynav, const wmEvent *event)
11220{
11221 if (keynav->is_keynav &&
11223 {
11224 keynav->is_keynav = false;
11225 }
11226
11227 return keynav->is_keynav;
11228}
11229#endif /* USE_KEYNAV_LIMIT */
11230
11232
11233/* -------------------------------------------------------------------- */
11236
11237static char menu_scroll_test(Block *block, int2 xy)
11238{
11239 rctf auto_scroll_bounds = block->rect;
11240 const float shadow_width = theme::get_menu_shadow_width() / block->aspect;
11241 BLI_rctf_pad(&auto_scroll_bounds, shadow_width, shadow_width);
11242 if (!BLI_rctf_isect_pt(&auto_scroll_bounds, UNPACK2(xy))) {
11243 return 0;
11244 }
11245 if (block->flag & (BLOCK_CLIPTOP | BLOCK_CLIPBOTTOM)) {
11246 if (block->flag & BLOCK_CLIPTOP) {
11247 if (xy[1] > block->rect.ymax - UI_MENU_SCROLL_MOUSE / block->aspect) {
11248 return 't';
11249 }
11250 }
11251 if (block->flag & BLOCK_CLIPBOTTOM) {
11252 if (xy[1] < block->rect.ymin + UI_MENU_SCROLL_MOUSE / block->aspect) {
11253 return 'b';
11254 }
11255 }
11256 }
11257 return 0;
11258}
11259
11260static void menu_scroll_apply_offset_y(ARegion *region, Block *block, float dy)
11261{
11262 BLI_assert(dy != 0.0f);
11263
11264 /* remember scroll offset for refreshes */
11265 const float prev_scroll = block->handle->scrolloffset;
11266 block->handle->scrolloffset = std::clamp(
11267 block->handle->scrolloffset + dy, block->handle->scrollmin, block->handle->scrollmax);
11268 dy = block->handle->scrolloffset - prev_scroll;
11269 /* Apply popup scroll delta to layout panels too. */
11271
11272 /* apply scroll offset */
11273 for (Button &bt : block->buttons()) {
11274 bt.rect.ymin += dy;
11275 bt.rect.ymax += dy;
11276 }
11277
11278 /* set flags again */
11280
11281 ED_region_tag_redraw(region);
11282}
11283
11285static bool menu_scroll_to_but(ARegion *region, Block *block, Button *but_target)
11286{
11287 float dy = 0.0;
11288 if (block->flag & BLOCK_CLIPTOP) {
11289 if (but_target->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_MOUSE / block->aspect) {
11290 dy = block->rect.ymax - but_target->rect.ymax - UI_MENU_SCROLL_MOUSE / block->aspect;
11291 }
11292 }
11293 if (block->flag & BLOCK_CLIPBOTTOM) {
11294 if (but_target->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_MOUSE / block->aspect) {
11295 dy = block->rect.ymin - but_target->rect.ymin + UI_MENU_SCROLL_MOUSE / block->aspect;
11296 }
11297 }
11298 if (dy != 0.0f) {
11299 menu_scroll_apply_offset_y(region, block, dy);
11300 return true;
11301 }
11302 return false;
11303}
11304
11306static bool menu_scroll_to_y(ARegion *region, Block *block, int y)
11307{
11308 const char test = menu_scroll_test(block,
11309 {int(BLI_rctf_cent_x(&block->rect)),
11310 std::clamp<int>(y, block->rect.ymin, block->rect.ymax)});
11311 float dy = 0.0f;
11312 if (test == 't') {
11313 dy = -UI_UNIT_Y / block->aspect; /* scroll to the top */
11314 }
11315 else if (test == 'b') {
11316 dy = UI_UNIT_Y / block->aspect; /* scroll to the bottom */
11317 }
11318 if (dy != 0.0f) {
11319 menu_scroll_apply_offset_y(region, block, dy);
11320 return true;
11321 }
11322 return false;
11323}
11324
11325static bool menu_scroll_step(ARegion *region, Block *block, const int scroll_dir)
11326{
11327 int my;
11328 if (scroll_dir == 1) {
11329 if ((block->flag & BLOCK_CLIPTOP) == 0) {
11330 return false;
11331 }
11332 my = block->rect.ymax + UI_UNIT_Y / block->aspect;
11333 }
11334 else if (scroll_dir == -1) {
11335 if ((block->flag & BLOCK_CLIPBOTTOM) == 0) {
11336 return false;
11337 }
11338 my = block->rect.ymin - UI_UNIT_Y / block->aspect;
11339 }
11340 else {
11341 BLI_assert(0);
11342 return false;
11343 }
11344
11345 return menu_scroll_to_y(region, block, my);
11346}
11347
11349
11350/* -------------------------------------------------------------------- */
11353
11355{
11356 for (Block &block : region->runtime->uiblocks) {
11357 block.auto_open = false;
11358 }
11359}
11360
11371 const Button *but,
11372 const int level,
11373 const bool is_parent_menu,
11374 const int retval)
11375{
11376 /* NOTE(@ideasman42): For `menu->popup` (not a nested tree of menus), don't pass events parents.
11377 * This is needed because enum popups (for example) aren't created with an active button.
11378 * Otherwise opening a popup & pressing the accelerator key would fail, see: #107838. */
11379 if ((level != 0) && (but == nullptr) && (is_parent_menu || menu->popup == false)) {
11381 (void)retval; /* so release builds with strict flags are happy as well */
11383 return true;
11384 }
11385 return false;
11386}
11387
11388static int handle_menu_button(bContext *C, const wmEvent *event, PopupBlockHandle *menu)
11389{
11390 ARegion *region = menu->region;
11391 Button *but = region_find_active_but(region);
11392
11393 if (but) {
11394 /* Its possible there is an active menu item NOT under the mouse,
11395 * in this case ignore mouse clicks outside the button (but Enter etc is accepted) */
11396 if (event->val == KM_RELEASE) {
11397 /* pass, needed so we can exit active menu-items when click-dragging out of them */
11398 }
11399 else if (but->type == ButtonType::SearchMenu) {
11400 /* Pass, needed so search popup can have RMB context menu.
11401 * This may be useful for other interactions which happen in the search popup
11402 * without being directly over the search button. */
11403 }
11404 else if (!block_is_menu(but->block) || block_is_pie_menu(but->block)) {
11405 /* pass, skip for dialogs */
11406 }
11407 else if (!region_contains_point_px(but->active->region, event->xy)) {
11408 /* Pass, needed to click-exit outside of non-floating menus. */
11410 }
11411 else if (ISMOUSE_BUTTON(event->type)) {
11412 if (!button_contains_point_px(but, but->active->region, event->xy)) {
11413 but = nullptr;
11414 }
11415 }
11416 }
11417
11418 int retval;
11419 if (but) {
11420 ScrArea *ctx_area = CTX_wm_area(C);
11421 ARegion *ctx_region = CTX_wm_region(C);
11422
11423 if (menu->ctx_area) {
11424 CTX_wm_area_set(C, menu->ctx_area);
11425 }
11426 if (menu->ctx_region) {
11428 }
11429
11430 retval = handle_button_event(C, event, but);
11431
11432 if (menu->ctx_area) {
11433 CTX_wm_area_set(C, ctx_area);
11434 }
11435 if (menu->ctx_region) {
11436 CTX_wm_region_set(C, ctx_region);
11437 }
11438 }
11439 else {
11440 retval = handle_button_over(C, event, region);
11441 }
11442
11443 return retval;
11444}
11445
11446float block_calc_pie_segment(Block *block, const float event_xy[2])
11447{
11448 float seg1[2];
11449
11450 if (block->pie_data->flags & PIE_INITIAL_DIRECTION) {
11451 copy_v2_v2(seg1, block->pie_data->pie_center_init);
11452 }
11453 else {
11454 copy_v2_v2(seg1, block->pie_data->pie_center_spawned);
11455 }
11456
11457 float seg2[2];
11458 sub_v2_v2v2(seg2, event_xy, seg1);
11459
11460 const float len = normalize_v2_v2(block->pie_data->pie_dir, seg2);
11461
11462 if (len < U.pie_menu_threshold * UI_SCALE_FAC) {
11463 block->pie_data->flags |= PIE_INVALID_DIR;
11464 }
11465 else {
11466 block->pie_data->flags &= ~PIE_INVALID_DIR;
11467 }
11468
11469 return len;
11470}
11471
11473 PointerRNA *props,
11474 const StringRefNull prop_name)
11475{
11476 if (event->type == EVT_SPACEKEY) {
11477 return;
11478 }
11479 /* Forward all keys except space-bar to the search. */
11480 const int num_bytes = BLI_str_utf8_size_or_error(event->utf8_buf);
11481 if (num_bytes == -1) {
11482 return;
11483 }
11484 char buf[sizeof(event->utf8_buf) + 1];
11485 memcpy(buf, event->utf8_buf, num_bytes);
11486 buf[num_bytes] = '\0';
11487 RNA_string_set(props, prop_name.c_str(), buf);
11488}
11489
11491{
11492 /* Start menu search if the menu has a name. */
11493 if (menu->menu_idname[0]) {
11494 AfterFunc *after = afterfunc_new();
11495 wmOperatorType *ot = WM_operatortype_find("WM_OT_search_single_menu", false);
11496 after->optype = ot;
11498 after->opptr = MEM_new<PointerRNA>(__func__, WM_operator_properties_create_ptr(ot));
11499 RNA_string_set(after->opptr, "menu_idname", menu->menu_idname);
11500 set_initial_search_query_from_event(event, after->opptr, "initial_query");
11501 menu->menuretval = RETURN_OK;
11502 return WM_UI_HANDLER_BREAK;
11503 }
11505}
11506
11508 const wmEvent *event,
11509 PopupBlockHandle *menu,
11510 int level,
11511 const bool is_parent_menu)
11512{
11513 ARegion *region = menu->region;
11514 Block *block = region->runtime->uiblocks.first();
11515 wmWindow *win = CTX_wm_window(C);
11516 Button *but = region_find_active_but(region);
11517 int mx = event->xy[0];
11518 int my = event->xy[1];
11519 window_to_block(region, block, &mx, &my);
11520
11521 /* Check if mouse is inside the menu. */
11522 const bool inside = BLI_rctf_isect_pt(&block->rect, mx, my);
11523
11524 int retval = WM_UI_HANDLER_CONTINUE;
11525 /* Remove the #menu::keep_open_timer once the mouse is within the popup. */
11526 if (!menu->mmb_panning && inside) {
11527 if (menu->keep_open_timer) {
11529 menu->keep_open_timer = nullptr;
11530 }
11531 }
11532 /* Once #menu::keep_open_timer ticks the menu can be closed automatically. */
11533 if (event->type == TIMER && event->customdata == menu->keep_open_timer) {
11535 menu->keep_open_timer = nullptr;
11536 retval = WM_UI_HANDLER_BREAK;
11537 }
11538
11539 if (menu->mmb_panning && event->type == MIDDLEMOUSE && event->val == KM_RELEASE) {
11541 WM_cursor_grab_disable(win, nullptr);
11542 menu->mmb_panning = false;
11543 if (!inside) {
11544 /* Set the threshold to prevent from closing the menu when middle mouse button panning ends
11545 * outside the menu bounds. */
11548 }
11549 retval = WM_UI_HANDLER_BREAK;
11550 }
11551 /* Handle middle mouse panning. */
11552 else if (menu->mmb_panning && event->type == MOUSEMOVE) {
11553 const int delta = (menu->mmb_panning_last_y - event->xy[1]) *
11554 (event->flag & WM_EVENT_SCROLL_INVERT ? 1 : -1);
11555 if (delta) {
11556 menu_scroll_apply_offset_y(region, block, delta);
11557 menu->mmb_panning_last_y = event->xy[1];
11558 }
11559 retval = WM_UI_HANDLER_BREAK;
11560 }
11561 else if (event->type == MOUSEMOVE && !inside && menu->keep_open_timer) {
11562 retval = WM_UI_HANDLER_BREAK;
11563 }
11564 else if (event->type == MIDDLEMOUSE && !inside) {
11565 /* Let parent menus handle middle mouse panning if the mouse is not within the current menu. */
11566 if (menu_pass_event_to_parent_if_nonactive(menu, but, level, is_parent_menu, 0)) {
11568 }
11569 }
11570 else if (event->type == MIDDLEMOUSE) {
11571 if (!(block->flag & (BLOCK_CLIPTOP | BLOCK_CLIPBOTTOM))) {
11572 return WM_UI_HANDLER_BREAK;
11573 }
11574 menu->mmb_panning = event->val == KM_PRESS;
11575 if (menu->mmb_panning) {
11576 but = region_find_active_but(region);
11577 if (but) {
11578 but->active->cancel = true;
11579 button_activate_exit(C, but, but->active, false, false);
11580 }
11581 }
11582 menu->mmb_panning_last_y = event->xy[1];
11583 menu->retvalue = 0;
11584 if (menu->mmb_panning) {
11585 rctf rectf;
11586 block_to_window_rctf(menu->region, block, &rectf, &block->rect);
11587 rcti bounds;
11588 BLI_rcti_rctf_copy(&bounds, &rectf);
11590 if (U.uiflag & USER_CONTINUOUS_MOUSE && !WM_event_is_tablet(event)) {
11592 }
11593 }
11594 retval = WM_UI_HANDLER_BREAK;
11595 }
11596 if (menu->mmb_panning && !ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE, TIMER)) {
11597 retval = WM_UI_HANDLER_BREAK;
11598 }
11599 return retval;
11600}
11601
11603 const wmEvent *event,
11604 PopupBlockHandle *menu,
11605 int level,
11606 const bool is_parent_inside,
11607 const bool is_parent_menu,
11608 const bool is_floating)
11609{
11610 Button *but;
11611 ARegion *region = menu->region;
11612 Block *block = region->runtime->uiblocks.first();
11613
11614 int retval = WM_UI_HANDLER_CONTINUE;
11615
11616 int mx = event->xy[0];
11617 int my = event->xy[1];
11618 window_to_block(region, block, &mx, &my);
11619
11620 /* check if mouse is inside block */
11621 const bool inside = BLI_rctf_isect_pt(&block->rect, mx, my);
11622 /* check for title dragging */
11623 const bool inside_title = inside && ((my + (UI_UNIT_Y * 1.4f)) > block->rect.ymax);
11624
11625 /* if there's an active modal button, don't check events or outside, except for search menu */
11626 but = region_find_active_but(region);
11627
11628#ifdef USE_DRAG_POPUP
11629
11630# if defined(__APPLE__)
11631 constexpr int PopupTitleHoverCursor = WM_CURSOR_HAND;
11632 constexpr int PopupTitleDragCursor = WM_CURSOR_HAND_CLOSED;
11633# else
11634 constexpr int PopupTitleHoverCursor = WM_CURSOR_MOVE;
11635 constexpr int PopupTitleDragCursor = WM_CURSOR_MOVE;
11636# endif
11637
11638 wmWindow *win = CTX_wm_window(C);
11639
11640 if (!menu->mmb_panning && !menu->is_grab && is_floating) {
11641 if (inside_title && (!but || but->type == ButtonType::Image)) {
11642 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
11643 /* Initial press before starting to drag. */
11644 WM_cursor_set(win, PopupTitleDragCursor);
11645 }
11646 else if (event->type == MOUSEMOVE && !win->modalcursor) {
11647 /* Hover over draggable area. */
11648 WM_cursor_set(win, PopupTitleHoverCursor);
11649 }
11650 }
11651 else if (win->cursor == PopupTitleHoverCursor) {
11653 }
11654 }
11655
11656 if (menu->is_grab) {
11657 if (event->type == LEFTMOUSE) {
11658 menu->is_grab = false;
11660 retval = WM_UI_HANDLER_BREAK;
11661 }
11662 else {
11663 if (event->type == MOUSEMOVE) {
11664 WM_cursor_set(win, PopupTitleDragCursor);
11665 int2 mdiff = int2(event->xy) - int2(menu->grab_xy_prev);
11666
11667 copy_v2_v2_int(menu->grab_xy_prev, event->xy);
11668
11669 menu->popup_create_vars.event_xy += mdiff;
11670
11671 popup_translate(region, mdiff);
11672 }
11673
11674 return retval;
11675 }
11676 }
11677#endif
11678 if (retval == WM_UI_HANDLER_CONTINUE) {
11679 retval = handle_menu_mmb_event(C, event, menu, level, is_parent_menu);
11680 }
11681 if (retval != WM_UI_HANDLER_CONTINUE) {
11682 }
11683 else if (but && button_modal_state(but->active->state)) {
11684 if (block->flag & (BLOCK_MOVEMOUSE_QUIT | BLOCK_POPOVER)) {
11685 /* if a button is activated modal, always reset the start mouse
11686 * position of the towards mechanism to avoid losing focus,
11687 * and don't handle events */
11688 mouse_motion_towards_reinit(menu, event->xy);
11689 }
11690 }
11691 else if (event->type == TIMER && event->customdata == menu->scrolltimer) {
11692 if (!menu_scroll_test(block, {mx, my})) {
11694 menu->scrolltimer = nullptr;
11695 }
11696 /* Don't auto-scroll while panning. */
11697 else if (!menu->mmb_panning && !menu->keep_open_timer) {
11698 menu_scroll_to_y(region, block, my);
11699 }
11700 }
11701 else {
11702 /* for mouse_motion_towards_block */
11703 if (event->type == MOUSEMOVE) {
11704 if (block->flag & (BLOCK_MOVEMOUSE_QUIT | BLOCK_POPOVER)) {
11705 mouse_motion_towards_init(menu, event->xy);
11706 }
11707
11708 /* add menu scroll timer, if needed */
11709 if (menu_scroll_test(block, {mx, my})) {
11710 if (menu->scrolltimer == nullptr) {
11713 }
11714 }
11715 }
11716
11717 /* first block own event func */
11718 if (block->block_event_func && block->block_event_func(C, block, event)) {
11719 /* pass */
11720 } /* events not for active search menu button */
11721 else {
11722 int act = 0;
11723
11724 switch (event->type) {
11725
11726 /* Closing sub-levels of pull-downs.
11727 *
11728 * The actual event is handled by the button under the cursor.
11729 * This is done so we can right click on menu items even when they have sub-menus open.
11730 */
11731 case RIGHTMOUSE:
11732 if (inside == false) {
11733 if (event->val == KM_PRESS && (block->flag & BLOCK_LOOP)) {
11734 if (block->saferct.first()) {
11735 /* Currently right clicking on a top level pull-down (typically in the header)
11736 * just closes the menu and doesn't support immediately handling the RMB event.
11737 *
11738 * To support we would need RETURN_OUT_PARENT to be handled by
11739 * top-level buttons, not just menus. Note that this isn't very important
11740 * since it's easy to manually close these menus by clicking on them. */
11741 menu->menuretval = (level > 0 && is_parent_inside) ? RETURN_OUT_PARENT :
11742 RETURN_OUT;
11743 }
11744 }
11745 retval = WM_UI_HANDLER_BREAK;
11746 }
11747 break;
11748
11749 /* Closing sub-levels of pull-downs. */
11750 case EVT_LEFTARROWKEY:
11751 if (event->val == KM_PRESS && (block->flag & BLOCK_LOOP)) {
11752 if (block->saferct.first()) {
11753 menu->menuretval = RETURN_OUT;
11754 }
11755 }
11756
11757 retval = WM_UI_HANDLER_BREAK;
11758 break;
11759
11760 /* Opening sub-levels of pull-downs. */
11761 case EVT_RIGHTARROWKEY:
11762 if (event->val == KM_PRESS && (block->flag & BLOCK_LOOP)) {
11763
11764 if (menu_pass_event_to_parent_if_nonactive(menu, but, level, is_parent_menu, retval)) {
11765 break;
11766 }
11767
11768 but = region_find_active_but(region);
11769
11770 if (!but) {
11771 /* no item active, we make first active */
11772 if (block->direction & UI_DIR_UP) {
11773 but = button_last(block);
11774 }
11775 else {
11776 but = button_first(block);
11777 }
11778 }
11779
11780 if (but && ELEM(but->type, ButtonType::Block, ButtonType::Pulldown)) {
11782 }
11783 }
11784
11785 retval = WM_UI_HANDLER_BREAK;
11786 break;
11787
11788 /* Smooth scrolling for popovers. */
11789 case MOUSEPAN: {
11790 if (event->modifier) {
11791 /* pass */
11792 }
11793 else if (block->flag & (BLOCK_CLIPTOP | BLOCK_CLIPBOTTOM)) {
11794 const float dy = event->xy[1] - event->prev_xy[1];
11795 if (dy != 0.0f) {
11796 menu_scroll_apply_offset_y(region, block, dy);
11797
11798 if (but) {
11799 but->active->cancel = true;
11800 button_activate_exit(C, but, but->active, false, false);
11801 }
11803 }
11804 break;
11805 }
11806 else if (!block_is_menu(block)) {
11807 break;
11808 }
11810 }
11811 case WHEELUPMOUSE:
11812 case WHEELDOWNMOUSE: {
11813 if (event->modifier) {
11814 /* pass */
11815 }
11816 else if (block->flag & (BLOCK_CLIPTOP | BLOCK_CLIPBOTTOM)) {
11817 const int scroll_dir = (event->type == WHEELUPMOUSE) ? 1 : -1;
11818 if (menu_scroll_step(region, block, scroll_dir)) {
11819 if (but) {
11820 but->active->cancel = true;
11821 button_activate_exit(C, but, but->active, false, false);
11822 }
11824 }
11825 break;
11826 }
11827 else if (!block_is_menu(block)) {
11828 break;
11829 }
11831 }
11832 case EVT_UPARROWKEY:
11833 case EVT_DOWNARROWKEY:
11834 case EVT_PAGEUPKEY:
11835 case EVT_PAGEDOWNKEY:
11836 case EVT_HOMEKEY:
11837 case EVT_ENDKEY:
11838 /* Arrow-keys: only handle for block_loop blocks. */
11839 if (event->modifier) {
11840 /* pass */
11841 }
11842 else if (inside || (block->flag & BLOCK_LOOP)) {
11843 int type = event->type;
11844 int val = event->val;
11845
11846 /* Convert pan to scroll-wheel. */
11847 if (type == MOUSEPAN) {
11848 pan_to_scroll(event, &type, &val);
11849 }
11850
11851 if (val == KM_PRESS) {
11852 /* Determine scroll operation. */
11853 MenuScrollType scrolltype;
11854
11855 if (ELEM(type, EVT_PAGEUPKEY, EVT_HOMEKEY)) {
11856 scrolltype = MENU_SCROLL_TOP;
11857 }
11858 else if (ELEM(type, EVT_PAGEDOWNKEY, EVT_ENDKEY)) {
11859 scrolltype = MENU_SCROLL_BOTTOM;
11860 }
11861 else if (ELEM(type, EVT_UPARROWKEY, WHEELUPMOUSE)) {
11862 scrolltype = MENU_SCROLL_UP;
11863 }
11864 else {
11865 scrolltype = MENU_SCROLL_DOWN;
11866 }
11867
11868 if (menu_pass_event_to_parent_if_nonactive(menu, but, level, is_parent_menu, retval))
11869 {
11870 break;
11871 }
11872
11873#ifdef USE_KEYNAV_LIMIT
11875#endif
11876
11877 but = region_find_active_but(region);
11878 if (but) {
11879 /* Apply scroll operation. */
11880 if (scrolltype == MENU_SCROLL_DOWN) {
11881 but = button_next(but);
11882 }
11883 else if (scrolltype == MENU_SCROLL_UP) {
11884 but = button_prev(but);
11885 }
11886 else if (scrolltype == MENU_SCROLL_TOP) {
11887 but = button_first(block);
11888 }
11889 else if (scrolltype == MENU_SCROLL_BOTTOM) {
11890 but = button_last(block);
11891 }
11892 }
11893
11894 if (!but) {
11895 /* Wrap button or no active button. */
11896 Button *but_wrap = nullptr;
11897 if (ELEM(scrolltype, MENU_SCROLL_UP, MENU_SCROLL_BOTTOM)) {
11898 but_wrap = button_last(block);
11899 }
11900 else if (ELEM(scrolltype, MENU_SCROLL_DOWN, MENU_SCROLL_TOP)) {
11901 but_wrap = button_first(block);
11902 }
11903 if (but_wrap) {
11904 but = but_wrap;
11905 }
11906 }
11907
11908 if (but) {
11910 menu_scroll_to_but(region, block, but);
11911 }
11912 }
11913
11914 retval = WM_UI_HANDLER_BREAK;
11915 }
11916
11917 break;
11918
11919 case EVT_ONEKEY:
11920 case EVT_PAD1:
11921 act = 1;
11923 case EVT_TWOKEY:
11924 case EVT_PAD2:
11925 if (act == 0) {
11926 act = 2;
11927 }
11929 case EVT_THREEKEY:
11930 case EVT_PAD3:
11931 if (act == 0) {
11932 act = 3;
11933 }
11935 case EVT_FOURKEY:
11936 case EVT_PAD4:
11937 if (act == 0) {
11938 act = 4;
11939 }
11941 case EVT_FIVEKEY:
11942 case EVT_PAD5:
11943 if (act == 0) {
11944 act = 5;
11945 }
11947 case EVT_SIXKEY:
11948 case EVT_PAD6:
11949 if (act == 0) {
11950 act = 6;
11951 }
11953 case EVT_SEVENKEY:
11954 case EVT_PAD7:
11955 if (act == 0) {
11956 act = 7;
11957 }
11959 case EVT_EIGHTKEY:
11960 case EVT_PAD8:
11961 if (act == 0) {
11962 act = 8;
11963 }
11965 case EVT_NINEKEY:
11966 case EVT_PAD9:
11967 if (act == 0) {
11968 act = 9;
11969 }
11971 case EVT_ZEROKEY:
11972 case EVT_PAD0:
11973 if (act == 0) {
11974 act = 10;
11975 }
11976
11977 if ((block->flag & BLOCK_NUMSELECT) && event->val == KM_PRESS) {
11978 int count;
11979
11980 if (menu_pass_event_to_parent_if_nonactive(menu, but, level, is_parent_menu, retval)) {
11981 break;
11982 }
11983
11984 /* Only respond to explicit press to avoid the event that opened the menu
11985 * activating an item when the key is held. */
11986 if (event->flag & WM_EVENT_IS_REPEAT) {
11987 break;
11988 }
11989
11990 if (event->modifier & KM_ALT) {
11991 act += 10;
11992 }
11993
11994 count = 0;
11995 for (Button &but : block->buttons()) {
11996 bool doit = false;
11997
11998 if (!ELEM(but.type,
12003 {
12004 count++;
12005 }
12006
12007 /* exception for rna layer buts */
12008 if (but.rnapoin && but.rnaprop &&
12010 {
12011 if (but.rnaindex == act - 1) {
12012 doit = true;
12013 }
12014 }
12015 else if (ELEM(but.type,
12021 count == act)
12022 {
12023 doit = true;
12024 }
12025
12026 if (!(but.flag & BUT_DISABLED) && doit) {
12027 /* activate buttons but open menu's */
12029 if (but.type == ButtonType::Pulldown) {
12031 }
12032 else {
12034 }
12035
12036 handle_button_activate(C, region, &but, activate);
12037 break;
12038 }
12039 }
12040
12041 retval = WM_UI_HANDLER_BREAK;
12042 }
12043 break;
12044
12045 /* Handle keystrokes on menu items */
12046 case EVT_AKEY:
12047 case EVT_BKEY:
12048 case EVT_CKEY:
12049 case EVT_DKEY:
12050 case EVT_EKEY:
12051 case EVT_FKEY:
12052 case EVT_GKEY:
12053 case EVT_HKEY:
12054 case EVT_IKEY:
12055 case EVT_JKEY:
12056 case EVT_KKEY:
12057 case EVT_LKEY:
12058 case EVT_MKEY:
12059 case EVT_NKEY:
12060 case EVT_OKEY:
12061 case EVT_PKEY:
12062 case EVT_QKEY:
12063 case EVT_RKEY:
12064 case EVT_SKEY:
12065 case EVT_TKEY:
12066 case EVT_UKEY:
12067 case EVT_VKEY:
12068 case EVT_WKEY:
12069 case EVT_XKEY:
12070 case EVT_YKEY:
12071 case EVT_ZKEY:
12072 case EVT_SPACEKEY: {
12073 if (ELEM(event->val, KM_PRESS, KM_DBL_CLICK) && ((event->modifier & ~KM_ALT) == 0) &&
12074 /* Only respond to explicit press to avoid the event that opened the menu
12075 * activating an item when the key is held. */
12076 (event->flag & WM_EVENT_IS_REPEAT) == 0)
12077 {
12078 /* Menu search if space-bar or #MenuTypeFlag::SearchOnKeyPress. */
12079 MenuType *mt = WM_menutype_find(menu->menu_idname, true);
12080 if ((mt && flag_is_set(mt->flag, MenuTypeFlag::SearchOnKeyPress)) ||
12081 event->type == EVT_SPACEKEY)
12082 {
12083 if ((level != 0) && (but == nullptr || !menu->menu_idname[0])) {
12084 /* Search parent if the child is open but not activated or not searchable. */
12086 }
12087 else {
12088 retval = handle_menu_letter_press_search(menu, event);
12089 }
12090 break;
12091 }
12092
12093 if (menu_pass_event_to_parent_if_nonactive(menu, but, level, is_parent_menu, retval)) {
12094 break;
12095 }
12096
12097 /* Accelerator keys that allow "pressing" a menu entry by pressing a single key. */
12098 for (Button &but_iter : block->buttons()) {
12099 if (!(but_iter.flag & BUT_DISABLED) && but_iter.menu_key == event->type) {
12100 if (ELEM(but_iter.type,
12104 {
12105 button_execute(C, region, &but_iter);
12106 }
12107 else {
12108 handle_button_activate_by_type(C, region, &but_iter);
12109 }
12110 retval = WM_UI_HANDLER_BREAK;
12111 break;
12112 }
12113 }
12114 }
12115 default: {
12116 break;
12117 }
12118 }
12119 }
12120 }
12121
12122 /* here we check return conditions for menus */
12123 if (block->flag & BLOCK_LOOP) {
12124 /* If we click outside the block, verify if we clicked on the
12125 * button that opened us, otherwise we need to close,
12126 *
12127 * note that there is an exception for root level menus and
12128 * popups which you can click again to close.
12129 *
12130 * Events handled above may have already set the return value,
12131 * don't overwrite them, see: #61015.
12132 */
12133 if ((inside == false) && (menu->menuretval == 0)) {
12134 SafetyRect *saferct = block->saferct.first();
12135
12136 if (event->type == MOUSEMOVE) {
12137 WM_tooltip_clear(C, win);
12138 }
12139
12140 if (ELEM(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE)) {
12141 if (ELEM(event->val, KM_PRESS, KM_DBL_CLICK)) {
12142 if ((is_parent_menu == false) && (U.uiflag & USER_MENUOPENAUTO) == 0) {
12143 /* for root menus, allow clicking to close */
12144 if (block->flag & BLOCK_OUT_1) {
12145 menu->menuretval = RETURN_OK;
12146 }
12147 else {
12148 menu->menuretval = RETURN_OUT;
12149 }
12150 }
12151 else if (saferct && !BLI_rctf_isect_pt(
12152 &saferct->parent, float(event->xy[0]), float(event->xy[1])))
12153 {
12154 if (block->flag & BLOCK_OUT_1) {
12155 menu->menuretval = RETURN_OK;
12156 }
12157 else {
12159 }
12160 }
12161 }
12162 else if (ELEM(event->val, KM_RELEASE, KM_CLICK)) {
12163 /* For buttons that use a hold function,
12164 * exit when mouse-up outside the menu. */
12165 if (block->flag & BLOCK_POPUP_HOLD) {
12166 /* NOTE: we could check the cursor is over the parent button. */
12167 menu->menuretval = RETURN_CANCEL;
12168 retval = WM_UI_HANDLER_CONTINUE;
12169 }
12170 }
12171 }
12172 }
12173
12174 if (menu->menuretval) {
12175 /* pass */
12176 }
12177#ifdef USE_KEYNAV_LIMIT
12178 else if ((event->type == MOUSEMOVE) && mouse_motion_keynav_test(&menu->keynav_state, event))
12179 {
12180 /* Don't handle the mouse-move if we're using key-navigation. */
12181 retval = WM_UI_HANDLER_BREAK;
12182 }
12183#endif
12184 else if (event->type == EVT_ESCKEY && event->val == KM_PRESS) {
12185 /* Escape cancels this and all preceding menus. */
12186 menu->menuretval = RETURN_CANCEL;
12187 }
12188 else if (ELEM(event->type, EVT_RETKEY, EVT_PADENTER) && event->val == KM_PRESS) {
12189 Button *but_active = region_find_active_but(region);
12191 region, BUT_ACTIVE_DEFAULT, UI_HIDDEN);
12192 if (but_active && menu->keynav_state.is_keynav) {
12193 /* Key-navigation activates the button navigated onto, not the default. */
12194 }
12195 else if ((but_default != nullptr) &&
12196 ((but_default->type == ButtonType::But) &&
12197 ((but_default->active == nullptr) ||
12198 (but_default->active->state == BUTTON_STATE_HIGHLIGHT))))
12199 {
12200 /* Regarding the #BUTTON_STATE_HIGHLIGHT check above.
12201 * It's important to run immediately in this case. Letting the button flash first
12202 * (see #BUTTON_STATE_WAIT_FLASH) delays running it until a timer fires,
12203 * so events after "Return" reach the popup while it's still open and are lost.
12204 * This can happen while typing quickly or a slow redraw.
12205 * It also happens during tests that use simulated events. */
12206
12207 button_execute(C, region, but_default);
12208 retval = WM_UI_HANDLER_BREAK;
12209 }
12210 else if ((but_default != nullptr) && (but_default->active == nullptr)) {
12211 handle_button_activate_by_type(C, region, but_default);
12212 }
12213 /* enter will always close this block, we let the event
12214 * get handled by the button if it is activated, otherwise we cancel */
12215 else if (but_active == nullptr) {
12217 }
12218 }
12219#ifdef USE_DRAG_POPUP
12220 else if ((event->type == LEFTMOUSE) && (event->val == KM_PRESS) &&
12221 (inside && is_floating && inside_title))
12222 {
12223 if (!but || but->type == ButtonType::Image ||
12224 !button_contains_point_px(but, region, event->xy))
12225 {
12226 if (but) {
12228 }
12229
12230 menu->is_grab = true;
12231 copy_v2_v2_int(menu->grab_xy_prev, event->xy);
12232 retval = WM_UI_HANDLER_BREAK;
12233 }
12234 }
12235#endif
12236 else {
12237
12238 /* check mouse moving outside of the menu */
12239 if (inside == false && (block->flag & (BLOCK_MOVEMOUSE_QUIT | BLOCK_POPOVER))) {
12240 SafetyRect *saferct;
12241
12242 mouse_motion_towards_check(block, menu, event->xy, is_parent_inside == false);
12243
12244 /* Check for all parent rects, enables arrow-keys to be used. */
12245 for (saferct = block->saferct.first(); saferct; saferct = saferct->next) {
12246 /* for mouse move we only check our own rect, for other
12247 * events we check all preceding block rects too to make
12248 * arrow keys navigation work */
12249 if (event->type != MOUSEMOVE || saferct == block->saferct.first()) {
12250 if (BLI_rctf_isect_pt(&saferct->parent, float(event->xy[0]), float(event->xy[1]))) {
12251 break;
12252 }
12253 if (BLI_rctf_isect_pt(&saferct->safety, float(event->xy[0]), float(event->xy[1]))) {
12254 break;
12255 }
12256 }
12257 }
12258
12259 /* strict check, and include the parent rect */
12260 if (!menu->dotowards && !saferct && ((U.flag & USER_MENU_CLOSE_LEAVE) || level > 0) &&
12261 !(menu->mmb_panning || menu->keep_open_timer))
12262 {
12263 if (block->flag & BLOCK_OUT_1) {
12264 menu->menuretval = RETURN_OK;
12265 }
12266 else {
12267 menu->menuretval = RETURN_OUT;
12268 }
12269 }
12270 else if (menu->dotowards && event->type == MOUSEMOVE) {
12271 retval = WM_UI_HANDLER_BREAK;
12272 }
12273 }
12274 }
12275
12276 /* end switch */
12277 }
12278 }
12279
12280 /* if we are didn't handle the event yet, lets pass it on to
12281 * buttons inside this region. disabled inside check .. not sure
12282 * anymore why it was there? but it meant enter didn't work
12283 * for example when mouse was not over submenu */
12284 if ((event->type == TIMER) ||
12285 (/* inside && */ (!menu->menuretval || (menu->menuretval & RETURN_UPDATE)) &&
12286 retval == WM_UI_HANDLER_CONTINUE))
12287 {
12288 retval = handle_menu_button(C, event, menu);
12289 }
12290
12291#ifdef USE_UI_POPOVER_ONCE
12292 if (block->flag & BLOCK_POPOVER_ONCE) {
12293 if ((event->type == LEFTMOUSE) && (event->val == KM_RELEASE)) {
12294 popover_once_clear(static_cast<Popover *>(menu->popup_create_vars.arg));
12295 block->flag &= ~BLOCK_POPOVER_ONCE;
12296 }
12297 }
12298#endif
12299
12300 /* Don't handle double click events, re-handle as regular press/release. */
12301 if (retval == WM_UI_HANDLER_CONTINUE && event->val == KM_DBL_CLICK) {
12302 return retval;
12303 }
12304
12305 /* if we set a menu return value, ensure we continue passing this on to
12306 * lower menus and buttons, so always set continue then, and if we are
12307 * inside the region otherwise, ensure we swallow the event */
12308 if (menu->menuretval) {
12310 }
12311 if (inside) {
12312 return WM_UI_HANDLER_BREAK;
12313 }
12314 return retval;
12315}
12316
12318{
12319 ARegion *region = menu->region;
12320 Block *block = region->runtime->uiblocks.first();
12321
12322 Button *but = region_find_active_but(region);
12323
12324 BLI_assert(but);
12325
12327 PopupBlockHandle *submenu = data->menu;
12328
12329 if (submenu->menuretval) {
12330 bool update;
12331
12332 /* first decide if we want to close our own menu cascading, if
12333 * so pass on the sub menu return value to our own menu handle */
12334 if ((submenu->menuretval & RETURN_OK) || (submenu->menuretval & RETURN_CANCEL)) {
12335 if (!(block->flag & BLOCK_KEEP_OPEN)) {
12336 menu->menuretval = submenu->menuretval;
12337 }
12338 }
12339
12340 update = (submenu->menuretval & RETURN_UPDATE) != 0;
12341
12342 /* now let activated button in this menu exit, which
12343 * will actually close the submenu too */
12344 handle_button_return_submenu(C, event, but);
12345
12346 if (update) {
12347 submenu->menuretval = 0;
12348 }
12349 }
12350
12351 if (block->flag & (BLOCK_MOVEMOUSE_QUIT | BLOCK_POPOVER)) {
12352 /* for cases where close does not cascade, allow the user to
12353 * move the mouse back towards the menu without closing */
12354 mouse_motion_towards_reinit(menu, event->xy);
12355 }
12356
12357 if (menu->menuretval) {
12359 }
12360 return WM_UI_HANDLER_BREAK;
12361}
12362
12364{
12366}
12367
12368static int but_pie_menu_apply(bContext *C, PopupBlockHandle *menu, Button *but, bool force_close)
12369{
12370 const int retval = WM_UI_HANDLER_BREAK;
12371
12372 if (but && but_pie_menu_supported_apply(but)) {
12373 if (but->type == ButtonType::Menu) {
12374 /* forcing the pie menu to close will not handle menus */
12375 if (!force_close) {
12376 Button *active_but = region_find_active_but(menu->region);
12377
12378 if (active_but) {
12379 button_activate_exit(C, active_but, active_but->active, false, false);
12380 }
12381
12383 return retval;
12384 }
12385 menu->menuretval = RETURN_CANCEL;
12386 }
12387 else {
12388 button_activate_exit(C, but, but->active, false, false);
12389
12390 menu->menuretval = RETURN_OK;
12391 }
12392 }
12393 else {
12394 menu->menuretval = RETURN_CANCEL;
12395
12397 }
12398
12399 return retval;
12400}
12401
12403{
12404 if ((block->flag & BLOCK_NUMSELECT) && event->val == KM_PRESS) {
12405 for (Button &but : block->buttons()) {
12406 if (but.pie_dir == dir && !ELEM(but.type, ButtonType::Sepr, ButtonType::SeprLine)) {
12407 return &but;
12408 }
12409 }
12410 }
12411
12412 return nullptr;
12413}
12414
12416{
12417 if (but == nullptr) {
12418 return WM_UI_HANDLER_BREAK;
12419 }
12420
12421 Button *active_but = region_find_active_but(menu->region);
12422
12423 if (active_but) {
12424 /* Use onfree to not execute the hovered active_but. */
12425 button_activate_exit(C, active_but, active_but->active, false, true);
12426 }
12427
12429 return but_pie_menu_apply(C, menu, but, false);
12430}
12431
12432static int pie_handler(bContext *C, const wmEvent *event, PopupBlockHandle *menu)
12433{
12434 /* we block all events, this is modal interaction,
12435 * except for drop events which is described below */
12436 int retval = WM_UI_HANDLER_BREAK;
12437
12438 if (event->type == EVT_DROP) {
12439 /* may want to leave this here for later if we support pie ovens */
12440
12441 retval = WM_UI_HANDLER_CONTINUE;
12442 }
12443
12444 ARegion *region = menu->region;
12445 Block *block = region->runtime->uiblocks.first();
12446
12447 const bool is_click_style = (block->pie_data->flags & PIE_CLICK_STYLE);
12448
12449 /* if there's an active modal button, don't check events or outside, except for search menu */
12450 Button *but_active = region_find_active_but(region);
12451
12452 if (menu->scrolltimer == nullptr) {
12455 menu->scrolltimer->time_duration = 0.0;
12456 }
12457
12458 const double duration = menu->scrolltimer->time_duration;
12459
12460 float event_xy[2] = {float(event->xy[0]), float(event->xy[1])};
12461
12462 window_to_block_fl(region, block, &event_xy[0], &event_xy[1]);
12463
12464 /* Distance from initial point. */
12465 const float dist = block_calc_pie_segment(block, event_xy);
12466
12467 if (but_active && button_modal_state(but_active->active->state)) {
12468 retval = handle_menu_button(C, event, menu);
12469 }
12470 else {
12471 if (event->type == TIMER) {
12472 if (event->customdata == menu->scrolltimer) {
12473 /* deactivate initial direction after a while */
12474 if (duration > 0.01 * U.pie_initial_timeout) {
12475 block->pie_data->flags &= ~PIE_INITIAL_DIRECTION;
12476 }
12477
12478 /* handle animation */
12479 if (!(block->pie_data->flags & PIE_ANIMATION_FINISHED)) {
12480 const bool animate = !(U.uiflag & USER_REDUCE_MOTION) && U.pie_animation_timeout;
12481 float fac = animate ? duration / (0.01f * double(U.pie_animation_timeout)) : 1.0f;
12482 if (fac >= 1.0f) {
12483 fac = 1.0f;
12484 block->pie_data->flags |= PIE_ANIMATION_FINISHED;
12485 }
12486
12487 const float pie_radius = U.pie_menu_radius * UI_SCALE_FAC;
12488
12489 for (Button &but : block->buttons()) {
12490 if (but.pie_dir != UI_RADIAL_NONE) {
12491 float vec[2];
12492 float center[2];
12493
12494 button_pie_dir(but.pie_dir, vec);
12495
12496 center[0] = (vec[0] > 0.01f) ? 0.5f : ((vec[0] < -0.01f) ? -0.5f : 0.0f);
12497 center[1] = (vec[1] > 0.99f) ? 0.5f : ((vec[1] < -0.99f) ? -0.5f : 0.0f);
12498
12499 center[0] *= BLI_rctf_size_x(&but.rect);
12500 center[1] *= BLI_rctf_size_y(&but.rect);
12501
12502 mul_v2_fl(vec, pie_radius);
12503 add_v2_v2(vec, center);
12504 mul_v2_fl(vec, fac);
12505 add_v2_v2(vec, block->pie_data->pie_center_spawned);
12506
12507 BLI_rctf_recenter(&but.rect, vec[0], vec[1]);
12508 }
12509 }
12510 block->pie_data->alphafac = fac;
12511
12512 ED_region_tag_redraw(region);
12513 }
12514 }
12515
12516 /* Check pie velocity here if gesture has ended. */
12517 if (block->pie_data->flags & PIE_GESTURE_END_WAIT) {
12518 float len_sq = 10;
12519
12520 /* use a time threshold to ensure we leave time to the mouse to move */
12521 if (duration - block->pie_data->duration_gesture > 0.02) {
12522 len_sq = len_squared_v2v2(event_xy, block->pie_data->last_pos);
12523 copy_v2_v2(block->pie_data->last_pos, event_xy);
12524 block->pie_data->duration_gesture = duration;
12525 }
12526
12527 if (len_sq < 1.0f) {
12528 Button *but = region_find_active_but(menu->region);
12529
12530 if (but) {
12531 return but_pie_menu_apply(C, menu, but, true);
12532 }
12533 }
12534 }
12535 }
12536
12537 if (event->type == block->pie_data->event_type && !is_click_style) {
12538 if (event->val != KM_RELEASE) {
12539 handle_menu_button(C, event, menu);
12540
12541 if (len_squared_v2v2(event_xy, block->pie_data->pie_center_init) > PIE_CLICK_THRESHOLD_SQ)
12542 {
12543 block->pie_data->flags |= PIE_DRAG_STYLE;
12544 }
12545 /* why redraw here? It's simple, we are getting many double click events here.
12546 * Those operate like mouse move events almost */
12547 ED_region_tag_redraw(region);
12548 }
12549 else {
12550 if ((duration < 0.01 * U.pie_tap_timeout) && !(block->pie_data->flags & PIE_DRAG_STYLE)) {
12551 block->pie_data->flags |= PIE_CLICK_STYLE;
12552 }
12553 else {
12554 Button *but = region_find_active_but(menu->region);
12555
12556 if (but && (U.pie_menu_confirm > 0) &&
12557 (dist >= UI_SCALE_FAC * (U.pie_menu_threshold + U.pie_menu_confirm)))
12558 {
12559 return but_pie_menu_apply(C, menu, but, true);
12560 }
12561
12562 retval = but_pie_menu_apply(C, menu, but, true);
12563 }
12564 }
12565 }
12566 else {
12567 /* direction from numpad */
12569
12570 switch (event->type) {
12571 case MOUSEMOVE:
12572 if (!is_click_style) {
12573 const float len_sq = len_squared_v2v2(event_xy, block->pie_data->pie_center_init);
12574
12575 /* here we use the initial position explicitly */
12576 if (len_sq > PIE_CLICK_THRESHOLD_SQ) {
12577 block->pie_data->flags |= PIE_DRAG_STYLE;
12578 }
12579
12580 /* here instead, we use the offset location to account for the initial
12581 * direction timeout */
12582 if ((U.pie_menu_confirm > 0) &&
12583 (dist >= UI_SCALE_FAC * (U.pie_menu_threshold + U.pie_menu_confirm)))
12584 {
12585 block->pie_data->flags |= PIE_GESTURE_END_WAIT;
12586 copy_v2_v2(block->pie_data->last_pos, event_xy);
12587 block->pie_data->duration_gesture = duration;
12588 }
12589 }
12590
12591 handle_menu_button(C, event, menu);
12592
12593 /* mouse move should always refresh the area for pie menus */
12594 ED_region_tag_redraw(region);
12595 break;
12596
12597 case LEFTMOUSE:
12598 if (is_click_style) {
12599 if (block->pie_data->flags & PIE_INVALID_DIR) {
12600 menu->menuretval = RETURN_CANCEL;
12601 }
12602 else {
12603 retval = handle_menu_button(C, event, menu);
12604 }
12605 }
12606 break;
12607
12608 case WINDEACTIVATE: {
12609 /* Prevent waiting for the pie key release if it was released outside of focus. */
12610 wmWindow *win = CTX_wm_window(C);
12611 if (win) {
12613 }
12614 menu->menuretval = RETURN_CANCEL;
12615 break;
12616 }
12617
12618 case EVT_ESCKEY:
12619 case RIGHTMOUSE:
12620 menu->menuretval = RETURN_CANCEL;
12621 break;
12622
12623 case EVT_AKEY:
12624 case EVT_BKEY:
12625 case EVT_CKEY:
12626 case EVT_DKEY:
12627 case EVT_EKEY:
12628 case EVT_FKEY:
12629 case EVT_GKEY:
12630 case EVT_HKEY:
12631 case EVT_IKEY:
12632 case EVT_JKEY:
12633 case EVT_KKEY:
12634 case EVT_LKEY:
12635 case EVT_MKEY:
12636 case EVT_NKEY:
12637 case EVT_OKEY:
12638 case EVT_PKEY:
12639 case EVT_QKEY:
12640 case EVT_RKEY:
12641 case EVT_SKEY:
12642 case EVT_TKEY:
12643 case EVT_UKEY:
12644 case EVT_VKEY:
12645 case EVT_WKEY:
12646 case EVT_XKEY:
12647 case EVT_YKEY:
12648 case EVT_ZKEY: {
12649 if (ELEM(event->val, KM_PRESS, KM_DBL_CLICK) && ((event->modifier & ~KM_ALT) == 0)) {
12650 for (Button &but : block->buttons()) {
12651 if (but.menu_key == event->type) {
12652 but_pie_button_activate(C, &but, menu);
12653 }
12654 }
12655 }
12656 break;
12657 }
12658
12659#define CASE_NUM_TO_DIR(n, d) \
12660 case (EVT_ZEROKEY + n): \
12661 case (EVT_PAD0 + n): { \
12662 if (num_dir == UI_RADIAL_NONE) { \
12663 num_dir = d; \
12664 } \
12665 } \
12666 (void)0
12667
12683 {
12684 Button *but = block_pie_dir_activate(block, event, num_dir);
12685 retval = but_pie_button_activate(C, but, menu);
12686 break;
12687 }
12688#undef CASE_NUM_TO_DIR
12689 default:
12690 retval = handle_menu_button(C, event, menu);
12691 break;
12692 }
12693 }
12694 }
12695
12696 return retval;
12697}
12698
12700 const wmEvent *event,
12701 PopupBlockHandle *menu,
12702 int level,
12703 const bool is_parent_inside,
12704 const bool is_parent_menu,
12705 const bool is_floating)
12706{
12707 int retval = WM_UI_HANDLER_CONTINUE;
12708 bool do_towards_reinit = false;
12709
12710 /* check if we have a submenu, and handle events for it first */
12711 Button *but = region_find_active_but(menu->region);
12712 HandleButtonData *data = (but) ? but->active : nullptr;
12713 PopupBlockHandle *submenu = (data) ? data->menu : nullptr;
12714
12715 if (submenu) {
12716 Block *block = menu->region->runtime->uiblocks.first();
12717 const bool is_menu = block_is_menu(block);
12718 bool inside = false;
12719 /* root pie menus accept the key that spawned
12720 * them as double click to improve responsiveness */
12721 const bool do_recursion = (!(block->flag & BLOCK_PIE_MENU) ||
12722 event->type != block->pie_data->event_type);
12723
12724 if (do_recursion) {
12725 if (is_parent_inside == false) {
12726 int mx = event->xy[0];
12727 int my = event->xy[1];
12728 window_to_block(menu->region, block, &mx, &my);
12729 inside = BLI_rctf_isect_pt(&block->rect, mx, my);
12730 }
12731
12732 retval = handle_menus_recursive(
12733 C, event, submenu, level + 1, is_parent_inside || inside, is_menu, false);
12734 }
12735 }
12736 else if (!but && event->val == KM_PRESS && event->type == LEFTMOUSE) {
12737 for (Block &block : menu->region->runtime->uiblocks) {
12738 if (block.panel) {
12739 int mx = event->xy[0];
12740 int my = event->xy[1];
12741 window_to_block(menu->region, &block, &mx, &my);
12742 if (!IN_RANGE(float(mx), block.rect.xmin, block.rect.xmax)) {
12743 break;
12744 }
12746 if (header) {
12749 ARegion *prev_region_popup = CTX_wm_region_popup(C);
12750 /* Set the current context popup region so the handler context can access to it. */
12753 /* Restore previous popup region. */
12754 CTX_wm_region_popup_set(C, prev_region_popup);
12755 retval = WM_UI_HANDLER_BREAK;
12756 }
12757 }
12758 }
12759 }
12760
12761 /* now handle events for our own menu */
12762
12763 if (retval == WM_UI_HANDLER_CONTINUE) {
12764 retval = handle_region_semi_modal_buttons(C, event, menu->region);
12765 }
12766
12767 if (retval == WM_UI_HANDLER_CONTINUE || event->type == TIMER) {
12768 const bool do_but_search = (but && (but->type == ButtonType::SearchMenu));
12769 if (submenu && submenu->menuretval) {
12770 const bool do_ret_out_parent = (submenu->menuretval & RETURN_OUT_PARENT) != 0;
12771 retval = handle_menu_return_submenu(C, event, menu);
12772 submenu = nullptr; /* hint not to use this, it may be freed by call above */
12773 (void)submenu;
12774 /* we may want to quit the submenu and handle the even in this menu,
12775 * if its important to use it, check 'data->menu' first */
12776 if (((retval == WM_UI_HANDLER_BREAK) && do_ret_out_parent) == false) {
12777 /* skip applying the event */
12778 return retval;
12779 }
12780 }
12781
12782 if (do_but_search) {
12783 Block *block = menu->region->runtime->uiblocks.first();
12784
12785 retval = handle_menu_button(C, event, menu);
12786
12787 if (block->flag & (BLOCK_MOVEMOUSE_QUIT | BLOCK_POPOVER)) {
12788 /* when there is a active search button and we close it,
12789 * we need to reinit the mouse coords #35346. */
12790 if (region_find_active_but(menu->region) != but) {
12791 do_towards_reinit = true;
12792 }
12793 }
12794 }
12795 else {
12796 Block *block = menu->region->runtime->uiblocks.first();
12797
12798 if (block->flag & BLOCK_PIE_MENU) {
12799 retval = pie_handler(C, event, menu);
12800 }
12801 else if (event->type == LEFTMOUSE || event->val != KM_DBL_CLICK) {
12802 bool handled = false;
12803 const Button *active_but = region_find_active_but(menu->region);
12804 const bool is_actbut_in_modal_state = active_but &&
12805 button_modal_state(active_but->active->state);
12806 /* Handle uilist events if there not an active button in modal state. */
12807 if (Button *listbox = listbox_find_mouse_over(menu->region, event);
12808 listbox && !is_actbut_in_modal_state)
12809 {
12810 const int retval_test = handle_uilist_event(C, event, menu->region, listbox);
12811 if (retval_test != WM_UI_HANDLER_CONTINUE) {
12812 retval = retval_test;
12813 handled = true;
12814 }
12815 }
12816
12817 if (handled == false) {
12818 retval = handle_menu_event(
12819 C, event, menu, level, is_parent_inside, is_parent_menu, is_floating);
12820 }
12821 }
12822 }
12823 }
12824
12825 if (!menu->retvalue) {
12826 handle_viewlist_items_hover(event, menu->region);
12827 }
12828 /* Handle mouse clicks on overlapping view item button. */
12829 handle_view_item_event(C, event, but, menu->region);
12830
12831 if (do_towards_reinit) {
12832 mouse_motion_towards_reinit(menu, event->xy);
12833 }
12834
12835 return retval;
12836}
12837
12838void popup_menu_retval_set(const Block *block, const int retval, const bool enable)
12839{
12840 PopupBlockHandle *menu = block->handle;
12841 if (menu) {
12842 menu->menuretval = enable ? (menu->menuretval | retval) : (menu->menuretval & retval);
12843 }
12844}
12845
12847
12848/* -------------------------------------------------------------------- */
12851
12852static int region_handler(bContext *C, const wmEvent *event, void * /*userdata*/)
12853{
12854 /* here we handle buttons at the region level, non-modal */
12855 ARegion *region = CTX_wm_region(C);
12856 int retval = WM_UI_HANDLER_CONTINUE;
12857
12858 if (region == nullptr || region->runtime->uiblocks.is_empty()) {
12859 return retval;
12860 }
12861
12862 /* either handle events for already activated button or try to activate */
12863 Button *but = region_find_active_but(region);
12864 Button *listbox = listbox_find_mouse_over(region, event);
12865
12866 retval = handler_panel_region(C, event, region, listbox ? listbox : but);
12867
12868 if (retval == WM_UI_HANDLER_CONTINUE && listbox) {
12869 retval = handle_uilist_event(C, event, region, listbox);
12870
12871 /* interactions with the listbox should disable tips */
12872 if (retval == WM_UI_HANDLER_BREAK) {
12873 if (but) {
12875 }
12876 }
12877 }
12878
12879 if (retval == WM_UI_HANDLER_CONTINUE) {
12880 retval = handle_region_semi_modal_buttons(C, event, region);
12881 }
12882
12883 if (retval == WM_UI_HANDLER_CONTINUE) {
12884 if (but) {
12885 retval = handle_button_event(C, event, but);
12886 }
12887 else {
12888 retval = handle_button_over(C, event, region);
12889 }
12890 }
12891
12892 /* Re-enable tool-tips. */
12893 if (event->type == MOUSEMOVE &&
12894 (event->xy[0] != event->prev_xy[0] || event->xy[1] != event->prev_xy[1]))
12895 {
12896 blocks_set_tooltips(region, true);
12897 }
12898
12899 /* Always do this, to reliably update view and UI-list item highlighting, even if
12900 * the mouse hovers a button nested in the item (it's an overlapping layout). */
12901 handle_viewlist_items_hover(event, region);
12902 if (retval == WM_UI_HANDLER_CONTINUE) {
12903 retval = handle_view_item_event(C, event, but, region);
12904 }
12905
12906 /* delayed apply callbacks */
12908
12909 return retval;
12910}
12911
12912static void region_handler_remove(bContext *C, void * /*userdata*/)
12913{
12914 ARegion *region = CTX_wm_region(C);
12915 if (region == nullptr) {
12916 return;
12917 }
12918
12919 blocklist_free(C, region);
12920 bScreen *screen = CTX_wm_screen(C);
12921 if (screen == nullptr) {
12922 return;
12923 }
12924
12925 /* delayed apply callbacks, but not for screen level regions, those
12926 * we rather do at the very end after closing them all, which will
12927 * be done in region_handler/window */
12928 if (BLI_findindex(&screen->regionbase, region) == -1) {
12930 }
12931}
12932
12933static int handle_region_semi_modal_buttons(bContext *C, const wmEvent *event, ARegion *region)
12934{
12935 /* If there's a fully modal button, it has priority. */
12936 if (const Button *active_but = region_find_active_but(region)) {
12937 BLI_assert(active_but->semi_modal_state == nullptr);
12938 if (button_modal_state(active_but->active->state)) {
12940 }
12941 }
12942
12943 int retval = WM_UI_HANDLER_CONTINUE;
12944
12945 foreach_semi_modal_but_as_active(C, region, [&](Button *semi_modal_but) {
12946 if (retval == WM_UI_HANDLER_CONTINUE) {
12947 retval = handle_button_event(C, event, semi_modal_but);
12948 }
12949 });
12950
12951 return retval;
12952}
12953
12954/* Return true if we should open another menu while one is already open. */
12955static bool can_activate_other_menu(Button *but, Button *but_other, const wmEvent *event)
12956{
12957 if (but == but_other || but_other->flag & BUT_DISABLED || but_other->menu_no_hover_open) {
12958 return false;
12959 }
12960
12962 return false;
12963 }
12964
12966 return false;
12967 }
12968
12970 if (!(data->menu->direction & (UI_DIR_DOWN | UI_DIR_UP))) {
12971 return true;
12972 }
12973
12974 if (data->menu && data->menu->region &&
12975 (BLI_rcti_size_x(&data->menu->region->winrct) > (600.0f * UI_SCALE_FAC)))
12976 {
12977 /* If the open menu is super wide then don't switch to any neighbors. */
12978 return false;
12979 }
12980
12981 /* Prevent menus from being closed while using middle mouse button panning. */
12982 if (data->menu && data->menu->region) {
12983 PopupBlockHandle *submenu = data->menu;
12984 while (submenu) {
12985 if (submenu->mmb_panning || submenu->keep_open_timer) {
12986 return false;
12987 }
12988 Button *but = region_find_active_but(submenu->region);
12989 HandleButtonData *data = (but) ? but->active : nullptr;
12990 submenu = (data) ? data->menu : nullptr;
12991 }
12992 }
12993
12994 float safety = 4.0f * UI_SCALE_FAC;
12995 if (!but_other->str.empty()) {
12996 safety += 4.0f * UI_SCALE_FAC;
12997 }
12998
12999 float left, right;
13000 if (but_other->rect.xmin < but->rect.xmin) {
13001 /* Right to Left. */
13002 if (but->rect.xmin - but_other->rect.xmax > (24.0f * UI_SCALE_FAC)) {
13003 /* If they are far enough part just switch. */
13004 return true;
13005 }
13006 right = but->rect.xmax;
13007 left = but_other->rect.xmax;
13008 if (ELEM(but_other->type, ButtonType::Popover, ButtonType::Menu)) {
13009 /* Skip the drop-down arrow on the right of it. */
13010 safety += 8.0f * UI_SCALE_FAC;
13011 }
13012 left -= safety;
13013 }
13014 else {
13015 /* Left to Right. */
13016 if (but_other->rect.xmin - but->rect.xmax > (24.0f * UI_SCALE_FAC)) {
13017 /* If they are far enough part just switch. */
13018 return true;
13019 }
13020 left = but->rect.xmin;
13021 right = but_other->rect.xmin;
13022 if (but_other->icon && !but_other->str.empty()) {
13023 /* Skip the icon on the left of it. */
13024 safety += 16.0f * UI_SCALE_FAC;
13025 }
13026 right += safety;
13027 }
13028
13029 return (event->mval[0] < int(left) || event->mval[0] > int(right));
13030}
13031
13032/* handle buttons at the window level, modal, for example while
13033 * number sliding, text editing, or when a menu block is open */
13034static int handler_region_menu(bContext *C, const wmEvent *event, void * /*userdata*/)
13035{
13036 ARegion *region_popup = CTX_wm_region_popup(C);
13037 ARegion *region = region_popup ? region_popup : CTX_wm_region(C);
13038 int retval = WM_UI_HANDLER_CONTINUE;
13039
13040 Button *but = region_find_active_but(region);
13041
13042 if (but) {
13043 bScreen *screen = CTX_wm_screen(C);
13044 Button *but_other;
13045
13046 /* handle activated button events */
13048
13049 if ((data->state == BUTTON_STATE_MENU_OPEN) &&
13050 /* Make sure this popup isn't dragging a button.
13051 * can happen with popovers (see #67882). */
13052 (region_find_active_but(data->menu->region) == nullptr) &&
13053 /* make sure mouse isn't inside another menu (see #43247) */
13054 (screen_region_find_mouse_over(screen, event) == nullptr) &&
13055 (but_other = but_find_mouse_over(region, event)) &&
13056 can_activate_other_menu(but, but_other, event) &&
13057 /* Hover-opening menu's doesn't work well for buttons over one another
13058 * along the same axis the menu is opening on (see #71719). */
13059 (((data->menu->direction & (UI_DIR_LEFT | UI_DIR_RIGHT)) &&
13060 BLI_rctf_isect_rect_x(&but->rect, &but_other->rect, nullptr)) ||
13061 ((data->menu->direction & (UI_DIR_DOWN | UI_DIR_UP)) &&
13062 BLI_rctf_isect_rect_y(&but->rect, &but_other->rect, nullptr))))
13063 {
13064 /* if mouse moves to a different root-level menu button,
13065 * open it to replace the current menu */
13066 handle_button_activate(C, region, but_other, BUTTON_ACTIVATE_OVER);
13068 retval = WM_UI_HANDLER_BREAK;
13069 }
13070 else if (data->state == BUTTON_STATE_MENU_OPEN) {
13071 /* handle events for menus and their buttons recursively,
13072 * this will handle events from the top to the bottom menu */
13073 if (data->menu) {
13074 retval = handle_menus_recursive(C, event, data->menu, 0, false, false, false);
13075 }
13076
13077 /* handle events for the activated button */
13078 if ((data->menu && (retval == WM_UI_HANDLER_CONTINUE)) || (event->type == TIMER)) {
13079 if (data->menu && data->menu->menuretval) {
13080 handle_button_return_submenu(C, event, but);
13081 retval = WM_UI_HANDLER_BREAK;
13082 }
13083 else {
13084 retval = handle_button_event(C, event, but);
13085 }
13086 }
13087 }
13088 else {
13089 /* handle events for the activated button */
13090 retval = handle_button_event(C, event, but);
13091 }
13092 }
13093
13094 /* Re-enable tool-tips. */
13095 if (event->type == MOUSEMOVE &&
13096 (event->xy[0] != event->prev_xy[0] || event->xy[1] != event->prev_xy[1]))
13097 {
13098 blocks_set_tooltips(region, true);
13099 }
13100
13101 if (but && but->active && but->active->menu) {
13102 /* Set correct context popup-region. The handling button above breaks if we set the region
13103 * first, so only set it for executing the #uiAfterFunc. */
13105 }
13106
13107 /* delayed apply callbacks */
13109
13110 /* Reset to previous context region. */
13111 CTX_wm_region_popup_set(C, region_popup);
13112
13113 /* Don't handle double-click events,
13114 * these will be converted into regular clicks which we handle. */
13115 if (retval == WM_UI_HANDLER_CONTINUE) {
13116 if (event->val == KM_DBL_CLICK) {
13118 }
13119 }
13120
13121 /* we block all events, this is modal interaction */
13122 return WM_UI_HANDLER_BREAK;
13123}
13124
13125/* two types of popups, one with operator + enum, other with regular callbacks */
13126static int popup_handler(bContext *C, const wmEvent *event, void *userdata)
13127{
13128 PopupBlockHandle *menu = static_cast<PopupBlockHandle *>(userdata);
13129 /* we block all events, this is modal interaction,
13130 * except for drop events which is described below */
13131 int retval = WM_UI_HANDLER_BREAK;
13132 bool reset_pie = false;
13133
13134 ARegion *region_popup = CTX_wm_region_popup(C);
13136
13137 if (event->type == EVT_DROP || event->val == KM_DBL_CLICK) {
13138 /* EVT_DROP:
13139 * If we're handling drop event we'll want it to be handled by popup callee as well,
13140 * so it'll be possible to perform such operations as opening .blend files by dropping
13141 * them into blender, even if there's opened popup like splash screen (sergey).
13142 * KM_DBL_CLICK:
13143 * Continue in case of double click so wm_handlers_do calls handler again with KM_PRESS
13144 * event. This is needed to ensure correct button handling for fast clicking (#47532).
13145 */
13146
13147 retval = WM_UI_HANDLER_CONTINUE;
13148 }
13149
13150 handle_menus_recursive(C, event, menu, 0, false, false, true);
13151
13152 /* free if done, does not free handle itself */
13153 if (menu->menuretval) {
13154 wmWindow *win = CTX_wm_window(C);
13155 /* copy values, we have to free first (closes region) */
13156 const PopupBlockHandle temp = *menu;
13157 Block *block = menu->region->runtime->uiblocks.first();
13158
13159 /* set last pie event to allow chained pie spawning */
13160 if (block->flag & BLOCK_PIE_MENU) {
13161 win->pie_event_type_last = block->pie_data->event_type;
13162 reset_pie = true;
13163 }
13164
13165 popup_block_free(C, menu);
13167 CTX_wm_region_popup_set(C, nullptr);
13168
13169#ifdef USE_DRAG_TOGGLE
13170 {
13172 &win->runtime->modalhandlers,
13175 }
13176#endif
13177
13178 if ((temp.menuretval & RETURN_OK) || (temp.menuretval & RETURN_POPUP_OK)) {
13179 if (temp.popup_func) {
13180 temp.popup_func(C, temp.popup_arg, temp.retvalue);
13181 }
13182 }
13183 else if (temp.cancel_func) {
13184 temp.cancel_func(C, temp.popup_arg);
13185 }
13186
13188 }
13189 else {
13190 /* Re-enable tool-tips */
13191 if (event->type == MOUSEMOVE &&
13192 (event->xy[0] != event->prev_xy[0] || event->xy[1] != event->prev_xy[1]))
13193 {
13194 blocks_set_tooltips(menu->region, true);
13195 }
13196 }
13197
13198 /* delayed apply callbacks */
13200
13201 if (reset_pie) {
13202 /* Reacquire window in case pie invalidates it somehow. */
13203 wmWindow *win = CTX_wm_window(C);
13204
13205 if (win) {
13207 }
13208 }
13209
13210 CTX_wm_region_set(C, region_popup);
13211
13212 return retval;
13213}
13214
13215static void popup_handler_remove(bContext *C, void *userdata)
13216{
13217 PopupBlockHandle *menu = static_cast<PopupBlockHandle *>(userdata);
13218
13219 /* More correct would be to expect RETURN_CANCEL here, but not wanting to
13220 * cancel when removing handlers because of file exit is a rare exception.
13221 * So instead of setting cancel flag for all menus before removing handlers,
13222 * just explicitly flag menu with RETURN_OK to avoid canceling it. */
13223 if ((menu->menuretval & RETURN_OK) == 0 && menu->cancel_func) {
13224 menu->cancel_func(C, menu->popup_arg);
13225 }
13226
13227 /* free menu block if window is closed for some reason */
13228 popup_block_free(C, menu);
13229
13230 /* delayed apply callbacks */
13232}
13233
13234void region_handlers_add(ListBaseT<wmEventHandler> *handlers)
13235{
13238 nullptr, handlers, region_handler, region_handler_remove, nullptr, eWM_EventHandlerFlag(0));
13239}
13240
13242 ListBaseT<wmEventHandler> *handlers,
13243 PopupBlockHandle *popup,
13244 const char flag)
13245{
13246#ifdef WITH_INPUT_IME
13247 /* A popup is modal, end the region's IME session so key presses reach the popup.
13248 * Button owned sessions are kept, the popup may belong to the button being edited
13249 * (a search menu for e.g.), see #wmIMEOwnerType. */
13250 if (wmWindow *win = CTX_wm_window(C)) {
13251 if (win->runtime->ime_owner == bke::wmIMEOwnerType::Region) {
13252 WM_window_IME_end(win);
13253 }
13254 }
13255#endif
13256
13258 C, handlers, popup_handler, popup_handler_remove, popup, eWM_EventHandlerFlag(flag));
13259}
13260
13261void popup_handlers_remove(ListBaseT<wmEventHandler> *handlers, PopupBlockHandle *popup)
13262{
13263 for (wmEventHandler &handler_base : *handlers) {
13264 if (handler_base.type == WM_HANDLER_TYPE_UI) {
13265 wmEventHandler_UI *handler = reinterpret_cast<wmEventHandler_UI *>(&handler_base);
13266
13267 if (handler->handle_fn == popup_handler && handler->remove_fn == popup_handler_remove &&
13268 handler->user_data == popup)
13269 {
13270 /* tag refresh parent popup */
13271 wmEventHandler_UI *handler_next = reinterpret_cast<wmEventHandler_UI *>(
13272 handler->head.next);
13273 if (handler_next && handler_next->head.type == WM_HANDLER_TYPE_UI &&
13274 handler_next->handle_fn == popup_handler &&
13275 handler_next->remove_fn == popup_handler_remove)
13276 {
13277 PopupBlockHandle *parent_popup = static_cast<PopupBlockHandle *>(
13278 handler_next->user_data);
13279 ED_region_tag_refresh_ui(parent_popup->region);
13280 }
13281 break;
13282 }
13283 }
13284 }
13285
13287}
13288
13289void popup_handlers_remove_all(bContext *C, ListBaseT<wmEventHandler> *handlers)
13290{
13292}
13293
13299static bool button_references_srna(Button &button, const StructRNA *srna)
13300{
13301 if (RNA_struct_is_a(button.rnapoin.type, srna)) {
13302 return true;
13303 }
13304 if (button.opptr && RNA_struct_is_a(button.opptr->type, srna)) {
13305 return true;
13306 }
13307 if (MenuType *mt = button_menutype_get(&button); mt && RNA_struct_is_a(mt->rna_ext.srna, srna)) {
13308 return true;
13309 }
13310 if (PanelType *pt = button_paneltype_get(&button); pt && RNA_struct_is_a(pt->rna_ext.srna, srna))
13311 {
13312 return true;
13313 }
13314 if (wmOperatorType *ot = button_operatortype_get_from_enum_menu(&button, nullptr);
13315 ot && RNA_struct_is_a(ot->rna_ext.srna, srna))
13316 {
13317 return true;
13318 }
13319 if (button.type == ButtonType::SearchMenu) {
13320 const ButtonSearch &search_button = static_cast<const ButtonSearch &>(button);
13321 if (RNA_struct_is_a(search_button.rnasearchpoin.type, srna)) {
13322 return true;
13323 }
13324 }
13325
13326 if (button.type == ButtonType::Decorator) {
13327 const ButtonDecorator &decorator_button = static_cast<const ButtonDecorator &>(button);
13328 if (RNA_struct_is_a(decorator_button.decorated_rnapoin.type, srna)) {
13329 return true;
13330 }
13331 }
13332 for (ButtonExtraOpIcon &extra_op_icon : button.extra_op_icons) {
13333 if (RNA_struct_is_a(extra_op_icon.optype_params->optype->rna_ext.srna, srna)) {
13334 return true;
13335 }
13336 }
13337 if (button.context) {
13338 for (const bContextStoreEntry &entry : button.context->entries) {
13339 if (const PointerRNA *ptr = std::get_if<PointerRNA>(&entry.value)) {
13340 if (RNA_struct_is_a(ptr->type, srna)) {
13341 return true;
13342 }
13343 }
13344 }
13345 }
13346 return false;
13347}
13348
13354 PopupBlockHandle *popup_block_handle,
13355 StructRNA *srna_to_unreg)
13356{
13357 bool have_reference = false;
13358
13359 bool valid_for_refresh = popup_block_handle->can_refresh;
13360
13361 if (popup_block_handle->srna_owner &&
13362 RNA_struct_is_a(popup_block_handle->srna_owner, srna_to_unreg))
13363 {
13364 have_reference = true;
13365 valid_for_refresh = false;
13366 }
13367 [&]() {
13368 for (Block &block : popup_block_handle->region->runtime->uiblocks) {
13369 for (Button &button : block.buttons()) {
13370 have_reference = have_reference || button_references_srna(button, srna_to_unreg);
13371 if (button.type == ButtonType::SearchMenu) {
13372 const ButtonSearch &search_button = static_cast<const ButtonSearch &>(button);
13373 if (search_button.active) {
13374 /* Search buttons may reference menus/operators but we can't lookup in its items, close
13375 * any popup with a search button active. */
13376 have_reference = true;
13377 valid_for_refresh = false;
13378 return;
13379 }
13380 }
13381 }
13382 }
13383 }();
13384
13385 Button *active_button = region_find_active_but(popup_block_handle->region);
13386 HandleButtonData *data = active_button ? active_button->active : nullptr;
13387 PopupBlockHandle *sub_handle = data ? data->menu : nullptr;
13388
13389 if (have_reference) {
13390 if (valid_for_refresh) {
13391 /* This popup needs to be refresh, close any sub-menu first. */
13392 if (sub_handle) {
13393 CTX_wm_region_popup_set(C, popup_block_handle->region);
13394 button_active_free(C, active_button);
13395 CTX_wm_region_popup_set(C, nullptr);
13396 }
13397 ED_region_tag_refresh_ui(popup_block_handle->region);
13398 ED_region_tag_redraw(popup_block_handle->region);
13399 return false;
13400 }
13401 /* This popup can't be refreshed, try to refresh parent popup. */
13402 return true;
13403 }
13404
13405 if (!(sub_handle && popup_needs_update_for_unreg_srna_recursive(C, sub_handle, srna_to_unreg))) {
13406 /* There is no child popup referencing the rna type or it can be refresh, no need to refresh
13407 * this popup. */
13408 return false;
13409 }
13410
13411 if (valid_for_refresh) {
13412 CTX_wm_region_popup_set(C, popup_block_handle->region);
13413 button_active_free(C, active_button);
13414 CTX_wm_region_popup_set(C, nullptr);
13415 ED_region_tag_refresh_ui(popup_block_handle->region);
13416 ED_region_tag_redraw(popup_block_handle->region);
13417 return false;
13418 }
13419
13420 /* This popup can't be refreshed, try to refresh parent popup. */
13421 return true;
13422}
13423
13424void refresh_for_srna_unregister(Main *bmain, StructRNA *srna_to_unreg)
13425{
13426 if (G.background) {
13427 return;
13428 }
13429 if (!srna_to_unreg) {
13430 return;
13431 }
13432 bContext *C = CTX_create();
13433 CTX_data_main_set(C, bmain);
13434 auto refresh_or_remove_handler = [&](wmEventHandler &handler_base,
13435 ListBaseT<wmEventHandler> &handlers) {
13436 if (handler_base.type == WM_HANDLER_TYPE_UI && !bool(handler_base.flag & WM_HANDLER_DO_FREE)) {
13437 wmEventHandler_UI *handler = reinterpret_cast<wmEventHandler_UI *>(&handler_base);
13438 if (handler->handle_fn != popup_handler) {
13439 return;
13440 }
13441 PopupBlockHandle *popup_block_handle = static_cast<PopupBlockHandle *>(handler->user_data);
13442 if (!popup_needs_update_for_unreg_srna_recursive(C, popup_block_handle, srna_to_unreg)) {
13443 return;
13444 }
13445 if (handler->remove_fn) {
13446 handler->remove_fn(C, handler->user_data);
13447 }
13448 BLI_remlink(&handlers, handler);
13449 wm_event_free_handler(&handler->head);
13450 }
13451 };
13452
13453 for (wmWindowManager &wm : bmain->wm) {
13455 for (wmWindow &win : wm.windows) {
13456 CTX_wm_window_set(C, &win);
13457 /* Close any active popup referencing the StructRNA. */
13458 for (wmEventHandler &handler_base : win.runtime->modalhandlers.items_mutable()) {
13459 refresh_or_remove_handler(handler_base, win.runtime->modalhandlers);
13460 }
13461 for (wmEventHandler &handler_base : win.runtime->handlers.items_mutable()) {
13462 refresh_or_remove_handler(handler_base, win.runtime->handlers);
13463 }
13464 /* Close any active menu referencing the StructRNA. */
13465 bScreen *screen = WM_window_get_active_screen(&win);
13466 if (screen && screen->active_region) {
13468 Button *active_button = region_find_active_but(screen->active_region);
13469 HandleButtonData *data = (active_button) ? active_button->active : nullptr;
13470 PopupBlockHandle *sub_handle = (data) ? data->menu : nullptr;
13471 if (sub_handle &&
13472 popup_needs_update_for_unreg_srna_recursive(C, sub_handle, srna_to_unreg))
13473 {
13474 button_active_free(C, active_button);
13477 }
13478 CTX_wm_region_set(C, nullptr);
13479 }
13480 }
13481 }
13482 CTX_free(C);
13483}
13484
13486 const void *rna_poin_data,
13487 const char *rna_prop_id)
13488{
13489 for (Button &but : block.buttons()) {
13491 if (but.rnaprop && but.rnapoin.data == rna_poin_data) {
13492 if (STREQ(RNA_property_identifier(but.rnaprop), rna_prop_id)) {
13493 return &but;
13494 }
13495 }
13496 }
13497 }
13498 return nullptr;
13499}
13500
13502 ARegion *region,
13503 const void *rna_poin_data,
13504 const char *rna_prop_id,
13505 Block &block)
13506{
13507 Button *but_text = block_find_rna_text_button(block, rna_poin_data, rna_prop_id);
13508
13509 if (!but_text) {
13510 return false;
13511 }
13512
13513 ARegion *region_ctx = CTX_wm_region(C);
13514 /* Temporary context override for activating the button. */
13515 CTX_wm_region_set(const_cast<bContext *>(C), region);
13516 button_active_only(C, region, &block, but_text);
13517 CTX_wm_region_set(const_cast<bContext *>(C), region_ctx);
13518 return true;
13519}
13520
13522 ARegion *region,
13523 const void *rna_poin_data,
13524 const char *rna_prop_id)
13525{
13526 Button *but_text = nullptr;
13527 Block *block_text = nullptr;
13528 for (Block &block : region->runtime->uiblocks) {
13529 but_text = block_find_rna_text_button(block, rna_poin_data, rna_prop_id);
13530 block_text = &block;
13531 if (but_text) {
13532 break;
13533 }
13534 }
13535 if (!but_text) {
13536 return false;
13537 }
13538 ARegion *region_ctx = CTX_wm_region(C);
13539 /* Temporary context override for activating the button. */
13540 CTX_wm_region_set(const_cast<bContext *>(C), region);
13541 button_active_only(C, region, block_text, but_text);
13542 CTX_wm_region_set(const_cast<bContext *>(C), region_ctx);
13543 return true;
13544}
13545
13547{
13548 ARegion *region = CTX_wm_region(C);
13549 Block *block_text = nullptr;
13550 Button *but_text = nullptr;
13551
13552 for (Block &block : region->runtime->uiblocks) {
13553 for (Button &but : block.buttons()) {
13554 if (&but == actbut && but.type == ButtonType::Text) {
13555 block_text = &block;
13556 but_text = &but;
13557 break;
13558 }
13559 }
13560
13561 if (but_text) {
13562 break;
13563 }
13564 }
13565
13566 if (but_text) {
13567 button_active_only(C, region, block_text, but_text);
13568 return true;
13569 }
13570 return false;
13571}
13572
13574
13575/* -------------------------------------------------------------------- */
13578
13580{
13581 for (Block &block : region->runtime->uiblocks) {
13582 for (Button &but : block.buttons()) {
13583 if (but.active == nullptr) {
13584 continue;
13585 }
13586 button_active_free(C, &but);
13587 }
13588 }
13589}
13590
13592{
13593 wmWindow *win = CTX_wm_window(C);
13594
13595 ED_screen_areas_iter (win, screen, area) {
13596 for (ARegion &region : area->regionbase) {
13597 Button *but = region_find_active_but(&region);
13598 if (but) {
13600
13601 if (data->menu == nullptr && data->searchbox == nullptr) {
13602 if (data->state == BUTTON_STATE_HIGHLIGHT) {
13603 button_active_free(C, but);
13604 }
13605 }
13606 }
13607 }
13608 }
13609}
13610
13612{
13613 ARegion *region = CTX_wm_region(C);
13614 Button *but = region_find_active_but(region);
13615
13616 if (but) {
13618 return but;
13619 }
13620 }
13621
13622 return nullptr;
13623}
13624
13626{
13627 return button_active_drop_name_button(C) != nullptr;
13628}
13629
13631{
13632 ARegion *region = CTX_wm_region(C);
13633
13634 if (region) {
13635 Button *but = region_find_active_but(region);
13636
13637 if (but && but->type == ButtonType::Color) {
13638 return true;
13639 }
13640 }
13641
13642 return false;
13643}
13644
13646
13647/* -------------------------------------------------------------------- */
13650
13652{
13653 block->custom_interaction_callbacks = *callbacks;
13654}
13655
13657 Block *block,
13658 const bool is_click)
13659{
13662
13663 int unique_retval_ids_len = 0;
13664 for (const Button &but : block->buttons()) {
13665 if (but.active || (but.flag & BUT_DRAG_MULTI)) {
13666 unique_retval_ids_len++;
13667 }
13668 }
13669
13670 int *unique_retval_ids = MEM_new_array_uninitialized<int>(unique_retval_ids_len, __func__);
13671 unique_retval_ids_len = 0;
13672 for (const Button &but : block->buttons()) {
13673 if (but.active || (but.flag & BUT_DRAG_MULTI)) {
13674 unique_retval_ids[unique_retval_ids_len++] = but.retval;
13675 }
13676 }
13677
13678 if (unique_retval_ids_len > 1) {
13679 qsort(unique_retval_ids, unique_retval_ids_len, sizeof(int), BLI_sortutil_cmp_int);
13680 unique_retval_ids_len = BLI_array_deduplicate_ordered(unique_retval_ids,
13681 unique_retval_ids_len);
13682 unique_retval_ids = static_cast<int *>(MEM_realloc_uninitialized(
13683 unique_retval_ids, sizeof(*unique_retval_ids) * unique_retval_ids_len));
13684 }
13685
13686 interaction->params.is_click = is_click;
13687 interaction->params.unique_retval_ids = unique_retval_ids;
13688 interaction->params.unique_retval_ids_len = unique_retval_ids_len;
13689
13690 interaction->user_data = block->custom_interaction_callbacks.begin_fn(
13691 C, &interaction->params, block->custom_interaction_callbacks.arg1);
13692 return interaction;
13693}
13694
13697 BlockInteraction_Handle *interaction)
13698{
13699 BLI_assert(callbacks->end_fn != nullptr);
13700 callbacks->end_fn(C, &interaction->params, callbacks->arg1, interaction->user_data);
13701 MEM_delete(interaction->params.unique_retval_ids);
13702 MEM_delete(interaction);
13703}
13704
13707 BlockInteraction_Handle *interaction)
13708{
13709 BLI_assert(callbacks->update_fn != nullptr);
13710 callbacks->update_fn(C, &interaction->params, callbacks->arg1, interaction->user_data);
13711}
13712
13725 Block *block,
13727 const bool is_click)
13728{
13729 if (data->custom_interaction_handle) {
13730 return;
13731 }
13732 if (block->custom_interaction_callbacks.begin_fn == nullptr) {
13733 return;
13734 }
13735
13736 BlockInteraction_Handle *interaction = block_interaction_begin(C, block, is_click);
13737 interaction->user_count = 1;
13738 data->custom_interaction_handle = interaction;
13739}
13740
13742
13744 ARegion *region,
13745 ActivationButtonState target_state,
13746 PointerRNA *ptr,
13747 PropertyRNA *prop,
13748 bool warp_cursor_at_button,
13749 int index)
13750{
13751 HandleButtonState state = [target_state]() {
13752 switch (target_state) {
13759 default:
13761 }
13762 }();
13763
13764 if (region->runtime->do_draw & RGN_DRAWING) {
13765 return std::nullopt;
13766 }
13767
13768 wmWindow *win = CTX_wm_window(C);
13769 bScreen *screen = CTX_wm_screen(C);
13770 ED_screen_areas_iter (win, screen, area) {
13771 for (ARegion &other_region : area->regionbase) {
13772 if (other_region.runtime->do_draw & RGN_DRAWING) {
13773 /* Ensure no one else is drawing too. */
13774 return std::nullopt;
13775 }
13776 }
13777 }
13778
13779 ScrArea *area = nullptr;
13780 for (ScrArea &test_area : screen->areabase) {
13781 if (std::find_if(test_area.regionbase.begin(),
13782 test_area.regionbase.end(),
13783 [region](ARegion &r) { return &r == region; }) != test_area.regionbase.end())
13784 {
13785
13786 area = &test_area;
13787 break;
13788 }
13789 }
13790
13791 if (!area) {
13792 return std::nullopt;
13793 }
13794
13795 Button *button = nullptr;
13796 for (Block &block : region->runtime->uiblocks) {
13797 auto but_itr = std::ranges::find_if(
13798 block.buttons_ptrs, [&](const std::unique_ptr<Button> &but) {
13799 return but->rnapoin.data == ptr->data && but->rnaprop == prop && but->rnaindex == index;
13800 });
13801 if (but_itr != block.buttons_ptrs.end()) {
13802 button = but_itr->get();
13803 break;
13804 }
13805 }
13806
13807 if (!button) {
13808 return std::nullopt;
13809 }
13810
13811 const int2 old_view_xy = {int(region->v2d.cur.xmin), int(region->v2d.cur.ymin)};
13812 if ((button->block->flag & BLOCK_CLIP_EVENTS) == 0) {
13813 /* Blocks with BLOCK_CLIP_EVENTS are overlapping their region, so scrolling
13814 * that region to ensure it is in view can't work and causes issues. #97530 */
13815 but_ensure_in_view(C, region, button);
13816 }
13817
13818 const int2 xy{BLI_rcti_cent_x(&region->winrct), BLI_rcti_cent_y(&region->winrct)};
13819
13820 ED_screen_areas_iter (win, screen, area) {
13821 for (ARegion &other_region : area->regionbase) {
13822 UI_region_free_active_but_all(C, &other_region);
13823 }
13824 }
13825
13827 ScrArea *current_screen = CTX_wm_area(C);
13828 ARegion *current_region = CTX_wm_region(C);
13829 ARegion *current_popup_region = CTX_wm_region_popup(C);
13830 const rctf button_rect = button->rect;
13831
13832 BLI_SCOPED_DEFER([&]() {
13833 CTX_wm_area_set(C, current_screen);
13834 CTX_wm_region_set(C, current_region);
13835 CTX_wm_region_popup_set(C, current_popup_region);
13836 /* Restore button position. */
13837 button->rect = button_rect;
13838 });
13839
13840 CTX_wm_area_set(C, area);
13841 CTX_wm_region_set(C, region);
13842 CTX_wm_region_popup_set(C, nullptr);
13843 /* Init button active data with state as #BUTTON_STATE_HIGHLIGHT */
13844 handle_button_activate(C, region, button, BUTTON_ACTIVATE);
13845
13846 /* Temporally override button position so its already in view when putting mouse over. */
13848 &button->rect, region->v2d.cur.xmin - old_view_xy.x, old_view_xy.y - region->v2d.cur.ymin);
13849 rctf button_view_rect;
13850 block_to_window_rctf(region, button->block, &button_view_rect, &button->rect);
13851
13852 if (warp_cursor_at_button) {
13853 WM_cursor_warp(win, BLI_rctf_cent_x(&button_view_rect), BLI_rctf_cent_y(&button_view_rect));
13854 }
13855
13856 /* Disable text-search interactive mode. */
13857 button->changed = false;
13858
13859 if (button->flag & (BUT_DISABLED | UI_HIDDEN)) {
13860 /* Restore button position. */
13861 button->rect = button_rect;
13862 return std::nullopt;
13863 }
13864
13865 if (state == BUTTON_STATE_TEXT_EDITING && ELEM(button->type,
13870 {
13872 }
13873
13874 if (state == BUTTON_STATE_NUM_EDITING && ELEM(button->type,
13879 {
13880
13881 wmEvent event = *win->runtime->eventstate;
13882 event.type = LEFTMOUSE;
13883 event.val = KM_PRESS;
13884 event.xy[0] = BLI_rctf_cent_x(&button_view_rect);
13885 event.xy[1] = BLI_rctf_cent_y(&button_view_rect);
13886 /* Use `ui_do_button` for #BUTTON_STATE_NUM_EDITING with a dummy event, some buttons do some
13887 * additional configurations on left click to start editing. */
13888 do_button(C, button->block, button, &event);
13889 }
13890
13893 {
13895 }
13896
13897 return int2{int(BLI_rctf_cent_x(&button_view_rect)), int(BLI_rctf_cent_y(&button_view_rect))};
13898}
13899
13900} // namespace blender::ui
#define BKE_UNDO_STR_MAX
#define SIZE_MAX
#define ATTR_FALLTHROUGH
Generic array manipulation API.
#define BLI_array_deduplicate_ordered(arr, arr_len)
#define M_PI
#define BLI_SCOPED_DEFER(function_to_defer)
#define STRNCPY_UTF8(dst, src)
Platform independent time functions.
#define IFACE_(msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO_NOCHECK(clg_ref, format,...)
Definition CLG_log.h:204
#define CM_TABLE
#define RGN_ALIGN_ENUM_FROM_MASK(align)
#define UI_LIST_AUTO_SIZE_THRESHOLD
#define UI_SCALE_FAC
#define UI_ICON_SIZE
#define ED_screen_areas_iter(win, screen, area_name)
Definition ED_screen.hh:322
Read Blender memory allocation and freeing.
#define RNA_SUBTYPE_UNIT_VALUE(subtype)
Definition RNA_types.hh:268
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define AUTOCOMPLETE_FULL_MATCH
#define AUTOCOMPLETE_NO_MATCH
#define UI_PRECISION_FLOAT_MAX
#define UI_PRECISION_FLOAT_SCALE
#define UI_TOOLTIP_DELAY
#define button_is_decorator(but)
#define UI_FSTYLE_WIDGET
#define UI_TOOLTIP_DELAY_QUICK
#define NC_WINDOW
Definition WM_types.hh:388
#define WM_UI_HANDLER_CONTINUE
Definition WM_types.hh:361
#define NC_MOVIECLIP
Definition WM_types.hh:410
#define NA_EDITED
Definition WM_types.hh:634
#define ND_SPACE_INFO_REPORT
Definition WM_types.hh:563
#define WM_UI_HANDLER_BREAK
Definition WM_types.hh:362
#define NC_SPACE
Definition WM_types.hh:405
#define CLAMP(a, b, c)
#define UNPACK2(a)
#define CLAMP_MAX(a, c)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define IN_RANGE(a, b, c)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
#define MEM_realloc_uninitialized(vmemh, len)
void activate(bool forceActivation=false) const
btScalar max
void jump(const btVector3 &v=btVector3(0, 0, 0))
#define output
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
static GHOST_ISystem * getSystem()
virtual void setAutoFocus(const bool auto_focus)=0
const T * data() const
Definition BLI_array.hh:323
constexpr const char * c_str() const
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
constexpr const char * end() const
constexpr const char * begin() const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr const char * data() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
virtual bool rename(const bContext &C, StringRefNull new_name)
virtual StringRef get_rename_string() const
#define logf(x)
#define expf(x)
#define powf(x, y)
#define asinf(x)
#define fmodf(x, y)
#define roundf(x)
#define str(s)
#define FLT_MAX
#define FLT_EPSILON
#define round
#define abs
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
#define BUTTON_FLASH_DELAY
#define DRAG_MULTINUM_THRESHOLD_DRAG_X
#define MENU_TOWARDS_WIGGLE_ROOM
#define BUTTON_MOUSE_TOWARDS_THRESH
#define DRAG_MULTINUM_THRESHOLD_DRAG_Y
#define UI_PROP_SCALE_LOG_SNAP_OFFSET
#define UI_PROP_SCALE_LOG_MIN
#define MENU_SCROLL_INTERVAL
#define MENU_TOWARDS_MARGIN
#define DRAG_MULTINUM_THRESHOLD_VERTICAL
#define UI_DRAG_MAP_SOFT_RANGE_PIXEL_MAX
#define IS_ALLSELECT_EVENT(event)
#define BUT_IS_SELECT_CONTEXT
#define CASE_NUM_TO_DIR(n, d)
#define BUTTON_KEYNAV_PX_LIMIT
#define PIE_MENU_INTERVAL
#define BUTTON_AUTO_OPEN_THRESH
#define UI_BITBUT_VALUE_TOGGLED(a, b)
#define PIE_CLICK_THRESHOLD_SQ
#define UI_TEXT_MARGIN_X
#define UI_MENU_SCROLL_MOUSE
const int state
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void invalidate_cursor_overlay(Scene &scene, CurveMapping *curve)
void add_recent_search(StringRef chosen_str)
void button_execute_begin(bContext *, ARegion *region, Button *but, void **active_back)
void butstore_free(Block *block, ButStore *bs_handle)
static void textedit_prev_but(Block *block, Button *actbut, HandleButtonData *data)
std::optional< int2 > try_activate_rna_button(bContext *C, ARegion *region, ActivationButtonState target_state, PointerRNA *ptr, PropertyRNA *prop, bool warp_cursor_at_button=false, int index=0)
void block_interaction_set(Block *block, BlockInteraction_CallbackData *callbacks)
bool button_active_drop_color(bContext *C)
static int popup_handler(bContext *C, const wmEvent *event, void *userdata)
size_t button_drawstr_len_without_sep_char(const Button *but)
static bool but_is_drag_toggle(const Button *but)
void textbox_add_scroll(ButtonTextBox *textbox, int step)
static void handle_afterfunc_add_operator_ex(wmOperatorType *ot, PointerRNA **properties, wm::OpCallContext opcontext, const Button *context_but)
LayoutPanelHeader * layout_panel_header_under_mouse(const Panel &panel, const int my)
void button_anim_decorate_update_from_flag(ButtonDecorator *but)
Block * block_func_COLOR(bContext *C, PopupBlockHandle *handle, void *arg_but)
static void textedit_move(Button *but, TextEdit &text_edit, eStrCursorJumpDirection direction, const bool select, eStrCursorJumpType jump, bool jump_all_multiline=false)
static void but_paste_curvemapping(bContext *C, Button *but)
static std::optional< StringRef > button_edit_unit_hint_get_from_prop_subtype(const PropertySubType subtype)
static eSnapType event_to_snap(const wmEvent *event)
static Button * context_button_active(const ARegion *region, bool(*but_check_cb)(const Button *))
void button_update_view_for_active(const bContext *C, const Block *block)
void blocklist_free(const bContext *C, ARegion *region)
int button_unit_type_get(const Button *but)
int button_string_get_maxncpy(Button *but)
void UI_screen_free_active_but_highlight(const bContext *C, bScreen *screen)
static void afterfunc_update_preferences_dirty(AfterFunc *after)
void button_execute_end(bContext *C, ARegion *, Button *but, void *active_back)
static void apply_but(bContext *C, Block *block, Button *but, HandleButtonData *data, const bool interactive)
UndoStack_Text * textedit_undo_stack_create()
static bool numedit_but_HSVCUBE(Button *but, HandleButtonData *data, int mx, int my, const eSnapType snap, const bool shift, const bool use_continuous_grab, bool start_drag=false)
static void numedit_begin(Button *but, HandleButtonData *data)
void(*)(bContext *C, Layout *layout, void *arg1) MenuCreateFunc
Button * block_active_but_get(const Block *block)
void layout_panel_popup_scroll_apply(Panel *panel, const float dy)
void hsvcircle_vals_from_pos(const rcti *rect, float mx, float my, float *r_val_rad, float *r_val_dist)
static bool button_value_default(Button *but, double *r_value)
void perceptual_to_scene_linear_space(Button *but, float rgb[3])
Button * button_prev(Button *but) ATTR_WARN_UNUSED_RESULT
ARegion * tooltip_create_from_button_or_extra_icon(bContext *C, ARegion *butregion, Button *but, ButtonExtraOpIcon *extra_icon, bool is_quick_tip)
Button * button_active_drop_name_button(const bContext *C)
static void do_but_extra_operator_icons_mousemove(Button *but, HandleButtonData *data, const wmEvent *event)
void button_extra_operator_icons_free(Button *but)
bool button_supports_cycling(const Button *but)
void button_active_free(const bContext *C, Button *but)
static int handle_button_event(bContext *C, const wmEvent *event, Button *but)
void button_active_string_clear_and_exit(bContext *C, Button *but)
void button_anim_flag(Button *but, const AnimationEvalContext *anim_eval_context)
bool layout_panel_toggle_open(const bContext *C, LayoutPanelHeader *header)
bool but_contains_point_px_icon(const Button *but, ARegion *region, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
static void apply_but_WAVEFORM(bContext *C, Button *but, HandleButtonData *data)
void button_anim_autokey(bContext *C, Button *but, Scene *scene, float cfra)
static bool drag_toggle_set_xy_xy(bContext *C, ARegion *region, const bool pushed_state, const int xy_src[2], const int xy_dst[2], const bool drag_lock[2])
Button * region_but_find_rect_over(const ARegion *region, const rcti *rect_px)
void button_string_get(Button *but, char *str, const size_t str_maxncpy)
static void but_get_pasted_text_from_clipboard(const bool ensure_utf8, char **r_buf_paste, int *r_buf_len, bool paste_all_lines)
bool button_contains_pt(const Button *but, float mx, float my) ATTR_WARN_UNUSED_RESULT
void(*)(bContext *C, void *argN, void *arg2) ButtonHandleNFunc
void region_views_clear_search_highlight(const ARegion *region)
bool but_contains_password(const Button *but) ATTR_WARN_UNUSED_RESULT
static void handle_button_activate(bContext *C, ARegion *region, Button *but, ButtonActivateType type)
void(*)(bContext *C, void *arg1, void *arg2) ButtonHandleFunc
bool button_menu_draw_as_popover(const Button *but)
static void apply_but_COLORBAND(bContext *C, Button *but, HandleButtonData *data)
static BlockInteraction_Handle * block_interaction_begin(bContext *C, Block *block, const bool is_click)
static int do_but_CURVEPROFILE(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static int uilist_get_increment(const uiList *list, const int type)
void butstore_register(ButStore *bs_handle, Button **but_p)
void button_v4_get(Button *but, float vec[4])
static bool numedit_but_HSVCIRCLE(Button *but, HandleButtonData *data, float mx, float my, const eSnapType snap, const bool shift, const bool use_continuous_grab, const bool start_drag=false)
static void apply_but_IMAGE(bContext *C, Button *but, HandleButtonData *data)
Button * view_item_find_mouse_over(const ARegion *region, const int xy[2]) ATTR_NONNULL(1
Vector< StringRef > textbox_wrap_lines(ButtonTextBox *textbox)
bool context_copy_to_selected_list(bContext *C, PointerRNA *ptr, PropertyRNA *prop, Vector< PointerRNA > *r_lb, bool *r_use_path_from_id, std::optional< std::string > *r_path)
static void mouse_scale_warp(HandleButtonData *data, const float mx, const float my, float *r_mx, float *r_my, const bool shift)
Button * view_item_find_search_highlight(const ARegion *region)
Button * context_active_but_get_respect_popup(const bContext *C)
void button_value_set(Button *but, double value)
void button_range_set_hard(Button *but)
const uiStyle * style_get()
static eStrCursorJumpType textedit_jump_type_from_event(const wmEvent *event)
static void mouse_motion_keynav_init(KeyNavLock *keynav, const wmEvent *event)
bool but_has_quick_tooltip(const Button *but)
void button_text_password_hide(std::string &password_str, Button *but, const bool restore)
bool region_contains_point_px(const ARegion *region, const int xy[2]) ATTR_NONNULL(1
void searchbox_update(bContext *C, ARegion *region, Button *but, bool reset)
static int handler_region_menu(bContext *C, const wmEvent *event, void *userdata)
static void menu_scroll_apply_offset_y(ARegion *region, Block *block, float dy)
static bool selectcontext_begin(bContext *C, Button *but, uiSelectContextStore *selctx_data)
void popup_block_free(bContext *C, PopupBlockHandle *handle)
bool button_is_cursor_warp(const Button *but) ATTR_WARN_UNUSED_RESULT
static int do_but_SEARCH_UNLINK(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
bool textbutton_activate_rna(const bContext *C, ARegion *region, const void *rna_poin_data, const char *rna_prop_id)
void button_semi_modal_state_free(const bContext *C, Button *but)
void UI_region_free_active_but_all(bContext *C, ARegion *region)
static float numedit_apply_snap(int temp, float softmin, float softmax, const eSnapType snap)
void button_pie_dir(RadialDirection dir, float vec[2])
void button_update_edited(Button *but)
bool button_is_float(const Button *but)
static bool but_dragedit_update_mval(HandleButtonData *data, int mx, FunctionRef< int()> drag_threshold_fn)
bool block_layout_needs_resolving(const Block *block)
bool button_is_userdef(const Button *but)
static int handle_region_semi_modal_buttons(bContext *C, const wmEvent *event, ARegion *region)
void button_activate_event(bContext *C, ARegion *region, Button *but)
static bool but_find_select_in_enum__cmp(const Button *but_a, const Button *but_b)
Button * but_find_mouse_over(const ARegion *region, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
void panel_drag_collapse_handler_add(const bContext *C, const bool was_open)
void button_drag_start(bContext *C, Button *but)
static void apply_but_VIEW_ITEM(bContext *C, Block *block, Button *but, HandleButtonData *data)
static bool textedit_delete_selection(Button *but, TextEdit &text_edit)
int handler_panel_region(bContext *C, const wmEvent *event, ARegion *region, const Button *active_but)
static bool event_is_snap(const wmEvent *event)
static void but_copy_numeric_array(Button *but, char *output, int output_maxncpy)
static CLG_LogRef LOG
Definition interface.cc:82
static void numedit_set_active(Button *but)
Button * region_find_first_but_test_flag(ARegion *region, int64_t flag_include, int64_t flag_exclude)
static int do_but_TOG(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
static int do_but_HOTKEYEVT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
bool button_active_drop_name(const bContext *C)
static bool menu_pass_event_to_parent_if_nonactive(PopupBlockHandle *menu, const Button *but, const int level, const bool is_parent_menu, const int retval)
bool view_item_can_rename(const AbstractViewItem &item)
bool searchbox_event(bContext *C, ARegion *region, Button *but, ARegion *butregion, const wmEvent *event)
void window_to_block(const ARegion *region, const Block *block, int *x, int *y)
Definition interface.cc:230
static void handle_button_return_submenu(bContext *C, const wmEvent *event, Button *but)
static int do_but_BUT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
void popup_block_scrolltest(Block *block)
PointerRNA * button_operator_ptr_ensure(Button *but)
wmOperator * context_active_operator_get(const bContext *C)
static int handle_viewlist_items_hover(const wmEvent *event, ARegion *region)
static void but_copy_colorband(Button *but)
static void apply_but_autokey(bContext *C, Button *but)
bool button_is_compatible(const Button *but_a, const Button *but_b)
static void multibut_states_create(Button *but_active, HandleButtonData *data)
static int do_but_COLOR(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
float block_to_window_scale(const ARegion *region, const Block *block)
Definition interface.cc:182
static void numedit_end(Button *but, HandleButtonData *data)
bool view_item_supports_drag(const AbstractViewItem &item)
static void textedit_string_set(Button *but, TextEdit &text_edit, const char *str)
void block_to_window_fl(const ARegion *region, const Block *block, float *x, float *y)
Definition interface.cc:144
static void blocks_set_tooltips(ARegion *region, const bool enable)
static void region_auto_open_clear(ARegion *region)
void fontstyle_set(const uiFontStyle *fs)
static void but_paste_CurveProfile(bContext *C, Button *but)
static bool numedit_but_HISTOGRAM(Button *but, HandleButtonData *data, int mx, int my)
wmOperatorType * button_operatortype_get_from_enum_menu(Button *but, PropertyRNA **r_prop)
static void apply_but_TEX(bContext *C, Button *but, HandleButtonData *data)
static void apply_but_BLOCK(bContext *C, Button *but, HandleButtonData *data)
static void but_copy_text(Button *but, char *output, int output_maxncpy)
static AfterFunc * afterfunc_new()
static int do_but_HISTOGRAM(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
Button * region_find_active_but(ARegion *region) ATTR_WARN_UNUSED_RESULT
static int do_but_SCROLL(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
PropertyScaleType button_scale_type(const Button *but)
void view_item_begin_rename(AbstractViewItem &item)
static void apply_but_funcs_after(bContext *C)
static void palette_set_active(ButtonColor *color_but)
ARegion * region_searchbox_region_get(const ARegion *button_region)
void searchbox_free(bContext *C, ARegion *region)
static void but_copy_operator(bContext *C, Button *but, char *output, int output_maxncpy)
void fontscale(float *points, float aspect)
size_t button_tip_len_only_first_line(const Button *but)
void hsvcube_pos_from_vals(const ButtonHSVCube *hsv_but, const rcti *rect, const float *hsv, float *r_xp, float *r_yp)
static int do_but_EXIT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
const char * textedit_undo(UndoStack_Text *stack, int direction, int *r_cursor_index)
static int do_but_UNITVEC(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
bool block_is_menu(const Block *block) ATTR_WARN_UNUSED_RESULT
static int handle_uilist_event(bContext *C, const wmEvent *event, ARegion *region, Button *listbox)
static int handle_menus_recursive(bContext *C, const wmEvent *event, PopupBlockHandle *menu, int level, const bool is_parent_inside, const bool is_parent_menu, const bool is_floating)
static int do_but_TAB(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static void button_tooltip_timer_reset(bContext *C, Button *but)
Block * region_block_find_mouse_over(const ARegion *region, const int xy[2], bool only_clip)
static int text_position_to_hidden(Button *but, int pos)
void textbox_textedit_set_cursor_pos(ButtonTextBox *textbox, const ARegion *region, const float2 xy)
PanelType * button_paneltype_get(const Button *but)
static int do_but_TRACKPREVIEW(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
void popup_handlers_remove_all(bContext *C, ListBaseT< wmEventHandler > *handlers)
static void set_initial_search_query_from_event(const wmEvent *event, PointerRNA *props, const StringRefNull prop_name)
static void numedit_begin_set_values(Button *but, HandleButtonData *data)
void button_range_set_soft(Button *but)
static void but_paste_numeric_array(bContext *C, Button *but, HandleButtonData *data, char *buf_paste)
Button * button_find_select_in_enum(Button *but, int direction)
static void multibut_free(HandleButtonData *data, Block *block)
static void rgb_to_color_picker_HSVCUBE_compat_v(const ButtonHSVCube *hsv_but, const float rgb[3], float hsv[3])
static bool numedit_but_CURVE(Block *block, Button *but, HandleButtonData *data, int evtx, int evty, bool snap, const bool shift)
static ButtonMultiState * multibut_lookup(HandleButtonData *data, const Button *but)
bool but_is_utf8(const Button *but)
static int handler_region_drag_toggle(bContext *C, const wmEvent *event, void *userdata)
static bool but_copy(bContext *C, Button *but, const bool copy_array)
PopupBlockHandle * popup_menu_create(bContext *C, ARegion *butregion, Button *but, MenuCreateFunc menu_func, void *arg, bool can_refresh)
void textedit_undo_stack_destroy(UndoStack_Text *stack)
static void float_array_to_string(const float *values, const int values_len, char *output, int output_maxncpy)
static int do_but_CURVE(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static void mouse_motion_towards_init(PopupBlockHandle *menu, const int xy[2])
static void apply_but_CURVEPROFILE(bContext *C, Button *but, HandleButtonData *data)
static void textedit_set_cursor_select(Button *but, HandleButtonData *data, const float2 xy)
static void multibut_restore(bContext *C, HandleButtonData *data, Block *block)
static int do_but_KEYEVT(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
bool button_is_bool(const Button *but)
static int text_position_from_hidden(Button *but, int pos)
static void but_paste_text(bContext *C, Button *but, HandleButtonData *data, char *buf_paste)
static bool multibut_drag_wait(const HandleButtonMulti &multi_data)
static bool handle_button_activate_by_type(bContext *C, ARegion *region, Button *but)
static void rna_update_preferences_dirty(PointerRNA *ptr, PropertyRNA *prop)
static int handle_menu_button(bContext *C, const wmEvent *event, PopupBlockHandle *menu)
void scene_linear_to_perceptual_space(Button *but, float rgb[3])
void region_to_window(const ARegion *region, int *x, int *y)
Definition interface.cc:263
bool button_anim_expression_get(Button *but, char *str, size_t str_maxncpy)
static bool textedit_delete(Button *but, TextEdit &text_edit, eStrCursorJumpDirection direction, eStrCursorJumpType jump)
bool button_color_has_alpha(Button *but)
static bool popup_needs_update_for_unreg_srna_recursive(bContext *C, PopupBlockHandle *popup_block_handle, StructRNA *srna_to_unreg)
static void but_paste(bContext *C, Button *but, HandleButtonData *data, const bool paste_array)
static bool button_modal_state(HandleButtonState state)
static bool multibut_states_tag(Button *but_active, HandleButtonData *data, const wmEvent *event)
static void apply_but_TAB(bContext *C, Button *but, HandleButtonData *data)
static bool but_copypaste_curve_alive
static void but_paste_menu(bContext *C, Button *but, const StringRef value)
static eCurveProfilePoint_Flag profile_select_to_active(eCurveProfilePoint_Flag selection_type)
static bool numedit_but_COLORBAND(Button *but, HandleButtonData *data, int mx)
void button_string_get_ex(Button *but, char *str, const size_t str_maxncpy, const int float_precision, const bool use_exp_float, bool *r_use_exp_float)
void popup_handlers_add(bContext *C, ListBaseT< wmEventHandler > *handlers, PopupBlockHandle *popup, char flag)
static int do_but_text_value_cycle(bContext *C, Button *but, HandleButtonData *data, const int inc_value)
Button * button_find_mouse_over_ex(const ARegion *region, const int xy[2], bool labeledit, bool for_tooltip, const ButtonFindPollFn find_poll, const void *find_custom_data) ATTR_NONNULL(1
void(*)(bContext *C, void *arg, int event) BlockHandleFunc
static void textedit_begin(bContext *C, Button *but, HandleButtonData *data)
std::optional< StringRef > button_edit_unit_hint_get(const Button &but)
static int do_but_WAVEFORM(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
void handle_afterfunc_add_operator(wmOperatorType *ot, wm::OpCallContext opcontext)
static int do_but_COLORBAND(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
bool button_string_eval_number(bContext *C, const Button *but, const char *str, double *r_value)
static void block_open_end(bContext *C, Button *but, HandleButtonData *data)
static void multibut_add(HandleButtonData *data, Button *but)
static void apply_but_BUT(bContext *C, Button *but, HandleButtonData *data)
void button_v3_get(Button *but, float vec[3])
void block_to_window_rctf(const ARegion *region, const Block *block, rctf *rct_dst, const rctf *rct_src)
Definition interface.cc:172
static int handle_view_item_event(bContext *C, const wmEvent *event, Button *active_but, ARegion *region)
ARegion ARegion * screen_region_find_mouse_over(bScreen *screen, const wmEvent *event)
Block *(*)(bContext *C, ARegion *region, void *arg1) BlockCreateFunc
void button_set_string_interactive(bContext *C, Button *but, const char *value)
void popup_handlers_remove(ListBaseT< wmEventHandler > *handlers, PopupBlockHandle *popup)
static void block_interaction_end(bContext *C, BlockInteraction_CallbackData *callbacks, BlockInteraction_Handle *interaction)
static constexpr double menu_keep_open_duration
static void but_paste_color(bContext *C, Button *but, char *buf_paste)
static int do_but_NUM(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static void apply_but_HISTOGRAM(bContext *C, Button *but, HandleButtonData *data)
static void apply_but_CURVE(bContext *C, Button *but, HandleButtonData *data)
static bool numedit_but_UNITVEC(Button *but, HandleButtonData *data, int mx, int my, const eSnapType snap)
static int get_but_property_array_length(Button *but)
static int textedit_autocomplete(bContext *C, Button *but, HandleButtonData *data)
static int do_but_textedit(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static void apply_but_undo(Button *but, bool use_undo_grouped=false)
static bool do_but_ANY_drag_toggle(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event, int *r_retval)
Button * region_active_but_prop_get(const ARegion *region, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
Block *(*)(bContext *C, PopupBlockHandle *handle, void *arg1) BlockHandleCreateFunc
static int do_button(bContext *C, Block *block, Button *but, const wmEvent *event)
static void but_copy_numeric_value(Button *but, char *output, int output_maxncpy)
static int do_but_BLOCK(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
static void apply_but_TOG(bContext *C, Button *but, HandleButtonData *data)
static Button * block_pie_dir_activate(Block *block, const wmEvent *event, RadialDirection dir)
static void mouse_motion_towards_init_ex(PopupBlockHandle *menu, const int xy[2], const bool force)
static ColorBand but_copypaste_coba
constexpr int textbox_minimum_visible_lines
static void region_handler_remove(bContext *C, void *)
static int do_but_GRIP(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
int textbox_wrapped_line_index_from_char_offset(Span< StringRef > lines, int offset)
void popup_menu_retval_set(const Block *block, int retval, bool enable)
static void mouse_motion_towards_reinit(PopupBlockHandle *menu, const int xy[2])
static void but_extra_operator_icon_apply(bContext *C, Button *but, ButtonExtraOpIcon *op_icon)
bool button_active_only(const bContext *C, ARegion *region, Block *block, Button *but)
void but_ensure_in_view(const bContext *C, ARegion *region, const Button *but)
void button_tooltip_refresh(bContext *C, Button *but)
static int pie_handler(bContext *C, const wmEvent *event, PopupBlockHandle *menu)
static bool drag_toggle_but_pushed_state(Button *but)
int button_menu_step(Button *but, int direction)
static bool menu_scroll_step(ARegion *region, Block *block, const int scroll_dir)
static void selectcontext_end(Button *but, uiSelectContextStore *selctx_data)
double button_value_get(Button *but)
static void textedit_end(bContext *C, Button *but, HandleButtonData *data)
PopupBlockHandle * popup_block_create(bContext *C, ARegion *butregion, Button *but, BlockCreateFunc create_func, BlockHandleCreateFunc handle_create_func, void *arg, FreeArgFunc arg_free, bool can_refresh)
static bool parse_float_array(char *text, float *values, int values_len_expected)
void button_tooltip_timer_remove(bContext *C, Button *but)
static int handle_menu_mmb_event(bContext *C, const wmEvent *event, PopupBlockHandle *menu, int level, const bool is_parent_menu)
Button * listrow_find_mouse_over(const ARegion *region, const int xy[2]) ATTR_NONNULL(1
static void apply_but_ROW(bContext *C, Block *block, Button *but, HandleButtonData *data)
void context_active_but_prop_handle(bContext *C, bool handle_undo)
bool button_string_set(bContext *C, Button *but, const char *str)
static int do_but_SLI(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static bool but_copy_popover(Button *but, char *output, int output_maxncpy)
int button_menu_direction(Button *but)
bool block_is_pie_menu(const Block *block) ATTR_WARN_UNUSED_RESULT
static char menu_scroll_test(Block *block, int2 xy)
bool button_is_editable(const Button *but) ATTR_WARN_UNUSED_RESULT
static int do_but_TEX(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static void but_set_float_array(bContext *C, Button *but, HandleButtonData *data, const float *values, const int values_len)
void refresh_for_srna_unregister(Main *bmain, StructRNA *srna_to_unreg)
static int handle_menu_event(bContext *C, const wmEvent *event, PopupBlockHandle *menu, int level, const bool is_parent_inside, const bool is_parent_menu, const bool is_floating)
static void clamp_axis_max_v3(float v[3], const float max)
static void multibut_states_apply(bContext *C, HandleButtonData *data, Block *block)
static void color_snap_hue(const eSnapType snap, float *r_hue)
bool searchbox_inside(ARegion *region, const int xy[2]) ATTR_NONNULL(1
Button * button_first(Block *block) ATTR_WARN_UNUSED_RESULT
static void apply_but_NUM(bContext *C, Button *but, HandleButtonData *data)
static void but_paste_colorband(bContext *C, Button *but, HandleButtonData *data)
static void popup_check(bContext *C, wmOperator *op)
static int do_but_HSVCUBE(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
static bool menu_scroll_to_y(ARegion *region, Block *block, int y)
static void but_copy_color(Button *but, char *output, int output_maxncpy)
float block_calc_pie_segment(Block *block, const float event_xy[2])
bool view_item_popup_keep_open(const AbstractViewItem &item)
static void apply_but_func(bContext *C, Button *but)
static int but_pie_button_activate(bContext *C, Button *but, PopupBlockHandle *menu)
static ButtonExtraOpIcon * but_extra_operator_icon_mouse_over_get(Button *but, ARegion *region, const wmEvent *event)
void button_convert_to_unit_alt_name(Button *but, char *str, size_t str_maxncpy)
static bool but_drag_init(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
static CurveMapping but_copypaste_curve
bool uilist_item_index_is_filtered_visible(const struct uiList *ui_list, int item_idx)
static int do_but_LISTROW(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
void pan_to_scroll(const wmEvent *event, int *type, int *val)
bool searchbox_apply(Button *but, ARegion *region)
char * button_string_get_dynamic(Button *but, int *r_str_size)
int fontstyle_height_max(const uiFontStyle *fs)
static void but_copy_CurveProfile(Button *but)
bool button_has_array_value(const Button *but) ATTR_WARN_UNUSED_RESULT
bool button_is_interactive(const Button *but, bool labeledit) ATTR_WARN_UNUSED_RESULT
bool button_drag_is_draggable(const Button *but)
static void rgb_to_color_picker_HSVCUBE_v(const ButtonHSVCube *hsv_but, const float rgb[3], float hsv[3])
Button * button_last(Block *block) ATTR_WARN_UNUSED_RESULT
void hsvcircle_pos_from_vals(const ColorPicker *cpicker, const rcti *rect, const float *hsv, float *r_xpos, float *r_ypos)
static bool do_but_extra_operator_icon(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
bool button_opens_link(const Button *button)
void color_picker_hsv_to_rgb(const float r_cp[3], float rgb[3])
static void textedit_set_cursor_pos(Button *but, const ARegion *region, const float2 xy)
Button * listbox_find_mouse_over(const ARegion *region, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
static void with_but_active_as_semi_modal(bContext *C, ARegion *region, Button *but, FunctionRef< void()> fn)
void context_update_anim_flag(const bContext *C)
static void handler_region_drag_toggle_remove(bContext *, void *userdata)
static void force_activate_view_item_but(bContext *C, ARegion *region, ButtonViewItem *but, const bool close_popup=true)
Button * button_find_rect_over(const ARegion *region, const rcti *rect_px) ATTR_WARN_UNUSED_RESULT
static void button_activate_init(bContext *C, ARegion *region, Button *but, ButtonActivateType type)
bool popup_context_menu_for_button(bContext *C, Button *but, const wmEvent *event)
bool button_contains_point_px(const Button *but, const ARegion *region, const int xy[2]) ATTR_NONNULL(1
bool button_is_editable_as_text(const Button *but) ATTR_WARN_UNUSED_RESULT
bool button_is_popover_once_compat(const Button *but) ATTR_WARN_UNUSED_RESULT
Button * region_active_but_get(const ARegion *region)
void button_activate_over(bContext *C, ARegion *region, Button *but)
static bool mouse_motion_towards_check(Block *block, PopupBlockHandle *menu, const int xy[2], const bool use_wiggle_room)
bool button_anim_decorate_pushed_state(ButtonDecorator *but)
static int do_but_VIEW_ITEM(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event)
int button_text_padding(const Button *button)
static CurveProfile but_copypaste_profile
void button_override_flag(Main *bmain, Button *but)
static void but_update_preferences_dirty(Button *but)
static bool but_pie_menu_supported_apply(Button *but)
static bool point_draw_handles(CurveProfilePoint *point)
void but_handle_data_free(HandleButtonData **data)
void button_v3_set(Button *but, const float vec[3])
static bool numedit_but_CURVEPROFILE(Block *block, Button *but, HandleButtonData *data, int evtx, int evty, bool snap, const bool shift)
static int but_pie_menu_apply(bContext *C, PopupBlockHandle *menu, Button *but, bool force_close)
Button * context_active_but_prop_get(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
MenuType * button_menutype_get(const Button *but)
void window_to_block_fl(const ARegion *region, const Block *block, float *x, float *y)
Definition interface.cc:193
static void but_paste_normalized_vector(bContext *C, Button *but, HandleButtonData *data, char *buf_paste)
float textbox_grip_height()
static void but_paste_numeric_value(bContext *C, Button *but, HandleButtonData *data, char *buf_paste)
static void numedit_apply(bContext *C, Block *block, Button *but, HandleButtonData *data)
Button * context_active_but_get(const bContext *C)
int searchbox_autocomplete(bContext *C, ARegion *region, Button *but, char *str)
static bool but_copypaste_profile_alive
static void button_activate_exit(bContext *C, Button *but, HandleButtonData *data, const bool mousemove, const bool onfree)
void popup_menu_memory_set(Block *block, Button *but)
static void block_open_begin(bContext *C, Button *but, HandleButtonData *data)
static int handle_menu_letter_press_search(PopupBlockHandle *menu, const wmEvent *event)
static bool numedit_but_SLI(Button *but, HandleButtonData *data, int mx, FunctionRef< int()> drag_threshold_fn, const bool is_horizontal, const bool is_motion, const bool snap, const bool shift)
int button_is_pushed(Button *but)
static bool textedit_insert_buf(Button *but, TextEdit &text_edit, const char *buf, int buf_len)
static void popup_handler_remove(bContext *C, void *userdata)
static void selectcontext_apply(bContext *C, Button *but, uiSelectContextStore *selctx_data, const double value, const double value_orig)
static void apply_but_VEC(bContext *C, Button *but, HandleButtonData *data)
void button_hsv_set(Button *but)
static void drag_toggle_set(bContext *C, uiDragToggleHandle *drag_info, const int xy_input[2])
static bool can_activate_other_menu(Button *but, Button *but_other, const wmEvent *event)
static void block_interaction_update(bContext *C, BlockInteraction_CallbackData *callbacks, BlockInteraction_Handle *interaction)
PopupBlockHandle * popover_panel_create(bContext *C, ARegion *butregion, Button *but, PopoverCreateFunc popover_func, const PanelType *panel_type)
void popup_translate(ARegion *region, const int mdiff[2])
static void color_picker_to_rgb_HSVCUBE_v(const ButtonHSVCube *hsv_but, const float hsv[3], float rgb[3])
static int do_but_textedit_select(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
bool int searchbox_find_index(ARegion *region, const char *name)
bool textbutton_activate_but(const bContext *C, Button *actbut)
static void textedit_string_ensure_max_length(Button *but, TextEdit &text_edit, int str_maxncpy)
void region_handlers_add(ListBaseT< wmEventHandler > *handlers)
static float mouse_scale_warp_factor(const bool shift)
static void but_copy_curvemapping(Button *but)
static int handle_menu_return_submenu(bContext *C, const wmEvent *event, PopupBlockHandle *menu)
static bool drag_toggle_but_is_supported(const Button *but)
bool view_item_drag_start(bContext &C, AbstractViewItem &item)
static bool afterfunc_check(const Block *block, const Button *but)
bool button_is_editing(const Button *but)
std::string button_get_link(const Button *button, bContext *C)
static float numedit_apply_snapf(Button *but, float tempf, float softmin, float softmax, const eSnapType snap)
bool button_draw_as_link(const Button *button)
void(*)(void *argN) ButtonArgNFree
float textbox_vertical_padding()
static void textedit_next_but(Block *block, Button *actbut, HandleButtonData *data)
static int do_but_HSVCIRCLE(bContext *C, Block *block, Button *but, HandleButtonData *data, const wmEvent *event)
void textedit_undo_push(UndoStack_Text *stack, const char *text, int cursor_index)
static bool numedit_but_WAVEFORM(Button *but, HandleButtonData *data, int mx, int my)
static int region_handler(bContext *C, const wmEvent *event, void *)
static int handle_button_over(bContext *C, const wmEvent *event, ARegion *region)
static bool menu_scroll_to_but(ARegion *region, Block *block, Button *but_target)
bool block_is_popup_any(const Block *block) ATTR_WARN_UNUSED_RESULT
static bool button_references_srna(Button &button, const StructRNA *srna)
void popover_once_clear(Popover *pup)
static bool numedit_but_NUM(ButtonNumber *but, HandleButtonData *data, int mx, FunctionRef< int()> drag_threshold_fn, const bool is_motion, const eSnapType snap, float fac)
StringRef button_drawstr_without_sep_char(const Button *but) ATTR_NONNULL()
void button_update(Button *but)
static ARegion * but_tooltip_init(bContext *C, ARegion *region, int *pass, double *r_pass_delay, bool *r_exit_on_event)
static int do_but_TEXTBOX(bContext *C, Block *block, ButtonTextBox *textbox, HandleButtonData *data, const wmEvent *event)
void color_picker_rgb_to_hsv_compat(const float rgb[3], float r_cp[3])
static void foreach_semi_modal_but_as_active(bContext *C, ARegion *region, FunctionRef< void(Button *semi_modal_but)> fn)
bool context_copy_to_selected_check(PointerRNA *ptr, PointerRNA *ptr_link, PropertyRNA *prop, const char *path, bool use_path_from_id, PointerRNA *r_ptr, PropertyRNA **r_prop)
static bool but_copy_menu(bContext *C, Button *but, char *output, int output_maxncpy)
static bool numedit_but_TRACKPREVIEW(bContext *C, Button *but, HandleButtonData *data, int mx, int my, const bool shift)
static Button * block_find_rna_text_button(Block &block, const void *rna_poin_data, const char *rna_prop_id)
static ListBaseT< AfterFunc > UIAfterFuncs
static void block_interaction_begin_ensure(bContext *C, Block *block, HandleButtonData *data, const bool is_click)
static void button_activate_state(bContext *C, Button *but, HandleButtonState state)
static void button_edit_unit_hint_refresh(bContext *C, Button *but, HandleButtonData *data)
@ BUT_ACTIVATE_ON_INIT_NO_SELECT
@ BUT_TEXTEDIT_AUTOCOMPLETE_KEEP_ACTIVE
ButStore * butstore_create(Block *block)
void textbox_scroll_to_cursor(ButtonTextBox *textbox)
bool button_is_rna_valid(Button *but)
static bool rna_is_userdef(PointerRNA *ptr, PropertyRNA *prop)
Button * button_next(Button *but) ATTR_WARN_UNUSED_RESULT
void button_execute(const bContext *C, ARegion *region, Button *but)
void textbox_jump_line(ButtonTextBox *textbox, eStrCursorJumpDirection direction, const bool select)
static Button * but_list_row_text_activate(bContext *C, Button *but, HandleButtonData *data, const wmEvent *event, ButtonActivateType activate_type)
static void apply_but_TRACKPREVIEW(bContext *C, Button *but, HandleButtonData *data)
void context_active_but_clear(bContext *C, wmWindow *win, ARegion *region)
static Button * but_find_open_event(ARegion *region, const wmEvent *event)
Block * block_find_mouse_over_ex(const ARegion *region, const int xy[2], bool only_clip) ATTR_NONNULL(1
static bool textedit_copypaste(Button *but, TextEdit &text_edit, const int mode)
void popup_menu_close_from_but(const Button *but, bool is_cancel=false)
static bool mouse_motion_keynav_test(KeyNavLock *keynav, const wmEvent *event)
bool button_is_unit(const Button *but)
static void apply_but_BUTM(bContext *C, Button *but, HandleButtonData *data)
MINLINE float min_ff(float a, float b)
void WM_main_add_notifier(uint type, void *reference)
UserDef U
Definition blender.cc:58
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
MINLINE int round_fl_to_int_clamp(float a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void WM_global_reportf(eReportType type, const char *format,...)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
ScrArea * CTX_wm_area(const bContext *C)
void WM_report_banner_show(wmWindowManager *wm, wmWindow *win)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
double WM_tooltip_time_closed()
Definition wm_tooltip.cc:26
void ED_screen_set_active_region(bContext *C, wmWindow *win, const int xy[2])
void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
@ RPT_PROPERTY
Definition BKE_report.hh:39
@ RPT_WARNING
Definition BKE_report.hh:40
void RNA_property_float_get_array_at_most(PointerRNA *ptr, PropertyRNA *prop, float *values, int values_num)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE void copy_v2_fl(float r[2], float f)
void ED_undo_grouped_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:357
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:835
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void WM_tooltip_timer_init_ex(bContext *C, wmWindow *win, ScrArea *area, ARegion *region, wmTooltipInitFn init, double delay)
Definition wm_tooltip.cc:46
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BLI_rctf_recenter(struct rctf *rect, float x, float y)
Definition rct.cc:616
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ UILST_SCROLL_TO_ACTIVE_ITEM
double BKE_unit_base_scalar(int system, int type)
Definition unit.cc:2561
void WM_event_start_drag(bContext *C, int icon, eWM_DragDataType type, void *poin, uint flags)
@ RGN_TYPE_ASSET_SHELF_HEADER
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE int round_fl_to_int(float a)
wmEventHandler_UI * WM_event_add_ui_handler(const bContext *C, ListBaseT< wmEventHandler > *handlers, wmUIHandlerFunc handle_fn, wmUIHandlerRemoveFunc remove_fn, void *user_data, const eWM_EventHandlerFlag flag)
@ WM_CURSOR_WRAP_XY
Definition WM_types.hh:242
std::optional< std::string > WM_prop_pystring_assign(bContext *C, PointerRNA *ptr, PropertyRNA *prop, int index)
PointerRNA RNA_id_pointer_create(ID *id)
MINLINE void mul_v2_fl(float r[2], float f)
void WM_operator_properties_free(PointerRNA *ptr)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
const char * RNA_property_identifier(const PropertyRNA *prop)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
void rgb_to_hsl_v(const float rgb[3], float r_hsl[3])
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void CTX_wm_window_set(bContext *C, wmWindow *win)
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
void WM_cursor_modal_set(wmWindow *win, int val)
void BLI_rcti_rctf_copy(struct rcti *dst, const struct rctf *src)
@ PROP_UNIT_LENGTH
Definition RNA_types.hh:222
@ PROP_UNIT_ROTATION
Definition RNA_types.hh:226
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
void BKE_curvemapping_copy_data(CurveMapping *target, const CurveMapping *cumap)
MINLINE float normalize_v2_length(float n[2], float unit_length)
unsigned short ushort
Definition BKE_text.h:56
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
@ WM_DRAG_COLOR
Definition WM_types.hh:1312
void rgb_to_hsv_compat_v(const float rgb[3], float r_hsv[3])
std::string RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_enum_value(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *r_value)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
float RNA_property_float_get_default_index(PointerRNA *ptr, PropertyRNA *prop, int index)
void BKE_curvemapping_copy_data_single(CurveMapping *target, const CurveMapping *cumap, int to_idx, int from_idx, bool make_copy)
MINLINE int len_manhattan_v2v2_int(const int a[2], const int b[2]) ATTR_WARN_UNUSED_RESULT
wmWindow * CTX_wm_window(const bContext *C)
bool WM_stereo3d_enabled(wmWindow *win, bool skip_stereo3d_check)
Definition wm_stereo.cc:141
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int WM_event_drag_threshold(const wmEvent *event)
MINLINE int max_ii(int a, int b)
void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const bool *values)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BKE_curveprofile_remove_by_flag(struct CurveProfile *profile, short flag)
void WM_tooltip_clear(bContext *C, wmWindow *win)
Definition wm_tooltip.cc:84
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
void BKE_curveprofile_copy_data(struct CurveProfile *target, const struct CurveProfile *profile)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
MINLINE float cube_f(float a)
std::optional< UndoEncodeHints > ED_undo_is_legacy_compatible_for_property(bContext *C, ID *id, const PointerRNA &ptr, const PropertyRNA &prop)
Definition ed_undo.cc:607
void WM_event_free_ui_handler_all(bContext *C, ListBaseT< wmEventHandler > *handlers, wmUIHandlerFunc handle_fn, wmUIHandlerRemoveFunc remove_fn)
const char * BKE_unit_display_name_short_get(const void *usys_pt, int index)
Definition unit.cc:2621
bContext * CTX_create()
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE float pow2f(float x)
AnimationEvalContext BKE_animsys_eval_context_construct(Depsgraph *depsgraph, float eval_time) ATTR_WARN_UNUSED_RESULT
Definition anim_sys.cc:602
wmEventModifierFlag
Definition WM_types.hh:290
void void BLI_linklist_prepend(LinkNode **listp, void *ptr) ATTR_NONNULL(1)
void BKE_curvemap_activate_nearest_point(struct CurveMap *cuma, const int i_last)
void WM_tooltip_timer_clear(bContext *C, wmWindow *win)
Definition wm_tooltip.cc:72
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
@ INBETWEEN_MOUSEMOVE
@ WM_IME_COMPOSITE_END
@ WM_IME_COMPOSITE_START
@ WM_IME_COMPOSITE_EVENT
const char * WM_key_event_string(const short type, const bool compact)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_array_check(PropertyRNA *prop)
Global G
Definition blender.cc:57
void BKE_brush_tag_unsaved_changes(Brush *brush)
Definition brush.cc:843
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
bool BLI_rctf_isect_rect_x(const struct rctf *src1, const struct rctf *src2, float range_x[2])
PropertyType RNA_property_type(PropertyRNA *prop)
bool WM_event_is_tablet(const wmEvent *event)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
std::string WM_operator_pystring_ex(bContext *C, wmOperator *op, const bool all_args, const bool macro_args, wmOperatorType *ot, PointerRNA *opptr)
void BLI_linklist_freeN(LinkNode *list)
@ WM_EVENT_SCROLL_INVERT
Definition WM_types.hh:737
@ WM_EVENT_IS_REPEAT
Definition WM_types.hh:744
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
PropertyScaleType
Definition RNA_types.hh:251
@ PROP_SCALE_LOG
Definition RNA_types.hh:258
@ PROP_SCALE_CUBIC
Definition RNA_types.hh:263
@ PROP_SCALE_LINEAR
Definition RNA_types.hh:253
VecBase< int32_t, 2 > int2
void BKE_curvemapping_free_data_single(CurveMapping *cumap, int index)
VecBase< float, 2 > float2
void WM_tooltip_refresh(bContext *C, wmWindow *win)
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
int BKE_preffered_unit_of_type_or_base_get(const UnitSettings &settings, int type)
Definition unit.cc:2599
MINLINE void add_v2_v2(float r[2], const float a[2])
void ED_region_tag_refresh_ui(ARegion *region)
Definition area.cc:738
@ PROP_BOOLEAN
Definition RNA_types.hh:210
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
BLI_INLINE int BLI_rcti_cent_y(const struct rcti *rct)
Definition BLI_rect.hh:190
bool BKE_curveprofile_move_handle(struct CurveProfilePoint *point, bool handle_1, bool snap, const float delta[2])
void WM_cursor_grab_enable(wmWindow *win, const eWM_CursorWrapAxis wrap, const rcti *wrap_region, const bool hide)
char * WM_clipboard_text_get_firstline(bool selection, bool ensure_utf8, int *r_len)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
MovieTrackingMarker * BKE_tracking_marker_ensure(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1407
eWM_EventHandlerFlag
Definition WM_api.hh:707
@ WM_HANDLER_DO_FREE
Definition WM_api.hh:715
@ WM_HANDLER_BLOCKING
Definition WM_api.hh:709
BMesh const char void * data
void hsv_clamp_v(float hsv[3], float v_max)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
wmWindowManager * CTX_wm_manager(const bContext *C)
bool BLI_rctf_isect_pt(const struct rctf *rect, float x, float y)
bool RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
BLI_INLINE void IMB_colormanagement_srgb_to_scene_linear_v3(float scene_linear[3], const float srgb[3])
int BLI_str_rstrip_float_zero(char *str, char pad) ATTR_NONNULL(1)
void BKE_brush_color_set(Paint *paint, Brush *brush, const float3 &color)
Definition brush.cc:1305
int RNA_property_flag(PropertyRNA *prop)
void BKE_curvemapping_changed(CurveMapping *cumap, bool rem_doubles)
MINLINE size_t min_zz(size_t a, size_t b)
void WM_cursor_modal_restore(wmWindow *win)
const char const char * BLI_str_find_next_char_utf8(const char *p, const char *str_end) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
void CTX_free(bContext *C)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
MovieClip * CTX_data_edit_movieclip(const bContext *C)
bool BLI_str_cursor_step_prev_utf8(const char *str, int str_maxlen, int *pos)
PanelType * WM_paneltype_find(const StringRef idname, bool quiet)
@ PROF_UPDATE_REMOVE_DOUBLES
int BKE_curvemapping_num_channels(const CurveMapping *cumap)
void WM_global_report(eReportType type, const char *message)
MINLINE float sqrt3f(float f)
void RNA_property_float_set_array_at_most(PointerRNA *ptr, PropertyRNA *prop, const float *values, int values_num)
ATTR_WARN_UNUSED_RESULT const size_t num
void BKE_unit_system_get(int system, int type, const void **r_usys_pt, int *r_len)
Definition unit.cc:2576
void BKE_colorband_update_sort(ColorBand *coba)
Definition colorband.cc:587
const char int BLI_str_utf8_size_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void rgb_to_hsl_compat_v(const float rgb[3], float r_hsl[3])
void WM_cursor_grab_disable(wmWindow *win, const int mouse_ungrab_xy[2])
void CTX_wm_region_popup_set(bContext *C, ARegion *region_popup)
bool BKE_unit_string_contains_unit(const char *str, int type)
Definition unit.cc:2388
bool BKE_unit_is_valid(int system, int type)
Definition unit.cc:2571
@ WM_DRAG_FREE_DATA
Definition WM_types.hh:1324
MINLINE void zero_v3(float r[3])
void dist_ensure_v2_v2fl(float v1[2], const float v2[2], float dist)
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
struct CurveProfilePoint * BKE_curveprofile_insert(struct CurveProfile *profile, float x, float y)
void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
bool RNA_property_enum_item_from_value(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, EnumPropertyItem *r_item)
@ ED_KEYMAP_VIEW2D
Definition ED_screen.hh:855
int BKE_curveprofile_table_size(const struct CurveProfile *profile)
void hsl_to_rgb_v(const float hsl[3], float r_rgb[3])
Definition math_color.cc:66
float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
CBData * BKE_colorband_element_add(ColorBand *coba, float position)
Definition colorband.cc:609
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
MINLINE float normalize_v3(float n[3])
PropertySubType RNA_property_subtype(PropertyRNA *prop)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_manhattan_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
bool hex_to_rgba(const char *hexcol, float *r_r, float *r_g, float *r_b, float *r_a)
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
@ WM_CURSOR_NS_SCROLL
Definition wm_cursors.hh:55
@ WM_CURSOR_HAND_CLOSED
Definition wm_cursors.hh:25
@ WM_CURSOR_MOVE
Definition wm_cursors.hh:23
@ WM_CURSOR_TEXT_EDIT
Definition wm_cursors.hh:18
@ WM_CURSOR_Y_MOVE
Definition wm_cursors.hh:42
@ WM_CURSOR_HAND
Definition wm_cursors.hh:24
@ WM_CURSOR_DEFAULT
Definition wm_cursors.hh:17
@ WM_CURSOR_X_MOVE
Definition wm_cursors.hh:41
@ WM_CURSOR_HAND_POINT
Definition wm_cursors.hh:26
float RNA_property_float_get_default(PointerRNA *ptr, PropertyRNA *prop)
void BKE_curvemapping_free_data(CurveMapping *cumap)
void WM_operator_stack_clear(wmWindowManager *wm)
Definition wm.cc:375
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BLI_rctf_transform_pt_v(const rctf *dst, const rctf *src, float xy_dst[2], const float xy_src[2])
Definition rct.cc:540
Main * CTX_data_main(const bContext *C)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
char * WM_clipboard_text_get(bool selection, bool ensure_utf8, int *r_len)
StructRNA * RNA_struct_base(StructRNA *type)
bool BLI_rctf_clamp_pt_v(const struct rctf *rect, float xy[2])
void BLI_str_cursor_step_utf8(const char *str, int str_maxlen, int *pos, eStrCursorJumpDirection direction, eStrCursorJumpType jump, bool use_init_step)
Paint * BKE_paint_get_active_from_context(const bContext *C)
MINLINE float max_ff(float a, float b)
@ USER_FLAG_RECENT_SEARCHES_DISABLE
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3_length(float n[3], float unit_length)
void WM_clipboard_text_set(const char *buf, bool selection)
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
void WM_event_remove_ui_handler(ListBaseT< wmEventHandler > *handlers, wmUIHandlerFunc handle_fn, wmUIHandlerRemoveFunc remove_fn, void *user_data, const bool postpone)
void WM_gestures_remove(wmWindow *win)
@ PROP_PROPORTIONAL
Definition RNA_types.hh:419
@ PROP_NO_DEG_UPDATE
Definition RNA_types.hh:509
bool BPY_string_compile_check(const char *expr)
ARegion * CTX_wm_region_popup(const bContext *C)
CurveMapPoint * BKE_curvemap_insert(CurveMap *cuma, float x, float y)
int BLI_path_sequence_decode(const char *path, char *head, size_t head_maxncpy, char *tail, size_t tail_maxncpy, unsigned short *r_digits_len)
Definition path_utils.cc:61
int RNA_property_int_get_default(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
int WM_event_absolute_delta_y(const wmEvent *event)
MINLINE float square_f(float a)
void RNA_property_float_get_default_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
size_t BLF_str_offset_from_cursor_position(int fontid, const char *str, size_t str_len, int location_x) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:713
void BLI_path_sequence_encode(char *path, size_t path_maxncpy, const char *head, const char *tail, unsigned short numlen, int pic)
void CTX_wm_manager_set(bContext *C, wmWindowManager *wm)
@ KM_DBL_CLICK
Definition WM_types.hh:327
@ KM_RELEASE
Definition WM_types.hh:325
void BLI_rctf_translate(struct rctf *rect, float x, float y)
Definition rct.cc:587
Brush * BKE_paint_brush(Paint *paint)
double BLI_time_now_seconds()
Definition time.cc:30
bool BKE_curveprofile_move_point(struct CurveProfile *profile, struct CurveProfilePoint *point, bool snap, const float delta[2])
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
Scene * CTX_data_scene(const bContext *C)
int BLI_sortutil_cmp_int(const void *a_, const void *b_)
Definition sort_utils.cc:55
bScreen * CTX_wm_screen(const bContext *C)
void CTX_wm_region_set(bContext *C, ARegion *region)
bool BLI_rctf_isect_rect_y(const struct rctf *src1, const struct rctf *src2, float range_y[2])
void CTX_data_main_set(bContext *C, Main *bmain)
void WM_cursor_set(wmWindow *win, int curs)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
void RNA_property_float_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, float value)
wmTimer * WM_event_timer_add(wmWindowManager *wm, wmWindow *win, const wmEventType event_type, const double time_step)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
bool WM_cursor_modal_is_set_ok(const wmWindow *win)
BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
Definition BLI_rect.hh:186
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_str_rstrip_digits(char *str) ATTR_NONNULL()
void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, bool value)
void CTX_store_set(bContext *C, const bContextStore *store)
@ PROP_PERCENTAGE
Definition RNA_types.hh:307
@ PROP_LAYER_MEMBER
Definition RNA_types.hh:335
@ PROP_PASSWORD
Definition RNA_types.hh:298
@ PROP_COLOR_GAMMA
Definition RNA_types.hh:329
ReportList * CTX_wm_reports(const bContext *C)
void WM_event_add_mousemove(wmWindow *win)
void WM_cursor_warp(wmWindow *win, int x, int y)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
@ B_UNIT_AREA
Definition BKE_unit.hh:167
@ B_UNIT_LENGTH
Definition BKE_unit.hh:166
@ B_UNIT_VOLUME
Definition BKE_unit.hh:168
bool BLI_rctf_isect_y(const rctf *rect, float y)
Definition rct.cc:106
void RNA_property_int_range(PointerRNA *ptr, PropertyRNA *prop, int *hardmin, int *hardmax)
int RNA_property_int_get_default_index(PointerRNA *ptr, PropertyRNA *prop, int index)
bScreen * WM_window_get_active_screen(const wmWindow *win)
size_t BLI_snprintf_utf8_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BKE_curveprofile_update(struct CurveProfile *profile, int update_flags)
MINLINE int min_ii(int a, int b)
MINLINE float normalize_v2_v2(float r[2], const float a[2])
void WM_tooltip_immediate_init(bContext *C, wmWindow *win, ScrArea *area, ARegion *region, wmTooltipInitFn init)
Definition wm_tooltip.cc:31
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
bool BLI_rctf_isect_segment(const struct rctf *rect, const float s1[2], const float s2[2])
void ED_region_tag_redraw_no_rebuild(ARegion *region)
Definition area.cc:727
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.hh:194
void BKE_curveprofile_free_data(struct CurveProfile *profile)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
void BKE_curveprofile_activate_nearest_point(struct CurveProfile *profile, const int i_last)
int RNA_property_int_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
void BLI_rctf_pad(struct rctf *rect, float pad_x, float pad_y)
Definition rct.cc:651
void wm_event_init_from_window(wmWindow *win, wmEvent *event)
void wm_event_free_handler(wmEventHandler *handler)
size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes) ATTR_NONNULL(1
void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin, float *hardmax)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE float clamp_f(float value, float min, float max)
void wm_event_handler_ui_cancel_ex(bContext *C, wmWindow *win, ARegion *region, bool reactivate_button)
void CTX_wm_area_set(bContext *C, ScrArea *area)
void BLI_str_cursor_step_bounds_utf8(const char *str, int str_maxlen, int pos, int *r_start, int *r_end)
void WM_operator_name_call_ptr_with_depends_on_cursor(bContext *C, wmOperatorType *ot, wm::OpCallContext opcontext, PointerRNA *properties, const wmEvent *event, const StringRef drawstr)
vector snap(vector a, vector b)
Definition node_math.h:147
#define fabsf
#define sqrtf
#define sinf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
ListBaseT< uiList > ui_lists
struct ARegion * next
bke::ARegionRuntime * runtime
eRegion_Type regiontype
struct BMLoop * next
eCurveMapPoint_Flag flag
CurveMapPoint * curve
CurveMapPoint * table
eCurveMappingFlags flag
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
char idname[BKE_ST_MAXNAME]
MenuTypeFlag flag
struct MovieTrackingTrack * track
struct MovieTrackingMarker * marker
ListBaseT< PaletteColor > colors
char idname[BKE_ST_MAXNAME]
StructRNA * type
Definition RNA_types.hh:63
Vector< AncestorPointerRNA, ANCESTOR_POINTERRNA_DEFAULT_SIZE > ancestors
Definition RNA_types.hh:90
struct UnitSettings unit
ListBaseT< ARegion > regionbase
std::variant< PointerRNA, std::string, int64_t > value
Vector< bContextStoreEntry > entries
struct ARegion * active_region
ListBaseT< struct ScrArea > areabase
ListBaseT< ARegion > regionbase
struct wmTooltipState * tool_tip
ListBaseT< ui::Block > uiblocks
ListBaseT< wmEventHandler > handlers
ListBaseT< wmEventHandler > modalhandlers
BlockInteraction_Handle * custom_interaction_handle
std::optional< bContextStore > context
std::function< void(bContext &, StringRefNull)> rename_func
std::function< void(bContext &)> apply_func
BlockInteraction_CallbackData custom_interaction_callbacks
std::function< void(StringRefNull)> rename_full_func
char undostr[BKE_UNDO_STR_MAX]
std::shared_ptr< void > search_arg
ListBaseT< ViewLink > views
int but_index(const Button *but) const
Definition interface.cc:276
ListBaseT< SafetyRect > saferct
int(*) block_event_func(const bContext *C, Block *, const wmEvent *)
Vector< std::unique_ptr< Button > > buttons_ptrs
Button * prev_but(const Button *but) const
Definition interface.cc:294
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
BlockHandleFunc handle_func
const UnitSettings * unit
Button * next_but(const Button *but) const
Definition interface.cc:288
std::unique_ptr< PieMenuData > pie_data
PopupBlockHandle * handle
BlockInteraction_CallbackData custom_interaction_callbacks
wmOperatorCallParams * optype_params
ButtonSearchCreateFn popup_create_fn
std::shared_ptr< void > arg
void line_scroll_set(int line_scroll)
std::unique_ptr< TextWrapCache > wrap_cache
wm::OpCallContext opcontext
ButtonArgNFree func_argN_free_fn
ButtonHandleFunc func
std::function< void(bContext &)> apply_func
ButtonCompleteFunc autocomplete_func
ButtonHandleHoldFunc hold_func
RadialDirection pie_dir
MenuCreateFunc menu_create_func
ButtonArgNCopy func_argN_copy_fn
HandleButtonData * semi_modal_state
BlockCreateFunc block_create_func
wmOperatorType * optype
const bContextStore * context
HandleButtonData * active
ButtonHandleNFunc funcN
ListBaseT< ButtonExtraOpIcon > extra_op_icons
BlockInteraction_Handle * custom_interaction_handle
enum blender::ui::HandleButtonMulti::@237201252321202235361334075032027002051041001241 init
void(*) cancel_func(bContext *C, void *arg)
void(*) popup_func(bContext *C, void *arg, int event)
UndoStack_Text * undo_stack_text
Vector< uiSelectContextElem > elems
wmUIHandlerRemoveFunc remove_fn
eWM_EventHandlerFlag flag
eWM_EventHandlerType type
wmEventType prev_press_type
Definition WM_types.hh:885
eWM_EventFlag flag
Definition WM_types.hh:848
int prev_press_xy[2]
Definition WM_types.hh:890
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
wm::OpCallContext opcontext
Definition WM_types.hh:1246
bool(*) check(bContext *C, wmOperator *op)
Definition WM_types.hh:1133
struct wmOperatorType * type
bke::WindowRuntime * runtime
uiFontStyle widget
#define ISMOUSE_BUTTON(event_type)
#define ISMOUSE_MOTION(event_type)
#define ISTIMER(event_type)
#define ISKEYBOARD(event_type)
#define ISHOTKEY(event_type)