4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2009 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
37 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_editVert.h"
44 #include "BKE_context.h"
45 #include "BKE_idprop.h"
46 #include "BKE_global.h"
47 #include "BKE_screen.h"
53 #include "RNA_access.h"
55 #include "ED_screen.h"
58 #include "UI_interface.h"
59 #include "UI_resources.h"
61 #include "view3d_intern.h" // own include
64 /* ******************* view3d space & buttons ************** */
66 static wmOperator *view3d_last_operator(const bContext *C)
68 wmWindowManager *wm= CTX_wm_manager(C);
71 /* only for operators that are registered and did an undo push */
72 for(op= wm->operators.last; op; op= op->prev)
73 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
79 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
81 wmWindowManager *wm= CTX_wm_manager(C);
85 IDPropertyTemplate val = {0};
86 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
89 /* poll() on this operator may still fail, at the moment there is no nice feedback when this happens
90 * just fails silently */
91 if(!WM_operator_repeat_check(C, op)) {
92 uiBlockSetButLock(uiLayoutGetBlock(pa->layout), TRUE, "Operator cannot redo");
93 uiItemL(pa->layout, "* Redo Unsupported *", 0); // XXX, could give some nicer feedback or not show redo panel at all?
96 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
98 op->layout= pa->layout;
99 op->type->ui((bContext*)C, op);
103 uiDefAutoButsRNA(pa->layout, &ptr, 1);
106 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
108 wmOperator *op= view3d_last_operator(C);
110 if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
111 else BLI_strncpy(pa->drawname, "Operator", sizeof(pa->drawname));
114 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
116 if(op->type->flag & OPTYPE_MACRO) {
117 for(op= op->macro.first; op; op= op->next) {
118 uiItemL(pa->layout, op->type->name, 0);
119 view3d_panel_operator_redo_operator(C, pa, op);
123 view3d_panel_operator_redo_buts(C, pa, op);
127 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
129 wmOperator *op= view3d_last_operator(C);
134 if(WM_operator_poll((bContext*)C, op->type) == 0)
137 block= uiLayoutGetBlock(pa->layout);
139 uiBlockSetFunc(block, ED_undo_operator_repeat_cb, op, NULL);
141 view3d_panel_operator_redo_operator(C, pa, op);
144 /* ******************* */
146 typedef struct CustomTool {
147 struct CustomTool *next, *prev;
148 char opname[OP_MAX_TYPENAME];
149 char context[OP_MAX_TYPENAME];
152 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
154 wmOperatorType *ot= arg2;
157 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
159 BLI_addtail(arg_listbase, ct);
160 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
161 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
166 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
168 wmOperatorType *ot = WM_operatortype_first();
170 for(; ot; ot= ot->next) {
172 if(BLI_strcasestr(ot->name, str)) {
173 if(WM_operator_poll((bContext*)C, ot)) {
175 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
183 /* ID Search browse menu, open */
184 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
186 static char search[OP_MAX_TYPENAME];
188 wmWindow *win= CTX_wm_window(C);
192 /* clear initial search string, then all items show */
195 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
196 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
198 /* fake button, it holds space for search items */
199 uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
201 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
202 uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
204 uiBoundsBlock(block, 6);
205 uiBlockSetDirection(block, UI_DOWN);
206 uiEndBlock(C, block);
208 event= *(win->eventstate); /* XXX huh huh? make api call */
209 event.type= EVT_BUT_OPEN;
211 event.customdata= but;
212 event.customdatafree= FALSE;
213 wm_event_add(win, &event);
219 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
221 SpaceLink *sl= CTX_wm_space_data(C);
224 const char *context= CTX_data_mode_string(C);
227 st= BKE_spacetype_from_id(sl->spacetype);
229 if(st && st->toolshelf.first) {
232 for(ct= st->toolshelf.first; ct; ct= ct->next) {
233 if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
234 col= uiLayoutColumn(pa->layout, 1);
235 uiItemFullO(col, ct->opname, NULL, 0, NULL, WM_OP_INVOKE_REGION_WIN, 0);
239 col= uiLayoutColumn(pa->layout, 1);
240 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");
244 void view3d_toolshelf_register(ARegionType *art)
248 pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
249 strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
250 strcpy(pt->label, "Tool Shelf");
251 pt->draw= view3d_panel_tool_shelf;
252 BLI_addtail(&art->paneltypes, pt);
255 void view3d_tool_props_register(ARegionType *art)
259 pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
260 strcpy(pt->idname, "VIEW3D_PT_last_operator");
261 strcpy(pt->label, "Operator");
262 pt->draw_header= view3d_panel_operator_redo_header;
263 pt->draw= view3d_panel_operator_redo;
264 BLI_addtail(&art->paneltypes, pt);
267 /* ********** operator to open/close toolshelf region */
269 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
271 ScrArea *sa= CTX_wm_area(C);
272 ARegion *ar= view3d_has_tools_region(sa);
275 ED_region_toggle_hidden(C, ar);
277 return OPERATOR_FINISHED;
280 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
282 ot->name= "Tool Shelf";
283 ot->description= "Toggles tool shelf display";
284 ot->idname= "VIEW3D_OT_toolshelf";
286 ot->exec= view3d_toolshelf;
287 ot->poll= ED_operator_view3d_active;