Recreating my GSoC branch.
[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 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_object_types.h"
38 #include "DNA_material_types.h"
39 #include "DNA_node_types.h"
40 #include "DNA_scene_types.h"
41
42 #include "BKE_context.h"
43 #include "BKE_colortools.h"
44 #include "BKE_global.h"
45 #include "BKE_image.h"
46 #include "BKE_library.h"
47 #include "BKE_main.h"
48 #include "BKE_node.h"
49 #include "BKE_material.h"
50 #include "BKE_paint.h"
51 #include "BKE_texture.h"
52 #include "BKE_report.h"
53 #include "BKE_scene.h"
54 #include "BKE_utildefines.h"
55
56 #include "BIF_gl.h"
57
58 #include "BLI_math.h"
59 #include "BLI_blenlib.h"
60 #include "BLI_storage_types.h"
61
62 #include "RE_pipeline.h"
63
64 #include "IMB_imbuf_types.h"
65
66 #include "ED_node.h"
67 #include "ED_screen.h"
68
69 #include "RNA_access.h"
70 #include "RNA_define.h"
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74
75 #include "UI_interface.h"
76 #include "UI_view2d.h"
77
78 #include "node_intern.h"
79
80 #define SOCK_IN         1
81 #define SOCK_OUT        2
82
83 /* ***************** composite job manager ********************** */
84
85 typedef struct CompoJob {
86         Scene *scene;
87         bNodeTree *ntree;
88         bNodeTree *localtree;
89         short *stop;
90         short *do_update;
91         float *progress;
92 } CompoJob;
93
94 /* called by compo, only to check job 'stop' value */
95 static int compo_breakjob(void *cjv)
96 {
97         CompoJob *cj= cjv;
98         
99         return *(cj->stop);
100 }
101
102 /* called by compo, wmJob sends notifier */
103 static void compo_redrawjob(void *cjv, char *str)
104 {
105         CompoJob *cj= cjv;
106         
107         *(cj->do_update)= 1;
108 }
109
110 static void compo_freejob(void *cjv)
111 {
112         CompoJob *cj= cjv;
113
114         if(cj->localtree) {
115                 ntreeLocalMerge(cj->localtree, cj->ntree);
116         }
117         MEM_freeN(cj);
118 }
119
120 /* only now we copy the nodetree, so adding many jobs while
121    sliding buttons doesn't frustrate */
122 static void compo_initjob(void *cjv)
123 {
124         CompoJob *cj= cjv;
125
126         cj->localtree= ntreeLocalize(cj->ntree);
127 }
128
129 /* called before redraw notifiers, it moves finished previews over */
130 static void compo_updatejob(void *cjv)
131 {
132         CompoJob *cj= cjv;
133         
134         ntreeLocalSync(cj->localtree, cj->ntree);
135 }
136
137 static void compo_progressjob(void *cjv, float progress)
138 {
139         CompoJob *cj= cjv;
140         
141         *(cj->progress) = progress;
142 }
143
144
145 /* only this runs inside thread */
146 static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
147 {
148         CompoJob *cj= cjv;
149         bNodeTree *ntree= cj->localtree;
150
151         if(cj->scene->use_nodes==0)
152                 return;
153         
154         cj->stop= stop;
155         cj->do_update= do_update;
156         cj->progress= progress;
157         
158         ntree->test_break= compo_breakjob;
159         ntree->tbh= cj;
160         ntree->stats_draw= compo_redrawjob;
161         ntree->sdh= cj;
162         ntree->progress= compo_progressjob;
163         ntree->prh= cj;
164         
165         // XXX BIF_store_spare();
166         
167         ntreeCompositExecTree(ntree, &cj->scene->r, 1); /* 1 is do_previews */
168         
169         ntree->test_break= NULL;
170         ntree->stats_draw= NULL;
171         ntree->progress= NULL;
172
173 }
174
175 void snode_composite_job(const bContext *C, ScrArea *sa)
176 {
177         SpaceNode *snode= sa->spacedata.first;
178         wmJob *steve;
179         CompoJob *cj;
180
181         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Compositing", WM_JOB_EXCL_RENDER|WM_JOB_PROGRESS);
182         cj= MEM_callocN(sizeof(CompoJob), "compo job");
183         
184         /* customdata for preview thread */
185         cj->scene= CTX_data_scene(C);
186         cj->ntree= snode->nodetree;
187         
188         /* setup job */
189         WM_jobs_customdata(steve, cj, compo_freejob);
190         WM_jobs_timer(steve, 0.1, NC_SCENE, NC_SCENE|ND_COMPO_RESULT);
191         WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob, NULL);
192         
193         WM_jobs_start(CTX_wm_manager(C), steve);
194         
195 }
196
197 /* ***************************************** */
198
199 /* also checks for edited groups */
200 bNode *editnode_get_active(bNodeTree *ntree)
201 {
202         bNode *node;
203         
204         /* check for edited group */
205         for(node= ntree->nodes.first; node; node= node->next)
206                 if(node->flag & NODE_GROUP_EDIT)
207                         break;
208         if(node)
209                 return nodeGetActive((bNodeTree *)node->id);
210         else
211                 return nodeGetActive(ntree);
212 }
213
214 void snode_notify(bContext *C, SpaceNode *snode)
215 {
216         if(snode->treetype==NTREE_SHADER)
217                 WM_event_add_notifier(C, NC_MATERIAL|ND_NODES, snode->id);
218         else if(snode->treetype==NTREE_COMPOSIT)
219                 WM_event_add_notifier(C, NC_SCENE|ND_NODES, snode->id);
220         else if(snode->treetype==NTREE_TEXTURE)
221                 WM_event_add_notifier(C, NC_TEXTURE|ND_NODES, snode->id);
222 }
223
224 bNode *node_tree_get_editgroup(bNodeTree *nodetree)
225 {
226         bNode *gnode;
227         
228         /* get the groupnode */
229         for(gnode= nodetree->nodes.first; gnode; gnode= gnode->next)
230                 if(gnode->flag & NODE_GROUP_EDIT)
231                         break;
232         return gnode;
233 }
234
235 /* assumes nothing being done in ntree yet, sets the default in/out node */
236 /* called from shading buttons or header */
237 void ED_node_shader_default(Material *ma)
238 {
239         bNode *in, *out;
240         bNodeSocket *fromsock, *tosock;
241         
242         /* but lets check it anyway */
243         if(ma->nodetree) {
244                 if (G.f & G_DEBUG)
245                         printf("error in shader initialize\n");
246                 return;
247         }
248         
249         ma->nodetree= ntreeAddTree(NTREE_SHADER);
250         
251         out= nodeAddNodeType(ma->nodetree, SH_NODE_OUTPUT, NULL, NULL);
252         out->locx= 300.0f; out->locy= 300.0f;
253         
254         in= nodeAddNodeType(ma->nodetree, SH_NODE_MATERIAL, NULL, NULL);
255         in->locx= 10.0f; in->locy= 300.0f;
256         nodeSetActive(ma->nodetree, in);
257         
258         /* only a link from color to color */
259         fromsock= in->outputs.first;
260         tosock= out->inputs.first;
261         nodeAddLink(ma->nodetree, in, fromsock, out, tosock);
262         
263         ntreeSolveOrder(ma->nodetree);  /* needed for pointers */
264 }
265
266 /* assumes nothing being done in ntree yet, sets the default in/out node */
267 /* called from shading buttons or header */
268 void ED_node_composit_default(Scene *sce)
269 {
270         bNode *in, *out;
271         bNodeSocket *fromsock, *tosock;
272         
273         /* but lets check it anyway */
274         if(sce->nodetree) {
275                 if (G.f & G_DEBUG)
276                         printf("error in composite initialize\n");
277                 return;
278         }
279         
280         sce->nodetree= ntreeAddTree(NTREE_COMPOSIT);
281         
282         out= nodeAddNodeType(sce->nodetree, CMP_NODE_COMPOSITE, NULL, NULL);
283         out->locx= 300.0f; out->locy= 400.0f;
284         out->id= &sce->id;
285         
286         in= nodeAddNodeType(sce->nodetree, CMP_NODE_R_LAYERS, NULL, NULL);
287         in->locx= 10.0f; in->locy= 400.0f;
288         in->id= &sce->id;
289         nodeSetActive(sce->nodetree, in);
290         
291         /* links from color to color */
292         fromsock= in->outputs.first;
293         tosock= out->inputs.first;
294         nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
295         
296         ntreeSolveOrder(sce->nodetree); /* needed for pointers */
297         
298         // XXX ntreeCompositForceHidden(sce->nodetree);
299 }
300
301 /* assumes nothing being done in ntree yet, sets the default in/out node */
302 /* called from shading buttons or header */
303 void ED_node_texture_default(Tex *tx)
304 {
305         bNode *in, *out;
306         bNodeSocket *fromsock, *tosock;
307         
308         /* but lets check it anyway */
309         if(tx->nodetree) {
310                 if (G.f & G_DEBUG)
311                         printf("error in texture initialize\n");
312                 return;
313         }
314         
315         tx->nodetree= ntreeAddTree(NTREE_TEXTURE);
316         
317         out= nodeAddNodeType(tx->nodetree, TEX_NODE_OUTPUT, NULL, NULL);
318         out->locx= 300.0f; out->locy= 300.0f;
319         
320         in= nodeAddNodeType(tx->nodetree, TEX_NODE_CHECKER, NULL, NULL);
321         in->locx= 10.0f; in->locy= 300.0f;
322         nodeSetActive(tx->nodetree, in);
323         
324         fromsock= in->outputs.first;
325         tosock= out->inputs.first;
326         nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
327         
328         ntreeSolveOrder(tx->nodetree);  /* needed for pointers */
329 }
330
331 void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
332 {
333         bNode *node= NULL;
334         short idtype= GS(id->name);
335
336         if(idtype == ID_MA) {
337                 *ntree= ((Material*)id)->nodetree;
338                 if(treetype) *treetype= NTREE_SHADER;
339         }
340         else if(idtype == ID_SCE) {
341                 *ntree= ((Scene*)id)->nodetree;
342                 if(treetype) *treetype= NTREE_COMPOSIT;
343         }
344         else if(idtype == ID_TE) {
345                 *ntree= ((Tex*)id)->nodetree;
346                 if(treetype) *treetype= NTREE_TEXTURE;
347         }
348
349         /* find editable group */
350         if(edittree) {
351                 if(*ntree)
352                         for(node= (*ntree)->nodes.first; node; node= node->next)
353                                 if(node->flag & NODE_GROUP_EDIT)
354                                         break;
355                 
356                 if(node && node->id)
357                         *edittree= (bNodeTree *)node->id;
358                 else
359                         *edittree= *ntree;
360         }
361 }
362
363 /* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
364 void snode_set_context(SpaceNode *snode, Scene *scene)
365 {
366         Object *ob= OBACT;
367         
368         snode->nodetree= NULL;
369         snode->edittree= NULL;
370         snode->id= snode->from= NULL;
371         
372         if(snode->treetype==NTREE_SHADER) {
373                 /* need active object, or we allow pinning... */
374                 if(ob) {
375                         Material *ma= give_current_material(ob, ob->actcol);
376                         if(ma) {
377                                 snode->from= &ob->id;
378                                 snode->id= &ma->id;
379                         }
380                 }
381         }
382         else if(snode->treetype==NTREE_COMPOSIT) {
383                 snode->from= NULL;
384                 snode->id= &scene->id;
385                 
386                 /* bit clumsy but reliable way to see if we draw first time */
387                 if(snode->nodetree==NULL)
388                         ntreeCompositForceHidden(scene->nodetree, scene);
389         }
390         else if(snode->treetype==NTREE_TEXTURE) {
391                 Tex *tx= NULL;
392
393                 if(snode->texfrom==SNODE_TEX_OBJECT) {
394                         if(ob) {
395                                 tx= give_current_object_texture(ob);
396
397                                 if(ob->type == OB_LAMP)
398                                         snode->from= (ID*)ob->data;
399                                 else
400                                         snode->from= (ID*)give_current_material(ob, ob->actcol);
401
402                                 /* from is not set fully for material nodes, should be ID + Node then */
403                                 snode->id= &tx->id;
404                         }
405                 }
406                 else if(snode->texfrom==SNODE_TEX_WORLD) {
407                         tx= give_current_world_texture(scene->world);
408                         snode->from= (ID *)scene->world;
409                         snode->id= &tx->id;
410                 }
411                 else {
412                         Brush *brush= NULL;
413                         
414                         if(ob && (ob->mode & OB_MODE_SCULPT))
415                                 brush= paint_brush(&scene->toolsettings->sculpt->paint);
416                         else
417                                 brush= paint_brush(&scene->toolsettings->imapaint.paint);
418
419                         if (brush) {
420                                 snode->from= (ID *)brush;
421                                 tx= give_current_brush_texture(brush);
422                                 snode->id= &tx->id;
423                         }
424                 }
425         }
426
427         if(snode->id)
428                 node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
429 }
430
431 void node_set_active(SpaceNode *snode, bNode *node)
432 {
433         nodeSetActive(snode->edittree, node);
434         
435         if(node->type!=NODE_GROUP) {
436                 /* tree specific activate calls */
437                 if(snode->treetype==NTREE_SHADER) {
438                         /* when we select a material, active texture is cleared, for buttons */
439                         if(node->id && GS(node->id->name)==ID_MA)
440                                 nodeClearActiveID(snode->edittree, ID_TE);
441
442                         // XXX
443 #if 0
444                         if(node->id)
445                                 ; // XXX BIF_preview_changed(-1);       /* temp hack to force texture preview to update */
446                         
447                         // allqueue(REDRAWBUTSSHADING, 1);
448                         // allqueue(REDRAWIPO, 0);
449 #endif
450                 }
451                 else if(snode->treetype==NTREE_COMPOSIT) {
452                         Scene *scene= (Scene*)snode->id;
453
454                         /* make active viewer, currently only 1 supported... */
455                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
456                                 bNode *tnode;
457                                 int was_output= (node->flag & NODE_DO_OUTPUT);
458
459                                 for(tnode= snode->edittree->nodes.first; tnode; tnode= tnode->next)
460                                         if( ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
461                                                 tnode->flag &= ~NODE_DO_OUTPUT;
462                                 
463                                 node->flag |= NODE_DO_OUTPUT;
464                                 if(was_output==0) {
465                                         bNode *gnode;
466                                         
467                                         NodeTagChanged(snode->edittree, node);
468                                         
469                                         /* if inside group, tag entire group */
470                                         gnode= node_tree_get_editgroup(snode->nodetree);
471                                         if(gnode)
472                                                 NodeTagIDChanged(snode->nodetree, gnode->id);
473                                         
474                                         ED_node_changed_update(snode->id, node);
475                                 }
476                                 
477                                 /* addnode() doesnt link this yet... */
478                                 node->id= (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
479                         }
480                         else if(node->type==CMP_NODE_R_LAYERS) {
481                                 if(node->id==NULL || node->id==(ID *)scene) {
482                                         scene->r.actlay= node->custom1;
483                                 }
484                         }
485                 }
486                 else if(snode->treetype==NTREE_TEXTURE) {
487                         // XXX
488 #if 0
489                         if(node->id)
490                                 ; // XXX BIF_preview_changed(-1);
491                         // allqueue(REDRAWBUTSSHADING, 1);
492                         // allqueue(REDRAWIPO, 0);
493 #endif
494                 }
495         }
496 }
497
498 /* when links in groups change, inputs/outputs change, nodes added/deleted... */
499 void node_tree_verify_groups(bNodeTree *nodetree)
500 {
501         bNode *gnode;
502         
503         gnode= node_tree_get_editgroup(nodetree);
504         
505         /* does all materials */
506         if(gnode)
507                 nodeVerifyGroup((bNodeTree *)gnode->id);
508         
509 }
510
511 /* ***************** Edit Group operator ************* */
512
513 void snode_make_group_editable(SpaceNode *snode, bNode *gnode)
514 {
515         bNode *node;
516         
517         /* make sure nothing has group editing on */
518         for(node= snode->nodetree->nodes.first; node; node= node->next)
519                 node->flag &= ~NODE_GROUP_EDIT;
520         
521         if(gnode==NULL) {
522                 /* with NULL argument we do a toggle */
523                 if(snode->edittree==snode->nodetree)
524                         gnode= nodeGetActive(snode->nodetree);
525         }
526         
527         if(gnode && gnode->type==NODE_GROUP && gnode->id) {
528                 if(gnode->id->lib)
529                         ntreeMakeLocal((bNodeTree *)gnode->id);
530
531                 gnode->flag |= NODE_GROUP_EDIT;
532                 snode->edittree= (bNodeTree *)gnode->id;
533                 
534                 /* deselect all other nodes, so we can also do grabbing of entire subtree */
535                 for(node= snode->nodetree->nodes.first; node; node= node->next)
536                         node->flag &= ~SELECT;
537                 gnode->flag |= SELECT;
538                 
539         }
540         else 
541                 snode->edittree= snode->nodetree;
542         
543         ntreeSolveOrder(snode->nodetree);
544 }
545
546 static int node_group_edit_exec(bContext *C, wmOperator *op)
547 {
548         SpaceNode *snode = CTX_wm_space_node(C);
549         bNode *gnode;
550
551         gnode= nodeGetActive(snode->edittree);
552         snode_make_group_editable(snode, gnode);
553
554         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
555
556         return OPERATOR_FINISHED;
557 }
558
559 static int node_group_edit_invoke(bContext *C, wmOperator *op, wmEvent *event)
560 {
561         SpaceNode *snode = CTX_wm_space_node(C);
562         bNode *gnode;
563
564         gnode= nodeGetActive(snode->edittree);
565         if(gnode && gnode->type==NODE_GROUP && gnode->id && gnode->id->lib) {
566                 uiPupMenuOkee(C, op->type->idname, "Make group local?");
567                 return OPERATOR_CANCELLED;
568         }
569
570         return node_group_edit_exec(C, op);
571 }
572
573 void NODE_OT_group_edit(wmOperatorType *ot)
574 {
575         /* identifiers */
576         ot->name = "Edit Group";
577         ot->description = "Edit node group";
578         ot->idname = "NODE_OT_group_edit";
579         
580         /* api callbacks */
581         ot->invoke = node_group_edit_invoke;
582         ot->exec = node_group_edit_exec;
583         ot->poll = ED_operator_node_active;
584         
585         /* flags */
586         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
587 }
588
589 /* ******************** Ungroup operator ********************** */
590
591 static int node_group_ungroup_exec(bContext *C, wmOperator *op)
592 {
593         SpaceNode *snode = CTX_wm_space_node(C);
594         bNode *gnode;
595
596         /* are we inside of a group? */
597         gnode= node_tree_get_editgroup(snode->nodetree);
598         if(gnode)
599                 snode_make_group_editable(snode, NULL);
600         
601         gnode= nodeGetActive(snode->edittree);
602         if(gnode==NULL)
603                 return OPERATOR_CANCELLED;
604         
605         if(gnode->type!=NODE_GROUP) {
606                 BKE_report(op->reports, RPT_ERROR, "Not a group");
607                 return OPERATOR_CANCELLED;
608         }
609         else if(!nodeGroupUnGroup(snode->edittree, gnode)) {
610                 BKE_report(op->reports, RPT_ERROR, "Can't ungroup");
611                 return OPERATOR_CANCELLED;
612         }
613
614         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
615
616         return OPERATOR_FINISHED;
617 }
618
619 void NODE_OT_group_ungroup(wmOperatorType *ot)
620 {
621         /* identifiers */
622         ot->name = "Ungroup";
623         ot->description = "Ungroup selected nodes";
624         ot->idname = "NODE_OT_group_ungroup";
625         
626         /* api callbacks */
627         ot->exec = node_group_ungroup_exec;
628         ot->poll = ED_operator_node_active;
629         
630         /* flags */
631         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
632 }
633
634 /* ************************** Node generic ************** */
635
636 /* allows to walk the list in order of visibility */
637 bNode *next_node(bNodeTree *ntree)
638 {
639         static bNode *current=NULL, *last= NULL;
640         
641         if(ntree) {
642                 /* set current to the first selected node */
643                 for(current= ntree->nodes.last; current; current= current->prev)
644                         if(current->flag & NODE_SELECT)
645                                 break;
646                 
647                 /* set last to the first unselected node */
648                 for(last= ntree->nodes.last; last; last= last->prev)
649                         if((last->flag & NODE_SELECT)==0)
650                                 break;
651                 
652                 if(current==NULL)
653                         current= last;
654                 
655                 return NULL;
656         }
657         /* no nodes, or we are ready */
658         if(current==NULL)
659                 return NULL;
660         
661         /* now we walk the list backwards, but we always return current */
662         if(current->flag & NODE_SELECT) {
663                 bNode *node= current;
664                 
665                 /* find previous selected */
666                 current= current->prev;
667                 while(current && (current->flag & NODE_SELECT)==0)
668                         current= current->prev;
669                 
670                 /* find first unselected */
671                 if(current==NULL)
672                         current= last;
673                 
674                 return node;
675         }
676         else {
677                 bNode *node= current;
678                 
679                 /* find previous unselected */
680                 current= current->prev;
681                 while(current && (current->flag & NODE_SELECT))
682                         current= current->prev;
683                 
684                 return node;
685         }
686         
687         return NULL;
688 }
689
690 /* is rct in visible part of node? */
691 static bNode *visible_node(SpaceNode *snode, rctf *rct)
692 {
693         bNode *tnode;
694         
695         for(next_node(snode->edittree); (tnode=next_node(NULL));) {
696                 if(BLI_isect_rctf(&tnode->totr, rct, NULL))
697                         break;
698         }
699         return tnode;
700 }
701
702 /* **************************** */
703
704 typedef struct NodeViewMove {
705         short mvalo[2];
706         int xmin, ymin, xmax, ymax;
707 } NodeViewMove;
708
709 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
710 {
711         SpaceNode *snode= CTX_wm_space_node(C);
712         ARegion *ar= CTX_wm_region(C);
713         NodeViewMove *nvm= op->customdata;
714
715         switch (event->type) {
716                 case MOUSEMOVE:
717                         
718                         snode->xof -= (nvm->mvalo[0]-event->mval[0]);
719                         snode->yof -= (nvm->mvalo[1]-event->mval[1]);
720                         nvm->mvalo[0]= event->mval[0];
721                         nvm->mvalo[1]= event->mval[1];
722                         
723                         /* prevent dragging image outside of the window and losing it! */
724                         CLAMP(snode->xof, nvm->xmin, nvm->xmax);
725                         CLAMP(snode->yof, nvm->ymin, nvm->ymax);
726                         
727                         ED_region_tag_redraw(ar);
728                         
729                         break;
730                         
731                 case LEFTMOUSE:
732                 case MIDDLEMOUSE:
733                 case RIGHTMOUSE:
734                         
735                         MEM_freeN(nvm);
736                         op->customdata= NULL;
737                         
738                         return OPERATOR_FINISHED;
739         }
740         
741         return OPERATOR_RUNNING_MODAL;
742 }
743
744 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
745 {
746         ARegion *ar= CTX_wm_region(C);
747         NodeViewMove *nvm;
748         Image *ima;
749         ImBuf *ibuf;
750         int pad= 10;
751         
752         ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
753         ibuf= BKE_image_get_ibuf(ima, NULL);
754         
755         if(ibuf == NULL)
756                 return OPERATOR_CANCELLED;
757
758         nvm= MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
759         op->customdata= nvm;
760         nvm->mvalo[0]= event->mval[0];
761         nvm->mvalo[1]= event->mval[1];
762
763         nvm->xmin = -(ar->winx/2) - ibuf->x/2 + pad;
764         nvm->xmax = ar->winx/2 + ibuf->x/2 - pad;
765         nvm->ymin = -(ar->winy/2) - ibuf->y/2 + pad;
766         nvm->ymax = ar->winy/2 + ibuf->y/2 - pad;
767         
768         /* add modal handler */
769         WM_event_add_modal_handler(C, op);
770         
771         return OPERATOR_RUNNING_MODAL;
772 }
773
774
775 void NODE_OT_backimage_move(wmOperatorType *ot)
776 {
777         /* identifiers */
778         ot->name= "Background Image Move";
779         ot->idname= "NODE_OT_backimage_move";
780         
781         /* api callbacks */
782         ot->invoke= snode_bg_viewmove_invoke;
783         ot->modal= snode_bg_viewmove_modal;
784         ot->poll= ED_operator_node_active;
785         
786         /* flags */
787         ot->flag= OPTYPE_BLOCKING;
788 }
789
790 static int backimage_zoom(bContext *C, wmOperator *op)
791 {
792         SpaceNode *snode= CTX_wm_space_node(C);
793         ARegion *ar= CTX_wm_region(C);
794         float fac= RNA_float_get(op->ptr, "factor");
795
796         snode->zoom *= fac;
797         ED_region_tag_redraw(ar);
798
799         return OPERATOR_FINISHED;
800 }
801
802
803 void NODE_OT_backimage_zoom(wmOperatorType *ot)
804 {
805         
806         /* identifiers */
807         ot->name= "Background Image Zoom";
808         ot->idname= "NODE_OT_backimage_zoom";
809         
810         /* api callbacks */
811         ot->exec= backimage_zoom;
812         ot->poll= ED_operator_node_active;
813         
814         /* flags */
815         ot->flag= OPTYPE_BLOCKING;
816
817         /* internal */
818         RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
819 }
820
821
822 /* ********************** size widget operator ******************** */
823
824 typedef struct NodeSizeWidget {
825         float mxstart;
826         float oldwidth;
827 } NodeSizeWidget;
828
829 static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
830 {
831         SpaceNode *snode= CTX_wm_space_node(C);
832         ARegion *ar= CTX_wm_region(C);
833         bNode *node= editnode_get_active(snode->edittree);
834         NodeSizeWidget *nsw= op->customdata;
835         float mx, my;
836         
837         switch (event->type) {
838                 case MOUSEMOVE:
839                         
840                         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
841                                                                          &mx, &my);
842                         
843                         if (node) {
844                                 if(node->flag & NODE_HIDDEN) {
845                                         node->miniwidth= nsw->oldwidth + mx - nsw->mxstart;
846                                         CLAMP(node->miniwidth, 0.0f, 100.0f);
847                                 }
848                                 else {
849                                         node->width= nsw->oldwidth + mx - nsw->mxstart;
850                                         CLAMP(node->width, node->typeinfo->minwidth, node->typeinfo->maxwidth);
851                                 }
852                         }
853                                 
854                         ED_region_tag_redraw(ar);
855
856                         break;
857                         
858                 case LEFTMOUSE:
859                 case MIDDLEMOUSE:
860                 case RIGHTMOUSE:
861                         
862                         MEM_freeN(nsw);
863                         op->customdata= NULL;
864                         
865                         return OPERATOR_FINISHED;
866         }
867         
868         return OPERATOR_RUNNING_MODAL;
869 }
870
871 static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
872 {
873         SpaceNode *snode= CTX_wm_space_node(C);
874         ARegion *ar= CTX_wm_region(C);
875         bNode *node= editnode_get_active(snode->edittree);
876         
877         if(node) {
878                 rctf totr;
879                 
880                 /* convert mouse coordinates to v2d space */
881                 UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
882                                                                  &snode->mx, &snode->my);
883                 
884                 /* rect we're interested in is just the bottom right corner */
885                 totr= node->totr;
886                 totr.xmin= totr.xmax-10.0f;
887                 totr.ymax= totr.ymin+10.0f;
888                 
889                 if(BLI_in_rctf(&totr, snode->mx, snode->my)) {
890                         NodeSizeWidget *nsw= MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
891                         
892                         op->customdata= nsw;
893                         nsw->mxstart= snode->mx;
894                         
895                         /* store old */
896                         if(node->flag & NODE_HIDDEN)
897                                 nsw->oldwidth= node->miniwidth;
898                         else
899                                 nsw->oldwidth= node->width;
900                         
901                         /* add modal handler */
902                         WM_event_add_modal_handler(C, op);
903
904                         return OPERATOR_RUNNING_MODAL;
905                 }
906         }
907         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
908 }
909
910 void NODE_OT_resize(wmOperatorType *ot)
911 {
912         /* identifiers */
913         ot->name= "Resize Node";
914         ot->idname= "NODE_OT_resize";
915         
916         /* api callbacks */
917         ot->invoke= node_resize_invoke;
918         ot->modal= node_resize_modal;
919         ot->poll= ED_operator_node_active;
920         
921         /* flags */
922         ot->flag= OPTYPE_BLOCKING;
923 }
924
925 /* ********************** select ******************** */
926
927
928 /* no undo here! */
929 void node_deselectall(SpaceNode *snode)
930 {
931         bNode *node;
932         
933         for(node= snode->edittree->nodes.first; node; node= node->next)
934                 node->flag &= ~SELECT;
935 }
936
937 /* return 1 if we need redraw otherwise zero. */
938 int node_select_same_type(SpaceNode *snode)
939 {
940         bNode *nac, *p;
941         int redraw;
942
943         /* search for the active node. */
944         for (nac= snode->edittree->nodes.first; nac; nac= nac->next) {
945                 if (nac->flag & SELECT)
946                         break;
947         }
948
949         /* no active node, return. */
950         if (!nac)
951                 return(0);
952
953         redraw= 0;
954         for (p= snode->edittree->nodes.first; p; p= p->next) {
955                 if (p->type != nac->type && p->flag & SELECT) {
956                         /* if it's selected but different type, unselect */
957                         redraw= 1;
958                         p->flag &= ~SELECT;
959                 }
960                 else if (p->type == nac->type && (!(p->flag & SELECT))) {
961                         /* if it's the same type and is not selected, select! */
962                         redraw= 1;
963                         p->flag |= SELECT;
964                 }
965         }
966         return(redraw);
967 }
968
969 /* return 1 if we need redraw, otherwise zero.
970  * dir can be 0 == next or 0 != prev.
971  */
972 int node_select_same_type_np(SpaceNode *snode, int dir)
973 {
974         bNode *nac, *p;
975
976         /* search the active one. */
977         for (nac= snode->edittree->nodes.first; nac; nac= nac->next) {
978                 if (nac->flag & SELECT)
979                         break;
980         }
981
982         /* no active node, return. */
983         if (!nac)
984                 return(0);
985
986         if (dir == 0)
987                 p= nac->next;
988         else
989                 p= nac->prev;
990
991         while (p) {
992                 /* Now search the next with the same type. */
993                 if (p->type == nac->type)
994                         break;
995
996                 if (dir == 0)
997                         p= p->next;
998                 else
999                         p= p->prev;
1000         }
1001
1002         if (p) {
1003                 node_deselectall(snode);
1004                 p->flag |= SELECT;
1005                 return(1);
1006         }
1007         return(0);
1008 }
1009
1010 int node_has_hidden_sockets(bNode *node)
1011 {
1012         bNodeSocket *sock;
1013         
1014         for(sock= node->inputs.first; sock; sock= sock->next)
1015                 if(sock->flag & SOCK_HIDDEN)
1016                         return 1;
1017         for(sock= node->outputs.first; sock; sock= sock->next)
1018                 if(sock->flag & SOCK_HIDDEN)
1019                         return 1;
1020         return 0;
1021 }
1022
1023 static void node_link_viewer(SpaceNode *snode, bNode *tonode)
1024 {
1025         bNode *node;
1026
1027         /* context check */
1028         if(tonode==NULL || tonode->outputs.first==NULL)
1029                 return;
1030         if( ELEM(tonode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
1031                 return;
1032         
1033         /* get viewer */
1034         for(node= snode->edittree->nodes.first; node; node= node->next)
1035                 if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
1036                         if(node->flag & NODE_DO_OUTPUT)
1037                                 break;
1038         /* no viewer, we make one active */
1039         if(node==NULL) {
1040                 for(node= snode->edittree->nodes.first; node; node= node->next) {
1041                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
1042                                 node->flag |= NODE_DO_OUTPUT;
1043                                 break;
1044                         }
1045                 }
1046         }
1047                 
1048         if(node) {
1049                 bNodeLink *link;
1050                 bNodeSocket *sock= NULL;
1051
1052                 /* try to find an already connected socket to cycle to the next */
1053                 for(link= snode->edittree->links.first; link; link= link->next)
1054                         if(link->tonode==node && link->fromnode==tonode)
1055                                 if(link->tosock==node->inputs.first)
1056                                         break;
1057
1058                 if(link) {
1059                         /* unlink existing connection */
1060                         sock= link->fromsock;
1061                         nodeRemLink(snode->edittree, link);
1062
1063                         /* find a socket after the previously connected socket */
1064                         for(sock=sock->next; sock; sock= sock->next)
1065                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
1066                                         break;
1067                 }
1068
1069                 /* find a socket starting from the first socket */
1070                 if(!sock) {
1071                         for(sock= tonode->outputs.first; sock; sock= sock->next)
1072                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
1073                                         break;
1074                 }
1075                 
1076                 if(sock) {
1077                         /* get link to viewer */
1078                         for(link= snode->edittree->links.first; link; link= link->next)
1079                                 if(link->tonode==node && link->tosock==node->inputs.first)
1080                                         break;
1081                         
1082                         if(link==NULL) {
1083                                 nodeAddLink(snode->edittree, tonode, sock, node, node->inputs.first);
1084                         }
1085                         else {
1086                                 link->fromnode= tonode;
1087                                 link->fromsock= sock;
1088                         }
1089                         ntreeSolveOrder(snode->edittree);
1090                         NodeTagChanged(snode->edittree, node);
1091                 }
1092         }
1093 }
1094
1095
1096 static int node_active_link_viewer(bContext *C, wmOperator *op)
1097 {
1098         SpaceNode *snode= CTX_wm_space_node(C);
1099         bNode *node;
1100         
1101         
1102         node= editnode_get_active(snode->edittree);
1103         
1104         if(node) {
1105                 node_link_viewer(snode, node);
1106                 snode_notify(C, snode);
1107         }
1108         return OPERATOR_FINISHED;
1109 }
1110
1111
1112
1113 void NODE_OT_link_viewer(wmOperatorType *ot)
1114 {
1115         /* identifiers */
1116         ot->name= "Link to Viewer Node";
1117         ot->description = "Link to Viewer Node";
1118         ot->idname= "NODE_OT_link_viewer";
1119         
1120         /* api callbacks */
1121         ot->exec= node_active_link_viewer;
1122         ot->poll= ED_operator_node_active;
1123         
1124         /* flags */
1125         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1126 }
1127
1128
1129
1130 /* return 0, nothing done */
1131 /*static*/ int node_mouse_groupheader(SpaceNode *snode)
1132 {
1133         bNode *gnode;
1134         float mx=0, my=0;
1135 // XXX  short mval[2];
1136         
1137         gnode= node_tree_get_editgroup(snode->nodetree);
1138         if(gnode==NULL) return 0;
1139         
1140 // XXX  getmouseco_areawin(mval);
1141 // XXX  areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1142         
1143         /* click in header or outside? */
1144         if(BLI_in_rctf(&gnode->totr, mx, my)==0) {
1145                 rctf rect= gnode->totr;
1146                 
1147                 rect.ymax += NODE_DY;
1148                 if(BLI_in_rctf(&rect, mx, my)==0)
1149                         snode_make_group_editable(snode, NULL); /* toggles, so exits editmode */
1150 //              else
1151 // XXX                  transform_nodes(snode->nodetree, 'g', "Move group");
1152                 
1153                 return 1;
1154         }
1155         return 0;
1156 }
1157
1158 /* checks snode->mouse position, and returns found node/socket */
1159 /* type is SOCK_IN and/or SOCK_OUT */
1160 static int find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
1161 {
1162         bNode *node;
1163         bNodeSocket *sock;
1164         rctf rect;
1165         
1166         /* check if we click in a socket */
1167         for(node= snode->edittree->nodes.first; node; node= node->next) {
1168                 
1169                 rect.xmin = snode->mx - (NODE_SOCKSIZE+4);
1170                 rect.ymin = snode->my - (NODE_SOCKSIZE+4);
1171                 rect.xmax = snode->mx + (NODE_SOCKSIZE+4);
1172                 rect.ymax = snode->my + (NODE_SOCKSIZE+4);
1173                 
1174                 if (!(node->flag & NODE_HIDDEN)) {
1175                         /* extra padding inside and out - allow dragging on the text areas too */
1176                         if (in_out == SOCK_IN) {
1177                                 rect.xmax += NODE_SOCKSIZE;
1178                                 rect.xmin -= NODE_SOCKSIZE*4;
1179                         } else if (in_out == SOCK_OUT) {
1180                                 rect.xmax += NODE_SOCKSIZE*4;
1181                                 rect.xmin -= NODE_SOCKSIZE;
1182                         }
1183                 }
1184                 
1185                 if(in_out & SOCK_IN) {
1186                         for(sock= node->inputs.first; sock; sock= sock->next) {
1187                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1188                                         if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1189                                                 if(node == visible_node(snode, &rect)) {
1190                                                         *nodep= node;
1191                                                         *sockp= sock;
1192                                                         return 1;
1193                                                 }
1194                                         }
1195                                 }
1196                         }
1197                 }
1198                 if(in_out & SOCK_OUT) {
1199                         for(sock= node->outputs.first; sock; sock= sock->next) {
1200                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1201                                         if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1202                                                 if(node == visible_node(snode, &rect)) {
1203                                                         *nodep= node;
1204                                                         *sockp= sock;
1205                                                         return 1;
1206                                                 }
1207                                         }
1208                                 }
1209                         }
1210                 }
1211         }
1212         return 0;
1213 }
1214
1215 static int node_socket_hilights(SpaceNode *snode, int in_out)
1216 {
1217         bNode *node;
1218         bNodeSocket *sock, *tsock, *socksel= NULL;
1219         short redraw= 0;
1220         
1221         if(snode->edittree==NULL) return 0;
1222         
1223         /* deselect sockets */
1224         for(node= snode->edittree->nodes.first; node; node= node->next) {
1225                 for(sock= node->inputs.first; sock; sock= sock->next) {
1226                         if(sock->flag & SELECT) {
1227                                 sock->flag &= ~SELECT;
1228                                 redraw++;
1229                                 socksel= sock;
1230                         }
1231                 }
1232                 for(sock= node->outputs.first; sock; sock= sock->next) {
1233                         if(sock->flag & SELECT) {
1234                                 sock->flag &= ~SELECT;
1235                                 redraw++;
1236                                 socksel= sock;
1237                         }
1238                 }
1239         }
1240         
1241         // XXX mousepos should be set here!
1242         
1243         if(find_indicated_socket(snode, &node, &tsock, in_out)) {
1244                 tsock->flag |= SELECT;
1245                 if(redraw==1 && tsock==socksel) redraw= 0;
1246                 else redraw= 1;
1247         }
1248         
1249         return redraw;
1250 }
1251
1252 /* ****************** Add *********************** */
1253
1254
1255 typedef struct bNodeListItem {
1256         struct bNodeListItem *next, *prev;
1257         struct bNode *node;     
1258 } bNodeListItem;
1259
1260 int sort_nodes_locx(void *a, void *b)
1261 {
1262         bNodeListItem *nli1 = (bNodeListItem *)a;
1263         bNodeListItem *nli2 = (bNodeListItem *)b;
1264         bNode *node1 = nli1->node;
1265         bNode *node2 = nli2->node;
1266         
1267         if (node1->locx > node2->locx)
1268                 return 1;
1269         else 
1270                 return 0;
1271 }
1272
1273 static int socket_is_available(bNodeTree *ntree, bNodeSocket *sock, int allow_used)
1274 {
1275         if (sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))
1276                 return 0;
1277         
1278         if (!allow_used) {
1279                 if (nodeCountSocketLinks(ntree, sock) > 0)
1280                         return 0;
1281         }
1282         return 1;
1283 }
1284
1285 static bNodeSocket *best_socket_output(bNodeTree *ntree, bNode *node, bNodeSocket *sock_target, int allow_multiple)
1286 {
1287         bNodeSocket *sock;
1288         
1289         /* first try to find a socket with a matching name */
1290         for (sock=node->outputs.first; sock; sock=sock->next) {
1291
1292                 if (!socket_is_available(ntree, sock, allow_multiple))
1293                         continue;
1294
1295                 /* check for same types */
1296                 if (sock->type == sock_target->type) {
1297                         if (strcmp(sock->name, sock_target->name)==0)
1298                                 return sock;
1299                 }
1300         }
1301         
1302         /* otherwise settle for the first available socket of the right type */
1303         for (sock=node->outputs.first; sock; sock=sock->next) {
1304
1305                 if (!socket_is_available(ntree, sock, allow_multiple))
1306                         continue;
1307                 
1308                 /* check for same types */
1309                 if (sock->type == sock_target->type) {
1310                         return sock;
1311                 }
1312         }
1313         
1314         return NULL;
1315 }
1316
1317 /* this is a bit complicated, but designed to prioritise finding 
1318  * sockets of higher types, such as image, first */
1319 static bNodeSocket *best_socket_input(bNodeTree *ntree, bNode *node, int num, int replace)
1320 {
1321         bNodeSocket *sock;
1322         int socktype, maxtype=0;
1323         int a = 0;
1324         
1325         for (sock=node->inputs.first; sock; sock=sock->next) {
1326                 maxtype = MAX2(sock->type, maxtype);
1327         }
1328         
1329         /* find sockets of higher 'types' first (i.e. image) */
1330         for (socktype=maxtype; socktype >= 0; socktype--) {
1331                 for (sock=node->inputs.first; sock; sock=sock->next) {
1332                         
1333                         if (!socket_is_available(ntree, sock, replace)) {
1334                                 a++;
1335                                 continue;
1336                         }
1337                                 
1338                         if (sock->type == socktype) {
1339                                 /* increment to make sure we don't keep finding 
1340                                  * the same socket on every attempt running this function */
1341                                 a++;
1342                                 if (a > num)
1343                                         return sock;
1344                         }
1345                 }
1346         }
1347         
1348         return NULL;
1349 }
1350
1351 void snode_autoconnect(SpaceNode *snode, int allow_multiple, int replace)
1352 {
1353         ListBase *nodelist = MEM_callocN(sizeof(ListBase), "items_list");
1354         bNodeListItem *nli;
1355         bNode *node;
1356         int i;
1357         
1358         for(node= snode->edittree->nodes.first; node; node= node->next) {
1359                 if(node->flag & NODE_SELECT) {
1360                         nli = MEM_mallocN(sizeof(bNodeListItem), "temporary node list item");
1361                         nli->node = node;
1362                         BLI_addtail(nodelist, nli);
1363                 }
1364         }
1365         
1366         /* sort nodes left to right */
1367         BLI_sortlist(nodelist, sort_nodes_locx);
1368         
1369         for (nli=nodelist->first; nli; nli=nli->next) {
1370                 bNode *node_fr, *node_to;
1371                 bNodeSocket *sock_fr, *sock_to;
1372                 
1373                 if (nli->next == NULL) break;
1374                 
1375                 node_fr = nli->node;
1376                 node_to = nli->next->node;
1377                 
1378                 /* check over input sockets first */
1379                 for (i=0; i<BLI_countlist(&node_to->inputs); i++) {
1380                         
1381                         /* find the best guess input socket */
1382                         sock_to = best_socket_input(snode->edittree, node_to, i, replace);
1383                         if (!sock_to) continue;
1384                         
1385                         /* check for an appropriate output socket to connect from */
1386                         sock_fr = best_socket_output(snode->edittree, node_fr, sock_to, allow_multiple);
1387                         if (!sock_fr) continue;
1388                         
1389                         /* then we can connect */
1390                         if (replace)
1391                                 nodeRemSocketLinks(snode->edittree, sock_to);
1392                         nodeAddLink(snode->edittree, node_fr, sock_fr, node_to, sock_to);
1393                         NodeTagChanged(snode->edittree, node_to);
1394                         break;
1395                 }
1396         }
1397         
1398         ntreeSolveOrder(snode->edittree);
1399         
1400         BLI_freelistN(nodelist);
1401         MEM_freeN(nodelist);
1402 }
1403
1404 /* can be called from menus too, but they should do own undopush and redraws */
1405 bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float locy)
1406 {
1407         bNode *node= NULL, *gnode;
1408         
1409         node_deselectall(snode);
1410         
1411         if(type>=NODE_DYNAMIC_MENU) {
1412                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1413         }
1414         else if(type>=NODE_GROUP_MENU) {
1415                 if(snode->edittree!=snode->nodetree) {
1416                         // XXX error("Can not add a Group in a Group");
1417                         return NULL;
1418                 }
1419                 else {
1420                         bNodeTree *ngroup= BLI_findlink(&G.main->nodetree, type-NODE_GROUP_MENU);
1421                         if(ngroup)
1422                                 node= nodeAddNodeType(snode->edittree, NODE_GROUP, ngroup, NULL);
1423                 }
1424         }
1425         else
1426                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1427         
1428         /* generics */
1429         if(node) {
1430                 node->locx= locx;
1431                 node->locy= locy + 60.0f;               // arbitrary.. so its visible
1432                 node->flag |= SELECT;
1433                 
1434                 gnode= node_tree_get_editgroup(snode->nodetree);
1435                 if(gnode) {
1436                         node->locx -= gnode->locx;
1437                         node->locy -= gnode->locy;
1438                 }
1439
1440                 node_tree_verify_groups(snode->nodetree);
1441                 node_set_active(snode, node);
1442                 
1443                 if(snode->nodetree->type==NTREE_COMPOSIT) {
1444                         if(ELEM4(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS, CMP_NODE_OUTPUT_FILE))
1445                                 node->id = &scene->id;
1446                         
1447                         ntreeCompositForceHidden(snode->edittree, scene);
1448                 }
1449                         
1450                 if(node->id)
1451                         id_us_plus(node->id);
1452                         
1453                 NodeTagChanged(snode->edittree, node);
1454         }
1455         
1456         if(snode->nodetree->type==NTREE_TEXTURE) {
1457                 ntreeTexCheckCyclics(snode->edittree);
1458         }
1459         
1460         return node;
1461 }
1462
1463 /* ****************** Duplicate *********************** */
1464
1465 static int node_duplicate_exec(bContext *C, wmOperator *op)
1466 {
1467         SpaceNode *snode= CTX_wm_space_node(C);
1468         
1469         ntreeCopyTree(snode->edittree, 1);      /* 1 == internally selected nodes */
1470         
1471         ntreeSolveOrder(snode->edittree);
1472         node_tree_verify_groups(snode->nodetree);
1473         snode_notify(C, snode);
1474
1475         return OPERATOR_FINISHED;
1476 }
1477
1478 void NODE_OT_duplicate(wmOperatorType *ot)
1479 {
1480         /* identifiers */
1481         ot->name= "Duplicate Nodes";
1482         ot->description = "Duplicate the nodes";
1483         ot->idname= "NODE_OT_duplicate";
1484         
1485         /* api callbacks */
1486         ot->exec= node_duplicate_exec;
1487         ot->poll= ED_operator_node_active;
1488         
1489         /* flags */
1490         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1491 }
1492
1493 /* *************************** add link op ******************** */
1494
1495 /* temp data to pass on to modal */
1496 typedef struct NodeLinkDrag
1497 {
1498         bNode *node;
1499         bNodeSocket *sock;
1500         bNodeLink *link;
1501         int in_out;
1502 } NodeLinkDrag;
1503
1504 static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeLink *link)
1505 {
1506         bNodeLink *tlink;
1507         bNodeSocket *sock;
1508         
1509         if(tsock && nodeCountSocketLinks(snode->edittree, link->tosock) > tsock->limit) {
1510                 
1511                 for(tlink= snode->edittree->links.first; tlink; tlink= tlink->next) {
1512                         if(link!=tlink && tlink->tosock==link->tosock)
1513                                 break;
1514                 }
1515                 if(tlink) {
1516                         /* is there a free input socket with same type? */
1517                         for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
1518                                 if(sock->type==tlink->fromsock->type)
1519                                         if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
1520                                                 break;
1521                         }
1522                         if(sock) {
1523                                 tlink->tosock= sock;
1524                                 sock->flag &= ~SOCK_HIDDEN;
1525                         }
1526                         else {
1527                                 nodeRemLink(snode->edittree, tlink);
1528                         }
1529                 }
1530         }
1531 }
1532
1533 /* loop that adds a nodelink, called by function below  */
1534 /* in_out = starting socket */
1535 static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
1536 {
1537         SpaceNode *snode= CTX_wm_space_node(C);
1538         ARegion *ar= CTX_wm_region(C);
1539         NodeLinkDrag *nldrag= op->customdata;
1540         bNode *tnode, *node;
1541         bNodeSocket *tsock= NULL, *sock;
1542         bNodeLink *link;
1543         int in_out;
1544
1545         in_out= nldrag->in_out;
1546         node= nldrag->node;
1547         sock= nldrag->sock;
1548         link= nldrag->link;
1549         
1550         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1551                                                          &snode->mx, &snode->my);
1552
1553         switch (event->type) {
1554                 case MOUSEMOVE:
1555                         
1556                         if(in_out==SOCK_OUT) {
1557                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_IN)) {
1558                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
1559                                                 if(tnode!=node  && link->tonode!=tnode && link->tosock!= tsock) {
1560                                                         link->tonode= tnode;
1561                                                         link->tosock= tsock;
1562                                                         ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
1563                                                 }
1564                                         }
1565                                 }
1566                                 else {
1567                                         link->tonode= NULL;
1568                                         link->tosock= NULL;
1569                                 }
1570                         }
1571                         else {
1572                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_OUT)) {
1573                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
1574                                                 if(nodeCountSocketLinks(snode->edittree, tsock) < tsock->limit) {
1575                                                         if(tnode!=node && link->fromnode!=tnode && link->fromsock!= tsock) {
1576                                                                 link->fromnode= tnode;
1577                                                                 link->fromsock= tsock;
1578                                                                 ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
1579                                                         }
1580                                                 }
1581                                         }
1582                                 }
1583                                 else {
1584                                         link->fromnode= NULL;
1585                                         link->fromsock= NULL;
1586                                 }
1587                         }
1588                         /* hilight target sockets only */
1589                         node_socket_hilights(snode, in_out==SOCK_OUT?SOCK_IN:SOCK_OUT);
1590                         ED_region_tag_redraw(ar);
1591                         break;
1592                         
1593                 case LEFTMOUSE:
1594                 case RIGHTMOUSE:
1595                 case MIDDLEMOUSE:
1596         
1597                         /* remove link? */
1598                         if(link->tonode==NULL || link->fromnode==NULL) {
1599                                 nodeRemLink(snode->edittree, link);
1600                         }
1601                         else {
1602                                 /* send changed events for original tonode and new */
1603                                 if(link->tonode) 
1604                                         NodeTagChanged(snode->edittree, link->tonode);
1605                                 
1606                                 /* we might need to remove a link */
1607                                 if(in_out==SOCK_OUT) node_remove_extra_links(snode, link->tosock, link);
1608                         }
1609                         
1610                         ntreeSolveOrder(snode->edittree);
1611                         node_tree_verify_groups(snode->nodetree);
1612                         snode_notify(C, snode);
1613                         
1614                         MEM_freeN(op->customdata);
1615                         op->customdata= NULL;
1616                         
1617                         return OPERATOR_FINISHED;
1618         }
1619         
1620         return OPERATOR_RUNNING_MODAL;
1621 }
1622
1623 /* return 1 when socket clicked */
1624 static int node_link_init(SpaceNode *snode, NodeLinkDrag *nldrag)
1625 {
1626         bNodeLink *link;
1627         
1628         /* output indicated? */
1629         if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_OUT)) {
1630                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
1631                         return SOCK_OUT;
1632                 else {
1633                         /* find if we break a link */
1634                         for(link= snode->edittree->links.first; link; link= link->next) {
1635                                 if(link->fromsock==nldrag->sock)
1636                                         break;
1637                         }
1638                         if(link) {
1639                                 nldrag->node= link->tonode;
1640                                 nldrag->sock= link->tosock;
1641                                 nodeRemLink(snode->edittree, link);
1642                                 return SOCK_IN;
1643                         }
1644                 }
1645         }
1646         /* or an input? */
1647         else if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_IN)) {
1648                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
1649                         return SOCK_IN;
1650                 else {
1651                         /* find if we break a link */
1652                         for(link= snode->edittree->links.first; link; link= link->next) {
1653                                 if(link->tosock==nldrag->sock)
1654                                         break;
1655                         }
1656                         if(link) {
1657                                 /* send changed event to original tonode */
1658                                 if(link->tonode) 
1659                                         NodeTagChanged(snode->edittree, link->tonode);
1660                                 
1661                                 nldrag->node= link->fromnode;
1662                                 nldrag->sock= link->fromsock;
1663                                 nodeRemLink(snode->edittree, link);
1664                                 return SOCK_OUT;
1665                         }
1666                 }
1667         }
1668         
1669         return 0;
1670 }
1671
1672 static int node_link_invoke(bContext *C, wmOperator *op, wmEvent *event)
1673 {
1674         SpaceNode *snode= CTX_wm_space_node(C);
1675         ARegion *ar= CTX_wm_region(C);
1676         NodeLinkDrag *nldrag= MEM_callocN(sizeof(NodeLinkDrag), "drag link op customdata");
1677         
1678         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1679                                                          &snode->mx, &snode->my);
1680
1681         nldrag->in_out= node_link_init(snode, nldrag);
1682                 
1683         if(nldrag->in_out) {
1684                 op->customdata= nldrag;
1685                 
1686                 /* we make a temporal link */
1687                 if(nldrag->in_out==SOCK_OUT)
1688                         nldrag->link= nodeAddLink(snode->edittree, nldrag->node, nldrag->sock, NULL, NULL);
1689                 else
1690                         nldrag->link= nodeAddLink(snode->edittree, NULL, NULL, nldrag->node, nldrag->sock);
1691                 
1692                 /* add modal handler */
1693                 WM_event_add_modal_handler(C, op);
1694                 
1695                 return OPERATOR_RUNNING_MODAL;
1696         }
1697         else {
1698                 MEM_freeN(nldrag);
1699                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1700         }
1701 }
1702
1703 void NODE_OT_link(wmOperatorType *ot)
1704 {
1705         /* identifiers */
1706         ot->name= "Link Nodes";
1707         ot->idname= "NODE_OT_link";
1708         
1709         /* api callbacks */
1710         ot->invoke= node_link_invoke;
1711         ot->modal= node_link_modal;
1712 //      ot->exec= node_link_exec;
1713         ot->poll= ED_operator_node_active;
1714         
1715         /* flags */
1716         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1717 }
1718
1719 /* ********************** Make Link operator ***************** */
1720
1721 /* makes a link between selected output and input sockets */
1722 static int node_make_link_exec(bContext *C, wmOperator *op)
1723 {
1724         SpaceNode *snode= CTX_wm_space_node(C);
1725         int replace = RNA_boolean_get(op->ptr, "replace");
1726
1727         snode_autoconnect(snode, 0, replace);
1728
1729         node_tree_verify_groups(snode->nodetree);
1730         snode_notify(C, snode);
1731         
1732         return OPERATOR_FINISHED;
1733 }
1734
1735 void NODE_OT_link_make(wmOperatorType *ot)
1736 {
1737         /* identifiers */
1738         ot->name= "Make Links";
1739         ot->description= "Makes a link between selected output in input sockets";
1740         ot->idname= "NODE_OT_link_make";
1741         
1742         /* callbacks */
1743         ot->exec= node_make_link_exec;
1744         ot->poll= ED_operator_node_active; // XXX we need a special poll which checks that there are selected input/output sockets
1745         
1746         /* flags */
1747         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1748         
1749         RNA_def_boolean(ot->srna, "replace", 0, "Replace", "Replace socket connections with the new links");
1750 }
1751
1752 /* ********************** Cut Link operator ***************** */
1753
1754 #define LINK_RESOL 12
1755 static int cut_links_intersect(bNodeLink *link, float mcoords[][2], int tot)
1756 {
1757         float coord_array[LINK_RESOL+1][2];
1758         int i, b;
1759         
1760         if(node_link_bezier_points(NULL, NULL, link, coord_array, LINK_RESOL)) {
1761
1762                 for(i=0; i<tot-1; i++)
1763                         for(b=0; b<LINK_RESOL-1; b++)
1764                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
1765                                         return 1;
1766         }
1767         return 0;
1768 }
1769
1770 static int cut_links_exec(bContext *C, wmOperator *op)
1771 {
1772         SpaceNode *snode= CTX_wm_space_node(C);
1773         ARegion *ar= CTX_wm_region(C);
1774         float mcoords[256][2];
1775         int i= 0;
1776         
1777         RNA_BEGIN(op->ptr, itemptr, "path") {
1778                 float loc[2];
1779                 
1780                 RNA_float_get_array(&itemptr, "loc", loc);
1781                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
1782                                                                  &mcoords[i][0], &mcoords[i][1]);
1783                 i++;
1784                 if(i>= 256) break;
1785         }
1786         RNA_END;
1787         
1788         if(i>1) {
1789                 bNodeLink *link, *next;
1790                 
1791                 for(link= snode->edittree->links.first; link; link= next) {
1792                         next= link->next;
1793                         
1794                         if(cut_links_intersect(link, mcoords, i)) {
1795                                 NodeTagChanged(snode->edittree, link->tonode);
1796                                 nodeRemLink(snode->edittree, link);
1797                         }
1798                 }
1799                 
1800                 ntreeSolveOrder(snode->edittree);
1801                 node_tree_verify_groups(snode->nodetree);
1802                 snode_notify(C, snode);
1803                 
1804                 return OPERATOR_FINISHED;
1805         }
1806         
1807         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1808 }
1809
1810 void NODE_OT_links_cut(wmOperatorType *ot)
1811 {
1812         PropertyRNA *prop;
1813         
1814         ot->name= "Cut links";
1815         ot->idname= "NODE_OT_links_cut";
1816         
1817         ot->invoke= WM_gesture_lines_invoke;
1818         ot->modal= WM_gesture_lines_modal;
1819         ot->exec= cut_links_exec;
1820         
1821         ot->poll= ED_operator_node_active;
1822         
1823         /* flags */
1824         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1825         
1826         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1827         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1828         /* internal */
1829         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
1830 }
1831
1832 /* ******************************** */
1833 // XXX some code needing updating to operators...
1834
1835 /* goes over all scenes, reads render layers */
1836 static int node_read_renderlayers_exec(bContext *C, wmOperator *op)
1837 {
1838         SpaceNode *snode= CTX_wm_space_node(C);
1839         Scene *curscene= CTX_data_scene(C), *scene;
1840         bNode *node;
1841
1842         /* first tag scenes unread */
1843         for(scene= G.main->scene.first; scene; scene= scene->id.next) 
1844                 scene->id.flag |= LIB_DOIT;
1845
1846         for(node= snode->edittree->nodes.first; node; node= node->next) {
1847                 if(node->type==CMP_NODE_R_LAYERS) {
1848                         ID *id= node->id;
1849                         if(id->flag & LIB_DOIT) {
1850                                 RE_ReadRenderResult(curscene, (Scene *)id);
1851                                 ntreeCompositTagRender((Scene *)id);
1852                                 id->flag &= ~LIB_DOIT;
1853                         }
1854                 }
1855         }
1856         
1857         snode_notify(C, snode);
1858         return OPERATOR_FINISHED;
1859 }
1860
1861 void NODE_OT_read_renderlayers(wmOperatorType *ot)
1862 {
1863         
1864         ot->name= "Read Render Layers";
1865         ot->idname= "NODE_OT_read_renderlayers";
1866         
1867         ot->exec= node_read_renderlayers_exec;
1868         
1869         ot->poll= ED_operator_node_active;
1870         
1871         /* flags */
1872         ot->flag= 0;
1873 }
1874
1875 static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *op)
1876 {
1877         SpaceNode *snode= CTX_wm_space_node(C);
1878         Scene *curscene= CTX_data_scene(C);
1879         Render *re= RE_NewRender(curscene->id.name);
1880
1881 //      WM_cursor_wait(1);
1882
1883         RE_MergeFullSample(re, curscene, snode->nodetree);
1884         snode_notify(C, snode);
1885         
1886 //      WM_cursor_wait(0);
1887         return OPERATOR_FINISHED;
1888 }
1889
1890
1891 void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
1892 {
1893         
1894         ot->name= "Read Full Sample Layers";
1895         ot->idname= "NODE_OT_read_fullsamplelayers";
1896         
1897         ot->exec= node_read_fullsamplelayers_exec;
1898         
1899         ot->poll= ED_operator_node_active;
1900         
1901         /* flags */
1902         ot->flag= 0;
1903 }
1904
1905
1906 /* ************************* */
1907
1908 void imagepaint_composite_tags(bNodeTree *ntree, Image *image, ImageUser *iuser)
1909 {
1910         bNode *node;
1911         
1912         if(ntree==NULL)
1913                 return;
1914         
1915         /* search for renderresults */
1916         if(image->type==IMA_TYPE_R_RESULT) {
1917                 for(node= ntree->nodes.first; node; node= node->next) {
1918                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
1919                                 /* imageuser comes from ImageWin, so indexes are offset 1 */
1920                                 if(node->custom1==iuser->layer-1)
1921                                         NodeTagChanged(ntree, node);
1922                         }
1923                 }
1924         }
1925         else {
1926                 for(node= ntree->nodes.first; node; node= node->next) {
1927                         if(node->id== &image->id)
1928                                 NodeTagChanged(ntree, node);
1929                 }
1930         }
1931 }
1932
1933 /* ****************** Make Group operator ******************* */
1934
1935 static int node_group_make_exec(bContext *C, wmOperator *op)
1936 {
1937         SpaceNode *snode = CTX_wm_space_node(C);
1938         bNode *gnode;
1939         
1940         if(snode->edittree!=snode->nodetree) {
1941                 BKE_report(op->reports, RPT_ERROR, "Can not add a new Group in a Group");
1942                 return OPERATOR_CANCELLED;
1943         }
1944         
1945         /* for time being... is too complex to handle */
1946         if(snode->treetype==NTREE_COMPOSIT) {
1947                 for(gnode=snode->nodetree->nodes.first; gnode; gnode= gnode->next) {
1948                         if(gnode->flag & SELECT)
1949                                 if(gnode->type==CMP_NODE_R_LAYERS)
1950                                         break;
1951                 }
1952                 
1953                 if(gnode) {
1954                         BKE_report(op->reports, RPT_ERROR, "Can not add RenderLayer in a Group");
1955                         return OPERATOR_CANCELLED;
1956                 }
1957         }
1958         
1959         gnode= nodeMakeGroupFromSelected(snode->nodetree);
1960         if(gnode==NULL) {
1961                 BKE_report(op->reports, RPT_ERROR, "Can not make Group");
1962                 return OPERATOR_CANCELLED;
1963         }
1964         else {
1965                 nodeSetActive(snode->nodetree, gnode);
1966                 ntreeSolveOrder(snode->nodetree);
1967         }
1968         
1969         snode_notify(C, snode);
1970         
1971         return OPERATOR_FINISHED;
1972 }
1973
1974 void NODE_OT_group_make(wmOperatorType *ot)
1975 {
1976         /* identifiers */
1977         ot->name = "Group";
1978         ot->description = "Make group from selected nodes";
1979         ot->idname = "NODE_OT_group_make";
1980         
1981         /* api callbacks */
1982         ot->exec = node_group_make_exec;
1983         ot->poll = ED_operator_node_active;
1984         
1985         /* flags */
1986         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1987 }
1988
1989 /* ****************** Hide operator *********************** */
1990
1991 static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
1992 {
1993         int tot_eq= 0, tot_neq= 0;
1994         bNode *node;
1995
1996         for(node= snode->edittree->nodes.first; node; node= node->next) {
1997                 if(node->flag & SELECT) {
1998
1999                         if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
2000                                 continue;
2001
2002                         if(node->flag & toggle_flag)
2003                                 tot_eq++;
2004                         else
2005                                 tot_neq++;
2006                 }
2007         }
2008         for(node= snode->edittree->nodes.first; node; node= node->next) {
2009                 if(node->flag & SELECT) {
2010
2011                         if(toggle_flag== NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW)==0)
2012                                 continue;
2013
2014                         if( (tot_eq && tot_neq) || tot_eq==0)
2015                                 node->flag |= toggle_flag;
2016                         else
2017                                 node->flag &= ~toggle_flag;
2018                 }
2019         }
2020 }
2021
2022 static int node_hide_exec(bContext *C, wmOperator *op)
2023 {
2024         SpaceNode *snode= CTX_wm_space_node(C);
2025         
2026         /* sanity checking (poll callback checks this already) */
2027         if((snode == NULL) || (snode->edittree == NULL))
2028                 return OPERATOR_CANCELLED;
2029         
2030         node_flag_toggle_exec(snode, NODE_HIDDEN);
2031         
2032         snode_notify(C, snode);
2033         
2034         return OPERATOR_FINISHED;
2035 }
2036
2037 void NODE_OT_hide_toggle(wmOperatorType *ot)
2038 {
2039         /* identifiers */
2040         ot->name= "Hide";
2041         ot->description= "Toggle hiding of selected nodes";
2042         ot->idname= "NODE_OT_hide_toggle";
2043         
2044         /* callbacks */
2045         ot->exec= node_hide_exec;
2046         ot->poll= ED_operator_node_active;
2047
2048         /* flags */
2049         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2050 }
2051
2052 static int node_preview_exec(bContext *C, wmOperator *op)
2053 {
2054         SpaceNode *snode= CTX_wm_space_node(C);
2055
2056         /* sanity checking (poll callback checks this already) */
2057         if((snode == NULL) || (snode->edittree == NULL))
2058                 return OPERATOR_CANCELLED;
2059
2060         node_flag_toggle_exec(snode, NODE_PREVIEW);
2061
2062         snode_notify(C, snode);
2063
2064         return OPERATOR_FINISHED;
2065 }
2066
2067 void NODE_OT_preview_toggle(wmOperatorType *ot)
2068 {
2069         /* identifiers */
2070         ot->name= "Toggle Node Preview";
2071         ot->description= "Toggle preview display for selected nodes";
2072         ot->idname= "NODE_OT_preview_toggle";
2073
2074         /* callbacks */
2075         ot->exec= node_preview_exec;
2076         ot->poll= ED_operator_node_active;
2077
2078         /* flags */
2079         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2080 }
2081
2082 static int node_socket_toggle_exec(bContext *C, wmOperator *op)
2083 {
2084         SpaceNode *snode= CTX_wm_space_node(C);
2085         bNode *node;
2086         int hidden= 0;
2087
2088         /* sanity checking (poll callback checks this already) */
2089         if((snode == NULL) || (snode->edittree == NULL))
2090                 return OPERATOR_CANCELLED;
2091
2092         for(node= snode->edittree->nodes.first; node; node= node->next) {
2093                 if(node->flag & SELECT) {
2094                         if(node_has_hidden_sockets(node)) {
2095                                 hidden= 1;
2096                                 break;
2097                         }
2098                 }
2099         }
2100
2101         for(node= snode->edittree->nodes.first; node; node= node->next) {
2102                 if(node->flag & SELECT) {
2103                         node_set_hidden_sockets(snode, node, !hidden);
2104                 }
2105         }
2106
2107         node_tree_verify_groups(snode->nodetree);
2108
2109         snode_notify(C, snode);
2110
2111         return OPERATOR_FINISHED;
2112 }
2113
2114 void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
2115 {
2116         /* identifiers */
2117         ot->name= "Toggle Hidden Node Sockets";
2118         ot->description= "Toggle unused node socket display";
2119         ot->idname= "NODE_OT_hide_socket_toggle";
2120
2121         /* callbacks */
2122         ot->exec= node_socket_toggle_exec;
2123         ot->poll= ED_operator_node_active;
2124
2125         /* flags */
2126         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2127 }
2128
2129 /* ****************** Mute operator *********************** */
2130
2131 static int node_mute_exec(bContext *C, wmOperator *op)
2132 {
2133         SpaceNode *snode= CTX_wm_space_node(C);
2134         bNode *node;
2135
2136         /* no disabling inside of groups */
2137         if(node_tree_get_editgroup(snode->nodetree))
2138                 return OPERATOR_CANCELLED;
2139         
2140         for(node= snode->edittree->nodes.first; node; node= node->next) {
2141                 if(node->flag & SELECT) {
2142                         if(node->inputs.first && node->outputs.first) {
2143                                 node->flag ^= NODE_MUTED;
2144                                 NodeTagChanged(snode->edittree, node);
2145                         }
2146                 }
2147         }
2148         
2149         snode_notify(C, snode);
2150         
2151         return OPERATOR_FINISHED;
2152 }
2153
2154 void NODE_OT_mute_toggle(wmOperatorType *ot)
2155 {
2156         /* identifiers */
2157         ot->name= "Toggle Node Mute";
2158         ot->description= "Toggle muting of the nodes";
2159         ot->idname= "NODE_OT_mute_toggle";
2160         
2161         /* callbacks */
2162         ot->exec= node_mute_exec;
2163         ot->poll= ED_operator_node_active;
2164         
2165         /* flags */
2166         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2167 }
2168
2169 /* ****************** Delete operator ******************* */
2170
2171 static int node_delete_exec(bContext *C, wmOperator *op)
2172 {
2173         SpaceNode *snode= CTX_wm_space_node(C);
2174         bNode *node, *next;
2175         
2176         for(node= snode->edittree->nodes.first; node; node= next) {
2177                 next= node->next;
2178                 if(node->flag & SELECT) {
2179                         /* check id user here, nodeFreeNode is called for free dbase too */
2180                         if(node->id)
2181                                 node->id->us--;
2182                         nodeFreeNode(snode->edittree, node);
2183                 }
2184         }
2185         
2186         node_tree_verify_groups(snode->nodetree);
2187
2188         snode_notify(C, snode);
2189         
2190         return OPERATOR_FINISHED;
2191 }
2192
2193 void NODE_OT_delete(wmOperatorType *ot)
2194 {
2195         /* identifiers */
2196         ot->name= "Delete";
2197         ot->description = "Delete selected nodes";
2198         ot->idname= "NODE_OT_delete";
2199         
2200         /* api callbacks */
2201         ot->exec= node_delete_exec;
2202         ot->poll= ED_operator_node_active;
2203         
2204         /* flags */
2205         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2206 }
2207
2208 /* ****************** Show Cyclic Dependencies Operator  ******************* */
2209
2210 static int node_show_cycles_exec(bContext *C, wmOperator *op)
2211 {
2212         SpaceNode *snode= CTX_wm_space_node(C);
2213         
2214         /* this is just a wrapper around this call... */
2215         ntreeSolveOrder(snode->edittree);
2216         snode_notify(C, snode);
2217         
2218         return OPERATOR_FINISHED;
2219 }
2220
2221 void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
2222 {
2223         /* identifiers */
2224         ot->name= "Show Cyclic Dependencies";
2225         ot->description= "Sort the nodes and show the cyclic dependencies between the nodes";
2226         ot->idname= "NODE_OT_show_cyclic_dependencies";
2227         
2228         /* callbacks */
2229         ot->exec= node_show_cycles_exec;
2230         ot->poll= ED_operator_node_active;
2231         
2232         /* flags */
2233         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2234 }
2235
2236 /* ****************** Add File Node Operator  ******************* */
2237
2238 static int node_add_file_exec(bContext *C, wmOperator *op)
2239 {
2240         Scene *scene= CTX_data_scene(C);
2241         SpaceNode *snode= CTX_wm_space_node(C);
2242         bNode *node;
2243         Image *ima= NULL;
2244         int ntype=0;
2245
2246         /* check input variables */
2247         if (RNA_property_is_set(op->ptr, "filepath"))
2248         {
2249                 char path[FILE_MAX];
2250                 RNA_string_get(op->ptr, "filepath", path);
2251                 ima= BKE_add_image_file(path, scene ? scene->r.cfra : 1);
2252         }
2253         else if(RNA_property_is_set(op->ptr, "name"))
2254         {
2255                 char name[32];
2256                 RNA_string_get(op->ptr, "name", name);
2257                 ima= (Image *)find_id("IM", name);
2258         }
2259         
2260         if(!ima) {
2261                 BKE_report(op->reports, RPT_ERROR, "Not an Image.");
2262                 return OPERATOR_CANCELLED;
2263         }
2264         
2265         
2266         node_deselectall(snode);
2267         
2268         if (snode->nodetree->type==NTREE_COMPOSIT)
2269                 ntype = CMP_NODE_IMAGE;
2270         
2271         node = node_add_node(snode, scene, ntype, snode->mx, snode->my);
2272         
2273         if (!node) {
2274                 BKE_report(op->reports, RPT_ERROR, "Could not add an image node.");
2275                 return OPERATOR_CANCELLED;
2276         }
2277         
2278         node->id = (ID *)ima;
2279         
2280         snode_notify(C, snode);
2281         
2282         return OPERATOR_FINISHED;
2283 }
2284
2285 static int node_add_file_invoke(bContext *C, wmOperator *op, wmEvent *event)
2286 {
2287         ARegion *ar= CTX_wm_region(C);
2288         SpaceNode *snode= CTX_wm_space_node(C);
2289         
2290         /* convert mouse coordinates to v2d space */
2291         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
2292                                                          &snode->mx, &snode->my);
2293         
2294         if (RNA_property_is_set(op->ptr, "filepath") || RNA_property_is_set(op->ptr, "name"))
2295                 return node_add_file_exec(C, op);
2296         else
2297                 return WM_operator_filesel(C, op, event);
2298 }
2299
2300 void NODE_OT_add_file(wmOperatorType *ot)
2301 {
2302         /* identifiers */
2303         ot->name= "Add File Node";
2304         ot->description= "Add a file node to the current node editor";
2305         ot->idname= "NODE_OT_add_file";
2306         
2307         /* callbacks */
2308         ot->exec= node_add_file_exec;
2309         ot->invoke= node_add_file_invoke;
2310         ot->poll= ED_operator_node_active;
2311         
2312         /* flags */
2313         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2314         
2315         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, 0);  //XXX TODO, relative_path
2316         RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Datablock name to assign.");
2317 }
2318