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