remove $Id: tags after discussion on the mailign list: http://markmail.org/message...
[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 *base, *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                 base = (id && id->lib)? id->lib->filepath: G.main->name;
117
118                 BLI_strncpy(path, str, FILE_MAX);
119                 BLI_path_abs(path, base);
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         uiFileBrowseContextProperty(C, &ptr, &prop);
165
166         if(!prop)
167                 return OPERATOR_CANCELLED;
168
169         str= RNA_property_string_get_alloc(&ptr, prop, NULL, 0, NULL);
170
171         /* useful yet irritating feature, Shift+Click to open the file
172          * Alt+Click to browse a folder in the OS's browser */
173         if(event->shift || event->alt) {
174                 PointerRNA props_ptr;
175
176                 if(event->alt) {
177                         char *lslash= BLI_last_slash(str);
178                         if(lslash)
179                                 *lslash= '\0';
180                 }
181
182
183                 WM_operator_properties_create(&props_ptr, "WM_OT_path_open");
184                 RNA_string_set(&props_ptr, "filepath", str);
185                 WM_operator_name_call(C, "WM_OT_path_open", WM_OP_EXEC_DEFAULT, &props_ptr);
186                 WM_operator_properties_free(&props_ptr);
187
188                 MEM_freeN(str);
189                 return OPERATOR_CANCELLED;
190         }
191         else {
192                 const char *path_prop= RNA_struct_find_property(op->ptr, "directory") ? "directory" : "filepath";
193                 fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
194                 fbo->ptr= ptr;
195                 fbo->prop= prop;
196                 op->customdata= fbo;
197
198                 RNA_string_set(op->ptr, path_prop, str);
199                 MEM_freeN(str);
200
201                 /* normally ED_fileselect_get_params would handle this but we need to because of stupid
202                  * user-prefs exception - campbell */
203                 if(RNA_struct_find_property(op->ptr, "relative_path")) {
204                         if(!RNA_property_is_set(op->ptr, "relative_path")) {
205                                 /* annoying exception!, if were dealign with the user prefs, default relative to be off */
206                                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS && (ptr.data != &U));
207                         }
208                 }
209                 WM_event_add_fileselect(C, op);
210
211                 return OPERATOR_RUNNING_MODAL;
212         }
213 }
214
215 void BUTTONS_OT_file_browse(wmOperatorType *ot)
216 {
217         /* identifiers */
218         ot->name= "Accept";
219         ot->description="Open a file browser, Hold Shift to open the file, Alt to browse containing directory";
220         ot->idname= "BUTTONS_OT_file_browse";
221         
222         /* api callbacks */
223         ot->invoke= file_browse_invoke;
224         ot->exec= file_browse_exec;
225         ot->cancel= file_browse_cancel;
226
227         /* properties */
228         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
229 }
230
231 /* second operator, only difference from BUTTONS_OT_file_browse is WM_FILESEL_DIRECTORY */
232 void BUTTONS_OT_directory_browse(wmOperatorType *ot)
233 {
234         /* identifiers */
235         ot->name= "Accept";
236         ot->description="Open a directory browser, Hold Shift to open the file, Alt to browse containing directory";
237         ot->idname= "BUTTONS_OT_directory_browse";
238
239         /* api callbacks */
240         ot->invoke= file_browse_invoke;
241         ot->exec= file_browse_exec;
242         ot->cancel= file_browse_cancel;
243
244         /* properties */
245         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH);
246 }