Merge branch 'master' into blender2.8
[blender.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_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_ghash.h"
45
46 #include "BLT_translation.h"
47
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "RNA_access.h"
56
57 #include "ED_screen.h"
58 #include "ED_undo.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62
63 #include "view3d_intern.h"  /* own include */
64
65 /* ******************* */
66
67 typedef struct CustomTool {
68         struct CustomTool *next, *prev;
69         char opname[OP_MAX_TYPENAME];
70         char context[OP_MAX_TYPENAME];
71 } CustomTool;
72
73 static void operator_call_cb(struct bContext *C, void *arg_listbase, void *arg2)
74 {
75         wmOperatorType *ot = arg2;
76         
77         if (ot) {
78                 CustomTool *ct = MEM_callocN(sizeof(CustomTool), "CustomTool");
79                 
80                 BLI_addtail(arg_listbase, ct);
81                 BLI_strncpy(ct->opname, ot->idname, OP_MAX_TYPENAME);
82                 BLI_strncpy(ct->context, CTX_data_mode_string(C), OP_MAX_TYPENAME);
83         }
84                 
85 }
86
87 static void operator_search_cb(const struct bContext *C, void *UNUSED(arg), const char *str, uiSearchItems *items)
88 {
89         GHashIterator iter;
90
91         for (WM_operatortype_iter(&iter); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) {
92                 wmOperatorType *ot = BLI_ghashIterator_getValue(&iter);
93
94                 if (BLI_strcasestr(ot->name, str)) {
95                         if (WM_operator_poll((bContext *)C, ot)) {
96                                 
97                                 if (false == UI_search_item_add(items, ot->name, ot, 0))
98                                         break;
99                         }
100                 }
101         }
102 }
103
104
105 /* ID Search browse menu, open */
106 static uiBlock *tool_search_menu(bContext *C, ARegion *ar, void *arg_listbase)
107 {
108         static char search[OP_MAX_TYPENAME];
109         wmEvent event;
110         wmWindow *win = CTX_wm_window(C);
111         uiBlock *block;
112         uiBut *but;
113         
114         /* clear initial search string, then all items show */
115         search[0] = 0;
116         
117         block = UI_block_begin(C, ar, "_popup", UI_EMBOSS);
118         UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_SEARCH_MENU);
119         
120         /* fake button, it holds space for search items */
121         uiDefBut(block, UI_BTYPE_LABEL, 0, "", 10, 15, UI_searchbox_size_x(), UI_searchbox_size_y(), NULL, 0, 0, 0, 0, NULL);
122         
123         but = uiDefSearchBut(block, search, 0, ICON_VIEWZOOM, sizeof(search), 10, 0, 150, 19, 0, 0, "");
124         UI_but_func_search_set(but, NULL, operator_search_cb, arg_listbase, operator_call_cb, NULL);
125         
126         UI_block_bounds_set_normal(block, 6);
127         UI_block_direction_set(block, UI_DIR_DOWN);
128         UI_block_end(C, block);
129         
130         wm_event_init_from_window(win, &event);
131         event.type = EVT_BUT_OPEN;
132         event.val = KM_PRESS;
133         event.customdata = but;
134         event.customdatafree = false;
135         wm_event_add(win, &event);
136         
137         return block;
138 }
139
140
141 static void view3d_panel_tool_shelf(const bContext *C, Panel *pa)
142 {
143         SpaceLink *sl = CTX_wm_space_data(C);
144         SpaceType *st = NULL;
145         uiLayout *col;
146         const char *context = CTX_data_mode_string(C);
147         
148         if (sl)
149                 st = BKE_spacetype_from_id(sl->spacetype);
150         
151         if (st && st->toolshelf.first) {
152                 CustomTool *ct;
153                 
154                 for (ct = st->toolshelf.first; ct; ct = ct->next) {
155                         if (STREQLEN(context, ct->context, OP_MAX_TYPENAME)) {
156                                 col = uiLayoutColumn(pa->layout, true);
157                                 uiItemFullO(col, ct->opname, NULL, ICON_NONE, NULL, WM_OP_INVOKE_REGION_WIN, 0, NULL);
158                         }
159                 }
160         }
161         col = uiLayoutColumn(pa->layout, true);
162         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");
163 }
164
165
166 void view3d_toolshelf_register(ARegionType *art)
167 {
168         PanelType *pt;
169
170         pt = MEM_callocN(sizeof(PanelType), "spacetype view3d panel tools");
171         strcpy(pt->idname, "VIEW3D_PT_tool_shelf");
172         strcpy(pt->label, N_("Tool Shelf"));
173         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
174         pt->draw = view3d_panel_tool_shelf;
175         BLI_addtail(&art->paneltypes, pt);
176 }
177
178 /* ********** operator to open/close toolshelf region */
179
180 static int view3d_toolshelf_toggle_exec(bContext *C, wmOperator *UNUSED(op))
181 {
182         ScrArea *sa = CTX_wm_area(C);
183         ARegion *ar = view3d_has_tools_region(sa);
184         
185         if (ar)
186                 ED_region_toggle_hidden(C, ar);
187
188         return OPERATOR_FINISHED;
189 }
190
191 void VIEW3D_OT_toolshelf(wmOperatorType *ot)
192 {
193         ot->name = "Tool Shelf";
194         ot->description = "Toggles tool shelf display";
195         ot->idname = "VIEW3D_OT_toolshelf";
196         
197         ot->exec = view3d_toolshelf_toggle_exec;
198         ot->poll = ED_operator_view3d_active;
199         
200         /* flags */
201         ot->flag = 0;
202 }
203