2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_view3d/view3d_toolbar.c
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
40 #include "MEM_guardedalloc.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_editVert.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
49 #include "BLF_translation.h"
51 #include "BKE_context.h"
52 #include "BKE_idprop.h"
53 #include "BKE_global.h"
54 #include "BKE_screen.h"
60 #include "RNA_access.h"
62 #include "ED_screen.h"
65 #include "UI_interface.h"
66 #include "UI_resources.h"
68 #include "view3d_intern.h" // own include
71 /* ******************* view3d space & buttons ************** */
73 static void view3d_panel_operator_redo_buts(const bContext *C, Panel *pa, wmOperator *op)
75 uiLayoutOperatorButs(C, pa->layout, op, NULL, 'V', 0);
78 static void view3d_panel_operator_redo_header(const bContext *C, Panel *pa)
80 wmOperator *op= WM_operator_last_redo(C);
82 if(op) BLI_strncpy(pa->drawname, op->type->name, sizeof(pa->drawname));
83 else BLI_strncpy(pa->drawname, IFACE_("Operator"), sizeof(pa->drawname));
86 static void view3d_panel_operator_redo_operator(const bContext *C, Panel *pa, wmOperator *op)
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);
95 view3d_panel_operator_redo_buts(C, pa, op);
99 /* TODO de-duplicate redo panel functions - campbell */
100 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
102 wmOperator *op= WM_operator_last_redo(C);
107 if(WM_operator_poll((bContext*)C, op->type) == 0)
110 block= uiLayoutGetBlock(pa->layout);
112 if (!WM_operator_check_ui_enabled(C, op->type->name))
113 uiLayoutSetEnabled(pa->layout, 0);
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);
118 view3d_panel_operator_redo_operator(C, pa, op);
121 /* ******************* */
123 typedef struct CustomTool {
124 struct CustomTool *next, *prev;
125 char opname[OP_MAX_TYPENAME];
126 char context[OP_MAX_TYPENAME];
129 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
131 wmOperatorType *ot= arg2;
134 CustomTool *ct= MEM_callocN(sizeof(CustomTool), "CustomTool");
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);
143 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
145 GHashIterator *iter= WM_operatortype_iter();
147 for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
148 wmOperatorType *ot= BLI_ghashIterator_getValue(iter);
150 if(BLI_strcasestr(ot->name, str)) {
151 if(WM_operator_poll((bContext*)C, ot)) {
153 if(0==uiSearchItemAdd(items, ot->name, ot, 0))
158 BLI_ghashIterator_free(iter);
162 /* ID Search browse menu, open */
163 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
165 static char search[OP_MAX_TYPENAME];
167 wmWindow *win= CTX_wm_window(C);
171 /* clear initial search string, then all items show */
174 block= uiBeginBlock(C, ar, "_popup", UI_EMBOSS);
175 uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_RET_1);
177 /* fake button, it holds space for search items */
178 uiDefBut(block, LABEL, 0, "", 10, 15, 150, uiSearchBoxhHeight(), NULL, 0, 0, 0, 0, NULL);
180 but= uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, 150, 19, 0, 0, "");
181 uiButSetSearchFunc(but, operator_search_cb, arg_listbase, operator_call_cb, NULL);
183 uiBoundsBlock(block, 6);
184 uiBlockSetDirection(block, UI_DOWN);
185 uiEndBlock(C, block);
187 event= *(win->eventstate); /* XXX huh huh? make api call */
188 event.type= EVT_BUT_OPEN;
190 event.customdata= but;
191 event.customdatafree= FALSE;
192 wm_event_add(win, &event);
198 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
200 SpaceLink *sl= CTX_wm_space_data(C);
203 const char *context= CTX_data_mode_string(C);
206 st= BKE_spacetype_from_id(sl->spacetype);
208 if(st && st->toolshelf.first) {
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);
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");
223 void view3d_toolshelf_register(ARegionType *art)
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);
234 void view3d_tool_props_register(ARegionType *art)
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);
246 /* ********** operator to open/close toolshelf region */
248 static int view3d_toolshelf(bContext *C, wmOperator *UNUSED(op))
250 ScrArea *sa= CTX_wm_area(C);
251 ARegion *ar= view3d_has_tools_region(sa);
254 ED_region_toggle_hidden(C, ar);
256 return OPERATOR_FINISHED;
259 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
261 ot->name= "Tool Shelf";
262 ot->description= "Toggles tool shelf display";
263 ot->idname= "VIEW3D_OT_toolshelf";
265 ot->exec= view3d_toolshelf;
266 ot->poll= ED_operator_view3d_active;