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 ************** */
68 /* XXX DUPLICATE CODE */
69 static void redo_cb(bContext *C, void *arg_op, void *UNUSED(arg2))
71 wmOperator *lastop= arg_op;
74 ARegion *ar= CTX_wm_region(C);
75 ARegion *ar1= BKE_area_find_region_type(CTX_wm_area(C), RGN_TYPE_WINDOW);
79 CTX_wm_region_set(C, ar1);
82 printf("operator redo %s\n", lastop->type->name);
84 ED_undo_pop_op(C, lastop);
85 retval= WM_operator_repeat(C, lastop);
87 if((retval & OPERATOR_FINISHED)==0) {
89 printf("operator redo failed %s\n", lastop->type->name);
94 CTX_wm_region_set(C, ar);
98 static wmOperator *view3d_last_operator(const bContext *C)
100 wmWindowManager *wm= CTX_wm_manager(C);
103 /* only for operators that are registered and did an undo push */
104 for(op= wm->operators.last; op; op= op->prev)
105 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
111 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
113 wmWindowManager *wm= CTX_wm_manager(C);
116 if(!op->properties) {
117 IDPropertyTemplate val = {0};
118 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
121 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
123 op->layout= pa->layout;
124 op->type->ui((bContext*)C, op);
128 uiDefAutoButsRNA(pa->layout, &ptr, 1);
131 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
133 wmOperator *op= view3d_last_operator(C);
135 if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
136 else BLI_strncpy(pa->drawname, "Operator", sizeof(pa->drawname));
139 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
141 if(op->type->flag & OPTYPE_MACRO) {
142 for(op= op->macro.first; op; op= op->next) {
143 uiItemL(pa->layout, op->type->name, 0);
144 view3d_panel_operator_redo_operator(C, pa, op);
148 view3d_panel_operator_redo_buts(C, pa, op);
152 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
154 wmOperator *op= view3d_last_operator(C);
159 if(WM_operator_poll((bContext*)C, op->type) == 0)
162 block= uiLayoutGetBlock(pa->layout);
164 uiBlockSetFunc(block, redo_cb, op, NULL);
166 view3d_panel_operator_redo_operator(C, pa, op);
169 /* ******************* */
171 typedef struct CustomTool {
172 struct CustomTool *next, *prev;
173 char opname[OP_MAX_TYPENAME];
174 char context[OP_MAX_TYPENAME];
177 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
179 wmOperatorType *ot= arg2;
182 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
184 BLI_addtail(arg_listbase, ct);
185 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
186 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
191 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
193 wmOperatorType *ot = WM_operatortype_first();
195 for(; ot; ot= ot->next) {
197 if(BLI_strcasestr(ot->name, str)) {
198 if(WM_operator_poll((bContext*)C, ot)) {
200 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
208 /* ID Search browse menu, open */
209 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
211 static char search[OP_MAX_TYPENAME];
213 wmWindow *win= CTX_wm_window(C);
217 /* clear initial search string, then all items show */
220 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
221 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
223 /* fake button, it holds space for search items */
224 uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
226 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, OP_MAX_TYPENAME, 10, 0, 150, 19, 0, 0, "");
227 uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
229 uiBoundsBlock(block, 6);
230 uiBlockSetDirection(block, UI_DOWN);
231 uiEndBlock(C, block);
233 event= *(win->eventstate); /* XXX huh huh? make api call */
234 event.type= EVT_BUT_OPEN;
236 event.customdata= but;
237 event.customdatafree= FALSE;
238 wm_event_add(win, &event);
244 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
246 SpaceLink *sl= CTX_wm_space_data(C);
249 const char *context= CTX_data_mode_string(C);
252 st= BKE_spacetype_from_id(sl->spacetype);
254 if(st && st->toolshelf.first) {
257 for(ct= st->toolshelf.first; ct; ct= ct->next) {
258 if(0==strncmp(context, ct->context, OP_MAX_TYPENAME)) {
259 col= uiLayoutColumn(pa->layout, 1);
260 uiItemFullO(col, ct->opname, NULL, 0, NULL, WM_OP_INVOKE_REGION_WIN, 0);
264 col= uiLayoutColumn(pa->layout, 1);
265 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");
269 void view3d_toolshelf_register(ARegionType *art)
273 pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
274 strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
275 strcpy(pt->label, "Tool Shelf");
276 pt->draw= view3d_panel_tool_shelf;
277 BLI_addtail(&art->paneltypes, pt);
280 void view3d_tool_props_register(ARegionType *art)
284 pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel last operator");
285 strcpy(pt->idname, "VIEW3D_PT_last_operator");
286 strcpy(pt->label, "Operator");
287 pt->draw_header= view3d_panel_operator_redo_header;
288 pt->draw= view3d_panel_operator_redo;
289 BLI_addtail(&art->paneltypes, pt);
292 /* ********** operator to open/close toolshelf region */
294 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
296 ScrArea *sa= CTX_wm_area(C);
297 ARegion *ar= view3d_has_tools_region(sa);
300 ED_region_toggle_hidden(C, ar);
302 return OPERATOR_FINISHED;
305 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
307 ot->name= "Tool Shelf";
308 ot->description= "Toggles tool shelf display";
309 ot->idname= "VIEW3D_OT_toolshelf";
311 ot->exec= view3d_toolshelf;
312 ot->poll= ED_operator_view3d_active;