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