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