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