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