Color info in node editor backdrop now supports color management.
[blender.git] / source / blender / editors / space_node / node_edit.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Nathan Letwory
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/space_node/node_edit.c
31  *  \ingroup spnode
32  */
33
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <math.h>
38 #include <string.h>
39 #include <errno.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_object_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_scene_types.h"
47
48 #include "BLI_math.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_storage_types.h"
51 #include "BLI_utildefines.h"
52
53 #include "BKE_context.h"
54 #include "BKE_global.h"
55 #include "BKE_image.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_node.h"
59 #include "BKE_material.h"
60 #include "BKE_paint.h"
61 #include "BKE_screen.h"
62 #include "BKE_texture.h"
63 #include "BKE_report.h"
64
65 #include "RE_pipeline.h"
66
67 #include "IMB_imbuf_types.h"
68
69 #include "ED_node.h"
70 #include "ED_screen.h"
71 #include "ED_space_api.h"
72 #include "ED_render.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "UI_interface.h"
81 #include "UI_view2d.h"
82
83 #include "IMB_imbuf.h"
84
85 #include "RNA_enum_types.h"
86
87 #include "node_intern.h"
88
89 static EnumPropertyItem socket_in_out_items[] = {
90         { SOCK_IN, "IN", 0, "In", "" },
91         { SOCK_OUT, "OUT", 0, "Out", "" },
92         { 0, NULL, 0, NULL, NULL}
93 };
94
95 /* ***************** composite job manager ********************** */
96
97 typedef struct CompoJob {
98         Scene *scene;
99         bNodeTree *ntree;
100         bNodeTree *localtree;
101         short *stop;
102         short *do_update;
103         float *progress;
104 } CompoJob;
105
106 /* called by compo, only to check job 'stop' value */
107 static int compo_breakjob(void *cjv)
108 {
109         CompoJob *cj= cjv;
110         
111         return *(cj->stop);
112 }
113
114 /* called by compo, wmJob sends notifier */
115 static void compo_redrawjob(void *cjv, char *UNUSED(str))
116 {
117         CompoJob *cj= cjv;
118         
119         *(cj->do_update)= 1;
120 }
121
122 static void compo_freejob(void *cjv)
123 {
124         CompoJob *cj= cjv;
125
126         if(cj->localtree) {
127                 ntreeLocalMerge(cj->localtree, cj->ntree);
128         }
129         MEM_freeN(cj);
130 }
131
132 /* only now we copy the nodetree, so adding many jobs while
133    sliding buttons doesn't frustrate */
134 static void compo_initjob(void *cjv)
135 {
136         CompoJob *cj= cjv;
137
138         cj->localtree= ntreeLocalize(cj->ntree);
139 }
140
141 /* called before redraw notifiers, it moves finished previews over */
142 static void compo_updatejob(void *cjv)
143 {
144         CompoJob *cj= cjv;
145         
146         ntreeLocalSync(cj->localtree, cj->ntree);
147 }
148
149 static void compo_progressjob(void *cjv, float progress)
150 {
151         CompoJob *cj= cjv;
152         
153         *(cj->progress) = progress;
154 }
155
156
157 /* only this runs inside thread */
158 static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
159 {
160         CompoJob *cj= cjv;
161         bNodeTree *ntree= cj->localtree;
162
163         if(cj->scene->use_nodes==0)
164                 return;
165         
166         cj->stop= stop;
167         cj->do_update= do_update;
168         cj->progress= progress;
169         
170         ntree->test_break= compo_breakjob;
171         ntree->tbh= cj;
172         ntree->stats_draw= compo_redrawjob;
173         ntree->sdh= cj;
174         ntree->progress= compo_progressjob;
175         ntree->prh= cj;
176         
177         // XXX BIF_store_spare();
178         
179         ntreeCompositExecTree(ntree, &cj->scene->r, 1); /* 1 is do_previews */
180         
181         ntree->test_break= NULL;
182         ntree->stats_draw= NULL;
183         ntree->progress= NULL;
184
185 }
186
187 void snode_composite_job(const bContext *C, ScrArea *sa)
188 {
189         SpaceNode *snode= sa->spacedata.first;
190         wmJob *steve;
191         CompoJob *cj;
192
193         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Compositing", WM_JOB_EXCL_RENDER|WM_JOB_PROGRESS);
194         cj= MEM_callocN(sizeof(CompoJob), "compo job");
195         
196         /* customdata for preview thread */
197         cj->scene= CTX_data_scene(C);
198         cj->ntree= snode->nodetree;
199         
200         /* setup job */
201         WM_jobs_customdata(steve, cj, compo_freejob);
202         WM_jobs_timer(steve, 0.1, NC_SCENE, NC_SCENE|ND_COMPO_RESULT);
203         WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob, NULL);
204         
205         WM_jobs_start(CTX_wm_manager(C), steve);
206         
207 }
208
209 /* ***************************************** */
210
211 /* operator poll callback */
212 static int composite_node_active(bContext *C)
213 {
214         if( ED_operator_node_active(C)) {
215                 SpaceNode *snode= CTX_wm_space_node(C);
216                 if(snode->treetype==NTREE_COMPOSIT)
217                         return 1;
218         }
219         return 0;
220 }
221
222 /* also checks for edited groups */
223 static bNode *editnode_get_active(bNodeTree *ntree)
224 {
225         bNode *node;
226         
227         /* check for edited group */
228         for(node= ntree->nodes.first; node; node= node->next)
229                 if(node->flag & NODE_GROUP_EDIT)
230                         break;
231         if(node)
232                 return nodeGetActive((bNodeTree *)node->id);
233         else
234                 return nodeGetActive(ntree);
235 }
236
237 void snode_notify(bContext *C, SpaceNode *snode)
238 {
239         WM_event_add_notifier(C, NC_NODE|NA_EDITED, NULL);
240
241         if(snode->treetype==NTREE_SHADER)
242                 WM_event_add_notifier(C, NC_MATERIAL|ND_NODES, snode->id);
243         else if(snode->treetype==NTREE_COMPOSIT)
244                 WM_event_add_notifier(C, NC_SCENE|ND_NODES, snode->id);
245         else if(snode->treetype==NTREE_TEXTURE)
246                 WM_event_add_notifier(C, NC_TEXTURE|ND_NODES, snode->id);
247 }
248
249 bNode *node_tree_get_editgroup(bNodeTree *nodetree)
250 {
251         bNode *gnode;
252         
253         /* get the groupnode */
254         for(gnode= nodetree->nodes.first; gnode; gnode= gnode->next)
255                 if(gnode->flag & NODE_GROUP_EDIT)
256                         break;
257         return gnode;
258 }
259
260 /* assumes nothing being done in ntree yet, sets the default in/out node */
261 /* called from shading buttons or header */
262 void ED_node_shader_default(Material *ma)
263 {
264         bNode *in, *out;
265         bNodeSocket *fromsock, *tosock;
266         
267         /* but lets check it anyway */
268         if(ma->nodetree) {
269                 if (G.f & G_DEBUG)
270                         printf("error in shader initialize\n");
271                 return;
272         }
273         
274         ma->nodetree= ntreeAddTree("Shader Nodetree", NTREE_SHADER, FALSE);
275         
276         out= nodeAddNodeType(ma->nodetree, SH_NODE_OUTPUT, NULL, NULL);
277         out->locx= 300.0f; out->locy= 300.0f;
278         
279         in= nodeAddNodeType(ma->nodetree, SH_NODE_MATERIAL, NULL, NULL);
280         in->locx= 10.0f; in->locy= 300.0f;
281         nodeSetActive(ma->nodetree, in);
282         
283         /* only a link from color to color */
284         fromsock= in->outputs.first;
285         tosock= out->inputs.first;
286         nodeAddLink(ma->nodetree, in, fromsock, out, tosock);
287         
288         ntreeSolveOrder(ma->nodetree);  /* needed for pointers */
289 }
290
291 /* assumes nothing being done in ntree yet, sets the default in/out node */
292 /* called from shading buttons or header */
293 void ED_node_composit_default(Scene *sce)
294 {
295         bNode *in, *out;
296         bNodeSocket *fromsock, *tosock;
297         
298         /* but lets check it anyway */
299         if(sce->nodetree) {
300                 if (G.f & G_DEBUG)
301                         printf("error in composite initialize\n");
302                 return;
303         }
304         
305         sce->nodetree= ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, FALSE);
306         
307         out= nodeAddNodeType(sce->nodetree, CMP_NODE_COMPOSITE, NULL, NULL);
308         out->locx= 300.0f; out->locy= 400.0f;
309         out->id= &sce->id;
310         id_us_plus(out->id);
311         
312         in= nodeAddNodeType(sce->nodetree, CMP_NODE_R_LAYERS, NULL, NULL);
313         in->locx= 10.0f; in->locy= 400.0f;
314         in->id= &sce->id;
315         id_us_plus(in->id);
316         nodeSetActive(sce->nodetree, in);
317         
318         /* links from color to color */
319         fromsock= in->outputs.first;
320         tosock= out->inputs.first;
321         nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
322         
323         ntreeSolveOrder(sce->nodetree); /* needed for pointers */
324         
325         // XXX ntreeCompositForceHidden(sce->nodetree);
326 }
327
328 /* assumes nothing being done in ntree yet, sets the default in/out node */
329 /* called from shading buttons or header */
330 void ED_node_texture_default(Tex *tx)
331 {
332         bNode *in, *out;
333         bNodeSocket *fromsock, *tosock;
334         
335         /* but lets check it anyway */
336         if(tx->nodetree) {
337                 if (G.f & G_DEBUG)
338                         printf("error in texture initialize\n");
339                 return;
340         }
341         
342         tx->nodetree= ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, FALSE);
343         
344         out= nodeAddNodeType(tx->nodetree, TEX_NODE_OUTPUT, NULL, NULL);
345         out->locx= 300.0f; out->locy= 300.0f;
346         
347         in= nodeAddNodeType(tx->nodetree, TEX_NODE_CHECKER, NULL, NULL);
348         in->locx= 10.0f; in->locy= 300.0f;
349         nodeSetActive(tx->nodetree, in);
350         
351         fromsock= in->outputs.first;
352         tosock= out->inputs.first;
353         nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
354         
355         ntreeSolveOrder(tx->nodetree);  /* needed for pointers */
356 }
357
358 /* id is supposed to contain a node tree */
359 void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
360 {
361         bNode *node= NULL;
362         short idtype= GS(id->name);
363
364         if(idtype == ID_MA) {
365                 *ntree= ((Material*)id)->nodetree;
366                 if(treetype) *treetype= NTREE_SHADER;
367         }
368         else if(idtype == ID_SCE) {
369                 *ntree= ((Scene*)id)->nodetree;
370                 if(treetype) *treetype= NTREE_COMPOSIT;
371         }
372         else if(idtype == ID_TE) {
373                 *ntree= ((Tex*)id)->nodetree;
374                 if(treetype) *treetype= NTREE_TEXTURE;
375         }
376         else {
377                 if(treetype) *treetype= 0;
378                 return;
379         }
380
381         /* find editable group */
382         if(edittree) {
383                 if(*ntree)
384                         for(node= (*ntree)->nodes.first; node; node= node->next)
385                                 if(node->flag & NODE_GROUP_EDIT)
386                                         break;
387                 
388                 if(node && node->id)
389                         *edittree= (bNodeTree *)node->id;
390                 else
391                         *edittree= *ntree;
392         }
393 }
394
395 /* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
396 void snode_set_context(SpaceNode *snode, Scene *scene)
397 {
398         Object *ob= OBACT;
399         
400         snode->nodetree= NULL;
401         snode->edittree= NULL;
402         snode->id= snode->from= NULL;
403         
404         if(snode->treetype==NTREE_SHADER) {
405                 /* need active object, or we allow pinning... */
406                 if(ob) {
407                         Material *ma= give_current_material(ob, ob->actcol);
408                         if(ma) {
409                                 snode->from= &ob->id;
410                                 snode->id= &ma->id;
411                         }
412                 }
413         }
414         else if(snode->treetype==NTREE_COMPOSIT) {
415                 snode->from= NULL;
416                 snode->id= &scene->id;
417                 
418                 /* bit clumsy but reliable way to see if we draw first time */
419                 if(snode->nodetree==NULL)
420                         ntreeCompositForceHidden(scene->nodetree, scene);
421         }
422         else if(snode->treetype==NTREE_TEXTURE) {
423                 Tex *tx= NULL;
424
425                 if(snode->texfrom==SNODE_TEX_OBJECT) {
426                         if(ob) {
427                                 tx= give_current_object_texture(ob);
428
429                                 if(ob->type == OB_LAMP)
430                                         snode->from= (ID*)ob->data;
431                                 else
432                                         snode->from= (ID*)give_current_material(ob, ob->actcol);
433
434                                 /* from is not set fully for material nodes, should be ID + Node then */
435                                 snode->id= &tx->id;
436                         }
437                 }
438                 else if(snode->texfrom==SNODE_TEX_WORLD) {
439                         tx= give_current_world_texture(scene->world);
440                         snode->from= (ID *)scene->world;
441                         snode->id= &tx->id;
442                 }
443                 else {
444                         struct Brush *brush= NULL;
445                         
446                         if(ob && (ob->mode & OB_MODE_SCULPT))
447                                 brush= paint_brush(&scene->toolsettings->sculpt->paint);
448                         else
449                                 brush= paint_brush(&scene->toolsettings->imapaint.paint);
450
451                         if (brush) {
452                                 snode->from= (ID *)brush;
453                                 tx= give_current_brush_texture(brush);
454                                 snode->id= &tx->id;
455                         }
456                 }
457         }
458
459         if(snode->id)
460                 node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
461 }
462
463 static void snode_tag_changed(SpaceNode *snode, bNode *node)
464 {
465         bNode *gnode;
466         
467         if (node)
468                 NodeTagChanged(snode->edittree, node);
469         
470         /* if inside group, tag entire group */
471         gnode= node_tree_get_editgroup(snode->nodetree);
472         if(gnode)
473                 NodeTagIDChanged(snode->nodetree, gnode->id);
474 }
475
476 void node_set_active(SpaceNode *snode, bNode *node)
477 {
478         nodeSetActive(snode->edittree, node);
479         
480         if(node->type!=NODE_GROUP) {
481                 int was_output= (node->flag & NODE_DO_OUTPUT);
482                 
483                 /* tree specific activate calls */
484                 if(snode->treetype==NTREE_SHADER) {
485                         /* when we select a material, active texture is cleared, for buttons */
486                         if(node->id && GS(node->id->name)==ID_MA)
487                                 nodeClearActiveID(snode->edittree, ID_TE);
488                         
489                         if(node->type==SH_NODE_OUTPUT) {
490                                 bNode *tnode;
491                                 
492                                 for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
493                                         if( tnode->type==SH_NODE_OUTPUT)
494                                                 tnode->flag &= ~NODE_DO_OUTPUT;
495                                 
496                                 node->flag |= NODE_DO_OUTPUT;
497                                 if(was_output==0)
498                                         ED_node_changed_update(snode->id, node);
499                         }
500
501                         // XXX
502 #if 0
503                         if(node->id)
504                                 ; // XXX BIF_preview_changed(-1);       /* temp hack to force texture preview to update */
505                         
506                         // allqueue(REDRAWBUTSSHADING, 1);
507                         // allqueue(REDRAWIPO, 0);
508 #endif
509                 }
510                 else if(snode->treetype==NTREE_COMPOSIT) {
511                         Scene *scene= (Scene*)snode->id;
512
513                         /* make active viewer, currently only 1 supported... */
514                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
515                                 bNode *tnode;
516                                 
517
518                                 for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
519                                         if( ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
520                                                 tnode->flag &= ~NODE_DO_OUTPUT;
521                                 
522                                 node->flag |= NODE_DO_OUTPUT;
523                                 if(was_output==0) {
524                                         snode_tag_changed(snode, node);
525                                         
526                                         ED_node_changed_update(snode->id, node);
527                                 }
528                                 
529                                 /* addnode() doesnt link this yet... */
530                                 node->id= (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
531                         }
532                         else if(node->type==CMP_NODE_R_LAYERS) {
533                                 if(node->id==NULL || node->id==(ID *)scene) {
534                                         scene->r.actlay= node->custom1;
535                                 }
536                         }
537                         else if(node->type==CMP_NODE_COMPOSITE) {
538                                 bNode *tnode;
539                                 
540                                 for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
541                                         if( tnode->type==CMP_NODE_COMPOSITE)
542                                                 tnode->flag &= ~NODE_DO_OUTPUT;
543                                 
544                                 node->flag |= NODE_DO_OUTPUT;
545                                 ED_node_changed_update(snode->id, node);
546                         }
547                 }
548                 else if(snode->treetype==NTREE_TEXTURE) {
549                         // XXX
550 #if 0
551                         if(node->id)
552                                 ; // XXX BIF_preview_changed(-1);
553                         // allqueue(REDRAWBUTSSHADING, 1);
554                         // allqueue(REDRAWIPO, 0);
555 #endif
556                 }
557         }
558 }
559
560 /* when links in groups change, inputs/outputs change, nodes added/deleted... */
561 void node_tree_verify_groups(bNodeTree *nodetree)
562 {
563         bNode *gnode;
564         
565         gnode= node_tree_get_editgroup(nodetree);
566         
567         /* does all materials */
568         if(gnode)
569                 nodeGroupVerify((bNodeTree *)gnode->id);
570         
571 }
572
573 /* ***************** Edit Group operator ************* */
574
575 void snode_make_group_editable(SpaceNode *snode, bNode *gnode)
576 {
577         bNode *node;
578         
579         /* make sure nothing has group editing on */
580         for(node= snode->nodetree->nodes.first; node; node= node->next)
581                 node->flag &= ~NODE_GROUP_EDIT;
582         
583         if(gnode==NULL) {
584                 /* with NULL argument we do a toggle */
585                 if(snode->edittree==snode->nodetree)
586                         gnode= nodeGetActive(snode->nodetree);
587         }
588         
589         if(gnode && gnode->type==NODE_GROUP && gnode->id) {
590                 if(gnode->id->lib)
591                         ntreeMakeLocal((bNodeTree *)gnode->id);
592
593                 gnode->flag |= NODE_GROUP_EDIT;
594                 snode->edittree= (bNodeTree *)gnode->id;
595                 
596                 /* deselect all other nodes, so we can also do grabbing of entire subtree */
597                 for(node= snode->nodetree->nodes.first; node; node= node->next)
598                         node->flag &= ~SELECT;
599                 gnode->flag |= SELECT;
600                 
601         }
602         else 
603                 snode->edittree= snode->nodetree;
604         
605         ntreeSolveOrder(snode->nodetree);
606 }
607
608 static int node_group_edit_exec(bContext *C, wmOperator *UNUSED(op))
609 {
610         SpaceNode *snode = CTX_wm_space_node(C);
611         bNode *gnode;
612
613         ED_preview_kill_jobs(C);
614
615         gnode= nodeGetActive(snode->edittree);
616         snode_make_group_editable(snode, gnode);
617
618         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
619
620         return OPERATOR_FINISHED;
621 }
622
623 static int node_group_edit_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
624 {
625         SpaceNode *snode = CTX_wm_space_node(C);
626         bNode *gnode;
627
628         gnode= nodeGetActive(snode->edittree);
629         if(gnode && gnode->type==NODE_GROUP && gnode->id && gnode->id->lib) {
630                 uiPupMenuOkee(C, op->type->idname, "Make group local?");
631                 return OPERATOR_CANCELLED;
632         }
633
634         return node_group_edit_exec(C, op);
635 }
636
637 void NODE_OT_group_edit(wmOperatorType *ot)
638 {
639         /* identifiers */
640         ot->name = "Edit Group";
641         ot->description = "Edit node group";
642         ot->idname = "NODE_OT_group_edit";
643         
644         /* api callbacks */
645         ot->invoke = node_group_edit_invoke;
646         ot->exec = node_group_edit_exec;
647         ot->poll = ED_operator_node_active;
648         
649         /* flags */
650         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
651 }
652
653 /* ***************** Add Group Socket operator ************* */
654
655 static int node_group_socket_add_exec(bContext *C, wmOperator *op)
656 {
657         SpaceNode *snode = CTX_wm_space_node(C);
658         int in_out= -1;
659         char name[32]= "";
660         int type= SOCK_VALUE;
661         bNodeTree *ngroup= snode->edittree;
662         bNodeSocket *sock;
663         
664         ED_preview_kill_jobs(C);
665         
666         if (RNA_property_is_set(op->ptr, "name"))
667                 RNA_string_get(op->ptr, "name", name);
668         
669         if (RNA_property_is_set(op->ptr, "type"))
670                 type = RNA_enum_get(op->ptr, "type");
671         
672         if (RNA_property_is_set(op->ptr, "in_out"))
673                 in_out = RNA_enum_get(op->ptr, "in_out");
674         else
675                 return OPERATOR_CANCELLED;
676         
677         sock = nodeGroupAddSocket(ngroup, name, type, in_out);
678         
679         node_tree_verify_groups(snode->nodetree);
680         
681         snode_notify(C, snode);
682         
683         return OPERATOR_FINISHED;
684 }
685
686 void NODE_OT_group_socket_add(wmOperatorType *ot)
687 {
688         /* identifiers */
689         ot->name = "Add Group Socket";
690         ot->description = "Add node group socket";
691         ot->idname = "NODE_OT_group_socket_add";
692         
693         /* api callbacks */
694         ot->exec = node_group_socket_add_exec;
695         ot->poll = ED_operator_node_active;
696         
697         /* flags */
698         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
699         
700         RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
701         RNA_def_string(ot->srna, "name", "", 32, "Name", "Group socket name");
702         RNA_def_enum(ot->srna, "type", node_socket_type_items, SOCK_VALUE, "Type", "Type of the group socket");
703 }
704
705 /* ***************** Remove Group Socket operator ************* */
706
707 static int node_group_socket_remove_exec(bContext *C, wmOperator *op)
708 {
709         SpaceNode *snode = CTX_wm_space_node(C);
710         int index= -1;
711         int in_out= -1;
712         bNodeTree *ngroup= snode->edittree;
713         bNodeSocket *sock;
714         
715         ED_preview_kill_jobs(C);
716         
717         if (RNA_property_is_set(op->ptr, "index"))
718                 index = RNA_int_get(op->ptr, "index");
719         else
720                 return OPERATOR_CANCELLED;
721         
722         if (RNA_property_is_set(op->ptr, "in_out"))
723                 in_out = RNA_enum_get(op->ptr, "in_out");
724         else
725                 return OPERATOR_CANCELLED;
726         
727         sock = (bNodeSocket*)BLI_findlink(in_out==SOCK_IN ? &ngroup->inputs : &ngroup->outputs, index);
728         if (sock) {
729                 nodeGroupRemoveSocket(ngroup, sock, in_out);
730                 node_tree_verify_groups(snode->nodetree);
731                 
732                 snode_notify(C, snode);
733         }
734         
735         return OPERATOR_FINISHED;
736 }
737
738 void NODE_OT_group_socket_remove(wmOperatorType *ot)
739 {
740         /* identifiers */
741         ot->name = "Remove Group Socket";
742         ot->description = "Removed node group socket";
743         ot->idname = "NODE_OT_group_socket_remove";
744         
745         /* api callbacks */
746         ot->exec = node_group_socket_remove_exec;
747         ot->poll = ED_operator_node_active;
748         
749         /* flags */
750         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
751         
752         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
753         RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
754 }
755
756 /* ***************** Move Group Socket Up operator ************* */
757
758 static int node_group_socket_move_up_exec(bContext *C, wmOperator *op)
759 {
760         SpaceNode *snode = CTX_wm_space_node(C);
761         int index= -1;
762         int in_out= -1;
763         bNodeTree *ngroup= snode->edittree;
764         bNodeSocket *sock, *prev;
765         
766         ED_preview_kill_jobs(C);
767         
768         if (RNA_property_is_set(op->ptr, "index"))
769                 index = RNA_int_get(op->ptr, "index");
770         else
771                 return OPERATOR_CANCELLED;
772         
773         if (RNA_property_is_set(op->ptr, "in_out"))
774                 in_out = RNA_enum_get(op->ptr, "in_out");
775         else
776                 return OPERATOR_CANCELLED;
777         
778         /* swap */
779         if (in_out==SOCK_IN) {
780                 sock = (bNodeSocket*)BLI_findlink(&ngroup->inputs, index);
781                 prev = sock->prev;
782                 /* can't move up the first socket */
783                 if (!prev)
784                         return OPERATOR_CANCELLED;
785                 BLI_remlink(&ngroup->inputs, sock);
786                 BLI_insertlinkbefore(&ngroup->inputs, prev, sock);
787         }
788         else if (in_out==SOCK_OUT) {
789                 sock = (bNodeSocket*)BLI_findlink(&ngroup->outputs, index);
790                 prev = sock->prev;
791                 /* can't move up the first socket */
792                 if (!prev)
793                         return OPERATOR_CANCELLED;
794                 BLI_remlink(&ngroup->outputs, sock);
795                 BLI_insertlinkbefore(&ngroup->outputs, prev, sock);
796         }
797         node_tree_verify_groups(snode->nodetree);
798         
799         snode_notify(C, snode);
800         
801         return OPERATOR_FINISHED;
802 }
803
804 void NODE_OT_group_socket_move_up(wmOperatorType *ot)
805 {
806         /* identifiers */
807         ot->name = "Move Group Socket Up";
808         ot->description = "Move up node group socket";
809         ot->idname = "NODE_OT_group_socket_move_up";
810         
811         /* api callbacks */
812         ot->exec = node_group_socket_move_up_exec;
813         ot->poll = ED_operator_node_active;
814         
815         /* flags */
816         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
817         
818         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
819         RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
820 }
821
822 /* ***************** Move Group Socket Up operator ************* */
823
824 static int node_group_socket_move_down_exec(bContext *C, wmOperator *op)
825 {
826         SpaceNode *snode = CTX_wm_space_node(C);
827         int index= -1;
828         int in_out= -1;
829         bNodeTree *ngroup= snode->edittree;
830         bNodeSocket *sock, *next;
831         
832         ED_preview_kill_jobs(C);
833         
834         if (RNA_property_is_set(op->ptr, "index"))
835                 index = RNA_int_get(op->ptr, "index");
836         else
837                 return OPERATOR_CANCELLED;
838         
839         if (RNA_property_is_set(op->ptr, "in_out"))
840                 in_out = RNA_enum_get(op->ptr, "in_out");
841         else
842                 return OPERATOR_CANCELLED;
843         
844         /* swap */
845         if (in_out==SOCK_IN) {
846                 sock = (bNodeSocket*)BLI_findlink(&ngroup->inputs, index);
847                 next = sock->next;
848                 /* can't move down the last socket */
849                 if (!next)
850                         return OPERATOR_CANCELLED;
851                 BLI_remlink(&ngroup->inputs, sock);
852                 BLI_insertlinkafter(&ngroup->inputs, next, sock);
853         }
854         else if (in_out==SOCK_OUT) {
855                 sock = (bNodeSocket*)BLI_findlink(&ngroup->outputs, index);
856                 next = sock->next;
857                 /* can't move down the last socket */
858                 if (!next)
859                         return OPERATOR_CANCELLED;
860                 BLI_remlink(&ngroup->outputs, sock);
861                 BLI_insertlinkafter(&ngroup->outputs, next, sock);
862         }
863         node_tree_verify_groups(snode->nodetree);
864         
865         snode_notify(C, snode);
866         
867         return OPERATOR_FINISHED;
868 }
869
870 void NODE_OT_group_socket_move_down(wmOperatorType *ot)
871 {
872         /* identifiers */
873         ot->name = "Move Group Socket Down";
874         ot->description = "Move down node group socket";
875         ot->idname = "NODE_OT_group_socket_move_down";
876         
877         /* api callbacks */
878         ot->exec = node_group_socket_move_down_exec;
879         ot->poll = ED_operator_node_active;
880         
881         /* flags */
882         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
883         
884         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, INT_MAX);
885         RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Type", "Input or Output");
886 }
887
888 /* ******************** Ungroup operator ********************** */
889
890 static int node_group_ungroup_exec(bContext *C, wmOperator *op)
891 {
892         SpaceNode *snode = CTX_wm_space_node(C);
893         bNode *gnode;
894
895         ED_preview_kill_jobs(C);
896
897         /* are we inside of a group? */
898         gnode= node_tree_get_editgroup(snode->nodetree);
899         if(gnode)
900                 snode_make_group_editable(snode, NULL);
901         
902         gnode= nodeGetActive(snode->edittree);
903         if(gnode==NULL)
904                 return OPERATOR_CANCELLED;
905         
906         if(gnode->type!=NODE_GROUP) {
907                 BKE_report(op->reports, RPT_WARNING, "Not a group");
908                 return OPERATOR_CANCELLED;
909         }
910         else if(!nodeGroupUnGroup(snode->edittree, gnode)) {
911                 BKE_report(op->reports, RPT_WARNING, "Can't ungroup");
912                 return OPERATOR_CANCELLED;
913         }
914
915         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
916
917         return OPERATOR_FINISHED;
918 }
919
920 void NODE_OT_group_ungroup(wmOperatorType *ot)
921 {
922         /* identifiers */
923         ot->name = "Ungroup";
924         ot->description = "Ungroup selected nodes";
925         ot->idname = "NODE_OT_group_ungroup";
926         
927         /* api callbacks */
928         ot->exec = node_group_ungroup_exec;
929         ot->poll = ED_operator_node_active;
930         
931         /* flags */
932         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
933 }
934
935 /* ************************** Node generic ************** */
936
937 /* allows to walk the list in order of visibility */
938 bNode *next_node(bNodeTree *ntree)
939 {
940         static bNode *current=NULL, *last= NULL;
941         
942         if(ntree) {
943                 /* set current to the first selected node */
944                 for(current= ntree->nodes.last; current; current= current->prev)
945                         if(current->flag & NODE_SELECT)
946                                 break;
947                 
948                 /* set last to the first unselected node */
949                 for(last= ntree->nodes.last; last; last= last->prev)
950                         if((last->flag & NODE_SELECT)==0)
951                                 break;
952                 
953                 if(current==NULL)
954                         current= last;
955                 
956                 return NULL;
957         }
958         /* no nodes, or we are ready */
959         if(current==NULL)
960                 return NULL;
961         
962         /* now we walk the list backwards, but we always return current */
963         if(current->flag & NODE_SELECT) {
964                 bNode *node= current;
965                 
966                 /* find previous selected */
967                 current= current->prev;
968                 while(current && (current->flag & NODE_SELECT)==0)
969                         current= current->prev;
970                 
971                 /* find first unselected */
972                 if(current==NULL)
973                         current= last;
974                 
975                 return node;
976         }
977         else {
978                 bNode *node= current;
979                 
980                 /* find previous unselected */
981                 current= current->prev;
982                 while(current && (current->flag & NODE_SELECT))
983                         current= current->prev;
984                 
985                 return node;
986         }
987         
988         return NULL;
989 }
990
991 /* is rct in visible part of node? */
992 static bNode *visible_node(SpaceNode *snode, rctf *rct)
993 {
994         bNode *tnode;
995         
996         for(next_node(snode->edittree); (tnode=next_node(NULL));) {
997                 if(BLI_isect_rctf(&tnode->totr, rct, NULL))
998                         break;
999         }
1000         return tnode;
1001 }
1002
1003 /* **************************** */
1004
1005 typedef struct NodeViewMove {
1006         short mvalo[2];
1007         int xmin, ymin, xmax, ymax;
1008 } NodeViewMove;
1009
1010 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1011 {
1012         SpaceNode *snode= CTX_wm_space_node(C);
1013         ARegion *ar= CTX_wm_region(C);
1014         NodeViewMove *nvm= op->customdata;
1015
1016         switch (event->type) {
1017                 case MOUSEMOVE:
1018                         
1019                         snode->xof -= (nvm->mvalo[0]-event->mval[0]);
1020                         snode->yof -= (nvm->mvalo[1]-event->mval[1]);
1021                         nvm->mvalo[0]= event->mval[0];
1022                         nvm->mvalo[1]= event->mval[1];
1023                         
1024                         /* prevent dragging image outside of the window and losing it! */
1025                         CLAMP(snode->xof, nvm->xmin, nvm->xmax);
1026                         CLAMP(snode->yof, nvm->ymin, nvm->ymax);
1027                         
1028                         ED_region_tag_redraw(ar);
1029                         
1030                         break;
1031                         
1032                 case LEFTMOUSE:
1033                 case MIDDLEMOUSE:
1034                 case RIGHTMOUSE:
1035                         
1036                         MEM_freeN(nvm);
1037                         op->customdata= NULL;
1038
1039                         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_NODE, NULL);
1040                         
1041                         return OPERATOR_FINISHED;
1042         }
1043         
1044         return OPERATOR_RUNNING_MODAL;
1045 }
1046
1047 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1048 {
1049         ARegion *ar= CTX_wm_region(C);
1050         NodeViewMove *nvm;
1051         Image *ima;
1052         ImBuf *ibuf;
1053         int pad= 10;
1054         void *lock;
1055         
1056         ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
1057         ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
1058         
1059         if(ibuf == NULL) {
1060                 BKE_image_release_ibuf(ima, lock);
1061                 return OPERATOR_CANCELLED;
1062         }
1063
1064         nvm= MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
1065         op->customdata= nvm;
1066         nvm->mvalo[0]= event->mval[0];
1067         nvm->mvalo[1]= event->mval[1];
1068
1069         nvm->xmin = -(ar->winx/2) - ibuf->x/2 + pad;
1070         nvm->xmax = ar->winx/2 + ibuf->x/2 - pad;
1071         nvm->ymin = -(ar->winy/2) - ibuf->y/2 + pad;
1072         nvm->ymax = ar->winy/2 + ibuf->y/2 - pad;
1073
1074         BKE_image_release_ibuf(ima, lock);
1075         
1076         /* add modal handler */
1077         WM_event_add_modal_handler(C, op);
1078         
1079         return OPERATOR_RUNNING_MODAL;
1080 }
1081
1082
1083 void NODE_OT_backimage_move(wmOperatorType *ot)
1084 {
1085         /* identifiers */
1086         ot->name= "Background Image Move";
1087         ot->description = "Move Node backdrop";
1088         ot->idname= "NODE_OT_backimage_move";
1089         
1090         /* api callbacks */
1091         ot->invoke= snode_bg_viewmove_invoke;
1092         ot->modal= snode_bg_viewmove_modal;
1093         ot->poll= composite_node_active;
1094         
1095         /* flags */
1096         ot->flag= OPTYPE_BLOCKING;
1097 }
1098
1099 static int backimage_zoom(bContext *C, wmOperator *op)
1100 {
1101         SpaceNode *snode= CTX_wm_space_node(C);
1102         ARegion *ar= CTX_wm_region(C);
1103         float fac= RNA_float_get(op->ptr, "factor");
1104
1105         snode->zoom *= fac;
1106         ED_region_tag_redraw(ar);
1107
1108         return OPERATOR_FINISHED;
1109 }
1110
1111
1112 void NODE_OT_backimage_zoom(wmOperatorType *ot)
1113 {
1114         
1115         /* identifiers */
1116         ot->name= "Background Image Zoom";
1117         ot->idname= "NODE_OT_backimage_zoom";
1118         
1119         /* api callbacks */
1120         ot->exec= backimage_zoom;
1121         ot->poll= composite_node_active;
1122         
1123         /* flags */
1124         ot->flag= OPTYPE_BLOCKING;
1125
1126         /* internal */
1127         RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
1128 }
1129
1130 /******************** sample backdrop operator ********************/
1131
1132 typedef struct ImageSampleInfo {
1133         ARegionType *art;
1134         void *draw_handle;
1135         int x, y;
1136         int channels;
1137         int color_manage;
1138
1139         char col[4];
1140         float colf[4];
1141
1142         int draw;
1143 } ImageSampleInfo;
1144
1145 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
1146 {
1147         ImageSampleInfo *info= arg_info;
1148
1149         draw_nodespace_color_info(ar, (CTX_data_scene(C)->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
1150                                                           info->x, info->y, info->col, info->colf);
1151 }
1152
1153 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
1154 {
1155         SpaceNode *snode= CTX_wm_space_node(C);
1156         ARegion *ar= CTX_wm_region(C);
1157         ImageSampleInfo *info= op->customdata;
1158         void *lock;
1159         Image *ima;
1160         ImBuf *ibuf;
1161         float fx, fy, bufx, bufy;
1162         int mx, my;
1163         
1164         ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
1165         ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
1166         if(!ibuf)
1167                 return;
1168         
1169         if(!ibuf->rect) {
1170                 if(info->color_manage)
1171                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
1172                 else
1173                         ibuf->profile = IB_PROFILE_NONE;
1174                 IMB_rect_from_float(ibuf);
1175         }
1176
1177         mx= event->x - ar->winrct.xmin;
1178         my= event->y - ar->winrct.ymin;
1179         /* map the mouse coords to the backdrop image space */
1180         bufx = ibuf->x * snode->zoom;
1181         bufy = ibuf->y * snode->zoom;
1182         fx = (bufx > 0.0f ? ((float)mx - 0.5f*ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
1183         fy = (bufy > 0.0f ? ((float)my - 0.5f*ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
1184
1185         if(fx>=0.0f && fy>=0.0f && fx<1.0f && fy<1.0f) {
1186                 float *fp;
1187                 char *cp;
1188                 int x= (int)(fx*ibuf->x), y= (int)(fy*ibuf->y);
1189
1190                 CLAMP(x, 0, ibuf->x-1);
1191                 CLAMP(y, 0, ibuf->y-1);
1192
1193                 info->x= x;
1194                 info->y= y;
1195                 info->draw= 1;
1196                 info->channels= ibuf->channels;
1197
1198                 if(ibuf->rect) {
1199                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1200
1201                         info->col[0]= cp[0];
1202                         info->col[1]= cp[1];
1203                         info->col[2]= cp[2];
1204                         info->col[3]= cp[3];
1205
1206                         info->colf[0]= (float)cp[0]/255.0f;
1207                         info->colf[1]= (float)cp[1]/255.0f;
1208                         info->colf[2]= (float)cp[2]/255.0f;
1209                         info->colf[3]= (float)cp[3]/255.0f;
1210                 }
1211                 if(ibuf->rect_float) {
1212                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1213
1214                         info->colf[0]= fp[0];
1215                         info->colf[1]= fp[1];
1216                         info->colf[2]= fp[2];
1217                         info->colf[3]= fp[3];
1218                 }
1219         }
1220         else
1221                 info->draw= 0;
1222
1223         BKE_image_release_ibuf(ima, lock);
1224         
1225         ED_area_tag_redraw(CTX_wm_area(C));
1226 }
1227
1228 static void sample_exit(bContext *C, wmOperator *op)
1229 {
1230         ImageSampleInfo *info= op->customdata;
1231
1232         ED_region_draw_cb_exit(info->art, info->draw_handle);
1233         ED_area_tag_redraw(CTX_wm_area(C));
1234         MEM_freeN(info);
1235 }
1236
1237 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1238 {
1239         SpaceNode *snode= CTX_wm_space_node(C);
1240         ARegion *ar= CTX_wm_region(C);
1241         ImageSampleInfo *info;
1242
1243         if(snode->treetype!=NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
1244                 return OPERATOR_CANCELLED;
1245         
1246         info= MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1247         info->art= ar->type;
1248         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1249         op->customdata= info;
1250
1251         sample_apply(C, op, event);
1252
1253         WM_event_add_modal_handler(C, op);
1254
1255         return OPERATOR_RUNNING_MODAL;
1256 }
1257
1258 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1259 {
1260         switch(event->type) {
1261                 case LEFTMOUSE:
1262                 case RIGHTMOUSE: // XXX hardcoded
1263                         sample_exit(C, op);
1264                         return OPERATOR_CANCELLED;
1265                 case MOUSEMOVE:
1266                         sample_apply(C, op, event);
1267                         break;
1268         }
1269
1270         return OPERATOR_RUNNING_MODAL;
1271 }
1272
1273 static int sample_cancel(bContext *C, wmOperator *op)
1274 {
1275         sample_exit(C, op);
1276         return OPERATOR_CANCELLED;
1277 }
1278
1279 void NODE_OT_backimage_sample(wmOperatorType *ot)
1280 {
1281         /* identifiers */
1282         ot->name= "Backimage Sample";
1283         ot->idname= "NODE_OT_backimage_sample";
1284         
1285         /* api callbacks */
1286         ot->invoke= sample_invoke;
1287         ot->modal= sample_modal;
1288         ot->cancel= sample_cancel;
1289         ot->poll= ED_operator_node_active;
1290
1291         /* flags */
1292         ot->flag= OPTYPE_BLOCKING;
1293 }
1294
1295 /* ********************** size widget operator ******************** */
1296
1297 typedef struct NodeSizeWidget {
1298         float mxstart;
1299         float oldwidth;
1300 } NodeSizeWidget;
1301
1302 static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
1303 {
1304         SpaceNode *snode= CTX_wm_space_node(C);
1305         ARegion *ar= CTX_wm_region(C);
1306         bNode *node= editnode_get_active(snode->edittree);
1307         NodeSizeWidget *nsw= op->customdata;
1308         float mx, my;
1309         
1310         switch (event->type) {
1311                 case MOUSEMOVE:
1312                         
1313                         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1314                                                                          &mx, &my);
1315                         
1316                         if (node) {
1317                                 if(node->flag & NODE_HIDDEN) {
1318                                         node->miniwidth= nsw->oldwidth + mx - nsw->mxstart;
1319                                         CLAMP(node->miniwidth, 0.0f, 100.0f);
1320                                 }
1321                                 else {
1322                                         node->width= nsw->oldwidth + mx - nsw->mxstart;
1323                                         CLAMP(node->width, node->typeinfo->minwidth, node->typeinfo->maxwidth);
1324                                 }
1325                         }
1326                                 
1327                         ED_region_tag_redraw(ar);
1328
1329                         break;
1330                         
1331                 case LEFTMOUSE:
1332                 case MIDDLEMOUSE:
1333                 case RIGHTMOUSE:
1334                         
1335                         MEM_freeN(nsw);
1336                         op->customdata= NULL;
1337                         
1338                         return OPERATOR_FINISHED;
1339         }
1340         
1341         return OPERATOR_RUNNING_MODAL;
1342 }
1343
1344 static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
1345 {
1346         SpaceNode *snode= CTX_wm_space_node(C);
1347         ARegion *ar= CTX_wm_region(C);
1348         bNode *node= editnode_get_active(snode->edittree);
1349         
1350         if(node) {
1351                 rctf totr;
1352                 
1353                 /* convert mouse coordinates to v2d space */
1354                 UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1355                                                                  &snode->mx, &snode->my);
1356                 
1357                 /* rect we're interested in is just the bottom right corner */
1358                 totr= node->totr;
1359                 totr.xmin= totr.xmax-10.0f;
1360                 totr.ymax= totr.ymin+10.0f;
1361                 
1362                 if(BLI_in_rctf(&totr, snode->mx, snode->my)) {
1363                         NodeSizeWidget *nsw= MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
1364                         
1365                         op->customdata= nsw;
1366                         nsw->mxstart= snode->mx;
1367                         
1368                         /* store old */
1369                         if(node->flag & NODE_HIDDEN)
1370                                 nsw->oldwidth= node->miniwidth;
1371                         else
1372                                 nsw->oldwidth= node->width;
1373                         
1374                         /* add modal handler */
1375                         WM_event_add_modal_handler(C, op);
1376
1377                         return OPERATOR_RUNNING_MODAL;
1378                 }
1379         }
1380         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1381 }
1382
1383 void NODE_OT_resize(wmOperatorType *ot)
1384 {
1385         /* identifiers */
1386         ot->name= "Resize Node";
1387         ot->idname= "NODE_OT_resize";
1388         
1389         /* api callbacks */
1390         ot->invoke= node_resize_invoke;
1391         ot->modal= node_resize_modal;
1392         ot->poll= ED_operator_node_active;
1393         
1394         /* flags */
1395         ot->flag= OPTYPE_BLOCKING;
1396 }
1397
1398 /* ********************** select ******************** */
1399
1400
1401 /* no undo here! */
1402 void node_deselectall(SpaceNode *snode)
1403 {
1404         bNode *node;
1405         
1406         for(node= snode->edittree->nodes.first; node; node= node->next)
1407                 node->flag &= ~SELECT;
1408 }
1409
1410 /* return 1 if we need redraw otherwise zero. */
1411 int node_select_same_type(SpaceNode *snode)
1412 {
1413         bNode *nac, *p;
1414         int redraw;
1415
1416         /* search for the active node. */
1417         for (nac= snode->edittree->nodes.first; nac; nac= nac->next) {
1418                 if (nac->flag & SELECT)
1419                         break;
1420         }
1421
1422         /* no active node, return. */
1423         if (!nac)
1424                 return(0);
1425
1426         redraw= 0;
1427         for (p= snode->edittree->nodes.first; p; p= p->next) {
1428                 if (p->type != nac->type && p->flag & SELECT) {
1429                         /* if it's selected but different type, unselect */
1430                         redraw= 1;
1431                         p->flag &= ~SELECT;
1432                 }
1433                 else if (p->type == nac->type && (!(p->flag & SELECT))) {
1434                         /* if it's the same type and is not selected, select! */
1435                         redraw= 1;
1436                         p->flag |= SELECT;
1437                 }
1438         }
1439         return(redraw);
1440 }
1441
1442 /* return 1 if we need redraw, otherwise zero.
1443  * dir can be 0 == next or 0 != prev.
1444  */
1445 int node_select_same_type_np(SpaceNode *snode, int dir)
1446 {
1447         bNode *nac, *p;
1448
1449         /* search the active one. */
1450         for (nac= snode->edittree->nodes.first; nac; nac= nac->next) {
1451                 if (nac->flag & SELECT)
1452                         break;
1453         }
1454
1455         /* no active node, return. */
1456         if (!nac)
1457                 return(0);
1458
1459         if (dir == 0)
1460                 p= nac->next;
1461         else
1462                 p= nac->prev;
1463
1464         while (p) {
1465                 /* Now search the next with the same type. */
1466                 if (p->type == nac->type)
1467                         break;
1468
1469                 if (dir == 0)
1470                         p= p->next;
1471                 else
1472                         p= p->prev;
1473         }
1474
1475         if (p) {
1476                 node_deselectall(snode);
1477                 p->flag |= SELECT;
1478                 return(1);
1479         }
1480         return(0);
1481 }
1482
1483 int node_has_hidden_sockets(bNode *node)
1484 {
1485         bNodeSocket *sock;
1486         
1487         for(sock= node->inputs.first; sock; sock= sock->next)
1488                 if(sock->flag & SOCK_HIDDEN)
1489                         return 1;
1490         for(sock= node->outputs.first; sock; sock= sock->next)
1491                 if(sock->flag & SOCK_HIDDEN)
1492                         return 1;
1493         return 0;
1494 }
1495
1496 static void node_link_viewer(SpaceNode *snode, bNode *tonode)
1497 {
1498         bNode *node;
1499
1500         /* context check */
1501         if(tonode==NULL || tonode->outputs.first==NULL)
1502                 return;
1503         if( ELEM(tonode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
1504                 return;
1505         
1506         /* get viewer */
1507         for(node= snode->edittree->nodes.first; node; node= node->next)
1508                 if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
1509                         if(node->flag & NODE_DO_OUTPUT)
1510                                 break;
1511         /* no viewer, we make one active */
1512         if(node==NULL) {
1513                 for(node= snode->edittree->nodes.first; node; node= node->next) {
1514                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
1515                                 node->flag |= NODE_DO_OUTPUT;
1516                                 break;
1517                         }
1518                 }
1519         }
1520                 
1521         if(node) {
1522                 bNodeLink *link;
1523                 bNodeSocket *sock= NULL;
1524
1525                 /* try to find an already connected socket to cycle to the next */
1526                 for(link= snode->edittree->links.first; link; link= link->next)
1527                         if(link->tonode==node && link->fromnode==tonode)
1528                                 if(link->tosock==node->inputs.first)
1529                                         break;
1530
1531                 if(link) {
1532                         /* unlink existing connection */
1533                         sock= link->fromsock;
1534                         nodeRemLink(snode->edittree, link);
1535
1536                         /* find a socket after the previously connected socket */
1537                         for(sock=sock->next; sock; sock= sock->next)
1538                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
1539                                         break;
1540                 }
1541
1542                 /* find a socket starting from the first socket */
1543                 if(!sock) {
1544                         for(sock= tonode->outputs.first; sock; sock= sock->next)
1545                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
1546                                         break;
1547                 }
1548                 
1549                 if(sock) {
1550                         /* get link to viewer */
1551                         for(link= snode->edittree->links.first; link; link= link->next)
1552                                 if(link->tonode==node && link->tosock==node->inputs.first)
1553                                         break;
1554                         
1555                         if(link==NULL) {
1556                                 nodeAddLink(snode->edittree, tonode, sock, node, node->inputs.first);
1557                         }
1558                         else {
1559                                 link->fromnode= tonode;
1560                                 link->fromsock= sock;
1561                         }
1562                         ntreeSolveOrder(snode->edittree);
1563                         snode_tag_changed(snode, node);
1564                 }
1565         }
1566 }
1567
1568
1569 static int node_active_link_viewer(bContext *C, wmOperator *UNUSED(op))
1570 {
1571         SpaceNode *snode= CTX_wm_space_node(C);
1572         bNode *node;
1573         
1574         node= editnode_get_active(snode->edittree);
1575         
1576         if(!node)
1577                 return OPERATOR_CANCELLED;
1578
1579         ED_preview_kill_jobs(C);
1580
1581         node_link_viewer(snode, node);
1582         snode_notify(C, snode);
1583
1584         return OPERATOR_FINISHED;
1585 }
1586
1587
1588
1589 void NODE_OT_link_viewer(wmOperatorType *ot)
1590 {
1591         /* identifiers */
1592         ot->name= "Link to Viewer Node";
1593         ot->description = "Link to Viewer Node";
1594         ot->idname= "NODE_OT_link_viewer";
1595         
1596         /* api callbacks */
1597         ot->exec= node_active_link_viewer;
1598         ot->poll= ED_operator_node_active;
1599         
1600         /* flags */
1601         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1602 }
1603
1604
1605
1606 /* return 0, nothing done */
1607 static int node_mouse_groupheader(SpaceNode *snode)
1608 {
1609         bNode *gnode;
1610         float mx=0, my=0;
1611 // XXX  short mval[2];
1612         
1613         gnode= node_tree_get_editgroup(snode->nodetree);
1614         if(gnode==NULL) return 0;
1615         
1616 // XXX  getmouseco_areawin(mval);
1617 // XXX  areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1618         
1619         /* click in header or outside? */
1620         if(BLI_in_rctf(&gnode->totr, mx, my)==0) {
1621                 rctf rect= gnode->totr;
1622                 
1623                 rect.ymax += NODE_DY;
1624                 if(BLI_in_rctf(&rect, mx, my)==0)
1625                         snode_make_group_editable(snode, NULL); /* toggles, so exits editmode */
1626 //              else
1627 // XXX                  transform_nodes(snode->nodetree, 'g', "Move group");
1628                 
1629                 return 1;
1630         }
1631         return 0;
1632 }
1633
1634 /* checks snode->mouse position, and returns found node/socket */
1635 /* type is SOCK_IN and/or SOCK_OUT */
1636 static int find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
1637 {
1638         bNode *node;
1639         bNodeSocket *sock;
1640         rctf rect;
1641         
1642         /* check if we click in a socket */
1643         for(node= snode->edittree->nodes.first; node; node= node->next) {
1644                 
1645                 rect.xmin = snode->mx - (NODE_SOCKSIZE+4);
1646                 rect.ymin = snode->my - (NODE_SOCKSIZE+4);
1647                 rect.xmax = snode->mx + (NODE_SOCKSIZE+4);
1648                 rect.ymax = snode->my + (NODE_SOCKSIZE+4);
1649                 
1650                 if (!(node->flag & NODE_HIDDEN)) {
1651                         /* extra padding inside and out - allow dragging on the text areas too */
1652                         if (in_out == SOCK_IN) {
1653                                 rect.xmax += NODE_SOCKSIZE;
1654                                 rect.xmin -= NODE_SOCKSIZE*4;
1655                         } else if (in_out == SOCK_OUT) {
1656                                 rect.xmax += NODE_SOCKSIZE*4;
1657                                 rect.xmin -= NODE_SOCKSIZE;
1658                         }
1659                 }
1660                 
1661                 if(in_out & SOCK_IN) {
1662                         for(sock= node->inputs.first; sock; sock= sock->next) {
1663                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1664                                         if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1665                                                 if(node == visible_node(snode, &rect)) {
1666                                                         *nodep= node;
1667                                                         *sockp= sock;
1668                                                         return 1;
1669                                                 }
1670                                         }
1671                                 }
1672                         }
1673                 }
1674                 if(in_out & SOCK_OUT) {
1675                         for(sock= node->outputs.first; sock; sock= sock->next) {
1676                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1677                                         if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1678                                                 if(node == visible_node(snode, &rect)) {
1679                                                         *nodep= node;
1680                                                         *sockp= sock;
1681                                                         return 1;
1682                                                 }
1683                                         }
1684                                 }
1685                         }
1686                 }
1687         }
1688         
1689         /* check group sockets
1690          * NB: using ngroup->outputs as input sockets and vice versa here!
1691          */
1692         if(in_out & SOCK_IN) {
1693                 for(sock= snode->edittree->outputs.first; sock; sock= sock->next) {
1694                         if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1695                                 if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1696                                         *nodep= NULL;   /* NULL node pointer indicates group socket */
1697                                         *sockp= sock;
1698                                         return 1;
1699                                 }
1700                         }
1701                 }
1702         }
1703         if(in_out & SOCK_OUT) {
1704                 for(sock= snode->edittree->inputs.first; sock; sock= sock->next) {
1705                         if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1706                                 if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1707                                         *nodep= NULL;   /* NULL node pointer indicates group socket */
1708                                         *sockp= sock;
1709                                         return 1;
1710                                 }
1711                         }
1712                 }
1713         }
1714         
1715         return 0;
1716 }
1717
1718 static int node_socket_hilights(SpaceNode *snode, int in_out)
1719 {
1720         bNode *node;
1721         bNodeSocket *sock, *tsock, *socksel= NULL;
1722         short redraw= 0;
1723         
1724         if(snode->edittree==NULL) return 0;
1725         
1726         /* deselect sockets */
1727         for(node= snode->edittree->nodes.first; node; node= node->next) {
1728                 for(sock= node->inputs.first; sock; sock= sock->next) {
1729                         if(sock->flag & SELECT) {
1730                                 sock->flag &= ~SELECT;
1731                                 redraw++;
1732                                 socksel= sock;
1733                         }
1734                 }
1735                 for(sock= node->outputs.first; sock; sock= sock->next) {
1736                         if(sock->flag & SELECT) {
1737                                 sock->flag &= ~SELECT;
1738                                 redraw++;
1739                                 socksel= sock;
1740                         }
1741                 }
1742         }
1743         
1744         // XXX mousepos should be set here!
1745         
1746         if(find_indicated_socket(snode, &node, &tsock, in_out)) {
1747                 tsock->flag |= SELECT;
1748                 if(redraw==1 && tsock==socksel) redraw= 0;
1749                 else redraw= 1;
1750         }
1751         
1752         return redraw;
1753 }
1754
1755 static int outside_group_rect(SpaceNode *snode)
1756 {
1757         bNode *gnode= node_tree_get_editgroup(snode->nodetree);
1758         if (gnode) {
1759                 return (snode->mx < gnode->totr.xmin || snode->mx >= gnode->totr.xmax
1760                                 || snode->my < gnode->totr.ymin || snode->my >= gnode->totr.ymax);
1761         }
1762         return 0;
1763 }
1764
1765 /* ****************** Add *********************** */
1766
1767
1768 typedef struct bNodeListItem {
1769         struct bNodeListItem *next, *prev;
1770         struct bNode *node;     
1771 } bNodeListItem;
1772
1773 static int sort_nodes_locx(void *a, void *b)
1774 {
1775         bNodeListItem *nli1 = (bNodeListItem *)a;
1776         bNodeListItem *nli2 = (bNodeListItem *)b;
1777         bNode *node1 = nli1->node;
1778         bNode *node2 = nli2->node;
1779         
1780         if (node1->locx > node2->locx)
1781                 return 1;
1782         else 
1783                 return 0;
1784 }
1785
1786 static int socket_is_available(bNodeTree *ntree, bNodeSocket *sock, int allow_used)
1787 {
1788         if (sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))
1789                 return 0;
1790         
1791         if (!allow_used) {
1792                 if (nodeCountSocketLinks(ntree, sock) > 0)
1793                         return 0;
1794         }
1795         return 1;
1796 }
1797
1798 static bNodeSocket *best_socket_output(bNodeTree *ntree, bNode *node, bNodeSocket *sock_target, int allow_multiple)
1799 {
1800         bNodeSocket *sock;
1801         
1802         /* first try to find a socket with a matching name */
1803         for (sock=node->outputs.first; sock; sock=sock->next) {
1804
1805                 if (!socket_is_available(ntree, sock, allow_multiple))
1806                         continue;
1807
1808                 /* check for same types */
1809                 if (sock->type == sock_target->type) {
1810                         if (strcmp(sock->name, sock_target->name)==0)
1811                                 return sock;
1812                 }
1813         }
1814         
1815         /* otherwise settle for the first available socket of the right type */
1816         for (sock=node->outputs.first; sock; sock=sock->next) {
1817
1818                 if (!socket_is_available(ntree, sock, allow_multiple))
1819                         continue;
1820                 
1821                 /* check for same types */
1822                 if (sock->type == sock_target->type) {
1823                         return sock;
1824                 }
1825         }
1826         
1827         return NULL;
1828 }
1829
1830 /* this is a bit complicated, but designed to prioritise finding 
1831  * sockets of higher types, such as image, first */
1832 static bNodeSocket *best_socket_input(bNodeTree *ntree, bNode *node, int num, int replace)
1833 {
1834         bNodeSocket *sock;
1835         int socktype, maxtype=0;
1836         int a = 0;
1837         
1838         for (sock=node->inputs.first; sock; sock=sock->next) {
1839                 maxtype = MAX2(sock->type, maxtype);
1840         }
1841         
1842         /* find sockets of higher 'types' first (i.e. image) */
1843         for (socktype=maxtype; socktype >= 0; socktype--) {
1844                 for (sock=node->inputs.first; sock; sock=sock->next) {
1845                         
1846                         if (!socket_is_available(ntree, sock, replace)) {
1847                                 a++;
1848                                 continue;
1849                         }
1850                                 
1851                         if (sock->type == socktype) {
1852                                 /* increment to make sure we don't keep finding 
1853                                  * the same socket on every attempt running this function */
1854                                 a++;
1855                                 if (a > num)
1856                                         return sock;
1857                         }
1858                 }
1859         }
1860         
1861         return NULL;
1862 }
1863
1864 void snode_autoconnect(SpaceNode *snode, int allow_multiple, int replace)
1865 {
1866         ListBase *nodelist = MEM_callocN(sizeof(ListBase), "items_list");
1867         bNodeListItem *nli;
1868         bNode *node;
1869         int i, numlinks=0;
1870         
1871         for(node= snode->edittree->nodes.first; node; node= node->next) {
1872                 if(node->flag & NODE_SELECT) {
1873                         nli = MEM_mallocN(sizeof(bNodeListItem), "temporary node list item");
1874                         nli->node = node;
1875                         BLI_addtail(nodelist, nli);
1876                 }
1877         }
1878         
1879         /* sort nodes left to right */
1880         BLI_sortlist(nodelist, sort_nodes_locx);
1881         
1882         for (nli=nodelist->first; nli; nli=nli->next) {
1883                 bNode *node_fr, *node_to;
1884                 bNodeSocket *sock_fr, *sock_to;
1885                 
1886                 if (nli->next == NULL) break;
1887                 
1888                 node_fr = nli->node;
1889                 node_to = nli->next->node;
1890                 
1891                 /* check over input sockets first */
1892                 for (i=0; i<BLI_countlist(&node_to->inputs); i++) {
1893                         
1894                         /* find the best guess input socket */
1895                         sock_to = best_socket_input(snode->edittree, node_to, i, replace);
1896                         if (!sock_to) continue;
1897                         
1898                         /* check for an appropriate output socket to connect from */
1899                         sock_fr = best_socket_output(snode->edittree, node_fr, sock_to, allow_multiple);
1900                         if (!sock_fr) continue;
1901                         
1902                         /* then we can connect */
1903                         if (replace)
1904                                 nodeRemSocketLinks(snode->edittree, sock_to);
1905                         nodeAddLink(snode->edittree, node_fr, sock_fr, node_to, sock_to);
1906                         snode_tag_changed(snode, node_to);
1907                         ++numlinks;
1908                         break;
1909                 }
1910         }
1911         
1912         if (numlinks > 0) {
1913                 node_tree_verify_groups(snode->nodetree);
1914                 ntreeSolveOrder(snode->edittree);
1915         }
1916         
1917         BLI_freelistN(nodelist);
1918         MEM_freeN(nodelist);
1919 }
1920
1921 /* can be called from menus too, but they should do own undopush and redraws */
1922 bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float locy)
1923 {
1924         bNode *node= NULL, *gnode;
1925         
1926         node_deselectall(snode);
1927         
1928         if(type>=NODE_DYNAMIC_MENU) {
1929                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1930         }
1931         else if(type>=NODE_GROUP_MENU) {
1932                 if(snode->edittree!=snode->nodetree) {
1933                         // XXX error("Can not add a Group in a Group");
1934                         return NULL;
1935                 }
1936                 else {
1937                         bNodeTree *ngroup= BLI_findlink(&G.main->nodetree, type-NODE_GROUP_MENU);
1938                         if(ngroup)
1939                                 node= nodeAddNodeType(snode->edittree, NODE_GROUP, ngroup, NULL);
1940                 }
1941         }
1942         else
1943                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1944         
1945         /* generics */
1946         if(node) {
1947                 node->locx= locx;
1948                 node->locy= locy + 60.0f;               // arbitrary.. so its visible, (0,0) is top of node
1949                 node->flag |= SELECT;
1950                 
1951                 gnode= node_tree_get_editgroup(snode->nodetree);
1952                 if(gnode) {
1953                         node->locx -= gnode->locx;
1954                         node->locy -= gnode->locy;
1955                 }
1956
1957                 node_tree_verify_groups(snode->nodetree);
1958                 node_set_active(snode, node);
1959                 
1960                 if(snode->nodetree->type==NTREE_COMPOSIT) {
1961                         if(ELEM4(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS, CMP_NODE_OUTPUT_FILE))
1962                                 node->id = &scene->id;
1963                         
1964                         ntreeCompositForceHidden(snode->edittree, scene);
1965                 }
1966                         
1967                 if(node->id)
1968                         id_us_plus(node->id);
1969                         
1970                 snode_tag_changed(snode, node);
1971         }
1972         
1973         if(snode->nodetree->type==NTREE_TEXTURE) {
1974                 ntreeTexCheckCyclics(snode->edittree);
1975         }
1976         
1977         return node;
1978 }
1979
1980 /* ****************** Duplicate *********************** */
1981
1982 static int node_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
1983 {
1984         SpaceNode *snode= CTX_wm_space_node(C);
1985         bNodeTree *ntree= snode->edittree;
1986         bNode *node, *newnode, *last;
1987         
1988         ED_preview_kill_jobs(C);
1989         
1990         last = ntree->nodes.last;
1991         for(node= ntree->nodes.first; node; node= node->next) {
1992                 if(node->flag & SELECT) {
1993                         newnode = nodeCopyNode(ntree, node);
1994                         
1995                         /* deselect old node, select the copy instead */
1996                         node->flag &= ~(NODE_SELECT|NODE_ACTIVE);
1997                         newnode->flag |= NODE_SELECT;
1998                         
1999                         if(newnode->id) {
2000                                 /* simple id user adjustment, node internal functions dont touch this
2001                                  * but operators and readfile.c do. */
2002                                 id_us_plus(newnode->id);
2003                                 /* to ensure redraws or rerenders happen */
2004                                 ED_node_changed_update(snode->id, newnode);
2005                         }
2006                 }
2007                 
2008                 /* make sure we don't copy new nodes again! */
2009                 if (node==last)
2010                         break;
2011         }
2012         
2013         ntreeSolveOrder(ntree);
2014         
2015         node_tree_verify_groups(snode->nodetree);
2016         snode_notify(C, snode);
2017
2018         return OPERATOR_FINISHED;
2019 }
2020
2021 void NODE_OT_duplicate(wmOperatorType *ot)
2022 {
2023         /* identifiers */
2024         ot->name= "Duplicate Nodes";
2025         ot->description = "Duplicate the nodes";
2026         ot->idname= "NODE_OT_duplicate";
2027         
2028         /* api callbacks */
2029         ot->exec= node_duplicate_exec;
2030         ot->poll= ED_operator_node_active;
2031         
2032         /* flags */
2033         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2034 }
2035
2036 /* *************************** add link op ******************** */
2037
2038 static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeLink *link)
2039 {
2040         bNodeLink *tlink;
2041         bNodeSocket *sock;
2042         
2043         if(tsock && nodeCountSocketLinks(snode->edittree, link->tosock) > tsock->limit) {
2044                 
2045                 for(tlink= snode->edittree->links.first; tlink; tlink= tlink->next) {
2046                         if(link!=tlink && tlink->tosock==link->tosock)
2047                                 break;
2048                 }
2049                 if(tlink) {
2050                         /* try to move the existing link to the next available socket */
2051                         if (tlink->tonode) {
2052                                 /* is there a free input socket with same type? */
2053                                 for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
2054                                         if(sock->type==tlink->fromsock->type)
2055                                                 if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
2056                                                         break;
2057                                 }
2058                                 if(sock) {
2059                                         tlink->tosock= sock;
2060                                         sock->flag &= ~SOCK_HIDDEN;
2061                                 }
2062                                 else {
2063                                         nodeRemLink(snode->edittree, tlink);
2064                                 }
2065                         }
2066                         else
2067                                 nodeRemLink(snode->edittree, tlink);
2068                 }
2069         }
2070 }
2071
2072 /* loop that adds a nodelink, called by function below  */
2073 /* in_out = starting socket */
2074 static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
2075 {
2076         SpaceNode *snode= CTX_wm_space_node(C);
2077         ARegion *ar= CTX_wm_region(C);
2078         bNodeLinkDrag *nldrag= op->customdata;
2079         bNode *tnode, *node;
2080         bNodeSocket *tsock= NULL, *sock;
2081         bNodeLink *link;
2082         int in_out;
2083
2084         in_out= nldrag->in_out;
2085         node= nldrag->node;
2086         sock= nldrag->sock;
2087         link= nldrag->link;
2088         
2089         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
2090                                                          &snode->mx, &snode->my);
2091
2092         switch (event->type) {
2093                 case MOUSEMOVE:
2094                         
2095                         if(in_out==SOCK_OUT) {
2096                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_IN)) {
2097                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
2098                                                 if( link->tosock!= tsock && (!tnode || (tnode!=node && link->tonode!=tnode)) ) {
2099                                                         link->tonode= tnode;
2100                                                         link->tosock= tsock;
2101                                                         if (link->prev==NULL && link->next==NULL)
2102                                                                 BLI_addtail(&snode->edittree->links, link);
2103                                                         ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
2104                                                 }
2105                                         }
2106                                 }
2107                                 else {
2108                                         BLI_remlink(&snode->edittree->links, link);
2109                                         link->prev = link->next = NULL;
2110                                         link->tonode= NULL;
2111                                         link->tosock= NULL;
2112                                 }
2113                         }
2114                         else {
2115                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_OUT)) {
2116                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
2117                                                 if(nodeCountSocketLinks(snode->edittree, tsock) < tsock->limit) {
2118                                                         if( link->fromsock!= tsock && (!tnode || (tnode!=node && link->fromnode!=tnode)) ) {
2119                                                                 link->fromnode= tnode;
2120                                                                 link->fromsock= tsock;
2121                                                                 if (link->prev==NULL && link->next==NULL)
2122                                                                         BLI_addtail(&snode->edittree->links, link);
2123                                                                 ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
2124                                                         }
2125                                                 }
2126                                         }
2127                                 }
2128                                 else {
2129                                         BLI_remlink(&snode->edittree->links, link);
2130                                         link->prev = link->next = NULL;
2131                                         link->fromnode= NULL;
2132                                         link->fromsock= NULL;
2133                                 }
2134                         }
2135                         /* hilight target sockets only */
2136                         node_socket_hilights(snode, in_out==SOCK_OUT?SOCK_IN:SOCK_OUT);
2137                         ED_region_tag_redraw(ar);
2138                         break;
2139                         
2140                 case LEFTMOUSE:
2141                 case RIGHTMOUSE:
2142                 case MIDDLEMOUSE:
2143                         if(link->tosock && link->fromsock) {
2144                                 /* send changed events for original tonode and new */
2145                                 snode_tag_changed(snode, link->tonode);
2146                                 
2147                                 /* we might need to remove a link */
2148                                 if(in_out==SOCK_OUT)
2149                                         node_remove_extra_links(snode, link->tosock, link);
2150                         }
2151                         else if (outside_group_rect(snode) && (link->tonode || link->fromnode)) {
2152                                 /* automatically add new group socket */
2153                                 if (link->tonode && link->tosock) {
2154                                         link->fromsock = nodeGroupExposeSocket(snode->edittree, link->tosock, SOCK_IN);
2155                                         link->fromnode = NULL;
2156                                         if (link->prev==NULL && link->next==NULL)
2157                                                 BLI_addtail(&snode->edittree->links, link);
2158                                 }
2159                                 else if (link->fromnode && link->fromsock) {
2160                                         link->tosock = nodeGroupExposeSocket(snode->edittree, link->fromsock, SOCK_OUT);
2161                                         link->tonode = NULL;
2162                                         if (link->prev==NULL && link->next==NULL)
2163                                                 BLI_addtail(&snode->edittree->links, link);
2164                                 }
2165                         }
2166                         else
2167                                 nodeRemLink(snode->edittree, link);
2168                         
2169                         ntreeSolveOrder(snode->edittree);
2170                         node_tree_verify_groups(snode->nodetree);
2171                         snode_notify(C, snode);
2172                         
2173                         BLI_remlink(&snode->linkdrag, nldrag);
2174                         MEM_freeN(nldrag);
2175                         
2176                         return OPERATOR_FINISHED;
2177         }
2178         
2179         return OPERATOR_RUNNING_MODAL;
2180 }
2181
2182 /* return 1 when socket clicked */
2183 static int node_link_init(SpaceNode *snode, bNodeLinkDrag *nldrag)
2184 {
2185         bNodeLink *link;
2186
2187         /* output indicated? */
2188         if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_OUT)) {
2189                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
2190                         return SOCK_OUT;
2191                 else {
2192                         /* find if we break a link */
2193                         for(link= snode->edittree->links.first; link; link= link->next) {
2194                                 if(link->fromsock==nldrag->sock)
2195                                         break;
2196                         }
2197                         if(link) {
2198                                 nldrag->node= link->tonode;
2199                                 nldrag->sock= link->tosock;
2200                                 nodeRemLink(snode->edittree, link);
2201                                 return SOCK_IN;
2202                         }
2203                 }
2204         }
2205         /* or an input? */
2206         else if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_IN)) {
2207                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
2208                         return SOCK_IN;
2209                 else {
2210                         /* find if we break a link */
2211                         for(link= snode->edittree->links.first; link; link= link->next) {
2212                                 if(link->tosock==nldrag->sock)
2213                                         break;
2214                         }
2215                         if(link) {
2216                                 /* send changed event to original tonode */
2217                                 if(link->tonode) 
2218                                         snode_tag_changed(snode, link->tonode);
2219                                 
2220                                 nldrag->node= link->fromnode;
2221                                 nldrag->sock= link->fromsock;
2222                                 nodeRemLink(snode->edittree, link);
2223                                 return SOCK_OUT;
2224                         }
2225                 }
2226         }
2227         
2228         return 0;
2229 }
2230
2231 static int node_link_invoke(bContext *C, wmOperator *op, wmEvent *event)
2232 {
2233         SpaceNode *snode= CTX_wm_space_node(C);
2234         ARegion *ar= CTX_wm_region(C);
2235         bNodeLinkDrag *nldrag= MEM_callocN(sizeof(bNodeLinkDrag), "drag link op customdata");
2236         
2237         
2238         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
2239                                                          &snode->mx, &snode->my);
2240
2241         ED_preview_kill_jobs(C);
2242
2243         nldrag->in_out= node_link_init(snode, nldrag);
2244                 
2245         if(nldrag->in_out) {
2246                 op->customdata= nldrag;
2247                 
2248                 /* we make a temporal link */
2249                 if(nldrag->in_out==SOCK_OUT) {
2250                         nldrag->link= MEM_callocN(sizeof(bNodeLink), "link");
2251                         nldrag->link->fromnode= nldrag->node;
2252                         nldrag->link->fromsock= nldrag->sock;
2253                         nldrag->link->tonode= NULL;
2254                         nldrag->link->tosock= NULL;
2255                 }
2256                 else {
2257                         nldrag->link= MEM_callocN(sizeof(bNodeLink), "link");
2258                         nldrag->link->fromnode= NULL;
2259                         nldrag->link->fromsock= NULL;
2260                         nldrag->link->tonode= nldrag->node;
2261                         nldrag->link->tosock= nldrag->sock;
2262                 }
2263                 BLI_addtail(&snode->linkdrag, nldrag);
2264                 
2265                 /* add modal handler */
2266                 WM_event_add_modal_handler(C, op);
2267                 
2268                 return OPERATOR_RUNNING_MODAL;
2269         }
2270         else {
2271                 MEM_freeN(nldrag);
2272                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2273         }
2274 }
2275
2276 void NODE_OT_link(wmOperatorType *ot)
2277 {
2278         /* identifiers */
2279         ot->name= "Link Nodes";
2280         ot->idname= "NODE_OT_link";
2281         
2282         /* api callbacks */
2283         ot->invoke= node_link_invoke;
2284         ot->modal= node_link_modal;
2285 //      ot->exec= node_link_exec;
2286         ot->poll= ED_operator_node_active;
2287         
2288         /* flags */
2289         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2290 }
2291
2292 /* ********************** Make Link operator ***************** */
2293
2294 /* makes a link between selected output and input sockets */
2295 static int node_make_link_exec(bContext *C, wmOperator *op)
2296 {
2297         SpaceNode *snode= CTX_wm_space_node(C);
2298         int replace = RNA_boolean_get(op->ptr, "replace");
2299
2300         ED_preview_kill_jobs(C);
2301
2302         snode_autoconnect(snode, 1, replace);
2303
2304         node_tree_verify_groups(snode->nodetree);
2305         snode_notify(C, snode);
2306         
2307         return OPERATOR_FINISHED;
2308 }
2309
2310 void NODE_OT_link_make(wmOperatorType *ot)
2311 {
2312         /* identifiers */
2313         ot->name= "Make Links";
2314         ot->description= "Makes a link between selected output in input sockets";
2315         ot->idname= "NODE_OT_link_make";
2316         
2317         /* callbacks */
2318         ot->exec= node_make_link_exec;
2319         ot->poll= ED_operator_node_active; // XXX we need a special poll which checks that there are selected input/output sockets
2320         
2321         /* flags */
2322         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2323         
2324         RNA_def_boolean(ot->srna, "replace", 0, "Replace", "Replace socket connections with the new links");
2325 }
2326
2327 /* ********************** Cut Link operator ***************** */
2328
2329 #define LINK_RESOL 12
2330 static int cut_links_intersect(bNodeLink *link, float mcoords[][2], int tot)
2331 {
2332         float coord_array[LINK_RESOL+1][2];
2333         int i, b;
2334         
2335         if(node_link_bezier_points(NULL, NULL, link, coord_array, LINK_RESOL)) {
2336
2337                 for(i=0; i<tot-1; i++)
2338                         for(b=0; b<LINK_RESOL; b++)
2339                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
2340                                         return 1;
2341         }
2342         return 0;
2343 }
2344
2345 static int cut_links_exec(bContext *C, wmOperator *op)
2346 {
2347         SpaceNode *snode= CTX_wm_space_node(C);
2348         ARegion *ar= CTX_wm_region(C);
2349         float mcoords[256][2];
2350         int i= 0;
2351         
2352         RNA_BEGIN(op->ptr, itemptr, "path") {
2353                 float loc[2];
2354                 
2355                 RNA_float_get_array(&itemptr, "loc", loc);
2356                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
2357                                                                  &mcoords[i][0], &mcoords[i][1]);
2358                 i++;
2359                 if(i>= 256) break;
2360         }
2361         RNA_END;
2362         
2363         if(i>1) {
2364                 bNodeLink *link, *next;
2365
2366                 ED_preview_kill_jobs(C);
2367                 
2368                 for(link= snode->edittree->links.first; link; link= next) {
2369                         next= link->next;
2370                         
2371                         if(cut_links_intersect(link, mcoords, i)) {
2372                                 snode_tag_changed(snode, link->tonode);
2373                                 nodeRemLink(snode->edittree, link);
2374                         }
2375                 }
2376                 
2377                 ntreeSolveOrder(snode->edittree);
2378                 node_tree_verify_groups(snode->nodetree);
2379                 snode_notify(C, snode);
2380                 
2381                 return OPERATOR_FINISHED;
2382         }
2383         
2384         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2385 }
2386
2387 void NODE_OT_links_cut(wmOperatorType *ot)
2388 {
2389         PropertyRNA *prop;
2390         
2391         ot->name= "Cut links";
2392         ot->idname= "NODE_OT_links_cut";
2393         
2394         ot->invoke= WM_gesture_lines_invoke;
2395         ot->modal= WM_gesture_lines_modal;
2396         ot->exec= cut_links_exec;
2397         
2398         ot->poll= ED_operator_node_active;
2399         
2400         /* flags */
2401         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2402         
2403         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2404         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2405         /* internal */
2406         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
2407 }
2408
2409 /* ******************************** */
2410 // XXX some code needing updating to operators...
2411
2412
2413 /* goes over all scenes, reads render layers */
2414 static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
2415 {
2416         Main *bmain= CTX_data_main(C);
2417         SpaceNode *snode= CTX_wm_space_node(C);
2418         Scene *curscene= CTX_data_scene(C), *scene;
2419         bNode *node;
2420
2421         ED_preview_kill_jobs(C);
2422
2423         /* first tag scenes unread */
2424         for(scene= bmain->scene.first; scene; scene= scene->id.next) 
2425                 scene->id.flag |= LIB_DOIT;
2426
2427         for(node= snode->edittree->nodes.first; node; node= node->next) {
2428                 if(node->type==CMP_NODE_R_LAYERS) {
2429                         ID *id= node->id;
2430                         if(id->flag & LIB_DOIT) {
2431                                 RE_ReadRenderResult(curscene, (Scene *)id);
2432                                 ntreeCompositTagRender((Scene *)id);
2433                                 id->flag &= ~LIB_DOIT;
2434                         }
2435                 }
2436         }
2437         
2438         snode_notify(C, snode);
2439         return OPERATOR_FINISHED;
2440 }
2441
2442 void NODE_OT_read_renderlayers(wmOperatorType *ot)
2443 {
2444         
2445         ot->name= "Read Render Layers";
2446         ot->idname= "NODE_OT_read_renderlayers";
2447         
2448         ot->exec= node_read_renderlayers_exec;
2449         
2450         ot->poll= composite_node_active;
2451         
2452         /* flags */
2453         ot->flag= 0;
2454 }
2455
2456 static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
2457 {
2458         Main *bmain= CTX_data_main(C);
2459         SpaceNode *snode= CTX_wm_space_node(C);
2460         Scene *curscene= CTX_data_scene(C);
2461         Render *re= RE_NewRender(curscene->id.name);
2462
2463         WM_cursor_wait(1);
2464
2465         RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
2466         snode_notify(C, snode);
2467         
2468         WM_cursor_wait(0);
2469         return OPERATOR_FINISHED;
2470 }
2471
2472
2473 void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
2474 {
2475         
2476         ot->name= "Read Full Sample Layers";
2477         ot->idname= "NODE_OT_read_fullsamplelayers";
2478         
2479         ot->exec= node_read_fullsamplelayers_exec;
2480         
2481         ot->poll= composite_node_active;
2482         
2483         /* flags */
2484         ot->flag= 0;
2485 }
2486
2487 int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
2488 {
2489         Scene *sce= CTX_data_scene(C);
2490         bNode *node;
2491         
2492         for(node= sce->nodetree->nodes.first; node; node= node->next) {
2493                 if(node->id==(ID *)sce && node->need_exec) {
2494                         break;
2495                 }
2496         }
2497         if(node) {
2498                 SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
2499                 
2500                 if(srl) {
2501                         PointerRNA op_ptr;
2502                         
2503                         WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
2504                         RNA_string_set(&op_ptr, "layer", srl->name);
2505                         RNA_string_set(&op_ptr, "scene", sce->id.name+2);
2506                         
2507                         /* to keep keypositions */
2508                         sce->r.scemode |= R_NO_FRAME_UPDATE;
2509                         
2510                         WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
2511
2512                         WM_operator_properties_free(&op_ptr);
2513                         
2514                         return OPERATOR_FINISHED;
2515                 }
2516                    
2517         }
2518         return OPERATOR_CANCELLED;
2519 }
2520
2521 void NODE_OT_render_changed(wmOperatorType *ot)
2522 {
2523         
2524         ot->name= "Render Changed Layer";
2525         ot->idname= "NODE_OT_render_changed";
2526         
2527         ot->exec= node_render_changed_exec;
2528         
2529         ot->poll= composite_node_active;
2530         
2531         /* flags */
2532         ot->flag= 0;
2533 }
2534
2535
2536 /* ****************** Make Group operator ******************* */
2537
2538 static int node_group_make_exec(bContext *C, wmOperator *op)
2539 {
2540         SpaceNode *snode = CTX_wm_space_node(C);
2541         bNode *gnode;
2542         
2543         if(snode->edittree!=snode->nodetree) {
2544                 BKE_report(op->reports, RPT_WARNING, "Can not add a new Group in a Group");
2545                 return OPERATOR_CANCELLED;
2546         }
2547         
2548         /* for time being... is too complex to handle */
2549         if(snode->treetype==NTREE_COMPOSIT) {
2550                 for(gnode=snode->nodetree->nodes.first; gnode; gnode= gnode->next) {
2551                         if(gnode->flag & SELECT)
2552                                 if(gnode->type==CMP_NODE_R_LAYERS)
2553                                         break;
2554                 }
2555                 
2556                 if(gnode) {
2557                         BKE_report(op->reports, RPT_WARNING, "Can not add RenderLayer in a Group");
2558                         return OPERATOR_CANCELLED;
2559                 }
2560         }
2561
2562         ED_preview_kill_jobs(C);
2563         
2564         gnode= nodeMakeGroupFromSelected(snode->nodetree);
2565         if(gnode==NULL) {
2566                 BKE_report(op->reports, RPT_WARNING, "Can not make Group");
2567                 return OPERATOR_CANCELLED;
2568         }
2569         else {
2570                 nodeSetActive(snode->nodetree, gnode);
2571                 ntreeSolveOrder(snode->nodetree);
2572         }
2573         
2574         snode_notify(C, snode);
2575         
2576         return OPERATOR_FINISHED;
2577 }
2578
2579 void NODE_OT_group_make(wmOperatorType *ot)
2580 {
2581         /* identifiers */
2582         ot->name = "Group";
2583         ot->description = "Make group from selected nodes";
2584         ot->idname = "NODE_OT_group_make";
2585         
2586         /* api callbacks */
2587         ot->exec = node_group_make_exec;
2588         ot->poll = ED_operator_node_active;
2589         
2590         /* flags */
2591         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2592 }
2593
2594 /* ****************** Hide operator *********************** */
2595
2596 static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
2597 {
2598         int tot_eq= 0, tot_neq= 0;
2599         bNode *node;
2600
2601         for(node= snode->edittree->nodes.first; node; node= node->next) {
2602                 if(node->flag & SELECT) {
2603
2604                         if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
2605                                 continue;
2606
2607                         if(node->flag & toggle_flag)
2608                                 tot_eq++;
2609                         else
2610                                 tot_neq++;
2611                 }
2612         }
2613         for(node= snode->edittree->nodes.first; node; node= node->next) {
2614                 if(node->flag & SELECT) {
2615
2616                         if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
2617                                 continue;
2618
2619                         if( (tot_eq && tot_neq) || tot_eq==0)
2620                                 node->flag |= toggle_flag;
2621                         else
2622                                 node->flag &= ~toggle_flag;
2623                 }
2624         }
2625 }
2626
2627 static int node_hide_exec(bContext *C, wmOperator *UNUSED(op))
2628 {
2629         SpaceNode *snode= CTX_wm_space_node(C);
2630         
2631         /* sanity checking (poll callback checks this already) */
2632         if((snode == NULL) || (snode->edittree == NULL))
2633                 return OPERATOR_CANCELLED;
2634         
2635         node_flag_toggle_exec(snode, NODE_HIDDEN);
2636         
2637         snode_notify(C, snode);
2638         
2639         return OPERATOR_FINISHED;
2640 }
2641
2642 void NODE_OT_hide_toggle(wmOperatorType *ot)
2643 {
2644         /* identifiers */
2645         ot->name= "Hide";
2646         ot->description= "Toggle hiding of selected nodes";
2647         ot->idname= "NODE_OT_hide_toggle";
2648         
2649         /* callbacks */
2650         ot->exec= node_hide_exec;
2651         ot->poll= ED_operator_node_active;
2652
2653         /* flags */
2654         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2655 }
2656
2657 static int node_preview_exec(bContext *C, wmOperator *UNUSED(op))
2658 {
2659         SpaceNode *snode= CTX_wm_space_node(C);
2660
2661         /* sanity checking (poll callback checks this already) */
2662         if((snode == NULL) || (snode->edittree == NULL))
2663                 return OPERATOR_CANCELLED;
2664
2665         ED_preview_kill_jobs(C);
2666
2667         node_flag_toggle_exec(snode, NODE_PREVIEW);
2668
2669         snode_notify(C, snode);
2670
2671         return OPERATOR_FINISHED;
2672 }
2673
2674 void NODE_OT_preview_toggle(wmOperatorType *ot)
2675 {
2676         /* identifiers */
2677         ot->name= "Toggle Node Preview";
2678         ot->description= "Toggle preview display for selected nodes";
2679         ot->idname= "NODE_OT_preview_toggle";
2680
2681         /* callbacks */
2682         ot->exec= node_preview_exec;
2683         ot->poll= ED_operator_node_active;
2684
2685         /* flags */
2686         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2687 }
2688
2689 static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2690 {
2691         SpaceNode *snode= CTX_wm_space_node(C);
2692         bNode *node;
2693         int hidden= 0;
2694
2695         /* sanity checking (poll callback checks this already) */
2696         if((snode == NULL) || (snode->edittree == NULL))
2697                 return OPERATOR_CANCELLED;
2698
2699         ED_preview_kill_jobs(C);
2700
2701         for(node= snode->edittree->nodes.first; node; node= node->next) {
2702                 if(node->flag & SELECT) {
2703                         if(node_has_hidden_sockets(node)) {
2704                                 hidden= 1;
2705                                 break;
2706                         }
2707                 }
2708         }
2709
2710         for(node= snode->edittree->nodes.first; node; node= node->next) {
2711                 if(node->flag & SELECT) {
2712                         node_set_hidden_sockets(snode, node, !hidden);
2713                 }
2714         }
2715
2716         node_tree_verify_groups(snode->nodetree);
2717
2718         snode_notify(C, snode);
2719
2720         return OPERATOR_FINISHED;
2721 }
2722
2723 void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
2724 {
2725         /* identifiers */
2726         ot->name= "Toggle Hidden Node Sockets";
2727         ot->description= "Toggle unused node socket display";
2728         ot->idname= "NODE_OT_hide_socket_toggle";
2729
2730         /* callbacks */
2731         ot->exec= node_socket_toggle_exec;
2732         ot->poll= ED_operator_node_active;
2733
2734         /* flags */
2735         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2736 }
2737
2738 /* ****************** Mute operator *********************** */
2739
2740 static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
2741 {
2742         SpaceNode *snode= CTX_wm_space_node(C);
2743         bNode *node;
2744
2745         /* no disabling inside of groups */
2746         if(node_tree_get_editgroup(snode->nodetree))
2747                 return OPERATOR_CANCELLED;
2748         
2749         ED_preview_kill_jobs(C);
2750
2751         for(node= snode->edittree->nodes.first; node; node= node->next) {
2752                 if(node->flag & SELECT) {
2753                         if(node->inputs.first && node->outputs.first) {
2754                                 node->flag ^= NODE_MUTED;
2755                                 snode_tag_changed(snode, node);
2756                         }
2757                 }
2758         }
2759         
2760         snode_notify(C, snode);
2761         
2762         return OPERATOR_FINISHED;
2763 }
2764
2765 void NODE_OT_mute_toggle(wmOperatorType *ot)
2766 {
2767         /* identifiers */
2768         ot->name= "Toggle Node Mute";
2769         ot->description= "Toggle muting of the nodes";
2770         ot->idname= "NODE_OT_mute_toggle";
2771         
2772         /* callbacks */
2773         ot->exec= node_mute_exec;
2774         ot->poll= ED_operator_node_active;
2775         
2776         /* flags */
2777         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2778 }
2779
2780 /* ****************** Delete operator ******************* */
2781
2782 static int node_delete_exec(bContext *C, wmOperator *UNUSED(op))
2783 {
2784         SpaceNode *snode= CTX_wm_space_node(C);
2785         bNode *node, *next;
2786         
2787         ED_preview_kill_jobs(C);
2788
2789         for(node= snode->edittree->nodes.first; node; node= next) {
2790                 next= node->next;
2791                 if(node->flag & SELECT) {
2792                         /* check id user here, nodeFreeNode is called for free dbase too */
2793                         if(node->id)
2794                                 node->id->us--;
2795                         nodeFreeNode(snode->edittree, node);
2796                 }
2797         }
2798         
2799         node_tree_verify_groups(snode->nodetree);
2800
2801         snode_notify(C, snode);
2802         
2803         return OPERATOR_FINISHED;
2804 }
2805
2806 void NODE_OT_delete(wmOperatorType *ot)
2807 {
2808         /* identifiers */
2809         ot->name= "Delete";
2810         ot->description = "Delete selected nodes";
2811         ot->idname= "NODE_OT_delete";
2812         
2813         /* api callbacks */
2814         ot->exec= node_delete_exec;
2815         ot->poll= ED_operator_node_active;
2816         
2817         /* flags */
2818         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2819 }
2820
2821 /* ****************** Show Cyclic Dependencies Operator  ******************* */
2822
2823 static int node_show_cycles_exec(bContext *C, wmOperator *UNUSED(op))
2824 {
2825         SpaceNode *snode= CTX_wm_space_node(C);
2826         
2827         /* this is just a wrapper around this call... */
2828         ntreeSolveOrder(snode->edittree);
2829         snode_notify(C, snode);
2830         
2831         return OPERATOR_FINISHED;
2832 }
2833
2834 void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
2835 {
2836         /* identifiers */
2837         ot->name= "Show Cyclic Dependencies";
2838         ot->description= "Sort the nodes and show the cyclic dependencies between the nodes";
2839         ot->idname= "NODE_OT_show_cyclic_dependencies";
2840         
2841         /* callbacks */
2842         ot->exec= node_show_cycles_exec;
2843         ot->poll= ED_operator_node_active;
2844         
2845         /* flags */
2846         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2847 }
2848
2849 /* ****************** Add File Node Operator  ******************* */
2850
2851 static int node_add_file_exec(bContext *C, wmOperator *op)
2852 {
2853         Scene *scene= CTX_data_scene(C);
2854         SpaceNode *snode= CTX_wm_space_node(C);
2855         bNode *node;
2856         Image *ima= NULL;
2857         int ntype=0;
2858
2859         /* check input variables */
2860         if (RNA_property_is_set(op->ptr, "filepath"))
2861         {
2862                 char path[FILE_MAX];
2863                 RNA_string_get(op->ptr, "filepath", path);
2864
2865                 errno= 0;
2866
2867                 ima= BKE_add_image_file(path);
2868
2869                 if(!ima) {
2870                         BKE_reportf(op->reports, RPT_ERROR, "Can't read: \"%s\", %s.", path, errno ? strerror(errno) : "Unsupported image format");
2871                         return OPERATOR_CANCELLED;
2872                 }
2873         }
2874         else if(RNA_property_is_set(op->ptr, "name"))
2875         {
2876                 char name[32];
2877                 RNA_string_get(op->ptr, "name", name);
2878                 ima= (Image *)find_id("IM", name);
2879
2880                 if(!ima) {
2881                         BKE_reportf(op->reports, RPT_ERROR, "Image named \"%s\", not found.", name);
2882                         return OPERATOR_CANCELLED;
2883                 }
2884         }
2885         
2886         node_deselectall(snode);
2887         
2888         if (snode->nodetree->type==NTREE_COMPOSIT)
2889                 ntype = CMP_NODE_IMAGE;
2890
2891         ED_preview_kill_jobs(C);
2892         
2893         node = node_add_node(snode, scene, ntype, snode->mx, snode->my);
2894         
2895         if (!node) {
2896                 BKE_report(op->reports, RPT_WARNING, "Could not add an image node.");
2897                 return OPERATOR_CANCELLED;
2898         }
2899         
2900         node->id = (ID *)ima;
2901         
2902         snode_notify(C, snode);
2903         
2904         return OPERATOR_FINISHED;
2905 }
2906
2907 static int node_add_file_invoke(bContext *C, wmOperator *op, wmEvent *event)
2908 {
2909         ARegion *ar= CTX_wm_region(C);
2910         SpaceNode *snode= CTX_wm_space_node(C);
2911         
2912         /* convert mouse coordinates to v2d space */
2913         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
2914                                                          &snode->mx, &snode->my);
2915         
2916         if (RNA_property_is_set(op->ptr, "filepath") || RNA_property_is_set(op->ptr, "name"))
2917                 return node_add_file_exec(C, op);
2918         else
2919                 return WM_operator_filesel(C, op, event);
2920 }
2921
2922 void NODE_OT_add_file(wmOperatorType *ot)
2923 {
2924         /* identifiers */
2925         ot->name= "Add File Node";
2926         ot->description= "Add a file node to the current node editor";
2927         ot->idname= "NODE_OT_add_file";
2928         
2929         /* callbacks */
2930         ot->exec= node_add_file_exec;
2931         ot->invoke= node_add_file_invoke;
2932         ot->poll= composite_node_active;
2933         
2934         /* flags */
2935         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2936         
2937         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH);  //XXX TODO, relative_path
2938         RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Datablock name to assign.");
2939 }
2940