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