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