Fix #33310: unnecessary redraw of outliner when editing materials and textures.
[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 */
187         if (ELEM3(tselem->type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
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         /* identifiers */
840         ot->name = "Show/Hide One Level";
841         ot->idname = "OUTLINER_OT_show_one_level";
842         ot->description = "Expand/collapse all entries by one level";
843         
844         /* callbacks */
845         ot->exec = outliner_one_level_exec;
846         ot->poll = ED_operator_outliner_active;
847         
848         /* no undo or registry, UI option */
849         
850         /* properties */
851         RNA_def_boolean(ot->srna, "open", 1, "Open", "Expand all entries one level deep");
852 }
853
854 /* Show Hierarchy ----------------------------------------------- */
855
856 /* helper function for tree_element_shwo_hierarchy() - recursively checks whether subtrees have any objects*/
857 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
858 {
859         TreeElement *te;
860         TreeStoreElem *tselem;
861         
862         for (te = lb->first; te; te = te->next) {
863                 tselem = TREESTORE(te);
864                 if (tselem->type == 0 && te->idcode == ID_OB) return 1;
865                 if (subtree_has_objects(soops, &te->subtree)) return 1;
866         }
867         return 0;
868 }
869
870 /* recursive helper function for Show Hierarchy operator */
871 static void tree_element_show_hierarchy(Scene *scene, SpaceOops *soops, ListBase *lb)
872 {
873         TreeElement *te;
874         TreeStoreElem *tselem;
875
876         /* open all object elems, close others */
877         for (te = lb->first; te; te = te->next) {
878                 tselem = TREESTORE(te);
879                 
880                 if (tselem->type == 0) {
881                         if (te->idcode == ID_SCE) {
882                                 if (tselem->id != (ID *)scene) tselem->flag |= TSE_CLOSED;
883                                 else tselem->flag &= ~TSE_CLOSED;
884                         }
885                         else if (te->idcode == ID_OB) {
886                                 if (subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
887                                 else tselem->flag |= TSE_CLOSED;
888                         }
889                 }
890                 else tselem->flag |= TSE_CLOSED;
891                 
892                 if (TSELEM_OPEN(tselem, soops)) tree_element_show_hierarchy(scene, soops, &te->subtree);
893         }
894 }
895
896 /* show entire object level hierarchy */
897 static int outliner_show_hierarchy_exec(bContext *C, wmOperator *UNUSED(op))
898 {
899         SpaceOops *soops = CTX_wm_space_outliner(C);
900         ARegion *ar = CTX_wm_region(C);
901         Scene *scene = CTX_data_scene(C);
902         
903         /* recursively open/close levels */
904         tree_element_show_hierarchy(scene, soops, &soops->tree);
905         
906         ED_region_tag_redraw(ar);
907         
908         return OPERATOR_FINISHED;
909 }
910
911 void OUTLINER_OT_show_hierarchy(wmOperatorType *ot)
912 {
913         /* identifiers */
914         ot->name = "Show Hierarchy";
915         ot->idname = "OUTLINER_OT_show_hierarchy";
916         ot->description = "Open all object entries and close all others";
917         
918         /* callbacks */
919         ot->exec = outliner_show_hierarchy_exec;
920         ot->poll = ED_operator_outliner_active; //  TODO: shouldn't be allowed in RNA views...
921         
922         /* no undo or registry, UI option */
923 }
924
925 /* ************************************************************** */
926 /* ANIMATO OPERATIONS */
927 /* KeyingSet and Driver Creation - Helper functions */
928
929 /* specialized poll callback for these operators to work in Datablocks view only */
930 static int ed_operator_outliner_datablocks_active(bContext *C)
931 {
932         ScrArea *sa = CTX_wm_area(C);
933         if ((sa) && (sa->spacetype == SPACE_OUTLINER)) {
934                 SpaceOops *so = CTX_wm_space_outliner(C);
935                 return (so->outlinevis == SO_DATABLOCKS);
936         }
937         return 0;
938 }
939
940
941 /* Helper func to extract an RNA path from selected tree element 
942  * NOTE: the caller must zero-out all values of the pointers that it passes here first, as
943  * this function does not do that yet 
944  */
945 static void tree_element_to_path(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, 
946                                  ID **id, char **path, int *array_index, short *flag, short *UNUSED(groupmode))
947 {
948         ListBase hierarchy = {NULL, NULL};
949         LinkData *ld;
950         TreeElement *tem, *temnext, *temsub;
951         TreeStoreElem *tse /* , *tsenext */ /* UNUSED */;
952         PointerRNA *ptr, *nextptr;
953         PropertyRNA *prop;
954         char *newpath = NULL;
955         
956         /* optimize tricks:
957          *      - Don't do anything if the selected item is a 'struct', but arrays are allowed
958          */
959         if (tselem->type == TSE_RNA_STRUCT)
960                 return;
961         
962         /* Overview of Algorithm:
963          *  1. Go up the chain of parents until we find the 'root', taking note of the
964          *         levels encountered in reverse-order (i.e. items are added to the start of the list
965          *      for more convenient looping later)
966          *  2. Walk down the chain, adding from the first ID encountered
967          *         (which will become the 'ID' for the KeyingSet Path), and build a  
968          *      path as we step through the chain
969          */
970          
971         /* step 1: flatten out hierarchy of parents into a flat chain */
972         for (tem = te->parent; tem; tem = tem->parent) {
973                 ld = MEM_callocN(sizeof(LinkData), "LinkData for tree_element_to_path()");
974                 ld->data = tem;
975                 BLI_addhead(&hierarchy, ld);
976         }
977         
978         /* step 2: step down hierarchy building the path
979          * (NOTE: addhead in previous loop was needed so that we can loop like this) */
980         for (ld = hierarchy.first; ld; ld = ld->next) {
981                 /* get data */
982                 tem = (TreeElement *)ld->data;
983                 tse = TREESTORE(tem);
984                 ptr = &tem->rnaptr;
985                 prop = tem->directdata;
986                 
987                 /* check if we're looking for first ID, or appending to path */
988                 if (*id) {
989                         /* just 'append' property to path 
990                          * - to prevent memory leaks, we must write to newpath not path, then free old path + swap them
991                          */
992                         if (tse->type == TSE_RNA_PROPERTY) {
993                                 if (RNA_property_type(prop) == PROP_POINTER) {
994                                         /* for pointer we just append property name */
995                                         newpath = RNA_path_append(*path, ptr, prop, 0, NULL);
996                                 }
997                                 else if (RNA_property_type(prop) == PROP_COLLECTION) {
998                                         char buf[128], *name;
999                                         
1000                                         temnext = (TreeElement *)(ld->next->data);
1001                                         /* tsenext = TREESTORE(temnext); */ /* UNUSED */
1002                                         
1003                                         nextptr = &temnext->rnaptr;
1004                                         name = RNA_struct_name_get_alloc(nextptr, buf, sizeof(buf), NULL);
1005                                         
1006                                         if (name) {
1007                                                 /* if possible, use name as a key in the path */
1008                                                 newpath = RNA_path_append(*path, NULL, prop, 0, name);
1009                                                 
1010                                                 if (name != buf)
1011                                                         MEM_freeN(name);
1012                                         }
1013                                         else {
1014                                                 /* otherwise use index */
1015                                                 int index = 0;
1016                                                 
1017                                                 for (temsub = tem->subtree.first; temsub; temsub = temsub->next, index++)
1018                                                         if (temsub == temnext)
1019                                                                 break;
1020                                                 
1021                                                 newpath = RNA_path_append(*path, NULL, prop, index, NULL);
1022                                         }
1023                                         
1024                                         ld = ld->next;
1025                                 }
1026                         }
1027                         
1028                         if (newpath) {
1029                                 if (*path) MEM_freeN(*path);
1030                                 *path = newpath;
1031                                 newpath = NULL;
1032                         }
1033                 }
1034                 else {
1035                         /* no ID, so check if entry is RNA-struct, and if that RNA-struct is an ID datablock to extract info from */
1036                         if (tse->type == TSE_RNA_STRUCT) {
1037                                 /* ptr->data not ptr->id.data seems to be the one we want,
1038                                  * since ptr->data is sometimes the owner of this ID? */
1039                                 if (RNA_struct_is_ID(ptr->type)) {
1040                                         *id = (ID *)ptr->data;
1041                                         
1042                                         /* clear path */
1043                                         if (*path) {
1044                                                 MEM_freeN(*path);
1045                                                 path = NULL;
1046                                         }
1047                                 }
1048                         }
1049                 }
1050         }
1051
1052         /* step 3: if we've got an ID, add the current item to the path */
1053         if (*id) {
1054                 /* add the active property to the path */
1055                 ptr = &te->rnaptr;
1056                 prop = te->directdata;
1057                 
1058                 /* array checks */
1059                 if (tselem->type == TSE_RNA_ARRAY_ELEM) {
1060                         /* item is part of an array, so must set the array_index */
1061                         *array_index = te->index;
1062                 }
1063                 else if (RNA_property_array_length(ptr, prop)) {
1064                         /* entire array was selected, so keyframe all */
1065                         *flag |= KSP_FLAG_WHOLE_ARRAY;
1066                 }
1067                 
1068                 /* path */
1069                 newpath = RNA_path_append(*path, NULL, prop, 0, NULL);
1070                 if (*path) MEM_freeN(*path);
1071                 *path = newpath;
1072         }
1073
1074         /* free temp data */
1075         BLI_freelistN(&hierarchy);
1076 }
1077
1078 /* =============================================== */
1079 /* Driver Operations */
1080
1081 /* These operators are only available in databrowser mode for now, as
1082  * they depend on having RNA paths and/or hierarchies available.
1083  */
1084 enum {
1085         DRIVERS_EDITMODE_ADD    = 0,
1086         DRIVERS_EDITMODE_REMOVE,
1087 } /*eDrivers_EditModes*/;
1088
1089 /* Utilities ---------------------------------- */ 
1090
1091 /* Recursively iterate over tree, finding and working on selected items */
1092 static void do_outliner_drivers_editop(SpaceOops *soops, ListBase *tree, ReportList *reports, short mode)
1093 {
1094         TreeElement *te;
1095         TreeStoreElem *tselem;
1096         
1097         for (te = tree->first; te; te = te->next) {
1098                 tselem = TREESTORE(te);
1099                 
1100                 /* if item is selected, perform operation */
1101                 if (tselem->flag & TSE_SELECTED) {
1102                         ID *id = NULL;
1103                         char *path = NULL;
1104                         int array_index = 0;
1105                         short flag = 0;
1106                         short groupmode = KSP_GROUP_KSNAME;
1107                         
1108                         /* check if RNA-property described by this selected element is an animatable prop */
1109                         if (ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM) &&
1110                             RNA_property_animateable(&te->rnaptr, te->directdata))
1111                         {
1112                                 /* get id + path + index info from the selected element */
1113                                 tree_element_to_path(soops, te, tselem, 
1114                                                      &id, &path, &array_index, &flag, &groupmode);
1115                         }
1116                         
1117                         /* only if ID and path were set, should we perform any actions */
1118                         if (id && path) {
1119                                 short dflags = CREATEDRIVER_WITH_DEFAULT_DVAR;
1120                                 int arraylen = 1;
1121                                 
1122                                 /* array checks */
1123                                 if (flag & KSP_FLAG_WHOLE_ARRAY) {
1124                                         /* entire array was selected, so add drivers for all */
1125                                         arraylen = RNA_property_array_length(&te->rnaptr, te->directdata);
1126                                 }
1127                                 else
1128                                         arraylen = array_index;
1129                                 
1130                                 /* we should do at least one step */
1131                                 if (arraylen == array_index)
1132                                         arraylen++;
1133                                 
1134                                 /* for each array element we should affect, add driver */
1135                                 for (; array_index < arraylen; array_index++) {
1136                                         /* action depends on mode */
1137                                         switch (mode) {
1138                                                 case DRIVERS_EDITMODE_ADD:
1139                                                 {
1140                                                         /* add a new driver with the information obtained (only if valid) */
1141                                                         ANIM_add_driver(reports, id, path, array_index, dflags, DRIVER_TYPE_PYTHON);
1142                                                 }
1143                                                 break;
1144                                                 case DRIVERS_EDITMODE_REMOVE:
1145                                                 {
1146                                                         /* remove driver matching the information obtained (only if valid) */
1147                                                         ANIM_remove_driver(reports, id, path, array_index, dflags);
1148                                                 }
1149                                                 break;
1150                                         }
1151                                 }
1152                                 
1153                                 /* free path, since it had to be generated */
1154                                 MEM_freeN(path);
1155                         }
1156                         
1157                         
1158                 }
1159                 
1160                 /* go over sub-tree */
1161                 if (TSELEM_OPEN(tselem, soops))
1162                         do_outliner_drivers_editop(soops, &te->subtree, reports, mode);
1163         }
1164 }
1165
1166 /* Add Operator ---------------------------------- */
1167
1168 static int outliner_drivers_addsel_exec(bContext *C, wmOperator *op)
1169 {
1170         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1171         
1172         /* check for invalid states */
1173         if (soutliner == NULL)
1174                 return OPERATOR_CANCELLED;
1175         
1176         /* recursively go into tree, adding selected items */
1177         do_outliner_drivers_editop(soutliner, &soutliner->tree, op->reports, DRIVERS_EDITMODE_ADD);
1178         
1179         /* send notifiers */
1180         WM_event_add_notifier(C, NC_ANIMATION | ND_FCURVES_ORDER, NULL); // XXX
1181         
1182         return OPERATOR_FINISHED;
1183 }
1184
1185 void OUTLINER_OT_drivers_add_selected(wmOperatorType *ot)
1186 {
1187         /* api callbacks */
1188         ot->idname = "OUTLINER_OT_drivers_add_selected";
1189         ot->name = "Add Drivers for Selected";
1190         ot->description = "Add drivers to selected items";
1191         
1192         /* api callbacks */
1193         ot->exec = outliner_drivers_addsel_exec;
1194         ot->poll = ed_operator_outliner_datablocks_active;
1195         
1196         /* flags */
1197         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1198 }
1199
1200
1201 /* Remove Operator ---------------------------------- */
1202
1203 static int outliner_drivers_deletesel_exec(bContext *C, wmOperator *op)
1204 {
1205         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1206         
1207         /* check for invalid states */
1208         if (soutliner == NULL)
1209                 return OPERATOR_CANCELLED;
1210         
1211         /* recursively go into tree, adding selected items */
1212         do_outliner_drivers_editop(soutliner, &soutliner->tree, op->reports, DRIVERS_EDITMODE_REMOVE);
1213         
1214         /* send notifiers */
1215         WM_event_add_notifier(C, ND_KEYS, NULL); // XXX
1216         
1217         return OPERATOR_FINISHED;
1218 }
1219
1220 void OUTLINER_OT_drivers_delete_selected(wmOperatorType *ot)
1221 {
1222         /* identifiers */
1223         ot->idname = "OUTLINER_OT_drivers_delete_selected";
1224         ot->name = "Delete Drivers for Selected";
1225         ot->description = "Delete drivers assigned to selected items";
1226         
1227         /* api callbacks */
1228         ot->exec = outliner_drivers_deletesel_exec;
1229         ot->poll = ed_operator_outliner_datablocks_active;
1230         
1231         /* flags */
1232         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1233 }
1234
1235 /* =============================================== */
1236 /* Keying Set Operations */
1237
1238 /* These operators are only available in databrowser mode for now, as
1239  * they depend on having RNA paths and/or hierarchies available.
1240  */
1241 enum {
1242         KEYINGSET_EDITMODE_ADD  = 0,
1243         KEYINGSET_EDITMODE_REMOVE,
1244 } /*eKeyingSet_EditModes*/;
1245
1246 /* Utilities ---------------------------------- */ 
1247  
1248 /* find the 'active' KeyingSet, and add if not found (if adding is allowed) */
1249 // TODO: should this be an API func?
1250 static KeyingSet *verify_active_keyingset(Scene *scene, short add)
1251 {
1252         KeyingSet *ks = NULL;
1253         
1254         /* sanity check */
1255         if (scene == NULL)
1256                 return NULL;
1257         
1258         /* try to find one from scene */
1259         if (scene->active_keyingset > 0)
1260                 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
1261                 
1262         /* add if none found */
1263         // XXX the default settings have yet to evolve
1264         if ((add) && (ks == NULL)) {
1265                 ks = BKE_keyingset_add(&scene->keyingsets, NULL, NULL, KEYINGSET_ABSOLUTE, 0);
1266                 scene->active_keyingset = BLI_countlist(&scene->keyingsets);
1267         }
1268         
1269         return ks;
1270 }
1271
1272 /* Recursively iterate over tree, finding and working on selected items */
1273 static void do_outliner_keyingset_editop(SpaceOops *soops, KeyingSet *ks, ListBase *tree, short mode)
1274 {
1275         TreeElement *te;
1276         TreeStoreElem *tselem;
1277         
1278         for (te = tree->first; te; te = te->next) {
1279                 tselem = TREESTORE(te);
1280                 
1281                 /* if item is selected, perform operation */
1282                 if (tselem->flag & TSE_SELECTED) {
1283                         ID *id = NULL;
1284                         char *path = NULL;
1285                         int array_index = 0;
1286                         short flag = 0;
1287                         short groupmode = KSP_GROUP_KSNAME;
1288                         
1289                         /* check if RNA-property described by this selected element is an animatable prop */
1290                         if (ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM) &&
1291                             RNA_property_animateable(&te->rnaptr, te->directdata))
1292                         {
1293                                 /* get id + path + index info from the selected element */
1294                                 tree_element_to_path(soops, te, tselem, 
1295                                                      &id, &path, &array_index, &flag, &groupmode);
1296                         }
1297                         
1298                         /* only if ID and path were set, should we perform any actions */
1299                         if (id && path) {
1300                                 /* action depends on mode */
1301                                 switch (mode) {
1302                                         case KEYINGSET_EDITMODE_ADD:
1303                                         {
1304                                                 /* add a new path with the information obtained (only if valid) */
1305                                                 /* TODO: what do we do with group name?
1306                                                  * for now, we don't supply one, and just let this use the KeyingSet name */
1307                                                 BKE_keyingset_add_path(ks, id, NULL, path, array_index, flag, groupmode);
1308                                                 ks->active_path = BLI_countlist(&ks->paths);
1309                                         }
1310                                         break;
1311                                         case KEYINGSET_EDITMODE_REMOVE:
1312                                         {
1313                                                 /* find the relevant path, then remove it from the KeyingSet */
1314                                                 KS_Path *ksp = BKE_keyingset_find_path(ks, id, NULL, path, array_index, groupmode);
1315                                                 
1316                                                 if (ksp) {
1317                                                         /* free path's data */
1318                                                         BKE_keyingset_free_path(ks, ksp);
1319
1320                                                         ks->active_path = 0;
1321                                                 }
1322                                         }
1323                                         break;
1324                                 }
1325                                 
1326                                 /* free path, since it had to be generated */
1327                                 MEM_freeN(path);
1328                         }
1329                 }
1330                 
1331                 /* go over sub-tree */
1332                 if (TSELEM_OPEN(tselem, soops))
1333                         do_outliner_keyingset_editop(soops, ks, &te->subtree, mode);
1334         }
1335 }
1336
1337 /* Add Operator ---------------------------------- */
1338
1339 static int outliner_keyingset_additems_exec(bContext *C, wmOperator *op)
1340 {
1341         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1342         Scene *scene = CTX_data_scene(C);
1343         KeyingSet *ks = verify_active_keyingset(scene, 1);
1344         
1345         /* check for invalid states */
1346         if (ks == NULL) {
1347                 BKE_report(op->reports, RPT_ERROR, "Operation requires an active keying set");
1348                 return OPERATOR_CANCELLED;
1349         }
1350         if (soutliner == NULL)
1351                 return OPERATOR_CANCELLED;
1352         
1353         /* recursively go into tree, adding selected items */
1354         do_outliner_keyingset_editop(soutliner, ks, &soutliner->tree, KEYINGSET_EDITMODE_ADD);
1355         
1356         /* send notifiers */
1357         WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
1358         
1359         return OPERATOR_FINISHED;
1360 }
1361
1362 void OUTLINER_OT_keyingset_add_selected(wmOperatorType *ot)
1363 {
1364         /* identifiers */
1365         ot->idname = "OUTLINER_OT_keyingset_add_selected";
1366         ot->name = "Keying Set Add Selected";
1367         ot->description = "Add selected items (blue-gray rows) to active Keying Set";
1368         
1369         /* api callbacks */
1370         ot->exec = outliner_keyingset_additems_exec;
1371         ot->poll = ed_operator_outliner_datablocks_active;
1372         
1373         /* flags */
1374         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1375 }
1376
1377
1378 /* Remove Operator ---------------------------------- */
1379
1380 static int outliner_keyingset_removeitems_exec(bContext *C, wmOperator *UNUSED(op))
1381 {
1382         SpaceOops *soutliner = CTX_wm_space_outliner(C);
1383         Scene *scene = CTX_data_scene(C);
1384         KeyingSet *ks = verify_active_keyingset(scene, 1);
1385         
1386         /* check for invalid states */
1387         if (soutliner == NULL)
1388                 return OPERATOR_CANCELLED;
1389         
1390         /* recursively go into tree, adding selected items */
1391         do_outliner_keyingset_editop(soutliner, ks, &soutliner->tree, KEYINGSET_EDITMODE_REMOVE);
1392         
1393         /* send notifiers */
1394         WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
1395         
1396         return OPERATOR_FINISHED;
1397 }
1398
1399 void OUTLINER_OT_keyingset_remove_selected(wmOperatorType *ot)
1400 {
1401         /* identifiers */
1402         ot->idname = "OUTLINER_OT_keyingset_remove_selected";
1403         ot->name = "Keying Set Remove Selected";
1404         ot->description = "Remove selected items (blue-gray rows) from active Keying Set";
1405         
1406         /* api callbacks */
1407         ot->exec = outliner_keyingset_removeitems_exec;
1408         ot->poll = ed_operator_outliner_datablocks_active;
1409         
1410         /* flags */
1411         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1412 }
1413
1414 /* ******************** Parent Drop Operator *********************** */
1415
1416 static int parent_drop_exec(bContext *C, wmOperator *op)
1417 {
1418         Object *par = NULL, *ob = NULL;
1419         Main *bmain = CTX_data_main(C);
1420         Scene *scene = CTX_data_scene(C);
1421         int partype = -1;
1422         char parname[MAX_ID_NAME], childname[MAX_ID_NAME];
1423
1424         partype = RNA_enum_get(op->ptr, "type");
1425         RNA_string_get(op->ptr, "parent", parname);
1426         par = (Object *)BKE_libblock_find_name(ID_OB, parname);
1427         RNA_string_get(op->ptr, "child", childname);
1428         ob = (Object *)BKE_libblock_find_name(ID_OB, childname);
1429
1430         ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, FALSE, FALSE);
1431
1432         DAG_scene_sort(bmain, scene);
1433         DAG_ids_flush_update(bmain, 0);
1434         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1435         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
1436
1437         return OPERATOR_FINISHED;
1438 }
1439
1440 /* Used for drag and drop parenting */
1441 TreeElement *outliner_dropzone_parent(bContext *C, wmEvent *event, TreeElement *te, float *fmval)
1442 {
1443         SpaceOops *soops = CTX_wm_space_outliner(C);
1444         TreeStoreElem *tselem = TREESTORE(te);
1445
1446         if ((fmval[1] > te->ys) && (fmval[1] < (te->ys + UI_UNIT_Y))) {
1447                 /* name and first icon */
1448                 if ((fmval[0] > te->xs + UI_UNIT_X) && (fmval[0] < te->xend)) {
1449                         /* always makes active object */
1450                         if (te->idcode == ID_OB && tselem->type == 0) {
1451                                 return te;
1452                         }
1453                         else {
1454                                 return NULL;
1455                         }
1456                 }
1457         }
1458
1459         /* Not it.  Let's look at its children. */
1460         if ((tselem->flag & TSE_CLOSED) == 0 && (te->subtree.first)) {
1461                 for (te = te->subtree.first; te; te = te->next) {
1462                         TreeElement *te_valid;
1463                         te_valid = outliner_dropzone_parent(C, event, te, fmval);
1464                         if (te_valid) return te_valid;
1465                 }
1466         }
1467         return NULL;
1468 }
1469
1470 static int parent_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1471 {
1472         Object *par = NULL;
1473         Object *ob = NULL;
1474         SpaceOops *soops = CTX_wm_space_outliner(C);
1475         ARegion *ar = CTX_wm_region(C);
1476         Main *bmain = CTX_data_main(C);
1477         Scene *scene = NULL;
1478         TreeElement *te = NULL;
1479         TreeElement *te_found = NULL;
1480         char childname[MAX_ID_NAME];
1481         char parname[MAX_ID_NAME];
1482         int partype = 0;
1483         float fmval[2];
1484
1485         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1486
1487         /* Find object hovered over */
1488         for (te = soops->tree.first; te; te = te->next) {
1489                 te_found = outliner_dropzone_parent(C, event, te, fmval);
1490                 if (te_found) break;
1491         }
1492
1493         if (te_found) {
1494                 RNA_string_set(op->ptr, "parent", te_found->name);
1495                 /* Identify parent and child */
1496                 RNA_string_get(op->ptr, "child", childname);
1497                 ob = (Object *)BKE_libblock_find_name(ID_OB, childname);
1498                 RNA_string_get(op->ptr, "parent", parname);
1499                 par = (Object *)BKE_libblock_find_name(ID_OB, parname);
1500                 
1501                 if (ELEM(NULL, ob, par)) {
1502                         if (par == NULL) printf("par==NULL\n");
1503                         return OPERATOR_CANCELLED;
1504                 }
1505                 if (ob == par) {
1506                         return OPERATOR_CANCELLED;
1507                 }
1508                 
1509                 scene = (Scene *)outliner_search_back(soops, te_found, ID_SCE);
1510
1511                 if (scene == NULL) {
1512                         /* currently outlier organized in a way, that if there's no parent scene
1513                          * element for object it means that all displayed objects belong to
1514                          * active scene and parenting them is allowed (sergey)
1515                          */
1516
1517                         scene = CTX_data_scene(C);
1518                 }
1519
1520                 if ((par->type != OB_ARMATURE) && (par->type != OB_CURVE) && (par->type != OB_LATTICE)) {
1521                         if (ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, FALSE, FALSE)) {
1522                                 DAG_scene_sort(bmain, scene);
1523                                 DAG_ids_flush_update(bmain, 0);
1524                                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1525                                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
1526                         }
1527                 }
1528                 else {
1529                         /* Menu creation */
1530                         uiPopupMenu *pup = uiPupMenuBegin(C, IFACE_("Set Parent To"), ICON_NONE);
1531                         uiLayout *layout = uiPupMenuLayout(pup);
1532                         
1533                         PointerRNA ptr;
1534                         
1535                         WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1536                         RNA_string_set(&ptr, "parent", parname);
1537                         RNA_string_set(&ptr, "child", childname);
1538                         RNA_enum_set(&ptr, "type", PAR_OBJECT);
1539                         /* Cannot use uiItemEnumO()... have multiple properties to set. */
1540                         uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Object"),
1541                                     0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1542                         
1543                         /* par becomes parent, make the associated menus */
1544                         if (par->type == OB_ARMATURE) {
1545                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1546                                 RNA_string_set(&ptr, "parent", parname);
1547                                 RNA_string_set(&ptr, "child", childname);
1548                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE);
1549                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Armature Deform"),
1550                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1551                                 
1552                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1553                                 RNA_string_set(&ptr, "parent", parname);
1554                                 RNA_string_set(&ptr, "child", childname);
1555                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE_NAME);
1556                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("   With Empty Groups"),
1557                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1558                                 
1559                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1560                                 RNA_string_set(&ptr, "parent", parname);
1561                                 RNA_string_set(&ptr, "child", childname);
1562                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE_ENVELOPE);
1563                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("   With Envelope Weights"),
1564                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1565                                 
1566                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1567                                 RNA_string_set(&ptr, "parent", parname);
1568                                 RNA_string_set(&ptr, "child", childname);
1569                                 RNA_enum_set(&ptr, "type", PAR_ARMATURE_AUTO);
1570                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("   With Automatic Weights"),
1571                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1572                                 
1573                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1574                                 RNA_string_set(&ptr, "parent", parname);
1575                                 RNA_string_set(&ptr, "child", childname);
1576                                 RNA_enum_set(&ptr, "type", PAR_BONE);
1577                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Bone"),
1578                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1579                         }
1580                         else if (par->type == OB_CURVE) {
1581                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1582                                 RNA_string_set(&ptr, "parent", parname);
1583                                 RNA_string_set(&ptr, "child", childname);
1584                                 RNA_enum_set(&ptr, "type", PAR_CURVE);
1585                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Curve Deform"),
1586                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1587                                 
1588                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1589                                 RNA_string_set(&ptr, "parent", parname);
1590                                 RNA_string_set(&ptr, "child", childname);
1591                                 RNA_enum_set(&ptr, "type", PAR_FOLLOW);
1592                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Follow Path"),
1593                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1594                                 
1595                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1596                                 RNA_string_set(&ptr, "parent", parname);
1597                                 RNA_string_set(&ptr, "child", childname);
1598                                 RNA_enum_set(&ptr, "type", PAR_PATH_CONST);
1599                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Path Constraint"),
1600                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1601                         }
1602                         else if (par->type == OB_LATTICE) {
1603                                 WM_operator_properties_create(&ptr, "OUTLINER_OT_parent_drop");
1604                                 RNA_string_set(&ptr, "parent", parname);
1605                                 RNA_string_set(&ptr, "child", childname);
1606                                 RNA_enum_set(&ptr, "type", PAR_LATTICE);
1607                                 uiItemFullO(layout, "OUTLINER_OT_parent_drop", IFACE_("Lattice Deform"),
1608                                             0, ptr.data, WM_OP_EXEC_DEFAULT, 0);
1609                         }
1610                         
1611                         uiPupMenuEnd(C, pup);
1612                         
1613                         return OPERATOR_CANCELLED;
1614                 }
1615         }
1616         else {
1617                 return OPERATOR_CANCELLED;
1618         }
1619
1620         return OPERATOR_FINISHED;
1621 }
1622
1623 void OUTLINER_OT_parent_drop(wmOperatorType *ot)
1624 {
1625         /* identifiers */
1626         ot->name = "Drop to Set Parent";
1627         ot->description = "Drag to parent in Outliner";
1628         ot->idname = "OUTLINER_OT_parent_drop";
1629
1630         /* api callbacks */
1631         ot->invoke = parent_drop_invoke;
1632         ot->exec = parent_drop_exec;
1633
1634         ot->poll = ED_operator_outliner_active;
1635
1636         /* flags */
1637         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1638
1639         /* properties */
1640         RNA_def_string(ot->srna, "child", "Object", MAX_ID_NAME, "Child", "Child Object");
1641         RNA_def_string(ot->srna, "parent", "Object", MAX_ID_NAME, "Parent", "Parent Object");
1642         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
1643 }
1644
1645 int outliner_dropzone_parent_clear(bContext *C, wmEvent *event, TreeElement *te, float *fmval)
1646 {
1647         SpaceOops *soops = CTX_wm_space_outliner(C);
1648         TreeStoreElem *tselem = TREESTORE(te);
1649
1650         /* Check for row */
1651         if ((fmval[1] > te->ys) && (fmval[1] < (te->ys + UI_UNIT_Y))) {
1652                 /* Ignore drop on scene tree elements */
1653                 if ((fmval[0] > te->xs + UI_UNIT_X) && (fmval[0] < te->xend)) {
1654                         if ((te->idcode == ID_SCE) && 
1655                             !ELEM3(tselem->type, TSE_R_LAYER_BASE, TSE_R_LAYER, TSE_R_PASS))
1656                         {
1657                                 return 0;
1658                         }
1659                         // Other codes to ignore?
1660                 }
1661                 
1662                 /* Left or right of: (+), first icon, and name */
1663                 if ((fmval[0] < (te->xs + UI_UNIT_X)) || (fmval[0] > te->xend)) {
1664                         return 1;
1665                 }
1666                 else if (te->idcode != ID_OB || ELEM(tselem->type, TSE_MODIFIER_BASE, TSE_CONSTRAINT_BASE)) {
1667                         return 1;
1668                 }
1669                 
1670                 return 0;       // ID_OB, but mouse in undefined dropzone.
1671         }
1672
1673         /* Not this row.  Let's look at its children. */
1674         if ((tselem->flag & TSE_CLOSED) == 0 && (te->subtree.first)) {
1675                 for (te = te->subtree.first; te; te = te->next) {
1676                         if (outliner_dropzone_parent_clear(C, event, te, fmval)) 
1677                                 return 1;
1678                 }
1679         }
1680         return 0;
1681 }
1682
1683 static int parent_clear_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1684 {
1685         Main *bmain = CTX_data_main(C);
1686         Scene *scene = NULL;
1687         Object *ob = NULL;
1688         SpaceOops *soops = CTX_wm_space_outliner(C);
1689         TreeElement *te;
1690         char obname[MAX_ID_NAME];
1691
1692         RNA_string_get(op->ptr, "dragged_obj", obname);
1693         ob = (Object *)BKE_libblock_find_name(ID_OB, obname);
1694
1695         /* search forwards to find the object */
1696         te = outliner_find_id(soops, &soops->tree, (ID *)ob);
1697         /* then search backwards to get the scene */
1698         scene = (Scene *)outliner_search_back(soops, te, ID_SCE);
1699
1700         if (scene == NULL) {
1701                 return OPERATOR_CANCELLED;
1702         }
1703
1704         ED_object_parent_clear(ob, RNA_enum_get(op->ptr, "type"));
1705
1706         DAG_scene_sort(bmain, scene);
1707         DAG_ids_flush_update(bmain, 0);
1708         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1709         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
1710         return OPERATOR_FINISHED;
1711 }
1712
1713 void OUTLINER_OT_parent_clear(wmOperatorType *ot)
1714 {
1715         /* identifiers */
1716         ot->name = "Drop to Clear Parent";
1717         ot->description = "Drag to clear parent in Outliner";
1718         ot->idname = "OUTLINER_OT_parent_clear";
1719
1720         /* api callbacks */
1721         ot->invoke = parent_clear_invoke;
1722
1723         ot->poll = ED_operator_outliner_active;
1724
1725         /* flags */
1726         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1727
1728         /* properties */
1729         RNA_def_string(ot->srna, "dragged_obj", "Object", MAX_ID_NAME, "Child", "Child Object");
1730         RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
1731 }
1732
1733 TreeElement *outliner_dropzone_scene(bContext *C, wmEvent *UNUSED(event), TreeElement *te, float *fmval)
1734 {
1735         SpaceOops *soops = CTX_wm_space_outliner(C);
1736         TreeStoreElem *tselem = TREESTORE(te);
1737
1738         if ((fmval[1] > te->ys) && (fmval[1] < (te->ys + UI_UNIT_Y))) {
1739                 /* name and first icon */
1740                 if ((fmval[0] > te->xs + UI_UNIT_X) && (fmval[0] < te->xend)) {
1741                         if (te->idcode == ID_SCE && tselem->type == 0) {
1742                                 return te;
1743                         }
1744                 }
1745         }
1746         return NULL;
1747 }
1748
1749 static int scene_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1750 {
1751         Scene *scene = NULL;
1752         Object *ob = NULL;
1753         SpaceOops *soops = CTX_wm_space_outliner(C);
1754         ARegion *ar = CTX_wm_region(C);
1755         Main *bmain = CTX_data_main(C);
1756         TreeElement *te = NULL;
1757         TreeElement *te_found = NULL;
1758         char obname[MAX_ID_NAME];
1759         float fmval[2];
1760
1761         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1762
1763         /* Find object hovered over */
1764         for (te = soops->tree.first; te; te = te->next) {
1765                 te_found = outliner_dropzone_scene(C, event, te, fmval);
1766                 if (te_found)
1767                         break;
1768         }
1769
1770         if (te_found) {
1771                 Base *base;
1772
1773                 RNA_string_set(op->ptr, "scene", te_found->name);
1774                 scene = (Scene *)BKE_libblock_find_name(ID_SCE, te_found->name);
1775
1776                 RNA_string_get(op->ptr, "object", obname);
1777                 ob = (Object *)BKE_libblock_find_name(ID_OB, obname);
1778
1779                 if (ELEM(NULL, ob, scene) || scene->id.lib != NULL) {
1780                         return OPERATOR_CANCELLED;
1781                 }
1782
1783                 base = ED_object_scene_link(scene, ob);
1784
1785                 if (base == NULL) {
1786                         return OPERATOR_CANCELLED;
1787                 }
1788
1789                 if (scene == CTX_data_scene(C)) {
1790                         /* when linking to an inactive scene don't touch the layer */
1791                         ob->lay = base->lay;
1792                         ED_base_object_select(base, BA_SELECT);
1793                 }
1794
1795                 DAG_scene_sort(bmain, scene);
1796                 DAG_ids_flush_update(bmain, 0);
1797
1798                 WM_main_add_notifier(NC_SCENE | ND_OB_SELECT, scene);
1799
1800                 return OPERATOR_FINISHED;
1801         }
1802
1803         return OPERATOR_CANCELLED;
1804 }
1805
1806 void OUTLINER_OT_scene_drop(wmOperatorType *ot)
1807 {
1808         /* identifiers */
1809         ot->name = "Drop Object to Scene";
1810         ot->description = "Drag object to scene in Outliner";
1811         ot->idname = "OUTLINER_OT_scene_drop";
1812
1813         /* api callbacks */
1814         ot->invoke = scene_drop_invoke;
1815
1816         ot->poll = ED_operator_outliner_active;
1817
1818         /* flags */
1819         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1820
1821         /* properties */
1822         RNA_def_string(ot->srna, "object", "Object", MAX_ID_NAME, "Object", "Target Object");
1823         RNA_def_string(ot->srna, "scene", "Scene", MAX_ID_NAME, "Scene", "Target Scene");
1824 }
1825
1826 static int material_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1827 {
1828         Main *bmain = CTX_data_main(C);
1829         Material *ma = NULL;
1830         Object *ob = NULL;
1831         SpaceOops *soops = CTX_wm_space_outliner(C);
1832         ARegion *ar = CTX_wm_region(C);
1833         TreeElement *te = NULL;
1834         TreeElement *te_found = NULL;
1835         char mat_name[MAX_ID_NAME - 2];
1836         float fmval[2];
1837
1838         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
1839
1840         /* Find object hovered over */
1841         for (te = soops->tree.first; te; te = te->next) {
1842                 te_found = outliner_dropzone_parent(C, event, te, fmval);
1843                 if (te_found)
1844                         break;
1845         }
1846
1847         if (te_found) {
1848                 RNA_string_set(op->ptr, "object", te_found->name);
1849                 ob = (Object *)BKE_libblock_find_name(ID_OB, te_found->name);
1850
1851                 RNA_string_get(op->ptr, "material", mat_name);
1852                 ma = (Material *)BKE_libblock_find_name(ID_MA, mat_name);
1853
1854                 if (ELEM(NULL, ob, ma)) {
1855                         return OPERATOR_CANCELLED;
1856                 }
1857
1858                 assign_material(ob, ma, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
1859
1860                 DAG_ids_flush_update(bmain, 0);
1861                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1862                 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
1863
1864                 return OPERATOR_FINISHED;
1865         }
1866
1867         return OPERATOR_CANCELLED;
1868 }
1869
1870 void OUTLINER_OT_material_drop(wmOperatorType *ot)
1871 {
1872         /* identifiers */
1873         ot->name = "Drop Material on Object";
1874         ot->description = "Drag material to object in Outliner";
1875         ot->idname = "OUTLINER_OT_material_drop";
1876
1877         /* api callbacks */
1878         ot->invoke = material_drop_invoke;
1879
1880         ot->poll = ED_operator_outliner_active;
1881
1882         /* flags */
1883         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1884
1885         /* properties */
1886         RNA_def_string(ot->srna, "object", "Object", MAX_ID_NAME, "Object", "Target Object");
1887         RNA_def_string(ot->srna, "material", "Material", MAX_ID_NAME, "Material", "Target Material");
1888 }
1889