Blender  V2.93
interface_template_search_operator.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
24 #include <string.h>
25 
26 #include "DNA_object_types.h"
27 #include "DNA_scene_types.h"
28 #include "DNA_texture_types.h"
29 
30 #include "BLI_alloca.h"
31 #include "BLI_ghash.h"
32 #include "BLI_string.h"
33 #include "BLI_utildefines.h"
34 
35 #include "BLT_translation.h"
36 
37 #include "BKE_context.h"
38 #include "BKE_global.h"
39 
40 #include "WM_api.h"
41 #include "WM_types.h"
42 
43 #include "UI_interface.h"
44 #include "interface_intern.h"
45 
46 /* -------------------------------------------------------------------- */
50 static void operator_search_exec_fn(bContext *C, void *UNUSED(arg1), void *arg2)
51 {
52  wmOperatorType *ot = arg2;
53 
54  if (ot) {
56  }
57 }
58 
60  void *UNUSED(arg),
61  const char *str,
62  uiSearchItems *items,
63  const bool UNUSED(is_first))
64 {
65  GHashIterator iter;
66 
67  /* Prepare BLI_string_all_words_matched. */
68  const size_t str_len = strlen(str);
69  const int words_max = BLI_string_max_possible_word_count(str_len);
70  int(*words)[2] = BLI_array_alloca(words, words_max);
71  const int words_len = BLI_string_find_split_words(str, str_len, ' ', words, words_max);
72 
73  for (WM_operatortype_iter(&iter); !BLI_ghashIterator_done(&iter);
74  BLI_ghashIterator_step(&iter)) {
76  const char *ot_ui_name = CTX_IFACE_(ot->translation_context, ot->name);
77 
78  if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0) {
79  continue;
80  }
81 
82  if (BLI_string_all_words_matched(ot_ui_name, str, words, words_len)) {
83  if (WM_operator_poll((bContext *)C, ot)) {
84  char name[256];
85  const int len = strlen(ot_ui_name);
86 
87  /* display name for menu, can hold hotkey */
88  BLI_strncpy(name, ot_ui_name, sizeof(name));
89 
90  /* check for hotkey */
91  if (len < sizeof(name) - 6) {
93  ot->idname,
95  NULL,
96  true,
97  &name[len + 1],
98  sizeof(name) - len - 1)) {
99  name[len] = UI_SEP_CHAR;
100  }
101  }
102 
103  if (!UI_search_item_add(items, name, ot, ICON_NONE, 0, 0)) {
104  break;
105  }
106  }
107  }
108  }
109 }
110 
113 /* -------------------------------------------------------------------- */
118 {
122  NULL,
123  false,
124  NULL,
126  NULL);
127 }
128 
130 {
131  uiBlock *block;
132  uiBut *but;
133  static char search[256] = "";
134 
135  block = uiLayoutGetBlock(layout);
136  UI_block_layout_set_current(block, layout);
137 
138  but = uiDefSearchBut(
139  block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
141 }
142 
@ G_DEBUG_WM
Definition: BKE_global.h:138
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:36
void BLI_ghashIterator_step(GHashIterator *ghi)
Definition: BLI_ghash.c:1086
BLI_INLINE bool BLI_ghashIterator_done(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:158
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:150
bool BLI_string_all_words_matched(const char *name, const char *str, int(*words)[2], const int words_len)
Definition: string.c:616
size_t int BLI_string_max_possible_word_count(const int str_len)
Definition: string.c:599
int BLI_string_find_split_words(const char *str, const size_t len, const char delim, int r_words[][2], int words_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:1242
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define UNUSED(x)
#define CTX_IFACE_(context, msgid)
Object is a sort of wrapper for general info.
#define C
Definition: RandGen.cpp:39
#define UI_UNIT_Y
uiBlock * uiLayoutGetBlock(uiLayout *layout)
#define UI_SEP_CHAR
Definition: UI_interface.h:86
bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid, int state, const uint8_t name_prefix_offset)
void UI_but_func_search_set(uiBut *but, uiButSearchCreateFn search_create_fn, uiButSearchUpdateFn search_update_fn, void *arg, const bool free_arg, uiButSearchArgFreeFn search_arg_free_fn, uiButHandleFunc search_exec_fn, void *active)
Definition: interface.c:6613
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.c:6573
void UI_block_layout_set_current(uiBlock *block, uiLayout *layout)
#define UI_UNIT_X
@ OPTYPE_INTERNAL
Definition: WM_types.h:175
@ WM_OP_INVOKE_DEFAULT
Definition: WM_types.h:197
@ WM_OP_EXEC_DEFAULT
Definition: WM_types.h:204
#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)
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
const char * translation_context
Definition: WM_types.h:724
#define G(x, y, z)
uint len
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
int WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, short context, PointerRNA *properties)
wmOperatorType * ot
Definition: wm_files.c:3156
char * WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, const bool is_strict, char *result, const int result_len)
Definition: wm_keymap.c:1587
void WM_operatortype_iter(GHashIterator *ghi)