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