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