Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_outliner / space_outliner.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_outliner/space_outliner.c
28  *  \ingroup spoutliner
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39 #include "BLI_mempool.h"
40
41 #include "BKE_context.h"
42 #include "BKE_layer.h"
43 #include "BKE_screen.h"
44 #include "BKE_scene.h"
45 #include "BKE_outliner_treehash.h"
46
47 #include "ED_space_api.h"
48 #include "ED_screen.h"
49
50 #include "WM_api.h"
51 #include "WM_message.h"
52 #include "WM_types.h"
53
54 #include "BIF_gl.h"
55
56 #include "RNA_access.h"
57
58 #include "DNA_scene_types.h"
59 #include "DNA_object_types.h"
60
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64
65 #include "outliner_intern.h"
66
67 static void outliner_main_region_init(wmWindowManager *wm, ARegion *ar)
68 {
69         ListBase *lb;
70         wmKeyMap *keymap;
71         
72         /* make sure we keep the hide flags */
73         ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
74         ar->v2d.scroll &= ~(V2D_SCROLL_LEFT | V2D_SCROLL_TOP);  /* prevent any noise of past */
75         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
76         ar->v2d.scroll |= V2D_SCROLL_VERTICAL_HIDE;
77
78         ar->v2d.align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_POS_Y);
79         ar->v2d.keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
80         ar->v2d.keeptot = V2D_KEEPTOT_STRICT;
81         ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
82
83         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
84         
85         /* own keymap */
86         keymap = WM_keymap_find(wm->defaultconf, "Outliner", SPACE_OUTLINER, 0);
87         /* don't pass on view2d mask, it's always set with scrollbar space, hide fails */
88         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, &ar->winrct);
89
90         /* Add dropboxes */
91         lb = WM_dropboxmap_find("Outliner", SPACE_OUTLINER, RGN_TYPE_WINDOW);
92         WM_event_add_dropbox_handler(&ar->handlers, lb);
93 }
94
95 static int outliner_parent_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
96 {
97         ARegion *ar = CTX_wm_region(C);
98         SpaceOops *soops = CTX_wm_space_outliner(C);
99         float fmval[2];
100         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
101
102         if (drag->type == WM_DRAG_ID) {
103                 ID *id = drag->poin;
104                 if (GS(id->name) == ID_OB) {
105                         /* Ensure item under cursor is valid drop target */
106                         TreeElement *te = outliner_dropzone_find(soops, fmval, true);
107                         TreeStoreElem *tselem = te ? TREESTORE(te) : NULL;
108
109                         if (!te) {
110                                 /* pass */
111                         }
112                         else if (te->idcode == ID_OB && tselem->type == 0) {
113                                 Scene *scene;
114                                 ID *te_id = tselem->id;
115
116                                 /* check if dropping self or parent */
117                                 if (te_id == id || (Object *)te_id == ((Object *)id)->parent)
118                                         return 0;
119
120                                 /* check that parent/child are both in the same scene */
121                                 scene = (Scene *)outliner_search_back(soops, te, ID_SCE);
122
123                                 /* currently outliner organized in a way that if there's no parent scene
124                                  * element for object it means that all displayed objects belong to
125                                  * active scene and parenting them is allowed (sergey)
126                                  */
127                                 if (!scene) {
128                                         return 1;
129                                 }
130                                 else {
131                                         for (ViewLayer *view_layer = scene->view_layers.first;
132                                              view_layer;
133                                              view_layer = view_layer->next)
134                                         {
135                                                 if (BKE_view_layer_base_find(view_layer, (Object *)id)) {
136                                                         return 1;
137                                                 }
138                                         }
139                                 }
140                         }
141                 }
142         }
143         return 0;
144 }
145
146 static void outliner_parent_drop_copy(wmDrag *drag, wmDropBox *drop)
147 {
148         ID *id = drag->poin;
149
150         RNA_string_set(drop->ptr, "child", id->name + 2);
151 }
152
153 static int outliner_parent_clear_poll(bContext *C, wmDrag *drag, const wmEvent *event)
154 {
155         ARegion *ar = CTX_wm_region(C);
156         SpaceOops *soops = CTX_wm_space_outliner(C);
157         TreeElement *te = NULL;
158         float fmval[2];
159
160         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
161
162         if (!ELEM(soops->outlinevis, SO_VIEW_LAYER)) {
163                 return false;
164         }
165
166         if (drag->type == WM_DRAG_ID) {
167                 ID *id = drag->poin;
168                 if (GS(id->name) == ID_OB) {
169                         if (((Object *)id)->parent) {
170                                 if ((te = outliner_dropzone_find(soops, fmval, true))) {
171                                         TreeStoreElem *tselem = TREESTORE(te);
172
173                                         switch (te->idcode) {
174                                                 case ID_SCE:
175                                                         return (ELEM(tselem->type, TSE_R_LAYER_BASE, TSE_R_LAYER));
176                                                 case ID_OB:
177                                                         return (ELEM(tselem->type, TSE_MODIFIER_BASE, TSE_CONSTRAINT_BASE));
178                                                 /* Other codes to ignore? */
179                                         }
180                                 }
181                                 return (te == NULL);
182                         }
183                 }
184         }
185         return 0;
186 }
187
188 static void outliner_parent_clear_copy(wmDrag *drag, wmDropBox *drop)
189 {
190         ID *id = drag->poin;
191         RNA_string_set(drop->ptr, "dragged_obj", id->name + 2);
192
193         /* Set to simple parent clear type. Avoid menus for drag and drop if possible.
194          * If desired, user can toggle the different "Clear Parent" types in the operator
195          * menu on tool shelf. */
196         RNA_enum_set(drop->ptr, "type", 0);
197 }
198
199 static int outliner_scene_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
200 {
201         ARegion *ar = CTX_wm_region(C);
202         SpaceOops *soops = CTX_wm_space_outliner(C);
203         float fmval[2];
204         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
205
206         if (drag->type == WM_DRAG_ID) {
207                 ID *id = drag->poin;
208                 if (GS(id->name) == ID_OB) {
209                         /* Ensure item under cursor is valid drop target */
210                         TreeElement *te = outliner_dropzone_find(soops, fmval, false);
211                         return (te && te->idcode == ID_SCE && TREESTORE(te)->type == 0);
212                 }
213         }
214         return 0;
215 }
216
217 static void outliner_scene_drop_copy(wmDrag *drag, wmDropBox *drop)
218 {
219         ID *id = drag->poin;
220
221         RNA_string_set(drop->ptr, "object", id->name + 2);
222 }
223
224 static int outliner_material_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
225 {
226         ARegion *ar = CTX_wm_region(C);
227         SpaceOops *soops = CTX_wm_space_outliner(C);
228         float fmval[2];
229         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
230
231         if (drag->type == WM_DRAG_ID) {
232                 ID *id = drag->poin;
233                 if (GS(id->name) == ID_MA) {
234                         /* Ensure item under cursor is valid drop target */
235                         TreeElement *te = outliner_dropzone_find(soops, fmval, true);
236                         return (te && te->idcode == ID_OB && TREESTORE(te)->type == 0);
237                 }
238         }
239         return 0;
240 }
241
242 static void outliner_material_drop_copy(wmDrag *drag, wmDropBox *drop)
243 {
244         ID *id = drag->poin;
245
246         RNA_string_set(drop->ptr, "material", id->name + 2);
247 }
248
249 static int outliner_collection_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
250 {
251         ARegion *ar = CTX_wm_region(C);
252         SpaceOops *soops = CTX_wm_space_outliner(C);
253         float fmval[2];
254         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
255
256         if (drag->type == WM_DRAG_ID) {
257                 ID *id = drag->poin;
258                 if (ELEM(GS(id->name), ID_OB, ID_GR)) {
259                         /* Ensure item under cursor is valid drop target */
260                         TreeElement *te = outliner_dropzone_find(soops, fmval, true);
261                         return (te && outliner_is_collection_tree_element(te));
262                 }
263         }
264         return 0;
265 }
266
267 static void outliner_collection_drop_copy(wmDrag *drag, wmDropBox *drop)
268 {
269         ID *id = drag->poin;
270         RNA_string_set(drop->ptr, "child", id->name + 2);
271 }
272
273 /* region dropbox definition */
274 static void outliner_dropboxes(void)
275 {
276         ListBase *lb = WM_dropboxmap_find("Outliner", SPACE_OUTLINER, RGN_TYPE_WINDOW);
277
278         WM_dropbox_add(lb, "OUTLINER_OT_parent_drop", outliner_parent_drop_poll, outliner_parent_drop_copy);
279         WM_dropbox_add(lb, "OUTLINER_OT_parent_clear", outliner_parent_clear_poll, outliner_parent_clear_copy);
280         WM_dropbox_add(lb, "OUTLINER_OT_scene_drop", outliner_scene_drop_poll, outliner_scene_drop_copy);
281         WM_dropbox_add(lb, "OUTLINER_OT_material_drop", outliner_material_drop_poll, outliner_material_drop_copy);
282         WM_dropbox_add(lb, "OUTLINER_OT_collection_drop", outliner_collection_drop_poll, outliner_collection_drop_copy);
283 }
284
285 static void outliner_main_region_draw(const bContext *C, ARegion *ar)
286 {
287         View2D *v2d = &ar->v2d;
288         View2DScrollers *scrollers;
289         
290         /* clear */
291         UI_ThemeClearColor(TH_BACK);
292         glClear(GL_COLOR_BUFFER_BIT);
293         
294         draw_outliner(C);
295         
296         /* reset view matrix */
297         UI_view2d_view_restore(C);
298         
299         /* scrollers */
300         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
301         UI_view2d_scrollers_draw(C, v2d, scrollers);
302         UI_view2d_scrollers_free(scrollers);
303 }
304
305
306 static void outliner_main_region_free(ARegion *UNUSED(ar))
307 {
308         
309 }
310
311 static void outliner_main_region_listener(
312         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
313         wmNotifier *wmn, const Scene *UNUSED(scene))
314 {
315         /* context changes */
316         switch (wmn->category) {
317                 case NC_SCENE:
318                         switch (wmn->data) {
319                                 case ND_OB_ACTIVE:
320                                 case ND_OB_SELECT:
321                                 case ND_OB_VISIBLE:
322                                 case ND_OB_RENDER:
323                                 case ND_MODE:
324                                 case ND_KEYINGSET:
325                                 case ND_FRAME:
326                                 case ND_RENDER_OPTIONS:
327                                 case ND_SEQUENCER:
328                                 case ND_LAYER:
329                                 case ND_LAYER_CONTENT:
330                                 case ND_WORLD:
331                                 case ND_SCENEBROWSE:
332                                         ED_region_tag_redraw(ar);
333                                         break;
334                         }
335                         break;
336                 case NC_OBJECT:
337                         switch (wmn->data) {
338                                 case ND_TRANSFORM:
339                                         /* transform doesn't change outliner data */
340                                         break;
341                                 case ND_BONE_ACTIVE:
342                                 case ND_BONE_SELECT:
343                                 case ND_DRAW:
344                                 case ND_PARENT:
345                                 case ND_OB_SHADING:
346                                         ED_region_tag_redraw(ar);
347                                         break;
348                                 case ND_CONSTRAINT:
349                                         switch (wmn->action) {
350                                                 case NA_ADDED:
351                                                 case NA_REMOVED:
352                                                 case NA_RENAME:
353                                                         ED_region_tag_redraw(ar);
354                                                         break;
355                                         }
356                                         break;
357                                 case ND_MODIFIER:
358                                         /* all modifier actions now */
359                                         ED_region_tag_redraw(ar);
360                                         break;
361                                 default:
362                                         /* Trigger update for NC_OBJECT itself */
363                                         ED_region_tag_redraw(ar);
364                                         break;
365                         }
366                         break;
367                 case NC_GROUP:
368                         /* all actions now, todo: check outliner view mode? */
369                         ED_region_tag_redraw(ar);
370                         break;
371                 case NC_LAMP:
372                         /* For updating lamp icons, when changing lamp type */
373                         if (wmn->data == ND_LIGHTING_DRAW)
374                                 ED_region_tag_redraw(ar);
375                         break;
376                 case NC_SPACE:
377                         if (wmn->data == ND_SPACE_OUTLINER)
378                                 ED_region_tag_redraw(ar);
379                         break;
380                 case NC_ID:
381                         if (wmn->action == NA_RENAME)
382                                 ED_region_tag_redraw(ar);
383                         break;
384                 case NC_MATERIAL:
385                         switch (wmn->data) {
386                                 case ND_SHADING_LINKS:
387                                         ED_region_tag_redraw(ar);
388                                         break;
389                         }
390                         break;
391                 case NC_GEOM:
392                         switch (wmn->data) {
393                                 case ND_VERTEX_GROUP:
394                                 case ND_DATA:
395                                         ED_region_tag_redraw(ar);
396                                         break;
397                         }
398                         break;
399                 case NC_ANIMATION:
400                         switch (wmn->data) {
401                                 case ND_NLA_ACTCHANGE:
402                                 case ND_KEYFRAME:
403                                         ED_region_tag_redraw(ar);
404                                         break;
405                                 case ND_ANIMCHAN:
406                                         if (wmn->action == NA_SELECTED)
407                                                 ED_region_tag_redraw(ar);
408                                         break;
409                         }
410                         break;
411                 case NC_GPENCIL:
412                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
413                                 ED_region_tag_redraw(ar);
414                         break;
415                 case NC_SCREEN:
416                         if (ELEM(wmn->data, ND_LAYER)) {
417                                 ED_region_tag_redraw(ar);
418                         }
419                         break;
420         }
421         
422 }
423
424 static void outliner_main_region_message_subscribe(
425         const struct bContext *UNUSED(C),
426         struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene),
427         struct bScreen *UNUSED(screen), struct ScrArea *sa, struct ARegion *ar,
428         struct wmMsgBus *mbus)
429 {
430         SpaceOops *soops = sa->spacedata.first;
431         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
432                 .owner = ar,
433                 .user_data = ar,
434                 .notify = ED_region_do_msg_notify_tag_redraw,
435         };
436
437         if (ELEM(soops->outlinevis, SO_VIEW_LAYER, SO_SCENES)) {
438                 WM_msg_subscribe_rna_anon_prop(mbus, Window, view_layer, &msg_sub_value_region_tag_redraw);
439         }
440 }
441
442
443 /* ************************ header outliner area region *********************** */
444
445 /* add handlers, stuff you only do once or on area/region changes */
446 static void outliner_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
447 {
448         ED_region_header_init(ar);
449 }
450
451 static void outliner_header_region_draw(const bContext *C, ARegion *ar)
452 {
453         ED_region_header(C, ar);
454 }
455
456 static void outliner_header_region_free(ARegion *UNUSED(ar))
457 {
458 }
459
460 static void outliner_header_region_listener(
461         bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
462         wmNotifier *wmn, const Scene *UNUSED(scene))
463 {
464         /* context changes */
465         switch (wmn->category) {
466                 case NC_SCENE:
467                         if (wmn->data == ND_KEYINGSET)
468                                 ED_region_tag_redraw(ar);
469                         break;
470                 case NC_SPACE:
471                         if (wmn->data == ND_SPACE_OUTLINER)
472                                 ED_region_tag_redraw(ar);
473                         break;
474         }
475 }
476
477 /* ******************** default callbacks for outliner space ***************** */
478
479 static SpaceLink *outliner_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
480 {
481         ARegion *ar;
482         SpaceOops *soutliner;
483
484         soutliner = MEM_callocN(sizeof(SpaceOops), "initoutliner");
485         soutliner->spacetype = SPACE_OUTLINER;
486         soutliner->filter_id_type = ID_GR;
487         
488         /* header */
489         ar = MEM_callocN(sizeof(ARegion), "header for outliner");
490         
491         BLI_addtail(&soutliner->regionbase, ar);
492         ar->regiontype = RGN_TYPE_HEADER;
493         ar->alignment = RGN_ALIGN_TOP;
494         
495         /* main region */
496         ar = MEM_callocN(sizeof(ARegion), "main region for outliner");
497         
498         BLI_addtail(&soutliner->regionbase, ar);
499         ar->regiontype = RGN_TYPE_WINDOW;
500         
501         return (SpaceLink *)soutliner;
502 }
503
504 /* not spacelink itself */
505 static void outliner_free(SpaceLink *sl)
506 {
507         SpaceOops *soutliner = (SpaceOops *)sl;
508         
509         outliner_free_tree(&soutliner->tree);
510         if (soutliner->treestore) {
511                 BLI_mempool_destroy(soutliner->treestore);
512         }
513         if (soutliner->treehash) {
514                 BKE_outliner_treehash_free(soutliner->treehash);
515         }
516 }
517
518 /* spacetype; init callback */
519 static void outliner_init(wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
520 {
521         
522 }
523
524 static SpaceLink *outliner_duplicate(SpaceLink *sl)
525 {
526         SpaceOops *soutliner = (SpaceOops *)sl;
527         SpaceOops *soutlinern = MEM_dupallocN(soutliner);
528
529         BLI_listbase_clear(&soutlinern->tree);
530         soutlinern->treestore = NULL;
531         soutlinern->treehash = NULL;
532         
533         return (SpaceLink *)soutlinern;
534 }
535
536 static void outliner_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
537 {
538         SpaceOops *so = (SpaceOops *)slink;
539
540         /* Some early out checks. */
541         if (!TREESTORE_ID_TYPE(old_id)) {
542                 return;  /* ID type is not used by outilner... */
543         }
544
545         if (so->search_tse.id == old_id) {
546                 so->search_tse.id = new_id;
547         }
548
549         if (so->treestore) {
550                 TreeStoreElem *tselem;
551                 BLI_mempool_iter iter;
552                 bool changed = false;
553
554                 BLI_mempool_iternew(so->treestore, &iter);
555                 while ((tselem = BLI_mempool_iterstep(&iter))) {
556                         if (tselem->id == old_id) {
557                                 tselem->id = new_id;
558                                 changed = true;
559                         }
560                 }
561                 if (so->treehash && changed) {
562                         /* rebuild hash table, because it depends on ids too */
563                         /* postpone a full rebuild because this can be called many times on-free */
564                         so->storeflag |= SO_TREESTORE_REBUILD;
565                 }
566         }
567 }
568
569 /* only called once, from space_api/spacetypes.c */
570 void ED_spacetype_outliner(void)
571 {
572         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype time");
573         ARegionType *art;
574         
575         st->spaceid = SPACE_OUTLINER;
576         strncpy(st->name, "Outliner", BKE_ST_MAXNAME);
577         
578         st->new = outliner_new;
579         st->free = outliner_free;
580         st->init = outliner_init;
581         st->duplicate = outliner_duplicate;
582         st->operatortypes = outliner_operatortypes;
583         st->keymap = outliner_keymap;
584         st->dropboxes = outliner_dropboxes;
585         st->id_remap = outliner_id_remap;
586
587         /* regions: main window */
588         art = MEM_callocN(sizeof(ARegionType), "spacetype outliner region");
589         art->regionid = RGN_TYPE_WINDOW;
590         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
591         
592         art->init = outliner_main_region_init;
593         art->draw = outliner_main_region_draw;
594         art->free = outliner_main_region_free;
595         art->listener = outliner_main_region_listener;
596         art->message_subscribe = outliner_main_region_message_subscribe;
597         BLI_addhead(&st->regiontypes, art);
598         
599         /* regions: header */
600         art = MEM_callocN(sizeof(ARegionType), "spacetype outliner header region");
601         art->regionid = RGN_TYPE_HEADER;
602         art->prefsizey = HEADERY;
603         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
604         
605         art->init = outliner_header_region_init;
606         art->draw = outliner_header_region_draw;
607         art->free = outliner_header_region_free;
608         art->listener = outliner_header_region_listener;
609         BLI_addhead(&st->regiontypes, art);
610         
611         BKE_spacetype_register(st);
612 }
613