2.5 MSVC9 projectfiles
[blender-staging.git] / source / blender / editors / space_node / node_select.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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation, Nathan Letwory
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdio.h>
30
31 #include "DNA_node_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_space_types.h"
35
36 #include "BKE_context.h"
37 #include "BKE_node.h"
38 #include "BKE_global.h"
39
40 #include "BLI_rect.h"
41
42 #include "ED_space_api.h"
43 #include "ED_screen.h"
44 #include "ED_types.h"
45
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "UI_view2d.h"
53  
54 #include "node_intern.h"
55  
56 static void node_mouse_select(SpaceNode *snode, ARegion *ar, short *mval, short extend)
57 {
58         bNode *node;
59         float mx, my;
60         
61         mx= (float)mval[0];
62         my= (float)mval[1];
63         
64         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
65         
66         for(next_node(snode->edittree); (node=next_node(NULL));) {
67                 
68                 /* first check for the headers or scaling widget */
69                 /* XXX if(node->flag & NODE_HIDDEN) {
70                         if(do_header_hidden_node(snode, node, mx, my))
71                                 return 1;
72                 }
73                 else {
74                         if(do_header_node(snode, node, mx, my))
75                                 return 1;
76                 }*/
77                 
78                 /* node body */
79                 if(BLI_in_rctf(&node->totr, mx, my))
80                         break;
81         }
82         if(node) {
83                 if((extend & KM_SHIFT)==0)
84                         node_deselectall(snode, 0);
85                 
86                 if(extend & KM_SHIFT) {
87                         if(node->flag & SELECT)
88                                 node->flag &= ~SELECT;
89                         else
90                                 node->flag |= SELECT;
91                 }
92                 else
93                         node->flag |= SELECT;
94                 
95                 node_set_active(snode, node);
96                 
97                 /* viewer linking */
98                 if(extend & KM_CTRL)
99                         ;//     node_link_viewer(snode, node);
100                 
101                 //std_rmouse_transform(node_transform_ext);     /* does undo push for select */
102                 ED_region_tag_redraw(ar);
103         }
104 }
105
106 static int node_select_exec(bContext *C, wmOperator *op)
107 {
108         SpaceNode *snode= (SpaceNode*)CTX_wm_space_data(C);
109         ARegion *ar= CTX_wm_region(C);
110         int select_type;
111         short mval[2];
112         short extend;
113
114         select_type = RNA_enum_get(op->ptr, "select_type");
115         
116         switch (select_type) {
117                 case NODE_SELECT_MOUSE:
118                         mval[0] = RNA_int_get(op->ptr, "mx");
119                         mval[1] = RNA_int_get(op->ptr, "my");
120                         extend = RNA_int_get(op->ptr, "extend");
121                         node_mouse_select(snode, ar, mval, extend);
122                         break;
123         }
124         return OPERATOR_FINISHED;
125 }
126
127 static int node_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
128 {
129         ARegion *ar= CTX_wm_region(C);
130         short mval[2];  
131         
132         mval[0]= event->x - ar->winrct.xmin;
133         mval[1]= event->y - ar->winrct.ymin;
134         
135         RNA_int_set(op->ptr, "mx", mval[0]);
136         RNA_int_set(op->ptr, "my", mval[1]);
137
138         return node_select_exec(C,op);
139 }
140
141 static int node_extend_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
142 {
143         RNA_int_set(op->ptr, "extend", KM_SHIFT);
144
145         return node_select_invoke(C, op, event);
146 }
147
148 /* operators */
149
150 static EnumPropertyItem prop_select_items[] = {
151         {NODE_SELECT_MOUSE, "NORMAL", "Normal Select", "Select using the mouse"},
152         {0, NULL, NULL, NULL}};
153
154 void NODE_OT_extend_select(wmOperatorType *ot)
155 {
156         PropertyRNA *prop;
157         
158         /* identifiers */
159         ot->name= "Activate/Select (Shift)";
160         ot->idname= "NODE_OT_extend_select";
161         
162         /* api callbacks */
163         ot->invoke= node_extend_select_invoke;
164         ot->poll= ED_operator_node_active;
165         
166         prop = RNA_def_property(ot->srna, "select_type", PROP_ENUM, PROP_NONE);
167         RNA_def_property_enum_items(prop, prop_select_items);
168         
169         prop = RNA_def_property(ot->srna, "mx", PROP_INT, PROP_NONE);
170         prop = RNA_def_property(ot->srna, "my", PROP_INT, PROP_NONE);
171         prop = RNA_def_property(ot->srna, "extend", PROP_INT, PROP_NONE);
172 }
173
174 void NODE_OT_select(wmOperatorType *ot)
175 {
176         PropertyRNA *prop;
177         
178         /* identifiers */
179         ot->name= "Activate/Select";
180         ot->idname= "NODE_OT_select";
181         
182         /* api callbacks */
183         ot->invoke= node_select_invoke;
184         ot->poll= ED_operator_node_active;
185         
186         prop = RNA_def_property(ot->srna, "select_type", PROP_ENUM, PROP_NONE);
187         RNA_def_property_enum_items(prop, prop_select_items);
188         
189         prop = RNA_def_property(ot->srna, "mx", PROP_INT, PROP_NONE);
190         prop = RNA_def_property(ot->srna, "my", PROP_INT, PROP_NONE);
191         prop = RNA_def_property(ot->srna, "extend", PROP_INT, PROP_NONE);
192 }
193
194 /* ****** Border Select ****** */
195
196 static EnumPropertyItem prop_select_types[] = {
197         {NODE_EXCLUSIVE, "EXCLUSIVE", "Exclusive", ""}, /* right mouse */
198         {NODE_EXTEND, "EXTEND", "Extend", ""}, /* left mouse */
199         {0, NULL, NULL, NULL}
200 };
201
202 static int node_borderselect_exec(bContext *C, wmOperator *op)
203 {
204         SpaceNode *snode= (SpaceNode*)CTX_wm_space_data(C);
205         ARegion *ar= CTX_wm_region(C);
206         bNode *node;
207         rcti rect;
208         rctf rectf;
209         short val;
210         
211         val= RNA_int_get(op->ptr, "event_type");
212         
213         rect.xmin= RNA_int_get(op->ptr, "xmin");
214         rect.ymin= RNA_int_get(op->ptr, "ymin");
215         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
216         
217         rect.xmax= RNA_int_get(op->ptr, "xmax");
218         rect.ymax= RNA_int_get(op->ptr, "ymax");
219         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
220         
221         for(node= snode->edittree->nodes.first; node; node= node->next) {
222                 if(BLI_isect_rctf(&rectf, &node->totr, NULL)) {
223                         if(val==NODE_EXTEND)
224                                 node->flag |= SELECT;
225                         else
226                                 node->flag &= ~SELECT;
227                 }
228         }
229         
230         return OPERATOR_FINISHED;
231 }
232
233 void NODE_OT_border_select(wmOperatorType *ot)
234 {
235         PropertyRNA *prop;
236         
237         /* identifiers */
238         ot->name= "Border Select";
239         ot->idname= "NODE_OT_border_select";
240         
241         /* api callbacks */
242         ot->invoke= WM_border_select_invoke;
243         ot->exec= node_borderselect_exec;
244         ot->modal= WM_border_select_modal;
245         
246         ot->poll= ED_operator_node_active;
247         
248         /* rna */
249         RNA_def_property(ot->srna, "event_type", PROP_INT, PROP_NONE);
250         RNA_def_property(ot->srna, "xmin", PROP_INT, PROP_NONE);
251         RNA_def_property(ot->srna, "xmax", PROP_INT, PROP_NONE);
252         RNA_def_property(ot->srna, "ymin", PROP_INT, PROP_NONE);
253         RNA_def_property(ot->srna, "ymax", PROP_INT, PROP_NONE);
254
255         prop = RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
256         RNA_def_property_enum_items(prop, prop_select_types);
257 }