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