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