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