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