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