Cleanup: split button search callbacks into their own struct
This commit is contained in:
parent
7181342e62
commit
74c01afe22
|
@ -502,15 +502,16 @@ typedef void (*uiButHandleRenameFunc)(struct bContext *C, void *arg, char *origs
|
|||
typedef void (*uiButHandleNFunc)(struct bContext *C, void *argN, void *arg2);
|
||||
typedef void (*uiButHandleHoldFunc)(struct bContext *C, struct ARegion *butregion, uiBut *but);
|
||||
typedef int (*uiButCompleteFunc)(struct bContext *C, char *str, void *arg);
|
||||
typedef struct ARegion *(*uiButSearchCreateFunc)(struct bContext *C,
|
||||
struct ARegion *butregion,
|
||||
uiBut *but);
|
||||
typedef void (*uiButSearchFunc)(const struct bContext *C,
|
||||
void *arg,
|
||||
const char *str,
|
||||
uiSearchItems *items);
|
||||
|
||||
typedef void (*uiButSearchArgFreeFunc)(void *arg);
|
||||
/* Search types. */
|
||||
typedef struct ARegion *(*uiButSearchCreateFn)(struct bContext *C,
|
||||
struct ARegion *butregion,
|
||||
uiBut *but);
|
||||
typedef void (*uiButSearchUpdateFn)(const struct bContext *C,
|
||||
void *arg,
|
||||
const char *str,
|
||||
uiSearchItems *items);
|
||||
typedef void (*uiButSearchArgFreeFn)(void *arg);
|
||||
|
||||
/* Must return allocated string. */
|
||||
typedef char *(*uiButToolTipFunc)(struct bContext *C, void *argN, const char *tip);
|
||||
|
@ -1573,10 +1574,10 @@ eAutoPropButsReturn uiDefAutoButsRNA(uiLayout *layout,
|
|||
bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid, int state);
|
||||
/* bfunc gets search item *poin as arg2, or if NULL the old string */
|
||||
void UI_but_func_search_set(uiBut *but,
|
||||
uiButSearchCreateFunc cfunc,
|
||||
uiButSearchFunc sfunc,
|
||||
uiButSearchCreateFn search_create_fn,
|
||||
uiButSearchUpdateFn search_update_fn,
|
||||
void *arg,
|
||||
uiButSearchArgFreeFunc search_arg_free_func,
|
||||
uiButSearchArgFreeFn search_arg_free_fn,
|
||||
uiButHandleFunc bfunc,
|
||||
const char *search_sep_string,
|
||||
void *active);
|
||||
|
|
|
@ -807,8 +807,7 @@ static bool ui_but_update_from_old_block(const bContext *C,
|
|||
|
||||
SWAP(ListBase, but->extra_op_icons, oldbut->extra_op_icons);
|
||||
|
||||
SWAP(uiButSearchArgFreeFunc, oldbut->search_arg_free_func, but->search_arg_free_func);
|
||||
SWAP(void *, oldbut->search_arg, but->search_arg);
|
||||
SWAP(struct uiButSearchData *, oldbut->search, but->search);
|
||||
|
||||
/* copy hardmin for list rows to prevent 'sticking' highlight to mouse position
|
||||
* when scrolling without moving mouse (see [#28432]) */
|
||||
|
@ -3227,9 +3226,12 @@ static void ui_but_free(const bContext *C, uiBut *but)
|
|||
MEM_freeN(but->hold_argN);
|
||||
}
|
||||
|
||||
if (but->search_arg_free_func) {
|
||||
but->search_arg_free_func(but->search_arg);
|
||||
but->search_arg = NULL;
|
||||
if (but->search != NULL) {
|
||||
if (but->search->arg_free_fn) {
|
||||
but->search->arg_free_fn(but->search->arg);
|
||||
but->search->arg = NULL;
|
||||
}
|
||||
MEM_freeN(but->search);
|
||||
}
|
||||
|
||||
if (but->active) {
|
||||
|
@ -6356,31 +6358,38 @@ uiBut *uiDefSearchBut(uiBlock *block,
|
|||
* showing the icon and highlighted text after the last instance of this string.
|
||||
*/
|
||||
void UI_but_func_search_set(uiBut *but,
|
||||
uiButSearchCreateFunc search_create_func,
|
||||
uiButSearchFunc search_func,
|
||||
uiButSearchCreateFn search_create_fn,
|
||||
uiButSearchUpdateFn search_update_fn,
|
||||
void *arg,
|
||||
uiButSearchArgFreeFunc search_arg_free_func,
|
||||
uiButSearchArgFreeFn search_arg_free_fn,
|
||||
uiButHandleFunc bfunc,
|
||||
const char *search_sep_string,
|
||||
void *active)
|
||||
{
|
||||
/* needed since callers don't have access to internal functions
|
||||
* (as an alternative we could expose it) */
|
||||
if (search_create_func == NULL) {
|
||||
search_create_func = ui_searchbox_create_generic;
|
||||
if (search_create_fn == NULL) {
|
||||
search_create_fn = ui_searchbox_create_generic;
|
||||
}
|
||||
|
||||
if (but->search_arg_free_func != NULL) {
|
||||
but->search_arg_free_func(but->search_arg);
|
||||
but->search_arg = NULL;
|
||||
struct uiButSearchData *search = but->search;
|
||||
if (search != NULL) {
|
||||
if (search->arg_free_fn != NULL) {
|
||||
search->arg_free_fn(but->search->arg);
|
||||
search->arg = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
search = MEM_callocN(sizeof(*but->search), __func__);
|
||||
but->search = search;
|
||||
}
|
||||
|
||||
but->search_create_func = search_create_func;
|
||||
but->search_func = search_func;
|
||||
search->create_fn = search_create_fn;
|
||||
search->update_fn = search_update_fn;
|
||||
|
||||
but->search_arg = arg;
|
||||
but->search_arg_free_func = search_arg_free_func;
|
||||
but->search_sep_string = search_sep_string;
|
||||
search->arg = arg;
|
||||
search->arg_free_fn = search_arg_free_fn;
|
||||
search->sep_string = search_sep_string;
|
||||
|
||||
if (bfunc) {
|
||||
#ifdef DEBUG
|
||||
|
|
|
@ -962,7 +962,7 @@ bool ui_popup_context_menu_for_button(bContext *C, uiBut *but)
|
|||
const PropertyType prop_type = RNA_property_type(but->rnaprop);
|
||||
if (((prop_type == PROP_POINTER) ||
|
||||
(prop_type == PROP_STRING && but->type == UI_BTYPE_SEARCH_MENU &&
|
||||
but->search_func == ui_rna_collection_search_cb)) &&
|
||||
but->search->update_fn == ui_rna_collection_search_update_fn)) &&
|
||||
ui_jump_to_target_button_poll(C)) {
|
||||
uiItemO(layout,
|
||||
CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Jump to Target"),
|
||||
|
|
|
@ -417,7 +417,7 @@ typedef struct uiAfterFunc {
|
|||
PropertyRNA *rnaprop;
|
||||
|
||||
void *search_arg;
|
||||
uiButSearchArgFreeFunc search_arg_free_func;
|
||||
uiButSearchArgFreeFn search_arg_free_fn;
|
||||
|
||||
bContextStore *context;
|
||||
|
||||
|
@ -753,10 +753,12 @@ static void ui_apply_but_func(bContext *C, uiBut *but)
|
|||
after->rnapoin = but->rnapoin;
|
||||
after->rnaprop = but->rnaprop;
|
||||
|
||||
after->search_arg_free_func = but->search_arg_free_func;
|
||||
after->search_arg = but->search_arg;
|
||||
but->search_arg_free_func = NULL;
|
||||
but->search_arg = NULL;
|
||||
if (but->search != NULL) {
|
||||
after->search_arg_free_fn = but->search->arg_free_fn;
|
||||
after->search_arg = but->search->arg;
|
||||
but->search->arg_free_fn = NULL;
|
||||
but->search->arg = NULL;
|
||||
}
|
||||
|
||||
if (but->context) {
|
||||
after->context = CTX_store_copy(but->context);
|
||||
|
@ -924,8 +926,8 @@ static void ui_apply_but_funcs_after(bContext *C)
|
|||
MEM_freeN(after.rename_orig);
|
||||
}
|
||||
|
||||
if (after.search_arg_free_func) {
|
||||
after.search_arg_free_func(after.search_arg);
|
||||
if (after.search_arg_free_fn) {
|
||||
after.search_arg_free_fn(after.search_arg);
|
||||
}
|
||||
|
||||
ui_afterfunc_update_preferences_dirty(&after);
|
||||
|
@ -3308,7 +3310,7 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
|
|||
|
||||
/* optional searchbox */
|
||||
if (but->type == UI_BTYPE_SEARCH_MENU) {
|
||||
data->searchbox = but->search_create_func(C, data->region, but);
|
||||
data->searchbox = but->search->create_fn(C, data->region, but);
|
||||
ui_searchbox_update(C, data->searchbox, but, true); /* true = reset */
|
||||
}
|
||||
|
||||
|
|
|
@ -145,6 +145,14 @@ enum {
|
|||
/* max amount of items a radial menu (pie menu) can contain */
|
||||
#define PIE_MAX_ITEMS 8
|
||||
|
||||
struct uiButSearchData {
|
||||
uiButSearchCreateFn create_fn;
|
||||
uiButSearchUpdateFn update_fn;
|
||||
void *arg;
|
||||
uiButSearchArgFreeFn arg_free_fn;
|
||||
const char *sep_string;
|
||||
};
|
||||
|
||||
struct uiBut {
|
||||
struct uiBut *next, *prev;
|
||||
int flag, drawflag;
|
||||
|
@ -201,11 +209,7 @@ struct uiBut {
|
|||
uiButCompleteFunc autocomplete_func;
|
||||
void *autofunc_arg;
|
||||
|
||||
uiButSearchCreateFunc search_create_func;
|
||||
uiButSearchFunc search_func;
|
||||
void *search_arg;
|
||||
uiButSearchArgFreeFunc search_arg_free_func;
|
||||
const char *search_sep_string;
|
||||
struct uiButSearchData *search;
|
||||
|
||||
uiButHandleRenameFunc rename_func;
|
||||
void *rename_arg1;
|
||||
|
@ -1012,7 +1016,7 @@ void UI_OT_eyedropper_gpencil_color(struct wmOperatorType *ot);
|
|||
bool ui_str_has_word_prefix(const char *haystack, const char *needle, size_t needle_len);
|
||||
|
||||
/**
|
||||
* For use with #ui_rna_collection_search_cb.
|
||||
* For use with #ui_rna_collection_search_update_fn.
|
||||
*/
|
||||
typedef struct uiRNACollectionSearch {
|
||||
PointerRNA target_ptr;
|
||||
|
@ -1027,10 +1031,10 @@ typedef struct uiRNACollectionSearch {
|
|||
/* Block has to be stored for freeing butstore (uiBut.block doesn't work with undo). */
|
||||
uiBlock *butstore_block;
|
||||
} uiRNACollectionSearch;
|
||||
void ui_rna_collection_search_cb(const struct bContext *C,
|
||||
void *arg,
|
||||
const char *str,
|
||||
uiSearchItems *items);
|
||||
void ui_rna_collection_search_update_fn(const struct bContext *C,
|
||||
void *arg,
|
||||
const char *str,
|
||||
uiSearchItems *items);
|
||||
|
||||
/* interface_ops.c */
|
||||
bool ui_jump_to_target_button_poll(struct bContext *C);
|
||||
|
|
|
@ -2697,7 +2697,7 @@ void ui_but_add_search(
|
|||
|
||||
UI_but_func_search_set(but,
|
||||
ui_searchbox_create_generic,
|
||||
ui_rna_collection_search_cb,
|
||||
ui_rna_collection_search_update_fn,
|
||||
coll_search,
|
||||
ui_rna_collection_search_free_cb,
|
||||
NULL,
|
||||
|
|
|
@ -1135,8 +1135,9 @@ static bool jump_to_target_button(bContext *C, bool poll)
|
|||
else if (type == PROP_STRING) {
|
||||
const uiBut *but = UI_context_active_but_get(C);
|
||||
|
||||
if (but->type == UI_BTYPE_SEARCH_MENU && but->search_func == ui_rna_collection_search_cb) {
|
||||
uiRNACollectionSearch *coll_search = but->search_arg;
|
||||
if (but->type == UI_BTYPE_SEARCH_MENU && but->search &&
|
||||
but->search->update_fn == ui_rna_collection_search_update_fn) {
|
||||
uiRNACollectionSearch *coll_search = but->search->arg;
|
||||
|
||||
char str_buf[MAXBONENAME];
|
||||
char *str_ptr = RNA_property_string_get_alloc(&ptr, prop, str_buf, sizeof(str_buf), NULL);
|
||||
|
|
|
@ -350,9 +350,9 @@ void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool re
|
|||
data->active = -1;
|
||||
|
||||
/* handle active */
|
||||
if (but->search_func && but->func_arg2) {
|
||||
if (but->search->update_fn && but->func_arg2) {
|
||||
data->items.active = but->func_arg2;
|
||||
but->search_func(C, but->search_arg, but->editstr, &data->items);
|
||||
but->search->update_fn(C, but->search->arg, but->editstr, &data->items);
|
||||
data->items.active = NULL;
|
||||
|
||||
/* found active item, calculate real offset by centering it */
|
||||
|
@ -381,8 +381,8 @@ void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool re
|
|||
}
|
||||
|
||||
/* callback */
|
||||
if (but->search_func) {
|
||||
but->search_func(C, but->search_arg, but->editstr, &data->items);
|
||||
if (but->search->update_fn) {
|
||||
but->search->update_fn(C, but->search->arg, but->editstr, &data->items);
|
||||
}
|
||||
|
||||
/* handle case where editstr is equal to one of items */
|
||||
|
@ -416,7 +416,7 @@ int ui_searchbox_autocomplete(bContext *C, ARegion *region, uiBut *but, char *st
|
|||
if (str[0]) {
|
||||
data->items.autocpl = UI_autocomplete_begin(str, ui_but_string_get_max_length(but));
|
||||
|
||||
but->search_func(C, but->search_arg, but->editstr, &data->items);
|
||||
but->search->update_fn(C, but->search->arg, but->editstr, &data->items);
|
||||
|
||||
match = UI_autocomplete_end(data->items.autocpl, str);
|
||||
data->items.autocpl = NULL;
|
||||
|
@ -603,7 +603,7 @@ ARegion *ui_searchbox_create_generic(bContext *C, ARegion *butregion, uiBut *but
|
|||
if (but->optype != NULL || (but->drawflag & UI_BUT_HAS_SHORTCUT) != 0) {
|
||||
data->use_sep = true;
|
||||
}
|
||||
data->sep_string = but->search_sep_string;
|
||||
data->sep_string = but->search->sep_string;
|
||||
|
||||
/* compute position */
|
||||
if (but->block->flag & UI_BLOCK_SEARCH_MENU) {
|
||||
|
@ -881,7 +881,7 @@ void ui_but_search_refresh(uiBut *but)
|
|||
items->names[x1] = MEM_callocN(but->hardmax + 1, "search names");
|
||||
}
|
||||
|
||||
but->search_func(but->block->evil_C, but->search_arg, but->drawstr, items);
|
||||
but->search->update_fn(but->block->evil_C, but->search->arg, but->drawstr, items);
|
||||
|
||||
/* only redalert when we are sure of it, this can miss cases when >10 matches */
|
||||
if (items->totitem == 0) {
|
||||
|
|
|
@ -203,7 +203,7 @@ static void template_add_button_search_menu(const bContext *C,
|
|||
|
||||
static uiBlock *template_common_search_menu(const bContext *C,
|
||||
ARegion *region,
|
||||
uiButSearchFunc search_func,
|
||||
uiButSearchUpdateFn search_update_fn,
|
||||
void *search_arg,
|
||||
uiButHandleFunc handle_func,
|
||||
void *active_item,
|
||||
|
@ -279,7 +279,7 @@ static uiBlock *template_common_search_menu(const bContext *C,
|
|||
}
|
||||
UI_but_func_search_set(but,
|
||||
ui_searchbox_create_generic,
|
||||
search_func,
|
||||
search_update_fn,
|
||||
search_arg,
|
||||
NULL,
|
||||
handle_func,
|
||||
|
@ -451,7 +451,8 @@ static uiBlock *id_search_menu(bContext *C, ARegion *region, void *arg_litem)
|
|||
{
|
||||
static TemplateID template_ui;
|
||||
PointerRNA active_item_ptr;
|
||||
void (*id_search_cb_p)(const bContext *, void *, const char *, uiSearchItems *) = id_search_cb;
|
||||
void (*id_search_update_fn)(
|
||||
const bContext *, void *, const char *, uiSearchItems *) = id_search_cb;
|
||||
|
||||
/* arg_litem is malloced, can be freed by parent button */
|
||||
template_ui = *((TemplateID *)arg_litem);
|
||||
|
@ -461,14 +462,14 @@ static uiBlock *id_search_menu(bContext *C, ARegion *region, void *arg_litem)
|
|||
/* Currently only used for objects. */
|
||||
if (template_ui.idcode == ID_OB) {
|
||||
if (template_ui.filter == UI_TEMPLATE_ID_FILTER_AVAILABLE) {
|
||||
id_search_cb_p = id_search_cb_objects_from_scene;
|
||||
id_search_update_fn = id_search_cb_objects_from_scene;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return template_common_search_menu(C,
|
||||
region,
|
||||
id_search_cb_p,
|
||||
id_search_update_fn,
|
||||
&template_ui,
|
||||
template_ID_set_property_cb,
|
||||
active_item_ptr.data,
|
||||
|
@ -1552,7 +1553,7 @@ static uiBlock *template_search_menu(bContext *C, ARegion *region, void *arg_tem
|
|||
|
||||
return template_common_search_menu(C,
|
||||
region,
|
||||
ui_rna_collection_search_cb,
|
||||
ui_rna_collection_search_update_fn,
|
||||
&template_search,
|
||||
template_search_handle_cb,
|
||||
active_ptr.data,
|
||||
|
|
|
@ -399,10 +399,10 @@ static int sort_search_items_list(const void *a, const void *b)
|
|||
}
|
||||
}
|
||||
|
||||
void ui_rna_collection_search_cb(const struct bContext *C,
|
||||
void *arg,
|
||||
const char *str,
|
||||
uiSearchItems *items)
|
||||
void ui_rna_collection_search_update_fn(const struct bContext *C,
|
||||
void *arg,
|
||||
const char *str,
|
||||
uiSearchItems *items)
|
||||
{
|
||||
uiRNACollectionSearch *data = arg;
|
||||
int i = 0, iconid = 0, flag = RNA_property_flag(data->target_prop);
|
||||
|
|
Loading…
Reference in New Issue