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