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