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