62cb0bc73cd27e629d257a515da22cab374d0aa4
[blender-staging.git] / source / blender / editors / space_node / space_node.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_node/space_node.c
28  *  \ingroup spnode
29  */
30
31 #include "DNA_lamp_types.h"
32 #include "DNA_material_types.h"
33 #include "DNA_node_types.h"
34 #include "DNA_world_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_math.h"
40
41 #include "BKE_context.h"
42 #include "BKE_library.h"
43 #include "BKE_scene.h"
44 #include "BKE_screen.h"
45 #include "BKE_node.h"
46
47 #include "ED_space_api.h"
48 #include "ED_node.h"
49 #include "ED_render.h"
50 #include "ED_screen.h"
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "UI_resources.h"
55 #include "UI_view2d.h"
56
57 #include "RNA_access.h"
58
59 #include "node_intern.h"  /* own include */
60
61
62 /* ******************** tree path ********************* */
63
64 void ED_node_tree_start(SpaceNode *snode, bNodeTree *ntree, ID *id, ID *from)
65 {
66         bNodeTreePath *path, *path_next;
67         for (path = snode->treepath.first; path; path = path_next) {
68                 path_next = path->next;
69                 MEM_freeN(path);
70         }
71         BLI_listbase_clear(&snode->treepath);
72         
73         if (ntree) {
74                 path = MEM_callocN(sizeof(bNodeTreePath), "node tree path");
75                 path->nodetree = ntree;
76                 path->parent_key = NODE_INSTANCE_KEY_BASE;
77                 
78                 /* copy initial offset from bNodeTree */
79                 copy_v2_v2(path->view_center, ntree->view_center);
80                 
81                 if (id)
82                         BLI_strncpy(path->node_name, id->name + 2, sizeof(path->node_name));
83                 
84                 BLI_addtail(&snode->treepath, path);
85                 
86                 id_us_ensure_real(&ntree->id);
87         }
88         
89         /* update current tree */
90         snode->nodetree = snode->edittree = ntree;
91         snode->id = id;
92         snode->from = from;
93         
94         ED_node_set_active_viewer_key(snode);
95         
96         WM_main_add_notifier(NC_SCENE | ND_NODES, NULL);
97 }
98
99 void ED_node_tree_push(SpaceNode *snode, bNodeTree *ntree, bNode *gnode)
100 {
101         bNodeTreePath *path = MEM_callocN(sizeof(bNodeTreePath), "node tree path");
102         bNodeTreePath *prev_path = snode->treepath.last;
103         path->nodetree = ntree;
104         if (gnode) {
105                 if (prev_path)
106                         path->parent_key = BKE_node_instance_key(prev_path->parent_key, prev_path->nodetree, gnode);
107                 else
108                         path->parent_key = NODE_INSTANCE_KEY_BASE;
109                 
110                 BLI_strncpy(path->node_name, gnode->name, sizeof(path->node_name));
111         }
112         else
113                 path->parent_key = NODE_INSTANCE_KEY_BASE;
114         
115         /* copy initial offset from bNodeTree */
116         copy_v2_v2(path->view_center, ntree->view_center);
117         
118         BLI_addtail(&snode->treepath, path);
119         
120         id_us_ensure_real(&ntree->id);
121         
122         /* update current tree */
123         snode->edittree = ntree;
124         
125         ED_node_set_active_viewer_key(snode);
126         
127         WM_main_add_notifier(NC_SCENE | ND_NODES, NULL);
128 }
129
130 void ED_node_tree_pop(SpaceNode *snode)
131 {
132         bNodeTreePath *path = snode->treepath.last;
133         
134         /* don't remove root */
135         if (path == snode->treepath.first)
136                 return;
137         
138         BLI_remlink(&snode->treepath, path);
139         MEM_freeN(path);
140         
141         /* update current tree */
142         path = snode->treepath.last;
143         snode->edittree = path->nodetree;
144         
145         ED_node_set_active_viewer_key(snode);
146         
147         /* listener updates the View2D center from edittree */
148         WM_main_add_notifier(NC_SCENE | ND_NODES, NULL);
149 }
150
151 int ED_node_tree_depth(SpaceNode *snode)
152 {
153         return BLI_listbase_count(&snode->treepath);
154 }
155
156 bNodeTree *ED_node_tree_get(SpaceNode *snode, int level)
157 {
158         bNodeTreePath *path;
159         int i;
160         for (path = snode->treepath.last, i = 0; path; path = path->prev, ++i) {
161                 if (i == level)
162                         return path->nodetree;
163         }
164         return NULL;
165 }
166
167 int ED_node_tree_path_length(SpaceNode *snode)
168 {
169         bNodeTreePath *path;
170         int length = 0;
171         int i;
172         for (path = snode->treepath.first, i = 0; path; path = path->next, ++i) {
173                 length += strlen(path->node_name);
174                 if (i > 0)
175                         length += 1;    /* for separator char */
176         }
177         return length;
178 }
179
180 void ED_node_tree_path_get(SpaceNode *snode, char *value)
181 {
182         bNodeTreePath *path;
183         int i;
184         
185         value[0] = '\0';
186         for (path = snode->treepath.first, i = 0; path; path = path->next, ++i) {
187                 if (i == 0) {
188                         strcpy(value, path->node_name);
189                         value += strlen(path->node_name);
190                 }
191                 else {
192                         sprintf(value, "/%s", path->node_name);
193                         value += strlen(path->node_name) + 1;
194                 }
195         }
196 }
197
198 void ED_node_tree_path_get_fixedbuf(SpaceNode *snode, char *value, int max_length)
199 {
200         bNodeTreePath *path;
201         int size, i;
202         
203         value[0] = '\0';
204         for (path = snode->treepath.first, i = 0; path; path = path->next, ++i) {
205                 if (i == 0) {
206                         BLI_strncpy(value, path->node_name, max_length);
207                         size = strlen(path->node_name);
208                 }
209                 else {
210                         BLI_snprintf(value, max_length, "/%s", path->node_name);
211                         size = strlen(path->node_name) + 1;
212                 }
213                 max_length -= size;
214                 if (max_length <= 0)
215                         break;
216                 value += size;
217         }
218 }
219
220 void ED_node_set_active_viewer_key(SpaceNode *snode)
221 {
222         bNodeTreePath *path = snode->treepath.last;
223         if (snode->nodetree && path) {
224                 snode->nodetree->active_viewer_key = path->parent_key;
225         }
226 }
227
228 void snode_group_offset(SpaceNode *snode, float *x, float *y)
229 {
230         bNodeTreePath *path = snode->treepath.last;
231         
232         if (path && path->prev) {
233                 float dcenter[2];
234                 sub_v2_v2v2(dcenter, path->view_center, path->prev->view_center);
235                 *x = dcenter[0];
236                 *y = dcenter[1];
237         }
238         else
239                 *x = *y = 0.0f;
240 }
241
242 /* ******************** manage regions ********************* */
243
244 ARegion *node_has_buttons_region(ScrArea *sa)
245 {
246         ARegion *ar, *arnew;
247
248         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
249         if (ar) return ar;
250
251         /* add subdiv level; after header */
252         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
253
254         /* is error! */
255         if (ar == NULL) return NULL;
256
257         arnew = MEM_callocN(sizeof(ARegion), "buttons for node");
258
259         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
260         arnew->regiontype = RGN_TYPE_UI;
261         arnew->alignment = RGN_ALIGN_RIGHT;
262
263         arnew->flag = RGN_FLAG_HIDDEN;
264
265         return arnew;
266 }
267
268 ARegion *node_has_tools_region(ScrArea *sa)
269 {
270         ARegion *ar, *arnew;
271         
272         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
273         if (ar) return ar;
274         
275         /* add subdiv level; after header */
276         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
277         
278         /* is error! */
279         if (ar == NULL) return NULL;
280         
281         arnew = MEM_callocN(sizeof(ARegion), "node tools");
282         
283         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
284         arnew->regiontype = RGN_TYPE_TOOLS;
285         arnew->alignment = RGN_ALIGN_LEFT;
286         
287         arnew->flag = RGN_FLAG_HIDDEN;
288         
289         return arnew;
290 }
291
292 /* ******************** default callbacks for node space ***************** */
293
294 static SpaceLink *node_new(const bContext *UNUSED(C))
295 {
296         ARegion *ar;
297         SpaceNode *snode;
298
299         snode = MEM_callocN(sizeof(SpaceNode), "initnode");
300         snode->spacetype = SPACE_NODE;
301
302         snode->flag = SNODE_SHOW_GPENCIL | SNODE_USE_ALPHA;
303
304         /* backdrop */
305         snode->zoom = 1.0f;
306
307         /* select the first tree type for valid type */
308         NODE_TREE_TYPES_BEGIN (treetype)
309         {
310                 strcpy(snode->tree_idname, treetype->idname);
311                 break;
312         }
313         NODE_TREE_TYPES_END;
314
315         /* header */
316         ar = MEM_callocN(sizeof(ARegion), "header for node");
317
318         BLI_addtail(&snode->regionbase, ar);
319         ar->regiontype = RGN_TYPE_HEADER;
320         ar->alignment = RGN_ALIGN_BOTTOM;
321
322         /* buttons/list view */
323         ar = MEM_callocN(sizeof(ARegion), "buttons for node");
324
325         BLI_addtail(&snode->regionbase, ar);
326         ar->regiontype = RGN_TYPE_UI;
327         ar->alignment = RGN_ALIGN_RIGHT;
328
329         /* toolbar */
330         ar = MEM_callocN(sizeof(ARegion), "node tools");
331
332         BLI_addtail(&snode->regionbase, ar);
333         ar->regiontype = RGN_TYPE_TOOLS;
334         ar->alignment = RGN_ALIGN_LEFT;
335
336         ar->flag = RGN_FLAG_HIDDEN;
337
338         /* main area */
339         ar = MEM_callocN(sizeof(ARegion), "main area for node");
340
341         BLI_addtail(&snode->regionbase, ar);
342         ar->regiontype = RGN_TYPE_WINDOW;
343
344         ar->v2d.tot.xmin =  -12.8f * U.widget_unit;
345         ar->v2d.tot.ymin =  -12.8f * U.widget_unit;
346         ar->v2d.tot.xmax = 38.4f * U.widget_unit;
347         ar->v2d.tot.ymax = 38.4f * U.widget_unit;
348
349         ar->v2d.cur =  ar->v2d.tot;
350
351         ar->v2d.min[0] = 1.0f;
352         ar->v2d.min[1] = 1.0f;
353
354         ar->v2d.max[0] = 32000.0f;
355         ar->v2d.max[1] = 32000.0f;
356
357         ar->v2d.minzoom = 0.09f;
358         ar->v2d.maxzoom = 2.31f;
359
360         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
361         ar->v2d.keepzoom = V2D_LIMITZOOM | V2D_KEEPASPECT;
362         ar->v2d.keeptot = 0;
363
364         return (SpaceLink *)snode;
365 }
366
367 static void node_free(SpaceLink *sl)
368 {
369         SpaceNode *snode = (SpaceNode *)sl;
370         bNodeTreePath *path, *path_next;
371
372         for (path = snode->treepath.first; path; path = path_next) {
373                 path_next = path->next;
374                 MEM_freeN(path);
375         }
376 }
377
378
379 /* spacetype; init callback */
380 static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
381 {
382
383 }
384
385 static void node_area_listener(bScreen *sc, ScrArea *sa, wmNotifier *wmn)
386 {
387         /* note, ED_area_tag_refresh will re-execute compositor */
388         SpaceNode *snode = sa->spacedata.first;
389         /* shaderfrom is only used for new shading nodes, otherwise all shaders are from objects */
390         short shader_type = BKE_scene_use_new_shading_nodes(sc->scene) ? snode->shaderfrom : SNODE_SHADER_OBJECT;
391
392         /* preview renders */
393         switch (wmn->category) {
394                 case NC_SCENE:
395                         switch (wmn->data) {
396                                 case ND_NODES:
397                                 {
398                                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
399                                         bNodeTreePath *path = snode->treepath.last;
400                                         /* shift view to node tree center */
401                                         if (ar && path)
402                                                 UI_view2d_center_set(&ar->v2d, path->view_center[0], path->view_center[1]);
403                                         
404                                         ED_area_tag_refresh(sa);
405                                         break;
406                                 }
407                                 case ND_FRAME:
408                                         ED_area_tag_refresh(sa);
409                                         break;
410                                 case ND_COMPO_RESULT:
411                                         ED_area_tag_redraw(sa);
412                                         break;
413                                 case ND_TRANSFORM_DONE:
414                                         if (ED_node_is_compositor(snode)) {
415                                                 if (snode->flag & SNODE_AUTO_RENDER) {
416                                                         snode->recalc = 1;
417                                                         ED_area_tag_refresh(sa);
418                                                 }
419                                         }
420                                         break;
421                                 case ND_LAYER_CONTENT:
422                                         ED_area_tag_refresh(sa);
423                                         break;
424                         }
425                         break;
426
427                 /* future: add ID checks? */
428                 case NC_MATERIAL:
429                         if (ED_node_is_shader(snode)) {
430                                 if (wmn->data == ND_SHADING)
431                                         ED_area_tag_refresh(sa);
432                                 else if (wmn->data == ND_SHADING_DRAW)
433                                         ED_area_tag_refresh(sa);
434                                 else if (wmn->data == ND_SHADING_LINKS)
435                                         ED_area_tag_refresh(sa);
436                                 else if (wmn->action == NA_ADDED && snode->edittree)
437                                         nodeSetActiveID(snode->edittree, ID_MA, wmn->reference);
438
439                         }
440                         break;
441                 case NC_TEXTURE:
442                         if (ED_node_is_shader(snode) || ED_node_is_texture(snode)) {
443                                 if (wmn->data == ND_NODES)
444                                         ED_area_tag_refresh(sa);
445                         }
446                         break;
447                 case NC_WORLD:
448                         if (ED_node_is_shader(snode) && shader_type == SNODE_SHADER_WORLD) {
449                                 ED_area_tag_refresh(sa);
450                         }
451                         break;
452                 case NC_OBJECT:
453                         if (ED_node_is_shader(snode)) {
454                                 if (wmn->data == ND_OB_SHADING)
455                                         ED_area_tag_refresh(sa);
456                         }
457                         break;
458                 case NC_SPACE:
459                         if (wmn->data == ND_SPACE_NODE)
460                                 ED_area_tag_refresh(sa);
461                         else if (wmn->data == ND_SPACE_NODE_VIEW)
462                                 ED_area_tag_redraw(sa);
463                         break;
464                 case NC_NODE:
465                         if (wmn->action == NA_EDITED)
466                                 ED_area_tag_refresh(sa);
467                         else if (wmn->action == NA_SELECTED)
468                                 ED_area_tag_redraw(sa);
469                         break;
470                 case NC_SCREEN:
471                         switch (wmn->data) {
472                                 case ND_ANIMPLAY:
473                                         ED_area_tag_refresh(sa);
474                                         break;
475                         }
476                         break;
477                 case NC_MASK:
478                         if (wmn->action == NA_EDITED) {
479                                 if (snode->nodetree && snode->nodetree->type == NTREE_COMPOSIT) {
480                                         ED_area_tag_refresh(sa);
481                                 }
482                         }
483                         break;
484
485                 case NC_IMAGE:
486                         if (wmn->action == NA_EDITED) {
487                                 if (ED_node_is_compositor(snode)) {
488                                         /* note that nodeUpdateID is already called by BKE_image_signal() on all
489                                          * scenes so really this is just to know if the images is used in the compo else
490                                          * painting on images could become very slow when the compositor is open. */
491                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
492                                                 ED_area_tag_refresh(sa);
493                                 }
494                         }
495                         break;
496
497                 case NC_MOVIECLIP:
498                         if (wmn->action == NA_EDITED) {
499                                 if (ED_node_is_compositor(snode)) {
500                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
501                                                 ED_area_tag_refresh(sa);
502                                 }
503                         }
504                         break;
505
506                 case NC_LINESTYLE:
507                         if (ED_node_is_shader(snode) && shader_type == SNODE_SHADER_LINESTYLE) {
508                                 ED_area_tag_refresh(sa);
509                         }
510                         break;
511                 case NC_WM:
512                         if (wmn->data == ND_UNDO) {
513                                 ED_area_tag_refresh(sa);
514                         }
515                         break;
516                 case NC_GPENCIL:
517                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
518                                 ED_area_tag_redraw(sa);
519                         }
520                         break;
521         }
522 }
523
524 static void node_area_refresh(const struct bContext *C, ScrArea *sa)
525 {
526         /* default now: refresh node is starting preview */
527         SpaceNode *snode = sa->spacedata.first;
528         
529         snode_set_context(C);
530
531         if (snode->nodetree) {
532                 if (snode->nodetree->type == NTREE_SHADER) {
533                         if (GS(snode->id->name) == ID_MA) {
534                                 Material *ma = (Material *)snode->id;
535                                 if (ma->use_nodes)
536                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
537                         }
538                         else if (GS(snode->id->name) == ID_LA) {
539                                 Lamp *la = (Lamp *)snode->id;
540                                 if (la->use_nodes)
541                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
542                         }
543                         else if (GS(snode->id->name) == ID_WO) {
544                                 World *wo = (World *)snode->id;
545                                 if (wo->use_nodes)
546                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
547                         }
548                 }
549                 else if (snode->nodetree->type == NTREE_COMPOSIT) {
550                         Scene *scene = (Scene *)snode->id;
551                         if (scene->use_nodes) {
552                                 /* recalc is set on 3d view changes for auto compo */
553                                 if (snode->recalc) {
554                                         snode->recalc = 0;
555                                         node_render_changed_exec((struct bContext *)C, NULL);
556                                 }
557                                 else {
558                                         ED_node_composite_job(C, snode->nodetree, scene);
559                                 }
560                         }
561                 }
562                 else if (snode->nodetree->type == NTREE_TEXTURE) {
563                         Tex *tex = (Tex *)snode->id;
564                         if (tex->use_nodes) {
565                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
566                         }
567                 }
568         }
569 }
570
571 static SpaceLink *node_duplicate(SpaceLink *sl)
572 {
573         SpaceNode *snode = (SpaceNode *)sl;
574         SpaceNode *snoden = MEM_dupallocN(snode);
575
576         BLI_duplicatelist(&snoden->treepath, &snode->treepath);
577
578         /* clear or remove stuff from old */
579         BLI_listbase_clear(&snoden->linkdrag);
580
581         /* Note: no need to set node tree user counts,
582          * the editor only keeps at least 1 (id_us_ensure_real),
583          * which is already done by the original SpaceNode.
584          */
585
586         return (SpaceLink *)snoden;
587 }
588
589
590 /* add handlers, stuff you only do once or on area/region changes */
591 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
592 {
593         wmKeyMap *keymap;
594
595         ED_region_panels_init(wm, ar);
596
597         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
598         WM_event_add_keymap_handler(&ar->handlers, keymap);
599 }
600
601 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
602 {
603         ED_region_panels(C, ar, 1, NULL, -1);
604 }
605
606 /* add handlers, stuff you only do once or on area/region changes */
607 static void node_toolbar_area_init(wmWindowManager *wm, ARegion *ar)
608 {
609         wmKeyMap *keymap;
610
611         ED_region_panels_init(wm, ar);
612
613         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
614         WM_event_add_keymap_handler(&ar->handlers, keymap);
615 }
616
617 static void node_toolbar_area_draw(const bContext *C, ARegion *ar)
618 {
619         ED_region_panels(C, ar, 1, NULL, -1);
620 }
621
622 static void node_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
623 {
624         SpaceNode *snode = sa->spacedata.first;
625
626         /* convert mouse coordinates to v2d space */
627         UI_view2d_region_to_view(&ar->v2d, win->eventstate->x - ar->winrct.xmin, win->eventstate->y - ar->winrct.ymin,
628                                  &snode->cursor[0], &snode->cursor[1]);
629         
630         /* here snode->cursor is used to detect the node edge for sizing */
631         node_set_cursor(win, snode, snode->cursor);
632
633         /* XXX snode->cursor is in placing new nodes space */
634         snode->cursor[0] /= UI_DPI_FAC;
635         snode->cursor[1] /= UI_DPI_FAC;
636         
637 }
638
639 /* Initialize main area, setting handlers. */
640 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
641 {
642         wmKeyMap *keymap;
643         ListBase *lb;
644
645         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
646
647         /* own keymaps */
648         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
649         WM_event_add_keymap_handler(&ar->handlers, keymap);
650
651         keymap = WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
652         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
653
654         /* add drop boxes */
655         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
656
657         WM_event_add_dropbox_handler(&ar->handlers, lb);
658 }
659
660 static void node_main_area_draw(const bContext *C, ARegion *ar)
661 {
662         drawnodespace(C, ar);
663 }
664
665
666 /* ************* dropboxes ************* */
667
668 static int node_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
669 {
670         if (drag->type == WM_DRAG_ID) {
671                 ID *id = drag->poin;
672                 if (GS(id->name) == ID_IM)
673                         return 1;
674         }
675         else if (drag->type == WM_DRAG_PATH) {
676                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE))   /* rule might not work? */
677                         return 1;
678         }
679         return 0;
680 }
681
682 static int node_mask_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
683 {
684         if (drag->type == WM_DRAG_ID) {
685                 ID *id = drag->poin;
686                 if (GS(id->name) == ID_MSK)
687                         return 1;
688         }
689         return 0;
690 }
691
692 static void node_id_drop_copy(wmDrag *drag, wmDropBox *drop)
693 {
694         ID *id = drag->poin;
695
696         RNA_string_set(drop->ptr, "name", id->name + 2);
697 }
698
699 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
700 {
701         ID *id = drag->poin;
702
703         if (id) {
704                 RNA_string_set(drop->ptr, "name", id->name + 2);
705                 RNA_struct_property_unset(drop->ptr, "filepath");
706         }
707         else if (drag->path[0]) {
708                 RNA_string_set(drop->ptr, "filepath", drag->path);
709                 RNA_struct_property_unset(drop->ptr, "name");
710         }
711 }
712
713 /* this region dropbox definition */
714 static void node_dropboxes(void)
715 {
716         ListBase *lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
717
718         WM_dropbox_add(lb, "NODE_OT_add_file", node_ima_drop_poll, node_id_path_drop_copy);
719         WM_dropbox_add(lb, "NODE_OT_add_mask", node_mask_drop_poll, node_id_drop_copy);
720
721 }
722
723 /* ************* end drop *********** */
724
725
726 /* add handlers, stuff you only do once or on area/region changes */
727 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
728 {
729         ED_region_header_init(ar);
730 }
731
732 static void node_header_area_draw(const bContext *C, ARegion *ar)
733 {
734         /* find and set the context */
735         snode_set_context(C);
736
737         ED_region_header(C, ar);
738 }
739
740 /* used for header + main area */
741 static void node_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
742 {
743         /* context changes */
744         switch (wmn->category) {
745                 case NC_SPACE:
746                         if (wmn->data == ND_SPACE_NODE)
747                                 ED_region_tag_redraw(ar);
748                         break;
749                 case NC_SCREEN:
750                         switch (wmn->data) {
751                                 case ND_SCREENCAST:
752                                 case ND_ANIMPLAY:
753                                         ED_region_tag_redraw(ar);
754                                         break;
755                         }
756                         break;
757                 case NC_SCENE:
758                 case NC_MATERIAL:
759                 case NC_TEXTURE:
760                 case NC_WORLD:
761                 case NC_NODE:
762                 case NC_LINESTYLE:
763                         ED_region_tag_redraw(ar);
764                         break;
765                 case NC_OBJECT:
766                         if (wmn->data == ND_OB_SHADING)
767                                 ED_region_tag_redraw(ar);
768                         break;
769                 case NC_ID:
770                         if (wmn->action == NA_RENAME)
771                                 ED_region_tag_redraw(ar);
772                         break;
773                 case NC_GPENCIL:
774                         if (wmn->action == NA_EDITED)
775                                 ED_region_tag_redraw(ar);
776                         else if (wmn->data & ND_GPENCIL_EDITMODE)
777                                 ED_region_tag_redraw(ar);
778                         break;
779         }
780 }
781
782 const char *node_context_dir[] = {"selected_nodes", "active_node", NULL};
783
784 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
785 {
786         SpaceNode *snode = CTX_wm_space_node(C);
787
788         if (CTX_data_dir(member)) {
789                 CTX_data_dir_set(result, node_context_dir);
790                 return 1;
791         }
792         else if (CTX_data_equals(member, "selected_nodes")) {
793                 bNode *node;
794
795                 if (snode->edittree) {
796                         for (node = snode->edittree->nodes.last; node; node = node->prev) {
797                                 if (node->flag & NODE_SELECT) {
798                                         CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
799                                 }
800                         }
801                 }
802                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
803                 return 1;
804         }
805         else if (CTX_data_equals(member, "active_node")) {
806                 if (snode->edittree) {
807                         bNode *node = nodeGetActive(snode->edittree);
808                         CTX_data_pointer_set(result, &snode->edittree->id, &RNA_Node, node);
809                 }
810
811                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
812                 return 1;
813         }
814         else if (CTX_data_equals(member, "node_previews")) {
815                 if (snode->nodetree) {
816                         CTX_data_pointer_set(result, &snode->nodetree->id, &RNA_NodeInstanceHash, snode->nodetree->previews);
817                 }
818
819                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
820                 return 1;
821         }
822
823         return 0;
824 }
825
826 /* only called once, from space/spacetypes.c */
827 void ED_spacetype_node(void)
828 {
829         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype node");
830         ARegionType *art;
831
832         st->spaceid = SPACE_NODE;
833         strncpy(st->name, "Node", BKE_ST_MAXNAME);
834
835         st->new = node_new;
836         st->free = node_free;
837         st->init = node_init;
838         st->duplicate = node_duplicate;
839         st->operatortypes = node_operatortypes;
840         st->keymap = node_keymap;
841         st->listener = node_area_listener;
842         st->refresh = node_area_refresh;
843         st->context = node_context;
844         st->dropboxes = node_dropboxes;
845
846         /* regions: main window */
847         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
848         art->regionid = RGN_TYPE_WINDOW;
849         art->init = node_main_area_init;
850         art->draw = node_main_area_draw;
851         art->listener = node_region_listener;
852         art->cursor = node_cursor;
853         art->event_cursor = true;
854         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
855
856         BLI_addhead(&st->regiontypes, art);
857
858         /* regions: header */
859         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
860         art->regionid = RGN_TYPE_HEADER;
861         art->prefsizey = HEADERY;
862         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
863         art->listener = node_region_listener;
864         art->init = node_header_area_init;
865         art->draw = node_header_area_draw;
866
867         BLI_addhead(&st->regiontypes, art);
868
869         /* regions: listview/buttons */
870         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
871         art->regionid = RGN_TYPE_UI;
872         art->prefsizex = 180; // XXX
873         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
874         art->listener = node_region_listener;
875         art->init = node_buttons_area_init;
876         art->draw = node_buttons_area_draw;
877         BLI_addhead(&st->regiontypes, art);
878
879         node_buttons_register(art);
880
881         /* regions: toolbar */
882         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
883         art->regionid = RGN_TYPE_TOOLS;
884         art->prefsizex = 160; /* XXX */
885         art->prefsizey = 50; /* XXX */
886         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
887         art->listener = node_region_listener;
888         art->init = node_toolbar_area_init;
889         art->draw = node_toolbar_area_draw;
890         BLI_addhead(&st->regiontypes, art);
891         
892         node_toolbar_register(art);
893
894         BKE_spacetype_register(st);
895 }
896