correct fsf address
[blender.git] / source / blender / editors / space_node / node_header.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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_space_types.h"
33 #include "DNA_node_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_texture_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43
44 #include "BKE_context.h"
45 #include "BKE_screen.h"
46 #include "BKE_node.h"
47 #include "BKE_main.h"
48 #include "BKE_utildefines.h"
49
50 #include "ED_screen.h"
51 #include "ED_types.h"
52 #include "ED_util.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "BIF_gl.h"
58 #include "BIF_glutil.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64 #include "node_intern.h"
65
66 /* ************************ add menu *********************** */
67
68 static void do_node_add(bContext *C, void *arg, int event)
69 {
70         SpaceNode *snode= CTX_wm_space_node(C);
71         bNode *node;
72         
73         /* store selection in temp test flag */
74         for(node= snode->edittree->nodes.first; node; node= node->next) {
75                 if(node->flag & NODE_SELECT) node->flag |= NODE_TEST;
76                 else node->flag &= ~NODE_TEST;
77         }
78         
79         node= node_add_node(snode, CTX_data_scene(C), event, snode->mx, snode->my);
80         
81         /* select previous selection before autoconnect */
82         for(node= snode->edittree->nodes.first; node; node= node->next) {
83                 if(node->flag & NODE_TEST) node->flag |= NODE_SELECT;
84         }
85         
86         snode_autoconnect(snode, 1, 0);
87         
88         /* deselect after autoconnection */
89         for(node= snode->edittree->nodes.first; node; node= node->next) {
90                 if(node->flag & NODE_TEST) node->flag &= ~NODE_SELECT;
91         }
92                 
93         snode_notify(C, snode);
94 }
95
96 static void node_auto_add_menu(bContext *C, uiLayout *layout, void *arg_nodeclass)
97 {
98         Main *bmain= CTX_data_main(C);
99         SpaceNode *snode= CTX_wm_space_node(C);
100         bNodeTree *ntree;
101         int nodeclass= GET_INT_FROM_POINTER(arg_nodeclass);
102         int tot= 0, a;
103         
104         ntree = snode->nodetree;
105
106         if(!ntree) {
107                 uiItemS(layout);
108                 return;
109         }
110
111         /* mostly taken from toolbox.c, node_add_sublevel() */
112         if(nodeclass==NODE_CLASS_GROUP) {
113                 bNodeTree *ngroup= bmain->nodetree.first;
114                 for(; ngroup; ngroup= ngroup->id.next)
115                         if(ngroup->type==ntree->type)
116                                 tot++;
117         }
118         else {
119                 bNodeType *type = ntree->alltypes.first;
120                 while(type) {
121                         if(type->nclass == nodeclass)
122                                 tot++;
123                         type= type->next;
124                 }
125         }       
126         
127         if(tot==0) {
128                 uiItemS(layout);
129                 return;
130         }
131
132         uiLayoutSetFunc(layout, do_node_add, NULL);
133         
134         if(nodeclass==NODE_CLASS_GROUP) {
135                 bNodeTree *ngroup= bmain->nodetree.first;
136
137                 for(tot=0, a=0; ngroup; ngroup= ngroup->id.next, tot++) {
138                         if(ngroup->type==ntree->type) {
139                                 uiItemV(layout, ngroup->id.name+2, 0, NODE_GROUP_MENU+tot);
140                                 a++;
141                         }
142                 }
143         }
144         else {
145                 bNodeType *type;
146                 int script=0;
147
148                 for(a=0, type= ntree->alltypes.first; type; type=type->next) {
149                         if(type->nclass == nodeclass && type->name) {
150                                 if(type->type == NODE_DYNAMIC) {
151                                         uiItemV(layout, type->name, 0, NODE_DYNAMIC_MENU+script);
152                                         script++;
153                                 }
154                                 else
155                                         uiItemV(layout, type->name, 0, type->type);
156
157                                 a++;
158                         }
159                 }
160         }
161 }
162
163 static void node_menu_add(const bContext *C, Menu *menu)
164 {
165         uiLayout *layout= menu->layout;
166         SpaceNode *snode= CTX_wm_space_node(C);
167
168         if(!snode->nodetree)
169                 uiLayoutSetActive(layout, 0);
170
171         if(snode->treetype==NTREE_SHADER) {
172                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
173                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
174                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
175                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
176                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
177                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
178                 uiItemMenuF(layout, "Dynamic", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_DYNAMIC));
179         }
180         else if(snode->treetype==NTREE_COMPOSIT) {
181                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
182                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
183                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
184                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
185                 uiItemMenuF(layout, "Filter", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_FILTER));
186                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
187                 uiItemMenuF(layout, "Matte", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_MATTE));
188                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
189                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
190         }
191         else if(snode->treetype==NTREE_TEXTURE) {
192                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
193                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
194                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
195                 uiItemMenuF(layout, "Patterns", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_PATTERN));
196                 uiItemMenuF(layout, "Textures", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_TEXTURE));
197                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
198                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
199                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
200         }
201 }
202
203 void node_menus_register(ARegionType *art)
204 {
205         MenuType *mt;
206
207         mt= MEM_callocN(sizeof(MenuType), "spacetype node menu add");
208         strcpy(mt->idname, "NODE_MT_add");
209         strcpy(mt->label, "Add");
210         mt->draw= node_menu_add;
211         WM_menutype_add(mt);
212 }
213