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