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