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