Merge branch 'master' into temp_remove_particles
[blender.git] / source / blender / editors / space_buttons / buttons_ops.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  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_buttons/buttons_ops.c
27  *  \ingroup spbuttons
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_userdef_types.h"
37
38 #include "BLI_utildefines.h"
39 #include "BLI_fileops.h"
40 #include "BLI_path_util.h"
41 #include "BLI_string.h"
42
43 #include "BLT_translation.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48 #include "BKE_report.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "ED_screen.h"
54 #include "ED_util.h"
55
56 #include "RNA_access.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60
61 #include "buttons_intern.h"  /* own include */
62
63 /********************** toolbox operator *********************/
64
65 static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
66 {
67         bScreen *sc = CTX_wm_screen(C);
68         SpaceButs *sbuts = CTX_wm_space_buts(C);
69         PointerRNA ptr;
70         uiPopupMenu *pup;
71         uiLayout *layout;
72
73         RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
74
75         pup = UI_popup_menu_begin(C, IFACE_("Align"), ICON_NONE);
76         layout = UI_popup_menu_layout(pup);
77         uiItemsEnumR(layout, &ptr, "align");
78         UI_popup_menu_end(C, pup);
79
80         return OPERATOR_INTERFACE;
81 }
82
83 void BUTTONS_OT_toolbox(wmOperatorType *ot)
84 {
85         /* identifiers */
86         ot->name = "Toolbox";
87         ot->description = "Display button panel toolbox";
88         ot->idname = "BUTTONS_OT_toolbox";
89         
90         /* api callbacks */
91         ot->invoke = toolbox_invoke;
92         ot->poll = ED_operator_buttons_active;
93 }
94
95 /********************** filebrowse operator *********************/
96
97 typedef struct FileBrowseOp {
98         PointerRNA ptr;
99         PropertyRNA *prop;
100         bool is_undo;
101 } FileBrowseOp;
102
103 static int file_browse_exec(bContext *C, wmOperator *op)
104 {
105         FileBrowseOp *fbo = op->customdata;
106         ID *id;
107         char *str, path[FILE_MAX];
108         const char *path_prop = RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
109         
110         if (RNA_struct_property_is_set(op->ptr, path_prop) == 0 || fbo == NULL)
111                 return OPERATOR_CANCELLED;
112         
113         str = RNA_string_get_alloc(op->ptr, path_prop, NULL, 0);
114
115         /* add slash for directories, important for some properties */
116         if (RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
117                 const bool is_relative = RNA_boolean_get(op->ptr, "relative_path");
118                 id = fbo->ptr.id.data;
119
120                 BLI_strncpy(path, str, FILE_MAX);
121                 BLI_path_abs(path, id ? ID_BLEND_PATH(G.main, id) : G.main->name);
122                 
123                 if (BLI_is_dir(path)) {
124                         /* do this first so '//' isnt converted to '//\' on windows */
125                         BLI_add_slash(path);
126                         if (is_relative) {
127                                 BLI_strncpy(path, str, FILE_MAX);
128                                 BLI_path_rel(path, G.main->name);
129                                 str = MEM_reallocN(str, strlen(path) + 2);
130                                 BLI_strncpy(str, path, FILE_MAX);
131                         }
132                         else {
133                                 str = MEM_reallocN(str, strlen(str) + 2);
134                         }
135                 }
136                 else {
137                         char * const lslash = (char *)BLI_last_slash(str);
138                         if (lslash) lslash[1] = '\0';
139                 }
140         }
141
142         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
143         RNA_property_update(C, &fbo->ptr, fbo->prop);
144         MEM_freeN(str);
145
146         if (fbo->is_undo) {
147                 const char *undostr = RNA_property_identifier(fbo->prop);
148                 ED_undo_push(C, undostr);
149         }
150
151         /* special, annoying exception, filesel on redo panel [#26618] */
152         {
153                 wmOperator *redo_op = WM_operator_last_redo(C);
154                 if (redo_op) {
155                         if (fbo->ptr.data == redo_op->ptr->data) {
156                                 ED_undo_operator_repeat(C, redo_op);
157                         }
158                 }
159         }
160
161         MEM_freeN(op->customdata);
162
163         return OPERATOR_FINISHED;
164 }
165
166 static void file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
167 {
168         MEM_freeN(op->customdata);
169         op->customdata = NULL;
170 }
171
172 static int file_browse_invoke(bContext *C, wmOperator *op, const wmEvent *event)
173 {
174         PointerRNA ptr;
175         PropertyRNA *prop;
176         bool is_undo;
177         FileBrowseOp *fbo;
178         char *str;
179
180         if (CTX_wm_space_file(C)) {
181                 BKE_report(op->reports, RPT_ERROR, "Cannot activate a file selector, one already open");
182                 return OPERATOR_CANCELLED;
183         }
184
185         UI_context_active_but_prop_get_filebrowser(C, &ptr, &prop, &is_undo);
186
187         if (!prop)
188                 return OPERATOR_CANCELLED;
189
190         str = RNA_property_string_get_alloc(&ptr, prop, NULL, 0, NULL);
191
192         /* useful yet irritating feature, Shift+Click to open the file
193          * Alt+Click to browse a folder in the OS's browser */
194         if (event->shift || event->alt) {
195                 wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true);
196                 PointerRNA props_ptr;
197
198                 if (event->alt) {
199                         char *lslash = (char *)BLI_last_slash(str);
200                         if (lslash)
201                                 *lslash = '\0';
202                 }
203
204
205                 WM_operator_properties_create_ptr(&props_ptr, ot);
206                 RNA_string_set(&props_ptr, "filepath", str);
207                 WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &props_ptr);
208                 WM_operator_properties_free(&props_ptr);
209
210                 MEM_freeN(str);
211                 return OPERATOR_CANCELLED;
212         }
213         else {
214                 PropertyRNA *prop_relpath;
215                 const char *path_prop = RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
216                 fbo = MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
217                 fbo->ptr = ptr;
218                 fbo->prop = prop;
219                 fbo->is_undo = is_undo;
220                 op->customdata = fbo;
221
222                 /* normally ED_fileselect_get_params would handle this but we need to because of stupid
223                  * user-prefs exception - campbell */
224                 if ((prop_relpath = RNA_struct_find_property(op->ptr, "relative_path"))) {
225                         if (!RNA_property_is_set(op->ptr, prop_relpath)) {
226                                 bool is_relative = (U.flag & USER_RELPATHS) != 0;
227
228                                 /* while we want to follow the defaults,
229                                  * we better not switch existing paths relative/absolute state. */
230                                 if (str[0]) {
231                                         is_relative = BLI_path_is_rel(str);
232                                 }
233
234                                 if (UNLIKELY(ptr.data == &U)) {
235                                         is_relative = false;
236                                 }
237
238                                 /* annoying exception!, if were dealing with the user prefs, default relative to be off */
239                                 RNA_property_boolean_set(op->ptr, prop_relpath, is_relative);
240                         }
241                 }
242
243                 RNA_string_set(op->ptr, path_prop, str);
244                 MEM_freeN(str);
245
246                 WM_event_add_fileselect(C, op);
247
248                 return OPERATOR_RUNNING_MODAL;
249         }
250 }
251
252 void BUTTONS_OT_file_browse(wmOperatorType *ot)
253 {
254         /* identifiers */
255         ot->name = "Accept";
256         ot->description = "Open a file browser, Hold Shift to open the file, Alt to browse containing directory";
257         ot->idname = "BUTTONS_OT_file_browse";
258         
259         /* api callbacks */
260         ot->invoke = file_browse_invoke;
261         ot->exec = file_browse_exec;
262         ot->cancel = file_browse_cancel;
263
264         /* conditional undo based on button flag */
265         ot->flag = 0;
266
267         /* properties */
268         WM_operator_properties_filesel(
269                 ot, 0, FILE_SPECIAL, FILE_OPENFILE,
270                 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
271 }
272
273 /* second operator, only difference from BUTTONS_OT_file_browse is WM_FILESEL_DIRECTORY */
274 void BUTTONS_OT_directory_browse(wmOperatorType *ot)
275 {
276         /* identifiers */
277         ot->name = "Accept";
278         ot->description = "Open a directory browser, Hold Shift to open the file, Alt to browse containing directory";
279         ot->idname = "BUTTONS_OT_directory_browse";
280
281         /* api callbacks */
282         ot->invoke = file_browse_invoke;
283         ot->exec = file_browse_exec;
284         ot->cancel = file_browse_cancel;
285
286         /* conditional undo based on button flag */
287         ot->flag = 0;
288
289         /* properties */
290         WM_operator_properties_filesel(
291                 ot, 0, FILE_SPECIAL, FILE_OPENFILE,
292                 WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
293 }