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