Renamed the FILE_OPEN option for the file browser to FILE_OPENFILE to cleanup compile...
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_screen_types.h"
34 #include "DNA_space_types.h"
35
36 #include "BKE_context.h"
37
38 #include "WM_api.h"
39 #include "WM_types.h"
40
41 #include "RNA_access.h"
42 #include "RNA_define.h"
43
44 #include "UI_interface.h"
45 #include "UI_resources.h"
46
47 #include "buttons_intern.h"     // own include
48
49 /********************** toolbox operator *********************/
50
51 static int toolbox_invoke(bContext *C, wmOperator *op, wmEvent *event)
52 {
53         bScreen *sc= CTX_wm_screen(C);
54         SpaceButs *sbuts= CTX_wm_space_buts(C);
55         PointerRNA ptr;
56         uiPopupMenu *pup;
57         uiLayout *layout;
58
59         RNA_pointer_create(&sc->id, &RNA_SpaceProperties, sbuts, &ptr);
60
61         pup= uiPupMenuBegin(C, "Align", 0);
62         layout= uiPupMenuLayout(pup);
63         uiItemsEnumR(layout, &ptr, "align");
64         uiPupMenuEnd(C, pup);
65
66         return OPERATOR_CANCELLED;
67 }
68
69 void BUTTONS_OT_toolbox(wmOperatorType *ot)
70 {
71         /* identifiers */
72         ot->name= "Toolbox";
73         ot->description="Display button panel toolbox";
74         ot->idname= "BUTTONS_OT_toolbox";
75         
76         /* api callbacks */
77         ot->invoke= toolbox_invoke;
78 }
79
80 /********************** filebrowse operator *********************/
81
82 typedef struct FileBrowseOp {
83         PointerRNA ptr;
84         PropertyRNA *prop;
85 } FileBrowseOp;
86
87 static int file_browse_exec(bContext *C, wmOperator *op)
88 {
89         FileBrowseOp *fbo= op->customdata;
90         char *str;
91         
92         if (RNA_property_is_set(op->ptr, "path")==0 || fbo==NULL)
93                 return OPERATOR_CANCELLED;
94         
95         str= RNA_string_get_alloc(op->ptr, "path", 0, 0);
96         RNA_property_string_set(&fbo->ptr, fbo->prop, str);
97         RNA_property_update(C, &fbo->ptr, fbo->prop);
98         MEM_freeN(str);
99
100         MEM_freeN(op->customdata);
101         return OPERATOR_FINISHED;
102 }
103
104 static int file_browse_cancel(bContext *C, wmOperator *op)
105 {
106         MEM_freeN(op->customdata);
107         op->customdata= NULL;
108
109         return OPERATOR_CANCELLED;
110 }
111
112 static int file_browse_invoke(bContext *C, wmOperator *op, wmEvent *event)
113 {
114         PointerRNA ptr;
115         PropertyRNA *prop;
116         FileBrowseOp *fbo;
117         char *str;
118
119         uiFileBrowseContextProperty(C, &ptr, &prop);
120
121         if(!prop)
122                 return OPERATOR_CANCELLED;
123         
124         fbo= MEM_callocN(sizeof(FileBrowseOp), "FileBrowseOp");
125         fbo->ptr= ptr;
126         fbo->prop= prop;
127         op->customdata= fbo;
128
129         str= RNA_property_string_get_alloc(&ptr, prop, 0, 0);
130         RNA_string_set(op->ptr, "path", str);
131         MEM_freeN(str);
132
133         WM_event_add_fileselect(C, op); 
134         
135         return OPERATOR_RUNNING_MODAL;
136 }
137
138 void BUTTONS_OT_file_browse(wmOperatorType *ot)
139 {
140         /* identifiers */
141         ot->name= "File Browse";
142         ot->description="Open a file browser.";
143         ot->idname= "BUTTONS_OT_file_browse";
144         
145         /* api callbacks */
146         ot->invoke= file_browse_invoke;
147         ot->exec= file_browse_exec;
148         ot->cancel= file_browse_cancel;
149
150         /* properties */
151         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE);
152 }
153