Blender  V2.93
wm_gizmo_type.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 
21 #include <stdio.h>
22 
23 #include "BLI_ghash.h"
24 #include "BLI_listbase.h"
25 #include "BLI_utildefines.h"
26 
27 #include "BKE_context.h"
28 #include "BKE_main.h"
29 
30 #include "DNA_screen_types.h"
31 #include "DNA_space_types.h"
32 
33 #include "MEM_guardedalloc.h"
34 
35 #include "RNA_access.h"
36 #include "RNA_define.h"
37 
38 #include "WM_api.h"
39 #include "WM_types.h"
40 
41 #include "ED_screen.h"
42 
43 /* only for own init/exit calls (wm_gizmotype_init/wm_gizmotype_free) */
44 #include "wm.h"
45 
46 /* own includes */
47 #include "wm_gizmo_intern.h"
48 #include "wm_gizmo_wmapi.h"
49 
50 /* -------------------------------------------------------------------- */
57 
58 const wmGizmoType *WM_gizmotype_find(const char *idname, bool quiet)
59 {
60  if (idname[0]) {
61  wmGizmoType *gzt;
62 
64  if (gzt) {
65  return gzt;
66  }
67 
68  if (!quiet) {
69  printf("search for unknown gizmo '%s'\n", idname);
70  }
71  }
72  else {
73  if (!quiet) {
74  printf("search for empty gizmo\n");
75  }
76  }
77 
78  return NULL;
79 }
80 
81 /* caller must free */
83 {
85 }
86 
88 {
89  wmGizmoType *gzt = MEM_callocN(sizeof(wmGizmoType), "gizmotype");
91 #if 0
92  /* Set the default i18n context now, so that opfunc can redefine it if needed! */
95 #endif
96  return gzt;
97 }
99 {
100  BLI_assert(gzt->struct_size >= sizeof(wmGizmo));
101 
103 
104  BLI_ghash_insert(global_gizmotype_hash, (void *)gzt->idname, gzt);
105 }
106 
107 void WM_gizmotype_append(void (*gtfunc)(struct wmGizmoType *))
108 {
110  gtfunc(gzt);
112 }
113 
114 void WM_gizmotype_append_ptr(void (*gtfunc)(struct wmGizmoType *, void *), void *userdata)
115 {
117  gtfunc(mt, userdata);
119 }
120 
125 {
126  if (gzt->rna_ext.srna) { /* python gizmo, allocs own string */
127  MEM_freeN((void *)gzt->idname);
128  }
129 
131  MEM_freeN(gzt);
132 }
133 
137 static void gizmotype_unlink(bContext *C, Main *bmain, wmGizmoType *gzt)
138 {
139  /* Free instances. */
140  for (bScreen *screen = bmain->screens.first; screen; screen = screen->id.next) {
141  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
142  LISTBASE_FOREACH (SpaceLink *, sl, &area->spacedata) {
143  ListBase *lb = (sl == area->spacedata.first) ? &area->regionbase : &sl->regionbase;
144  LISTBASE_FOREACH (ARegion *, region, lb) {
145  wmGizmoMap *gzmap = region->gizmo_map;
146  if (gzmap) {
147  wmGizmoGroup *gzgroup;
148  for (gzgroup = gzmap->groups.first; gzgroup; gzgroup = gzgroup->next) {
149  for (wmGizmo *gz = gzgroup->gizmos.first, *gz_next; gz; gz = gz_next) {
150  gz_next = gz->next;
151  BLI_assert(gzgroup->parent_gzmap == gzmap);
152  if (gz->type == gzt) {
153  WM_gizmo_unlink(&gzgroup->gizmos, gzgroup->parent_gzmap, gz, C);
155  }
156  }
157  }
158  }
159  }
160  }
161  }
162  }
163 }
164 
166 {
167  BLI_assert(gzt == WM_gizmotype_find(gzt->idname, false));
168 
170 
171  gizmotype_unlink(C, bmain, gzt);
172 }
173 
174 bool WM_gizmotype_remove(bContext *C, Main *bmain, const char *idname)
175 {
177 
178  if (gzt == NULL) {
179  return false;
180  }
181 
182  WM_gizmotype_remove_ptr(C, bmain, gzt);
183 
184  return true;
185 }
186 
188 {
190 }
191 
193 {
196 }
197 
198 /* called on initialize WM_init() */
200 {
201  /* reserve size is set based on blender default setup */
202  global_gizmotype_hash = BLI_ghash_str_new_ex("wm_gizmotype_init gh", 128);
203 }
204 
#define BLI_assert(a)
Definition: BLI_assert.h:58
void(* GHashValFreeFP)(void *val)
Definition: BLI_ghash.h:50
GHash * BLI_ghash_str_new_ex(const char *info, const 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:900
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh)
Definition: BLI_ghash.c:1065
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
void ED_region_tag_redraw_editor_overlays(struct ARegion *region)
Definition: area.c:706
Read Guarded memory(de)allocation.
StructRNA RNA_GizmoProperties
#define C
Definition: RandGen.cpp:39
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
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:1224
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
Definition: rna_define.c:919
void RNA_def_struct_translation_context(StructRNA *srna, const char *context)
Definition: rna_define.c:1272
BlenderRNA BLENDER_RNA
StructRNA * srna
Definition: RNA_types.h:681
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase screens
Definition: BKE_main.h:161
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:802
const char * translation_context
Definition: WM_types.h:724
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_gizmo_unlink(ListBase *gizmolist, wmGizmoMap *gzmap, wmGizmo *gz, bContext *C)
Definition: wm_gizmo.c:194
static wmGizmoType * wm_gizmotype_append__begin(void)
Definition: wm_gizmo_type.c:87
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:56
static void wm_gizmotype_append__end(wmGizmoType *gzt)
Definition: wm_gizmo_type.c:98
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:58
void WM_gizmotype_append(void(*gtfunc)(struct wmGizmoType *))
void WM_gizmotype_iter(GHashIterator *ghi)
Definition: wm_gizmo_type.c:82
void WM_gizmotype_free_ptr(wmGizmoType *gzt)