Sculpt:
[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 /* ******************** rename ******************* */
1146
1147
1148 /* should go through RNA */
1149 void node_rename(SpaceNode *snode)
1150 {
1151         bNode *node, *rename_node;
1152         short found_node= 0;
1153
1154         /* check if a node is selected */
1155         for(node= snode->edittree->nodes.first; node; node= node->next) {
1156                 if(node->flag & SELECT) {
1157                         found_node= 1;
1158                         break;
1159                 }
1160         }
1161
1162         if(found_node) {
1163                 rename_node= nodeGetActive(snode->edittree);
1164                 node_rename_but((char *)rename_node->name);
1165         
1166                 // allqueue(REDRAWNODE, 1);
1167         }
1168 }
1169
1170 /* ********************** select ******************** */
1171
1172 /* used in buttons to check context, also checks for edited groups */
1173 bNode *editnode_get_active_idnode(bNodeTree *ntree, short id_code)
1174 {
1175         return nodeGetActiveID(ntree, id_code);
1176 }
1177
1178 /* used in buttons to check context, also checks for edited groups */
1179 Material *editnode_get_active_material(Material *ma)
1180 {
1181         if(ma && ma->use_nodes && ma->nodetree) {
1182                 bNode *node= editnode_get_active_idnode(ma->nodetree, ID_MA);
1183                 if(node)
1184                         return (Material *)node->id;
1185                 else
1186                         return NULL;
1187         }
1188         return ma;
1189 }
1190 #endif /* 0 */
1191
1192
1193 /* no undo here! */
1194 void node_deselectall(SpaceNode *snode)
1195 {
1196         bNode *node;
1197         
1198         for(node= snode->edittree->nodes.first; node; node= node->next)
1199                 node->flag &= ~SELECT;
1200 }
1201
1202 int node_has_hidden_sockets(bNode *node)
1203 {
1204         bNodeSocket *sock;
1205         
1206         for(sock= node->inputs.first; sock; sock= sock->next)
1207                 if(sock->flag & SOCK_HIDDEN)
1208                         return 1;
1209         for(sock= node->outputs.first; sock; sock= sock->next)
1210                 if(sock->flag & SOCK_HIDDEN)
1211                         return 1;
1212         return 0;
1213 }
1214
1215 static void node_link_viewer(SpaceNode *snode, bNode *tonode)
1216 {
1217         bNode *node;
1218
1219         /* context check */
1220         if(tonode==NULL || tonode->outputs.first==NULL)
1221                 return;
1222         if( ELEM(tonode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
1223                 return;
1224         
1225         /* get viewer */
1226         for(node= snode->edittree->nodes.first; node; node= node->next)
1227                 if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
1228                         if(node->flag & NODE_DO_OUTPUT)
1229                                 break;
1230                 
1231         if(node) {
1232                 bNodeLink *link;
1233                 
1234                 /* get link to viewer */
1235                 for(link= snode->edittree->links.first; link; link= link->next)
1236                         if(link->tonode==node)
1237                                 break;
1238
1239                 if(link) {
1240                         link->fromnode= tonode;
1241                         link->fromsock= tonode->outputs.first;
1242                         NodeTagChanged(snode->edittree, node);
1243                         
1244 // XXX                  snode_handle_recalc(snode);
1245                 }
1246         }
1247 }
1248
1249
1250 void node_active_link_viewer(SpaceNode *snode)
1251 {
1252         bNode *node= editnode_get_active(snode->edittree);
1253         if(node)
1254                 node_link_viewer(snode, node);
1255 }
1256
1257 /* return 0, nothing done */
1258 /*static*/ int node_mouse_groupheader(SpaceNode *snode)
1259 {
1260         bNode *gnode;
1261         float mx=0, my=0;
1262 // XXX  short mval[2];
1263         
1264         gnode= node_tree_get_editgroup(snode->nodetree);
1265         if(gnode==NULL) return 0;
1266         
1267 // XXX  getmouseco_areawin(mval);
1268 // XXX  areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1269         
1270         /* click in header or outside? */
1271         if(BLI_in_rctf(&gnode->totr, mx, my)==0) {
1272                 rctf rect= gnode->totr;
1273                 
1274                 rect.ymax += NODE_DY;
1275                 if(BLI_in_rctf(&rect, mx, my)==0)
1276                         snode_make_group_editable(snode, NULL); /* toggles, so exits editmode */
1277 //              else
1278 // XXX                  transform_nodes(snode->nodetree, 'g', "Move group");
1279                 
1280                 return 1;
1281         }
1282         return 0;
1283 }
1284
1285 /* checks snode->mouse position, and returns found node/socket */
1286 /* type is SOCK_IN and/or SOCK_OUT */
1287 static int find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
1288 {
1289         bNode *node;
1290         bNodeSocket *sock;
1291         rctf rect;
1292         
1293         /* check if we click in a socket */
1294         for(node= snode->edittree->nodes.first; node; node= node->next) {
1295                 
1296                 rect.xmin = snode->mx - NODE_SOCKSIZE+3;
1297                 rect.ymin = snode->my - NODE_SOCKSIZE+3;
1298                 rect.xmax = rect.xmin + 2*NODE_SOCKSIZE+6;
1299                 rect.ymax = rect.ymin + 2*NODE_SOCKSIZE+6;
1300                 
1301                 if (!(node->flag & NODE_HIDDEN)) {
1302                         /* extra padding inside and out - allow dragging on the text areas too */
1303                         if (in_out == SOCK_IN) {
1304                                 rect.xmax += NODE_SOCKSIZE;
1305                                 rect.xmin -= NODE_SOCKSIZE*4;
1306                         } else if (in_out == SOCK_OUT) {
1307                                 rect.xmax += NODE_SOCKSIZE*4;
1308                                 rect.xmin -= NODE_SOCKSIZE;
1309                         }
1310                 }
1311                 
1312                 if(in_out & SOCK_IN) {
1313                         for(sock= node->inputs.first; sock; sock= sock->next) {
1314                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1315                                         if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1316                                                 if(node == visible_node(snode, &rect)) {
1317                                                         *nodep= node;
1318                                                         *sockp= sock;
1319                                                         return 1;
1320                                                 }
1321                                         }
1322                                 }
1323                         }
1324                 }
1325                 if(in_out & SOCK_OUT) {
1326                         for(sock= node->outputs.first; sock; sock= sock->next) {
1327                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1328                                         if(BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1329                                                 if(node == visible_node(snode, &rect)) {
1330                                                         *nodep= node;
1331                                                         *sockp= sock;
1332                                                         return 1;
1333                                                 }
1334                                         }
1335                                 }
1336                         }
1337                 }
1338         }
1339         return 0;
1340 }
1341
1342 static int node_socket_hilights(SpaceNode *snode, int in_out)
1343 {
1344         bNode *node;
1345         bNodeSocket *sock, *tsock, *socksel= NULL;
1346         short redraw= 0;
1347         
1348         if(snode->edittree==NULL) return 0;
1349         
1350         /* deselect sockets */
1351         for(node= snode->edittree->nodes.first; node; node= node->next) {
1352                 for(sock= node->inputs.first; sock; sock= sock->next) {
1353                         if(sock->flag & SELECT) {
1354                                 sock->flag &= ~SELECT;
1355                                 redraw++;
1356                                 socksel= sock;
1357                         }
1358                 }
1359                 for(sock= node->outputs.first; sock; sock= sock->next) {
1360                         if(sock->flag & SELECT) {
1361                                 sock->flag &= ~SELECT;
1362                                 redraw++;
1363                                 socksel= sock;
1364                         }
1365                 }
1366         }
1367         
1368         // XXX mousepos should be set here!
1369         
1370         if(find_indicated_socket(snode, &node, &tsock, in_out)) {
1371                 tsock->flag |= SELECT;
1372                 if(redraw==1 && tsock==socksel) redraw= 0;
1373                 else redraw= 1;
1374         }
1375         
1376         return redraw;
1377 }
1378
1379 /* ****************** Add *********************** */
1380
1381 void snode_autoconnect(SpaceNode *snode, bNode *node_to, int flag)
1382 {
1383         bNodeSocket *sock, *sockfrom[8];
1384         bNode *node, *nodefrom[8];
1385         int totsock= 0, socktype=0;
1386
1387         if(node_to==NULL || node_to->inputs.first==NULL)
1388                 return;
1389         
1390         /* no inputs for node allowed (code it) */
1391
1392         /* connect first 1 socket type now */
1393         for(sock= node_to->inputs.first; sock; sock= sock->next)
1394                 if(socktype<sock->type)
1395                         socktype= sock->type;
1396
1397         
1398         /* find potential sockets, max 8 should work */
1399         for(node= snode->edittree->nodes.first; node; node= node->next) {
1400                 if((node->flag & flag) && node!=node_to) {
1401                         for(sock= node->outputs.first; sock; sock= sock->next) {
1402                                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
1403                                         sockfrom[totsock]= sock;
1404                                         nodefrom[totsock]= node;
1405                                         totsock++;
1406                                         if(totsock>7)
1407                                                 break;
1408                                 }
1409                         }
1410                 }
1411                 if(totsock>7)
1412                         break;
1413         }
1414
1415         /* now just get matching socket types and create links */
1416         for(sock= node_to->inputs.first; sock; sock= sock->next) {
1417                 int a;
1418                 
1419                 for(a=0; a<totsock; a++) {
1420                         if(sockfrom[a]) {
1421                                 if(sock->type==sockfrom[a]->type && sock->type==socktype) {
1422                                         nodeAddLink(snode->edittree, nodefrom[a], sockfrom[a], node_to, sock);
1423                                         sockfrom[a]= NULL;
1424                                         break;
1425                                 }
1426                         }
1427                 }
1428         }
1429         
1430         ntreeSolveOrder(snode->edittree);
1431 }
1432
1433
1434 /* can be called from menus too, but they should do own undopush and redraws */
1435 bNode *node_add_node(SpaceNode *snode, Scene *scene, int type, float locx, float locy)
1436 {
1437         bNode *node= NULL, *gnode;
1438         
1439         node_deselectall(snode);
1440         
1441         if(type>=NODE_DYNAMIC_MENU) {
1442                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1443         }
1444         else if(type>=NODE_GROUP_MENU) {
1445                 if(snode->edittree!=snode->nodetree) {
1446                         // XXX error("Can not add a Group in a Group");
1447                         return NULL;
1448                 }
1449                 else {
1450                         bNodeTree *ngroup= BLI_findlink(&G.main->nodetree, type-NODE_GROUP_MENU);
1451                         if(ngroup)
1452                                 node= nodeAddNodeType(snode->edittree, NODE_GROUP, ngroup, NULL);
1453                 }
1454         }
1455         else
1456                 node= nodeAddNodeType(snode->edittree, type, NULL, NULL);
1457         
1458         /* generics */
1459         if(node) {
1460                 node->locx= locx;
1461                 node->locy= locy + 60.0f;               // arbitrary.. so its visible
1462                 node->flag |= SELECT;
1463                 
1464                 gnode= node_tree_get_editgroup(snode->nodetree);
1465                 if(gnode) {
1466                         node->locx -= gnode->locx;
1467                         node->locy -= gnode->locy;
1468                 }
1469
1470                 node_tree_verify_groups(snode->nodetree);
1471                 node_set_active(snode, node);
1472                 
1473                 if(snode->nodetree->type==NTREE_COMPOSIT) {
1474                         if(ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE))
1475                                 node->id = &scene->id;
1476                         
1477                         ntreeCompositForceHidden(snode->edittree, scene);
1478                 }
1479                         
1480                 if(node->id)
1481                         id_us_plus(node->id);
1482                         
1483                 NodeTagChanged(snode->edittree, node);
1484         }
1485         
1486         if(snode->nodetree->type==NTREE_TEXTURE) {
1487                 ntreeTexCheckCyclics(snode->edittree);
1488         }
1489         
1490         return node;
1491 }
1492
1493 /* ****************** Duplicate *********************** */
1494
1495 static int node_duplicate_exec(bContext *C, wmOperator *op)
1496 {
1497         SpaceNode *snode= CTX_wm_space_node(C);
1498         
1499         ntreeCopyTree(snode->edittree, 1);      /* 1 == internally selected nodes */
1500         
1501         ntreeSolveOrder(snode->edittree);
1502         node_tree_verify_groups(snode->nodetree);
1503         snode_handle_recalc(C, snode);
1504
1505         return OPERATOR_FINISHED;
1506 }
1507
1508 void NODE_OT_duplicate(wmOperatorType *ot)
1509 {
1510         /* identifiers */
1511         ot->name= "Duplicate Nodes";
1512         ot->description = "Duplicate the nodes.";
1513         ot->idname= "NODE_OT_duplicate";
1514         
1515         /* api callbacks */
1516         ot->exec= node_duplicate_exec;
1517         ot->poll= ED_operator_node_active;
1518         
1519         /* flags */
1520         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1521 }
1522
1523 /* *************************** add link op ******************** */
1524
1525 /* temp data to pass on to modal */
1526 typedef struct NodeLinkDrag
1527 {
1528         bNode *node;
1529         bNodeSocket *sock;
1530         bNodeLink *link;
1531         int in_out;
1532 } NodeLinkDrag;
1533
1534 /*static*/ void reset_sel_socket(SpaceNode *snode, int in_out)
1535 {
1536         bNode *node;
1537         bNodeSocket *sock;
1538         
1539         for(node= snode->edittree->nodes.first; node; node= node->next) {
1540                 if(in_out & SOCK_IN) {
1541                         for(sock= node->inputs.first; sock; sock= sock->next)
1542                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1543                 }
1544                 if(in_out & SOCK_OUT) {
1545                         for(sock= node->outputs.first; sock; sock= sock->next)
1546                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1547                 }
1548         }
1549 }
1550
1551
1552 static void node_remove_extra_links(SpaceNode *snode, bNodeSocket *tsock, bNodeLink *link)
1553 {
1554         bNodeLink *tlink;
1555         bNodeSocket *sock;
1556         
1557         if(tsock && nodeCountSocketLinks(snode->edittree, link->tosock) > tsock->limit) {
1558                 
1559                 for(tlink= snode->edittree->links.first; tlink; tlink= tlink->next) {
1560                         if(link!=tlink && tlink->tosock==link->tosock)
1561                                 break;
1562                 }
1563                 if(tlink) {
1564                         /* is there a free input socket with same type? */
1565                         for(sock= tlink->tonode->inputs.first; sock; sock= sock->next) {
1566                                 if(sock->type==tlink->fromsock->type)
1567                                         if(nodeCountSocketLinks(snode->edittree, sock) < sock->limit)
1568                                                 break;
1569                         }
1570                         if(sock) {
1571                                 tlink->tosock= sock;
1572                                 sock->flag &= ~SOCK_HIDDEN;
1573                         }
1574                         else {
1575                                 nodeRemLink(snode->edittree, tlink);
1576                         }
1577                 }
1578         }
1579 }
1580
1581 /* loop that adds a nodelink, called by function below  */
1582 /* in_out = starting socket */
1583 static int node_link_modal(bContext *C, wmOperator *op, wmEvent *event)
1584 {
1585         SpaceNode *snode= CTX_wm_space_node(C);
1586         ARegion *ar= CTX_wm_region(C);
1587         NodeLinkDrag *nldrag= op->customdata;
1588         bNode *tnode, *node;
1589         bNodeSocket *tsock= NULL, *sock;
1590         bNodeLink *link;
1591         int in_out;
1592
1593         in_out= nldrag->in_out;
1594         node= nldrag->node;
1595         sock= nldrag->sock;
1596         link= nldrag->link;
1597         
1598         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1599                                                          &snode->mx, &snode->my);
1600
1601         switch (event->type) {
1602                 case MOUSEMOVE:
1603                         
1604                         if(in_out==SOCK_OUT) {
1605                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_IN)) {
1606                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
1607                                                 if(tnode!=node  && link->tonode!=tnode && link->tosock!= tsock) {
1608                                                         link->tonode= tnode;
1609                                                         link->tosock= tsock;
1610                                                         ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
1611                                                 }
1612                                         }
1613                                 }
1614                                 else {
1615                                         link->tonode= NULL;
1616                                         link->tosock= NULL;
1617                                 }
1618                         }
1619                         else {
1620                                 if(find_indicated_socket(snode, &tnode, &tsock, SOCK_OUT)) {
1621                                         if(nodeFindLink(snode->edittree, sock, tsock)==NULL) {
1622                                                 if(nodeCountSocketLinks(snode->edittree, tsock) < tsock->limit) {
1623                                                         if(tnode!=node && link->fromnode!=tnode && link->fromsock!= tsock) {
1624                                                                 link->fromnode= tnode;
1625                                                                 link->fromsock= tsock;
1626                                                                 ntreeSolveOrder(snode->edittree);       /* for interactive red line warning */
1627                                                         }
1628                                                 }
1629                                         }
1630                                 }
1631                                 else {
1632                                         link->fromnode= NULL;
1633                                         link->fromsock= NULL;
1634                                 }
1635                         }
1636                         /* hilight target sockets only */
1637                         node_socket_hilights(snode, in_out==SOCK_OUT?SOCK_IN:SOCK_OUT);
1638                         ED_region_tag_redraw(ar);
1639                         break;
1640                         
1641                 case LEFTMOUSE:
1642                 case RIGHTMOUSE:
1643                 case MIDDLEMOUSE:
1644         
1645                         /* remove link? */
1646                         if(link->tonode==NULL || link->fromnode==NULL) {
1647                                 nodeRemLink(snode->edittree, link);
1648                         }
1649                         else {
1650                                 /* send changed events for original tonode and new */
1651                                 if(link->tonode) 
1652                                         NodeTagChanged(snode->edittree, link->tonode);
1653                                 
1654                                 /* we might need to remove a link */
1655                                 if(in_out==SOCK_OUT) node_remove_extra_links(snode, link->tosock, link);
1656                         }
1657                         
1658                         ntreeSolveOrder(snode->edittree);
1659                         node_tree_verify_groups(snode->nodetree);
1660                         snode_handle_recalc(C, snode);
1661                         
1662                         MEM_freeN(op->customdata);
1663                         op->customdata= NULL;
1664                         
1665                         return OPERATOR_FINISHED;
1666         }
1667         
1668         return OPERATOR_RUNNING_MODAL;
1669 }
1670
1671 /* return 1 when socket clicked */
1672 static int node_link_init(SpaceNode *snode, NodeLinkDrag *nldrag)
1673 {
1674         bNodeLink *link;
1675         
1676         /* output indicated? */
1677         if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_OUT)) {
1678                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
1679                         return SOCK_OUT;
1680                 else {
1681                         /* find if we break a link */
1682                         for(link= snode->edittree->links.first; link; link= link->next) {
1683                                 if(link->fromsock==nldrag->sock)
1684                                         break;
1685                         }
1686                         if(link) {
1687                                 nldrag->node= link->tonode;
1688                                 nldrag->sock= link->tosock;
1689                                 nodeRemLink(snode->edittree, link);
1690                                 return SOCK_IN;
1691                         }
1692                 }
1693         }
1694         /* or an input? */
1695         else if(find_indicated_socket(snode, &nldrag->node, &nldrag->sock, SOCK_IN)) {
1696                 if(nodeCountSocketLinks(snode->edittree, nldrag->sock) < nldrag->sock->limit)
1697                         return SOCK_IN;
1698                 else {
1699                         /* find if we break a link */
1700                         for(link= snode->edittree->links.first; link; link= link->next) {
1701                                 if(link->tosock==nldrag->sock)
1702                                         break;
1703                         }
1704                         if(link) {
1705                                 /* send changed event to original tonode */
1706                                 if(link->tonode) 
1707                                         NodeTagChanged(snode->edittree, link->tonode);
1708                                 
1709                                 nldrag->node= link->fromnode;
1710                                 nldrag->sock= link->fromsock;
1711                                 nodeRemLink(snode->edittree, link);
1712                                 return SOCK_OUT;
1713                         }
1714                 }
1715         }
1716         
1717         return 0;
1718 }
1719
1720 static int node_link_invoke(bContext *C, wmOperator *op, wmEvent *event)
1721 {
1722         SpaceNode *snode= CTX_wm_space_node(C);
1723         ARegion *ar= CTX_wm_region(C);
1724         NodeLinkDrag *nldrag= MEM_callocN(sizeof(NodeLinkDrag), "drag link op customdata");
1725         
1726         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, 
1727                                                          &snode->mx, &snode->my);
1728
1729         nldrag->in_out= node_link_init(snode, nldrag);
1730                 
1731         if(nldrag->in_out) {
1732                 op->customdata= nldrag;
1733                 
1734                 /* we make a temporal link */
1735                 if(nldrag->in_out==SOCK_OUT)
1736                         nldrag->link= nodeAddLink(snode->edittree, nldrag->node, nldrag->sock, NULL, NULL);
1737                 else
1738                         nldrag->link= nodeAddLink(snode->edittree, NULL, NULL, nldrag->node, nldrag->sock);
1739                 
1740                 /* add modal handler */
1741                 WM_event_add_modal_handler(C, op);
1742                 
1743                 return OPERATOR_RUNNING_MODAL;
1744         }
1745         else {
1746                 MEM_freeN(nldrag);
1747                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1748         }
1749 }
1750
1751 void NODE_OT_link(wmOperatorType *ot)
1752 {
1753         /* identifiers */
1754         ot->name= "Link Nodes";
1755         ot->idname= "NODE_OT_link";
1756         
1757         /* api callbacks */
1758         ot->invoke= node_link_invoke;
1759         ot->modal= node_link_modal;
1760 //      ot->exec= node_link_exec;
1761         ot->poll= ED_operator_node_active;
1762         
1763         /* flags */
1764         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1765 }
1766
1767 /* ********************** Make Link operator ***************** */
1768
1769 /* makes a link between selected output and input sockets */
1770 static int node_make_link_exec(bContext *C, wmOperator *op)
1771 {
1772         SpaceNode *snode= CTX_wm_space_node(C);
1773         bNode *fromnode, *tonode;
1774         bNodeLink *link;
1775         bNodeSocket *outsock= snode->edittree->selout;
1776         bNodeSocket *insock= snode->edittree->selin;
1777         
1778         if (!insock || !outsock) {
1779                 BKE_report(op->reports, RPT_ERROR, "No input or output socket(s) selected");
1780                 return OPERATOR_CANCELLED;
1781         }
1782         if (nodeFindLink(snode->edittree, outsock, insock)) {
1783                 BKE_report(op->reports, RPT_ERROR, "There is already a link between these sockets");
1784                 return OPERATOR_CANCELLED;
1785         }
1786
1787         if (nodeFindNode(snode->edittree, outsock, &fromnode, NULL) &&
1788                 nodeFindNode(snode->edittree, insock, &tonode, NULL)) 
1789         {
1790                 link= nodeAddLink(snode->edittree, fromnode, outsock, tonode, insock);
1791                 NodeTagChanged(snode->edittree, tonode);
1792                 node_remove_extra_links(snode, insock, link);
1793         }
1794         else 
1795                 return OPERATOR_CANCELLED;
1796
1797         ntreeSolveOrder(snode->edittree);
1798         node_tree_verify_groups(snode->nodetree);
1799         snode_handle_recalc(C, snode);
1800         
1801         return OPERATOR_FINISHED;
1802 }
1803
1804 void NODE_OT_link_make(wmOperatorType *ot)
1805 {
1806         /* identifiers */
1807         ot->name= "Make Links";
1808         ot->description= "Makes a link between selected output in input sockets.";
1809         ot->idname= "NODE_OT_link_make";
1810         
1811         /* callbacks */
1812         ot->exec= node_make_link_exec;
1813         ot->poll= ED_operator_node_active; // XXX we need a special poll which checks that there are selected input/output sockets
1814         
1815         /* flags */
1816         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1817 }
1818
1819 /* ********************** Cut Link operator ***************** */
1820
1821 #define LINK_RESOL 12
1822 static int cut_links_intersect(bNodeLink *link, float mcoords[][2], int tot)
1823 {
1824         float coord_array[LINK_RESOL+1][2];
1825         int i, b;
1826         
1827         if(node_link_bezier_points(NULL, NULL, link, coord_array, LINK_RESOL)) {
1828
1829                 for(i=0; i<tot-1; i++)
1830                         for(b=0; b<LINK_RESOL-1; b++)
1831                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
1832                                         return 1;
1833         }
1834         return 0;
1835 }
1836
1837 static int cut_links_exec(bContext *C, wmOperator *op)
1838 {
1839         SpaceNode *snode= CTX_wm_space_node(C);
1840         ARegion *ar= CTX_wm_region(C);
1841         float mcoords[256][2];
1842         int i= 0;
1843         
1844         RNA_BEGIN(op->ptr, itemptr, "path") {
1845                 float loc[2];
1846                 
1847                 RNA_float_get_array(&itemptr, "loc", loc);
1848                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
1849                                                                  &mcoords[i][0], &mcoords[i][1]);
1850                 i++;
1851                 if(i>= 256) break;
1852         }
1853         RNA_END;
1854         
1855         if(i>1) {
1856                 bNodeLink *link, *next;
1857                 
1858                 for(link= snode->edittree->links.first; link; link= next) {
1859                         next= link->next;
1860                         
1861                         if(cut_links_intersect(link, mcoords, i)) {
1862                                 NodeTagChanged(snode->edittree, link->tonode);
1863                                 nodeRemLink(snode->edittree, link);
1864                         }
1865                 }
1866                 
1867                 ntreeSolveOrder(snode->edittree);
1868                 node_tree_verify_groups(snode->nodetree);
1869                 snode_handle_recalc(C, snode);
1870                 
1871                 return OPERATOR_FINISHED;
1872         }
1873         
1874         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
1875 }
1876
1877 void NODE_OT_links_cut(wmOperatorType *ot)
1878 {
1879         PropertyRNA *prop;
1880         
1881         ot->name= "Cut links";
1882         ot->idname= "NODE_OT_links_cut";
1883         
1884         ot->invoke= WM_gesture_lines_invoke;
1885         ot->modal= WM_gesture_lines_modal;
1886         ot->exec= cut_links_exec;
1887         
1888         ot->poll= ED_operator_node_active;
1889         
1890         /* flags */
1891         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1892         
1893         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
1894         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
1895         /* internal */
1896         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
1897 }
1898
1899 /* ******************************** */
1900 // XXX some code needing updating to operators...
1901
1902 /* goes over all scenes, reads render layerss */
1903 void node_read_renderlayers(SpaceNode *snode)
1904 {
1905         Scene *curscene= NULL; // XXX
1906         Scene *scene;
1907         bNode *node;
1908
1909         /* first tag scenes unread */
1910         for(scene= G.main->scene.first; scene; scene= scene->id.next) 
1911                 scene->id.flag |= LIB_DOIT;
1912
1913         for(node= snode->edittree->nodes.first; node; node= node->next) {
1914                 if(node->type==CMP_NODE_R_LAYERS) {
1915                         ID *id= node->id;
1916                         if(id->flag & LIB_DOIT) {
1917                                 RE_ReadRenderResult(curscene, (Scene *)id);
1918                                 ntreeCompositTagRender((Scene *)id);
1919                                 id->flag &= ~LIB_DOIT;
1920                         }
1921                 }
1922         }
1923         
1924         // XXX                  snode_handle_recalc(snode);
1925 }
1926
1927 void node_read_fullsamplelayers(SpaceNode *snode)
1928 {
1929         Scene *curscene= NULL; // XXX
1930         Render *re= RE_NewRender(curscene->id.name);
1931
1932         WM_cursor_wait(1);
1933
1934         //BIF_init_render_callbacks(re, 1);
1935         RE_MergeFullSample(re, curscene, snode->nodetree);
1936         //BIF_end_render_callbacks();
1937         
1938         // allqueue(REDRAWNODE, 1);
1939         // allqueue(REDRAWIMAGE, 1);
1940         
1941         WM_cursor_wait(0);
1942 }
1943
1944 void imagepaint_composite_tags(bNodeTree *ntree, Image *image, ImageUser *iuser)
1945 {
1946         bNode *node;
1947         
1948         if(ntree==NULL)
1949                 return;
1950         
1951         /* search for renderresults */
1952         if(image->type==IMA_TYPE_R_RESULT) {
1953                 for(node= ntree->nodes.first; node; node= node->next) {
1954                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
1955                                 /* imageuser comes from ImageWin, so indexes are offset 1 */
1956                                 if(node->custom1==iuser->layer-1)
1957                                         NodeTagChanged(ntree, node);
1958                         }
1959                 }
1960         }
1961         else {
1962                 for(node= ntree->nodes.first; node; node= node->next) {
1963                         if(node->id== &image->id)
1964                                 NodeTagChanged(ntree, node);
1965                 }
1966         }
1967 }
1968
1969 /* ****************** Make Group operator ******************* */
1970
1971 static int node_group_make_exec(bContext *C, wmOperator *op)
1972 {
1973         SpaceNode *snode = CTX_wm_space_node(C);
1974         bNode *gnode;
1975         
1976         if(snode->edittree!=snode->nodetree) {
1977                 BKE_report(op->reports, RPT_ERROR, "Can not add a new Group in a Group");
1978                 return OPERATOR_CANCELLED;
1979         }
1980         
1981         /* for time being... is too complex to handle */
1982         if(snode->treetype==NTREE_COMPOSIT) {
1983                 for(gnode=snode->nodetree->nodes.first; gnode; gnode= gnode->next) {
1984                         if(gnode->flag & SELECT)
1985                                 if(gnode->type==CMP_NODE_R_LAYERS)
1986                                         break;
1987                 }
1988                 
1989                 if(gnode) {
1990                         BKE_report(op->reports, RPT_ERROR, "Can not add RenderLayer in a Group");
1991                         return OPERATOR_CANCELLED;
1992                 }
1993         }
1994         
1995         gnode= nodeMakeGroupFromSelected(snode->nodetree);
1996         if(gnode==NULL) {
1997                 BKE_report(op->reports, RPT_ERROR, "Can not make Group");
1998                 return OPERATOR_CANCELLED;
1999         }
2000         else {
2001                 nodeSetActive(snode->nodetree, gnode);
2002                 ntreeSolveOrder(snode->nodetree);
2003         }
2004         
2005         snode_handle_recalc(C, snode);
2006         
2007         return OPERATOR_FINISHED;
2008 }
2009
2010 void NODE_OT_group_make(wmOperatorType *ot)
2011 {
2012         /* identifiers */
2013         ot->name = "Group";
2014         ot->description = "Make group from selected nodes.";
2015         ot->idname = "NODE_OT_group_make";
2016         
2017         /* api callbacks */
2018         ot->exec = node_group_make_exec;
2019         ot->poll = ED_operator_node_active;
2020         
2021         /* flags */
2022         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2023 }
2024
2025 /* ****************** Hide operator *********************** */
2026
2027 static int node_hide_exec(bContext *C, wmOperator *op)
2028 {
2029         SpaceNode *snode= CTX_wm_space_node(C);
2030         bNode *node;
2031         int nothidden=0, ishidden=0;
2032         
2033         /* sanity checking (poll callback checks this already) */
2034         if((snode == NULL) || (snode->edittree == NULL))
2035                 return OPERATOR_CANCELLED;
2036         
2037         for(node= snode->edittree->nodes.first; node; node= node->next) {
2038                 if(node->flag & SELECT) {
2039                         if(node->flag & NODE_HIDDEN)
2040                                 ishidden++;
2041                         else
2042                                 nothidden++;
2043                 }
2044         }
2045         for(node= snode->edittree->nodes.first; node; node= node->next) {
2046                 if(node->flag & SELECT) {
2047                         if( (ishidden && nothidden) || ishidden==0)
2048                                 node->flag |= NODE_HIDDEN;
2049                         else 
2050                                 node->flag &= ~NODE_HIDDEN;
2051                 }
2052         }
2053         
2054         snode_handle_recalc(C, snode);
2055         
2056         return OPERATOR_FINISHED;
2057 }
2058
2059 void NODE_OT_hide(wmOperatorType *ot)
2060 {
2061         /* identifiers */
2062         ot->name= "Hide";
2063         ot->description= "Toggle hiding of the nodes.";
2064         ot->idname= "NODE_OT_hide";
2065         
2066         /* callbacks */
2067         ot->exec= node_hide_exec;
2068         ot->poll= ED_operator_node_active;
2069         
2070         /* flags */
2071         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2072 }
2073
2074 /* ****************** Mute operator *********************** */
2075
2076 static int node_mute_exec(bContext *C, wmOperator *op)
2077 {
2078         SpaceNode *snode= CTX_wm_space_node(C);
2079         bNode *node;
2080
2081         /* no disabling inside of groups */
2082         if(node_tree_get_editgroup(snode->nodetree))
2083                 return OPERATOR_CANCELLED;
2084         
2085         for(node= snode->edittree->nodes.first; node; node= node->next) {
2086                 if(node->flag & SELECT) {
2087                         if(node->inputs.first && node->outputs.first) {
2088                                 node->flag ^= NODE_MUTED;
2089                         }
2090                 }
2091         }
2092         
2093         snode_handle_recalc(C, snode);
2094         
2095         return OPERATOR_FINISHED;
2096 }
2097
2098 void NODE_OT_mute(wmOperatorType *ot)
2099 {
2100         /* identifiers */
2101         ot->name= "Mute";
2102         ot->description= "Toggle muting of the nodes.";
2103         ot->idname= "NODE_OT_mute";
2104         
2105         /* callbacks */
2106         ot->exec= node_mute_exec;
2107         ot->poll= ED_operator_node_active;
2108         
2109         /* flags */
2110         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2111 }
2112
2113 /* ****************** Delete operator ******************* */
2114
2115 static int node_delete_exec(bContext *C, wmOperator *op)
2116 {
2117         SpaceNode *snode= CTX_wm_space_node(C);
2118         bNode *node, *next;
2119         bNodeSocket *sock;
2120         
2121         for(node= snode->edittree->nodes.first; node; node= next) {
2122                 next= node->next;
2123                 if(node->flag & SELECT) {
2124                         /* set selin and selout NULL if the sockets belong to a node to be deleted */
2125                         for(sock= node->inputs.first; sock; sock= sock->next)
2126                                 if(snode->edittree->selin == sock) snode->edittree->selin= NULL;
2127                         
2128                         for(sock= node->outputs.first; sock; sock= sock->next)
2129                                 if(snode->edittree->selout == sock) snode->edittree->selout= NULL;
2130                                 
2131                         /* check id user here, nodeFreeNode is called for free dbase too */
2132                         if(node->id)
2133                                 node->id->us--;
2134                         nodeFreeNode(snode->edittree, node);
2135                 }
2136         }
2137         
2138         node_tree_verify_groups(snode->nodetree);
2139
2140         snode_handle_recalc(C, snode);
2141         
2142         return OPERATOR_FINISHED;
2143 }
2144
2145 void NODE_OT_delete(wmOperatorType *ot)
2146 {
2147         /* identifiers */
2148         ot->name= "Delete";
2149         ot->description = "Delete selected nodes.";
2150         ot->idname= "NODE_OT_delete";
2151         
2152         /* api callbacks */
2153         ot->exec= node_delete_exec;
2154         ot->poll= ED_operator_node_active;
2155         
2156         /* flags */
2157         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2158 }
2159
2160 /* ****************** Show Cyclic Dependencies Operator  ******************* */
2161
2162 static int node_show_cycles_exec(bContext *C, wmOperator *op)
2163 {
2164         SpaceNode *snode= CTX_wm_space_node(C);
2165         
2166         /* this is just a wrapper around this call... */
2167         ntreeSolveOrder(snode->edittree);
2168         snode_handle_recalc(C, snode);
2169         
2170         return OPERATOR_FINISHED;
2171 }
2172
2173 void NODE_OT_show_cyclic_dependencies(wmOperatorType *ot)
2174 {
2175         /* identifiers */
2176         ot->name= "Show Cyclic Dependencies";
2177         ot->description= "Sort the nodes and show the cyclic dependencies between the nodes.";
2178         ot->idname= "NODE_OT_show_cyclic_dependencies";
2179         
2180         /* callbacks */
2181         ot->exec= node_show_cycles_exec;
2182         ot->poll= ED_operator_node_active;
2183         
2184         /* flags */
2185         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2186 }
2187
2188 #if 0
2189
2190 /* ******************** main event loop ****************** */
2191
2192 void winqreadnodespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2193 {
2194         if(val) {
2195                 switch(event) {
2196                 case CKEY:      /* sort again, showing cyclics */
2197                         ntreeSolveOrder(snode->edittree);
2198                         doredraw= 1;
2199                         break;
2200                 case EKEY:
2201                         // XXX                  snode_handle_recalc(snode);
2202                         break;
2203                 
2204                 case RKEY:
2205                         if(G.qual==LR_CTRLKEY) {
2206                                 node_rename(snode);
2207                         } 
2208                         else if(G.qual==LR_SHIFTKEY) {
2209                                 if(okee("Read saved Full Sample Layers"))
2210                                         node_read_fullsamplelayers(snode);
2211                         }
2212                         else {
2213                                 if(okee("Read saved Render Layers"))
2214                                         node_read_renderlayers(snode);
2215                         }
2216                         break;
2217                 }
2218         }
2219 }
2220 #endif
2221
2222