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