Blender  V3.3
interface_template_search_operator.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
10 #include <string.h>
11 
12 #include "DNA_object_types.h"
13 #include "DNA_scene_types.h"
14 #include "DNA_texture_types.h"
15 
16 #include "BLI_alloca.h"
17 #include "BLI_ghash.h"
18 #include "BLI_string.h"
19 #include "BLI_utildefines.h"
20 
21 #include "BLT_translation.h"
22 
23 #include "BKE_context.h"
24 #include "BKE_global.h"
25 
26 #include "WM_api.h"
27 #include "WM_types.h"
28 
29 #include "UI_interface.h"
30 #include "interface_intern.h"
31 
32 /* -------------------------------------------------------------------- */
36 static void operator_search_exec_fn(bContext *C, void *UNUSED(arg1), void *arg2)
37 {
38  wmOperatorType *ot = arg2;
39 
40  if (ot) {
42  }
43 }
44 
46  void *UNUSED(arg),
47  const char *str,
48  uiSearchItems *items,
49  const bool UNUSED(is_first))
50 {
51  GHashIterator iter;
52 
53  /* Prepare BLI_string_all_words_matched. */
54  const size_t str_len = strlen(str);
55  const int words_max = BLI_string_max_possible_word_count(str_len);
56  int(*words)[2] = BLI_array_alloca(words, words_max);
57  const int words_len = BLI_string_find_split_words(str, str_len, ' ', words, words_max);
58 
59  for (WM_operatortype_iter(&iter); !BLI_ghashIterator_done(&iter);
60  BLI_ghashIterator_step(&iter)) {
62  const char *ot_ui_name = CTX_IFACE_(ot->translation_context, ot->name);
63 
64  if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0) {
65  continue;
66  }
67 
68  if (BLI_string_all_words_matched(ot_ui_name, str, words, words_len)) {
69  if (WM_operator_poll((bContext *)C, ot)) {
70  char name[256];
71  const int len = strlen(ot_ui_name);
72 
73  /* display name for menu, can hold hotkey */
74  BLI_strncpy(name, ot_ui_name, sizeof(name));
75 
76  /* check for hotkey */
77  if (len < sizeof(name) - 6) {
79  ot->idname,
81  NULL,
82  true,
83  &name[len + 1],
84  sizeof(name) - len - 1)) {
85  name[len] = UI_SEP_CHAR;
86  }
87  }
88 
89  if (!UI_search_item_add(items, name, ot, ICON_NONE, 0, 0)) {
90  break;
91  }
92  }
93  }
94  }
95 }
96 
99 /* -------------------------------------------------------------------- */
104 {
108  NULL,
109  false,
110  NULL,
112  NULL);
113 }
114 
116 {
117  uiBlock *block;
118  uiBut *but;
119  static char search[256] = "";
120 
121  block = uiLayoutGetBlock(layout);
122  UI_block_layout_set_current(block, layout);
123 
124  but = uiDefSearchBut(
125  block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
127 }
128 
@ G_DEBUG_WM
Definition: BKE_global.h:179
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:22
void BLI_ghashIterator_step(GHashIterator *ghi)
Definition: BLI_ghash.c:914
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:302
BLI_INLINE bool BLI_ghashIterator_done(const GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:310
size_t int BLI_string_max_possible_word_count(int str_len)
Definition: string.c:559
bool BLI_string_all_words_matched(const char *name, const char *str, int(*words)[2], int words_len)
Definition: string.c:576
int BLI_string_find_split_words(const char *str, size_t len, char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:1049
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define UNUSED(x)
#define CTX_IFACE_(context, msgid)
Object is a sort of wrapper for general info.
#define C
Definition: RandGen.cpp:25
#define UI_UNIT_Y
uiBlock * uiLayoutGetBlock(uiLayout *layout)
#define UI_SEP_CHAR
Definition: UI_interface.h:83
void UI_but_func_search_set(uiBut *but, uiButSearchCreateFn search_create_fn, uiButSearchUpdateFn search_update_fn, void *arg, bool free_arg, uiFreeArgFunc search_arg_free_fn, uiButHandleFunc search_exec_fn, void *active)
Definition: interface.cc:6242
bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid, int but_flag, uint8_t name_prefix_offset)
uiBut * uiDefSearchBut(uiBlock *block, void *arg, int retval, int icon, int maxlen, int x, int y, short width, short height, float a1, float a2, const char *tip)
Definition: interface.cc:6217
void UI_block_layout_set_current(uiBlock *block, uiLayout *layout)
#define UI_UNIT_X
@ OPTYPE_INTERNAL
Definition: WM_types.h:168
@ WM_OP_INVOKE_DEFAULT
Definition: WM_types.h:201
@ WM_OP_EXEC_DEFAULT
Definition: WM_types.h:208
int len
Definition: draw_manager.c:108
#define str(s)
struct ARegion * ui_searchbox_create_operator(struct bContext *C, struct ARegion *butregion, uiButSearch *search_but)
static void operator_search_update_fn(const bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items, const bool UNUSED(is_first))
void uiTemplateOperatorSearch(uiLayout *layout)
void UI_but_func_operator_search(uiBut *but)
static void operator_search_exec_fn(bContext *C, void *UNUSED(arg1), void *arg2)
#define G(x, y, z)
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
const char * translation_context
Definition: WM_types.h:891
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
int WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wmOperatorCallContext context, PointerRNA *properties, const wmEvent *event)
wmOperatorType * ot
Definition: wm_files.c:3479
char * WM_key_event_operator_string(const bContext *C, const char *opname, wmOperatorCallContext opcontext, IDProperty *properties, const bool is_strict, char *result, const int result_len)
Definition: wm_keymap.c:1636
void WM_operatortype_iter(GHashIterator *ghi)