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