migrated NDOF code from soc-2010-merwin, SpaceNavigator now works on Mac blender
[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 /** \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 #include "ED_util.h"
55
56 #include "RNA_access.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60
61 #include "buttons_intern.h"     // own include
62
63 /********************** toolbox operator *********************/
64
65 static int toolbox_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
66 {
67         bScreen *sc= CTX_wm_screen(C);
68         SpaceButs *sbuts= CTX_wm_space_buts(C);
69         PointerRNA ptr;
70         uiPopupMenu *pup;
71         uiLayout *layout;
72
73         RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
74
75         pup= uiPupMenuBegin(C, "Align", ICON_NONE);
76         layout= uiPupMenuLayout(pup);
77         uiItemsEnumR(layout, &ptr, "align");
78         uiPupMenuEnd(C, pup);
79
80         return OPERATOR_CANCELLED;
81 }
82
83 void BUTTONS_OT_toolbox(wmOperatorType *ot)
84 {
85         /* identifiers */
86         ot->name= "Toolbox";
87         ot->description="Display button panel toolbox";
88         ot->idname= "BUTTONS_OT_toolbox";
89         
90         /* api callbacks */
91         ot->invoke= toolbox_invoke;
92         ot->poll= ED_operator_buttons_active;
93 }
94
95 /********************** filebrowse operator *********************/
96
97 typedef struct FileBrowseOp {
98         PointerRNA ptr;
99         PropertyRNA *prop;
100 } FileBrowseOp;
101
102 static int file_browse_exec(bContext *C, wmOperator *op)
103 {
104         FileBrowseOp *fbo= op->customdata;
105         ID *id;
106         char *base, *str, path[FILE_MAX];
107         
108         if (RNA_property_is_set(op->ptr, "filepath")==0 || fbo==NULL)
109                 return OPERATOR_CANCELLED;
110         
111         str= RNA_string_get_alloc(op->ptr, "filepath", NULL, 0);
112
113         /* add slash for directories, important for some properties */
114         if(RNA_property_subtype(fbo->prop) == PROP_DIRPATH) {
115                 char name[FILE_MAX];
116                 
117                 id = fbo->ptr.id.data;
118                 base = (id && id->lib)? id->lib->filepath: G.main->name;
119
120                 BLI_strncpy(path, str, FILE_MAX);
121                 BLI_path_abs(path, base);
122                 
123                 if(BLI_is_dir(path)) {
124                         str = MEM_reallocN(str, strlen(str)+2);
125                         BLI_add_slash(str);
126                 }
127                 else
128                         BLI_splitdirstring(str, name);
129         }
130
131         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
132         RNA_property_update(C, &fbo->ptr, fbo->prop);
133         MEM_freeN(str);
134
135
136         /* special, annoying exception, filesel on redo panel [#26618] */
137         {
138                 wmOperator *redo_op= WM_operator_last_redo(C);
139                 if(redo_op) {
140                         if(fbo->ptr.data == redo_op->ptr->data) {
141                                 ED_undo_operator_repeat(C, redo_op);
142                         }
143                 }
144         }
145
146         MEM_freeN(op->customdata);
147
148         return OPERATOR_FINISHED;
149 }
150
151 static int file_browse_cancel(bContext *UNUSED(C), wmOperator *op)
152 {
153         MEM_freeN(op->customdata);
154         op->customdata= NULL;
155
156         return OPERATOR_CANCELLED;
157 }
158
159 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
160 {
161         PointerRNA ptr;
162         PropertyRNA *prop;
163         FileBrowseOp *fbo;
164         char *str;
165
166         uiFileBrowseContextProperty(C, &ptr, &prop);
167
168         if(!prop)
169                 return OPERATOR_CANCELLED;
170
171         str= RNA_property_string_get_alloc(&ptr, prop, NULL, 0);
172
173         /* useful yet irritating feature, Shift+Click to open the file
174          * Alt+Click to browse a folder in the OS's browser */
175         if(event->shift || event->alt) {
176                 PointerRNA props_ptr;
177
178                 if(event->alt) {
179                         char *lslash= BLI_last_slash(str);
180                         if(lslash)
181                                 *lslash= '\0';
182                 }
183
184
185                 WM_operator_properties_create(&props_ptr, "WM_OT_path_open");
186                 RNA_string_set(&props_ptr, "filepath", str);
187                 WM_operator_name_call(C, "WM_OT_path_open", WM_OP_EXEC_DEFAULT, &props_ptr);
188                 WM_operator_properties_free(&props_ptr);
189
190                 MEM_freeN(str);
191                 return OPERATOR_CANCELLED;
192         }
193         else {
194                 fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
195                 fbo->ptr= ptr;
196                 fbo->prop= prop;
197                 op->customdata= fbo;
198
199                 RNA_string_set(op->ptr, "filepath", str);
200                 MEM_freeN(str);
201
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