bugfix [#25230] Quick extrude Ctrl-LMB : wrong behaviour of 'RotateSource' option.
[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 static wmOperator *view3d_last_operator(const bContext *C)
67 {
68         wmWindowManager *wm= CTX_wm_manager(C);
69         wmOperator *op;
70
71         /* only for operators that are registered and did an undo push */
72         for(op= wm->operators.last; op; op= op->prev)
73                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
74                         break;
75
76         return op;
77 }
78
79 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
80 {
81         wmWindowManager *wm= CTX_wm_manager(C);
82         PointerRNA ptr;
83         
84         if(!op->properties) {
85                 IDPropertyTemplate val = {0};
86                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
87         }
88         
89         /* poll() on this operator may still fail, at the moment there is no nice feedback when this happens
90          * just fails silently */
91         if(!WM_operator_repeat_check(C, op)) {
92                 uiBlockSetButLock(uiLayoutGetBlock(pa->layout), TRUE, "Operator cannot redo");
93                 uiItemL(pa->layout, "* Redo Unsupported *", 0); // XXX, could give some nicer feedback or not show redo panel at all?
94         }
95
96         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
97         if(op->type->ui) {
98                 op->layout= pa->layout;
99                 op->type->ui((bContext*)C, op);
100                 op->layout= NULL;
101         }
102         else
103                 uiDefAutoButsRNA(pa->layout, &ptr, 1);
104 }
105
106 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
107 {
108         wmOperator *op= view3d_last_operator(C);
109
110         if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
111         else BLI_strncpy(pa->drawname, "Operator", sizeof(pa->drawname));
112 }
113
114 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
115 {
116         if(op->type->flag & OPTYPE_MACRO) {
117                 for(op= op->macro.first; op; op= op->next) {
118                         uiItemL(pa->layout, op->type->name, 0);
119                         view3d_panel_operator_redo_operator(C, pa, op);
120                 }
121         }
122         else {
123                 view3d_panel_operator_redo_buts(C, pa, op);
124         }
125 }
126
127 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
128 {
129         wmOperator *op= view3d_last_operator(C);
130         uiBlock *block;
131         
132         if(op==NULL)
133                 return;
134         if(WM_operator_poll((bContext*)C, op->type) == 0)
135                 return;
136         
137         block= uiLayoutGetBlock(pa->layout);
138
139         uiBlockSetFunc(block, ED_undo_operator_repeat_cb, op, NULL);
140         
141         view3d_panel_operator_redo_operator(C, pa, op);
142 }
143
144 /* ******************* */
145
146 typedef struct CustomTool {
147         struct CustomTool *next, *prev;
148         char opname[OP_MAX_TYPENAME];
149         char context[OP_MAX_TYPENAME];
150 } CustomTool;
151
152 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
153 {
154         wmOperatorType *ot= arg2;
155         
156         if(ot) {
157                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
158                 
159                 BLI_addtail(arg_listbase, ct);
160                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
161                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
162         }
163                 
164 }
165
166 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
167 {
168         wmOperatorType *ot = WM_operatortype_first();
169         
170         for(; ot; ot= ot->next) {
171                 
172                 if(BLI_strcasestr(ot->name, str)) {
173                         if(WM_operator_poll((bContext*)C, ot)) {
174                                 
175                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
176                                         break;
177                         }
178                 }
179         }
180 }
181
182
183 /* ID Search browse menu, open */
184 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
185 {
186         static char search[OP_MAX_TYPENAME];
187         wmEvent event;
188         wmWindow *win= CTX_wm_window(C);
189         uiBlock *block;
190         uiBut *but;
191         
192         /* clear initial search string, then all items show */
193         search[0]= 0;
194         
195         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
196         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
197         
198         /* fake button, it holds space for search items */
199         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
200         
201         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
202         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
203         
204         uiBoundsBlock(block, 6);
205         uiBlockSetDirection(block, UI_DOWN);    
206         uiEndBlock(C, block);
207         
208         event= *(win->eventstate);      /* XXX huh huh? make api call */
209         event.type= EVT_BUT_OPEN;
210         event.val= KM_PRESS;
211         event.customdata= but;
212         event.customdatafree= FALSE;
213         wm_event_add(win, &event);
214         
215         return block;
216 }
217
218
219 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
220 {
221         SpaceLink *sl= CTX_wm_space_data(C);
222         SpaceType *st= NULL;
223         uiLayout *col;
224         const char *context= CTX_data_mode_string(C);
225         
226         if(sl)
227                 st= BKE_spacetype_from_id(sl->spacetype);
228         
229         if(st && st->toolshelf.first) {
230                 CustomTool *ct;
231                 
232                 for(ct= st->toolshelf.first; ct; ct= ct->next) {
233                         if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
234                                 col= uiLayoutColumn(pa->layout, 1);
235                                 uiItemFullO(col, ct->opname, NULL, 0, NULL, WM_OP_INVOKE_REGION_WIN, 0);
236                         }
237                 }
238         }
239         col= uiLayoutColumn(pa->layout, 1);
240         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");
241 }
242
243
244 void view3d_toolshelf_register(ARegionType *art)
245 {
246         PanelType *pt;
247
248         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
249         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
250         strcpy(pt->label, "Tool Shelf");
251         pt->draw= view3d_panel_tool_shelf;
252         BLI_addtail(&art->paneltypes, pt);
253 }
254
255 void view3d_tool_props_register(ARegionType *art)
256 {
257         PanelType *pt;
258         
259         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
260         strcpy(pt->idname, "VIEW3D_PT_last_operator");
261         strcpy(pt->label, "Operator");
262         pt->draw_header= view3d_panel_operator_redo_header;
263         pt->draw= view3d_panel_operator_redo;
264         BLI_addtail(&art->paneltypes, pt);
265 }
266
267 /* ********** operator to open/close toolshelf region */
268
269 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
270 {
271         ScrArea *sa= CTX_wm_area(C);
272         ARegion *ar= view3d_has_tools_region(sa);
273         
274         if(ar)
275                 ED_region_toggle_hidden(C, ar);
276
277         return OPERATOR_FINISHED;
278 }
279
280 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
281 {
282         ot->name= "Tool Shelf";
283         ot->description= "Toggles tool shelf display";
284         ot->idname= "VIEW3D_OT_toolshelf";
285         
286         ot->exec= view3d_toolshelf;
287         ot->poll= ED_operator_view3d_active;
288         
289         /* flags */
290         ot->flag= 0;
291 }