BLI_string: add BLI_snprintf_rlen
[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                         size = BLI_strncpy_rlen(value, path->node_name, max_length);
207                 }
208                 else {
209                         size = BLI_snprintf_rlen(value, max_length, "/%s", path->node_name);
210                 }
211                 max_length -= size;
212                 if (max_length <= 0)
213                         break;
214                 value += size;
215         }
216 }
217
218 void ED_node_set_active_viewer_key(SpaceNode *snode)
219 {
220         bNodeTreePath *path = snode->treepath.last;
221         if (snode->nodetree && path) {
222                 snode->nodetree->active_viewer_key = path->parent_key;
223         }
224 }
225
226 void snode_group_offset(SpaceNode *snode, float *x, float *y)
227 {
228         bNodeTreePath *path = snode->treepath.last;
229         
230         if (path && path->prev) {
231                 float dcenter[2];
232                 sub_v2_v2v2(dcenter, path->view_center, path->prev->view_center);
233                 *x = dcenter[0];
234                 *y = dcenter[1];
235         }
236         else
237                 *x = *y = 0.0f;
238 }
239
240 /* ******************** manage regions ********************* */
241
242 ARegion *node_has_buttons_region(ScrArea *sa)
243 {
244         ARegion *ar, *arnew;
245
246         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
247         if (ar) return ar;
248
249         /* add subdiv level; after header */
250         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
251
252         /* is error! */
253         if (ar == NULL) return NULL;
254
255         arnew = MEM_callocN(sizeof(ARegion), "buttons for node");
256
257         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
258         arnew->regiontype = RGN_TYPE_UI;
259         arnew->alignment = RGN_ALIGN_RIGHT;
260
261         arnew->flag = RGN_FLAG_HIDDEN;
262
263         return arnew;
264 }
265
266 ARegion *node_has_tools_region(ScrArea *sa)
267 {
268         ARegion *ar, *arnew;
269         
270         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
271         if (ar) return ar;
272         
273         /* add subdiv level; after header */
274         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
275         
276         /* is error! */
277         if (ar == NULL) return NULL;
278         
279         arnew = MEM_callocN(sizeof(ARegion), "node tools");
280         
281         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
282         arnew->regiontype = RGN_TYPE_TOOLS;
283         arnew->alignment = RGN_ALIGN_LEFT;
284         
285         arnew->flag = RGN_FLAG_HIDDEN;
286         
287         return arnew;
288 }
289
290 /* ******************** default callbacks for node space ***************** */
291
292 static SpaceLink *node_new(const bContext *UNUSED(C))
293 {
294         ARegion *ar;
295         SpaceNode *snode;
296
297         snode = MEM_callocN(sizeof(SpaceNode), "initnode");
298         snode->spacetype = SPACE_NODE;
299
300         snode->flag = SNODE_SHOW_GPENCIL | SNODE_USE_ALPHA;
301
302         /* backdrop */
303         snode->zoom = 1.0f;
304
305         /* select the first tree type for valid type */
306         NODE_TREE_TYPES_BEGIN (treetype)
307         {
308                 strcpy(snode->tree_idname, treetype->idname);
309                 break;
310         }
311         NODE_TREE_TYPES_END;
312
313         /* header */
314         ar = MEM_callocN(sizeof(ARegion), "header for node");
315
316         BLI_addtail(&snode->regionbase, ar);
317         ar->regiontype = RGN_TYPE_HEADER;
318         ar->alignment = RGN_ALIGN_BOTTOM;
319
320         /* buttons/list view */
321         ar = MEM_callocN(sizeof(ARegion), "buttons for node");
322
323         BLI_addtail(&snode->regionbase, ar);
324         ar->regiontype = RGN_TYPE_UI;
325         ar->alignment = RGN_ALIGN_RIGHT;
326
327         /* toolbar */
328         ar = MEM_callocN(sizeof(ARegion), "node tools");
329
330         BLI_addtail(&snode->regionbase, ar);
331         ar->regiontype = RGN_TYPE_TOOLS;
332         ar->alignment = RGN_ALIGN_LEFT;
333
334         ar->flag = RGN_FLAG_HIDDEN;
335
336         /* main area */
337         ar = MEM_callocN(sizeof(ARegion), "main area for node");
338
339         BLI_addtail(&snode->regionbase, ar);
340         ar->regiontype = RGN_TYPE_WINDOW;
341
342         ar->v2d.tot.xmin =  -12.8f * U.widget_unit;
343         ar->v2d.tot.ymin =  -12.8f * U.widget_unit;
344         ar->v2d.tot.xmax = 38.4f * U.widget_unit;
345         ar->v2d.tot.ymax = 38.4f * U.widget_unit;
346
347         ar->v2d.cur =  ar->v2d.tot;
348
349         ar->v2d.min[0] = 1.0f;
350         ar->v2d.min[1] = 1.0f;
351
352         ar->v2d.max[0] = 32000.0f;
353         ar->v2d.max[1] = 32000.0f;
354
355         ar->v2d.minzoom = 0.09f;
356         ar->v2d.maxzoom = 2.31f;
357
358         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
359         ar->v2d.keepzoom = V2D_LIMITZOOM | V2D_KEEPASPECT;
360         ar->v2d.keeptot = 0;
361
362         return (SpaceLink *)snode;
363 }
364
365 static void node_free(SpaceLink *sl)
366 {
367         SpaceNode *snode = (SpaceNode *)sl;
368         bNodeTreePath *path, *path_next;
369
370         for (path = snode->treepath.first; path; path = path_next) {
371                 path_next = path->next;
372                 MEM_freeN(path);
373         }
374 }
375
376
377 /* spacetype; init callback */
378 static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
379 {
380
381 }
382
383 static void node_area_listener(bScreen *sc, ScrArea *sa, wmNotifier *wmn)
384 {
385         /* note, ED_area_tag_refresh will re-execute compositor */
386         SpaceNode *snode = sa->spacedata.first;
387         /* shaderfrom is only used for new shading nodes, otherwise all shaders are from objects */
388         short shader_type = BKE_scene_use_new_shading_nodes(sc->scene) ? snode->shaderfrom : SNODE_SHADER_OBJECT;
389
390         /* preview renders */
391         switch (wmn->category) {
392                 case NC_SCENE:
393                         switch (wmn->data) {
394                                 case ND_NODES:
395                                 {
396                                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
397                                         bNodeTreePath *path = snode->treepath.last;
398                                         /* shift view to node tree center */
399                                         if (ar && path)
400                                                 UI_view2d_center_set(&ar->v2d, path->view_center[0], path->view_center[1]);
401                                         
402                                         ED_area_tag_refresh(sa);
403                                         break;
404                                 }
405                                 case ND_FRAME:
406                                         ED_area_tag_refresh(sa);
407                                         break;
408                                 case ND_COMPO_RESULT:
409                                         ED_area_tag_redraw(sa);
410                                         break;
411                                 case ND_TRANSFORM_DONE:
412                                         if (ED_node_is_compositor(snode)) {
413                                                 if (snode->flag & SNODE_AUTO_RENDER) {
414                                                         snode->recalc = 1;
415                                                         ED_area_tag_refresh(sa);
416                                                 }
417                                         }
418                                         break;
419                                 case ND_LAYER_CONTENT:
420                                         ED_area_tag_refresh(sa);
421                                         break;
422                         }
423                         break;
424
425                 /* future: add ID checks? */
426                 case NC_MATERIAL:
427                         if (ED_node_is_shader(snode)) {
428                                 if (wmn->data == ND_SHADING)
429                                         ED_area_tag_refresh(sa);
430                                 else if (wmn->data == ND_SHADING_DRAW)
431                                         ED_area_tag_refresh(sa);
432                                 else if (wmn->data == ND_SHADING_LINKS)
433                                         ED_area_tag_refresh(sa);
434                                 else if (wmn->action == NA_ADDED && snode->edittree)
435                                         nodeSetActiveID(snode->edittree, ID_MA, wmn->reference);
436
437                         }
438                         break;
439                 case NC_TEXTURE:
440                         if (ED_node_is_shader(snode) || ED_node_is_texture(snode)) {
441                                 if (wmn->data == ND_NODES)
442                                         ED_area_tag_refresh(sa);
443                         }
444                         break;
445                 case NC_WORLD:
446                         if (ED_node_is_shader(snode) && shader_type == SNODE_SHADER_WORLD) {
447                                 ED_area_tag_refresh(sa);
448                         }
449                         break;
450                 case NC_OBJECT:
451                         if (ED_node_is_shader(snode)) {
452                                 if (wmn->data == ND_OB_SHADING)
453                                         ED_area_tag_refresh(sa);
454                         }
455                         break;
456                 case NC_SPACE:
457                         if (wmn->data == ND_SPACE_NODE)
458                                 ED_area_tag_refresh(sa);
459                         else if (wmn->data == ND_SPACE_NODE_VIEW)
460                                 ED_area_tag_redraw(sa);
461                         break;
462                 case NC_NODE:
463                         if (wmn->action == NA_EDITED)
464                                 ED_area_tag_refresh(sa);
465                         else if (wmn->action == NA_SELECTED)
466                                 ED_area_tag_redraw(sa);
467                         break;
468                 case NC_SCREEN:
469                         switch (wmn->data) {
470                                 case ND_ANIMPLAY:
471                                         ED_area_tag_refresh(sa);
472                                         break;
473                         }
474                         break;
475                 case NC_MASK:
476                         if (wmn->action == NA_EDITED) {
477                                 if (snode->nodetree && snode->nodetree->type == NTREE_COMPOSIT) {
478                                         ED_area_tag_refresh(sa);
479                                 }
480                         }
481                         break;
482
483                 case NC_IMAGE:
484                         if (wmn->action == NA_EDITED) {
485                                 if (ED_node_is_compositor(snode)) {
486                                         /* note that nodeUpdateID is already called by BKE_image_signal() on all
487                                          * scenes so really this is just to know if the images is used in the compo else
488                                          * painting on images could become very slow when the compositor is open. */
489                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
490                                                 ED_area_tag_refresh(sa);
491                                 }
492                         }
493                         break;
494
495                 case NC_MOVIECLIP:
496                         if (wmn->action == NA_EDITED) {
497                                 if (ED_node_is_compositor(snode)) {
498                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
499                                                 ED_area_tag_refresh(sa);
500                                 }
501                         }
502                         break;
503
504                 case NC_LINESTYLE:
505                         if (ED_node_is_shader(snode) && shader_type == SNODE_SHADER_LINESTYLE) {
506                                 ED_area_tag_refresh(sa);
507                         }
508                         break;
509                 case NC_WM:
510                         if (wmn->data == ND_UNDO) {
511                                 ED_area_tag_refresh(sa);
512                         }
513                         break;
514                 case NC_GPENCIL:
515                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
516                                 ED_area_tag_redraw(sa);
517                         }
518                         break;
519         }
520 }
521
522 static void node_area_refresh(const struct bContext *C, ScrArea *sa)
523 {
524         /* default now: refresh node is starting preview */
525         SpaceNode *snode = sa->spacedata.first;
526         
527         snode_set_context(C);
528
529         if (snode->nodetree) {
530                 if (snode->nodetree->type == NTREE_SHADER) {
531                         if (GS(snode->id->name) == ID_MA) {
532                                 Material *ma = (Material *)snode->id;
533                                 if (ma->use_nodes)
534                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
535                         }
536                         else if (GS(snode->id->name) == ID_LA) {
537                                 Lamp *la = (Lamp *)snode->id;
538                                 if (la->use_nodes)
539                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
540                         }
541                         else if (GS(snode->id->name) == ID_WO) {
542                                 World *wo = (World *)snode->id;
543                                 if (wo->use_nodes)
544                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
545                         }
546                 }
547                 else if (snode->nodetree->type == NTREE_COMPOSIT) {
548                         Scene *scene = (Scene *)snode->id;
549                         if (scene->use_nodes) {
550                                 /* recalc is set on 3d view changes for auto compo */
551                                 if (snode->recalc) {
552                                         snode->recalc = 0;
553                                         node_render_changed_exec((struct bContext *)C, NULL);
554                                 }
555                                 else {
556                                         ED_node_composite_job(C, snode->nodetree, scene);
557                                 }
558                         }
559                 }
560                 else if (snode->nodetree->type == NTREE_TEXTURE) {
561                         Tex *tex = (Tex *)snode->id;
562                         if (tex->use_nodes) {
563                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
564                         }
565                 }
566         }
567 }
568
569 static SpaceLink *node_duplicate(SpaceLink *sl)
570 {
571         SpaceNode *snode = (SpaceNode *)sl;
572         SpaceNode *snoden = MEM_dupallocN(snode);
573
574         BLI_duplicatelist(&snoden->treepath, &snode->treepath);
575
576         /* clear or remove stuff from old */
577         BLI_listbase_clear(&snoden->linkdrag);
578
579         /* Note: no need to set node tree user counts,
580          * the editor only keeps at least 1 (id_us_ensure_real),
581          * which is already done by the original SpaceNode.
582          */
583
584         return (SpaceLink *)snoden;
585 }
586
587
588 /* add handlers, stuff you only do once or on area/region changes */
589 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
590 {
591         wmKeyMap *keymap;
592
593         ED_region_panels_init(wm, ar);
594
595         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
596         WM_event_add_keymap_handler(&ar->handlers, keymap);
597 }
598
599 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
600 {
601         ED_region_panels(C, ar, 1, NULL, -1);
602 }
603
604 /* add handlers, stuff you only do once or on area/region changes */
605 static void node_toolbar_area_init(wmWindowManager *wm, ARegion *ar)
606 {
607         wmKeyMap *keymap;
608
609         ED_region_panels_init(wm, ar);
610
611         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
612         WM_event_add_keymap_handler(&ar->handlers, keymap);
613 }
614
615 static void node_toolbar_area_draw(const bContext *C, ARegion *ar)
616 {
617         ED_region_panels(C, ar, 1, NULL, -1);
618 }
619
620 static void node_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
621 {
622         SpaceNode *snode = sa->spacedata.first;
623
624         /* convert mouse coordinates to v2d space */
625         UI_view2d_region_to_view(&ar->v2d, win->eventstate->x - ar->winrct.xmin, win->eventstate->y - ar->winrct.ymin,
626                                  &snode->cursor[0], &snode->cursor[1]);
627         
628         /* here snode->cursor is used to detect the node edge for sizing */
629         node_set_cursor(win, snode, snode->cursor);
630
631         /* XXX snode->cursor is in placing new nodes space */
632         snode->cursor[0] /= UI_DPI_FAC;
633         snode->cursor[1] /= UI_DPI_FAC;
634         
635 }
636
637 /* Initialize main area, setting handlers. */
638 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
639 {
640         wmKeyMap *keymap;
641         ListBase *lb;
642
643         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
644
645         /* own keymaps */
646         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
647         WM_event_add_keymap_handler(&ar->handlers, keymap);
648
649         keymap = WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
650         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
651
652         /* add drop boxes */
653         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
654
655         WM_event_add_dropbox_handler(&ar->handlers, lb);
656 }
657
658 static void node_main_area_draw(const bContext *C, ARegion *ar)
659 {
660         drawnodespace(C, ar);
661 }
662
663
664 /* ************* dropboxes ************* */
665
666 static int node_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
667 {
668         if (drag->type == WM_DRAG_ID) {
669                 ID *id = drag->poin;
670                 if (GS(id->name) == ID_IM)
671                         return 1;
672         }
673         else if (drag->type == WM_DRAG_PATH) {
674                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE))   /* rule might not work? */
675                         return 1;
676         }
677         return 0;
678 }
679
680 static int node_mask_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
681 {
682         if (drag->type == WM_DRAG_ID) {
683                 ID *id = drag->poin;
684                 if (GS(id->name) == ID_MSK)
685                         return 1;
686         }
687         return 0;
688 }
689
690 static void node_id_drop_copy(wmDrag *drag, wmDropBox *drop)
691 {
692         ID *id = drag->poin;
693
694         RNA_string_set(drop->ptr, "name", id->name + 2);
695 }
696
697 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
698 {
699         ID *id = drag->poin;
700
701         if (id) {
702                 RNA_string_set(drop->ptr, "name", id->name + 2);
703                 RNA_struct_property_unset(drop->ptr, "filepath");
704         }
705         else if (drag->path[0]) {
706                 RNA_string_set(drop->ptr, "filepath", drag->path);
707                 RNA_struct_property_unset(drop->ptr, "name");
708         }
709 }
710
711 /* this region dropbox definition */
712 static void node_dropboxes(void)
713 {
714         ListBase *lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
715
716         WM_dropbox_add(lb, "NODE_OT_add_file", node_ima_drop_poll, node_id_path_drop_copy);
717         WM_dropbox_add(lb, "NODE_OT_add_mask", node_mask_drop_poll, node_id_drop_copy);
718
719 }
720
721 /* ************* end drop *********** */
722
723
724 /* add handlers, stuff you only do once or on area/region changes */
725 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
726 {
727         ED_region_header_init(ar);
728 }
729
730 static void node_header_area_draw(const bContext *C, ARegion *ar)
731 {
732         /* find and set the context */
733         snode_set_context(C);
734
735         ED_region_header(C, ar);
736 }
737
738 /* used for header + main area */
739 static void node_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
740 {
741         /* context changes */
742         switch (wmn->category) {
743                 case NC_SPACE:
744                         if (wmn->data == ND_SPACE_NODE)
745                                 ED_region_tag_redraw(ar);
746                         break;
747                 case NC_SCREEN:
748                         switch (wmn->data) {
749                                 case ND_SCREENCAST:
750                                 case ND_ANIMPLAY:
751                                         ED_region_tag_redraw(ar);
752                                         break;
753                         }
754                         break;
755                 case NC_SCENE:
756                 case NC_MATERIAL:
757                 case NC_TEXTURE:
758                 case NC_WORLD:
759                 case NC_NODE:
760                 case NC_LINESTYLE:
761                         ED_region_tag_redraw(ar);
762                         break;
763                 case NC_OBJECT:
764                         if (wmn->data == ND_OB_SHADING)
765                                 ED_region_tag_redraw(ar);
766                         break;
767                 case NC_ID:
768                         if (wmn->action == NA_RENAME)
769                                 ED_region_tag_redraw(ar);
770                         break;
771                 case NC_GPENCIL:
772                         if (wmn->action == NA_EDITED)
773                                 ED_region_tag_redraw(ar);
774                         else if (wmn->data & ND_GPENCIL_EDITMODE)
775                                 ED_region_tag_redraw(ar);
776                         break;
777         }
778 }
779
780 const char *node_context_dir[] = {"selected_nodes", "active_node", NULL};
781
782 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
783 {
784         SpaceNode *snode = CTX_wm_space_node(C);
785
786         if (CTX_data_dir(member)) {
787                 CTX_data_dir_set(result, node_context_dir);
788                 return 1;
789         }
790         else if (CTX_data_equals(member, "selected_nodes")) {
791                 bNode *node;
792
793                 if (snode->edittree) {
794                         for (node = snode->edittree->nodes.last; node; node = node->prev) {
795                                 if (node->flag & NODE_SELECT) {
796                                         CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
797                                 }
798                         }
799                 }
800                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
801                 return 1;
802         }
803         else if (CTX_data_equals(member, "active_node")) {
804                 if (snode->edittree) {
805                         bNode *node = nodeGetActive(snode->edittree);
806                         CTX_data_pointer_set(result, &snode->edittree->id, &RNA_Node, node);
807                 }
808
809                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
810                 return 1;
811         }
812         else if (CTX_data_equals(member, "node_previews")) {
813                 if (snode->nodetree) {
814                         CTX_data_pointer_set(result, &snode->nodetree->id, &RNA_NodeInstanceHash, snode->nodetree->previews);
815                 }
816
817                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
818                 return 1;
819         }
820
821         return 0;
822 }
823
824 /* only called once, from space/spacetypes.c */
825 void ED_spacetype_node(void)
826 {
827         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype node");
828         ARegionType *art;
829
830         st->spaceid = SPACE_NODE;
831         strncpy(st->name, "Node", BKE_ST_MAXNAME);
832
833         st->new = node_new;
834         st->free = node_free;
835         st->init = node_init;
836         st->duplicate = node_duplicate;
837         st->operatortypes = node_operatortypes;
838         st->keymap = node_keymap;
839         st->listener = node_area_listener;
840         st->refresh = node_area_refresh;
841         st->context = node_context;
842         st->dropboxes = node_dropboxes;
843
844         /* regions: main window */
845         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
846         art->regionid = RGN_TYPE_WINDOW;
847         art->init = node_main_area_init;
848         art->draw = node_main_area_draw;
849         art->listener = node_region_listener;
850         art->cursor = node_cursor;
851         art->event_cursor = true;
852         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
853
854         BLI_addhead(&st->regiontypes, art);
855
856         /* regions: header */
857         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
858         art->regionid = RGN_TYPE_HEADER;
859         art->prefsizey = HEADERY;
860         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
861         art->listener = node_region_listener;
862         art->init = node_header_area_init;
863         art->draw = node_header_area_draw;
864
865         BLI_addhead(&st->regiontypes, art);
866
867         /* regions: listview/buttons */
868         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
869         art->regionid = RGN_TYPE_UI;
870         art->prefsizex = 180; // XXX
871         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
872         art->listener = node_region_listener;
873         art->init = node_buttons_area_init;
874         art->draw = node_buttons_area_draw;
875         BLI_addhead(&st->regiontypes, art);
876
877         node_buttons_register(art);
878
879         /* regions: toolbar */
880         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
881         art->regionid = RGN_TYPE_TOOLS;
882         art->prefsizex = 160; /* XXX */
883         art->prefsizey = 50; /* XXX */
884         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
885         art->listener = node_region_listener;
886         art->init = node_toolbar_area_init;
887         art->draw = node_toolbar_area_draw;
888         BLI_addhead(&st->regiontypes, art);
889         
890         node_toolbar_register(art);
891
892         BKE_spacetype_register(st);
893 }
894