synched with trunk at revision 30243
[blender-staging.git] / source / blender / editors / util / ed_util.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_editVert.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43
44 #include "ED_armature.h"
45 #include "ED_mesh.h"
46 #include "ED_object.h"
47 #include "ED_sculpt.h"
48 #include "ED_util.h"
49
50 #include "UI_interface.h"
51
52 /* ********* general editor util funcs, not BKE stuff please! ********* */
53
54 void ED_editors_init(bContext *C)
55 {
56         Main *bmain= CTX_data_main(C);
57         Scene *sce= CTX_data_scene(C);
58         Object *ob, *obact= (sce && sce->basact)? sce->basact->object: NULL;
59         ID *data;
60
61         /* toggle on modes for objects that were saved with these enabled. for
62            e.g. linked objects we have to ensure that they are actually the
63            active object in this scene. */
64         for(ob=bmain->object.first; ob; ob=ob->id.next) {
65                 int mode= ob->mode;
66
67                 if(mode && (mode != OB_MODE_POSE)) {
68                         ob->mode= 0;
69                         data= ob->data;
70
71                         if(ob == obact && !ob->id.lib && !(data && data->lib))
72                                 ED_object_toggle_modes(C, mode);
73                 }
74         }
75 }
76
77 /* frees all editmode stuff */
78 void ED_editors_exit(bContext *C)
79 {
80         Scene *sce;
81         
82         /* frees all editmode undos */
83         undo_editmode_clear();
84         ED_undo_paint_free();
85         
86         for(sce=G.main->scene.first; sce; sce= sce->id.next) {
87                 if(sce->obedit) {
88                         Object *ob= sce->obedit;
89                 
90                         /* global in meshtools... */
91                         mesh_octree_table(NULL, NULL, NULL, 'e');
92                         mesh_mirrtopo_table(NULL, 'e');
93                         
94                         if(ob) {
95                                 if(ob->type==OB_MESH) {
96                                         Mesh *me= ob->data;
97                                         if(me->edit_mesh) {
98                                                 free_editMesh(me->edit_mesh);
99                                                 MEM_freeN(me->edit_mesh);
100                                                 me->edit_mesh= NULL;
101                                         }
102                                 }
103                                 else if(ob->type==OB_ARMATURE) {
104                                         ED_armature_edit_free(ob);
105                                 }
106                                 else if(ob->type==OB_FONT) {
107                                         //                      free_editText();
108                                 }
109                                 //              else if(ob->type==OB_MBALL) 
110                                 //                      BLI_freelistN(&editelems);
111                                 //      free_editLatt();
112                                 //      free_posebuf();         // XXX this is still a global...
113                         }
114                 }
115                 else if(sce->basact && sce->basact->object) {
116                         Object *ob= sce->basact->object;
117                         
118                         /* if weight-painting is on, free mesh octree data */
119                         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
120                                 mesh_octree_table(NULL, NULL, NULL, 'e');
121                                 mesh_mirrtopo_table(NULL, 'e');
122                         }
123                 }
124         }
125         
126 }
127
128
129 /* ***** XXX: functions are using old blender names, cleanup later ***** */
130
131
132 /* now only used in 2d spaces, like time, ipo, nla, sima... */
133 /* XXX shift/ctrl not configurable */
134 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
135 {
136         /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
137         if(invert)
138                 ctrl= !ctrl;
139         
140         if(ctrl && shift) {
141                 if(fac3!= 0.0) *val= fac3*floor(*val/fac3 +.5);
142         }
143         else if(ctrl) {
144                 if(fac2!= 0.0) *val= fac2*floor(*val/fac2 +.5);
145         }
146         else {
147                 if(fac1!= 0.0) *val= fac1*floor(*val/fac1 +.5);
148         }
149 }
150
151
152 int GetButStringLength(char *str) 
153 {
154         int rt;
155         
156         rt= UI_GetStringWidth(str);
157         
158         return rt + 15;
159 }
160