Blender V4.3
wm_gizmo_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
9#include <cstdio>
10
11#include "BLI_ghash.h"
12#include "BLI_listbase.h"
13#include "BLI_utildefines.h"
14
15#include "BKE_main.hh"
16
17#include "DNA_screen_types.h"
18#include "DNA_space_types.h"
19
20#include "MEM_guardedalloc.h"
21
22#include "RNA_access.hh"
23#include "RNA_define.hh"
24#include "RNA_prototypes.hh"
25
26#include "WM_types.hh"
27
28#include "ED_screen.hh"
29
30/* Own includes. */
31#include "wm_gizmo_intern.hh"
32#include "wm_gizmo_wmapi.hh"
33
34/* -------------------------------------------------------------------- */
40static GHash *global_gizmotype_hash = nullptr;
41
42const wmGizmoType *WM_gizmotype_find(const char *idname, bool quiet)
43{
44 if (idname[0]) {
45 wmGizmoType *gzt;
46
47 gzt = static_cast<wmGizmoType *>(BLI_ghash_lookup(global_gizmotype_hash, idname));
48 if (gzt) {
49 return gzt;
50 }
51
52 if (!quiet) {
53 printf("search for unknown gizmo '%s'\n", idname);
54 }
55 }
56 else {
57 if (!quiet) {
58 printf("search for empty gizmo\n");
59 }
60 }
61
62 return nullptr;
63}
64
69
71{
72 wmGizmoType *gzt = static_cast<wmGizmoType *>(MEM_callocN(sizeof(wmGizmoType), "gizmotype"));
73 gzt->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_GizmoProperties);
74#if 0
75 /* Set the default i18n context now, so that opfunc can redefine it if needed! */
78#endif
79 return gzt;
80}
82{
83 BLI_assert(gzt->struct_size >= sizeof(wmGizmo));
84
86
88}
89
90void WM_gizmotype_append(void (*gtfunc)(wmGizmoType *))
91{
93 gtfunc(gzt);
95}
96
97void WM_gizmotype_append_ptr(void (*gtfunc)(wmGizmoType *, void *), void *userdata)
98{
100 gtfunc(mt, userdata);
102}
103
105{
106 /* Python gizmo, allocates its own string. */
107 if (gzt->rna_ext.srna) {
108 MEM_freeN((void *)gzt->idname);
109 }
110
112 MEM_freeN(gzt);
113}
114
118static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
119{
120 /* Free instances. */
121 for (bScreen *screen = static_cast<bScreen *>(bmain->screens.first); screen;
122 screen = static_cast<bScreen *>(screen->id.next))
123 {
124 LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
125 LISTBASE_FOREACH (SpaceLink *, sl, &area->spacedata) {
126 ListBase *lb = (sl == area->spacedata.first) ? &area->regionbase : &sl->regionbase;
127 LISTBASE_FOREACH (ARegion *, region, lb) {
128 wmGizmoMap *gzmap = region->gizmo_map;
129 if (gzmap) {
130 LISTBASE_FOREACH (wmGizmoGroup *, gzgroup, &gzmap->groups) {
131 for (wmGizmo *gz = static_cast<wmGizmo *>(gzgroup->gizmos.first), *gz_next; gz;
132 gz = gz_next)
133 {
134 gz_next = gz->next;
135 BLI_assert(gzgroup->parent_gzmap == gzmap);
136 if (gz->type == gzt) {
137 WM_gizmo_unlink(&gzgroup->gizmos, gzgroup->parent_gzmap, gz, C);
139 }
140 }
141 }
142 }
143 }
144 }
145 }
146 }
147}
148
150{
151 BLI_assert(gzt == WM_gizmotype_find(gzt->idname, false));
152
153 BLI_ghash_remove(global_gizmotype_hash, gzt->idname, nullptr, nullptr);
154
155 gizmotype_unlink(C, bmain, gzt);
156}
157
158bool WM_gizmotype_remove(bContext *C, Main *bmain, const char *idname)
159{
160 wmGizmoType *gzt = static_cast<wmGizmoType *>(BLI_ghash_lookup(global_gizmotype_hash, idname));
161
162 if (gzt == nullptr) {
163 return false;
164 }
165
166 WM_gizmotype_remove_ptr(C, bmain, gzt);
167
168 return true;
169}
170
175
181
183{
184 /* Reserve size is set based on blender default setup. */
185 global_gizmotype_hash = BLI_ghash_str_new_ex("wm_gizmotype_init gh", 128);
186}
187
#define BLI_assert(a)
Definition BLI_assert.h:50
void(* GHashValFreeFP)(void *val)
Definition BLI_ghash.h:39
GHash * BLI_ghash_str_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.c:787
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.c:731
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.c:707
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.c:860
void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh)
Definition BLI_ghash.c:895
#define LISTBASE_FOREACH(type, var, list)
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:496
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
void ED_region_tag_redraw_editor_overlays(ARegion *region)
Definition area.cc:669
Read Guarded memory(de)allocation.
#define printf
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void RNA_def_struct_identifier(BlenderRNA *brna, StructRNA *srna, const char *identifier)
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
BlenderRNA BLENDER_RNA
StructRNA * srna
Definition RNA_types.hh:780
void * first
ListBase screens
Definition BKE_main.hh:225
ListBase groups
StructRNA * srna
ExtensionRNA rna_ext
ListBase target_property_defs
const char * idname
const char * translation_context
Definition WM_types.hh:994
StructRNA * srna
Definition WM_types.hh:1080
wmOperatorType * ot
Definition wm_files.cc:4125
void WM_gizmo_unlink(ListBase *gizmolist, wmGizmoMap *gzmap, wmGizmo *gz, bContext *C)
Definition wm_gizmo.cc:165
void WM_gizmotype_append_ptr(void(*gtfunc)(wmGizmoType *, void *), void *userdata)
static void wm_gizmotype_ghash_free_cb(wmGizmoType *gzt)
static GHash * global_gizmotype_hash
static void wm_gizmotype_append__end(wmGizmoType *gzt)
void WM_gizmotype_append(void(*gtfunc)(wmGizmoType *))
const wmGizmoType * WM_gizmotype_find(const char *idname, bool quiet)
bool WM_gizmotype_remove(bContext *C, Main *bmain, const char *idname)
void WM_gizmotype_remove_ptr(bContext *C, Main *bmain, wmGizmoType *gzt)
static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
void wm_gizmotype_init()
void wm_gizmotype_free()
void WM_gizmotype_iter(GHashIterator *ghi)
static wmGizmoType * wm_gizmotype_append__begin()
void WM_gizmotype_free_ptr(wmGizmoType *gzt)