NLA SoC: Merge from 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 /* ******************* */
152
153 typedef struct CustomTool {
154         struct CustomTool *next, *prev;
155         char opname[OP_MAX_TYPENAME];
156 } CustomTool;
157
158 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
159 {
160         wmOperatorType *ot= arg2;
161         
162         if(ot) {
163                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
164                 
165                 BLI_addtail(arg_listbase, ct);
166                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
167         }
168                 
169 }
170
171 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
172 {
173         wmOperatorType *ot = WM_operatortype_first();
174         
175         for(; ot; ot= ot->next) {
176                 
177                 if(BLI_strcasestr(ot->name, str)) {
178                         if(ot->poll==NULL || ot->poll((bContext *)C)) {
179                                 
180                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
181                                         break;
182                         }
183                 }
184         }
185 }
186
187
188 /* ID Search browse menu, open */
189 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
190 {
191         static char search[OP_MAX_TYPENAME];
192         wmEvent event;
193         wmWindow *win= CTX_wm_window(C);
194         uiBlock *block;
195         uiBut *but;
196         
197         /* clear initial search string, then all items show */
198         search[0]= 0;
199         
200         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
201         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
202         
203         /* fake button, it holds space for search items */
204         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
205         
206         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, "");
207         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
208         
209         uiBoundsBlock(block, 6);
210         uiBlockSetDirection(block, UI_DOWN);    
211         uiEndBlock(C, block);
212         
213         event= *(win->eventstate);      /* XXX huh huh? make api call */
214         event.type= EVT_BUT_OPEN;
215         event.val= KM_PRESS;
216         event.customdata= but;
217         event.customdatafree= FALSE;
218         wm_event_add(win, &event);
219         
220         return block;
221 }
222
223
224 static void view3d_panel_tools(const bContext *C, Panel *pa)
225 {
226         static ListBase tools= {NULL, NULL};
227         Object *obedit= CTX_data_edit_object(C);
228 //      Object *obact = CTX_data_active_object(C);
229         uiLayout *col;
230         
231         if(obedit) {
232                 if(obedit->type==OB_MESH) {
233                         
234                         col= uiLayoutColumn(pa->layout, 1);
235                         uiItemFullO(col, NULL, 0, "MESH_OT_spin", NULL, WM_OP_INVOKE_REGION_WIN);
236                         uiItemFullO(col, NULL, 0, "MESH_OT_screw", NULL, WM_OP_INVOKE_REGION_WIN);
237                         
238                         if(tools.first) {
239                                 CustomTool *ct;
240                                 
241                                 for(ct= tools.first; ct; ct= ct->next) {
242                                         col= uiLayoutColumn(pa->layout, 1);
243                                         uiItemFullO(col, NULL, 0, ct->opname, NULL, WM_OP_INVOKE_REGION_WIN);
244                                 }
245                         }
246                         col= uiLayoutColumn(pa->layout, 1);
247                         uiDefBlockBut(uiLayoutGetBlock(pa->layout), tool_search_menu, &tools, "Add Operator", 0, 0, UI_UNIT_X, UI_UNIT_Y, "Add tool");
248                 }
249         }
250         else {
251                 
252                 col= uiLayoutColumn(pa->layout, 1);
253                 uiItemFullO(col, NULL, 0, "OBJECT_OT_delete", NULL, WM_OP_INVOKE_REGION_WIN);
254                 uiItemFullO(col, NULL, 0, "OBJECT_OT_primitive_add", NULL, WM_OP_INVOKE_REGION_WIN);
255                 
256                 col= uiLayoutColumn(pa->layout, 1);
257                 uiItemFullO(col, NULL, 0, "OBJECT_OT_parent_set", NULL, WM_OP_INVOKE_REGION_WIN);
258                 uiItemFullO(col, NULL, 0, "OBJECT_OT_parent_clear", NULL, WM_OP_INVOKE_REGION_WIN);
259                 
260         }
261 }
262
263
264 void view3d_toolbar_register(ARegionType *art)
265 {
266         PanelType *pt;
267
268         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
269         strcpy(pt->idname, "VIEW3D_PT_tools");
270         strcpy(pt->label, "Tools");
271         pt->draw= view3d_panel_tools;
272         BLI_addtail(&art->paneltypes, pt);
273         
274         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
275         strcpy(pt->idname, "VIEW3D_PT_last_operator");
276         strcpy(pt->label, "Last Operator");
277         pt->draw= view3d_panel_operator_redo;
278         BLI_addtail(&art->paneltypes, pt);
279 }
280
281 static int view3d_toolbar(bContext *C, wmOperator *op)
282 {
283         ScrArea *sa= CTX_wm_area(C);
284         ARegion *ar= view3d_has_tools_region(sa);
285         
286         if(ar) {
287                 ar->flag ^= RGN_FLAG_HIDDEN;
288                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
289                 
290                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
291                 ED_area_tag_redraw(sa);
292         }
293         return OPERATOR_FINISHED;
294 }
295
296 void VIEW3D_OT_toolbar(wmOperatorType *ot)
297 {
298         ot->name= "Toolbar";
299         ot->idname= "VIEW3D_OT_toolbar";
300         
301         ot->exec= view3d_toolbar;
302         ot->poll= ED_operator_view3d_active;
303         
304         /* flags */
305         ot->flag= 0;
306 }