merge from trunk 38379
[blender.git] / source / blender / editors / space_buttons / buttons_ops.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_buttons/buttons_ops.c
29  *  \ingroup spbuttons
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_userdef_types.h"
39
40 #include "BLI_fileops.h"
41 #include "BLI_path_util.h"
42 #include "BLI_storage.h"
43 #include "BLI_string.h"
44 #include "BLI_utildefines.h"
45
46 #include "BLF_api.h"
47
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "ED_screen.h"
56 #include "ED_util.h"
57
58 #include "RNA_access.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62
63 #include "buttons_intern.h"     // own include
64
65 /********************** toolbox operator *********************/
66
67 static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
68 {
69         bScreen *sc= CTX_wm_screen(C);
70         SpaceButs *sbuts= CTX_wm_space_buts(C);
71         PointerRNA ptr;
72         uiPopupMenu *pup;
73         uiLayout *layout;
74
75         RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
76
77         pup= uiPupMenuBegin(C, "Align", ICON_NONE);
78         layout= uiPupMenuLayout(pup);
79         uiItemsEnumR(layout, &ptr, "align");
80         uiPupMenuEnd(C, pup);
81
82         return OPERATOR_CANCELLED;
83 }
84
85 void BUTTONS_OT_toolbox(wmOperatorType *ot)
86 {
87         /* identifiers */
88         ot->name= _("Toolbox");
89         ot->description=_("Display button panel toolbox");
90         ot->idname= "BUTTONS_OT_toolbox";
91         
92         /* api callbacks */
93         ot->invoke= toolbox_invoke;
94         ot->poll= ED_operator_buttons_active;
95 }
96
97 /********************** filebrowse operator *********************/
98
99 typedef struct FileBrowseOp {
100         PointerRNA ptr;
101         PropertyRNA *prop;
102 } FileBrowseOp;
103
104 static int file_browse_exec(bContext *C, wmOperator *op)
105 {
106         FileBrowseOp *fbo= op->customdata;
107         ID *id;
108         char *base, *str, path[FILE_MAX];
109         const char *path_prop= RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
110         
111         if (RNA_property_is_set(op->ptr, path_prop)==0 || fbo==NULL)
112                 return OPERATOR_CANCELLED;
113         
114         str= RNA_string_get_alloc(op->ptr, path_prop, NULL, 0);
115
116         /* add slash for directories, important for some properties */
117         if(RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
118                 char name[FILE_MAX];
119                 
120                 id = fbo->ptr.id.data;
121                 base = (id && id->lib)? id->lib->filepath: G.main->name;
122
123                 BLI_strncpy(path, str, FILE_MAX);
124                 BLI_path_abs(path, base);
125                 
126                 if(BLI_is_dir(path)) {
127                         str = MEM_reallocN(str, strlen(str)+2);
128                         BLI_add_slash(str);
129                 }
130                 else
131                         BLI_splitdirstring(str, name);
132         }
133
134         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
135         RNA_property_update(C, &fbo->ptr, fbo->prop);
136         MEM_freeN(str);
137
138
139         /* special, annoying exception, filesel on redo panel [#26618] */
140         {
141                 wmOperator *redo_op= WM_operator_last_redo(C);
142                 if(redo_op) {
143                         if(fbo->ptr.data == redo_op->ptr->data) {
144                                 ED_undo_operator_repeat(C, redo_op);
145                         }
146                 }
147         }
148
149         MEM_freeN(op->customdata);
150
151         return OPERATOR_FINISHED;
152 }
153
154 static int file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
155 {
156         MEM_freeN(op->customdata);
157         op->customdata= NULL;
158
159         return OPERATOR_CANCELLED;
160 }
161
162 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
163 {
164         PointerRNA ptr;
165         PropertyRNA *prop;
166         FileBrowseOp *fbo;
167         char *str;
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);
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                 if(RNA_struct_find_property(op->ptr, "relative_path")) {
207                         if(!RNA_property_is_set(op->ptr, "relative_path")) {
208                                 /* annoying exception!, if were dealign with the user prefs, default relative to be off */
209                                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS && (ptr.data != &U));
210                         }
211                 }
212                 WM_event_add_fileselect(C, op);
213
214                 return OPERATOR_RUNNING_MODAL;
215         }
216 }
217
218 void BUTTONS_OT_file_browse(wmOperatorType *ot)
219 {
220         /* identifiers */
221         ot->name= _("Accept");
222         ot->description=_("Open a file browser, Hold Shift to open the file, Alt to browse containing directory");
223         ot->idname= "BUTTONS_OT_file_browse";
224         
225         /* api callbacks */
226         ot->invoke= file_browse_invoke;
227         ot->exec= file_browse_exec;
228         ot->cancel= file_browse_cancel;
229
230         /* properties */
231         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
232 }
233
234 /* second operator, only difference from BUTTONS_OT_file_browse is WM_FILESEL_DIRECTORY */
235 void BUTTONS_OT_directory_browse(wmOperatorType *ot)
236 {
237         /* identifiers */
238         ot->name= _("Accept");
239         ot->description=_("Open a directory browser, Hold Shift to open the file, Alt to browse containing directory");
240         ot->idname= "BUTTONS_OT_directory_browse";
241
242         /* api callbacks */
243         ot->invoke= file_browse_invoke;
244         ot->exec= file_browse_exec;
245         ot->cancel= file_browse_cancel;
246
247         /* properties */
248         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH);
249 }