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