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