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