2.5 3D_View:
[blender-staging.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
75 #include "WM_api.h"
76 #include "WM_types.h"
77
78 #include "RNA_access.h"
79 #include "RNA_define.h"
80
81 #include "ED_armature.h"
82 #include "ED_curve.h"
83 #include "ED_image.h"
84 #include "ED_keyframing.h"
85 #include "ED_mesh.h"
86 #include "ED_object.h"
87 #include "ED_particle.h"
88 #include "ED_screen.h"
89 #include "ED_transform.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_op(C, lastop);
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_buts(const bContext *C, Panel *pa, wmOperator *op)
122 {
123         wmWindowManager *wm= CTX_wm_manager(C);
124         PointerRNA ptr;
125         
126         if(!op->properties) {
127                 IDPropertyTemplate val = {0};
128                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
129         }
130         
131         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
132         uiDefAutoButsRNA(C, pa->layout, &ptr, 1);
133         
134 }
135
136 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
137 {
138         wmWindowManager *wm= CTX_wm_manager(C);
139         wmOperator *op;
140         uiBlock *block;
141         
142         block= uiLayoutGetBlock(pa->layout);
143
144         /* only for operators that are registered and did an undo push */
145         for(op= wm->operators.last; op; op= op->prev)
146                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
147                         break;
148         
149         if(op==NULL)
150                 return;
151         if(op->type->poll && op->type->poll((bContext *)C)==0)
152                 return;
153         
154         uiBlockSetFunc(block, redo_cb, op, NULL);
155         
156         if(op->macro.first) {
157                 for(op= op->macro.first; op; op= op->next)
158                         view3d_panel_operator_redo_buts(C, pa, op);
159         }
160         else {
161                 view3d_panel_operator_redo_buts(C, pa, op);
162         }
163 }
164
165 /* ******************* */
166
167 typedef struct CustomTool {
168         struct CustomTool *next, *prev;
169         char opname[OP_MAX_TYPENAME];
170         char context[OP_MAX_TYPENAME];
171 } CustomTool;
172
173 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
174 {
175         wmOperatorType *ot= arg2;
176         
177         if(ot) {
178                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
179                 
180                 BLI_addtail(arg_listbase, ct);
181                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
182                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
183         }
184                 
185 }
186
187 static void operator_search_cb(const struct bContext *C, void *arg, char *str, uiSearchItems *items)
188 {
189         wmOperatorType *ot = WM_operatortype_first();
190         
191         for(; ot; ot= ot->next) {
192                 
193                 if(BLI_strcasestr(ot->name, str)) {
194                         if(ot->poll==NULL || ot->poll((bContext *)C)) {
195                                 
196                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
197                                         break;
198                         }
199                 }
200         }
201 }
202
203
204 /* ID Search browse menu, open */
205 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
206 {
207         static char search[OP_MAX_TYPENAME];
208         wmEvent event;
209         wmWindow *win= CTX_wm_window(C);
210         uiBlock *block;
211         uiBut *but;
212         
213         /* clear initial search string, then all items show */
214         search[0]= 0;
215         
216         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
217         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
218         
219         /* fake button, it holds space for search items */
220         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
221         
222         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, "");
223         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
224         
225         uiBoundsBlock(block, 6);
226         uiBlockSetDirection(block, UI_DOWN);    
227         uiEndBlock(C, block);
228         
229         event= *(win->eventstate);      /* XXX huh huh? make api call */
230         event.type= EVT_BUT_OPEN;
231         event.val= KM_PRESS;
232         event.customdata= but;
233         event.customdatafree= FALSE;
234         wm_event_add(win, &event);
235         
236         return block;
237 }
238
239
240 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
241 {
242         SpaceLink *sl= CTX_wm_space_data(C);
243         SpaceType *st= NULL;
244         uiLayout *col;
245         const char *context= CTX_data_mode_string(C);
246         
247         if(sl)
248                 st= BKE_spacetype_from_id(sl->spacetype);
249         
250         if(st && st->toolshelf.first) {
251                 CustomTool *ct;
252                 
253                 for(ct= st->toolshelf.first; ct; ct= ct->next) {
254                         if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
255                                 col= uiLayoutColumn(pa->layout, 1);
256                                 uiItemFullO(col, NULL, 0, ct->opname, NULL, WM_OP_INVOKE_REGION_WIN);
257                         }
258                 }
259         }
260         col= uiLayoutColumn(pa->layout, 1);
261         uiDefBlockBut(uiLayoutGetBlock(pa->layout), tool_search_menu, &st->toolshelf, "Add Tool", 0, 0, UI_UNIT_X, UI_UNIT_Y, "Add Tool in shelf, gets saved in files");
262 }
263
264
265 void view3d_toolbar_register(ARegionType *art)
266 {
267         PanelType *pt;
268
269         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
270         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
271         strcpy(pt->label, "Tool Shelf");
272         pt->draw= view3d_panel_tool_shelf;
273         BLI_addtail(&art->paneltypes, pt);
274 }
275
276 void view3d_tool_props_register(ARegionType *art)
277 {
278         PanelType *pt;
279         
280         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
281         strcpy(pt->idname, "VIEW3D_PT_last_operator");
282         strcpy(pt->label, "Last Operator");
283         pt->draw= view3d_panel_operator_redo;
284         BLI_addtail(&art->paneltypes, pt);
285 }
286
287 /* ********** operator to open/close toolbar region */
288
289 static int view3d_toolbar(bContext *C, wmOperator *op)
290 {
291         ScrArea *sa= CTX_wm_area(C);
292         ARegion *ar= view3d_has_tools_region(sa);
293         
294         if(ar) {
295                 ar->flag ^= RGN_FLAG_HIDDEN;
296                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
297                 
298                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
299                 ED_area_tag_redraw(sa);
300         }
301         return OPERATOR_FINISHED;
302 }
303
304 void VIEW3D_OT_toolbar(wmOperatorType *ot)
305 {
306         ot->name= "Toolbar";
307         ot->idname= "VIEW3D_OT_toolbar";
308         
309         ot->exec= view3d_toolbar;
310         ot->poll= ED_operator_view3d_active;
311         
312         /* flags */
313         ot->flag= 0;
314 }