9190d5aff546710d2155fce0b6b91df9084eb9b7
[blender-staging.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 "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_main.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "ED_screen.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         
107         if (RNA_property_is_set(op->ptr, "filepath")==0 || fbo==NULL)
108                 return OPERATOR_CANCELLED;
109         
110         str= RNA_string_get_alloc(op->ptr, "filepath", 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                 base = (id && id->lib)? id->lib->filepath: G.main->name;
118
119                 BLI_strncpy(path, str, FILE_MAX);
120                 BLI_path_abs(path, base);
121                 
122                 if(BLI_is_dir(path)) {
123                         str = MEM_reallocN(str, strlen(str)+2);
124                         BLI_add_slash(str);
125                 }
126                 else
127                         BLI_splitdirstring(str, name);
128         }
129
130         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
131         RNA_property_update(C, &fbo->ptr, fbo->prop);
132         MEM_freeN(str);
133
134         MEM_freeN(op->customdata);
135         return OPERATOR_FINISHED;
136 }
137
138 static int file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
139 {
140         MEM_freeN(op->customdata);
141         op->customdata= NULL;
142
143         return OPERATOR_CANCELLED;
144 }
145
146 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
147 {
148         PointerRNA ptr;
149         PropertyRNA *prop;
150         FileBrowseOp *fbo;
151         char *str;
152
153         uiFileBrowseContextProperty(C, &ptr, &prop);
154
155         if(!prop)
156                 return OPERATOR_CANCELLED;
157
158         str= RNA_property_string_get_alloc(&ptr, prop, NULL, 0);
159
160         /* useful yet irritating feature, Shift+Click to open the file
161          * Alt+Click to browse a folder in the OS's browser */
162         if(event->shift || event->alt) {
163                 PointerRNA props_ptr;
164
165                 if(event->alt) {
166                         char *lslash= BLI_last_slash(str);
167                         if(lslash)
168                                 *lslash= '\0';
169                 }
170
171
172                 WM_operator_properties_create(&props_ptr, "WM_OT_path_open");
173                 RNA_string_set(&props_ptr, "filepath", str);
174                 WM_operator_name_call(C, "WM_OT_path_open", WM_OP_EXEC_DEFAULT, &props_ptr);
175                 WM_operator_properties_free(&props_ptr);
176
177                 MEM_freeN(str);
178                 return OPERATOR_CANCELLED;
179         }
180         else {
181                 fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
182                 fbo->ptr= ptr;
183                 fbo->prop= prop;
184                 op->customdata= fbo;
185
186                 RNA_string_set(op->ptr, "filepath", str);
187                 MEM_freeN(str);
188
189                 if(RNA_struct_find_property(op->ptr, "relative_path")) {
190                         if(!RNA_property_is_set(op->ptr, "relative_path")) {
191                                 /* annoying exception!, if were dealign with the user prefs, default relative to be off */
192                                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS && (ptr.data != &U));
193                         }
194                 }
195                 WM_event_add_fileselect(C, op);
196
197                 return OPERATOR_RUNNING_MODAL;
198         }
199 }
200
201 void BUTTONS_OT_file_browse(wmOperatorType *ot)
202 {
203         /* identifiers */
204         ot->name= "Accept";
205         ot->description="Open a file browser, Hold Shift to open the file, Alt to browse containing directory";
206         ot->idname= "BUTTONS_OT_file_browse";
207         
208         /* api callbacks */
209         ot->invoke= file_browse_invoke;
210         ot->exec= file_browse_exec;
211         ot->cancel= file_browse_cancel;
212
213         /* properties */
214         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
215 }
216