Blender V5.0
wm_menu_type.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdio>
12
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_utildefines.h"
18#include "BLI_vector_set.hh"
19
20#include "BKE_context.hh"
21#include "BKE_screen.hh"
22#include "BKE_workspace.hh"
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
28
29static auto &get_menu_type_map()
30{
31 struct IDNameGetter {
32 StringRef operator()(const MenuType *value) const
33 {
34 return StringRef(value->idname);
35 }
36 };
38 return map;
39}
40
41MenuType *WM_menutype_find(const StringRef idname, bool quiet)
42{
43 if (!idname.is_empty()) {
44 if (MenuType *const *mt = get_menu_type_map().lookup_key_ptr_as(idname)) {
45 return *mt;
46 }
47 }
48
49 if (!quiet) {
50 printf("search for unknown menutype %s\n", std::string(idname).c_str());
51 }
52
53 return nullptr;
54}
55
60
62{
63 BLI_assert((mt->description == nullptr) || (mt->description[0]));
64 get_menu_type_map().add(mt);
65 return true;
66}
67
69{
70 bool ok = get_menu_type_map().remove(mt);
71 MEM_freeN(mt);
72
73 BLI_assert(ok);
75}
76
78{
79 /* Reserve size is set based on blender default setup. */
80 get_menu_type_map().reserve(512);
81}
82
84{
85 for (MenuType *mt : get_menu_type_map()) {
86 if (mt->rna_ext.free) {
87 mt->rna_ext.free(mt->rna_ext.data);
88 }
89 MEM_freeN(mt);
90 }
91 get_menu_type_map().clear();
92}
93
95{
96 /* If we're tagged, only use compatible. */
97 if (mt->owner_id[0] != '\0') {
98 const WorkSpace *workspace = CTX_wm_workspace(C);
99 if (BKE_workspace_owner_id_check(workspace, mt->owner_id) == false) {
100 return false;
101 }
102 }
103
104 if (mt->poll != nullptr) {
105 return mt->poll(C, mt);
106 }
107 return true;
108}
109
111 const bContext * /*C*/,
112 PointerRNA * /*ptr*/,
113 PropertyRNA * /*prop*/,
114 const char * /*edit_text*/,
116{
117 for (MenuType *mt : get_menu_type_map()) {
118 StringPropertySearchVisitParams visit_params{};
119 visit_params.text = mt->idname;
120 visit_params.info = mt->label;
121 visit_fn(visit_params);
122 }
123}
WorkSpace * CTX_wm_workspace(const bContext *C)
bool BKE_workspace_owner_id_check(const WorkSpace *workspace, const char *owner_id) ATTR_NONNULL()
Definition workspace.cc:537
#define BLI_assert(a)
Definition BLI_assert.h:46
#define UNUSED_VARS_NDEBUG(...)
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
constexpr bool is_empty() const
#define printf(...)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
const char * description
bool(* poll)(const bContext *C, MenuType *mt)
char idname[BKE_ST_MAXNAME]
char owner_id[128]
std::optional< std::string > info
Definition RNA_types.hh:773
void WM_menutype_free()
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
static auto & get_menu_type_map()
void WM_menutype_idname_visit_for_search(const bContext *, PointerRNA *, PropertyRNA *, const char *, blender::FunctionRef< void(StringPropertySearchVisitParams)> visit_fn)
bool WM_menutype_add(MenuType *mt)
bool WM_menutype_poll(bContext *C, MenuType *mt)
blender::Span< MenuType * > WM_menutypes_registered_get()
void WM_menutype_init()
void WM_menutype_freelink(MenuType *mt)