Split id->flag in two, persistent flags and runtime tags.
[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_gpencil_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_linestyle_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meta_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_sequence_types.h"
45 #include "DNA_world_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_constraint_types.h"
48 #include "DNA_modifier_types.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_utildefines.h"
52
53 #include "BKE_animsys.h"
54 #include "BKE_context.h"
55 #include "BKE_constraint.h"
56 #include "BKE_depsgraph.h"
57 #include "BKE_fcurve.h"
58 #include "BKE_group.h"
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_report.h"
62 #include "BKE_scene.h"
63 #include "BKE_sequencer.h"
64
65 #include "ED_armature.h"
66 #include "ED_object.h"
67 #include "ED_screen.h"
68 #include "ED_sequencer.h"
69 #include "ED_util.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "UI_interface.h"
75 #include "UI_view2d.h"
76 #include "UI_resources.h"
77
78 #include "RNA_access.h"
79 #include "RNA_define.h"
80 #include "RNA_enum_types.h"
81
82 #include "outliner_intern.h"
83
84
85 /* ****************************************************** */
86
87 /* ************ SELECTION OPERATIONS ********* */
88
89 static void set_operation_types(SpaceOops *soops, ListBase *lb,
90                                 int *scenelevel,
91                                 int *objectlevel,
92                                 int *idlevel,
93                                 int *datalevel)
94 {
95         TreeElement *te;
96         TreeStoreElem *tselem;
97         
98         for (te = lb->first; te; te = te->next) {
99                 tselem = TREESTORE(te);
100                 if (tselem->flag & TSE_SELECTED) {
101                         if (tselem->type) {
102                                 if (*datalevel == 0)
103                                         *datalevel = tselem->type;
104                                 else if (*datalevel != tselem->type)
105                                         *datalevel = -1;
106                         }
107                         else {
108                                 int idcode = GS(tselem->id->name);
109                                 switch (idcode) {
110                                         case ID_SCE:
111                                                 *scenelevel = 1;
112                                                 break;
113                                         case ID_OB:
114                                                 *objectlevel = 1;
115                                                 break;
116                                                 
117                                         case ID_ME: case ID_CU: case ID_MB: case ID_LT:
118                                         case ID_LA: case ID_AR: case ID_CA: case ID_SPK:
119                                         case ID_MA: case ID_TE: case ID_IP: case ID_IM:
120                                         case ID_SO: case ID_KE: case ID_WO: case ID_AC:
121                                         case ID_NLA: case ID_TXT: case ID_GR: case ID_LS:
122                                         case ID_LI:
123                                                 if (*idlevel == 0) *idlevel = idcode;
124                                                 else if (*idlevel != idcode) *idlevel = -1;
125                                                 break;
126                                 }
127                         }
128                 }
129                 if (TSELEM_OPEN(tselem, soops)) {
130                         set_operation_types(soops, &te->subtree,
131                                             scenelevel, objectlevel, idlevel, datalevel);
132                 }
133         }
134 }
135
136 static void unlink_action_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
137                              TreeStoreElem *tsep, TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
138 {
139         /* just set action to NULL */
140         BKE_animdata_set_action(CTX_wm_reports(C), tsep->id, NULL);
141 }
142
143 static void unlink_material_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeElement *te,
144                                TreeStoreElem *tsep, TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
145 {
146         Material **matar = NULL;
147         int a, totcol = 0;
148         
149         if (GS(tsep->id->name) == ID_OB) {
150                 Object *ob = (Object *)tsep->id;
151                 totcol = ob->totcol;
152                 matar = ob->mat;
153         }
154         else if (GS(tsep->id->name) == ID_ME) {
155                 Mesh *me = (Mesh *)tsep->id;
156                 totcol = me->totcol;
157                 matar = me->mat;
158         }
159         else if (GS(tsep->id->name) == ID_CU) {
160                 Curve *cu = (Curve *)tsep->id;
161                 totcol = cu->totcol;
162                 matar = cu->mat;
163         }
164         else if (GS(tsep->id->name) == ID_MB) {
165                 MetaBall *mb = (MetaBall *)tsep->id;
166                 totcol = mb->totcol;
167                 matar = mb->mat;
168         }
169         else {
170                 BLI_assert(0);
171         }
172
173         if (LIKELY(matar != NULL)) {
174                 for (a = 0; a < totcol; a++) {
175                         if (a == te->index && matar[a]) {
176                                 id_us_min(&matar[a]->id);
177                                 matar[a] = NULL;
178                         }
179                 }
180         }
181 }
182
183 static void unlink_texture_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeElement *te,
184                               TreeStoreElem *tsep, TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
185 {
186         MTex **mtex = NULL;
187         int a;
188         
189         if (GS(tsep->id->name) == ID_MA) {
190                 Material *ma = (Material *)tsep->id;
191                 mtex = ma->mtex;
192         }
193         else if (GS(tsep->id->name) == ID_LA) {
194                 Lamp *la = (Lamp *)tsep->id;
195                 mtex = la->mtex;
196         }
197         else if (GS(tsep->id->name) == ID_WO) {
198                 World *wrld = (World *)tsep->id;
199                 mtex = wrld->mtex;
200         }
201         else if (GS(tsep->id->name) == ID_LS) {
202                 FreestyleLineStyle *ls = (FreestyleLineStyle *)tsep->id;
203                 mtex = ls->mtex;
204         }
205         else {
206                 return;
207         }
208
209         for (a = 0; a < MAX_MTEX; a++) {
210                 if (a == te->index && mtex[a]) {
211                         if (mtex[a]->tex) {
212                                 id_us_min(&mtex[a]->tex->id);
213                                 mtex[a]->tex = NULL;
214                         }
215                 }
216         }
217 }
218
219 static void unlink_group_cb(
220         bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
221         TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
222 {
223         Group *group = (Group *)tselem->id;
224         
225         if (tsep) {
226                 if (GS(tsep->id->name) == ID_OB) {
227                         Object *ob = (Object *)tsep->id;
228                         ob->dup_group = NULL;
229                 }
230         }
231         else {
232                 Main *bmain = CTX_data_main(C);
233                 BKE_group_unlink(bmain, group);
234         }
235 }
236
237 static void unlink_world_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeElement *UNUSED(te),
238                             TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
239 {
240         Scene *parscene = (Scene *)tsep->id;
241         World *wo = (World *)tselem->id;
242         
243         /* need to use parent scene not just scene, otherwise may end up getting wrong one */
244         id_us_min(&wo->id);
245         parscene->world = NULL;
246 }
247
248 static void outliner_do_libdata_operation(
249         bContext *C, Scene *scene, SpaceOops *soops, ListBase *lb, 
250         void (*operation_cb)(bContext *C, Scene *scene, TreeElement *, TreeStoreElem *, TreeStoreElem *, void *),
251         void *user_data)
252 {
253         TreeElement *te;
254         TreeStoreElem *tselem;
255         
256         for (te = lb->first; te; te = te->next) {
257                 tselem = TREESTORE(te);
258                 if (tselem->flag & TSE_SELECTED) {
259                         if (tselem->type == 0) {
260                                 TreeStoreElem *tsep = te->parent ? TREESTORE(te->parent) : NULL;
261                                 operation_cb(C, scene, te, tsep, tselem, user_data);
262                         }
263                 }
264                 if (TSELEM_OPEN(tselem, soops)) {
265                         outliner_do_libdata_operation(C, scene, soops, &te->subtree, operation_cb, user_data);
266                 }
267         }
268 }
269
270 /* ******************************************** */
271 typedef enum eOutliner_PropSceneOps {
272         OL_SCENE_OP_DELETE = 1
273 } eOutliner_PropSceneOps;
274
275 static EnumPropertyItem prop_scene_op_types[] = {
276         {OL_SCENE_OP_DELETE, "DELETE", ICON_X, "Delete", ""},
277         {0, NULL, 0, NULL, NULL}
278 };
279
280 static bool outliner_do_scene_operation(
281         bContext *C, eOutliner_PropSceneOps event, ListBase *lb,
282         bool (*operation_cb)(bContext *, eOutliner_PropSceneOps, TreeElement *, TreeStoreElem *))
283 {
284         TreeElement *te;
285         TreeStoreElem *tselem;
286         bool success = false;
287
288         for (te = lb->first; te; te = te->next) {
289                 tselem = TREESTORE(te);
290                 if (tselem->flag & TSE_SELECTED) {
291                         if (operation_cb(C, event, te, tselem)) {
292                                 success = true;
293                         }
294                 }
295         }
296
297         return success;
298 }
299
300 static bool scene_cb(bContext *C, eOutliner_PropSceneOps event, TreeElement *UNUSED(te), TreeStoreElem *tselem)
301 {
302         Scene *scene = (Scene *)tselem->id;
303
304         if (event == OL_SCENE_OP_DELETE) {
305                 if (ED_screen_delete_scene(C, scene)) {
306                         WM_event_add_notifier(C, NC_SCENE | NA_REMOVED, scene);
307                 }
308                 else {
309                         return false;
310                 }
311         }
312
313         return true;
314 }
315
316 static int outliner_scene_operation_exec(bContext *C, wmOperator *op)
317 {
318         SpaceOops *soops = CTX_wm_space_outliner(C);
319         const eOutliner_PropSceneOps event = RNA_enum_get(op->ptr, "type");
320
321         if (outliner_do_scene_operation(C, event, &soops->tree, scene_cb) == false) {
322                 return OPERATOR_CANCELLED;
323         }
324
325         if (event == OL_SCENE_OP_DELETE) {
326                 outliner_cleanup_tree(soops);
327                 ED_undo_push(C, "Delete Scene(s)");
328         }
329         else {
330                 BLI_assert(0);
331                 return OPERATOR_CANCELLED;
332         }
333
334         return OPERATOR_FINISHED;
335 }
336
337 void OUTLINER_OT_scene_operation(wmOperatorType *ot)
338 {
339         /* identifiers */
340         ot->name = "Outliner Scene Operation";
341         ot->idname = "OUTLINER_OT_scene_operation";
342         ot->description = "Context menu for scene operations";
343
344         /* callbacks */
345         ot->invoke = WM_menu_invoke;
346         ot->exec = outliner_scene_operation_exec;
347         ot->poll = ED_operator_outliner_active;
348
349         ot->flag = 0;
350
351         ot->prop = RNA_def_enum(ot->srna, "type", prop_scene_op_types, 0, "Scene Operation", "");
352 }
353 /* ******************************************** */
354
355 static void object_select_cb(bContext *UNUSED(C), Scene *scene, TreeElement *te,
356                              TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
357 {
358         Base *base = (Base *)te->directdata;
359         
360         if (base == NULL) base = BKE_scene_base_find(scene, (Object *)tselem->id);
361         if (base && ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0)) {
362                 base->flag |= SELECT;
363                 base->object->flag |= SELECT;
364         }
365 }
366
367 static void object_select_hierarchy_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
368                              TreeStoreElem *UNUSED(tsep), TreeStoreElem *UNUSED(tselem), void *UNUSED(user_data))
369 {
370         /* From where do i get the x,y coordinate of the mouse event ? */
371         wmWindow *win = CTX_wm_window(C);
372         int x = win->eventstate->mval[0];
373         int y = win->eventstate->mval[1];
374         outliner_item_do_activate(C, x, y, true, true);
375 }
376
377
378 static void object_deselect_cb(bContext *UNUSED(C), Scene *scene, TreeElement *te,
379                                TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
380 {
381         Base *base = (Base *)te->directdata;
382         
383         if (base == NULL) base = BKE_scene_base_find(scene, (Object *)tselem->id);
384         if (base) {
385                 base->flag &= ~SELECT;
386                 base->object->flag &= ~SELECT;
387         }
388 }
389
390 static void object_delete_cb(bContext *C, Scene *scene, TreeElement *te,
391                              TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
392 {
393         Base *base = (Base *)te->directdata;
394         
395         if (base == NULL)
396                 base = BKE_scene_base_find(scene, (Object *)tselem->id);
397         if (base) {
398                 // check also library later
399                 if (scene->obedit == base->object)
400                         ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);
401                 
402                 ED_base_object_free_and_unlink(CTX_data_main(C), scene, base);
403                 te->directdata = NULL;
404                 tselem->id = NULL;
405         }
406 }
407
408 static void id_local_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
409                         TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
410 {
411         if (tselem->id->lib && (tselem->id->tag & LIB_TAG_EXTERN)) {
412                 /* if the ID type has no special local function,
413                  * just clear the lib */
414                 if (id_make_local(tselem->id, false) == false) {
415                         Main *bmain = CTX_data_main(C);
416                         id_clear_lib_data(bmain, tselem->id);
417                 }
418         }
419 }
420
421 static void id_fake_user_set_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeElement *UNUSED(te),
422                                 TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
423 {
424         ID *id = tselem->id;
425         
426         id_fake_user_set(id);
427 }
428
429 static void id_fake_user_clear_cb(bContext *UNUSED(C), Scene *UNUSED(scene), TreeElement *UNUSED(te),
430                                   TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
431 {
432         ID *id = tselem->id;
433         
434         id_fake_user_clear(id);
435 }
436
437 static void id_select_linked_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
438                                 TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
439 {
440         ID *id = tselem->id;
441
442         ED_object_select_linked_by_id(C, id);
443 }
444
445 static void singleuser_action_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
446                                  TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
447 {
448         ID *id = tselem->id;
449         
450         if (id) {
451                 IdAdtTemplate *iat = (IdAdtTemplate *)tsep->id;
452                 PointerRNA ptr = {{NULL}};
453                 PropertyRNA *prop;
454                 
455                 RNA_pointer_create(&iat->id, &RNA_AnimData, iat->adt, &ptr);
456                 prop = RNA_struct_find_property(&ptr, "action");
457                 
458                 id_single_user(C, id, &ptr, prop);
459         }
460 }
461
462 static void singleuser_world_cb(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te),
463                                 TreeStoreElem *tsep, TreeStoreElem *tselem, void *UNUSED(user_data))
464 {
465         ID *id = tselem->id;
466         
467         /* need to use parent scene not just scene, otherwise may end up getting wrong one */
468         if (id) {
469                 Scene *parscene = (Scene *)tsep->id;
470                 PointerRNA ptr = {{NULL}};
471                 PropertyRNA *prop;
472                 
473                 RNA_id_pointer_create(&parscene->id, &ptr);
474                 prop = RNA_struct_find_property(&ptr, "world");
475                 
476                 id_single_user(C, id, &ptr, prop);
477         }
478 }
479
480 static void group_linkobs2scene_cb(bContext *UNUSED(C), Scene *scene, TreeElement *UNUSED(te),
481                                    TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
482 {
483         Group *group = (Group *)tselem->id;
484         GroupObject *gob;
485         Base *base;
486         
487         for (gob = group->gobject.first; gob; gob = gob->next) {
488                 base = BKE_scene_base_find(scene, gob->ob);
489                 if (base) {
490                         base->object->flag |= SELECT;
491                         base->flag |= SELECT;
492                 }
493                 else {
494                         /* link to scene */
495                         base = MEM_callocN(sizeof(Base), "add_base");
496                         BLI_addhead(&scene->base, base);
497                         base->lay = gob->ob->lay;
498                         gob->ob->flag |= SELECT;
499                         base->flag = gob->ob->flag;
500                         base->object = gob->ob;
501                         id_lib_extern((ID *)gob->ob); /* in case these are from a linked group */
502                 }
503         }
504 }
505
506 static void group_instance_cb(bContext *C, Scene *scene, TreeElement *UNUSED(te),
507                               TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
508 {
509         Group *group = (Group *)tselem->id;
510
511         Object *ob = ED_object_add_type(C, OB_EMPTY, group->id.name + 2, scene->cursor, NULL, false, scene->layact);
512         ob->dup_group = group;
513         ob->transflag |= OB_DUPLIGROUP;
514         id_lib_extern(&group->id);
515 }
516
517 /**
518  * \param select_recurse: Set to false for operations which are already recursively operating on their children.
519  */
520 void outliner_do_object_operation_ex(
521         bContext *C, Scene *scene_act, SpaceOops *soops, ListBase *lb,
522         void (*operation_cb)(bContext *C, Scene *scene, TreeElement *,
523                              TreeStoreElem *, TreeStoreElem *, void *),
524         bool select_recurse)
525 {
526         TreeElement *te;
527         
528         for (te = lb->first; te; te = te->next) {
529                 TreeStoreElem *tselem = TREESTORE(te);
530                 bool select_handled = false;
531                 if (tselem->flag & TSE_SELECTED) {
532                         if (tselem->type == 0 && te->idcode == ID_OB) {
533                                 // when objects selected in other scenes... dunno if that should be allowed
534                                 Scene *scene_owner = (Scene *)outliner_search_back(soops, te, ID_SCE);
535                                 if (scene_owner && scene_act != scene_owner) {
536                                         ED_screen_set_scene(C, CTX_wm_screen(C), scene_owner);
537                                 }
538                                 /* important to use 'scene_owner' not scene_act else deleting objects can crash.
539                                  * only use 'scene_act' when 'scene_owner' is NULL, which can happen when the
540                                  * outliner isn't showing scenes: Visible Layer draw mode for eg. */
541                                 operation_cb(C, scene_owner ? scene_owner : scene_act, te, NULL, tselem, NULL);
542                                 select_handled = true;
543                         }
544                 }
545                 if (TSELEM_OPEN(tselem, soops)) {
546                         if ((select_handled == false) || select_recurse) {
547                                 outliner_do_object_operation_ex(C, scene_act, soops, &te->subtree, operation_cb, select_recurse);
548                         }
549                 }
550         }
551 }
552
553 void outliner_do_object_operation(
554         bContext *C, Scene *scene_act, SpaceOops *soops, ListBase *lb,
555         void (*operation_cb)(bContext *, Scene *, TreeElement *, TreeStoreElem *, TreeStoreElem *, void *))
556 {
557         outliner_do_object_operation_ex(C, scene_act, soops, lb, operation_cb, true);
558 }
559
560 /* ******************************************** */
561
562 static void clear_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
563                               TreeStoreElem *tselem, void *UNUSED(arg))
564 {
565         BKE_animdata_free(tselem->id);
566 }
567
568
569 static void unlinkact_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
570                                   TreeStoreElem *tselem, void *UNUSED(arg))
571 {
572         /* just set action to NULL */
573         BKE_animdata_set_action(NULL, tselem->id, NULL);
574 }
575
576 static void cleardrivers_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
577                                      TreeStoreElem *tselem, void *UNUSED(arg))
578 {
579         IdAdtTemplate *iat = (IdAdtTemplate *)tselem->id;
580         
581         /* just free drivers - stored as a list of F-Curves */
582         free_fcurves(&iat->adt->drivers);
583 }
584
585 static void refreshdrivers_animdata_cb(int UNUSED(event), TreeElement *UNUSED(te),
586                                        TreeStoreElem *tselem, void *UNUSED(arg))
587 {
588         IdAdtTemplate *iat = (IdAdtTemplate *)tselem->id;
589         FCurve *fcu;
590         
591         /* loop over drivers, performing refresh (i.e. check graph_buttons.c and rna_fcurve.c for details) */
592         for (fcu = iat->adt->drivers.first; fcu; fcu = fcu->next) {
593                 fcu->flag &= ~FCURVE_DISABLED;
594                 
595                 if (fcu->driver)
596                         fcu->driver->flag &= ~DRIVER_FLAG_INVALID;
597         }
598 }
599
600 /* --------------------------------- */
601
602 typedef enum eOutliner_PropDataOps {
603         OL_DOP_SELECT = 1,
604         OL_DOP_DESELECT,
605         OL_DOP_HIDE,
606         OL_DOP_UNHIDE,
607         OL_DOP_SELECT_LINKED,
608 } eOutliner_PropDataOps;
609
610 typedef enum eOutliner_PropConstraintOps {
611         OL_CONSTRAINTOP_ENABLE = 1,
612         OL_CONSTRAINTOP_DISABLE,
613         OL_CONSTRAINTOP_DELETE
614 } eOutliner_PropConstraintOps;
615
616 typedef enum eOutliner_PropModifierOps {
617         OL_MODIFIER_OP_TOGVIS = 1,
618         OL_MODIFIER_OP_TOGREN,
619         OL_MODIFIER_OP_DELETE
620 } eOutliner_PropModifierOps;
621
622 static void pchan_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
623 {
624         bPoseChannel *pchan = (bPoseChannel *)te->directdata;
625         
626         if (event == OL_DOP_SELECT)
627                 pchan->bone->flag |= BONE_SELECTED;
628         else if (event == OL_DOP_DESELECT)
629                 pchan->bone->flag &= ~BONE_SELECTED;
630         else if (event == OL_DOP_HIDE) {
631                 pchan->bone->flag |= BONE_HIDDEN_P;
632                 pchan->bone->flag &= ~BONE_SELECTED;
633         }
634         else if (event == OL_DOP_UNHIDE)
635                 pchan->bone->flag &= ~BONE_HIDDEN_P;
636 }
637
638 static void bone_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
639 {
640         Bone *bone = (Bone *)te->directdata;
641         
642         if (event == OL_DOP_SELECT)
643                 bone->flag |= BONE_SELECTED;
644         else if (event == OL_DOP_DESELECT)
645                 bone->flag &= ~BONE_SELECTED;
646         else if (event == OL_DOP_HIDE) {
647                 bone->flag |= BONE_HIDDEN_P;
648                 bone->flag &= ~BONE_SELECTED;
649         }
650         else if (event == OL_DOP_UNHIDE)
651                 bone->flag &= ~BONE_HIDDEN_P;
652 }
653
654 static void ebone_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
655 {
656         EditBone *ebone = (EditBone *)te->directdata;
657         
658         if (event == OL_DOP_SELECT)
659                 ebone->flag |= BONE_SELECTED;
660         else if (event == OL_DOP_DESELECT)
661                 ebone->flag &= ~BONE_SELECTED;
662         else if (event == OL_DOP_HIDE) {
663                 ebone->flag |= BONE_HIDDEN_A;
664                 ebone->flag &= ~BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL;
665         }
666         else if (event == OL_DOP_UNHIDE)
667                 ebone->flag &= ~BONE_HIDDEN_A;
668 }
669
670 static void sequence_cb(int event, TreeElement *te, TreeStoreElem *tselem, void *scene_ptr)
671 {
672         Sequence *seq = (Sequence *)te->directdata;
673         if (event == OL_DOP_SELECT) {
674                 Scene *scene = (Scene *)scene_ptr;
675                 Editing *ed = BKE_sequencer_editing_get(scene, false);
676                 if (BLI_findindex(ed->seqbasep, seq) != -1) {
677                         ED_sequencer_select_sequence_single(scene, seq, true);
678                 }
679         }
680
681         (void)tselem;
682 }
683
684 static void gp_layer_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *UNUSED(arg))
685 {
686         bGPDlayer *gpl = (bGPDlayer *)te->directdata;
687         
688         if (event == OL_DOP_SELECT)
689                 gpl->flag |= GP_LAYER_SELECT;
690         else if (event == OL_DOP_DESELECT)
691                 gpl->flag &= ~GP_LAYER_SELECT;
692         else if (event == OL_DOP_HIDE)
693                 gpl->flag |= GP_LAYER_HIDE;
694         else if (event == OL_DOP_UNHIDE)
695                 gpl->flag &= ~GP_LAYER_HIDE;
696 }
697
698 static void data_select_linked_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *C_v)
699 {
700         if (event == OL_DOP_SELECT_LINKED) {
701                 if (RNA_struct_is_ID(te->rnaptr.type)) {
702                         bContext *C = (bContext *) C_v;
703                         ID *id = te->rnaptr.data;
704
705                         ED_object_select_linked_by_id(C, id);
706                 }
707         }
708 }
709
710 static void constraint_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *C_v)
711 {
712         bContext *C = C_v;
713         SpaceOops *soops = CTX_wm_space_outliner(C);
714         bConstraint *constraint = (bConstraint *)te->directdata;
715         Object *ob = (Object *)outliner_search_back(soops, te, ID_OB);
716
717         if (event == OL_CONSTRAINTOP_ENABLE) {
718                 constraint->flag &= ~CONSTRAINT_OFF;
719                 ED_object_constraint_update(ob);
720                 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
721         }
722         else if (event == OL_CONSTRAINTOP_DISABLE) {
723                 constraint->flag = CONSTRAINT_OFF;
724                 ED_object_constraint_update(ob);
725                 WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT, ob);
726         }
727         else if (event == OL_CONSTRAINTOP_DELETE) {
728                 ListBase *lb = NULL;
729
730                 if (TREESTORE(te->parent->parent)->type == TSE_POSE_CHANNEL) {
731                         lb = &((bPoseChannel *)te->parent->parent->directdata)->constraints;
732                 }
733                 else {
734                         lb = &ob->constraints;
735                 }
736
737                 if (BKE_constraint_remove_ex(lb, ob, constraint, true)) {
738                         /* there's no active constraint now, so make sure this is the case */
739                         BKE_constraints_active_set(&ob->constraints, NULL);
740                         ED_object_constraint_update(ob); /* needed to set the flags on posebones correctly */
741                         WM_event_add_notifier(C, NC_OBJECT | ND_CONSTRAINT | NA_REMOVED, ob);
742                         te->store_elem->flag &= ~TSE_SELECTED;
743                 }
744         }
745 }
746
747 static void modifier_cb(int event, TreeElement *te, TreeStoreElem *UNUSED(tselem), void *Carg)
748 {
749         bContext *C = (bContext *)Carg;
750         Main *bmain = CTX_data_main(C);
751         SpaceOops *soops = CTX_wm_space_outliner(C);
752         ModifierData *md = (ModifierData *)te->directdata;
753         Object *ob = (Object *)outliner_search_back(soops, te, ID_OB);
754
755         if (event == OL_MODIFIER_OP_TOGVIS) {
756                 md->mode ^= eModifierMode_Realtime;
757                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
758                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
759         }
760         else if (event == OL_MODIFIER_OP_TOGREN) {
761                 md->mode ^= eModifierMode_Render;
762                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
763                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
764         }
765         else if (event == OL_MODIFIER_OP_DELETE) {
766                 ED_object_modifier_remove(NULL, bmain, ob, md);
767                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER | NA_REMOVED, ob);
768                 te->store_elem->flag &= ~TSE_SELECTED;
769         }
770 }
771
772 static void outliner_do_data_operation(SpaceOops *soops, int type, int event, ListBase *lb,
773                                        void (*operation_cb)(int, TreeElement *, TreeStoreElem *, void *),
774                                        void *arg)
775 {
776         TreeElement *te;
777         TreeStoreElem *tselem;
778         
779         for (te = lb->first; te; te = te->next) {
780                 tselem = TREESTORE(te);
781                 if (tselem->flag & TSE_SELECTED) {
782                         if (tselem->type == type) {
783                                 operation_cb(event, te, tselem, arg);
784                         }
785                 }
786                 if (TSELEM_OPEN(tselem, soops)) {
787                         outliner_do_data_operation(soops, type, event, &te->subtree, operation_cb, arg);
788                 }
789         }
790 }
791
792 static Base *outline_delete_hierarchy(bContext *C, Scene *scene, Base *base)
793 {
794         Base *child_base, *base_next;
795         Object *parent;
796
797         if (!base) {
798                 return NULL;
799         }
800
801         for (child_base = scene->base.first; child_base; child_base = base_next) {
802                 base_next = child_base->next;
803                 for (parent = child_base->object->parent; parent && (parent != base->object); parent = parent->parent);
804                 if (parent) {
805                         base_next = outline_delete_hierarchy(C, scene, child_base);
806                 }
807         }
808
809         base_next = base->next;
810         ED_base_object_free_and_unlink(CTX_data_main(C), scene, base);
811         return base_next;
812 }
813
814 static void object_delete_hierarchy_cb(
815         bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data))
816 {
817         Base *base = (Base *)te->directdata;
818         Object *obedit = scene->obedit;
819
820         if (!base) {
821                 base = BKE_scene_base_find(scene, (Object *)tselem->id);
822         }
823         if (base) {
824                 /* Check also library later. */
825                 for (; obedit && (obedit != base->object); obedit = obedit->parent);
826                 if (obedit == base->object) {
827                         ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);
828                 }
829
830                 outline_delete_hierarchy(C, scene, base);
831                 te->directdata = NULL;
832                 tselem->id = NULL;
833         }
834
835         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
836 }
837
838 /* **************************************** */
839
840 enum {
841         OL_OP_SELECT = 1,
842         OL_OP_DESELECT,
843         OL_OP_SELECT_HIERARCHY,
844         OL_OP_DELETE,
845         OL_OP_DELETE_HIERARCHY,
846         OL_OP_LOCALIZED,  /* disabled, see below */
847         OL_OP_TOGVIS,
848         OL_OP_TOGSEL,
849         OL_OP_TOGREN,
850         OL_OP_RENAME,
851 };
852
853 static EnumPropertyItem prop_object_op_types[] = {
854         {OL_OP_SELECT, "SELECT", 0, "Select", ""},
855         {OL_OP_DESELECT, "DESELECT", 0, "Deselect", ""},
856         {OL_OP_SELECT_HIERARCHY, "SELECT_HIERARCHY", 0, "Select Hierarchy", ""},
857         {OL_OP_DELETE, "DELETE", 0, "Delete", ""},
858         {OL_OP_DELETE_HIERARCHY, "DELETE_HIERARCHY", 0, "Delete Hierarchy", ""},
859         {OL_OP_TOGVIS, "TOGVIS", 0, "Toggle Visible", ""},
860         {OL_OP_TOGSEL, "TOGSEL", 0, "Toggle Selectable", ""},
861         {OL_OP_TOGREN, "TOGREN", 0, "Toggle Renderable", ""},
862         {OL_OP_RENAME, "RENAME", 0, "Rename", ""},
863         {0, NULL, 0, NULL, NULL}
864 };
865
866 static int outliner_object_operation_exec(bContext *C, wmOperator *op)
867 {
868         Main *bmain = CTX_data_main(C);
869         Scene *scene = CTX_data_scene(C);
870         SpaceOops *soops = CTX_wm_space_outliner(C);
871         int event;
872         const char *str = NULL;
873         
874         /* check for invalid states */
875         if (soops == NULL)
876                 return OPERATOR_CANCELLED;
877         
878         event = RNA_enum_get(op->ptr, "type");
879
880         if (event == OL_OP_SELECT) {
881                 Scene *sce = scene;  // to be able to delete, scenes are set...
882                 outliner_do_object_operation(C, scene, soops, &soops->tree, object_select_cb);
883                 if (scene != sce) {
884                         ED_screen_set_scene(C, CTX_wm_screen(C), sce);
885                 }
886                 
887                 str = "Select Objects";
888                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
889         }
890         else if (event == OL_OP_SELECT_HIERARCHY) {
891                 Scene *sce = scene;  // to be able to delete, scenes are set...
892                 outliner_do_object_operation_ex(C, scene, soops, &soops->tree, object_select_hierarchy_cb, false);
893                 if (scene != sce) {
894                         ED_screen_set_scene(C, CTX_wm_screen(C), sce);
895                 }       
896                 str = "Select Object Hierarchy";
897                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
898         }
899         else if (event == OL_OP_DESELECT) {
900                 outliner_do_object_operation(C, scene, soops, &soops->tree, object_deselect_cb);
901                 str = "Deselect Objects";
902                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
903         }
904         else if (event == OL_OP_DELETE) {
905                 outliner_do_object_operation(C, scene, soops, &soops->tree, object_delete_cb);
906
907                 /* XXX: tree management normally happens from draw_outliner(), but when
908                  *      you're clicking to fast on Delete object from context menu in
909                  *      outliner several mouse events can be handled in one cycle without
910                  *      handling notifiers/redraw which leads to deleting the same object twice.
911                  *      cleanup tree here to prevent such cases. */
912                 outliner_cleanup_tree(soops);
913
914                 DAG_relations_tag_update(bmain);
915                 str = "Delete Objects";
916                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
917         }
918         else if (event == OL_OP_DELETE_HIERARCHY) {
919                 outliner_do_object_operation_ex(C, scene, soops, &soops->tree, object_delete_hierarchy_cb, false);
920
921                 /* XXX: See OL_OP_DELETE comment above. */
922                 outliner_cleanup_tree(soops);
923
924                 DAG_relations_tag_update(bmain);
925                 str = "Delete Object Hierarchy";
926                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
927         }
928         else if (event == OL_OP_LOCALIZED) {    /* disabled, see above enum (ton) */
929                 outliner_do_object_operation(C, scene, soops, &soops->tree, id_local_cb);
930                 str = "Localized Objects";
931         }
932         else if (event == OL_OP_TOGVIS) {
933                 outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_visibility_cb);
934                 str = "Toggle Visibility";
935                 WM_event_add_notifier(C, NC_SCENE | ND_OB_VISIBLE, scene);
936         }
937         else if (event == OL_OP_TOGSEL) {
938                 outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_selectability_cb);
939                 str = "Toggle Selectability";
940                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
941         }
942         else if (event == OL_OP_TOGREN) {
943                 outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_renderability_cb);
944                 str = "Toggle Renderability";
945                 WM_event_add_notifier(C, NC_SCENE | ND_OB_RENDER, scene);
946         }
947         else if (event == OL_OP_RENAME) {
948                 outliner_do_object_operation(C, scene, soops, &soops->tree, item_rename_cb);
949                 str = "Rename Object";
950         }
951         else {
952                 BLI_assert(0);
953                 return OPERATOR_CANCELLED;
954         }
955
956         ED_undo_push(C, str);
957         
958         return OPERATOR_FINISHED;
959 }
960
961
962 void OUTLINER_OT_object_operation(wmOperatorType *ot)
963 {
964         /* identifiers */
965         ot->name = "Outliner Object Operation";
966         ot->idname = "OUTLINER_OT_object_operation";
967         ot->description = "";
968         
969         /* callbacks */
970         ot->invoke = WM_menu_invoke;
971         ot->exec = outliner_object_operation_exec;
972         ot->poll = ED_operator_outliner_active;
973         
974         ot->flag = 0;
975
976         ot->prop = RNA_def_enum(ot->srna, "type", prop_object_op_types, 0, "Object Operation", "");
977 }
978
979 /* **************************************** */
980
981 typedef enum eOutliner_PropGroupOps {
982         OL_GROUPOP_UNLINK = 1,
983         OL_GROUPOP_LOCAL,
984         OL_GROUPOP_LINK,
985         OL_GROUPOP_INSTANCE,
986         OL_GROUPOP_TOGVIS,
987         OL_GROUPOP_TOGSEL,
988         OL_GROUPOP_TOGREN,
989         OL_GROUPOP_RENAME,
990 } eOutliner_PropGroupOps;
991
992 static EnumPropertyItem prop_group_op_types[] = {
993         {OL_GROUPOP_UNLINK, "UNLINK",     0, "Unlink Group", ""},
994         {OL_GROUPOP_LOCAL, "LOCAL",       0, "Make Local Group", ""},
995         {OL_GROUPOP_LINK, "LINK",         0, "Link Group Objects to Scene", ""},
996         {OL_GROUPOP_INSTANCE, "INSTANCE", 0, "Instance Groups in Scene", ""},
997         {OL_GROUPOP_TOGVIS, "TOGVIS",     0, "Toggle Visible Group", ""},
998         {OL_GROUPOP_TOGSEL, "TOGSEL",     0, "Toggle Selectable", ""},
999         {OL_GROUPOP_TOGREN, "TOGREN",     0, "Toggle Renderable", ""},
1000         {OL_GROUPOP_RENAME, "RENAME",     0, "Rename", ""},
1001         {0, NULL, 0, NULL, NULL}
1002 };
1003
1004 static int outliner_group_operation_exec(bContext *C, wmOperator *op)
1005 {
1006         Scene *scene = CTX_data_scene(C);
1007         SpaceOops *soops = CTX_wm_space_outliner(C);
1008         int event;
1009         
1010         /* check for invalid states */
1011         if (soops == NULL)
1012                 return OPERATOR_CANCELLED;
1013         
1014         event = RNA_enum_get(op->ptr, "type");
1015
1016         switch (event) {
1017                 case OL_GROUPOP_UNLINK:
1018                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, unlink_group_cb, NULL);
1019                         break;
1020                 case OL_GROUPOP_LOCAL:
1021                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, id_local_cb, NULL);
1022                         break;
1023                 case OL_GROUPOP_LINK:
1024                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, group_linkobs2scene_cb, NULL);
1025                         break;
1026                 case OL_GROUPOP_INSTANCE:
1027                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, group_instance_cb, NULL);
1028                         break;
1029                 case OL_GROUPOP_TOGVIS:
1030                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, group_toggle_visibility_cb, NULL);
1031                         break;
1032                 case OL_GROUPOP_TOGSEL:
1033                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, group_toggle_selectability_cb, NULL);
1034                         break;
1035                 case OL_GROUPOP_TOGREN:
1036                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, group_toggle_renderability_cb, NULL);
1037                         break;
1038                 case OL_GROUPOP_RENAME:
1039                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, item_rename_cb, NULL);
1040                         break;
1041                 default:
1042                         BLI_assert(0);
1043         }
1044
1045         if (event == 3) { /* instance */
1046                 /* works without this except if you try render right after, see: 22027 */
1047                 DAG_relations_tag_update(CTX_data_main(C));
1048         }
1049
1050         ED_undo_push(C, prop_group_op_types[event - 1].name);
1051         WM_event_add_notifier(C, NC_GROUP, NULL);
1052
1053         return OPERATOR_FINISHED;
1054 }
1055
1056
1057 void OUTLINER_OT_group_operation(wmOperatorType *ot)
1058 {
1059         /* identifiers */
1060         ot->name = "Outliner Group Operation";
1061         ot->idname = "OUTLINER_OT_group_operation";
1062         ot->description = "";
1063         
1064         /* callbacks */
1065         ot->invoke = WM_menu_invoke;
1066         ot->exec = outliner_group_operation_exec;
1067         ot->poll = ED_operator_outliner_active;
1068         
1069         ot->flag = 0;
1070         
1071         ot->prop = RNA_def_enum(ot->srna, "type", prop_group_op_types, 0, "Group Operation", "");
1072 }
1073
1074 /* **************************************** */
1075
1076 typedef enum eOutlinerIdOpTypes {
1077         OUTLINER_IDOP_INVALID = 0,
1078         
1079         OUTLINER_IDOP_UNLINK,
1080         OUTLINER_IDOP_LOCAL,
1081         OUTLINER_IDOP_SINGLE,
1082         
1083         OUTLINER_IDOP_FAKE_ADD,
1084         OUTLINER_IDOP_FAKE_CLEAR,
1085         OUTLINER_IDOP_RENAME,
1086
1087         OUTLINER_IDOP_SELECT_LINKED
1088 } eOutlinerIdOpTypes;
1089
1090 // TODO: implement support for changing the ID-block used
1091 static EnumPropertyItem prop_id_op_types[] = {
1092         {OUTLINER_IDOP_UNLINK, "UNLINK", 0, "Unlink", ""},
1093         {OUTLINER_IDOP_LOCAL, "LOCAL", 0, "Make Local", ""},
1094         {OUTLINER_IDOP_SINGLE, "SINGLE", 0, "Make Single User", ""},
1095         {OUTLINER_IDOP_FAKE_ADD, "ADD_FAKE", 0, "Add Fake User",
1096          "Ensure datablock gets saved even if it isn't in use (e.g. for motion and material libraries)"},
1097         {OUTLINER_IDOP_FAKE_CLEAR, "CLEAR_FAKE", 0, "Clear Fake User", ""},
1098         {OUTLINER_IDOP_RENAME, "RENAME", 0, "Rename", ""},
1099         {OUTLINER_IDOP_SELECT_LINKED, "SELECT_LINKED", 0, "Select Linked", ""},
1100         {0, NULL, 0, NULL, NULL}
1101 };
1102
1103 static int outliner_id_operation_exec(bContext *C, wmOperator *op)
1104 {
1105         Scene *scene = CTX_data_scene(C);
1106         SpaceOops *soops = CTX_wm_space_outliner(C);
1107         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1108         eOutlinerIdOpTypes event;
1109         
1110         /* check for invalid states */
1111         if (soops == NULL)
1112                 return OPERATOR_CANCELLED;
1113         
1114         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1115         
1116         event = RNA_enum_get(op->ptr, "type");
1117         
1118         switch (event) {
1119                 case OUTLINER_IDOP_UNLINK:
1120                 {
1121                         /* unlink datablock from its parent */
1122                         switch (idlevel) {
1123                                 case ID_AC:
1124                                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, unlink_action_cb, NULL);
1125                                         
1126                                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1127                                         ED_undo_push(C, "Unlink action");
1128                                         break;
1129                                 case ID_MA:
1130                                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, unlink_material_cb, NULL);
1131                                         
1132                                         WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, NULL);
1133                                         ED_undo_push(C, "Unlink material");
1134                                         break;
1135                                 case ID_TE:
1136                                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, unlink_texture_cb, NULL);
1137                                         
1138                                         WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, NULL);
1139                                         ED_undo_push(C, "Unlink texture");
1140                                         break;
1141                                 case ID_WO:
1142                                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, unlink_world_cb, NULL);
1143                                         
1144                                         WM_event_add_notifier(C, NC_SCENE | ND_WORLD, NULL);
1145                                         ED_undo_push(C, "Unlink world");
1146                                         break;
1147                                 default:
1148                                         BKE_report(op->reports, RPT_WARNING, "Not yet implemented");
1149                                         break;
1150                         }
1151                         break;
1152                 }
1153                 case OUTLINER_IDOP_LOCAL:
1154                 {
1155                         /* make local */
1156                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, id_local_cb, NULL);
1157                         ED_undo_push(C, "Localized Data");
1158                         break;
1159                 }
1160                 case OUTLINER_IDOP_SINGLE:
1161                 {
1162                         /* make single user */
1163                         switch (idlevel) {
1164                                 case ID_AC:
1165                                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, singleuser_action_cb, NULL);
1166                                         
1167                                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1168                                         ED_undo_push(C, "Single-User Action");
1169                                         break;
1170                                         
1171                                 case ID_WO:
1172                                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, singleuser_world_cb, NULL);
1173                                         
1174                                         WM_event_add_notifier(C, NC_SCENE | ND_WORLD, NULL);
1175                                         ED_undo_push(C, "Single-User World");
1176                                         break;
1177                                         
1178                                 default:
1179                                         BKE_report(op->reports, RPT_WARNING, "Not yet implemented");
1180                                         break;
1181                         }
1182                         break;
1183                 }
1184                 case OUTLINER_IDOP_FAKE_ADD:
1185                 {
1186                         /* set fake user */
1187                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, id_fake_user_set_cb, NULL);
1188                         
1189                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1190                         ED_undo_push(C, "Add Fake User");
1191                         break;
1192                 }
1193                 case OUTLINER_IDOP_FAKE_CLEAR:
1194                 {
1195                         /* clear fake user */
1196                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, id_fake_user_clear_cb, NULL);
1197                         
1198                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1199                         ED_undo_push(C, "Clear Fake User");
1200                         break;
1201                 }
1202                 case OUTLINER_IDOP_RENAME:
1203                 {
1204                         /* rename */
1205                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, item_rename_cb, NULL);
1206                         
1207                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1208                         ED_undo_push(C, "Rename");
1209                         break;
1210                 }
1211                 case OUTLINER_IDOP_SELECT_LINKED:
1212                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, id_select_linked_cb, NULL);
1213                         ED_undo_push(C, "Select");
1214                         break;
1215                         
1216                 default:
1217                         // invalid - unhandled
1218                         break;
1219         }
1220         
1221         /* wrong notifier still... */
1222         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1223         
1224         // XXX: this is just so that outliner is always up to date 
1225         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
1226         
1227         return OPERATOR_FINISHED;
1228 }
1229
1230
1231 void OUTLINER_OT_id_operation(wmOperatorType *ot)
1232 {
1233         /* identifiers */
1234         ot->name = "Outliner ID data Operation";
1235         ot->idname = "OUTLINER_OT_id_operation";
1236         ot->description = "";
1237         
1238         /* callbacks */
1239         ot->invoke = WM_menu_invoke;
1240         ot->exec = outliner_id_operation_exec;
1241         ot->poll = ED_operator_outliner_active;
1242         
1243         ot->flag = 0;
1244         
1245         ot->prop = RNA_def_enum(ot->srna, "type", prop_id_op_types, 0, "ID data Operation", "");
1246 }
1247
1248 /* **************************************** */
1249
1250 typedef enum eOutlinerLibOpTypes {
1251         OL_LIB_INVALID = 0,
1252
1253         OL_LIB_RENAME,
1254         OL_LIB_DELETE,
1255 } eOutlinerLibOpTypes;
1256
1257 static EnumPropertyItem outliner_lib_op_type_items[] = {
1258         {OL_LIB_RENAME, "RENAME", 0, "Rename", ""},
1259     {OL_LIB_DELETE, "DELETE", 0, "Delete", "Delete this library and all its item from Blender (needs a save/reload)"},
1260         {0, NULL, 0, NULL, NULL}
1261
1262 };
1263
1264 static int outliner_lib_operation_exec(bContext *C, wmOperator *op)
1265 {
1266         Scene *scene = CTX_data_scene(C);
1267         SpaceOops *soops = CTX_wm_space_outliner(C);
1268         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1269         eOutlinerIdOpTypes event;
1270
1271         /* check for invalid states */
1272         if (soops == NULL)
1273                 return OPERATOR_CANCELLED;
1274
1275         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1276
1277         event = RNA_enum_get(op->ptr, "type");
1278
1279         switch (event) {
1280                 case OL_LIB_RENAME:
1281                 {
1282                         /* rename */
1283                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, item_rename_cb, NULL);
1284
1285                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1286                         ED_undo_push(C, "Rename");
1287                         break;
1288                 }
1289                 case OL_LIB_DELETE:
1290                 {
1291                         /* delete */
1292                         outliner_do_libdata_operation(C, scene, soops, &soops->tree, lib_delete_cb, NULL);
1293
1294                         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1295                         /* Note: no undo possible here really, not 100% sure why...
1296                          * Probably because of library optimizations in undo/redo process? */
1297                         /* ED_undo_push(C, "Rename"); */
1298                         break;
1299                 }
1300                 default:
1301                         /* invalid - unhandled */
1302                         break;
1303         }
1304
1305         /* wrong notifier still... */
1306         WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL);
1307
1308         /* XXX: this is just so that outliner is always up to date */
1309         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_OUTLINER, NULL);
1310
1311         return OPERATOR_FINISHED;
1312 }
1313
1314
1315 void OUTLINER_OT_lib_operation(wmOperatorType *ot)
1316 {
1317         /* identifiers */
1318         ot->name = "Outliner Library Operation";
1319         ot->idname = "OUTLINER_OT_lib_operation";
1320         ot->description = "";
1321
1322         /* callbacks */
1323         ot->invoke = WM_menu_invoke;
1324         ot->exec = outliner_lib_operation_exec;
1325         ot->poll = ED_operator_outliner_active;
1326
1327         ot->prop = RNA_def_enum(ot->srna, "type", outliner_lib_op_type_items, 0, "Library Operation", "");
1328 }
1329
1330 /* **************************************** */
1331
1332 static void outliner_do_id_set_operation(SpaceOops *soops, int type, ListBase *lb, ID *newid,
1333                                          void (*operation_cb)(TreeElement *, TreeStoreElem *, TreeStoreElem *, ID *))
1334 {
1335         TreeElement *te;
1336         TreeStoreElem *tselem;
1337         
1338         for (te = lb->first; te; te = te->next) {
1339                 tselem = TREESTORE(te);
1340                 if (tselem->flag & TSE_SELECTED) {
1341                         if (tselem->type == type) {
1342                                 TreeStoreElem *tsep = te->parent ? TREESTORE(te->parent) : NULL;
1343                                 operation_cb(te, tselem, tsep, newid);
1344                         }
1345                 }
1346                 if (TSELEM_OPEN(tselem, soops)) {
1347                         outliner_do_id_set_operation(soops, type, &te->subtree, newid, operation_cb);
1348                 }
1349         }
1350 }
1351
1352 /* ------------------------------------------ */
1353
1354 static void actionset_id_cb(TreeElement *UNUSED(te), TreeStoreElem *tselem, TreeStoreElem *tsep, ID *actId)
1355 {
1356         bAction *act = (bAction *)actId;
1357         
1358         if (tselem->type == TSE_ANIM_DATA) {
1359                 /* "animation" entries - action is child of this */
1360                 BKE_animdata_set_action(NULL, tselem->id, act);
1361         }
1362         /* TODO: if any other "expander" channels which own actions need to support this menu, 
1363          * add: tselem->type = ...
1364          */
1365         else if (tsep && (tsep->type == TSE_ANIM_DATA)) {
1366                 /* "animation" entries case again */
1367                 BKE_animdata_set_action(NULL, tsep->id, act);
1368         }
1369         // TODO: other cases not supported yet
1370 }
1371
1372 static int outliner_action_set_exec(bContext *C, wmOperator *op)
1373 {
1374         SpaceOops *soops = CTX_wm_space_outliner(C);
1375         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1376         
1377         bAction *act;
1378         
1379         /* check for invalid states */
1380         if (soops == NULL)
1381                 return OPERATOR_CANCELLED;
1382         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1383         
1384         /* get action to use */
1385         act = BLI_findlink(&CTX_data_main(C)->action, RNA_enum_get(op->ptr, "action"));
1386         
1387         if (act == NULL) {
1388                 BKE_report(op->reports, RPT_ERROR, "No valid action to add");
1389                 return OPERATOR_CANCELLED;
1390         }
1391         else if (act->idroot == 0) {
1392                 /* hopefully in this case (i.e. library of userless actions), the user knows what they're doing... */
1393                 BKE_reportf(op->reports, RPT_WARNING,
1394                             "Action '%s' does not specify what datablocks it can be used on "
1395                             "(try setting the 'ID Root Type' setting from the Datablocks Editor "
1396                             "for this action to avoid future problems)",
1397                             act->id.name + 2);
1398         }
1399         
1400         /* perform action if valid channel */
1401         if (datalevel == TSE_ANIM_DATA)
1402                 outliner_do_id_set_operation(soops, datalevel, &soops->tree, (ID *)act, actionset_id_cb);
1403         else if (idlevel == ID_AC)
1404                 outliner_do_id_set_operation(soops, idlevel, &soops->tree, (ID *)act, actionset_id_cb);
1405         else
1406                 return OPERATOR_CANCELLED;
1407                 
1408         /* set notifier that things have changed */
1409         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1410         ED_undo_push(C, "Set action");
1411         
1412         /* done */
1413         return OPERATOR_FINISHED;
1414 }
1415
1416 void OUTLINER_OT_action_set(wmOperatorType *ot)
1417 {
1418         PropertyRNA *prop;
1419
1420         /* identifiers */
1421         ot->name = "Outliner Set Action";
1422         ot->idname = "OUTLINER_OT_action_set";
1423         ot->description = "Change the active action used";
1424         
1425         /* api callbacks */
1426         ot->invoke = WM_enum_search_invoke;
1427         ot->exec = outliner_action_set_exec;
1428         ot->poll = ED_operator_outliner_active;
1429         
1430         /* flags */
1431         ot->flag = 0;
1432         
1433         /* props */
1434         // TODO: this would be nicer as an ID-pointer...
1435         prop = RNA_def_enum(ot->srna, "action", DummyRNA_NULL_items, 0, "Action", "");
1436         RNA_def_enum_funcs(prop, RNA_action_itemf);
1437         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1438         ot->prop = prop;
1439 }
1440
1441 /* **************************************** */
1442
1443 typedef enum eOutliner_AnimDataOps {
1444         OUTLINER_ANIMOP_INVALID = 0,
1445         
1446         OUTLINER_ANIMOP_CLEAR_ADT,
1447         
1448         OUTLINER_ANIMOP_SET_ACT,
1449         OUTLINER_ANIMOP_CLEAR_ACT,
1450         
1451         OUTLINER_ANIMOP_REFRESH_DRV,
1452         OUTLINER_ANIMOP_CLEAR_DRV
1453         
1454         //OUTLINER_ANIMOP_COPY_DRIVERS,
1455         //OUTLINER_ANIMOP_PASTE_DRIVERS
1456 } eOutliner_AnimDataOps;
1457
1458 static EnumPropertyItem prop_animdata_op_types[] = {
1459         {OUTLINER_ANIMOP_CLEAR_ADT, "CLEAR_ANIMDATA", 0, "Clear Animation Data", "Remove this animation data container"},
1460         {OUTLINER_ANIMOP_SET_ACT, "SET_ACT", 0, "Set Action", ""},
1461         {OUTLINER_ANIMOP_CLEAR_ACT, "CLEAR_ACT", 0, "Unlink Action", ""},
1462         {OUTLINER_ANIMOP_REFRESH_DRV, "REFRESH_DRIVERS", 0, "Refresh Drivers", ""},
1463         //{OUTLINER_ANIMOP_COPY_DRIVERS, "COPY_DRIVERS", 0, "Copy Drivers", ""},
1464         //{OUTLINER_ANIMOP_PASTE_DRIVERS, "PASTE_DRIVERS", 0, "Paste Drivers", ""},
1465         {OUTLINER_ANIMOP_CLEAR_DRV, "CLEAR_DRIVERS", 0, "Clear Drivers", ""},
1466         {0, NULL, 0, NULL, NULL}
1467 };
1468
1469 static int outliner_animdata_operation_exec(bContext *C, wmOperator *op)
1470 {
1471         SpaceOops *soops = CTX_wm_space_outliner(C);
1472         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1473         eOutliner_AnimDataOps event;
1474         short updateDeps = 0;
1475         
1476         /* check for invalid states */
1477         if (soops == NULL)
1478                 return OPERATOR_CANCELLED;
1479         
1480         event = RNA_enum_get(op->ptr, "type");
1481         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1482         
1483         if (datalevel != TSE_ANIM_DATA)
1484                 return OPERATOR_CANCELLED;
1485         
1486         /* perform the core operation */
1487         switch (event) {
1488                 case OUTLINER_ANIMOP_CLEAR_ADT:
1489                         /* Remove Animation Data - this may remove the active action, in some cases... */
1490                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, clear_animdata_cb, NULL);
1491                         
1492                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1493                         ED_undo_push(C, "Clear Animation Data");
1494                         break;
1495                 
1496                 case OUTLINER_ANIMOP_SET_ACT:
1497                         /* delegate once again... */
1498                         WM_operator_name_call(C, "OUTLINER_OT_action_set", WM_OP_INVOKE_REGION_WIN, NULL);
1499                         break;
1500                 
1501                 case OUTLINER_ANIMOP_CLEAR_ACT:
1502                         /* clear active action - using standard rules */
1503                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, unlinkact_animdata_cb, NULL);
1504                         
1505                         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
1506                         ED_undo_push(C, "Unlink action");
1507                         break;
1508                         
1509                 case OUTLINER_ANIMOP_REFRESH_DRV:
1510                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, refreshdrivers_animdata_cb, NULL);
1511                         
1512                         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN, NULL);
1513                         //ED_undo_push(C, "Refresh Drivers"); /* no undo needed - shouldn't have any impact? */
1514                         updateDeps = 1;
1515                         break;
1516                         
1517                 case OUTLINER_ANIMOP_CLEAR_DRV:
1518                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, cleardrivers_animdata_cb, NULL);
1519                         
1520                         WM_event_add_notifier(C, NC_ANIMATION | ND_ANIMCHAN, NULL);
1521                         ED_undo_push(C, "Clear Drivers");
1522                         updateDeps = 1;
1523                         break;
1524                         
1525                 default: // invalid
1526                         break;
1527         }
1528         
1529         /* update dependencies */
1530         if (updateDeps) {
1531                 /* rebuild depsgraph for the new deps */
1532                 DAG_relations_tag_update(CTX_data_main(C));
1533         }
1534         
1535         return OPERATOR_FINISHED;
1536 }
1537
1538
1539 void OUTLINER_OT_animdata_operation(wmOperatorType *ot)
1540 {
1541         /* identifiers */
1542         ot->name = "Outliner Animation Data Operation";
1543         ot->idname = "OUTLINER_OT_animdata_operation";
1544         ot->description = "";
1545         
1546         /* callbacks */
1547         ot->invoke = WM_menu_invoke;
1548         ot->exec = outliner_animdata_operation_exec;
1549         ot->poll = ED_operator_outliner_active;
1550         
1551         ot->flag = 0;
1552         
1553         ot->prop = RNA_def_enum(ot->srna, "type", prop_animdata_op_types, 0, "Animation Operation", "");
1554 }
1555
1556 /* **************************************** */
1557
1558 static EnumPropertyItem prop_constraint_op_types[] = {
1559         {OL_CONSTRAINTOP_ENABLE, "ENABLE", ICON_RESTRICT_VIEW_OFF, "Enable", ""},
1560         {OL_CONSTRAINTOP_DISABLE, "DISABLE", ICON_RESTRICT_VIEW_ON, "Disable", ""},
1561         {OL_CONSTRAINTOP_DELETE, "DELETE", ICON_X, "Delete", ""},
1562         {0, NULL, 0, NULL, NULL}
1563 };
1564
1565 static int outliner_constraint_operation_exec(bContext *C, wmOperator *op)
1566 {
1567         SpaceOops *soops = CTX_wm_space_outliner(C);
1568         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1569         eOutliner_PropConstraintOps event;
1570
1571         event = RNA_enum_get(op->ptr, "type");
1572         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1573
1574         outliner_do_data_operation(soops, datalevel, event, &soops->tree, constraint_cb, C);
1575
1576         if (event == OL_CONSTRAINTOP_DELETE) {
1577                 outliner_cleanup_tree(soops);
1578         }
1579
1580         ED_undo_push(C, "Constraint operation");
1581
1582         return OPERATOR_FINISHED;
1583 }
1584
1585 void OUTLINER_OT_constraint_operation(wmOperatorType *ot)
1586 {
1587         /* identifiers */
1588         ot->name = "Outliner Constraint Operation";
1589         ot->idname = "OUTLINER_OT_constraint_operation";
1590         ot->description = "";
1591
1592         /* callbacks */
1593         ot->invoke = WM_menu_invoke;
1594         ot->exec = outliner_constraint_operation_exec;
1595         ot->poll = ED_operator_outliner_active;
1596
1597         ot->flag = 0;
1598
1599         ot->prop = RNA_def_enum(ot->srna, "type", prop_constraint_op_types, 0, "Constraint Operation", "");
1600 }
1601
1602 /* ******************** */
1603
1604 static EnumPropertyItem prop_modifier_op_types[] = {
1605         {OL_MODIFIER_OP_TOGVIS, "TOGVIS", ICON_RESTRICT_VIEW_OFF, "Toggle viewport use", ""},
1606         {OL_MODIFIER_OP_TOGREN, "TOGREN", ICON_RESTRICT_RENDER_OFF, "Toggle render use", ""},
1607         {OL_MODIFIER_OP_DELETE, "DELETE", ICON_X, "Delete", ""},
1608         {0, NULL, 0, NULL, NULL}
1609 };
1610
1611 static int outliner_modifier_operation_exec(bContext *C, wmOperator *op)
1612 {
1613         SpaceOops *soops = CTX_wm_space_outliner(C);
1614         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1615         eOutliner_PropModifierOps event;
1616
1617         event = RNA_enum_get(op->ptr, "type");
1618         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1619
1620         outliner_do_data_operation(soops, datalevel, event, &soops->tree, modifier_cb, C);
1621
1622         if (event == OL_MODIFIER_OP_DELETE) {
1623                 outliner_cleanup_tree(soops);
1624         }
1625
1626         ED_undo_push(C, "Modifier operation");
1627
1628         return OPERATOR_FINISHED;
1629 }
1630
1631 void OUTLINER_OT_modifier_operation(wmOperatorType *ot)
1632 {
1633         /* identifiers */
1634         ot->name = "Outliner Modifier Operation";
1635         ot->idname = "OUTLINER_OT_modifier_operation";
1636         ot->description = "";
1637
1638         /* callbacks */
1639         ot->invoke = WM_menu_invoke;
1640         ot->exec = outliner_modifier_operation_exec;
1641         ot->poll = ED_operator_outliner_active;
1642
1643         ot->flag = 0;
1644
1645         ot->prop = RNA_def_enum(ot->srna, "type", prop_modifier_op_types, 0, "Modifier Operation", "");
1646 }
1647
1648 /* ******************** */
1649
1650 // XXX: select linked is for RNA structs only
1651 static EnumPropertyItem prop_data_op_types[] = {
1652         {OL_DOP_SELECT, "SELECT", 0, "Select", ""},
1653         {OL_DOP_DESELECT, "DESELECT", 0, "Deselect", ""},
1654         {OL_DOP_HIDE, "HIDE", 0, "Hide", ""},
1655         {OL_DOP_UNHIDE, "UNHIDE", 0, "Unhide", ""},
1656         {OL_DOP_SELECT_LINKED, "SELECT_LINKED", 0, "Select Linked", ""},
1657         {0, NULL, 0, NULL, NULL}
1658 };
1659
1660 static int outliner_data_operation_exec(bContext *C, wmOperator *op)
1661 {
1662         SpaceOops *soops = CTX_wm_space_outliner(C);
1663         int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1664         eOutliner_PropDataOps event;
1665         
1666         /* check for invalid states */
1667         if (soops == NULL)
1668                 return OPERATOR_CANCELLED;
1669         
1670         event = RNA_enum_get(op->ptr, "type");
1671         set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1672         
1673         switch (datalevel) {
1674                 case TSE_POSE_CHANNEL:
1675                 {
1676                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, pchan_cb, NULL);
1677                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1678                         ED_undo_push(C, "PoseChannel operation");
1679
1680                         break;
1681                 }
1682                 case TSE_BONE:
1683                 {
1684                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, bone_cb, NULL);
1685                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1686                         ED_undo_push(C, "Bone operation");
1687
1688                         break;
1689                 }
1690                 case TSE_EBONE:
1691                 {
1692                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, ebone_cb, NULL);
1693                         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, NULL);
1694                         ED_undo_push(C, "EditBone operation");
1695
1696                         break;
1697                 }
1698                 case TSE_SEQUENCE:
1699                 {
1700                         Scene *scene = CTX_data_scene(C);
1701                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, sequence_cb, scene);
1702
1703                         break;
1704                 }
1705                 case TSE_GP_LAYER:
1706                 {
1707                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, gp_layer_cb, NULL);
1708                         WM_event_add_notifier(C, NC_GPENCIL | ND_DATA, NULL);
1709                         ED_undo_push(C, "Grease Pencil Layer operation");
1710
1711                         break;
1712                 }
1713                 case TSE_RNA_STRUCT:
1714                         if (event == OL_DOP_SELECT_LINKED) {
1715                                 outliner_do_data_operation(soops, datalevel, event, &soops->tree, data_select_linked_cb, C);
1716                         }
1717
1718                         break;
1719
1720                 default:
1721                         BKE_report(op->reports, RPT_WARNING, "Not yet implemented");
1722                         break;
1723         }
1724         
1725         return OPERATOR_FINISHED;
1726 }
1727
1728
1729 void OUTLINER_OT_data_operation(wmOperatorType *ot)
1730 {
1731         /* identifiers */
1732         ot->name = "Outliner Data Operation";
1733         ot->idname = "OUTLINER_OT_data_operation";
1734         ot->description = "";
1735         
1736         /* callbacks */
1737         ot->invoke = WM_menu_invoke;
1738         ot->exec = outliner_data_operation_exec;
1739         ot->poll = ED_operator_outliner_active;
1740         
1741         ot->flag = 0;
1742         
1743         ot->prop = RNA_def_enum(ot->srna, "type", prop_data_op_types, 0, "Data Operation", "");
1744 }
1745
1746
1747 /* ******************** */
1748
1749
1750 static int do_outliner_operation_event(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops,
1751                                        TreeElement *te, const wmEvent *event, const float mval[2])
1752 {
1753         ReportList *reports = CTX_wm_reports(C); // XXX...
1754         
1755         if (mval[1] > te->ys && mval[1] < te->ys + UI_UNIT_Y) {
1756                 int scenelevel = 0, objectlevel = 0, idlevel = 0, datalevel = 0;
1757                 TreeStoreElem *tselem = TREESTORE(te);
1758                 
1759                 /* select object that's clicked on and popup context menu */
1760                 if (!(tselem->flag & TSE_SELECTED)) {
1761                         
1762                         if (outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
1763                                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
1764                         
1765                         tselem->flag |= TSE_SELECTED;
1766                         /* redraw, same as outliner_select function */
1767                         soops->storeflag |= SO_TREESTORE_REDRAW;
1768                         ED_region_tag_redraw(ar);
1769                 }
1770                 
1771                 set_operation_types(soops, &soops->tree, &scenelevel, &objectlevel, &idlevel, &datalevel);
1772                 
1773                 if (scenelevel) {
1774                         if (objectlevel || datalevel || idlevel) {
1775                                 BKE_report(reports, RPT_WARNING, "Mixed selection");
1776                         }
1777                         else {
1778                                 WM_operator_name_call(C, "OUTLINER_OT_scene_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1779                         }
1780                 }
1781                 else if (objectlevel) {
1782                         WM_operator_name_call(C, "OUTLINER_OT_object_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1783                 }
1784                 else if (idlevel) {
1785                         if (idlevel == -1 || datalevel) {
1786                                 BKE_report(reports, RPT_WARNING, "Mixed selection");
1787                         }
1788                         else {
1789                                 switch (idlevel) {
1790                                         case ID_GR:
1791                                                 WM_operator_name_call(C, "OUTLINER_OT_group_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1792                                                 break;
1793                                         case ID_LI:
1794                                                 WM_operator_name_call(C, "OUTLINER_OT_lib_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1795                                                 break;
1796                                         default:
1797                                                 WM_operator_name_call(C, "OUTLINER_OT_id_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1798                                                 break;
1799                                 }
1800                         }
1801                 }
1802                 else if (datalevel) {
1803                         if (datalevel == -1) {
1804                                 BKE_report(reports, RPT_WARNING, "Mixed selection");
1805                         }
1806                         else {
1807                                 if (datalevel == TSE_ANIM_DATA)
1808                                         WM_operator_name_call(C, "OUTLINER_OT_animdata_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1809                                 else if (datalevel == TSE_DRIVER_BASE) {
1810                                         /* do nothing... no special ops needed yet */
1811                                 }
1812                                 else if (ELEM(datalevel, TSE_R_LAYER_BASE, TSE_R_LAYER, TSE_R_PASS)) {
1813                                         /*WM_operator_name_call(C, "OUTLINER_OT_renderdata_operation", WM_OP_INVOKE_REGION_WIN, NULL)*/
1814                                 }
1815                                 else if (datalevel == TSE_ID_BASE) {
1816                                         /* do nothing... there are no ops needed here yet */
1817                                 }
1818                                 else if (datalevel == TSE_CONSTRAINT) {
1819                                         WM_operator_name_call(C, "OUTLINER_OT_constraint_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1820                                 }
1821                                 else if (datalevel == TSE_MODIFIER) {
1822                                         WM_operator_name_call(C, "OUTLINER_OT_modifier_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1823                                 }
1824                                 else {
1825                                         WM_operator_name_call(C, "OUTLINER_OT_data_operation", WM_OP_INVOKE_REGION_WIN, NULL);
1826                                 }
1827                         }
1828                 }
1829                 
1830                 return 1;
1831         }
1832         
1833         for (te = te->subtree.first; te; te = te->next) {
1834                 if (do_outliner_operation_event(C, scene, ar, soops, te, event, mval))
1835                         return 1;
1836         }
1837         return 0;
1838 }
1839
1840
1841 static int outliner_operation(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
1842 {
1843         Scene *scene = CTX_data_scene(C);
1844         ARegion *ar = CTX_wm_region(C);
1845         SpaceOops *soops = CTX_wm_space_outliner(C);
1846         uiBut *but = UI_context_active_but_get(C);
1847         TreeElement *te;
1848         float fmval[2];
1849
1850         if (but) {
1851                 UI_but_tooltip_timer_remove(C, but);
1852         }
1853
1854         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1855         
1856         for (te = soops->tree.first; te; te = te->next) {
1857                 if (do_outliner_operation_event(C, scene, ar, soops, te, event, fmval)) {
1858                         break;
1859                 }
1860         }
1861         
1862         return OPERATOR_FINISHED;
1863 }
1864
1865 /* Menu only! Calls other operators */
1866 void OUTLINER_OT_operation(wmOperatorType *ot)
1867 {
1868         ot->name = "Execute Operation";
1869         ot->idname = "OUTLINER_OT_operation";
1870         ot->description = "Context menu for item operations";
1871         
1872         ot->invoke = outliner_operation;
1873         
1874         ot->poll = ED_operator_outliner_active;
1875 }
1876
1877 /* ****************************************************** */