1064611b84f3a204a4c7b26b737f11d8412a4271
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_object_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_editVert.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_idprop.h"
46 #include "BKE_global.h"
47 #include "BKE_screen.h"
48
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "RNA_access.h"
54
55 #include "ED_screen.h"
56 #include "ED_util.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60
61 #include "view3d_intern.h"      // own include
62
63
64 /* ******************* view3d space & buttons ************** */
65
66
67 /* op->exec */
68 /* XXX DUPLICATE CODE */
69 static void redo_cb(bContext *C, void *arg_op, void *UNUSED(arg2))
70 {
71         wmOperator *lastop= arg_op;
72         
73         if(lastop) {
74                 ARegion *ar= CTX_wm_region(C);
75                 ARegion *ar1= BKE_area_find_region_type(CTX_wm_area(C), RGN_TYPE_WINDOW);
76                 int retval;
77                 
78                 if(ar1)
79                         CTX_wm_region_set(C, ar1);
80                 
81                 if (G.f & G_DEBUG)
82                         printf("operator redo %s\n", lastop->type->name);
83                 
84                 ED_undo_pop_op(C, lastop);
85                 retval= WM_operator_repeat(C, lastop);
86                 
87                 if((retval & OPERATOR_FINISHED)==0) {
88                         if (G.f & G_DEBUG)
89                                 printf("operator redo failed %s\n", lastop->type->name);
90                         ED_undo_redo(C);
91                 }
92                 
93                 /* set region back */
94                 CTX_wm_region_set(C, ar);
95         }
96 }
97
98 static wmOperator *view3d_last_operator(const bContext *C)
99 {
100         wmWindowManager *wm= CTX_wm_manager(C);
101         wmOperator *op;
102
103         /* only for operators that are registered and did an undo push */
104         for(op= wm->operators.last; op; op= op->prev)
105                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
106                         break;
107
108         return op;
109 }
110
111 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
112 {
113         wmWindowManager *wm= CTX_wm_manager(C);
114         PointerRNA ptr;
115         
116         if(!op->properties) {
117                 IDPropertyTemplate val = {0};
118                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
119         }
120         
121         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
122         if(op->type->ui) {
123                 op->layout= pa->layout;
124                 op->type->ui((bContext*)C, op);
125                 op->layout= NULL;
126         }
127         else
128                 uiDefAutoButsRNA(pa->layout, &ptr, 1);
129 }
130
131 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
132 {
133         wmOperator *op= view3d_last_operator(C);
134
135         if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
136         else BLI_strncpy(pa->drawname, "Operator", sizeof(pa->drawname));
137 }
138
139 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
140 {
141         if(op->type->flag & OPTYPE_MACRO) {
142                 for(op= op->macro.first; op; op= op->next) {
143                         uiItemL(pa->layout, op->type->name, 0);
144                         view3d_panel_operator_redo_operator(C, pa, op);
145                 }
146         }
147         else {
148                 view3d_panel_operator_redo_buts(C, pa, op);
149         }
150 }
151
152 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
153 {
154         wmOperator *op= view3d_last_operator(C);
155         uiBlock *block;
156         
157         if(op==NULL)
158                 return;
159         if(WM_operator_poll((bContext*)C, op->type) == 0)
160                 return;
161         
162         block= uiLayoutGetBlock(pa->layout);
163
164         uiBlockSetFunc(block, redo_cb, op, NULL);
165         
166         view3d_panel_operator_redo_operator(C, pa, op);
167 }
168
169 /* ******************* */
170
171 typedef struct CustomTool {
172         struct CustomTool *next, *prev;
173         char opname[OP_MAX_TYPENAME];
174         char context[OP_MAX_TYPENAME];
175 } CustomTool;
176
177 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
178 {
179         wmOperatorType *ot= arg2;
180         
181         if(ot) {
182                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
183                 
184                 BLI_addtail(arg_listbase, ct);
185                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
186                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
187         }
188                 
189 }
190
191 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
192 {
193         wmOperatorType *ot = WM_operatortype_first();
194         
195         for(; ot; ot= ot->next) {
196                 
197                 if(BLI_strcasestr(ot->name, str)) {
198                         if(WM_operator_poll((bContext*)C, ot)) {
199                                 
200                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
201                                         break;
202                         }
203                 }
204         }
205 }
206
207
208 /* ID Search browse menu, open */
209 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
210 {
211         static char search[OP_MAX_TYPENAME];
212         wmEvent event;
213         wmWindow *win= CTX_wm_window(C);
214         uiBlock *block;
215         uiBut *but;
216         
217         /* clear initial search string, then all items show */
218         search[0]= 0;
219         
220         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
221         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
222         
223         /* fake button, it holds space for search items */
224         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
225         
226         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
227         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
228         
229         uiBoundsBlock(block, 6);
230         uiBlockSetDirection(block, UI_DOWN);    
231         uiEndBlock(C, block);
232         
233         event= *(win->eventstate);      /* XXX huh huh? make api call */
234         event.type= EVT_BUT_OPEN;
235         event.val= KM_PRESS;
236         event.customdata= but;
237         event.customdatafree= FALSE;
238         wm_event_add(win, &event);
239         
240         return block;
241 }
242
243
244 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
245 {
246         SpaceLink *sl= CTX_wm_space_data(C);
247         SpaceType *st= NULL;
248         uiLayout *col;
249         const char *context= CTX_data_mode_string(C);
250         
251         if(sl)
252                 st= BKE_spacetype_from_id(sl->spacetype);
253         
254         if(st && st->toolshelf.first) {
255                 CustomTool *ct;
256                 
257                 for(ct= st->toolshelf.first; ct; ct= ct->next) {
258                         if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
259                                 col= uiLayoutColumn(pa->layout, 1);
260                                 uiItemFullO(col, ct->opname, NULL, 0, NULL, WM_OP_INVOKE_REGION_WIN, 0);
261                         }
262                 }
263         }
264         col= uiLayoutColumn(pa->layout, 1);
265         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");
266 }
267
268
269 void view3d_toolshelf_register(ARegionType *art)
270 {
271         PanelType *pt;
272
273         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
274         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
275         strcpy(pt->label, "Tool Shelf");
276         pt->draw= view3d_panel_tool_shelf;
277         BLI_addtail(&art->paneltypes, pt);
278 }
279
280 void view3d_tool_props_register(ARegionType *art)
281 {
282         PanelType *pt;
283         
284         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
285         strcpy(pt->idname, "VIEW3D_PT_last_operator");
286         strcpy(pt->label, "Operator");
287         pt->draw_header= view3d_panel_operator_redo_header;
288         pt->draw= view3d_panel_operator_redo;
289         BLI_addtail(&art->paneltypes, pt);
290 }
291
292 /* ********** operator to open/close toolshelf region */
293
294 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
295 {
296         ScrArea *sa= CTX_wm_area(C);
297         ARegion *ar= view3d_has_tools_region(sa);
298         
299         if(ar)
300                 ED_region_toggle_hidden(C, ar);
301
302         return OPERATOR_FINISHED;
303 }
304
305 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
306 {
307         ot->name= "Tool Shelf";
308         ot->description= "Toggles tool shelf display";
309         ot->idname= "VIEW3D_OT_toolshelf";
310         
311         ot->exec= view3d_toolshelf;
312         ot->poll= ED_operator_view3d_active;
313         
314         /* flags */
315         ot->flag= 0;
316 }