Merge branch 'blender2.7'
[blender.git] / source / blender / editors / space_outliner / outliner_tools.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) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_outliner/outliner_tools.c
29  *  \ingroup spoutliner
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_anim_types.h"
35 #include "DNA_armature_types.h"
36 #include "DNA_collection_types.h"
37 #include "DNA_gpencil_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_linestyle_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meta_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_sequence_types.h"
45 #include "DNA_world_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_constraint_types.h"
48 #include "DNA_modifier_types.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_utildefines.h"
52
53 #include "BKE_animsys.h"
54 #include "BKE_collection.h"
55 #include "BKE_context.h"
56 #include "BKE_constraint.h"
57 #include "BKE_fcurve.h"
58 #include "BKE_global.h"
59 #include "BKE_layer.h"
60 #include "BKE_library.h"
61 #include "BKE_library_override.h"
62 #include "BKE_library_query.h"
63 #include "BKE_main.h"
64 #include "BKE_report.h"
65 #include "BKE_scene.h"
66 #include "BKE_screen.h"
67 #include "BKE_sequencer.h"
68
69 #include "DEG_depsgraph.h"
70 #include "DEG_depsgraph_build.h"
71
72 #include "ED_armature.h"
73 #include "ED_object.h"
74 #include "ED_scene.h"
75 #include "ED_screen.h"
76 #include "ED_sequencer.h"
77 #include "ED_undo.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 #include "UI_interface.h"
83 #include "UI_view2d.h"
84 #include "UI_resources.h"
85
86 #include "RNA_access.h"
87 #include "RNA_define.h"
88 #include "RNA_enum_types.h"
89
90 #include "outliner_intern.h"
91
92
93 /* ****************************************************** */
94
95 /* ************ SELECTION OPERATIONS ********* */
96
97 static void set_operation_types(SpaceOops *soops, ListBase *lb,
98                                 int *scenelevel,
99                                 int *objectlevel,
100                                 int *idlevel,
101                                 int *datalevel)
102 {
103         TreeElement *te;
104         TreeStoreElem *tselem;
105
106         for (te = lb->first; te; te = te->next) {
107                 tselem = TREESTORE(te);
108                 if (tselem->flag & TSE_SELECTED) {
109                         /* Layer collection points to collection ID. */
110                         if (!ELEM(tselem->type, 0, TSE_LAYER_COLLECTION)) {
111                                 if (*datalevel == 0)
112                                         *datalevel = tselem->type;
113                                 else if (*datalevel != tselem->type)
114                                         *datalevel = -1;
115                         }
116                         else {
117                                 int idcode = GS(tselem->id->name);
118                                 switch (idcode) {
119                                         case ID_SCE:
120                                                 *scenelevel = 1;
121                                                 break;
122                                         case ID_OB:
123                                                 *objectlevel = 1;
124                                                 break;
125
126                                         case ID_ME: case ID_CU: case ID_MB: case ID_LT:
127                                         case ID_LA: case ID_AR: case ID_CA: case ID_SPK:
128                                         case ID_MA: case ID_TE: case ID_IP: case ID_IM:
129                                         case ID_SO: case ID_KE: case ID_WO: case ID_AC:
130                                         case ID_NLA: case ID_TXT: case ID_GR: case ID_LS:
131                                         case ID_LI:
132                                                 if (*idlevel == 0) *idlevel = idcode;
133                                                 else if (*idlevel != idcode) *idlevel = -1;
134                                                 if (ELEM(*datalevel, TSE_VIEW_COLLECTION_BASE, TSE_SCENE_COLLECTION_BASE)) {
135                                                         *datalevel = 0;
136                                                 }
137                                                 break;
138                                 }
139                         }
140                 }
141                 if (TSELEM_OPEN(tselem, soops)) {
142                         set_operation_types(soops, &te->subtree,
143                                             scenelevel, objectlevel, idlevel, datalevel);
144                 }
145         }
146 }
147
148 static void unlink_action_cb(
149         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
150         TreeStoreElem *tsep, TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
151 {
152         /* just set action to NULL */
153         BKE_animdata_set_action(CTX_wm_reports(C), tsep->id, NULL);
154 }
155
156 static void unlink_material_cb(
157         bContext *UNUSED(C), ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *te,
158         TreeStoreElem *tsep, TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
159 {
160         Material **matar = NULL;
161         int a, totcol = 0;
162
163         if (GS(tsep->id->name) == ID_OB) {
164                 Object *ob = (Object *)tsep->id;
165                 totcol = ob->totcol;
166                 matar = ob->mat;
167         }
168         else if (GS(tsep->id->name) == ID_ME) {
169                 Mesh *me = (Mesh *)tsep->id;
170                 totcol = me->totcol;
171                 matar = me->mat;
172         }
173         else if (GS(tsep->id->name) == ID_CU) {
174                 Curve *cu = (Curve *)tsep->id;
175                 totcol = cu->totcol;
176                 matar = cu->mat;
177         }
178         else if (GS(tsep->id->name) == ID_MB) {
179                 MetaBall *mb = (MetaBall *)tsep->id;
180                 totcol = mb->totcol;
181                 matar = mb->mat;
182         }
183         else {
184                 BLI_assert(0);
185         }
186
187         if (LIKELY(matar != NULL)) {
188                 for (a = 0; a < totcol; a++) {
189                         if (a == te->index && matar[a]) {
190                                 id_us_min(&matar[a]->id);
191                                 matar[a] = NULL;
192                         }
193                 }
194         }
195 }
196
197 static void unlink_texture_cb(
198         bContext *UNUSED(C), ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *te,
199         TreeStoreElem *tsep, TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
200 {
201         MTex **mtex = NULL;
202         int a;
203
204         if (GS(tsep->id->name) == ID_LS) {
205                 FreestyleLineStyle *ls = (FreestyleLineStyle *)tsep->id;
206                 mtex = ls->mtex;
207         }
208         else {
209                 return;
210         }
211
212         for (a = 0; a < MAX_MTEX; a++) {
213                 if (a == te->index && mtex[a]) {
214                         if (mtex[a]->tex) {
215                                 id_us_min(&mtex[a]->tex->id);
216                                 mtex[a]->tex = NULL;
217                         }
218                 }
219         }
220 }
221
222 static void unlink_collection_cb(
223         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
224         TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
225 {
226         Main *bmain = CTX_data_main(C);
227         Collection *collection = (Collection *)tselem->id;
228
229         if (tsep) {
230                 if (GS(tsep->id->name) == ID_OB) {
231                         Object *ob = (Object *)tsep->id;
232                         ob->dup_group = NULL;
233                         DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
234                         DEG_relations_tag_update(bmain);
235                 }
236                 else if (GS(tsep->id->name) == ID_GR) {
237                         Collection *parent = (Collection *)tsep->id;
238                         id_fake_user_set(&collection->id);
239                         BKE_collection_child_remove(bmain, parent, collection);
240                         DEG_id_tag_update(&parent->id, ID_RECALC_COPY_ON_WRITE);
241                         DEG_relations_tag_update(bmain);
242                 }
243                 else if (GS(tsep->id->name) == ID_SCE) {
244                         Scene *scene = (Scene *)tsep->id;
245                         Collection *parent = BKE_collection_master(scene);
246                         id_fake_user_set(&collection->id);
247                         BKE_collection_child_remove(bmain, parent, collection);
248                         DEG_id_tag_update(&scene->id, ID_RECALC_COPY_ON_WRITE);
249                         DEG_relations_tag_update(bmain);
250                 }
251         }
252 }
253
254 static void unlink_object_cb(
255         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
256         TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
257 {
258         Main *bmain = CTX_data_main(C);
259         Object *ob = (Object *)tselem->id;
260
261         if (tsep) {
262                 if (GS(tsep->id->name) == ID_GR) {
263                         Collection *parent = (Collection *)tsep->id;
264                         BKE_collection_object_remove(bmain, parent, ob, true);
265                         DEG_id_tag_update(&parent->id, ID_RECALC_COPY_ON_WRITE);
266                         DEG_relations_tag_update(bmain);
267                 }
268                 else if (GS(tsep->id->name) == ID_SCE) {
269                         Scene *scene = (Scene *)tsep->id;
270                         Collection *parent = BKE_collection_master(scene);
271                         BKE_collection_object_remove(bmain, parent, ob, true);
272                         DEG_id_tag_update(&scene->id, ID_RECALC_COPY_ON_WRITE);
273                         DEG_relations_tag_update(bmain);
274                 }
275         }
276 }
277
278 static void unlink_world_cb(
279         bContext *UNUSED(C), ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
280         TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
281 {
282         Scene *parscene = (Scene *)tsep->id;
283         World *wo = (World *)tselem->id;
284
285         /* need to use parent scene not just scene, otherwise may end up getting wrong one */
286         id_us_min(&wo->id);
287         parscene->world = NULL;
288 }
289
290 static void outliner_do_libdata_operation(
291         bContext *C, ReportList *reports, Scene *scene, SpaceOops *soops, ListBase *lb,
292         outliner_operation_cb operation_cb,
293         void *user_data)
294 {
295         TreeElement *te;
296         TreeStoreElem *tselem;
297
298         for (te = lb->first; te; te = te->next) {
299                 tselem = TREESTORE(te);
300                 if (tselem->flag & TSE_SELECTED) {
301                         if (ELEM(tselem->type, 0, TSE_LAYER_COLLECTION)) {
302                                 TreeStoreElem *tsep = te->parent ? TREESTORE(te->parent) : NULL;
303                                 operation_cb(C, reports, scene, te, tsep, tselem, user_data);
304                         }
305                 }
306                 if (TSELEM_OPEN(tselem, soops)) {
307                         outliner_do_libdata_operation(C, reports, scene, soops, &te->subtree, operation_cb, user_data);
308                 }
309         }
310 }
311
312 /* ******************************************** */
313 typedef enum eOutliner_PropSceneOps {
314         OL_SCENE_OP_DELETE = 1,
315 } eOutliner_PropSceneOps;
316
317 static const EnumPropertyItem prop_scene_op_types[] = {
318         {OL_SCENE_OP_DELETE, "DELETE", ICON_X, "Delete", ""},
319         {0, NULL, 0, NULL, NULL}
320 };
321
322 static bool outliner_do_scene_operation(
323         bContext *C, eOutliner_PropSceneOps event, ListBase *lb,
324         bool (*operation_cb)(bContext *, eOutliner_PropSceneOps, TreeElement *, TreeStoreElem *))
325 {
326         TreeElement *te;
327         TreeStoreElem *tselem;
328         bool success = false;
329
330         for (te = lb->first; te; te = te->next) {
331                 tselem = TREESTORE(te);
332                 if (tselem->flag & TSE_SELECTED) {
333                         if (operation_cb(C, event, te, tselem)) {
334                                 success = true;
335                         }
336                 }
337         }
338
339         return success;
340 }
341
342 static bool scene_cb(bContext *C, eOutliner_PropSceneOps event, TreeElement *UNUSED(te), TreeStoreElem *tselem)
343 {
344         Scene *scene = (Scene *)tselem->id;
345
346         if (event == OL_SCENE_OP_DELETE) {
347                 if (ED_scene_delete(C, CTX_data_main(C), CTX_wm_window(C), scene)) {
348                         WM_event_add_notifier(C, NC_SCENE | NA_REMOVED, scene);
349                 }
350                 else {
351                         return false;
352                 }
353         }
354
355         return true;
356 }
357
358 static int outliner_scene_operation_exec(bContext *C, wmOperator *op)
359 {
360         SpaceOops *soops = CTX_wm_space_outliner(C);
361         const eOutliner_PropSceneOps event = RNA_enum_get(op->ptr, "type");
362
363         if (outliner_do_scene_operation(C, event, &soops->tree, scene_cb) == false) {
364                 return OPERATOR_CANCELLED;
365         }
366
367         if (event == OL_SCENE_OP_DELETE) {
368                 outliner_cleanup_tree(soops);
369                 ED_undo_push(C, "Delete Scene(s)");
370         }
371         else {
372                 BLI_assert(0);
373                 return OPERATOR_CANCELLED;
374         }
375
376         return OPERATOR_FINISHED;
377 }
378
379 void OUTLINER_OT_scene_operation(wmOperatorType *ot)
380 {
381         /* identifiers */
382         ot->name = "Outliner Scene Operation";
383         ot->idname = "OUTLINER_OT_scene_operation";
384         ot->description = "Context menu for scene operations";
385
386         /* callbacks */
387         ot->invoke = WM_menu_invoke;
388         ot->exec = outliner_scene_operation_exec;
389         ot->poll = ED_operator_outliner_active;
390
391         ot->flag = 0;
392
393         ot->prop = RNA_def_enum(ot->srna, "type", prop_scene_op_types, 0, "Scene Operation", "");
394 }
395 /* ******************************************** */
396
397 static void object_select_cb(
398         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
399         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
400 {
401         ViewLayer *view_layer = CTX_data_view_layer(C);
402         Object *ob = (Object *)tselem->id;
403         Base *base = BKE_view_layer_base_find(view_layer, ob);
404
405         if (base && ((base->flag & BASE_VISIBLE) != 0)) {
406                 base->flag |= BASE_SELECTED;
407         }
408 }
409
410 static void object_select_hierarchy_cb(
411         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *te,
412         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
413 {
414         /* Don't extend because this toggles, which is nice for Ctrl-Click but not for a menu item.
415          * it's especially confusing when multiple items are selected since some toggle on/off. */
416         outliner_item_do_activate_from_tree_element(C, te, tselem, false, true);
417 }
418
419 static void object_deselect_cb(
420         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
421         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
422 {
423         ViewLayer *view_layer = CTX_data_view_layer(C);
424         Object *ob = (Object *)tselem->id;
425         Base *base = BKE_view_layer_base_find(view_layer, ob);
426
427         if (base) {
428                 base->flag &= ~BASE_SELECTED;
429         }
430 }
431
432 static void object_delete_cb(
433         bContext *C, ReportList *reports, Scene *scene, TreeElement *UNUSED(te),
434         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
435 {
436         Object *ob = (Object *)tselem->id;
437         if (ob) {
438                 Main *bmain = CTX_data_main(C);
439                 if (ob->id.tag & LIB_TAG_INDIRECT) {
440                         BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
441                         return;
442                 }
443                 else if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
444                          ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
445                 {
446                         BKE_reportf(reports, RPT_WARNING,
447                                     "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
448                                     ob->id.name + 2, scene->id.name + 2);
449                         return;
450                 }
451
452                 // check also library later
453                 if (ob == CTX_data_edit_object(C)) {
454                         ED_object_editmode_exit(C, EM_FREEDATA);
455                 }
456                 ED_object_base_free_and_unlink(CTX_data_main(C), scene, ob);
457                 /* leave for ED_outliner_id_unref to handle */
458 #if 0
459                 te->directdata = NULL;
460                 tselem->id = NULL;
461 #endif
462         }
463 }
464
465 static void id_local_cb(
466         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
467         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
468 {
469         if (ID_IS_LINKED(tselem->id) && (tselem->id->tag & LIB_TAG_EXTERN)) {
470                 Main *bmain = CTX_data_main(C);
471                 /* if the ID type has no special local function,
472                  * just clear the lib */
473                 if (id_make_local(bmain, tselem->id, false, false) == false) {
474                         id_clear_lib_data(bmain, tselem->id);
475                 }
476                 else {
477                         BKE_main_id_clear_newpoins(bmain);
478                 }
479         }
480 }
481
482 static void id_static_override_cb(
483         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
484         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
485 {
486         if (ID_IS_LINKED(tselem->id) && (tselem->id->tag & LIB_TAG_EXTERN)) {
487                 Main *bmain = CTX_data_main(C);
488                 ID *override_id = BKE_override_static_create_from_id(bmain, tselem->id);
489                 if (override_id != NULL) {
490                         BKE_main_id_clear_newpoins(bmain);
491                 }
492         }
493 }
494
495 static void id_fake_user_set_cb(
496         bContext *UNUSED(C), ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
497         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
498 {
499         ID *id = tselem->id;
500
501         id_fake_user_set(id);
502 }
503
504 static void id_fake_user_clear_cb(
505         bContext *UNUSED(C), ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
506         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
507 {
508         ID *id = tselem->id;
509
510         id_fake_user_clear(id);
511 }
512
513 static void id_select_linked_cb(
514         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
515         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
516 {
517         ID *id = tselem->id;
518
519         ED_object_select_linked_by_id(C, id);
520 }
521
522 static void singleuser_action_cb(
523         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
524         TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
525 {
526         ID *id = tselem->id;
527
528         if (id) {
529                 IdAdtTemplate *iat = (IdAdtTemplate *)tsep->id;
530                 PointerRNA ptr = {{NULL}};
531                 PropertyRNA *prop;
532
533                 RNA_pointer_create(&iat->id, &RNA_AnimData, iat->adt, &ptr);
534                 prop = RNA_struct_find_property(&ptr, "action");
535
536                 id_single_user(C, id, &ptr, prop);
537         }
538 }
539
540 static void singleuser_world_cb(
541         bContext *C, ReportList *UNUSED(reports), Scene *UNUSED(scene), TreeElement *UNUSED(te),
542         TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
543 {
544         ID *id = tselem->id;
545
546         /* need to use parent scene not just scene, otherwise may end up getting wrong one */
547         if (id) {
548                 Scene *parscene = (Scene *)tsep->id;
549                 PointerRNA ptr = {{NULL}};
550                 PropertyRNA *prop;
551
552                 RNA_id_pointer_create(&parscene->id, &ptr);
553                 prop = RNA_struct_find_property(&ptr, "world");
554
555                 id_single_user(C, id, &ptr, prop);
556         }
557 }
558
559 /**
560  * \param select_recurse: Set to false for operations which are already recursively operating on their children.
561  */
562 void outliner_do_object_operation_ex(
563         bContext *C, ReportList *reports, Scene *scene_act, SpaceOops *soops, ListBase *lb,
564         outliner_operation_cb operation_cb, void *user_data, bool select_recurse)
565 {
566         TreeElement *te;
567
568         for (te = lb->first; te; te = te->next) {
569                 TreeStoreElem *tselem = TREESTORE(te);
570                 bool select_handled = false;
571                 if (tselem->flag & TSE_SELECTED) {
572                         if (tselem->type == 0 && te->idcode == ID_OB) {
573                                 // when objects selected in other scenes... dunno if that should be allowed
574                                 Scene *scene_owner = (Scene *)outliner_search_back(soops, te, ID_SCE);
575                                 if (scene_owner && scene_act != scene_owner) {
576                                         WM_window_set_active_scene(CTX_data_main(C), C, CTX_wm_window(C), scene_owner);
577                                 }
578                                 /* important to use 'scene_owner' not scene_act else deleting objects can crash.
579                                  * only use 'scene_act' when 'scene_owner' is NULL, which can happen when the
580                                  * outliner isn't showing scenes: Visible Layer draw mode for eg. */
581                                 operation_cb(C, reports, scene_owner ? scene_owner : scene_act, te, NULL, tselem, user_data);
582                                 select_handled = true;
583                         }
584                 }
585                 if (TSELEM_OPEN(tselem, soops)) {
586                         if ((select_handled == false) || select_recurse) {
587                                 outliner_do_object_operation_ex(
588                                             C, reports, scene_act, soops, &te->subtree, operation_cb, NULL, select_recurse);
589                         }
590                 }
591         }
592 }
593
594 void outliner_do_object_operation(
595         bContext *C, ReportList *reports, Scene *scene_act, SpaceOops *soops, ListBase *lb,
596         outliner_operation_cb operation_cb)
597 {
598         outliner_do_object_operation_ex(C, reports, scene_act, soops, lb, operation_cb, NULL, true);
599 }
600
601 /* ******************************************** */
602
603 static void clear_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
604                               TreeStoreElem *tselem, void *UNUSED(arg))
605 {
606         BKE_animdata_free(tselem->id, true);
607 }
608
609
610 static void unlinkact_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
611                                   TreeStoreElem *tselem, void *UNUSED(arg))
612 {
613         /* just set action to NULL */
614         BKE_animdata_set_action(NULL, tselem->id, NULL);
615 }
616
617 static void cleardrivers_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
618                                      TreeStoreElem *tselem, void *UNUSED(arg))
619 {
620         IdAdtTemplate *iat = (IdAdtTemplate *)tselem->id;
621
622         /* just free drivers - stored as a list of F-Curves */
623         free_fcurves(&iat->adt->drivers);
624 }
625
626 static void refreshdrivers_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
627                                        TreeStoreElem *tselem, void *UNUSED(arg))
628 {
629         IdAdtTemplate *iat = (IdAdtTemplate *)tselem->id;
630         FCurve *fcu;
631
632         /* loop over drivers, performing refresh (i.e. check graph_buttons.c and rna_fcurve.c for details) */
633         for (fcu = iat->adt->drivers.first; fcu; fcu = fcu->next) {
634                 fcu->flag &= ~FCURVE_DISABLED;
635
636                 if (fcu->driver)
637                         fcu->driver->flag &= ~DRIVER_FLAG_INVALID;
638         }
639 }
640
641 /* --------------------------------- */
642
643 typedef enum eOutliner_PropDataOps {
644         OL_DOP_SELECT = 1,
645         OL_DOP_DESELECT,
646         OL_DOP_HIDE,
647         OL_DOP_UNHIDE,
648         OL_DOP_SELECT_LINKED,
649 } eOutliner_PropDataOps;
650
651 typedef enum eOutliner_PropConstraintOps {
652         OL_CONSTRAINTOP_ENABLE = 1,
653         OL_CONSTRAINTOP_DISABLE,
654         OL_CONSTRAINTOP_DELETE
655 } eOutliner_PropConstraintOps;
656
657 typedef enum eOutliner_PropModifierOps {
658         OL_MODIFIER_OP_TOGVIS = 1,
659         OL_MODIFIER_OP_TOGREN,
660         OL_MODIFIER_OP_DELETE
661 } eOutliner_PropModifierOps;
662
663 static void pchan_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
664 {
665         bPoseChannel *pchan = (bPoseChannel *)te->directdata;
666
667         if (event == OL_DOP_SELECT)
668                 pchan->bone->flag |= BONE_SELECTED;
669         else if (event == OL_DOP_DESELECT)
670                 pchan->bone->flag &= ~BONE_SELECTED;
671         else if (event == OL_DOP_HIDE) {
672                 pchan->bone->flag |= BONE_HIDDEN_P;
673                 pchan->bone->flag &= ~BONE_SELECTED;
674         }
675         else if (event == OL_DOP_UNHIDE)
676                 pchan->bone->flag &= ~BONE_HIDDEN_P;
677 }
678
679 static void bone_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
680 {
681         Bone *bone = (Bone *)te->directdata;
682
683         if (event == OL_DOP_SELECT)
684                 bone->flag |= BONE_SELECTED;
685         else if (event == OL_DOP_DESELECT)
686                 bone->flag &= ~BONE_SELECTED;
687         else if (event == OL_DOP_HIDE) {
688                 bone->flag |= BONE_HIDDEN_P;
689                 bone->flag &= ~BONE_SELECTED;
690         }
691         else if (event == OL_DOP_UNHIDE)
692                 bone->flag &= ~BONE_HIDDEN_P;
693 }
694
695 static void ebone_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
696 {
697         EditBone *ebone = (EditBone *)te->directdata;
698
699         if (event == OL_DOP_SELECT)
700                 ebone->flag |= BONE_SELECTED;
701         else if (event == OL_DOP_DESELECT)
702                 ebone->flag &= ~BONE_SELECTED;
703         else if (event == OL_DOP_HIDE) {
704                 ebone->flag |= BONE_HIDDEN_A;
705                 ebone->flag &= ~BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL;
706         }
707         else if (event == OL_DOP_UNHIDE)
708                 ebone->flag &= ~BONE_HIDDEN_A;
709 }
710
711 static void sequence_cb(int event, TreeElement *te, TreeStoreElem *tselem, void *scene_ptr)
712 {
713         Sequence *seq = (Sequence *)te->directdata;
714         if (event == OL_DOP_SELECT) {
715                 Scene *scene = (Scene *)scene_ptr;
716                 Editing *ed = BKE_sequencer_editing_get(scene, false);
717                 if (BLI_findindex(ed->seqbasep, seq) != -1) {
718                         ED_sequencer_select_sequence_single(scene, seq, true);
719                 }
720         }
721
722         (void)tselem;
723 }
724
725 static void gp_layer_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
726 {
727         bGPDlayer *gpl = (bGPDlayer *)te->directdata;
728
729         if (event == OL_DOP_SELECT)
730                 gpl->flag |= GP_LAYER_SELECT;
731         else if (event == OL_DOP_DESELECT)
732                 gpl->flag &= ~GP_LAYER_SELECT;
733         else if (event == OL_DOP_HIDE)
734                 gpl->flag |= GP_LAYER_HIDE;
735         else if (event == OL_DOP_UNHIDE)
736                 gpl->flag &= ~GP_LAYER_HIDE;
737 }
738
739 static void data_select_linked_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *C_v)
740 {
741         if (event == OL_DOP_SELECT_LINKED) {
742                 if (RNA_struct_is_ID(te->rnaptr.type)) {
743                         bContext *C = (bContext *) C_v;
744                         ID *id = te->rnaptr.data;
745
746                         ED_object_select_linked_by_id(C, id);
747                 }
748         }
749 }
750
751 static void constraint_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *C_v)
752 {
753         bContext *C = C_v;
754         Main *bmain = CTX_data_main(C);
755         SpaceOops *soops = CTX_wm_space_outliner(C);
756         bConstraint *constraint = (bConstraint *)te->directdata;
757         Object *ob = (Object *)outliner_search_back(soops, te, ID_OB);
758
759         if (event == OL_CONSTRAINTOP_ENABLE) {
760                 constraint->flag &= ~CONSTRAINT_OFF;
761                 ED_object_constraint_update(bmain, ob);
762                 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
763         }
764         else if (event == OL_CONSTRAINTOP_DISABLE) {
765                 constraint->flag = CONSTRAINT_OFF;
766                 ED_object_constraint_update(bmain, ob);
767                 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
768         }
769         else if (event == OL_CONSTRAINTOP_DELETE) {
770                 ListBase *lb = NULL;
771
772                 if (TREESTORE(te->parent->parent)->type == TSE_POSE_CHANNEL) {
773                         lb = &((bPoseChannel *)te->parent->parent->directdata)->constraints;
774                 }
775                 else {
776                         lb = &ob->constraints;
777                 }
778
779                 if (BKE_constraint_remove_ex(lb, ob, constraint, true)) {
780                         /* there's no active constraint now, so make sure this is the case */
781                         BKE_constraints_active_set(&ob->constraints, NULL);
782
783                         /* needed to set the flags on posebones correctly */
784                         ED_object_constraint_update(bmain, ob);
785
786                         WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT | NA_REMOVED, ob);
787                         te->store_elem->flag &= ~TSE_SELECTED;
788                 }
789         }
790 }
791
792 static void modifier_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *Carg)
793 {
794         bContext *C = (bContext *)Carg;
795         Main *bmain = CTX_data_main(C);
796         SpaceOops *soops = CTX_wm_space_outliner(C);
797         ModifierData *md = (ModifierData *)te->directdata;
798         Object *ob = (Object *)outliner_search_back(soops, te, ID_OB);
799
800         if (event == OL_MODIFIER_OP_TOGVIS) {
801                 md->mode ^= eModifierMode_Realtime;
802                 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
803                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
804         }
805         else if (event == OL_MODIFIER_OP_TOGREN) {
806                 md->mode ^= eModifierMode_Render;
807                 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
808                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
809         }
810         else if (event == OL_MODIFIER_OP_DELETE) {
811                 ED_object_modifier_remove(NULL, bmain, ob, md);
812                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER | NA_REMOVED, ob);
813                 te->store_elem->flag &= ~TSE_SELECTED;
814         }
815 }
816
817 static void outliner_do_data_operation(SpaceOops *soops, int type, int event, ListBase *lb,
818                                        void (*operation_cb)(int, TreeElement *, TreeStoreElem *, void *),
819                                        void *arg)
820 {
821         TreeElement *te;
822         TreeStoreElem *tselem;
823
824         for (te = lb->first; te; te = te->next) {
825                 tselem = TREESTORE(te);
826                 if (tselem->flag & TSE_SELECTED) {
827                         if (tselem->type == type) {
828                                 operation_cb(event, te, tselem, arg);
829                         }
830                 }
831                 if (TSELEM_OPEN(tselem, soops)) {
832                         outliner_do_data_operation(soops, type, event, &te->subtree, operation_cb, arg);
833                 }
834         }
835 }
836
837 static Base *outline_delete_hierarchy(bContext *C, ReportList *reports, Scene *scene, Base *base)
838 {
839         Base *child_base, *base_next;
840         Object *parent;
841         ViewLayer *view_layer = CTX_data_view_layer(C);
842
843         if (!base) {
844                 return NULL;
845         }
846
847         for (child_base = view_layer->object_bases.first; child_base; child_base = base_next) {
848                 base_next = child_base->next;
849                 for (parent = child_base->object->parent; parent && (parent != base->object); parent = parent->parent);
850                 if (parent) {
851                         base_next = outline_delete_hierarchy(C, reports, scene, child_base);
852                 }
853         }
854
855         base_next = base->next;
856
857         Main *bmain = CTX_data_main(C);
858         if (base->object->id.tag & LIB_TAG_INDIRECT) {
859                 BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2);
860                 return base_next;
861         }
862         else if (BKE_library_ID_is_indirectly_used(bmain, base->object) &&
863                  ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0)
864         {
865                 BKE_reportf(reports, RPT_WARNING,
866                             "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
867                             base->object->id.name + 2, scene->id.name + 2);
868                 return base_next;
869         }
870         ED_object_base_free_and_unlink(CTX_data_main(C), scene, base->object);
871         return base_next;
872 }
873
874 static void object_delete_hierarchy_cb(
875         bContext *C, ReportList *reports, Scene *scene,
876         TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
877 {
878         ViewLayer *view_layer = CTX_data_view_layer(C);
879         Base *base = (Base *)te->directdata;
880         Object *obedit = CTX_data_edit_object(C);
881
882         if (!base) {
883                 base = BKE_view_layer_base_find(view_layer, (Object *)tselem->id);
884         }
885         if (base) {
886                 /* Check also library later. */
887                 for (; obedit && (obedit != base->object); obedit = obedit->parent);
888                 if (obedit == base->object) {
889                         ED_object_editmode_exit(C, EM_FREEDATA);
890                 }
891
892                 outline_delete_hierarchy(C, reports, scene, base);
893                 /* leave for ED_outliner_id_unref to handle */
894 #if 0
895                 te->directdata = NULL;
896                 tselem->id = NULL;
897 #endif
898         }
899
900         DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
901         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
902 }
903
904 static Base *outline_batch_delete_hierarchy(
905         ReportList *reports, Main *bmain, ViewLayer *view_layer, Scene *scene, Base *base)
906 {
907         Base *child_base, *base_next;
908         Object *object, *parent;
909
910         if (!base) {
911                 return NULL;
912         }
913
914         object = base->object;
915         for (child_base = view_layer->object_bases.first; child_base; child_base = base_next) {
916                 base_next = child_base->next;
917                 for (parent = child_base->object->parent; parent && (parent != object); parent = parent->parent);
918                 if (parent) {
919                         base_next = outline_batch_delete_hierarchy(reports, bmain, view_layer, scene, child_base);
920                 }
921         }
922
923         base_next = base->next;
924
925         if (object->id.tag & LIB_TAG_INDIRECT) {
926                 BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2);
927                 return base_next;
928         }
929         else if (BKE_library_ID_is_indirectly_used(bmain, object) &&
930                  ID_REAL_USERS(object) <= 1 && ID_EXTRA_USERS(object) == 0)
931         {
932                 BKE_reportf(reports, RPT_WARNING,
933                             "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
934                             object->id.name + 2, scene->id.name + 2);
935                 return base_next;
936         }
937
938         DEG_id_tag_update_ex(bmain, &object->id, ID_RECALC_BASE_FLAGS);
939         BKE_scene_collections_object_remove(bmain, scene, object, false);
940
941         if (object->id.us == 0) {
942                 object->id.tag |= LIB_TAG_DOIT;
943         }
944
945         return base_next;
946 }
947
948 static void object_batch_delete_hierarchy_cb(
949         bContext *C, ReportList *reports, Scene *scene,
950         TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
951 {
952         ViewLayer *view_layer = CTX_data_view_layer(C);
953         Base *base = (Base *)te->directdata;
954         Object *obedit = CTX_data_edit_object(C);
955
956         if (!base) {
957                 base = BKE_view_layer_base_find(view_layer, (Object *)tselem->id);
958         }
959         if (base) {
960                 /* Check also library later. */
961                 for (; obedit && (obedit != base->object); obedit = obedit->parent);
962                 if (obedit == base->object) {
963                         ED_object_editmode_exit(C, EM_FREEDATA);
964                 }
965
966                 outline_batch_delete_hierarchy(reports, CTX_data_main(C), view_layer, scene, base);
967                 /* leave for ED_outliner_id_unref to handle */
968 #if 0
969                 te->directdata = NULL;
970                 tselem->id = NULL;
971 #endif
972         }
973
974         DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
975         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
976 }
977
978 /* **************************************** */
979
980 enum {
981         OL_OP_SELECT = 1,
982         OL_OP_DESELECT,
983         OL_OP_SELECT_HIERARCHY,
984         OL_OP_DELETE,
985         OL_OP_DELETE_HIERARCHY,
986         OL_OP_REMAP,
987         OL_OP_LOCALIZED,  /* disabled, see below */
988         OL_OP_TOGVIS,
989         OL_OP_TOGSEL,
990         OL_OP_TOGREN,
991         OL_OP_RENAME,
992         OL_OP_OBJECT_MODE_ENTER,
993         OL_OP_OBJECT_MODE_EXIT,
994 };
995
996 static const EnumPropertyItem prop_object_op_types[] = {
997         {OL_OP_SELECT, "SELECT", 0, "Select", ""},
998         {OL_OP_DESELECT, "DESELECT", 0, "Deselect", ""},
999         {OL_OP_SELECT_HIERARCHY, "SELECT_HIERARCHY", 0, "Select Hierarchy", ""},
1000         {OL_OP_DELETE, "DELETE", 0, "Delete", ""},
1001         {OL_OP_DELETE_HIERARCHY, "DELETE_HIERARCHY", 0, "Delete Hierarchy", ""},
1002         {OL_OP_REMAP, "REMAP",   0, "Remap Users",
1003          "Make all users of selected data-blocks to use instead a new chosen one"},
1004         {OL_OP_RENAME, "RENAME", 0, "Rename", ""},
1005         {OL_OP_OBJECT_MODE_ENTER, "OBJECT_MODE_ENTER", 0, "Enter Mode", ""},
1006         {OL_OP_OBJECT_MODE_EXIT, "OBJECT_MODE_EXIT", 0, "Exit Mode", ""},
1007         {0, NULL, 0, NULL, NULL}
1008 };
1009
1010 static int outliner_object_operation_exec(bContext *C, wmOperator *op)
1011 {
1012         Main *bmain = CTX_data_main(C);
1013         Scene *scene = CTX_data_scene(C);
1014         wmWindow *win = CTX_wm_window(C);
1015         SpaceOops *soops = CTX_wm_space_outliner(C);
1016         int event;
1017         const char *str = NULL;
1018
1019         /* check for invalid states */
1020         if (soops == NULL)
1021                 return OPERATOR_CANCELLED;
1022
1023         event = RNA_enum_get(op->ptr, "type");
1024
1025         if (event == OL_OP_SELECT) {
1026                 Scene *sce = scene;  // to be able to delete, scenes are set...
1027                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, object_select_cb);
1028                 if (scene != sce) {
1029                         WM_window_set_active_scene(bmain, C, win, sce);
1030                 }
1031
1032                 str = "Select Objects";
1033                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1034                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1035         }
1036         else if (event == OL_OP_SELECT_HIERARCHY) {
1037                 Scene *sce = scene;  // to be able to delete, scenes are set...
1038                 outliner_do_object_operation_ex(
1039                             C, op->reports, scene, soops, &soops->tree, object_select_hierarchy_cb, NULL, false);
1040                 if (scene != sce) {
1041                         WM_window_set_active_scene(bmain, C, win, sce);
1042                 }
1043                 str = "Select Object Hierarchy";
1044                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1045                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1046         }
1047         else if (event == OL_OP_DESELECT) {
1048                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, object_deselect_cb);
1049                 str = "Deselect Objects";
1050                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1051                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1052         }
1053         else if (event == OL_OP_DELETE) {
1054                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, object_delete_cb);
1055
1056                 /* XXX: tree management normally happens from draw_outliner(), but when
1057                  *      you're clicking to fast on Delete object from context menu in
1058                  *      outliner several mouse events can be handled in one cycle without
1059                  *      handling notifiers/redraw which leads to deleting the same object twice.
1060                  *      cleanup tree here to prevent such cases. */
1061                 outliner_cleanup_tree(soops);
1062
1063                 DEG_relations_tag_update(bmain);
1064                 str = "Delete Objects";
1065                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1066                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1067         }
1068         else if (event == OL_OP_DELETE_HIERARCHY) {
1069                 /* Keeping old 'safe and slow' code for a bit (new one enabled on 28/01/2019). */
1070                 if (G.debug_value == 666) {
1071                         outliner_do_object_operation_ex(
1072                                     C, op->reports, scene, soops, &soops->tree, object_delete_hierarchy_cb, NULL, false);
1073                 }
1074                 else {
1075                         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
1076
1077                         outliner_do_object_operation_ex(
1078                                     C, op->reports, scene, soops, &soops->tree, object_batch_delete_hierarchy_cb, NULL, false);
1079
1080                         BKE_id_multi_tagged_delete(bmain);
1081                 }
1082
1083                 /* XXX: See OL_OP_DELETE comment above. */
1084                 outliner_cleanup_tree(soops);
1085
1086                 DEG_relations_tag_update(bmain);
1087                 str = "Delete Object Hierarchy";
1088                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1089                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1090         }
1091         else if (event == OL_OP_REMAP) {
1092                 outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_remap_cb, NULL);
1093                 str = "Remap ID";
1094         }
1095         else if (event == OL_OP_LOCALIZED) {    /* disabled, see above enum (ton) */
1096                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, id_local_cb);
1097                 str = "Localized Objects";
1098         }
1099         else if (event == OL_OP_RENAME) {
1100                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, item_rename_cb);
1101                 str = "Rename Object";
1102         }
1103         else if (event == OL_OP_OBJECT_MODE_ENTER) {
1104                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, item_object_mode_enter_cb);
1105                 str = "Enter Current Mode";
1106         }
1107         else if (event == OL_OP_OBJECT_MODE_EXIT) {
1108                 outliner_do_object_operation(C, op->reports, scene, soops, &soops->tree, item_object_mode_exit_cb);
1109                 str = "Exit Current Mode";
1110         }
1111         else {
1112                 BLI_assert(0);
1113                 return OPERATOR_CANCELLED;
1114         }
1115
1116         ED_undo_push(C, str);
1117
1118         return OPERATOR_FINISHED;
1119 }
1120
1121
1122 void OUTLINER_OT_object_operation(wmOperatorType *ot)
1123 {
1124         /* identifiers */
1125         ot->name = "Outliner Object Operation";
1126         ot->idname = "OUTLINER_OT_object_operation";
1127
1128         /* callbacks */
1129         ot->invoke = WM_menu_invoke;
1130         ot->exec = outliner_object_operation_exec;
1131         ot->poll = ED_operator_outliner_active;
1132
1133         ot->flag = 0;
1134
1135         ot->prop = RNA_def_enum(ot->srna, "type", prop_object_op_types, 0, "Object Operation", "");
1136 }
1137
1138 /* **************************************** */
1139
1140 typedef enum eOutlinerIdOpTypes {
1141         OUTLINER_IDOP_INVALID = 0,
1142
1143         OUTLINER_IDOP_UNLINK,
1144         OUTLINER_IDOP_LOCAL,
1145         OUTLINER_IDOP_STATIC_OVERRIDE,
1146         OUTLINER_IDOP_SINGLE,
1147         OUTLINER_IDOP_DELETE,
1148         OUTLINER_IDOP_REMAP,
1149
1150         OUTLINER_IDOP_FAKE_ADD,
1151         OUTLINER_IDOP_FAKE_CLEAR,
1152         OUTLINER_IDOP_RENAME,
1153
1154         OUTLINER_IDOP_SELECT_LINKED
1155 } eOutlinerIdOpTypes;
1156
1157 // TODO: implement support for changing the ID-block used
1158 static const EnumPropertyItem prop_id_op_types[] = {
1159         {OUTLINER_IDOP_UNLINK, "UNLINK", 0, "Unlink", ""},
1160         {OUTLINER_IDOP_LOCAL, "LOCAL", 0, "Make Local", ""},
1161         {OUTLINER_IDOP_STATIC_OVERRIDE, "STATIC_OVERRIDE", 0, "Add Static Override",
1162          "Add a local static override of this data-block"},
1163         {OUTLINER_IDOP_SINGLE, "SINGLE", 0, "Make Single User", ""},
1164         {OUTLINER_IDOP_DELETE, "DELETE", 0, "Delete", "WARNING: no undo"},
1165         {OUTLINER_IDOP_REMAP, "REMAP", 0, "Remap Users",
1166          "Make all users of selected data-blocks to use instead current (clicked) one"},
1167         {OUTLINER_IDOP_FAKE_ADD, "ADD_FAKE", 0, "Add Fake User",
1168          "Ensure data-block gets saved even if it isn't in use (e.g. for motion and material libraries)"},
1169         {OUTLINER_IDOP_FAKE_CLEAR, "CLEAR_FAKE", 0, "Clear Fake User", ""},
1170         {OUTLINER_IDOP_RENAME, "RENAME", 0, "Rename", ""},
1171         {OUTLINER_IDOP_SELECT_LINKED, "SELECT_LINKED", 0, "Select Linked", ""},
1172         {0, NULL, 0, NULL, NULL}
1173 };
1174
1175 static const EnumPropertyItem *outliner_id_operation_itemf(
1176         bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1177 {
1178         if (BKE_override_static_is_enabled()) {
1179                 *r_free = false;
1180                 return prop_id_op_types;
1181         }
1182
1183         EnumPropertyItem *items = NULL;
1184         int totitem = 0;
1185
1186         for (const EnumPropertyItem *it = prop_id_op_types; it->identifier != NULL; it++) {
1187                 if (it->value == OUTLINER_IDOP_STATIC_OVERRIDE) {
1188                         continue;
1189                 }
1190                 RNA_enum_item_add(&items, &totitem, it);
1191         }
1192         RNA_enum_item_end(&items, &totitem);
1193         *r_free = true;
1194
1195         return items;
1196 }
1197
1198 static int outliner_id_operation_exec(bContext *C, wmOperator *op)
1199 {
1200         Scene *scene = CTX_data_scene(C);
1201         SpaceOops *soops = CTX_wm_space_outliner(C);
1202         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1203         eOutlinerIdOpTypes event;
1204
1205         /* check for invalid states */
1206         if (soops == NULL)
1207                 return OPERATOR_CANCELLED;
1208
1209         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1210
1211         event = RNA_enum_get(op->ptr, "type");
1212
1213         switch (event) {
1214                 case OUTLINER_IDOP_UNLINK:
1215                 {
1216                         /* unlink datablock from its parent */
1217                         if (objectlevel) {
1218                                 outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, unlink_object_cb, NULL);
1219
1220                                 WM_event_add_notifier(C, NC_SCENE | ND_LAYER, NULL);
1221                                 ED_undo_push(C, "Unlink Object");
1222                                 break;
1223                         }
1224
1225                         switch (idlevel) {
1226                                 case ID_AC:
1227                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, unlink_action_cb, NULL);
1228
1229                                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1230                                         ED_undo_push(C, "Unlink action");
1231                                         break;
1232                                 case ID_MA:
1233                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, unlink_material_cb, NULL);
1234
1235                                         WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, NULL);
1236                                         ED_undo_push(C, "Unlink material");
1237                                         break;
1238                                 case ID_TE:
1239                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, unlink_texture_cb, NULL);
1240
1241                                         WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, NULL);
1242                                         ED_undo_push(C, "Unlink texture");
1243                                         break;
1244                                 case ID_WO:
1245                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, unlink_world_cb, NULL);
1246
1247                                         WM_event_add_notifier(C, NC_SCENE | ND_WORLD, NULL);
1248                                         ED_undo_push(C, "Unlink world");
1249                                         break;
1250                                 case ID_GR:
1251                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, unlink_collection_cb, NULL);
1252
1253                                         WM_event_add_notifier(C, NC_SCENE | ND_LAYER, NULL);
1254                                         ED_undo_push(C, "Unlink Collection");
1255                                         break;
1256                                 default:
1257                                         BKE_report(op->reports, RPT_WARNING, "Not yet implemented");
1258                                         break;
1259                         }
1260                         break;
1261                 }
1262                 case OUTLINER_IDOP_LOCAL:
1263                 {
1264                         /* make local */
1265                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_local_cb, NULL);
1266                         ED_undo_push(C, "Localized Data");
1267                         break;
1268                 }
1269                 case OUTLINER_IDOP_STATIC_OVERRIDE:
1270                 {
1271                         if (BKE_override_static_is_enabled()) {
1272                                 /* make local */
1273                                 outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_static_override_cb, NULL);
1274                                 ED_undo_push(C, "Overridden Data");
1275                         }
1276                         break;
1277                 }
1278                 case OUTLINER_IDOP_SINGLE:
1279                 {
1280                         /* make single user */
1281                         switch (idlevel) {
1282                                 case ID_AC:
1283                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, singleuser_action_cb, NULL);
1284
1285                                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1286                                         ED_undo_push(C, "Single-User Action");
1287                                         break;
1288
1289                                 case ID_WO:
1290                                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, singleuser_world_cb, NULL);
1291
1292                                         WM_event_add_notifier(C, NC_SCENE | ND_WORLD, NULL);
1293                                         ED_undo_push(C, "Single-User World");
1294                                         break;
1295
1296                                 default:
1297                                         BKE_report(op->reports, RPT_WARNING, "Not yet implemented");
1298                                         break;
1299                         }
1300                         break;
1301                 }
1302                 case OUTLINER_IDOP_DELETE:
1303                 {
1304                         if (idlevel > 0) {
1305                                 outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_delete_cb, NULL);
1306                                 ED_undo_push(C, "Delete");
1307                         }
1308                         break;
1309                 }
1310                 case OUTLINER_IDOP_REMAP:
1311                 {
1312                         if (idlevel > 0) {
1313                                 outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_remap_cb, NULL);
1314                                 ED_undo_push(C, "Remap");
1315                         }
1316                         break;
1317                 }
1318                 case OUTLINER_IDOP_FAKE_ADD:
1319                 {
1320                         /* set fake user */
1321                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_fake_user_set_cb, NULL);
1322
1323                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1324                         ED_undo_push(C, "Add Fake User");
1325                         break;
1326                 }
1327                 case OUTLINER_IDOP_FAKE_CLEAR:
1328                 {
1329                         /* clear fake user */
1330                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_fake_user_clear_cb, NULL);
1331
1332                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1333                         ED_undo_push(C, "Clear Fake User");
1334                         break;
1335                 }
1336                 case OUTLINER_IDOP_RENAME:
1337                 {
1338                         /* rename */
1339                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, item_rename_cb, NULL);
1340
1341                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1342                         ED_undo_push(C, "Rename");
1343                         break;
1344                 }
1345                 case OUTLINER_IDOP_SELECT_LINKED:
1346                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_select_linked_cb, NULL);
1347                         ED_undo_push(C, "Select");
1348                         break;
1349
1350                 default:
1351                         // invalid - unhandled
1352                         break;
1353         }
1354
1355         /* wrong notifier still... */
1356         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1357
1358         // XXX: this is just so that outliner is always up to date
1359         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
1360
1361         return OPERATOR_FINISHED;
1362 }
1363
1364
1365 void OUTLINER_OT_id_operation(wmOperatorType *ot)
1366 {
1367         /* identifiers */
1368         ot->name = "Outliner ID data Operation";
1369         ot->idname = "OUTLINER_OT_id_operation";
1370
1371         /* callbacks */
1372         ot->invoke = WM_menu_invoke;
1373         ot->exec = outliner_id_operation_exec;
1374         ot->poll = ED_operator_outliner_active;
1375
1376         ot->flag = 0;
1377
1378         ot->prop = RNA_def_enum(ot->srna, "type", prop_id_op_types, 0, "ID data Operation", "");
1379         RNA_def_enum_funcs(ot->prop, outliner_id_operation_itemf);
1380 }
1381
1382 /* **************************************** */
1383
1384 typedef enum eOutlinerLibOpTypes {
1385         OL_LIB_INVALID = 0,
1386
1387         OL_LIB_RENAME,
1388         OL_LIB_DELETE,
1389         OL_LIB_RELOCATE,
1390         OL_LIB_RELOAD,
1391 } eOutlinerLibOpTypes;
1392
1393 static const EnumPropertyItem outliner_lib_op_type_items[] = {
1394         {OL_LIB_RENAME, "RENAME", 0, "Rename", ""},
1395         {OL_LIB_DELETE, "DELETE", 0, "Delete", "Delete this library and all its item from Blender - WARNING: no undo"},
1396         {OL_LIB_RELOCATE, "RELOCATE", 0, "Relocate", "Select a new path for this library, and reload all its data"},
1397         {OL_LIB_RELOAD, "RELOAD", 0, "Reload", "Reload all data from this library"},
1398         {0, NULL, 0, NULL, NULL}
1399 };
1400
1401 static int outliner_lib_operation_exec(bContext *C, wmOperator *op)
1402 {
1403         Scene *scene = CTX_data_scene(C);
1404         SpaceOops *soops = CTX_wm_space_outliner(C);
1405         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1406         eOutlinerLibOpTypes event;
1407
1408         /* check for invalid states */
1409         if (soops == NULL)
1410                 return OPERATOR_CANCELLED;
1411
1412         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1413
1414         event = RNA_enum_get(op->ptr, "type");
1415
1416         switch (event) {
1417                 case OL_LIB_RENAME:
1418                 {
1419                         /* rename */
1420                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, item_rename_cb, NULL);
1421
1422                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1423                         ED_undo_push(C, "Rename Library");
1424                         break;
1425                 }
1426                 case OL_LIB_DELETE:
1427                 {
1428                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_delete_cb, NULL);
1429                         ED_undo_push(C, "Delete Library");
1430                         break;
1431                 }
1432                 case OL_LIB_RELOCATE:
1433                 {
1434                         /* rename */
1435                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, lib_relocate_cb, NULL);
1436                         ED_undo_push(C, "Relocate Library");
1437                         break;
1438                 }
1439                 case OL_LIB_RELOAD:
1440                 {
1441                         /* rename */
1442                         outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, lib_reload_cb, NULL);
1443                         break;
1444                 }
1445                 default:
1446                         /* invalid - unhandled */
1447                         break;
1448         }
1449
1450         /* wrong notifier still... */
1451         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1452
1453         /* XXX: this is just so that outliner is always up to date */
1454         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
1455
1456         return OPERATOR_FINISHED;
1457 }
1458
1459
1460 void OUTLINER_OT_lib_operation(wmOperatorType *ot)
1461 {
1462         /* identifiers */
1463         ot->name = "Outliner Library Operation";
1464         ot->idname = "OUTLINER_OT_lib_operation";
1465
1466         /* callbacks */
1467         ot->invoke = WM_menu_invoke;
1468         ot->exec = outliner_lib_operation_exec;
1469         ot->poll = ED_operator_outliner_active;
1470
1471         ot->prop = RNA_def_enum(ot->srna, "type", outliner_lib_op_type_items, 0, "Library Operation", "");
1472 }
1473
1474 /* **************************************** */
1475
1476 static void outliner_do_id_set_operation(SpaceOops *soops, int type, ListBase *lb, ID *newid,
1477                                          void (*operation_cb)(TreeElement *, TreeStoreElem *, TreeStoreElem *, ID *))
1478 {
1479         TreeElement *te;
1480         TreeStoreElem *tselem;
1481
1482         for (te = lb->first; te; te = te->next) {
1483                 tselem = TREESTORE(te);
1484                 if (tselem->flag & TSE_SELECTED) {
1485                         if (tselem->type == type) {
1486                                 TreeStoreElem *tsep = te->parent ? TREESTORE(te->parent) : NULL;
1487                                 operation_cb(te, tselem, tsep, newid);
1488                         }
1489                 }
1490                 if (TSELEM_OPEN(tselem, soops)) {
1491                         outliner_do_id_set_operation(soops, type, &te->subtree, newid, operation_cb);
1492                 }
1493         }
1494 }
1495
1496 /* ------------------------------------------ */
1497
1498 static void actionset_id_cb(TreeElement *UNUSED(te), TreeStoreElem *tselem, TreeStoreElem *tsep, ID *actId)
1499 {
1500         bAction *act = (bAction *)actId;
1501
1502         if (tselem->type == TSE_ANIM_DATA) {
1503                 /* "animation" entries - action is child of this */
1504                 BKE_animdata_set_action(NULL, tselem->id, act);
1505         }
1506         /* TODO: if any other "expander" channels which own actions need to support this menu,
1507          * add: tselem->type = ...
1508          */
1509         else if (tsep && (tsep->type == TSE_ANIM_DATA)) {
1510                 /* "animation" entries case again */
1511                 BKE_animdata_set_action(NULL, tsep->id, act);
1512         }
1513         // TODO: other cases not supported yet
1514 }
1515
1516 static int outliner_action_set_exec(bContext *C, wmOperator *op)
1517 {
1518         SpaceOops *soops = CTX_wm_space_outliner(C);
1519         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1520
1521         bAction *act;
1522
1523         /* check for invalid states */
1524         if (soops == NULL)
1525                 return OPERATOR_CANCELLED;
1526         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1527
1528         /* get action to use */
1529         act = BLI_findlink(&CTX_data_main(C)->action, RNA_enum_get(op->ptr, "action"));
1530
1531         if (act == NULL) {
1532                 BKE_report(op->reports, RPT_ERROR, "No valid action to add");
1533                 return OPERATOR_CANCELLED;
1534         }
1535         else if (act->idroot == 0) {
1536                 /* hopefully in this case (i.e. library of userless actions), the user knows what they're doing... */
1537                 BKE_reportf(op->reports, RPT_WARNING,
1538                             "Action '%s' does not specify what data-blocks it can be used on "
1539                             "(try setting the 'ID Root Type' setting from the data-blocks editor "
1540                             "for this action to avoid future problems)",
1541                             act->id.name + 2);
1542         }
1543
1544         /* perform action if valid channel */
1545         if (datalevel == TSE_ANIM_DATA)
1546                 outliner_do_id_set_operation(soops, datalevel, &soops->tree, (ID *)act, actionset_id_cb);
1547         else if (idlevel == ID_AC)
1548                 outliner_do_id_set_operation(soops, idlevel, &soops->tree, (ID *)act, actionset_id_cb);
1549         else
1550                 return OPERATOR_CANCELLED;
1551
1552         /* set notifier that things have changed */
1553         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1554         ED_undo_push(C, "Set action");
1555
1556         /* done */
1557         return OPERATOR_FINISHED;
1558 }
1559
1560 void OUTLINER_OT_action_set(wmOperatorType *ot)
1561 {
1562         PropertyRNA *prop;
1563
1564         /* identifiers */
1565         ot->name = "Outliner Set Action";
1566         ot->idname = "OUTLINER_OT_action_set";
1567         ot->description = "Change the active action used";
1568
1569         /* api callbacks */
1570         ot->invoke = WM_enum_search_invoke;
1571         ot->exec = outliner_action_set_exec;
1572         ot->poll = ED_operator_outliner_active;
1573
1574         /* flags */
1575         ot->flag = 0;
1576
1577         /* props */
1578         // TODO: this would be nicer as an ID-pointer...
1579         prop = RNA_def_enum(ot->srna, "action", DummyRNA_NULL_items, 0, "Action", "");
1580         RNA_def_enum_funcs(prop, RNA_action_itemf);
1581         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1582         ot->prop = prop;
1583 }
1584
1585 /* **************************************** */
1586
1587 typedef enum eOutliner_AnimDataOps {
1588         OUTLINER_ANIMOP_INVALID = 0,
1589
1590         OUTLINER_ANIMOP_CLEAR_ADT,
1591
1592         OUTLINER_ANIMOP_SET_ACT,
1593         OUTLINER_ANIMOP_CLEAR_ACT,
1594
1595         OUTLINER_ANIMOP_REFRESH_DRV,
1596         OUTLINER_ANIMOP_CLEAR_DRV
1597
1598         //OUTLINER_ANIMOP_COPY_DRIVERS,
1599         //OUTLINER_ANIMOP_PASTE_DRIVERS
1600 } eOutliner_AnimDataOps;
1601
1602 static const EnumPropertyItem prop_animdata_op_types[] = {
1603         {OUTLINER_ANIMOP_CLEAR_ADT, "CLEAR_ANIMDATA", 0, "Clear Animation Data", "Remove this animation data container"},
1604         {OUTLINER_ANIMOP_SET_ACT, "SET_ACT", 0, "Set Action", ""},
1605         {OUTLINER_ANIMOP_CLEAR_ACT, "CLEAR_ACT", 0, "Unlink Action", ""},
1606         {OUTLINER_ANIMOP_REFRESH_DRV, "REFRESH_DRIVERS", 0, "Refresh Drivers", ""},
1607         //{OUTLINER_ANIMOP_COPY_DRIVERS, "COPY_DRIVERS", 0, "Copy Drivers", ""},
1608         //{OUTLINER_ANIMOP_PASTE_DRIVERS, "PASTE_DRIVERS", 0, "Paste Drivers", ""},
1609         {OUTLINER_ANIMOP_CLEAR_DRV, "CLEAR_DRIVERS", 0, "Clear Drivers", ""},
1610         {0, NULL, 0, NULL, NULL}
1611 };
1612
1613 static int outliner_animdata_operation_exec(bContext *C, wmOperator *op)
1614 {
1615         SpaceOops *soops = CTX_wm_space_outliner(C);
1616         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1617         eOutliner_AnimDataOps event;
1618         short updateDeps = 0;
1619
1620         /* check for invalid states */
1621         if (soops == NULL)
1622                 return OPERATOR_CANCELLED;
1623
1624         event = RNA_enum_get(op->ptr, "type");
1625         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1626
1627         if (datalevel != TSE_ANIM_DATA)
1628                 return OPERATOR_CANCELLED;
1629
1630         /* perform the core operation */
1631         switch (event) {
1632                 case OUTLINER_ANIMOP_CLEAR_ADT:
1633                         /* Remove Animation Data - this may remove the active action, in some cases... */
1634                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, clear_animdata_cb, NULL);
1635
1636                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1637                         ED_undo_push(C, "Clear Animation Data");
1638                         break;
1639
1640                 case OUTLINER_ANIMOP_SET_ACT:
1641                         /* delegate once again... */
1642                         WM_operator_name_call(C, "OUTLINER_OT_action_set", WM_OP_INVOKE_REGION_WIN, NULL);
1643                         break;
1644
1645                 case OUTLINER_ANIMOP_CLEAR_ACT:
1646                         /* clear active action - using standard rules */
1647                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, unlinkact_animdata_cb, NULL);
1648
1649                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1650                         ED_undo_push(C, "Unlink action");
1651                         break;
1652
1653                 case OUTLINER_ANIMOP_REFRESH_DRV:
1654                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, refreshdrivers_animdata_cb, NULL);
1655
1656                         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN, NULL);
1657                         //ED_undo_push(C, "Refresh Drivers"); /* no undo needed - shouldn't have any impact? */
1658                         updateDeps = 1;
1659                         break;
1660
1661                 case OUTLINER_ANIMOP_CLEAR_DRV:
1662                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, cleardrivers_animdata_cb, NULL);
1663
1664                         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN, NULL);
1665                         ED_undo_push(C, "Clear Drivers");
1666                         updateDeps = 1;
1667                         break;
1668
1669                 default: // invalid
1670                         break;
1671         }
1672
1673         /* update dependencies */
1674         if (updateDeps) {
1675                 /* rebuild depsgraph for the new deps */
1676                 DEG_relations_tag_update(CTX_data_main(C));
1677         }
1678
1679         return OPERATOR_FINISHED;
1680 }
1681
1682
1683 void OUTLINER_OT_animdata_operation(wmOperatorType *ot)
1684 {
1685         /* identifiers */
1686         ot->name = "Outliner Animation Data Operation";
1687         ot->idname = "OUTLINER_OT_animdata_operation";
1688
1689         /* callbacks */
1690         ot->invoke = WM_menu_invoke;
1691         ot->exec = outliner_animdata_operation_exec;
1692         ot->poll = ED_operator_outliner_active;
1693
1694         ot->flag = 0;
1695
1696         ot->prop = RNA_def_enum(ot->srna, "type", prop_animdata_op_types, 0, "Animation Operation", "");
1697 }
1698
1699 /* **************************************** */
1700
1701 static const EnumPropertyItem prop_constraint_op_types[] = {
1702         {OL_CONSTRAINTOP_ENABLE, "ENABLE", ICON_HIDE_OFF, "Enable", ""},
1703         {OL_CONSTRAINTOP_DISABLE, "DISABLE", ICON_HIDE_ON, "Disable", ""},
1704         {OL_CONSTRAINTOP_DELETE, "DELETE", ICON_X, "Delete", ""},
1705         {0, NULL, 0, NULL, NULL}
1706 };
1707
1708 static int outliner_constraint_operation_exec(bContext *C, wmOperator *op)
1709 {
1710         SpaceOops *soops = CTX_wm_space_outliner(C);
1711         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1712         eOutliner_PropConstraintOps event;
1713
1714         event = RNA_enum_get(op->ptr, "type");
1715         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1716
1717         outliner_do_data_operation(soops, datalevel, event, &soops->tree, constraint_cb, C);
1718
1719         if (event == OL_CONSTRAINTOP_DELETE) {
1720                 outliner_cleanup_tree(soops);
1721         }
1722
1723         ED_undo_push(C, "Constraint operation");
1724
1725         return OPERATOR_FINISHED;
1726 }
1727
1728 void OUTLINER_OT_constraint_operation(wmOperatorType *ot)
1729 {
1730         /* identifiers */
1731         ot->name = "Outliner Constraint Operation";
1732         ot->idname = "OUTLINER_OT_constraint_operation";
1733
1734         /* callbacks */
1735         ot->invoke = WM_menu_invoke;
1736         ot->exec = outliner_constraint_operation_exec;
1737         ot->poll = ED_operator_outliner_active;
1738
1739         ot->flag = 0;
1740
1741         ot->prop = RNA_def_enum(ot->srna, "type", prop_constraint_op_types, 0, "Constraint Operation", "");
1742 }
1743
1744 /* ******************** */
1745
1746 static const EnumPropertyItem prop_modifier_op_types[] = {
1747         {OL_MODIFIER_OP_TOGVIS, "TOGVIS", ICON_RESTRICT_VIEW_OFF, "Toggle viewport use", ""},
1748         {OL_MODIFIER_OP_TOGREN, "TOGREN", ICON_RESTRICT_RENDER_OFF, "Toggle render use", ""},
1749         {OL_MODIFIER_OP_DELETE, "DELETE", ICON_X, "Delete", ""},
1750         {0, NULL, 0, NULL, NULL}
1751 };
1752
1753 static int outliner_modifier_operation_exec(bContext *C, wmOperator *op)
1754 {
1755         SpaceOops *soops = CTX_wm_space_outliner(C);
1756         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1757         eOutliner_PropModifierOps event;
1758
1759         event = RNA_enum_get(op->ptr, "type");
1760         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1761
1762         outliner_do_data_operation(soops, datalevel, event, &soops->tree, modifier_cb, C);
1763
1764         if (event == OL_MODIFIER_OP_DELETE) {
1765                 outliner_cleanup_tree(soops);
1766         }
1767
1768         ED_undo_push(C, "Modifier operation");
1769
1770         return OPERATOR_FINISHED;
1771 }
1772
1773 void OUTLINER_OT_modifier_operation(wmOperatorType *ot)
1774 {
1775         /* identifiers */
1776         ot->name = "Outliner Modifier Operation";
1777         ot->idname = "OUTLINER_OT_modifier_operation";
1778
1779         /* callbacks */
1780         ot->invoke = WM_menu_invoke;
1781         ot->exec = outliner_modifier_operation_exec;
1782         ot->poll = ED_operator_outliner_active;
1783
1784         ot->flag = 0;
1785
1786         ot->prop = RNA_def_enum(ot->srna, "type", prop_modifier_op_types, 0, "Modifier Operation", "");
1787 }
1788
1789 /* ******************** */
1790
1791 // XXX: select linked is for RNA structs only
1792 static const EnumPropertyItem prop_data_op_types[] = {
1793         {OL_DOP_SELECT, "SELECT", 0, "Select", ""},
1794         {OL_DOP_DESELECT, "DESELECT", 0, "Deselect", ""},
1795         {OL_DOP_HIDE, "HIDE", 0, "Hide", ""},
1796         {OL_DOP_UNHIDE, "UNHIDE", 0, "Unhide", ""},
1797         {OL_DOP_SELECT_LINKED, "SELECT_LINKED", 0, "Select Linked", ""},
1798         {0, NULL, 0, NULL, NULL}
1799 };
1800
1801 static int outliner_data_operation_exec(bContext *C, wmOperator *op)
1802 {
1803         SpaceOops *soops = CTX_wm_space_outliner(C);
1804         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1805         eOutliner_PropDataOps event;
1806
1807         /* check for invalid states */
1808         if (soops == NULL)
1809                 return OPERATOR_CANCELLED;
1810
1811         event = RNA_enum_get(op->ptr, "type");
1812         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1813
1814         switch (datalevel) {
1815                 case TSE_POSE_CHANNEL:
1816                 {
1817                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, pchan_cb, NULL);
1818                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1819                         ED_undo_push(C, "PoseChannel operation");
1820
1821                         break;
1822                 }
1823                 case TSE_BONE:
1824                 {
1825                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, bone_cb, NULL);
1826                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1827                         ED_undo_push(C, "Bone operation");
1828
1829                         break;
1830                 }
1831                 case TSE_EBONE:
1832                 {
1833                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, ebone_cb, NULL);
1834                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1835                         ED_undo_push(C, "EditBone operation");
1836
1837                         break;
1838                 }
1839                 case TSE_SEQUENCE:
1840                 {
1841                         Scene *scene = CTX_data_scene(C);
1842                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, sequence_cb, scene);
1843
1844                         break;
1845                 }
1846                 case TSE_GP_LAYER:
1847                 {
1848                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, gp_layer_cb, NULL);
1849                         WM_event_add_notifier(C, NC_GPENCIL | ND_DATA, NULL);
1850                         ED_undo_push(C, "Grease Pencil Layer operation");
1851
1852                         break;
1853                 }
1854                 case TSE_RNA_STRUCT:
1855                         if (event == OL_DOP_SELECT_LINKED) {
1856                                 outliner_do_data_operation(soops, datalevel, event, &soops->tree, data_select_linked_cb, C);
1857                         }
1858
1859                         break;
1860
1861                 default:
1862                         BKE_report(op->reports, RPT_WARNING, "Not yet implemented");
1863                         break;
1864         }
1865
1866         return OPERATOR_FINISHED;
1867 }
1868
1869
1870 void OUTLINER_OT_data_operation(wmOperatorType *ot)
1871 {
1872         /* identifiers */
1873         ot->name = "Outliner Data Operation";
1874         ot->idname = "OUTLINER_OT_data_operation";
1875
1876         /* callbacks */
1877         ot->invoke = WM_menu_invoke;
1878         ot->exec = outliner_data_operation_exec;
1879         ot->poll = ED_operator_outliner_active;
1880
1881         ot->flag = 0;
1882
1883         ot->prop = RNA_def_enum(ot->srna, "type", prop_data_op_types, 0, "Data Operation", "");
1884 }
1885
1886
1887 /* ******************** */
1888
1889 static int outliner_operator_menu(bContext *C, const char *opname)
1890 {
1891         wmOperatorType *ot = WM_operatortype_find(opname, false);
1892         uiPopupMenu *pup = UI_popup_menu_begin(C, RNA_struct_ui_name(ot->srna), ICON_NONE);
1893         uiLayout *layout = UI_popup_menu_layout(pup);
1894
1895         /* set this so the default execution context is the same as submenus */
1896         uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_REGION_WIN);
1897         uiItemsEnumO(layout, ot->idname, RNA_property_identifier(ot->prop));
1898
1899         MenuType *mt = WM_menutype_find("OUTLINER_MT_context", false);
1900         if (mt) {
1901                 uiItemS(layout);
1902                 UI_menutype_draw(C, mt, layout);
1903         }
1904
1905         UI_popup_menu_end(C, pup);
1906
1907         return OPERATOR_INTERFACE;
1908 }
1909
1910 static int do_outliner_operation_event(bContext *C, ARegion *ar, SpaceOops *soops,
1911                                        TreeElement *te, const float mval[2])
1912 {
1913         ReportList *reports = CTX_wm_reports(C); // XXX...
1914
1915         if (mval[1] > te->ys && mval[1] < te->ys + UI_UNIT_Y) {
1916                 int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1917                 TreeStoreElem *tselem = TREESTORE(te);
1918
1919                 /* select object that's clicked on and popup context menu */
1920                 if (!(tselem->flag & TSE_SELECTED)) {
1921
1922                         if (outliner_flag_is_any_test(&soops->tree, TSE_SELECTED, 1))
1923                                 outliner_flag_set(&soops->tree, TSE_SELECTED, 0);
1924
1925                         tselem->flag |= TSE_SELECTED;
1926
1927                         /* Only redraw, don't rebuild here because TreeElement pointers will
1928                          * become invalid and operations will crash. */
1929                         ED_region_tag_redraw_no_rebuild(ar);
1930                 }
1931
1932                 set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1933
1934                 if (scenelevel) {
1935                         if (objectlevel || datalevel || idlevel) {
1936                                 BKE_report(reports, RPT_WARNING, "Mixed selection");
1937                                 return OPERATOR_CANCELLED;
1938                         }
1939                         else {
1940                                 return outliner_operator_menu(C, "OUTLINER_OT_scene_operation");
1941                         }
1942                 }
1943                 else if (objectlevel) {
1944                         WM_menu_name_call(C, "OUTLINER_MT_object", WM_OP_INVOKE_REGION_WIN);
1945                         return OPERATOR_FINISHED;
1946                 }
1947                 else if (idlevel) {
1948                         if (idlevel == -1 || datalevel) {
1949                                 BKE_report(reports, RPT_WARNING, "Mixed selection");
1950                                 return OPERATOR_CANCELLED;
1951                         }
1952                         else {
1953                                 switch (idlevel) {
1954                                         case ID_GR:
1955                                                 WM_menu_name_call(C, "OUTLINER_MT_collection", WM_OP_INVOKE_REGION_WIN);
1956                                                 return OPERATOR_FINISHED;
1957                                                 break;
1958                                         case ID_LI:
1959                                                 return outliner_operator_menu(C, "OUTLINER_OT_lib_operation");
1960                                                 break;
1961                                         default:
1962                                                 return outliner_operator_menu(C, "OUTLINER_OT_id_operation");
1963                                                 break;
1964                                 }
1965                         }
1966                 }
1967                 else if (datalevel) {
1968                         if (datalevel == -1) {
1969                                 BKE_report(reports, RPT_WARNING, "Mixed selection");
1970                                 return OPERATOR_CANCELLED;
1971                         }
1972                         else {
1973                                 if (datalevel == TSE_ANIM_DATA) {
1974                                         return outliner_operator_menu(C, "OUTLINER_OT_animdata_operation");
1975                                 }
1976                                 else if (datalevel == TSE_DRIVER_BASE) {
1977                                         /* do nothing... no special ops needed yet */
1978                                         return OPERATOR_CANCELLED;
1979                                 }
1980                                 else if (datalevel == TSE_LAYER_COLLECTION) {
1981                                         WM_menu_name_call(C, "OUTLINER_MT_collection", WM_OP_INVOKE_REGION_WIN);
1982                                         return OPERATOR_FINISHED;
1983                                 }
1984                                 else if (ELEM(datalevel, TSE_SCENE_COLLECTION_BASE, TSE_VIEW_COLLECTION_BASE)) {
1985                                         WM_menu_name_call(C, "OUTLINER_MT_collection_new", WM_OP_INVOKE_REGION_WIN);
1986                                         return OPERATOR_FINISHED;
1987                                 }
1988                                 else if (datalevel == TSE_ID_BASE) {
1989                                         /* do nothing... there are no ops needed here yet */
1990                                 }
1991                                 else if (datalevel == TSE_CONSTRAINT) {
1992                                         return outliner_operator_menu(C, "OUTLINER_OT_constraint_operation");
1993                                 }
1994                                 else if (datalevel == TSE_MODIFIER) {
1995                                         return outliner_operator_menu(C, "OUTLINER_OT_modifier_operation");
1996                                 }
1997                                 else {
1998                                         return outliner_operator_menu(C, "OUTLINER_OT_data_operation");
1999                                 }
2000                         }
2001                 }
2002
2003                 return 0;
2004         }
2005
2006         for (te = te->subtree.first; te; te = te->next) {
2007                 int retval = do_outliner_operation_event(C, ar, soops, te, mval);
2008                 if (retval) {
2009                         return retval;
2010                 }
2011         }
2012
2013         return 0;
2014 }
2015
2016
2017 static int outliner_operation(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
2018 {
2019         ARegion *ar = CTX_wm_region(C);
2020         SpaceOops *soops = CTX_wm_space_outliner(C);
2021         uiBut *but = UI_context_active_but_get(C);
2022         TreeElement *te;
2023         float fmval[2];
2024
2025         if (but) {
2026                 UI_but_tooltip_timer_remove(C, but);
2027         }
2028
2029         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
2030
2031         for (te = soops->tree.first; te; te = te->next) {
2032                 int retval = do_outliner_operation_event(C, ar, soops, te, fmval);
2033                 if (retval) {
2034                         return retval;
2035                 }
2036         }
2037
2038         /* Menus for clicking in empty space. */
2039         if (soops->outlinevis == SO_VIEW_LAYER) {
2040                 WM_menu_name_call(C, "OUTLINER_MT_collection_new", WM_OP_INVOKE_REGION_WIN);
2041                 return OPERATOR_FINISHED;
2042         }
2043
2044         WM_menu_name_call(C, "OUTLINER_MT_context", WM_OP_INVOKE_REGION_WIN);
2045         return OPERATOR_FINISHED;
2046 }
2047
2048 /* Menu only! Calls other operators */
2049 void OUTLINER_OT_operation(wmOperatorType *ot)
2050 {
2051         ot->name = "Context Menu";
2052         ot->idname = "OUTLINER_OT_operation";
2053         ot->description = "Context menu for item operations";
2054
2055         ot->invoke = outliner_operation;
2056
2057         ot->poll = ED_operator_outliner_active;
2058 }
2059
2060 /* ****************************************************** */