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