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