* Tweaks for node editor Make Links (F key)
[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 static bNodeSocket *get_next_outputsocket(bNodeSocket *sock, bNodeSocket **sockfrom, int totsock)
1357 {
1358         int a;
1359         
1360         /* first try to find a sockets with matching name */
1361         for (a=0; a<totsock; a++) {
1362                 if(sockfrom[a]) {
1363                         if(sock->type==sockfrom[a]->type) {
1364                                 if (strcmp(sockfrom[a]->name, sock->name)==0)
1365                                         return sockfrom[a];
1366                         }
1367                 }
1368         }
1369         
1370         /* otherwise settle for the first available socket of the right type */
1371         for (a=0; a<totsock; a++) {
1372                 if(sockfrom[a]) {
1373                         if(sock->type==sockfrom[a]->type) {
1374                                 return sockfrom[a];
1375                         }
1376                 }
1377         }
1378         
1379         return NULL;
1380 }
1381
1382 void snode_autoconnect(SpaceNode *snode, bNode *node_to, int flag, int replace)
1383 {
1384         bNodeSocket *sock, *sockfrom[8];
1385         bNode *node, *nodefrom[8];
1386         int totsock= 0, socktype=0;
1387
1388         if(node_to==NULL || node_to->inputs.first==NULL)
1389                 return;
1390
1391         /* connect first 1 socket type or first available socket now */
1392         for(sock= node_to->inputs.first; sock; sock= sock->next) {
1393                 if (!replace && nodeCountSocketLinks(snode->edittree, sock))
1394                         continue;
1395                 if(socktype<sock->type)
1396                         socktype= sock->type;
1397         }
1398         
1399         /* find potential sockets, max 8 should work */
1400         for(node= snode->edittree->nodes.first; node; node= node->next) {
1401                 if((node->flag & flag) && node!=node_to) {
1402                         for(sock= node->outputs.first; sock; sock= sock->next) {
1403                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1404                                         sockfrom[totsock]= sock;
1405                                         nodefrom[totsock]= node;
1406                                         totsock++;
1407                                         if(totsock>7)
1408                                                 break;
1409                                 }
1410                         }
1411                 }
1412                 if(totsock>7)
1413                         break;
1414         }
1415
1416         /* now just get matching socket types and create links */
1417         for(sock= node_to->inputs.first; sock; sock= sock->next) {
1418                 bNodeSocket *sock_from;
1419                 bNode *node_from;
1420                 
1421                 if (sock->type != socktype)
1422                         continue;
1423                 
1424                 /* find a potential output socket and associated node */
1425                 sock_from = get_next_outputsocket(sock, sockfrom, totsock);
1426                 if (!sock_from)
1427                         continue;
1428                 nodeFindNode(snode->edittree, sock_from, &node_from, NULL);
1429                 
1430                 /* then connect up the links */
1431                 if (replace) {
1432                         nodeRemSocketLinks(snode->edittree, sock);
1433                         nodeAddLink(snode->edittree, node_from, sock_from, node_to, sock);
1434                 } else {
1435                         if (nodeCountSocketLinks(snode->edittree, sock)==0)
1436                                 nodeAddLink(snode->edittree, node_from, sock_from, node_to, sock);
1437                 }
1438                 sock_from = NULL;
1439         }
1440         
1441         ntreeSolveOrder(snode->edittree);
1442 }
1443
1444
1445 /* can be called from menus too, but they should do own undopush and redraws */
1446 bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float locy)
1447 {
1448         bNode *node= NULL, *gnode;
1449         
1450         node_deselectall(snode);
1451         
1452         if(type>=NODE_DYNAMIC_MENU) {
1453                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1454         }
1455         else if(type>=NODE_GROUP_MENU) {
1456                 if(snode->edittree!=snode->nodetree) {
1457                         // XXX error("Can not add a Group in a Group");
1458                         return NULL;
1459                 }
1460                 else {
1461                         bNodeTree *ngroup= BLI_findlink(&G.main->nodetree, type-NODE_GROUP_MENU);
1462                         if(ngroup)
1463                                 node= nodeAddNodeType(snode->edittree, NODE_GROUP, ngroup, NULL);
1464                 }
1465         }
1466         else
1467                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1468         
1469         /* generics */
1470         if(node) {
1471                 node->locx= locx;
1472                 node->locy= locy + 60.0f;               // arbitrary.. so its visible
1473                 node->flag |= SELECT;
1474                 
1475                 gnode= node_tree_get_editgroup(snode->nodetree);
1476                 if(gnode) {
1477                         node->locx -= gnode->locx;
1478                         node->locy -= gnode->locy;
1479                 }
1480
1481                 node_tree_verify_groups(snode->nodetree);
1482                 node_set_active(snode, node);
1483                 
1484                 if(snode->nodetree->type==NTREE_COMPOSIT) {
1485                         if(ELEM3(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS))
1486                                 node->id = &scene->id;
1487                         
1488                         ntreeCompositForceHidden(snode->edittree, scene);
1489                 }
1490                         
1491                 if(node->id)
1492                         id_us_plus(node->id);
1493                         
1494                 NodeTagChanged(snode->edittree, node);
1495         }
1496         
1497         if(snode->nodetree->type==NTREE_TEXTURE) {
1498                 ntreeTexCheckCyclics(snode->edittree);
1499         }
1500         
1501         return node;
1502 }
1503
1504 /* ****************** Duplicate *********************** */
1505
1506 static int node_duplicate_exec(bContext *C, wmOperator *op)
1507 {
1508         SpaceNode *snode= CTX_wm_space_node(C);
1509         
1510         ntreeCopyTree(snode->edittree, 1);      /* 1 == internally selected nodes */
1511         
1512         ntreeSolveOrder(snode->edittree);
1513         node_tree_verify_groups(snode->nodetree);
1514         snode_handle_recalc(C, snode);
1515
1516         return OPERATOR_FINISHED;
1517 }
1518
1519 void NODE_OT_duplicate(wmOperatorType *ot)
1520 {
1521         /* identifiers */
1522         ot->name= "Duplicate Nodes";
1523         ot->description = "Duplicate the nodes.";
1524         ot->idname= "NODE_OT_duplicate";
1525         
1526         /* api callbacks */
1527         ot->exec= node_duplicate_exec;
1528         ot->poll= ED_operator_node_active;
1529         
1530         /* flags */
1531         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1532 }
1533
1534 /* *************************** add link op ******************** */
1535
1536 /* temp data to pass on to modal */
1537 typedef struct NodeLinkDrag
1538 {
1539         bNode *node;
1540         bNodeSocket *sock;
1541         bNodeLink *link;
1542         int in_out;
1543 } NodeLinkDrag;
1544
1545 /*static*/ void reset_sel_socket(SpaceNode *snode, int in_out)
1546 {
1547         bNode *node;
1548         bNodeSocket *sock;
1549         
1550         for(node= snode->edittree->nodes.first; node; node= node->next) {
1551                 if(in_out & SOCK_IN) {
1552                         for(sock= node->inputs.first; sock; sock= sock->next)
1553                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1554                 }
1555                 if(in_out & SOCK_OUT) {
1556                         for(sock= node->outputs.first; sock; sock= sock->next)
1557                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1558                 }
1559         }
1560 }
1561
1562
1563 static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeLink *link)
1564 {
1565         bNodeLink *tlink;
1566         bNodeSocket *sock;
1567         
1568         if(tsock && nodeCountSocketLinks(snode->edittree, link->tosock) > tsock->limit) {
1569                 
1570                 for(tlink= snode->edittree->links.first; tlink; tlink= tlink->next) {
1571                         if(link!=tlink && tlink->tosock==link->tosock)
1572                                 break;
1573                 }
1574                 if(tlink) {
1575                         /* is there a free input socket with same type? */
1576                         for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
1577                                 if(sock->type==tlink->fromsock->type)
1578                                         if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
1579                                                 break;
1580                         }
1581                         if(sock) {
1582                                 tlink->tosock= sock;
1583                                 sock->flag &= ~SOCK_HIDDEN;
1584                         }
1585                         else {
1586                                 nodeRemLink(snode->edittree, tlink);
1587                         }
1588                 }
1589         }
1590 }
1591
1592 /* loop that adds a nodelink, called by function below  */
1593 /* in_out = starting socket */
1594 static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
1595 {
1596         SpaceNode *snode= CTX_wm_space_node(C);
1597         ARegion *ar= CTX_wm_region(C);
1598         NodeLinkDrag *nldrag= op->customdata;
1599         bNode *tnode, *node;
1600         bNodeSocket *tsock= NULL, *sock;
1601         bNodeLink *link;
1602         int in_out;
1603
1604         in_out= nldrag->in_out;
1605         node= nldrag->node;
1606         sock= nldrag->sock;
1607         link= nldrag->link;
1608         
1609         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1610                                                          &snode->mx, &snode->my);
1611
1612         switch (event->type) {
1613                 case MOUSEMOVE:
1614                         
1615                         if(in_out==SOCK_OUT) {
1616                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_IN)) {
1617                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
1618                                                 if(tnode!=node  && link->tonode!=tnode && link->tosock!= tsock) {
1619                                                         link->tonode= tnode;
1620                                                         link->tosock= tsock;
1621                                                         ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
1622                                                 }
1623                                         }
1624                                 }
1625                                 else {
1626                                         link->tonode= NULL;
1627                                         link->tosock= NULL;
1628                                 }
1629                         }
1630                         else {
1631                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_OUT)) {
1632                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
1633                                                 if(nodeCountSocketLinks(snode->edittree, tsock) < tsock->limit) {
1634                                                         if(tnode!=node && link->fromnode!=tnode && link->fromsock!= tsock) {
1635                                                                 link->fromnode= tnode;
1636                                                                 link->fromsock= tsock;
1637                                                                 ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
1638                                                         }
1639                                                 }
1640                                         }
1641                                 }
1642                                 else {
1643                                         link->fromnode= NULL;
1644                                         link->fromsock= NULL;
1645                                 }
1646                         }
1647                         /* hilight target sockets only */
1648                         node_socket_hilights(snode, in_out==SOCK_OUT?SOCK_IN:SOCK_OUT);
1649                         ED_region_tag_redraw(ar);
1650                         break;
1651                         
1652                 case LEFTMOUSE:
1653                 case RIGHTMOUSE:
1654                 case MIDDLEMOUSE:
1655         
1656                         /* remove link? */
1657                         if(link->tonode==NULL || link->fromnode==NULL) {
1658                                 nodeRemLink(snode->edittree, link);
1659                         }
1660                         else {
1661                                 /* send changed events for original tonode and new */
1662                                 if(link->tonode) 
1663                                         NodeTagChanged(snode->edittree, link->tonode);
1664                                 
1665                                 /* we might need to remove a link */
1666                                 if(in_out==SOCK_OUT) node_remove_extra_links(snode, link->tosock, link);
1667                         }
1668                         
1669                         ntreeSolveOrder(snode->edittree);
1670                         node_tree_verify_groups(snode->nodetree);
1671                         snode_handle_recalc(C, snode);
1672                         
1673                         MEM_freeN(op->customdata);
1674                         op->customdata= NULL;
1675                         
1676                         return OPERATOR_FINISHED;
1677         }
1678         
1679         return OPERATOR_RUNNING_MODAL;
1680 }
1681
1682 /* return 1 when socket clicked */
1683 static int node_link_init(SpaceNode *snode, NodeLinkDrag *nldrag)
1684 {
1685         bNodeLink *link;
1686         
1687         /* output indicated? */
1688         if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_OUT)) {
1689                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
1690                         return SOCK_OUT;
1691                 else {
1692                         /* find if we break a link */
1693                         for(link= snode->edittree->links.first; link; link= link->next) {
1694                                 if(link->fromsock==nldrag->sock)
1695                                         break;
1696                         }
1697                         if(link) {
1698                                 nldrag->node= link->tonode;
1699                                 nldrag->sock= link->tosock;
1700                                 nodeRemLink(snode->edittree, link);
1701                                 return SOCK_IN;
1702                         }
1703                 }
1704         }
1705         /* or an input? */
1706         else if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_IN)) {
1707                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
1708                         return SOCK_IN;
1709                 else {
1710                         /* find if we break a link */
1711                         for(link= snode->edittree->links.first; link; link= link->next) {
1712                                 if(link->tosock==nldrag->sock)
1713                                         break;
1714                         }
1715                         if(link) {
1716                                 /* send changed event to original tonode */
1717                                 if(link->tonode) 
1718                                         NodeTagChanged(snode->edittree, link->tonode);
1719                                 
1720                                 nldrag->node= link->fromnode;
1721                                 nldrag->sock= link->fromsock;
1722                                 nodeRemLink(snode->edittree, link);
1723                                 return SOCK_OUT;
1724                         }
1725                 }
1726         }
1727         
1728         return 0;
1729 }
1730
1731 static int node_link_invoke(bContext *C, wmOperator *op, wmEvent *event)
1732 {
1733         SpaceNode *snode= CTX_wm_space_node(C);
1734         ARegion *ar= CTX_wm_region(C);
1735         NodeLinkDrag *nldrag= MEM_callocN(sizeof(NodeLinkDrag), "drag link op customdata");
1736         
1737         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1738                                                          &snode->mx, &snode->my);
1739
1740         nldrag->in_out= node_link_init(snode, nldrag);
1741                 
1742         if(nldrag->in_out) {
1743                 op->customdata= nldrag;
1744                 
1745                 /* we make a temporal link */
1746                 if(nldrag->in_out==SOCK_OUT)
1747                         nldrag->link= nodeAddLink(snode->edittree, nldrag->node, nldrag->sock, NULL, NULL);
1748                 else
1749                         nldrag->link= nodeAddLink(snode->edittree, NULL, NULL, nldrag->node, nldrag->sock);
1750                 
1751                 /* add modal handler */
1752                 WM_event_add_modal_handler(C, op);
1753                 
1754                 return OPERATOR_RUNNING_MODAL;
1755         }
1756         else {
1757                 MEM_freeN(nldrag);
1758                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1759         }
1760 }
1761
1762 void NODE_OT_link(wmOperatorType *ot)
1763 {
1764         /* identifiers */
1765         ot->name= "Link Nodes";
1766         ot->idname= "NODE_OT_link";
1767         
1768         /* api callbacks */
1769         ot->invoke= node_link_invoke;
1770         ot->modal= node_link_modal;
1771 //      ot->exec= node_link_exec;
1772         ot->poll= ED_operator_node_active;
1773         
1774         /* flags */
1775         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1776 }
1777
1778 /* ********************** Make Link operator ***************** */
1779
1780 /* makes a link between selected output and input sockets */
1781 static int node_make_link_exec(bContext *C, wmOperator *op)
1782 {
1783         SpaceNode *snode= CTX_wm_space_node(C);
1784         bNode *fromnode, *tonode;
1785         bNodeLink *link;
1786         bNodeSocket *outsock= snode->edittree->selout;
1787         bNodeSocket *insock= snode->edittree->selin;
1788         int replace = RNA_boolean_get(op->ptr, "replace");
1789         
1790         if (!insock || !outsock) {
1791                 bNode *node;
1792                 
1793                 /* no socket selection, join nodes themselves, guessing connections */
1794                 tonode = nodeGetActive(snode->edittree);
1795                 
1796                 if (!tonode) {
1797                         BKE_report(op->reports, RPT_ERROR, "No active node");
1798                         return OPERATOR_CANCELLED;      
1799                 }
1800                 
1801                 /* store selection in temp test flag */
1802                 for(node= snode->edittree->nodes.first; node; node= node->next) {
1803                         if(node->flag & NODE_SELECT) node->flag |= NODE_TEST;
1804                         else node->flag &= ~NODE_TEST;
1805                 }
1806                 
1807                 snode_autoconnect(snode, tonode, NODE_TEST, replace);
1808                 node_tree_verify_groups(snode->nodetree);
1809                 snode_handle_recalc(C, snode);
1810                 
1811                 return OPERATOR_FINISHED;
1812         }
1813         
1814         
1815         if (nodeFindLink(snode->edittree, outsock, insock)) {
1816                 BKE_report(op->reports, RPT_ERROR, "There is already a link between these sockets");
1817                 return OPERATOR_CANCELLED;
1818         }
1819
1820         if (nodeFindNode(snode->edittree, outsock, &fromnode, NULL) &&
1821                 nodeFindNode(snode->edittree, insock, &tonode, NULL)) 
1822         {
1823                 link= nodeAddLink(snode->edittree, fromnode, outsock, tonode, insock);
1824                 NodeTagChanged(snode->edittree, tonode);
1825                 node_remove_extra_links(snode, insock, link);
1826         }
1827         else 
1828                 return OPERATOR_CANCELLED;
1829
1830         ntreeSolveOrder(snode->edittree);
1831         node_tree_verify_groups(snode->nodetree);
1832         snode_handle_recalc(C, snode);
1833         
1834         return OPERATOR_FINISHED;
1835 }
1836
1837 void NODE_OT_link_make(wmOperatorType *ot)
1838 {
1839         /* identifiers */
1840         ot->name= "Make Links";
1841         ot->description= "Makes a link between selected output in input sockets.";
1842         ot->idname= "NODE_OT_link_make";
1843         
1844         /* callbacks */
1845         ot->exec= node_make_link_exec;
1846         ot->poll= ED_operator_node_active; // XXX we need a special poll which checks that there are selected input/output sockets
1847         
1848         /* flags */
1849         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1850         
1851         RNA_def_boolean(ot->srna, "replace", 0, "Replace", "Replace socket connections with the new links");
1852 }
1853
1854 /* ********************** Cut Link operator ***************** */
1855
1856 #define LINK_RESOL 12
1857 static int cut_links_intersect(bNodeLink *link, float mcoords[][2], int tot)
1858 {
1859         float coord_array[LINK_RESOL+1][2];
1860         int i, b;
1861         
1862         if(node_link_bezier_points(NULL, NULL, link, coord_array, LINK_RESOL)) {
1863
1864                 for(i=0; i<tot-1; i++)
1865                         for(b=0; b<LINK_RESOL-1; b++)
1866                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
1867                                         return 1;
1868         }
1869         return 0;
1870 }
1871
1872 static int cut_links_exec(bContext *C, wmOperator *op)
1873 {
1874         SpaceNode *snode= CTX_wm_space_node(C);
1875         ARegion *ar= CTX_wm_region(C);
1876         float mcoords[256][2];
1877         int i= 0;
1878         
1879         RNA_BEGIN(op->ptr, itemptr, "path") {
1880                 float loc[2];
1881                 
1882                 RNA_float_get_array(&itemptr, "loc", loc);
1883                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
1884                                                                  &mcoords[i][0], &mcoords[i][1]);
1885                 i++;
1886                 if(i>= 256) break;
1887         }
1888         RNA_END;
1889         
1890         if(i>1) {
1891                 bNodeLink *link, *next;
1892                 
1893                 for(link= snode->edittree->links.first; link; link= next) {
1894                         next= link->next;
1895                         
1896                         if(cut_links_intersect(link, mcoords, i)) {
1897                                 NodeTagChanged(snode->edittree, link->tonode);
1898                                 nodeRemLink(snode->edittree, link);
1899                         }
1900                 }
1901                 
1902                 ntreeSolveOrder(snode->edittree);
1903                 node_tree_verify_groups(snode->nodetree);
1904                 snode_handle_recalc(C, snode);
1905                 
1906                 return OPERATOR_FINISHED;
1907         }
1908         
1909         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1910 }
1911
1912 void NODE_OT_links_cut(wmOperatorType *ot)
1913 {
1914         PropertyRNA *prop;
1915         
1916         ot->name= "Cut links";
1917         ot->idname= "NODE_OT_links_cut";
1918         
1919         ot->invoke= WM_gesture_lines_invoke;
1920         ot->modal= WM_gesture_lines_modal;
1921         ot->exec= cut_links_exec;
1922         
1923         ot->poll= ED_operator_node_active;
1924         
1925         /* flags */
1926         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1927         
1928         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1929         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1930         /* internal */
1931         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
1932 }
1933
1934 /* ******************************** */
1935 // XXX some code needing updating to operators...
1936
1937 /* goes over all scenes, reads render layerss */
1938 void node_read_renderlayers(SpaceNode *snode)
1939 {
1940         Scene *curscene= NULL; // XXX
1941         Scene *scene;
1942         bNode *node;
1943
1944         /* first tag scenes unread */
1945         for(scene= G.main->scene.first; scene; scene= scene->id.next) 
1946                 scene->id.flag |= LIB_DOIT;
1947
1948         for(node= snode->edittree->nodes.first; node; node= node->next) {
1949                 if(node->type==CMP_NODE_R_LAYERS) {
1950                         ID *id= node->id;
1951                         if(id->flag & LIB_DOIT) {
1952                                 RE_ReadRenderResult(curscene, (Scene *)id);
1953                                 ntreeCompositTagRender((Scene *)id);
1954                                 id->flag &= ~LIB_DOIT;
1955                         }
1956                 }
1957         }
1958         
1959         // XXX                  snode_handle_recalc(snode);
1960 }
1961
1962 void node_read_fullsamplelayers(SpaceNode *snode)
1963 {
1964         Scene *curscene= NULL; // XXX
1965         Render *re= RE_NewRender(curscene->id.name);
1966
1967         WM_cursor_wait(1);
1968
1969         //BIF_init_render_callbacks(re, 1);
1970         RE_MergeFullSample(re, curscene, snode->nodetree);
1971         //BIF_end_render_callbacks();
1972         
1973         // allqueue(REDRAWNODE, 1);
1974         // allqueue(REDRAWIMAGE, 1);
1975         
1976         WM_cursor_wait(0);
1977 }
1978
1979 void imagepaint_composite_tags(bNodeTree *ntree, Image *image, ImageUser *iuser)
1980 {
1981         bNode *node;
1982         
1983         if(ntree==NULL)
1984                 return;
1985         
1986         /* search for renderresults */
1987         if(image->type==IMA_TYPE_R_RESULT) {
1988                 for(node= ntree->nodes.first; node; node= node->next) {
1989                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
1990                                 /* imageuser comes from ImageWin, so indexes are offset 1 */
1991                                 if(node->custom1==iuser->layer-1)
1992                                         NodeTagChanged(ntree, node);
1993                         }
1994                 }
1995         }
1996         else {
1997                 for(node= ntree->nodes.first; node; node= node->next) {
1998                         if(node->id== &image->id)
1999                                 NodeTagChanged(ntree, node);
2000                 }
2001         }
2002 }
2003
2004 /* ****************** Make Group operator ******************* */
2005
2006 static int node_group_make_exec(bContext *C, wmOperator *op)
2007 {
2008         SpaceNode *snode = CTX_wm_space_node(C);
2009         bNode *gnode;
2010         
2011         if(snode->edittree!=snode->nodetree) {
2012                 BKE_report(op->reports, RPT_ERROR, "Can not add a new Group in a Group");
2013                 return OPERATOR_CANCELLED;
2014         }
2015         
2016         /* for time being... is too complex to handle */
2017         if(snode->treetype==NTREE_COMPOSIT) {
2018                 for(gnode=snode->nodetree->nodes.first; gnode; gnode= gnode->next) {
2019                         if(gnode->flag & SELECT)
2020                                 if(gnode->type==CMP_NODE_R_LAYERS)
2021                                         break;
2022                 }
2023                 
2024                 if(gnode) {
2025                         BKE_report(op->reports, RPT_ERROR, "Can not add RenderLayer in a Group");
2026                         return OPERATOR_CANCELLED;
2027                 }
2028         }
2029         
2030         gnode= nodeMakeGroupFromSelected(snode->nodetree);
2031         if(gnode==NULL) {
2032                 BKE_report(op->reports, RPT_ERROR, "Can not make Group");
2033                 return OPERATOR_CANCELLED;
2034         }
2035         else {
2036                 nodeSetActive(snode->nodetree, gnode);
2037                 ntreeSolveOrder(snode->nodetree);
2038         }
2039         
2040         snode_handle_recalc(C, snode);
2041         
2042         return OPERATOR_FINISHED;
2043 }
2044
2045 void NODE_OT_group_make(wmOperatorType *ot)
2046 {
2047         /* identifiers */
2048         ot->name = "Group";
2049         ot->description = "Make group from selected nodes.";
2050         ot->idname = "NODE_OT_group_make";
2051         
2052         /* api callbacks */
2053         ot->exec = node_group_make_exec;
2054         ot->poll = ED_operator_node_active;
2055         
2056         /* flags */
2057         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2058 }
2059
2060 /* ****************** Hide operator *********************** */
2061
2062 static int node_hide_exec(bContext *C, wmOperator *op)
2063 {
2064         SpaceNode *snode= CTX_wm_space_node(C);
2065         bNode *node;
2066         int nothidden=0, ishidden=0;
2067         
2068         /* sanity checking (poll callback checks this already) */
2069         if((snode == NULL) || (snode->edittree == NULL))
2070                 return OPERATOR_CANCELLED;
2071         
2072         for(node= snode->edittree->nodes.first; node; node= node->next) {
2073                 if(node->flag & SELECT) {
2074                         if(node->flag & NODE_HIDDEN)
2075                                 ishidden++;
2076                         else
2077                                 nothidden++;
2078                 }
2079         }
2080         for(node= snode->edittree->nodes.first; node; node= node->next) {
2081                 if(node->flag & SELECT) {
2082                         if( (ishidden && nothidden) || ishidden==0)
2083                                 node->flag |= NODE_HIDDEN;
2084                         else 
2085                                 node->flag &= ~NODE_HIDDEN;
2086                 }
2087         }
2088         
2089         snode_handle_recalc(C, snode);
2090         
2091         return OPERATOR_FINISHED;
2092 }
2093
2094 void NODE_OT_hide(wmOperatorType *ot)
2095 {
2096         /* identifiers */
2097         ot->name= "Hide";
2098         ot->description= "Toggle hiding of the nodes.";
2099         ot->idname= "NODE_OT_hide";
2100         
2101         /* callbacks */
2102         ot->exec= node_hide_exec;
2103         ot->poll= ED_operator_node_active;
2104         
2105         /* flags */
2106         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2107 }
2108
2109 /* ****************** Mute operator *********************** */
2110
2111 static int node_mute_exec(bContext *C, wmOperator *op)
2112 {
2113         SpaceNode *snode= CTX_wm_space_node(C);
2114         bNode *node;
2115
2116         /* no disabling inside of groups */
2117         if(node_tree_get_editgroup(snode->nodetree))
2118                 return OPERATOR_CANCELLED;
2119         
2120         for(node= snode->edittree->nodes.first; node; node= node->next) {
2121                 if(node->flag & SELECT) {
2122                         if(node->inputs.first && node->outputs.first) {
2123                                 node->flag ^= NODE_MUTED;
2124                         }
2125                 }
2126         }
2127         
2128         snode_handle_recalc(C, snode);
2129         
2130         return OPERATOR_FINISHED;
2131 }
2132
2133 void NODE_OT_mute(wmOperatorType *ot)
2134 {
2135         /* identifiers */
2136         ot->name= "Mute";
2137         ot->description= "Toggle muting of the nodes.";
2138         ot->idname= "NODE_OT_mute";
2139         
2140         /* callbacks */
2141         ot->exec= node_mute_exec;
2142         ot->poll= ED_operator_node_active;
2143         
2144         /* flags */
2145         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2146 }
2147
2148 /* ****************** Delete operator ******************* */
2149
2150 static int node_delete_exec(bContext *C, wmOperator *op)
2151 {
2152         SpaceNode *snode= CTX_wm_space_node(C);
2153         bNode *node, *next;
2154         bNodeSocket *sock;
2155         
2156         for(node= snode->edittree->nodes.first; node; node= next) {
2157                 next= node->next;
2158                 if(node->flag & SELECT) {
2159                         /* set selin and selout NULL if the sockets belong to a node to be deleted */
2160                         for(sock= node->inputs.first; sock; sock= sock->next)
2161                                 if(snode->edittree->selin == sock) snode->edittree->selin= NULL;
2162                         
2163                         for(sock= node->outputs.first; sock; sock= sock->next)
2164                                 if(snode->edittree->selout == sock) snode->edittree->selout= NULL;
2165                                 
2166                         /* check id user here, nodeFreeNode is called for free dbase too */
2167                         if(node->id)
2168                                 node->id->us--;
2169                         nodeFreeNode(snode->edittree, node);
2170                 }
2171         }
2172         
2173         node_tree_verify_groups(snode->nodetree);
2174
2175         snode_handle_recalc(C, snode);
2176         
2177         return OPERATOR_FINISHED;
2178 }
2179
2180 void NODE_OT_delete(wmOperatorType *ot)
2181 {
2182         /* identifiers */
2183         ot->name= "Delete";
2184         ot->description = "Delete selected nodes.";
2185         ot->idname= "NODE_OT_delete";
2186         
2187         /* api callbacks */
2188         ot->exec= node_delete_exec;
2189         ot->poll= ED_operator_node_active;
2190         
2191         /* flags */
2192         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2193 }
2194
2195 /* ****************** Show Cyclic Dependencies Operator  ******************* */
2196
2197 static int node_show_cycles_exec(bContext *C, wmOperator *op)
2198 {
2199         SpaceNode *snode= CTX_wm_space_node(C);
2200         
2201         /* this is just a wrapper around this call... */
2202         ntreeSolveOrder(snode->edittree);
2203         snode_handle_recalc(C, snode);
2204         
2205         return OPERATOR_FINISHED;
2206 }
2207
2208 void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
2209 {
2210         /* identifiers */
2211         ot->name= "Show Cyclic Dependencies";
2212         ot->description= "Sort the nodes and show the cyclic dependencies between the nodes.";
2213         ot->idname= "NODE_OT_show_cyclic_dependencies";
2214         
2215         /* callbacks */
2216         ot->exec= node_show_cycles_exec;
2217         ot->poll= ED_operator_node_active;
2218         
2219         /* flags */
2220         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2221 }
2222
2223