Small safety fix: clear temporary link list in node space when copying.
[blender.git] / source / blender / editors / space_node / space_node.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_node/space_node.c
30  *  \ingroup spnode
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "DNA_node_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_scene_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_rand.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_context.h"
50 #include "BKE_screen.h"
51 #include "BKE_node.h"
52
53 #include "ED_space_api.h"
54 #include "ED_render.h"
55 #include "ED_screen.h"
56
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64 #include "RNA_access.h"
65
66 #include "node_intern.h"        // own include
67
68 /* ******************** manage regions ********************* */
69
70 ARegion *node_has_buttons_region(ScrArea *sa)
71 {
72         ARegion *ar, *arnew;
73         
74         for(ar= sa->regionbase.first; ar; ar= ar->next)
75                 if(ar->regiontype==RGN_TYPE_UI)
76                         return ar;
77         
78         /* add subdiv level; after header */
79         for(ar= sa->regionbase.first; ar; ar= ar->next)
80                 if(ar->regiontype==RGN_TYPE_HEADER)
81                         break;
82         
83         /* is error! */
84         if(ar==NULL) return NULL;
85         
86         arnew= MEM_callocN(sizeof(ARegion), "buttons for node");
87         
88         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
89         arnew->regiontype= RGN_TYPE_UI;
90         arnew->alignment= RGN_ALIGN_RIGHT;
91         
92         arnew->flag = RGN_FLAG_HIDDEN;
93         
94         return arnew;
95 }
96
97 /* ******************** default callbacks for node space ***************** */
98
99 static SpaceLink *node_new(const bContext *UNUSED(C))
100 {
101         ARegion *ar;
102         SpaceNode *snode;
103         
104         snode= MEM_callocN(sizeof(SpaceNode), "initnode");
105         snode->spacetype= SPACE_NODE;   
106         
107         /* backdrop */
108         snode->zoom = 1.0f;
109         
110         /* header */
111         ar= MEM_callocN(sizeof(ARegion), "header for node");
112         
113         BLI_addtail(&snode->regionbase, ar);
114         ar->regiontype= RGN_TYPE_HEADER;
115         ar->alignment= RGN_ALIGN_BOTTOM;
116         
117         /* buttons/list view */
118         ar= MEM_callocN(sizeof(ARegion), "buttons for node");
119         
120         BLI_addtail(&snode->regionbase, ar);
121         ar->regiontype= RGN_TYPE_UI;
122         ar->alignment= RGN_ALIGN_RIGHT;
123         ar->flag = RGN_FLAG_HIDDEN;
124         
125         /* main area */
126         ar= MEM_callocN(sizeof(ARegion), "main area for node");
127         
128         BLI_addtail(&snode->regionbase, ar);
129         ar->regiontype= RGN_TYPE_WINDOW;
130         
131         ar->v2d.tot.xmin=  -256.0f;
132         ar->v2d.tot.ymin=  -256.0f;
133         ar->v2d.tot.xmax= 768.0f;
134         ar->v2d.tot.ymax= 768.0f;
135         
136         ar->v2d.cur.xmin=  -256.0f;
137         ar->v2d.cur.ymin=  -256.0f;
138         ar->v2d.cur.xmax= 768.0f;
139         ar->v2d.cur.ymax= 768.0f;
140         
141         ar->v2d.min[0]= 1.0f;
142         ar->v2d.min[1]= 1.0f;
143         
144         ar->v2d.max[0]= 32000.0f;
145         ar->v2d.max[1]= 32000.0f;
146         
147         ar->v2d.minzoom= 0.09f;
148         ar->v2d.maxzoom= 2.31f;
149         
150         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
151         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
152         ar->v2d.keeptot= 0;
153         
154         return (SpaceLink *)snode;
155 }
156
157 /* not spacelink itself */
158 static void node_free(SpaceLink *UNUSED(sl))
159 {       
160         
161 }
162
163
164 /* spacetype; init callback */
165 static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
166 {
167
168 }
169
170 static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
171 {
172         /* note, ED_area_tag_refresh will re-execute compositor */
173         SpaceNode *snode= sa->spacedata.first;
174         int type= snode->treetype;
175         
176         /* preview renders */
177         switch(wmn->category) {
178                 case NC_SCENE:
179                         switch (wmn->data) {
180                                 case ND_NODES:
181                                 case ND_FRAME:
182                                         ED_area_tag_refresh(sa);
183                                         break;
184                                 case ND_TRANSFORM_DONE:
185                                         if(type==NTREE_COMPOSIT) {
186                                                 if(snode->flag & SNODE_AUTO_RENDER) {
187                                                         snode->recalc= 1;
188                                                         ED_area_tag_refresh(sa);
189                                                 }
190                                         }
191                                         break;
192                         }
193                         break;
194                 case NC_WM:
195                         if(wmn->data==ND_FILEREAD)
196                                 ED_area_tag_refresh(sa);
197                         break;
198                 
199                 /* future: add ID checks? */
200                 case NC_MATERIAL:
201                         if(type==NTREE_SHADER) {
202                                 if(wmn->data==ND_SHADING)
203                                         ED_area_tag_refresh(sa);
204                                 else if(wmn->data==ND_SHADING_DRAW)
205                                         ED_area_tag_refresh(sa);
206                         }
207                         break;
208                 case NC_TEXTURE:
209                         if(type==NTREE_SHADER || type==NTREE_TEXTURE) {
210                                 if(wmn->data==ND_NODES)
211                                         ED_area_tag_refresh(sa);
212                         }
213                         break;
214                 case NC_TEXT:
215                         /* pynodes */
216                         if(wmn->data==ND_SHADING)
217                                 ED_area_tag_refresh(sa);
218                         break;
219                 case NC_SPACE:
220                         if(wmn->data==ND_SPACE_NODE)
221                                 ED_area_tag_refresh(sa);
222                         else if(wmn->data==ND_SPACE_NODE_VIEW)
223                                 ED_area_tag_redraw(sa);
224                         break;
225                 case NC_NODE:
226                         if (wmn->action == NA_EDITED)
227                                 ED_area_tag_refresh(sa);
228                         break;
229
230                 case NC_IMAGE:
231                         if (wmn->action == NA_EDITED) {
232                                 if(type==NTREE_COMPOSIT) {
233                                         Scene *scene= wmn->window->screen->scene;
234                                         
235                                         /* note that NodeTagIDChanged is alredy called by BKE_image_signal() on all
236                                          * scenes so really this is just to know if the images is used in the compo else
237                                          * painting on images could become very slow when the compositor is open. */
238                                         if(NodeTagIDChanged(scene->nodetree, wmn->reference))
239                                                 ED_area_tag_refresh(sa);
240                                 }
241                         }
242                         break;
243         }
244 }
245
246 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
247 {
248         /* default now: refresh node is starting preview */
249         SpaceNode *snode= sa->spacedata.first;
250
251         snode_set_context(snode, CTX_data_scene(C));
252         
253         if(snode->nodetree) {
254                 if(snode->treetype==NTREE_SHADER) {
255                         Material *ma= (Material *)snode->id;
256                         if(ma->use_nodes)
257                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
258                 }
259                 else if(snode->treetype==NTREE_COMPOSIT) {
260                         Scene *scene= (Scene *)snode->id;
261                         if(scene->use_nodes) {
262                                 /* recalc is set on 3d view changes for auto compo */
263                                 if(snode->recalc) {
264                                         snode->recalc= 0;
265                                         node_render_changed_exec((struct bContext*)C, NULL);
266                                 }
267                                 else 
268                                         snode_composite_job(C, sa);
269                         }
270                 }
271                 else if(snode->treetype==NTREE_TEXTURE) {
272                         Tex *tex= (Tex *)snode->id;
273                         if(tex->use_nodes) {
274                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
275                         }
276                 }
277         }
278 }
279
280 static SpaceLink *node_duplicate(SpaceLink *sl)
281 {
282         SpaceNode *snoden= MEM_dupallocN(sl);
283         
284         /* clear or remove stuff from old */
285         snoden->nodetree= NULL;
286         snoden->linkdrag.first= snoden->linkdrag.last= NULL;
287         
288         return (SpaceLink *)snoden;
289 }
290
291
292 /* add handlers, stuff you only do once or on area/region changes */
293 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
294 {
295         wmKeyMap *keymap;
296
297         ED_region_panels_init(wm, ar);
298         
299         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
300         WM_event_add_keymap_handler(&ar->handlers, keymap);
301 }
302
303 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
304 {
305         ED_region_panels(C, ar, 1, NULL, -1);
306 }
307
308 /* Initialise main area, setting handlers. */
309 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
310 {
311         wmKeyMap *keymap;
312         ListBase *lb;
313         
314         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
315         
316         /* own keymaps */
317         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
318         WM_event_add_keymap_handler(&ar->handlers, keymap);
319         
320         keymap= WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
321         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
322         
323         /* add drop boxes */
324         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
325         
326         WM_event_add_dropbox_handler(&ar->handlers, lb);
327 }
328
329 static void node_main_area_draw(const bContext *C, ARegion *ar)
330 {
331         View2D *v2d= &ar->v2d;
332         
333         drawnodespace(C, ar, v2d);
334 }
335
336
337 /* ************* dropboxes ************* */
338
339 static int node_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
340 {
341         if(drag->type==WM_DRAG_ID) {
342                 ID *id= (ID *)drag->poin;
343                 if( GS(id->name)==ID_IM )
344                         return 1;
345         }
346         else if(drag->type==WM_DRAG_PATH){
347                 if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
348                         return 1;
349         }
350         return 0;
351 }
352
353 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
354 {
355         ID *id= (ID *)drag->poin;
356         
357         if(id) {
358                 RNA_string_set(drop->ptr, "name", id->name+2);
359         }
360         if (drag->path[0]) {
361                 RNA_string_set(drop->ptr, "filepath", drag->path);
362         }
363 }
364
365 /* this region dropbox definition */
366 static void node_dropboxes(void)
367 {
368         ListBase *lb= WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
369         
370         WM_dropbox_add(lb, "NODE_OT_add_file", node_drop_poll, node_id_path_drop_copy);
371         
372 }
373
374 /* ************* end drop *********** */
375
376
377 /* add handlers, stuff you only do once or on area/region changes */
378 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
379 {
380         ED_region_header_init(ar);
381 }
382
383 static void node_header_area_draw(const bContext *C, ARegion *ar)
384 {
385         SpaceNode *snode= CTX_wm_space_node(C);
386         Scene *scene= CTX_data_scene(C);
387
388         /* find and set the context */
389         snode_set_context(snode, scene);
390
391         ED_region_header(C, ar);
392 }
393
394 /* used for header + main area */
395 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
396 {
397         /* context changes */
398         switch(wmn->category) {
399                 case NC_SPACE:
400                         if(wmn->data==ND_SPACE_NODE)
401                                 ED_region_tag_redraw(ar);
402                         break;
403                 case NC_SCREEN:
404                         if(wmn->data == ND_GPENCIL)     
405                                 ED_region_tag_redraw(ar);
406                         break;
407                 case NC_SCENE:
408                 case NC_MATERIAL:
409                 case NC_TEXTURE:
410                 case NC_NODE:
411                         ED_region_tag_redraw(ar);
412                         break;
413                 case NC_ID:
414                         if(wmn->action == NA_RENAME)
415                                 ED_region_tag_redraw(ar);
416                         break;
417         }
418 }
419
420 const char *node_context_dir[] = {"selected_nodes", NULL};
421
422 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
423 {
424         SpaceNode *snode= CTX_wm_space_node(C);
425         
426         if(CTX_data_dir(member)) {
427                 CTX_data_dir_set(result, node_context_dir);
428                 return 1;
429         }
430         else if(CTX_data_equals(member, "selected_nodes")) {
431                 bNode *node;
432                 
433                 for(next_node(snode->edittree); (node=next_node(NULL));) {
434                         if(node->flag & SELECT) {
435                                 CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
436                         }
437                 }
438                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
439                 return 1;
440         }
441         
442         return 0;
443 }
444
445 /* only called once, from space/spacetypes.c */
446 void ED_spacetype_node(void)
447 {
448         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
449         ARegionType *art;
450         
451         st->spaceid= SPACE_NODE;
452         strncpy(st->name, "Node", BKE_ST_MAXNAME);
453         
454         st->new= node_new;
455         st->free= node_free;
456         st->init= node_init;
457         st->duplicate= node_duplicate;
458         st->operatortypes= node_operatortypes;
459         st->keymap= node_keymap;
460         st->listener= node_area_listener;
461         st->refresh= node_area_refresh;
462         st->context= node_context;
463         st->dropboxes = node_dropboxes;
464         
465         /* regions: main window */
466         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
467         art->regionid = RGN_TYPE_WINDOW;
468         art->init= node_main_area_init;
469         art->draw= node_main_area_draw;
470         art->listener= node_region_listener;
471         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
472
473         BLI_addhead(&st->regiontypes, art);
474         
475         /* regions: header */
476         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
477         art->regionid = RGN_TYPE_HEADER;
478         art->prefsizey= HEADERY;
479         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
480         art->listener= node_region_listener;
481         art->init= node_header_area_init;
482         art->draw= node_header_area_draw;
483         
484         BLI_addhead(&st->regiontypes, art);
485
486         node_menus_register();
487         
488         /* regions: listview/buttons */
489         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
490         art->regionid = RGN_TYPE_UI;
491         art->prefsizex= 180; // XXX
492         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
493         art->listener= node_region_listener;
494         art->init= node_buttons_area_init;
495         art->draw= node_buttons_area_draw;
496         BLI_addhead(&st->regiontypes, art);
497         
498         node_buttons_register(art);
499         
500         BKE_spacetype_register(st);
501 }
502