1
1

Compare commits

...

1 Commits

Author SHA1 Message Date
2935fd5f4d Cleanup: Convert interface_region_search.cc to C++ 2021-11-10 14:42:59 -06:00
3 changed files with 59 additions and 51 deletions

View File

@@ -66,7 +66,7 @@ set(SRC
interface_region_menu_popup.c interface_region_menu_popup.c
interface_region_popover.c interface_region_popover.c
interface_region_popup.c interface_region_popup.c
interface_region_search.c interface_region_search.cc
interface_region_tooltip.c interface_region_tooltip.c
interface_regions.c interface_regions.c
interface_style.c interface_style.c

View File

@@ -23,9 +23,9 @@
* Search Box Region & Interaction * Search Box Region & Interaction
*/ */
#include <stdarg.h> #include <cstdarg>
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#include "DNA_ID.h" #include "DNA_ID.h"
#include "MEM_guardedalloc.h" #include "MEM_guardedalloc.h"
@@ -84,7 +84,7 @@ struct uiSearchItems {
void *active; void *active;
}; };
typedef struct uiSearchboxData { struct uiSearchboxData {
rcti bbox; rcti bbox;
uiFontStyle fstyle; uiFontStyle fstyle;
uiSearchItems items; uiSearchItems items;
@@ -102,7 +102,7 @@ typedef struct uiSearchboxData {
* Used so we can show leading text to menu items less prominently (not related to 'use_sep'). * Used so we can show leading text to menu items less prominently (not related to 'use_sep').
*/ */
const char *sep_string; const char *sep_string;
} uiSearchboxData; };
#define SEARCH_ITEMS 10 #define SEARCH_ITEMS 10
@@ -166,9 +166,9 @@ bool UI_search_item_add(uiSearchItems *items,
if (name_prefix_offset != 0) { if (name_prefix_offset != 0) {
/* Lazy initialize, as this isn't used often. */ /* Lazy initialize, as this isn't used often. */
if (items->name_prefix_offsets == NULL) { if (items->name_prefix_offsets == nullptr) {
items->name_prefix_offsets = MEM_callocN( items->name_prefix_offsets = (uint8_t *)MEM_callocN(
items->maxitem * sizeof(*items->name_prefix_offsets), "search name prefix offsets"); items->maxitem * sizeof(*items->name_prefix_offsets), __func__);
} }
items->name_prefix_offsets[items->totitem] = name_prefix_offset; items->name_prefix_offsets[items->totitem] = name_prefix_offset;
} }
@@ -198,7 +198,7 @@ int UI_searchbox_size_x(void)
int UI_search_items_find_index(uiSearchItems *items, const char *name) int UI_search_items_find_index(uiSearchItems *items, const char *name)
{ {
if (items->name_prefix_offsets != NULL) { if (items->name_prefix_offsets != nullptr) {
for (int i = 0; i < items->totitem; i++) { for (int i = 0; i < items->totitem; i++) {
if (STREQ(name, items->names[i] + items->name_prefix_offsets[i])) { if (STREQ(name, items->names[i] + items->name_prefix_offsets[i])) {
return i; return i;
@@ -218,7 +218,7 @@ int UI_search_items_find_index(uiSearchItems *items, const char *name)
/* region is the search box itself */ /* region is the search box itself */
static void ui_searchbox_select(bContext *C, ARegion *region, uiBut *but, int step) static void ui_searchbox_select(bContext *C, ARegion *region, uiBut *but, int step)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
/* apply step */ /* apply step */
data->active += step; data->active += step;
@@ -285,14 +285,14 @@ static void ui_searchbox_butrect(rcti *r_rect, uiSearchboxData *data, int itemnr
int ui_searchbox_find_index(ARegion *region, const char *name) int ui_searchbox_find_index(ARegion *region, const char *name)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
return UI_search_items_find_index(&data->items, name); return UI_search_items_find_index(&data->items, name);
} }
/* x and y in screen-coords. */ /* x and y in screen-coords. */
bool ui_searchbox_inside(ARegion *region, const int xy[2]) bool ui_searchbox_inside(ARegion *region, const int xy[2])
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
return BLI_rcti_isect_pt(&data->bbox, xy[0] - region->winrct.xmin, xy[1] - region->winrct.ymin); return BLI_rcti_isect_pt(&data->bbox, xy[0] - region->winrct.xmin, xy[1] - region->winrct.ymin);
} }
@@ -300,12 +300,12 @@ bool ui_searchbox_inside(ARegion *region, const int xy[2])
/* string validated to be of correct length (but->hardmax) */ /* string validated to be of correct length (but->hardmax) */
bool ui_searchbox_apply(uiBut *but, ARegion *region) bool ui_searchbox_apply(uiBut *but, ARegion *region)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
uiButSearch *search_but = (uiButSearch *)but; uiButSearch *search_but = (uiButSearch *)but;
BLI_assert(but->type == UI_BTYPE_SEARCH_MENU); BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
search_but->item_active = NULL; search_but->item_active = nullptr;
if (data->active != -1) { if (data->active != -1) {
const char *name = data->items.names[data->active] + const char *name = data->items.names[data->active] +
@@ -314,7 +314,7 @@ bool ui_searchbox_apply(uiBut *but, ARegion *region)
data->items.name_prefix_offsets[data->active] : data->items.name_prefix_offsets[data->active] :
0); 0);
const char *name_sep = data->use_shortcut_sep ? strrchr(name, UI_SEP_CHAR) : NULL; const char *name_sep = data->use_shortcut_sep ? strrchr(name, UI_SEP_CHAR) : nullptr;
/* Search button with dynamic string properties may have their own method of applying /* Search button with dynamic string properties may have their own method of applying
* the search results, so only copy the result if there is a proper space for it. */ * the search results, so only copy the result if there is a proper space for it. */
@@ -356,7 +356,7 @@ static struct ARegion *wm_searchbox_tooltip_init(struct bContext *C,
} }
ARegion *searchbox_region = UI_region_searchbox_region_get(region); ARegion *searchbox_region = UI_region_searchbox_region_get(region);
uiSearchboxData *data = searchbox_region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(searchbox_region->regiondata);
BLI_assert(data->items.pointers[data->active] == search_but->item_active); BLI_assert(data->items.pointers[data->active] == search_but->item_active);
@@ -367,13 +367,13 @@ static struct ARegion *wm_searchbox_tooltip_init(struct bContext *C,
C, region, &rect, search_but->arg, search_but->item_active); C, region, &rect, search_but->arg, search_but->item_active);
} }
} }
return NULL; return nullptr;
} }
bool ui_searchbox_event( bool ui_searchbox_event(
bContext *C, ARegion *region, uiBut *but, ARegion *butregion, const wmEvent *event) bContext *C, ARegion *region, uiBut *but, ARegion *butregion, const wmEvent *event)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
uiButSearch *search_but = (uiButSearch *)but; uiButSearch *search_but = (uiButSearch *)but;
int type = event->type, val = event->val; int type = event->type, val = event->val;
bool handled = false; bool handled = false;
@@ -481,7 +481,7 @@ static void ui_searchbox_update_fn(bContext *C,
void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool reset) void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool reset)
{ {
uiButSearch *search_but = (uiButSearch *)but; uiButSearch *search_but = (uiButSearch *)but;
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
BLI_assert(but->type == UI_BTYPE_SEARCH_MENU); BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
@@ -499,7 +499,7 @@ void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool re
if (search_but->items_update_fn && search_but->item_active) { if (search_but->items_update_fn && search_but->item_active) {
data->items.active = search_but->item_active; data->items.active = search_but->item_active;
ui_searchbox_update_fn(C, search_but, but->editstr, &data->items); ui_searchbox_update_fn(C, search_but, but->editstr, &data->items);
data->items.active = NULL; data->items.active = nullptr;
/* found active item, calculate real offset by centering it */ /* found active item, calculate real offset by centering it */
if (data->items.totitem) { if (data->items.totitem) {
@@ -538,7 +538,7 @@ void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool re
/* Never include the prefix in the button. */ /* Never include the prefix in the button. */
(data->items.name_prefix_offsets ? data->items.name_prefix_offsets[a] : (data->items.name_prefix_offsets ? data->items.name_prefix_offsets[a] :
0); 0);
const char *name_sep = data->use_shortcut_sep ? strrchr(name, UI_SEP_CHAR) : NULL; const char *name_sep = data->use_shortcut_sep ? strrchr(name, UI_SEP_CHAR) : nullptr;
if (STREQLEN(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen)) { if (STREQLEN(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen)) {
data->active = a; data->active = a;
break; break;
@@ -558,7 +558,7 @@ void ui_searchbox_update(bContext *C, ARegion *region, uiBut *but, const bool re
int ui_searchbox_autocomplete(bContext *C, ARegion *region, uiBut *but, char *str) int ui_searchbox_autocomplete(bContext *C, ARegion *region, uiBut *but, char *str)
{ {
uiButSearch *search_but = (uiButSearch *)but; uiButSearch *search_but = (uiButSearch *)but;
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
int match = AUTOCOMPLETE_NO_MATCH; int match = AUTOCOMPLETE_NO_MATCH;
BLI_assert(but->type == UI_BTYPE_SEARCH_MENU); BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
@@ -569,7 +569,7 @@ int ui_searchbox_autocomplete(bContext *C, ARegion *region, uiBut *but, char *st
ui_searchbox_update_fn(C, search_but, but->editstr, &data->items); ui_searchbox_update_fn(C, search_but, but->editstr, &data->items);
match = UI_autocomplete_end(data->items.autocpl, str); match = UI_autocomplete_end(data->items.autocpl, str);
data->items.autocpl = NULL; data->items.autocpl = nullptr;
} }
return match; return match;
@@ -577,7 +577,7 @@ int ui_searchbox_autocomplete(bContext *C, ARegion *region, uiBut *but, char *st
static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region) static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
/* pixel space */ /* pixel space */
wmOrtho2_region_pixelspace(region); wmOrtho2_region_pixelspace(region);
@@ -630,7 +630,7 @@ static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
const int state = ((a == data->active) ? UI_ACTIVE : 0) | data->items.states[a]; const int state = ((a == data->active) ? UI_ACTIVE : 0) | data->items.states[a];
char *name = data->items.names[a]; char *name = data->items.names[a];
int icon = data->items.icons[a]; int icon = data->items.icons[a];
char *name_sep_test = NULL; char *name_sep_test = nullptr;
uiMenuItemSeparatorType separator_type = UI_MENU_ITEM_SEPARATOR_NONE; uiMenuItemSeparatorType separator_type = UI_MENU_ITEM_SEPARATOR_NONE;
if (data->use_shortcut_sep) { if (data->use_shortcut_sep) {
@@ -652,15 +652,15 @@ static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
} }
/* Simple menu item. */ /* Simple menu item. */
ui_draw_menu_item(&data->fstyle, &rect, name, icon, state, separator_type, NULL); ui_draw_menu_item(&data->fstyle, &rect, name, icon, state, separator_type, nullptr);
} }
else { else {
/* Split menu item, faded text before the separator. */ /* Split menu item, faded text before the separator. */
char *name_sep = NULL; char *name_sep = nullptr;
do { do {
name_sep = name_sep_test; name_sep = name_sep_test;
name_sep_test = strstr(name_sep + search_sep_len, data->sep_string); name_sep_test = strstr(name_sep + search_sep_len, data->sep_string);
} while (name_sep_test != NULL); } while (name_sep_test != nullptr);
name_sep += search_sep_len; name_sep += search_sep_len;
const char name_sep_prev = *name_sep; const char name_sep_prev = *name_sep;
@@ -683,7 +683,7 @@ static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
} }
/* The previous menu item draws the active selection. */ /* The previous menu item draws the active selection. */
ui_draw_menu_item(&data->fstyle, &rect, name_sep, icon, state, separator_type, NULL); ui_draw_menu_item(&data->fstyle, &rect, name_sep, icon, state, separator_type, nullptr);
} }
} }
/* indicate more */ /* indicate more */
@@ -705,7 +705,7 @@ static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
static void ui_searchbox_region_free_fn(ARegion *region) static void ui_searchbox_region_free_fn(ARegion *region)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
/* free search data */ /* free search data */
for (int a = 0; a < data->items.maxitem; a++) { for (int a = 0; a < data->items.maxitem; a++) {
@@ -716,12 +716,12 @@ static void ui_searchbox_region_free_fn(ARegion *region)
MEM_freeN(data->items.icons); MEM_freeN(data->items.icons);
MEM_freeN(data->items.states); MEM_freeN(data->items.states);
if (data->items.name_prefix_offsets != NULL) { if (data->items.name_prefix_offsets != nullptr) {
MEM_freeN(data->items.name_prefix_offsets); MEM_freeN(data->items.name_prefix_offsets);
} }
MEM_freeN(data); MEM_freeN(data);
region->regiondata = NULL; region->regiondata = nullptr;
} }
static ARegion *ui_searchbox_create_generic_ex(bContext *C, static ARegion *ui_searchbox_create_generic_ex(bContext *C,
@@ -746,7 +746,7 @@ static ARegion *ui_searchbox_create_generic_ex(bContext *C,
region->type = &type; region->type = &type;
/* create searchbox data */ /* create searchbox data */
uiSearchboxData *data = MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData"); uiSearchboxData *data = (uiSearchboxData *)MEM_callocN(sizeof(uiSearchboxData), __func__);
/* set font, get bb */ /* set font, get bb */
data->fstyle = style->widget; /* copy struct */ data->fstyle = style->widget; /* copy struct */
@@ -767,7 +767,7 @@ static ARegion *ui_searchbox_create_generic_ex(bContext *C,
data->prv_cols = but->a2; data->prv_cols = but->a2;
} }
if (but->optype != NULL || use_shortcut_sep) { if (but->optype != nullptr || use_shortcut_sep) {
data->use_shortcut_sep = true; data->use_shortcut_sep = true;
} }
data->sep_string = search_but->item_sep_string; data->sep_string = search_but->item_sep_string;
@@ -881,13 +881,13 @@ static ARegion *ui_searchbox_create_generic_ex(bContext *C,
/* In case the button's string is dynamic, make sure there are buffers available. */ /* In case the button's string is dynamic, make sure there are buffers available. */
data->items.maxstrlen = but->hardmax == 0 ? UI_MAX_NAME_STR : but->hardmax; data->items.maxstrlen = but->hardmax == 0 ? UI_MAX_NAME_STR : but->hardmax;
data->items.totitem = 0; data->items.totitem = 0;
data->items.names = MEM_callocN(data->items.maxitem * sizeof(void *), "search names"); data->items.names = (char **)MEM_callocN(data->items.maxitem * sizeof(void *), __func__);
data->items.pointers = MEM_callocN(data->items.maxitem * sizeof(void *), "search pointers"); data->items.pointers = (void **)MEM_callocN(data->items.maxitem * sizeof(void *), __func__);
data->items.icons = MEM_callocN(data->items.maxitem * sizeof(int), "search icons"); data->items.icons = (int *)MEM_callocN(data->items.maxitem * sizeof(int), __func__);
data->items.states = MEM_callocN(data->items.maxitem * sizeof(int), "search flags"); data->items.states = (int *)MEM_callocN(data->items.maxitem * sizeof(int), __func__);
data->items.name_prefix_offsets = NULL; /* Lazy initialized as needed. */ data->items.name_prefix_offsets = nullptr; /* Lazy initialized as needed. */
for (int i = 0; i < data->items.maxitem; i++) { for (int i = 0; i < data->items.maxitem; i++) {
data->items.names[i] = MEM_callocN(data->items.maxstrlen + 1, "search pointers"); data->items.names[i] = (char *)MEM_callocN(data->items.maxstrlen + 1, __func__);
} }
return region; return region;
@@ -924,7 +924,7 @@ static void str_tolower_titlecaps_ascii(char *str, const size_t len)
static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARegion *region) static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARegion *region)
{ {
uiSearchboxData *data = region->regiondata; uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
/* pixel space */ /* pixel space */
wmOrtho2_region_pixelspace(region); wmOrtho2_region_pixelspace(region);
@@ -952,10 +952,10 @@ static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARe
{ {
const int state = ((a == data->active) ? UI_ACTIVE : 0) | data->items.states[a]; const int state = ((a == data->active) ? UI_ACTIVE : 0) | data->items.states[a];
wmOperatorType *ot = data->items.pointers[a]; wmOperatorType *ot = static_cast<wmOperatorType *>(data->items.pointers[a]);
char text_pre[128]; char text_pre[128];
char *text_pre_p = strstr(ot->idname, "_OT_"); const char *text_pre_p = strstr(ot->idname, "_OT_");
if (text_pre_p == NULL) { if (text_pre_p == nullptr) {
text_pre[0] = '\0'; text_pre[0] = '\0';
} }
else { else {
@@ -975,7 +975,7 @@ static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARe
data->items.icons[a], data->items.icons[a],
state, state,
UI_MENU_ITEM_SEPARATOR_NONE, UI_MENU_ITEM_SEPARATOR_NONE,
NULL); nullptr);
ui_draw_menu_item(&data->fstyle, ui_draw_menu_item(&data->fstyle,
&rect_post, &rect_post,
data->items.names[a], data->items.names[a],
@@ -983,7 +983,7 @@ static void ui_searchbox_region_draw_cb__operator(const bContext *UNUSED(C), ARe
state, state,
data->use_shortcut_sep ? UI_MENU_ITEM_SEPARATOR_SHORTCUT : data->use_shortcut_sep ? UI_MENU_ITEM_SEPARATOR_SHORTCUT :
UI_MENU_ITEM_SEPARATOR_NONE, UI_MENU_ITEM_SEPARATOR_NONE,
NULL); nullptr);
} }
} }
/* indicate more */ /* indicate more */
@@ -1044,17 +1044,17 @@ void ui_but_search_refresh(uiButSearch *search_but)
return; return;
} }
uiSearchItems *items = MEM_callocN(sizeof(uiSearchItems), "search items"); uiSearchItems *items = (uiSearchItems *)MEM_callocN(sizeof(uiSearchItems), __func__);
/* setup search struct */ /* setup search struct */
items->maxitem = 10; items->maxitem = 10;
items->maxstrlen = 256; items->maxstrlen = 256;
items->names = MEM_callocN(items->maxitem * sizeof(void *), "search names"); items->names = (char **)MEM_callocN(items->maxitem * sizeof(void *), __func__);
for (int i = 0; i < items->maxitem; i++) { for (int i = 0; i < items->maxitem; i++) {
items->names[i] = MEM_callocN(but->hardmax + 1, "search names"); items->names[i] = (char *)MEM_callocN(but->hardmax + 1, __func__);
} }
ui_searchbox_update_fn(but->block->evil_C, search_but, but->drawstr, items); ui_searchbox_update_fn((bContext *)but->block->evil_C, search_but, but->drawstr, items);
if (!search_but->results_are_suggestions) { if (!search_but->results_are_suggestions) {
/* Only red-alert when we are sure of it, this can miss cases when >10 matches. */ /* Only red-alert when we are sure of it, this can miss cases when >10 matches. */

View File

@@ -22,9 +22,17 @@
#pragma once #pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* interface_region_menu_popup.c */ /* interface_region_menu_popup.c */
uint ui_popup_menu_hash(const char *str); uint ui_popup_menu_hash(const char *str);
/* interface_regions_intern.h */ /* interface_regions_intern.h */
ARegion *ui_region_temp_add(bScreen *screen); ARegion *ui_region_temp_add(bScreen *screen);
void ui_region_temp_remove(struct bContext *C, bScreen *screen, ARegion *region); void ui_region_temp_remove(struct bContext *C, bScreen *screen, ARegion *region);
#ifdef __cplusplus
}
#endif