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