Fix #28467: Crash while deleting objects in outliner too fast
[blender-staging.git] / source / blender / editors / space_outliner / outliner_tree.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_outliner/outliner_tree.c
29  *  \ingroup spoutliner
30  */
31  
32 #include <math.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stddef.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_camera_types.h"
43 #include "DNA_group_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meta_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_world_types.h"
52 #include "DNA_sequence_types.h"
53 #include "DNA_speaker_types.h"
54 #include "DNA_object_types.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_utildefines.h"
58 #include "BLI_math_base.h"
59
60 #if defined WIN32 && !defined _LIBC
61 # include "BLI_fnmatch.h" /* use fnmatch included in blenlib */
62 #else
63 #  ifndef _GNU_SOURCE
64 #    define _GNU_SOURCE
65 #  endif
66 # include <fnmatch.h>
67 #endif
68
69
70 #include "BKE_animsys.h"
71 #include "BKE_context.h"
72 #include "BKE_deform.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_global.h"
76 #include "BKE_group.h"
77 #include "BKE_library.h"
78 #include "BKE_main.h"
79 #include "BKE_modifier.h"
80 #include "BKE_report.h"
81 #include "BKE_scene.h"
82 #include "BKE_sequencer.h"
83
84 #include "ED_armature.h"
85 #include "ED_object.h"
86 #include "ED_screen.h"
87 #include "ED_util.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 #include "RNA_define.h"
102 #include "RNA_enum_types.h"
103
104 #include "outliner_intern.h"
105
106 /* ********************************************************* */
107 /* Defines */
108
109 #define TS_CHUNK        128
110
111 /* ********************************************************* */
112 /* Persistant Data */
113
114 static void outliner_storage_cleanup(SpaceOops *soops)
115 {
116         TreeStore *ts= soops->treestore;
117         
118         if(ts) {
119                 TreeStoreElem *tselem;
120                 int a, unused= 0;
121                 
122                 /* each element used once, for ID blocks with more users to have each a treestore */
123                 for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) tselem->used= 0;
124                 
125                 /* cleanup only after reading file or undo step, and always for
126                  * RNA datablocks view in order to save memory */
127                 if(soops->storeflag & SO_TREESTORE_CLEANUP) {
128                         
129                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
130                                 if(tselem->id==NULL) unused++;
131                         }
132                         
133                         if(unused) {
134                                 if(ts->usedelem == unused) {
135                                         MEM_freeN(ts->data);
136                                         ts->data= NULL;
137                                         ts->usedelem= ts->totelem= 0;
138                                 }
139                                 else {
140                                         TreeStoreElem *tsnewar, *tsnew;
141                                         
142                                         tsnew=tsnewar= MEM_mallocN((ts->usedelem-unused)*sizeof(TreeStoreElem), "new tselem");
143                                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
144                                                 if(tselem->id) {
145                                                         *tsnew= *tselem;
146                                                         tsnew++;
147                                                 }
148                                         }
149                                         MEM_freeN(ts->data);
150                                         ts->data= tsnewar;
151                                         ts->usedelem-= unused;
152                                         ts->totelem= ts->usedelem;
153                                 }
154                         }
155                 }
156         }
157 }
158
159 static void check_persistant(SpaceOops *soops, TreeElement *te, ID *id, short type, short nr)
160 {
161         TreeStore *ts;
162         TreeStoreElem *tselem;
163         int a;
164         
165         /* case 1; no TreeStore */
166         if(soops->treestore==NULL) {
167                 soops->treestore= MEM_callocN(sizeof(TreeStore), "treestore");
168         }
169         ts= soops->treestore;
170         
171         /* check if 'te' is in treestore */
172         tselem= ts->data;
173         for(a=0; a<ts->usedelem; a++, tselem++) {
174                 if(tselem->id==id && tselem->used==0) {
175                         if((type==0 && tselem->type==0) ||(tselem->type==type && tselem->nr==nr)) {
176                                 te->store_index= a;
177                                 tselem->used= 1;
178                                 return;
179                         }
180                 }
181         }
182         
183         /* add 1 element to treestore */
184         if(ts->usedelem==ts->totelem) {
185                 TreeStoreElem *tsnew;
186                 
187                 tsnew= MEM_mallocN((ts->totelem+TS_CHUNK)*sizeof(TreeStoreElem), "treestore data");
188                 if(ts->data) {
189                         memcpy(tsnew, ts->data, ts->totelem*sizeof(TreeStoreElem));
190                         MEM_freeN(ts->data);
191                 }
192                 ts->data= tsnew;
193                 ts->totelem+= TS_CHUNK;
194         }
195         
196         tselem= ts->data+ts->usedelem;
197         
198         tselem->type= type;
199         if(type) tselem->nr= nr; // we're picky! :)
200         else tselem->nr= 0;
201         tselem->id= id;
202         tselem->used = 0;
203         tselem->flag= TSE_CLOSED;
204         te->store_index= ts->usedelem;
205         
206         ts->usedelem++;
207 }
208
209 /* ********************************************************* */
210 /* Tree Management */
211
212 void outliner_free_tree(ListBase *lb)
213 {
214         while(lb->first) {
215                 TreeElement *te= lb->first;
216                 
217                 outliner_free_tree(&te->subtree);
218                 BLI_remlink(lb, te);
219                 
220                 if(te->flag & TE_FREE_NAME) MEM_freeN((void *)te->name);
221                 MEM_freeN(te);
222         }
223 }
224
225 void outliner_cleanup_tree(SpaceOops *soops)
226 {
227         outliner_free_tree(&soops->tree);
228         outliner_storage_cleanup(soops);
229 }
230
231 /* Find ith item from the treestore */
232 static TreeElement *outliner_find_tree_element(ListBase *lb, int store_index)
233 {
234         TreeElement *te= lb->first, *tes;
235         while(te) {
236                 if(te->store_index==store_index) return te;
237                 tes= outliner_find_tree_element(&te->subtree, store_index);
238                 if(tes) return tes;
239                 te= te->next;
240         }
241         return NULL;
242 }
243
244 /* tse is not in the treestore, we use its contents to find a match */
245 TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
246 {
247         TreeStore *ts= soops->treestore;
248         TreeStoreElem *tselem;
249         int a;
250         
251         if(tse->id==NULL) return NULL;
252         
253         /* check if 'tse' is in treestore */
254         tselem= ts->data;
255         for(a=0; a<ts->usedelem; a++, tselem++) {
256                 if((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
257                         if(tselem->id==tse->id) {
258                                 break;
259                         }
260                 }
261         }
262         if(tselem) 
263                 return outliner_find_tree_element(&soops->tree, a);
264         
265         return NULL;
266 }
267
268 /* Find treestore that refers to given ID */
269 TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
270 {
271         TreeElement *te, *tes;
272         TreeStoreElem *tselem;
273         
274         for(te= lb->first; te; te= te->next) {
275                 tselem= TREESTORE(te);
276                 if(tselem->type==0) {
277                         if(tselem->id==id) return te;
278                         /* only deeper on scene or object */
279                         if( te->idcode==ID_OB || te->idcode==ID_SCE || (soops->outlinevis == SO_GROUPS && te->idcode==ID_GR)) {
280                                 tes= outliner_find_id(soops, &te->subtree, id);
281                                 if(tes) return tes;
282                         }
283                 }
284         }
285         return NULL;
286 }
287
288
289 ID *outliner_search_back(SpaceOops *soops, TreeElement *te, short idcode)
290 {
291         TreeStoreElem *tselem;
292         te= te->parent;
293         
294         while(te) {
295                 tselem= TREESTORE(te);
296                 if(tselem->type==0 && te->idcode==idcode) return tselem->id;
297                 te= te->parent;
298         }
299         return NULL;
300 }
301
302
303 /* ********************************************************* */
304
305 /* Prototype, see functions below */
306 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
307                                                                                  TreeElement *parent, short type, short index);
308
309 /* -------------------------------------------------------- */
310
311 /* special handling of hierarchical non-lib data */
312 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone, 
313                                                           TreeElement *parent, int *a)
314 {
315         TreeElement *te= outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
316         
317         (*a)++;
318         te->name= curBone->name;
319         te->directdata= curBone;
320         
321         for(curBone= curBone->childbase.first; curBone; curBone=curBone->next) {
322                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
323         }
324 }
325
326 /* -------------------------------------------------------- */
327
328 #define LOG2I(x) (int)(log(x)/M_LN2)
329
330 static void outliner_add_passes(SpaceOops *soops, TreeElement *tenla, ID *id, SceneRenderLayer *srl)
331 {
332         TreeStoreElem *tselem = NULL;
333         TreeElement *te = NULL;
334
335         /* log stuff is to convert bitflags (powers of 2) to small integers,
336          * in order to not overflow short tselem->nr */
337         
338         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_COMBINED));
339         te->name= "Combined";
340         te->directdata= &srl->passflag;
341         
342         /* save cpu cycles, but we add the first to invoke an open/close triangle */
343         tselem = TREESTORE(tenla);
344         if(tselem->flag & TSE_CLOSED)
345                 return;
346         
347         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_Z));
348         te->name= "Z";
349         te->directdata= &srl->passflag;
350         
351         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_VECTOR));
352         te->name= "Vector";
353         te->directdata= &srl->passflag;
354         
355         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_NORMAL));
356         te->name= "Normal";
357         te->directdata= &srl->passflag;
358         
359         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_UV));
360         te->name= "UV";
361         te->directdata= &srl->passflag;
362         
363         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_MIST));
364         te->name= "Mist";
365         te->directdata= &srl->passflag;
366         
367         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_INDEXOB));
368         te->name= "Index Object";
369         te->directdata= &srl->passflag;
370         
371         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_INDEXMA));
372         te->name= "Index Material";
373         te->directdata= &srl->passflag;
374
375         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_RGBA));
376         te->name= "Color";
377         te->directdata= &srl->passflag;
378         
379         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_DIFFUSE));
380         te->name= "Diffuse";
381         te->directdata= &srl->passflag;
382         
383         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_SPEC));
384         te->name= "Specular";
385         te->directdata= &srl->passflag;
386         
387         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_SHADOW));
388         te->name= "Shadow";
389         te->directdata= &srl->passflag;
390         
391         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_AO));
392         te->name= "AO";
393         te->directdata= &srl->passflag;
394         
395         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_REFLECT));
396         te->name= "Reflection";
397         te->directdata= &srl->passflag;
398         
399         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_REFRACT));
400         te->name= "Refraction";
401         te->directdata= &srl->passflag;
402         
403         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_INDIRECT));
404         te->name= "Indirect";
405         te->directdata= &srl->passflag;
406
407         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_ENVIRONMENT));
408         te->name= "Environment";
409         te->directdata= &srl->passflag;
410
411         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_EMIT));
412         te->name= "Emit";
413         te->directdata= &srl->passflag;
414 }
415
416 #undef LOG2I
417
418 static void outliner_add_scene_contents(SpaceOops *soops, ListBase *lb, Scene *sce, TreeElement *te)
419 {
420         SceneRenderLayer *srl;
421         TreeElement *tenla= outliner_add_element(soops, lb, sce, te, TSE_R_LAYER_BASE, 0);
422         int a;
423         
424         tenla->name= "RenderLayers";
425         for(a=0, srl= sce->r.layers.first; srl; srl= srl->next, a++) {
426                 TreeElement *tenlay= outliner_add_element(soops, &tenla->subtree, sce, te, TSE_R_LAYER, a);
427                 tenlay->name= srl->name;
428                 tenlay->directdata= &srl->passflag;
429                 
430                 if(srl->light_override)
431                         outliner_add_element(soops, &tenlay->subtree, srl->light_override, tenlay, TSE_LINKED_LAMP, 0);
432                 if(srl->mat_override)
433                         outliner_add_element(soops, &tenlay->subtree, srl->mat_override, tenlay, TSE_LINKED_MAT, 0);
434                 
435                 outliner_add_passes(soops, tenlay, &sce->id, srl);
436         }
437         
438         // TODO: move this to the front?
439         if (sce->adt)
440                 outliner_add_element(soops, lb, sce, te, TSE_ANIM_DATA, 0);
441         
442         outliner_add_element(soops,  lb, sce->world, te, 0, 0);
443 }
444
445 // can be inlined if necessary
446 static void outliner_add_object_contents(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, Object *ob)
447 {
448         int a = 0;
449         
450         if (ob->adt)
451                 outliner_add_element(soops, &te->subtree, ob, te, TSE_ANIM_DATA, 0);
452         
453         outliner_add_element(soops, &te->subtree, ob->poselib, te, 0, 0); // XXX FIXME.. add a special type for this
454         
455         if (ob->proxy && ob->id.lib==NULL)
456                 outliner_add_element(soops, &te->subtree, ob->proxy, te, TSE_PROXY, 0);
457         
458         outliner_add_element(soops, &te->subtree, ob->data, te, 0, 0);
459         
460         if (ob->pose) {
461                 bArmature *arm= ob->data;
462                 bPoseChannel *pchan;
463                 TreeElement *ten;
464                 TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSE_BASE, 0);
465                 
466                 tenla->name= "Pose";
467                 
468                 /* channels undefined in editmode, but we want the 'tenla' pose icon itself */
469                 if ((arm->edbo == NULL) && (ob->mode & OB_MODE_POSE)) {
470                         int a= 0, const_index= 1000;    /* ensure unique id for bone constraints */
471                         
472                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
473                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSE_CHANNEL, a);
474                                 ten->name= pchan->name;
475                                 ten->directdata= pchan;
476                                 pchan->prev= (bPoseChannel *)ten;
477                                 
478                                 if(pchan->constraints.first) {
479                                         //Object *target;
480                                         bConstraint *con;
481                                         TreeElement *ten1;
482                                         TreeElement *tenla1= outliner_add_element(soops, &ten->subtree, ob, ten, TSE_CONSTRAINT_BASE, 0);
483                                         //char *str;
484                                         
485                                         tenla1->name= "Constraints";
486                                         for(con= pchan->constraints.first; con; con= con->next, const_index++) {
487                                                 ten1= outliner_add_element(soops, &tenla1->subtree, ob, tenla1, TSE_CONSTRAINT, const_index);
488 #if 0 /* disabled as it needs to be reworked for recoded constraints system */
489                                                 target= get_constraint_target(con, &str);
490                                                 if(str && str[0]) ten1->name= str;
491                                                 else if(target) ten1->name= target->id.name+2;
492                                                 else ten1->name= con->name;
493 #endif
494                                                 ten1->name= con->name;
495                                                 ten1->directdata= con;
496                                                 /* possible add all other types links? */
497                                         }
498                                 }
499                         }
500                         /* make hierarchy */
501                         ten= tenla->subtree.first;
502                         while(ten) {
503                                 TreeElement *nten= ten->next, *par;
504                                 tselem= TREESTORE(ten);
505                                 if(tselem->type==TSE_POSE_CHANNEL) {
506                                         pchan= (bPoseChannel *)ten->directdata;
507                                         if(pchan->parent) {
508                                                 BLI_remlink(&tenla->subtree, ten);
509                                                 par= (TreeElement *)pchan->parent->prev;
510                                                 BLI_addtail(&par->subtree, ten);
511                                                 ten->parent= par;
512                                         }
513                                 }
514                                 ten= nten;
515                         }
516                         /* restore prev pointers */
517                         pchan= ob->pose->chanbase.first;
518                         if(pchan) pchan->prev= NULL;
519                         for(; pchan; pchan= pchan->next) {
520                                 if(pchan->next) pchan->next->prev= pchan;
521                         }
522                 }
523                 
524                 /* Pose Groups */
525                 if(ob->pose->agroups.first) {
526                         bActionGroup *agrp;
527                         TreeElement *ten;
528                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSEGRP_BASE, 0);
529                         int a= 0;
530                         
531                         tenla->name= "Bone Groups";
532                         for (agrp=ob->pose->agroups.first; agrp; agrp=agrp->next, a++) {
533                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSEGRP, a);
534                                 ten->name= agrp->name;
535                                 ten->directdata= agrp;
536                         }
537                 }
538         }
539         
540         for(a=0; a<ob->totcol; a++) 
541                 outliner_add_element(soops, &te->subtree, ob->mat[a], te, 0, a);
542         
543         if(ob->constraints.first) {
544                 //Object *target;
545                 bConstraint *con;
546                 TreeElement *ten;
547                 TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_CONSTRAINT_BASE, 0);
548                 //char *str;
549                 
550                 tenla->name= "Constraints";
551                 for (con=ob->constraints.first, a=0; con; con= con->next, a++) {
552                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_CONSTRAINT, a);
553 #if 0 /* disabled due to constraints system targets recode... code here needs review */
554                         target= get_constraint_target(con, &str);
555                         if(str && str[0]) ten->name= str;
556                         else if(target) ten->name= target->id.name+2;
557                         else ten->name= con->name;
558 #endif
559                         ten->name= con->name;
560                         ten->directdata= con;
561                         /* possible add all other types links? */
562                 }
563         }
564         
565         if (ob->modifiers.first) {
566                 ModifierData *md;
567                 TreeElement *temod = outliner_add_element(soops, &te->subtree, ob, te, TSE_MODIFIER_BASE, 0);
568                 int index;
569                 
570                 temod->name = "Modifiers";
571                 for (index=0,md=ob->modifiers.first; md; index++,md=md->next) {
572                         TreeElement *te = outliner_add_element(soops, &temod->subtree, ob, temod, TSE_MODIFIER, index);
573                         te->name= md->name;
574                         te->directdata = md;
575                         
576                         if (md->type==eModifierType_Lattice) {
577                                 outliner_add_element(soops, &te->subtree, ((LatticeModifierData*) md)->object, te, TSE_LINKED_OB, 0);
578                         } 
579                         else if (md->type==eModifierType_Curve) {
580                                 outliner_add_element(soops, &te->subtree, ((CurveModifierData*) md)->object, te, TSE_LINKED_OB, 0);
581                         } 
582                         else if (md->type==eModifierType_Armature) {
583                                 outliner_add_element(soops, &te->subtree, ((ArmatureModifierData*) md)->object, te, TSE_LINKED_OB, 0);
584                         } 
585                         else if (md->type==eModifierType_Hook) {
586                                 outliner_add_element(soops, &te->subtree, ((HookModifierData*) md)->object, te, TSE_LINKED_OB, 0);
587                         } 
588                         else if (md->type==eModifierType_ParticleSystem) {
589                                 TreeElement *ten;
590                                 ParticleSystem *psys= ((ParticleSystemModifierData*) md)->psys;
591                                 
592                                 ten = outliner_add_element(soops, &te->subtree, ob, te, TSE_LINKED_PSYS, 0);
593                                 ten->directdata = psys;
594                                 ten->name = psys->part->id.name+2;
595                         }
596                 }
597         }
598         
599         /* vertex groups */
600         if (ob->defbase.first) {
601                 bDeformGroup *defgroup;
602                 TreeElement *ten;
603                 TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_DEFGROUP_BASE, 0);
604                 
605                 tenla->name= "Vertex Groups";
606                 for (defgroup=ob->defbase.first, a=0; defgroup; defgroup=defgroup->next, a++) {
607                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_DEFGROUP, a);
608                         ten->name= defgroup->name;
609                         ten->directdata= defgroup;
610                 }
611         }
612         
613         /* duplicated group */
614         if (ob->dup_group)
615                 outliner_add_element(soops, &te->subtree, ob->dup_group, te, 0, 0);     
616 }
617
618 // can be inlined if necessary
619 static void outliner_add_id_contents(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, ID *id)
620 {
621         /* tuck pointer back in object, to construct hierarchy */
622         if (GS(id->name)==ID_OB) id->newid= (ID *)te;
623         
624         /* expand specific data always */
625         switch (GS(id->name)) {
626                 case ID_LI:
627                 {
628                         te->name= ((Library *)id)->name;
629                 }
630                         break;
631                 case ID_SCE:
632                 {
633                         outliner_add_scene_contents(soops, &te->subtree, (Scene *)id, te);
634                 }
635                         break;
636                 case ID_OB:
637                 {
638                         outliner_add_object_contents(soops, te, tselem, (Object *)id);
639                 }
640                         break;
641                 case ID_ME:
642                 {
643                         Mesh *me= (Mesh *)id;
644                         int a;
645                         
646                         if (me->adt)
647                                 outliner_add_element(soops, &te->subtree, me, te, TSE_ANIM_DATA, 0);
648                         
649                         outliner_add_element(soops, &te->subtree, me->key, te, 0, 0);
650                         for(a=0; a<me->totcol; a++) 
651                                 outliner_add_element(soops, &te->subtree, me->mat[a], te, 0, a);
652                         /* could do tfaces with image links, but the images are not grouped nicely.
653                            would require going over all tfaces, sort images in use. etc... */
654                 }
655                         break;
656                 case ID_CU:
657                 {
658                         Curve *cu= (Curve *)id;
659                         int a;
660                         
661                         if (cu->adt)
662                                 outliner_add_element(soops, &te->subtree, cu, te, TSE_ANIM_DATA, 0);
663                         
664                         for(a=0; a<cu->totcol; a++) 
665                                 outliner_add_element(soops, &te->subtree, cu->mat[a], te, 0, a);
666                 }
667                         break;
668                 case ID_MB:
669                 {
670                         MetaBall *mb= (MetaBall *)id;
671                         int a;
672                         
673                         if (mb->adt)
674                                 outliner_add_element(soops, &te->subtree, mb, te, TSE_ANIM_DATA, 0);
675                         
676                         for(a=0; a<mb->totcol; a++) 
677                                 outliner_add_element(soops, &te->subtree, mb->mat[a], te, 0, a);
678                 }
679                         break;
680                 case ID_MA:
681                 {
682                         Material *ma= (Material *)id;
683                         int a;
684                         
685                         if (ma->adt)
686                                 outliner_add_element(soops, &te->subtree, ma, te, TSE_ANIM_DATA, 0);
687                         
688                         for(a=0; a<MAX_MTEX; a++) {
689                                 if(ma->mtex[a]) outliner_add_element(soops, &te->subtree, ma->mtex[a]->tex, te, 0, a);
690                         }
691                 }
692                         break;
693                 case ID_TE:
694                 {
695                         Tex *tex= (Tex *)id;
696                         
697                         if (tex->adt)
698                                 outliner_add_element(soops, &te->subtree, tex, te, TSE_ANIM_DATA, 0);
699                         
700                         outliner_add_element(soops, &te->subtree, tex->ima, te, 0, 0);
701                 }
702                         break;
703                 case ID_CA:
704                 {
705                         Camera *ca= (Camera *)id;
706                         
707                         if (ca->adt)
708                                 outliner_add_element(soops, &te->subtree, ca, te, TSE_ANIM_DATA, 0);
709                 }
710                         break;
711                 case ID_LA:
712                 {
713                         Lamp *la= (Lamp *)id;
714                         int a;
715                         
716                         if (la->adt)
717                                 outliner_add_element(soops, &te->subtree, la, te, TSE_ANIM_DATA, 0);
718                         
719                         for(a=0; a<MAX_MTEX; a++) {
720                                 if(la->mtex[a]) outliner_add_element(soops, &te->subtree, la->mtex[a]->tex, te, 0, a);
721                         }
722                 }
723                         break;
724                 case ID_SPK:
725                         {
726                                 Speaker *spk= (Speaker *)id;
727
728                                 if(spk->adt)
729                                         outliner_add_element(soops, &te->subtree, spk, te, TSE_ANIM_DATA, 0);
730                         }
731                         break;
732                 case ID_WO:
733                 {
734                         World *wrld= (World *)id;
735                         int a;
736                         
737                         if (wrld->adt)
738                                 outliner_add_element(soops, &te->subtree, wrld, te, TSE_ANIM_DATA, 0);
739                         
740                         for(a=0; a<MAX_MTEX; a++) {
741                                 if(wrld->mtex[a]) outliner_add_element(soops, &te->subtree, wrld->mtex[a]->tex, te, 0, a);
742                         }
743                 }
744                         break;
745                 case ID_KE:
746                 {
747                         Key *key= (Key *)id;
748                         
749                         if (key->adt)
750                                 outliner_add_element(soops, &te->subtree, key, te, TSE_ANIM_DATA, 0);
751                 }
752                         break;
753                 case ID_AC:
754                 {
755                         // XXX do we want to be exposing the F-Curves here?
756                         //bAction *act= (bAction *)id;
757                 }
758                         break;
759                 case ID_AR:
760                 {
761                         bArmature *arm= (bArmature *)id;
762                         int a= 0;
763                         
764                         if (arm->adt)
765                                 outliner_add_element(soops, &te->subtree, arm, te, TSE_ANIM_DATA, 0);
766                         
767                         if(arm->edbo) {
768                                 EditBone *ebone;
769                                 TreeElement *ten;
770                                 
771                                 for (ebone = arm->edbo->first; ebone; ebone=ebone->next, a++) {
772                                         ten= outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
773                                         ten->directdata= ebone;
774                                         ten->name= ebone->name;
775                                         ebone->temp= ten;
776                                 }
777                                 /* make hierarchy */
778                                 ten= te->subtree.first;
779                                 while(ten) {
780                                         TreeElement *nten= ten->next, *par;
781                                         ebone= (EditBone *)ten->directdata;
782                                         if(ebone->parent) {
783                                                 BLI_remlink(&te->subtree, ten);
784                                                 par= ebone->parent->temp;
785                                                 BLI_addtail(&par->subtree, ten);
786                                                 ten->parent= par;
787                                         }
788                                         ten= nten;
789                                 }
790                         }
791                         else {
792                                 /* do not extend Armature when we have posemode */
793                                 tselem= TREESTORE(te->parent);
794                                 if( GS(tselem->id->name)==ID_OB && ((Object *)tselem->id)->mode & OB_MODE_POSE);
795                                 else {
796                                         Bone *curBone;
797                                         for (curBone=arm->bonebase.first; curBone; curBone=curBone->next){
798                                                 outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
799                                         }
800                                 }
801                         }
802                 }
803                         break;
804         }
805 }
806
807 // TODO: this function needs to be split up! It's getting a bit too large...
808 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
809                                                                                  TreeElement *parent, short type, short index)
810 {
811         TreeElement *te;
812         TreeStoreElem *tselem;
813         ID *id= idv;
814         int a = 0;
815         
816         if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
817                 id= ((PointerRNA*)idv)->id.data;
818                 if(!id) id= ((PointerRNA*)idv)->data;
819         }
820
821         if(id==NULL) return NULL;
822
823         te= MEM_callocN(sizeof(TreeElement), "tree elem");
824         /* add to the visual tree */
825         BLI_addtail(lb, te);
826         /* add to the storage */
827         check_persistant(soops, te, id, type, index);
828         tselem= TREESTORE(te);  
829         
830         te->parent= parent;
831         te->index= index;       // for data arays
832         if(ELEM3(type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP));
833         else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM));
834         else if(type==TSE_ANIM_DATA);
835         else {
836                 te->name= id->name+2; // default, can be overridden by Library or non-ID data
837                 te->idcode= GS(id->name);
838         }
839         
840         if(type==0) {
841                 /* ID datablock */
842                 outliner_add_id_contents(soops, te, tselem, id);
843         }
844         else if(type==TSE_ANIM_DATA) {
845                 IdAdtTemplate *iat = (IdAdtTemplate *)idv;
846                 AnimData *adt= (AnimData *)iat->adt;
847                 
848                 /* this element's info */
849                 te->name= "Animation";
850                 te->directdata= adt;
851                 
852                 /* Action */
853                 outliner_add_element(soops, &te->subtree, adt->action, te, 0, 0);
854                 
855                 /* Drivers */
856                 if (adt->drivers.first) {
857                         TreeElement *ted= outliner_add_element(soops, &te->subtree, adt, te, TSE_DRIVER_BASE, 0);
858                         ID *lastadded= NULL;
859                         FCurve *fcu;
860                         
861                         ted->name= "Drivers";
862                 
863                         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
864                                 if (fcu->driver && fcu->driver->variables.first)  {
865                                         ChannelDriver *driver= fcu->driver;
866                                         DriverVar *dvar;
867                                         
868                                         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
869                                                 /* loop over all targets used here */
870                                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
871                                                 {
872                                                         if (lastadded != dtar->id) {
873                                                                 // XXX this lastadded check is rather lame, and also fails quite badly...
874                                                                 outliner_add_element(soops, &ted->subtree, dtar->id, ted, TSE_LINKED_OB, 0);
875                                                                 lastadded= dtar->id;
876                                                         }
877                                                 }
878                                                 DRIVER_TARGETS_LOOPER_END
879                                         }
880                                 }
881                         }
882                 }
883                 
884                 /* NLA Data */
885                 if (adt->nla_tracks.first) {
886                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, adt, te, TSE_NLA, 0);
887                         NlaTrack *nlt;
888                         int a= 0;
889                         
890                         tenla->name= "NLA Tracks";
891                         
892                         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
893                                 TreeElement *tenlt= outliner_add_element(soops, &tenla->subtree, nlt, tenla, TSE_NLA_TRACK, a);
894                                 NlaStrip *strip;
895                                 TreeElement *ten;
896                                 int b= 0;
897                                 
898                                 tenlt->name= nlt->name;
899                                 
900                                 for (strip=nlt->strips.first; strip; strip=strip->next, b++) {
901                                         ten= outliner_add_element(soops, &tenlt->subtree, strip->act, tenlt, TSE_NLA_ACTION, b);
902                                         if(ten) ten->directdata= strip;
903                                 }
904                         }
905                 }
906         }
907         else if(type==TSE_SEQUENCE) {
908                 Sequence *seq= (Sequence*) idv;
909                 Sequence *p;
910
911                 /*
912                  * The idcode is a little hack, but the outliner
913                  * only check te->idcode if te->type is equal to zero,
914                  * so this is "safe".
915                  */
916                 te->idcode= seq->type;
917                 te->directdata= seq;
918
919                 if(seq->type<7) {
920                         /*
921                          * This work like the sequence.
922                          * If the sequence have a name (not default name)
923                          * show it, in other case put the filename.
924                          */
925                         if(strcmp(seq->name, "SQ"))
926                                 te->name= seq->name;
927                         else {
928                                 if((seq->strip) && (seq->strip->stripdata))
929                                         te->name= seq->strip->stripdata->name;
930                                 else
931                                         te->name= "SQ None";
932                         }
933
934                         if(seq->type==SEQ_META) {
935                                 te->name= "Meta Strip";
936                                 p= seq->seqbase.first;
937                                 while(p) {
938                                         outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
939                                         p= p->next;
940                                 }
941                         }
942                         else
943                                 outliner_add_element(soops, &te->subtree, (void*)seq->strip, te, TSE_SEQ_STRIP, index);
944                 }
945                 else
946                         te->name= "Effect";
947         }
948         else if(type==TSE_SEQ_STRIP) {
949                 Strip *strip= (Strip *)idv;
950
951                 if(strip->dir)
952                         te->name= strip->dir;
953                 else
954                         te->name= "Strip None";
955                 te->directdata= strip;
956         }
957         else if(type==TSE_SEQUENCE_DUP) {
958                 Sequence *seq= (Sequence*)idv;
959
960                 te->idcode= seq->type;
961                 te->directdata= seq;
962                 te->name= seq->strip->stripdata->name;
963         }
964         else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
965                 PointerRNA pptr, propptr, *ptr= (PointerRNA*)idv;
966                 PropertyRNA *prop, *iterprop;
967                 PropertyType proptype;
968                 int a, tot;
969
970                 /* we do lazy build, for speed and to avoid infinite recusion */
971
972                 if(ptr->data == NULL) {
973                         te->name= "(empty)";
974                 }
975                 else if(type == TSE_RNA_STRUCT) {
976                         /* struct */
977                         te->name= RNA_struct_name_get_alloc(ptr, NULL, 0);
978
979                         if(te->name)
980                                 te->flag |= TE_FREE_NAME;
981                         else
982                                 te->name= (char*)RNA_struct_ui_name(ptr->type);
983
984                         iterprop= RNA_struct_iterator_property(ptr->type);
985                         tot= RNA_property_collection_length(ptr, iterprop);
986
987                         /* auto open these cases */
988                         if(!parent || (RNA_property_type(parent->directdata)) == PROP_POINTER)
989                                 if(!tselem->used)
990                                         tselem->flag &= ~TSE_CLOSED;
991
992                         if(!(tselem->flag & TSE_CLOSED)) {
993                                 for(a=0; a<tot; a++)
994                                         outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_PROPERTY, a);
995                         }
996                         else if(tot)
997                                 te->flag |= TE_LAZY_CLOSED;
998
999                         te->rnaptr= *ptr;
1000                 }
1001                 else if(type == TSE_RNA_PROPERTY) {
1002                         /* property */
1003                         iterprop= RNA_struct_iterator_property(ptr->type);
1004                         RNA_property_collection_lookup_int(ptr, iterprop, index, &propptr);
1005
1006                         prop= propptr.data;
1007                         proptype= RNA_property_type(prop);
1008
1009                         te->name= (char*)RNA_property_ui_name(prop);
1010                         te->directdata= prop;
1011                         te->rnaptr= *ptr;
1012
1013                         if(proptype == PROP_POINTER) {
1014                                 pptr= RNA_property_pointer_get(ptr, prop);
1015
1016                                 if(pptr.data) {
1017                                         if(!(tselem->flag & TSE_CLOSED))
1018                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, -1);
1019                                         else
1020                                                 te->flag |= TE_LAZY_CLOSED;
1021                                 }
1022                         }
1023                         else if(proptype == PROP_COLLECTION) {
1024                                 tot= RNA_property_collection_length(ptr, prop);
1025
1026                                 if(!(tselem->flag & TSE_CLOSED)) {
1027                                         for(a=0; a<tot; a++) {
1028                                                 RNA_property_collection_lookup_int(ptr, prop, a, &pptr);
1029                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, a);
1030                                         }
1031                                 }
1032                                 else if(tot)
1033                                         te->flag |= TE_LAZY_CLOSED;
1034                         }
1035                         else if(ELEM3(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1036                                 tot= RNA_property_array_length(ptr, prop);
1037
1038                                 if(!(tselem->flag & TSE_CLOSED)) {
1039                                         for(a=0; a<tot; a++)
1040                                                 outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_ARRAY_ELEM, a);
1041                                 }
1042                                 else if(tot)
1043                                         te->flag |= TE_LAZY_CLOSED;
1044                         }
1045                 }
1046                 else if(type == TSE_RNA_ARRAY_ELEM) {
1047                         char c;
1048
1049                         prop= parent->directdata;
1050
1051                         te->directdata= prop;
1052                         te->rnaptr= *ptr;
1053                         te->index= index;
1054
1055                         c= RNA_property_array_item_char(prop, index);
1056
1057                         te->name= MEM_callocN(sizeof(char)*20, "OutlinerRNAArrayName");
1058                         if(c) sprintf((char *)te->name, "  %c", c);
1059                         else sprintf((char *)te->name, "  %d", index+1);
1060                         te->flag |= TE_FREE_NAME;
1061                 }
1062         }
1063         else if(type == TSE_KEYMAP) {
1064                 wmKeyMap *km= (wmKeyMap *)idv;
1065                 wmKeyMapItem *kmi;
1066                 char opname[OP_MAX_TYPENAME];
1067                 
1068                 te->directdata= idv;
1069                 te->name= km->idname;
1070                 
1071                 if(!(tselem->flag & TSE_CLOSED)) {
1072                         a= 0;
1073                         
1074                         for (kmi= km->items.first; kmi; kmi= kmi->next, a++) {
1075                                 const char *key= WM_key_event_string(kmi->type);
1076                                 
1077                                 if(key[0]) {
1078                                         wmOperatorType *ot= NULL;
1079                                         
1080                                         if(kmi->propvalue);
1081                                         else ot= WM_operatortype_find(kmi->idname, 0);
1082                                         
1083                                         if(ot || kmi->propvalue) {
1084                                                 TreeElement *ten= outliner_add_element(soops, &te->subtree, kmi, te, TSE_KEYMAP_ITEM, a);
1085                                                 
1086                                                 ten->directdata= kmi;
1087                                                 
1088                                                 if(kmi->propvalue) {
1089                                                         ten->name= "Modal map, not yet";
1090                                                 }
1091                                                 else {
1092                                                         WM_operator_py_idname(opname, ot->idname);
1093                                                         ten->name= BLI_strdup(opname);
1094                                                         ten->flag |= TE_FREE_NAME;
1095                                                 }
1096                                         }
1097                                 }
1098                         }
1099                 }
1100                 else 
1101                         te->flag |= TE_LAZY_CLOSED;
1102         }
1103
1104         return te;
1105 }
1106
1107 /* ======================================================= */
1108 /* Sequencer mode tree building */
1109
1110 /* Helped function to put duplicate sequence in the same tree. */
1111 static int need_add_seq_dup(Sequence *seq)
1112 {
1113         Sequence *p;
1114
1115         if((!seq->strip) || (!seq->strip->stripdata) || (!seq->strip->stripdata->name))
1116                 return(1);
1117
1118         /*
1119          * First check backward, if we found a duplicate
1120          * sequence before this, don't need it, just return.
1121          */
1122         p= seq->prev;
1123         while(p) {
1124                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1125                         p= p->prev;
1126                         continue;
1127                 }
1128
1129                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1130                         return(2);
1131                 p= p->prev;
1132         }
1133
1134         p= seq->next;
1135         while(p) {
1136                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1137                         p= p->next;
1138                         continue;
1139                 }
1140
1141                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1142                         return(0);
1143                 p= p->next;
1144         }
1145         return(1);
1146 }
1147
1148 static void outliner_add_seq_dup(SpaceOops *soops, Sequence *seq, TreeElement *te, short index)
1149 {
1150         TreeElement *ch;
1151         Sequence *p;
1152
1153         p= seq;
1154         while(p) {
1155                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1156                         p= p->next;
1157                         continue;
1158                 }
1159
1160                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1161                         ch= outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
1162                 p= p->next;
1163         }
1164 }
1165
1166 /* ======================================================= */
1167 /* Generic Tree Building helpers - order these are called is top to bottom */
1168
1169 /* Hierarchy --------------------------------------------- */
1170
1171 /* make sure elements are correctly nested */
1172 static void outliner_make_hierarchy(SpaceOops *soops, ListBase *lb)
1173 {
1174         TreeElement *te, *ten, *tep;
1175         TreeStoreElem *tselem;
1176
1177         /* build hierarchy */
1178         // XXX also, set extents here...
1179         te= lb->first;
1180         while(te) {
1181                 ten= te->next;
1182                 tselem= TREESTORE(te);
1183                 
1184                 if(tselem->type==0 && te->idcode==ID_OB) {
1185                         Object *ob= (Object *)tselem->id;
1186                         if(ob->parent && ob->parent->id.newid) {
1187                                 BLI_remlink(lb, te);
1188                                 tep= (TreeElement *)ob->parent->id.newid;
1189                                 BLI_addtail(&tep->subtree, te);
1190                                 // set correct parent pointers
1191                                 for(te=tep->subtree.first; te; te= te->next) te->parent= tep;
1192                         }
1193                 }
1194                 te= ten;
1195         }
1196 }
1197
1198 /* Sorting ------------------------------------------------------ */
1199
1200 typedef struct tTreeSort {
1201         TreeElement *te;
1202         ID *id;
1203         const char *name;
1204         short idcode;
1205 } tTreeSort;
1206
1207 /* alphabetical comparator */
1208 static int treesort_alpha(const void *v1, const void *v2)
1209 {
1210         const tTreeSort *x1= v1, *x2= v2;
1211         int comp;
1212         
1213         /* first put objects last (hierarchy) */
1214         comp= (x1->idcode==ID_OB);
1215         if(x2->idcode==ID_OB) comp+=2;
1216         
1217         if(comp==1) return 1;
1218         else if(comp==2) return -1;
1219         else if(comp==3) {
1220                 comp= strcmp(x1->name, x2->name);
1221                 
1222                 if( comp>0 ) return 1;
1223                 else if( comp<0) return -1;
1224                 return 0;
1225         }
1226         return 0;
1227 }
1228
1229 /* this is nice option for later? doesnt look too useful... */
1230 #if 0
1231 static int treesort_obtype_alpha(const void *v1, const void *v2)
1232 {
1233         const tTreeSort *x1= v1, *x2= v2;
1234         
1235         /* first put objects last (hierarchy) */
1236         if(x1->idcode==ID_OB && x2->idcode!=ID_OB) return 1;
1237         else if(x2->idcode==ID_OB && x1->idcode!=ID_OB) return -1;
1238         else {
1239                 /* 2nd we check ob type */
1240                 if(x1->idcode==ID_OB && x2->idcode==ID_OB) {
1241                         if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return 1;
1242                         else if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
1243                         else return 0;
1244                 }
1245                 else {
1246                         int comp= strcmp(x1->name, x2->name);
1247                         
1248                         if( comp>0 ) return 1;
1249                         else if( comp<0) return -1;
1250                         return 0;
1251                 }
1252         }
1253 }
1254 #endif
1255
1256 /* sort happens on each subtree individual */
1257 static void outliner_sort(SpaceOops *soops, ListBase *lb)
1258 {
1259         TreeElement *te;
1260         TreeStoreElem *tselem;
1261         int totelem=0;
1262         
1263         te= lb->last;
1264         if(te==NULL) return;
1265         tselem= TREESTORE(te);
1266         
1267         /* sorting rules; only object lists or deformgroups */
1268         if( (tselem->type==TSE_DEFGROUP) || (tselem->type==0 && te->idcode==ID_OB)) {
1269                 
1270                 /* count first */
1271                 for(te= lb->first; te; te= te->next) totelem++;
1272                 
1273                 if(totelem>1) {
1274                         tTreeSort *tear= MEM_mallocN(totelem*sizeof(tTreeSort), "tree sort array");
1275                         tTreeSort *tp=tear;
1276                         int skip= 0;
1277                         
1278                         for(te= lb->first; te; te= te->next, tp++) {
1279                                 tselem= TREESTORE(te);
1280                                 tp->te= te;
1281                                 tp->name= te->name;
1282                                 tp->idcode= te->idcode;
1283                                 if(tselem->type && tselem->type!=TSE_DEFGROUP) tp->idcode= 0;   // dont sort this
1284                                 tp->id= tselem->id;
1285                         }
1286                         /* keep beginning of list */
1287                         for(tp= tear, skip=0; skip<totelem; skip++, tp++)
1288                                 if(tp->idcode) break;
1289                         
1290                         if(skip<totelem)
1291                                 qsort(tear+skip, totelem-skip, sizeof(tTreeSort), treesort_alpha);
1292                         
1293                         lb->first=lb->last= NULL;
1294                         tp= tear;
1295                         while(totelem--) {
1296                                 BLI_addtail(lb, tp->te);
1297                                 tp++;
1298                         }
1299                         MEM_freeN(tear);
1300                 }
1301         }
1302         
1303         for(te= lb->first; te; te= te->next) {
1304                 outliner_sort(soops, &te->subtree);
1305         }
1306 }
1307
1308 /* Filtering ----------------------------------------------- */
1309
1310 static int outliner_filter_has_name(TreeElement *te, const char *name, int flags)
1311 {
1312 #if 0
1313         int found= 0;
1314         
1315         /* determine if match */
1316         if (flags & SO_FIND_CASE_SENSITIVE) {
1317                 if (flags & SO_FIND_COMPLETE)
1318                         found= strcmp(te->name, name) == 0;
1319                 else
1320                         found= strstr(te->name, name) != NULL;
1321         }
1322         else {
1323                 if (flags & SO_FIND_COMPLETE)
1324                         found= BLI_strcasecmp(te->name, name) == 0;
1325                 else
1326                         found= BLI_strcasestr(te->name, name) != NULL;
1327         }
1328 #else
1329         
1330         int fn_flag= 0;
1331         int found= 0;
1332         
1333         if ((flags & SO_FIND_CASE_SENSITIVE) == 0)
1334                 fn_flag |= FNM_CASEFOLD;
1335
1336         if (flags & SO_FIND_COMPLETE) {
1337                 found= fnmatch(name, te->name, fn_flag)==0;
1338         }
1339         else {
1340                 char fn_name[sizeof(((struct SpaceOops *)NULL)->search_string) + 2];
1341                 sprintf(fn_name, "*%s*", name);
1342                 found= fnmatch(fn_name, te->name, fn_flag)==0;
1343         }
1344         return found;
1345 #endif
1346 }
1347
1348 static int outliner_filter_tree(SpaceOops *soops, ListBase *lb)
1349 {
1350         TreeElement *te, *ten;
1351         TreeStoreElem *tselem;
1352         
1353         /* although we don't have any search string, we return TRUE 
1354          * since the entire tree is ok then...
1355          */
1356         if (soops->search_string[0]==0) 
1357                 return 1;
1358
1359         for (te= lb->first; te; te= ten) {
1360                 ten= te->next;
1361                 
1362                 if (0==outliner_filter_has_name(te, soops->search_string, soops->search_flags)) {
1363                         /* item isn't something we're looking for, but...
1364                          *      - if the subtree is expanded, check if there are any matches that can be easily found
1365                          *              so that searching for "cu" in the default scene will still match the Cube
1366                          *      - otherwise, we can't see within the subtree and the item doesn't match,
1367                          *              so these can be safely ignored (i.e. the subtree can get freed)
1368                          */
1369                         tselem= TREESTORE(te);
1370                         
1371                         if ((tselem->flag & TSE_CLOSED) || outliner_filter_tree(soops, &te->subtree)==0) { 
1372                                 outliner_free_tree(&te->subtree);
1373                                 BLI_remlink(lb, te);
1374                                 
1375                                 if(te->flag & TE_FREE_NAME) MEM_freeN((void *)te->name);
1376                                 MEM_freeN(te);
1377                         }
1378                 }
1379                 else {
1380                         /* filter subtree too */
1381                         outliner_filter_tree(soops, &te->subtree);
1382                 }
1383         }
1384         
1385         /* if there are still items in the list, that means that there were still some matches */
1386         return (lb->first != NULL);
1387 }
1388
1389 /* ======================================================= */
1390 /* Main Tree Building API */
1391
1392 /* Main entry point for building the tree data-structure that the outliner represents */
1393 // TODO: split each mode into its own function?
1394 void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops)
1395 {
1396         Base *base;
1397         Object *ob;
1398         TreeElement *te=NULL, *ten;
1399         TreeStoreElem *tselem;
1400         int show_opened= (soops->treestore==NULL); /* on first view, we open scenes */
1401
1402         if(soops->tree.first && (soops->storeflag & SO_TREESTORE_REDRAW))
1403                 return;
1404
1405         outliner_free_tree(&soops->tree);
1406         outliner_storage_cleanup(soops);
1407         
1408         /* clear ob id.new flags */
1409         for(ob= mainvar->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
1410         
1411         /* options */
1412         if(soops->outlinevis == SO_LIBRARIES) {
1413                 Library *lib;
1414                 
1415                 for(lib= mainvar->library.first; lib; lib= lib->id.next) {
1416                         ten= outliner_add_element(soops, &soops->tree, lib, NULL, 0, 0);
1417                         lib->id.newid= (ID *)ten;
1418                 }
1419                 /* make hierarchy */
1420                 ten= soops->tree.first;
1421                 while(ten) {
1422                         TreeElement *nten= ten->next, *par;
1423                         tselem= TREESTORE(ten);
1424                         lib= (Library *)tselem->id;
1425                         if(lib->parent) {
1426                                 BLI_remlink(&soops->tree, ten);
1427                                 par= (TreeElement *)lib->parent->id.newid;
1428                                 BLI_addtail(&par->subtree, ten);
1429                                 ten->parent= par;
1430                         }
1431                         ten= nten;
1432                 }
1433                 /* restore newid pointers */
1434                 for(lib= mainvar->library.first; lib; lib= lib->id.next)
1435                         lib->id.newid= NULL;
1436                 
1437         }
1438         else if(soops->outlinevis == SO_ALL_SCENES) {
1439                 Scene *sce;
1440                 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
1441                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
1442                         tselem= TREESTORE(te);
1443                         if(sce==scene && show_opened) 
1444                                 tselem->flag &= ~TSE_CLOSED;
1445                         
1446                         for(base= sce->base.first; base; base= base->next) {
1447                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
1448                                 ten->directdata= base;
1449                         }
1450                         outliner_make_hierarchy(soops, &te->subtree);
1451                         /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1452                         for(base= sce->base.first; base; base= base->next) base->object->id.newid= NULL;
1453                 }
1454         }
1455         else if(soops->outlinevis == SO_CUR_SCENE) {
1456                 
1457                 outliner_add_scene_contents(soops, &soops->tree, scene, NULL);
1458                 
1459                 for(base= scene->base.first; base; base= base->next) {
1460                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1461                         ten->directdata= base;
1462                 }
1463                 outliner_make_hierarchy(soops, &soops->tree);
1464         }
1465         else if(soops->outlinevis == SO_VISIBLE) {
1466                 for(base= scene->base.first; base; base= base->next) {
1467                         if(base->lay & scene->lay)
1468                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1469                 }
1470                 outliner_make_hierarchy(soops, &soops->tree);
1471         }
1472         else if(soops->outlinevis == SO_GROUPS) {
1473                 Group *group;
1474                 GroupObject *go;
1475                 
1476                 for(group= mainvar->group.first; group; group= group->id.next) {
1477                         if(group->gobject.first) {
1478                                 te= outliner_add_element(soops, &soops->tree, group, NULL, 0, 0);
1479                                 
1480                                 for(go= group->gobject.first; go; go= go->next) {
1481                                         ten= outliner_add_element(soops, &te->subtree, go->ob, te, 0, 0);
1482                                         ten->directdata= NULL; /* eh, why? */
1483                                 }
1484                                 outliner_make_hierarchy(soops, &te->subtree);
1485                                 /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1486                                 for(go= group->gobject.first; go; go= go->next) go->ob->id.newid= NULL;
1487                         }
1488                 }
1489         }
1490         else if(soops->outlinevis == SO_SAME_TYPE) {
1491                 Object *ob= OBACT;
1492                 if(ob) {
1493                         for(base= scene->base.first; base; base= base->next) {
1494                                 if(base->object->type==ob->type) {
1495                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1496                                         ten->directdata= base;
1497                                 }
1498                         }
1499                         outliner_make_hierarchy(soops, &soops->tree);
1500                 }
1501         }
1502         else if(soops->outlinevis == SO_SELECTED) {
1503                 for(base= scene->base.first; base; base= base->next) {
1504                         if(base->lay & scene->lay) {
1505                                 if(base==BASACT || (base->flag & SELECT)) {
1506                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1507                                         ten->directdata= base;
1508                                 }
1509                         }
1510                 }
1511                 outliner_make_hierarchy(soops, &soops->tree);
1512         }
1513         else if(soops->outlinevis==SO_SEQUENCE) {
1514                 Sequence *seq;
1515                 Editing *ed= seq_give_editing(scene, FALSE);
1516                 int op;
1517
1518                 if(ed==NULL)
1519                         return;
1520
1521                 seq= ed->seqbasep->first;
1522                 if(!seq)
1523                         return;
1524
1525                 while(seq) {
1526                         op= need_add_seq_dup(seq);
1527                         if(op==1)
1528                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1529                         else if(op==0) {
1530                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1531                                 outliner_add_seq_dup(soops, seq, ten, 0);
1532                         }
1533                         seq= seq->next;
1534                 }
1535         }
1536         else if(soops->outlinevis==SO_DATABLOCKS) {
1537                 PointerRNA mainptr;
1538
1539                 RNA_main_pointer_create(mainvar, &mainptr);
1540
1541                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1542
1543                 if(show_opened)  {
1544                         tselem= TREESTORE(ten);
1545                         tselem->flag &= ~TSE_CLOSED;
1546                 }
1547         }
1548         else if(soops->outlinevis==SO_USERDEF) {
1549                 PointerRNA userdefptr;
1550
1551                 RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &userdefptr);
1552
1553                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1554
1555                 if(show_opened)  {
1556                         tselem= TREESTORE(ten);
1557                         tselem->flag &= ~TSE_CLOSED;
1558                 }
1559         }
1560         else if(soops->outlinevis==SO_KEYMAP) {
1561                 wmWindowManager *wm= mainvar->wm.first;
1562                 wmKeyMap *km;
1563                 
1564                 for(km= wm->defaultconf->keymaps.first; km; km= km->next) {
1565                         ten= outliner_add_element(soops, &soops->tree, (void*)km, NULL, TSE_KEYMAP, 0);
1566                 }
1567         }
1568         else {
1569                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1570                 if(ten) ten->directdata= BASACT;
1571         }
1572
1573         outliner_sort(soops, &soops->tree);
1574         outliner_filter_tree(soops, &soops->tree);
1575 }
1576
1577