Fix #32408: image editor does not show paint brush circle on loading a .blend
[blender.git] / source / blender / editors / util / ed_util.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/util/ed_util.c
28  *  \ingroup edutil
29  */
30
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_mesh_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_packedFile_types.h"
42
43 #include "BLI_blenlib.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48 #include "BKE_utildefines.h"
49 #include "BKE_packedFile.h"
50
51 #include "ED_armature.h"
52 #include "ED_image.h"
53 #include "ED_mesh.h"
54 #include "ED_object.h"
55 #include "ED_sculpt.h"
56 #include "ED_util.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60
61 #include "WM_types.h"
62 #include "WM_api.h"
63 #include "RNA_access.h"
64
65
66
67 /* ********* general editor util funcs, not BKE stuff please! ********* */
68
69 void ED_editors_init(bContext *C)
70 {
71         wmWindowManager *wm = CTX_wm_manager(C);
72         Main *bmain = CTX_data_main(C);
73         Scene *sce = CTX_data_scene(C);
74         Object *ob, *obact = (sce && sce->basact) ? sce->basact->object : NULL;
75         ID *data;
76
77         /* toggle on modes for objects that were saved with these enabled. for
78          * e.g. linked objects we have to ensure that they are actually the
79          * active object in this scene. */
80         for (ob = bmain->object.first; ob; ob = ob->id.next) {
81                 int mode = ob->mode;
82
83                 if (mode && (mode != OB_MODE_POSE)) {
84                         ob->mode = 0;
85                         data = ob->data;
86
87                         if (ob == obact && !ob->id.lib && !(data && data->lib))
88                                 ED_object_toggle_modes(C, mode);
89                 }
90         }
91
92         /* image editor paint mode */
93         ED_space_image_paint_update(wm, sce->toolsettings);
94 }
95
96 /* frees all editmode stuff */
97 void ED_editors_exit(bContext *C)
98 {
99         Main *bmain = CTX_data_main(C);
100         Scene *sce;
101
102         if (!bmain)
103                 return;
104         
105         /* frees all editmode undos */
106         undo_editmode_clear();
107         ED_undo_paint_free();
108         
109         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
110                 if (sce->obedit) {
111                         Object *ob = sce->obedit;
112                 
113                         if (ob) {
114                                 if (ob->type == OB_MESH) {
115                                         Mesh *me = ob->data;
116                                         if (me->edit_btmesh) {
117                                                 EDBM_mesh_free(me->edit_btmesh);
118                                                 MEM_freeN(me->edit_btmesh);
119                                                 me->edit_btmesh = NULL;
120                                         }
121                                 }
122                                 else if (ob->type == OB_ARMATURE) {
123                                         ED_armature_edit_free(ob);
124                                 }
125                         }
126                 }
127         }
128
129         /* global in meshtools... */
130         mesh_octree_table(NULL, NULL, NULL, 'e');
131         mesh_mirrtopo_table(NULL, 'e');
132 }
133
134
135 /* ***** XXX: functions are using old blender names, cleanup later ***** */
136
137
138 /* now only used in 2d spaces, like time, ipo, nla, sima... */
139 /* XXX shift/ctrl not configurable */
140 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
141 {
142         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
143         if (invert)
144                 ctrl = !ctrl;
145         
146         if (ctrl && shift) {
147                 if (fac3 != 0.0f) *val = fac3 * floorf(*val / fac3 + 0.5f);
148         }
149         else if (ctrl) {
150                 if (fac2 != 0.0f) *val = fac2 * floorf(*val / fac2 + 0.5f);
151         }
152         else {
153                 if (fac1 != 0.0f) *val = fac1 * floorf(*val / fac1 + 0.5f);
154         }
155 }
156
157 #if 0 /* UNUSED */
158 int GetButStringLength(const char *str) 
159 {
160         int rt;
161         
162         rt = UI_GetStringWidth(str);
163         
164         return rt + 15;
165 }
166 #endif
167
168 void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf)
169 {
170         PointerRNA props_ptr;
171         uiPopupMenu *pup;
172         uiLayout *layout;
173         char line[FILE_MAX + 100];
174         wmOperatorType *ot = WM_operatortype_find(opname, 1);
175
176         pup = uiPupMenuBegin(C, "Unpack file", ICON_NONE);
177         layout = uiPupMenuLayout(pup);
178
179         strcpy(line, "Remove Pack");
180         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
181         RNA_enum_set(&props_ptr, "method", PF_REMOVE);
182         RNA_string_set(&props_ptr, "id", id_name);
183
184         if (G.relbase_valid) {
185                 char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
186
187                 BLI_strncpy(local_name, abs_name, sizeof(local_name));
188                 BLI_splitdirstring(local_name, fi);
189                 BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
190                 if (strcmp(abs_name, local_name) != 0) {
191                         switch (checkPackedFile(local_name, pf)) {
192                                 case PF_NOFILE:
193                                         BLI_snprintf(line, sizeof(line), "Create %s", local_name);
194                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
195                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
196                                         RNA_string_set(&props_ptr, "id", id_name);
197
198                                         break;
199                                 case PF_EQUAL:
200                                         BLI_snprintf(line, sizeof(line), "Use %s (identical)", local_name);
201                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
202                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
203                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
204                                         RNA_string_set(&props_ptr, "id", id_name);
205
206                                         break;
207                                 case PF_DIFFERS:
208                                         BLI_snprintf(line, sizeof(line), "Use %s (differs)", local_name);
209                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
210                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
211                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
212                                         RNA_string_set(&props_ptr, "id", id_name);
213
214                                         BLI_snprintf(line, sizeof(line), "Overwrite %s", local_name);
215                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_LOCAL);
216                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
217                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
218                                         RNA_string_set(&props_ptr, "id", id_name);
219                                         break;
220                         }
221                 }
222         }
223
224         switch (checkPackedFile(abs_name, pf)) {
225                 case PF_NOFILE:
226                         BLI_snprintf(line, sizeof(line), "Create %s", abs_name);
227                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
228                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
229                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
230                         RNA_string_set(&props_ptr, "id", id_name);
231                         break;
232                 case PF_EQUAL:
233                         BLI_snprintf(line, sizeof(line), "Use %s (identical)", abs_name);
234                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
235                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
236                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
237                         RNA_string_set(&props_ptr, "id", id_name);
238                         break;
239                 case PF_DIFFERS:
240                         BLI_snprintf(line, sizeof(line), "Use %s (differs)", abs_name);
241                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
242                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
243                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
244                         RNA_string_set(&props_ptr, "id", id_name);
245
246                         BLI_snprintf(line, sizeof(line), "Overwrite %s", abs_name);
247                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
248                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
249                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
250                         RNA_string_set(&props_ptr, "id", id_name);
251                         break;
252         }
253
254         uiPupMenuEnd(C, pup);
255 }