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