UI mafia fix thanks to venomgfx for reporting
[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") == TRUE);
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") == FALSE);
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") == FALSE);
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, "type", 0, NULL, ICON_NONE);
1586         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1587         if (RNA_enum_get(ptr, "type") == CMP_NODE_DILATEERODE_DISTANCE) {
1588                 uiItemR(layout, ptr, "edge", 0, NULL, ICON_NONE);
1589         }
1590 }
1591
1592 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1593 {
1594         uiLayout *col;
1595         
1596         col = uiLayoutColumn(layout, 1);
1597         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1598         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1599 }
1600
1601 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1602 {
1603         uiLayout *col, *row;
1604         
1605         col = uiLayoutColumn(layout, 1);
1606    
1607         uiItemL(layout, "Color Space:", ICON_NONE);
1608         row= uiLayoutRow(layout, 0);
1609         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1610
1611         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1612         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1613 }
1614
1615 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1616 {
1617         uiLayout *row, *col;
1618         
1619         uiItemL(layout, "Despill Channel:", ICON_NONE);
1620         row = uiLayoutRow(layout, 0);
1621         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1622
1623         col= uiLayoutColumn(layout, 0);
1624         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1625
1626         if (RNA_enum_get(ptr, "limit_method")==0) {
1627                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1628                 row=uiLayoutRow(col, 0);
1629                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1630         }
1631
1632         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1633         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1634         if (RNA_boolean_get(ptr, "use_unspill") == TRUE) {
1635                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1636                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1637                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1638         }
1639 }
1640
1641 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1642 {
1643         uiLayout *col;
1644         
1645         col= uiLayoutColumn(layout, 0);
1646         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1647         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1648         
1649         col= uiLayoutColumn(layout, 1);
1650         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1651         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1652         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1653 }
1654
1655 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1656 {
1657         uiLayout *col;
1658         
1659         col= uiLayoutColumn(layout, 1);
1660         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1661         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1662         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1663 }
1664
1665 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1666 {       
1667         uiLayout *col, *row;
1668
1669         uiItemL(layout, "Color Space:", ICON_NONE);
1670         row= uiLayoutRow(layout, 0);
1671         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1672
1673         col=uiLayoutColumn(layout, 0);
1674         uiItemL(col, "Key Channel:", ICON_NONE);
1675         row= uiLayoutRow(col, 0);
1676         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1677
1678         col = uiLayoutColumn(layout, 0);
1679
1680         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1681         if (RNA_enum_get(ptr, "limit_method")==0) {
1682                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1683                 row=uiLayoutRow(col, 0);
1684                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1685         }
1686
1687         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1688         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1689 }
1690
1691 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1692 {
1693         uiLayout *col;
1694         
1695         col= uiLayoutColumn(layout, 1);
1696         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1697         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1698 }
1699
1700 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1701 {
1702         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1703 }
1704
1705 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1706 {
1707         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1708         uiItemR(layout, ptr, "use_smooth_mask", 0, NULL, ICON_NONE);
1709 }
1710
1711 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1712 static void node_draw_input_file_output(const bContext *C, uiBlock *block,
1713                                          bNodeTree *ntree, bNode *node, bNodeSocket *sock,
1714                                          const char *UNUSED(name), int x, int y, int width)
1715 {
1716         uiLayout *layout, *row;
1717         PointerRNA nodeptr, inputptr, imfptr;
1718         int imtype;
1719         int rx, ry;
1720         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
1721         
1722         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, x, y+NODE_DY, width, 20, UI_GetStyle());
1723         row = uiLayoutRow(layout, 0);
1724         
1725         imfptr = RNA_pointer_get(&nodeptr, "format");
1726         imtype = RNA_enum_get(&imfptr, "file_format");
1727         if (imtype == R_IMF_IMTYPE_MULTILAYER) {
1728                 NodeImageMultiFileSocket *input = sock->storage;
1729                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotLayer, input, &inputptr);
1730                 
1731                 uiItemL(row, input->layer, 0);
1732         }
1733         else {
1734                 NodeImageMultiFileSocket *input = sock->storage;
1735                 PropertyRNA *imtype_prop;
1736                 const char *imtype_name;
1737                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotFile, input, &inputptr);
1738                 
1739                 uiItemL(row, input->path, 0);
1740                 
1741                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1742                         imfptr = RNA_pointer_get(&inputptr, "format");
1743                 
1744                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1745                 RNA_property_enum_name((bContext*)C, &imfptr, imtype_prop, RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1746                 uiBlockSetEmboss(block, UI_EMBOSSP);
1747                 uiItemL(row, imtype_name, 0);
1748                 uiBlockSetEmboss(block, UI_EMBOSSN);
1749         }
1750         
1751         uiBlockLayoutResolve(block, &rx, &ry);
1752 }
1753 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1754 {
1755         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1756         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1757         
1758         if (multilayer)
1759                 uiItemL(layout, "Path:", 0);
1760         else
1761                 uiItemL(layout, "Base Path:", 0);
1762         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1763 }
1764 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1765 {
1766         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1767         PointerRNA active_input_ptr, op_ptr;
1768         uiLayout *row;
1769         int active_index;
1770         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1771         
1772         node_composit_buts_file_output(layout, C, ptr);
1773         uiTemplateImageSettings(layout, &imfptr);
1774         
1775         uiItemS(layout);
1776         
1777         uiItemO(layout, "Add Input", ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
1778         
1779         active_index = RNA_int_get(ptr, "active_input_index");
1780         /* using different collection properties if multilayer format is enabled */
1781         if (multilayer) {
1782                 uiTemplateList(layout, C, ptr, "layer_slots", ptr, "active_input_index", NULL, 0, 0, 0);
1783                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "layer_slots"), active_index, &active_input_ptr);
1784         }
1785         else {
1786                 uiTemplateList(layout, C, ptr, "file_slots", ptr, "active_input_index", NULL, 0, 0, 0);
1787                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_slots"), active_index, &active_input_ptr);
1788         }
1789         /* XXX collection lookup does not return the ID part of the pointer, setting this manually here */
1790         active_input_ptr.id.data = ptr->id.data;
1791         
1792         row = uiLayoutRow(layout, 1);
1793         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1794         RNA_enum_set(&op_ptr, "direction", 1);
1795         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1796         RNA_enum_set(&op_ptr, "direction", 2);
1797         
1798         if (active_input_ptr.data) {
1799                 if (multilayer) {
1800                         uiLayout *row, *col;
1801                         col = uiLayoutColumn(layout, 1);
1802                         
1803                         uiItemL(col, "Layer:", 0);
1804                         row = uiLayoutRow(col, 0);
1805                         uiItemR(row, &active_input_ptr, "name", 0, "", 0);
1806                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
1807                 }
1808                 else {
1809                         uiLayout *row, *col;
1810                         col = uiLayoutColumn(layout, 1);
1811                         
1812                         uiItemL(col, "File Path:", 0);
1813                         row = uiLayoutRow(col, 0);
1814                         uiItemR(row, &active_input_ptr, "path", 0, "", 0);
1815                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
1816                         
1817                         /* format details for individual files */
1818                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
1819                         
1820                         col = uiLayoutColumn(layout, 1);
1821                         uiItemL(col, "Format:", 0);
1822                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, 0);
1823                         
1824                         col= uiLayoutColumn(layout, 0);
1825                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format") == FALSE);
1826                         uiTemplateImageSettings(col, &imfptr);
1827                 }
1828         }
1829 }
1830
1831 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1832 {
1833         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
1834 }
1835
1836 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1837 {
1838         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1839 }
1840
1841 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1842 {
1843         uiLayout *col;
1844         
1845         col= uiLayoutColumn(layout, 0);
1846         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
1847         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
1848 }
1849
1850 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1851 {
1852         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
1853 }
1854
1855 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1856 {
1857         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1858 }
1859
1860 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1861 {
1862         uiLayout *split, *col, *row;
1863         
1864         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1865         
1866         if (RNA_enum_get(ptr, "correction_method")== 0) {
1867         
1868                 split = uiLayoutSplit(layout, 0, 0);
1869                 col = uiLayoutColumn(split, 0);
1870                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
1871                 row = uiLayoutRow(col, 0);
1872                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
1873                 
1874                 col = uiLayoutColumn(split, 0);
1875                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
1876                 row = uiLayoutRow(col, 0);
1877                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
1878                 
1879                 col = uiLayoutColumn(split, 0);
1880                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
1881                 row = uiLayoutRow(col, 0);
1882                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
1883
1884         }
1885         else {
1886                 
1887                 split = uiLayoutSplit(layout, 0, 0);
1888                 col = uiLayoutColumn(split, 0);
1889                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
1890                 row = uiLayoutRow(col, 0);
1891                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
1892                 
1893                 col = uiLayoutColumn(split, 0);
1894                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
1895                 row = uiLayoutRow(col, 0);
1896                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
1897                 
1898                 col = uiLayoutColumn(split, 0);
1899                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
1900                 row = uiLayoutRow(col, 0);
1901                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
1902         }
1903
1904 }
1905 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1906 {
1907         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1908
1909         if (RNA_enum_get(ptr, "correction_method")== 0) {
1910
1911         uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
1912                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
1913
1914                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
1915                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
1916
1917                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
1918                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
1919         }
1920         else {
1921                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
1922                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
1923
1924                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
1925                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
1926
1927                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
1928                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
1929         }
1930 }
1931
1932
1933 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1934 {
1935         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
1936 }
1937
1938 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1939
1940         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
1941 }
1942
1943 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
1944 {
1945         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1946 }
1947
1948 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
1949 {
1950         bNode *node= ptr->data;
1951
1952         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1953
1954         if (!node->id)
1955                 return;
1956
1957         uiItemR(layout, ptr, "filter_type", 0, "", 0);
1958 }
1959
1960 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1961 {
1962         uiItemR(layout, ptr, "filter_type", 0, "", 0);
1963 }
1964
1965 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
1966 {
1967         bNode *node= ptr->data;
1968
1969         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1970
1971         if (!node->id)
1972                 return;
1973
1974         uiItemR(layout, ptr, "distortion_type", 0, "", 0);
1975 }
1976
1977 static void node_composit_buts_colorcorrection(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1978 {
1979         uiLayout *row;
1980         
1981         row = uiLayoutRow(layout, 0);
1982         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
1983         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
1984         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
1985
1986         row = uiLayoutRow(layout, 0);
1987         uiItemL(row, "", 0);
1988         uiItemL(row, "Saturation", 0);
1989         uiItemL(row, "Contrast", 0);
1990         uiItemL(row, "Gamma", 0);
1991         uiItemL(row, "Gain", 0);
1992         uiItemL(row, "Lift", 0);
1993
1994         row = uiLayoutRow(layout, 0);
1995         uiItemL(row, "Master", 0);
1996         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
1997         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
1998         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
1999         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2000         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2001
2002         row = uiLayoutRow(layout, 0);
2003         uiItemL(row, "Highlights", 0);
2004         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2005         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2006         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2007         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2008         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2009
2010         row = uiLayoutRow(layout, 0);
2011         uiItemL(row, "Midtones", 0);
2012         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2013         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2014         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2015         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2016         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2017
2018         row = uiLayoutRow(layout, 0);
2019         uiItemL(row, "Shadows", 0);
2020         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2021         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2022         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2023         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2024         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2025
2026         row = uiLayoutRow(layout, 0);
2027         uiItemR(row, ptr, "midtones_start", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2028         uiItemR(row, ptr, "midtones_end", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2029 }
2030
2031 static void node_composit_buts_colorcorrection_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) {
2032         uiLayout *row;
2033         
2034         row = uiLayoutRow(layout, 0);
2035         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2036         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2037         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2038         row = layout;
2039         uiItemL(row, "Saturation", 0);
2040         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2041         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2042         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2043         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2044
2045         uiItemL(row, "Contrast", 0);
2046         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2047         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2048         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2049         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2050
2051         uiItemL(row, "Gamma", 0);
2052         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2053         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2054         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2055         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2056
2057         uiItemL(row, "Gain", 0);
2058         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2059         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2060         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2061         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2062         
2063         uiItemL(row, "Lift", 0);
2064         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2065         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2066         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2067         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2068
2069         row = uiLayoutRow(layout, 0);
2070         uiItemR(row, ptr, "midtones_start", 0, NULL, ICON_NONE);
2071         uiItemR(row, ptr, "midtones_end", 0, NULL, ICON_NONE);
2072 }
2073
2074 static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2075 {
2076         uiItemR(layout, ptr, "check", 0, NULL, ICON_NONE);
2077 }
2078
2079 static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2080 {
2081         uiLayout *row;
2082         
2083         row= uiLayoutRow(layout, 1);
2084         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2085         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2086         
2087         row= uiLayoutRow(layout, 1);
2088         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2089         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2090
2091         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2092         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2093 }
2094
2095 static void node_composit_buts_bokehimage(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2096 {
2097         uiItemR(layout, ptr, "flaps", 0, NULL, ICON_NONE);
2098         uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
2099         uiItemR(layout, ptr, "rounding", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2100         uiItemR(layout, ptr, "catadioptric", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2101         uiItemR(layout, ptr, "shift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2102 }
2103
2104 void node_composit_backdrop_viewer(SpaceNode* snode, ImBuf* backdrop, bNode* node, int x, int y)
2105 {
2106 //      node_composit_backdrop_canvas(snode, backdrop, node, x, y);
2107         if (node->custom1 == 0) { /// @todo: why did we need this one?
2108                 const float backdropWidth = backdrop->x;
2109                 const float backdropHeight = backdrop->y;
2110                 const float cx  = x+snode->zoom*backdropWidth*node->custom3;
2111                 const float cy = y+snode->zoom*backdropHeight*node->custom4;
2112
2113                 glColor3f(1.0, 1.0, 1.0);
2114
2115                 glBegin(GL_LINES);
2116                 glVertex2f(cx-25, cy-25);
2117                 glVertex2f(cx+25, cy+25);
2118                 glVertex2f(cx+25, cy-25);
2119                 glVertex2f(cx-25, cy+25);
2120                 glEnd();
2121         }
2122 }
2123
2124 void node_composit_backdrop_boxmask(SpaceNode* snode, ImBuf* backdrop, bNode* node, int x, int y)
2125 {
2126         NodeBoxMask * boxmask = node->storage;
2127         const float backdropWidth = backdrop->x;
2128         const float backdropHeight = backdrop->y;
2129         const float aspect = backdropWidth/backdropHeight;
2130         const float rad = DEG2RAD(-boxmask->rotation);
2131         const float cosine = cos(rad);
2132         const float sine = sin(rad);
2133         const float halveBoxWidth = backdropWidth*(boxmask->width/2.0f);
2134         const float halveBoxHeight = backdropHeight*(boxmask->height/2.0f)*aspect;
2135
2136         float cx, cy, x1, x2, x3, x4;
2137         float y1, y2, y3, y4;
2138
2139
2140         /* keep this, saves us from a version patch */
2141         if(snode->zoom==0.0f) snode->zoom= 1.0f;
2142
2143         glColor3f(1.0, 1.0, 1.0);
2144
2145         cx  = x+snode->zoom*backdropWidth*boxmask->x;
2146         cy = y+snode->zoom*backdropHeight*boxmask->y;
2147
2148         x1 = cx - (cosine*halveBoxWidth+sine*halveBoxHeight)*snode->zoom;
2149         x2 = cx - (cosine*-halveBoxWidth+sine*halveBoxHeight)*snode->zoom;
2150         x3 = cx - (cosine*-halveBoxWidth+sine*-halveBoxHeight)*snode->zoom;
2151         x4 = cx - (cosine*halveBoxWidth+sine*-halveBoxHeight)*snode->zoom;
2152         y1 = cy - (-sine*halveBoxWidth + cosine*halveBoxHeight)*snode->zoom;
2153         y2 = cy - (-sine*-halveBoxWidth + cosine*halveBoxHeight)*snode->zoom;
2154         y3 = cy - (-sine*-halveBoxWidth + cosine*-halveBoxHeight)*snode->zoom;
2155         y4 = cy - (-sine*halveBoxWidth + cosine*-halveBoxHeight)*snode->zoom;
2156
2157         glBegin(GL_LINE_LOOP);
2158         glVertex2f(x1, y1);
2159         glVertex2f(x2, y2);
2160         glVertex2f(x3, y3);
2161         glVertex2f(x4, y4);
2162         glEnd();
2163 }
2164
2165 void node_composit_backdrop_ellipsemask(SpaceNode* snode, ImBuf* backdrop, bNode* node, int x, int y)
2166 {
2167         NodeEllipseMask * ellipsemask = node->storage;
2168         const float backdropWidth = backdrop->x;
2169         const float backdropHeight = backdrop->y;
2170         const float aspect = backdropWidth/backdropHeight;
2171         const float rad = DEG2RAD(-ellipsemask->rotation);
2172         const float cosine = cos(rad);
2173         const float sine = sin(rad);
2174         const float halveBoxWidth = backdropWidth*(ellipsemask->width/2.0f);
2175         const float halveBoxHeight = backdropHeight*(ellipsemask->height/2.0f)*aspect;
2176
2177         float cx, cy, x1, x2, x3, x4;
2178         float y1, y2, y3, y4;
2179
2180
2181         /* keep this, saves us from a version patch */
2182         if(snode->zoom==0.0f) snode->zoom= 1.0f;
2183
2184         glColor3f(1.0, 1.0, 1.0);
2185
2186         cx  = x+snode->zoom*backdropWidth*ellipsemask->x;
2187         cy = y+snode->zoom*backdropHeight*ellipsemask->y;
2188
2189         x1 = cx - (cosine*halveBoxWidth+sine*halveBoxHeight)*snode->zoom;
2190         x2 = cx - (cosine*-halveBoxWidth+sine*halveBoxHeight)*snode->zoom;
2191         x3 = cx - (cosine*-halveBoxWidth+sine*-halveBoxHeight)*snode->zoom;
2192         x4 = cx - (cosine*halveBoxWidth+sine*-halveBoxHeight)*snode->zoom;
2193         y1 = cy - (-sine*halveBoxWidth + cosine*halveBoxHeight)*snode->zoom;
2194         y2 = cy - (-sine*-halveBoxWidth + cosine*halveBoxHeight)*snode->zoom;
2195         y3 = cy - (-sine*-halveBoxWidth + cosine*-halveBoxHeight)*snode->zoom;
2196         y4 = cy - (-sine*halveBoxWidth + cosine*-halveBoxHeight)*snode->zoom;
2197
2198         glBegin(GL_LINE_LOOP);
2199
2200         glVertex2f(x1, y1);
2201         glVertex2f(x2, y2);
2202         glVertex2f(x3, y3);
2203         glVertex2f(x4, y4);
2204         glEnd();
2205 }
2206
2207 static void node_composit_buts_ellipsemask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2208 {
2209         uiLayout *row;
2210         row= uiLayoutRow(layout, 1);
2211         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2212         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2213         row= uiLayoutRow(layout, 1);
2214         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2215         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2216
2217         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2218         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2219 }
2220
2221 static void node_composit_buts_viewer_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2222 {
2223         uiLayout *col;
2224         
2225         uiItemR(layout, ptr, "tile_order", 0, NULL, ICON_NONE);
2226         if (RNA_enum_get(ptr, "tile_order")==0) {
2227                 col= uiLayoutColumn(layout, 1);
2228                 uiItemR(col, ptr, "center_x", 0, NULL, ICON_NONE);
2229                 uiItemR(col, ptr, "center_y", 0, NULL, ICON_NONE);
2230         }
2231 }
2232
2233 /* only once called */
2234 static void node_composit_set_butfunc(bNodeType *ntype)
2235 {
2236         ntype->uifuncbut = NULL;
2237         switch (ntype->type) {
2238                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2239
2240                 case CMP_NODE_IMAGE:
2241                         ntype->uifunc= node_composit_buts_image;
2242                         break;
2243                 case CMP_NODE_R_LAYERS:
2244                         ntype->uifunc= node_composit_buts_renderlayers;
2245                         break;
2246                 case CMP_NODE_NORMAL:
2247                         ntype->uifunc= node_buts_normal;
2248                         break;
2249                 case CMP_NODE_CURVE_VEC:
2250                         ntype->uifunc= node_buts_curvevec;
2251                         break;
2252                 case CMP_NODE_CURVE_RGB:
2253                         ntype->uifunc= node_buts_curvecol;
2254                         break;
2255                 case CMP_NODE_VALUE:
2256                         ntype->uifunc= node_buts_value;
2257                         break;
2258                 case CMP_NODE_RGB:
2259                         ntype->uifunc= node_buts_rgb;
2260                         break;
2261                 case CMP_NODE_FLIP:
2262                         ntype->uifunc= node_composit_buts_flip;
2263                         break;
2264                 case CMP_NODE_SPLITVIEWER:
2265                         ntype->uifunc= node_composit_buts_splitviewer;
2266                         break;
2267                 case CMP_NODE_MIX_RGB:
2268                         ntype->uifunc= node_buts_mix_rgb;
2269                         break;
2270                 case CMP_NODE_VALTORGB:
2271                         ntype->uifunc= node_buts_colorramp;
2272                         break;
2273                 case CMP_NODE_CROP:
2274                         ntype->uifunc= node_composit_buts_crop;
2275                         break;
2276                 case CMP_NODE_BLUR:
2277                         ntype->uifunc= node_composit_buts_blur;
2278                         break;
2279                 case CMP_NODE_DBLUR:
2280                         ntype->uifunc= node_composit_buts_dblur;
2281                         break;
2282                 case CMP_NODE_BILATERALBLUR:
2283                         ntype->uifunc= node_composit_buts_bilateralblur;
2284                         break;
2285                 case CMP_NODE_DEFOCUS:
2286                         ntype->uifunc = node_composit_buts_defocus;
2287                         break;
2288                 case CMP_NODE_GLARE:
2289                         ntype->uifunc = node_composit_buts_glare;
2290                         break;
2291                 case CMP_NODE_TONEMAP:
2292                         ntype->uifunc = node_composit_buts_tonemap;
2293                         break;
2294                 case CMP_NODE_LENSDIST:
2295                         ntype->uifunc = node_composit_buts_lensdist;
2296                         break;
2297                 case CMP_NODE_VECBLUR:
2298                         ntype->uifunc= node_composit_buts_vecblur;
2299                         break;
2300                 case CMP_NODE_FILTER:
2301                         ntype->uifunc= node_composit_buts_filter;
2302                         break;
2303                 case CMP_NODE_MAP_VALUE:
2304                         ntype->uifunc= node_composit_buts_map_value;
2305                         break;
2306                 case CMP_NODE_TIME:
2307                         ntype->uifunc= node_buts_time;
2308                         break;
2309                 case CMP_NODE_ALPHAOVER:
2310                         ntype->uifunc= node_composit_buts_alphaover;
2311                         break;
2312                 case CMP_NODE_HUE_SAT:
2313                         ntype->uifunc= node_composit_buts_hue_sat;
2314                         break;
2315                 case CMP_NODE_TEXTURE:
2316                         ntype->uifunc= node_buts_texture;
2317                         break;
2318                 case CMP_NODE_DILATEERODE:
2319                         ntype->uifunc= node_composit_buts_dilateerode;
2320                         break;
2321                 case CMP_NODE_OUTPUT_FILE:
2322                         ntype->uifunc= node_composit_buts_file_output;
2323                         ntype->uifuncbut= node_composit_buts_file_output_details;
2324                         ntype->drawinputfunc = node_draw_input_file_output;
2325                         break;
2326                 case CMP_NODE_DIFF_MATTE:
2327                         ntype->uifunc=node_composit_buts_diff_matte;
2328                         break;
2329                 case CMP_NODE_DIST_MATTE:
2330                         ntype->uifunc=node_composit_buts_distance_matte;
2331                         break;
2332                 case CMP_NODE_COLOR_SPILL:
2333                         ntype->uifunc=node_composit_buts_color_spill;
2334                         break;
2335                 case CMP_NODE_CHROMA_MATTE:
2336                         ntype->uifunc=node_composit_buts_chroma_matte;
2337                         break;
2338                 case CMP_NODE_COLOR_MATTE:
2339                         ntype->uifunc=node_composit_buts_color_matte;
2340                         break;
2341                 case CMP_NODE_SCALE:
2342                         ntype->uifunc= node_composit_buts_scale;
2343                         break;
2344                 case CMP_NODE_ROTATE:
2345                         ntype->uifunc=node_composit_buts_rotate;
2346                         break;
2347                 case CMP_NODE_CHANNEL_MATTE:
2348                         ntype->uifunc= node_composit_buts_channel_matte;
2349                         break;
2350                 case CMP_NODE_LUMA_MATTE:
2351                         ntype->uifunc= node_composit_buts_luma_matte;
2352                         break;
2353                 case CMP_NODE_MAP_UV:
2354                         ntype->uifunc= node_composit_buts_map_uv;
2355                         break;
2356                 case CMP_NODE_ID_MASK:
2357                         ntype->uifunc= node_composit_buts_id_mask;
2358                         break;
2359                 case CMP_NODE_DOUBLEEDGEMASK:
2360                         ntype->uifunc= node_composit_buts_double_edge_mask;
2361                         break;
2362                 case CMP_NODE_MATH:
2363                         ntype->uifunc= node_buts_math;
2364                         break;
2365                 case CMP_NODE_INVERT:
2366                         ntype->uifunc= node_composit_buts_invert;
2367                         break;
2368                 case CMP_NODE_PREMULKEY:
2369                         ntype->uifunc= node_composit_buts_premulkey;
2370                         break;
2371                 case CMP_NODE_VIEW_LEVELS:
2372                         ntype->uifunc=node_composit_buts_view_levels;
2373                         break;
2374                 case CMP_NODE_COLORBALANCE:
2375                         ntype->uifunc=node_composit_buts_colorbalance;
2376                         ntype->uifuncbut=node_composit_buts_colorbalance_but;
2377                         break;
2378                 case CMP_NODE_HUECORRECT:
2379                         ntype->uifunc=node_composit_buts_huecorrect;
2380                         break;
2381                 case CMP_NODE_ZCOMBINE:
2382                         ntype->uifunc=node_composit_buts_zcombine;
2383                         break;
2384                 case CMP_NODE_COMBYCCA:
2385                 case CMP_NODE_SEPYCCA:
2386                         ntype->uifunc=node_composit_buts_ycc;
2387                         break;
2388                 case CMP_NODE_MOVIECLIP:
2389                         ntype->uifunc= node_composit_buts_movieclip;
2390                         break;
2391                 case CMP_NODE_STABILIZE2D:
2392                         ntype->uifunc= node_composit_buts_stabilize2d;
2393                         break;
2394                 case CMP_NODE_TRANSFORM:
2395                         ntype->uifunc= node_composit_buts_transform;
2396                         break;
2397                 case CMP_NODE_MOVIEDISTORTION:
2398                         ntype->uifunc= node_composit_buts_moviedistortion;
2399                         break;
2400                 case CMP_NODE_COLORCORRECTION:
2401                         ntype->uifunc=node_composit_buts_colorcorrection;
2402                         ntype->uifuncbut=node_composit_buts_colorcorrection_but;
2403                         break;
2404                 case CMP_NODE_SWITCH:
2405                         ntype->uifunc= node_composit_buts_switch;
2406                         break;
2407                 case CMP_NODE_MASK_BOX:
2408                         ntype->uifunc= node_composit_buts_boxmask;
2409                         ntype->uibackdropfunc = node_composit_backdrop_boxmask;
2410                         break;
2411                 case CMP_NODE_MASK_ELLIPSE:
2412                         ntype->uifunc= node_composit_buts_ellipsemask;
2413                         ntype->uibackdropfunc = node_composit_backdrop_ellipsemask;
2414                         break;
2415                 case CMP_NODE_BOKEHIMAGE:
2416                         ntype->uifunc= node_composit_buts_bokehimage;
2417                         break;
2418                 case CMP_NODE_VIEWER:
2419                         ntype->uifunc = NULL;
2420                         ntype->uifuncbut= node_composit_buts_viewer_but;
2421                         ntype->uibackdropfunc = node_composit_backdrop_viewer;
2422                         break;
2423
2424                 default:
2425                         ntype->uifunc= NULL;
2426         }
2427         if (ntype->uifuncbut == NULL) ntype->uifuncbut = ntype->uifunc;
2428
2429 }
2430
2431 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2432
2433 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2434 {
2435         uiLayout *col;
2436         
2437         col= uiLayoutColumn(layout, 1);
2438         uiItemR(col, ptr, "offset", 0, "Offset", ICON_NONE);
2439         uiItemR(col, ptr, "offset_frequency", 0, "Frequency", ICON_NONE);
2440         
2441         col= uiLayoutColumn(layout, 1);
2442         uiItemR(col, ptr, "squash", 0, "Squash", ICON_NONE);
2443         uiItemR(col, ptr, "squash_frequency", 0, "Frequency", ICON_NONE);
2444 }
2445
2446 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2447 {
2448         PointerRNA tex_ptr;
2449         bNode *node= ptr->data;
2450         ID *id= ptr->id.data;
2451         Tex *tex = (Tex *)node->storage;
2452         uiLayout *col, *row;
2453         
2454         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2455
2456         col= uiLayoutColumn(layout, 0);
2457
2458         switch ( tex->type ) {
2459                 case TEX_BLEND:
2460                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2461                         row= uiLayoutRow(col, 0);
2462                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2463                         break;
2464
2465                 case TEX_MARBLE:
2466                         row= uiLayoutRow(col, 0);
2467                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2468                         row= uiLayoutRow(col, 0);
2469                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2470                         row= uiLayoutRow(col, 0);
2471                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2472                         row= uiLayoutRow(col, 0);
2473                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2474                         break;
2475
2476                 case TEX_MAGIC:
2477                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2478                         break;
2479
2480                 case TEX_STUCCI:
2481                         row= uiLayoutRow(col, 0);
2482                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2483                         row= uiLayoutRow(col, 0);
2484                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2485                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2486                         break;
2487
2488                 case TEX_WOOD:
2489                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2490                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2491                         row= uiLayoutRow(col, 0);
2492                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2493                         row= uiLayoutRow(col, 0);
2494                         uiLayoutSetActive(row, !(RNA_enum_get(&tex_ptr, "wood_type")==TEX_BAND || RNA_enum_get(&tex_ptr, "wood_type")==TEX_RING)); 
2495                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2496                         break;
2497                         
2498                 case TEX_CLOUDS:
2499                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2500                         row= uiLayoutRow(col, 0);
2501                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2502                         row= uiLayoutRow(col, 0);
2503                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2504                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, "Depth", ICON_NONE);
2505                         break;
2506                         
2507                 case TEX_DISTNOISE:
2508                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2509                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2510                         break;
2511
2512                 case TEX_MUSGRAVE:
2513                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2514                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2515                         break;
2516                 case TEX_VORONOI:
2517                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2518                         if (tex->vn_distm == TEX_MINKOVSKY) {
2519                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2520                         }
2521                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2522                         break;
2523         }
2524 }
2525
2526 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2527 {
2528         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2529 }
2530
2531 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2532 {
2533         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2534 }
2535
2536 /* only once called */
2537 static void node_texture_set_butfunc(bNodeType *ntype)
2538 {
2539         ntype->uifuncbut = NULL;
2540         if ( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
2541                 ntype->uifunc = node_texture_buts_proc;
2542         }
2543         else {
2544                 switch (ntype->type) {
2545
2546                 case TEX_NODE_MATH:
2547                         ntype->uifunc = node_buts_math;
2548                         break;
2549
2550                 case TEX_NODE_MIX_RGB:
2551                         ntype->uifunc = node_buts_mix_rgb;
2552                         break;
2553
2554                 case TEX_NODE_VALTORGB:
2555                         ntype->uifunc = node_buts_colorramp;
2556                         break;
2557
2558                 case TEX_NODE_CURVE_RGB:
2559                         ntype->uifunc= node_buts_curvecol;
2560                         break;
2561
2562                 case TEX_NODE_CURVE_TIME:
2563                         ntype->uifunc = node_buts_time;
2564                         break;
2565
2566                 case TEX_NODE_TEXTURE:
2567                         ntype->uifunc = node_buts_texture;
2568                         break;
2569
2570                 case TEX_NODE_BRICKS:
2571                         ntype->uifunc = node_texture_buts_bricks;
2572                         break;
2573
2574                 case TEX_NODE_IMAGE:
2575                         ntype->uifunc = node_texture_buts_image;
2576                         break;
2577
2578                 case TEX_NODE_OUTPUT:
2579                         ntype->uifunc = node_texture_buts_output;
2580                         break;
2581                 }
2582         }
2583
2584         if (ntype->uifuncbut == NULL) {
2585                 ntype->uifuncbut = ntype->uifunc;
2586         }
2587 }
2588
2589 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2590
2591 void ED_init_node_butfuncs(void)
2592 {
2593         bNodeTreeType *treetype;
2594         bNodeType *ntype;
2595         bNodeSocketType *stype;
2596         int i;
2597         
2598         /* node type ui functions */
2599         for (i=0; i < NUM_NTREE_TYPES; ++i) {
2600                 treetype = ntreeGetType(i);
2601                 if (treetype) {
2602                         for (ntype= treetype->node_types.first; ntype; ntype= ntype->next) {
2603                                 /* default ui functions */
2604                                 ntype->drawfunc = node_draw_default;
2605                                 ntype->drawupdatefunc = node_update_default;
2606                                 ntype->uifunc = NULL;
2607                                 ntype->uifuncbut = NULL;
2608                                 ntype->drawinputfunc = node_draw_input_default;
2609                                 ntype->drawoutputfunc = node_draw_output_default;
2610                                 ntype->resize_area_func = node_resize_area_default;
2611                                 
2612                                 node_common_set_butfunc(ntype);
2613                                 
2614                                 switch (i) {
2615                                 case NTREE_COMPOSIT:
2616                                         node_composit_set_butfunc(ntype);
2617                                         break;
2618                                 case NTREE_SHADER:
2619                                         node_shader_set_butfunc(ntype);
2620                                         break;
2621                                 case NTREE_TEXTURE:
2622                                         node_texture_set_butfunc(ntype);
2623                                         break;
2624                                 }
2625                         }
2626                 }
2627         }
2628         
2629         /* socket type ui functions */
2630         for (i=0; i < NUM_SOCKET_TYPES; ++i) {
2631                 stype = ntreeGetSocketType(i);
2632                 if (stype) {
2633                         switch (stype->type) {
2634                         case SOCK_FLOAT:
2635                         case SOCK_INT:
2636                         case SOCK_BOOLEAN:
2637                                 stype->buttonfunc = node_socket_button_default;
2638                                 break;
2639                         case SOCK_VECTOR:
2640                                 stype->buttonfunc = node_socket_button_components;
2641                                 break;
2642                         case SOCK_RGBA:
2643                                 stype->buttonfunc = node_socket_button_color;
2644                                 break;
2645                         case SOCK_SHADER:
2646                                 stype->buttonfunc = node_socket_button_label;
2647                                 break;
2648                         default:
2649                                 stype->buttonfunc = NULL;
2650                         }
2651                 }
2652         }
2653 }
2654
2655 /* ************** Generic drawing ************** */
2656
2657 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
2658 {
2659         
2660         if ((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2661                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2662                 void *lock;
2663                 ImBuf *ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
2664                 if (ibuf) {
2665                         float x, y; 
2666                         
2667                         glMatrixMode(GL_PROJECTION);
2668                         glPushMatrix();
2669                         glMatrixMode(GL_MODELVIEW);
2670                         glPushMatrix();
2671
2672                         /* keep this, saves us from a version patch */
2673                         if (snode->zoom==0.0f) snode->zoom= 1.0f;
2674                         
2675                         /* somehow the offset has to be calculated inverse */
2676                         
2677                         glaDefine2DArea(&ar->winrct);
2678                         /* ortho at pixel level curarea */
2679                         wmOrtho2(-0.375, ar->winx-0.375, -0.375, ar->winy-0.375);
2680                         
2681                         x = (ar->winx-snode->zoom*ibuf->x)/2 + snode->xof;
2682                         y = (ar->winy-snode->zoom*ibuf->y)/2 + snode->yof;
2683                         
2684                         if (!ibuf->rect) {
2685                                 if (color_manage)
2686                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
2687                                 else
2688                                         ibuf->profile = IB_PROFILE_NONE;
2689                                 IMB_rect_from_float(ibuf);
2690                         }
2691
2692                         if (ibuf->rect) {
2693                                 if (snode->flag & SNODE_SHOW_ALPHA) {
2694                                         glPixelZoom(snode->zoom, snode->zoom);
2695                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2696                                         if (ENDIAN_ORDER == B_ENDIAN)
2697                                                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
2698                                         
2699                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
2700                                         
2701                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
2702                                         glPixelZoom(1.0f, 1.0f);
2703                                 }
2704                                 else if (snode->flag & SNODE_USE_ALPHA) {
2705                                         glEnable(GL_BLEND);
2706                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2707                                         glPixelZoom(snode->zoom, snode->zoom);
2708                                         
2709                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2710                                         
2711                                         glPixelZoom(1.0f, 1.0f);
2712                                         glDisable(GL_BLEND);
2713                                 }
2714                                 else {
2715                                         glPixelZoom(snode->zoom, snode->zoom);
2716                                         
2717                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2718                                         
2719                                         glPixelZoom(1.0f, 1.0f);
2720                                 }
2721                         }
2722
2723                         /// @note draw selected info on backdrop
2724                         if (snode->edittree) {
2725                                 bNode *node = snode->edittree->nodes.first;
2726                                 while (node) {
2727                                         if (node->flag & NODE_SELECT) {
2728                                                 if (node->typeinfo->uibackdropfunc) {
2729                                                         node->typeinfo->uibackdropfunc(snode, ibuf, node, x, y);
2730                                                 }
2731                                         }
2732                                         node = node->next;
2733                                 }
2734                         }
2735                         
2736                         glMatrixMode(GL_PROJECTION);
2737                         glPopMatrix();
2738                         glMatrixMode(GL_MODELVIEW);
2739                         glPopMatrix();
2740                 }
2741
2742                 BKE_image_release_ibuf(ima, lock);
2743         }
2744 }
2745
2746 #if 0
2747 /* note: needs to be userpref or opengl profile option */
2748 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2749 {
2750
2751         draw_nodespace_grid(snode);
2752         
2753         if (snode->flag & SNODE_BACKDRAW) {
2754                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2755                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2756                 if (ibuf) {
2757                         int x, y;
2758                         float zoom = 1.0;
2759
2760                         glMatrixMode(GL_PROJECTION);
2761                         glPushMatrix();
2762                         glMatrixMode(GL_MODELVIEW);
2763                         glPushMatrix();
2764                         
2765                         glaDefine2DArea(&sa->winrct);
2766
2767                         if (ibuf->x > sa->winx || ibuf->y > sa->winy) {
2768                                 float zoomx, zoomy;
2769                                 zoomx= (float)sa->winx/ibuf->x;
2770                                 zoomy= (float)sa->winy/ibuf->y;
2771                                 zoom = MIN2(zoomx, zoomy);
2772                         }
2773                         
2774                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2775                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2776
2777                         glPixelZoom(zoom, zoom);
2778
2779                         glColor4f(1.0, 1.0, 1.0, 1.0);
2780                         if (ibuf->rect)
2781                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2782                         else if (ibuf->channels==4)
2783                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2784
2785                         glPixelZoom(1.0, 1.0);
2786
2787                         glMatrixMode(GL_PROJECTION);
2788                         glPopMatrix();
2789                         glMatrixMode(GL_MODELVIEW);
2790                         glPopMatrix();
2791                 }
2792         }
2793 }
2794 #endif
2795
2796 /* if v2d not NULL, it clips and returns 0 if not visible */
2797 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
2798 {
2799         float dist, vec[4][2];
2800         
2801         /* in v0 and v3 we put begin/end points */
2802         if (link->fromsock) {
2803                 vec[0][0]= link->fromsock->locx;
2804                 vec[0][1]= link->fromsock->locy;
2805         }
2806         else {
2807                 if (snode==NULL) return 0;
2808                 vec[0][0]= snode->mx;
2809                 vec[0][1]= snode->my;
2810         }
2811         if (link->tosock) {
2812                 vec[3][0]= link->tosock->locx;
2813                 vec[3][1]= link->tosock->locy;
2814         }
2815         else {
2816                 if (snode==NULL) return 0;
2817                 vec[3][0]= snode->mx;
2818                 vec[3][1]= snode->my;
2819         }
2820
2821         dist= UI_GetThemeValue(TH_NODE_CURVING)*0.10f*ABS(vec[0][0] - vec[3][0]);
2822         
2823         /* check direction later, for top sockets */
2824         vec[1][0]= vec[0][0]+dist;
2825         vec[1][1]= vec[0][1];
2826         
2827         vec[2][0]= vec[3][0]-dist;
2828         vec[2][1]= vec[3][1];
2829         
2830         if (v2d && MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax); /* clipped */
2831         else if (v2d && MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin); /* clipped */
2832         else {
2833                 
2834                 /* always do all three, to prevent data hanging around */
2835                 BKE_curve_forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2836                 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);
2837                 
2838                 return 1;
2839         }
2840         return 0;
2841 }
2842
2843 #define LINK_RESOL      24
2844 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 )
2845 {
2846         float coord_array[LINK_RESOL+1][2];
2847         
2848         if (node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
2849                 float dist, spline_step = 0.0f;
2850                 int i;
2851                 
2852                 /* store current linewidth */
2853                 float linew;
2854                 glGetFloatv(GL_LINE_WIDTH, &linew);
2855                 
2856                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2857                 dist = 1.0f/(float)LINK_RESOL;
2858                 
2859                 glEnable(GL_LINE_SMOOTH);
2860                 
2861                 if (do_triple) {
2862                         UI_ThemeColorShadeAlpha(th_col3, -80, -120);
2863                         glLineWidth(4.0f);
2864                         
2865                         glBegin(GL_LINE_STRIP);
2866                         for (i=0; i<=LINK_RESOL; i++) {
2867                                 glVertex2fv(coord_array[i]);
2868                         }
2869                         glEnd();
2870                 }
2871                 
2872                 /* XXX using GL_LINES for shaded node lines is a workaround
2873                  * for Intel hardware, this breaks with GL_LINE_STRIP and
2874                  * changing color in begin/end blocks.
2875                  */
2876                 glLineWidth(1.5f);
2877                 if (do_shaded) {
2878                         glBegin(GL_LINES);
2879                         for (i=0; i<LINK_RESOL; i++) {
2880                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step);
2881                                 glVertex2fv(coord_array[i]);
2882                                 
2883                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step+dist);
2884                                 glVertex2fv(coord_array[i+1]);
2885                                 
2886                                 spline_step += dist;
2887                         }
2888                         glEnd();
2889                 }
2890                 else {
2891                         UI_ThemeColor(th_col1);
2892                         glBegin(GL_LINE_STRIP);
2893                         for (i=0; i<=LINK_RESOL; i++) {
2894                                 glVertex2fv(coord_array[i]);
2895                         }
2896                         glEnd();
2897                 }
2898                 
2899                 glDisable(GL_LINE_SMOOTH);
2900                 
2901                 /* restore previuos linewidth */
2902                 glLineWidth(linew);
2903         }
2904 }
2905
2906 static void node_link_straight_points(View2D *UNUSED(v2d), SpaceNode *snode, bNodeLink *link, float coord_array[][2])
2907 {
2908         if (link->fromsock) {
2909                 coord_array[0][0]= link->fromsock->locx;
2910                 coord_array[0][1]= link->fromsock->locy;
2911         }
2912         else {
2913                 if (snode==NULL) return;
2914                 coord_array[0][0]= snode->mx;
2915                 coord_array[0][1]= snode->my;
2916         }
2917         if (link->tosock) {
2918                 coord_array[1][0]= link->tosock->locx;
2919                 coord_array[1][1]= link->tosock->locy;
2920         }
2921         else {
2922                 if (snode==NULL) return;
2923                 coord_array[1][0]= snode->mx;
2924                 coord_array[1][1]= snode->my;
2925         }
2926 }
2927
2928 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 )
2929 {
2930         float coord_array[2][2];
2931         float linew;
2932         int i;
2933         
2934         node_link_straight_points(v2d, snode, link, coord_array);
2935         
2936         /* store current linewidth */
2937         glGetFloatv(GL_LINE_WIDTH, &linew);
2938         
2939         glEnable(GL_LINE_SMOOTH);
2940         
2941         if (do_triple) {
2942                 UI_ThemeColorShadeAlpha(th_col3, -80, -120);
2943                 glLineWidth(4.0f);
2944                 
2945                 glBegin(GL_LINES);
2946                 glVertex2fv(coord_array[0]);
2947                 glVertex2fv(coord_array[1]);
2948                 glEnd();
2949         }
2950         
2951         UI_ThemeColor(th_col1);
2952         glLineWidth(1.5f);
2953         
2954         /* XXX using GL_LINES for shaded node lines is a workaround
2955          * for Intel hardware, this breaks with GL_LINE_STRIP and
2956          * changing color in begin/end blocks.
2957          */
2958         if (do_shaded) {
2959                 glBegin(GL_LINES);
2960                 for (i=0; i < LINK_RESOL-1; ++i) {
2961                         float t= (float)i/(float)(LINK_RESOL-1);
2962                         UI_ThemeColorBlend(th_col1, th_col2, t);
2963                         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]);
2964                         
2965                         t= (float)(i+1)/(float)(LINK_RESOL-1);
2966                         UI_ThemeColorBlend(th_col1, th_col2, t);
2967                         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]);
2968                 }
2969                 glEnd();
2970         }
2971         else {
2972                 glBegin(GL_LINE_STRIP);
2973                 for (i=0; i < LINK_RESOL; ++i) {
2974                         float t= (float)i/(float)(LINK_RESOL-1);
2975                         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]);
2976                 }
2977                 glEnd();
2978         }
2979         
2980         glDisable(GL_LINE_SMOOTH);
2981         
2982         /* restore previuos linewidth */
2983         glLineWidth(linew);
2984 }
2985
2986 /* note; this is used for fake links in groups too */
2987 void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link)
2988 {
2989         int do_shaded= FALSE, th_col1= TH_HEADER, th_col2= TH_HEADER;
2990         int do_triple= FALSE, th_col3= TH_WIRE;
2991         
2992         if (link->fromsock==NULL && link->tosock==NULL)
2993                 return;
2994         
2995         /* new connection */
2996         if (!link->fromsock || !link->tosock) {
2997                 th_col1 = TH_ACTIVE;
2998                 do_triple = TRUE;
2999         }
3000         else {
3001                 /* going to give issues once... */
3002                 if (link->tosock->flag & SOCK_UNAVAIL)
3003                         return;
3004                 if (link->fromsock->flag & SOCK_UNAVAIL)
3005                         return;
3006                 
3007                 /* a bit ugly... but thats how we detect the internal group links */
3008                 if (!link->fromnode || !link->tonode) {
3009                         UI_ThemeColorBlend(TH_BACK, TH_WIRE, 0.5f);
3010                         do_shaded = FALSE;
3011                 }
3012                 else {
3013                         /* check cyclic */
3014                         if ((link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) && (link->flag & NODE_LINK_VALID)) {
3015                                 /* special indicated link, on drop-node */
3016                                 if (link->flag & NODE_LINKFLAG_HILITE) {
3017                                         th_col1= th_col2= TH_ACTIVE;
3018                                 }
3019                                 else {
3020                                         /* regular link */
3021                                         if (link->fromnode->flag & SELECT)
3022                                                 th_col1= TH_EDGE_SELECT;
3023                                         if (link->tonode->flag & SELECT)
3024                                                 th_col2= TH_EDGE_SELECT;
3025                                 }
3026                                 do_shaded = TRUE;
3027                                 do_triple = TRUE;
3028                         }                               
3029                         else {
3030                                 th_col1 = TH_REDALERT;
3031                         }
3032                 }
3033         }
3034         
3035         node_draw_link_bezier(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
3036 //      node_draw_link_straight(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
3037 }