Bugfix #25570
[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., 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 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_idprop.h"
47 #include "BKE_global.h"
48 #include "BKE_screen.h"
49
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "RNA_access.h"
55
56 #include "ED_screen.h"
57 #include "ED_util.h"
58
59 #include "UI_interface.h"
60 #include "UI_resources.h"
61
62 #include "view3d_intern.h"      // own include
63
64
65 /* ******************* view3d space & buttons ************** */
66
67 static wmOperator *view3d_last_operator(const bContext *C)
68 {
69         wmWindowManager *wm= CTX_wm_manager(C);
70         wmOperator *op;
71
72         /* only for operators that are registered and did an undo push */
73         for(op= wm->operators.last; op; op= op->prev)
74                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
75                         break;
76
77         return op;
78 }
79
80 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
81 {
82         uiLayoutOperatorButs(C, pa->layout, op, NULL, 'V', 0);
83 }
84
85 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
86 {
87         wmOperator *op= view3d_last_operator(C);
88
89         if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
90         else BLI_strncpy(pa->drawname, "Operator", sizeof(pa->drawname));
91 }
92
93 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
94 {
95         if(op->type->flag & OPTYPE_MACRO) {
96                 for(op= op->macro.first; op; op= op->next) {
97                         uiItemL(pa->layout, op->type->name, ICON_NULL);
98                         view3d_panel_operator_redo_operator(C, pa, op);
99                 }
100         }
101         else {
102                 view3d_panel_operator_redo_buts(C, pa, op);
103         }
104 }
105
106 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
107 {
108         wmOperator *op= view3d_last_operator(C);
109         uiBlock *block;
110         
111         if(op==NULL)
112                 return;
113         if(WM_operator_poll((bContext*)C, op->type) == 0)
114                 return;
115         
116         block= uiLayoutGetBlock(pa->layout);
117         
118         if(ED_undo_valid(C, op->type->name)==0)
119                 uiLayoutSetEnabled(pa->layout, 0);
120
121         uiBlockSetFunc(block, ED_undo_operator_repeat_cb, op, NULL);
122         
123         view3d_panel_operator_redo_operator(C, pa, op);
124 }
125
126 /* ******************* */
127
128 typedef struct CustomTool {
129         struct CustomTool *next, *prev;
130         char opname[OP_MAX_TYPENAME];
131         char context[OP_MAX_TYPENAME];
132 } CustomTool;
133
134 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
135 {
136         wmOperatorType *ot= arg2;
137         
138         if(ot) {
139                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
140                 
141                 BLI_addtail(arg_listbase, ct);
142                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
143                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
144         }
145                 
146 }
147
148 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
149 {
150         wmOperatorType *ot = WM_operatortype_first();
151         
152         for(; ot; ot= ot->next) {
153                 
154                 if(BLI_strcasestr(ot->name, str)) {
155                         if(WM_operator_poll((bContext*)C, ot)) {
156                                 
157                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
158                                         break;
159                         }
160                 }
161         }
162 }
163
164
165 /* ID Search browse menu, open */
166 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
167 {
168         static char search[OP_MAX_TYPENAME];
169         wmEvent event;
170         wmWindow *win= CTX_wm_window(C);
171         uiBlock *block;
172         uiBut *but;
173         
174         /* clear initial search string, then all items show */
175         search[0]= 0;
176         
177         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
178         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
179         
180         /* fake button, it holds space for search items */
181         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
182         
183         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
184         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
185         
186         uiBoundsBlock(block, 6);
187         uiBlockSetDirection(block, UI_DOWN);    
188         uiEndBlock(C, block);
189         
190         event= *(win->eventstate);      /* XXX huh huh? make api call */
191         event.type= EVT_BUT_OPEN;
192         event.val= KM_PRESS;
193         event.customdata= but;
194         event.customdatafree= FALSE;
195         wm_event_add(win, &event);
196         
197         return block;
198 }
199
200
201 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
202 {
203         SpaceLink *sl= CTX_wm_space_data(C);
204         SpaceType *st= NULL;
205         uiLayout *col;
206         const char *context= CTX_data_mode_string(C);
207         
208         if(sl)
209                 st= BKE_spacetype_from_id(sl->spacetype);
210         
211         if(st && st->toolshelf.first) {
212                 CustomTool *ct;
213                 
214                 for(ct= st->toolshelf.first; ct; ct= ct->next) {
215                         if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
216                                 col= uiLayoutColumn(pa->layout, 1);
217                                 uiItemFullO(col, ct->opname, NULL, ICON_NULL, NULL, WM_OP_INVOKE_REGION_WIN, 0);
218                         }
219                 }
220         }
221         col= uiLayoutColumn(pa->layout, 1);
222         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");
223 }
224
225
226 void view3d_toolshelf_register(ARegionType *art)
227 {
228         PanelType *pt;
229
230         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
231         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
232         strcpy(pt->label, "Tool Shelf");
233         pt->draw= view3d_panel_tool_shelf;
234         BLI_addtail(&art->paneltypes, pt);
235 }
236
237 void view3d_tool_props_register(ARegionType *art)
238 {
239         PanelType *pt;
240         
241         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
242         strcpy(pt->idname, "VIEW3D_PT_last_operator");
243         strcpy(pt->label, "Operator");
244         pt->draw_header= view3d_panel_operator_redo_header;
245         pt->draw= view3d_panel_operator_redo;
246         BLI_addtail(&art->paneltypes, pt);
247 }
248
249 /* ********** operator to open/close toolshelf region */
250
251 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
252 {
253         ScrArea *sa= CTX_wm_area(C);
254         ARegion *ar= view3d_has_tools_region(sa);
255         
256         if(ar)
257                 ED_region_toggle_hidden(C, ar);
258
259         return OPERATOR_FINISHED;
260 }
261
262 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
263 {
264         ot->name= "Tool Shelf";
265         ot->description= "Toggles tool shelf display";
266         ot->idname= "VIEW3D_OT_toolshelf";
267         
268         ot->exec= view3d_toolshelf;
269         ot->poll= ED_operator_view3d_active;
270         
271         /* flags */
272         ot->flag= 0;
273 }