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 #include "GPU_framebuffer.h"
67
68 static void outliner_main_region_init(wmWindowManager *wm, ARegion *ar)
69 {
70         ListBase *lb;
71         wmKeyMap *keymap;
72
73         /* make sure we keep the hide flags */
74         ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
75         ar->v2d.scroll &= ~(V2D_SCROLL_LEFT | V2D_SCROLL_TOP);  /* prevent any noise of past */
76         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
77         ar->v2d.scroll |= V2D_SCROLL_VERTICAL_HIDE;
78
79         ar->v2d.align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_POS_Y);
80         ar->v2d.keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
81         ar->v2d.keeptot = V2D_KEEPTOT_STRICT;
82         ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
83
84         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
85
86         /* own keymap */
87         keymap = WM_keymap_find(wm->defaultconf, "Outliner", SPACE_OUTLINER, 0);
88         /* don't pass on view2d mask, it's always set with scrollbar space, hide fails */
89         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, &ar->winrct);
90
91         /* Add dropboxes */
92         lb = WM_dropboxmap_find("Outliner", SPACE_OUTLINER, RGN_TYPE_WINDOW);
93         WM_event_add_dropbox_handler(&ar->handlers, lb);
94 }
95
96 static void outliner_main_region_draw(const bContext *C, ARegion *ar)
97 {
98         View2D *v2d = &ar->v2d;
99         View2DScrollers *scrollers;
100
101         /* clear */
102         UI_ThemeClearColor(TH_BACK);
103         GPU_clear(GPU_COLOR_BIT);
104
105         draw_outliner(C);
106
107         /* reset view matrix */
108         UI_view2d_view_restore(C);
109
110         /* scrollers */
111         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
112         UI_view2d_scrollers_draw(C, v2d, scrollers);
113         UI_view2d_scrollers_free(scrollers);
114 }
115
116
117 static void outliner_main_region_free(ARegion *UNUSED(ar))
118 {
119
120 }
121
122 static void outliner_main_region_listener(
123         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
124         wmNotifier *wmn, const Scene *UNUSED(scene))
125 {
126         /* context changes */
127         switch (wmn->category) {
128                 case NC_SCENE:
129                         switch (wmn->data) {
130                                 case ND_OB_ACTIVE:
131                                 case ND_OB_SELECT:
132                                 case ND_OB_VISIBLE:
133                                 case ND_OB_RENDER:
134                                 case ND_MODE:
135                                 case ND_KEYINGSET:
136                                 case ND_FRAME:
137                                 case ND_RENDER_OPTIONS:
138                                 case ND_SEQUENCER:
139                                 case ND_LAYER:
140                                 case ND_LAYER_CONTENT:
141                                 case ND_WORLD:
142                                 case ND_SCENEBROWSE:
143                                         ED_region_tag_redraw(ar);
144                                         break;
145                         }
146                         break;
147                 case NC_OBJECT:
148                         switch (wmn->data) {
149                                 case ND_TRANSFORM:
150                                         /* transform doesn't change outliner data */
151                                         break;
152                                 case ND_BONE_ACTIVE:
153                                 case ND_BONE_SELECT:
154                                 case ND_DRAW:
155                                 case ND_PARENT:
156                                 case ND_OB_SHADING:
157                                         ED_region_tag_redraw(ar);
158                                         break;
159                                 case ND_CONSTRAINT:
160                                         switch (wmn->action) {
161                                                 case NA_ADDED:
162                                                 case NA_REMOVED:
163                                                 case NA_RENAME:
164                                                         ED_region_tag_redraw(ar);
165                                                         break;
166                                         }
167                                         break;
168                                 case ND_MODIFIER:
169                                         /* all modifier actions now */
170                                         ED_region_tag_redraw(ar);
171                                         break;
172                                 default:
173                                         /* Trigger update for NC_OBJECT itself */
174                                         ED_region_tag_redraw(ar);
175                                         break;
176                         }
177                         break;
178                 case NC_GROUP:
179                         /* all actions now, todo: check outliner view mode? */
180                         ED_region_tag_redraw(ar);
181                         break;
182                 case NC_LAMP:
183                         /* For updating lamp icons, when changing lamp type */
184                         if (wmn->data == ND_LIGHTING_DRAW)
185                                 ED_region_tag_redraw(ar);
186                         break;
187                 case NC_SPACE:
188                         if (wmn->data == ND_SPACE_OUTLINER)
189                                 ED_region_tag_redraw(ar);
190                         break;
191                 case NC_ID:
192                         if (wmn->action == NA_RENAME)
193                                 ED_region_tag_redraw(ar);
194                         break;
195                 case NC_MATERIAL:
196                         switch (wmn->data) {
197                                 case ND_SHADING_LINKS:
198                                         ED_region_tag_redraw(ar);
199                                         break;
200                         }
201                         break;
202                 case NC_GEOM:
203                         switch (wmn->data) {
204                                 case ND_VERTEX_GROUP:
205                                 case ND_DATA:
206                                         ED_region_tag_redraw(ar);
207                                         break;
208                         }
209                         break;
210                 case NC_ANIMATION:
211                         switch (wmn->data) {
212                                 case ND_NLA_ACTCHANGE:
213                                 case ND_KEYFRAME:
214                                         ED_region_tag_redraw(ar);
215                                         break;
216                                 case ND_ANIMCHAN:
217                                         if (wmn->action == NA_SELECTED)
218                                                 ED_region_tag_redraw(ar);
219                                         break;
220                         }
221                         break;
222                 case NC_GPENCIL:
223                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
224                                 ED_region_tag_redraw(ar);
225                         break;
226                 case NC_SCREEN:
227                         if (ELEM(wmn->data, ND_LAYER)) {
228                                 ED_region_tag_redraw(ar);
229                         }
230                         break;
231         }
232
233 }
234
235 static void outliner_main_region_message_subscribe(
236         const struct bContext *UNUSED(C),
237         struct WorkSpace *UNUSED(workspace), struct Scene *UNUSED(scene),
238         struct bScreen *UNUSED(screen), struct ScrArea *sa, struct ARegion *ar,
239         struct wmMsgBus *mbus)
240 {
241         SpaceOops *soops = sa->spacedata.first;
242         wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
243                 .owner = ar,
244                 .user_data = ar,
245                 .notify = ED_region_do_msg_notify_tag_redraw,
246         };
247
248         if (ELEM(soops->outlinevis, SO_VIEW_LAYER, SO_SCENES)) {
249                 WM_msg_subscribe_rna_anon_prop(mbus, Window, view_layer, &msg_sub_value_region_tag_redraw);
250         }
251 }
252
253
254 /* ************************ header outliner area region *********************** */
255
256 /* add handlers, stuff you only do once or on area/region changes */
257 static void outliner_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
258 {
259         ED_region_header_init(ar);
260 }
261
262 static void outliner_header_region_draw(const bContext *C, ARegion *ar)
263 {
264         ED_region_header(C, ar);
265 }
266
267 static void outliner_header_region_free(ARegion *UNUSED(ar))
268 {
269 }
270
271 static void outliner_header_region_listener(
272         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
273         wmNotifier *wmn, const Scene *UNUSED(scene))
274 {
275         /* context changes */
276         switch (wmn->category) {
277                 case NC_SCENE:
278                         if (wmn->data == ND_KEYINGSET)
279                                 ED_region_tag_redraw(ar);
280                         break;
281                 case NC_SPACE:
282                         if (wmn->data == ND_SPACE_OUTLINER)
283                                 ED_region_tag_redraw(ar);
284                         break;
285         }
286 }
287
288 /* ******************** default callbacks for outliner space ***************** */
289
290 static SpaceLink *outliner_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
291 {
292         ARegion *ar;
293         SpaceOops *soutliner;
294
295         soutliner = MEM_callocN(sizeof(SpaceOops), "initoutliner");
296         soutliner->spacetype = SPACE_OUTLINER;
297         soutliner->filter_id_type = ID_GR;
298
299         /* header */
300         ar = MEM_callocN(sizeof(ARegion), "header for outliner");
301
302         BLI_addtail(&soutliner->regionbase, ar);
303         ar->regiontype = RGN_TYPE_HEADER;
304         ar->alignment = RGN_ALIGN_TOP;
305
306         /* main region */
307         ar = MEM_callocN(sizeof(ARegion), "main region for outliner");
308
309         BLI_addtail(&soutliner->regionbase, ar);
310         ar->regiontype = RGN_TYPE_WINDOW;
311
312         return (SpaceLink *)soutliner;
313 }
314
315 /* not spacelink itself */
316 static void outliner_free(SpaceLink *sl)
317 {
318         SpaceOops *soutliner = (SpaceOops *)sl;
319
320         outliner_free_tree(&soutliner->tree);
321         if (soutliner->treestore) {
322                 BLI_mempool_destroy(soutliner->treestore);
323         }
324         if (soutliner->treehash) {
325                 BKE_outliner_treehash_free(soutliner->treehash);
326         }
327 }
328
329 /* spacetype; init callback */
330 static void outliner_init(wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
331 {
332
333 }
334
335 static SpaceLink *outliner_duplicate(SpaceLink *sl)
336 {
337         SpaceOops *soutliner = (SpaceOops *)sl;
338         SpaceOops *soutlinern = MEM_dupallocN(soutliner);
339
340         BLI_listbase_clear(&soutlinern->tree);
341         soutlinern->treestore = NULL;
342         soutlinern->treehash = NULL;
343
344         return (SpaceLink *)soutlinern;
345 }
346
347 static void outliner_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
348 {
349         SpaceOops *so = (SpaceOops *)slink;
350
351         /* Some early out checks. */
352         if (!TREESTORE_ID_TYPE(old_id)) {
353                 return;  /* ID type is not used by outilner... */
354         }
355
356         if (so->search_tse.id == old_id) {
357                 so->search_tse.id = new_id;
358         }
359
360         if (so->treestore) {
361                 TreeStoreElem *tselem;
362                 BLI_mempool_iter iter;
363                 bool changed = false;
364
365                 BLI_mempool_iternew(so->treestore, &iter);
366                 while ((tselem = BLI_mempool_iterstep(&iter))) {
367                         if (tselem->id == old_id) {
368                                 tselem->id = new_id;
369                                 changed = true;
370                         }
371                 }
372                 if (so->treehash && changed) {
373                         /* rebuild hash table, because it depends on ids too */
374                         /* postpone a full rebuild because this can be called many times on-free */
375                         so->storeflag |= SO_TREESTORE_REBUILD;
376                 }
377         }
378 }
379
380 /* only called once, from space_api/spacetypes.c */
381 void ED_spacetype_outliner(void)
382 {
383         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype time");
384         ARegionType *art;
385
386         st->spaceid = SPACE_OUTLINER;
387         strncpy(st->name, "Outliner", BKE_ST_MAXNAME);
388
389         st->new = outliner_new;
390         st->free = outliner_free;
391         st->init = outliner_init;
392         st->duplicate = outliner_duplicate;
393         st->operatortypes = outliner_operatortypes;
394         st->keymap = outliner_keymap;
395         st->dropboxes = outliner_dropboxes;
396         st->id_remap = outliner_id_remap;
397
398         /* regions: main window */
399         art = MEM_callocN(sizeof(ARegionType), "spacetype outliner region");
400         art->regionid = RGN_TYPE_WINDOW;
401         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
402
403         art->init = outliner_main_region_init;
404         art->draw = outliner_main_region_draw;
405         art->free = outliner_main_region_free;
406         art->listener = outliner_main_region_listener;
407         art->message_subscribe = outliner_main_region_message_subscribe;
408         BLI_addhead(&st->regiontypes, art);
409
410         /* regions: header */
411         art = MEM_callocN(sizeof(ARegionType), "spacetype outliner header region");
412         art->regionid = RGN_TYPE_HEADER;
413         art->prefsizey = HEADERY;
414         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
415
416         art->init = outliner_header_region_init;
417         art->draw = outliner_header_region_draw;
418         art->free = outliner_header_region_free;
419         art->listener = outliner_header_region_listener;
420         BLI_addhead(&st->regiontypes, art);
421
422         BKE_spacetype_register(st);
423 }