AlphaOver node feature: "Premul" slider allows to mix between the
[blender-staging.git] / source / blender / src / 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36
37 #include "DNA_action_types.h"
38 #include "DNA_color_types.h"
39 #include "DNA_customdata_types.h"
40 #include "DNA_ipo_types.h"
41 #include "DNA_ID.h"
42 #include "DNA_image_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_texture_types.h"
51 #include "DNA_userdef_types.h"
52
53 #include "BKE_global.h"
54 #include "BKE_image.h"
55 #include "BKE_library.h"
56 #include "BKE_main.h"
57 #include "BKE_material.h"
58 #include "BKE_node.h"
59 #include "BKE_object.h"
60 #include "BKE_texture.h"
61 #include "BKE_utildefines.h"
62
63 #include "CMP_node.h"
64 #include "SHD_node.h"
65
66 #include "BIF_gl.h"
67 #include "BIF_glutil.h"
68 #include "BIF_interface.h"
69 #include "BIF_interface_icons.h"
70 #include "BIF_language.h"
71 #include "BIF_mywindow.h"
72 #include "BIF_previewrender.h"
73 #include "BIF_resources.h"
74 #include "BIF_screen.h"
75 #include "BIF_space.h"
76
77 #include "BSE_drawipo.h"
78 #include "BSE_node.h"
79 #include "BSE_view.h"
80
81 #include "BMF_Api.h"
82
83 #include "MEM_guardedalloc.h"
84
85 #include "RE_pipeline.h"
86 #include "IMB_imbuf_types.h"
87
88 #include "blendef.h"
89 #include "butspace.h"
90 #include "interface.h"  /* urm...  for rasterpos_safe, roundbox */
91 #include "mydevice.h"
92
93 extern void autocomplete_uv(char *str, void *arg_v);
94 extern int verify_valid_uv_name(char *str);
95
96 /* autocomplete callback for buttons */
97 static void autocomplete_vcol(char *str, void *arg_v)
98 {
99         Mesh *me;
100         CustomDataLayer *layer;
101         AutoComplete *autocpl;
102         int a;
103
104         if(str[0]==0)
105                 return;
106
107         autocpl= autocomplete_begin(str, 32);
108                 
109         /* search if str matches the beginning of name */
110         for(me= G.main->mesh.first; me; me=me->id.next)
111                 for(a=0, layer= me->fdata.layers; a<me->fdata.totlayer; a++, layer++)
112                         if(layer->type == CD_MCOL)
113                                 autocomplete_do_name(autocpl, layer->name);
114         
115         autocomplete_end(autocpl, str);
116 }
117
118 static int verify_valid_vcol_name(char *str)
119 {
120         Mesh *me;
121         CustomDataLayer *layer;
122         int a;
123         
124         if(str[0]==0)
125                 return 1;
126
127         /* search if str matches the name */
128         for(me= G.main->mesh.first; me; me=me->id.next)
129                 for(a=0, layer= me->fdata.layers; a<me->fdata.totlayer; a++, layer++)
130                         if(layer->type == CD_MCOL)
131                                 if(strcmp(layer->name, str)==0)
132                                         return 1;
133         
134         return 0;
135 }
136
137 static void snode_drawstring(SpaceNode *snode, char *str, int okwidth)
138 {
139         char drawstr[NODE_MAXSTR];
140         int width;
141         
142         if(str[0]==0 || okwidth<4) return;
143         
144         BLI_strncpy(drawstr, str, NODE_MAXSTR);
145         width= snode->aspect*BIF_GetStringWidth(snode->curfont, drawstr, 0);
146
147         if(width > okwidth) {
148                 int len= strlen(drawstr)-1;
149                 
150                 while(width > okwidth && len>=0) {
151                         drawstr[len]= 0;
152                         
153                         width= snode->aspect*BIF_GetStringWidth(snode->curfont, drawstr, 0);
154                         len--;
155                 }
156                 if(len==0) return;
157         }
158         BIF_DrawString(snode->curfont, drawstr, 0);
159
160 }
161
162 /* **************  Socket callbacks *********** */
163
164 static void socket_vector_menu_cb(void *node_v, void *ntree_v)
165 {
166         if(node_v && ntree_v) {
167                 NodeTagChanged(ntree_v, node_v); 
168                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+((bNode *)node_v)->nr);
169         }
170 }
171
172 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
173 static uiBlock *socket_vector_menu(void *socket_v)
174 {
175         SpaceNode *snode= curarea->spacedata.first;
176         bNode *node;
177         bNodeSocket *sock= socket_v;
178         bNodeStack *ns= &sock->ns;
179         uiBlock *block;
180         uiBut *bt;
181         
182         /* a bit ugly... retrieve the node the socket comes from */
183         for(node= snode->nodetree->nodes.first; node; node= node->next) {
184                 bNodeSocket *sockt;
185                 for(sockt= node->inputs.first; sockt; sockt= sockt->next)
186                         if(sockt==sock)
187                                 break;
188                 if(sockt)
189                         break;
190         }
191         
192         block= uiNewBlock(&curarea->uiblocks, "socket menu", UI_EMBOSS, UI_HELV, curarea->win);
193
194         /* use this for a fake extra empy space around the buttons */
195         uiDefBut(block, LABEL, 0, "",                   -4, -4, 188, 68, NULL, 0, 0, 0, 0, "");
196         
197         uiBlockBeginAlign(block);
198         bt= uiDefButF(block, NUMSLI, 0, "X ",    0,40,180,20, ns->vec, ns->min, ns->max, 10, 0, "");
199         uiButSetFunc(bt, socket_vector_menu_cb, node, snode->nodetree);
200         bt= uiDefButF(block, NUMSLI, 0, "Y ",    0,20,180,20, ns->vec+1, ns->min, ns->max, 10, 0, "");
201         uiButSetFunc(bt, socket_vector_menu_cb, node, snode->nodetree);
202         bt= uiDefButF(block, NUMSLI, 0, "Z ",    0,0,180,20, ns->vec+2, ns->min, ns->max, 10, 0, "");
203         uiButSetFunc(bt, socket_vector_menu_cb, node, snode->nodetree);
204         
205         uiBlockSetDirection(block, UI_TOP);
206         
207         allqueue(REDRAWNODE, 0);
208         
209         return block;
210 }
211
212 static void node_sync_cb(void *snode_v, void *node_v)
213 {
214         SpaceNode *snode= snode_v;
215         
216         if(snode->treetype==NTREE_SHADER) {
217                 nodeShaderSynchronizeID(node_v, 1);
218                 allqueue(REDRAWBUTSSHADING, 0);
219         }
220 }
221
222 /* ****************** GENERAL CALLBACKS FOR NODES ***************** */
223
224 static void node_ID_title_cb(void *node_v, void *unused_v)
225 {
226         bNode *node= node_v;
227         
228         if(node->id) {
229                 test_idbutton(node->id->name+2);        /* library.c, verifies unique name */
230                 BLI_strncpy(node->name, node->id->name+2, 21);
231                 
232                 allqueue(REDRAWBUTSSHADING, 0);
233                 allqueue(REDRAWNODE, 0);
234                 allqueue(REDRAWOOPS, 0);
235         }
236 }
237
238
239 static void node_but_title_cb(void *node_v, void *but_v)
240 {
241         bNode *node= node_v;
242         uiBut *bt= but_v;
243         BLI_strncpy(node->name, bt->drawstr, NODE_MAXSTR);
244         
245         allqueue(REDRAWNODE, 0);
246 }
247
248
249 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
250
251 static int node_buts_group(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
252 {
253         if(block && node->id) {
254                 uiBut *bt;
255                 short width;
256                 
257                 uiBlockBeginAlign(block);
258                 
259                 /* name button */
260                 width= (short)(butr->xmax-butr->xmin - (node->id->us>1?19.0f:0.0f));
261                 bt= uiDefBut(block, TEX, B_NOP, "NT:",
262                                          butr->xmin, butr->ymin, width, 19, 
263                                          node->id->name+2, 0.0, 19.0, 0, 0, "NodeTree name");
264                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
265                 
266                 /* user amount */
267                 if(node->id->us>1) {
268                         char str1[32];
269                         sprintf(str1, "%d", node->id->us);
270                         bt= uiDefBut(block, BUT, B_NOP, str1, 
271                                                  butr->xmax-19, butr->ymin, 19, 19, 
272                                                  NULL, 0, 0, 0, 0, "Displays number of users.");
273                         //uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
274                 }
275                 
276                 uiBlockEndAlign(block);
277         }       
278         return 19;
279 }
280
281 static int node_buts_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
282 {
283         if(block) {
284                 bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
285                 
286                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", 
287                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
288                                   sock->ns.vec, sock->ns.min, sock->ns.max, 10, 2, "");
289                 
290         }
291         return 20;
292 }
293
294 static int node_buts_rgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
295 {
296         if(block) {
297                 bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
298                 if(sock) {
299                         /* enforce square box drawing */
300                         uiBlockSetEmboss(block, UI_EMBOSSP);
301                         
302                         uiDefButF(block, HSVCUBE, B_NODE_EXEC+node->nr, "", 
303                                           butr->xmin, butr->ymin, butr->xmax-butr->xmin, 12, 
304                                           sock->ns.vec, 0.0f, 1.0f, 3, 0, "");
305                         uiDefButF(block, HSVCUBE, B_NODE_EXEC+node->nr, "", 
306                                           butr->xmin, butr->ymin+15, butr->xmax-butr->xmin, butr->ymax-butr->ymin -15 -15, 
307                                           sock->ns.vec, 0.0f, 1.0f, 2, 0, "");
308                         uiDefButF(block, COL, B_NOP, "",                
309                                           butr->xmin, butr->ymax-12, butr->xmax-butr->xmin, 12, 
310                                           sock->ns.vec, 0.0, 0.0, -1, 0, "");
311                         /* the -1 above prevents col button to popup a color picker */
312                         
313                         uiBlockSetEmboss(block, UI_EMBOSS);
314                 }
315         }
316         return 30 + (int)(node->width-NODE_DY);
317 }
318
319 static int node_buts_mix_rgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
320 {
321         if(block) {
322                 uiBut *bt;
323                 int a_but= (ntree->type==NTREE_COMPOSIT);
324                 
325                 /* blend type */
326                 uiBlockBeginAlign(block);
327                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Mix %x0|Add %x1|Subtract %x3|Multiply %x2|Screen %x4|Overlay %x9|Divide %x5|Difference %x6|Darken %x7|Lighten %x8|Dodge %x10|Burn %x11|Color %x15|Value %x14|Saturation %x13|Hue %x12",
328                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin -(a_but?20:0), 20, 
329                                          &node->custom1, 0, 0, 0, 0, "");
330                 uiButSetFunc(bt, node_but_title_cb, node, bt);
331                 /* Alpha option, composite */
332                 if(a_but)
333                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "A",
334                                   butr->xmax-20, butr->ymin, 20, 20, 
335                                   &node->custom2, 0, 0, 0, 0, "Include Alpha of 2nd input in this operation");
336         }
337         return 20;
338 }
339
340 static int node_buts_time(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
341 {
342         if(block) {
343                 CurveMapping *cumap= node->storage;
344                 short dx= (short)((butr->xmax-butr->xmin)/2);
345                 butr->ymin += 26;
346
347                 curvemap_buttons(block, node->storage, 's', B_NODE_EXEC+node->nr, B_REDR, butr);
348                 
349                 if(cumap) cumap->flag |= CUMA_DRAW_CFRA;
350                 if(node->custom1<node->custom2)
351                         cumap->sample[0]= (float)(CFRA - node->custom1)/(float)(node->custom2-node->custom1);
352
353                 uiBlockBeginAlign(block);
354                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Sta:",
355                                   butr->xmin, butr->ymin-22, dx, 19, 
356                                   &node->custom1, 1.0, 20000.0, 0, 0, "Start frame");
357                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "End:",
358                                   butr->xmin+dx, butr->ymin-22, dx, 19, 
359                                   &node->custom2, 1.0, 20000.0, 0, 0, "End frame");
360         }
361         
362         return node->width-NODE_DY;
363 }
364
365 static int node_buts_valtorgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
366 {
367         if(block) {
368                 if(node->storage) {
369                         draw_colorband_buts_small(block, node->storage, butr, B_NODE_EXEC+node->nr);
370                 }
371         }
372         return 40;
373 }
374
375 static int node_buts_curvevec(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
376 {
377         if(block) {
378                 curvemap_buttons(block, node->storage, 'v', B_NODE_EXEC+node->nr, B_REDR, butr);
379         }       
380         return (int)(node->width-NODE_DY);
381 }
382
383 static float *_sample_col= NULL;        // bad bad, 2.5 will do better?
384 void node_curvemap_sample(float *col)
385 {
386         _sample_col= col;
387 }
388
389 static int node_buts_curvecol(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
390 {
391         if(block) {
392                 CurveMapping *cumap= node->storage;
393                 if(_sample_col) {
394                         cumap->flag |= CUMA_DRAW_SAMPLE;
395                         VECCOPY(cumap->sample, _sample_col);
396                 }
397                 else 
398                         cumap->flag &= ~CUMA_DRAW_SAMPLE;
399
400                 curvemap_buttons(block, node->storage, 'c', B_NODE_EXEC+node->nr, B_REDR, butr);
401         }       
402         return (int)(node->width-NODE_DY);
403 }
404
405 static int node_buts_normal(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
406 {
407         if(block) {
408                 bNodeSocket *sock= node->outputs.first;         /* first socket stores normal */
409                 
410                 uiDefButF(block, BUT_NORMAL, B_NODE_EXEC+node->nr, "", 
411                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, butr->ymax-butr->ymin, 
412                                   sock->ns.vec, 0.0f, 1.0f, 0, 0, "");
413                 
414         }       
415         return (int)(node->width-NODE_DY);
416 }
417
418 static void node_browse_tex_cb(void *ntree_v, void *node_v)
419 {
420         bNodeTree *ntree= ntree_v;
421         bNode *node= node_v;
422         Tex *tex;
423         
424         if(node->menunr<1) return;
425         
426         if(node->id) {
427                 node->id->us--;
428                 node->id= NULL;
429         }
430         tex= BLI_findlink(&G.main->tex, node->menunr-1);
431
432         node->id= &tex->id;
433         id_us_plus(node->id);
434         BLI_strncpy(node->name, node->id->name+2, 21);
435         
436         nodeSetActive(ntree, node);
437         
438         allqueue(REDRAWBUTSSHADING, 0);
439         allqueue(REDRAWNODE, 0);
440         NodeTagChanged(ntree, node); 
441         
442         node->menunr= 0;
443 }
444
445 static int node_buts_texture(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
446 {
447         if(block) {
448                 uiBut *bt;
449                 char *strp;
450                 
451                 /* browse button texture */
452                 uiBlockBeginAlign(block);
453                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->tex), NULL, NULL);
454                 node->menunr= 0;
455                 bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
456                                           butr->xmin, butr->ymin, 20, 19, 
457                                           &node->menunr, 0, 0, 0, 0, "Browse texture");
458                 uiButSetFunc(bt, node_browse_tex_cb, ntree, node);
459                 if(strp) MEM_freeN(strp);
460                 
461                 if(node->id) {
462                         bt= uiDefBut(block, TEX, B_NOP, "TE:",
463                                                  butr->xmin+19, butr->ymin, butr->xmax-butr->xmin-19, 19, 
464                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Texture name");
465                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
466                 }
467                 
468         }       
469         return 19;
470 }
471
472 static int node_buts_math(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
473
474         if(block) { 
475                 uiBut *bt; 
476
477                 bt=uiDefButS(block, MENU, B_NODE_EXEC,  "Add %x0|Subtract %x1|Multiply %x2|Divide %x3|Sine %x4|Cosine %x5|Tangent %x6|Arcsine %x7|Arccosine %x8|Arctangent %x9|Power %x10|Logarithm %x11|Minimum %x12|Maximum %x13|Round %x14", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 0, 0, 0, ""); 
478                 uiButSetFunc(bt, node_but_title_cb, node, bt); 
479         } 
480         return 20; 
481 }
482
483
484 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
485
486
487 static void node_mat_alone_cb(void *node_v, void *unused)
488 {
489         bNode *node= node_v;
490         
491         node->id= (ID *)copy_material((Material *)node->id);
492         
493         BIF_undo_push("Single user material");
494         allqueue(REDRAWBUTSSHADING, 0);
495         allqueue(REDRAWNODE, 0);
496         allqueue(REDRAWOOPS, 0);
497 }
498
499 static void node_browse_mat_cb(void *ntree_v, void *node_v)
500 {
501         bNodeTree *ntree= ntree_v;
502         bNode *node= node_v;
503         
504         if(node->menunr<1) return;
505         
506         if(node->menunr==32767) {       /* code for Add New */
507                 if(node->id) {
508                         /* make copy, but make sure it doesnt have the node tag nor nodes */
509                         Material *ma= (Material *)node->id;
510                         ma->id.us--;
511                         ma= copy_material(ma);
512                         ma->use_nodes= 0;
513                         if(ma->nodetree) {
514                                 ntreeFreeTree(ma->nodetree);
515                                 MEM_freeN(ma->nodetree);
516                         }
517                         ma->nodetree= NULL;
518                         node->id= (ID *)ma;
519                 }
520                 else node->id= (ID *)add_material("MatNode");
521         }
522         else {
523                 if(node->id) node->id->us--;
524                 node->id= BLI_findlink(&G.main->mat, node->menunr-1);
525                 id_us_plus(node->id);
526         }
527         BLI_strncpy(node->name, node->id->name+2, 21);
528         
529         nodeSetActive(ntree, node);
530
531         allqueue(REDRAWBUTSSHADING, 0);
532         allqueue(REDRAWNODE, 0);
533         BIF_preview_changed(ID_MA);
534
535         node->menunr= 0;
536 }
537
538 static void node_new_mat_cb(void *ntree_v, void *node_v)
539 {
540         bNodeTree *ntree= ntree_v;
541         bNode *node= node_v;
542         
543         node->id= (ID *)add_material("MatNode");
544         BLI_strncpy(node->name, node->id->name+2, 21);
545
546         nodeSetActive(ntree, node);
547
548         allqueue(REDRAWBUTSSHADING, 0);
549         allqueue(REDRAWNODE, 0);
550         BIF_preview_changed(ID_MA);
551
552 }
553
554 static void node_texmap_cb(void *texmap_v, void *unused_v)
555 {
556         init_mapping(texmap_v);
557 }
558
559 static int node_shader_buts_material(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
560 {
561         if(block) {
562                 uiBut *bt;
563                 short dx= (short)((butr->xmax-butr->xmin)/3.0f), has_us= (node->id && node->id->us>1);
564                 short dy= (short)butr->ymin;
565                 char *strp;
566                 
567                 /* WATCH IT: we use this callback in material buttons, but then only want first row */
568                 if(butr->ymax-butr->ymin > 21.0f) dy+= 19;
569                 
570                 uiBlockBeginAlign(block);
571                 if(node->id==NULL) uiBlockSetCol(block, TH_REDALERT);
572                 else if(has_us) uiBlockSetCol(block, TH_BUT_SETTING1);
573                 else uiBlockSetCol(block, TH_BUT_SETTING2);
574                 
575                 /* browse button */
576                 IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->mat), NULL, NULL);
577                 node->menunr= 0;
578                 bt= uiDefButS(block, MENU, B_NOP, strp, 
579                                   butr->xmin, dy, 19, 19, 
580                                   &node->menunr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
581                 uiButSetFunc(bt, node_browse_mat_cb, ntree, node);
582                 if(strp) MEM_freeN(strp);
583                 
584                 /* Add New button */
585                 if(node->id==NULL) {
586                         bt= uiDefBut(block, BUT, B_NOP, "Add New",
587                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
588                                                  NULL, 0.0, 0.0, 0, 0, "Add new Material");
589                         uiButSetFunc(bt, node_new_mat_cb, ntree, node);
590                         uiBlockSetCol(block, TH_AUTO);
591                 }
592                 else {
593                         /* name button */
594                         short width= (short)(butr->xmax-butr->xmin-19.0f - (has_us?19.0f:0.0f));
595                         bt= uiDefBut(block, TEX, B_NOP, "MA:",
596                                                   butr->xmin+19, dy, width, 19, 
597                                                   node->id->name+2, 0.0, 19.0, 0, 0, "Material name");
598                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
599                         
600                         /* user amount */
601                         if(has_us) {
602                                 char str1[32];
603                                 sprintf(str1, "%d", node->id->us);
604                                 bt= uiDefBut(block, BUT, B_NOP, str1, 
605                                                           butr->xmax-19, dy, 19, 19, 
606                                                           NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
607                                 uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
608                         }
609                         
610                         /* WATCH IT: we use this callback in material buttons, but then only want first row */
611                         if(butr->ymax-butr->ymin > 21.0f) {
612                                 /* node options */
613                                 uiBlockSetCol(block, TH_AUTO);
614                                 uiDefButBitS(block, TOG, SH_NODE_MAT_DIFF, B_NODE_EXEC+node->nr, "Diff",
615                                                          butr->xmin, butr->ymin, dx, 19, 
616                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Diffuse");
617                                 uiDefButBitS(block, TOG, SH_NODE_MAT_SPEC, B_NODE_EXEC+node->nr, "Spec",
618                                                          butr->xmin+dx, butr->ymin, dx, 19, 
619                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Specular");
620                                 uiDefButBitS(block, TOG, SH_NODE_MAT_NEG, B_NODE_EXEC+node->nr, "Neg Normal",
621                                                          butr->xmax-dx, butr->ymin, dx, 19,
622                                                          &node->custom1, 0, 0, 0, 0, "Material Node uses inverted Normal");
623                         }
624                 }
625                 uiBlockEndAlign(block);
626         }       
627         return 38;
628 }
629
630 static int node_shader_buts_mapping(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
631 {
632         if(block) {
633                 TexMapping *texmap= node->storage;
634                 short dx= (short)((butr->xmax-butr->xmin)/7.0f);
635                 short dy= (short)(butr->ymax-19);
636                 
637                 uiBlockSetFunc(block, node_texmap_cb, texmap, NULL);    /* all buttons get this */
638                 
639                 uiBlockBeginAlign(block);
640                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
641                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->loc+1, -1000.0f, 1000.0f, 10, 2, "");
642                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->loc+2, -1000.0f, 1000.0f, 10, 2, "");
643                 dy-= 19;
644                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->rot, -1000.0f, 1000.0f, 1000, 1, "");
645                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->rot+1, -1000.0f, 1000.0f, 1000, 1, "");
646                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->rot+2, -1000.0f, 1000.0f, 1000, 1, "");
647                 dy-= 19;
648                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 2, "");
649                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->size+1, -1000.0f, 1000.0f, 10, 2, "");
650                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->size+2, -1000.0f, 1000.0f, 10, 2, "");
651                 dy-= 25;
652                 uiBlockBeginAlign(block);
653                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->min, -10.0f, 10.0f, 100, 2, "");
654                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->min+1, -10.0f, 10.0f, 100, 2, "");
655                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->min+2, -10.0f, 10.0f, 100, 2, "");
656                 dy-= 19;
657                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->max, -10.0f, 10.0f, 10, 2, "");
658                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->max+1, -10.0f, 10.0f, 10, 2, "");
659                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->max+2, -10.0f, 10.0f, 10, 2, "");
660                 uiBlockEndAlign(block);
661                 
662                 /* labels/options */
663                 
664                 dy= (short)(butr->ymax-19);
665                 uiDefBut(block, LABEL, B_NOP, "Loc", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
666                 dy-= 19;
667                 uiDefBut(block, LABEL, B_NOP, "Rot", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
668                 dy-= 19;
669                 uiDefBut(block, LABEL, B_NOP, "Size", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
670                 dy-= 25;
671                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
672                 dy-= 19;
673                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
674                 
675         }       
676         return 5*19 + 6;
677 }
678
679 static int node_shader_buts_vect_math(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
680
681         if(block) { 
682                 uiBut *bt; 
683         
684                 bt=uiDefButS(block, MENU, B_NODE_EXEC,  "Add %x0|Subtract %x1|Average %x2|Dot Product %x3 |Cross Product %x4|Normalize %x5", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 0, 0, 0, ""); 
685                 uiButSetFunc(bt, node_but_title_cb, node, bt); 
686         } 
687         return 20; 
688 }
689
690 static int node_shader_buts_geometry(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
691 {
692         if(block) {
693                 uiBut *but;
694                 NodeGeometry *ngeo= (NodeGeometry*)node->storage;
695
696                 if(!verify_valid_uv_name(ngeo->uvname))
697                         uiBlockSetCol(block, TH_REDALERT);
698                 but= uiDefBut(block, TEX, B_NODE_EXEC+node->nr, "UV:", butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20, ngeo->uvname, 0, 31, 0, 0, "Set name of UV layer to use, default is active UV layer");
699                 uiButSetCompleteFunc(but, autocomplete_uv, NULL);
700                 uiBlockSetCol(block, TH_AUTO);
701
702                 if(!verify_valid_vcol_name(ngeo->colname))
703                         uiBlockSetCol(block, TH_REDALERT);
704                 but= uiDefBut(block, TEX, B_NODE_EXEC+node->nr, "Col:", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, ngeo->colname, 0, 31, 0, 0, "Set name of vertex color layer to use, default is active vertex color layer");
705                 uiButSetCompleteFunc(but, autocomplete_vcol, NULL);
706                 uiBlockSetCol(block, TH_AUTO);
707         }
708
709         return 40;
710 }
711
712 /* only once called */
713 static void node_shader_set_butfunc(bNodeType *ntype)
714 {
715         switch(ntype->type) {
716                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
717
718                 case SH_NODE_MATERIAL:
719                 case SH_NODE_MATERIAL_EXT:
720                         ntype->butfunc= node_shader_buts_material;
721                         break;
722                 case SH_NODE_TEXTURE:
723                         ntype->butfunc= node_buts_texture;
724                         break;
725                 case SH_NODE_NORMAL:
726                         ntype->butfunc= node_buts_normal;
727                         break;
728                 case SH_NODE_CURVE_VEC:
729                         ntype->butfunc= node_buts_curvevec;
730                         break;
731                 case SH_NODE_CURVE_RGB:
732                         ntype->butfunc= node_buts_curvecol;
733                         break;
734                 case SH_NODE_MAPPING:
735                         ntype->butfunc= node_shader_buts_mapping;
736                         break;
737                 case SH_NODE_VALUE:
738                         ntype->butfunc= node_buts_value;
739                         break;
740                 case SH_NODE_RGB:
741                         ntype->butfunc= node_buts_rgb;
742                         break;
743                 case SH_NODE_MIX_RGB:
744                         ntype->butfunc= node_buts_mix_rgb;
745                         break;
746                 case SH_NODE_VALTORGB:
747                         ntype->butfunc= node_buts_valtorgb;
748                         break;
749                 case SH_NODE_MATH: 
750                         ntype->butfunc= node_buts_math;
751                         break; 
752                 case SH_NODE_VECT_MATH: 
753                         ntype->butfunc= node_shader_buts_vect_math;
754                         break; 
755                 case SH_NODE_GEOMETRY:
756                         ntype->butfunc= node_shader_buts_geometry;
757                         break;
758                 default:
759                         ntype->butfunc= NULL;
760         }
761 }
762
763 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
764
765
766
767 static void node_browse_image_cb(void *ntree_v, void *node_v)
768 {
769         bNodeTree *ntree= ntree_v;
770         bNode *node= node_v;
771         
772         nodeSetActive(ntree, node);
773         
774         if(node->menunr<1) return;
775         if(node->menunr==32767) {       /* code for Load New */
776                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_LOADIMAGE);
777         }
778         else {
779                 if(node->id) node->id->us--;
780                 node->id= BLI_findlink(&G.main->image, node->menunr-1);
781                 id_us_plus(node->id);
782
783                 BLI_strncpy(node->name, node->id->name+2, 21);
784
785                 NodeTagChanged(ntree, node); 
786                 BKE_image_signal((Image *)node->id, node->storage, IMA_SIGNAL_USER_NEW_IMAGE);
787                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+node->nr);
788         }
789         node->menunr= 0;
790 }
791
792 static void node_active_cb(void *ntree_v, void *node_v)
793 {
794         nodeSetActive(ntree_v, node_v);
795 }
796 static void node_image_type_cb(void *node_v, void *unused)
797 {
798         
799         allqueue(REDRAWNODE, 1);
800 }
801
802 static char *node_image_type_pup(void)
803 {
804         char *str= MEM_mallocN(256, "image type pup");
805         int a;
806         
807         str[0]= 0;
808         
809         a= sprintf(str, "Image Type %%t|");
810         a+= sprintf(str+a, "  Image %%x%d %%i%d|", IMA_SRC_FILE, ICON_IMAGE_DEHLT);
811         a+= sprintf(str+a, "  Movie %%x%d %%i%d|", IMA_SRC_MOVIE, ICON_SEQUENCE);
812         a+= sprintf(str+a, "  Sequence %%x%d %%i%d|", IMA_SRC_SEQUENCE, ICON_IMAGE_COL);
813         a+= sprintf(str+a, "  Generated %%x%d %%i%d", IMA_SRC_GENERATED, ICON_BLANK1);
814         
815         return str;
816 }
817
818 /* copy from buttons_shading.c */
819 static char *layer_menu(RenderResult *rr)
820 {
821         RenderLayer *rl;
822         int len= 40 + 40*BLI_countlist(&rr->layers);
823         short a, nr;
824         char *str= MEM_callocN(len, "menu layers");
825         
826         strcpy(str, "Layer %t");
827         a= strlen(str);
828         for(nr=0, rl= rr->layers.first; rl; rl= rl->next, nr++) {
829                 a+= sprintf(str+a, "|%s %%x%d", rl->name, nr);
830         }
831         
832         return str;
833 }
834
835 static void image_layer_cb(void *ima_v, void *iuser_v)
836 {
837         
838         ntreeCompositForceHidden(G.scene->nodetree);
839         BKE_image_multilayer_index(ima_v, iuser_v);
840         allqueue(REDRAWNODE, 0);
841 }
842
843 static int node_composit_buts_image(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
844 {
845         ImageUser *iuser= node->storage;
846         
847         if(block) {
848                 uiBut *bt;
849                 short dy= (short)butr->ymax-19;
850                 char *strp;
851                 
852                 uiBlockBeginAlign(block);
853                 uiBlockSetCol(block, TH_BUT_SETTING2);
854                 
855                 /* browse button */
856                 IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
857                 node->menunr= 0;
858                 bt= uiDefButS(block, MENU, B_NOP, strp, 
859                                           butr->xmin, dy, 19, 19, 
860                                           &node->menunr, 0, 0, 0, 0, "Browses existing choices");
861                 uiButSetFunc(bt, node_browse_image_cb, ntree, node);
862                 if(strp) MEM_freeN(strp);
863                 
864                 /* Add New button */
865                 if(node->id==NULL) {
866                         bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
867                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
868                                                  NULL, 0.0, 0.0, 0, 0, "Add new Image");
869                         uiButSetFunc(bt, node_active_cb, ntree, node);
870                         uiBlockSetCol(block, TH_AUTO);
871                 }
872                 else {
873                         /* name button + type */
874                         Image *ima= (Image *)node->id;
875                         short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
876                         short width= xmax - xmin - 45;
877                         short icon= ICON_IMAGE_DEHLT;
878                         
879                         if(ima->source==IMA_SRC_MOVIE) icon= ICON_SEQUENCE;
880                         else if(ima->source==IMA_SRC_SEQUENCE) icon= ICON_IMAGE_COL;
881                         else if(ima->source==IMA_SRC_GENERATED) icon= ICON_BLANK1;
882                         
883                         bt= uiDefBut(block, TEX, B_NOP, "IM:",
884                                                  xmin+19, dy, width, 19, 
885                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
886                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
887                         
888                         /* buffer type option */
889                         strp= node_image_type_pup();
890                         bt= uiDefIconTextButS(block, MENU, B_NOP, icon, strp,
891                                                  xmax-26, dy, 26, 19, 
892                                                  &ima->source, 0.0, 19.0, 0, 0, "Image type");
893                         uiButSetFunc(bt, node_image_type_cb, node, ima);
894                         MEM_freeN(strp);
895                         
896                         if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) ) {
897                                 width= (xmax-xmin)/2;
898                                 
899                                 dy-= 19;
900                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "Frs:",
901                                                   xmin, dy, width, 19, 
902                                                   &iuser->frames, 0.0, MAXFRAMEF, 0, 0, "Amount of images used in animation");
903                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra:",
904                                                   xmin+width, dy, width, 19, 
905                                                   &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Start frame of animation");
906                                 dy-= 19;
907                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "Offs:",
908                                                   xmin, dy, width, 19, 
909                                                   &iuser->offset, -MAXFRAMEF, MAXFRAMEF, 0, 0, "Offsets the number of the frame to use in the animation");
910                                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Cycl",
911                                                   xmin+width, dy, width-20, 19, 
912                                                   &iuser->cycl, 0.0, 0.0, 0, 0, "Make animation go cyclic");
913                                 uiDefIconButBitS(block, TOG, IMA_ANIM_ALWAYS, B_NODE_EXEC+node->nr, ICON_AUTO,
914                                                   xmax-20, dy, 20, 19, 
915                                                   &iuser->flag, 0.0, 0.0, 0, 0, "Always refresh Image on frame changes");
916                         }
917                         if( ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
918                                 RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
919                                 if(rl) {
920                                         width= (xmax-xmin);
921                                         dy-= 19;
922                                         strp= layer_menu(ima->rr);
923                                         bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp,
924                                                           xmin, dy, width, 19, 
925                                                           &iuser->layer, 0.0, 10000.0, 0, 0, "Layer");
926                                         uiButSetFunc(bt, image_layer_cb, ima, node->storage);
927                                         MEM_freeN(strp);
928                                 }
929                         }
930                 }
931                 
932         }       
933         if(node->id) {
934                 Image *ima= (Image *)node->id;
935                 int retval= 19;
936                 
937                 /* for each draw we test for anim refresh event */
938                 if(iuser->flag & IMA_ANIM_REFRESHED) {
939                         iuser->flag &= ~IMA_ANIM_REFRESHED;
940                         addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+node->nr);
941                 }
942                 
943                 if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) )
944                         retval+= 38;
945                 if( ima->type==IMA_TYPE_MULTILAYER)
946                         retval+= 19;
947                 return retval;
948         }
949         else
950                 return 19;
951 }
952
953 /* if we use render layers from other scene, we make a nice title */
954 static void set_render_layers_title(void *node_v, void *unused)
955 {
956         bNode *node= node_v;
957         Scene *sce;
958         SceneRenderLayer *srl;
959         char str[64];
960         
961         if(node->id) {
962                 BLI_strncpy(str, node->id->name+2, 21);
963                 strcat(str, "|");
964                 sce= (Scene *)node->id;
965         }
966         else {
967                 str[0]= 0;
968                 sce= G.scene;
969         }
970         srl= BLI_findlink(&sce->r.layers, node->custom1);
971         if(srl==NULL) {
972                 node->custom1= 0;
973                 srl= sce->r.layers.first;
974         }
975         
976         strcat(str, srl->name);
977         BLI_strncpy(node->name, str, 32);
978 }
979
980 static char *scene_layer_menu(Scene *sce)
981 {
982         SceneRenderLayer *srl;
983         int len= 40 + 40*BLI_countlist(&sce->r.layers);
984         short a, nr;
985         char *str= MEM_callocN(len, "menu layers");
986         
987         strcpy(str, "Active Layer %t");
988         a= strlen(str);
989         for(nr=0, srl= sce->r.layers.first; srl; srl= srl->next, nr++) {
990                 a+= sprintf(str+a, "|%s %%x%d", srl->name, nr);
991         }
992         
993         return str;
994 }
995
996 static void node_browse_scene_cb(void *ntree_v, void *node_v)
997 {
998         bNodeTree *ntree= ntree_v;
999         bNode *node= node_v;
1000         Scene *sce;
1001         
1002         if(node->menunr<1) return;
1003         
1004         if(node->id) {
1005                 node->id->us--;
1006                 node->id= NULL;
1007         }
1008         sce= BLI_findlink(&G.main->scene, node->menunr-1);
1009         if(sce!=G.scene) {
1010                 node->id= &sce->id;
1011                 id_us_plus(node->id);
1012         }
1013         
1014         set_render_layers_title(node, NULL);
1015         nodeSetActive(ntree, node);
1016
1017         allqueue(REDRAWBUTSSHADING, 0);
1018         allqueue(REDRAWNODE, 0);
1019         NodeTagChanged(ntree, node); 
1020
1021         node->menunr= 0;
1022 }
1023
1024
1025 static int node_composit_buts_renderlayers(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1026 {
1027         if(block) {
1028                 uiBut *bt;
1029                 char *strp;
1030                 
1031                 /* browse button scene */
1032                 uiBlockBeginAlign(block);
1033                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->scene), NULL, NULL);
1034                 node->menunr= 0;
1035                 bt= uiDefButS(block, MENU, B_NOP, strp, 
1036                                           butr->xmin, butr->ymin, 20, 19, 
1037                                           &node->menunr, 0, 0, 0, 0, "Browse Scene to use RenderLayer from");
1038                 uiButSetFunc(bt, node_browse_scene_cb, ntree, node);
1039                 if(strp) MEM_freeN(strp);
1040                 
1041                 /* browse button layer */
1042                 strp= scene_layer_menu(node->id?(Scene *)node->id:G.scene);
1043                 if(node->id)
1044                         bt= uiDefIconTextButS(block, MENU, B_NODE_EXEC+node->nr, ICON_SCENE_DEHLT, strp, 
1045                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1046                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1047                 else
1048                         bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
1049                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1050                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1051                 uiButSetFunc(bt, set_render_layers_title, node, NULL);
1052                 MEM_freeN(strp);
1053                 
1054                 /* re-render */
1055                 /* uses custom2, not the best implementation of the world... but we need it to work now :) */
1056                 bt= uiDefIconButS(block, TOG, B_NODE_EXEC+node->nr, ICON_SCENE, 
1057                                   butr->xmax-20, butr->ymin, 20, 19, 
1058                                   &node->custom2, 0, 0, 0, 0, "Re-render this Layer");
1059                 
1060         }
1061         return 19;
1062 }
1063
1064 static void node_blur_relative_cb(void *node, void *poin2)
1065 {
1066         bNode *nodev= node;
1067         NodeBlurData *nbd= nodev->storage;
1068         if(nbd->image_in_width != 0){
1069                 if(nbd->relative){ /* convert absolute values to relative */
1070                         nbd->percentx= (float)(nbd->sizex)/nbd->image_in_width;
1071                         nbd->percenty= (float)(nbd->sizey)/nbd->image_in_height;
1072                 }else{ /* convert relative values to absolute */
1073                         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1074                         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1075                 }
1076         }
1077         allqueue(REDRAWNODE, 0);
1078 }
1079 static void node_blur_update_sizex_cb(void *node, void *poin2)
1080 {
1081         bNode *nodev= node;
1082         NodeBlurData *nbd= nodev->storage;
1083
1084         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1085 }
1086 static void node_blur_update_sizey_cb(void *node, void *poin2)
1087 {
1088         bNode *nodev= node;
1089         NodeBlurData *nbd= nodev->storage;
1090
1091         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1092 }
1093 static int node_composit_buts_blur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1094 {
1095         if(block) {
1096                 NodeBlurData *nbd= node->storage;
1097                 uiBut *bt;
1098                 short dy= butr->ymin+58;
1099                 short dx= (butr->xmax-butr->xmin)/2;
1100                 char str[256];
1101                 
1102                 uiBlockBeginAlign(block);
1103                 sprintf(str, "Filter Type%%t|Flat %%x%d|Tent %%x%d|Quad %%x%d|Cubic %%x%d|Gauss %%x%d|Fast Gauss%%x%d|CatRom %%x%d|Mitch %%x%d", R_FILTER_BOX, R_FILTER_TENT, R_FILTER_QUAD, R_FILTER_CUBIC, R_FILTER_GAUSS, R_FILTER_FAST_GAUSS, R_FILTER_CATROM, R_FILTER_MITCH);
1104                 uiDefButS(block, MENU, B_NODE_EXEC+node->nr,str,
1105                                   butr->xmin, dy, dx*2, 19, 
1106                                   &nbd->filtertype, 0, 0, 0, 0, "Set sampling filter for blur");
1107                 dy-=19;
1108                 if (nbd->filtertype != R_FILTER_FAST_GAUSS) { 
1109                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Bokeh",
1110                                         butr->xmin, dy, dx, 19, 
1111                                         &nbd->bokeh, 0, 0, 0, 0, "Uses circular filter, warning it's slow!");
1112                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Gamma",
1113                                         butr->xmin+dx, dy, dx, 19, 
1114                                         &nbd->gamma, 0, 0, 0, 0, "Applies filter on gamma corrected values");
1115                 } else {
1116                         uiBlockEndAlign(block);
1117                         uiBlockBeginAlign(block);
1118                 }
1119                 dy-=19;
1120                 bt= uiDefButS(block, TOG, B_NOP, "Relative",
1121                                   butr->xmin, dy, dx*2, 19,
1122                                   &nbd->relative, 0, 0, 0, 0, "Use relative (percent) values to define blur radius");
1123                 uiButSetFunc(bt, node_blur_relative_cb, node, NULL);
1124
1125                 dy-=19;
1126                 if(nbd->relative) {
1127                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "X:",
1128                                                  butr->xmin, dy, dx, 19, 
1129                                                  &nbd->percentx, 0.0f, 1.0f, 0, 0, "");
1130                         uiButSetFunc(bt, node_blur_update_sizex_cb, node, NULL);
1131                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1132                                                  butr->xmin+dx, dy, dx, 19, 
1133                                                  &nbd->percenty, 0.0f, 1.0f, 0, 0, "");
1134                         uiButSetFunc(bt, node_blur_update_sizey_cb, node, NULL);
1135                 }
1136                 else {
1137                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X:",
1138                                                  butr->xmin, dy, dx, 19, 
1139                                                  &nbd->sizex, 0, 256, 0, 0, "");
1140                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1141                                                  butr->xmin+dx, dy, dx, 19, 
1142                                                  &nbd->sizey, 0, 256, 0, 0, "");
1143                 }
1144                 uiBlockEndAlign(block);
1145         }
1146         return 77;
1147 }
1148
1149 static int node_composit_buts_dblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1150 {
1151         if(block) {
1152                 NodeDBlurData *ndbd = node->storage;
1153                 short dy = butr->ymin + 171;
1154                 short dx = butr->xmax - butr->xmin;
1155                 short halfdx= (short)dx/2;
1156
1157                 uiBlockBeginAlign(block);
1158                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1159                                 butr->xmin, dy, dx, 19,
1160                                 &ndbd->iter, 1, 32, 10, 0, "Amount of iterations");
1161                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Wrap",
1162                                 butr->xmin, dy-= 19, dx, 19, 
1163                                 &ndbd->wrap, 0, 0, 0, 0, "Wrap blur");
1164                 uiBlockEndAlign(block);
1165
1166                 dy-= 9;
1167
1168                 uiDefBut(block, LABEL, B_NOP, "Center", butr->xmin, dy-= 19, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
1169
1170                 uiBlockBeginAlign(block);
1171                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "X:",
1172                                 butr->xmin, dy-= 19, halfdx, 19,
1173                                 &ndbd->center_x, 0.0f, 1.0f, 10, 0, "X center in percents");
1174                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1175                                 butr->xmin+halfdx, dy, halfdx, 19,
1176                                 &ndbd->center_y, 0.0f, 1.0f, 10, 0, "Y center in percents");
1177                 uiBlockEndAlign(block);
1178
1179                 dy-= 9;
1180
1181                 uiBlockBeginAlign(block);
1182                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Distance:",
1183                                 butr->xmin, dy-= 19, dx, 19,
1184                                 &ndbd->distance, -1.0f, 1.0f, 10, 0, "Amount of which the image moves");
1185                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Angle:",
1186                                 butr->xmin, dy-= 19, dx, 19,
1187                                 &ndbd->angle, 0.0f, 360.0f, 1000, 0, "Angle in which the image will be moved");
1188                 uiBlockEndAlign(block);
1189
1190                 dy-= 9;
1191
1192                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Spin:",
1193                                 butr->xmin, dy-= 19, dx, 19,
1194                                 &ndbd->spin, -360.0f, 360.0f, 1000, 0, "Angle that is used to spin the image");
1195
1196                 dy-= 9;
1197
1198                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Zoom:",
1199                                 butr->xmin, dy-= 19, dx, 19,
1200                                 &ndbd->zoom, 0.0f, 100.0f, 100, 0, "Amount of which the image is zoomed");
1201
1202         }
1203         return 190;
1204 }
1205
1206 static int node_composit_buts_bilateralblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1207 {
1208         if(block) {
1209                 NodeBilateralBlurData *nbbd= node->storage;
1210                 short dy= butr->ymin+38;
1211                 short dx= (butr->xmax-butr->xmin);
1212                 
1213                 uiBlockBeginAlign(block);
1214                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1215                                  butr->xmin, dy, dx, 19, 
1216                                  &nbbd->iter, 1, 128, 0, 0, "Amount of iterations");
1217                 dy-=19;
1218                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Color Sigma:",
1219                                   butr->xmin, dy, dx, 19, 
1220                                   &nbbd->sigma_color,0.01, 3, 10, 0, "Sigma value used to modify color");
1221                 dy-=19;
1222                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Space Sigma:",
1223                                   butr->xmin, dy, dx, 19, 
1224                                   &nbbd->sigma_space ,0.01, 30, 10, 0, "Sigma value used to modify space");
1225                 
1226         }
1227         return 57;
1228 }
1229
1230 /* qdn: defocus node */
1231 static int node_composit_buts_defocus(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1232 {
1233         if(block) {
1234                 NodeDefocus *nqd = node->storage;
1235                 short dy = butr->ymin + 209;
1236                 short dx = butr->xmax - butr->xmin; 
1237                 char* mstr1 = "Bokeh Type%t|Octagon %x8|Heptagon %x7|Hexagon %x6|Pentagon %x5|Square %x4|Triangle %x3|Disk %x0";
1238
1239                 uiDefBut(block, LABEL, B_NOP, "Bokeh Type", butr->xmin, dy, dx, 19, NULL, 0, 0, 0, 0, "");
1240                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mstr1,
1241                           butr->xmin, dy-19, dx, 19,
1242                           &nqd->bktype, 0, 0, 0, 0, "Bokeh type");
1243                 if (nqd->bktype) { /* for some reason rotating a disk doesn't seem to work... ;) */
1244                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Rotate:",
1245                                   butr->xmin, dy-38, dx, 19,
1246                                   &nqd->rotation, 0, 90, 0, 0, "Bokeh shape rotation offset in degrees");
1247                 }
1248                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Gamma Correct",
1249                           butr->xmin, dy-57, dx, 19,
1250                           &nqd->gamco, 0, 0, 0, 0, "Enable gamma correction before and after main process");
1251                 if (nqd->no_zbuf==0) {
1252                         // only needed for zbuffer input
1253                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "fStop:",
1254                                   butr->xmin, dy-76, dx, 19,
1255                                   &nqd->fstop, 0.5, 128, 10, 0, "Amount of focal blur, 128=infinity=perfect focus, half the value doubles the blur radius");
1256                 }
1257                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Maxblur:",
1258                           butr->xmin, dy-95, dx, 19,
1259                           &nqd->maxblur, 0, 10000, 1000, 0, "blur limit, maximum CoC radius, 0=no limit");
1260                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "BThreshold:",
1261                           butr->xmin, dy-114, dx, 19,
1262                           &nqd->bthresh, 0, 100, 100, 0, "CoC radius threshold, prevents background bleed on in-focus midground, 0=off");
1263                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Preview",
1264                           butr->xmin, dy-142, dx, 19,
1265                           &nqd->preview, 0, 0, 0, 0, "Enable sampling mode, useful for preview when using low samplecounts");
1266                 if (nqd->preview) {
1267                         /* only visible when sampling mode enabled */
1268                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Samples:",
1269                                   butr->xmin, dy-161, dx, 19,
1270                                   &nqd->samples, 16, 256, 0, 0, "Number of samples (16=grainy, higher=less noise)");
1271                 }
1272                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "No zbuffer",
1273                           butr->xmin, dy-190, dx, 19,
1274                           &nqd->no_zbuf, 0, 0, 0, 0, "Enable when using an image as input instead of actual zbuffer (auto enabled if node not image based, eg. time node)");
1275                 if (nqd->no_zbuf) {
1276                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Zscale:",
1277                             butr->xmin, dy-209, dx, 19,
1278                             &nqd->scale, 0, 1000, 100, 0, "Scales the Z input when not using a zbuffer, controls maximum blur designated by the color white or input value 1");
1279                 }
1280         }
1281         return 228;
1282 }
1283
1284
1285 /* qdn: glare node */
1286 static int node_composit_buts_glare(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1287 {
1288         if(block) {
1289                 NodeGlare *ndg = node->storage;
1290                 short dy = butr->ymin + 152, dx = butr->xmax - butr->xmin; 
1291                 char* mn1 = "Type%t|Ghosts%x3|Streaks%x2|Fog Glow%x1|Simple Star%x0";
1292                 char* mn2 = "Quality/Speed%t|High/Slow%x0|Medium/Medium%x1|Low/Fast%x2";
1293                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mn1,
1294                           butr->xmin, dy, dx, 19,
1295                           &ndg->type, 0, 0, 0, 0, "Glow/Flare/Bloom type");
1296                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mn2,
1297                           butr->xmin, dy-19, dx, 19,
1298                           &ndg->quality, 0, 0, 0, 0,
1299                           "Quality speed trade off, if not set to high quality, effect will be applied to low-res copy of source image");
1300                 if (ndg->type != 1) {
1301                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1302                                   butr->xmin, dy-38, dx, 19,
1303                                   &ndg->iter, 2, 5, 1, 0,
1304                                   "higher values will generate longer/more streaks/ghosts");
1305                         if (ndg->type != 0)
1306                                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "ColMod:",
1307                                           butr->xmin, dy-57, dx, 19,
1308                                           &ndg->colmod, 0, 1, 10, 0,
1309                                           "Amount of Color Modulation, modulates colors of streaks and ghosts for a spectral dispersion effect");
1310                 }
1311                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Mix:",
1312                           butr->xmin, dy-76, dx, 19,
1313                           &ndg->mix, -1, 1, 10, 0,
1314                           "Mix balance, -1 is original image only, 0 is exact 50/50 mix, 1 is processed image only");
1315                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Threshold:",
1316                           butr->xmin, dy-95, dx, 19,
1317                           &ndg->threshold, 0, 1000, 10, 0,
1318                           "Brightness threshold, the glarefilter will be applied only to pixels brighter than this value");
1319                 if ((ndg->type == 2) || (ndg->type == 0))
1320                 {
1321                         if (ndg->type == 2) {
1322                                 uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "streaks:",
1323                                           butr->xmin, dy-114, dx, 19,
1324                                           &ndg->angle, 2, 16, 1000, 0,
1325                                           "Total number of streaks");
1326                                 uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "AngOfs:",
1327                                           butr->xmin, dy-133, dx, 19,
1328                                           &ndg->angle_ofs, 0, 180, 1000, 0,
1329                                           "Streak angle rotation offset in degrees");
1330                         }
1331                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Fade:",
1332                                   butr->xmin, dy-152, dx, 19,
1333                                   &ndg->fade, 0.75, 1, 5, 0,
1334                                   "Streak fade out factor");
1335                 }
1336                 if (ndg->type == 0)
1337                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Rot45",
1338                                   butr->xmin, dy-114, dx, 19,
1339                                   &ndg->angle, 0, 0, 0, 0,
1340                                   "simple star filter, add 45 degree rotation offset");
1341                 if ((ndg->type == 1) || (ndg->type > 3))        // PBGH and fog glow
1342                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Size:",
1343                                   butr->xmin, dy-114, dx, 19,
1344                                   &ndg->size, 6, 9, 1000, 0,
1345                                   "glow/glare size (not actual size, relative to initial size of bright area of pixels)");
1346         }
1347         return 171;
1348 }
1349
1350 /* qdn: tonemap node */
1351 static int node_composit_buts_tonemap(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1352 {
1353         if(block) {
1354                 NodeTonemap *ntm = node->storage;
1355                 short dy = butr->ymin + 76, dx = butr->xmax - butr->xmin; 
1356                 char* mn = "Type%t|R/D Photoreceptor%x1|Rh Simple%x0";
1357                 
1358                 uiBlockBeginAlign(block);
1359                 uiDefButI(block, MENU, B_NODE_EXEC+node->nr, mn,
1360                           butr->xmin, dy, dx, 19,
1361                           &ntm->type, 0, 0, 0, 0,
1362                           "Tone mapping type");
1363                 if (ntm->type == 0) {
1364                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Key:",
1365                                   butr->xmin, dy-19, dx, 19,
1366                                   &ntm->key, 0, 1, 5, 0,
1367                                   "The value the average luminance is mapped to");
1368                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offset:",
1369                                   butr->xmin, dy-38, dx, 19,
1370                                   &ntm->offset, 0.001, 10, 5, 0,
1371                                   "Tonemap offset, normally always 1, but can be used as an extra control to alter the brightness curve");
1372                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Gamma:",
1373                                   butr->xmin, dy-57, dx, 19,
1374                                   &ntm->gamma, 0.001, 3, 5, 0,
1375                                   "Gamma factor, if not used, set to 1");
1376                 }
1377                 else {
1378                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Intensity:",
1379                                   butr->xmin, dy-19, dx, 19,
1380                                   &ntm->f, -8, 8, 10, 0, "if less than zero, darkens image, otherwise makes it brighter");
1381                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Contrast:",
1382                                   butr->xmin, dy-38, dx, 19,
1383                                   &ntm->m, 0, 1, 5, 0, "Set to 0 to use estimate from input image");
1384                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Adaptation:",
1385                                   butr->xmin, dy-57, dx, 19,
1386                                   &ntm->a, 0, 1, 5, 0, "if 0, global, if 1, based on pixel intensity");
1387                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "ColCorrect:",
1388                                   butr->xmin, dy-76, dx, 19,
1389                                   &ntm->c, 0, 1, 5, 0, "color correction, if 0, same for all channels, if 1, each independent");
1390                 }
1391                 uiBlockEndAlign(block);
1392         }
1393         return 95;
1394 }
1395
1396 /* qdn: lens distortion node */
1397 static int node_composit_buts_lensdist(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1398 {
1399         if(block) {
1400                 NodeLensDist *nld = node->storage;
1401                 short dy = butr->ymin + 19, dx = butr->xmax - butr->xmin; 
1402                 uiBlockBeginAlign(block);
1403                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Projector",
1404                           butr->xmin, dy, dx, 19,
1405                           &nld->proj, 0, 0, 0, 0,
1406                           "Enable/disable projector mode, effect is applied in horizontal direction only");
1407                 if (!nld->proj) {
1408                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Jitter",
1409                                   butr->xmin, dy-19, dx/2, 19,
1410                                   &nld->jit, 0, 0, 0, 0,
1411                                   "Enable/disable jittering, faster, but also noisier");
1412                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Fit",
1413                                   butr->xmin+dx/2, dy-19, dx/2, 19,
1414                                   &nld->fit, 0, 0, 0, 0,
1415                                   "For positive distortion factor only, scale image such that black areas are not visible");
1416                 }
1417                 uiBlockEndAlign(block);
1418         }
1419         return 38;
1420 }
1421
1422
1423 static int node_composit_buts_vecblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1424 {
1425         if(block) {
1426                 NodeBlurData *nbd= node->storage;
1427                 short dy= butr->ymin;
1428                 short dx= (butr->xmax-butr->xmin);
1429                 
1430                 uiBlockBeginAlign(block);
1431                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Samples:",
1432                                  butr->xmin, dy+76, dx, 19, 
1433                                  &nbd->samples, 1, 256, 0, 0, "Amount of samples");
1434                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "MinSpeed:",
1435                                   butr->xmin, dy+57, dx, 19, 
1436                                   &nbd->minspeed, 0, 1024, 0, 0, "Minimum speed for a pixel to be blurred, used to separate background from foreground");
1437                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "MaxSpeed:",
1438                                   butr->xmin, dy+38, dx, 19, 
1439                                   &nbd->maxspeed, 0, 1024, 0, 0, "If not zero, maximum speed in pixels");
1440                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "BlurFac:",
1441                                   butr->xmin, dy+19, dx, 19, 
1442                                   &nbd->fac, 0.0f, 2.0f, 10, 2, "Scaling factor for motion vectors, actually 'shutter speed' in frames");
1443                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Curved",
1444                                   butr->xmin, dy, dx, 19, 
1445                                   &nbd->curved, 0.0f, 2.0f, 10, 2, "Interpolate between frames in a bezier curve, rather than linearly");
1446                 uiBlockEndAlign(block);
1447         }
1448         return 95;
1449 }
1450
1451 static int node_composit_buts_filter(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1452 {
1453         if(block) {
1454                 uiBut *bt;
1455                 
1456                 /* blend type */
1457                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Soften %x0|Sharpen %x1|Laplace %x2|Sobel %x3|Prewitt %x4|Kirsch %x5|Shadow %x6",
1458                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1459                                          &node->custom1, 0, 0, 0, 0, "");
1460                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1461         }
1462         return 20;
1463 }
1464
1465 static int node_composit_buts_flip(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
1466 {
1467         if(block) {
1468                 uiBut *bt;
1469                 
1470                 /* flip x\y */
1471                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Flip X %x0|Flip Y %x1|Flip X & Y %x2",
1472                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1473                                          &node->custom1, 0, 0, 0, 0, "");
1474                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1475         }
1476         return 20;      
1477 }
1478
1479 static int node_composit_buts_crop(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1480 {
1481         if(block) {
1482                 NodeTwoXYs *ntxy= node->storage;
1483                 char elementheight = 19;
1484                 short dx= (butr->xmax-butr->xmin)/2;
1485                 short dy= butr->ymax - elementheight;
1486                 short xymin= 0, xymax= 10000;
1487
1488                 uiBlockBeginAlign(block);
1489
1490                 /* crop image size toggle */
1491                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Crop Image Size",
1492                                   butr->xmin, dy, dx*2, elementheight, 
1493                                   &node->custom1, 0, 0, 0, 0, "Crop the size of the input image.");
1494
1495                 dy-=elementheight;
1496
1497                 /* x1 */
1498                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X1:",
1499                                          butr->xmin, dy, dx, elementheight,
1500                                          &ntxy->x1, xymin, xymax, 0, 0, "");
1501                 /* y1 */
1502                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y1:",
1503                                          butr->xmin+dx, dy, dx, elementheight,
1504                                          &ntxy->y1, xymin, xymax, 0, 0, "");
1505
1506                 dy-=elementheight;
1507
1508                 /* x2 */
1509                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X2:",
1510                                          butr->xmin, dy, dx, elementheight,
1511                                          &ntxy->x2, xymin, xymax, 0, 0, "");
1512                 /* y2 */
1513                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y2:",
1514                                          butr->xmin+dx, dy, dx, elementheight,
1515                                          &ntxy->y2, xymin, xymax, 0, 0, "");
1516
1517                 uiBlockEndAlign(block);
1518         }
1519         return 60;
1520 }
1521
1522 static int node_composit_buts_splitviewer(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1523 {
1524         if(block) {     
1525                 uiBlockBeginAlign(block);
1526                 
1527                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "X",
1528                                   butr->xmin, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1529                                   &node->custom2, 0.0, 0.0, 0, 0, "");
1530                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Y",
1531                                   butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1532                                   &node->custom2, 0.0, 1.0, 0, 0, "");
1533                                   
1534                 uiDefButS(block, NUMSLI, B_NODE_EXEC+node->nr, "Split %: ",
1535                                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 100, 10, 0, "");
1536         }
1537         return 40;
1538 }
1539
1540 static int node_composit_buts_map_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1541 {
1542         if(block) {
1543                 TexMapping *texmap= node->storage;
1544                 short xstart= (short)butr->xmin;
1545                 short dy= (short)(butr->ymax-19.0f);
1546                 short dx= (short)(butr->xmax-butr->xmin)/2;
1547                 
1548                 uiBlockBeginAlign(block);
1549                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offs:", xstart, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
1550                 dy-= 19;
1551                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Size:", xstart, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 3, "");
1552                 dy-= 23;
1553                 uiBlockBeginAlign(block);
1554                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1555                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->min, -1000.0f, 1000.0f, 10, 2, "");
1556                 dy-= 19;
1557                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1558                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->max, -1000.0f, 1000.0f, 10, 2, "");
1559         }
1560         return 80;
1561 }
1562
1563 static int node_composit_buts_alphaover(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1564 {
1565         if(block) {
1566                 NodeTwoFloats *ntf= node->storage;
1567                 
1568                 /* alpha type */
1569                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "ConvertPremul",
1570                                   butr->xmin, butr->ymin+19, butr->xmax-butr->xmin, 19, 
1571                                   &node->custom1, 0, 0, 0, 0, "");
1572                 /* mix factor */
1573                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Premul: ",
1574                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 19, 
1575                                   &ntf->x, 0.0f, 1.0f, 100, 0, "");
1576         }
1577         return 38;
1578 }
1579
1580 static int node_composit_buts_hue_sat(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1581 {
1582         if(block) {
1583                 NodeHueSat *nhs= node->storage;
1584                 
1585                 uiBlockBeginAlign(block);
1586                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Hue: ",
1587                                   butr->xmin, butr->ymin+40.0f, butr->xmax-butr->xmin, 20, 
1588                                   &nhs->hue, 0.0f, 1.0f, 100, 0, "");
1589                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Sat: ",
1590                                   butr->xmin, butr->ymin+20.0f, butr->xmax-butr->xmin, 20, 
1591                                   &nhs->sat, 0.0f, 2.0f, 100, 0, "");
1592                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Val: ",
1593                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1594                                   &nhs->val, 0.0f, 2.0f, 100, 0, "");
1595         }
1596         return 60;
1597 }
1598
1599 static int node_composit_buts_dilateerode(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1600 {
1601         if(block) {
1602                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Distance:",
1603                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1604                                   &node->custom2, -100, 100, 0, 0, "Distance to grow/shrink (number of iterations)");
1605         }
1606         return 20;
1607 }
1608
1609 static int node_composit_buts_diff_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1610 {
1611         if(block) {
1612                 short sx= (butr->xmax-butr->xmin)/4;
1613                 short dx= (butr->xmax-butr->xmin)/3;
1614                 NodeChroma *c= node->storage;
1615                 
1616                 uiBlockBeginAlign(block);
1617                 /*color space selectors*/
1618                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"RGB",
1619                                                         butr->xmin,butr->ymin+60,sx,20,
1620                                                         &node->custom1,1,1, 0, 0, "RGB Color Space");
1621                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"HSV",
1622                                                         butr->xmin+sx,butr->ymin+60,sx,20,
1623                                                         &node->custom1,1,2, 0, 0, "HSV Color Space");
1624                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YUV",
1625                                                         butr->xmin+2*sx,butr->ymin+60,sx,20,
1626                                                         &node->custom1,1,3, 0, 0, "YUV Color Space");
1627                                         uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YCC",
1628                                                         butr->xmin+3*sx,butr->ymin+60,sx,20,
1629                                                         &node->custom1,1,4, 0, 0, "YCbCr Color Space");
1630                 /*channel tolorences*/
1631                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1632                                                         butr->xmin, butr->ymin+40, dx, 20,
1633                                                         &c->t1, 0.0f, 1.0f, 100, 0, "Channel 1 Tolerance");
1634                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1635                                                         butr->xmin+dx, butr->ymin+40, dx, 20,
1636                                                         &c->t2, 0.0f, 1.0f, 100, 0, "Channel 2 Tolorence");
1637                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1638                                                         butr->xmin+2*dx, butr->ymin+40, dx, 20,
1639                                                         &c->t3, 0.0f, 1.0f, 100, 0, "Channel 3 Tolorence");
1640                 /*falloff parameters*/
1641                 /*
1642                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Falloff Size ",
1643                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1644                         &c->fsize, 0.0f, 1.0f, 100, 0, "");
1645                 */
1646                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Falloff: ",
1647                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1648                         &c->fstrength, 0.0f, 1.0f, 100, 0, "");
1649         }
1650         return 80;
1651 }
1652
1653 static int node_composit_buts_color_spill(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1654 {
1655         if(block) {
1656                 short dx= (butr->xmax-butr->xmin)/3;
1657
1658                 NodeChroma *c=node->storage;
1659                 uiBlockBeginAlign(block);
1660                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Enhance: ", 
1661                                 butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1662                                 &c->t1, 0.0f, 0.5f, 100, 2, "Adjusts how much selected channel is affected by color spill algorithm");
1663                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "R",
1664                                 butr->xmin,butr->ymin,dx,20,
1665                                 &node->custom1,1,1, 0, 0, "Red Spill Suppression");
1666                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "G",
1667                                 butr->xmin+dx,butr->ymin,dx,20,
1668                                 &node->custom1,1,2, 0, 0, "Green Spill Suppression");
1669                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "B",
1670                                 butr->xmin+2*dx,butr->ymin,dx,20,
1671                                 &node->custom1, 1, 3, 0, 0, "Blue Spill Suppression");
1672                 uiBlockEndAlign(block);
1673         }
1674         return 60;
1675 }
1676
1677 static int node_composit_buts_chroma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1678 {
1679         if(block) {
1680                 short dx=(butr->xmax-butr->xmin)/2;
1681                 NodeChroma *c= node->storage;
1682                 uiBlockBeginAlign(block);
1683
1684                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Acceptance ",
1685                         butr->xmin, butr->ymin+60, butr->xmax-butr->xmin, 20,
1686                         &c->t1, 1.0f, 80.0f, 100, 0, "Tolerance for colors to be considered a keying color");
1687                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Cutoff ",
1688                         butr->xmin, butr->ymin+40, butr->xmax-butr->xmin, 20,
1689                         &c->t2, 0.0f, 30.0f, 100, 0, "Colors below this will be considered as exact matches for keying color");
1690
1691                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Lift ",
1692                         butr->xmin, butr->ymin+20, dx, 20,
1693                         &c->fsize, 0.0f, 1.0f, 100, 0, "Alpha Lift");
1694                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Gain ",
1695                         butr->xmin+dx, butr->ymin+20, dx, 20,
1696                         &c->fstrength, 0.0f, 1.0f, 100, 0, "Alpha Gain");
1697
1698                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Shadow Adjust ",
1699                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1700                         &c->t3, 0.0f, 1.0f, 100, 0, "Adjusts the brightness of any shadows captured");
1701
1702                 if(c->t2 > c->t1)
1703                         c->t2=c->t1;
1704         }
1705         return 80;
1706 }
1707
1708 static int node_composit_buts_channel_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1709 {
1710         if(block) {
1711                 short sx= (butr->xmax-butr->xmin)/4;
1712                 short cx= (butr->xmax-butr->xmin)/3;
1713                 NodeChroma *c=node->storage;
1714                 char *c1, *c2, *c3;
1715         
1716                 /*color space selectors*/
1717                 uiBlockBeginAlign(block);
1718                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"RGB",
1719                         butr->xmin,butr->ymin+60,sx,20,&node->custom1,1,1, 0, 0, "RGB Color Space");
1720                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"HSV",
1721                         butr->xmin+sx,butr->ymin+60,sx,20,&node->custom1,1,2, 0, 0, "HSV Color Space");
1722                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YUV",
1723                         butr->xmin+2*sx,butr->ymin+60,sx,20,&node->custom1,1,3, 0, 0, "YUV Color Space");
1724                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YCC",
1725                         butr->xmin+3*sx,butr->ymin+60,sx,20,&node->custom1,1,4, 0, 0, "YCbCr Color Space");
1726         
1727                 if (node->custom1==1) {
1728                         c1="R"; c2="G"; c3="B";
1729                 }
1730                 else if(node->custom1==2){
1731                         c1="H"; c2="S"; c3="V";
1732                 }
1733                 else if(node->custom1==3){
1734                         c1="Y"; c2="U"; c3="V";
1735                 }
1736                 else { // if(node->custom1==4){
1737                         c1="Y"; c2="Cb"; c3="Cr";
1738                 }
1739         
1740                 /*channel selector */
1741                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c1,
1742                         butr->xmin,butr->ymin+40,cx,20,&node->custom2,1, 1, 0, 0, "Channel 1");
1743                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c2,
1744                         butr->xmin+cx,butr->ymin+40,cx,20,&node->custom2,1, 2, 0, 0, "Channel 2");
1745                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c3,
1746                         butr->xmin+cx+cx,butr->ymin+40,cx,20,&node->custom2, 1, 3, 0, 0, "Channel 3");
1747         
1748                 /*tolerance sliders */
1749                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "High ", 
1750                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1751                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1752                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Low ", 
1753                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1754                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1755                 uiBlockEndAlign(block);
1756         
1757                 /*keep t2 (low) less than t1 (high) */
1758                 if(c->t2 > c->t1) {
1759                         c->t2=c->t1;
1760                 }
1761         }
1762         return 80;
1763 }
1764
1765 static int node_composit_buts_luma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1766 {
1767         if(block) {
1768                 NodeChroma *c=node->storage;
1769         
1770                 /*tolerance sliders */
1771                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "High ", 
1772                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1773                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1774                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Low ", 
1775                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1776                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1777                 uiBlockEndAlign(block);
1778         
1779                 /*keep t2 (low) less than t1 (high) */
1780                 if(c->t2 > c->t1) {
1781                         c->t2=c->t1;
1782                 }
1783         }
1784         return 40;
1785 }
1786
1787 static int node_composit_buts_map_uv(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1788 {
1789         if(block) {
1790                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Alpha:",
1791                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1792                                   &node->custom1, 0, 100, 0, 0, "Conversion percentage of UV differences to Alpha");
1793         }
1794         return 20;
1795 }
1796
1797 static int node_composit_buts_id_mask(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1798 {
1799         if(block) {
1800                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "ID:",
1801                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1802                                   &node->custom1, 0, 10000, 0, 0, "Pass Index number to convert to Alpha");
1803         }
1804         return 20;
1805 }
1806
1807
1808 /* allocate sufficient! */
1809 static void node_imagetype_string(char *str)
1810 {
1811         str += sprintf(str, "Save Image as: %%t|");
1812         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1813         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1814         str += sprintf(str, "PNG %%x%d|", R_PNG);
1815         str += sprintf(str, "BMP %%x%d|", R_BMP);
1816         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1817         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1818         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1819         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1820         str += sprintf(str, "DPX %%x%d|", R_DPX);
1821         str += sprintf(str, "OpenEXR %%x%d", R_OPENEXR);
1822 }
1823
1824 static int node_composit_buts_file_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1825 {
1826         if(block) {
1827                 NodeImageFile *nif= node->storage;
1828                 short x= (short)butr->xmin;
1829                 short y= (short)butr->ymin;
1830                 short w= (short)butr->xmax-butr->xmin;
1831                 char str[320];
1832                 
1833                 node_imagetype_string(str);
1834                 
1835                 uiBlockBeginAlign(block);
1836                 
1837                 uiDefBut(block, TEX, B_NOP, "",
1838                                   x, y+60, w, 20, 
1839                                   nif->name, 0.0f, 240.0f, 0, 0, "");
1840                 
1841                 uiDefButS(block, MENU, B_NOP, str,
1842                                   x, y+40, w, 20, 
1843                                   &nif->imtype, 0.0f, 1.0f, 0, 0, "");
1844                 
1845                 if(nif->imtype==R_OPENEXR) {
1846                         uiDefButBitS(block, TOG, R_OPENEXR_HALF, B_REDR, "Half",        
1847                                                 x, y+20, w/2, 20, 
1848                                                 &nif->subimtype, 0, 0, 0, 0, "");
1849
1850                         uiDefButS(block, MENU,B_NOP, "Codec %t|None %x0|Pxr24 (lossy) %x1|ZIP (lossless) %x2|PIZ (lossless) %x3|RLE (lossless) %x4",  
1851                                                 x+w/2, y+20, w/2, 20, 
1852                                                 &nif->codec, 0, 0, 0, 0, "");
1853                 }
1854                 else {
1855                         uiDefButS(block, NUM, B_NOP, "Quality: ",
1856                                   x, y+20, w, 20, 
1857                                   &nif->quality, 10.0f, 100.0f, 10, 0, "");
1858                 }
1859                 
1860                 /* start frame, end frame */
1861                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra: ", 
1862                                   x, y, w/2, 20, 
1863                                   &nif->sfra, 1, MAXFRAMEF, 10, 0, "");
1864                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "EFra: ", 
1865                                   x+w/2, y, w/2, 20, 
1866                                   &nif->efra, 1, MAXFRAMEF, 10, 0, "");
1867                 
1868         }
1869         return 80;
1870 }
1871
1872 static void node_scale_cb(void *node_v, void *unused_v)
1873 {
1874         bNode *node= node_v;
1875         bNodeSocket *nsock;
1876
1877         /* check the 2 inputs, and set them to reasonable values */
1878         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
1879                 if(ELEM(node->custom1, CMP_SCALE_RELATIVE, CMP_SCALE_SCENEPERCENT))
1880                         nsock->ns.vec[0]= 1.0;
1881                 else {
1882                         if(nsock->next==NULL)
1883                                 nsock->ns.vec[0]= (float)G.scene->r.ysch;
1884                         else
1885                                 nsock->ns.vec[0]= (float)G.scene->r.xsch;
1886                 }
1887         }       
1888 }
1889
1890 static int node_composit_buts_scale(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1891 {
1892         if(block) {
1893                 uiBut *bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Relative %x0|Absolute %x1|Scene Size % %x2|",
1894                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1895                                   &node->custom1, 0, 0, 0, 0, "Scale new image to absolute pixel size, size relative to the incoming image, or using the 'percent' size of the scene");
1896                 uiButSetFunc(bt, node_scale_cb, node, NULL);
1897         }
1898         return 20;
1899 }
1900
1901 static int node_composit_buts_invert(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
1902 {
1903         if(block) {
1904                 uiBlockBeginAlign(block);
1905                 uiDefButBitS(block, TOG, CMP_CHAN_RGB, B_NODE_EXEC+node->nr, "RGB",
1906                                          butr->xmin, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
1907                                          &node->custom1, 0, 0, 0, 0, "");
1908                 uiDefButBitS(block, TOG, CMP_CHAN_A, B_NODE_EXEC+node->nr, "A",
1909                                          butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
1910                                          &node->custom1, 0, 0, 0, 0, "");
1911                 uiBlockEndAlign(block);
1912         }
1913         return 20;      
1914 }
1915
1916 /* only once called */
1917 static void node_composit_set_butfunc(bNodeType *ntype)
1918 {
1919         switch(ntype->type) {
1920                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1921
1922                 case CMP_NODE_IMAGE:
1923                         ntype->butfunc= node_composit_buts_image;
1924                         break;
1925                 case CMP_NODE_R_LAYERS:
1926                         ntype->butfunc= node_composit_buts_renderlayers;
1927                         break;
1928                 case CMP_NODE_NORMAL:
1929                         ntype->butfunc= node_buts_normal;
1930                         break;
1931                 case CMP_NODE_CURVE_VEC:
1932                         ntype->butfunc= node_buts_curvevec;
1933                         break;
1934                 case CMP_NODE_CURVE_RGB:
1935                         ntype->butfunc= node_buts_curvecol;
1936                         break;
1937                 case CMP_NODE_VALUE:
1938                         ntype->butfunc= node_buts_value;
1939                         break;
1940                 case CMP_NODE_RGB:
1941                         ntype->butfunc= node_buts_rgb;
1942                         break;
1943                 case CMP_NODE_FLIP:
1944                         ntype->butfunc= node_composit_buts_flip;
1945                         break;
1946                 case CMP_NODE_SPLITVIEWER:
1947                         ntype->butfunc= node_composit_buts_splitviewer;
1948                         break;
1949                 case CMP_NODE_MIX_RGB:
1950                         ntype->butfunc= node_buts_mix_rgb;
1951                         break;
1952                 case CMP_NODE_VALTORGB:
1953                         ntype->butfunc= node_buts_valtorgb;
1954                         break;
1955                 case CMP_NODE_CROP:
1956                         ntype->butfunc= node_composit_buts_crop;
1957                         break;
1958                 case CMP_NODE_BLUR:
1959                         ntype->butfunc= node_composit_buts_blur;
1960                         break;
1961                 case CMP_NODE_DBLUR:
1962                         ntype->butfunc= node_composit_buts_dblur;
1963                         break;
1964                 case CMP_NODE_BILATERALBLUR:
1965                         ntype->butfunc= node_composit_buts_bilateralblur;
1966                         break;
1967                 /* qdn: defocus node */
1968                 case CMP_NODE_DEFOCUS:
1969                         ntype->butfunc = node_composit_buts_defocus;
1970                         break;
1971                 /* qdn: glare node */
1972                 case CMP_NODE_GLARE:
1973                         ntype->butfunc = node_composit_buts_glare;
1974                         break;
1975                 /* qdn: tonemap node */
1976                 case CMP_NODE_TONEMAP:
1977                         ntype->butfunc = node_composit_buts_tonemap;
1978                         break;
1979                 /* qdn: lens distortion node */
1980                 case CMP_NODE_LENSDIST:
1981                         ntype->butfunc = node_composit_buts_lensdist;
1982                         break;
1983                 case CMP_NODE_VECBLUR:
1984                         ntype->butfunc= node_composit_buts_vecblur;
1985                         break;
1986                 case CMP_NODE_FILTER:
1987                         ntype->butfunc= node_composit_buts_filter;
1988                         break;
1989                 case CMP_NODE_MAP_VALUE:
1990                         ntype->butfunc= node_composit_buts_map_value;
1991                         break;
1992                 case CMP_NODE_TIME:
1993                         ntype->butfunc= node_buts_time;
1994                         break;
1995                 case CMP_NODE_ALPHAOVER:
1996                         ntype->butfunc= node_composit_buts_alphaover;
1997                         break;
1998                 case CMP_NODE_HUE_SAT:
1999                         ntype->butfunc= node_composit_buts_hue_sat;
2000                         break;
2001                 case CMP_NODE_TEXTURE:
2002                         ntype->butfunc= node_buts_texture;
2003                         break;
2004                 case CMP_NODE_DILATEERODE:
2005                         ntype->butfunc= node_composit_buts_dilateerode;
2006                         break;
2007                 case CMP_NODE_OUTPUT_FILE:
2008                         ntype->butfunc= node_composit_buts_file_output;
2009                         break;
2010         
2011                 case CMP_NODE_DIFF_MATTE:
2012                         ntype->butfunc=node_composit_buts_diff_matte;
2013                         break;
2014                 case CMP_NODE_COLOR_SPILL:
2015                         ntype->butfunc=node_composit_buts_color_spill;
2016                         break;
2017                 case CMP_NODE_CHROMA:
2018                         ntype->butfunc=node_composit_buts_chroma_matte;
2019                         break;
2020                 case CMP_NODE_SCALE:
2021                         ntype->butfunc= node_composit_buts_scale;
2022                         break;
2023                 case CMP_NODE_CHANNEL_MATTE:
2024                         ntype->butfunc= node_composit_buts_channel_matte;
2025                         break;
2026                 case CMP_NODE_LUMA_MATTE:
2027                         ntype->butfunc= node_composit_buts_luma_matte;
2028                         break;
2029                 case CMP_NODE_MAP_UV:
2030                         ntype->butfunc= node_composit_buts_map_uv;
2031                         break;
2032                 case CMP_NODE_ID_MASK:
2033                         ntype->butfunc= node_composit_buts_id_mask;
2034                         break;
2035                 case CMP_NODE_MATH:
2036                         ntype->butfunc= node_buts_math;
2037                         break;
2038                 case CMP_NODE_INVERT:
2039                         ntype->butfunc= node_composit_buts_invert;
2040                         break;
2041                 default:
2042                         ntype->butfunc= NULL;
2043         }
2044 }
2045
2046
2047 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2048
2049 void init_node_butfuncs(void)
2050 {
2051         bNodeType *ntype;
2052         
2053         /* shader nodes */
2054         ntype= node_all_shaders.first;
2055         while(ntype) {
2056                 node_shader_set_butfunc(ntype);
2057                 ntype= ntype->next;
2058         }
2059         /* composit nodes */
2060         ntype= node_all_composit.first;
2061         while(ntype) {
2062                 node_composit_set_butfunc(ntype);
2063                 ntype= ntype->next;
2064         }
2065 }
2066
2067 /* ************** Generic drawing ************** */
2068
2069 void node_rename_but(char *s)
2070 {
2071         uiBlock *block;
2072         ListBase listb={0, 0};
2073         int dy, x1, y1, sizex=80, sizey=30;
2074         short pivot[2], mval[2], ret=0;
2075         
2076         getmouseco_sc(mval);
2077
2078         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
2079         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
2080         
2081         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
2082                 warp_pointer(pivot[0], pivot[1]);
2083
2084         mywinset(G.curscreen->mainwin);
2085         
2086         x1= pivot[0]-sizex+10;
2087         y1= pivot[1]-sizey/2;
2088         dy= sizey/2;
2089         
2090         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
2091         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
2092         
2093         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
2094         uiBlockBeginAlign(block);
2095         
2096         uiDefBut(block, TEX, B_NOP, "Name: ", (short)(x1),(short)(y1+dy), 150, 19, s, 0.0, 19.0, 0, 0, "Node user name");
2097         
2098         uiBlockEndAlign(block);
2099
2100         uiDefBut(block, BUT, 32767, "OK", (short)(x1+150), (short)(y1+dy), 29, 19, NULL, 0, 0, 0, 0, "");
2101
2102         uiBoundsBlock(block, 2);
2103
2104         ret= uiDoBlocks(&listb, 0, 0);
2105 }
2106
2107
2108 static void draw_nodespace_grid(SpaceNode *snode)
2109 {
2110         float start, step= 25.0f;
2111
2112         BIF_ThemeColorShade(TH_BACK, -10);
2113         
2114         start= snode->v2d.cur.xmin -fmod(snode->v2d.cur.xmin, step);
2115         
2116         glBegin(GL_LINES);
2117         for(; start<snode->v2d.cur.xmax; start+=step) {
2118                 glVertex2f(start, snode->v2d.cur.ymin);
2119                 glVertex2f(start, snode->v2d.cur.ymax);
2120         }
2121
2122         start= snode->v2d.cur.ymin -fmod(snode->v2d.cur.ymin, step);
2123         for(; start<snode->v2d.cur.ymax; start+=step) {
2124                 glVertex2f(snode->v2d.cur.xmin, start);
2125                 glVertex2f(snode->v2d.cur.xmax, start);
2126         }
2127         
2128         /* X and Y axis */
2129         BIF_ThemeColorShade(TH_BACK, -18);
2130         glVertex2f(0.0f, snode->v2d.cur.ymin);
2131         glVertex2f(0.0f, snode->v2d.cur.ymax);
2132         glVertex2f(snode->v2d.cur.xmin, 0.0f);
2133         glVertex2f(snode->v2d.cur.xmax, 0.0f);
2134         
2135         glEnd();
2136 }
2137
2138 static void draw_nodespace_back_pix(ScrArea *sa, SpaceNode *snode)
2139 {
2140         
2141         draw_nodespace_grid(snode);
2142         
2143         if(snode->flag & SNODE_BACKDRAW) {
2144                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2145                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2146                 if(ibuf) {
2147                         int x, y; 
2148                         /* somehow the offset has to be calculated inverse */
2149                         
2150                         glaDefine2DArea(&sa->winrct);
2151                         /* ortho at pixel level curarea */
2152                         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2153                         
2154                         x = (sa->winx-ibuf->x)/2 + snode->xof;
2155                         y = (sa->winx-ibuf->y)/2 + snode->yof;
2156                         
2157                         if(ibuf->rect)
2158                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2159                         else if(ibuf->channels==4)
2160                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
2161                         
2162                         /* sort this out, this should not be needed */
2163                         myortho2(snode->v2d.cur.xmin, snode->v2d.cur.xmax, snode->v2d.cur.ymin, snode->v2d.cur.ymax);
2164                         bwin_clear_viewmat(sa->win);    /* clear buttons view */
2165                         glLoadIdentity();
2166                 }
2167         }
2168 }
2169
2170 #if 0
2171 /* note: needs to be userpref or opengl profile option */
2172 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2173 {
2174
2175         draw_nodespace_grid(snode);
2176         
2177         if(snode->flag & SNODE_BACKDRAW) {
2178                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2179                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2180                 if(ibuf) {
2181                         int x, y;
2182                         float zoom = 1.0;
2183
2184                         glMatrixMode(GL_PROJECTION);
2185                         glPushMatrix();
2186                         glMatrixMode(GL_MODELVIEW);
2187                         glPushMatrix();
2188                         
2189                         glaDefine2DArea(&sa->winrct);
2190
2191                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2192                                 float zoomx, zoomy;
2193                                 zoomx= (float)sa->winx/ibuf->x;
2194                                 zoomy= (float)sa->winy/ibuf->y;
2195                                 zoom = MIN2(zoomx, zoomy);
2196                         }
2197                         
2198                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2199                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2200
2201                         glPixelZoom(zoom, zoom);
2202
2203                         glColor4f(1.0, 1.0, 1.0, 1.0);
2204                         if(ibuf->rect)
2205                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2206                         else if(ibuf->channels==4)
2207                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2208
2209                         glPixelZoom(1.0, 1.0);
2210
2211                         glMatrixMode(GL_PROJECTION);
2212                         glPopMatrix();
2213                         glMatrixMode(GL_MODELVIEW);
2214                         glPopMatrix();
2215                 }
2216         }
2217 }
2218 #endif
2219
2220 /* nice AA filled circle */
2221 /* this might have some more generic use */
2222 static void circle_draw(float x, float y, float size, int type, int col[3])
2223 {
2224         /* 16 values of sin function */
2225         static float si[16] = {
2226                 0.00000000, 0.39435585,0.72479278,0.93775213,
2227                 0.99871650,0.89780453,0.65137248,0.29936312,
2228                 -0.10116832,-0.48530196,-0.79077573,-0.96807711,
2229                 -0.98846832,-0.84864425,-0.57126821,-0.20129852
2230         };
2231         /* 16 values of cos function */
2232         static float co[16] ={
2233                 1.00000000,0.91895781,0.68896691,0.34730525,
2234                 -0.05064916,-0.44039415,-0.75875812,-0.95413925,
2235                 -0.99486932,-0.87434661,-0.61210598,-0.25065253,
2236                 0.15142777,0.52896401,0.82076344,0.97952994,
2237         };
2238         int a;
2239         
2240         glColor3ub(col[0], col[1], col[2]);
2241         
2242         glBegin(GL_POLYGON);
2243         for(a=0; a<16; a++)
2244                 glVertex2f(x+size*si[a], y+size*co[a]);
2245         glEnd();
2246         
2247         glColor4ub(0, 0, 0, 150);
2248         glEnable(GL_BLEND);
2249         glEnable( GL_LINE_SMOOTH );
2250         glBegin(GL_LINE_LOOP);
2251         for(a=0; a<16; a++)
2252                 glVertex2f(x+size*si[a], y+size*co[a]);
2253         glEnd();
2254         glDisable( GL_LINE_SMOOTH );
2255         glDisable(GL_BLEND);
2256 }
2257
2258 static void socket_circle_draw(bNodeSocket *sock, float size)
2259 {
2260         int col[3];
2261         
2262         /* choose color based on sock flags */
2263         if(sock->flag & SELECT) {
2264                 if(sock->flag & SOCK_SEL) {
2265                         col[0]= 240; col[1]= 200; col[2]= 40;}
2266                 else if(sock->type==SOCK_VALUE) {
2267                         col[0]= 200; col[1]= 200; col[2]= 200;}
2268                 else if(sock->type==SOCK_VECTOR) {
2269                         col[0]= 140; col[1]= 140; col[2]= 240;}
2270                 else if(sock->type==SOCK_RGBA) {
2271                         col[0]= 240; col[1]= 240; col[2]= 100;}
2272                 else {
2273                         col[0]= 140; col[1]= 240; col[2]= 140;}
2274         }
2275         else if(sock->flag & SOCK_SEL) {
2276                 col[0]= 200; col[1]= 160; col[2]= 0;}
2277         else {
2278                 if(sock->type==-1) {
2279                         col[0]= 0; col[1]= 0; col[2]= 0;}
2280                 else if(sock->type==SOCK_VALUE) {
2281                         col[0]= 160; col[1]= 160; col[2]= 160;}
2282                 else if(sock->type==SOCK_VECTOR) {
2283                         col[0]= 100; col[1]= 100; col[2]= 200;}
2284                 else if(sock->type==SOCK_RGBA) {
2285                         col[0]= 200; col[1]= 200; col[2]= 40;}
2286                 else { 
2287                         col[0]= 100; col[1]= 200; col[2]= 100;}
2288         }
2289         
2290         circle_draw(sock->locx, sock->locy, size, sock->type, col);
2291 }
2292
2293 /* not a callback */
2294 static void node_draw_preview(bNodePreview *preview, rctf *prv)
2295 {
2296         float xscale= (prv->xmax-prv->xmin)/((float)preview->xsize);
2297         float yscale= (prv->ymax-prv->ymin)/((float)preview->ysize);
2298         float tile= (prv->xmax - prv->xmin) / 10.0;
2299         float x, y;
2300         
2301         /* draw checkerboard backdrop to show alpha */
2302         glColor3ub(120, 120, 120);
2303         glRectf(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2304         glColor3ub(160, 160, 160);
2305         
2306         for(y=prv->ymin; y<prv->ymax; y+=tile*2) {
2307                 for(x=prv->xmin; x<prv->xmax; x+=tile*2) {
2308                         float tilex= tile, tiley= tile;
2309
2310                         if(x+tile > prv->xmax)
2311                                 tilex= prv->xmax-x;
2312                         if(y+tile > prv->ymax)
2313                                 tiley= prv->ymax-y;
2314
2315                         glRectf(x, y, x + tilex, y + tiley);
2316                 }
2317         }
2318         for(y=prv->ymin+tile; y<prv->ymax; y+=tile*2) {
2319                 for(x=prv->xmin+tile; x<prv->xmax; x+=tile*2) {
2320                         float tilex= tile, tiley= tile;
2321
2322                         if(x+tile > prv->xmax)
2323                                 tilex= prv->xmax-x;
2324                         if(y+tile > prv->ymax)
2325                                 tiley= prv->ymax-y;
2326
2327                         glRectf(x, y, x + tilex, y + tiley);
2328                 }
2329         }
2330         
2331         glPixelZoom(xscale, yscale);
2332         glEnable(GL_BLEND);
2333         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );  /* premul graphics */
2334         
2335         glColor4f(1.0, 1.0, 1.0, 1.0);
2336         glaDrawPixelsTex(prv->xmin, prv->ymin, preview->xsize, preview->ysize, GL_FLOAT, preview->rect);
2337         
2338         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2339         glDisable(GL_BLEND);
2340         glPixelZoom(1.0f, 1.0f);
2341
2342         BIF_ThemeColorShadeAlpha(TH_BACK, -15, +100);
2343         fdrawbox(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2344         
2345 }
2346
2347 /* based on settings in node, sets drawing rect info. each redraw! */
2348 static void node_update_hidden(bNode *node)
2349 {
2350         bNodeSocket *nsock;
2351         float rad, drad, hiddenrad= HIDDEN_RAD;
2352         int totin=0, totout=0, tot;
2353         
2354         /* calculate minimal radius */
2355         for(nsock= node->inputs.first; nsock; nsock= nsock->next)
2356                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2357                         totin++;
2358         for(nsock= node->outputs.first; nsock; nsock= nsock->next)
2359                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2360                         totout++;
2361         
2362         tot= MAX2(totin, totout);
2363         if(tot>4) {
2364                 hiddenrad += 5.0*(float)(tot-4);
2365         }
2366         
2367         node->totr.xmin= node->locx;
2368         node->totr.xmax= node->locx + 3*hiddenrad + node->miniwidth;
2369         node->totr.ymax= node->locy + (hiddenrad - 0.5f*NODE_DY);
2370         node->totr.ymin= node->totr.ymax - 2*hiddenrad;
2371         
2372         /* output sockets */
2373         rad=drad= M_PI/(1.0f + (float)totout);
2374         
2375         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2376                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2377                         nsock->locx= node->totr.xmax - hiddenrad + sin(rad)*hiddenrad;
2378                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2379                         rad+= drad;
2380                 }
2381         }
2382         
2383         /* input sockets */
2384         rad=drad= - M_PI/(1.0f + (float)totin);
2385         
2386         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2387                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2388                         nsock->locx= node->totr.xmin + hiddenrad + sin(rad)*hiddenrad;
2389                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2390                         rad+= drad;
2391                 }
2392         }
2393 }
2394
2395 /* based on settings in node, sets drawing rect info. each redraw! */
2396 static void node_update(bNode *node)
2397 {
2398         bNodeSocket *nsock;
2399         float dy= node->locy;
2400         
2401         /* header */
2402         dy-= NODE_DY;
2403         
2404         /* little bit space in top */
2405         if(node->outputs.first)
2406                 dy-= NODE_DYS/2;
2407         
2408         /* output sockets */
2409         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2410                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2411                         nsock->locx= node->locx + node->width;
2412                         nsock->locy= dy - NODE_DYS;
2413                         dy-= NODE_DY;
2414                 }
2415         }
2416         
2417         node->prvr.xmin= node->butr.xmin= node->locx + NODE_DYS;
2418         node->prvr.xmax= node->butr.xmax= node->locx + node->width- NODE_DYS;
2419         
2420         /* preview rect? */
2421         if(node->flag & NODE_PREVIEW) {
2422                 /* only recalculate size when there's a preview actually, otherwise we use stored result */
2423                 if(node->preview && node->preview->rect) {
2424                         float aspect= 1.0f;
2425                         
2426                         if(node->preview && node->preview->xsize && node->preview->ysize) 
2427                                 aspect= (float)node->preview->ysize/(float)node->preview->xsize;
2428                         
2429                         dy-= NODE_DYS/2;
2430                         node->prvr.ymax= dy;
2431                         
2432                         if(aspect <= 1.0f)
2433                                 node->prvr.ymin= dy - aspect*(node->width-NODE_DY);
2434                         else {
2435                                 float dx= (node->width - NODE_DYS) - (node->width- NODE_DYS)/aspect;    /* width correction of image */
2436                                 
2437                                 node->prvr.ymin= dy - (node->width-NODE_DY);
2438                                 
2439                                 node->prvr.xmin+= 0.5*dx;
2440                                 node->prvr.xmax-= 0.5*dx;
2441                         }
2442
2443                         dy= node->prvr.ymin - NODE_DYS/2;
2444
2445                         /* make sure that maximums are bigger or equal to minimums */
2446                         if(node->prvr.xmax < node->prvr.xmin) SWAP(float, node->prvr.xmax, node->prvr.xmin);
2447                         if(node->prvr.ymax < node->prvr.ymin) SWAP(float, node->prvr.ymax, node->prvr.ymin);
2448                 }
2449                 else {
2450                         float oldh= node->prvr.ymax - node->prvr.ymin;
2451                         if(oldh==0.0f)
2452                                 oldh= 0.6f*node->width-NODE_DY;
2453                         dy-= NODE_DYS/2;
2454                         node->prvr.ymax= dy;
2455                         node->prvr.ymin= dy - oldh;
2456                         dy= node->prvr.ymin - NODE_DYS/2;
2457                 }
2458         }
2459
2460         /* XXX ugly hack, typeinfo for group is generated */
2461         if(node->type == NODE_GROUP)
2462                 node->typeinfo->butfunc= node_buts_group;
2463         
2464         /* buttons rect? */
2465         if((node->flag & NODE_OPTIONS) && node->typeinfo->butfunc) {
2466                 dy-= NODE_DYS/2;
2467                 node->butr.ymax= dy;
2468                 node->butr.ymin= dy - (float)node->typeinfo->butfunc(NULL, NULL, node, NULL);
2469                 dy= node->butr.ymin - NODE_DYS/2;
2470         }
2471         
2472         /* input sockets */
2473         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2474                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2475                         nsock->locx= node->locx;
2476                         nsock->locy= dy - NODE_DYS;
2477                         dy-= NODE_DY;
2478                 }
2479         }
2480         
2481         /* little bit space in end */
2482         if(node->inputs.first || (node->flag & (NODE_OPTIONS|NODE_PREVIEW))==0 )
2483                 dy-= NODE_DYS/2;
2484         
2485         node->totr.xmin= node->locx;
2486         node->totr.xmax= node->locx + node->width;
2487         node->totr.ymax= node->locy;
2488         node->totr.ymin= dy;
2489 }
2490
2491 /* based on settings in node, sets drawing rect info. each redraw! */
2492 /* note: this assumes only 1 group at a time is drawn (linked data) */
2493 /* in node->totr the entire boundbox for the group is stored */
2494 static void node_update_group(bNode *gnode)
2495 {
2496         bNodeTree *ngroup= (bNodeTree *)gnode->id;
2497         bNode *node;
2498         bNodeSocket *nsock;
2499         rctf *rect= &gnode->totr;
2500         int counter;
2501         
2502         /* center them, is a bit of abuse of locx and locy though */
2503         for(node= ngroup->nodes.first; node; node= node->next) {
2504                 node->locx+= gnode->locx;
2505                 node->locy+= gnode->locy;
2506                 if(node->flag & NODE_HIDDEN)
2507                         node_update_hidden(node);
2508                 else
2509                         node_update(node);
2510                 node->locx-= gnode->locx;
2511                 node->locy-= gnode->locy;
2512         }
2513         counter= 1;
2514         for(node= ngroup->nodes.first; node; node= node->next) {
2515                 if(counter) {
2516                         *rect= node->totr;
2517                         counter= 0;
2518                 }
2519                 else
2520                         BLI_union_rctf(rect, &node->totr);
2521         }
2522         if(counter==1) return;  /* should be prevented? */
2523         
2524         rect->xmin-= NODE_DY;
2525         rect->ymin-= NODE_DY;
2526         rect->xmax+= NODE_DY;
2527         rect->ymax+= NODE_DY;
2528         
2529         /* output sockets */
2530         for(nsock= gnode->outputs.first; nsock; nsock= nsock->next) {
2531                 nsock->locx= rect->xmax;
2532                 nsock->locy= nsock->tosock->locy;
2533         }
2534         
2535         /* input sockets */
2536         for(nsock= gnode->inputs.first; nsock; nsock= nsock->next) {
2537                 nsock->locx= rect->xmin;
2538                 nsock->locy= nsock->tosock->locy;
2539         }
2540 }
2541
2542 static void node_scaling_widget(int color_id, float aspect, float xmin, float ymin, float xmax, float ymax)
2543 {
2544         float dx;
2545         float dy;
2546         
2547         dx= 0.5f*(xmax-xmin);
2548         dy= 0.5f*(ymax-ymin);
2549         
2550         BIF_ThemeColorShade(color_id, +30);     
2551         fdrawline(xmin, ymin, xmax, ymax);
2552         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
2553         
2554         BIF_ThemeColorShade(color_id, -10);
2555         fdrawline(xmin, ymin+aspect, xmax, ymax+aspect);
2556         fdrawline(xmin+dx, ymin+aspect, xmax, ymax-dy+aspect);
2557 }
2558
2559 static int node_get_colorid(bNode *node)
2560 {
2561         if(node->typeinfo->nclass==NODE_CLASS_INPUT)
2562                 return TH_NODE_IN_OUT;
2563         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
2564                 if(node->flag & NODE_DO_OUTPUT)
2565                         return TH_NODE_IN_OUT;
2566                 else
2567                         return TH_NODE;
2568         }
2569         if(node->typeinfo->nclass==NODE_CLASS_CONVERTOR)
2570                 return TH_NODE_CONVERTOR;
2571         if(ELEM3(node->typeinfo->nclass, NODE_CLASS_OP_COLOR, NODE_CLASS_OP_VECTOR, NODE_CLASS_OP_FILTER))
2572                 return TH_NODE_OPERATOR;
2573         if(node->typeinfo->nclass==NODE_CLASS_GROUP)
2574                 return TH_NODE_GROUP;
2575         return TH_NODE;
2576 }
2577
2578 static void node_draw_basis(ScrArea *sa, SpaceNode *snode, bNode *node)
2579 {
2580         bNodeSocket *sock;
2581         uiBlock *block= NULL;
2582         uiBut *bt;
2583         rctf *rct= &node->totr;
2584         float slen, iconofs;
2585         int ofs, color_id= node_get_colorid(node);
2586         char showname[128];
2587         
2588         uiSetRoundBox(15-4);
2589         ui_dropshadow(rct, BASIS_RAD, snode->aspect, node->flag & SELECT);
2590         
2591         /* header */
2592         if(color_id==TH_NODE)
2593                 BIF_ThemeColorShade(color_id, -20);
2594         else
2595                 BIF_ThemeColor(color_id);
2596                 
2597         uiSetRoundBox(3);
2598         uiRoundBox(rct->xmin, rct->ymax-NODE_DY, rct->xmax, rct->ymax, BASIS_RAD);
2599         
2600         /* show/hide icons, note this sequence is copied in editnode.c */
2601         iconofs= rct->xmax;
2602         
2603         if(node->typeinfo->flag & NODE_PREVIEW) {
2604                 int icon_id;
2605                 
2606                 if(node->flag & (NODE_ACTIVE_ID|NODE_DO_OUTPUT))
2607                         icon_id= ICON_MATERIAL;
2608                 else
2609                         icon_id= ICON_MATERIAL_DEHLT;
2610                 iconofs-= 18.0f;
2611                 glEnable(GL_BLEND);
2612                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, icon_id, snode->aspect, -60);
2613                 glDisable(GL_BLEND);
2614         }
2615         if(node->type == NODE_GROUP) {
2616                 
2617                 iconofs-= 18.0f;
2618                 glEnable(GL_BLEND);
2619                 if(node->id->lib) {
2620                         glPixelTransferf(GL_GREEN_SCALE, 0.7f);
2621                         glPixelTransferf(GL_BLUE_SCALE, 0.3f);
2622                         BIF_icon_draw_aspect(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, snode->aspect);
2623                         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
2624                         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
2625                 }
2626                 else {
2627                         BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, snode->aspect, -60);
2628                 }
2629                 glDisable(GL_BLEND);
2630         }
2631         if(node->typeinfo->flag & NODE_OPTIONS) {
2632                 iconofs-= 18.0f;
2633                 glEnable(GL_BLEND);
2634                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_BUTS, snode->aspect, -60);
2635                 glDisable(GL_BLEND);
2636         }
2637         {       /* always hide/reveil unused sockets */ 
2638                 int shade;
2639
2640                 iconofs-= 18.0f;
2641                 if(node_has_hidden_sockets(node))
2642                         shade= -40;
2643                 else
2644                         shade= -90;
2645                 glEnable(GL_BLEND);
2646                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_PLUS, snode->aspect, shade);
2647                 glDisable(GL_BLEND);
2648         }
2649         
2650         /* title */
2651         if(node->flag & SELECT) 
2652                 BIF_ThemeColor(TH_TEXT_HI);
2653         else
2654                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
2655         
2656         /* open/close entirely? */
2657         ui_draw_tria_icon(rct->xmin+8.0f, rct->ymax-NODE_DY+4.0f, snode->aspect, 'v');
2658
2659         if(node->flag & SELECT) 
2660                 BIF_ThemeColor(TH_TEXT_HI);
2661         else
2662                 BIF_ThemeColor(TH_TEXT);
2663         
2664         ui_rasterpos_safe(rct->xmin+19.0f, rct->ymax-NODE_DY+5.0f, snode->aspect);
2665         
2666         if(node->username[0]) {
2667                 strcpy(showname,"(");
2668                 strcat(showname, node->username);
2669                 strcat(showname,") ");
2670                 strcat(showname, node->name);
2671         }
2672         else
2673                 strcpy(showname, node->name);
2674
2675         snode_drawstring(snode, showname, (int)(iconofs - rct->xmin-18.0f));
2676
2677         /* body */
2678         BIF_ThemeColor4(TH_NODE);
2679         glEnable(GL_BLEND);
2680         uiSetRoundBox(8);
2681         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax-NODE_DY, BASIS_RAD);
2682         glDisable(GL_BLEND);
2683         
2684         /* scaling indicator */
2685         node_scaling_widget(TH_NODE, snode->aspect, rct->xmax-BASIS_RAD*snode->aspect, rct->ymin, rct->xmax, rct->ymin+BASIS_RAD*snode->aspect);
2686
2687         /* outline active emphasis */
2688         if(node->flag & NODE_ACTIVE) {
2689                 glEnable(GL_BLEND);
2690                 glColor4ub(200, 200, 200, 140);
2691                 uiSetRoundBox(15-4);
2692                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
2693                 glDisable(GL_BLEND);
2694         }
2695         
2696         /* we make buttons for input sockets, if... */
2697         if(node->flag & NODE_OPTIONS) {
2698                 if(node->inputs.first || node->typeinfo->butfunc) {
2699                         char str[32];
2700                         
2701                         /* make unique block name, also used for handling blocks in editnode.c */
2702                         sprintf(str, "node buttons %p", node);
2703                         
2704                         block= uiNewBlock(&sa->uiblocks, str, UI_EMBOSS, UI_HELV, sa->win);
2705                         uiBlockSetFlag(block, UI_BLOCK_NO_HILITE);
2706                         if(snode->id)
2707                                 uiSetButLock(snode->id->lib!=NULL, ERROR_LIBDATA_MESSAGE);
2708                 }
2709         }
2710         
2711         /* hurmf... another candidate for callback, have to see how this works first */
2712         if(node->id && block && snode->treetype==NTREE_SHADER)
2713                 nodeShaderSynchronizeID(node, 0);
2714         
2715         /* socket inputs, buttons */
2716         for(sock= node->inputs.first; sock; sock= sock->next) {
2717                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2718                         socket_circle_draw(sock, NODE_SOCKSIZE);
2719                         
2720                         if(block && sock->link==NULL) {
2721                                 float *butpoin= sock->ns.vec;
2722                                 
2723                                 if(sock->type==SOCK_VALUE) {
2724                                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, sock->name, 
2725                                                   (short)sock->locx+NODE_DYS, (short)(sock->locy)-9, (short)node->width-NODE_DY, 17, 
2726                                                   butpoin, sock->ns.min, sock->ns.max, 10, 2, "");
2727                                         uiButSetFunc(bt, node_sync_cb, snode, node);
2728                                 }
2729                                 else if(sock->type==SOCK_VECTOR) {
2730                                         uiDefBlockBut(block, socket_vector_menu, sock, sock->name, 
2731                                                   (short)sock->locx+NODE_DYS, (short)sock->locy-9, (short)node->width-NODE_DY, 17, 
2732                                                   "");
2733                                 }
2734                                 else if(block && sock->type==SOCK_RGBA) {
2735                                         short labelw= node->width-NODE_DY-40, width;
2736                                         
2737                                         if(labelw>0) width= 40; else width= node->width-NODE_DY;
2738                                         
2739                                         bt= uiDefButF(block, COL, B_NODE_EXEC+node->nr, "", 
2740                                                 (short)(sock->locx+NODE_DYS), (short)sock->locy-8, width, 15, 
2741                                                    butpoin, 0, 0, 0, 0, "");
2742                                         uiButSetFunc(bt, node_sync_cb, snode, node);
2743                                         
2744                                         if(labelw>0) uiDefBut(block, LABEL, 0, sock->name, 
2745                                                                                    (short)(sock->locx+NODE_DYS) + 40, (short)sock->locy-8, labelw, 15, 
2746                                                                                    NULL, 0, 0, 0, 0, "");
2747                                 }
2748                         }
2749                         else {
2750                                 BIF_ThemeColor(TH_TEXT);
2751                                 ui_rasterpos_safe(sock->locx+8.0f, sock->locy-5.0f, snode->aspect);
2752                                 BIF_DrawString(snode->curfont, sock->name, 0);
2753                         }
2754                 }
2755         }
2756         
2757         /* socket outputs */
2758         for(sock= node->outputs.first; sock; sock= sock->next) {
2759                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2760                         socket_circle_draw(sock, NODE_SOCKSIZE);
2761                         
2762                         BIF_ThemeColor(TH_TEXT);
2763                         ofs= 0;
2764                         slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name, 0);
2765                         while(slen > node->width) {
2766                                 ofs++;
2767                                 slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name+ofs, 0);
2768                         }
2769                         ui_rasterpos_safe(sock->locx-8.0f-slen, sock->locy-5.0f, snode->aspect);
2770                         BIF_DrawString(snode->curfont, sock->name+ofs, 0);
2771                 }
2772         }
2773         
2774         /* preview */
2775         if(node->flag & NODE_PREVIEW)
2776                 if(node->preview && node->preview->rect)
2777                         node_draw_preview(node->preview, &node->prvr);
2778                 
2779         /* buttons */
2780         if(node->flag & NODE_OPTIONS) {
2781                 if(block) {
2782                         if(node->typeinfo->butfunc) {
2783                                 node->typeinfo->butfunc(block, snode->nodetree, node, &node->butr);
2784                         }
2785                         uiDrawBlock(block);
2786                 }
2787         }
2788
2789 }
2790
2791 static void node_draw_hidden(SpaceNode *snode, bNode *node)
2792 {
2793         bNodeSocket *sock;
2794         rctf *rct= &node->totr;
2795         float dx, centy= 0.5f*(rct->ymax+rct->ymin);
2796         float hiddenrad= 0.5f*(rct->ymax-rct->ymin);
2797         int color_id= node_get_colorid(node);
2798         char showname[128];
2799         
2800         /* shadow */
2801         uiSetRoundBox(15);
2802         ui_dropshadow(rct, hiddenrad, snode->aspect, node->flag & SELECT);
2803
2804         /* body */
2805         BIF_ThemeColor(color_id);       
2806         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad);
2807         
2808         /* outline active emphasis */
2809         if(node->flag & NODE_ACTIVE) {
2810                 glEnable(GL_BLEND);
2811                 glColor4ub(200, 200, 200, 140);
2812                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad);
2813                 glDisable(GL_BLEND);
2814         }
2815         
2816         /* title */
2817         if(node->flag & SELECT) 
2818                 BIF_ThemeColor(TH_TEXT_HI);
2819         else
2820                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
2821         
2822         /* open entirely icon */
2823         ui_draw_tria_icon(rct->xmin+9.0f, centy-6.0f, snode->aspect, 'h');      
2824         
2825         if(node->flag & SELECT) 
2826                 BIF_ThemeColor(TH_TEXT_HI);
2827         else
2828                 BIF_ThemeColor(TH_TEXT);
2829         
2830         if(node->miniwidth>0.0f) {
2831                 ui_rasterpos_safe(rct->xmin+21.0f, centy-4.0f, snode->aspect);
2832
2833                 if(node->username[0]) {
2834                         strcpy(showname,"(");
2835                         strcat(showname, node->username);
2836                         strcat(showname,") ");
2837                         strcat(showname, node->name);
2838                 }
2839                 else
2840                         strcpy(showname, node->name);
2841
2842                 snode_drawstring(snode, showname, (int)(rct->xmax - rct->xmin-18.0f -12.0f));
2843         }       
2844
2845         /* scale widget thing */
2846         BIF_ThemeColorShade(color_id, -10);     
2847         dx= 10.0f;
2848         fdrawline(rct->xmax-dx, centy-4.0f, rct->xmax-dx, centy+4.0f);
2849         fdrawline(rct->xmax-dx-3.0f*snode->aspect, centy-4.0f, rct->xmax-dx-3.0f*snode->aspect, centy+4.0f);
2850         
2851         BIF_ThemeColorShade(color_id, +30);
2852         dx-= snode->aspect;
2853         fdrawline(rct->xmax-dx, centy-4.0f, rct->xmax-dx, centy+4.0f);
2854         fdrawline(rct->xmax-dx-3.0f*snode->aspect, centy-4.0f, rct->xmax-dx-3.0f*snode->aspect, centy+4.0f);
2855         
2856         /* sockets */
2857         for(sock= node->inputs.first; sock; sock= sock->next) {
2858                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2859                         socket_circle_draw(sock, NODE_SOCKSIZE);
2860         }
2861         
2862         for(sock= node->outputs.first; sock; sock= sock->next) {
2863                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2864                         socket_circle_draw(sock, NODE_SOCKSIZE);
2865         }
2866 }
2867
2868 /* note; this is used for fake links in groups too */
2869 void node_draw_link(SpaceNode *snode, bNodeLink *link)
2870 {
2871         float vec[4][3];
2872         float dist, spline_step, mx=0.0f, my=0.0f;
2873         int curve_res, do_shaded= 1, th_col1= TH_WIRE, th_col2= TH_WIRE;
2874         
2875         if(link->fromnode==NULL && link->tonode==NULL)
2876                 return;
2877         
2878         /* this is dragging link */
2879         if(link->fromnode==NULL || link->tonode==NULL) {
2880                 short mval[2];
2881                 getmouseco_areawin(mval);
2882                 areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
2883                 BIF_ThemeColor(TH_WIRE);
2884                 do_shaded= 0;
2885         }
2886         else {
2887                 /* going to give issues once... */
2888                 if(link->tosock->flag & SOCK_UNAVAIL)
2889                         return;
2890                 if(link->fromsock->flag & SOCK_UNAVAIL)
2891                         return;
2892                 
2893                 /* a bit ugly... but thats how we detect the internal group links */
2894                 if(link->fromnode==link->tonode) {
2895                         BIF_ThemeColorBlend(TH_BACK, TH_WIRE, 0.25f);
2896                         do_shaded= 0;
2897                 }
2898                 else {
2899                         /* check cyclic */
2900                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2901                                 if(link->fromnode->flag & SELECT)
2902                                         th_col1= TH_EDGE_SELECT;
2903                                 if(link->tonode->flag & SELECT)
2904                                         th_col2= TH_EDGE_SELECT;
2905                         }                               
2906                         else {
2907                                 BIF_ThemeColor(TH_REDALERT);
2908                                 do_shaded= 0;
2909                         }
2910                 }
2911         }
2912         
2913         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0; /* only 2d spline, set the Z to 0*/
2914         
2915         /* in v0 and v3 we put begin/end points */
2916         if(link->fromnode) {
2917                 vec[0][0]= link->fromsock->locx;
2918                 vec[0][1]= link->fromsock->locy;
2919         }
2920         else {
2921                 vec[0][0]= mx;
2922                 vec[0][1]= my;
2923         }
2924         if(link->tonode) {
2925                 vec[3][0]= link->tosock->locx;
2926                 vec[3][1]= link->tosock->locy;
2927         }
2928         else {
2929                 vec[3][0]= mx;
2930                 vec[3][1]= my;
2931         }
2932         
2933         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2934         
2935         /* check direction later, for top sockets */
2936         vec[1][0]= vec[0][0]+dist;
2937         vec[1][1]= vec[0][1];
2938         
2939         vec[2][0]= vec[3][0]-dist;
2940         vec[2][1]= vec[3][1];
2941         
2942         if( MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > G.v2d->cur.xmax); /* clipped */  
2943         else if ( MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < G.v2d->cur.xmin); /* clipped */
2944         else {
2945                 curve_res = 24;
2946                 
2947                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2948                 dist = 1.0f/curve_res;
2949                 spline_step = 0.0f;
2950                 
2951                 glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
2952                 glBegin(GL_LINE_STRIP);
2953                 while (spline_step < 1.000001f) {
2954                         if(do_shaded)
2955                                 BIF_ThemeColorBlend(th_col1, th_col2, spline_step);
2956                         glEvalCoord1f(spline_step);
2957                         spline_step += dist;
2958                 }
2959                 glEnd();
2960         }
2961 }
2962
2963 static void node_draw_nodetree(ScrArea *sa, SpaceNode *snode, bNodeTree *ntree)
2964 {
2965         bNode *node;
2966         bNodeLink *link;
2967         int a;
2968         
2969         if(ntree==NULL) return;         /* groups... */
2970         
2971         /* node lines */
2972         glEnable(GL_BLEND);
2973         glEnable( GL_LINE_SMOOTH );
2974         for(link= ntree->links.first; link; link= link->next)
2975                 node_draw_link(snode, link);
2976         glDisable(GL_BLEND);
2977         glDisable( GL_LINE_SMOOTH );
2978         
2979         /* not selected first */
2980         for(a=0, node= ntree->nodes.first; node; node= node->next, a++) {
2981                 node->nr= a;            /* index of node in list, used for exec event code */
2982                 if(!(node->flag & SELECT)) {
2983                         if(node->flag & NODE_GROUP_EDIT);
2984                         else if(node->flag & NODE_HIDDEN)
2985                                 node_draw_hidden(snode, node);
2986                         else
2987                                 node_draw_basis(sa, snode, node);
2988                 }
2989         }
2990         
2991         /* selected */
2992         for(node= ntree->nodes.first; node; node= node->next) {
2993                 if(node->flag & SELECT) {
2994                         if(node->flag & NODE_GROUP_EDIT);
2995                         else if(node->flag & NODE_HIDDEN)
2996                                 node_draw_hidden(snode, node);
2997                         else
2998                                 node_draw_basis(sa, snode, node);
2999                 }
3000         }       
3001 }
3002
3003 /* fake links from groupnode to internal nodes */
3004 static void node_draw_group_links(SpaceNode *snode, bNode *gnode)
3005 {
3006         bNodeLink fakelink;
3007         bNodeSocket *sock;
3008         
3009         glEnable(GL_BLEND);
3010         glEnable( GL_LINE_SMOOTH );
3011         
3012         fakelink.tonode= fakelink.fromnode= gnode;
3013         
3014         for(sock= gnode->inputs.first; sock; sock= sock->next) {
3015                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
3016                         if(sock->tosock) {
3017                                 fakelink.fromsock= sock;
3018                                 fakelink.tosock= sock->tosock;
3019                                 node_draw_link(snode, &fakelink);
3020                         }
3021                 }
3022         }
3023         
3024         for(sock= gnode->outputs.first; sock; sock= sock->next) {
3025                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
3026                         if(sock->tosock) {
3027                                 fakelink.tosock= sock;
3028                                 fakelink.fromsock= sock->tosock;
3029           &