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