code cleanup: favor braces when blocks have mixed brace use.
[blender.git] / source / blender / editors / space_outliner / outliner_edit.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_edit.c
29  *  \ingroup spoutliner
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_anim_types.h"
35 #include "DNA_group_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_material_types.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42
43 #include "BLF_translation.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_context.h"
47 #include "BKE_depsgraph.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_report.h"
51 #include "BKE_scene.h"
52 #include "BKE_material.h"
53
54 #include "ED_object.h"
55 #include "ED_screen.h"
56 #include "ED_keyframing.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63 #include "UI_view2d.h"
64
65 #include "RNA_access.h"
66 #include "RNA_define.h"
67
68 #include "outliner_intern.h"
69
70 /* ************************************************************** */
71 /* Unused Utilities */
72 // XXX: where to place these?
73
74 /* This is not used anywhere at the moment */
75 #if 0
76 /* return 1 when levels were opened */
77 static int outliner_open_back(SpaceOops *soops, TreeElement *te)
78 {
79         TreeStoreElem *tselem;
80         int retval = 0;
81         
82         for (te = te->parent; te; te = te->parent) {
83                 tselem = TREESTORE(te);
84                 if (tselem->flag & TSE_CLOSED) {
85                         tselem->flag &= ~TSE_CLOSED;
86                         retval = 1;
87                 }
88         }
89         return retval;
90 }
91
92 static void outliner_open_reveal(SpaceOops *soops, ListBase *lb, TreeElement *teFind, int *found)
93 {
94         TreeElement *te;
95         TreeStoreElem *tselem;
96         
97         for (te = lb->first; te; te = te->next) {
98                 /* check if this tree-element was the one we're seeking */
99                 if (te == teFind) {
100                         *found = 1;
101                         return;
102                 }
103                 
104                 /* try to see if sub-tree contains it then */
105                 outliner_open_reveal(soops, &te->subtree, teFind, found);
106                 if (*found) {
107                         tselem = TREESTORE(te);
108                         if (tselem->flag & TSE_CLOSED) 
109                                 tselem->flag &= ~TSE_CLOSED;
110                         return;
111                 }
112         }
113 }
114 #endif
115
116 /* ************************************************************** */
117 /* Click Activated */
118
119 /* Toggle Open/Closed ------------------------------------------- */
120
121 static int do_outliner_item_openclose(bContext *C, SpaceOops *soops, TreeElement *te, int all, const float mval[2])
122 {
123         
124         if (mval[1] > te->ys && mval[1] < te->ys + UI_UNIT_Y) {
125                 TreeStoreElem *tselem = TREESTORE(te);
126                 
127                 /* all below close/open? */
128                 if (all) {
129                         tselem->flag &= ~TSE_CLOSED;
130                         outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
131                 }
132                 else {
133                         if (tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
134                         else tselem->flag |= TSE_CLOSED;
135                 }
136                 
137                 return 1;
138         }
139         
140         for (te = te->subtree.first; te; te = te->next) {
141                 if (do_outliner_item_openclose(C, soops, te, all, mval)) 
142                         return 1;
143         }
144         return 0;
145         
146 }
147
148 /* event can enterkey, then it opens/closes */
149 static int outliner_item_openclose(bContext *C, wmOperator *op, wmEvent *event)
150 {
151         ARegion *ar = CTX_wm_region(C);
152         SpaceOops *soops = CTX_wm_space_outliner(C);
153         TreeElement *te;
154         float fmval[2];
155         int all = RNA_boolean_get(op->ptr, "all");
156         
157         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], fmval, fmval + 1);
158         
159         for (te = soops->tree.first; te; te = te->next) {
160                 if (do_outliner_item_openclose(C, soops, te, all, fmval)) 
161                         break;
162         }
163
164         ED_region_tag_redraw(ar);
165         
166         return OPERATOR_FINISHED;
167 }
168
169 void OUTLINER_OT_item_openclose(wmOperatorType *ot)
170 {
171         ot->name = "Open/Close Item";
172         ot->idname = "OUTLINER_OT_item_openclose";
173         ot->description = "Toggle whether item under cursor is enabled or closed";
174         
175         ot->invoke = outliner_item_openclose;
176         
177         ot->poll = ED_operator_outliner_active;
178         
179         RNA_def_boolean(ot->srna, "all", 1, "All", "Close or open all items");
180 }
181
182 /* Rename --------------------------------------------------- */
183
184 static void do_item_rename(ARegion *ar, TreeElement *te, TreeStoreElem *tselem, ReportList *reports)
185 {
186         /* can't rename rna datablocks entries or listbases */
187         if (ELEM4(tselem->type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM, TSE_ID_BASE)) {
188                 /* do nothing */;
189         }
190         else if (ELEM10(tselem->type, TSE_ANIM_DATA, TSE_NLA, TSE_DEFGROUP_BASE, TSE_CONSTRAINT_BASE, TSE_MODIFIER_BASE,
191                         TSE_SCRIPT_BASE, TSE_POSE_BASE, TSE_POSEGRP_BASE, TSE_R_LAYER_BASE, TSE_R_PASS))
192         {
193                 BKE_report(reports, RPT_WARNING, "Cannot edit builtin name");
194         }
195         else if (ELEM3(tselem->type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP)) {
196                 BKE_report(reports, RPT_WARNING, "Cannot edit sequence name");
197         }
198         else if (tselem->id->lib) {
199                 // XXX                                          error_libdata();
200         }
201         else if (te->idcode == ID_LI && te->parent) {
202                 BKE_report(reports, RPT_WARNING, "Cannot edit the path of an indirectly linked library");
203         }
204         else {
205                 tselem->flag |= TSE_TEXTBUT;
206                 ED_region_tag_redraw(ar);
207         }
208 }
209
210 void item_rename_cb(bContext *C, Scene *UNUSED(scene), TreeElement *te,
211                     TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
212 {
213         ARegion *ar = CTX_wm_region(C);
214         ReportList *reports = CTX_wm_reports(C); // XXX
215         do_item_rename(ar, te, tselem, reports);
216 }
217
218 static int do_outliner_item_rename(bContext *C, ARegion *ar, SpaceOops *soops, TreeElement *te, const float mval[2])
219 {       
220         ReportList *reports = CTX_wm_reports(C); // XXX
221         
222         if (mval[1] > te->ys && mval[1] < te->ys + UI_UNIT_Y) {
223                 TreeStoreElem *tselem = TREESTORE(te);
224                 
225                 /* name and first icon */
226                 if (mval[0] > te->xs + UI_UNIT_X && mval[0] < te->xend) {
227                         
228                         do_item_rename(ar, te, tselem, reports);
229                 }
230                 return 1;
231         }
232         
233         for (te = te->subtree.first; te; te = te->next) {
234                 if (do_outliner_item_rename(C, ar, soops, te, mval)) return 1;
235         }
236         return 0;
237 }
238
239 static int outliner_item_rename(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
240 {
241         ARegion *ar = CTX_wm_region(C);
242         SpaceOops *soops = CTX_wm_space_outliner(C);
243         TreeElement *te;
244         float fmval[2];
245         
246         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], fmval, fmval + 1);
247         
248         for (te = soops->tree.first; te; te = te->next) {
249                 if (do_outliner_item_rename(C, ar, soops, te, fmval)) break;
250         }
251         
252         return OPERATOR_FINISHED;
253 }
254
255
256 void OUTLINER_OT_item_rename(wmOperatorType *ot)
257 {
258         ot->name = "Rename Item";
259         ot->idname = "OUTLINER_OT_item_rename";
260         ot->description = "Rename item under cursor";
261         
262         ot->invoke = outliner_item_rename;
263         
264         ot->poll = ED_operator_outliner_active;
265 }
266
267 /* ************************************************************** */
268 /* Setting Toggling Operators */
269
270 /* =============================================== */
271 /* Toggling Utilities (Exported) */
272
273 /* Apply Settings ------------------------------- */
274
275 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
276 {
277         TreeElement *te;
278         int level = curlevel, lev;
279         
280         for (te = lb->first; te; te = te->next) {
281                 
282                 lev = outliner_count_levels(soops, &te->subtree, curlevel + 1);
283                 if (lev > level) level = lev;
284         }
285         return level;
286 }
287
288 int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
289 {
290         TreeElement *te;
291         TreeStoreElem *tselem;
292         int level;
293         
294         for (te = lb->first; te; te = te->next) {
295                 tselem = TREESTORE(te);
296                 if (tselem->flag & flag) return curlevel;
297                 
298                 level = outliner_has_one_flag(soops, &te->subtree, flag, curlevel + 1);
299                 if (level) return level;
300         }
301         return 0;
302 }
303
304 void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
305 {
306         TreeElement *te;
307         TreeStoreElem *tselem;
308         
309         for (te = lb->first; te; te = te->next) {
310                 tselem = TREESTORE(te);
311                 if (set == 0) tselem->flag &= ~flag;
312                 else tselem->flag |= flag;
313                 outliner_set_flag(soops, &te->subtree, flag, set);
314         }
315 }
316
317 /* Restriction Columns ------------------------------- */
318
319 /* same check needed for both object operation and restrict column button func
320  * return 0 when in edit mode (cannot restrict view or select)
321  * otherwise return 1 */
322 int common_restrict_check(bContext *C, Object *ob)
323 {
324         /* Don't allow hide an object in edit mode,
325          * check the bug #22153 and #21609, #23977
326          */
327         Object *obedit = CTX_data_edit_object(C);
328         if (obedit && obedit == ob) {
329                 /* found object is hidden, reset */
330                 if (ob->restrictflag & OB_RESTRICT_VIEW)
331                         ob->restrictflag &= ~OB_RESTRICT_VIEW;
332                 /* found object is unselectable, reset */
333                 if (ob->restrictflag & OB_RESTRICT_SELECT)
334                         ob->restrictflag &= ~OB_RESTRICT_SELECT;
335                 return 0;
336         }
337         
338         return 1;
339 }
340
341 /* =============================================== */
342 /* Restriction toggles */
343
344 /* Toggle Visibility ---------------------------------------- */
345
346 void object_toggle_visibility_cb(bContext *C, Scene *scene, TreeElement *te,
347                                  TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
348 {
349         Base *base = (Base *)te->directdata;
350         Object *ob = (Object *)tselem->id;
351         
352         /* add check for edit mode */
353         if (!common_restrict_check(C, ob)) return;
354         
355         if (base || (base = BKE_scene_base_find(scene, ob))) {
356                 if ((base->object->restrictflag ^= OB_RESTRICT_VIEW)) {
357                         ED_base_object_select(base, BA_DESELECT);
358                 }
359         }
360 }
361
362 void group_toggle_visibility_cb(bContext *UNUSED(C), Scene *scene, TreeElement *UNUSED(te),
363                                 TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
364 {
365         Group *group = (Group *)tselem->id;
366         restrictbutton_gr_restrict_flag(scene, group, OB_RESTRICT_VIEW);
367 }
368
369 static int outliner_toggle_visibility_exec(bContext *C, wmOperator *UNUSED(op))
370 {
371         Main *bmain = CTX_data_main(C);
372         SpaceOops *soops = CTX_wm_space_outliner(C);
373         Scene *scene = CTX_data_scene(C);
374         ARegion *ar = CTX_wm_region(C);
375         
376         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_visibility_cb);
377         
378         DAG_id_type_tag(bmain, ID_OB);
379         WM_event_add_notifier(C, NC_SCENE | ND_OB_VISIBLE, scene);
380         ED_region_tag_redraw(ar);
381         
382         return OPERATOR_FINISHED;
383 }
384
385 void OUTLINER_OT_visibility_toggle(wmOperatorType *ot)
386 {
387         /* identifiers */
388         ot->name = "Toggle Visibility";
389         ot->idname = "OUTLINER_OT_visibility_toggle";
390         ot->description = "Toggle the visibility of selected items";
391         
392         /* callbacks */
393         ot->exec = outliner_toggle_visibility_exec;
394         ot->poll = ED_operator_outliner_active_no_editobject;
395         
396         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
397 }
398
399 /* Toggle Selectability ---------------------------------------- */
400
401 void object_toggle_selectability_cb(bContext *UNUSED(C), Scene *scene, TreeElement *te,
402                                     TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
403 {
404         Base *base = (Base *)te->directdata;
405         
406         if (base == NULL) base = BKE_scene_base_find(scene, (Object *)tselem->id);
407         if (base) {
408                 base->object->restrictflag ^= OB_RESTRICT_SELECT;
409         }
410 }
411
412 void group_toggle_selectability_cb(bContext *UNUSED(C), Scene *scene, TreeElement *UNUSED(te),
413                                    TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
414 {
415         Group *group = (Group *)tselem->id;
416         restrictbutton_gr_restrict_flag(scene, group, OB_RESTRICT_SELECT);
417 }
418
419 static int outliner_toggle_selectability_exec(bContext *C, wmOperator *UNUSED(op))
420 {
421         SpaceOops *soops = CTX_wm_space_outliner(C);
422         Scene *scene = CTX_data_scene(C);
423         ARegion *ar = CTX_wm_region(C);
424         
425         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_selectability_cb);
426         
427         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
428         ED_region_tag_redraw(ar);
429         
430         return OPERATOR_FINISHED;
431 }
432
433 void OUTLINER_OT_selectability_toggle(wmOperatorType *ot)
434 {
435         /* identifiers */
436         ot->name = "Toggle Selectability";
437         ot->idname = "OUTLINER_OT_selectability_toggle";
438         ot->description = "Toggle the selectability";
439         
440         /* callbacks */
441         ot->exec = outliner_toggle_selectability_exec;
442         ot->poll = ED_operator_outliner_active_no_editobject;
443         
444         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
445 }
446
447 /* Toggle Renderability ---------------------------------------- */
448
449 void object_toggle_renderability_cb(bContext *UNUSED(C), Scene *scene, TreeElement *te,
450                                     TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
451 {
452         Base *base = (Base *)te->directdata;
453         
454         if (base == NULL) base = BKE_scene_base_find(scene, (Object *)tselem->id);
455         if (base) {
456                 base->object->restrictflag ^= OB_RESTRICT_RENDER;
457         }
458 }
459
460 void group_toggle_renderability_cb(bContext *UNUSED(C), Scene *scene, TreeElement *UNUSED(te),
461                                    TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
462 {
463         Group *group = (Group *)tselem->id;
464         restrictbutton_gr_restrict_flag(scene, group, OB_RESTRICT_RENDER);
465 }
466
467 static int outliner_toggle_renderability_exec(bContext *C, wmOperator *UNUSED(op))
468 {
469         Main *bmain = CTX_data_main(C);
470         SpaceOops *soops = CTX_wm_space_outliner(C);
471         Scene *scene = CTX_data_scene(C);
472         
473         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_renderability_cb);
474         
475         DAG_id_type_tag(bmain, ID_OB);
476         WM_event_add_notifier(C, NC_SCENE | ND_OB_RENDER, scene);
477         
478         return OPERATOR_FINISHED;
479 }
480
481 void OUTLINER_OT_renderability_toggle(wmOperatorType *ot)
482 {
483         /* identifiers */
484         ot->name = "Toggle Renderability";
485         ot->idname = "OUTLINER_OT_renderability_toggle";
486         ot->description = "Toggle the renderability of selected items";
487         
488         /* callbacks */
489         ot->exec = outliner_toggle_renderability_exec;
490         ot->poll = ED_operator_outliner_active;
491         
492         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
493 }
494
495 /* =============================================== */
496 /* Outliner setting toggles */
497
498 /* Toggle Expanded (Outliner) ---------------------------------------- */
499
500 static int outliner_toggle_expanded_exec(bContext *C, wmOperator *UNUSED(op))
501 {
502         SpaceOops *soops = CTX_wm_space_outliner(C);
503         ARegion *ar = CTX_wm_region(C);
504         
505         if (outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
506                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
507         else 
508                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
509         
510         ED_region_tag_redraw(ar);
511         
512         return OPERATOR_FINISHED;
513 }
514
515 void OUTLINER_OT_expanded_toggle(wmOperatorType *ot)
516 {
517         /* identifiers */
518         ot->name = "Expand/Collapse All";
519         ot->idname = "OUTLINER_OT_expanded_toggle";
520         ot->description = "Expand/Collapse all items";
521         
522         /* callbacks */
523         ot->exec = outliner_toggle_expanded_exec;
524         ot->poll = ED_operator_outliner_active;
525         
526         /* no undo or registry, UI option */
527 }
528
529 /* Toggle Selected (Outliner) ---------------------------------------- */
530
531 static int outliner_toggle_selected_exec(bContext *C, wmOperator *UNUSED(op))
532 {
533         SpaceOops *soops = CTX_wm_space_outliner(C);
534         ARegion *ar = CTX_wm_region(C);
535         Scene *scene = CTX_data_scene(C);
536         
537         if (outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
538                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
539         else 
540                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
541         
542         soops->storeflag |= SO_TREESTORE_REDRAW;
543         
544         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
545         ED_region_tag_redraw(ar);
546         
547         return OPERATOR_FINISHED;
548 }
549
550 void OUTLINER_OT_selected_toggle(wmOperatorType *ot)
551 {
552         /* identifiers */
553         ot->name = "Toggle Selected";
554         ot->idname = "OUTLINER_OT_selected_toggle";
555         ot->description = "Toggle the Outliner selection of items";
556         
557         /* callbacks */
558         ot->exec = outliner_toggle_selected_exec;
559         ot->poll = ED_operator_outliner_active;
560         
561         /* no undo or registry, UI option */
562 }
563
564 /* ************************************************************** */
565 /* Hotkey Only Operators */
566
567 /* Show Active --------------------------------------------------- */
568
569 static int outliner_show_active_exec(bContext *C, wmOperator *UNUSED(op))
570 {
571         SpaceOops *so = CTX_wm_space_outliner(C);
572         Scene *scene = CTX_data_scene(C);
573         ARegion *ar = CTX_wm_region(C);
574         View2D *v2d = &ar->v2d;
575         
576         TreeElement *te;
577         int xdelta, ytop;
578         
579         // TODO: make this get this info from context instead...
580         if (OBACT == NULL) 
581                 return OPERATOR_CANCELLED;
582         
583         te = outliner_find_id(so, &so->tree, (ID *)OBACT);
584         if (te) {
585                 /* make te->ys center of view */
586                 ytop = (int)(te->ys + BLI_rcti_size_y(&v2d->mask) / 2);
587                 if (ytop > 0) ytop = 0;
588                 
589                 v2d->cur.ymax = (float)ytop;
590                 v2d->cur.ymin = (float)(ytop - BLI_rcti_size_y(&v2d->mask));
591                 
592                 /* make te->xs ==> te->xend center of view */
593                 xdelta = (int)(te->xs - v2d->cur.xmin);
594                 v2d->cur.xmin += xdelta;
595                 v2d->cur.xmax += xdelta;
596                 
597                 so->storeflag |= SO_TREESTORE_REDRAW;
598         }
599         
600         ED_region_tag_redraw(ar);
601         
602         return OPERATOR_FINISHED;
603 }
604
605 void OUTLINER_OT_show_active(wmOperatorType *ot)
606 {
607         /* identifiers */
608         ot->name = "Show Active";
609         ot->idname = "OUTLINER_OT_show_active";
610         ot->description = "Adjust the view so that the active Object is shown centered";
611         
612         /* callbacks */
613         ot->exec = outliner_show_active_exec;
614         ot->poll = ED_operator_outliner_active;
615 }
616
617 /* View Panning --------------------------------------------------- */
618
619 static int outliner_scroll_page_exec(bContext *C, wmOperator *op)
620 {
621         ARegion *ar = CTX_wm_region(C);
622         int dy = BLI_rcti_size_y(&ar->v2d.mask);
623         int up = 0;
624         
625         if (RNA_boolean_get(op->ptr, "up"))
626                 up = 1;
627
628         if (up == 0) dy = -dy;
629         ar->v2d.cur.ymin += dy;
630         ar->v2d.cur.ymax += dy;
631         
632         ED_region_tag_redraw(ar);
633         
634         return OPERATOR_FINISHED;
635 }
636
637
638 void OUTLINER_OT_scroll_page(wmOperatorType *ot)
639 {
640         /* identifiers */
641         ot->name = "Scroll Page";
642         ot->idname = "OUTLINER_OT_scroll_page";
643         ot->description = "Scroll page up or down";
644         
645         /* callbacks */
646         ot->exec = outliner_scroll_page_exec;
647         ot->poll = ED_operator_outliner_active;
648         
649         /* properties */
650         RNA_def_boolean(ot->srna, "up", 0, "Up", "Scroll up one page");
651 }
652
653 /* Search ------------------------------------------------------- */
654 // TODO: probably obsolete now with filtering?
655
656 #if 0
657
658 /* recursive helper for function below */
659 static void outliner_set_coordinates_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
660 {
661         TreeStoreElem *tselem = TREESTORE(te);
662         
663         /* store coord and continue, we need coordinates for elements outside view too */
664         te->xs = (float)startx;
665         te->ys = (float)(*starty);
666         *starty -= UI_UNIT_Y;
667         
668         if (TSELEM_OPEN(tselem, soops)) {
669                 TreeElement *ten;
670                 for (ten = te->subtree.first; ten; ten = ten->next) {
671                         outliner_set_coordinates_element(soops, ten, startx + UI_UNIT_X, starty);
672                 }
673         }
674         
675 }
676
677 /* to retrieve coordinates with redrawing the entire tree */
678 static void outliner_set_coordinates(ARegion *ar, SpaceOops *soops)
679 {
680         TreeElement *te;
681         int starty = (int)(ar->v2d.tot.ymax) - UI_UNIT_Y;
682         int startx = 0;
683         
684         for (te = soops->tree.first; te; te = te->next) {
685                 outliner_set_coordinates_element(soops, te, startx, &starty);
686         }
687 }
688
689 /* find next element that has this name */
690 static TreeElement *outliner_find_name(SpaceOops *soops, ListBase *lb, char *name, int flags,
691                                        TreeElement *prev, int *prevFound)
692 {
693         TreeElement *te, *tes;
694         
695         for (te = lb->first; te; te = te->next) {
696                 int found = outliner_filter_has_name(te, name, flags);
697                 
698                 if (found) {
699                         /* name is right, but is element the previous one? */
700                         if (prev) {
701                                 if ((te != prev) && (*prevFound)) 
702                                         return te;
703                                 if (te == prev) {
704                                         *prevFound = 1;
705                                 }
706                         }
707                         else
708                                 return te;
709                 }
710                 
711                 tes = outliner_find_name(soops, &te->subtree, name, flags, prev, prevFound);
712                 if (tes) return tes;
713         }
714
715         /* nothing valid found */
716         return NULL;
717 }
718
719 static void outliner_find_panel(Scene *UNUSED(scene), ARegion *ar, SpaceOops *soops, int again, int flags) 
720 {
721         ReportList *reports = NULL; // CTX_wm_reports(C);
722         TreeElement *te = NULL;
723         TreeElement *last_find;
724         TreeStoreElem *tselem;
725         int ytop, xdelta, prevFound = 0;
726         char name[sizeof(soops->search_string)];
727         
728         /* get last found tree-element based on stored search_tse */
729         last_find = outliner_find_tse(soops, &soops->search_tse);
730         
731         /* determine which type of search to do */
732         if (again && last_find) {
733                 /* no popup panel - previous + user wanted to search for next after previous */
734                 BLI_strncpy(name, soops->search_string, sizeof(name));
735                 flags = soops->search_flags;
736                 
737                 /* try to find matching element */
738                 te = outliner_find_name(soops, &soops->tree, name, flags, last_find, &prevFound);
739                 if (te == NULL) {
740                         /* no more matches after previous, start from beginning again */
741                         prevFound = 1;
742                         te = outliner_find_name(soops, &soops->tree, name, flags, last_find, &prevFound);
743                 }
744         }
745         else {
746                 /* pop up panel - no previous, or user didn't want search after previous */
747                 name[0] = '\0';
748 // XXX          if (sbutton(name, 0, sizeof(name)-1, "Find: ") && name[0]) {
749 //                      te = outliner_find_name(soops, &soops->tree, name, flags, NULL, &prevFound);
750 //              }
751 //              else return; /* XXX RETURN! XXX */
752         }
753
754         /* do selection and reveal */
755         if (te) {
756                 tselem = TREESTORE(te);
757                 if (tselem) {
758                         /* expand branches so that it will be visible, we need to get correct coordinates */
759                         if (outliner_open_back(soops, te))
760                                 outliner_set_coordinates(ar, soops);
761                         
762                         /* deselect all visible, and select found element */
763                         outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
764                         tselem->flag |= TSE_SELECTED;
765                         
766                         /* make te->ys center of view */
767                         ytop = (int)(te->ys + BLI_rctf_size_y(&ar->v2d.mask) / 2);
768                         if (ytop > 0) ytop = 0;
769                         ar->v2d.cur.ymax = (float)ytop;
770                         ar->v2d.cur.ymin = (float)(ytop - BLI_rctf_size_y(&ar->v2d.mask));
771                         
772                         /* make te->xs ==> te->xend center of view */
773                         xdelta = (int)(te->xs - ar->v2d.cur.xmin);
774                         ar->v2d.cur.xmin += xdelta;
775                         ar->v2d.cur.xmax += xdelta;
776                         
777                         /* store selection */
778                         soops->search_tse = *tselem;
779                         
780                         BLI_strncpy(soops->search_string, name, sizeof(soops->search_string));
781                         soops->search_flags = flags;
782                         
783                         /* redraw */
784                         soops->storeflag |= SO_TREESTORE_REDRAW;
785                 }
786         }
787         else {
788                 /* no tree-element found */
789                 BKE_reportf(reports, RPT_WARNING, "Not found: %s", name);
790         }
791 }
792 #endif
793
794 /* Show One Level ----------------------------------------------- */
795
796 /* helper function for Show/Hide one level operator */
797 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
798 {
799         TreeElement *te;
800         TreeStoreElem *tselem;
801         
802         for (te = lb->first; te; te = te->next) {
803                 tselem = TREESTORE(te);
804                 
805                 if (open) {
806                         if (curlevel <= level) tselem->flag &= ~TSE_CLOSED;
807                 }
808                 else {
809                         if (curlevel >= level) tselem->flag |= TSE_CLOSED;
810                 }
811                 
812                 outliner_openclose_level(soops, &te->subtree, curlevel + 1, level, open);
813         }
814 }
815
816 static int outliner_one_level_exec(bContext *C, wmOperator *op)
817 {
818         SpaceOops *soops = CTX_wm_space_outliner(C);
819         ARegion *ar = CTX_wm_region(C);
820         int add = RNA_boolean_get(op->ptr, "open");
821         int level;
822         
823         level = outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
824         if (add == 1) {
825                 if (level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
826         }
827         else {
828                 if (level == 0) level = outliner_count_levels(soops, &soops->tree, 0);
829                 if (level) outliner_openclose_level(soops, &soops->tree, 1, level - 1, 0);
830         }
831         
832         ED_region_tag_redraw(ar);
833         
834         return OPERATOR_FINISHED;
835 }
836
837 void OUTLINER_OT_show_one_level(wmOperatorType *ot)
838 {
839         PropertyRNA *prop;
840
841         /* identifiers */
842         ot->name = "Show/Hide One Level";
843         ot->idname = "OUTLINER_OT_show_one_level";
844         ot->description = "Expand/collapse all entries by one level";
845         
846         /* callbacks */
847         ot->exec = outliner_one_level_exec;
848         ot->poll = ED_operator_outliner_active;
849         
850         /* no undo or registry, UI option */
851         
852         /* properties */
853         prop = RNA_def_boolean(ot->srna, "open", 1, "Open", "Expand all entries one level deep");
854         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
855 }
856
857 /* Show Hierarchy ----------------------------------------------- */
858
859 /* helper function for tree_element_shwo_hierarchy() - recursively checks whether subtrees have any objects*/
860 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
861 {
862         TreeElement *te;
863         TreeStoreElem *tselem;
864         
865         for (te = lb->first; te; te = te->next) {
866                 tselem = TREESTORE(te);
867                 if (tselem->type == 0 && te->idcode == ID_OB) return 1;
868                 if (subtree_has_objects(soops, &te->subtree)) return 1;
869         }
870         return 0;
871 }
872
873 /* recursive helper function for Show Hierarchy operator */
874 static void tree_element_show_hierarchy(Scene *scene, SpaceOops *soops, ListBase *lb)
875 {
876         TreeElement *te;
877         TreeStoreElem *tselem;
878
879         /* open all object elems, close others */
880         for (te = lb->first; te; te = te->next) {
881                 tselem = TREESTORE(te);
882                 
883                 if (tselem->type == 0) {
884                         if (te->idcode == ID_SCE) {
885                                 if (tselem->id != (ID *)scene) tselem->flag |= TSE_CLOSED;
886                                 else tselem->flag &= ~TSE_CLOSED;
887                         }
888                         else if (te->idcode == ID_OB) {
889                                 if (subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
890                                 else tselem->flag |= TSE_CLOSED;
891                         }
892                 }
893                 else {
894                         tselem->flag |= TSE_CLOSED;
895                 }
896
897                 if (TSELEM_OPEN(tselem, soops)) {
898                         tree_element_show_hierarchy(scene, soops, &te->subtree);
899                 }
900         }
901 }
902
903 /* show entire object level hierarchy */
904 static int outliner_show_hierarchy_exec(bContext *C, wmOperator *UNUSED(op))
905 {
906         SpaceOops *soops = CTX_wm_space_outliner(C);
907         ARegion *ar = CTX_wm_region(C);
908         Scene *scene = CTX_data_scene(C);
909         
910         /* recursively open/close levels */
911         tree_element_show_hierarchy(scene, soops, &soops->tree);
912         
913         ED_region_tag_redraw(ar);
914         
915         return OPERATOR_FINISHED;
916 }
917
918 void OUTLINER_OT_show_hierarchy(wmOperatorType *ot)
919 {
920         /* identifiers */
921         ot->name = "Show Hierarchy";
922         ot->idname = "OUTLINER_OT_show_hierarchy";
923         ot->description = "Open all object entries and close all others";
924         
925         /* callbacks */
926         ot->exec = outliner_show_hierarchy_exec;
927         ot->poll = ED_operator_outliner_active; //  TODO: shouldn't be allowed in RNA views...
928         
929         /* no undo or registry, UI option */
930 }
931
932 /* ************************************************************** */
933 /* ANIMATO OPERATIONS */
934 /* KeyingSet and Driver Creation - Helper functions */
935
936 /* specialized poll callback for these operators to work in Datablocks view only */
937 static int ed_operator_outliner_datablocks_active(bContext *C)
938 {
939         ScrArea *sa = CTX_wm_area(C);
940         if ((sa) && (sa->spacetype == SPACE_OUTLINER)) {
941                 SpaceOops *so = CTX_wm_space_outliner(C);
942                 return (so->outlinevis == SO_DATABLOCKS);
943         }
944         return 0;
945 }
946
947
948 /* Helper func to extract an RNA path from selected tree element 
949  * NOTE: the caller must zero-out all values of the pointers that it passes here first, as
950  * this function does not do that yet 
951  */
952 static void tree_element_to_path(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, 
953                                  ID **id, char **path, int *array_index, short *flag, short *UNUSED(groupmode))
954 {
955         ListBase hierarchy = {NULL, NULL};
956         LinkData *ld;
957         TreeElement *tem, *temnext, *temsub;
958         TreeStoreElem *tse /* , *tsenext */ /* UNUSED */;
959         PointerRNA *ptr, *nextptr;
960         PropertyRNA *prop;
961         char *newpath = NULL;
962         
963         /* optimize tricks:
964          *      - Don't do anything if the selected item is a 'struct', but arrays are allowed
965          */
966         if (tselem->type == TSE_RNA_STRUCT)
967                 return;
968         
969         /* Overview of Algorithm:
970          *  1. Go up the chain of parents until we find the 'root', taking note of the
971          *         levels encountered in reverse-order (i.e. items are added to the start of the list
972          *      for more convenient looping later)
973          *  2. Walk down the chain, adding from the first ID encountered
974          *         (which will become the 'ID' for the KeyingSet Path), and build a  
975          *      path as we step through the chain
976          */
977          
978         /* step 1: flatten out hierarchy of parents into a flat chain */
979         for (tem = te->parent; tem; tem = tem->parent) {
980                 ld = MEM_callocN(sizeof(LinkData), "LinkData for tree_element_to_path()");
981                 ld->data = tem;
982                 BLI_addhead(&hierarchy, ld);
983         }
984         
985         /* step 2: step down hierarchy building the path
986          * (NOTE: addhead in previous loop was needed so that we can loop like this) */
987         for (ld = hierarchy.first; ld; ld = ld->next) {
988                 /* get data */
989                 tem = (TreeElement *)ld->data;
990                 tse = TREESTORE(tem);
991                 ptr = &tem->rnaptr;
992                 prop = tem->directdata;
993                 
994                 /* check if we're looking for first ID, or appending to path */
995                 if (*id) {
996                         /* just 'append' property to path 
997                          * - to prevent memory leaks, we must write to newpath not path, then free old path + swap them
998                          */
999                         if (tse->type == TSE_RNA_PROPERTY) {
1000                                 if (RNA_property_type(prop) == PROP_POINTER) {
1001                                         /* for pointer we just append property name */
1002                                         newpath = RNA_path_append(*path, ptr, prop, 0, NULL);
1003                                 }
1004                                 else if (RNA_property_type(prop) == PROP_COLLECTION) {
1005                                         char buf[128], *name;
1006                                         
1007                                         temnext = (TreeElement *)(ld->next->data);
1008                                         /* tsenext = TREESTORE(temnext); */ /* UNUSED */
1009                                         
1010                                         nextptr = &temnext->rnaptr;
1011                                         name = RNA_struct_name_get_alloc(nextptr, buf, sizeof(buf), NULL);
1012                                         
1013                                         if (name) {
1014                                                 /* if possible, use name as a key in the path */
1015                                                 newpath = RNA_path_append(*path, NULL, prop, 0, name);
1016                                                 
1017                                                 if (name != buf)
1018                                                         MEM_freeN(name);
1019                                         }
1020                                         else {
1021                                                 /* otherwise use index */
1022                                                 int index = 0;
1023                                                 
1024                                                 for (temsub = tem->subtree.first; temsub; temsub = temsub->next, index++)
1025                                                         if (temsub == temnext)
1026                                                                 break;
1027                                                 
1028                                                 newpath = RNA_path_append(*path, NULL, prop, index, NULL);
1029                                         }
1030                                         
1031                                         ld = ld->next;
1032                                 }
1033                         }
1034                         
1035                         if (newpath) {
1036                                 if (*path) MEM_freeN(*path);
1037                                 *path = newpath;
1038                                 newpath = NULL;
1039                         }
1040                 }
1041                 else {
1042                         /* no ID, so check if entry is RNA-struct, and if that RNA-struct is an ID datablock to extract info from */
1043                         if (tse->type == TSE_RNA_STRUCT) {
1044                                 /* ptr->data not ptr->id.data seems to be the one we want,
1045                                  * since ptr->data is sometimes the owner of this ID? */
1046                                 if (RNA_struct_is_ID(ptr->type)) {
1047                                         *id = (ID *)ptr->data;
1048                                         
1049                                         /* clear path */
1050                                         if (*path) {
1051                                                 MEM_freeN(*path);
1052                                                 path = NULL;
1053                                         }
1054                                 }
1055                         }
1056                 }
1057         }
1058
1059         /* step 3: if we've got an ID, add the current item to the path */
1060         if (*id) {
1061                 /* add the active property to the path */
1062                 ptr = &te->rnaptr;
1063                 prop = te->directdata;
1064                 
1065                 /* array checks */
1066                 if (tselem->type == TSE_RNA_ARRAY_ELEM) {
1067                         /* item is part of an array, so must set the array_index */
1068                         *array_index = te->index;
1069                 }
1070                 else if (RNA_property_array_length(ptr, prop)) {
1071                         /* entire array was selected, so keyframe all */
1072                         *flag |= KSP_FLAG_WHOLE_ARRAY;
1073                 }
1074                 
1075                 /* path */
1076                 newpath = RNA_path_append(*path, NULL, prop, 0, NULL);
1077                 if (*path) MEM_freeN(*path);
1078                 *path = newpath;
1079         }
1080
1081         /* free temp data */
1082         BLI_freelistN(&hierarchy);
1083 }
1084
1085 /* =============================================== */
1086 /* Driver Operations */
1087
1088 /* These operators are only available in databrowser mode for now, as
1089  * they depend on having RNA paths and/or hierarchies available.
1090  */
1091 enum {
1092         DRIVERS_EDITMODE_ADD    = 0,
1093         DRIVERS_EDITMODE_REMOVE,
1094 } /*eDrivers_EditModes*/;
1095
1096 /* Utilities ---------------------------------- */ 
1097
1098 /* Recursively iterate over tree, finding and working on selected items */
1099 static void do_outliner_drivers_editop(SpaceOops *soops, ListBase *tree, ReportList *reports, short mode)
1100 {
1101         TreeElement *te;
1102         TreeStoreElem *tselem;
1103         
1104         for (te = tree->first; te; te = te->next) {
1105                 tselem = TREESTORE(te);
1106                 
1107                 /* if item is selected, perform operation */
1108                 if (tselem->flag & TSE_SELECTED) {
1109                         ID *id = NULL;
1110                         char *path = NULL;
1111                         int array_index = 0;
1112                         short flag = 0;
1113                         short groupmode = KSP_GROUP_KSNAME;
1114                         
1115                         /* check if RNA-property described by this selected element is an animatable prop */
1116                         if (ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM) &&
1117                             RNA_property_animateable(&te->rnaptr, te->directdata))
1118                         {
1119                                 /* get id + path + index info from the selected element */
1120                                 tree_element_to_path(soops, te, tselem, 
1121                                                      &id, &path, &array_index, &flag, &groupmode);
1122                         }
1123                         
1124                         /* only if ID and path were set, should we perform any actions */
1125                         if (id && path) {
1126                                 short dflags = CREATEDRIVER_WITH_DEFAULT_DVAR;
1127                                 int arraylen = 1;
1128                                 
1129                                 /* array checks */
1130                                 if (flag & KSP_FLAG_WHOLE_ARRAY) {
1131                                         /* entire array was selected, so add drivers for all */
1132                                         arraylen = RNA_property_array_length(&te->rnaptr, te->directdata);
1133                                 }
1134                                 else
1135                                         arraylen = array_index;
1136                                 
1137                                 /* we should do at least one step */
1138                                 if (arraylen == array_index)
1139                                         arraylen++;
1140                                 
1141                                 /* for each array element we should affect, add driver */
1142                                 for (; array_index < arraylen; array_index++) {
1143                                         /* action depends on mode */
1144                                         switch (mode) {
1145                                                 case DRIVERS_EDITMODE_ADD:
1146                                                 {
1147                                                         /* add a new driver with the information obtained (only if valid) */
1148                                                         ANIM_add_driver(reports, id, path, array_index, dflags, DRIVER_TYPE_PYTHON);
1149                                                 }
1150                                                 break;
1151                                                 case DRIVERS_EDITMODE_REMOVE:
1152                                                 {
1153                                                         /* remove driver matching the information obtained (only if valid) */
1154                                                         ANIM_remove_driver(reports, id, path, array_index, dflags);
1155                                                 }
1156                                                 break;
1157                                         }
1158                                 }
1159                                 
1160                                 /* free path, since it had to be generated */
1161                                 MEM_freeN(path);
1162                         }
1163                         
1164                         
1165                 }
1166                 
1167                 /* go over sub-tree */
1168                 if (TSELEM_OPEN(tselem, soops))
1169                         do_outliner_drivers_editop(soops, &te->subtree, reports, mode);
1170         }
1171 }
1172
1173 /* Add Operator ---------------------------------- */
1174
1175 static int outliner_drivers_addsel_exec(bContext *C, wmOperator *op)
1176 {
1177         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1178         
1179         /* check for invalid states */
1180         if (soutliner == NULL)
1181                 return OPERATOR_CANCELLED;
1182         
1183         /* recursively go into tree, adding selected items */
1184         do_outliner_drivers_editop(soutliner, &soutliner->tree, op->reports, DRIVERS_EDITMODE_ADD);
1185         
1186         /* send notifiers */
1187         WM_event_add_notifier(C, NC_ANIMATION | ND_FCURVES_ORDER, NULL); // XXX
1188         
1189         return OPERATOR_FINISHED;
1190 }
1191
1192 void OUTLINER_OT_drivers_add_selected(wmOperatorType *ot)
1193 {
1194         /* api callbacks */
1195         ot->idname = "OUTLINER_OT_drivers_add_selected";
1196         ot->name = "Add Drivers for Selected";
1197         ot->description = "Add drivers to selected items";
1198         
1199         /* api callbacks */
1200         ot->exec = outliner_drivers_addsel_exec;
1201         ot->poll = ed_operator_outliner_datablocks_active;
1202         
1203         /* flags */
1204         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1205 }
1206
1207
1208 /* Remove Operator ---------------------------------- */
1209
1210 static int outliner_drivers_deletesel_exec(bContext *C, wmOperator *op)
1211 {
1212         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1213         
1214         /* check for invalid states */
1215         if (soutliner == NULL)
1216                 return OPERATOR_CANCELLED;
1217         
1218         /* recursively go into tree, adding selected items */
1219         do_outliner_drivers_editop(soutliner, &soutliner->tree, op->reports, DRIVERS_EDITMODE_REMOVE);
1220         
1221         /* send notifiers */
1222         WM_event_add_notifier(C, ND_KEYS, NULL); // XXX
1223         
1224         return OPERATOR_FINISHED;
1225 }
1226
1227 void OUTLINER_OT_drivers_delete_selected(wmOperatorType *ot)
1228 {
1229         /* identifiers */
1230         ot->idname = "OUTLINER_OT_drivers_delete_selected";
1231         ot->name = "Delete Drivers for Selected";
1232         ot->description = "Delete drivers assigned to selected items";
1233         
1234         /* api callbacks */
1235         ot->exec = outliner_drivers_deletesel_exec;
1236         ot->poll = ed_operator_outliner_datablocks_active;
1237         
1238         /* flags */
1239         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1240 }
1241
1242 /* =============================================== */
1243 /* Keying Set Operations */
1244
1245 /* These operators are only available in databrowser mode for now, as
1246  * they depend on having RNA paths and/or hierarchies available.
1247  */
1248 enum {
1249         KEYINGSET_EDITMODE_ADD  = 0,
1250         KEYINGSET_EDITMODE_REMOVE,
1251 } /*eKeyingSet_EditModes*/;
1252
1253 /* Utilities ---------------------------------- */ 
1254  
1255 /* find the 'active' KeyingSet, and add if not found (if adding is allowed) */
1256 // TODO: should this be an API func?
1257 static KeyingSet *verify_active_keyingset(Scene *scene, short add)
1258 {
1259         KeyingSet *ks = NULL;
1260         
1261         /* sanity check */
1262         if (scene == NULL)
1263                 return NULL;
1264         
1265         /* try to find one from scene */
1266         if (scene->active_keyingset > 0)
1267                 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
1268                 
1269         /* add if none found */
1270         // XXX the default settings have yet to evolve
1271         if ((add) && (ks == NULL)) {
1272                 ks = BKE_keyingset_add(&scene->keyingsets, NULL, NULL, KEYINGSET_ABSOLUTE, 0);
1273                 scene->active_keyingset = BLI_countlist(&scene->keyingsets);
1274         }
1275         
1276         return ks;
1277 }
1278
1279 /* Recursively iterate over tree, finding and working on selected items */
1280 static void do_outliner_keyingset_editop(SpaceOops *soops, KeyingSet *ks, ListBase *tree, short mode)
1281 {
1282         TreeElement *te;
1283         TreeStoreElem *tselem;
1284         
1285         for (te = tree->first; te; te = te->next) {
1286                 tselem = TREESTORE(te);
1287                 
1288                 /* if item is selected, perform operation */
1289                 if (tselem->flag & TSE_SELECTED) {
1290                         ID *id = NULL;
1291                         char *path = NULL;
1292                         int array_index = 0;
1293                         short flag = 0;
1294                         short groupmode = KSP_GROUP_KSNAME;
1295                         
1296                         /* check if RNA-property described by this selected element is an animatable prop */
1297                         if (ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM) &&
1298                             RNA_property_animateable(&te->rnaptr, te->directdata))
1299                         {
1300                                 /* get id + path + index info from the selected element */
1301                                 tree_element_to_path(soops, te, tselem, 
1302                                                      &id, &path, &array_index, &flag, &groupmode);
1303                         }
1304                         
1305                         /* only if ID and path were set, should we perform any actions */
1306                         if (id && path) {
1307                                 /* action depends on mode */
1308                                 switch (mode) {
1309                                         case KEYINGSET_EDITMODE_ADD:
1310                                         {
1311                                                 /* add a new path with the information obtained (only if valid) */
1312                                                 /* TODO: what do we do with group name?
1313                                                  * for now, we don't supply one, and just let this use the KeyingSet name */
1314                                                 BKE_keyingset_add_path(ks, id, NULL, path, array_index, flag, groupmode);
1315                                                 ks->active_path = BLI_countlist(&ks->paths);
1316                                                 break;
1317                                         }
1318                                         case KEYINGSET_EDITMODE_REMOVE:
1319                                         {
1320                                                 /* find the relevant path, then remove it from the KeyingSet */
1321                                                 KS_Path *ksp = BKE_keyingset_find_path(ks, id, NULL, path, array_index, groupmode);
1322                                                 
1323                                                 if (ksp) {
1324                                                         /* free path's data */
1325                                                         BKE_keyingset_free_path(ks, ksp);
1326
1327                                                         ks->active_path = 0;
1328                                                 }
1329                                                 break;
1330                                         }
1331                                 }
1332                                 
1333                                 /* free path, since it had to be generated */
1334                                 MEM_freeN(path);
1335                         }
1336                 }
1337                 
1338                 /* go over sub-tree */
1339                 if (TSELEM_OPEN(tselem, soops))
1340                         do_outliner_keyingset_editop(soops, ks, &te->subtree, mode);
1341         }
1342 }
1343
1344 /* Add Operator ---------------------------------- */
1345
1346 static int outliner_keyingset_additems_exec(bContext *C, wmOperator *op)
1347 {
1348         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1349         Scene *scene = CTX_data_scene(C);
1350         KeyingSet *ks = verify_active_keyingset(scene, 1);
1351         
1352         /* check for invalid states */
1353         if (ks == NULL) {
1354                 BKE_report(op->reports, RPT_ERROR, "Operation requires an active keying set");
1355                 return OPERATOR_CANCELLED;
1356         }
1357         if (soutliner == NULL)
1358                 return OPERATOR_CANCELLED;
1359         
1360         /* recursively go into tree, adding selected items */
1361         do_outliner_keyingset_editop(soutliner, ks, &soutliner->tree, KEYINGSET_EDITMODE_ADD);
1362         
1363         /* send notifiers */
1364         WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
1365         
1366         return OPERATOR_FINISHED;
1367 }
1368
1369 void OUTLINER_OT_keyingset_add_selected(wmOperatorType *ot)
1370 {
1371         /* identifiers */
1372         ot->idname = "OUTLINER_OT_keyingset_add_selected";
1373         ot->name = "Keying Set Add Selected";
1374         ot->description = "Add selected items (blue-gray rows) to active Keying Set";
1375         
1376         /* api callbacks */
1377         ot->exec = outliner_keyingset_additems_exec;
1378         ot->poll = ed_operator_outliner_datablocks_active;
1379         
1380         /* flags */
1381         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1382 }
1383
1384
1385 /* Remove Operator ---------------------------------- */
1386
1387 static int outliner_keyingset_removeitems_exec(bContext *C, wmOperator *UNUSED(op))
1388 {
1389         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1390         Scene *scene = CTX_data_scene(C);
1391         KeyingSet *ks = verify_active_keyingset(scene, 1);
1392         
1393         /* check for invalid states */
1394         if (soutliner == NULL)
1395                 return OPERATOR_CANCELLED;
1396         
1397         /* recursively go into tree, adding selected items */
1398         do_outliner_keyingset_editop(soutliner, ks, &soutliner->tree, KEYINGSET_EDITMODE_REMOVE);
1399         
1400         /* send notifiers */
1401         WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
1402         
1403         return OPERATOR_FINISHED;
1404 }
1405
1406 void OUTLINER_OT_keyingset_remove_selected(wmOperatorType *ot)
1407 {
1408         /* identifiers */
1409         ot->idname = "OUTLINER_OT_keyingset_remove_selected";
1410         ot->name = "Keying Set Remove Selected";
1411         ot->description = "Remove selected items (blue-gray rows) from active Keying Set";
1412         
1413         /* api callbacks */
1414         ot->exec = outliner_keyingset_removeitems_exec;
1415         ot->poll = ed_operator_outliner_datablocks_active;
1416         
1417         /* flags */
1418         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1419 }
1420
1421 /* ******************** Parent Drop Operator *********************** */
1422
1423 static int parent_drop_exec(bContext *C, wmOperator *op)
1424 {
1425         Object *par = NULL, *ob = NULL;
1426         Main *bmain = CTX_data_main(C);
1427         Scene *scene = CTX_data_scene(C);
1428         int partype = -1;
1429         char parname[MAX_ID_NAME], childname[MAX_ID_NAME];
1430
1431         partype = RNA_enum_get(op->ptr, "type");
1432         RNA_string_get(op->ptr, "parent", parname);
1433         par = (Object *)BKE_libblock_find_name(ID_OB, parname);
1434         RNA_string_get(op->ptr, "child", childname);
1435         ob = (Object *)BKE_libblock_find_name(ID_OB, childname);
1436
1437         ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, FALSE, FALSE);
1438
1439         DAG_relations_tag_update(bmain);
1440         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1441         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
1442
1443         return OPERATOR_FINISHED;
1444 }
1445
1446 /* Used for drag and drop parenting */
1447 TreeElement *outliner_dropzone_parent(bContext *C, wmEvent *event, TreeElement *te, float *fmval)
1448 {
1449         SpaceOops *soops = CTX_wm_space_outliner(C);
1450         TreeStoreElem *tselem = TREESTORE(te);
1451
1452         if ((fmval[1] > te->ys) && (fmval[1] < (te->ys + UI_UNIT_Y))) {
1453                 /* name and first icon */
1454                 if ((fmval[0] > te->xs + UI_UNIT_X) && (fmval[0] < te->xend)) {
1455                         /* always makes active object */
1456                         if (te->idcode == ID_OB && tselem->type == 0) {
1457                                 return te;
1458                         }
1459                         else {
1460                                 return NULL;
1461                         }
1462                 }
1463         }
1464
1465         /* Not it.  Let's look at its children. */
1466         if ((tselem->flag & TSE_CLOSED) == 0 && (te->subtree.first)) {
1467                 for (te = te->subtree.first; te; te = te->next) {
1468                         TreeElement *te_valid;
1469                         te_valid = outliner_dropzone_parent(C, event, te, fmval);
1470                         if (te_valid) return te_valid;
1471                 }
1472         }
1473         return NULL;
1474 }
1475
1476 static int parent_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1477 {
1478         Object *par = NULL;
1479         Object *ob = NULL;
1480         SpaceOops *soops = CTX_wm_space_outliner(C);
1481         ARegion *ar = CTX_wm_region(C);
1482         Main *bmain = CTX_data_main(C);
1483         Scene *scene = NULL;
1484         TreeElement *te = NULL;
1485         TreeElement *te_found = NULL;
1486         char childname[MAX_ID_NAME];
1487         char parname[MAX_ID_NAME];
1488         int partype = 0;
1489         float fmval[2];
1490
1491         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1492
1493         /* Find object hovered over */
1494         for (te = soops->tree.first; te; te = te->next) {
1495                 te_found = outliner_dropzone_parent(C, event, te, fmval);
1496                 if (te_found) break;
1497         }
1498
1499         if (te_found) {
1500                 RNA_string_set(op->ptr, "parent", te_found->name);
1501                 /* Identify parent and child */
1502                 RNA_string_get(op->ptr, "child", childname);
1503                 ob = (Object *)BKE_libblock_find_name(ID_OB, childname);
1504                 RNA_string_get(op->ptr, "parent", parname);
1505                 par = (Object *)BKE_libblock_find_name(ID_OB, parname);
1506                 
1507                 if (ELEM(NULL, ob, par)) {
1508                         if (par == NULL) printf("par==NULL\n");
1509                         return OPERATOR_CANCELLED;
1510                 }
1511                 if (ob == par) {
1512                         return OPERATOR_CANCELLED;
1513                 }
1514                 
1515                 scene = (Scene *)outliner_search_back(soops, te_found, ID_SCE);
1516
1517                 if (scene == NULL) {
1518                         /* currently outlier organized in a way, that if there's no parent scene
1519                          * element for object it means that all displayed objects belong to
1520                          * active scene and parenting them is allowed (sergey)
1521                          */
1522
1523                         scene = CTX_data_scene(C);
1524                 }
1525
1526                 if ((par->type != OB_ARMATURE) && (par->type != OB_CURVE) && (par->type != OB_LATTICE)) {
1527                         if (ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, FALSE, FALSE)) {
1528                                 DAG_relations_tag_update(bmain);
1529                                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1530                                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
1531                         }
1532                 }
1533                 else {
1534                         /* Menu creation */
1535                         uiPopupMenu *pup = uiPupMenuBegin(C, IFACE_("Set Parent To"), ICON_NONE);
1536                         uiLayout *layout = uiPupMenuLayout(pup);
1537                         
1538                         PointerRNA ptr;
1539                         
1540                         WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1541                         RNA_string_set(&ptr, "parent", parname);
1542                         RNA_string_set(&ptr, "child", childname);
1543                         RNA_enum_set(&ptr, "type", PAR_OBJECT);
1544                         /* Cannot use uiItemEnumO()... have multiple properties to set. */
1545                         uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Object"),
1546                                     0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1547                         
1548                         /* par becomes parent, make the associated menus */
1549                         if (par->type == OB_ARMATURE) {
1550                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1551                                 RNA_string_set(&ptr, "parent", parname);
1552                                 RNA_string_set(&ptr, "child", childname);
1553                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE);
1554                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Armature Deform"),
1555                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1556                                 
1557                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1558                                 RNA_string_set(&ptr, "parent", parname);
1559                                 RNA_string_set(&ptr, "child", childname);
1560                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE_NAME);
1561                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("   With Empty Groups"),
1562                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1563                                 
1564                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1565                                 RNA_string_set(&ptr, "parent", parname);
1566                                 RNA_string_set(&ptr, "child", childname);
1567                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE_ENVELOPE);
1568                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("   With Envelope Weights"),
1569                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1570                                 
1571                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1572                                 RNA_string_set(&ptr, "parent", parname);
1573                                 RNA_string_set(&ptr, "child", childname);
1574                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE_AUTO);
1575                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("   With Automatic Weights"),
1576                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1577                                 
1578                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1579                                 RNA_string_set(&ptr, "parent", parname);
1580                                 RNA_string_set(&ptr, "child", childname);
1581                                 RNA_enum_set(&ptr, "type", PAR_BONE);
1582                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Bone"),
1583                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1584                         }
1585                         else if (par->type == OB_CURVE) {
1586                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1587                                 RNA_string_set(&ptr, "parent", parname);
1588                                 RNA_string_set(&ptr, "child", childname);
1589                                 RNA_enum_set(&ptr, "type", PAR_CURVE);
1590                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Curve Deform"),
1591                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1592                                 
1593                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1594                                 RNA_string_set(&ptr, "parent", parname);
1595                                 RNA_string_set(&ptr, "child", childname);
1596                                 RNA_enum_set(&ptr, "type", PAR_FOLLOW);
1597                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Follow Path"),
1598                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1599                                 
1600                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1601                                 RNA_string_set(&ptr, "parent", parname);
1602                                 RNA_string_set(&ptr, "child", childname);
1603                                 RNA_enum_set(&ptr, "type", PAR_PATH_CONST);
1604                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Path Constraint"),
1605                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1606                         }
1607                         else if (par->type == OB_LATTICE) {
1608                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1609                                 RNA_string_set(&ptr, "parent", parname);
1610                                 RNA_string_set(&ptr, "child", childname);
1611                                 RNA_enum_set(&ptr, "type", PAR_LATTICE);
1612                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Lattice Deform"),
1613                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1614                         }
1615                         
1616                         uiPupMenuEnd(C, pup);
1617                         
1618                         return OPERATOR_CANCELLED;
1619                 }
1620         }
1621         else {
1622                 return OPERATOR_CANCELLED;
1623         }
1624
1625         return OPERATOR_FINISHED;
1626 }
1627
1628 void OUTLINER_OT_parent_drop(wmOperatorType *ot)
1629 {
1630         /* identifiers */
1631         ot->name = "Drop to Set Parent";
1632         ot->description = "Drag to parent in Outliner";
1633         ot->idname = "OUTLINER_OT_parent_drop";
1634
1635         /* api callbacks */
1636         ot->invoke = parent_drop_invoke;
1637         ot->exec = parent_drop_exec;
1638
1639         ot->poll = ED_operator_outliner_active;
1640
1641         /* flags */
1642         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
1643
1644         /* properties */
1645         RNA_def_string(ot->srna, "child", "Object", MAX_ID_NAME, "Child", "Child Object");
1646         RNA_def_string(ot->srna, "parent", "Object", MAX_ID_NAME, "Parent", "Parent Object");
1647         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
1648 }
1649
1650 int outliner_dropzone_parent_clear(bContext *C, wmEvent *event, TreeElement *te, float *fmval)
1651 {
1652         SpaceOops *soops = CTX_wm_space_outliner(C);
1653         TreeStoreElem *tselem = TREESTORE(te);
1654
1655         /* Check for row */
1656         if ((fmval[1] > te->ys) && (fmval[1] < (te->ys + UI_UNIT_Y))) {
1657                 /* Ignore drop on scene tree elements */
1658                 if ((fmval[0] > te->xs + UI_UNIT_X) && (fmval[0] < te->xend)) {
1659                         if ((te->idcode == ID_SCE) && 
1660                             !ELEM3(tselem->type, TSE_R_LAYER_BASE, TSE_R_LAYER, TSE_R_PASS))
1661                         {
1662                                 return 0;
1663                         }
1664                         // Other codes to ignore?
1665                 }
1666                 
1667                 /* Left or right of: (+), first icon, and name */
1668                 if ((fmval[0] < (te->xs + UI_UNIT_X)) || (fmval[0] > te->xend)) {
1669                         return 1;
1670                 }
1671                 else if (te->idcode != ID_OB || ELEM(tselem->type, TSE_MODIFIER_BASE, TSE_CONSTRAINT_BASE)) {
1672                         return 1;
1673                 }
1674                 
1675                 return 0;       // ID_OB, but mouse in undefined dropzone.
1676         }
1677
1678         /* Not this row.  Let's look at its children. */
1679         if ((tselem->flag & TSE_CLOSED) == 0 && (te->subtree.first)) {
1680                 for (te = te->subtree.first; te; te = te->next) {
1681                         if (outliner_dropzone_parent_clear(C, event, te, fmval)) 
1682                                 return 1;
1683                 }
1684         }
1685         return 0;
1686 }
1687
1688 static int parent_clear_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1689 {
1690         Main *bmain = CTX_data_main(C);
1691         Scene *scene = NULL;
1692         Object *ob = NULL;
1693         SpaceOops *soops = CTX_wm_space_outliner(C);
1694         TreeElement *te;
1695         char obname[MAX_ID_NAME];
1696
1697         RNA_string_get(op->ptr, "dragged_obj", obname);
1698         ob = (Object *)BKE_libblock_find_name(ID_OB, obname);
1699
1700         /* search forwards to find the object */
1701         te = outliner_find_id(soops, &soops->tree, (ID *)ob);
1702         /* then search backwards to get the scene */
1703         scene = (Scene *)outliner_search_back(soops, te, ID_SCE);
1704
1705         if (scene == NULL) {
1706                 return OPERATOR_CANCELLED;
1707         }
1708
1709         ED_object_parent_clear(ob, RNA_enum_get(op->ptr, "type"));
1710
1711         DAG_relations_tag_update(bmain);
1712         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1713         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
1714         return OPERATOR_FINISHED;
1715 }
1716
1717 void OUTLINER_OT_parent_clear(wmOperatorType *ot)
1718 {
1719         /* identifiers */
1720         ot->name = "Drop to Clear Parent";
1721         ot->description = "Drag to clear parent in Outliner";
1722         ot->idname = "OUTLINER_OT_parent_clear";
1723
1724         /* api callbacks */
1725         ot->invoke = parent_clear_invoke;
1726
1727         ot->poll = ED_operator_outliner_active;
1728
1729         /* flags */
1730         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
1731
1732         /* properties */
1733         RNA_def_string(ot->srna, "dragged_obj", "Object", MAX_ID_NAME, "Child", "Child Object");
1734         RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
1735 }
1736
1737 TreeElement *outliner_dropzone_scene(bContext *C, wmEvent *UNUSED(event), TreeElement *te, float *fmval)
1738 {
1739         SpaceOops *soops = CTX_wm_space_outliner(C);
1740         TreeStoreElem *tselem = TREESTORE(te);
1741
1742         if ((fmval[1] > te->ys) && (fmval[1] < (te->ys + UI_UNIT_Y))) {
1743                 /* name and first icon */
1744                 if ((fmval[0] > te->xs + UI_UNIT_X) && (fmval[0] < te->xend)) {
1745                         if (te->idcode == ID_SCE && tselem->type == 0) {
1746                                 return te;
1747                         }
1748                 }
1749         }
1750         return NULL;
1751 }
1752
1753 static int scene_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1754 {
1755         Scene *scene = NULL;
1756         Object *ob = NULL;
1757         SpaceOops *soops = CTX_wm_space_outliner(C);
1758         ARegion *ar = CTX_wm_region(C);
1759         Main *bmain = CTX_data_main(C);
1760         TreeElement *te = NULL;
1761         TreeElement *te_found = NULL;
1762         char obname[MAX_ID_NAME];
1763         float fmval[2];
1764
1765         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1766
1767         /* Find object hovered over */
1768         for (te = soops->tree.first; te; te = te->next) {
1769                 te_found = outliner_dropzone_scene(C, event, te, fmval);
1770                 if (te_found)
1771                         break;
1772         }
1773
1774         if (te_found) {
1775                 Base *base;
1776
1777                 RNA_string_set(op->ptr, "scene", te_found->name);
1778                 scene = (Scene *)BKE_libblock_find_name(ID_SCE, te_found->name);
1779
1780                 RNA_string_get(op->ptr, "object", obname);
1781                 ob = (Object *)BKE_libblock_find_name(ID_OB, obname);
1782
1783                 if (ELEM(NULL, ob, scene) || scene->id.lib != NULL) {
1784                         return OPERATOR_CANCELLED;
1785                 }
1786
1787                 base = ED_object_scene_link(scene, ob);
1788
1789                 if (base == NULL) {
1790                         return OPERATOR_CANCELLED;
1791                 }
1792
1793                 if (scene == CTX_data_scene(C)) {
1794                         /* when linking to an inactive scene don't touch the layer */
1795                         ob->lay = base->lay;
1796                         ED_base_object_select(base, BA_SELECT);
1797                 }
1798
1799                 DAG_relations_tag_update(bmain);
1800
1801                 WM_main_add_notifier(NC_SCENE | ND_OB_SELECT, scene);
1802
1803                 return OPERATOR_FINISHED;
1804         }
1805
1806         return OPERATOR_CANCELLED;
1807 }
1808
1809 void OUTLINER_OT_scene_drop(wmOperatorType *ot)
1810 {
1811         /* identifiers */
1812         ot->name = "Drop Object to Scene";
1813         ot->description = "Drag object to scene in Outliner";
1814         ot->idname = "OUTLINER_OT_scene_drop";
1815
1816         /* api callbacks */
1817         ot->invoke = scene_drop_invoke;
1818
1819         ot->poll = ED_operator_outliner_active;
1820
1821         /* flags */
1822         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
1823
1824         /* properties */
1825         RNA_def_string(ot->srna, "object", "Object", MAX_ID_NAME, "Object", "Target Object");
1826         RNA_def_string(ot->srna, "scene", "Scene", MAX_ID_NAME, "Scene", "Target Scene");
1827 }
1828
1829 static int material_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1830 {
1831         Material *ma = NULL;
1832         Object *ob = NULL;
1833         SpaceOops *soops = CTX_wm_space_outliner(C);
1834         ARegion *ar = CTX_wm_region(C);
1835         TreeElement *te = NULL;
1836         TreeElement *te_found = NULL;
1837         char mat_name[MAX_ID_NAME - 2];
1838         float fmval[2];
1839
1840         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1841
1842         /* Find object hovered over */
1843         for (te = soops->tree.first; te; te = te->next) {
1844                 te_found = outliner_dropzone_parent(C, event, te, fmval);
1845                 if (te_found)
1846                         break;
1847         }
1848
1849         if (te_found) {
1850                 RNA_string_set(op->ptr, "object", te_found->name);
1851                 ob = (Object *)BKE_libblock_find_name(ID_OB, te_found->name);
1852
1853                 RNA_string_get(op->ptr, "material", mat_name);
1854                 ma = (Material *)BKE_libblock_find_name(ID_MA, mat_name);
1855
1856                 if (ELEM(NULL, ob, ma)) {
1857                         return OPERATOR_CANCELLED;
1858                 }
1859
1860                 assign_material(ob, ma, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
1861
1862                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1863                 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
1864
1865                 return OPERATOR_FINISHED;
1866         }
1867
1868         return OPERATOR_CANCELLED;
1869 }
1870
1871 void OUTLINER_OT_material_drop(wmOperatorType *ot)
1872 {
1873         /* identifiers */
1874         ot->name = "Drop Material on Object";
1875         ot->description = "Drag material to object in Outliner";
1876         ot->idname = "OUTLINER_OT_material_drop";
1877
1878         /* api callbacks */
1879         ot->invoke = material_drop_invoke;
1880
1881         ot->poll = ED_operator_outliner_active;
1882
1883         /* flags */
1884         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
1885
1886         /* properties */
1887         RNA_def_string(ot->srna, "object", "Object", MAX_ID_NAME, "Object", "Target Object");
1888         RNA_def_string(ot->srna, "material", "Material", MAX_ID_NAME, "Material", "Target Material");
1889 }
1890