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