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