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