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