4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2009 Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/space_buttons/buttons_ops.c
36 #include "MEM_guardedalloc.h"
38 #include "DNA_userdef_types.h"
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"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
53 #include "ED_screen.h"
55 #include "RNA_access.h"
57 #include "UI_interface.h"
58 #include "UI_resources.h"
60 #include "buttons_intern.h" // own include
62 /********************** toolbox operator *********************/
64 static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
66 bScreen *sc= CTX_wm_screen(C);
67 SpaceButs *sbuts= CTX_wm_space_buts(C);
72 RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
74 pup= uiPupMenuBegin(C, "Align", ICON_NONE);
75 layout= uiPupMenuLayout(pup);
76 uiItemsEnumR(layout, &ptr, "align");
79 return OPERATOR_CANCELLED;
82 void BUTTONS_OT_toolbox(wmOperatorType *ot)
86 ot->description="Display button panel toolbox";
87 ot->idname= "BUTTONS_OT_toolbox";
90 ot->invoke= toolbox_invoke;
91 ot->poll= ED_operator_buttons_active;
94 /********************** filebrowse operator *********************/
96 typedef struct FileBrowseOp {
101 static int file_browse_exec(bContext *C, wmOperator *op)
103 FileBrowseOp *fbo= op->customdata;
105 char *base, *str, path[FILE_MAX];
107 if (RNA_property_is_set(op->ptr, "filepath")==0 || fbo==NULL)
108 return OPERATOR_CANCELLED;
110 str= RNA_string_get_alloc(op->ptr, "filepath", NULL, 0);
112 /* add slash for directories, important for some properties */
113 if(RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
116 id = fbo->ptr.id.data;
117 base = (id && id->lib)? id->lib->filepath: G.main->name;
119 BLI_strncpy(path, str, FILE_MAX);
120 BLI_path_abs(path, base);
122 if(BLI_is_dir(path)) {
123 str = MEM_reallocN(str, strlen(str)+2);
127 BLI_splitdirstring(str, name);
130 RNA_property_string_set(&fbo->ptr, fbo->prop, str);
131 RNA_property_update(C, &fbo->ptr, fbo->prop);
134 MEM_freeN(op->customdata);
135 return OPERATOR_FINISHED;
138 static int file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
140 MEM_freeN(op->customdata);
141 op->customdata= NULL;
143 return OPERATOR_CANCELLED;
146 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
153 uiFileBrowseContextProperty(C, &ptr, &prop);
156 return OPERATOR_CANCELLED;
158 str= RNA_property_string_get_alloc(&ptr, prop, NULL, 0);
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;
166 char *lslash= BLI_last_slash(str);
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);
178 return OPERATOR_CANCELLED;
181 fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
186 RNA_string_set(op->ptr, "filepath", str);
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));
195 WM_event_add_fileselect(C, op);
197 return OPERATOR_RUNNING_MODAL;
201 void BUTTONS_OT_file_browse(wmOperatorType *ot)
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";
209 ot->invoke= file_browse_invoke;
210 ot->exec= file_browse_exec;
211 ot->cancel= file_browse_cancel;
214 WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);