workaround/fix for [#24540] Adding the path to an image Editor in the file preference...
[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 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_userdef_types.h"
34
35 #include "BLI_fileops.h"
36 #include "BLI_path_util.h"
37 #include "BLI_storage.h"
38 #include "BLI_string.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "ED_screen.h"
48
49 #include "RNA_access.h"
50
51 #include "UI_interface.h"
52
53 #include "buttons_intern.h"     // own include
54
55 /********************** toolbox operator *********************/
56
57 static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
58 {
59         bScreen *sc= CTX_wm_screen(C);
60         SpaceButs *sbuts= CTX_wm_space_buts(C);
61         PointerRNA ptr;
62         uiPopupMenu *pup;
63         uiLayout *layout;
64
65         RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
66
67         pup= uiPupMenuBegin(C, "Align", 0);
68         layout= uiPupMenuLayout(pup);
69         uiItemsEnumR(layout, &ptr, "align");
70         uiPupMenuEnd(C, pup);
71
72         return OPERATOR_CANCELLED;
73 }
74
75 void BUTTONS_OT_toolbox(wmOperatorType *ot)
76 {
77         /* identifiers */
78         ot->name= "Toolbox";
79         ot->description="Display button panel toolbox";
80         ot->idname= "BUTTONS_OT_toolbox";
81         
82         /* api callbacks */
83         ot->invoke= toolbox_invoke;
84         ot->poll= ED_operator_buttons_active;
85 }
86
87 /********************** filebrowse operator *********************/
88
89 typedef struct FileBrowseOp {
90         PointerRNA ptr;
91         PropertyRNA *prop;
92 } FileBrowseOp;
93
94 static int file_browse_exec(bContext *C, wmOperator *op)
95 {
96         FileBrowseOp *fbo= op->customdata;
97         ID *id;
98         char *base, *str, path[FILE_MAX];
99         
100         if (RNA_property_is_set(op->ptr, "filepath")==0 || fbo==NULL)
101                 return OPERATOR_CANCELLED;
102         
103         str= RNA_string_get_alloc(op->ptr, "filepath", 0, 0);
104
105         /* add slash for directories, important for some properties */
106         if(RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
107                 id = fbo->ptr.id.data;
108                 base = (id && id->lib)? id->lib->filepath: G.main->name;
109
110                 BLI_strncpy(path, str, FILE_MAX);
111                 BLI_path_abs(path, base);
112
113                 if(BLI_is_dir(path)) {
114                         str = MEM_reallocN(str, strlen(str)+2);
115                         BLI_add_slash(str);
116                 }
117         }
118
119         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
120         RNA_property_update(C, &fbo->ptr, fbo->prop);
121         MEM_freeN(str);
122
123         MEM_freeN(op->customdata);
124         return OPERATOR_FINISHED;
125 }
126
127 static int file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
128 {
129         MEM_freeN(op->customdata);
130         op->customdata= NULL;
131
132         return OPERATOR_CANCELLED;
133 }
134
135 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
136 {
137         PointerRNA ptr;
138         PropertyRNA *prop;
139         FileBrowseOp *fbo;
140         char *str;
141
142         uiFileBrowseContextProperty(C, &ptr, &prop);
143
144         if(!prop)
145                 return OPERATOR_CANCELLED;
146
147         str= RNA_property_string_get_alloc(&ptr, prop, 0, 0);
148
149         /* useful yet irritating feature, Shift+Click to open the file
150          * Alt+Click to browse a folder in the OS's browser */
151         if(event->shift || event->alt) {
152                 PointerRNA props_ptr;
153
154                 if(event->alt) {
155                         char *lslash= BLI_last_slash(str);
156                         if(lslash)
157                                 *lslash= '\0';
158                 }
159
160
161                 WM_operator_properties_create(&props_ptr, "WM_OT_path_open");
162                 RNA_string_set(&props_ptr, "filepath", str);
163                 WM_operator_name_call(C, "WM_OT_path_open", WM_OP_EXEC_DEFAULT, &props_ptr);
164                 WM_operator_properties_free(&props_ptr);
165
166                 MEM_freeN(str);
167                 return OPERATOR_CANCELLED;
168         }
169         else {
170                 fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
171                 fbo->ptr= ptr;
172                 fbo->prop= prop;
173                 op->customdata= fbo;
174
175                 RNA_string_set(op->ptr, "filepath", str);
176                 MEM_freeN(str);
177
178                 if(RNA_struct_find_property(op->ptr, "relative_path")) {
179                         if(!RNA_property_is_set(op->ptr, "relative_path")) {
180                                 /* annoying exception!, if were dealign with the user prefs, default relative to be off */
181                                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS && (ptr.data != &U));
182                         }
183                 }
184                 WM_event_add_fileselect(C, op);
185
186                 return OPERATOR_RUNNING_MODAL;
187         }
188 }
189
190 void BUTTONS_OT_file_browse(wmOperatorType *ot)
191 {
192         /* identifiers */
193         ot->name= "Accept";
194         ot->description="Open a file browser, Hold Shift to open the file, Alt to browse containing directory";
195         ot->idname= "BUTTONS_OT_file_browse";
196         
197         /* api callbacks */
198         ot->invoke= file_browse_invoke;
199         ot->exec= file_browse_exec;
200         ot->cancel= file_browse_cancel;
201
202         /* properties */
203         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
204 }
205