From cdd4beeb5ee4559ff6ce3ad9ef2066b09a6bb9bd Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Mon, 5 Jun 2023 12:45:33 +0200 Subject: [PATCH] Cleanup: Use C++ functor for button pushed query callback C-style callbacks often rely on `void` pointer arguments that are unsafe because of the removed type. C++ functors allow passing arbitrary data along the callback, plus convenient features like defining the callback using a lambda. Didn't port the `typedef` because it doesn't add much in this case, just hides the type from the reader who has to look it up first. Note that this function isn't used in the main branch currently. --- source/blender/editors/include/UI_interface.h | 3 --- source/blender/editors/include/UI_interface.hh | 4 ++++ source/blender/editors/interface/interface.cc | 5 ++--- source/blender/editors/interface/interface_intern.hh | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h index 2b7616d3c84..845a04c2f03 100644 --- a/source/blender/editors/include/UI_interface.h +++ b/source/blender/editors/include/UI_interface.h @@ -552,7 +552,6 @@ typedef void (*uiButSearchListenFn)(const struct wmRegionListenerParams *params, /* Must return allocated string. */ typedef char *(*uiButToolTipFunc)(struct bContext *C, void *argN, const char *tip); -typedef int (*uiButPushedStateFunc)(struct uiBut *but, const void *arg); typedef void (*uiBlockHandleFunc)(struct bContext *C, void *arg, int event); @@ -1758,8 +1757,6 @@ void UI_but_focus_on_enter_event(struct wmWindow *win, uiBut *but); void UI_but_func_hold_set(uiBut *but, uiButHandleHoldFunc func, void *argN); -void UI_but_func_pushed_state_set(uiBut *but, uiButPushedStateFunc func, const void *arg); - struct PointerRNA *UI_but_extra_operator_icon_add(uiBut *but, const char *opname, wmOperatorCallContext opcontext, diff --git a/source/blender/editors/include/UI_interface.hh b/source/blender/editors/include/UI_interface.hh index a1ab5387239..bb7583ba640 100644 --- a/source/blender/editors/include/UI_interface.hh +++ b/source/blender/editors/include/UI_interface.hh @@ -8,6 +8,7 @@ #pragma once +#include #include #include "BLI_function_ref.hh" @@ -25,6 +26,7 @@ struct bContext; struct PointerRNA; struct StructRNA; struct uiBlock; +struct uiBut; struct uiLayout; struct uiList; struct uiSearchItems; @@ -37,6 +39,8 @@ namespace blender::ui { class AbstractGridView; class AbstractTreeView; +void UI_but_func_pushed_state_set(uiBut *but, std::function func); + /** * An item in a breadcrumb-like context. Currently this struct is very simple, but more * could be added to it in the future, to support interactivity or tooltips, for example. diff --git a/source/blender/editors/interface/interface.cc b/source/blender/editors/interface/interface.cc index 6f7861c5174..07b05fd5bdf 100644 --- a/source/blender/editors/interface/interface.cc +++ b/source/blender/editors/interface/interface.cc @@ -2204,7 +2204,7 @@ int ui_but_is_pushed_ex(uiBut *but, double *value) { int is_push = 0; if (but->pushed_state_func) { - return but->pushed_state_func(but, but->pushed_state_arg); + return but->pushed_state_func(*but); } if (but->bit) { @@ -6089,10 +6089,9 @@ void UI_but_func_tooltip_set(uiBut *but, uiButToolTipFunc func, void *arg, uiFre but->tip_arg_free = free_arg; } -void UI_but_func_pushed_state_set(uiBut *but, uiButPushedStateFunc func, const void *arg) +void UI_but_func_pushed_state_set(uiBut *but, std::function func) { but->pushed_state_func = func; - but->pushed_state_arg = arg; ui_but_update(but); } diff --git a/source/blender/editors/interface/interface_intern.hh b/source/blender/editors/interface/interface_intern.hh index f3cfd833bb6..a919b067255 100644 --- a/source/blender/editors/interface/interface_intern.hh +++ b/source/blender/editors/interface/interface_intern.hh @@ -267,8 +267,7 @@ struct uiBut { double *editval = nullptr; float *editvec = nullptr; - uiButPushedStateFunc pushed_state_func = nullptr; - const void *pushed_state_arg = nullptr; + std::function pushed_state_func; /** Little indicator (e.g., counter) displayed on top of some icons. */ IconTextOverlay icon_overlay_text = {};