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