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