svn merge -r 23528:23646 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / space_outliner / outliner.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_anim_types.h"
37 #include "DNA_action_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_camera_types.h"
42 #include "DNA_image_types.h"
43 #include "DNA_ipo_types.h"
44 #include "DNA_group_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meta_types.h"
50 #include "DNA_modifier_types.h"
51 #include "DNA_nla_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_outliner_types.h"
54 #include "DNA_particle_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_screen_types.h"
57 #include "DNA_space_types.h"
58 #include "DNA_texture_types.h"
59 #include "DNA_text_types.h"
60 #include "DNA_world_types.h"
61 #include "DNA_sequence_types.h"
62
63 #include "BLI_blenlib.h"
64
65 #include "IMB_imbuf_types.h"
66
67 #include "BKE_animsys.h"
68 #include "BKE_constraint.h"
69 #include "BKE_context.h"
70 #include "BKE_deform.h"
71 #include "BKE_depsgraph.h"
72 #include "BKE_global.h"
73 #include "BKE_group.h"
74 #include "BKE_library.h"
75 #include "BKE_main.h"
76 #include "BKE_material.h"
77 #include "BKE_modifier.h"
78 #include "BKE_object.h"
79 #include "BKE_report.h"
80 #include "BKE_screen.h"
81 #include "BKE_scene.h"
82 #include "BKE_sequence.h"
83 #include "BKE_utildefines.h"
84
85 #include "ED_armature.h"
86 #include "ED_object.h"
87 #include "ED_screen.h"
88 #include "ED_util.h"
89 #include "ED_types.h"
90
91 #include "WM_api.h"
92 #include "WM_types.h"
93
94 #include "BIF_gl.h"
95 #include "BIF_glutil.h"
96
97 #include "UI_interface.h"
98 #include "UI_interface_icons.h"
99 #include "UI_resources.h"
100 #include "UI_view2d.h"
101
102 #include "RNA_access.h"
103 #include "RNA_define.h"
104
105 #include "ED_armature.h"
106 #include "ED_keyframing.h"
107 #include "ED_object.h"
108 #include "ED_particle.h"
109 #include "ED_screen.h"
110 #include "ED_view3d.h"
111
112 #include "outliner_intern.h"
113
114 #include "PIL_time.h" 
115
116
117 #define OL_H    19
118 #define OL_X    18
119
120 #define OL_TOG_RESTRICT_VIEWX   54
121 #define OL_TOG_RESTRICT_SELECTX 36
122 #define OL_TOG_RESTRICT_RENDERX 18
123
124 #define OL_TOGW OL_TOG_RESTRICT_VIEWX
125
126 #define OL_RNA_COLX                     300
127 #define OL_RNA_COL_SIZEX        150
128 #define OL_RNA_COL_SPACEX       50
129
130 #define TS_CHUNK        128
131
132 #define TREESTORE(a) ((a)?soops->treestore->data+(a)->store_index:NULL)
133
134 /* ************* XXX **************** */
135
136 static void error() {}
137
138 /* ********************************** */
139
140
141 /* ******************** PROTOTYPES ***************** */
142 static void outliner_draw_tree_element(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, int startx, int *starty);
143 static void outliner_do_object_operation(bContext *C, Scene *scene, SpaceOops *soops, ListBase *lb, 
144                                                                                  void (*operation_cb)(bContext *C, Scene *scene, TreeElement *, TreeStoreElem *, TreeStoreElem *));
145
146
147 /* ******************** PERSISTANT DATA ***************** */
148
149 static void outliner_storage_cleanup(SpaceOops *soops)
150 {
151         TreeStore *ts= soops->treestore;
152         
153         if(ts) {
154                 TreeStoreElem *tselem;
155                 int a, unused= 0;
156                 
157                 /* each element used once, for ID blocks with more users to have each a treestore */
158                 for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) tselem->used= 0;
159
160                 /* cleanup only after reading file or undo step, and always for
161                  * RNA datablocks view in order to save memory */
162                 if(soops->storeflag & SO_TREESTORE_CLEANUP) {
163                         
164                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
165                                 if(tselem->id==NULL) unused++;
166                         }
167
168                         if(unused) {
169                                 if(ts->usedelem == unused) {
170                                         MEM_freeN(ts->data);
171                                         ts->data= NULL;
172                                         ts->usedelem= ts->totelem= 0;
173                                 }
174                                 else {
175                                         TreeStoreElem *tsnewar, *tsnew;
176                                         
177                                         tsnew=tsnewar= MEM_mallocN((ts->usedelem-unused)*sizeof(TreeStoreElem), "new tselem");
178                                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
179                                                 if(tselem->id) {
180                                                         *tsnew= *tselem;
181                                                         tsnew++;
182                                                 }
183                                         }
184                                         MEM_freeN(ts->data);
185                                         ts->data= tsnewar;
186                                         ts->usedelem-= unused;
187                                         ts->totelem= ts->usedelem;
188                                 }
189                         }
190                 }
191         }
192 }
193
194 static void check_persistant(SpaceOops *soops, TreeElement *te, ID *id, short type, short nr)
195 {
196         TreeStore *ts;
197         TreeStoreElem *tselem;
198         int a;
199         
200         /* case 1; no TreeStore */
201         if(soops->treestore==NULL) {
202                 ts= soops->treestore= MEM_callocN(sizeof(TreeStore), "treestore");
203         }
204         ts= soops->treestore;
205         
206         /* check if 'te' is in treestore */
207         tselem= ts->data;
208         for(a=0; a<ts->usedelem; a++, tselem++) {
209                 if(tselem->id==id && tselem->used==0) {
210                         if((type==0 && tselem->type==0) ||(tselem->type==type && tselem->nr==nr)) {
211                                 te->store_index= a;
212                                 tselem->used= 1;
213                                 return;
214                         }
215                 }
216         }
217         
218         /* add 1 element to treestore */
219         if(ts->usedelem==ts->totelem) {
220                 TreeStoreElem *tsnew;
221                 
222                 tsnew= MEM_mallocN((ts->totelem+TS_CHUNK)*sizeof(TreeStoreElem), "treestore data");
223                 if(ts->data) {
224                         memcpy(tsnew, ts->data, ts->totelem*sizeof(TreeStoreElem));
225                         MEM_freeN(ts->data);
226                 }
227                 ts->data= tsnew;
228                 ts->totelem+= TS_CHUNK;
229         }
230         
231         tselem= ts->data+ts->usedelem;
232         
233         tselem->type= type;
234         if(type) tselem->nr= nr; // we're picky! :)
235         else tselem->nr= 0;
236         tselem->id= id;
237         tselem->used = 0;
238         tselem->flag= TSE_CLOSED;
239         te->store_index= ts->usedelem;
240         
241         ts->usedelem++;
242 }
243
244 /* ******************** TREE MANAGEMENT ****************** */
245
246 void outliner_free_tree(ListBase *lb)
247 {
248         
249         while(lb->first) {
250                 TreeElement *te= lb->first;
251                 
252                 outliner_free_tree(&te->subtree);
253                 BLI_remlink(lb, te);
254
255                 if(te->flag & TE_FREE_NAME) MEM_freeN(te->name);
256                 MEM_freeN(te);
257         }
258 }
259
260 static void outliner_height(SpaceOops *soops, ListBase *lb, int *h)
261 {
262         TreeElement *te= lb->first;
263         while(te) {
264                 TreeStoreElem *tselem= TREESTORE(te);
265                 if((tselem->flag & TSE_CLOSED)==0) 
266                         outliner_height(soops, &te->subtree, h);
267                 (*h) += OL_H;
268                 te= te->next;
269         }
270 }
271
272 #if 0  // XXX this is currently disabled until te->xend is set correctly
273 static void outliner_width(SpaceOops *soops, ListBase *lb, int *w)
274 {
275         TreeElement *te= lb->first;
276         while(te) {
277 //              TreeStoreElem *tselem= TREESTORE(te);
278                 
279                 // XXX fixme... te->xend is not set yet
280                 if(tselem->flag & TSE_CLOSED) {
281                         if (te->xend > *w)
282                                 *w = te->xend;
283                 }
284                 outliner_width(soops, &te->subtree, w);
285                 te= te->next;
286         }
287 }
288 #endif
289
290 static void outliner_rna_width(SpaceOops *soops, ListBase *lb, int *w, int startx)
291 {
292         TreeElement *te= lb->first;
293         while(te) {
294                 TreeStoreElem *tselem= TREESTORE(te);
295                         // XXX fixme... (currently, we're using a fixed length of 100)!
296                 /*if(te->xend) {
297                         if(te->xend > *w)
298                                 *w = te->xend;
299                 }*/
300                 if(startx+100 > *w)
301                         *w = startx+100;
302
303                 if((tselem->flag & TSE_CLOSED)==0)
304                         outliner_rna_width(soops, &te->subtree, w, startx+OL_X);
305                 te= te->next;
306         }
307 }
308
309 static TreeElement *outliner_find_tree_element(ListBase *lb, int store_index)
310 {
311         TreeElement *te= lb->first, *tes;
312         while(te) {
313                 if(te->store_index==store_index) return te;
314                 tes= outliner_find_tree_element(&te->subtree, store_index);
315                 if(tes) return tes;
316                 te= te->next;
317         }
318         return NULL;
319 }
320
321
322
323 static ID *outliner_search_back(SpaceOops *soops, TreeElement *te, short idcode)
324 {
325         TreeStoreElem *tselem;
326         te= te->parent;
327         
328         while(te) {
329                 tselem= TREESTORE(te);
330                 if(tselem->type==0 && te->idcode==idcode) return tselem->id;
331                 te= te->parent;
332         }
333         return NULL;
334 }
335
336 struct treesort {
337         TreeElement *te;
338         ID *id;
339         char *name;
340         short idcode;
341 };
342
343 static int treesort_alpha(const void *v1, const void *v2)
344 {
345         const struct treesort *x1= v1, *x2= v2;
346         int comp;
347         
348         /* first put objects last (hierarchy) */
349         comp= (x1->idcode==ID_OB);
350         if(x2->idcode==ID_OB) comp+=2;
351         
352         if(comp==1) return 1;
353         else if(comp==2) return -1;
354         else if(comp==3) {
355                 int comp= strcmp(x1->name, x2->name);
356                 
357                 if( comp>0 ) return 1;
358                 else if( comp<0) return -1;
359                 return 0;
360         }
361         return 0;
362 }
363
364 /* this is nice option for later? doesnt look too useful... */
365 #if 0
366 static int treesort_obtype_alpha(const void *v1, const void *v2)
367 {
368         const struct treesort *x1= v1, *x2= v2;
369         
370         /* first put objects last (hierarchy) */
371         if(x1->idcode==ID_OB && x2->idcode!=ID_OB) return 1;
372         else if(x2->idcode==ID_OB && x1->idcode!=ID_OB) return -1;
373         else {
374                 /* 2nd we check ob type */
375                 if(x1->idcode==ID_OB && x2->idcode==ID_OB) {
376                         if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return 1;
377                         else if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
378                         else return 0;
379                 }
380                 else {
381                         int comp= strcmp(x1->name, x2->name);
382                         
383                         if( comp>0 ) return 1;
384                         else if( comp<0) return -1;
385                         return 0;
386                 }
387         }
388 }
389 #endif
390
391 /* sort happens on each subtree individual */
392 static void outliner_sort(SpaceOops *soops, ListBase *lb)
393 {
394         TreeElement *te;
395         TreeStoreElem *tselem;
396         int totelem=0;
397         
398         te= lb->last;
399         if(te==NULL) return;
400         tselem= TREESTORE(te);
401         
402         /* sorting rules; only object lists or deformgroups */
403         if( (tselem->type==TSE_DEFGROUP) || (tselem->type==0 && te->idcode==ID_OB)) {
404                 
405                 /* count first */
406                 for(te= lb->first; te; te= te->next) totelem++;
407                 
408                 if(totelem>1) {
409                         struct treesort *tear= MEM_mallocN(totelem*sizeof(struct treesort), "tree sort array");
410                         struct treesort *tp=tear;
411                         int skip= 0;
412                         
413                         for(te= lb->first; te; te= te->next, tp++) {
414                                 tselem= TREESTORE(te);
415                                 tp->te= te;
416                                 tp->name= te->name;
417                                 tp->idcode= te->idcode;
418                                 if(tselem->type && tselem->type!=TSE_DEFGROUP) tp->idcode= 0;   // dont sort this
419                                 tp->id= tselem->id;
420                         }
421                         /* keep beginning of list */
422                         for(tp= tear, skip=0; skip<totelem; skip++, tp++)
423                                 if(tp->idcode) break;
424                         
425                         if(skip<totelem)
426                                 qsort(tear+skip, totelem-skip, sizeof(struct treesort), treesort_alpha);
427                         
428                         lb->first=lb->last= NULL;
429                         tp= tear;
430                         while(totelem--) {
431                                 BLI_addtail(lb, tp->te);
432                                 tp++;
433                         }
434                         MEM_freeN(tear);
435                 }
436         }
437         
438         for(te= lb->first; te; te= te->next) {
439                 outliner_sort(soops, &te->subtree);
440         }
441 }
442
443 /* Prototype, see functions below */
444 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
445                                                                                  TreeElement *parent, short type, short index);
446
447
448 static void outliner_add_passes(SpaceOops *soops, TreeElement *tenla, ID *id, SceneRenderLayer *srl)
449 {
450         TreeStoreElem *tselem= TREESTORE(tenla);
451         TreeElement *te;
452         
453         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_COMBINED);
454         te->name= "Combined";
455         te->directdata= &srl->passflag;
456         
457         /* save cpu cycles, but we add the first to invoke an open/close triangle */
458         if(tselem->flag & TSE_CLOSED)
459                 return;
460         
461         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_Z);
462         te->name= "Z";
463         te->directdata= &srl->passflag;
464         
465         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_VECTOR);
466         te->name= "Vector";
467         te->directdata= &srl->passflag;
468         
469         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_NORMAL);
470         te->name= "Normal";
471         te->directdata= &srl->passflag;
472         
473         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_UV);
474         te->name= "UV";
475         te->directdata= &srl->passflag;
476         
477         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_MIST);
478         te->name= "Mist";
479         te->directdata= &srl->passflag;
480         
481         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_INDEXOB);
482         te->name= "Index Object";
483         te->directdata= &srl->passflag;
484         
485         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_RGBA);
486         te->name= "Color";
487         te->directdata= &srl->passflag;
488         
489         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_DIFFUSE);
490         te->name= "Diffuse";
491         te->directdata= &srl->passflag;
492         
493         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_SPEC);
494         te->name= "Specular";
495         te->directdata= &srl->passflag;
496         
497         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_SHADOW);
498         te->name= "Shadow";
499         te->directdata= &srl->passflag;
500         
501         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_AO);
502         te->name= "AO";
503         te->directdata= &srl->passflag;
504         
505         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_REFLECT);
506         te->name= "Reflection";
507         te->directdata= &srl->passflag;
508         
509         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_REFRACT);
510         te->name= "Refraction";
511         te->directdata= &srl->passflag;
512         
513         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_RADIO);
514         te->name= "Radiosity";
515         te->directdata= &srl->passflag;
516         
517 }
518
519
520 /* special handling of hierarchical non-lib data */
521 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone, 
522                                                           TreeElement *parent, int *a)
523 {
524         TreeElement *te= outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
525         
526         (*a)++;
527         te->name= curBone->name;
528         te->directdata= curBone;
529         
530         for(curBone= curBone->childbase.first; curBone; curBone=curBone->next) {
531                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
532         }
533 }
534
535 static void outliner_add_scene_contents(SpaceOops *soops, ListBase *lb, Scene *sce, TreeElement *te)
536 {
537         SceneRenderLayer *srl;
538         TreeElement *tenla= outliner_add_element(soops, lb, sce, te, TSE_R_LAYER_BASE, 0);
539         int a;
540         
541         tenla->name= "RenderLayers";
542         for(a=0, srl= sce->r.layers.first; srl; srl= srl->next, a++) {
543                 TreeElement *tenlay= outliner_add_element(soops, &tenla->subtree, sce, te, TSE_R_LAYER, a);
544                 tenlay->name= srl->name;
545                 tenlay->directdata= &srl->passflag;
546                 
547                 if(srl->light_override)
548                         outliner_add_element(soops, &tenlay->subtree, srl->light_override, tenlay, TSE_LINKED_LAMP, 0);
549                 if(srl->mat_override)
550                         outliner_add_element(soops, &tenlay->subtree, srl->mat_override, tenlay, TSE_LINKED_MAT, 0);
551                 
552                 outliner_add_passes(soops, tenlay, &sce->id, srl);
553         }
554         
555         outliner_add_element(soops,  lb, sce->world, te, 0, 0);
556 }
557
558 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
559                                                                                  TreeElement *parent, short type, short index)
560 {
561         TreeElement *te;
562         TreeStoreElem *tselem;
563         ID *id= idv;
564         int a = 0;
565         
566         if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
567                 id= ((PointerRNA*)idv)->id.data;
568                 if(!id) id= ((PointerRNA*)idv)->data;
569         }
570
571         if(id==NULL) return NULL;
572
573         te= MEM_callocN(sizeof(TreeElement), "tree elem");
574         /* add to the visual tree */
575         BLI_addtail(lb, te);
576         /* add to the storage */
577         check_persistant(soops, te, id, type, index);
578         tselem= TREESTORE(te);  
579         
580         te->parent= parent;
581         te->index= index;       // for data arays
582         if(ELEM3(type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP));
583         else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM));
584         else if(type==TSE_ANIM_DATA);
585         else {
586                 te->name= id->name+2; // default, can be overridden by Library or non-ID data
587                 te->idcode= GS(id->name);
588         }
589         
590         if(type==0) {
591
592                 /* tuck pointer back in object, to construct hierarchy */
593                 if(GS(id->name)==ID_OB) id->newid= (ID *)te;
594                 
595                 /* expand specific data always */
596                 switch(GS(id->name)) {
597                 case ID_LI:
598                         te->name= ((Library *)id)->name;
599                         break;
600                 case ID_SCE:
601                         outliner_add_scene_contents(soops, &te->subtree, (Scene *)id, te);
602                         break;
603                 case ID_OB:
604                         {
605                                 Object *ob= (Object *)id;
606                                 
607                                 outliner_add_element(soops, &te->subtree, ob->adt, te, TSE_ANIM_DATA, 0);
608                                 outliner_add_element(soops, &te->subtree, ob->poselib, te, 0, 0); // XXX FIXME.. add a special type for this
609                                 
610                                 if(ob->proxy && ob->id.lib==NULL)
611                                         outliner_add_element(soops, &te->subtree, ob->proxy, te, TSE_PROXY, 0);
612                                 
613                                 outliner_add_element(soops, &te->subtree, ob->data, te, 0, 0);
614                                 
615                                 if(ob->pose) {
616                                         bArmature *arm= ob->data;
617                                         bPoseChannel *pchan;
618                                         TreeElement *ten;
619                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSE_BASE, 0);
620                                         
621                                         tenla->name= "Pose";
622                                         
623                                         if(arm->edbo==NULL && (ob->mode & OB_MODE_POSE)) {      // channels undefined in editmode, but we want the 'tenla' pose icon itself
624                                                 int a= 0, const_index= 1000;    /* ensure unique id for bone constraints */
625                                                 
626                                                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
627                                                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSE_CHANNEL, a);
628                                                         ten->name= pchan->name;
629                                                         ten->directdata= pchan;
630                                                         pchan->prev= (bPoseChannel *)ten;
631                                                         
632                                                         if(pchan->constraints.first) {
633                                                                 //Object *target;
634                                                                 bConstraint *con;
635                                                                 TreeElement *ten1;
636                                                                 TreeElement *tenla1= outliner_add_element(soops, &ten->subtree, ob, ten, TSE_CONSTRAINT_BASE, 0);
637                                                                 //char *str;
638                                                                 
639                                                                 tenla1->name= "Constraints";
640                                                                 for(con= pchan->constraints.first; con; con= con->next, const_index++) {
641                                                                         ten1= outliner_add_element(soops, &tenla1->subtree, ob, tenla1, TSE_CONSTRAINT, const_index);
642 #if 0 /* disabled as it needs to be reworked for recoded constraints system */
643                                                                         target= get_constraint_target(con, &str);
644                                                                         if(str && str[0]) ten1->name= str;
645                                                                         else if(target) ten1->name= target->id.name+2;
646                                                                         else ten1->name= con->name;
647 #endif
648                                                                         ten1->name= con->name;
649                                                                         ten1->directdata= con;
650                                                                         /* possible add all other types links? */
651                                                                 }
652                                                         }
653                                                 }
654                                                 /* make hierarchy */
655                                                 ten= tenla->subtree.first;
656                                                 while(ten) {
657                                                         TreeElement *nten= ten->next, *par;
658                                                         tselem= TREESTORE(ten);
659                                                         if(tselem->type==TSE_POSE_CHANNEL) {
660                                                                 pchan= (bPoseChannel *)ten->directdata;
661                                                                 if(pchan->parent) {
662                                                                         BLI_remlink(&tenla->subtree, ten);
663                                                                         par= (TreeElement *)pchan->parent->prev;
664                                                                         BLI_addtail(&par->subtree, ten);
665                                                                         ten->parent= par;
666                                                                 }
667                                                         }
668                                                         ten= nten;
669                                                 }
670                                                 /* restore prev pointers */
671                                                 pchan= ob->pose->chanbase.first;
672                                                 if(pchan) pchan->prev= NULL;
673                                                 for(; pchan; pchan= pchan->next) {
674                                                         if(pchan->next) pchan->next->prev= pchan;
675                                                 }
676                                         }
677                                         
678                                         /* Pose Groups */
679                                         if(ob->pose->agroups.first) {
680                                                 bActionGroup *agrp;
681                                                 TreeElement *ten;
682                                                 TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSEGRP_BASE, 0);
683                                                 int a= 0;
684                                                 
685                                                 tenla->name= "Bone Groups";
686                                                 for (agrp=ob->pose->agroups.first; agrp; agrp=agrp->next, a++) {
687                                                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSEGRP, a);
688                                                         ten->name= agrp->name;
689                                                         ten->directdata= agrp;
690                                                 }
691                                         }
692                                 }
693                                 
694                                 for(a=0; a<ob->totcol; a++) 
695                                         outliner_add_element(soops, &te->subtree, ob->mat[a], te, 0, a);
696                                 
697                                 if(ob->constraints.first) {
698                                         //Object *target;
699                                         bConstraint *con;
700                                         TreeElement *ten;
701                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_CONSTRAINT_BASE, 0);
702                                         int a= 0;
703                                         //char *str;
704                                         
705                                         tenla->name= "Constraints";
706                                         for(con= ob->constraints.first; con; con= con->next, a++) {
707                                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_CONSTRAINT, a);
708 #if 0 /* disabled due to constraints system targets recode... code here needs review */
709                                                 target= get_constraint_target(con, &str);
710                                                 if(str && str[0]) ten->name= str;
711                                                 else if(target) ten->name= target->id.name+2;
712                                                 else ten->name= con->name;
713 #endif
714                                                 ten->name= con->name;
715                                                 ten->directdata= con;
716                                                 /* possible add all other types links? */
717                                         }
718                                 }
719                                 
720                                 if(ob->modifiers.first) {
721                                         ModifierData *md;
722                                         TreeElement *temod = outliner_add_element(soops, &te->subtree, ob, te, TSE_MODIFIER_BASE, 0);
723                                         int index;
724
725                                         temod->name = "Modifiers";
726                                         for (index=0,md=ob->modifiers.first; md; index++,md=md->next) {
727                                                 TreeElement *te = outliner_add_element(soops, &temod->subtree, ob, temod, TSE_MODIFIER, index);
728                                                 te->name= md->name;
729                                                 te->directdata = md;
730
731                                                 if (md->type==eModifierType_Lattice) {
732                                                         outliner_add_element(soops, &te->subtree, ((LatticeModifierData*) md)->object, te, TSE_LINKED_OB, 0);
733                                                 } else if (md->type==eModifierType_Curve) {
734                                                         outliner_add_element(soops, &te->subtree, ((CurveModifierData*) md)->object, te, TSE_LINKED_OB, 0);
735                                                 } else if (md->type==eModifierType_Armature) {
736                                                         outliner_add_element(soops, &te->subtree, ((ArmatureModifierData*) md)->object, te, TSE_LINKED_OB, 0);
737                                                 } else if (md->type==eModifierType_Hook) {
738                                                         outliner_add_element(soops, &te->subtree, ((HookModifierData*) md)->object, te, TSE_LINKED_OB, 0);
739                                                 } else if (md->type==eModifierType_ParticleSystem) {
740                                                         TreeElement *ten;
741                                                         ParticleSystem *psys= ((ParticleSystemModifierData*) md)->psys;
742                                                         
743                                                         ten = outliner_add_element(soops, &te->subtree, ob, te, TSE_LINKED_PSYS, 0);
744                                                         ten->directdata = psys;
745                                                         ten->name = psys->part->id.name+2;
746                                                 }
747                                         }
748                                 }
749                                 if(ob->defbase.first) {
750                                         bDeformGroup *defgroup;
751                                         TreeElement *ten;
752                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_DEFGROUP_BASE, 0);
753                                         int a= 0;
754                                         
755                                         tenla->name= "Vertex Groups";
756                                         for (defgroup=ob->defbase.first; defgroup; defgroup=defgroup->next, a++) {
757                                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_DEFGROUP, a);
758                                                 ten->name= defgroup->name;
759                                                 ten->directdata= defgroup;
760                                         }
761                                 }
762                                 
763                                 if(ob->dup_group)
764                                         outliner_add_element(soops, &te->subtree, ob->dup_group, te, 0, 0);     
765                                 
766                         }
767                         break;
768                 case ID_ME:
769                         {
770                                 Mesh *me= (Mesh *)id;
771                                 
772                                 //outliner_add_element(soops, &te->subtree, me->adt, te, TSE_ANIM_DATA, 0);
773                                 
774                                 outliner_add_element(soops, &te->subtree, me->key, te, 0, 0);
775                                 for(a=0; a<me->totcol; a++) 
776                                         outliner_add_element(soops, &te->subtree, me->mat[a], te, 0, a);
777                                 /* could do tfaces with image links, but the images are not grouped nicely.
778                                    would require going over all tfaces, sort images in use. etc... */
779                         }
780                         break;
781                 case ID_CU:
782                         {
783                                 Curve *cu= (Curve *)id;
784                                 
785                                 outliner_add_element(soops, &te->subtree, cu->adt, te, TSE_ANIM_DATA, 0);
786                                 
787                                 for(a=0; a<cu->totcol; a++) 
788                                         outliner_add_element(soops, &te->subtree, cu->mat[a], te, 0, a);
789                         }
790                         break;
791                 case ID_MB:
792                         {
793                                 MetaBall *mb= (MetaBall *)id;
794                                 for(a=0; a<mb->totcol; a++) 
795                                         outliner_add_element(soops, &te->subtree, mb->mat[a], te, 0, a);
796                         }
797                         break;
798                 case ID_MA:
799                 {
800                         Material *ma= (Material *)id;
801                         
802                         outliner_add_element(soops, &te->subtree, ma->adt, te, TSE_ANIM_DATA, 0);
803                         
804                         for(a=0; a<MAX_MTEX; a++) {
805                                 if(ma->mtex[a]) outliner_add_element(soops, &te->subtree, ma->mtex[a]->tex, te, 0, a);
806                         }
807                 }
808                         break;
809                 case ID_TE:
810                         {
811                                 Tex *tex= (Tex *)id;
812                                 
813                                 outliner_add_element(soops, &te->subtree, tex->adt, te, TSE_ANIM_DATA, 0);
814                                 outliner_add_element(soops, &te->subtree, tex->ima, te, 0, 0);
815                         }
816                         break;
817                 case ID_CA:
818                         {
819                                 Camera *ca= (Camera *)id;
820                                 outliner_add_element(soops, &te->subtree, ca->adt, te, TSE_ANIM_DATA, 0);
821                         }
822                         break;
823                 case ID_LA:
824                         {
825                                 Lamp *la= (Lamp *)id;
826                                 
827                                 outliner_add_element(soops, &te->subtree, la->adt, te, TSE_ANIM_DATA, 0);
828                                 
829                                 for(a=0; a<MAX_MTEX; a++) {
830                                         if(la->mtex[a]) outliner_add_element(soops, &te->subtree, la->mtex[a]->tex, te, 0, a);
831                                 }
832                         }
833                         break;
834                 case ID_WO:
835                         {
836                                 World *wrld= (World *)id;
837                                 
838                                 outliner_add_element(soops, &te->subtree, wrld->adt, te, TSE_ANIM_DATA, 0);
839                                 
840                                 for(a=0; a<MAX_MTEX; a++) {
841                                         if(wrld->mtex[a]) outliner_add_element(soops, &te->subtree, wrld->mtex[a]->tex, te, 0, a);
842                                 }
843                         }
844                         break;
845                 case ID_KE:
846                         {
847                                 Key *key= (Key *)id;
848                                 
849                                 outliner_add_element(soops, &te->subtree, key->adt, te, TSE_ANIM_DATA, 0);
850                         }
851                         break;
852                 case ID_AC:
853                         {
854                                 // XXX do we want to be exposing the F-Curves here?
855                                 //bAction *act= (bAction *)id;
856                         }
857                         break;
858                 case ID_AR:
859                         {
860                                 bArmature *arm= (bArmature *)id;
861                                 int a= 0;
862                                 
863                                 if(arm->edbo) {
864                                         EditBone *ebone;
865                                         TreeElement *ten;
866                                         
867                                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next, a++) {
868                                                 ten= outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
869                                                 ten->directdata= ebone;
870                                                 ten->name= ebone->name;
871                                                 ebone->temp= ten;
872                                         }
873                                         /* make hierarchy */
874                                         ten= te->subtree.first;
875                                         while(ten) {
876                                                 TreeElement *nten= ten->next, *par;
877                                                 ebone= (EditBone *)ten->directdata;
878                                                 if(ebone->parent) {
879                                                         BLI_remlink(&te->subtree, ten);
880                                                         par= ebone->parent->temp;
881                                                         BLI_addtail(&par->subtree, ten);
882                                                         ten->parent= par;
883                                                 }
884                                                 ten= nten;
885                                         }
886                                 }
887                                 else {
888                                         /* do not extend Armature when we have posemode */
889                                         tselem= TREESTORE(te->parent);
890                                         if( GS(tselem->id->name)==ID_OB && ((Object *)tselem->id)->mode & OB_MODE_POSE);
891                                         else {
892                                                 Bone *curBone;
893                                                 for (curBone=arm->bonebase.first; curBone; curBone=curBone->next){
894                                                         outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
895                                                 }
896                                         }
897                                 }
898                         }
899                         break;
900                 }
901         }
902         else if(type==TSE_ANIM_DATA) {
903                 AnimData *adt= (AnimData *)idv;
904                 
905                 /* this element's info */
906                 te->name= "Animation";
907                 
908                 /* Action */
909                 outliner_add_element(soops, &te->subtree, adt->action, te, 0, 0);
910                 
911                 /* Drivers */
912                 if (adt->drivers.first) {
913                         TreeElement *ted= outliner_add_element(soops, &te->subtree, adt, te, TSE_DRIVER_BASE, 0);
914                         ID *lastadded= NULL;
915                         FCurve *fcu;
916                         DriverTarget *dtar;
917                         
918                         ted->name= "Drivers";
919                 
920                         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
921                                 if (fcu->driver && fcu->driver->targets.first)  {
922                                         for (dtar= fcu->driver->targets.first; dtar; dtar= dtar->next) {
923                                                 if (lastadded != dtar->id) {
924                                                         // XXX this lastadded check is rather lame, and also fails quite badly...
925                                                         outliner_add_element(soops, &ted->subtree, dtar->id, ted, TSE_LINKED_OB, 0);
926                                                         lastadded= dtar->id;
927                                                 }
928                                         }
929                                 }
930                         }
931                 }
932                 
933                 /* NLA Data */
934                 if (adt->nla_tracks.first) {
935                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, adt, te, TSE_NLA, 0);
936                         NlaTrack *nlt;
937                         int a= 0;
938                         
939                         tenla->name= "NLA Tracks";
940                         
941                         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
942                                 TreeElement *tenlt= outliner_add_element(soops, &tenla->subtree, nlt, tenla, TSE_NLA_TRACK, a);
943                                 NlaStrip *strip;
944                                 TreeElement *ten;
945                                 int b= 0;
946                                 
947                                 tenlt->name= nlt->name;
948                                 
949                                 for (strip=nlt->strips.first; strip; strip=strip->next, b++) {
950                                         ten= outliner_add_element(soops, &tenlt->subtree, strip->act, tenlt, TSE_NLA_ACTION, b);
951                                         if(ten) ten->directdata= strip;
952                                 }
953                         }
954                 }
955         }
956         else if(type==TSE_SEQUENCE) {
957                 Sequence *seq= (Sequence*) idv;
958                 Sequence *p;
959
960                 /*
961                  * The idcode is a little hack, but the outliner
962                  * only check te->idcode if te->type is equal to zero,
963                  * so this is "safe".
964                  */
965                 te->idcode= seq->type;
966                 te->directdata= seq;
967
968                 if(seq->type<7) {
969                         /*
970                          * This work like the sequence.
971                          * If the sequence have a name (not default name)
972                          * show it, in other case put the filename.
973                          */
974                         if(strcmp(seq->name, "SQ"))
975                                 te->name= seq->name;
976                         else {
977                                 if((seq->strip) && (seq->strip->stripdata))
978                                         te->name= seq->strip->stripdata->name;
979                                 else if((seq->strip) && (seq->strip->tstripdata) && (seq->strip->tstripdata->ibuf))
980                                         te->name= seq->strip->tstripdata->ibuf->name;
981                                 else
982                                         te->name= "SQ None";
983                         }
984
985                         if(seq->type==SEQ_META) {
986                                 te->name= "Meta Strip";
987                                 p= seq->seqbase.first;
988                                 while(p) {
989                                         outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
990                                         p= p->next;
991                                 }
992                         }
993                         else
994                                 outliner_add_element(soops, &te->subtree, (void*)seq->strip, te, TSE_SEQ_STRIP, index);
995                 }
996                 else
997                         te->name= "Effect";
998         }
999         else if(type==TSE_SEQ_STRIP) {
1000                 Strip *strip= (Strip *)idv;
1001
1002                 if(strip->dir)
1003                         te->name= strip->dir;
1004                 else
1005                         te->name= "Strip None";
1006                 te->directdata= strip;
1007         }
1008         else if(type==TSE_SEQUENCE_DUP) {
1009                 Sequence *seq= (Sequence*)idv;
1010
1011                 te->idcode= seq->type;
1012                 te->directdata= seq;
1013                 te->name= seq->strip->stripdata->name;
1014         }
1015         else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
1016                 PointerRNA pptr, propptr, *ptr= (PointerRNA*)idv;
1017                 PropertyRNA *prop, *iterprop;
1018                 PropertyType proptype;
1019                 int a, tot;
1020
1021                 /* we do lazy build, for speed and to avoid infinite recusion */
1022
1023                 if(ptr->data == NULL) {
1024                         te->name= "(empty)";
1025                 }
1026                 else if(type == TSE_RNA_STRUCT) {
1027                         /* struct */
1028                         te->name= RNA_struct_name_get_alloc(ptr, NULL, 0);
1029
1030                         if(te->name)
1031                                 te->flag |= TE_FREE_NAME;
1032                         else
1033                                 te->name= (char*)RNA_struct_ui_name(ptr->type);
1034
1035                         iterprop= RNA_struct_iterator_property(ptr->type);
1036                         tot= RNA_property_collection_length(ptr, iterprop);
1037
1038                         /* auto open these cases */
1039                         if(!parent || (RNA_property_type(parent->directdata)) == PROP_POINTER)
1040                                 if(!tselem->used)
1041                                         tselem->flag &= ~TSE_CLOSED;
1042
1043                         if(!(tselem->flag & TSE_CLOSED)) {
1044                                 for(a=0; a<tot; a++)
1045                                         outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_PROPERTY, a);
1046                         }
1047                         else if(tot)
1048                                 te->flag |= TE_LAZY_CLOSED;
1049
1050                         te->rnaptr= *ptr;
1051                 }
1052                 else if(type == TSE_RNA_PROPERTY) {
1053                         /* property */
1054                         iterprop= RNA_struct_iterator_property(ptr->type);
1055                         RNA_property_collection_lookup_int(ptr, iterprop, index, &propptr);
1056
1057                         prop= propptr.data;
1058                         proptype= RNA_property_type(prop);
1059
1060                         te->name= (char*)RNA_property_ui_name(prop);
1061                         te->directdata= prop;
1062                         te->rnaptr= *ptr;
1063
1064                         if(proptype == PROP_POINTER) {
1065                                 pptr= RNA_property_pointer_get(ptr, prop);
1066
1067                                 if(pptr.data) {
1068                                         if(!(tselem->flag & TSE_CLOSED))
1069                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, -1);
1070                                         else
1071                                                 te->flag |= TE_LAZY_CLOSED;
1072                                 }
1073                         }
1074                         else if(proptype == PROP_COLLECTION) {
1075                                 tot= RNA_property_collection_length(ptr, prop);
1076
1077                                 if(!(tselem->flag & TSE_CLOSED)) {
1078                                         for(a=0; a<tot; a++) {
1079                                                 RNA_property_collection_lookup_int(ptr, prop, a, &pptr);
1080                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, -1);
1081                                         }
1082                                 }
1083                                 else if(tot)
1084                                         te->flag |= TE_LAZY_CLOSED;
1085                         }
1086                         else if(ELEM3(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1087                                 tot= RNA_property_array_length(ptr, prop);
1088
1089                                 if(!(tselem->flag & TSE_CLOSED)) {
1090                                         for(a=0; a<tot; a++)
1091                                                 outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_ARRAY_ELEM, a);
1092                                 }
1093                                 else if(tot)
1094                                         te->flag |= TE_LAZY_CLOSED;
1095                         }
1096                 }
1097                 else if(type == TSE_RNA_ARRAY_ELEM) {
1098                         char c;
1099
1100                         prop= parent->directdata;
1101
1102                         te->directdata= prop;
1103                         te->rnaptr= *ptr;
1104                         te->index= index;
1105
1106                         c= RNA_property_array_item_char(prop, index);
1107
1108                         te->name= MEM_callocN(sizeof(char)*20, "OutlinerRNAArrayName");
1109                         if(c) sprintf(te->name, "  %c", c);
1110                         else sprintf(te->name, "  %d", index+1);
1111                         te->flag |= TE_FREE_NAME;
1112                 }
1113         }
1114         else if(type == TSE_KEYMAP) {
1115                 wmKeyMap *km= (wmKeyMap *)idv;
1116                 wmKeymapItem *kmi;
1117                 char opname[OP_MAX_TYPENAME];
1118                 
1119                 te->directdata= idv;
1120                 te->name= km->nameid;
1121                 
1122                 if(!(tselem->flag & TSE_CLOSED)) {
1123                         a= 0;
1124                         
1125                         for (kmi= km->keymap.first; kmi; kmi= kmi->next, a++) {
1126                                 const char *key= WM_key_event_string(kmi->type);
1127                                 
1128                                 if(key[0]) {
1129                                         wmOperatorType *ot= NULL;
1130                                         
1131                                         if(kmi->propvalue);
1132                                         else ot= WM_operatortype_find(kmi->idname, 0);
1133                                         
1134                                         if(ot || kmi->propvalue) {
1135                                                 TreeElement *ten= outliner_add_element(soops, &te->subtree, kmi, te, TSE_KEYMAP_ITEM, a);
1136                                                 
1137                                                 ten->directdata= kmi;
1138                                                 
1139                                                 if(kmi->propvalue) {
1140                                                         ten->name= "Modal map, not yet";
1141                                                 }
1142                                                 else {
1143                                                         WM_operator_py_idname(opname, ot->idname);
1144                                                         ten->name= BLI_strdup(opname);
1145                                                         ten->flag |= TE_FREE_NAME;
1146                                                 }
1147                                         }
1148                                 }
1149                         }
1150                 }
1151                 else 
1152                         te->flag |= TE_LAZY_CLOSED;
1153         }
1154
1155         return te;
1156 }
1157
1158 static void outliner_make_hierarchy(SpaceOops *soops, ListBase *lb)
1159 {
1160         TreeElement *te, *ten, *tep;
1161         TreeStoreElem *tselem;
1162
1163         /* build hierarchy */
1164         // XXX also, set extents here...
1165         te= lb->first;
1166         while(te) {
1167                 ten= te->next;
1168                 tselem= TREESTORE(te);
1169                 
1170                 if(tselem->type==0 && te->idcode==ID_OB) {
1171                         Object *ob= (Object *)tselem->id;
1172                         if(ob->parent && ob->parent->id.newid) {
1173                                 BLI_remlink(lb, te);
1174                                 tep= (TreeElement *)ob->parent->id.newid;
1175                                 BLI_addtail(&tep->subtree, te);
1176                                 // set correct parent pointers
1177                                 for(te=tep->subtree.first; te; te= te->next) te->parent= tep;
1178                         }
1179                 }
1180                 te= ten;
1181         }
1182 }
1183
1184 /* Helped function to put duplicate sequence in the same tree. */
1185 int need_add_seq_dup(Sequence *seq)
1186 {
1187         Sequence *p;
1188
1189         if((!seq->strip) || (!seq->strip->stripdata) || (!seq->strip->stripdata->name))
1190                 return(1);
1191
1192         /*
1193          * First check backward, if we found a duplicate
1194          * sequence before this, don't need it, just return.
1195          */
1196         p= seq->prev;
1197         while(p) {
1198                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1199                         p= p->prev;
1200                         continue;
1201                 }
1202
1203                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1204                         return(2);
1205                 p= p->prev;
1206         }
1207
1208         p= seq->next;
1209         while(p) {
1210                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1211                         p= p->next;
1212                         continue;
1213                 }
1214
1215                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1216                         return(0);
1217                 p= p->next;
1218         }
1219         return(1);
1220 }
1221
1222 void add_seq_dup(SpaceOops *soops, Sequence *seq, TreeElement *te, short index)
1223 {
1224         TreeElement *ch;
1225         Sequence *p;
1226
1227         p= seq;
1228         while(p) {
1229                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1230                         p= p->next;
1231                         continue;
1232                 }
1233
1234                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1235                         ch= outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
1236                 p= p->next;
1237         }
1238 }
1239
1240 static void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops)
1241 {
1242         Base *base;
1243         Object *ob;
1244         TreeElement *te=NULL, *ten;
1245         TreeStoreElem *tselem;
1246         int show_opened= (soops->treestore==NULL); /* on first view, we open scenes */
1247
1248         if(soops->tree.first && (soops->storeflag & SO_TREESTORE_REDRAW))
1249            return;
1250            
1251         outliner_free_tree(&soops->tree);
1252         outliner_storage_cleanup(soops);
1253         
1254         /* clear ob id.new flags */
1255         for(ob= mainvar->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
1256         
1257         /* options */
1258         if(soops->outlinevis == SO_LIBRARIES) {
1259                 Library *lib;
1260                 
1261                 for(lib= mainvar->library.first; lib; lib= lib->id.next) {
1262                         ten= outliner_add_element(soops, &soops->tree, lib, NULL, 0, 0);
1263                         lib->id.newid= (ID *)ten;
1264                 }
1265                 /* make hierarchy */
1266                 ten= soops->tree.first;
1267                 while(ten) {
1268                         TreeElement *nten= ten->next, *par;
1269                         tselem= TREESTORE(ten);
1270                         lib= (Library *)tselem->id;
1271                         if(lib->parent) {
1272                                 BLI_remlink(&soops->tree, ten);
1273                                 par= (TreeElement *)lib->parent->id.newid;
1274                                 BLI_addtail(&par->subtree, ten);
1275                                 ten->parent= par;
1276                         }
1277                         ten= nten;
1278                 }
1279                 /* restore newid pointers */
1280                 for(lib= mainvar->library.first; lib; lib= lib->id.next)
1281                         lib->id.newid= NULL;
1282                 
1283         }
1284         else if(soops->outlinevis == SO_ALL_SCENES) {
1285                 Scene *sce;
1286                 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
1287                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
1288                         tselem= TREESTORE(te);
1289                         if(sce==scene && show_opened) 
1290                                 tselem->flag &= ~TSE_CLOSED;
1291                         
1292                         for(base= sce->base.first; base; base= base->next) {
1293                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
1294                                 ten->directdata= base;
1295                         }
1296                         outliner_make_hierarchy(soops, &te->subtree);
1297                         /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1298                         for(base= sce->base.first; base; base= base->next) base->object->id.newid= NULL;
1299                 }
1300         }
1301         else if(soops->outlinevis == SO_CUR_SCENE) {
1302                 
1303                 outliner_add_scene_contents(soops, &soops->tree, scene, NULL);
1304                 
1305                 for(base= scene->base.first; base; base= base->next) {
1306                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1307                         ten->directdata= base;
1308                 }
1309                 outliner_make_hierarchy(soops, &soops->tree);
1310         }
1311         else if(soops->outlinevis == SO_VISIBLE) {
1312                 for(base= scene->base.first; base; base= base->next) {
1313                         if(base->lay & scene->lay)
1314                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1315                 }
1316                 outliner_make_hierarchy(soops, &soops->tree);
1317         }
1318         else if(soops->outlinevis == SO_GROUPS) {
1319                 Group *group;
1320                 GroupObject *go;
1321                 
1322                 for(group= mainvar->group.first; group; group= group->id.next) {
1323                         if(group->id.us) {
1324                                 te= outliner_add_element(soops, &soops->tree, group, NULL, 0, 0);
1325                                 tselem= TREESTORE(te);
1326                                 
1327                                 for(go= group->gobject.first; go; go= go->next) {
1328                                         ten= outliner_add_element(soops, &te->subtree, go->ob, te, 0, 0);
1329                                         ten->directdata= NULL; /* eh, why? */
1330                                 }
1331                                 outliner_make_hierarchy(soops, &te->subtree);
1332                                 /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1333                                 for(go= group->gobject.first; go; go= go->next) go->ob->id.newid= NULL;
1334                         }
1335                 }
1336         }
1337         else if(soops->outlinevis == SO_SAME_TYPE) {
1338                 Object *ob= OBACT;
1339                 if(ob) {
1340                         for(base= scene->base.first; base; base= base->next) {
1341                                 if(base->object->type==ob->type) {
1342                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1343                                         ten->directdata= base;
1344                                 }
1345                         }
1346                         outliner_make_hierarchy(soops, &soops->tree);
1347                 }
1348         }
1349         else if(soops->outlinevis == SO_SELECTED) {
1350                 for(base= scene->base.first; base; base= base->next) {
1351                         if(base->lay & scene->lay) {
1352                                 if(base==BASACT || (base->flag & SELECT)) {
1353                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1354                                         ten->directdata= base;
1355                                 }
1356                         }
1357                 }
1358                 outliner_make_hierarchy(soops, &soops->tree);
1359         }
1360         else if(soops->outlinevis==SO_SEQUENCE) {
1361                 Sequence *seq;
1362                 Editing *ed= seq_give_editing(scene, FALSE);
1363                 int op;
1364
1365                 if(ed==NULL)
1366                         return;
1367
1368                 seq= ed->seqbasep->first;
1369                 if(!seq)
1370                         return;
1371
1372                 while(seq) {
1373                         op= need_add_seq_dup(seq);
1374                         if(op==1)
1375                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1376                         else if(op==0) {
1377                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1378                                 add_seq_dup(soops, seq, ten, 0);
1379                         }
1380                         seq= seq->next;
1381                 }
1382         }
1383         else if(soops->outlinevis==SO_DATABLOCKS) {
1384                 PointerRNA mainptr;
1385
1386                 RNA_main_pointer_create(mainvar, &mainptr);
1387
1388                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1389
1390                 if(show_opened)  {
1391                         tselem= TREESTORE(ten);
1392                         tselem->flag &= ~TSE_CLOSED;
1393                 }
1394         }
1395         else if(soops->outlinevis==SO_USERDEF) {
1396                 PointerRNA userdefptr;
1397
1398                 RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &userdefptr);
1399
1400                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1401
1402                 if(show_opened)  {
1403                         tselem= TREESTORE(ten);
1404                         tselem->flag &= ~TSE_CLOSED;
1405                 }
1406         }
1407         else if(soops->outlinevis==SO_KEYMAP) {
1408                 wmWindowManager *wm= mainvar->wm.first;
1409                 wmKeyMap *km;
1410                 
1411                 for(km= wm->keymaps.first; km; km= km->next) {
1412                         ten= outliner_add_element(soops, &soops->tree, (void*)km, NULL, TSE_KEYMAP, 0);
1413                 }
1414         }
1415         else {
1416                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1417                 if(ten) ten->directdata= BASACT;
1418         }
1419
1420         outliner_sort(soops, &soops->tree);
1421 }
1422
1423 /* **************** INTERACTIVE ************* */
1424
1425 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
1426 {
1427         TreeElement *te;
1428         int level=curlevel, lev;
1429         
1430         for(te= lb->first; te; te= te->next) {
1431                 
1432                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
1433                 if(lev>level) level= lev;
1434         }
1435         return level;
1436 }
1437
1438 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
1439 {
1440         TreeElement *te;
1441         TreeStoreElem *tselem;
1442         int level;
1443         
1444         for(te= lb->first; te; te= te->next) {
1445                 tselem= TREESTORE(te);
1446                 if(tselem->flag & flag) return curlevel;
1447                 
1448                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
1449                 if(level) return level;
1450         }
1451         return 0;
1452 }
1453
1454 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
1455 {
1456         TreeElement *te;
1457         TreeStoreElem *tselem;
1458         
1459         for(te= lb->first; te; te= te->next) {
1460                 tselem= TREESTORE(te);
1461                 if(set==0) tselem->flag &= ~flag;
1462                 else tselem->flag |= flag;
1463                 outliner_set_flag(soops, &te->subtree, flag, set);
1464         }
1465 }
1466
1467 /* --- */
1468
1469 void object_toggle_visibility_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1470 {
1471         Base *base= (Base *)te->directdata;
1472         
1473         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1474         if(base) {
1475                 base->object->restrictflag^=OB_RESTRICT_VIEW;
1476         }
1477 }
1478
1479 static int outliner_toggle_visibility_exec(bContext *C, wmOperator *op)
1480 {
1481         SpaceOops *soops= CTX_wm_space_outliner(C);
1482         Scene *scene= CTX_data_scene(C);
1483         ARegion *ar= CTX_wm_region(C);
1484         
1485         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_visibility_cb);
1486         
1487         ED_region_tag_redraw(ar);
1488         
1489         return OPERATOR_FINISHED;
1490 }
1491
1492 void OUTLINER_OT_visibility_toggle(wmOperatorType *ot)
1493 {
1494         /* identifiers */
1495         ot->name= "Toggle Visability";
1496         ot->idname= "OUTLINER_OT_visibility_toggle";
1497         ot->description= "Toggle the visibility of selected items.";
1498         
1499         /* callbacks */
1500         ot->exec= outliner_toggle_visibility_exec;
1501         ot->poll= ED_operator_outliner_active;
1502         
1503         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1504 }
1505
1506 /* --- */
1507
1508 static void object_toggle_selectability_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1509 {
1510         Base *base= (Base *)te->directdata;
1511         
1512         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1513         if(base) {
1514                 base->object->restrictflag^=OB_RESTRICT_SELECT;
1515         }
1516 }
1517
1518 static int outliner_toggle_selectability_exec(bContext *C, wmOperator *op)
1519 {
1520         SpaceOops *soops= CTX_wm_space_outliner(C);
1521         Scene *scene= CTX_data_scene(C);
1522         ARegion *ar= CTX_wm_region(C);
1523         
1524         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_selectability_cb);
1525         
1526         ED_region_tag_redraw(ar);
1527         
1528         return OPERATOR_FINISHED;
1529 }
1530
1531 void OUTLINER_OT_selectability_toggle(wmOperatorType *ot)
1532 {
1533         /* identifiers */
1534         ot->name= "Toggle Selectability";
1535         ot->idname= "OUTLINER_OT_selectability_toggle";
1536         ot->description= "Toggle the selectability";
1537         
1538         /* callbacks */
1539         ot->exec= outliner_toggle_selectability_exec;
1540         ot->poll= ED_operator_outliner_active;
1541         
1542         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1543 }
1544
1545 /* --- */
1546
1547 void object_toggle_renderability_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1548 {
1549         Base *base= (Base *)te->directdata;
1550         
1551         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1552         if(base) {
1553                 base->object->restrictflag^=OB_RESTRICT_RENDER;
1554         }
1555 }
1556
1557 static int outliner_toggle_renderability_exec(bContext *C, wmOperator *op)
1558 {
1559         SpaceOops *soops= CTX_wm_space_outliner(C);
1560         Scene *scene= CTX_data_scene(C);
1561         ARegion *ar= CTX_wm_region(C);
1562         
1563         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_renderability_cb);
1564         
1565         ED_region_tag_redraw(ar);
1566         
1567         return OPERATOR_FINISHED;
1568 }
1569
1570 void OUTLINER_OT_renderability_toggle(wmOperatorType *ot)
1571 {
1572         /* identifiers */
1573         ot->name= "Toggle Renderability";
1574         ot->idname= "OUTLINER_OT_renderability_toggle";
1575         ot->description= "Toggle the renderbility of selected items.";
1576         
1577         /* callbacks */
1578         ot->exec= outliner_toggle_renderability_exec;
1579         ot->poll= ED_operator_outliner_active;
1580         
1581         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1582 }
1583
1584 /* --- */
1585
1586 static int outliner_toggle_expanded_exec(bContext *C, wmOperator *op)
1587 {
1588         SpaceOops *soops= CTX_wm_space_outliner(C);
1589         ARegion *ar= CTX_wm_region(C);
1590         
1591         if (outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
1592                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
1593         else 
1594                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
1595         
1596         ED_region_tag_redraw(ar);
1597         
1598         return OPERATOR_FINISHED;
1599 }
1600
1601 void OUTLINER_OT_expanded_toggle(wmOperatorType *ot)
1602 {
1603         /* identifiers */
1604         ot->name= "Expand/Collapse All";
1605         ot->idname= "OUTLINER_OT_expanded_toggle";
1606         ot->description= "Expand/Collapse all items.";
1607         
1608         /* callbacks */
1609         ot->exec= outliner_toggle_expanded_exec;
1610         ot->poll= ED_operator_outliner_active;
1611         
1612         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1613 }
1614
1615 /* --- */
1616
1617 static int outliner_toggle_selected_exec(bContext *C, wmOperator *op)
1618 {
1619         SpaceOops *soops= CTX_wm_space_outliner(C);
1620         ARegion *ar= CTX_wm_region(C);
1621         
1622         if (outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
1623                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
1624         else 
1625                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
1626         
1627         soops->storeflag |= SO_TREESTORE_REDRAW;
1628         
1629         ED_region_tag_redraw(ar);
1630         
1631         return OPERATOR_FINISHED;
1632 }
1633
1634 void OUTLINER_OT_selected_toggle(wmOperatorType *ot)
1635 {
1636         /* identifiers */
1637         ot->name= "Toggle Selected";
1638         ot->idname= "OUTLINER_OT_selected_toggle";
1639         ot->description= "Toggle the Outliner selection of items.";
1640         
1641         /* callbacks */
1642         ot->exec= outliner_toggle_selected_exec;
1643         ot->poll= ED_operator_outliner_active;
1644         
1645         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1646 }
1647
1648 /* --- */
1649
1650 /* helper function for Show/Hide one level operator */
1651 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
1652 {
1653         TreeElement *te;
1654         TreeStoreElem *tselem;
1655         
1656         for(te= lb->first; te; te= te->next) {
1657                 tselem= TREESTORE(te);
1658                 
1659                 if(open) {
1660                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
1661                 }
1662                 else {
1663                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
1664                 }
1665                 
1666                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
1667         }
1668 }
1669
1670 static int outliner_one_level_exec(bContext *C, wmOperator *op)
1671 {
1672         SpaceOops *soops= CTX_wm_space_outliner(C);
1673         ARegion *ar= CTX_wm_region(C);
1674         int add= RNA_boolean_get(op->ptr, "open");
1675         int level;
1676         
1677         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
1678         if(add==1) {
1679                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
1680         }
1681         else {
1682                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
1683                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
1684         }
1685         
1686         ED_region_tag_redraw(ar);
1687         
1688         return OPERATOR_FINISHED;
1689 }
1690
1691 void OUTLINER_OT_show_one_level(wmOperatorType *ot)
1692 {
1693         /* identifiers */
1694         ot->name= "Show/Hide One Level";
1695         ot->idname= "OUTLINER_OT_show_one_level";
1696         
1697         /* callbacks */
1698         ot->exec= outliner_one_level_exec;
1699         ot->poll= ED_operator_outliner_active;
1700         
1701         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1702         
1703         /* properties */
1704         RNA_def_boolean(ot->srna, "open", 1, "Open", "Expand all entries one level deep.");
1705 }
1706
1707 /* return 1 when levels were opened */
1708 static int outliner_open_back(SpaceOops *soops, TreeElement *te)
1709 {
1710         TreeStoreElem *tselem;
1711         int retval= 0;
1712         
1713         for (te= te->parent; te; te= te->parent) {
1714                 tselem= TREESTORE(te);
1715                 if (tselem->flag & TSE_CLOSED) { 
1716                         tselem->flag &= ~TSE_CLOSED;
1717                         retval= 1;
1718                 }
1719         }
1720         return retval;
1721 }
1722
1723 /* This is not used anywhere at the moment */
1724 #if 0
1725 static void outliner_open_reveal(SpaceOops *soops, ListBase *lb, TreeElement *teFind, int *found)
1726 {
1727         TreeElement *te;
1728         TreeStoreElem *tselem;
1729         
1730         for (te= lb->first; te; te= te->next) {
1731                 /* check if this tree-element was the one we're seeking */
1732                 if (te == teFind) {
1733                         *found= 1;
1734                         return;
1735                 }
1736                 
1737                 /* try to see if sub-tree contains it then */
1738                 outliner_open_reveal(soops, &te->subtree, teFind, found);
1739                 if (*found) {
1740                         tselem= TREESTORE(te);
1741                         if (tselem->flag & TSE_CLOSED) 
1742                                 tselem->flag &= ~TSE_CLOSED;
1743                         return;
1744                 }
1745         }
1746 }
1747 #endif
1748
1749 // XXX just use View2D ops for this?
1750 void outliner_page_up_down(Scene *scene, ARegion *ar, SpaceOops *soops, int up)
1751 {
1752         int dy= ar->v2d.mask.ymax-ar->v2d.mask.ymin;
1753         
1754         if(up == -1) dy= -dy;
1755         ar->v2d.cur.ymin+= dy;
1756         ar->v2d.cur.ymax+= dy;
1757         
1758         soops->storeflag |= SO_TREESTORE_REDRAW;
1759 }
1760
1761 /* **** do clicks on items ******* */
1762
1763 static int tree_element_active_renderlayer(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
1764 {
1765         Scene *sce;
1766         
1767         /* paranoia check */
1768         if(te->idcode!=ID_SCE)
1769                 return 0;
1770         sce= (Scene *)tselem->id;
1771         
1772         if(set) {
1773                 sce->r.actlay= tselem->nr;
1774                 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, sce);
1775         }
1776         else {
1777                 return sce->r.actlay==tselem->nr;
1778         }
1779         return 0;
1780 }
1781
1782 static void tree_element_set_active_object(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1783 {
1784         TreeStoreElem *tselem= TREESTORE(te);
1785         Scene *sce;
1786         Base *base;
1787         Object *ob= NULL;
1788         
1789         /* if id is not object, we search back */
1790         if(te->idcode==ID_OB) ob= (Object *)tselem->id;
1791         else {
1792                 ob= (Object *)outliner_search_back(soops, te, ID_OB);
1793                 if(ob==OBACT) return;
1794         }
1795         if(ob==NULL) return;
1796         
1797         sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
1798         if(sce && scene != sce) {
1799                 ED_screen_set_scene(C, sce);
1800         }
1801         
1802         /* find associated base in current scene */
1803         for(base= FIRSTBASE; base; base= base->next) 
1804                 if(base->object==ob) break;
1805         if(base) {
1806                 if(set==2) {
1807                         /* swap select */
1808                         if(base->flag & SELECT)
1809                                 ED_base_object_select(base, BA_DESELECT);
1810                         else 
1811                                 ED_base_object_select(base, BA_SELECT);
1812                 }
1813                 else {
1814                         Base *b;
1815                         /* deleselect all */
1816                         for(b= FIRSTBASE; b; b= b->next) {
1817                                 b->flag &= ~SELECT;
1818                                 b->object->flag= b->flag;
1819                         }
1820                         ED_base_object_select(base, BA_SELECT);
1821                 }
1822                 if(C)
1823                         ED_base_object_activate(C, base); /* adds notifier */
1824         }
1825         
1826         if(ob!=scene->obedit) 
1827                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
1828         
1829         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
1830
1831 }
1832
1833 static int tree_element_active_material(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1834 {
1835         TreeElement *tes;
1836         Object *ob;
1837         
1838         /* we search for the object parent */
1839         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1840         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1841         
1842         /* searching in ob mat array? */
1843         tes= te->parent;
1844         if(tes->idcode==ID_OB) {
1845                 if(set) {
1846                         ob->actcol= te->index+1;
1847                         ob->matbits[te->index]= 1;      // make ob material active too
1848                         ob->colbits |= (1<<te->index);
1849                 }
1850                 else {
1851                         if(ob->actcol == te->index+1) 
1852                                 if(ob->matbits[te->index]) return 1;
1853                 }
1854         }
1855         /* or we search for obdata material */
1856         else {
1857                 if(set) {
1858                         ob->actcol= te->index+1;
1859                         ob->matbits[te->index]= 0;      // make obdata material active too
1860                         ob->colbits &= ~(1<<te->index);
1861                 }
1862                 else {
1863                         if(ob->actcol == te->index+1)
1864                                 if(ob->matbits[te->index]==0) return 1;
1865                 }
1866         }
1867         if(set) {
1868                 WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING, NULL);
1869         }
1870         return 0;
1871 }
1872
1873 static int tree_element_active_texture(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1874 {
1875         TreeElement *tep;
1876         TreeStoreElem *tselem, *tselemp;
1877         Object *ob=OBACT;
1878         SpaceButs *sbuts=NULL;
1879         
1880         if(ob==NULL) return 0; // no active object
1881         
1882         tselem= TREESTORE(te);
1883         
1884         /* find buttons area (note, this is undefined really still, needs recode in blender) */
1885         /* XXX removed finding sbuts */
1886         
1887         /* where is texture linked to? */
1888         tep= te->parent;
1889         tselemp= TREESTORE(tep);
1890         
1891         if(tep->idcode==ID_WO) {
1892                 World *wrld= (World *)tselemp->id;
1893
1894                 if(set) {
1895                         if(sbuts) {
1896                                 // XXX sbuts->tabo= TAB_SHADING_TEX;    // hack from header_buttonswin.c
1897                                 // XXX sbuts->texfrom= 1;
1898                         }
1899 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1900                         wrld->texact= te->index;
1901                 }
1902                 else if(tselemp->id == (ID *)(scene->world)) {
1903                         if(wrld->texact==te->index) return 1;
1904                 }
1905         }
1906         else if(tep->idcode==ID_LA) {
1907                 Lamp *la= (Lamp *)tselemp->id;
1908                 if(set) {
1909                         if(sbuts) {
1910                                 // XXX sbuts->tabo= TAB_SHADING_TEX;    // hack from header_buttonswin.c
1911                                 // XXX sbuts->texfrom= 2;
1912                         }
1913 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1914                         la->texact= te->index;
1915                 }
1916                 else {
1917                         if(tselemp->id == ob->data) {
1918                                 if(la->texact==te->index) return 1;
1919                         }
1920                 }
1921         }
1922         else if(tep->idcode==ID_MA) {
1923                 Material *ma= (Material *)tselemp->id;
1924                 if(set) {
1925                         if(sbuts) {
1926                                 //sbuts->tabo= TAB_SHADING_TEX; // hack from header_buttonswin.c
1927                                 // XXX sbuts->texfrom= 0;
1928                         }
1929 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1930                         ma->texact= (char)te->index;
1931                         
1932                         /* also set active material */
1933                         ob->actcol= tep->index+1;
1934                 }
1935                 else if(tep->flag & TE_ACTIVE) {        // this is active material
1936                         if(ma->texact==te->index) return 1;
1937                 }
1938         }
1939         
1940         return 0;
1941 }
1942
1943
1944 static int tree_element_active_lamp(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1945 {
1946         Object *ob;
1947         
1948         /* we search for the object parent */
1949         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1950         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1951         
1952         if(set) {
1953 // XXX          extern_set_butspace(F5KEY, 0);
1954         }
1955         else return 1;
1956         
1957         return 0;
1958 }
1959
1960 static int tree_element_active_world(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1961 {
1962         TreeElement *tep;
1963         TreeStoreElem *tselem=NULL;
1964         Scene *sce=NULL;
1965         
1966         tep= te->parent;
1967         if(tep) {
1968                 tselem= TREESTORE(tep);
1969                 sce= (Scene *)tselem->id;
1970         }
1971         
1972         if(set) {       // make new scene active
1973                 if(sce && scene != sce) {
1974                         ED_screen_set_scene(C, sce);
1975                 }
1976         }
1977         
1978         if(tep==NULL || tselem->id == (ID *)scene) {
1979                 if(set) {
1980 // XXX                  extern_set_butspace(F8KEY, 0);
1981                 }
1982                 else {
1983                         return 1;
1984                 }
1985         }
1986         return 0;
1987 }
1988
1989 static int tree_element_active_defgroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
1990 {
1991         Object *ob;
1992         
1993         /* id in tselem is object */
1994         ob= (Object *)tselem->id;
1995         if(set) {
1996                 ob->actdef= te->index+1;
1997                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1998                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
1999         }
2000         else {
2001                 if(ob==OBACT)
2002                         if(ob->actdef== te->index+1) return 1;
2003         }
2004         return 0;
2005 }
2006
2007 static int tree_element_active_posegroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2008 {
2009         Object *ob= (Object *)tselem->id;
2010         
2011         if(set) {
2012                 if (ob->pose) {
2013                         ob->pose->active_group= te->index+1;
2014                         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
2015                 }
2016         }
2017         else {
2018                 if(ob==OBACT && ob->pose) {
2019                         if (ob->pose->active_group== te->index+1) return 1;
2020                 }
2021         }
2022         return 0;
2023 }
2024
2025 static int tree_element_active_posechannel(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2026 {
2027         Object *ob= (Object *)tselem->id;
2028         bPoseChannel *pchan= te->directdata;
2029         
2030         if(set) {
2031                 if(!(pchan->bone->flag & BONE_HIDDEN_P)) {
2032                         
2033                         if(set==2) ED_pose_deselectall(ob, 2, 0);       // 2 = clear active tag
2034                         else ED_pose_deselectall(ob, 0, 0);     // 0 = deselect 
2035                         
2036                         if(set==2 && (pchan->bone->flag & BONE_SELECTED))
2037                                 pchan->bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
2038                         else
2039                                 pchan->bone->flag |= BONE_SELECTED|BONE_ACTIVE;
2040                         
2041                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, ob);
2042
2043                 }
2044         }
2045         else {
2046                 if(ob==OBACT && ob->pose) {
2047                         if (pchan->bone->flag & BONE_SELECTED) return 1;
2048                 }
2049         }
2050         return 0;
2051 }
2052
2053 static int tree_element_active_bone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2054 {
2055         bArmature *arm= (bArmature *)tselem->id;
2056         Bone *bone= te->directdata;
2057         
2058         if(set) {
2059                 if(!(bone->flag & BONE_HIDDEN_P)) {
2060                         if(set==2) ED_pose_deselectall(OBACT, 2, 0);    // 2 is clear active tag
2061                         else ED_pose_deselectall(OBACT, 0, 0);
2062                         
2063                         if(set==2 && (bone->flag & BONE_SELECTED))
2064                                 bone->flag &= ~(BONE_SELECTED|BONE_ACTIVE);
2065                         else
2066                                 bone->flag |= BONE_SELECTED|BONE_ACTIVE;
2067                         
2068                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, OBACT);
2069                 }
2070         }
2071         else {
2072                 Object *ob= OBACT;
2073                 
2074                 if(ob && ob->data==arm) {
2075                         if (bone->flag & BONE_SELECTED) return 1;
2076                 }
2077         }
2078         return 0;
2079 }
2080
2081
2082 /* ebones only draw in editmode armature */
2083 static int tree_element_active_ebone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2084 {
2085         EditBone *ebone= te->directdata;
2086         
2087         if(set) {
2088                 if(!(ebone->flag & BONE_HIDDEN_A)) {
2089                         
2090                         if(set==2) ED_armature_deselectall(scene->obedit, 2, 0);        // only clear active tag
2091                         else ED_armature_deselectall(scene->obedit, 0, 0);      // deselect
2092
2093                         ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL|BONE_ACTIVE;
2094                         // flush to parent?
2095                         if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
2096                         
2097                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, scene->obedit);
2098                 }
2099         }
2100         else {
2101                 if (ebone->flag & BONE_SELECTED) return 1;
2102         }
2103         return 0;
2104 }
2105
2106 static int tree_element_active_modifier(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2107 {
2108         if(set) {
2109                 Object *ob= (Object *)tselem->id;
2110                 
2111                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
2112
2113 // XXX          extern_set_butspace(F9KEY, 0);
2114         }
2115         
2116         return 0;
2117 }
2118
2119 static int tree_element_active_psys(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2120 {
2121         if(set) {
2122                 Object *ob= (Object *)tselem->id;
2123                 
2124                 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE_DATA, ob);
2125                 
2126 // XXX          extern_set_butspace(F7KEY, 0);
2127         }
2128         
2129         return 0;
2130 }
2131
2132 static int tree_element_active_constraint(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2133 {
2134         if(set) {
2135                 Object *ob= (Object *)tselem->id;
2136                 
2137                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
2138 // XXX          extern_set_butspace(F7KEY, 0);
2139         }
2140         
2141         return 0;
2142 }
2143
2144 static int tree_element_active_text(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2145 {
2146         // XXX removed
2147         return 0;
2148 }
2149
2150 /* generic call for ID data check or make/check active in UI */
2151 static int tree_element_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2152 {
2153
2154         switch(te->idcode) {
2155                 case ID_MA:
2156                         return tree_element_active_material(C, scene, soops, te, set);
2157                 case ID_WO:
2158                         return tree_element_active_world(C, scene, soops, te, set);
2159                 case ID_LA:
2160                         return tree_element_active_lamp(C, scene, soops, te, set);
2161                 case ID_TE:
2162                         return tree_element_active_texture(C, scene, soops, te, set);
2163                 case ID_TXT:
2164                         return tree_element_active_text(C, scene, soops, te, set);
2165         }
2166         return 0;
2167 }
2168
2169 static int tree_element_active_pose(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2170 {
2171         Object *ob= (Object *)tselem->id;
2172         Base *base= object_in_scene(ob, scene);
2173         
2174         if(set) {
2175                 if(scene->obedit) 
2176                         ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
2177                 
2178                 if(ob->mode & OB_MODE_POSE) 
2179                         ED_armature_exit_posemode(C, base);
2180                 else 
2181                         ED_armature_enter_posemode(C, base);
2182         }
2183         else {
2184                 if(ob->mode & OB_MODE_POSE) return 1;
2185         }
2186         return 0;
2187 }
2188
2189 static int tree_element_active_sequence(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2190 {
2191         Sequence *seq= (Sequence*) te->directdata;
2192
2193         if(set) {
2194 // XXX          select_single_seq(seq, 1);
2195         }
2196         else {
2197                 if(seq->flag & SELECT)
2198                         return(1);
2199         }
2200         return(0);
2201 }
2202
2203 static int tree_element_active_sequence_dup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2204 {
2205         Sequence *seq, *p;
2206         Editing *ed= seq_give_editing(scene, FALSE);
2207
2208         seq= (Sequence*)te->directdata;
2209         if(set==0) {
2210                 if(seq->flag & SELECT)
2211                         return(1);
2212                 return(0);
2213         }
2214
2215 // XXX  select_single_seq(seq, 1);
2216         p= ed->seqbasep->first;
2217         while(p) {
2218                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
2219                         p= p->next;
2220                         continue;
2221                 }
2222
2223 //              if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
2224 // XXX                  select_single_seq(p, 0);
2225                 p= p->next;
2226         }
2227         return(0);
2228 }
2229
2230 static int tree_element_active_keymap_item(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2231 {
2232         wmKeymapItem *kmi= te->directdata;
2233         
2234         if(set==0) {
2235                 if(kmi->inactive) return 0;
2236                 return 1;
2237         }
2238         else {
2239                 kmi->inactive= !kmi->inactive;
2240         }
2241         return 0;
2242 }
2243
2244
2245 /* generic call for non-id data to make/check active in UI */
2246 /* Context can be NULL when set==0 */
2247 static int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
2248 {
2249         
2250         switch(tselem->type) {
2251                 case TSE_DEFGROUP:
2252                         return tree_element_active_defgroup(C, scene, te, tselem, set);
2253                 case TSE_BONE:
2254                         return tree_element_active_bone(C, scene, te, tselem, set);
2255                 case TSE_EBONE:
2256                         return tree_element_active_ebone(C, scene, te, tselem, set);
2257                 case TSE_MODIFIER:
2258                         return tree_element_active_modifier(C, te, tselem, set);
2259                 case TSE_LINKED_OB:
2260                         if(set) tree_element_set_active_object(C, scene, soops, te, set);
2261                         else if(tselem->id==(ID *)OBACT) return 1;
2262                         break;
2263                 case TSE_LINKED_PSYS:
2264                         return tree_element_active_psys(C, scene, te, tselem, set);
2265                 case TSE_POSE_BASE:
2266                         return tree_element_active_pose(C, scene, te, tselem, set);
2267                 case TSE_POSE_CHANNEL:
2268                         return tree_element_active_posechannel(C, scene, te, tselem, set);
2269                 case TSE_CONSTRAINT:
2270                         return tree_element_active_constraint(C, te, tselem, set);
2271                 case TSE_R_LAYER:
2272                         return tree_element_active_renderlayer(C, te, tselem, set);
2273                 case TSE_POSEGRP:
2274                         return tree_element_active_posegroup(C, scene, te, tselem, set);
2275                 case TSE_SEQUENCE:
2276                         return tree_element_active_sequence(C, te, tselem, set);
2277                 case TSE_SEQUENCE_DUP:
2278                         return tree_element_active_sequence_dup(C, scene, te, tselem, set);
2279                 case TSE_KEYMAP_ITEM:
2280                         return tree_element_active_keymap_item(C, te, tselem, set);
2281                         
2282         }
2283         return 0;
2284 }
2285
2286 static int do_outliner_item_activate(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, int extend, float *mval)
2287 {
2288         
2289         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2290                 TreeStoreElem *tselem= TREESTORE(te);
2291                 int openclose= 0;
2292
2293                 /* open close icon */
2294                 if((te->flag & TE_ICONROW)==0) {                                // hidden icon, no open/close
2295                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) 
2296                                 openclose= 1;
2297                 }
2298
2299                 if(openclose) {
2300                         /* all below close/open? */
2301                         if(extend) {
2302                                 tselem->flag &= ~TSE_CLOSED;
2303                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2304                         }
2305                         else {
2306                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2307                                 else tselem->flag |= TSE_CLOSED;
2308                                 
2309                         }
2310
2311                         return 1;
2312                 }
2313                 /* name and first icon */
2314                 else if(mval[0]>te->xs && mval[0]<te->xend) {
2315                         
2316                         /* always makes active object */
2317                         if(tselem->type!=TSE_SEQUENCE && tselem->type!=TSE_SEQ_STRIP && tselem->type!=TSE_SEQUENCE_DUP)
2318                                 tree_element_set_active_object(C, scene, soops, te, 1 + (extend!=0 && tselem->type==0));
2319                         
2320                         if(tselem->type==0) { // the lib blocks
2321                                 /* editmode? */
2322                                 if(te->idcode==ID_SCE) {
2323                                         if(scene!=(Scene *)tselem->id) {
2324                                                 ED_screen_set_scene(C, (Scene *)tselem->id);
2325                                         }
2326                                 }
2327                                 else if(ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
2328                                         Object *obedit= CTX_data_edit_object(C);
2329                                         if(obedit) 
2330                                                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
2331                                         else {
2332                                                 ED_object_enter_editmode(C, EM_WAITCURSOR);
2333                                                 // XXX extern_set_butspace(F9KEY, 0);
2334                                         }
2335                                 } else {        // rest of types
2336                                         tree_element_active(C, scene, soops, te, 1);
2337                                 }
2338                                 
2339                         }
2340                         else tree_element_type_active(C, scene, soops, te, tselem, 1+(extend!=0));
2341
2342                         return 1;
2343                 }
2344         }
2345         
2346         for(te= te->subtree.first; te; te= te->next) {
2347                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, mval)) return 1;
2348         }
2349         return 0;
2350 }
2351
2352 /* event can enterkey, then it opens/closes */
2353 static int outliner_item_activate(bContext *C, wmOperator *op, wmEvent *event)
2354 {
2355         Scene *scene= CTX_data_scene(C);
2356         ARegion *ar= CTX_wm_region(C);
2357         SpaceOops *soops= CTX_wm_space_outliner(C);
2358         TreeElement *te;
2359         float fmval[2];
2360         int extend= RNA_boolean_get(op->ptr, "extend");
2361         
2362         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2363         
2364         for(te= soops->tree.first; te; te= te->next) {
2365                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, fmval)) break;
2366         }
2367         
2368         if(te) {
2369                 ED_undo_push(C, "Outliner click event");
2370         }
2371         else {
2372                 short selecting= -1;
2373                 int row;
2374                 
2375                 /* get row number - 100 here is just a dummy value since we don't need the column */
2376                 UI_view2d_listview_view_to_cell(&ar->v2d, 1000, OL_H, 0.0f, 0.0f, 
2377                                                 fmval[0], fmval[1], NULL, &row);
2378                 
2379                 /* select relevant row */
2380                 outliner_select(soops, &soops->tree, &row, &selecting);
2381                 
2382                 soops->storeflag |= SO_TREESTORE_REDRAW;
2383                 
2384                 ED_undo_push(C, "Outliner selection event");
2385         }
2386         
2387         ED_region_tag_redraw(ar);
2388
2389         return OPERATOR_FINISHED;
2390 }
2391
2392 void OUTLINER_OT_item_activate(wmOperatorType *ot)
2393 {
2394         ot->name= "Activate Item";
2395         ot->idname= "OUTLINER_OT_item_activate";
2396         
2397         ot->invoke= outliner_item_activate;
2398         
2399         ot->poll= ED_operator_outliner_active;
2400         
2401         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection for activation.");
2402 }
2403
2404 /* *********** */
2405
2406 static int do_outliner_item_openclose(bContext *C, SpaceOops *soops, TreeElement *te, int all, float *mval)
2407 {
2408         
2409         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2410                 TreeStoreElem *tselem= TREESTORE(te);
2411
2412                 /* all below close/open? */
2413                 if(all) {
2414                         tselem->flag &= ~TSE_CLOSED;
2415                         outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2416                 }
2417                 else {
2418                         if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2419                         else tselem->flag |= TSE_CLOSED;
2420                 }
2421
2422                 return 1;
2423         }
2424         
2425         for(te= te->subtree.first; te; te= te->next) {
2426                 if(do_outliner_item_openclose(C, soops, te, all, mval)) 
2427                         return 1;
2428         }
2429         return 0;
2430         
2431 }
2432
2433 /* event can enterkey, then it opens/closes */
2434 static int outliner_item_openclose(bContext *C, wmOperator *op, wmEvent *event)
2435 {
2436         ARegion *ar= CTX_wm_region(C);
2437         SpaceOops *soops= CTX_wm_space_outliner(C);
2438         TreeElement *te;
2439         float fmval[2];
2440         int all= RNA_boolean_get(op->ptr, "all");
2441         
2442         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2443         
2444         for(te= soops->tree.first; te; te= te->next) {
2445                 if(do_outliner_item_openclose(C, soops, te, all, fmval)) 
2446                         break;
2447         }
2448
2449         ED_region_tag_redraw(ar);
2450         
2451         return OPERATOR_FINISHED;
2452 }
2453
2454 void OUTLINER_OT_item_openclose(wmOperatorType *ot)
2455 {
2456         ot->name= "Open/Close Item";
2457         ot->idname= "OUTLINER_OT_item_openclose";
2458         
2459         ot->invoke= outliner_item_openclose;
2460         
2461         ot->poll= ED_operator_outliner_active;
2462         
2463         RNA_def_boolean(ot->srna, "all", 1, "All", "Close or open all items.");
2464
2465 }
2466
2467
2468 /* ********************************************** */
2469
2470 static int do_outliner_item_rename(bContext *C, ARegion *ar, SpaceOops *soops, TreeElement *te, float *mval)
2471 {
2472         
2473         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2474                 TreeStoreElem *tselem= TREESTORE(te);
2475                 
2476                 /* name and first icon */
2477                 if(mval[0]>te->xs && mval[0]<te->xend) {
2478
2479                         if(ELEM10(tselem->type, TSE_ANIM_DATA, TSE_NLA, TSE_DEFGROUP_BASE, TSE_CONSTRAINT_BASE, TSE_MODIFIER_BASE, TSE_SCRIPT_BASE, TSE_POSE_BASE, TSE_POSEGRP_BASE, TSE_R_LAYER_BASE, TSE_R_PASS)) 
2480                                         error("Cannot edit builtin name");
2481                         else if(ELEM3(tselem->type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP))
2482                                 error("Cannot edit sequence name");
2483                         else if(tselem->id->lib) {
2484                                 // XXX                                          error_libdata();
2485                         } else if(te->idcode == ID_LI && te->parent) {
2486                                 error("Cannot edit the path of an indirectly linked library");
2487                         } else {
2488                                 tselem->flag |= TSE_TEXTBUT;
2489                                 ED_region_tag_redraw(ar);
2490                         }
2491                 }
2492                 return 1;
2493         }
2494         
2495         for(te= te->subtree.first; te; te= te->next) {
2496                 if(do_outliner_item_rename(C, ar, soops, te, mval)) return 1;
2497         }
2498         return 0;
2499 }
2500
2501 static int outliner_item_rename(bContext *C, wmOperator *op, wmEvent *event)
2502 {
2503         ARegion *ar= CTX_wm_region(C);
2504         SpaceOops *soops= CTX_wm_space_outliner(C);
2505         TreeElement *te;
2506         float fmval[2];
2507         
2508         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2509         
2510         for(te= soops->tree.first; te; te= te->next) {
2511                 if(do_outliner_item_rename(C, ar, soops, te, fmval)) break;
2512         }
2513         
2514         return OPERATOR_FINISHED;
2515 }
2516
2517
2518 void OUTLINER_OT_item_rename(wmOperatorType *ot)
2519 {
2520         ot->name= "Rename Item";
2521         ot->idname= "OUTLINER_OT_item_rename";
2522         
2523         ot->invoke= outliner_item_rename;
2524         
2525         ot->poll= ED_operator_outliner_active;
2526 }
2527
2528
2529
2530 /* recursive helper for function below */
2531 static void outliner_set_coordinates_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
2532 {
2533         TreeStoreElem *tselem= TREESTORE(te);
2534         
2535         /* store coord and continue, we need coordinates for elements outside view too */
2536         te->xs= (float)startx;
2537         te->ys= (float)(*starty);
2538         *starty-= OL_H;
2539         
2540         if((tselem->flag & TSE_CLOSED)==0) {
2541                 TreeElement *ten;
2542                 for(ten= te->subtree.first; ten; ten= ten->next) {
2543                         outliner_set_coordinates_element(soops, ten, startx+OL_X, starty);
2544                 }
2545         }
2546         
2547 }
2548
2549 /* to retrieve coordinates with redrawing the entire tree */
2550 static void outliner_set_coordinates(ARegion *ar, SpaceOops *soops)
2551 {
2552         TreeElement *te;
2553         int starty= (int)(ar->v2d.tot.ymax)-OL_H;
2554         int startx= 0;
2555         
2556         for(te= soops->tree.first; te; te= te->next) {
2557                 outliner_set_coordinates_element(soops, te, startx, &starty);
2558         }
2559 }
2560
2561 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
2562 {
2563         TreeElement *te, *tes;
2564         TreeStoreElem *tselem;
2565         
2566         for(te= lb->first; te; te= te->next) {
2567                 tselem= TREESTORE(te);
2568                 if(tselem->type==0) {
2569                         if(tselem->id==id) return te;
2570                         /* only deeper on scene or object */
2571                         if( te->idcode==ID_OB || te->idcode==ID_SCE) { 
2572                                 tes= outliner_find_id(soops, &te->subtree, id);
2573                                 if(tes) return tes;
2574                         }
2575                 }
2576         }
2577         return NULL;
2578 }
2579
2580 static int outliner_show_active_exec(bContext *C, wmOperator *op)
2581 {
2582         SpaceOops *so= CTX_wm_space_outliner(C);
2583         Scene *scene= CTX_data_scene(C);
2584         ARegion *ar= CTX_wm_region(C);
2585         View2D *v2d= &ar->v2d;
2586         
2587         TreeElement *te;
2588         int xdelta, ytop;
2589         
2590         // TODO: make this get this info from context instead...
2591         if (OBACT == NULL) 
2592                 return OPERATOR_CANCELLED;
2593         
2594         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
2595         if (te) {
2596                 /* make te->ys center of view */
2597                 ytop= (int)(te->ys + (v2d->mask.ymax - v2d->mask.ymin)/2);
2598                 if (ytop>0) ytop= 0;
2599                 
2600                 v2d->cur.ymax= (float)ytop;
2601                 v2d->cur.ymin= (float)(ytop-(v2d->mask.ymax - v2d->mask.ymin));
2602                 
2603                 /* make te->xs ==> te->xend center of view */
2604                 xdelta = (int)(te->xs - v2d->cur.xmin);
2605                 v2d->cur.xmin += xdelta;
2606                 v2d->cur.xmax += xdelta;
2607                 
2608                 so->storeflag |= SO_TREESTORE_REDRAW;
2609         }
2610         
2611         ED_region_tag_redraw(ar);
2612         
2613         return OPERATOR_FINISHED;
2614 }
2615
2616 void OUTLINER_OT_show_active(wmOperatorType *ot)
2617 {
2618         /* identifiers */
2619         ot->name= "Show Active";
2620         ot->idname= "OUTLINER_OT_show_active";
2621         ot->description= "Adjust the view so that the active Object is shown centered.";
2622         
2623         /* callbacks */
2624         ot->exec= outliner_show_active_exec;
2625         ot->poll= ED_operator_outliner_active;
2626         
2627         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2628 }
2629
2630 /* find next element that has this name */
2631 static TreeElement *outliner_find_named(SpaceOops *soops, ListBase *lb, char *name, int flags, TreeElement *prev, int *prevFound)
2632 {
2633         TreeElement *te, *tes;
2634         
2635         for (te= lb->first; te; te= te->next) {
2636                 int found;
2637                 
2638                 /* determine if match */
2639                 if(flags==OL_FIND)
2640                         found= BLI_strcasestr(te->name, name)!=NULL;
2641                 else if(flags==OL_FIND_CASE)
2642                         found= strstr(te->name, name)!=NULL;
2643                 else if(flags==OL_FIND_COMPLETE)
2644                         found= BLI_strcasecmp(te->name, name)==0;
2645                 else
2646                         found= strcmp(te->name, name)==0;
2647                 
2648                 if(found) {
2649                         /* name is right, but is element the previous one? */
2650                         if (prev) {
2651                                 if ((te != prev) && (*prevFound)) 
2652                                         return te;
2653                                 if (te == prev) {
2654                                         *prevFound = 1;
2655                                 }
2656                         }
2657                         else
2658                                 return te;
2659                 }
2660                 
2661                 tes= outliner_find_named(soops, &te->subtree, name, flags, prev, prevFound);
2662                 if(tes) return tes;
2663         }
2664
2665         /* nothing valid found */
2666         return NULL;
2667 }
2668
2669 /* tse is not in the treestore, we use its contents to find a match */
2670 static TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
2671 {
2672         TreeStore *ts= soops->treestore;
2673         TreeStoreElem *tselem;
2674         int a;
2675         
2676         if(tse->id==NULL) return NULL;
2677         
2678         /* check if 'tse' is in treestore */
2679         tselem= ts->data;
2680         for(a=0; a<ts->usedelem; a++, tselem++) {
2681                 if((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
2682                         if(tselem->id==tse->id) {
2683                                 break;
2684                         }
2685                 }
2686         }
2687         if(tselem) 
2688                 return outliner_find_tree_element(&soops->tree, a);
2689         
2690         return NULL;
2691 }
2692
2693
2694 /* Called to find an item based on name.
2695  */
2696 void outliner_find_panel(Scene *scene, ARegion *ar, SpaceOops *soops, int again, int flags) 
2697 {
2698         TreeElement *te= NULL;
2699         TreeElement *last_find;
2700         TreeStoreElem *tselem;
2701         int ytop, xdelta, prevFound=0;
2702         char name[33];
2703         
2704         /* get last found tree-element based on stored search_tse */
2705         last_find= outliner_find_tse(soops, &soops->search_tse);
2706         
2707         /* determine which type of search to do */
2708         if (again && last_find) {
2709                 /* no popup panel - previous + user wanted to search for next after previous */         
2710                 BLI_strncpy(name, soops->search_string, 33);
2711                 flags= soops->search_flags;
2712                 
2713                 /* try to find matching element */
2714                 te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2715                 if (te==NULL) {
2716                         /* no more matches after previous, start from beginning again */
2717                         prevFound= 1;
2718                         te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2719                 }
2720         }
2721         else {
2722                 /* pop up panel - no previous, or user didn't want search after previous */
2723                 strcpy(name, "");
2724 // XXX          if (sbutton(name, 0, sizeof(name)-1, "Find: ") && name[0]) {
2725 //                      te= outliner_find_named(soops, &soops->tree, name, flags, NULL, &prevFound);
2726 //              }
2727 //              else return; /* XXX RETURN! XXX */
2728         }
2729
2730         /* do selection and reveal */
2731         if (te) {
2732                 tselem= TREESTORE(te);
2733                 if (tselem) {
2734                         /* expand branches so that it will be visible, we need to get correct coordinates */
2735                         if( outliner_open_back(soops, te))
2736                                 outliner_set_coordinates(ar, soops);
2737                         
2738                         /* deselect all visible, and select found element */
2739                         outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
2740                         tselem->flag |= TSE_SELECTED;
2741                         
2742                         /* make te->ys center of view */
2743                         ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2744                         if(ytop>0) ytop= 0;
2745                         ar->v2d.cur.ymax= (float)ytop;
2746                         ar->v2d.cur.ymin= (float)(ytop-(ar->v2d.mask.ymax-ar->v2d.mask.ymin));
2747                         
2748                         /* make te->xs ==> te->xend center of view */
2749                         xdelta = (int)(te->xs - ar->v2d.cur.xmin);
2750                         ar->v2d.cur.xmin += xdelta;
2751                         ar->v2d.cur.xmax += xdelta;
2752                         
2753                         /* store selection */
2754                         soops->search_tse= *tselem;
2755                         
2756                         BLI_strncpy(soops->search_string, name, 33);
2757                         soops->search_flags= flags;
2758                         
2759                         /* redraw */
2760                         soops->storeflag |= SO_TREESTORE_REDRAW;
2761                 }
2762         }
2763         else {
2764                 /* no tree-element found */
2765                 error("Not found: %s", name);
2766         }
2767 }
2768
2769 /* helper function for tree_element_shwo_hierarchy() - recursively checks whether subtrees have any objects*/
2770 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
2771 {
2772         TreeElement *te;
2773         TreeStoreElem *tselem;
2774         
2775         for(te= lb->first; te; te= te->next) {
2776                 tselem= TREESTORE(te);
2777                 if(tselem->type==0 && te->idcode==ID_OB) return 1;
2778                 if( subtree_has_objects(soops, &te->subtree)) return 1;
2779         }
2780         return 0;
2781 }
2782
2783 /* recursive helper function for Show Hierarchy operator */
2784 static void tree_element_show_hierarchy(Scene *scene, SpaceOops *soops, ListBase *lb)
2785 {
2786         TreeElement *te;
2787         TreeStoreElem *tselem;
2788
2789         /* open all object elems, close others */
2790         for(te= lb->first; te; te= te->next) {
2791                 tselem= TREESTORE(te);
2792                 
2793                 if(tselem->type==0) {
2794                         if(te->idcode==ID_SCE) {
2795                                 if(tselem->id!=(ID *)scene) tselem->flag |= TSE_CLOSED;
2796                                         else tselem->flag &= ~TSE_CLOSED;
2797                         }
2798                         else if(te->idcode==ID_OB) {
2799                                 if(subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
2800                                 else tselem->flag |= TSE_CLOSED;
2801                         }
2802                 }
2803                 else tselem->flag |= TSE_CLOSED;
2804                 
2805                 if(tselem->flag & TSE_CLOSED); else tree_element_show_hierarchy(scene, soops, &te->subtree);
2806         }
2807 }
2808
2809 /* show entire object level hierarchy */
2810 static int outliner_show_hierarchy_exec(bContext *C, wmOperator *op)
2811 {
2812         SpaceOops *soops= CTX_wm_space_outliner(C);
2813         ARegion *ar= CTX_wm_region(C);
2814         Scene *scene= CTX_data_scene(C);
2815         
2816         /* recursively open/close levels */
2817         tree_element_show_hierarchy(scene, soops, &soops->tree);
2818         
2819         ED_region_tag_redraw(ar);
2820         
2821         return OPERATOR_FINISHED;
2822 }
2823
2824 void OUTLINER_OT_show_hierarchy(wmOperatorType *ot)
2825 {
2826         /* identifiers */
2827         ot->name= "Show Hierarchy";
2828         ot->idname= "OUTLINER_OT_show_hierarchy";
2829         ot->description= "Open all object entries and close all others.";
2830         
2831         /* callbacks */
2832         ot->exec= outliner_show_hierarchy_exec;
2833         ot->poll= ED_operator_outliner_active; //  TODO: shouldn't be allowed in RNA views...
2834         
2835         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2836 }
2837
2838 void outliner_select(SpaceOops *soops, ListBase *lb, int *index, short *selecting)
2839 {
2840         TreeElement *te;
2841         TreeStoreElem *tselem;
2842         
2843         for (te= lb->first; te && *index >= 0; te=te->next, (*index)--) {
2844                 tselem= TREESTORE(te);
2845                 
2846                 /* if we've encountered the right item, set its 'Outliner' selection status */
2847                 if (*index == 0) {
2848                         /* this should be the last one, so no need to do anything with index */
2849                         if ((te->flag & TE_ICONROW)==0) {
2850                                 /* -1 value means toggle testing for now... */
2851                                 if (*selecting == -1) {
2852                                         if (tselem->flag & TSE_SELECTED) 
2853                                                 *selecting= 0;
2854                                         else 
2855                                                 *selecting= 1;
2856                                 }
2857                                 
2858                                 /* set selection */
2859                                 if (*selecting) 
2860                                         tselem->flag |= TSE_SELECTED;
2861                                 else 
2862                                         tselem->flag &= ~TSE_SELECTED;
2863                         }
2864                 }
2865                 else if ((tselem->flag & TSE_CLOSED)==0) {
2866                         /* Only try selecting sub-elements if we haven't hit the right element yet
2867                          *
2868                          * Hack warning:
2869                          *      Index must be reduced before supplying it to the sub-tree to try to do
2870                          *      selection, however, we need to increment it again for the next loop to 
2871                          *      function correctly
2872                          */
2873                         (*index)--;
2874                         outliner_select(soops, &te->subtree, index, selecting);
2875                         (*index)++;
2876                 }
2877         }
2878 }
2879
2880 /* ************ SELECTION OPERATIONS ********* */
2881
2882 static void set_operation_types(SpaceOops *soops, ListBase *lb,
2883                                 int *scenelevel,
2884                                 int *objectlevel,
2885                                 int *idlevel,
2886                                 int *datalevel)
2887 {
2888         TreeElement *te;
2889         TreeStoreElem *tselem;
2890         
2891         for(te= lb->first; te; te= te->next) {
2892                 tselem= TREESTORE(te);
2893                 if(tselem->flag & TSE_SELECTED) {
2894                         if(tselem->type) {
2895                                 if(*datalevel==0) 
2896                                         *datalevel= tselem->type;
2897                                 else if(*datalevel!=tselem->type) 
2898                                         *datalevel= -1;
2899                         }
2900                         else {
2901                                 int idcode= GS(tselem->id->name);
2902                                 switch(idcode) {
2903                                         case ID_SCE:
2904                                                 *scenelevel= 1;
2905                                                 break;
2906                                         case ID_OB:
2907                                                 *objectlevel= 1;
2908                                                 break;
2909                                                 
2910                                         case ID_ME: case ID_CU: case ID_MB: case ID_LT:
2911                                         case ID_LA: case ID_AR: case ID_CA:
2912                                         case ID_MA: case ID_TE: case ID_IP: case ID_IM:
2913                                         case ID_SO: case ID_KE: case ID_WO: case ID_AC:
2914                                         case ID_NLA: case ID_TXT: case ID_GR:
2915                                                 if(*idlevel==0) *idlevel= idcode;
2916                                                 else if(*idlevel!=idcode) *idlevel= -1;
2917                                                         break;
2918                                 }
2919                         }
2920                 }
2921                 if((tselem->flag & TSE_CLOSED)==0) {
2922                         set_operation_types(soops, &te->subtree,
2923                                                                 scenelevel, objectlevel, idlevel, datalevel);
2924                 }
2925         }
2926 }
2927
2928 static void unlink_material_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2929 {
2930         Material **matar=NULL;
2931         int a, totcol=0;
2932         
2933         if( GS(tsep->id->name)==ID_OB) {
2934                 Object *ob= (Object *)tsep->id;
2935                 totcol= ob->totcol;
2936                 matar= ob->mat;
2937         }
2938         else if( GS(tsep->id->name)==ID_ME) {
2939                 Mesh *me= (Mesh *)tsep->id;
2940                 totcol= me->totcol;
2941                 matar= me->mat;
2942         }
2943         else if( GS(tsep->id->name)==ID_CU) {
2944                 Curve *cu= (Curve *)tsep->id;
2945                 totcol= cu->totcol;
2946                 matar= cu->mat;
2947         }
2948         else if( GS(tsep->id->name)==ID_MB) {
2949                 MetaBall *mb= (MetaBall *)tsep->id;
2950                 totcol= mb->totcol;
2951                 matar= mb->mat;
2952         }