Cycles: fix crash in 3d view status text display.
[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 /** \file blender/editors/space_node/node_header.c
30  *  \ingroup spnode
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "DNA_space_types.h"
38 #include "DNA_node_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49 #include "BKE_node.h"
50 #include "BKE_main.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58 #include "UI_interface_icons.h"
59 #include "UI_view2d.h"
60
61 #include "node_intern.h"
62
63 /* ************************ add menu *********************** */
64
65 static void do_node_add(bContext *C, void *UNUSED(arg), int event)
66 {
67         Main *bmain= CTX_data_main(C);
68         Scene *scene= CTX_data_scene(C);
69         SpaceNode *snode= CTX_wm_space_node(C);
70         ScrArea *sa= CTX_wm_area(C);
71         ARegion *ar;
72         bNode *node;
73         
74         /* get location to add node at mouse */
75         for(ar=sa->regionbase.first; ar; ar=ar->next) {
76                 if(ar->regiontype == RGN_TYPE_WINDOW) {
77                         wmWindow *win= CTX_wm_window(C);
78                         int x= win->eventstate->x - ar->winrct.xmin;
79                         int y= win->eventstate->y - ar->winrct.ymin;
80                         
81                         if(y < 60) y+= 60;
82                         UI_view2d_region_to_view(&ar->v2d, x, y, &snode->mx, &snode->my);
83                 }
84         }
85         
86         /* store selection in temp test flag */
87         for(node= snode->edittree->nodes.first; node; node= node->next) {
88                 if(node->flag & NODE_SELECT) node->flag |= NODE_TEST;
89                 else node->flag &= ~NODE_TEST;
90         }
91         
92         node= node_add_node(snode, bmain, scene, event, snode->mx, snode->my);
93         
94         /* select previous selection before autoconnect */
95         for(node= snode->edittree->nodes.first; node; node= node->next) {
96                 if(node->flag & NODE_TEST) node->flag |= NODE_SELECT;
97         }
98         
99         /* deselect after autoconnection */
100         for(node= snode->edittree->nodes.first; node; node= node->next) {
101                 if(node->flag & NODE_TEST) node->flag &= ~NODE_SELECT;
102         }
103                 
104         snode_notify(C, snode);
105         snode_dag_update(C, snode);
106 }
107
108 static void node_auto_add_menu(bContext *C, uiLayout *layout, void *arg_nodeclass)
109 {
110         Main *bmain= CTX_data_main(C);
111         SpaceNode *snode= CTX_wm_space_node(C);
112         bNodeTree *ntree;
113         int nodeclass= GET_INT_FROM_POINTER(arg_nodeclass);
114         int tot= 0, a;
115         
116         ntree = snode->nodetree;
117
118         if(!ntree) {
119                 uiItemS(layout);
120                 return;
121         }
122
123         /* mostly taken from toolbox.c, node_add_sublevel() */
124         if(nodeclass==NODE_CLASS_GROUP) {
125                 bNodeTree *ngroup= bmain->nodetree.first;
126                 for(; ngroup; ngroup= ngroup->id.next)
127                         if(ngroup->type==ntree->type)
128                                 tot++;
129         }
130         else {
131                 bNodeType *type = ntree->alltypes.first;
132                 while(type) {
133                         if(type->nclass == nodeclass)
134                                 tot++;
135                         type= type->next;
136                 }
137         }       
138         
139         if(tot==0) {
140                 uiItemS(layout);
141                 return;
142         }
143
144         uiLayoutSetFunc(layout, do_node_add, NULL);
145         
146         if(nodeclass==NODE_CLASS_GROUP) {
147                 bNodeTree *ngroup= bmain->nodetree.first;
148
149                 for(tot=0, a=0; ngroup; ngroup= ngroup->id.next, tot++) {
150                         if(ngroup->type==ntree->type) {
151                                 uiItemV(layout, ngroup->id.name+2, ICON_NONE, NODE_GROUP_MENU+tot);
152                                 a++;
153                         }
154                 }
155         }
156         else {
157                 bNodeType *type;
158                 int script=0;
159
160                 for(a=0, type= ntree->alltypes.first; type; type=type->next) {
161                         if(type->nclass == nodeclass && type->name) {
162                                 if(type->type == NODE_DYNAMIC) {
163                                         uiItemV(layout, type->name, ICON_NONE, NODE_DYNAMIC_MENU+script);
164                                         script++;
165                                 }
166                                 else
167                                         uiItemV(layout, type->name, ICON_NONE, type->type);
168
169                                 a++;
170                         }
171                 }
172         }
173 }
174
175 static void node_menu_add(const bContext *C, Menu *menu)
176 {
177         SpaceNode *snode= CTX_wm_space_node(C);
178         uiLayout *layout= menu->layout;
179
180         if(!snode->nodetree)
181                 uiLayoutSetActive(layout, 0);
182
183         if(snode->treetype==NTREE_SHADER) {
184                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
185                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
186                 uiItemMenuF(layout, "Closure", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CLOSURE));
187                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
188                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
189                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
190                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
191                 uiItemMenuF(layout, "Texture", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_TEXTURE));
192         }
193         else if(snode->treetype==NTREE_COMPOSIT) {
194                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
195                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
196                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
197                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
198                 uiItemMenuF(layout, "Filter", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_FILTER));
199                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
200                 uiItemMenuF(layout, "Matte", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_MATTE));
201                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
202                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
203         }
204         else if(snode->treetype==NTREE_TEXTURE) {
205                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
206                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
207                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
208                 uiItemMenuF(layout, "Patterns", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_PATTERN));
209                 uiItemMenuF(layout, "Textures", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_TEXTURE));
210                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
211                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
212                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
213         }
214 }
215
216 void node_menus_register(void)
217 {
218         MenuType *mt;
219
220         mt= MEM_callocN(sizeof(MenuType), "spacetype node menu add");
221         strcpy(mt->idname, "NODE_MT_add");
222         strcpy(mt->label, "Add");
223         mt->draw= node_menu_add;
224         WM_menutype_add(mt);
225 }
226