svn merge ^/trunk/blender -r45927:HEAD --accept=theirs-full
[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_fileops.h"
39 #include "BLI_path_util.h"
40 #include "BLI_string.h"
41 #include "BLI_utildefines.h"
42
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_main.h"
46 #include "BKE_report.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "ED_screen.h"
52 #include "ED_util.h"
53
54 #include "RNA_access.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58
59 #include "buttons_intern.h"  /* own include */
60
61 /********************** toolbox operator *********************/
62
63 static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
64 {
65         bScreen *sc = CTX_wm_screen(C);
66         SpaceButs *sbuts = CTX_wm_space_buts(C);
67         PointerRNA ptr;
68         uiPopupMenu *pup;
69         uiLayout *layout;
70
71         RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
72
73         pup = uiPupMenuBegin(C, "Align", ICON_NONE);
74         layout = uiPupMenuLayout(pup);
75         uiItemsEnumR(layout, &ptr, "align");
76         uiPupMenuEnd(C, pup);
77
78         return OPERATOR_CANCELLED;
79 }
80
81 void BUTTONS_OT_toolbox(wmOperatorType *ot)
82 {
83         /* identifiers */
84         ot->name = "Toolbox";
85         ot->description = "Display button panel toolbox";
86         ot->idname = "BUTTONS_OT_toolbox";
87         
88         /* api callbacks */
89         ot->invoke = toolbox_invoke;
90         ot->poll = ED_operator_buttons_active;
91 }
92
93 /********************** filebrowse operator *********************/
94
95 typedef struct FileBrowseOp {
96         PointerRNA ptr;
97         PropertyRNA *prop;
98 } FileBrowseOp;
99
100 static int file_browse_exec(bContext *C, wmOperator *op)
101 {
102         FileBrowseOp *fbo = op->customdata;
103         ID *id;
104         char *str, path[FILE_MAX];
105         const char *path_prop = RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
106         
107         if (RNA_struct_property_is_set(op->ptr, path_prop) == 0 || fbo == NULL)
108                 return OPERATOR_CANCELLED;
109         
110         str = RNA_string_get_alloc(op->ptr, path_prop, NULL, 0);
111
112         /* add slash for directories, important for some properties */
113         if (RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
114                 char name[FILE_MAX];
115                 
116                 id = fbo->ptr.id.data;
117
118                 BLI_strncpy(path, str, FILE_MAX);
119                 BLI_path_abs(path, id ? ID_BLEND_PATH(G.main, id) : G.main->name);
120                 
121                 if (BLI_is_dir(path)) {
122                         str = MEM_reallocN(str, strlen(str) + 2);
123                         BLI_add_slash(str);
124                 }
125                 else
126                         BLI_splitdirstring(str, name);
127         }
128
129         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
130         RNA_property_update(C, &fbo->ptr, fbo->prop);
131         MEM_freeN(str);
132
133
134         /* special, annoying exception, filesel on redo panel [#26618] */
135         {
136                 wmOperator *redo_op = WM_operator_last_redo(C);
137                 if (redo_op) {
138                         if (fbo->ptr.data == redo_op->ptr->data) {
139                                 ED_undo_operator_repeat(C, redo_op);
140                         }
141                 }
142         }
143
144         MEM_freeN(op->customdata);
145
146         return OPERATOR_FINISHED;
147 }
148
149 static int file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
150 {
151         MEM_freeN(op->customdata);
152         op->customdata = NULL;
153
154         return OPERATOR_CANCELLED;
155 }
156
157 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
158 {
159         PointerRNA ptr;
160         PropertyRNA *prop;
161         FileBrowseOp *fbo;
162         char *str;
163
164         if (CTX_wm_space_file(C)) {
165                 BKE_report(op->reports, RPT_ERROR, "Can't activate a file selector, one already open");
166                 return OPERATOR_CANCELLED;
167         }
168
169         uiFileBrowseContextProperty(C, &ptr, &prop);
170
171         if (!prop)
172                 return OPERATOR_CANCELLED;
173
174         str = RNA_property_string_get_alloc(&ptr, prop, NULL, 0, NULL);
175
176         /* useful yet irritating feature, Shift+Click to open the file
177          * Alt+Click to browse a folder in the OS's browser */
178         if (event->shift || event->alt) {
179                 PointerRNA props_ptr;
180
181                 if (event->alt) {
182                         char *lslash = BLI_last_slash(str);
183                         if (lslash)
184                                 *lslash = '\0';
185                 }
186
187
188                 WM_operator_properties_create(&props_ptr, "WM_OT_path_open");
189                 RNA_string_set(&props_ptr, "filepath", str);
190                 WM_operator_name_call(C, "WM_OT_path_open", WM_OP_EXEC_DEFAULT, &props_ptr);
191                 WM_operator_properties_free(&props_ptr);
192
193                 MEM_freeN(str);
194                 return OPERATOR_CANCELLED;
195         }
196         else {
197                 const char *path_prop = RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
198                 fbo = MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
199                 fbo->ptr = ptr;
200                 fbo->prop = prop;
201                 op->customdata = fbo;
202
203                 RNA_string_set(op->ptr, path_prop, str);
204                 MEM_freeN(str);
205
206                 /* normally ED_fileselect_get_params would handle this but we need to because of stupid
207                  * user-prefs exception - campbell */
208                 if (RNA_struct_find_property(op->ptr, "relative_path")) {
209                         if (!RNA_struct_property_is_set(op->ptr, "relative_path")) {
210                                 /* annoying exception!, if were dealign with the user prefs, default relative to be off */
211                                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS && (ptr.data != &U));
212                         }
213                 }
214                 WM_event_add_fileselect(C, op);
215
216                 return OPERATOR_RUNNING_MODAL;
217         }
218 }
219
220 void BUTTONS_OT_file_browse(wmOperatorType *ot)
221 {
222         /* identifiers */
223         ot->name = "Accept";
224         ot->description = "Open a file browser, Hold Shift to open the file, Alt to browse containing directory";
225         ot->idname = "BUTTONS_OT_file_browse";
226         
227         /* api callbacks */
228         ot->invoke = file_browse_invoke;
229         ot->exec = file_browse_exec;
230         ot->cancel = file_browse_cancel;
231
232         /* properties */
233         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
234 }
235
236 /* second operator, only difference from BUTTONS_OT_file_browse is WM_FILESEL_DIRECTORY */
237 void BUTTONS_OT_directory_browse(wmOperatorType *ot)
238 {
239         /* identifiers */
240         ot->name = "Accept";
241         ot->description = "Open a directory browser, Hold Shift to open the file, Alt to browse containing directory";
242         ot->idname = "BUTTONS_OT_directory_browse";
243
244         /* api callbacks */
245         ot->invoke = file_browse_invoke;
246         ot->exec = file_browse_exec;
247         ot->cancel = file_browse_cancel;
248
249         /* properties */
250         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
251 }