svn merge ^/trunk/blender -r42564:42586
[blender-staging.git] / source / blender / editors / space_view3d / view3d_toolbar.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_toolbar.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_math.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_editVert.h"
45 #include "BLI_rand.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
48
49 #include "BLF_translation.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, IFACE_("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 /* TODO de-duplicate redo panel functions - campbell */
100 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
101 {
102         wmOperator *op= WM_operator_last_redo(C);
103         uiBlock *block;
104         
105         if(op==NULL)
106                 return;
107         if(WM_operator_poll((bContext*)C, op->type) == 0)
108                 return;
109         
110         block= uiLayoutGetBlock(pa->layout);
111         
112         if (!WM_operator_check_ui_enabled(C, op->type->name))
113                 uiLayoutSetEnabled(pa->layout, 0);
114
115         /* note, blockfunc is a default but->func, use Handle func to allow button callbacks too */
116         uiBlockSetHandleFunc(block, ED_undo_operator_repeat_cb_evt, op);
117         
118         view3d_panel_operator_redo_operator(C, pa, op);
119 }
120
121 /* ******************* */
122
123 typedef struct CustomTool {
124         struct CustomTool *next, *prev;
125         char opname[OP_MAX_TYPENAME];
126         char context[OP_MAX_TYPENAME];
127 } CustomTool;
128
129 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
130 {
131         wmOperatorType *ot= arg2;
132         
133         if(ot) {
134                 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
135                 
136                 BLI_addtail(arg_listbase, ct);
137                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
138                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
139         }
140                 
141 }
142
143 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
144 {
145         GHashIterator *iter= WM_operatortype_iter();
146
147         for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
148                 wmOperatorType *ot= BLI_ghashIterator_getValue(iter);
149
150                 if(BLI_strcasestr(ot->name, str)) {
151                         if(WM_operator_poll((bContext*)C, ot)) {
152                                 
153                                 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
154                                         break;
155                         }
156                 }
157         }
158         BLI_ghashIterator_free(iter);
159 }
160
161
162 /* ID Search browse menu, open */
163 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
164 {
165         static char search[OP_MAX_TYPENAME];
166         wmEvent event;
167         wmWindow *win= CTX_wm_window(C);
168         uiBlock *block;
169         uiBut *but;
170         
171         /* clear initial search string, then all items show */
172         search[0]= 0;
173         
174         block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
175         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
176         
177         /* fake button, it holds space for search items */
178         uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
179         
180         but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
181         uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
182         
183         uiBoundsBlock(block, 6);
184         uiBlockSetDirection(block, UI_DOWN);    
185         uiEndBlock(C, block);
186         
187         event= *(win->eventstate);      /* XXX huh huh? make api call */
188         event.type= EVT_BUT_OPEN;
189         event.val= KM_PRESS;
190         event.customdata= but;
191         event.customdatafree= FALSE;
192         wm_event_add(win, &event);
193         
194         return block;
195 }
196
197
198 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
199 {
200         SpaceLink *sl= CTX_wm_space_data(C);
201         SpaceType *st= NULL;
202         uiLayout *col;
203         const char *context= CTX_data_mode_string(C);
204         
205         if(sl)
206                 st= BKE_spacetype_from_id(sl->spacetype);
207         
208         if(st && st->toolshelf.first) {
209                 CustomTool *ct;
210                 
211                 for(ct= st->toolshelf.first; ct; ct= ct->next) {
212                         if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
213                                 col= uiLayoutColumn(pa->layout, 1);
214                                 uiItemFullO(col, ct->opname, NULL, ICON_NONE, NULL, WM_OP_INVOKE_REGION_WIN, 0);
215                         }
216                 }
217         }
218         col= uiLayoutColumn(pa->layout, 1);
219         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");
220 }
221
222
223 void view3d_toolshelf_register(ARegionType *art)
224 {
225         PanelType *pt;
226
227         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
228         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
229         strcpy(pt->label, "Tool Shelf");
230         pt->draw= view3d_panel_tool_shelf;
231         BLI_addtail(&art->paneltypes, pt);
232 }
233
234 void view3d_tool_props_register(ARegionType *art)
235 {
236         PanelType *pt;
237         
238         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
239         strcpy(pt->idname, "VIEW3D_PT_last_operator");
240         strcpy(pt->label, "Operator");
241         pt->draw_header= view3d_panel_operator_redo_header;
242         pt->draw= view3d_panel_operator_redo;
243         BLI_addtail(&art->paneltypes, pt);
244 }
245
246 /* ********** operator to open/close toolshelf region */
247
248 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
249 {
250         ScrArea *sa= CTX_wm_area(C);
251         ARegion *ar= view3d_has_tools_region(sa);
252         
253         if(ar)
254                 ED_region_toggle_hidden(C, ar);
255
256         return OPERATOR_FINISHED;
257 }
258
259 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
260 {
261         ot->name= "Tool Shelf";
262         ot->description= "Toggles tool shelf display";
263         ot->idname= "VIEW3D_OT_toolshelf";
264         
265         ot->exec= view3d_toolshelf;
266         ot->poll= ED_operator_view3d_active;
267         
268         /* flags */
269         ot->flag= 0;
270 }
271