patch [#34103] path_util_split_dirstring.patch, path_util_split_dirstring_2.patch...
[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 "BLF_translation.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_packedFile.h"
51
52 #include "ED_armature.h"
53 #include "ED_image.h"
54 #include "ED_mesh.h"
55 #include "ED_object.h"
56 #include "ED_sculpt.h"
57 #include "ED_util.h"
58
59 #include "UI_interface.h"
60 #include "UI_resources.h"
61
62 #include "WM_types.h"
63 #include "WM_api.h"
64 #include "RNA_access.h"
65
66
67
68 /* ********* general editor util funcs, not BKE stuff please! ********* */
69
70 void ED_editors_init(bContext *C)
71 {
72         wmWindowManager *wm = CTX_wm_manager(C);
73         Main *bmain = CTX_data_main(C);
74         Scene *sce = CTX_data_scene(C);
75         Object *ob, *obact = (sce && sce->basact) ? sce->basact->object : NULL;
76         ID *data;
77
78         /* toggle on modes for objects that were saved with these enabled. for
79          * e.g. linked objects we have to ensure that they are actually the
80          * active object in this scene. */
81         for (ob = bmain->object.first; ob; ob = ob->id.next) {
82                 int mode = ob->mode;
83
84                 if (mode && (mode != OB_MODE_POSE)) {
85                         ob->mode = 0;
86                         data = ob->data;
87
88                         if (ob == obact && !ob->id.lib && !(data && data->lib))
89                                 ED_object_toggle_modes(C, mode);
90                 }
91         }
92
93         /* image editor paint mode */
94         if (sce) {
95                 ED_space_image_paint_update(wm, sce->toolsettings);
96         }
97 }
98
99 /* frees all editmode stuff */
100 void ED_editors_exit(bContext *C)
101 {
102         Main *bmain = CTX_data_main(C);
103         Scene *sce;
104
105         if (!bmain)
106                 return;
107         
108         /* frees all editmode undos */
109         undo_editmode_clear();
110         ED_undo_paint_free();
111         
112         for (sce = bmain->scene.first; sce; sce = sce->id.next) {
113                 if (sce->obedit) {
114                         Object *ob = sce->obedit;
115                 
116                         if (ob) {
117                                 if (ob->type == OB_MESH) {
118                                         Mesh *me = ob->data;
119                                         if (me->edit_btmesh) {
120                                                 EDBM_mesh_free(me->edit_btmesh);
121                                                 MEM_freeN(me->edit_btmesh);
122                                                 me->edit_btmesh = NULL;
123                                         }
124                                 }
125                                 else if (ob->type == OB_ARMATURE) {
126                                         ED_armature_edit_free(ob);
127                                 }
128                         }
129                 }
130         }
131
132         /* global in meshtools... */
133         mesh_octree_table(NULL, NULL, NULL, 'e');
134         mesh_mirrtopo_table(NULL, 'e');
135 }
136
137
138 /* ***** XXX: functions are using old blender names, cleanup later ***** */
139
140
141 /* now only used in 2d spaces, like time, ipo, nla, sima... */
142 /* XXX shift/ctrl not configurable */
143 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
144 {
145         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
146         if (invert)
147                 ctrl = !ctrl;
148         
149         if (ctrl && shift) {
150                 if (fac3 != 0.0f) *val = fac3 * floorf(*val / fac3 + 0.5f);
151         }
152         else if (ctrl) {
153                 if (fac2 != 0.0f) *val = fac2 * floorf(*val / fac2 + 0.5f);
154         }
155         else {
156                 if (fac1 != 0.0f) *val = fac1 * floorf(*val / fac1 + 0.5f);
157         }
158 }
159
160 #if 0 /* UNUSED */
161 int GetButStringLength(const char *str) 
162 {
163         int rt;
164         
165         rt = UI_GetStringWidth(str);
166         
167         return rt + 15;
168 }
169 #endif
170
171 void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf)
172 {
173         PointerRNA props_ptr;
174         uiPopupMenu *pup;
175         uiLayout *layout;
176         char line[FILE_MAX + 100];
177         wmOperatorType *ot = WM_operatortype_find(opname, 1);
178
179         pup = uiPupMenuBegin(C, IFACE_("Unpack File"), ICON_NONE);
180         layout = uiPupMenuLayout(pup);
181
182         strcpy(line, IFACE_("Remove Pack"));
183         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
184         RNA_enum_set(&props_ptr, "method", PF_REMOVE);
185         RNA_string_set(&props_ptr, "id", id_name);
186
187         if (G.relbase_valid) {
188                 char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
189
190                 BLI_split_file_part(abs_name, fi, sizeof(fi));
191                 BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
192                 if (strcmp(abs_name, local_name) != 0) {
193                         switch (checkPackedFile(local_name, pf)) {
194                                 case PF_NOFILE:
195                                         BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), local_name);
196                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
197                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
198                                         RNA_string_set(&props_ptr, "id", id_name);
199
200                                         break;
201                                 case PF_EQUAL:
202                                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (identical)"), local_name);
203                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
204                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
205                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
206                                         RNA_string_set(&props_ptr, "id", id_name);
207
208                                         break;
209                                 case PF_DIFFERS:
210                                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (differs)"), local_name);
211                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_LOCAL);
212                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
213                                         RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
214                                         RNA_string_set(&props_ptr, "id", id_name);
215
216                                         BLI_snprintf(line, sizeof(line), IFACE_("Overwrite %s"), local_name);
217                                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_LOCAL);
218                                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
219                                         RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
220                                         RNA_string_set(&props_ptr, "id", id_name);
221                                         break;
222                         }
223                 }
224         }
225
226         switch (checkPackedFile(abs_name, pf)) {
227                 case PF_NOFILE:
228                         BLI_snprintf(line, sizeof(line), IFACE_("Create %s"), abs_name);
229                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
230                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
231                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
232                         RNA_string_set(&props_ptr, "id", id_name);
233                         break;
234                 case PF_EQUAL:
235                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (identical)"), abs_name);
236                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
237                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
238                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
239                         RNA_string_set(&props_ptr, "id", id_name);
240                         break;
241                 case PF_DIFFERS:
242                         BLI_snprintf(line, sizeof(line), IFACE_("Use %s (differs)"), abs_name);
243                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_USE_ORIGINAL);
244                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
245                         RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
246                         RNA_string_set(&props_ptr, "id", id_name);
247
248                         BLI_snprintf(line, sizeof(line), IFACE_("Overwrite %s"), abs_name);
249                         //uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);
250                         props_ptr = uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
251                         RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
252                         RNA_string_set(&props_ptr, "id", id_name);
253                         break;
254         }
255
256         uiPupMenuEnd(C, pup);
257 }