2.5
[blender.git] / source / blender / editors / space_view3d / view3d_toolbar.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_ID.h"
35 #include "DNA_action_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_curve_types.h"
38 #include "DNA_camera_types.h"
39 #include "DNA_lamp_types.h"
40 #include "DNA_lattice_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_userdef_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_world_types.h"
51
52 #include "MEM_guardedalloc.h"
53
54 #include "BLI_arithb.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_editVert.h"
57 #include "BLI_rand.h"
58
59 #include "BKE_action.h"
60 #include "BKE_brush.h"
61 #include "BKE_context.h"
62 #include "BKE_curve.h"
63 #include "BKE_customdata.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_idprop.h"
66 #include "BKE_mesh.h"
67 #include "BKE_object.h"
68 #include "BKE_global.h"
69 #include "BKE_scene.h"
70 #include "BKE_screen.h"
71 #include "BKE_utildefines.h"
72
73 #include "BIF_gl.h"
74 #include "BIF_transform.h"
75
76 #include "WM_api.h"
77 #include "WM_types.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81
82 #include "ED_armature.h"
83 #include "ED_curve.h"
84 #include "ED_image.h"
85 #include "ED_keyframing.h"
86 #include "ED_mesh.h"
87 #include "ED_object.h"
88 #include "ED_particle.h"
89 #include "ED_screen.h"
90 #include "ED_types.h"
91 #include "ED_util.h"
92
93 #include "UI_interface.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 #include "view3d_intern.h"      // own include
98
99
100 /* ******************* view3d space & buttons ************** */
101
102
103 /* op->invoke */
104 static void redo_cb(bContext *C, void *arg_op, void *arg2)
105 {
106         wmOperator *lastop= arg_op;
107         
108         if(lastop) {
109                 int retval;
110                 
111                 printf("operator redo %s\n", lastop->type->name);
112                 ED_undo_pop(C);
113                 retval= WM_operator_repeat(C, lastop);
114                 if((retval & OPERATOR_FINISHED)==0) {
115                         printf("operator redo failed %s\n", lastop->type->name);
116                         ED_undo_redo(C);
117                 }
118         }
119 }
120
121 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
122 {
123         wmWindowManager *wm= CTX_wm_manager(C);
124         wmOperator *op;
125         PointerRNA ptr;
126         uiBlock *block;
127         
128         block= uiLayoutGetBlock(pa->layout);
129
130         /* only for operators that are registered and did an undo push */
131         for(op= wm->operators.last; op; op= op->prev)
132                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
133                         break;
134         
135         if(op==NULL)
136                 return;
137         if(op->type->poll && op->type->poll((bContext *)C)==0)
138                 return;
139         
140         uiBlockSetFunc(block, redo_cb, op, NULL);
141         
142         if(!op->properties) {
143                 IDPropertyTemplate val = {0};
144                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
145         }
146         
147         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
148         uiDefAutoButsRNA(C, pa->layout, &ptr, 1);
149 }
150
151 static void view3d_panel_tools(const bContext *C, Panel *pa)
152 {
153         Object *obedit= CTX_data_edit_object(C);
154 //      Object *obact = CTX_data_active_object(C);
155         uiLayout *col;
156         
157         if(obedit) {
158                 if(obedit->type==OB_MESH) {
159                         
160                         // void uiItemFullO(uiLayout *layout, char *name, int icon, char *idname, IDProperty *properties, int context)
161                         col= uiLayoutColumn(pa->layout, 1);
162                         uiItemFullO(col, NULL, 0, "MESH_OT_delete", NULL, WM_OP_INVOKE_REGION_WIN);
163                         
164                         col= uiLayoutColumn(pa->layout, 1);
165                         uiItemFullO(col, NULL, 0, "MESH_OT_subdivide", NULL, WM_OP_INVOKE_REGION_WIN);
166                         
167                         col= uiLayoutColumn(pa->layout, 1);
168                         uiItemFullO(col, NULL, 0, "MESH_OT_primitive_monkey_add", NULL, WM_OP_INVOKE_REGION_WIN);
169                         uiItemFullO(col, NULL, 0, "MESH_OT_primitive_uv_sphere_add", NULL, WM_OP_INVOKE_REGION_WIN);
170                         
171                         col= uiLayoutColumn(pa->layout, 1);
172                         uiItemFullO(col, NULL, 0, "MESH_OT_select_all_toggle", NULL, WM_OP_INVOKE_REGION_WIN);
173                         
174                         col= uiLayoutColumn(pa->layout, 1);
175                         uiItemFullO(col, NULL, 0, "MESH_OT_spin", NULL, WM_OP_INVOKE_REGION_WIN);
176                         uiItemFullO(col, NULL, 0, "MESH_OT_screw", NULL, WM_OP_INVOKE_REGION_WIN);
177                         
178                 }
179         }
180         else {
181                 
182                 col= uiLayoutColumn(pa->layout, 1);
183                 uiItemFullO(col, NULL, 0, "OBJECT_OT_delete", NULL, WM_OP_INVOKE_REGION_WIN);
184                 uiItemFullO(col, NULL, 0, "OBJECT_OT_primitive_add", NULL, WM_OP_INVOKE_REGION_WIN);
185                 
186                 col= uiLayoutColumn(pa->layout, 1);
187                 uiItemFullO(col, NULL, 0, "OBJECT_OT_parent_set", NULL, WM_OP_INVOKE_REGION_WIN);
188                 uiItemFullO(col, NULL, 0, "OBJECT_OT_parent_clear", NULL, WM_OP_INVOKE_REGION_WIN);
189                 
190         }
191 }
192
193
194 void view3d_toolbar_register(ARegionType *art)
195 {
196         PanelType *pt;
197
198         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
199         strcpy(pt->idname, "VIEW3D_PT_tools");
200         strcpy(pt->label, "Tools");
201         pt->draw= view3d_panel_tools;
202         BLI_addtail(&art->paneltypes, pt);
203         
204         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
205         strcpy(pt->idname, "VIEW3D_PT_last_operator");
206         strcpy(pt->label, "Last Operator");
207         pt->draw= view3d_panel_operator_redo;
208         BLI_addtail(&art->paneltypes, pt);
209 }
210
211 static int view3d_toolbar(bContext *C, wmOperator *op)
212 {
213         ScrArea *sa= CTX_wm_area(C);
214         ARegion *ar= view3d_has_tools_region(sa);
215         
216         if(ar) {
217                 ar->flag ^= RGN_FLAG_HIDDEN;
218                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
219                 
220                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
221                 ED_area_tag_redraw(sa);
222         }
223         return OPERATOR_FINISHED;
224 }
225
226 void VIEW3D_OT_toolbar(wmOperatorType *ot)
227 {
228         ot->name= "Toolbar";
229         ot->idname= "VIEW3D_OT_toolbar";
230         
231         ot->exec= view3d_toolbar;
232         ot->poll= ED_operator_view3d_active;
233         
234         /* flags */
235         ot->flag= 0;
236 }