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