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