Merging r46456 through r46468 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_node / drawnode.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Bob Holcomb, Thomas Dinges
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_node/drawnode.c
29  *  \ingroup spnode
30  */
31
32
33 #include <math.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_utildefines.h"
40
41 #include "DNA_node_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_screen_types.h"
47
48 #include "BKE_context.h"
49 #include "BKE_curve.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_library.h"
53 #include "BKE_main.h"
54 #include "BKE_node.h"
55
56 #include "NOD_composite.h"
57 #include "NOD_shader.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "BLF_api.h"
63
64 #include "MEM_guardedalloc.h"
65
66
67 #include "RNA_access.h"
68
69 #include "ED_node.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "UI_interface.h"
75 #include "UI_resources.h"
76
77 #include "IMB_imbuf.h"
78 #include "IMB_imbuf_types.h"
79
80 #include "node_intern.h"
81
82 /* ****************** SOCKET BUTTON DRAW FUNCTIONS ***************** */
83
84 static void node_sync_cb(bContext *UNUSED(C), void *snode_v, void *node_v)
85 {
86         SpaceNode *snode= snode_v;
87         
88         if (snode->treetype==NTREE_SHADER) {
89                 nodeShaderSynchronizeID(node_v, 1);
90                 // allqueue(REDRAWBUTSSHADING, 0);
91         }
92 }
93
94 static void node_socket_button_label(const bContext *UNUSED(C), uiBlock *block,
95                                                           bNodeTree *UNUSED(ntree), bNode *UNUSED(node), bNodeSocket *sock,
96                                                           const char *UNUSED(name), int x, int y, int width)
97 {
98         uiDefBut(block, LABEL, 0, sock->name, x, y, width, NODE_DY, NULL, 0, 0, 0, 0, "");
99 }
100
101 static void node_socket_button_default(const bContext *C, uiBlock *block,
102                                                                 bNodeTree *ntree, bNode *node, bNodeSocket *sock,
103                                                                 const char *name, int x, int y, int width)
104 {
105         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
106                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
107         else {
108                 PointerRNA ptr;
109                 uiBut *bt;
110                 
111                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
112                 
113                 bt = uiDefButR(block, NUM, B_NODE_EXEC, name,
114                                            x, y+1, width, NODE_DY-2, 
115                                            &ptr, "default_value", 0, 0, 0, -1, -1, NULL);
116                 if (node)
117                         uiButSetFunc(bt, node_sync_cb, CTX_wm_space_node(C), node);
118         }
119 }
120
121 typedef struct SocketComponentMenuArgs {
122         PointerRNA ptr;
123         int x, y, width;
124         uiButHandleFunc cb;
125         void *arg1, *arg2;
126 } SocketComponentMenuArgs;
127 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
128 static uiBlock *socket_component_menu(bContext *C, ARegion *ar, void *args_v)
129 {
130         SocketComponentMenuArgs *args= (SocketComponentMenuArgs*)args_v;
131         uiBlock *block;
132         uiLayout *layout;
133         
134         block= uiBeginBlock(C, ar, __func__, UI_EMBOSS);
135         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
136         
137         layout= uiLayoutColumn(uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, args->x, args->y+2, args->width, NODE_DY, UI_GetStyle()), 0);
138         
139         uiItemR(layout, &args->ptr, "default_value", UI_ITEM_R_EXPAND, "", ICON_NONE);
140         
141         return block;
142 }
143 static void node_socket_button_components(const bContext *C, uiBlock *block,
144                                                                    bNodeTree *ntree, bNode *node, bNodeSocket *sock,
145                                                                    const char *name, int x, int y, int width)
146 {
147         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
148                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
149         else {
150                 PointerRNA ptr;
151                 SocketComponentMenuArgs *args;
152                 
153                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
154                 
155                 args= MEM_callocN(sizeof(SocketComponentMenuArgs), "SocketComponentMenuArgs");
156                 
157                 args->ptr = ptr;
158                 args->x = x;
159                 args->y = y;
160                 args->width = width;
161                 args->cb = node_sync_cb;
162                 args->arg1 = CTX_wm_space_node(C);
163                 args->arg2 = node;
164                 
165                 uiDefBlockButN(block, socket_component_menu, args, name, x, y+1, width, NODE_DY-2, "");
166         }
167 }
168
169 static void node_socket_button_color(const bContext *C, uiBlock *block,
170                                                           bNodeTree *ntree, bNode *node, bNodeSocket *sock,
171                                                           const char *name, int x, int y, int width)
172 {
173         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
174                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
175         else {
176                 PointerRNA ptr;
177                 uiBut *bt;
178                 int labelw= width - 40;
179                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
180                 
181                 bt=uiDefButR(block, COL, B_NODE_EXEC, "",
182                                          x, y+2, (labelw>0 ? 40 : width), NODE_DY-2, 
183                                          &ptr, "default_value", 0, 0, 0, -1, -1, NULL);
184                 if (node)
185                         uiButSetFunc(bt, node_sync_cb, CTX_wm_space_node(C), node);
186                 
187                 if (name[0]!='\0' && labelw>0)
188                         uiDefBut(block, LABEL, 0, name, x + 40, y+2, labelw, NODE_DY-2, NULL, 0, 0, 0, 0, "");
189         }
190 }
191
192 /* standard draw function, display the default input value */
193 static void node_draw_input_default(const bContext *C, uiBlock *block,
194                                      bNodeTree *ntree, bNode *node, bNodeSocket *sock,
195                                      const char *name, int x, int y, int width)
196 {
197         bNodeSocketType *stype = ntreeGetSocketType(sock->type);
198         if (stype->buttonfunc)
199                 stype->buttonfunc(C, block, ntree, node, sock, name, x, y, width);
200         else
201                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
202 }
203
204 static void node_draw_output_default(const bContext *C, uiBlock *block,
205                                      bNodeTree *UNUSED(ntree), bNode *node, bNodeSocket *sock,
206                                      const char *name, int UNUSED(x), int UNUSED(y), int UNUSED(width))
207 {
208         SpaceNode *snode = CTX_wm_space_node(C);
209         float slen;
210         int ofs = 0;
211         UI_ThemeColor(TH_TEXT);
212         slen= snode->aspect*UI_GetStringWidth(name);
213         while (slen > node->width) {
214                 ofs++;
215                 slen= snode->aspect*UI_GetStringWidth(name+ofs);
216         }
217         uiDefBut(block, LABEL, 0, name+ofs, (short)(sock->locx-15.0f-slen), (short)(sock->locy-9.0f), 
218              (short)(node->width-NODE_DY), NODE_DY,  NULL, 0, 0, 0, 0, "");
219 }
220
221 /* ****************** BASE DRAW FUNCTIONS FOR NEW OPERATOR NODES ***************** */
222
223 #if 0 /* UNUSED */
224 static void node_draw_socket_new(bNodeSocket *sock, float size)
225 {
226         float x=sock->locx, y=sock->locy;
227         
228         /* 16 values of sin function */
229         static float si[16] = {
230                 0.00000000f, 0.39435585f, 0.72479278f,0.93775213f,
231                 0.99871650f,0.89780453f,0.65137248f,0.29936312f,
232                 -0.10116832f,-0.48530196f,-0.79077573f,-0.96807711f,
233                 -0.98846832f,-0.84864425f,-0.57126821f,-0.20129852f
234         };
235         /* 16 values of cos function */
236         static float co[16] ={
237                 1.00000000f,0.91895781f,0.68896691f,0.34730525f,
238                 -0.05064916f,-0.44039415f,-0.75875812f,-0.95413925f,
239                 -0.99486932f,-0.87434661f,-0.61210598f,-0.25065253f,
240                 0.15142777f,0.52896401f,0.82076344f,0.97952994f,
241         };
242         int a;
243         
244         glColor3ub(180, 180, 180);
245         
246         glBegin(GL_POLYGON);
247         for (a=0; a<16; a++)
248                 glVertex2f(x+size*si[a], y+size*co[a]);
249         glEnd();
250         
251         glColor4ub(0, 0, 0, 150);
252         glEnable(GL_BLEND);
253         glEnable(GL_LINE_SMOOTH);
254         glBegin(GL_LINE_LOOP);
255         for (a=0; a<16; a++)
256                 glVertex2f(x+size*si[a], y+size*co[a]);
257         glEnd();
258         glDisable(GL_LINE_SMOOTH);
259         glDisable(GL_BLEND);
260 }
261 #endif
262
263 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
264
265 static void node_buts_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
266 {
267         PointerRNA sockptr;
268         PropertyRNA *prop;
269         
270         /* first socket stores value */
271         prop = RNA_struct_find_property(ptr, "outputs");
272         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
273         
274         uiItemR(layout, &sockptr, "default_value", 0, "", ICON_NONE);
275 }
276
277 static void node_buts_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
278 {
279         uiLayout *col;
280         PointerRNA sockptr;
281         PropertyRNA *prop;
282         
283         /* first socket stores value */
284         prop = RNA_struct_find_property(ptr, "outputs");
285         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
286         
287         col = uiLayoutColumn(layout, 0);
288         uiTemplateColorWheel(col, &sockptr, "default_value", 1, 0, 0, 0);
289         uiItemR(col, &sockptr, "default_value", 0, "", ICON_NONE);
290 }
291
292 static void node_buts_mix_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
293 {       
294         uiLayout *row;
295
296         bNodeTree *ntree= (bNodeTree*)ptr->id.data;
297
298         row= uiLayoutRow(layout, 1);
299         uiItemR(row, ptr, "blend_type", 0, "", ICON_NONE);
300         if (ntree->type == NTREE_COMPOSIT)
301                 uiItemR(row, ptr, "use_alpha", 0, "", ICON_IMAGE_RGB_ALPHA);
302 }
303
304 static void node_buts_time(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
305 {
306         uiLayout *row;
307 #if 0
308         /* XXX no context access here .. */
309         bNode *node= ptr->data;
310         CurveMapping *cumap= node->storage;
311         
312         if (cumap) {
313                 cumap->flag |= CUMA_DRAW_CFRA;
314                 if (node->custom1<node->custom2)
315                         cumap->sample[0]= (float)(CFRA - node->custom1)/(float)(node->custom2-node->custom1);
316         }
317 #endif
318
319         uiTemplateCurveMapping(layout, ptr, "curve", 's', 0, 0);
320
321         row= uiLayoutRow(layout, 1);
322         uiItemR(row, ptr, "frame_start", 0, "Sta", ICON_NONE);
323         uiItemR(row, ptr, "frame_end", 0, "End", ICON_NONE);
324 }
325
326 static void node_buts_colorramp(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
327 {
328         uiTemplateColorRamp(layout, ptr, "color_ramp", 0);
329 }
330
331 static void node_buts_curvevec(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
332 {
333         uiTemplateCurveMapping(layout, ptr, "mapping", 'v', 0, 0);
334 }
335
336 static float *_sample_col= NULL;        // bad bad, 2.5 will do better?
337 #if 0
338 static void node_curvemap_sample(float *col)
339 {
340         _sample_col= col;
341 }
342 #endif
343
344 static void node_buts_curvecol(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
345 {
346         bNode *node= ptr->data;
347         CurveMapping *cumap= node->storage;
348
349         if (_sample_col) {
350                 cumap->flag |= CUMA_DRAW_SAMPLE;
351                 copy_v3_v3(cumap->sample, _sample_col);
352         }
353         else 
354                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
355
356         uiTemplateCurveMapping(layout, ptr, "mapping", 'c', 0, 0);
357 }
358
359 static void node_normal_cb(bContext *C, void *ntree_v, void *node_v)
360 {
361         Main *bmain = CTX_data_main(C);
362
363         ED_node_generic_update(bmain, ntree_v, node_v);
364         WM_event_add_notifier(C, NC_NODE|NA_EDITED, ntree_v);
365 }
366
367 static void node_buts_normal(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
368 {
369         uiBlock *block= uiLayoutAbsoluteBlock(layout);
370         bNodeTree *ntree= ptr->id.data;
371         bNode *node= ptr->data;
372         rctf *butr= &node->butr;
373         bNodeSocket *sock= node->outputs.first;         /* first socket stores normal */
374         float *nor= ((bNodeSocketValueVector*)sock->default_value)->value;
375         uiBut *bt;
376         
377         bt= uiDefButF(block, BUT_NORMAL, B_NODE_EXEC, "", 
378                       (short)butr->xmin, (short)butr->xmin,
379                       butr->xmax-butr->xmin, butr->xmax-butr->xmin,
380                       nor, 0.0f, 1.0f, 0, 0, "");
381         uiButSetFunc(bt, node_normal_cb, ntree, node);
382 }
383 #if 0 // not used in 2.5x yet
384 static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
385 {
386         Main *bmain= CTX_data_main(C);
387         bNodeTree *ntree= ntree_v;
388         bNode *node= node_v;
389         Tex *tex;
390         
391         if (node->menunr<1) return;
392         
393         if (node->id) {
394                 node->id->us--;
395                 node->id= NULL;
396         }
397         tex= BLI_findlink(&bmain->tex, node->menunr-1);
398
399         node->id= &tex->id;
400         id_us_plus(node->id);
401         BLI_strncpy(node->name, node->id->name+2, sizeof(node->name));
402         
403         nodeSetActive(ntree, node);
404         
405         if ( ntree->type == NTREE_TEXTURE )
406                 ntreeTexCheckCyclics(ntree);
407         
408         // allqueue(REDRAWBUTSSHADING, 0);
409         // allqueue(REDRAWNODE, 0);
410         NodeTagChanged(ntree, node); 
411         
412         node->menunr= 0;
413 }
414 #endif
415 static void node_dynamic_update_cb(bContext *C, void *UNUSED(ntree_v), void *node_v)
416 {
417         Main *bmain= CTX_data_main(C);
418         Material *ma;
419         bNode *node= (bNode *)node_v;
420         ID *id= node->id;
421         int error= 0;
422
423         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) error= 1;
424
425         /* Users only have to press the "update" button in one pynode
426          * and we also update all others sharing the same script */
427         for (ma= bmain->mat.first; ma; ma= ma->id.next) {
428                 if (ma->nodetree) {
429                         bNode *nd;
430                         for (nd= ma->nodetree->nodes.first; nd; nd= nd->next) {
431                                 if ((nd->type == NODE_DYNAMIC) && (nd->id == id)) {
432                                         nd->custom1= 0;
433                                         nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_REPARSE);
434                                         nd->menunr= 0;
435                                         if (error)
436                                                 nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_ERROR);
437                                 }
438                         }
439                 }
440         }
441
442         // allqueue(REDRAWBUTSSHADING, 0);
443         // allqueue(REDRAWNODE, 0);
444         // XXX BIF_preview_changed(ID_MA);
445 }
446
447 static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
448 {
449         bNode *node= ptr->data;
450
451         short multi = (
452                 node->id &&
453                 ((Tex*)node->id)->use_nodes &&
454                 (node->type != CMP_NODE_TEXTURE) &&
455                 (node->type != TEX_NODE_TEXTURE)
456         );
457         
458         uiItemR(layout, ptr, "texture", 0, "", ICON_NONE);
459         
460         if (multi) {
461                 /* Number Drawing not optimal here, better have a list*/
462                 uiItemR(layout, ptr, "node_output", 0, "", ICON_NONE);
463         }
464 }
465
466 static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
467
468         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
469 }
470
471 static int node_resize_area_default(bNode *node, int x, int y)
472 {
473         if (node->flag & NODE_HIDDEN) {
474                 rctf totr= node->totr;
475                 /* right part of node */
476                 totr.xmin = node->totr.xmax-20.0f;
477                 return BLI_in_rctf(&totr, x, y);
478         }
479         else {
480                 /* rect we're interested in is just the bottom right corner */
481                 rctf totr= node->totr;
482                 /* bottom right corner */
483                 totr.xmin = totr.xmax-10.0f;
484                 totr.ymax = totr.ymin+10.0f;
485                 return BLI_in_rctf(&totr, x, y);
486         }
487 }
488
489 /* ****************** BUTTON CALLBACKS FOR COMMON NODES ***************** */
490
491 /* width of socket columns in group display */
492 #define NODE_GROUP_FRAME                120
493
494 /* based on settings in node, sets drawing rect info. each redraw! */
495 /* note: this assumes only 1 group at a time is drawn (linked data) */
496 /* in node->totr the entire boundbox for the group is stored */
497 static void node_update_group(const bContext *C, bNodeTree *ntree, bNode *gnode)
498 {
499         if (!(gnode->flag & NODE_GROUP_EDIT)) {
500                 node_update_default(C, ntree, gnode);
501         }
502         else {
503                 bNodeTree *ngroup= (bNodeTree *)gnode->id;
504                 bNode *node;
505                 bNodeSocket *sock, *gsock;
506                 float locx, locy;
507                 rctf *rect= &gnode->totr;
508                 float node_group_frame= U.dpi*NODE_GROUP_FRAME/72;
509                 float group_header= 26*U.dpi/72;
510                 int counter;
511                 int dy;
512                 
513                 /* get "global" coords */
514                 nodeSpaceCoords(gnode, &locx, &locy);
515                 
516                 /* center them, is a bit of abuse of locx and locy though */
517                 node_update_nodetree(C, ngroup, locx, locy);
518                 
519                 rect->xmin = rect->xmax = locx;
520                 rect->ymin = rect->ymax = locy;
521                 
522                 counter= 1;
523                 for (node= ngroup->nodes.first; node; node= node->next) {
524                         if (counter) {
525                                 *rect= node->totr;
526                                 counter= 0;
527                         }
528                         else
529                                 BLI_union_rctf(rect, &node->totr);
530                 }
531                 
532                 /* add some room for links to group sockets */
533                 rect->xmin -= 4*NODE_DY;
534                 rect->xmax += 4*NODE_DY;
535                 rect->ymin-= NODE_DY;
536                 rect->ymax+= NODE_DY;
537                 
538                 /* input sockets */
539                 dy = 0.5f*(rect->ymin+rect->ymax) + NODE_DY*(BLI_countlist(&gnode->inputs)-1);
540                 gsock=ngroup->inputs.first;
541                 sock=gnode->inputs.first;
542                 while (gsock || sock) {
543                         while (sock && !sock->groupsock) {
544                                 sock->locx = rect->xmin - node_group_frame;
545                                 sock->locy = dy;
546
547                                 /* prevent long socket lists from growing out of the group box */
548                                 if (dy-3*NODE_DYS < rect->ymin)
549                                         rect->ymin = dy-3*NODE_DYS;
550                                 if (dy+3*NODE_DYS > rect->ymax)
551                                         rect->ymax = dy+3*NODE_DYS;
552                                 dy -= 2*NODE_DY;
553                                 
554                                 sock = sock->next;
555                         }
556                         while (gsock && (!sock || sock->groupsock!=gsock)) {
557                                 gsock->locx = rect->xmin;
558                                 gsock->locy = dy;
559                                 
560                                 /* prevent long socket lists from growing out of the group box */
561                                 if (dy-3*NODE_DYS < rect->ymin)
562                                         rect->ymin = dy-3*NODE_DYS;
563                                 if (dy+3*NODE_DYS > rect->ymax)
564                                         rect->ymax = dy+3*NODE_DYS;
565                                 dy -= 2*NODE_DY;
566                                 
567                                 gsock = gsock->next;
568                         }
569                         while (sock && gsock && sock->groupsock==gsock) {
570                                 gsock->locx = rect->xmin;
571                                 sock->locx = rect->xmin - node_group_frame;
572                                 sock->locy = gsock->locy = dy;
573                                 
574                                 /* prevent long socket lists from growing out of the group box */
575                                 if (dy-3*NODE_DYS < rect->ymin)
576                                         rect->ymin = dy-3*NODE_DYS;
577                                 if (dy+3*NODE_DYS > rect->ymax)
578                                         rect->ymax = dy+3*NODE_DYS;
579                                 dy -= 2*NODE_DY;
580                                 
581                                 sock = sock->next;
582                                 gsock = gsock->next;
583                         }
584                 }
585                 
586                 /* output sockets */
587                 dy = 0.5f*(rect->ymin+rect->ymax) + NODE_DY*(BLI_countlist(&gnode->outputs)-1);
588                 gsock=ngroup->outputs.first;
589                 sock=gnode->outputs.first;
590                 while (gsock || sock) {
591                         while (sock && !sock->groupsock) {
592                                 sock->locx = rect->xmax + node_group_frame;
593                                 sock->locy = dy - NODE_DYS;
594                                 
595                                 /* prevent long socket lists from growing out of the group box */
596                                 if (dy-3*NODE_DYS < rect->ymin)
597                                         rect->ymin = dy-3*NODE_DYS;
598                                 if (dy+3*NODE_DYS > rect->ymax)
599                                         rect->ymax = dy+3*NODE_DYS;
600                                 dy -= 2*NODE_DY;
601                                 
602                                 sock = sock->next;
603                         }
604                         while (gsock && (!sock || sock->groupsock!=gsock)) {
605                                 gsock->locx = rect->xmax;
606                                 gsock->locy = dy - NODE_DYS;
607                                 
608                                 /* prevent long socket lists from growing out of the group box */
609                                 if (dy-3*NODE_DYS < rect->ymin)
610                                         rect->ymin = dy-3*NODE_DYS;
611                                 if (dy+3*NODE_DYS > rect->ymax)
612                                         rect->ymax = dy+3*NODE_DYS;
613                                 dy -= 2*NODE_DY;
614                                 
615                                 gsock = gsock->next;
616                         }
617                         while (sock && gsock && sock->groupsock==gsock) {
618                                 gsock->locx = rect->xmax;
619                                 sock->locx = rect->xmax + node_group_frame;
620                                 sock->locy = gsock->locy = dy - NODE_DYS;
621                                 
622                                 /* prevent long socket lists from growing out of the group box */
623                                 if (dy-3*NODE_DYS < rect->ymin)
624                                         rect->ymin = dy-3*NODE_DYS;
625                                 if (dy+3*NODE_DYS > rect->ymax)
626                                         rect->ymax = dy+3*NODE_DYS;
627                                 dy -= 2*NODE_DY;
628                                 
629                                 sock = sock->next;
630                                 gsock = gsock->next;
631                         }
632                 }
633                 
634                 /* Set the block bounds to clip mouse events from underlying nodes.
635                  * Add margin for header and input/output columns.
636                  */
637                 uiExplicitBoundsBlock(gnode->block,
638                                                           rect->xmin - node_group_frame,
639                                                           rect->ymin,
640                                                           rect->xmax + node_group_frame,
641                                                           rect->ymax + group_header);
642         }
643 }
644
645 static void update_group_input_cb(bContext *UNUSED(C), void *UNUSED(snode_v), void *ngroup_v)
646 {
647         bNodeTree *ngroup= (bNodeTree*)ngroup_v;
648         
649         ngroup->update |= NTREE_UPDATE_GROUP_IN;
650         ntreeUpdateTree(ngroup);
651 }
652
653 static void update_group_output_cb(bContext *UNUSED(C), void *UNUSED(snode_v), void *ngroup_v)
654 {
655         bNodeTree *ngroup= (bNodeTree*)ngroup_v;
656         
657         ngroup->update |= NTREE_UPDATE_GROUP_OUT;
658         ntreeUpdateTree(ngroup);
659 }
660
661 static void draw_group_socket_name(SpaceNode *snode, bNode *gnode, bNodeSocket *sock, int in_out, float xoffset, float yoffset)
662 {
663         bNodeTree *ngroup= (bNodeTree*)gnode->id;
664         uiBut *bt;
665         
666         if (sock->flag & SOCK_DYNAMIC) {
667                 bt = uiDefBut(gnode->block, TEX, 0, "", 
668                                           sock->locx+xoffset, sock->locy+1+yoffset, 72, NODE_DY,
669                                           sock->name, 0, sizeof(sock->name), 0, 0, "");
670                 if (in_out==SOCK_IN)
671                         uiButSetFunc(bt, update_group_input_cb, snode, ngroup);
672                 else
673                         uiButSetFunc(bt, update_group_output_cb, snode, ngroup);
674         }
675         else {
676                 uiDefBut(gnode->block, LABEL, 0, sock->name,
677                          sock->locx+xoffset, sock->locy+1+yoffset, 72, NODE_DY,
678                          NULL, 0, sizeof(sock->name), 0, 0, "");
679         }
680 }
681
682 static void draw_group_socket(const bContext *C, SpaceNode *snode, bNodeTree *ntree, bNode *gnode, bNodeSocket *sock, bNodeSocket *gsock, int index, int in_out)
683 {
684         bNodeTree *ngroup= (bNodeTree*)gnode->id;
685         bNodeSocketType *stype= ntreeGetSocketType(gsock ? gsock->type : sock->type);
686         uiBut *bt;
687         float offset;
688         int draw_value;
689         float node_group_frame= U.dpi*NODE_GROUP_FRAME/72;
690         float socket_size= NODE_SOCKSIZE*U.dpi/72;
691         float arrowbutw= 0.8f*UI_UNIT_X;
692         /* layout stuff for buttons on group left frame */
693         float colw= 0.6f*node_group_frame;
694         float col1= 6 - node_group_frame;
695         float col2= col1 + colw+6;
696         float col3= - arrowbutw - 6;
697         /* layout stuff for buttons on group right frame */
698         float cor1= 6;
699         float cor2= cor1 + arrowbutw + 6;
700         float cor3= cor2 + arrowbutw + 6;
701         
702         /* node and group socket circles */
703         if (sock)
704                 node_socket_circle_draw(ntree, sock, socket_size);
705         if (gsock)
706                 node_socket_circle_draw(ngroup, gsock, socket_size);
707         
708         /* socket name */
709         offset = (in_out==SOCK_IN ? col1 : cor3);
710         if (!gsock)
711                 offset += (in_out==SOCK_IN ? node_group_frame : -node_group_frame);
712         
713         /* draw both name and value button if:
714          * 1) input: not internal
715          * 2) output: (node type uses const outputs) and (group output is unlinked)
716          */
717         draw_value = 0;
718         switch (in_out) {
719         case SOCK_IN:
720                 draw_value = !(gsock && (gsock->flag & SOCK_INTERNAL));
721                 break;
722         case SOCK_OUT:
723                 if (gnode->typeinfo->flag & NODE_CONST_OUTPUT)
724                         draw_value = !(gsock && gsock->link);
725                 break;
726         }
727         if (draw_value) {
728                 /* both name and value buttons */
729                 if (gsock) {
730                         draw_group_socket_name(snode, gnode, gsock, in_out, offset, 0);
731                         if (stype->buttonfunc)
732                                 stype->buttonfunc(C, gnode->block, ngroup, NULL, gsock, "", gsock->locx+offset, gsock->locy-NODE_DY, colw);
733                 }
734                 else {
735                         draw_group_socket_name(snode, gnode, sock, in_out, offset, 0);
736                         if (stype->buttonfunc)
737                                 stype->buttonfunc(C, gnode->block, ngroup, NULL, sock, "", sock->locx+offset, sock->locy-NODE_DY, colw);
738                 }
739         }
740         else {
741                 /* only name, no value button */
742                 if (gsock)
743                         draw_group_socket_name(snode, gnode, gsock, in_out, offset, -NODE_DYS);
744                 else
745                         draw_group_socket_name(snode, gnode, sock, in_out, offset, -NODE_DYS);
746         }
747         
748         if (gsock && (gsock->flag & SOCK_DYNAMIC)) {
749                 /* up/down buttons */
750                 offset = (in_out==SOCK_IN ? col2 : cor2);
751                 uiBlockSetDirection(gnode->block, UI_TOP);
752                 uiBlockBeginAlign(gnode->block);
753                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_move_up", 0, ICON_TRIA_UP,
754                                                    gsock->locx+offset, gsock->locy, arrowbutw, arrowbutw, "");
755                 if (!gsock->prev || !(gsock->prev->flag & SOCK_DYNAMIC))
756                         uiButSetFlag(bt, UI_BUT_DISABLED);
757                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
758                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
759                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_move_down", 0, ICON_TRIA_DOWN,
760                                                    gsock->locx+offset, gsock->locy-arrowbutw, arrowbutw, arrowbutw, "");
761                 if (!gsock->next || !(gsock->next->flag & SOCK_DYNAMIC))
762                         uiButSetFlag(bt, UI_BUT_DISABLED);
763                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
764                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
765                 uiBlockEndAlign(gnode->block);
766                 uiBlockSetDirection(gnode->block, 0);
767                 
768                 /* remove button */
769                 offset = (in_out==SOCK_IN ? col3 : cor1);
770                 uiBlockSetEmboss(gnode->block, UI_EMBOSSN);
771                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_remove", 0, ICON_X,
772                                                    gsock->locx+offset, gsock->locy-0.5f*arrowbutw, arrowbutw, arrowbutw, "");
773                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
774                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
775                 uiBlockSetEmboss(gnode->block, UI_EMBOSS);
776         }
777 }
778
779 /* groups are, on creation, centered around 0,0 */
780 static void node_draw_group(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *ntree, bNode *gnode)
781 {
782         if (!(gnode->flag & NODE_GROUP_EDIT)) {
783                 node_draw_default(C, ar, snode, ntree, gnode);
784         }
785         else {
786                 bNodeTree *ngroup= (bNodeTree *)gnode->id;
787                 bNodeSocket *sock, *gsock;
788                 uiLayout *layout;
789                 PointerRNA ptr;
790                 rctf rect= gnode->totr;
791                 float node_group_frame= U.dpi*NODE_GROUP_FRAME/72;
792                 float group_header= 26*U.dpi/72;
793                 
794                 int index;
795                 
796                 /* backdrop header */
797                 glEnable(GL_BLEND);
798                 uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
799                 UI_ThemeColorShadeAlpha(TH_NODE_GROUP, 0, -70);
800                 uiDrawBox(GL_POLYGON, rect.xmin-node_group_frame, rect.ymax, rect.xmax+node_group_frame, rect.ymax+group_header, BASIS_RAD);
801                 
802                 /* backdrop body */
803                 UI_ThemeColorShadeAlpha(TH_BACK, -8, -70);
804                 uiSetRoundBox(UI_CNR_NONE);
805                 uiDrawBox(GL_POLYGON, rect.xmin, rect.ymin, rect.xmax, rect.ymax, BASIS_RAD);
806         
807                 /* input column */
808                 UI_ThemeColorShadeAlpha(TH_BACK, 10, -50);
809                 uiSetRoundBox(UI_CNR_BOTTOM_LEFT);
810                 uiDrawBox(GL_POLYGON, rect.xmin-node_group_frame, rect.ymin, rect.xmin, rect.ymax, BASIS_RAD);
811         
812                 /* output column */
813                 UI_ThemeColorShadeAlpha(TH_BACK, 10, -50);
814                 uiSetRoundBox(UI_CNR_BOTTOM_RIGHT);
815                 uiDrawBox(GL_POLYGON, rect.xmax, rect.ymin, rect.xmax+node_group_frame, rect.ymax, BASIS_RAD);
816         
817                 /* input column separator */
818                 glColor4ub(200, 200, 200, 140);
819                 glBegin(GL_LINES);
820                 glVertex2f(rect.xmin, rect.ymin);
821                 glVertex2f(rect.xmin, rect.ymax);
822                 glEnd();
823         
824                 /* output column separator */
825                 glColor4ub(200, 200, 200, 140);
826                 glBegin(GL_LINES);
827                 glVertex2f(rect.xmax, rect.ymin);
828                 glVertex2f(rect.xmax, rect.ymax);
829                 glEnd();
830         
831                 /* group node outline */
832                 uiSetRoundBox(UI_CNR_ALL);
833                 glColor4ub(200, 200, 200, 140);
834                 glEnable(GL_LINE_SMOOTH);
835                 uiDrawBox(GL_LINE_LOOP, rect.xmin-node_group_frame, rect.ymin, rect.xmax+node_group_frame, rect.ymax+group_header, BASIS_RAD);
836                 glDisable(GL_LINE_SMOOTH);
837                 glDisable(GL_BLEND);
838                 
839                 /* backdrop title */
840                 UI_ThemeColor(TH_TEXT_HI);
841         
842                 layout = uiBlockLayout(gnode->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, (short)(rect.xmin+15), (short)(rect.ymax+group_header),
843                                        MIN2((int)(rect.xmax - rect.xmin-18.0f), node_group_frame+20), group_header, UI_GetStyle());
844                 RNA_pointer_create(&ntree->id, &RNA_Node, gnode, &ptr);
845                 uiTemplateIDBrowse(layout, (bContext*)C, &ptr, "node_tree", NULL, NULL, NULL);
846                 uiBlockLayoutResolve(gnode->block, NULL, NULL);
847         
848                 /* draw the internal tree nodes and links */
849                 node_draw_nodetree(C, ar, snode, ngroup);
850         
851                 /* group sockets */
852                 gsock=ngroup->inputs.first;
853                 sock=gnode->inputs.first;
854                 index = 0;
855                 while (gsock || sock) {
856                         while (sock && !sock->groupsock) {
857                                 draw_group_socket(C, snode, ntree, gnode, sock, NULL, index, SOCK_IN);
858                                 sock = sock->next;
859                         }
860                         while (gsock && (!sock || sock->groupsock!=gsock)) {
861                                 draw_group_socket(C, snode, ntree, gnode, NULL, gsock, index, SOCK_IN);
862                                 gsock = gsock->next;
863                                 ++index;
864                         }
865                         while (sock && gsock && sock->groupsock==gsock) {
866                                 draw_group_socket(C, snode, ntree, gnode, sock, gsock, index, SOCK_IN);
867                                 sock = sock->next;
868                                 gsock = gsock->next;
869                                 ++index;
870                         }
871                 }
872                 gsock=ngroup->outputs.first;
873                 sock=gnode->outputs.first;
874                 index = 0;
875                 while (gsock || sock) {
876                         while (sock && !sock->groupsock) {
877                                 draw_group_socket(C, snode, ntree, gnode, sock, NULL, index, SOCK_OUT);
878                                 sock = sock->next;
879                         }
880                         while (gsock && (!sock || sock->groupsock!=gsock)) {
881                                 draw_group_socket(C, snode, ntree, gnode, NULL, gsock, index, SOCK_OUT);
882                                 gsock = gsock->next;
883                                 ++index;
884                         }
885                         while (sock && gsock && sock->groupsock==gsock) {
886                                 draw_group_socket(C, snode, ntree, gnode, sock, gsock, index, SOCK_OUT);
887                                 sock = sock->next;
888                                 gsock = gsock->next;
889                                 ++index;
890                         }
891                 }
892                 
893                 uiEndBlock(C, gnode->block);
894                 uiDrawBlock(C, gnode->block);
895                 gnode->block= NULL;
896         }
897 }
898
899 void node_uifunc_group(uiLayout *layout, bContext *C, PointerRNA *ptr)
900 {
901         uiTemplateIDBrowse(layout, C, ptr, "node_tree", NULL, NULL, NULL);
902 }
903
904 static void node_common_buts_whileloop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
905 {
906         uiItemR(layout, ptr, "max_iterations", 0, NULL, 0);
907 }
908
909 static void node_update_frame(const bContext *UNUSED(C), bNodeTree *UNUSED(ntree), bNode *node)
910 {
911         float locx, locy;
912
913         /* get "global" coords */
914         nodeSpaceCoords(node, &locx, &locy);
915
916         node->prvr.xmin = locx + NODE_DYS;
917         node->prvr.xmax = locx + node->width- NODE_DYS;
918
919         node->totr.xmin = locx;
920         node->totr.xmax = locx + node->width;
921         node->totr.ymax = locy;
922         node->totr.ymin = locy - node->height;
923 }
924
925 static void node_common_set_butfunc(bNodeType *ntype)
926 {
927         switch (ntype->type) {
928                 case NODE_GROUP:
929                         ntype->uifunc= node_uifunc_group;
930                         ntype->drawfunc= node_draw_group;
931                         ntype->drawupdatefunc= node_update_group;
932                         break;
933                 case NODE_FORLOOP:
934 //                      ntype->uifunc= node_common_buts_group;
935                         ntype->drawfunc= node_draw_group;
936                         ntype->drawupdatefunc= node_update_group;
937                         break;
938                 case NODE_WHILELOOP:
939                         ntype->uifunc= node_common_buts_whileloop;
940                         ntype->drawfunc= node_draw_group;
941                         ntype->drawupdatefunc= node_update_group;
942                         break;
943                 case NODE_FRAME:
944                         ntype->drawupdatefunc= node_update_frame;
945                         break;
946         }
947 }
948
949 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
950
951 static void node_browse_text_cb(bContext *C, void *ntree_v, void *node_v)
952 {
953         Main *bmain= CTX_data_main(C);
954         bNodeTree *ntree= ntree_v;
955         bNode *node= node_v;
956         /* ID *oldid; */ /* UNUSED */
957         
958         if (node->menunr<1) return;
959         
960         if (node->id) {
961                 node->id->us--;
962         }
963         /* oldid= node->id; */ /* UNUSED */
964         node->id= BLI_findlink(&bmain->text, node->menunr-1);
965         id_us_plus(node->id);
966         BLI_strncpy(node->name, node->id->name+2, sizeof(node->name));
967
968         node->custom1= BSET(node->custom1, NODE_DYNAMIC_NEW);
969         
970         nodeSetActive(ntree, node);
971
972         // allqueue(REDRAWBUTSSHADING, 0);
973         // allqueue(REDRAWNODE, 0);
974
975         node->menunr= 0;
976 }
977
978 static void node_shader_buts_material(uiLayout *layout, bContext *C, PointerRNA *ptr)
979 {
980         bNode *node= ptr->data;
981         uiLayout *col;
982         
983         uiTemplateID(layout, C, ptr, "material", "MATERIAL_OT_new", NULL, NULL);
984         
985         if (!node->id) return;
986         
987         col= uiLayoutColumn(layout, 0);
988         uiItemR(col, ptr, "use_diffuse", 0, NULL, ICON_NONE);
989         uiItemR(col, ptr, "use_specular", 0, NULL, ICON_NONE);
990         uiItemR(col, ptr, "invert_normal", 0, NULL, ICON_NONE);
991 }
992
993 static void node_shader_buts_mapping(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
994 {
995         uiLayout *row;
996         
997         uiItemL(layout, "Location:", ICON_NONE);
998         row= uiLayoutRow(layout, 1);
999         uiItemR(row, ptr, "translation", 0, "", ICON_NONE);
1000         
1001         uiItemL(layout, "Rotation:", ICON_NONE);
1002         row= uiLayoutRow(layout, 1);
1003         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
1004         
1005         uiItemL(layout, "Scale:", ICON_NONE);
1006         row= uiLayoutRow(layout, 1);
1007         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
1008         
1009         row= uiLayoutRow(layout, 1);
1010         uiItemR(row, ptr, "use_min", 0, "Min", ICON_NONE);
1011         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
1012         
1013         row= uiLayoutRow(layout, 1);
1014         uiItemR(row, ptr, "use_max", 0, "Max", ICON_NONE);
1015         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
1016 }
1017
1018 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1019
1020         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
1021 }
1022
1023 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
1024 {
1025         PointerRNA obptr= CTX_data_pointer_get(C, "active_object");
1026         uiLayout *col;
1027
1028         col= uiLayoutColumn(layout, 0);
1029
1030         if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
1031                 PointerRNA dataptr= RNA_pointer_get(&obptr, "data");
1032
1033                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
1034                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
1035         }
1036         else {
1037                 uiItemR(col, ptr, "uv_layer", 0, "UV", ICON_NONE);
1038                 uiItemR(col, ptr, "color_layer", 0, "VCol", ICON_NONE);
1039         }
1040 }
1041
1042 static void node_shader_buts_attribute(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1043 {
1044         uiItemR(layout, ptr, "attribute_name", 0, "Name", ICON_NONE);
1045 }
1046
1047 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1048 {
1049         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1050         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1051 }
1052
1053
1054 static void node_shader_buts_tex_environment(uiLayout *layout, bContext *C, PointerRNA *ptr)
1055 {
1056         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1057         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1058         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
1059 }
1060
1061 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1062 {
1063         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
1064         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
1065 }
1066
1067 static void node_shader_buts_tex_gradient(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1068 {
1069         uiItemR(layout, ptr, "gradient_type", 0, "", ICON_NONE);
1070 }
1071
1072 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1073 {
1074         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1075 }
1076
1077 static void node_shader_buts_tex_wave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1078 {
1079         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
1080 }
1081
1082 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1083 {
1084         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
1085 }
1086
1087 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1088 {
1089         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
1090 }
1091
1092 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1093 {
1094         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
1095 }
1096
1097 static void node_shader_buts_dynamic(uiLayout *layout, bContext *C, PointerRNA *ptr)
1098
1099         Main *bmain= CTX_data_main(C);
1100         uiBlock *block= uiLayoutAbsoluteBlock(layout);
1101         bNode *node= ptr->data;
1102         bNodeTree *ntree= ptr->id.data;
1103         rctf *butr= &node->butr;
1104         uiBut *bt;
1105         // XXX SpaceNode *snode= curarea->spacedata.first;
1106         short dy= (short)butr->ymin;
1107         int xoff=0;
1108
1109         /* B_NODE_EXEC is handled in butspace.c do_node_buts */
1110         if (!node->id) {
1111                         const char *strp;
1112                         IDnames_to_pupstring(&strp, NULL, "", &(bmain->text), NULL, NULL);
1113                         node->menunr= 0;
1114                         bt= uiDefButS(block, MENU, B_NODE_EXEC/*+node->nr*/, strp, 
1115                                                         butr->xmin, dy, 19, 19, 
1116                                                         &node->menunr, 0, 0, 0, 0, "Browses existing choices");
1117                         uiButSetFunc(bt, node_browse_text_cb, ntree, node);
1118                         xoff=19;
1119                         if (strp) MEM_freeN((void *)strp);
1120         }
1121         else {
1122                 bt = uiDefBut(block, BUT, B_NOP, "Update",
1123                                 butr->xmin+xoff, butr->ymin+20, 50, 19,
1124                                 &node->menunr, 0.0, 19.0, 0, 0, "Refresh this node (and all others that use the same script)");
1125                 uiButSetFunc(bt, node_dynamic_update_cb, ntree, node);
1126
1127                 if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) {
1128                         // UI_ThemeColor(TH_REDALERT);
1129                         // XXX ui_rasterpos_safe(butr->xmin + xoff, butr->ymin + 5, snode->aspect);
1130                         // XXX snode_drawstring(snode, "Error! Check console...", butr->xmax - butr->xmin);
1131                         ;
1132                 }
1133         }
1134 }
1135
1136 /* only once called */
1137 static void node_shader_set_butfunc(bNodeType *ntype)
1138 {
1139         ntype->uifuncbut = NULL;
1140         switch (ntype->type) {
1141                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1142
1143                 case SH_NODE_MATERIAL:
1144                 case SH_NODE_MATERIAL_EXT:
1145                         ntype->uifunc= node_shader_buts_material;
1146                         break;
1147                 case SH_NODE_TEXTURE:
1148                         ntype->uifunc= node_buts_texture;
1149                         break;
1150                 case SH_NODE_NORMAL:
1151                         ntype->uifunc= node_buts_normal;
1152                         break;
1153                 case SH_NODE_CURVE_VEC:
1154                         ntype->uifunc= node_buts_curvevec;
1155                         break;
1156                 case SH_NODE_CURVE_RGB:
1157                         ntype->uifunc= node_buts_curvecol;
1158                         break;
1159                 case SH_NODE_MAPPING:
1160                         ntype->uifunc= node_shader_buts_mapping;
1161                         break;
1162                 case SH_NODE_VALUE:
1163                         ntype->uifunc= node_buts_value;
1164                         break;
1165                 case SH_NODE_RGB:
1166                         ntype->uifunc= node_buts_rgb;
1167                         break;
1168                 case SH_NODE_MIX_RGB:
1169                         ntype->uifunc= node_buts_mix_rgb;
1170                         break;
1171                 case SH_NODE_VALTORGB:
1172                         ntype->uifunc= node_buts_colorramp;
1173                         break;
1174                 case SH_NODE_MATH: 
1175                         ntype->uifunc= node_buts_math;
1176                         break; 
1177                 case SH_NODE_VECT_MATH: 
1178                         ntype->uifunc= node_shader_buts_vect_math;
1179                         break; 
1180                 case SH_NODE_GEOMETRY:
1181                         ntype->uifunc= node_shader_buts_geometry;
1182                         break;
1183                 case SH_NODE_ATTRIBUTE:
1184                         ntype->uifunc= node_shader_buts_attribute;
1185                         break;
1186                 case SH_NODE_TEX_SKY:
1187                         ntype->uifunc= node_shader_buts_tex_sky;
1188                         break;
1189                 case SH_NODE_TEX_IMAGE:
1190                         ntype->uifunc= node_shader_buts_tex_image;
1191                         break;
1192                 case SH_NODE_TEX_ENVIRONMENT:
1193                         ntype->uifunc= node_shader_buts_tex_environment;
1194                         break;
1195                 case SH_NODE_TEX_GRADIENT:
1196                         ntype->uifunc= node_shader_buts_tex_gradient;
1197                         break;
1198                 case SH_NODE_TEX_MAGIC:
1199                         ntype->uifunc= node_shader_buts_tex_magic;
1200                         break;
1201                 case SH_NODE_TEX_WAVE:
1202                         ntype->uifunc= node_shader_buts_tex_wave;
1203                         break;
1204                 case SH_NODE_TEX_MUSGRAVE:
1205                         ntype->uifunc= node_shader_buts_tex_musgrave;
1206                         break;
1207                 case SH_NODE_TEX_VORONOI:
1208                         ntype->uifunc= node_shader_buts_tex_voronoi;
1209                         break;
1210                 case SH_NODE_BSDF_GLOSSY:
1211                 case SH_NODE_BSDF_GLASS:
1212                         ntype->uifunc= node_shader_buts_glossy;
1213                         break;
1214                 case NODE_DYNAMIC:
1215                         ntype->uifunc= node_shader_buts_dynamic;
1216                         break;
1217         }
1218                 if (ntype->uifuncbut == NULL) ntype->uifuncbut = ntype->uifunc;
1219 }
1220
1221 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
1222
1223 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1224 {
1225         uiLayout *col;
1226         bNode *node= ptr->data;
1227         PointerRNA imaptr;
1228         PropertyRNA *prop;
1229         int source;
1230         
1231         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1232         
1233         if (!node->id) return;
1234         
1235         prop = RNA_struct_find_property(ptr, "image");
1236         if (!prop || RNA_property_type(prop) != PROP_POINTER) return;
1237         imaptr= RNA_property_pointer_get(ptr, prop);
1238         
1239         col= uiLayoutColumn(layout, 0);
1240         
1241         uiItemR(col, &imaptr, "source", 0, NULL, ICON_NONE);
1242         
1243         source= RNA_enum_get(&imaptr, "source");
1244
1245         if (source == IMA_SRC_SEQUENCE) {
1246                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
1247                 Scene *scene= CTX_data_scene(C);
1248                 ImageUser *iuser= node->storage;
1249                 char numstr[32];
1250                 const int framenr= BKE_image_user_frame_get(iuser, CFRA, 0);
1251                 BLI_snprintf(numstr, sizeof(numstr), "Frame: %d", framenr);
1252                 uiItemL(layout, numstr, ICON_NONE);
1253         }
1254
1255         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1256                 col= uiLayoutColumn(layout, 1);
1257                 uiItemR(col, ptr, "frame_duration", 0, NULL, ICON_NONE);
1258                 uiItemR(col, ptr, "frame_start", 0, NULL, ICON_NONE);
1259                 uiItemR(col, ptr, "frame_offset", 0, NULL, ICON_NONE);
1260                 uiItemR(col, ptr, "use_cyclic", 0, NULL, ICON_NONE);
1261                 uiItemR(col, ptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
1262         }
1263
1264         col= uiLayoutColumn(layout, 0);
1265         
1266         if (RNA_enum_get(&imaptr, "type")== IMA_TYPE_MULTILAYER)
1267                 uiItemR(col, ptr, "layer", 0, NULL, ICON_NONE);
1268 }
1269
1270 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
1271 {
1272         bNode *node= ptr->data;
1273         uiLayout *col, *row;
1274         PointerRNA op_ptr;
1275         PointerRNA scn_ptr;
1276         PropertyRNA *prop;
1277         const char *layer_name;
1278         char scene_name[MAX_ID_NAME-2];
1279         wmOperatorType *ot = WM_operatortype_find("RENDER_OT_render", 1);
1280
1281         BLI_assert(ot != 0);
1282
1283         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
1284         
1285         if (!node->id) return;
1286
1287         col= uiLayoutColumn(layout, 0);
1288         row = uiLayoutRow(col, 0);
1289         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
1290         
1291         prop = RNA_struct_find_property(ptr, "layer");
1292         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
1293                 return;
1294         
1295         scn_ptr = RNA_pointer_get(ptr, "scene");
1296         RNA_string_get(&scn_ptr, "name", scene_name);
1297         
1298         WM_operator_properties_create_ptr(&op_ptr, ot);
1299         RNA_string_set(&op_ptr, "layer", layer_name);
1300         RNA_string_set(&op_ptr, "scene", scene_name);
1301         uiItemFullO_ptr(row, ot, "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
1302
1303 }
1304
1305
1306 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1307 {
1308         uiLayout *col, *row;
1309         
1310         col= uiLayoutColumn(layout, 0);
1311         
1312         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
1313         if (RNA_enum_get(ptr, "filter_type")!= R_FILTER_FAST_GAUSS) {
1314                 uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
1315                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1316         }
1317         
1318         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
1319         
1320         if (RNA_boolean_get(ptr, "use_relative")) {
1321                 uiItemL(col, "Aspect Correction", 0);
1322                 row= uiLayoutRow(layout, 1);
1323                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, 0);
1324                 
1325                 col= uiLayoutColumn(layout, 1);
1326                 uiItemR(col, ptr, "factor_x", 0, "X", ICON_NONE);
1327                 uiItemR(col, ptr, "factor_y", 0, "Y", ICON_NONE);
1328         }
1329         else {
1330                 col= uiLayoutColumn(layout, 1);
1331                 uiItemR(col, ptr, "size_x", 0, "X", ICON_NONE);
1332                 uiItemR(col, ptr, "size_y", 0, "Y", ICON_NONE);
1333         }
1334 }
1335
1336 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1337 {
1338         uiLayout *col;
1339         
1340         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1341         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
1342         
1343         col= uiLayoutColumn(layout, 1);
1344         uiItemL(col, "Center:", ICON_NONE);
1345         uiItemR(col, ptr, "center_x", 0, "X", ICON_NONE);
1346         uiItemR(col, ptr, "center_y", 0, "Y", ICON_NONE);
1347         
1348         uiItemS(layout);
1349         
1350         col= uiLayoutColumn(layout, 1);
1351         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
1352         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1353         
1354         uiItemS(layout);
1355         
1356         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
1357         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
1358 }
1359
1360 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1361 {       
1362         uiLayout *col;
1363         
1364         col= uiLayoutColumn(layout, 1);
1365         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
1366         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
1367         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
1368 }
1369
1370 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1371 {
1372         uiLayout *sub, *col;
1373         
1374         col= uiLayoutColumn(layout, 0);
1375         uiItemL(col, "Bokeh Type:", ICON_NONE);
1376         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
1377         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1378
1379         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1380
1381         col = uiLayoutColumn(layout, 0);
1382         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer")==1);
1383         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
1384
1385         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
1386         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1387
1388         col = uiLayoutColumn(layout, 0);
1389         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
1390         sub = uiLayoutColumn(col, 0);
1391         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_preview"));
1392         uiItemR(sub, ptr, "samples", 0, NULL, ICON_NONE);
1393         
1394         col = uiLayoutColumn(layout, 0);
1395         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
1396         sub = uiLayoutColumn(col, 0);
1397         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer")==0);
1398         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
1399 }
1400
1401 /* qdn: glare node */
1402 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1403 {       
1404         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
1405         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
1406
1407         if (RNA_enum_get(ptr, "glare_type")!= 1) {
1408                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1409         
1410                 if (RNA_enum_get(ptr, "glare_type")!= 0) 
1411                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1412         }
1413         
1414         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
1415         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1416
1417         if (RNA_enum_get(ptr, "glare_type")== 2) {
1418                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
1419                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
1420         }
1421         if (RNA_enum_get(ptr, "glare_type")== 0 || RNA_enum_get(ptr, "glare_type")== 2) {
1422                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1423                 
1424                 if (RNA_enum_get(ptr, "glare_type")== 0) 
1425                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
1426         }
1427         if (RNA_enum_get(ptr, "glare_type")== 1) {
1428                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
1429         }
1430 }
1431
1432 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1433 {       
1434         uiLayout *col;
1435
1436         col = uiLayoutColumn(layout, 0);
1437         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
1438         if (RNA_enum_get(ptr, "tonemap_type")== 0) {
1439                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1440                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1441                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
1442         }
1443         else {
1444                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
1445                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1446                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1447                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1448         }
1449 }
1450
1451 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1452 {
1453         uiLayout *col;
1454
1455         col= uiLayoutColumn(layout, 0);
1456         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
1457
1458         col = uiLayoutColumn(col, 0);
1459         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector")==0);
1460         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
1461         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
1462 }
1463
1464 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1465 {
1466         uiLayout *col;
1467         
1468         col= uiLayoutColumn(layout, 0);
1469         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
1470         uiItemR(col, ptr, "factor", 0, "Blur", ICON_NONE);
1471         
1472         col= uiLayoutColumn(layout, 1);
1473         uiItemL(col, "Speed:", ICON_NONE);
1474         uiItemR(col, ptr, "speed_min", 0, "Min", ICON_NONE);
1475         uiItemR(col, ptr, "speed_max", 0, "Max", ICON_NONE);
1476
1477         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
1478 }
1479
1480 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1481 {
1482         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1483 }
1484
1485 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1486 {
1487         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1488 }
1489
1490 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1491 {
1492         uiLayout *col;
1493
1494         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
1495         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
1496
1497         col= uiLayoutColumn(layout, 1);
1498         if (RNA_boolean_get(ptr, "relative")) {
1499                 uiItemR(col, ptr, "rel_min_x", 0, "Left", ICON_NONE);
1500                 uiItemR(col, ptr, "rel_max_x", 0, "Right", ICON_NONE);
1501                 uiItemR(col, ptr, "rel_min_y", 0, "Up", ICON_NONE);
1502                 uiItemR(col, ptr, "rel_max_y", 0, "Down", ICON_NONE);
1503         }
1504         else {
1505                 uiItemR(col, ptr, "min_x", 0, "Left", ICON_NONE);
1506                 uiItemR(col, ptr, "max_x", 0, "Right", ICON_NONE);
1507                 uiItemR(col, ptr, "min_y", 0, "Up", ICON_NONE);
1508                 uiItemR(col, ptr, "max_y", 0, "Down", ICON_NONE);
1509         }
1510 }
1511
1512 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1513 {
1514         uiLayout *row, *col;
1515         
1516         col= uiLayoutColumn(layout, 0);
1517         row= uiLayoutRow(col, 0);
1518         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1519         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
1520 }
1521
1522 static void node_composit_buts_double_edge_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1523 {
1524         uiLayout *col;
1525
1526         col= uiLayoutColumn(layout, 0);
1527
1528         uiItemL(col, "Inner Edge:", ICON_NONE);
1529         uiItemR(col, ptr, "inner_mode", 0, "", ICON_NONE);
1530         uiItemL(col, "Buffer Edge:", ICON_NONE);
1531         uiItemR(col, ptr, "edge_mode", 0, "", ICON_NONE);
1532 }
1533
1534 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1535 {
1536         uiLayout *sub, *col;
1537         
1538         col = uiLayoutColumn(layout, 1);
1539         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1540         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
1541         
1542         col = uiLayoutColumn(layout, 1);
1543         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
1544         sub = uiLayoutColumn(col, 0);
1545         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1546         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
1547         
1548         col = uiLayoutColumn(layout, 1);
1549         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
1550         sub = uiLayoutColumn(col, 0);
1551         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1552         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
1553 }
1554
1555 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1556 {       
1557         uiLayout *col;
1558         
1559         col = uiLayoutColumn(layout, 1);
1560         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
1561         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
1562 }
1563
1564 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1565 {       
1566         uiLayout *col;
1567         
1568         col = uiLayoutColumn(layout, 1);
1569         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
1570 }
1571
1572
1573 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1574 {
1575         uiLayout *col;
1576         
1577         col = uiLayoutColumn(layout, 0);
1578         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1579         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1580         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1581 }
1582
1583 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1584 {
1585         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1586 }
1587
1588 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1589 {
1590         uiLayout *col;
1591         
1592         col = uiLayoutColumn(layout, 1);
1593         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1594         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1595 }
1596
1597 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1598 {
1599         uiLayout *col, *row;
1600         
1601         col = uiLayoutColumn(layout, 1);
1602    
1603         uiItemL(layout, "Color Space:", ICON_NONE);
1604         row= uiLayoutRow(layout, 0);
1605         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1606
1607         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1608         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1609 }
1610
1611 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1612 {
1613         uiLayout *row, *col;
1614         
1615         uiItemL(layout, "Despill Channel:", ICON_NONE);
1616         row = uiLayoutRow(layout, 0);
1617         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1618
1619         col= uiLayoutColumn(layout, 0);
1620         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1621
1622         if (RNA_enum_get(ptr, "limit_method")==0) {
1623                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1624                 row=uiLayoutRow(col, 0);
1625                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1626         }
1627
1628         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1629         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1630         if (RNA_boolean_get(ptr, "use_unspill")== 1) {
1631                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1632                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1633                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1634         }
1635 }
1636
1637 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1638 {
1639         uiLayout *col;
1640         
1641         col= uiLayoutColumn(layout, 0);
1642         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1643         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1644         
1645         col= uiLayoutColumn(layout, 1);
1646         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1647         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1648         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1649 }
1650
1651 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1652 {
1653         uiLayout *col;
1654         
1655         col= uiLayoutColumn(layout, 1);
1656         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1657         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1658         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1659 }
1660
1661 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1662 {       
1663         uiLayout *col, *row;
1664
1665         uiItemL(layout, "Color Space:", ICON_NONE);
1666         row= uiLayoutRow(layout, 0);
1667         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1668
1669         col=uiLayoutColumn(layout, 0);
1670         uiItemL(col, "Key Channel:", ICON_NONE);
1671         row= uiLayoutRow(col, 0);
1672         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1673
1674         col = uiLayoutColumn(layout, 0);
1675
1676         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1677         if (RNA_enum_get(ptr, "limit_method")==0) {
1678                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1679                 row=uiLayoutRow(col, 0);
1680                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1681         }
1682
1683         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1684         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1685 }
1686
1687 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1688 {
1689         uiLayout *col;
1690         
1691         col= uiLayoutColumn(layout, 1);
1692         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1693         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1694 }
1695
1696 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1697 {
1698         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1699 }
1700
1701 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1702 {
1703         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1704         uiItemR(layout, ptr, "use_smooth_mask", 0, NULL, ICON_NONE);
1705 }
1706
1707 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1708 static void node_draw_input_file_output(const bContext *C, uiBlock *block,
1709                                          bNodeTree *ntree, bNode *node, bNodeSocket *sock,
1710                                          const char *UNUSED(name), int x, int y, int width)
1711 {
1712         NodeImageMultiFileSocket *input = sock->storage;
1713         uiLayout *layout, *row;
1714         PointerRNA nodeptr, inputptr, imfptr;
1715         int imtype;
1716         int rx, ry;
1717         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
1718         RNA_pointer_create(&ntree->id, &RNA_NodeImageFileSocket, input, &inputptr);
1719         
1720         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, x, y+NODE_DY, width, 20, UI_GetStyle());
1721         row = uiLayoutRow(layout, 0);           
1722         
1723         uiItemL(row, input->path, 0);
1724         
1725         imfptr = RNA_pointer_get(&nodeptr, "format");
1726         imtype = RNA_enum_get(&imfptr, "file_format");
1727         /* in multilayer format all socket format details are ignored */
1728         if (imtype != R_IMF_IMTYPE_MULTILAYER) {
1729                 PropertyRNA *imtype_prop;
1730                 const char *imtype_name;
1731                 
1732                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1733                         imfptr = RNA_pointer_get(&inputptr, "format");
1734                 
1735                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1736                 RNA_property_enum_name((bContext*)C, &imfptr, imtype_prop, RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1737                 uiBlockSetEmboss(block, UI_EMBOSSP);
1738                 uiItemL(row, imtype_name, 0);
1739                 uiBlockSetEmboss(block, UI_EMBOSSN);
1740         }
1741         
1742         uiBlockLayoutResolve(block, &rx, &ry);
1743 }
1744 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1745 {
1746         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1747         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1748         
1749         if (multilayer)
1750                 uiItemL(layout, "Path:", 0);
1751         else
1752                 uiItemL(layout, "Base Path:", 0);
1753         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1754 }
1755 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1756 {
1757         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1758         PointerRNA active_input_ptr, op_ptr;
1759         uiLayout *row;
1760         int active_index;
1761         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1762         
1763         node_composit_buts_file_output(layout, C, ptr);
1764         uiTemplateImageSettings(layout, &imfptr);
1765         
1766         uiItemS(layout);
1767         
1768         uiItemO(layout, "Add Input", ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
1769         
1770         uiTemplateList(layout, C, ptr, "file_inputs", ptr, "active_input_index", NULL, 0, 0, 0);
1771         
1772         active_index = RNA_int_get(ptr, "active_input_index");
1773         RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_inputs"), active_index, &active_input_ptr);
1774         
1775         row = uiLayoutRow(layout, 1);
1776         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1777         RNA_enum_set(&op_ptr, "direction", 1);
1778         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1779         RNA_enum_set(&op_ptr, "direction", 2);
1780         
1781         if (active_input_ptr.data) {
1782                 uiLayout *row, *col;
1783                 
1784                 col = uiLayoutColumn(layout, 1);
1785                 if (multilayer)
1786                         uiItemL(col, "Layer Name:", 0);
1787                 else
1788                         uiItemL(col, "File Path:", 0);
1789                 row = uiLayoutRow(col, 0);
1790                 uiItemR(row, &active_input_ptr, "path", 0, "", 0);
1791                 uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
1792                 
1793                 /* in multilayer format all socket format details are ignored */
1794                 if (!multilayer) {
1795                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
1796                         
1797                         col = uiLayoutColumn(layout, 1);
1798                         uiItemL(col, "Format:", 0);
1799                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, 0);
1800                         
1801                         col= uiLayoutColumn(layout, 0);
1802                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format")==0);
1803                         uiTemplateImageSettings(col, &imfptr);
1804                 }
1805         }
1806 }
1807
1808 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1809 {
1810         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
1811 }
1812
1813 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1814 {
1815         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1816 }
1817
1818 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1819 {
1820         uiLayout *col;
1821         
1822         col= uiLayoutColumn(layout, 0);
1823         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
1824         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
1825 }
1826
1827 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1828 {
1829         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
1830 }
1831
1832 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1833 {
1834         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1835 }
1836
1837 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1838 {
1839         uiLayout *split, *col, *row;
1840         
1841         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1842         
1843         if (RNA_enum_get(ptr, "correction_method")== 0) {
1844         
1845                 split = uiLayoutSplit(layout, 0, 0);
1846                 col = uiLayoutColumn(split, 0);
1847                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
1848                 row = uiLayoutRow(col, 0);
1849                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
1850                 
1851                 col = uiLayoutColumn(split, 0);
1852                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
1853                 row = uiLayoutRow(col, 0);
1854                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
1855                 
1856                 col = uiLayoutColumn(split, 0);
1857                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
1858                 row = uiLayoutRow(col, 0);
1859                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
1860
1861         }
1862         else {
1863                 
1864                 split = uiLayoutSplit(layout, 0, 0);
1865                 col = uiLayoutColumn(split, 0);
1866                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
1867                 row = uiLayoutRow(col, 0);
1868                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
1869                 
1870                 col = uiLayoutColumn(split, 0);
1871                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
1872                 row = uiLayoutRow(col, 0);
1873                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
1874                 
1875                 col = uiLayoutColumn(split, 0);
1876                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
1877                 row = uiLayoutRow(col, 0);
1878                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
1879         }
1880
1881 }
1882 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1883 {
1884         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1885
1886         if (RNA_enum_get(ptr, "correction_method")== 0) {
1887
1888         uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
1889                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
1890
1891                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
1892                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
1893
1894                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
1895                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
1896         }
1897         else {
1898                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
1899                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
1900
1901                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
1902                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
1903
1904                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
1905                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
1906         }
1907 }
1908
1909
1910 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1911 {
1912         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
1913 }
1914
1915 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1916
1917         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
1918 }
1919
1920 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
1921 {
1922         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1923 }
1924
1925 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
1926 {
1927         bNode *node= ptr->data;
1928
1929         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1930
1931         if (!node->id)
1932                 return;
1933
1934         uiItemR(layout, ptr, "filter_type", 0, "", 0);
1935 }
1936
1937 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1938 {
1939         uiItemR(layout, ptr, "filter_type", 0, "", 0);
1940 }
1941
1942 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
1943 {
1944         bNode *node= ptr->data;
1945
1946         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1947
1948         if (!node->id)
1949                 return;
1950
1951         uiItemR(layout, ptr, "distortion_type", 0, "", 0);
1952 }
1953
1954 static void node_composit_buts_mask(uiLayout *layout, bContext *C, PointerRNA *ptr)
1955 {
1956         uiTemplateID(layout, C, ptr, "mask", NULL, NULL, NULL);
1957 }
1958
1959 /* only once called */
1960 static void node_composit_set_butfunc(bNodeType *ntype)
1961 {
1962         ntype->uifuncbut = NULL;
1963         switch (ntype->type) {
1964                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1965
1966                 case CMP_NODE_IMAGE:
1967                         ntype->uifunc= node_composit_buts_image;
1968                         break;
1969                 case CMP_NODE_R_LAYERS:
1970                         ntype->uifunc= node_composit_buts_renderlayers;
1971                         break;
1972                 case CMP_NODE_NORMAL:
1973                         ntype->uifunc= node_buts_normal;
1974                         break;
1975                 case CMP_NODE_CURVE_VEC:
1976                         ntype->uifunc= node_buts_curvevec;
1977                         break;
1978                 case CMP_NODE_CURVE_RGB:
1979                         ntype->uifunc= node_buts_curvecol;
1980                         break;
1981                 case CMP_NODE_VALUE:
1982                         ntype->uifunc= node_buts_value;
1983                         break;
1984                 case CMP_NODE_RGB:
1985                         ntype->uifunc= node_buts_rgb;
1986                         break;
1987                 case CMP_NODE_FLIP:
1988                         ntype->uifunc= node_composit_buts_flip;
1989                         break;
1990                 case CMP_NODE_SPLITVIEWER:
1991                         ntype->uifunc= node_composit_buts_splitviewer;
1992                         break;
1993                 case CMP_NODE_MIX_RGB:
1994                         ntype->uifunc= node_buts_mix_rgb;
1995                         break;
1996                 case CMP_NODE_VALTORGB:
1997                         ntype->uifunc= node_buts_colorramp;
1998                         break;
1999                 case CMP_NODE_CROP:
2000                         ntype->uifunc= node_composit_buts_crop;
2001                         break;
2002                 case CMP_NODE_BLUR:
2003                         ntype->uifunc= node_composit_buts_blur;
2004                         break;
2005                 case CMP_NODE_DBLUR:
2006                         ntype->uifunc= node_composit_buts_dblur;
2007                         break;
2008                 case CMP_NODE_BILATERALBLUR:
2009                         ntype->uifunc= node_composit_buts_bilateralblur;
2010                         break;
2011                 case CMP_NODE_DEFOCUS:
2012                         ntype->uifunc = node_composit_buts_defocus;
2013                         break;
2014                 case CMP_NODE_GLARE:
2015                         ntype->uifunc = node_composit_buts_glare;
2016                         break;
2017                 case CMP_NODE_TONEMAP:
2018                         ntype->uifunc = node_composit_buts_tonemap;
2019                         break;
2020                 case CMP_NODE_LENSDIST:
2021                         ntype->uifunc = node_composit_buts_lensdist;
2022                         break;
2023                 case CMP_NODE_VECBLUR:
2024                         ntype->uifunc= node_composit_buts_vecblur;
2025                         break;
2026                 case CMP_NODE_FILTER:
2027                         ntype->uifunc= node_composit_buts_filter;
2028                         break;
2029                 case CMP_NODE_MAP_VALUE:
2030                         ntype->uifunc= node_composit_buts_map_value;
2031                         break;
2032                 case CMP_NODE_TIME:
2033                         ntype->uifunc= node_buts_time;
2034                         break;
2035                 case CMP_NODE_ALPHAOVER:
2036                         ntype->uifunc= node_composit_buts_alphaover;
2037                         break;
2038                 case CMP_NODE_HUE_SAT:
2039                         ntype->uifunc= node_composit_buts_hue_sat;
2040                         break;
2041                 case CMP_NODE_TEXTURE:
2042                         ntype->uifunc= node_buts_texture;
2043                         break;
2044                 case CMP_NODE_DILATEERODE:
2045                         ntype->uifunc= node_composit_buts_dilateerode;
2046                         break;
2047                 case CMP_NODE_OUTPUT_FILE:
2048                         ntype->uifunc= node_composit_buts_file_output;
2049                         ntype->uifuncbut= node_composit_buts_file_output_details;
2050                         ntype->drawinputfunc = node_draw_input_file_output;
2051                         break;
2052                 case CMP_NODE_DIFF_MATTE:
2053                         ntype->uifunc=node_composit_buts_diff_matte;
2054                         break;
2055                 case CMP_NODE_DIST_MATTE:
2056                         ntype->uifunc=node_composit_buts_distance_matte;
2057                         break;
2058                 case CMP_NODE_COLOR_SPILL:
2059                         ntype->uifunc=node_composit_buts_color_spill;
2060                         break;
2061                 case CMP_NODE_CHROMA_MATTE:
2062                         ntype->uifunc=node_composit_buts_chroma_matte;
2063                         break;
2064                 case CMP_NODE_COLOR_MATTE:
2065                         ntype->uifunc=node_composit_buts_color_matte;
2066                         break;
2067                 case CMP_NODE_SCALE:
2068                         ntype->uifunc= node_composit_buts_scale;
2069                         break;
2070                 case CMP_NODE_ROTATE:
2071                         ntype->uifunc=node_composit_buts_rotate;
2072                         break;
2073                 case CMP_NODE_CHANNEL_MATTE:
2074                         ntype->uifunc= node_composit_buts_channel_matte;
2075                         break;
2076                 case CMP_NODE_LUMA_MATTE:
2077                         ntype->uifunc= node_composit_buts_luma_matte;
2078                         break;
2079                 case CMP_NODE_MAP_UV:
2080                         ntype->uifunc= node_composit_buts_map_uv;
2081                         break;
2082                 case CMP_NODE_ID_MASK:
2083                         ntype->uifunc= node_composit_buts_id_mask;
2084                         break;
2085                 case CMP_NODE_DOUBLEEDGEMASK:
2086                         ntype->uifunc= node_composit_buts_double_edge_mask;
2087                         break;
2088                 case CMP_NODE_MATH:
2089                         ntype->uifunc= node_buts_math;
2090                         break;
2091                 case CMP_NODE_INVERT:
2092                         ntype->uifunc= node_composit_buts_invert;
2093                         break;
2094                 case CMP_NODE_PREMULKEY:
2095                         ntype->uifunc= node_composit_buts_premulkey;
2096                         break;
2097                 case CMP_NODE_VIEW_LEVELS:
2098                         ntype->uifunc=node_composit_buts_view_levels;
2099                         break;
2100                 case CMP_NODE_COLORBALANCE:
2101                         ntype->uifunc=node_composit_buts_colorbalance;
2102                         ntype->uifuncbut=node_composit_buts_colorbalance_but;
2103                         break;
2104                 case CMP_NODE_HUECORRECT:
2105                         ntype->uifunc=node_composit_buts_huecorrect;
2106                         break;
2107                 case CMP_NODE_ZCOMBINE:
2108                         ntype->uifunc=node_composit_buts_zcombine;
2109                         break;
2110                 case CMP_NODE_COMBYCCA:
2111                 case CMP_NODE_SEPYCCA:
2112                         ntype->uifunc=node_composit_buts_ycc;
2113                         break;
2114                 case CMP_NODE_MOVIECLIP:
2115                         ntype->uifunc= node_composit_buts_movieclip;
2116                         break;
2117                 case CMP_NODE_STABILIZE2D:
2118                         ntype->uifunc= node_composit_buts_stabilize2d;
2119                         break;
2120                 case CMP_NODE_TRANSFORM:
2121                         ntype->uifunc= node_composit_buts_transform;
2122                         break;
2123                 case CMP_NODE_MOVIEDISTORTION:
2124                         ntype->uifunc= node_composit_buts_moviedistortion;
2125                         break;
2126                 case CMP_NODE_MASK:
2127                         ntype->uifunc= node_composit_buts_mask;
2128                         break;
2129                 default:
2130                         ntype->uifunc= NULL;
2131         }
2132         if (ntype->uifuncbut == NULL) ntype->uifuncbut = ntype->uifunc;
2133
2134 }
2135
2136 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2137
2138 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2139 {
2140         uiLayout *col;
2141         
2142         col= uiLayoutColumn(layout, 1);
2143         uiItemR(col, ptr, "offset", 0, "Offset", ICON_NONE);
2144         uiItemR(col, ptr, "offset_frequency", 0, "Frequency", ICON_NONE);
2145         
2146         col= uiLayoutColumn(layout, 1);
2147         uiItemR(col, ptr, "squash", 0, "Squash", ICON_NONE);
2148         uiItemR(col, ptr, "squash_frequency", 0, "Frequency", ICON_NONE);
2149 }
2150
2151 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2152 {
2153         PointerRNA tex_ptr;
2154         bNode *node= ptr->data;
2155         ID *id= ptr->id.data;
2156         Tex *tex = (Tex *)node->storage;
2157         uiLayout *col, *row;
2158         
2159         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2160
2161         col= uiLayoutColumn(layout, 0);
2162
2163         switch ( tex->type ) {
2164                 case TEX_BLEND:
2165                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2166                         row= uiLayoutRow(col, 0);
2167                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2168                         break;
2169
2170                 case TEX_MARBLE:
2171                         row= uiLayoutRow(col, 0);
2172                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2173                         row= uiLayoutRow(col, 0);
2174                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2175                         row= uiLayoutRow(col, 0);
2176                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2177                         row= uiLayoutRow(col, 0);
2178                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2179                         break;
2180
2181                 case TEX_MAGIC:
2182                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2183                         break;
2184
2185                 case TEX_STUCCI:
2186                         row= uiLayoutRow(col, 0);
2187                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2188                         row= uiLayoutRow(col, 0);
2189                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2190                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2191                         break;
2192
2193                 case TEX_WOOD:
2194                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2195                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2196                         row= uiLayoutRow(col, 0);
2197                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2198                         row= uiLayoutRow(col, 0);
2199                         uiLayoutSetActive(row, !(RNA_enum_get(&tex_ptr, "wood_type")==TEX_BAND || RNA_enum_get(&tex_ptr, "wood_type")==TEX_RING)); 
2200                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2201                         break;
2202                         
2203                 case TEX_CLOUDS:
2204                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2205                         row= uiLayoutRow(col, 0);
2206                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2207                         row= uiLayoutRow(col, 0);
2208                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2209                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, "Depth", ICON_NONE);
2210                         break;
2211                         
2212                 case TEX_DISTNOISE:
2213                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2214                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2215                         break;
2216
2217                 case TEX_MUSGRAVE:
2218                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2219                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2220                         break;
2221                 case TEX_VORONOI:
2222                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2223                         if (tex->vn_distm == TEX_MINKOVSKY) {
2224                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2225                         }
2226                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2227                         break;
2228         }
2229 }
2230
2231 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2232 {
2233         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2234 }
2235
2236 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2237 {
2238         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2239 }
2240
2241 /* only once called */
2242 static void node_texture_set_butfunc(bNodeType *ntype)
2243 {
2244         ntype->uifuncbut = NULL;
2245         if ( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
2246                 ntype->uifunc = node_texture_buts_proc;
2247         }
2248         else {
2249                 switch (ntype->type) {
2250
2251                 case TEX_NODE_MATH:
2252                         ntype->uifunc = node_buts_math;
2253                         break;
2254
2255                 case TEX_NODE_MIX_RGB:
2256                         ntype->uifunc = node_buts_mix_rgb;
2257                         break;
2258
2259                 case TEX_NODE_VALTORGB:
2260                         ntype->uifunc = node_buts_colorramp;
2261                         break;
2262
2263                 case TEX_NODE_CURVE_RGB:
2264                         ntype->uifunc= node_buts_curvecol;
2265                         break;
2266
2267                 case TEX_NODE_CURVE_TIME:
2268                         ntype->uifunc = node_buts_time;
2269                         break;
2270
2271                 case TEX_NODE_TEXTURE:
2272                         ntype->uifunc = node_buts_texture;
2273                         break;
2274
2275                 case TEX_NODE_BRICKS:
2276                         ntype->uifunc = node_texture_buts_bricks;
2277                         break;
2278
2279                 case TEX_NODE_IMAGE:
2280                         ntype->uifunc = node_texture_buts_image;
2281                         break;
2282
2283                 case TEX_NODE_OUTPUT:
2284                         ntype->uifunc = node_texture_buts_output;
2285                         break;
2286                 }
2287         }
2288
2289         if (ntype->uifuncbut == NULL) {
2290                 ntype->uifuncbut = ntype->uifunc;
2291         }
2292 }
2293
2294 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2295
2296 void ED_init_node_butfuncs(void)
2297 {
2298         bNodeTreeType *treetype;
2299         bNodeType *ntype;
2300         bNodeSocketType *stype;
2301         int i;
2302         
2303         /* node type ui functions */
2304         for (i=0; i < NUM_NTREE_TYPES; ++i) {
2305                 treetype = ntreeGetType(i);
2306                 if (treetype) {
2307                         for (ntype= treetype->node_types.first; ntype; ntype= ntype->next) {
2308                                 /* default ui functions */
2309                                 ntype->drawfunc = node_draw_default;
2310                                 ntype->drawupdatefunc = node_update_default;
2311                                 ntype->uifunc = NULL;
2312                                 ntype->uifuncbut = NULL;
2313                                 ntype->drawinputfunc = node_draw_input_default;
2314                                 ntype->drawoutputfunc = node_draw_output_default;
2315                                 ntype->resize_area_func = node_resize_area_default;
2316                                 
2317                                 node_common_set_butfunc(ntype);
2318                                 
2319                                 switch (i) {
2320                                 case NTREE_COMPOSIT:
2321                                         node_composit_set_butfunc(ntype);
2322                                         break;
2323                                 case NTREE_SHADER:
2324                                         node_shader_set_butfunc(ntype);
2325                                         break;
2326                                 case NTREE_TEXTURE:
2327                                         node_texture_set_butfunc(ntype);
2328                                         break;
2329                                 }
2330                         }
2331                 }
2332         }
2333         
2334         /* socket type ui functions */
2335         for (i=0; i < NUM_SOCKET_TYPES; ++i) {
2336                 stype = ntreeGetSocketType(i);
2337                 if (stype) {
2338                         switch (stype->type) {
2339                         case SOCK_FLOAT:
2340                         case SOCK_INT:
2341                         case SOCK_BOOLEAN:
2342                                 stype->buttonfunc = node_socket_button_default;
2343                                 break;
2344                         case SOCK_VECTOR:
2345                                 stype->buttonfunc = node_socket_button_components;
2346                                 break;
2347                         case SOCK_RGBA:
2348                                 stype->buttonfunc = node_socket_button_color;
2349                                 break;
2350                         case SOCK_SHADER:
2351                                 stype->buttonfunc = node_socket_button_label;
2352                                 break;
2353                         default:
2354                                 stype->buttonfunc = NULL;
2355                         }
2356                 }
2357         }
2358 }
2359
2360 /* ************** Generic drawing ************** */
2361
2362 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
2363 {
2364         
2365         if ((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2366                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2367                 void *lock;
2368                 ImBuf *ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
2369                 if (ibuf) {
2370                         float x, y; 
2371                         
2372                         glMatrixMode(GL_PROJECTION);
2373                         glPushMatrix();
2374                         glMatrixMode(GL_MODELVIEW);
2375                         glPushMatrix();
2376
2377                         /* keep this, saves us from a version patch */
2378                         if (snode->zoom==0.0f) snode->zoom= 1.0f;
2379                         
2380                         /* somehow the offset has to be calculated inverse */
2381                         
2382                         glaDefine2DArea(&ar->winrct);
2383                         /* ortho at pixel level curarea */
2384                         wmOrtho2(-0.375, ar->winx-0.375, -0.375, ar->winy-0.375);
2385                         
2386                         x = (ar->winx-snode->zoom*ibuf->x)/2 + snode->xof;
2387                         y = (ar->winy-snode->zoom*ibuf->y)/2 + snode->yof;
2388                         
2389                         if (!ibuf->rect) {
2390                                 if (color_manage)
2391                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
2392                                 else
2393                                         ibuf->profile = IB_PROFILE_NONE;
2394                                 IMB_rect_from_float(ibuf);
2395                         }
2396
2397                         if (ibuf->rect) {
2398                                 if (snode->flag & SNODE_SHOW_ALPHA) {
2399                                         glPixelZoom(snode->zoom, snode->zoom);
2400                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2401                                         if (ENDIAN_ORDER == B_ENDIAN)
2402                                                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
2403                                         
2404                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
2405                                         
2406                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
2407                                         glPixelZoom(1.0f, 1.0f);
2408                                 }
2409                                 else if (snode->flag & SNODE_USE_ALPHA) {
2410                                         glEnable(GL_BLEND);
2411                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2412                                         glPixelZoom(snode->zoom, snode->zoom);
2413                                         
2414                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2415                                         
2416                                         glPixelZoom(1.0f, 1.0f);
2417                                         glDisable(GL_BLEND);
2418                                 }
2419                                 else {
2420                                         glPixelZoom(snode->zoom, snode->zoom);
2421                                         
2422                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2423                                         
2424                                         glPixelZoom(1.0f, 1.0f);
2425                                 }
2426                         }
2427                         
2428                         glMatrixMode(GL_PROJECTION);
2429                         glPopMatrix();
2430                         glMatrixMode(GL_MODELVIEW);
2431                         glPopMatrix();
2432                 }
2433
2434                 BKE_image_release_ibuf(ima, lock);
2435         }
2436 }
2437
2438 #if 0
2439 /* note: needs to be userpref or opengl profile option */
2440 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2441 {
2442
2443         draw_nodespace_grid(snode);
2444         
2445         if (snode->flag & SNODE_BACKDRAW) {
2446                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2447                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2448                 if (ibuf) {
2449                         int x, y;
2450                         float zoom = 1.0;
2451
2452                         glMatrixMode(GL_PROJECTION);
2453                         glPushMatrix();
2454                         glMatrixMode(GL_MODELVIEW);
2455                         glPushMatrix();
2456                         
2457                         glaDefine2DArea(&sa->winrct);
2458
2459                         if (ibuf->x > sa->winx || ibuf->y > sa->winy) {
2460                                 float zoomx, zoomy;
2461                                 zoomx= (float)sa->winx/ibuf->x;
2462                                 zoomy= (float)sa->winy/ibuf->y;
2463                                 zoom = MIN2(zoomx, zoomy);
2464                         }
2465                         
2466                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2467                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2468
2469                         glPixelZoom(zoom, zoom);
2470
2471                         glColor4f(1.0, 1.0, 1.0, 1.0);
2472                         if (ibuf->rect)
2473                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2474                         else if (ibuf->channels==4)
2475                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2476
2477                         glPixelZoom(1.0, 1.0);
2478
2479                         glMatrixMode(GL_PROJECTION);
2480                         glPopMatrix();
2481                         glMatrixMode(GL_MODELVIEW);
2482                         glPopMatrix();
2483                 }
2484         }
2485 }
2486 #endif
2487
2488 /* if v2d not NULL, it clips and returns 0 if not visible */
2489 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
2490 {
2491         float dist, vec[4][2];
2492         
2493         /* in v0 and v3 we put begin/end points */
2494         if (link->fromsock) {
2495                 vec[0][0]= link->fromsock->locx;
2496                 vec[0][1]= link->fromsock->locy;
2497         }
2498         else {
2499                 if (snode==NULL) return 0;
2500                 vec[0][0]= snode->mx;
2501                 vec[0][1]= snode->my;
2502         }
2503         if (link->tosock) {
2504                 vec[3][0]= link->tosock->locx;
2505                 vec[3][1]= link->tosock->locy;
2506         }
2507         else {
2508                 if (snode==NULL) return 0;
2509                 vec[3][0]= snode->mx;
2510                 vec[3][1]= snode->my;
2511         }
2512
2513         dist= UI_GetThemeValue(TH_NODE_CURVING)*0.10f*ABS(vec[0][0] - vec[3][0]);
2514         
2515         /* check direction later, for top sockets */
2516         vec[1][0]= vec[0][0]+dist;
2517         vec[1][1]= vec[0][1];
2518         
2519         vec[2][0]= vec[3][0]-dist;
2520         vec[2][1]= vec[3][1];
2521         
2522         if (v2d && MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax); /* clipped */
2523         else if (v2d && MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin); /* clipped */
2524         else {
2525                 
2526                 /* always do all three, to prevent data hanging around */
2527                 BKE_curve_forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2528                 BKE_curve_forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
2529                 
2530                 return 1;
2531         }
2532         return 0;
2533 }
2534
2535 #define LINK_RESOL      24
2536 void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, int th_col1, int do_shaded, int th_col2, int do_triple, int th_col3 )
2537 {
2538         float coord_array[LINK_RESOL+1][2];
2539         
2540         if (node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
2541                 float dist, spline_step = 0.0f;
2542                 int i;
2543                 
2544                 /* store current linewidth */
2545                 float linew;
2546                 glGetFloatv(GL_LINE_WIDTH, &linew);
2547                 
2548                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2549                 dist = 1.0f/(float)LINK_RESOL;
2550                 
2551                 glEnable(GL_LINE_SMOOTH);
2552                 
2553                 if (do_triple) {
2554                         UI_ThemeColorShadeAlpha(th_col3, -80, -120);
2555                         glLineWidth(4.0f);
2556                         
2557                         glBegin(GL_LINE_STRIP);
2558                         for (i=0; i<=LINK_RESOL; i++) {
2559                                 glVertex2fv(coord_array[i]);
2560                         }
2561                         glEnd();
2562                 }
2563                 
2564                 /* XXX using GL_LINES for shaded node lines is a workaround
2565                  * for Intel hardware, this breaks with GL_LINE_STRIP and
2566                  * changing color in begin/end blocks.
2567                  */
2568                 glLineWidth(1.5f);
2569                 if (do_shaded) {
2570                         glBegin(GL_LINES);
2571                         for (i=0; i<LINK_RESOL; i++) {
2572                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step);
2573                                 glVertex2fv(coord_array[i]);
2574                                 
2575                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step+dist);
2576                                 glVertex2fv(coord_array[i+1]);
2577                                 
2578                                 spline_step += dist;
2579                         }
2580                         glEnd();
2581                 }
2582                 else {
2583                         UI_ThemeColor(th_col1);
2584                         glBegin(GL_LINE_STRIP);
2585                         for (i=0; i<=LINK_RESOL; i++) {
2586                                 glVertex2fv(coord_array[i]);
2587                         }
2588                         glEnd();
2589                 }
2590                 
2591                 glDisable(GL_LINE_SMOOTH);
2592                 
2593                 /* restore previuos linewidth */
2594                 glLineWidth(linew);
2595         }
2596 }
2597
2598 static void node_link_straight_points(View2D *UNUSED(v2d), SpaceNode *snode, bNodeLink *link, float coord_array[][2])
2599 {
2600         if (link->fromsock) {
2601                 coord_array[0][0]= link->fromsock->locx;
2602                 coord_array[0][1]= link->fromsock->locy;
2603         }
2604         else {
2605                 if (snode==NULL) return;
2606                 coord_array[0][0]= snode->mx;
2607                 coord_array[0][1]= snode->my;
2608         }
2609         if (link->tosock) {
2610                 coord_array[1][0]= link->tosock->locx;
2611                 coord_array[1][1]= link->tosock->locy;
2612         }
2613         else {
2614                 if (snode==NULL) return;
2615                 coord_array[1][0]= snode->mx;
2616                 coord_array[1][1]= snode->my;
2617         }
2618 }
2619
2620 void node_draw_link_straight(View2D *v2d, SpaceNode *snode, bNodeLink *link, int th_col1, int do_shaded, int th_col2, int do_triple, int th_col3 )
2621 {
2622         float coord_array[2][2];
2623         float linew;
2624         int i;
2625         
2626         node_link_straight_points(v2d, snode, link, coord_array);
2627         
2628         /* store current linewidth */
2629         glGetFloatv(GL_LINE_WIDTH, &linew);
2630         
2631         glEnable(GL_LINE_SMOOTH);
2632         
2633         if (do_triple) {
2634                 UI_ThemeColorShadeAlpha(th_col3, -80, -120);
2635                 glLineWidth(4.0f);
2636                 
2637                 glBegin(GL_LINES);
2638                 glVertex2fv(coord_array[0]);
2639                 glVertex2fv(coord_array[1]);
2640                 glEnd();
2641         }
2642         
2643         UI_ThemeColor(th_col1);
2644         glLineWidth(1.5f);
2645         
2646         /* XXX using GL_LINES for shaded node lines is a workaround
2647          * for Intel hardware, this breaks with GL_LINE_STRIP and
2648          * changing color in begin/end blocks.
2649          */
2650         if (do_shaded) {
2651                 glBegin(GL_LINES);
2652                 for (i=0; i < LINK_RESOL-1; ++i) {
2653                         float t= (float)i/(float)(LINK_RESOL-1);
2654                         UI_ThemeColorBlend(th_col1, th_col2, t);
2655                         glVertex2f((1.0f-t)*coord_array[0][0]+t*coord_array[1][0], (1.0f-t)*coord_array[0][1]+t*coord_array[1][1]);
2656                         
2657                         t= (float)(i+1)/(float)(LINK_RESOL-1);
2658                         UI_ThemeColorBlend(th_col1, th_col2, t);
2659                         glVertex2f((1.0f-t)*coord_array[0][0]+t*coord_array[1][0], (1.0f-t)*coord_array[0][1]+t*coord_array[1][1]);
2660                 }
2661                 glEnd();
2662         }
2663         else {
2664                 glBegin(GL_LINE_STRIP);
2665                 for (i=0; i < LINK_RESOL; ++i) {
2666                         float t= (float)i/(float)(LINK_RESOL-1);
2667                         glVertex2f((1.0f-t)*coord_array[0][0]+t*coord_array[1][0], (1.0f-t)*coord_array[0][1]+t*coord_array[1][1]);
2668                 }
2669                 glEnd();
2670         }
2671         
2672         glDisable(GL_LINE_SMOOTH);
2673         
2674         /* restore previuos linewidth */
2675         glLineWidth(linew);
2676 }
2677
2678 /* note; this is used for fake links in groups too */
2679 void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link)
2680 {
2681         int do_shaded= 0, th_col1= TH_HEADER, th_col2= TH_HEADER;
2682         int do_triple= 0, th_col3= TH_WIRE;
2683         
2684         if (link->fromsock==NULL && link->tosock==NULL)
2685                 return;
2686         
2687         /* new connection */
2688         if (!link->fromsock || !link->tosock) {
2689                 th_col1 = TH_ACTIVE;
2690                 do_triple = 1;
2691         }
2692         else {
2693                 /* going to give issues once... */
2694                 if (link->tosock->flag & SOCK_UNAVAIL)
2695                         return;
2696                 if (link->fromsock->flag & SOCK_UNAVAIL)
2697                         return;
2698                 
2699                 /* a bit ugly... but thats how we detect the internal group links */
2700                 if (!link->fromnode || !link->tonode) {
2701                         UI_ThemeColorBlend(TH_BACK, TH_WIRE, 0.5f);
2702                         do_shaded= 0;
2703                 }
2704                 else {
2705                         /* check cyclic */
2706                         if ((link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) && (link->flag & NODE_LINK_VALID)) {
2707                                 /* special indicated link, on drop-node */
2708                                 if (link->flag & NODE_LINKFLAG_HILITE) {
2709                                         th_col1= th_col2= TH_ACTIVE;
2710                                 }
2711                                 else {
2712                                         /* regular link */
2713                                         if (link->fromnode->flag & SELECT)
2714                                                 th_col1= TH_EDGE_SELECT;
2715                                         if (link->tonode->flag & SELECT)
2716                                                 th_col2= TH_EDGE_SELECT;
2717                                 }
2718                                 do_shaded= 1;
2719                                 do_triple= 1;
2720                         }                               
2721                         else {
2722                                 th_col1 = TH_REDALERT;
2723                         }
2724                 }
2725         }
2726         
2727         node_draw_link_bezier(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
2728 //      node_draw_link_straight(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
2729 }