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