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