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