Blender  V3.3
wm_gizmo_type.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <stdio.h>
8 
9 #include "BLI_ghash.h"
10 #include "BLI_listbase.h"
11 #include "BLI_utildefines.h"
12 
13 #include "BKE_context.h"
14 #include "BKE_main.h"
15 
16 #include "DNA_screen_types.h"
17 #include "DNA_space_types.h"
18 
19 #include "MEM_guardedalloc.h"
20 
21 #include "RNA_access.h"
22 #include "RNA_define.h"
23 #include "RNA_prototypes.h"
24 
25 #include "WM_api.h"
26 #include "WM_types.h"
27 
28 #include "ED_screen.h"
29 
30 /* only for own init/exit calls (wm_gizmotype_init/wm_gizmotype_free) */
31 #include "wm.h"
32 
33 /* own includes */
34 #include "wm_gizmo_intern.h"
35 #include "wm_gizmo_wmapi.h"
36 
37 /* -------------------------------------------------------------------- */
44 
45 const wmGizmoType *WM_gizmotype_find(const char *idname, bool quiet)
46 {
47  if (idname[0]) {
48  wmGizmoType *gzt;
49 
51  if (gzt) {
52  return gzt;
53  }
54 
55  if (!quiet) {
56  printf("search for unknown gizmo '%s'\n", idname);
57  }
58  }
59  else {
60  if (!quiet) {
61  printf("search for empty gizmo\n");
62  }
63  }
64 
65  return NULL;
66 }
67 
69 {
71 }
72 
74 {
75  wmGizmoType *gzt = MEM_callocN(sizeof(wmGizmoType), "gizmotype");
76  gzt->srna = RNA_def_struct_ptr(&BLENDER_RNA, "", &RNA_GizmoProperties);
77 #if 0
78  /* Set the default i18n context now, so that opfunc can redefine it if needed! */
81 #endif
82  return gzt;
83 }
85 {
86  BLI_assert(gzt->struct_size >= sizeof(wmGizmo));
87 
89 
90  BLI_ghash_insert(global_gizmotype_hash, (void *)gzt->idname, gzt);
91 }
92 
93 void WM_gizmotype_append(void (*gtfunc)(struct wmGizmoType *))
94 {
96  gtfunc(gzt);
98 }
99 
100 void WM_gizmotype_append_ptr(void (*gtfunc)(struct wmGizmoType *, void *), void *userdata)
101 {
103  gtfunc(mt, userdata);
105 }
106 
108 {
109  if (gzt->rna_ext.srna) { /* python gizmo, allocs own string */
110  MEM_freeN((void *)gzt->idname);
111  }
112 
114  MEM_freeN(gzt);
115 }
116 
120 static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
121 {
122  /* Free instances. */
123  for (bScreen *screen = bmain->screens.first; screen; screen = screen->id.next) {
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  wmGizmoGroup *gzgroup;
131  for (gzgroup = gzmap->groups.first; gzgroup; gzgroup = gzgroup->next) {
132  for (wmGizmo *gz = gzgroup->gizmos.first, *gz_next; gz; gz = gz_next) {
133  gz_next = gz->next;
134  BLI_assert(gzgroup->parent_gzmap == gzmap);
135  if (gz->type == gzt) {
136  WM_gizmo_unlink(&gzgroup->gizmos, gzgroup->parent_gzmap, gz, C);
138  }
139  }
140  }
141  }
142  }
143  }
144  }
145  }
146 }
147 
149 {
150  BLI_assert(gzt == WM_gizmotype_find(gzt->idname, false));
151 
153 
154  gizmotype_unlink(C, bmain, gzt);
155 }
156 
157 bool WM_gizmotype_remove(bContext *C, Main *bmain, const char *idname)
158 {
160 
161  if (gzt == NULL) {
162  return false;
163  }
164 
165  WM_gizmotype_remove_ptr(C, bmain, gzt);
166 
167  return true;
168 }
169 
171 {
173 }
174 
176 {
179 }
180 
182 {
183  /* reserve size is set based on blender default setup */
184  global_gizmotype_hash = BLI_ghash_str_new_ex("wm_gizmotype_init gh", 128);
185 }
186 
#define BLI_assert(a)
Definition: BLI_assert.h:46
GHash * BLI_ghash_str_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void(* GHashValFreeFP)(void *val)
Definition: BLI_ghash.h:38
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:734
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:790
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:710
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:863
void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh)
Definition: BLI_ghash.c:898
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
void ED_region_tag_redraw_editor_overlays(struct ARegion *region)
Definition: area.c:690
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static void area(int d1, int d2, int e1, int e2, float weights[2])
void RNA_def_struct_identifier(BlenderRNA *brna, StructRNA *srna, const char *identifier)
Definition: rna_define.c:1205
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
Definition: rna_define.c:900
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
Definition: rna_define.c:1250
BlenderRNA BLENDER_RNA
StructRNA * srna
Definition: RNA_types.h:766
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
ListBase screens
Definition: BKE_main.h:183
struct wmGizmoGroup * next
ListBase gizmos
struct wmGizmoMap * parent_gzmap
ListBase groups
ExtensionRNA rna_ext
ListBase target_property_defs
const char * idname
struct StructRNA * srna
struct StructRNA * srna
Definition: WM_types.h:969
const char * translation_context
Definition: WM_types.h:891
wmOperatorType * ot
Definition: wm_files.c:3479
void WM_gizmo_unlink(ListBase *gizmolist, wmGizmoMap *gzmap, wmGizmo *gz, bContext *C)
Definition: wm_gizmo.c:165
static wmGizmoType * wm_gizmotype_append__begin(void)
Definition: wm_gizmo_type.c:73
void WM_gizmotype_append_ptr(void(*gtfunc)(struct wmGizmoType *, void *), void *userdata)
static void wm_gizmotype_ghash_free_cb(wmGizmoType *gzt)
static GHash * global_gizmotype_hash
Definition: wm_gizmo_type.c:43
static void wm_gizmotype_append__end(wmGizmoType *gzt)
Definition: wm_gizmo_type.c:84
void wm_gizmotype_init(void)
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_free(void)
const wmGizmoType * WM_gizmotype_find(const char *idname, bool quiet)
Definition: wm_gizmo_type.c:45
void WM_gizmotype_append(void(*gtfunc)(struct wmGizmoType *))
Definition: wm_gizmo_type.c:93
void WM_gizmotype_iter(GHashIterator *ghi)
Definition: wm_gizmo_type.c:68
void WM_gizmotype_free_ptr(wmGizmoType *gzt)