a5caf1c930b15b46a73437ea7da18c7b8e5c401d
[blender.git] / source / blender / editors / interface / interface_ops.c
1 /**
2  * $Id: interface_ops.c 24699 2009-11-20 10:21:31Z aligorith $
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, Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdio.h>
29 #include <math.h>
30 #include <string.h>
31
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_userdef_types.h"
39 #include "DNA_vec_types.h"
40 #include "DNA_view2d_types.h"
41
42 #include "BLI_blenlib.h"
43
44 #include "BKE_context.h"
45 #include "BKE_utildefines.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "BIF_gl.h"
54
55 #include "ED_screen.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59
60 /* ********************************************************** */
61
62 /* Copy to Clipboard Button Operator ------------------------ */
63
64 static int copy_clipboard_button_exec(bContext *C, wmOperator *op)
65 {
66         PointerRNA ptr;
67         PropertyRNA *prop= NULL;
68         char *path;
69         short success= 0;
70         int index;
71
72         /* try to create driver using property retrieved from UI */
73         memset(&ptr, 0, sizeof(PointerRNA));
74         uiAnimContextProperty(C, &ptr, &prop, &index);
75
76         if (ptr.data && prop) {
77                 path= RNA_path_from_ID_to_property(&ptr, prop);
78                 
79                 if (path) {
80                         WM_clipboard_text_set(path, FALSE);
81                         MEM_freeN(path);
82                 }
83         }
84
85         /* since we're just copying, we don't really need to do anything else...*/
86         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
87 }
88
89 void UI_OT_copy_clipboard_button(wmOperatorType *ot)
90 {
91         /* identifiers */
92         ot->name= "Copy Data Path";
93         ot->idname= "UI_OT_copy_clipboard_button";
94         ot->description= "Copy the RNA data path for this property to the clipboard.";
95
96         /* callbacks */
97         ot->exec= copy_clipboard_button_exec;
98         //op->poll= ??? // TODO: need to have some valid property before this can be done
99
100         /* flags */
101         ot->flag= OPTYPE_REGISTER;
102 }
103
104 /* Reset to Default Values Button Operator ------------------------ */
105
106 static int reset_default_button_exec(bContext *C, wmOperator *op)
107 {
108         PointerRNA ptr;
109         PropertyRNA *prop= NULL;
110         short success= 0;
111         int index, len;
112         int all = RNA_boolean_get(op->ptr, "all");
113
114         /* try to reset the nominated setting to its default value */
115         memset(&ptr, 0, sizeof(PointerRNA));
116         uiAnimContextProperty(C, &ptr, &prop, &index);
117         
118         /* if there is a valid property that is editable... */
119         if (ptr.data && prop && RNA_property_editable(&ptr, prop)) {
120                 /* get the length of the array to work with */
121                 len= RNA_property_array_length(&ptr, prop);
122                 
123                 /* get and set the default values as appropriate for the various types */
124                 switch (RNA_property_type(prop)) {
125                         case PROP_BOOLEAN:
126                                 if (len) {
127                                         if (all) {
128                                                 int *tmparray= MEM_callocN(sizeof(int)*len, "reset_defaults - boolean");
129                                                 
130                                                 RNA_property_boolean_get_default_array(&ptr, prop, tmparray);
131                                                 RNA_property_boolean_set_array(&ptr, prop, tmparray);
132                                                 
133                                                 MEM_freeN(tmparray);
134                                         }
135                                         else {
136                                                 int value= RNA_property_boolean_get_default_index(&ptr, prop, index);
137                                                 RNA_property_boolean_set_index(&ptr, prop, index, value);
138                                         }
139                                 }
140                                 else {
141                                         int value= RNA_property_boolean_get_default(&ptr, prop);
142                                         RNA_property_boolean_set(&ptr, prop, value);
143                                 }
144                                 break;
145                         case PROP_INT:
146                                 if (len) {
147                                         if (all) {
148                                                 int *tmparray= MEM_callocN(sizeof(int)*len, "reset_defaults - int");
149                                                 
150                                                 RNA_property_int_get_default_array(&ptr, prop, tmparray);
151                                                 RNA_property_int_set_array(&ptr, prop, tmparray);
152                                                 
153                                                 MEM_freeN(tmparray);
154                                         }
155                                         else {
156                                                 int value= RNA_property_int_get_default_index(&ptr, prop, index);
157                                                 RNA_property_int_set_index(&ptr, prop, index, value);
158                                         }
159                                 }
160                                 else {
161                                         int value= RNA_property_int_get_default(&ptr, prop);
162                                         RNA_property_int_set(&ptr, prop, value);
163                                 }
164                                 break;
165                         case PROP_FLOAT:
166                                 if (len) {
167                                         if (all) {
168                                                 float *tmparray= MEM_callocN(sizeof(float)*len, "reset_defaults - float");
169                                                 
170                                                 RNA_property_float_get_default_array(&ptr, prop, tmparray);
171                                                 RNA_property_float_set_array(&ptr, prop, tmparray);
172                                                 
173                                                 MEM_freeN(tmparray);
174                                         }
175                                         else {
176                                                 float value= RNA_property_float_get_default_index(&ptr, prop, index);
177                                                 RNA_property_float_set_index(&ptr, prop, index, value);
178                                         }
179                                 }
180                                 else {
181                                         float value= RNA_property_float_get_default(&ptr, prop);
182                                         RNA_property_float_set(&ptr, prop, value);
183                                 }
184                                 break;
185                         case PROP_ENUM:
186                         {
187                                 int value= RNA_property_enum_get_default(&ptr, prop);
188                                 RNA_property_enum_set(&ptr, prop, value);
189                         }
190                                 break;
191                         
192                         //case PROP_POINTER:
193                         //case PROP_STRING:
194                         default: 
195                                 // FIXME: many of the other types such as strings and pointers need this implemented too!
196                                 break;
197                 }
198                 
199                 /* perform updates required for this property */
200                 RNA_property_update(C, &ptr, prop);
201                 
202                 success= 1;
203         }
204         
205         return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
206 }
207
208 void UI_OT_reset_default_button(wmOperatorType *ot)
209 {
210         /* identifiers */
211         ot->name= "Reset to Default Value";
212         ot->idname= "UI_OT_reset_default_button";
213         ot->description= "Copy the RNA data path for this property to the clipboard.";
214
215         /* callbacks */
216         ot->exec= reset_default_button_exec;
217         //op->poll= ??? // TODO: need to have some valid property before this can be done
218
219         /* flags */
220         ot->flag= OPTYPE_REGISTER;
221         
222         /* properties */
223         RNA_def_boolean(ot->srna, "all", 1, "All", "Reset to default values all elements of the array.");
224 }
225  
226 /* ********************************************************* */
227 /* Registration */
228
229 void ui_buttons_operatortypes(void)
230 {
231         WM_operatortype_append(UI_OT_copy_clipboard_button);
232         WM_operatortype_append(UI_OT_reset_default_button);
233 }
234