svn merge ^/trunk/blender -r49890:49917
[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  *  \brief lower level node drawing for nodes (boarders, headers etc), also node layout.
31  */
32
33 #include "BLI_blenlib.h"
34 #include "BLI_math.h"
35
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_screen_types.h"
40
41 #include "BKE_context.h"
42 #include "BKE_curve.h"
43 #include "BKE_image.h"
44 #include "BKE_main.h"
45 #include "BKE_node.h"
46 #include "BKE_tracking.h"
47
48 #include "BLF_api.h"
49 #include "BLF_translation.h"
50
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "MEM_guardedalloc.h"
56
57 #include "RNA_access.h"
58
59 #include "ED_node.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "UI_resources.h"
65
66 #include "IMB_colormanagement.h"
67 #include "IMB_imbuf.h"
68 #include "IMB_imbuf_types.h"
69
70 #include "node_intern.h"  /* own include */
71
72 /* XXX interface.h */
73 extern void ui_dropshadow(rctf *rct, float radius, float aspect, float alpha, int select);
74
75 /* ****************** SOCKET BUTTON DRAW FUNCTIONS ***************** */
76
77 static void node_sync_cb(bContext *UNUSED(C), void *snode_v, void *node_v)
78 {
79         SpaceNode *snode = snode_v;
80         
81         if (snode->treetype == NTREE_SHADER) {
82                 nodeShaderSynchronizeID(node_v, 1);
83                 // allqueue(REDRAWBUTSSHADING, 0);
84         }
85 }
86
87 static void node_socket_button_label(const bContext *UNUSED(C), uiBlock *block,
88                                      bNodeTree *UNUSED(ntree), bNode *UNUSED(node), bNodeSocket *sock,
89                                      const char *UNUSED(name), int x, int y, int width)
90 {
91         uiDefBut(block, LABEL, 0, IFACE_(sock->name), x, y, width, NODE_DY, NULL, 0, 0, 0, 0, "");
92 }
93
94 static void node_socket_button_default(const bContext *C, uiBlock *block,
95                                        bNodeTree *ntree, bNode *node, bNodeSocket *sock,
96                                        const char *name, int x, int y, int width)
97 {
98         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
99                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
100         else {
101                 PointerRNA ptr;
102                 uiBut *bt;
103                 
104                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
105                 
106                 bt = uiDefButR(block, NUM, B_NODE_EXEC, IFACE_(name),
107                                x, y + 1, width, NODE_DY - 2,
108                                &ptr, "default_value", 0, 0, 0, -1, -1, NULL);
109                 if (node)
110                         uiButSetFunc(bt, node_sync_cb, CTX_wm_space_node(C), node);
111         }
112 }
113
114 typedef struct SocketComponentMenuArgs {
115         PointerRNA ptr;
116         int x, y, width;
117         uiButHandleFunc cb;
118         void *arg1, *arg2;
119 } SocketComponentMenuArgs;
120 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
121 static uiBlock *socket_component_menu(bContext *C, ARegion *ar, void *args_v)
122 {
123         SocketComponentMenuArgs *args = (SocketComponentMenuArgs *)args_v;
124         uiBlock *block;
125         uiLayout *layout;
126         
127         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
128         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
129         
130         layout = uiLayoutColumn(uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL,
131                                               args->x, args->y + 2, args->width, NODE_DY, UI_GetStyle()), FALSE);
132         
133         uiItemR(layout, &args->ptr, "default_value", UI_ITEM_R_EXPAND, "", ICON_NONE);
134         
135         return block;
136 }
137 static void node_socket_button_components(const bContext *C, uiBlock *block,
138                                           bNodeTree *ntree, bNode *node, bNodeSocket *sock,
139                                           const char *name, int x, int y, int width)
140 {
141         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
142                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
143         else {
144                 PointerRNA ptr;
145                 SocketComponentMenuArgs *args;
146                 
147                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
148                 
149                 args = MEM_callocN(sizeof(SocketComponentMenuArgs), "SocketComponentMenuArgs");
150                 
151                 args->ptr = ptr;
152                 args->x = x;
153                 args->y = y;
154                 args->width = width;
155                 args->cb = node_sync_cb;
156                 args->arg1 = CTX_wm_space_node(C);
157                 args->arg2 = node;
158                 
159                 uiDefBlockButN(block, socket_component_menu, args, IFACE_(name), x, y + 1, width, NODE_DY - 2, "");
160         }
161 }
162
163 static void node_socket_button_color(const bContext *C, uiBlock *block,
164                                      bNodeTree *ntree, bNode *node, bNodeSocket *sock,
165                                      const char *name, int x, int y, int width)
166 {
167         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
168                 node_socket_button_label(C, block, ntree, node, sock, IFACE_(name), x, y, width);
169         else {
170                 PointerRNA ptr;
171                 uiBut *bt;
172                 int labelw = width - 40;
173                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
174                 
175                 bt = uiDefButR(block, COL, B_NODE_EXEC, "",
176                                x, y + 2, (labelw > 0 ? 40 : width), NODE_DY - 2,
177                                &ptr, "default_value", 0, 0, 0, -1, -1, NULL);
178                 if (node)
179                         uiButSetFunc(bt, node_sync_cb, CTX_wm_space_node(C), node);
180                 
181                 if (name[0] != '\0' && labelw > 0)
182                         uiDefBut(block, LABEL, 0, IFACE_(name), x + 40, y + 2, labelw, NODE_DY - 2, NULL, 0, 0, 0, 0, "");
183         }
184 }
185
186 /* standard draw function, display the default input value */
187 static void node_draw_input_default(const bContext *C, uiBlock *block,
188                                     bNodeTree *ntree, bNode *node, bNodeSocket *sock,
189                                     const char *name, int x, int y, int width)
190 {
191         bNodeSocketType *stype = ntreeGetSocketType(sock->type);
192         if (stype->buttonfunc)
193                 stype->buttonfunc(C, block, ntree, node, sock, name, x, y, width);
194         else
195                 node_socket_button_label(C, block, ntree, node, sock, IFACE_(name), x, y, width);
196 }
197
198 static void node_draw_output_default(const bContext *C, uiBlock *block,
199                                      bNodeTree *UNUSED(ntree), bNode *node, bNodeSocket *sock,
200                                      const char *name, int UNUSED(x), int UNUSED(y), int UNUSED(width))
201 {
202         SpaceNode *snode = CTX_wm_space_node(C);
203         float slen;
204         int ofs = 0;
205         const char *ui_name = IFACE_(name);
206         int len = strlen(ui_name);
207         UI_ThemeColor(TH_TEXT);
208         slen = (UI_GetStringWidth(ui_name) + NODE_MARGIN_X) * snode->aspect_sqrt;
209         while (slen > node->width && ofs < len) {
210                 ofs++;
211                 slen = (UI_GetStringWidth(ui_name + ofs) + NODE_MARGIN_X) * snode->aspect_sqrt;
212         }
213
214         if (ofs < len) {
215                 uiDefBut(block, LABEL, 0, ui_name + ofs,
216                          (int)(sock->locx - slen), (int)(sock->locy - 9.0f),
217                          (short)(node->width - NODE_DY), (short)NODE_DY,
218                          NULL, 0, 0, 0, 0, "");
219         }
220
221         (void)snode;
222 }
223
224 /* ****************** BASE DRAW FUNCTIONS FOR NEW OPERATOR NODES ***************** */
225
226 #if 0 /* UNUSED */
227 static void node_draw_socket_new(bNodeSocket *sock, float size)
228 {
229         float x = sock->locx, y = sock->locy;
230         
231         /* 16 values of sin function */
232         static float si[16] = {
233                 0.00000000f, 0.39435585f, 0.72479278f, 0.93775213f,
234                 0.99871650f, 0.89780453f, 0.65137248f, 0.29936312f,
235                 -0.10116832f, -0.48530196f, -0.79077573f, -0.96807711f,
236                 -0.98846832f, -0.84864425f, -0.57126821f, -0.20129852f
237         };
238         /* 16 values of cos function */
239         static float co[16] = {
240                 1.00000000f, 0.91895781f, 0.68896691f, 0.34730525f,
241                 -0.05064916f, -0.44039415f, -0.75875812f, -0.95413925f,
242                 -0.99486932f, -0.87434661f, -0.61210598f, -0.25065253f,
243                 0.15142777f, 0.52896401f, 0.82076344f, 0.97952994f,
244         };
245         int a;
246         
247         glColor3ub(180, 180, 180);
248         
249         glBegin(GL_POLYGON);
250         for (a = 0; a < 16; a++)
251                 glVertex2f(x + size * si[a], y + size * co[a]);
252         glEnd();
253         
254         glColor4ub(0, 0, 0, 150);
255         glEnable(GL_BLEND);
256         glEnable(GL_LINE_SMOOTH);
257         glBegin(GL_LINE_LOOP);
258         for (a = 0; a < 16; a++)
259                 glVertex2f(x + size * si[a], y + size * co[a]);
260         glEnd();
261         glDisable(GL_LINE_SMOOTH);
262         glDisable(GL_BLEND);
263 }
264 #endif
265
266 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
267
268 static void node_buts_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
269 {
270         PointerRNA sockptr;
271         PropertyRNA *prop;
272         
273         /* first socket stores value */
274         prop = RNA_struct_find_property(ptr, "outputs");
275         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
276         
277         uiItemR(layout, &sockptr, "default_value", 0, "", ICON_NONE);
278 }
279
280 static void node_buts_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
281 {
282         uiLayout *col;
283         PointerRNA sockptr;
284         PropertyRNA *prop;
285         
286         /* first socket stores value */
287         prop = RNA_struct_find_property(ptr, "outputs");
288         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
289         
290         col = uiLayoutColumn(layout, FALSE);
291         uiTemplateColorWheel(col, &sockptr, "default_value", 1, 0, 0, 0);
292         uiItemR(col, &sockptr, "default_value", 0, "", ICON_NONE);
293 }
294
295 static void node_buts_mix_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
296 {       
297         uiLayout *row, *col;
298
299         bNodeTree *ntree = (bNodeTree *)ptr->id.data;
300
301         col = uiLayoutColumn(layout, FALSE);
302         row = uiLayoutRow(col, TRUE);
303         uiItemR(row, ptr, "blend_type", 0, "", ICON_NONE);
304         if (ntree->type == NTREE_COMPOSIT)
305                 uiItemR(row, ptr, "use_alpha", 0, "", ICON_IMAGE_RGB_ALPHA);
306
307         uiItemR(col, ptr, "use_clamp", 0, NULL, ICON_NONE);
308 }
309
310 static void node_buts_time(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
311 {
312         uiLayout *row;
313 #if 0
314         /* XXX no context access here .. */
315         bNode *node = ptr->data;
316         CurveMapping *cumap = node->storage;
317         
318         if (cumap) {
319                 cumap->flag |= CUMA_DRAW_CFRA;
320                 if (node->custom1 < node->custom2)
321                         cumap->sample[0] = (float)(CFRA - node->custom1) / (float)(node->custom2 - node->custom1);
322         }
323 #endif
324
325         uiTemplateCurveMapping(layout, ptr, "curve", 's', 0, 0);
326
327         row = uiLayoutRow(layout, TRUE);
328         uiItemR(row, ptr, "frame_start", 0, IFACE_("Sta"), ICON_NONE);
329         uiItemR(row, ptr, "frame_end", 0, IFACE_("End"), ICON_NONE);
330 }
331
332 static void node_buts_colorramp(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
333 {
334         uiTemplateColorRamp(layout, ptr, "color_ramp", 0);
335 }
336
337 static void node_buts_curvevec(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
338 {
339         uiTemplateCurveMapping(layout, ptr, "mapping", 'v', 0, 0);
340 }
341
342 #define SAMPLE_FLT_ISNONE FLT_MAX
343 static float _sample_col[4] = {SAMPLE_FLT_ISNONE};  /* bad bad, 2.5 will do better?... no it won't... */
344 void ED_node_sample_set(const float col[4])
345 {
346         if (col) {
347                 copy_v4_v4(_sample_col, col);
348         }
349         else {
350                 copy_v4_fl(_sample_col, SAMPLE_FLT_ISNONE);
351         }
352 }
353
354 static void node_buts_curvecol(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
355 {
356         bNode *node = ptr->data;
357         CurveMapping *cumap = node->storage;
358
359         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
360                 cumap->flag |= CUMA_DRAW_SAMPLE;
361                 copy_v3_v3(cumap->sample, _sample_col);
362         }
363         else {
364                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
365         }
366
367         uiTemplateCurveMapping(layout, ptr, "mapping", 'c', 0, 0);
368 }
369
370 static void node_normal_cb(bContext *C, void *ntree_v, void *node_v)
371 {
372         Main *bmain = CTX_data_main(C);
373
374         ED_node_generic_update(bmain, ntree_v, node_v);
375         WM_event_add_notifier(C, NC_NODE | NA_EDITED, ntree_v);
376 }
377
378 static void node_buts_normal(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
379 {
380         uiBlock *block = uiLayoutAbsoluteBlock(layout);
381         bNodeTree *ntree = ptr->id.data;
382         bNode *node = ptr->data;
383         rctf *butr = &node->butr;
384         bNodeSocket *sock = node->outputs.first;     /* first socket stores normal */
385         float *nor = ((bNodeSocketValueVector *)sock->default_value)->value;
386         uiBut *bt;
387         
388         bt = uiDefButF(block, BUT_NORMAL, B_NODE_EXEC, "",
389                        (int)butr->xmin, (int)butr->xmin,
390                        (short)(butr->xmax - butr->xmin), (short)(butr->xmax - butr->xmin),
391                        nor, 0.0f, 1.0f, 0, 0, "");
392         uiButSetFunc(bt, node_normal_cb, ntree, node);
393 }
394 #if 0 /* not used in 2.5x yet */
395 static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
396 {
397         Main *bmain = CTX_data_main(C);
398         bNodeTree *ntree = ntree_v;
399         bNode *node = node_v;
400         Tex *tex;
401         
402         if (node->menunr < 1) return;
403         
404         if (node->id) {
405                 node->id->us--;
406                 node->id = NULL;
407         }
408         tex = BLI_findlink(&bmain->tex, node->menunr - 1);
409
410         node->id = &tex->id;
411         id_us_plus(node->id);
412         BLI_strncpy(node->name, node->id->name + 2, sizeof(node->name));
413         
414         nodeSetActive(ntree, node);
415         
416         if (ntree->type == NTREE_TEXTURE)
417                 ntreeTexCheckCyclics(ntree);
418         
419         // allqueue(REDRAWBUTSSHADING, 0);
420         // allqueue(REDRAWNODE, 0);
421         NodeTagChanged(ntree, node); 
422         
423         node->menunr = 0;
424 }
425 #endif
426
427 static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
428 {
429         bNode *node = ptr->data;
430
431         short multi = (
432             node->id &&
433             ((Tex *)node->id)->use_nodes &&
434             (node->type != CMP_NODE_TEXTURE) &&
435             (node->type != TEX_NODE_TEXTURE)
436             );
437         
438         uiItemR(layout, ptr, "texture", 0, "", ICON_NONE);
439         
440         if (multi) {
441                 /* Number Drawing not optimal here, better have a list*/
442                 uiItemR(layout, ptr, "node_output", 0, "", ICON_NONE);
443         }
444 }
445
446 static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
447
448         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
449         uiItemR(layout, ptr, "use_clamp", 0, NULL, ICON_NONE);
450 }
451
452 static int node_resize_area_default(bNode *node, int x, int y)
453 {
454         if (node->flag & NODE_HIDDEN) {
455                 rctf totr = node->totr;
456                 /* right part of node */
457                 totr.xmin = node->totr.xmax - 20.0f;
458                 if (BLI_in_rctf(&totr, x, y))
459                         return NODE_RESIZE_RIGHT;
460                 else
461                         return 0;
462         }
463         else {
464                 const float size = 10.0f;
465                 rctf totr = node->totr;
466                 int dir = 0;
467                 
468                 if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
469                         dir |= NODE_RESIZE_RIGHT;
470                 if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
471                         dir |= NODE_RESIZE_LEFT;
472                 return dir;
473         }
474 }
475
476 /* ****************** BUTTON CALLBACKS FOR COMMON NODES ***************** */
477
478 /* width of socket columns in group display */
479 #define NODE_GROUP_FRAME        120
480
481 /* based on settings in node, sets drawing rect info. each redraw! */
482 /* note: this assumes only 1 group at a time is drawn (linked data) */
483 /* in node->totr the entire boundbox for the group is stored */
484 static void node_update_group(const bContext *C, bNodeTree *ntree, bNode *gnode)
485 {
486         if (!(gnode->flag & NODE_GROUP_EDIT)) {
487                 node_update_default(C, ntree, gnode);
488         }
489         else {
490                 bNodeTree *ngroup = (bNodeTree *)gnode->id;
491                 bNode *node;
492                 bNodeSocket *sock, *gsock;
493                 float locx, locy;
494                 rctf *rect = &gnode->totr;
495                 float node_group_frame = U.dpi * NODE_GROUP_FRAME / 72;
496                 float group_header = 26 * U.dpi / 72;
497                 int counter;
498                 int dy;
499                 
500                 /* get "global" coords */
501                 nodeToView(gnode, 0.0f, 0.0f, &locx, &locy);
502                 
503                 /* center them, is a bit of abuse of locx and locy though */
504                 node_update_nodetree(C, ngroup, locx, locy);
505                 
506                 rect->xmin = rect->xmax = locx;
507                 rect->ymin = rect->ymax = locy;
508                 
509                 counter = 1;
510                 for (node = ngroup->nodes.first; node; node = node->next) {
511                         if (counter) {
512                                 *rect = node->totr;
513                                 counter = 0;
514                         }
515                         else
516                                 BLI_rctf_union(rect, &node->totr);
517                 }
518                 
519                 /* add some room for links to group sockets */
520                 rect->xmin -= 4 * NODE_DY;
521                 rect->xmax += 4 * NODE_DY;
522                 rect->ymin -= NODE_DY;
523                 rect->ymax += NODE_DY;
524                 
525                 /* input sockets */
526                 dy = 0.5f * (rect->ymin + rect->ymax) + NODE_DY * (BLI_countlist(&gnode->inputs) - 1);
527                 gsock = ngroup->inputs.first;
528                 sock = gnode->inputs.first;
529                 while (gsock || sock) {
530                         while (sock && !sock->groupsock) {
531                                 sock->locx = rect->xmin - node_group_frame;
532                                 sock->locy = dy;
533
534                                 /* prevent long socket lists from growing out of the group box */
535                                 if (dy - 3 * NODE_DYS < rect->ymin)
536                                         rect->ymin = dy - 3 * NODE_DYS;
537                                 if (dy + 3 * NODE_DYS > rect->ymax)
538                                         rect->ymax = dy + 3 * NODE_DYS;
539                                 dy -= 2 * NODE_DY;
540                                 
541                                 sock = sock->next;
542                         }
543                         while (gsock && (!sock || sock->groupsock != gsock)) {
544                                 gsock->locx = rect->xmin;
545                                 gsock->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                                 gsock = gsock->next;
555                         }
556                         while (sock && gsock && sock->groupsock == gsock) {
557                                 gsock->locx = rect->xmin;
558                                 sock->locx = rect->xmin - node_group_frame;
559                                 sock->locy = gsock->locy = dy;
560                                 
561                                 /* prevent long socket lists from growing out of the group box */
562                                 if (dy - 3 * NODE_DYS < rect->ymin)
563                                         rect->ymin = dy - 3 * NODE_DYS;
564                                 if (dy + 3 * NODE_DYS > rect->ymax)
565                                         rect->ymax = dy + 3 * NODE_DYS;
566                                 dy -= 2 * NODE_DY;
567                                 
568                                 sock = sock->next;
569                                 gsock = gsock->next;
570                         }
571                 }
572                 
573                 /* output sockets */
574                 dy = 0.5f * (rect->ymin + rect->ymax) + NODE_DY * (BLI_countlist(&gnode->outputs) - 1);
575                 gsock = ngroup->outputs.first;
576                 sock = gnode->outputs.first;
577                 while (gsock || sock) {
578                         while (sock && !sock->groupsock) {
579                                 sock->locx = rect->xmax + node_group_frame;
580                                 sock->locy = dy - NODE_DYS;
581                                 
582                                 /* prevent long socket lists from growing out of the group box */
583                                 if (dy - 3 * NODE_DYS < rect->ymin)
584                                         rect->ymin = dy - 3 * NODE_DYS;
585                                 if (dy + 3 * NODE_DYS > rect->ymax)
586                                         rect->ymax = dy + 3 * NODE_DYS;
587                                 dy -= 2 * NODE_DY;
588                                 
589                                 sock = sock->next;
590                         }
591                         while (gsock && (!sock || sock->groupsock != gsock)) {
592                                 gsock->locx = rect->xmax;
593                                 gsock->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                                 gsock = gsock->next;
603                         }
604                         while (sock && gsock && sock->groupsock == gsock) {
605                                 gsock->locx = rect->xmax;
606                                 sock->locx = rect->xmax + node_group_frame;
607                                 sock->locy = gsock->locy = dy - NODE_DYS;
608                                 
609                                 /* prevent long socket lists from growing out of the group box */
610                                 if (dy - 3 * NODE_DYS < rect->ymin)
611                                         rect->ymin = dy - 3 * NODE_DYS;
612                                 if (dy + 3 * NODE_DYS > rect->ymax)
613                                         rect->ymax = dy + 3 * NODE_DYS;
614                                 dy -= 2 * NODE_DY;
615                                 
616                                 sock = sock->next;
617                                 gsock = gsock->next;
618                         }
619                 }
620                 
621                 /* Set the block bounds to clip mouse events from underlying nodes.
622                  * Add margin for header and input/output columns.
623                  */
624                 uiExplicitBoundsBlock(gnode->block,
625                                       rect->xmin - node_group_frame,
626                                       rect->ymin,
627                                       rect->xmax + node_group_frame,
628                                       rect->ymax + group_header);
629         }
630 }
631
632 static void update_group_input_cb(bContext *UNUSED(C), void *UNUSED(snode_v), void *ngroup_v)
633 {
634         bNodeTree *ngroup = (bNodeTree *)ngroup_v;
635         
636         ngroup->update |= NTREE_UPDATE_GROUP_IN;
637         ntreeUpdateTree(ngroup);
638 }
639
640 static void update_group_output_cb(bContext *UNUSED(C), void *UNUSED(snode_v), void *ngroup_v)
641 {
642         bNodeTree *ngroup = (bNodeTree *)ngroup_v;
643         
644         ngroup->update |= NTREE_UPDATE_GROUP_OUT;
645         ntreeUpdateTree(ngroup);
646 }
647
648 static void draw_group_socket_name(SpaceNode *snode, bNode *gnode, bNodeSocket *sock,
649                                    int in_out, float xoffset, float yoffset)
650 {
651         bNodeTree *ngroup = (bNodeTree *)gnode->id;
652         uiBut *bt;
653         const char *ui_name = IFACE_(sock->name);
654         
655         if (sock->flag & SOCK_DYNAMIC) {
656                 bt = uiDefBut(gnode->block, TEX, 0, "", 
657                               sock->locx + xoffset, sock->locy + 1 + yoffset, 72, NODE_DY,
658                               sock->name, 0, sizeof(sock->name), 0, 0, "");
659                 if (in_out == SOCK_IN)
660                         uiButSetFunc(bt, update_group_input_cb, snode, ngroup);
661                 else
662                         uiButSetFunc(bt, update_group_output_cb, snode, ngroup);
663         }
664         else {
665                 uiDefBut(gnode->block, LABEL, 0, ui_name,
666                          sock->locx + xoffset, sock->locy + 1 + yoffset, 72, NODE_DY,
667                          NULL, 0, sizeof(ui_name), 0, 0, "");
668         }
669 }
670
671 static void draw_group_socket(const bContext *C, SpaceNode *snode, bNodeTree *ntree, bNode *gnode,
672                               bNodeSocket *sock, bNodeSocket *gsock, int index, int in_out)
673 {
674         bNodeTree *ngroup = (bNodeTree *)gnode->id;
675         bNodeSocketType *stype = ntreeGetSocketType(gsock ? gsock->type : sock->type);
676         uiBut *bt;
677         float offset;
678         int draw_value;
679         float node_group_frame = U.dpi * NODE_GROUP_FRAME / 72;
680         float socket_size = NODE_SOCKSIZE * U.dpi / 72;
681         float arrowbutw = 0.8f * UI_UNIT_X;
682         /* layout stuff for buttons on group left frame */
683         float colw = 0.6f * node_group_frame;
684         float col1 = 6 - node_group_frame;
685         float col2 = col1 + colw + 6;
686         float col3 = -arrowbutw - 6;
687         /* layout stuff for buttons on group right frame */
688         float cor1 = 6;
689         float cor2 = cor1 + arrowbutw + 6;
690         float cor3 = cor2 + arrowbutw + 6;
691         
692         /* node and group socket circles */
693         if (sock)
694                 node_socket_circle_draw(ntree, sock, socket_size, sock->flag & SELECT);
695         if (gsock)
696                 node_socket_circle_draw(ngroup, gsock, socket_size, gsock->flag & SELECT);
697         
698         /* socket name */
699         offset = (in_out == SOCK_IN ? col1 : cor3);
700         if (!gsock)
701                 offset += (in_out == SOCK_IN ? node_group_frame : -node_group_frame);
702         
703         /* draw both name and value button if:
704          * 1) input: not internal
705          * 2) output: (node type uses const outputs) and (group output is unlinked)
706          */
707         draw_value = 0;
708         switch (in_out) {
709                 case SOCK_IN:
710                         draw_value = !(gsock && (gsock->flag & SOCK_INTERNAL));
711                         break;
712                 case SOCK_OUT:
713                         if (gnode->typeinfo->flag & NODE_CONST_OUTPUT)
714                                 draw_value = !(gsock && gsock->link);
715                         break;
716         }
717         if (draw_value) {
718                 /* both name and value buttons */
719                 if (gsock) {
720                         draw_group_socket_name(snode, gnode, gsock, in_out, offset, 0);
721                         if (stype->buttonfunc)
722                                 stype->buttonfunc(C, gnode->block, ngroup, NULL, gsock, "",
723                                                   gsock->locx + offset, gsock->locy - NODE_DY, colw);
724                 }
725                 else {
726                         draw_group_socket_name(snode, gnode, sock, in_out, offset, 0);
727                         if (stype->buttonfunc)
728                                 stype->buttonfunc(C, gnode->block, ngroup, NULL, sock, "",
729                                                   sock->locx + offset, sock->locy - NODE_DY, colw);
730                 }
731         }
732         else {
733                 /* only name, no value button */
734                 if (gsock)
735                         draw_group_socket_name(snode, gnode, gsock, in_out, offset, -NODE_DYS);
736                 else
737                         draw_group_socket_name(snode, gnode, sock, in_out, offset, -NODE_DYS);
738         }
739         
740         if (gsock && (gsock->flag & SOCK_DYNAMIC)) {
741                 /* up/down buttons */
742                 offset = (in_out == SOCK_IN ? col2 : cor2);
743                 uiBlockSetDirection(gnode->block, UI_TOP);
744                 uiBlockBeginAlign(gnode->block);
745                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_move_up", 0, ICON_TRIA_UP,
746                                    gsock->locx + offset, gsock->locy, arrowbutw, arrowbutw, "");
747                 if (!gsock->prev || !(gsock->prev->flag & SOCK_DYNAMIC))
748                         uiButSetFlag(bt, UI_BUT_DISABLED);
749                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
750                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
751                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_move_down", 0, ICON_TRIA_DOWN,
752                                    gsock->locx + offset, gsock->locy - arrowbutw, arrowbutw, arrowbutw, "");
753                 if (!gsock->next || !(gsock->next->flag & SOCK_DYNAMIC))
754                         uiButSetFlag(bt, UI_BUT_DISABLED);
755                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
756                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
757                 uiBlockEndAlign(gnode->block);
758                 uiBlockSetDirection(gnode->block, 0);
759                 
760                 /* remove button */
761                 offset = (in_out == SOCK_IN ? col3 : cor1);
762                 uiBlockSetEmboss(gnode->block, UI_EMBOSSN);
763                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_remove", 0, ICON_X,
764                                    gsock->locx + offset, gsock->locy - 0.5f * arrowbutw, arrowbutw, arrowbutw, "");
765                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
766                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
767                 uiBlockSetEmboss(gnode->block, UI_EMBOSS);
768         }
769 }
770
771 /* groups are, on creation, centered around 0,0 */
772 static void node_draw_group(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *ntree, bNode *gnode)
773 {
774         if (!(gnode->flag & NODE_GROUP_EDIT)) {
775                 node_draw_default(C, ar, snode, ntree, gnode);
776         }
777         else {
778                 bNodeTree *ngroup = (bNodeTree *)gnode->id;
779                 bNodeSocket *sock, *gsock;
780                 uiLayout *layout;
781                 PointerRNA ptr;
782                 rctf rect = gnode->totr;
783                 const float dpi_fac = U.dpi / 72.0f;
784                 float node_group_frame = NODE_GROUP_FRAME * dpi_fac;
785                 float group_header = 26 * dpi_fac;
786                 
787                 int index;
788                 
789                 /* backdrop header */
790                 glEnable(GL_BLEND);
791                 uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
792                 UI_ThemeColorShadeAlpha(TH_NODE_GROUP, 0, -70);
793                 uiDrawBox(GL_POLYGON,
794                           rect.xmin - node_group_frame, rect.ymax,
795                           rect.xmax + node_group_frame, rect.ymax + group_header, BASIS_RAD);
796                 
797                 /* backdrop body */
798                 UI_ThemeColorShadeAlpha(TH_BACK, -8, -70);
799                 uiSetRoundBox(UI_CNR_NONE);
800                 uiDrawBox(GL_POLYGON, rect.xmin, rect.ymin, rect.xmax, rect.ymax, BASIS_RAD);
801         
802                 /* input column */
803                 UI_ThemeColorShadeAlpha(TH_BACK, 10, -50);
804                 uiSetRoundBox(UI_CNR_BOTTOM_LEFT);
805                 uiDrawBox(GL_POLYGON, rect.xmin - node_group_frame, rect.ymin, rect.xmin, rect.ymax, BASIS_RAD);
806         
807                 /* output column */
808                 UI_ThemeColorShadeAlpha(TH_BACK, 10, -50);
809                 uiSetRoundBox(UI_CNR_BOTTOM_RIGHT);
810                 uiDrawBox(GL_POLYGON, rect.xmax, rect.ymin, rect.xmax + node_group_frame, rect.ymax, BASIS_RAD);
811         
812                 /* input column separator */
813                 glColor4ub(200, 200, 200, 140);
814                 glBegin(GL_LINES);
815                 glVertex2f(rect.xmin, rect.ymin);
816                 glVertex2f(rect.xmin, rect.ymax);
817                 glEnd();
818         
819                 /* output column separator */
820                 glColor4ub(200, 200, 200, 140);
821                 glBegin(GL_LINES);
822                 glVertex2f(rect.xmax, rect.ymin);
823                 glVertex2f(rect.xmax, rect.ymax);
824                 glEnd();
825         
826                 /* group node outline */
827                 uiSetRoundBox(UI_CNR_ALL);
828                 glColor4ub(200, 200, 200, 140);
829                 glEnable(GL_LINE_SMOOTH);
830                 uiDrawBox(GL_LINE_LOOP,
831                           rect.xmin - node_group_frame, rect.ymin,
832                           rect.xmax + node_group_frame, rect.ymax + group_header, BASIS_RAD);
833                 glDisable(GL_LINE_SMOOTH);
834                 glDisable(GL_BLEND);
835                 
836                 /* backdrop title */
837                 UI_ThemeColor(TH_TEXT_HI);
838         
839                 layout = uiBlockLayout(gnode->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL,
840                                        (int)(rect.xmin + NODE_MARGIN_X), (int)(rect.ymax + (group_header - (2.5f * dpi_fac))),
841                                        mini((int)(rect.xmax - rect.xmin - 18.0f), node_group_frame + 20), group_header, UI_GetStyle());
842                 RNA_pointer_create(&ntree->id, &RNA_Node, gnode, &ptr);
843                 uiTemplateIDBrowse(layout, (bContext *)C, &ptr, "node_tree", NULL, NULL, NULL);
844                 uiBlockLayoutResolve(gnode->block, NULL, NULL);
845         
846                 /* draw the internal tree nodes and links */
847                 node_draw_nodetree(C, ar, snode, ngroup);
848         
849                 /* group sockets */
850                 gsock = ngroup->inputs.first;
851                 sock = gnode->inputs.first;
852                 index = 0;
853                 while (gsock || sock) {
854                         while (sock && !sock->groupsock) {
855                                 draw_group_socket(C, snode, ntree, gnode, sock, NULL, index, SOCK_IN);
856                                 sock = sock->next;
857                         }
858                         while (gsock && (!sock || sock->groupsock != gsock)) {
859                                 draw_group_socket(C, snode, ntree, gnode, NULL, gsock, index, SOCK_IN);
860                                 gsock = gsock->next;
861                                 ++index;
862                         }
863                         while (sock && gsock && sock->groupsock == gsock) {
864                                 draw_group_socket(C, snode, ntree, gnode, sock, gsock, index, SOCK_IN);
865                                 sock = sock->next;
866                                 gsock = gsock->next;
867                                 ++index;
868                         }
869                 }
870                 gsock = ngroup->outputs.first;
871                 sock = gnode->outputs.first;
872                 index = 0;
873                 while (gsock || sock) {
874                         while (sock && !sock->groupsock) {
875                                 draw_group_socket(C, snode, ntree, gnode, sock, NULL, index, SOCK_OUT);
876                                 sock = sock->next;
877                         }
878                         while (gsock && (!sock || sock->groupsock != gsock)) {
879                                 draw_group_socket(C, snode, ntree, gnode, NULL, gsock, index, SOCK_OUT);
880                                 gsock = gsock->next;
881                                 ++index;
882                         }
883                         while (sock && gsock && sock->groupsock == gsock) {
884                                 draw_group_socket(C, snode, ntree, gnode, sock, gsock, index, SOCK_OUT);
885                                 sock = sock->next;
886                                 gsock = gsock->next;
887                                 ++index;
888                         }
889                 }
890                 
891                 uiEndBlock(C, gnode->block);
892                 uiDrawBlock(C, gnode->block);
893                 gnode->block = NULL;
894         }
895 }
896
897 void node_uifunc_group(uiLayout *layout, bContext *C, PointerRNA *ptr)
898 {
899         uiTemplateIDBrowse(layout, C, ptr, "node_tree", NULL, NULL, NULL);
900 }
901
902 static void node_common_buts_whileloop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
903 {
904         uiItemR(layout, ptr, "max_iterations", 0, NULL, 0);
905 }
906
907 /* XXX Does a bounding box update by iterating over all children.
908  * Not ideal to do this in every draw call, but doing as transform callback doesn't work,
909  * since the child node totr rects are not updated properly at that point.
910  */
911 static void node_update_frame(const bContext *UNUSED(C), bNodeTree *ntree, bNode *node)
912 {
913         const float margin = 30.0f;
914         NodeFrame *data = (NodeFrame *)node->storage;
915         int bbinit;
916         bNode *tnode;
917         rctf rect, noderect;
918         float xmax, ymax;
919         
920         /* init rect from current frame size */
921         nodeToView(node, node->offsetx, node->offsety, &rect.xmin, &rect.ymax);
922         nodeToView(node, node->offsetx + node->width, node->offsety - node->height, &rect.xmax, &rect.ymin);
923         
924         /* frame can be resized manually only if shrinking is disabled or no children are attached */
925         data->flag |= NODE_FRAME_RESIZEABLE;
926         /* for shrinking bbox, initialize the rect from first child node */
927         bbinit = (data->flag & NODE_FRAME_SHRINK);
928         /* fit bounding box to all children */
929         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
930                 if (tnode->parent != node)
931                         continue;
932                 
933                 /* add margin to node rect */
934                 noderect = tnode->totr;
935                 noderect.xmin -= margin;
936                 noderect.xmax += margin;
937                 noderect.ymin -= margin;
938                 noderect.ymax += margin;
939                 
940                 /* first child initializes frame */
941                 if (bbinit) {
942                         bbinit = 0;
943                         rect = noderect;
944                         data->flag &= ~NODE_FRAME_RESIZEABLE;
945                 }
946                 else
947                         BLI_rctf_union(&rect, &noderect);
948         }
949         
950         /* now adjust the frame size from view-space bounding box */
951         nodeFromView(node, rect.xmin, rect.ymax, &node->offsetx, &node->offsety);
952         nodeFromView(node, rect.xmax, rect.ymin, &xmax, &ymax);
953         node->width = xmax - node->offsetx;
954         node->height = -ymax + node->offsety;
955         
956         node->totr = rect;
957 }
958
959 static void node_draw_frame_label(bNode *node, const float aspect)
960 {
961         /* XXX font id is crap design */
962         const int fontid = UI_GetStyle()->widgetlabel.uifont_id;
963         NodeFrame *data = (NodeFrame *)node->storage;
964         rctf *rct = &node->totr;
965         int color_id = node_get_colorid(node);
966         const char *label = nodeLabel(node);
967         /* XXX a bit hacky, should use separate align values for x and y */
968         float width, ascender;
969         float x, y;
970         const int font_size = data->label_size / aspect;
971
972         BLF_enable(fontid, BLF_ASPECT);
973         BLF_aspect(fontid, aspect, aspect, 1.0f);
974         BLF_size(fontid, MIN2(24, font_size), U.dpi); /* clamp otherwise it can suck up a LOT of memory */
975         
976         /* title color */
977         UI_ThemeColorBlendShade(TH_TEXT, color_id, 0.8f, 10);
978
979         width = BLF_width(fontid, label);
980         ascender = BLF_ascender(fontid);
981         
982         /* 'x' doesn't need aspect correction */
983         x = 0.5f * (rct->xmin + rct->xmax) - 0.5f * width;
984         y = rct->ymax - (((NODE_DY / 4) / aspect) + (ascender * aspect));
985
986         BLF_position(fontid, x, y, 0);
987         BLF_draw(fontid, label, BLF_DRAW_STR_DUMMY_MAX);
988
989         BLF_disable(fontid, BLF_ASPECT);
990 }
991
992 static void node_draw_frame(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *UNUSED(ntree), bNode *node)
993 {
994         rctf *rct = &node->totr;
995         int color_id = node_get_colorid(node);
996         unsigned char color[4];
997         float alpha;
998         
999         /* skip if out of view */
1000         if (BLI_rctf_isect(&node->totr, &ar->v2d.cur, NULL) == FALSE) {
1001                 uiEndBlock(C, node->block);
1002                 node->block = NULL;
1003                 return;
1004         }
1005
1006         UI_GetThemeColor4ubv(TH_NODE_FRAME, color);
1007         alpha = (float)(color[3]) / 255.0f;
1008         
1009         /* shadow */
1010         node_draw_shadow(snode, node, BASIS_RAD, alpha);
1011         
1012         /* body */
1013         if (node->flag & NODE_CUSTOM_COLOR)
1014                 glColor4f(node->color[0], node->color[1], node->color[2], alpha);
1015         else
1016                 UI_ThemeColor4(TH_NODE_FRAME);
1017         glEnable(GL_BLEND);
1018         uiSetRoundBox(UI_CNR_ALL);
1019         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
1020         glDisable(GL_BLEND);
1021
1022         /* outline active and selected emphasis */
1023         if (node->flag & SELECT) {
1024                 glEnable(GL_BLEND);
1025                 glEnable(GL_LINE_SMOOTH);
1026                 
1027                 if (node->flag & NODE_ACTIVE)
1028                         UI_ThemeColorShadeAlpha(TH_ACTIVE, 0, -40);
1029                 else
1030                         UI_ThemeColorShadeAlpha(TH_SELECT, 0, -40);
1031                 uiSetRoundBox(UI_CNR_ALL);
1032                 uiDrawBox(GL_LINE_LOOP,
1033                           rct->xmin, rct->ymin,
1034                           rct->xmax, rct->ymax, BASIS_RAD);
1035                 
1036                 glDisable(GL_LINE_SMOOTH);
1037                 glDisable(GL_BLEND);
1038         }
1039         
1040         /* label */
1041         node_draw_frame_label(node, snode->aspect);
1042         
1043         UI_ThemeClearColor(color_id);
1044                 
1045         uiEndBlock(C, node->block);
1046         uiDrawBlock(C, node->block);
1047         node->block = NULL;
1048 }
1049
1050 static int node_resize_area_frame(bNode *node, int x, int y)
1051 {
1052         const float size = 10.0f;
1053         NodeFrame *data = (NodeFrame *)node->storage;
1054         rctf totr = node->totr;
1055         int dir = 0;
1056         
1057         /* shrinking frame size is determined by child nodes */
1058         if (!(data->flag & NODE_FRAME_RESIZEABLE))
1059                 return 0;
1060         
1061         if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
1062                 dir |= NODE_RESIZE_RIGHT;
1063         if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
1064                 dir |= NODE_RESIZE_LEFT;
1065         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymax - size && y < totr.ymax)
1066                 dir |= NODE_RESIZE_TOP;
1067         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymin && y < totr.ymin + size)
1068                 dir |= NODE_RESIZE_BOTTOM;
1069         
1070         return dir;
1071 }
1072
1073 static void node_buts_frame_details(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1074 {
1075         uiItemR(layout, ptr, "label_size", 0, IFACE_("Label Size"), ICON_NONE);
1076         uiItemR(layout, ptr, "shrink", 0, IFACE_("Shrink"), ICON_NONE);
1077 }
1078
1079
1080 #define NODE_REROUTE_SIZE   8.0f
1081
1082 static void node_update_reroute(const bContext *UNUSED(C), bNodeTree *UNUSED(ntree), bNode *node)
1083 {
1084         bNodeSocket *nsock;
1085         float locx, locy;
1086         float size = NODE_REROUTE_SIZE;
1087         
1088         /* get "global" coords */
1089         nodeToView(node, 0.0f, 0.0f, &locx, &locy);
1090         
1091         /* reroute node has exactly one input and one output, both in the same place */
1092         nsock = node->outputs.first;
1093         nsock->locx = locx;
1094         nsock->locy = locy;
1095
1096         nsock = node->inputs.first;
1097         nsock->locx = locx;
1098         nsock->locy = locy;
1099
1100         node->width = size * 2;
1101         node->totr.xmin = locx - size;
1102         node->totr.xmax = locx + size;
1103         node->totr.ymax = locy + size;
1104         node->totr.ymin = locy - size;
1105 }
1106
1107 static void node_draw_reroute(const bContext *C, ARegion *ar, SpaceNode *UNUSED(snode),  bNodeTree *ntree, bNode *node)
1108 {
1109         bNodeSocket *sock;
1110 #if 0   /* UNUSED */
1111         rctf *rct = &node->totr;
1112         float size = NODE_REROUTE_SIZE;
1113 #endif
1114         float socket_size = NODE_SOCKSIZE;
1115
1116         /* skip if out of view */
1117         if (node->totr.xmax < ar->v2d.cur.xmin || node->totr.xmin > ar->v2d.cur.xmax ||
1118             node->totr.ymax < ar->v2d.cur.ymin || node->totr.ymin > ar->v2d.cur.ymax) {
1119
1120                 uiEndBlock(C, node->block);
1121                 node->block = NULL;
1122                 return;
1123         }
1124
1125         /* XXX only kept for debugging
1126          * selection state is indicated by socket outline below!
1127          */
1128 #if 0
1129         /* body */
1130         uiSetRoundBox(15);
1131         UI_ThemeColor4(TH_NODE);
1132         glEnable(GL_BLEND);
1133         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
1134         glDisable(GL_BLEND);
1135
1136         /* outline active and selected emphasis */
1137         if (node->flag & SELECT) {
1138                 glEnable(GL_BLEND);
1139                 glEnable(GL_LINE_SMOOTH);
1140                 /* using different shades of TH_TEXT_HI for the empasis, like triangle */
1141                 if (node->flag & NODE_ACTIVE)
1142                         UI_ThemeColorShadeAlpha(TH_TEXT_HI, 0, -40);
1143                 else
1144                         UI_ThemeColorShadeAlpha(TH_TEXT_HI, -20, -120);
1145                 uiDrawBox(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
1146
1147                 glDisable(GL_LINE_SMOOTH);
1148                 glDisable(GL_BLEND);
1149         }
1150 #endif
1151
1152         /* only draw input socket. as they all are placed on the same position.
1153          * highlight also if node itself is selected, since we don't display the node body separately!
1154          */
1155         for (sock = node->inputs.first; sock; sock = sock->next) {
1156                 node_socket_circle_draw(ntree, sock, socket_size, (sock->flag & SELECT) || (node->flag & SELECT));
1157         }
1158
1159         uiEndBlock(C, node->block);
1160         uiDrawBlock(C, node->block);
1161         node->block = NULL;
1162 }
1163
1164 /* Special tweak area for reroute node.
1165  * Since this node is quite small, we use a larger tweak area for grabbing than for selection.
1166  */
1167 static int node_tweak_area_reroute(bNode *node, int x, int y)
1168 {
1169         /* square of tweak radius */
1170         static const float tweak_radius_sq = 576;  /* 24 * 24 */
1171         
1172         bNodeSocket *sock = node->inputs.first;
1173         float dx = sock->locx - x;
1174         float dy = sock->locy - y;
1175         return (dx * dx + dy * dy <= tweak_radius_sq);
1176 }
1177
1178 static void node_common_set_butfunc(bNodeType *ntype)
1179 {
1180         switch (ntype->type) {
1181                 case NODE_GROUP:
1182                         ntype->uifunc = node_uifunc_group;
1183                         ntype->drawfunc = node_draw_group;
1184                         ntype->drawupdatefunc = node_update_group;
1185                         break;
1186                 case NODE_FORLOOP:
1187 //                      ntype->uifunc= node_common_buts_group;
1188                         ntype->drawfunc = node_draw_group;
1189                         ntype->drawupdatefunc = node_update_group;
1190                         break;
1191                 case NODE_WHILELOOP:
1192                         ntype->uifunc = node_common_buts_whileloop;
1193                         ntype->drawfunc = node_draw_group;
1194                         ntype->drawupdatefunc = node_update_group;
1195                         break;
1196                 case NODE_FRAME:
1197                         ntype->drawfunc = node_draw_frame;
1198                         ntype->drawupdatefunc = node_update_frame;
1199                         ntype->uifuncbut = node_buts_frame_details;
1200                         ntype->resize_area_func = node_resize_area_frame;
1201                         break;
1202                 case NODE_REROUTE:
1203                         ntype->drawfunc = node_draw_reroute;
1204                         ntype->drawupdatefunc = node_update_reroute;
1205                         ntype->tweak_area_func = node_tweak_area_reroute;
1206                         break;
1207         }
1208 }
1209
1210 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
1211
1212 static void node_buts_image_user(uiLayout *layout, bContext *C, PointerRNA *ptr,
1213                                  PointerRNA *imaptr, PointerRNA *iuserptr)
1214 {
1215         uiLayout *col;
1216         int source;
1217
1218         if (!imaptr->data)
1219                 return;
1220
1221         col = uiLayoutColumn(layout, FALSE);
1222         
1223         uiItemR(col, imaptr, "source", 0, "", ICON_NONE);
1224         
1225         source = RNA_enum_get(imaptr, "source");
1226
1227         if (source == IMA_SRC_SEQUENCE) {
1228                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
1229                 Scene *scene = CTX_data_scene(C);
1230                 ImageUser *iuser = iuserptr->data;
1231                 char numstr[32];
1232                 const int framenr = BKE_image_user_frame_get(iuser, CFRA, 0, NULL);
1233                 BLI_snprintf(numstr, sizeof(numstr), IFACE_("Frame: %d"), framenr);
1234                 uiItemL(layout, numstr, ICON_NONE);
1235         }
1236
1237         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1238                 col = uiLayoutColumn(layout, TRUE);
1239                 uiItemR(col, ptr, "frame_duration", 0, NULL, ICON_NONE);
1240                 uiItemR(col, ptr, "frame_start", 0, NULL, ICON_NONE);
1241                 uiItemR(col, ptr, "frame_offset", 0, NULL, ICON_NONE);
1242                 uiItemR(col, ptr, "use_cyclic", 0, NULL, ICON_NONE);
1243                 uiItemR(col, ptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
1244         }
1245
1246         col = uiLayoutColumn(layout, FALSE);
1247
1248         if (RNA_enum_get(imaptr, "type") == IMA_TYPE_MULTILAYER)
1249                 uiItemR(col, ptr, "layer", 0, NULL, ICON_NONE);
1250 }
1251
1252 static void node_shader_buts_material(uiLayout *layout, bContext *C, PointerRNA *ptr)
1253 {
1254         bNode *node = ptr->data;
1255         uiLayout *col;
1256         
1257         uiTemplateID(layout, C, ptr, "material", "MATERIAL_OT_new", NULL, NULL);
1258         
1259         if (!node->id) return;
1260         
1261         col = uiLayoutColumn(layout, FALSE);
1262         uiItemR(col, ptr, "use_diffuse", 0, NULL, ICON_NONE);
1263         uiItemR(col, ptr, "use_specular", 0, NULL, ICON_NONE);
1264         uiItemR(col, ptr, "invert_normal", 0, NULL, ICON_NONE);
1265 }
1266
1267 static void node_shader_buts_mapping(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1268 {
1269         uiLayout *row;
1270         
1271         uiItemL(layout, IFACE_("Location:"), ICON_NONE);
1272         row = uiLayoutRow(layout, TRUE);
1273         uiItemR(row, ptr, "translation", 0, "", ICON_NONE);
1274         
1275         uiItemL(layout, IFACE_("Rotation:"), ICON_NONE);
1276         row = uiLayoutRow(layout, TRUE);
1277         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
1278         
1279         uiItemL(layout, IFACE_("Scale:"), ICON_NONE);
1280         row = uiLayoutRow(layout, TRUE);
1281         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
1282         
1283         row = uiLayoutRow(layout, TRUE);
1284         uiItemR(row, ptr, "use_min", 0, "Min", ICON_NONE);
1285         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
1286         
1287         row = uiLayoutRow(layout, TRUE);
1288         uiItemR(row, ptr, "use_max", 0, "Max", ICON_NONE);
1289         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
1290 }
1291
1292 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1293
1294         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
1295 }
1296
1297 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
1298 {
1299         PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
1300         uiLayout *col;
1301
1302         col = uiLayoutColumn(layout, FALSE);
1303
1304         if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
1305                 PointerRNA dataptr = RNA_pointer_get(&obptr, "data");
1306
1307                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
1308                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
1309         }
1310         else {
1311                 uiItemR(col, ptr, "uv_layer", 0, IFACE_("UV"), ICON_NONE);
1312                 uiItemR(col, ptr, "color_layer", 0, IFACE_("VCol"), ICON_NONE);
1313         }
1314 }
1315
1316 static void node_shader_buts_attribute(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1317 {
1318         uiItemR(layout, ptr, "attribute_name", 0, IFACE_("Name"), ICON_NONE);
1319 }
1320
1321 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1322 {
1323         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
1324         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
1325
1326         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1327         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1328         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
1329
1330         if(RNA_enum_get(ptr, "projection") == SHD_PROJ_BOX)
1331                 uiItemR(layout, ptr, "projection_blend", 0, "Blend", ICON_NONE);
1332
1333         /* note: image user properties used directly here, unlike compositor image node,
1334          * which redefines them in the node struct RNA to get proper updates.
1335          */
1336         node_buts_image_user(layout, C, &iuserptr, &imaptr, &iuserptr);
1337 }
1338
1339 static void node_shader_buts_tex_environment(uiLayout *layout, bContext *C, PointerRNA *ptr)
1340 {
1341         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
1342         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
1343
1344         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1345         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1346         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
1347
1348         node_buts_image_user(layout, C, ptr, &imaptr, &iuserptr);
1349 }
1350
1351 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1352 {
1353         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
1354         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
1355 }
1356
1357 static void node_shader_buts_tex_gradient(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1358 {
1359         uiItemR(layout, ptr, "gradient_type", 0, "", ICON_NONE);
1360 }
1361
1362 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1363 {
1364         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1365 }
1366
1367 static void node_shader_buts_tex_wave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1368 {
1369         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
1370 }
1371
1372 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1373 {
1374         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
1375 }
1376
1377 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1378 {
1379         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
1380 }
1381
1382 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1383 {
1384         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
1385 }
1386
1387 /* only once called */
1388 static void node_shader_set_butfunc(bNodeType *ntype)
1389 {
1390         switch (ntype->type) {
1391                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1392
1393                 case SH_NODE_MATERIAL:
1394                 case SH_NODE_MATERIAL_EXT:
1395                         ntype->uifunc = node_shader_buts_material;
1396                         break;
1397                 case SH_NODE_TEXTURE:
1398                         ntype->uifunc = node_buts_texture;
1399                         break;
1400                 case SH_NODE_NORMAL:
1401                         ntype->uifunc = node_buts_normal;
1402                         break;
1403                 case SH_NODE_CURVE_VEC:
1404                         ntype->uifunc = node_buts_curvevec;
1405                         break;
1406                 case SH_NODE_CURVE_RGB:
1407                         ntype->uifunc = node_buts_curvecol;
1408                         break;
1409                 case SH_NODE_MAPPING:
1410                         ntype->uifunc = node_shader_buts_mapping;
1411                         break;
1412                 case SH_NODE_VALUE:
1413                         ntype->uifunc = node_buts_value;
1414                         break;
1415                 case SH_NODE_RGB:
1416                         ntype->uifunc = node_buts_rgb;
1417                         break;
1418                 case SH_NODE_MIX_RGB:
1419                         ntype->uifunc = node_buts_mix_rgb;
1420                         break;
1421                 case SH_NODE_VALTORGB:
1422                         ntype->uifunc = node_buts_colorramp;
1423                         break;
1424                 case SH_NODE_MATH: 
1425                         ntype->uifunc = node_buts_math;
1426                         break; 
1427                 case SH_NODE_VECT_MATH: 
1428                         ntype->uifunc = node_shader_buts_vect_math;
1429                         break; 
1430                 case SH_NODE_GEOMETRY:
1431                         ntype->uifunc = node_shader_buts_geometry;
1432                         break;
1433                 case SH_NODE_ATTRIBUTE:
1434                         ntype->uifunc = node_shader_buts_attribute;
1435                         break;
1436                 case SH_NODE_TEX_SKY:
1437                         ntype->uifunc = node_shader_buts_tex_sky;
1438                         break;
1439                 case SH_NODE_TEX_IMAGE:
1440                         ntype->uifunc = node_shader_buts_tex_image;
1441                         break;
1442                 case SH_NODE_TEX_ENVIRONMENT:
1443                         ntype->uifunc = node_shader_buts_tex_environment;
1444                         break;
1445                 case SH_NODE_TEX_GRADIENT:
1446                         ntype->uifunc = node_shader_buts_tex_gradient;
1447                         break;
1448                 case SH_NODE_TEX_MAGIC:
1449                         ntype->uifunc = node_shader_buts_tex_magic;
1450                         break;
1451                 case SH_NODE_TEX_WAVE:
1452                         ntype->uifunc = node_shader_buts_tex_wave;
1453                         break;
1454                 case SH_NODE_TEX_MUSGRAVE:
1455                         ntype->uifunc = node_shader_buts_tex_musgrave;
1456                         break;
1457                 case SH_NODE_TEX_VORONOI:
1458                         ntype->uifunc = node_shader_buts_tex_voronoi;
1459                         break;
1460                 case SH_NODE_BSDF_GLOSSY:
1461                 case SH_NODE_BSDF_GLASS:
1462                         ntype->uifunc = node_shader_buts_glossy;
1463                         break;
1464         }
1465 }
1466
1467 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
1468
1469 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1470 {
1471         bNode *node = ptr->data;
1472         PointerRNA imaptr, iuserptr;
1473         
1474         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1475         
1476         if (!node->id) return;
1477         
1478         imaptr = RNA_pointer_get(ptr, "image");
1479         RNA_pointer_create((ID *)ptr->id.data, &RNA_ImageUser, node->storage, &iuserptr);
1480         
1481         node_buts_image_user(layout, C, ptr, &imaptr, &iuserptr);
1482 }
1483
1484 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
1485 {
1486         bNode *node = ptr->data;
1487         uiLayout *col, *row;
1488         PointerRNA op_ptr;
1489         PointerRNA scn_ptr;
1490         PropertyRNA *prop;
1491         const char *layer_name;
1492         char scene_name[MAX_ID_NAME - 2];
1493         wmOperatorType *ot = WM_operatortype_find("RENDER_OT_render", 1);
1494
1495         BLI_assert(ot != 0);
1496
1497         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
1498         
1499         if (!node->id) return;
1500
1501         col = uiLayoutColumn(layout, FALSE);
1502         row = uiLayoutRow(col, FALSE);
1503         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
1504         
1505         prop = RNA_struct_find_property(ptr, "layer");
1506         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
1507                 return;
1508         
1509         scn_ptr = RNA_pointer_get(ptr, "scene");
1510         RNA_string_get(&scn_ptr, "name", scene_name);
1511         
1512         WM_operator_properties_create_ptr(&op_ptr, ot);
1513         RNA_string_set(&op_ptr, "layer", layer_name);
1514         RNA_string_set(&op_ptr, "scene", scene_name);
1515         uiItemFullO_ptr(row, ot, "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
1516
1517 }
1518
1519
1520 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1521 {
1522         uiLayout *col, *row;
1523         int reference;
1524         int filter;
1525         
1526         col = uiLayoutColumn(layout, FALSE);
1527         filter = RNA_enum_get(ptr, "filter_type");
1528         reference = RNA_boolean_get(ptr, "use_variable_size");
1529
1530         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
1531         if (filter != R_FILTER_FAST_GAUSS) {
1532                 uiItemR(col, ptr, "use_variable_size", 0, NULL, ICON_NONE);
1533                 if (!reference) {
1534                         uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
1535                 }
1536                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1537         }
1538         
1539         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
1540         
1541         if (RNA_boolean_get(ptr, "use_relative")) {
1542                 uiItemL(col, IFACE_("Aspect Correction"), 0);
1543                 row = uiLayoutRow(layout, TRUE);
1544                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, 0);
1545                 
1546                 col = uiLayoutColumn(layout, TRUE);
1547                 uiItemR(col, ptr, "factor_x", 0, IFACE_("X"), ICON_NONE);
1548                 uiItemR(col, ptr, "factor_y", 0, IFACE_("Y"), ICON_NONE);
1549         }
1550         else {
1551                 col = uiLayoutColumn(layout, TRUE);
1552                 uiItemR(col, ptr, "size_x", 0, IFACE_("X"), ICON_NONE);
1553                 uiItemR(col, ptr, "size_y", 0, IFACE_("Y"), ICON_NONE);
1554         }
1555 }
1556
1557 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1558 {
1559         uiLayout *col;
1560         
1561         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1562         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
1563         
1564         col = uiLayoutColumn(layout, TRUE);
1565         uiItemL(col, IFACE_("Center:"), ICON_NONE);
1566         uiItemR(col, ptr, "center_x", 0, IFACE_("X"), ICON_NONE);
1567         uiItemR(col, ptr, "center_y", 0, IFACE_("Y"), ICON_NONE);
1568         
1569         uiItemS(layout);
1570         
1571         col = uiLayoutColumn(layout, TRUE);
1572         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
1573         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1574         
1575         uiItemS(layout);
1576         
1577         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
1578         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
1579 }
1580
1581 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1582 {       
1583         uiLayout *col;
1584         
1585         col = uiLayoutColumn(layout, TRUE);
1586         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
1587         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
1588         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
1589 }
1590
1591 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1592 {
1593         uiLayout *sub, *col;
1594         
1595         col = uiLayoutColumn(layout, FALSE);
1596         uiItemL(col, IFACE_("Bokeh Type:"), ICON_NONE);
1597         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
1598         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1599
1600         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1601
1602         col = uiLayoutColumn(layout, FALSE);
1603         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer") == TRUE);
1604         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
1605
1606         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
1607         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1608
1609         col = uiLayoutColumn(layout, FALSE);
1610         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
1611         
1612         col = uiLayoutColumn(layout, FALSE);
1613         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
1614         sub = uiLayoutColumn(col, FALSE);
1615         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer") == FALSE);
1616         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
1617 }
1618
1619 /* qdn: glare node */
1620 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1621 {       
1622         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
1623         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
1624
1625         if (RNA_enum_get(ptr, "glare_type") != 1) {
1626                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1627         
1628                 if (RNA_enum_get(ptr, "glare_type") != 0)
1629                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1630         }
1631         
1632         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
1633         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1634
1635         if (RNA_enum_get(ptr, "glare_type") == 2) {
1636                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
1637                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
1638         }
1639         if (RNA_enum_get(ptr, "glare_type") == 0 || RNA_enum_get(ptr, "glare_type") == 2) {
1640                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1641                 
1642                 if (RNA_enum_get(ptr, "glare_type") == 0)
1643                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
1644         }
1645         if (RNA_enum_get(ptr, "glare_type") == 1) {
1646                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
1647         }
1648 }
1649
1650 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1651 {       
1652         uiLayout *col;
1653
1654         col = uiLayoutColumn(layout, FALSE);
1655         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
1656         if (RNA_enum_get(ptr, "tonemap_type") == 0) {
1657                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1658                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1659                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
1660         }
1661         else {
1662                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
1663                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1664                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1665                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1666         }
1667 }
1668
1669 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1670 {
1671         uiLayout *col;
1672
1673         col = uiLayoutColumn(layout, FALSE);
1674         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
1675
1676         col = uiLayoutColumn(col, FALSE);
1677         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector") == FALSE);
1678         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
1679         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
1680 }
1681
1682 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1683 {
1684         uiLayout *col;
1685         
1686         col = uiLayoutColumn(layout, FALSE);
1687         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
1688         uiItemR(col, ptr, "factor", 0, IFACE_("Blur"), ICON_NONE);
1689         
1690         col = uiLayoutColumn(layout, TRUE);
1691         uiItemL(col, IFACE_("Speed:"), ICON_NONE);
1692         uiItemR(col, ptr, "speed_min", 0, IFACE_("Min"), ICON_NONE);
1693         uiItemR(col, ptr, "speed_max", 0, IFACE_("Max"), ICON_NONE);
1694
1695         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
1696 }
1697
1698 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1699 {
1700         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1701 }
1702
1703 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1704 {
1705         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1706 }
1707
1708 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1709 {
1710         uiLayout *col;
1711
1712         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
1713         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
1714
1715         col = uiLayoutColumn(layout, TRUE);
1716         if (RNA_boolean_get(ptr, "relative")) {
1717                 uiItemR(col, ptr, "rel_min_x", 0, IFACE_("Left"), ICON_NONE);
1718                 uiItemR(col, ptr, "rel_max_x", 0, IFACE_("Right"), ICON_NONE);
1719                 uiItemR(col, ptr, "rel_min_y", 0, IFACE_("Up"), ICON_NONE);
1720                 uiItemR(col, ptr, "rel_max_y", 0, IFACE_("Down"), ICON_NONE);
1721         }
1722         else {
1723                 uiItemR(col, ptr, "min_x", 0, IFACE_("Left"), ICON_NONE);
1724                 uiItemR(col, ptr, "max_x", 0, IFACE_("Right"), ICON_NONE);
1725                 uiItemR(col, ptr, "min_y", 0, IFACE_("Up"), ICON_NONE);
1726                 uiItemR(col, ptr, "max_y", 0, IFACE_("Down"), ICON_NONE);
1727         }
1728 }
1729
1730 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1731 {
1732         uiLayout *row, *col;
1733         
1734         col = uiLayoutColumn(layout, FALSE);
1735         row = uiLayoutRow(col, FALSE);
1736         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1737         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
1738 }
1739
1740 static void node_composit_buts_double_edge_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1741 {
1742         uiLayout *col;
1743
1744         col = uiLayoutColumn(layout, FALSE);
1745
1746         uiItemL(col, IFACE_("Inner Edge:"), ICON_NONE);
1747         uiItemR(col, ptr, "inner_mode", 0, "", ICON_NONE);
1748         uiItemL(col, IFACE_("Buffer Edge:"), ICON_NONE);
1749         uiItemR(col, ptr, "edge_mode", 0, "", ICON_NONE);
1750 }
1751
1752 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1753 {
1754         uiLayout *sub, *col;
1755         
1756         col = uiLayoutColumn(layout, TRUE);
1757         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1758         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
1759         
1760         col = uiLayoutColumn(layout, TRUE);
1761         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
1762         sub = uiLayoutColumn(col, FALSE);
1763         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1764         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
1765         
1766         col = uiLayoutColumn(layout, TRUE);
1767         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
1768         sub = uiLayoutColumn(col, FALSE);
1769         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1770         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
1771 }
1772
1773 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1774 {       
1775         uiLayout *col;
1776         
1777         col = uiLayoutColumn(layout, TRUE);
1778         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
1779         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
1780 }
1781
1782 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1783 {       
1784         uiLayout *col;
1785         
1786         col = uiLayoutColumn(layout, TRUE);
1787         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
1788 }
1789
1790
1791 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1792 {
1793         uiLayout *col;
1794         
1795         col = uiLayoutColumn(layout, FALSE);
1796         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1797         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1798         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1799 }
1800
1801 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1802 {
1803         uiItemR(layout, ptr, "type", 0, NULL, ICON_NONE);
1804         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1805         switch (RNA_enum_get(ptr, "type")) {
1806                 case CMP_NODE_DILATEERODE_DISTANCE_THRESH:
1807                         uiItemR(layout, ptr, "edge", 0, NULL, ICON_NONE);
1808                         break;
1809                 case CMP_NODE_DILATEERODE_DISTANCE_FEATHER:
1810                         uiItemR(layout, ptr, "falloff", 0, NULL, ICON_NONE);
1811                         break;
1812         }
1813 }
1814
1815 static void node_composit_buts_inpaint(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1816 {
1817         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1818 }
1819
1820 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1821 {
1822         uiLayout *col;
1823         
1824         col = uiLayoutColumn(layout, TRUE);
1825         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1826         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1827 }
1828
1829 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1830 {
1831         uiLayout *col, *row;
1832         
1833         col = uiLayoutColumn(layout, TRUE);
1834    
1835         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1836         row = uiLayoutRow(layout, FALSE);
1837         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1838
1839         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1840         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1841 }
1842
1843 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1844 {
1845         uiLayout *row, *col;
1846         
1847         uiItemL(layout, IFACE_("Despill Channel:"), ICON_NONE);
1848         row = uiLayoutRow(layout, FALSE);
1849         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1850
1851         col = uiLayoutColumn(layout, FALSE);
1852         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1853
1854         if (RNA_enum_get(ptr, "limit_method") == 0) {
1855                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1856                 row = uiLayoutRow(col, FALSE);
1857                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1858         }
1859
1860         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1861         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1862         if (RNA_boolean_get(ptr, "use_unspill") == TRUE) {
1863                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1864                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1865                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1866         }
1867 }
1868
1869 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1870 {
1871         uiLayout *col;
1872         
1873         col = uiLayoutColumn(layout, FALSE);
1874         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1875         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1876         
1877         col = uiLayoutColumn(layout, TRUE);
1878         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1879         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1880         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1881 }
1882
1883 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1884 {
1885         uiLayout *col;
1886         
1887         col = uiLayoutColumn(layout, TRUE);
1888         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1889         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1890         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1891 }
1892
1893 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1894 {       
1895         uiLayout *col, *row;
1896
1897         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1898         row = uiLayoutRow(layout, FALSE);
1899         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1900
1901         col = uiLayoutColumn(layout, FALSE);
1902         uiItemL(col, IFACE_("Key Channel:"), ICON_NONE);
1903         row = uiLayoutRow(col, FALSE);
1904         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1905
1906         col = uiLayoutColumn(layout, FALSE);
1907
1908         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1909         if (RNA_enum_get(ptr, "limit_method") == 0) {
1910                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1911                 row = uiLayoutRow(col, FALSE);
1912                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1913         }
1914
1915         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1916         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1917 }
1918
1919 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1920 {
1921         uiLayout *col;
1922         
1923         col = uiLayoutColumn(layout, TRUE);
1924         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1925         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1926 }
1927
1928 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1929 {
1930         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1931 }
1932
1933 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1934 {
1935         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1936         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
1937 }
1938
1939 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1940 static void node_draw_input_file_output(const bContext *C, uiBlock *block,
1941                                         bNodeTree *ntree, bNode *node, bNodeSocket *sock,
1942                                         const char *UNUSED(name), int x, int y, int width)
1943 {
1944         uiLayout *layout, *row;
1945         PointerRNA nodeptr, inputptr, imfptr;
1946         int imtype;
1947         int rx, ry;
1948         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
1949         
1950         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, x, y + NODE_DY, width, 20, UI_GetStyle());
1951         row = uiLayoutRow(layout, FALSE);
1952         
1953         imfptr = RNA_pointer_get(&nodeptr, "format");
1954         imtype = RNA_enum_get(&imfptr, "file_format");
1955         if (imtype == R_IMF_IMTYPE_MULTILAYER) {
1956                 NodeImageMultiFileSocket *input = sock->storage;
1957                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotLayer, input, &inputptr);
1958                 
1959                 uiItemL(row, input->layer, 0);
1960         }
1961         else {
1962                 NodeImageMultiFileSocket *input = sock->storage;
1963                 PropertyRNA *imtype_prop;
1964                 const char *imtype_name;
1965                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotFile, input, &inputptr);
1966                 
1967                 uiItemL(row, input->path, 0);
1968                 
1969                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1970                         imfptr = RNA_pointer_get(&inputptr, "format");
1971                 
1972                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1973                 RNA_property_enum_name((bContext *)C, &imfptr, imtype_prop,
1974                                        RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1975                 uiBlockSetEmboss(block, UI_EMBOSSP);
1976                 uiItemL(row, imtype_name, 0);
1977                 uiBlockSetEmboss(block, UI_EMBOSSN);
1978         }
1979         
1980         uiBlockLayoutResolve(block, &rx, &ry);
1981 }
1982 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1983 {
1984         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1985         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1986         
1987         if (multilayer)
1988                 uiItemL(layout, IFACE_("Path:"), 0);
1989         else
1990                 uiItemL(layout, IFACE_("Base Path:"), 0);
1991         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1992 }
1993 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1994 {
1995         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1996         PointerRNA active_input_ptr, op_ptr;
1997         uiLayout *row;
1998         int active_index;
1999         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
2000         
2001         node_composit_buts_file_output(layout, C, ptr);
2002         uiTemplateImageSettings(layout, &imfptr);
2003         
2004         uiItemS(layout);
2005         
2006         uiItemO(layout, IFACE_("Add Input"), ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
2007         
2008         active_index = RNA_int_get(ptr, "active_input_index");
2009         /* using different collection properties if multilayer format is enabled */
2010         if (multilayer) {
2011                 uiTemplateList(layout, C, ptr, "layer_slots", ptr, "active_input_index", NULL, 0, 0, 0);
2012                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "layer_slots"),
2013                                                    active_index, &active_input_ptr);
2014         }
2015         else {
2016                 uiTemplateList(layout, C, ptr, "file_slots", ptr, "active_input_index", NULL, 0, 0, 0);
2017                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_slots"),
2018                                                    active_index, &active_input_ptr);
2019         }
2020         /* XXX collection lookup does not return the ID part of the pointer, setting this manually here */
2021         active_input_ptr.id.data = ptr->id.data;
2022         
2023         row = uiLayoutRow(layout, TRUE);
2024         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "",
2025                              ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
2026         RNA_enum_set(&op_ptr, "direction", 1);
2027         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "",
2028                              ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
2029         RNA_enum_set(&op_ptr, "direction", 2);
2030         
2031         if (active_input_ptr.data) {
2032                 if (multilayer) {
2033                         uiLayout *row, *col;
2034                         col = uiLayoutColumn(layout, TRUE);
2035                         
2036                         uiItemL(col, IFACE_("Layer:"), 0);
2037                         row = uiLayoutRow(col, FALSE);
2038                         uiItemR(row, &active_input_ptr, "name", 0, "", 0);
2039                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "",
2040                                     ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2041                 }
2042                 else {
2043                         uiLayout *row, *col;
2044                         col = uiLayoutColumn(layout, TRUE);
2045                         
2046                         uiItemL(col, IFACE_("File Path:"), 0);
2047                         row = uiLayoutRow(col, FALSE);
2048                         uiItemR(row, &active_input_ptr, "path", 0, "", 0);
2049                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "",
2050                                     ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2051                         
2052                         /* format details for individual files */
2053                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
2054                         
2055                         col = uiLayoutColumn(layout, TRUE);
2056                         uiItemL(col, IFACE_("Format:"), 0);
2057                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, 0);
2058                         
2059                         col = uiLayoutColumn(layout, FALSE);
2060                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format") == FALSE);
2061                         uiTemplateImageSettings(col, &imfptr);
2062                 }
2063         }
2064 }
2065
2066 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2067 {
2068         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
2069
2070         if (RNA_enum_get(ptr, "space") == CMP_SCALE_RENDERPERCENT) {
2071                 uiLayout *row;
2072                 uiItemR(layout, ptr, "frame_method", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2073                 row = uiLayoutRow(layout, TRUE);
2074                 uiItemR(row, ptr, "offset_x", 0, "X", ICON_NONE);
2075                 uiItemR(row, ptr, "offset_y", 0, "Y", ICON_NONE);
2076         }
2077 }
2078
2079 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2080 {
2081         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
2082 }
2083
2084 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2085 {
2086         uiLayout *col;
2087         
2088         col = uiLayoutColumn(layout, FALSE);
2089         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
2090         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
2091 }
2092
2093 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2094 {
2095         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
2096 }
2097
2098 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2099 {
2100         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2101 }
2102
2103 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2104 {
2105         uiLayout *split, *col, *row;
2106         
2107         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2108         
2109         if (RNA_enum_get(ptr, "correction_method") == 0) {
2110         
2111                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2112                 col = uiLayoutColumn(split, FALSE);
2113                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
2114                 row = uiLayoutRow(col, FALSE);
2115                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
2116                 
2117                 col = uiLayoutColumn(split, FALSE);
2118                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
2119                 row = uiLayoutRow(col, FALSE);
2120                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
2121                 
2122                 col = uiLayoutColumn(split, FALSE);
2123                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
2124                 row = uiLayoutRow(col, FALSE);
2125                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
2126
2127         }
2128         else {
2129                 
2130                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2131                 col = uiLayoutColumn(split, FALSE);
2132                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
2133                 row = uiLayoutRow(col, FALSE);
2134                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
2135                 
2136                 col = uiLayoutColumn(split, FALSE);
2137                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
2138                 row = uiLayoutRow(col, FALSE);
2139                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
2140                 
2141                 col = uiLayoutColumn(split, FALSE);
2142                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
2143                 row = uiLayoutRow(col, FALSE);
2144                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
2145         }
2146
2147 }
2148 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2149 {
2150         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2151
2152         if (RNA_enum_get(ptr, "correction_method") == 0) {
2153
2154                 uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
2155                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
2156
2157                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
2158                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
2159
2160                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
2161                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
2162         }
2163         else {
2164                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
2165                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
2166
2167                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
2168                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
2169
2170                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
2171                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
2172         }
2173 }
2174
2175
2176 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2177 {
2178         bNode *node = ptr->data;
2179         CurveMapping *cumap = node->storage;
2180
2181         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
2182                 cumap->flag |= CUMA_DRAW_SAMPLE;
2183                 copy_v3_v3(cumap->sample, _sample_col);
2184         }
2185         else {
2186                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
2187         }
2188
2189         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
2190 }
2191
2192 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2193
2194         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
2195 }
2196
2197 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
2198 {
2199         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2200 }
2201
2202 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
2203 {
2204         bNode *node = ptr->data;
2205
2206         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2207
2208         if (!node->id)
2209                 return;
2210
2211         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2212 }
2213
2214 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2215 {
2216         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2217 }
2218
2219 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
2220 {
2221         bNode *node = ptr->data;
2222
2223         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2224
2225         if (!node->id)
2226                 return;
2227
2228         uiItemR(layout, ptr, "distortion_type", 0, "", 0);
2229 }
2230
2231 static void node_composit_buts_colorcorrection(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2232 {
2233         uiLayout *row;
2234         
2235         row = uiLayoutRow(layout, FALSE);
2236         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2237         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2238         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2239
2240         row = uiLayoutRow(layout, FALSE);
2241         uiItemL(row, "", 0);
2242         uiItemL(row, IFACE_("Saturation"), 0);
2243         uiItemL(row, IFACE_("Contrast"), 0);
2244         uiItemL(row, IFACE_("Gamma"), 0);
2245         uiItemL(row, IFACE_("Gain"), 0);
2246         uiItemL(row, IFACE_("Lift"), 0);
2247
2248         row = uiLayoutRow(layout, FALSE);
2249         uiItemL(row, IFACE_("Master"), 0);
2250         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2251         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2252         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2253         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2254         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2255
2256         row = uiLayoutRow(layout, FALSE);
2257         uiItemL(row, IFACE_("Highlights"), 0);
2258         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2259         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2260         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2261         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2262         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2263
2264         row = uiLayoutRow(layout, FALSE);
2265         uiItemL(row, IFACE_("Midtones"), 0);
2266         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2267         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2268         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2269         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2270         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2271
2272         row = uiLayoutRow(layout, FALSE);
2273         uiItemL(row, IFACE_("Shadows"), 0);
2274         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2275         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2276         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2277         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2278         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2279
2280         row = uiLayoutRow(layout, FALSE);
2281         uiItemR(row, ptr, "midtones_start", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2282         uiItemR(row, ptr, "midtones_end", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2283 }
2284
2285 static void node_composit_buts_colorcorrection_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2286 {
2287         uiLayout *row;
2288         
2289         row = uiLayoutRow(layout, FALSE);
2290         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2291         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2292         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2293         row = layout;
2294         uiItemL(row, IFACE_("Saturation"), 0);
2295         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2296         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2297         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2298         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2299
2300         uiItemL(row, IFACE_("Contrast"), 0);
2301         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2302         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2303         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2304         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2305
2306         uiItemL(row, IFACE_("Gamma"), 0);
2307         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2308         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2309         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2310         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2311
2312         uiItemL(row, IFACE_("Gain"), 0);
2313         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2314         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2315         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2316         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2317         
2318         uiItemL(row, IFACE_("Lift"), 0);
2319         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2320         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2321         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2322         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2323
2324         row = uiLayoutRow(layout, FALSE);
2325         uiItemR(row, ptr, "midtones_start", 0, NULL, ICON_NONE);
2326         uiItemR(row, ptr, "midtones_end", 0, NULL, ICON_NONE);
2327 }
2328
2329 static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2330 {
2331         uiItemR(layout, ptr, "check", 0, NULL, ICON_NONE);
2332 }
2333
2334 static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2335 {
2336         uiLayout *row;
2337         
2338         row = uiLayoutRow(layout, TRUE);
2339         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2340         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2341         
2342         row = uiLayoutRow(layout, TRUE);
2343         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2344         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2345
2346         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2347         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2348 }
2349
2350 static void node_composit_buts_bokehimage(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2351 {
2352         uiItemR(layout, ptr, "flaps", 0, NULL, ICON_NONE);
2353         uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
2354         uiItemR(layout, ptr, "rounding", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2355         uiItemR(layout, ptr, "catadioptric", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2356         uiItemR(layout, ptr, "shift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2357 }
2358
2359 static void node_composit_buts_bokehblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2360 {
2361         uiItemR(layout, ptr, "use_variable_size", 0, NULL, ICON_NONE);
2362         // uiItemR(layout, ptr, "f_stop", 0, NULL, ICON_NONE);  // UNUSED
2363         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
2364 }
2365
2366 void node_composit_backdrop_viewer(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2367 {
2368 //      node_composit_backdrop_canvas(snode, backdrop, node, x, y);
2369         if (node->custom1 == 0) {
2370                 const float backdropWidth = backdrop->x;
2371                 const float backdropHeight = backdrop->y;
2372                 const float cx  = x + snode->zoom * backdropWidth * node->custom3;
2373                 const float cy = y + snode->zoom * backdropHeight * node->custom4;
2374
2375                 glColor3f(1.0, 1.0, 1.0);
2376
2377                 glBegin(GL_LINES);
2378                 glVertex2f(cx - 25, cy - 25);
2379                 glVertex2f(cx + 25, cy + 25);
2380                 glVertex2f(cx + 25, cy - 25);
2381                 glVertex2f(cx - 25, cy + 25);
2382                 glEnd();
2383         }
2384 }
2385
2386 void node_composit_backdrop_boxmask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2387 {
2388         NodeBoxMask *boxmask = node->storage;
2389         const float backdropWidth = backdrop->x;
2390         const float backdropHeight = backdrop->y;
2391         const float aspect = backdropWidth / backdropHeight;
2392         const float rad = DEG2RADF(-boxmask->rotation);
2393         const float cosine = cosf(rad);
2394         const float sine = sinf(rad);
2395         const float halveBoxWidth = backdropWidth * (boxmask->width / 2.0f);
2396         const float halveBoxHeight = backdropHeight * (boxmask->height / 2.0f) * aspect;
2397
2398         float cx, cy, x1, x2, x3, x4;
2399         float y1, y2, y3, y4;
2400
2401
2402         /* keep this, saves us from a version patch */
2403         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2404
2405         glColor3f(1.0, 1.0, 1.0);
2406
2407         cx  = x + snode->zoom * backdropWidth * boxmask->x;
2408         cy = y + snode->zoom * backdropHeight * boxmask->y;
2409
2410         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2411         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2412         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2413         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2414         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2415         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2416         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2417         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2418
2419         glBegin(GL_LINE_LOOP);
2420         glVertex2f(x1, y1);
2421         glVertex2f(x2, y2);
2422         glVertex2f(x3, y3);
2423         glVertex2f(x4, y4);
2424         glEnd();
2425 }
2426
2427 void node_composit_backdrop_ellipsemask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2428 {
2429         NodeEllipseMask *ellipsemask = node->storage;
2430         const float backdropWidth = backdrop->x;
2431         const float backdropHeight = backdrop->y;
2432         const float aspect = backdropWidth / backdropHeight;
2433         const float rad = DEG2RADF(-ellipsemask->rotation);
2434         const float cosine = cosf(rad);
2435         const float sine = sinf(rad);
2436         const float halveBoxWidth = backdropWidth * (ellipsemask->width / 2.0f);
2437         const float halveBoxHeight = backdropHeight * (ellipsemask->height / 2.0f) * aspect;
2438
2439         float cx, cy, x1, x2, x3, x4;
2440         float y1, y2, y3, y4;
2441
2442
2443         /* keep this, saves us from a version patch */
2444         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2445
2446         glColor3f(1.0, 1.0, 1.0);
2447
2448         cx  = x + snode->zoom * backdropWidth * ellipsemask->x;
2449         cy = y + snode->zoom * backdropHeight * ellipsemask->y;
2450
2451         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2452         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2453         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2454         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2455         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2456         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2457         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2458         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2459
2460         glBegin(GL_LINE_LOOP);
2461
2462         glVertex2f(x1, y1);
2463         glVertex2f(x2, y2);
2464         glVertex2f(x3, y3);
2465         glVertex2f(x4, y4);
2466         glEnd();
2467 }
2468
2469 static void node_composit_buts_ellipsemask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2470 {
2471         uiLayout *row;
2472         row = uiLayoutRow(layout, TRUE);
2473         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2474         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2475         row = uiLayoutRow(layout, TRUE);
2476         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2477         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2478
2479         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2480         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2481 }
2482
2483 static void node_composit_buts_viewer_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2484 {
2485         uiLayout *col;
2486         
2487         uiItemR(layout, ptr, "tile_order", 0, NULL, ICON_NONE);
2488         if (RNA_enum_get(ptr, "tile_order") == 0) {
2489                 col = uiLayoutColumn(layout, TRUE);
2490                 uiItemR(col, ptr, "center_x", 0, NULL, ICON_NONE);
2491                 uiItemR(col, ptr, "center_y", 0, NULL, ICON_NONE);
2492         }
2493 }
2494
2495 static void node_composit_buts_mask(uiLayout *layout, bContext *C, PointerRNA *ptr)
2496 {
2497         bNode *node = ptr->data;
2498
2499         uiTemplateID(layout, C, ptr, "mask", NULL, NULL, NULL);
2500         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
2501         uiItemR(layout, ptr, "use_feather", 0, NULL, ICON_NONE);
2502
2503         uiItemR(layout, ptr, "size_source", 0, "", ICON_NONE);
2504
2505         if (node->custom1 & (CMP_NODEFLAG_MASK_FIXED | CMP_NODEFLAG_MASK_FIXED_SCENE)) {
2506                 uiItemR(layout, ptr, "size_x", 0, NULL, ICON_NONE);
2507                 uiItemR(layout, ptr, "size_y", 0, NULL, ICON_NONE);
2508         }
2509
2510         uiItemR(layout, ptr, "use_motion_blur", 0, NULL, ICON_NONE);
2511         if (node->custom1 & CMP_NODEFLAG_MASK_MOTION_BLUR) {
2512                 uiItemR(layout, ptr, "motion_blur_samples", 0, NULL, ICON_NONE);
2513                 uiItemR(layout, ptr, "motion_blur_shutter", 0, NULL, ICON_NONE);
2514         }
2515 }
2516
2517 static void node_composit_buts_keyingscreen(uiLayout *layout, bContext *C, PointerRNA *ptr)
2518 {
2519         bNode *node = ptr->data;
2520
2521         uiTemplateID(layout, C, ptr, "clip", NULL, NULL, NULL);
2522
2523         if (node->id) {
2524                 MovieClip *clip = (MovieClip *) node->id;
2525                 uiLayout *col;
2526                 PointerRNA tracking_ptr;
2527
2528                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, &clip->tracking, &tracking_ptr);
2529
2530                 col = uiLayoutColumn(layout, TRUE);
2531                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2532         }
2533 }
2534
2535 static void node_composit_buts_keying(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2536 {
2537         /* bNode *node= ptr->data; */ /* UNUSED */
2538
2539         uiItemR(layout, ptr, "blur_pre", 0, NULL, ICON_NONE);
2540         uiItemR(layout, ptr, "screen_balance", 0, NULL, ICON_NONE);
2541         uiItemR(layout, ptr, "despill_factor", 0, NULL, ICON_NONE);
2542         uiItemR(layout, ptr, "despill_balance", 0, NULL, ICON_NONE);
2543         uiItemR(layout, ptr, "edge_kernel_radius", 0, NULL, ICON_NONE);
2544         uiItemR(layout, ptr, "edge_kernel_tolerance", 0, NULL, ICON_NONE);
2545         uiItemR(layout, ptr, "clip_black", 0, NULL, ICON_NONE);
2546         uiItemR(layout, ptr, "clip_white", 0, NULL, ICON_NONE);
2547         uiItemR(layout, ptr, "dilate_distance", 0, NULL, ICON_NONE);
2548         uiItemR(layout, ptr, "feather_falloff", 0, NULL, ICON_NONE);
2549         uiItemR(layout, ptr, "feather_distance", 0, NULL, ICON_NONE);
2550         uiItemR(layout, ptr, "blur_post", 0, NULL, ICON_NONE);
2551 }
2552
2553 static void node_composit_buts_trackpos(uiLayout *layout, bContext *C, PointerRNA *ptr)
2554 {
2555         bNode *node = ptr->data;
2556
2557         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2558
2559         if (node->id) {
2560                 MovieClip *clip = (MovieClip *) node->id;
2561                 MovieTracking *tracking = &clip->tracking;
2562                 MovieTrackingObject *object;
2563                 uiLayout *col;
2564                 PointerRNA tracking_ptr;
2565                 NodeTrackPosData *data = node->storage;
2566
2567                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, tracking, &tracking_ptr);
2568
2569                 col = uiLayoutColumn(layout, 0);
2570                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2571
2572                 object = BKE_tracking_object_get_named(tracking, data->tracking_object);
2573                 if (object) {
2574                         PointerRNA object_ptr;
2575
2576                         RNA_pointer_create(&clip->id, &RNA_MovieTrackingObject, object, &object_ptr);
2577
2578                         uiItemPointerR(col, ptr, "track_name", &object_ptr, "tracks", "", ICON_ANIM_DATA);
2579                 }
2580                 else {
2581                         uiItemR(layout, ptr, "track_name", 0, "", ICON_ANIM_DATA);
2582                 }
2583
2584                 uiItemR(layout, ptr, "position", 0, NULL, ICON_NONE);
2585
2586                 if (node->custom1 == 2) {
2587                         uiItemR(layout, ptr, "relative_frame", 0, NULL, ICON_NONE);
2588                 }
2589         }
2590 }
2591
2592 /* only once called */
2593 static void node_composit_set_butfunc(bNodeType *ntype)
2594 {
2595         switch (ntype->type) {
2596                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2597
2598                 case CMP_NODE_IMAGE:
2599                         ntype->uifunc = node_composit_buts_image;
2600                         break;
2601                 case CMP_NODE_R_LAYERS:
2602                         ntype->uifunc = node_composit_buts_renderlayers;
2603                         break;
2604                 case CMP_NODE_NORMAL:
2605                         ntype->uifunc = node_buts_normal;
2606                         break;
2607                 case CMP_NODE_CURVE_VEC:
2608                         ntype->uifunc = node_buts_curvevec;
2609                         break;
2610                 case CMP_NODE_CURVE_RGB:
2611                         ntype->uifunc = node_buts_curvecol;
2612                         break;
2613                 case CMP_NODE_VALUE:
2614                         ntype->uifunc = node_buts_value;
2615                         break;
2616                 case CMP_NODE_RGB:
2617                         ntype->uifunc = node_buts_rgb;
2618                         break;
2619                 case CMP_NODE_FLIP:
2620                         ntype->uifunc = node_composit_buts_flip;
2621                         break;
2622                 case CMP_NODE_SPLITVIEWER:
2623                         ntype->uifunc = node_composit_buts_splitviewer;
2624                         break;
2625                 case CMP_NODE_MIX_RGB:
2626                         ntype->uifunc = node_buts_mix_rgb;
2627                         break;
2628                 case CMP_NODE_VALTORGB:
2629                         ntype->uifunc = node_buts_colorramp;
2630                         break;
2631                 case CMP_NODE_CROP:
2632                         ntype->uifunc = node_composit_buts_crop;
2633                         break;
2634                 case CMP_NODE_BLUR:
2635                         ntype->uifunc = node_composit_buts_blur;
2636                         break;
2637                 case CMP_NODE_DBLUR:
2638                         ntype->uifunc = node_composit_buts_dblur;
2639                         break;
2640                 case CMP_NODE_BILATERALBLUR:
2641                         ntype->uifunc = node_composit_buts_bilateralblur;
2642                         break;
2643                 case CMP_NODE_DEFOCUS:
2644                         ntype->uifunc = node_composit_buts_defocus;
2645                         break;
2646                 case CMP_NODE_GLARE:
2647                         ntype->uifunc = node_composit_buts_glare;
2648                         break;
2649                 case CMP_NODE_TONEMAP:
2650                         ntype->uifunc = node_composit_buts_tonemap;
2651                         break;
2652                 case CMP_NODE_LENSDIST:
2653                         ntype->uifunc = node_composit_buts_lensdist;
2654                         break;
2655                 case CMP_NODE_VECBLUR:
2656                         ntype->uifunc = node_composit_buts_vecblur;
2657                         break;
2658                 case CMP_NODE_FILTER:
2659                         ntype->uifunc = node_composit_buts_filter;
2660                         break;
2661                 case CMP_NODE_MAP_VALUE:
2662                         ntype->uifunc = node_composit_buts_map_value;
2663                         break;
2664                 case CMP_NODE_TIME:
2665                         ntype->uifunc = node_buts_time;
2666                         break;
2667                 case CMP_NODE_ALPHAOVER:
2668                         ntype->uifunc = node_composit_buts_alphaover;
2669                         break;
2670                 case CMP_NODE_HUE_SAT:
2671                         ntype->uifunc = node_composit_buts_hue_sat;
2672                         break;
2673                 case CMP_NODE_TEXTURE:
2674                         ntype->uifunc = node_buts_texture;
2675                         break;
2676                 case CMP_NODE_DILATEERODE:
2677                         ntype->uifunc = node_composit_buts_dilateerode;
2678                         break;
2679                 case CMP_NODE_INPAINT:
2680                         ntype->uifunc = node_composit_buts_inpaint;
2681                         break;
2682                 case CMP_NODE_OUTPUT_FILE:
2683                         ntype->uifunc = node_composit_buts_file_output;
2684                         ntype->uifuncbut = node_composit_buts_file_output_details;
2685                         ntype->drawinputfunc = node_draw_input_file_output;
2686                         break;
2687                 case CMP_NODE_DIFF_MATTE:
2688                         ntype->uifunc = node_composit_buts_diff_matte;
2689                         break;
2690                 case CMP_NODE_DIST_MATTE:
2691                         ntype->uifunc = node_composit_buts_distance_matte;
2692                         break;
2693                 case CMP_NODE_COLOR_SPILL:
2694                         ntype->uifunc = node_composit_buts_color_spill;
2695                         break;
2696                 case CMP_NODE_CHROMA_MATTE:
2697                         ntype->uifunc = node_composit_buts_chroma_matte;
2698                         break;
2699                 case CMP_NODE_COLOR_MATTE:
2700                         ntype->uifunc = node_composit_buts_color_matte;
2701                         break;
2702                 case CMP_NODE_SCALE:
2703                         ntype->uifunc = node_composit_buts_scale;
2704                         break;
2705                 case CMP_NODE_ROTATE:
2706                         ntype->uifunc = node_composit_buts_rotate;
2707                         break;
2708                 case CMP_NODE_CHANNEL_MATTE:
2709                         ntype->uifunc = node_composit_buts_channel_matte;
2710                         break;
2711                 case CMP_NODE_LUMA_MATTE:
2712                         ntype->uifunc = node_composit_buts_luma_matte;
2713                         break;
2714                 case CMP_NODE_MAP_UV:
2715                         ntype->uifunc = node_composit_buts_map_uv;
2716                         break;
2717                 case CMP_NODE_ID_MASK:
2718                         ntype->uifunc = node_composit_buts_id_mask;
2719                         break;
2720                 case CMP_NODE_DOUBLEEDGEMASK:
2721                         ntype->uifunc = node_composit_buts_double_edge_mask;
2722                         break;
2723                 case CMP_NODE_MATH:
2724                         ntype->uifunc = node_buts_math;
2725                         break;
2726                 case CMP_NODE_INVERT:
2727                         ntype->uifunc = node_composit_buts_invert;
2728                         break;
2729                 case CMP_NODE_PREMULKEY:
2730                         ntype->uifunc = node_composit_buts_premulkey;
2731                         break;
2732                 case CMP_NODE_VIEW_LEVELS:
2733                         ntype->uifunc = node_composit_buts_view_levels;
2734                         break;
2735                 case CMP_NODE_COLORBALANCE:
2736                         ntype->uifunc = node_composit_buts_colorbalance;
2737                         ntype->uifuncbut = node_composit_buts_colorbalance_but;
2738                         break;
2739                 case CMP_NODE_HUECORRECT:
2740                         ntype->uifunc = node_composit_buts_huecorrect;
2741                         break;
2742                 case CMP_NODE_ZCOMBINE:
2743                         ntype->uifunc = node_composit_buts_zcombine;
2744                         break;
2745                 case CMP_NODE_COMBYCCA:
2746                 case CMP_NODE_SEPYCCA:
2747                         ntype->uifunc = node_composit_buts_ycc;
2748                         break;
2749                 case CMP_NODE_MOVIECLIP:
2750                         ntype->uifunc = node_composit_buts_movieclip;
2751                         break;
2752                 case CMP_NODE_STABILIZE2D:
2753                         ntype->uifunc = node_composit_buts_stabilize2d;
2754                         break;
2755                 case CMP_NODE_TRANSFORM:
2756                         ntype->uifunc = node_composit_buts_transform;
2757                         break;
2758                 case CMP_NODE_MOVIEDISTORTION:
2759                         ntype->uifunc = node_composit_buts_moviedistortion;
2760                         break;
2761                 case CMP_NODE_COLORCORRECTION:
2762                         ntype->uifunc = node_composit_buts_colorcorrection;
2763                         ntype->uifuncbut = node_composit_buts_colorcorrection_but;
2764                         break;
2765                 case CMP_NODE_SWITCH:
2766                         ntype->uifunc = node_composit_buts_switch;
2767                         break;
2768                 case CMP_NODE_MASK_BOX:
2769                         ntype->uifunc = node_composit_buts_boxmask;
2770                         ntype->uibackdropfunc = node_composit_backdrop_boxmask;
2771                         break;
2772                 case CMP_NODE_MASK_ELLIPSE:
2773                         ntype->uifunc = node_composit_buts_ellipsemask;
2774                         ntype->uibackdropfunc = node_composit_backdrop_ellipsemask;
2775                         break;
2776                 case CMP_NODE_BOKEHIMAGE:
2777                         ntype->uifunc = node_composit_buts_bokehimage;
2778                         break;
2779                 case CMP_NODE_BOKEHBLUR:
2780                         ntype->uifunc = node_composit_buts_bokehblur;
2781                         break;
2782                 case CMP_NODE_VIEWER:
2783                         ntype->uifunc = NULL;
2784                         ntype->uifuncbut = node_composit_buts_viewer_but;
2785                         ntype->uibackdropfunc = node_composit_backdrop_viewer;
2786                         break;
2787                 case CMP_NODE_MASK:
2788                         ntype->uifunc = node_composit_buts_mask;
2789                         break;
2790                 case CMP_NODE_KEYINGSCREEN:
2791                         ntype->uifunc = node_composit_buts_keyingscreen;
2792                         break;
2793                 case CMP_NODE_KEYING:
2794                         ntype->uifunc = node_composit_buts_keying;
2795                         break;
2796                 case CMP_NODE_TRACKPOS:
2797                         ntype->uifunc = node_composit_buts_trackpos;
2798                         break;
2799                 default:
2800                         ntype->uifunc = NULL;
2801         }
2802 }
2803
2804 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2805
2806 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2807 {
2808         uiLayout *col;
2809         
2810         col = uiLayoutColumn(layout, TRUE);
2811         uiItemR(col, ptr, "offset", 0, IFACE_("Offset"), ICON_NONE);
2812         uiItemR(col, ptr, "offset_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2813         
2814         col = uiLayoutColumn(layout, TRUE);
2815         uiItemR(col, ptr, "squash", 0, IFACE_("Squash"), ICON_NONE);
2816         uiItemR(col, ptr, "squash_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2817 }
2818
2819 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2820 {
2821         PointerRNA tex_ptr;
2822         bNode *node = ptr->data;
2823         ID *id = ptr->id.data;
2824         Tex *tex = (Tex *)node->storage;
2825         uiLayout *col, *row;
2826         
2827         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2828
2829         col = uiLayoutColumn(layout, FALSE);
2830
2831         switch (tex->type) {
2832                 case TEX_BLEND:
2833                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2834                         row = uiLayoutRow(col, FALSE);
2835                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2836                         break;
2837
2838                 case TEX_MARBLE:
2839                         row = uiLayoutRow(col, FALSE);
2840                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2841                         row = uiLayoutRow(col, FALSE);
2842                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2843                         row = uiLayoutRow(col, FALSE);
2844                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2845                         row = uiLayoutRow(col, FALSE);
2846                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2847                         break;
2848
2849                 case TEX_MAGIC:
2850                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2851                         break;
2852
2853                 case TEX_STUCCI:
2854                         row = uiLayoutRow(col, FALSE);
2855                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2856                         row = uiLayoutRow(col, FALSE);
2857                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2858                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2859                         break;
2860
2861                 case TEX_WOOD:
2862                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2863                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2864                         row = uiLayoutRow(col, FALSE);
2865                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2866                         row = uiLayoutRow(col, FALSE);
2867                         uiLayoutSetActive(row, !(ELEM(tex->stype, TEX_BAND, TEX_RING)));
2868                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2869                         break;
2870                         
2871                 case TEX_CLOUDS:
2872                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2873                         row = uiLayoutRow(col, FALSE);
2874                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2875                         row = uiLayoutRow(col, FALSE);
2876                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2877                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, IFACE_("Depth"), ICON_NONE);
2878                         break;
2879                         
2880                 case TEX_DISTNOISE:
2881                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2882                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2883                         break;
2884
2885                 case TEX_MUSGRAVE:
2886                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2887                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2888                         break;
2889                 case TEX_VORONOI:
2890                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2891                         if (tex->vn_distm == TEX_MINKOVSKY) {
2892                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2893                         }
2894                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2895                         break;
2896         }
2897 }
2898
2899 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2900 {
2901         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2902 }
2903
2904 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2905 {
2906         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2907 }
2908
2909 /* only once called */
2910 static void node_texture_set_butfunc(bNodeType *ntype)
2911 {
2912         if (ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX) {
2913                 ntype->uifunc = node_texture_buts_proc;
2914         }
2915         else {
2916                 switch (ntype->type) {
2917
2918                         case TEX_NODE_MATH:
2919                                 ntype->uifunc = node_buts_math;
2920                                 break;
2921
2922                         case TEX_NODE_MIX_RGB:
2923                                 ntype->uifunc = node_buts_mix_rgb;
2924                                 break;
2925
2926                         case TEX_NODE_VALTORGB:
2927                                 ntype->uifunc = node_buts_colorramp;
2928                                 break;
2929
2930                         case TEX_NODE_CURVE_RGB:
2931                                 ntype->uifunc = node_buts_curvecol;
2932                                 break;
2933
2934                         case TEX_NODE_CURVE_TIME:
2935                                 ntype->uifunc = node_buts_time;
2936                                 break;
2937
2938                         case TEX_NODE_TEXTURE:
2939                                 ntype->uifunc = node_buts_texture;
2940                                 break;
2941
2942                         case TEX_NODE_BRICKS:
2943                                 ntype->uifunc = node_texture_buts_bricks;
2944                                 break;
2945
2946                         case TEX_NODE_IMAGE:
2947                                 ntype->uifunc = node_texture_buts_image;
2948                                 break;
2949
2950                         case TEX_NODE_OUTPUT:
2951                                 ntype->uifunc = node_texture_buts_output;
2952                                 break;
2953                 }
2954         }
2955 }
2956
2957 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2958
2959 void ED_node_init_butfuncs(void)
2960 {
2961         bNodeTreeType *treetype;
2962         bNodeType *ntype;
2963         bNodeSocketType *stype;
2964         int i;
2965         
2966         /* node type ui functions */
2967         for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2968                 treetype = ntreeGetType(i);
2969                 if (treetype) {
2970                         for (ntype = treetype->node_types.first; ntype; ntype = ntype->next) {
2971                                 /* default ui functions */
2972                                 ntype->drawfunc = node_draw_default;
2973                                 ntype->drawupdatefunc = node_update_default;
2974                                 ntype->select_area_func = node_select_area_default;
2975                                 ntype->tweak_area_func = node_tweak_area_default;
2976                                 ntype->uifunc = NULL;
2977                                 ntype->uifuncbut = NULL;
2978                                 ntype->drawinputfunc = node_draw_input_default;
2979                                 ntype->drawoutputfunc = node_draw_output_default;
2980                                 ntype->resize_area_func = node_resize_area_default;
2981                                 
2982                                 node_common_set_butfunc(ntype);
2983                                 
2984                                 switch (i) {
2985                                         case NTREE_COMPOSIT:
2986                                                 node_composit_set_butfunc(ntype);
2987                                                 break;
2988                                         case NTREE_SHADER:
2989                                                 node_shader_set_butfunc(ntype);
2990                                                 break;
2991                                         case NTREE_TEXTURE:
2992                                                 node_texture_set_butfunc(ntype);
2993                                                 break;
2994                                 }
2995                         }
2996                 }
2997         }
2998         
2999         /* socket type ui functions */
3000         for (i = 0; i < NUM_SOCKET_TYPES; ++i) {
3001                 stype = ntreeGetSocketType(i);
3002                 if (stype) {
3003                         switch (stype->type) {
3004                                 case SOCK_FLOAT:
3005                                 case SOCK_INT:
3006                                 case SOCK_BOOLEAN:
3007                                         stype->buttonfunc = node_socket_button_default;
3008                                         break;
3009                                 case SOCK_VECTOR:
3010                                         stype->buttonfunc = node_socket_button_components;
3011                                         break;
3012                                 case SOCK_RGBA:
3013                                         stype->buttonfunc = node_socket_button_color;
3014                                         break;
3015                                 case SOCK_SHADER:
3016                                         stype->buttonfunc = node_socket_button_label;
3017                                         break;
3018                                 default:
3019                                         stype->buttonfunc = NULL;
3020                         }
3021                 }
3022         }
3023 }
3024
3025 /* ************** Generic drawing ************** */
3026
3027 void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
3028 {
3029         
3030         if ((snode->flag & SNODE_BACKDRAW) && snode->treetype == NTREE_COMPOSIT) {
3031                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
3032                 void *lock;
3033                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
3034                 if (ibuf) {
3035                         SpaceNode *snode = CTX_wm_space_node(C);
3036                         wmWindow *win = CTX_wm_window(C);
3037                         const ColorManagedViewSettings *view_settings;
3038                         float x, y; 
3039                         unsigned char *display_buffer;
3040                         void *cache_handle;
3041                         
3042                         glMatrixMode(GL_PROJECTION);
3043                         glPushMatrix();
3044                         glMatrixMode(GL_MODELVIEW);
3045                         glPushMatrix();
3046
3047                         /* keep this, saves us from a version patch */
3048                         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
3049                         
3050                         /* somehow the offset has to be calculated inverse */
3051                         
3052                         glaDefine2DArea(&ar->winrct);
3053                         /* ortho at pixel level curarea */
3054                         wmOrtho2(-GLA_PIXEL_OFS, ar->winx - GLA_PIXEL_OFS, -GLA_PIXEL_OFS, ar->winy - GLA_PIXEL_OFS);
3055                         
3056                         x = (ar->winx - snode->zoom * ibuf->x) / 2 + snode->xof;
3057                         y = (ar->winy - snode->zoom * ibuf->y) / 2 + snode->yof;
3058                         
3059
3060                         view_settings = IMB_view_settings_get_effective(win, &snode->view_settings);
3061                         display_buffer = IMB_display_buffer_acquire(ibuf, view_settings, &win->display_settings, &cache_handle);
3062
3063                         if (display_buffer) {
3064                                 if (snode->flag & (SNODE_SHOW_R | SNODE_SHOW_G | SNODE_SHOW_B)) {
3065                                         int ofs;
3066
3067 #ifdef __BIG_ENDIAN__
3068                                         if      (snode->flag & SNODE_SHOW_R) ofs = 2;
3069                                         else if (snode->flag & SNODE_SHOW_G) ofs = 1;
3070                                         else                                 ofs = 0;
3071 #else
3072                                         if      (snode->flag & SNODE_SHOW_R) ofs = 1;
3073                                         else if (snode->flag & SNODE_SHOW_G) ofs = 2;
3074                                         else                                 ofs = 3;
3075 #endif
3076
3077                                         glPixelZoom(snode->zoom, snode->zoom);
3078                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
3079
3080                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT,
3081                                                           ((unsigned char *)ibuf->rect) + ofs);
3082
3083                                         glPixelZoom(1.0f, 1.0f);
3084                                 }
3085                                 else if (snode->flag & SNODE_SHOW_ALPHA) {
3086                                         glPixelZoom(snode->zoom, snode->zoom);
3087                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
3088 #ifdef __BIG_ENDIAN__
3089                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
3090 #endif
3091                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, display_buffer);
3092
3093 #ifdef __BIG_ENDIAN__
3094                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
3095 #endif
3096                                         glPixelZoom(1.0f, 1.0f);
3097                                 }
3098                                 else if (snode->flag & SNODE_USE_ALPHA) {
3099                                         glEnable(GL_BLEND);
3100                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3101                                         glPixelZoom(snode->zoom, snode->zoom);
3102                                         
3103                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, display_buffer);
3104                                         
3105                                         glPixelZoom(1.0f, 1.0f);
3106                                         glDisable(GL_BLEND);
3107                                 }
3108             &nb