removed unneeded fnmatch include from outliner, comment files as needing header cleanup.
[blender-staging.git] / source / blender / editors / space_outliner / outliner_tree.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_outliner/outliner_tree.c
29  *  \ingroup spoutliner
30  */
31  
32 #include <math.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stddef.h>
36
37 #if defined WIN32 && !defined _LIBC  || defined __sun
38 # include "BLI_fnmatch.h" /* use fnmatch included in blenlib */
39 #else
40 #  ifndef _GNU_SOURCE
41 #    define _GNU_SOURCE
42 #  endif
43 # include <fnmatch.h>
44 #endif
45
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_anim_types.h"
49 #include "DNA_armature_types.h"
50 #include "DNA_constraint_types.h"
51 #include "DNA_camera_types.h"
52 #include "DNA_group_types.h"
53 #include "DNA_key_types.h"
54 #include "DNA_lamp_types.h"
55 #include "DNA_material_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_meta_types.h"
58 #include "DNA_particle_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_world_types.h"
61 #include "DNA_sequence_types.h"
62 #include "DNA_speaker_types.h"
63 #include "DNA_object_types.h"
64
65 #include "BLI_blenlib.h"
66 #include "BLI_utildefines.h"
67 #include "BLI_math_base.h"
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 /* Persistent 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_persistent(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 void outliner_cleanup_tree(SpaceOops *soops)
225 {
226         outliner_free_tree(&soops->tree);
227         outliner_storage_cleanup(soops);
228 }
229
230 /* Find ith item from the treestore */
231 static TreeElement *outliner_find_tree_element(ListBase *lb, int store_index)
232 {
233         TreeElement *te= lb->first, *tes;
234         while (te) {
235                 if (te->store_index==store_index) return te;
236                 tes= outliner_find_tree_element(&te->subtree, store_index);
237                 if (tes) return tes;
238                 te= te->next;
239         }
240         return NULL;
241 }
242
243 /* tse is not in the treestore, we use its contents to find a match */
244 TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
245 {
246         TreeStore *ts= soops->treestore;
247         TreeStoreElem *tselem;
248         int a;
249         
250         if (tse->id==NULL) return NULL;
251         
252         /* check if 'tse' is in treestore */
253         tselem= ts->data;
254         for (a=0; a<ts->usedelem; a++, tselem++) {
255                 if ((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
256                         if (tselem->id==tse->id) {
257                                 break;
258                         }
259                 }
260         }
261         if (tselem) 
262                 return outliner_find_tree_element(&soops->tree, a);
263         
264         return NULL;
265 }
266
267 /* Find treestore that refers to given ID */
268 TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
269 {
270         TreeElement *te, *tes;
271         TreeStoreElem *tselem;
272         
273         for (te= lb->first; te; te= te->next) {
274                 tselem= TREESTORE(te);
275                 if (tselem->type==0) {
276                         if (tselem->id==id) return te;
277                         /* only deeper on scene or object */
278                         if ( te->idcode==ID_OB || te->idcode==ID_SCE || (soops->outlinevis == SO_GROUPS && te->idcode==ID_GR)) {
279                                 tes= outliner_find_id(soops, &te->subtree, id);
280                                 if (tes) return tes;
281                         }
282                 }
283         }
284         return NULL;
285 }
286
287
288 ID *outliner_search_back(SpaceOops *soops, TreeElement *te, short idcode)
289 {
290         TreeStoreElem *tselem;
291         te= te->parent;
292         
293         while (te) {
294                 tselem= TREESTORE(te);
295                 if (tselem->type==0 && te->idcode==idcode) return tselem->id;
296                 te= te->parent;
297         }
298         return NULL;
299 }
300
301
302 /* ********************************************************* */
303
304 /* Prototype, see functions below */
305 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
306                                                                                  TreeElement *parent, short type, short index);
307
308 /* -------------------------------------------------------- */
309
310 /* special handling of hierarchical non-lib data */
311 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone, 
312                                                           TreeElement *parent, int *a)
313 {
314         TreeElement *te= outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
315         
316         (*a)++;
317         te->name= curBone->name;
318         te->directdata= curBone;
319         
320         for (curBone= curBone->childbase.first; curBone; curBone=curBone->next) {
321                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
322         }
323 }
324
325 /* -------------------------------------------------------- */
326
327 #define LOG2I(x) (int)(log(x)/M_LN2)
328
329 static void outliner_add_passes(SpaceOops *soops, TreeElement *tenla, ID *id, SceneRenderLayer *srl)
330 {
331         TreeStoreElem *tselem = NULL;
332         TreeElement *te = NULL;
333
334         /* log stuff is to convert bitflags (powers of 2) to small integers,
335          * in order to not overflow short tselem->nr */
336         
337         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_COMBINED));
338         te->name= "Combined";
339         te->directdata= &srl->passflag;
340         
341         /* save cpu cycles, but we add the first to invoke an open/close triangle */
342         tselem = TREESTORE(tenla);
343         if (tselem->flag & TSE_CLOSED)
344                 return;
345         
346         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_Z));
347         te->name= "Z";
348         te->directdata= &srl->passflag;
349         
350         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_VECTOR));
351         te->name= "Vector";
352         te->directdata= &srl->passflag;
353         
354         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_NORMAL));
355         te->name= "Normal";
356         te->directdata= &srl->passflag;
357         
358         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_UV));
359         te->name= "UV";
360         te->directdata= &srl->passflag;
361         
362         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_MIST));
363         te->name= "Mist";
364         te->directdata= &srl->passflag;
365         
366         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_INDEXOB));
367         te->name= "Index Object";
368         te->directdata= &srl->passflag;
369         
370         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_INDEXMA));
371         te->name= "Index Material";
372         te->directdata= &srl->passflag;
373
374         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_RGBA));
375         te->name= "Color";
376         te->directdata= &srl->passflag;
377         
378         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_DIFFUSE));
379         te->name= "Diffuse";
380         te->directdata= &srl->passflag;
381         
382         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_SPEC));
383         te->name= "Specular";
384         te->directdata= &srl->passflag;
385         
386         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_SHADOW));
387         te->name= "Shadow";
388         te->directdata= &srl->passflag;
389         
390         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_AO));
391         te->name= "AO";
392         te->directdata= &srl->passflag;
393         
394         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_REFLECT));
395         te->name= "Reflection";
396         te->directdata= &srl->passflag;
397         
398         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_REFRACT));
399         te->name= "Refraction";
400         te->directdata= &srl->passflag;
401         
402         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_INDIRECT));
403         te->name= "Indirect";
404         te->directdata= &srl->passflag;
405
406         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_ENVIRONMENT));
407         te->name= "Environment";
408         te->directdata= &srl->passflag;
409
410         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, LOG2I(SCE_PASS_EMIT));
411         te->name= "Emit";
412         te->directdata= &srl->passflag;
413 }
414
415 #undef LOG2I
416
417 static void outliner_add_scene_contents(SpaceOops *soops, ListBase *lb, Scene *sce, TreeElement *te)
418 {
419         SceneRenderLayer *srl;
420         TreeElement *tenla= outliner_add_element(soops, lb, sce, te, TSE_R_LAYER_BASE, 0);
421         int a;
422         
423         tenla->name= "RenderLayers";
424         for (a=0, srl= sce->r.layers.first; srl; srl= srl->next, a++) {
425                 TreeElement *tenlay= outliner_add_element(soops, &tenla->subtree, sce, te, TSE_R_LAYER, a);
426                 tenlay->name= srl->name;
427                 tenlay->directdata= &srl->passflag;
428                 
429                 if (srl->light_override)
430                         outliner_add_element(soops, &tenlay->subtree, srl->light_override, tenlay, TSE_LINKED_LAMP, 0);
431                 if (srl->mat_override)
432                         outliner_add_element(soops, &tenlay->subtree, srl->mat_override, tenlay, TSE_LINKED_MAT, 0);
433                 
434                 outliner_add_passes(soops, tenlay, &sce->id, srl);
435         }
436         
437         // TODO: move this to the front?
438         if (sce->adt)
439                 outliner_add_element(soops, lb, sce, te, TSE_ANIM_DATA, 0);
440         
441         outliner_add_element(soops,  lb, sce->world, te, 0, 0);
442 }
443
444 // can be inlined if necessary
445 static void outliner_add_object_contents(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, Object *ob)
446 {
447         int a = 0;
448         
449         if (ob->adt)
450                 outliner_add_element(soops, &te->subtree, ob, te, TSE_ANIM_DATA, 0);
451         
452         outliner_add_element(soops, &te->subtree, ob->poselib, te, 0, 0); // XXX FIXME.. add a special type for this
453         
454         if (ob->proxy && ob->id.lib==NULL)
455                 outliner_add_element(soops, &te->subtree, ob->proxy, te, TSE_PROXY, 0);
456         
457         outliner_add_element(soops, &te->subtree, ob->data, te, 0, 0);
458         
459         if (ob->pose) {
460                 bArmature *arm= ob->data;
461                 bPoseChannel *pchan;
462                 TreeElement *ten;
463                 TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSE_BASE, 0);
464                 
465                 tenla->name= "Pose";
466                 
467                 /* channels undefined in editmode, but we want the 'tenla' pose icon itself */
468                 if ((arm->edbo == NULL) && (ob->mode & OB_MODE_POSE)) {
469                         int a= 0, const_index= 1000;    /* ensure unique id for bone constraints */
470                         
471                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
472                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSE_CHANNEL, a);
473                                 ten->name= pchan->name;
474                                 ten->directdata= pchan;
475                                 pchan->temp= (void *)ten;
476                                 
477                                 if (pchan->constraints.first) {
478                                         //Object *target;
479                                         bConstraint *con;
480                                         TreeElement *ten1;
481                                         TreeElement *tenla1= outliner_add_element(soops, &ten->subtree, ob, ten, TSE_CONSTRAINT_BASE, 0);
482                                         //char *str;
483                                         
484                                         tenla1->name= "Constraints";
485                                         for (con= pchan->constraints.first; con; con= con->next, const_index++) {
486                                                 ten1= outliner_add_element(soops, &tenla1->subtree, ob, tenla1, TSE_CONSTRAINT, const_index);
487 #if 0 /* disabled as it needs to be reworked for recoded constraints system */
488                                                 target= get_constraint_target(con, &str);
489                                                 if (str && str[0]) ten1->name= str;
490                                                 else if (target) ten1->name= target->id.name+2;
491                                                 else ten1->name= con->name;
492 #endif
493                                                 ten1->name= con->name;
494                                                 ten1->directdata= con;
495                                                 /* possible add all other types links? */
496                                         }
497                                 }
498                         }
499                         /* make hierarchy */
500                         ten= tenla->subtree.first;
501                         while (ten) {
502                                 TreeElement *nten= ten->next, *par;
503                                 tselem= TREESTORE(ten);
504                                 if (tselem->type==TSE_POSE_CHANNEL) {
505                                         pchan= (bPoseChannel *)ten->directdata;
506                                         if (pchan->parent) {
507                                                 BLI_remlink(&tenla->subtree, ten);
508                                                 par= (TreeElement *)pchan->parent->temp;
509                                                 BLI_addtail(&par->subtree, ten);
510                                                 ten->parent= par;
511                                         }
512                                 }
513                                 ten= nten;
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_SPK:
718                         {
719                                 Speaker *spk= (Speaker *)id;
720
721                                 if (spk->adt)
722                                         outliner_add_element(soops, &te->subtree, spk, te, TSE_ANIM_DATA, 0);
723                         }
724                         break;
725                 case ID_WO:
726                 {
727                         World *wrld= (World *)id;
728                         int a;
729                         
730                         if (wrld->adt)
731                                 outliner_add_element(soops, &te->subtree, wrld, te, TSE_ANIM_DATA, 0);
732                         
733                         for (a=0; a<MAX_MTEX; a++) {
734                                 if (wrld->mtex[a]) outliner_add_element(soops, &te->subtree, wrld->mtex[a]->tex, te, 0, a);
735                         }
736                 }
737                         break;
738                 case ID_KE:
739                 {
740                         Key *key= (Key *)id;
741                         
742                         if (key->adt)
743                                 outliner_add_element(soops, &te->subtree, key, te, TSE_ANIM_DATA, 0);
744                 }
745                         break;
746                 case ID_AC:
747                 {
748                         // XXX do we want to be exposing the F-Curves here?
749                         //bAction *act= (bAction *)id;
750                 }
751                         break;
752                 case ID_AR:
753                 {
754                         bArmature *arm= (bArmature *)id;
755                         int a= 0;
756                         
757                         if (arm->adt)
758                                 outliner_add_element(soops, &te->subtree, arm, te, TSE_ANIM_DATA, 0);
759                         
760                         if (arm->edbo) {
761                                 EditBone *ebone;
762                                 TreeElement *ten;
763                                 
764                                 for (ebone = arm->edbo->first; ebone; ebone=ebone->next, a++) {
765                                         ten= outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
766                                         ten->directdata= ebone;
767                                         ten->name= ebone->name;
768                                         ebone->temp= ten;
769                                 }
770                                 /* make hierarchy */
771                                 ten= arm->edbo->first ? ((EditBone *)arm->edbo->first)->temp : NULL;
772                                 while (ten) {
773                                         TreeElement *nten= ten->next, *par;
774                                         ebone= (EditBone *)ten->directdata;
775                                         if (ebone->parent) {
776                                                 BLI_remlink(&te->subtree, ten);
777                                                 par= ebone->parent->temp;
778                                                 BLI_addtail(&par->subtree, ten);
779                                                 ten->parent= par;
780                                         }
781                                         ten= nten;
782                                 }
783                         }
784                         else {
785                                 /* do not extend Armature when we have posemode */
786                                 tselem= TREESTORE(te->parent);
787                                 if ( GS(tselem->id->name)==ID_OB && ((Object *)tselem->id)->mode & OB_MODE_POSE);
788                                 else {
789                                         Bone *curBone;
790                                         for (curBone=arm->bonebase.first; curBone; curBone=curBone->next) {
791                                                 outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
792                                         }
793                                 }
794                         }
795                 }
796                         break;
797         }
798 }
799
800 // TODO: this function needs to be split up! It's getting a bit too large...
801 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
802                                                                                  TreeElement *parent, short type, short index)
803 {
804         TreeElement *te;
805         TreeStoreElem *tselem;
806         ID *id= idv;
807         int a = 0;
808         
809         if (ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
810                 id= ((PointerRNA*)idv)->id.data;
811                 if (!id) id= ((PointerRNA*)idv)->data;
812         }
813
814         if (id==NULL) return NULL;
815
816         te= MEM_callocN(sizeof(TreeElement), "tree elem");
817         /* add to the visual tree */
818         BLI_addtail(lb, te);
819         /* add to the storage */
820         check_persistent(soops, te, id, type, index);
821         tselem= TREESTORE(te);  
822         
823         /* if we are searching for something expand to see child elements */
824         if (SEARCHING_OUTLINER(soops))
825                 tselem->flag |= TSE_CHILDSEARCH;
826         
827         te->parent= parent;
828         te->index= index;       // for data arays
829         if (ELEM3(type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP));
830         else if (ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM));
831         else if (type==TSE_ANIM_DATA);
832         else {
833                 te->name= id->name+2; // default, can be overridden by Library or non-ID data
834                 te->idcode= GS(id->name);
835         }
836         
837         if (type==0) {
838                 /* ID datablock */
839                 outliner_add_id_contents(soops, te, tselem, id);
840         }
841         else if (type==TSE_ANIM_DATA) {
842                 IdAdtTemplate *iat = (IdAdtTemplate *)idv;
843                 AnimData *adt= (AnimData *)iat->adt;
844                 
845                 /* this element's info */
846                 te->name= "Animation";
847                 te->directdata= adt;
848                 
849                 /* Action */
850                 outliner_add_element(soops, &te->subtree, adt->action, te, 0, 0);
851                 
852                 /* Drivers */
853                 if (adt->drivers.first) {
854                         TreeElement *ted= outliner_add_element(soops, &te->subtree, adt, te, TSE_DRIVER_BASE, 0);
855                         ID *lastadded= NULL;
856                         FCurve *fcu;
857                         
858                         ted->name= "Drivers";
859                 
860                         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
861                                 if (fcu->driver && fcu->driver->variables.first) {
862                                         ChannelDriver *driver= fcu->driver;
863                                         DriverVar *dvar;
864                                         
865                                         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
866                                                 /* loop over all targets used here */
867                                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
868                                                 {
869                                                         if (lastadded != dtar->id) {
870                                                                 // XXX this lastadded check is rather lame, and also fails quite badly...
871                                                                 outliner_add_element(soops, &ted->subtree, dtar->id, ted, TSE_LINKED_OB, 0);
872                                                                 lastadded= dtar->id;
873                                                         }
874                                                 }
875                                                 DRIVER_TARGETS_LOOPER_END
876                                         }
877                                 }
878                         }
879                 }
880                 
881                 /* NLA Data */
882                 if (adt->nla_tracks.first) {
883                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, adt, te, TSE_NLA, 0);
884                         NlaTrack *nlt;
885                         int a= 0;
886                         
887                         tenla->name= "NLA Tracks";
888                         
889                         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
890                                 TreeElement *tenlt= outliner_add_element(soops, &tenla->subtree, nlt, tenla, TSE_NLA_TRACK, a);
891                                 NlaStrip *strip;
892                                 TreeElement *ten;
893                                 int b= 0;
894                                 
895                                 tenlt->name= nlt->name;
896                                 
897                                 for (strip=nlt->strips.first; strip; strip=strip->next, b++) {
898                                         ten= outliner_add_element(soops, &tenlt->subtree, strip->act, tenlt, TSE_NLA_ACTION, b);
899                                         if (ten) ten->directdata= strip;
900                                 }
901                         }
902                 }
903         }
904         else if (type==TSE_SEQUENCE) {
905                 Sequence *seq= (Sequence*) idv;
906                 Sequence *p;
907
908                 /*
909                  * The idcode is a little hack, but the outliner
910                  * only check te->idcode if te->type is equal to zero,
911                  * so this is "safe".
912                  */
913                 te->idcode= seq->type;
914                 te->directdata= seq;
915
916                 if (seq->type<7) {
917                         /*
918                          * This work like the sequence.
919                          * If the sequence have a name (not default name)
920                          * show it, in other case put the filename.
921                          */
922                         if (strcmp(seq->name, "SQ"))
923                                 te->name= seq->name;
924                         else {
925                                 if ((seq->strip) && (seq->strip->stripdata))
926                                         te->name= seq->strip->stripdata->name;
927                                 else
928                                         te->name= "SQ None";
929                         }
930
931                         if (seq->type==SEQ_META) {
932                                 te->name= "Meta Strip";
933                                 p= seq->seqbase.first;
934                                 while (p) {
935                                         outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
936                                         p= p->next;
937                                 }
938                         }
939                         else
940                                 outliner_add_element(soops, &te->subtree, (void*)seq->strip, te, TSE_SEQ_STRIP, index);
941                 }
942                 else
943                         te->name= "Effect";
944         }
945         else if (type==TSE_SEQ_STRIP) {
946                 Strip *strip= (Strip *)idv;
947
948                 if (strip->dir)
949                         te->name= strip->dir;
950                 else
951                         te->name= "Strip None";
952                 te->directdata= strip;
953         }
954         else if (type==TSE_SEQUENCE_DUP) {
955                 Sequence *seq= (Sequence*)idv;
956
957                 te->idcode= seq->type;
958                 te->directdata= seq;
959                 te->name= seq->strip->stripdata->name;
960         }
961         else if (ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
962                 PointerRNA pptr, propptr, *ptr= (PointerRNA*)idv;
963                 PropertyRNA *prop, *iterprop;
964                 PropertyType proptype;
965                 int a, tot;
966
967                 /* we do lazy build, for speed and to avoid infinite recusion */
968
969                 if (ptr->data == NULL) {
970                         te->name= "(empty)";
971                 }
972                 else if (type == TSE_RNA_STRUCT) {
973                         /* struct */
974                         te->name= RNA_struct_name_get_alloc(ptr, NULL, 0, NULL);
975
976                         if (te->name)
977                                 te->flag |= TE_FREE_NAME;
978                         else
979                                 te->name= RNA_struct_ui_name(ptr->type);
980
981                         /* If searching don't expand RNA entries */
982                         if (SEARCHING_OUTLINER(soops) && BLI_strcasecmp("RNA",te->name)==0) tselem->flag &= ~TSE_CHILDSEARCH;
983
984                         iterprop= RNA_struct_iterator_property(ptr->type);
985                         tot= RNA_property_collection_length(ptr, iterprop);
986
987                         /* auto open these cases */
988                         if (!parent || (RNA_property_type(parent->directdata)) == PROP_POINTER)
989                                 if (!tselem->used)
990                                         tselem->flag &= ~TSE_CLOSED;
991
992                         if (TSELEM_OPEN(tselem,soops)) {
993                                 for (a=0; a<tot; a++)
994                                         outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_PROPERTY, a);
995                         }
996                         else if (tot)
997                                 te->flag |= TE_LAZY_CLOSED;
998
999                         te->rnaptr= *ptr;
1000                 }
1001                 else if (type == TSE_RNA_PROPERTY) {
1002                         /* property */
1003                         iterprop= RNA_struct_iterator_property(ptr->type);
1004                         RNA_property_collection_lookup_int(ptr, iterprop, index, &propptr);
1005
1006                         prop= propptr.data;
1007                         proptype= RNA_property_type(prop);
1008
1009                         te->name= RNA_property_ui_name(prop);
1010                         te->directdata= prop;
1011                         te->rnaptr= *ptr;
1012
1013                         /* If searching don't expand RNA entries */
1014                         if (SEARCHING_OUTLINER(soops) && BLI_strcasecmp("RNA",te->name)==0) tselem->flag &= ~TSE_CHILDSEARCH;
1015
1016                         if (proptype == PROP_POINTER) {
1017                                 pptr= RNA_property_pointer_get(ptr, prop);
1018
1019                                 if (pptr.data) {
1020                                         if (TSELEM_OPEN(tselem,soops))
1021                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, -1);
1022                                         else
1023                                                 te->flag |= TE_LAZY_CLOSED;
1024                                 }
1025                         }
1026                         else if (proptype == PROP_COLLECTION) {
1027                                 tot= RNA_property_collection_length(ptr, prop);
1028
1029                                 if (TSELEM_OPEN(tselem,soops)) {
1030                                         for (a=0; a<tot; a++) {
1031                                                 RNA_property_collection_lookup_int(ptr, prop, a, &pptr);
1032                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, a);
1033                                         }
1034                                 }
1035                                 else if (tot)
1036                                         te->flag |= TE_LAZY_CLOSED;
1037                         }
1038                         else if (ELEM3(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1039                                 tot= RNA_property_array_length(ptr, prop);
1040
1041                                 if (TSELEM_OPEN(tselem,soops)) {
1042                                         for (a=0; a<tot; a++)
1043                                                 outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_ARRAY_ELEM, a);
1044                                 }
1045                                 else if (tot)
1046                                         te->flag |= TE_LAZY_CLOSED;
1047                         }
1048                 }
1049                 else if (type == TSE_RNA_ARRAY_ELEM) {
1050                         char c;
1051
1052                         prop= parent->directdata;
1053
1054                         te->directdata= prop;
1055                         te->rnaptr= *ptr;
1056                         te->index= index;
1057
1058                         c= RNA_property_array_item_char(prop, index);
1059
1060                         te->name= MEM_callocN(sizeof(char)*20, "OutlinerRNAArrayName");
1061                         if (c) sprintf((char *)te->name, "  %c", c);
1062                         else sprintf((char *)te->name, "  %d", index+1);
1063                         te->flag |= TE_FREE_NAME;
1064                 }
1065         }
1066         else if (type == TSE_KEYMAP) {
1067                 wmKeyMap *km= (wmKeyMap *)idv;
1068                 wmKeyMapItem *kmi;
1069                 char opname[OP_MAX_TYPENAME];
1070                 
1071                 te->directdata= idv;
1072                 te->name= km->idname;
1073                 
1074                 if (TSELEM_OPEN(tselem,soops)) {
1075                         a= 0;
1076                         
1077                         for (kmi = km->items.first; kmi; kmi = kmi->next, a++) {
1078                                 const char *key= WM_key_event_string(kmi->type);
1079                                 
1080                                 if (key[0]) {
1081                                         wmOperatorType *ot= NULL;
1082                                         
1083                                         if (kmi->propvalue);
1084                                         else ot= WM_operatortype_find(kmi->idname, 0);
1085                                         
1086                                         if (ot || kmi->propvalue) {
1087                                                 TreeElement *ten= outliner_add_element(soops, &te->subtree, kmi, te, TSE_KEYMAP_ITEM, a);
1088                                                 
1089                                                 ten->directdata= kmi;
1090                                                 
1091                                                 if (kmi->propvalue) {
1092                                                         ten->name= "Modal map, not yet";
1093                                                 }
1094                                                 else {
1095                                                         WM_operator_py_idname(opname, ot->idname);
1096                                                         ten->name= BLI_strdup(opname);
1097                                                         ten->flag |= TE_FREE_NAME;
1098                                                 }
1099                                         }
1100                                 }
1101                         }
1102                 }
1103                 else 
1104                         te->flag |= TE_LAZY_CLOSED;
1105         }
1106
1107         return te;
1108 }
1109
1110 /* ======================================================= */
1111 /* Sequencer mode tree building */
1112
1113 /* Helped function to put duplicate sequence in the same tree. */
1114 static int need_add_seq_dup(Sequence *seq)
1115 {
1116         Sequence *p;
1117
1118         if ((!seq->strip) || (!seq->strip->stripdata) || (!seq->strip->stripdata->name))
1119                 return(1);
1120
1121         /*
1122          * First check backward, if we found a duplicate
1123          * sequence before this, don't need it, just return.
1124          */
1125         p= seq->prev;
1126         while (p) {
1127                 if ((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1128                         p= p->prev;
1129                         continue;
1130                 }
1131
1132                 if (!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1133                         return(2);
1134                 p= p->prev;
1135         }
1136
1137         p= seq->next;
1138         while (p) {
1139                 if ((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1140                         p= p->next;
1141                         continue;
1142                 }
1143
1144                 if (!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1145                         return(0);
1146                 p= p->next;
1147         }
1148         return(1);
1149 }
1150
1151 static void outliner_add_seq_dup(SpaceOops *soops, Sequence *seq, TreeElement *te, short index)
1152 {
1153         /* TreeElement *ch; */ /* UNUSED */
1154         Sequence *p;
1155
1156         p= seq;
1157         while (p) {
1158                 if ((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1159                         p= p->next;
1160                         continue;
1161                 }
1162
1163                 if (!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1164                         /* ch= */ /* UNUSED */ outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
1165                 p= p->next;
1166         }
1167 }
1168
1169 /* ======================================================= */
1170 /* Generic Tree Building helpers - order these are called is top to bottom */
1171
1172 /* Hierarchy --------------------------------------------- */
1173
1174 /* make sure elements are correctly nested */
1175 static void outliner_make_hierarchy(SpaceOops *soops, ListBase *lb)
1176 {
1177         TreeElement *te, *ten, *tep;
1178         TreeStoreElem *tselem;
1179
1180         /* build hierarchy */
1181         // XXX also, set extents here...
1182         te= lb->first;
1183         while (te) {
1184                 ten= te->next;
1185                 tselem= TREESTORE(te);
1186                 
1187                 if (tselem->type==0 && te->idcode==ID_OB) {
1188                         Object *ob= (Object *)tselem->id;
1189                         if (ob->parent && ob->parent->id.newid) {
1190                                 BLI_remlink(lb, te);
1191                                 tep= (TreeElement *)ob->parent->id.newid;
1192                                 BLI_addtail(&tep->subtree, te);
1193                                 // set correct parent pointers
1194                                 for (te=tep->subtree.first; te; te= te->next) te->parent= tep;
1195                         }
1196                 }
1197                 te= ten;
1198         }
1199 }
1200
1201 /* Sorting ------------------------------------------------------ */
1202
1203 typedef struct tTreeSort {
1204         TreeElement *te;
1205         ID *id;
1206         const char *name;
1207         short idcode;
1208 } tTreeSort;
1209
1210 /* alphabetical comparator */
1211 static int treesort_alpha(const void *v1, const void *v2)
1212 {
1213         const tTreeSort *x1= v1, *x2= v2;
1214         int comp;
1215         
1216         /* first put objects last (hierarchy) */
1217         comp= (x1->idcode==ID_OB);
1218         if (x2->idcode==ID_OB) comp+=2;
1219         
1220         if (comp==1) return 1;
1221         else if (comp==2) return -1;
1222         else if (comp==3) {
1223                 comp= strcmp(x1->name, x2->name);
1224                 
1225                 if ( comp>0 ) return 1;
1226                 else if ( comp<0) return -1;
1227                 return 0;
1228         }
1229         return 0;
1230 }
1231
1232 /* this is nice option for later? doesnt look too useful... */
1233 #if 0
1234 static int treesort_obtype_alpha(const void *v1, const void *v2)
1235 {
1236         const tTreeSort *x1= v1, *x2= v2;
1237         
1238         /* first put objects last (hierarchy) */
1239         if (x1->idcode==ID_OB && x2->idcode!=ID_OB) return 1;
1240         else if (x2->idcode==ID_OB && x1->idcode!=ID_OB) return -1;
1241         else {
1242                 /* 2nd we check ob type */
1243                 if (x1->idcode==ID_OB && x2->idcode==ID_OB) {
1244                         if ( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return 1;
1245                         else if ( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
1246                         else return 0;
1247                 }
1248                 else {
1249                         int comp= strcmp(x1->name, x2->name);
1250                         
1251                         if ( comp>0 ) return 1;
1252                         else if ( comp<0) return -1;
1253                         return 0;
1254                 }
1255         }
1256 }
1257 #endif
1258
1259 /* sort happens on each subtree individual */
1260 static void outliner_sort(SpaceOops *soops, ListBase *lb)
1261 {
1262         TreeElement *te;
1263         TreeStoreElem *tselem;
1264         int totelem=0;
1265         
1266         te= lb->last;
1267         if (te==NULL) return;
1268         tselem= TREESTORE(te);
1269         
1270         /* sorting rules; only object lists or deformgroups */
1271         if ( (tselem->type==TSE_DEFGROUP) || (tselem->type==0 && te->idcode==ID_OB)) {
1272                 
1273                 /* count first */
1274                 for (te= lb->first; te; te= te->next) totelem++;
1275                 
1276                 if (totelem>1) {
1277                         tTreeSort *tear= MEM_mallocN(totelem*sizeof(tTreeSort), "tree sort array");
1278                         tTreeSort *tp=tear;
1279                         int skip= 0;
1280                         
1281                         for (te= lb->first; te; te= te->next, tp++) {
1282                                 tselem= TREESTORE(te);
1283                                 tp->te= te;
1284                                 tp->name= te->name;
1285                                 tp->idcode= te->idcode;
1286                                 if (tselem->type && tselem->type!=TSE_DEFGROUP) tp->idcode= 0;  // don't sort this
1287                                 tp->id= tselem->id;
1288                         }
1289                         /* keep beginning of list */
1290                         for (tp= tear, skip=0; skip<totelem; skip++, tp++)
1291                                 if (tp->idcode) break;
1292                         
1293                         if (skip<totelem)
1294                                 qsort(tear+skip, totelem-skip, sizeof(tTreeSort), treesort_alpha);
1295                         
1296                         lb->first=lb->last= NULL;
1297                         tp= tear;
1298                         while (totelem--) {
1299                                 BLI_addtail(lb, tp->te);
1300                                 tp++;
1301                         }
1302                         MEM_freeN(tear);
1303                 }
1304         }
1305         
1306         for (te= lb->first; te; te= te->next) {
1307                 outliner_sort(soops, &te->subtree);
1308         }
1309 }
1310
1311 /* Filtering ----------------------------------------------- */
1312
1313 static int outliner_filter_has_name(TreeElement *te, const char *name, int flags)
1314 {
1315 #if 0
1316         int found= 0;
1317         
1318         /* determine if match */
1319         if (flags & SO_FIND_CASE_SENSITIVE) {
1320                 if (flags & SO_FIND_COMPLETE)
1321                         found= strcmp(te->name, name) == 0;
1322                 else
1323                         found= strstr(te->name, name) != NULL;
1324         }
1325         else {
1326                 if (flags & SO_FIND_COMPLETE)
1327                         found= BLI_strcasecmp(te->name, name) == 0;
1328                 else
1329                         found= BLI_strcasestr(te->name, name) != NULL;
1330         }
1331 #else
1332         
1333         int fn_flag= 0;
1334         int found= 0;
1335         
1336         if ((flags & SO_FIND_CASE_SENSITIVE) == 0)
1337                 fn_flag |= FNM_CASEFOLD;
1338
1339         if (flags & SO_FIND_COMPLETE) {
1340                 found= fnmatch(name, te->name, fn_flag)==0;
1341         }
1342         else {
1343                 char fn_name[sizeof(((struct SpaceOops *)NULL)->search_string) + 2];
1344                 BLI_snprintf(fn_name, sizeof(fn_name), "*%s*", name);
1345                 found= fnmatch(fn_name, te->name, fn_flag)==0;
1346         }
1347         return found;
1348 #endif
1349 }
1350
1351 static int outliner_filter_tree(SpaceOops *soops, ListBase *lb)
1352 {
1353         TreeElement *te, *ten;
1354         TreeStoreElem *tselem;
1355         
1356         /* although we don't have any search string, we return TRUE 
1357          * since the entire tree is ok then...
1358          */
1359         if (soops->search_string[0]==0) 
1360                 return 1;
1361
1362         for (te= lb->first; te; te= ten) {
1363                 ten= te->next;
1364                 
1365                 if (0==outliner_filter_has_name(te, soops->search_string, soops->search_flags)) {
1366                         /* item isn't something we're looking for, but...
1367                          *      - if the subtree is expanded, check if there are any matches that can be easily found
1368                          *              so that searching for "cu" in the default scene will still match the Cube
1369                          *      - otherwise, we can't see within the subtree and the item doesn't match,
1370                          *              so these can be safely ignored (i.e. the subtree can get freed)
1371                          */
1372                         tselem= TREESTORE(te);
1373                         
1374                         /* flag as not a found item */
1375                         tselem->flag &= ~TSE_SEARCHMATCH;
1376                         
1377                         if ((!TSELEM_OPEN(tselem,soops)) || outliner_filter_tree(soops, &te->subtree)==0) { 
1378                                 outliner_free_tree(&te->subtree);
1379                                 BLI_remlink(lb, te);
1380                                 
1381                                 if (te->flag & TE_FREE_NAME) MEM_freeN((void *)te->name);
1382                                 MEM_freeN(te);
1383                         }
1384                 }
1385                 else {
1386                         tselem= TREESTORE(te);
1387                         
1388                         /* flag as a found item - we can then highlight it */
1389                         tselem->flag |= TSE_SEARCHMATCH;
1390                         
1391                         /* filter subtree too */
1392                         outliner_filter_tree(soops, &te->subtree);
1393                 }
1394         }
1395         
1396         /* if there are still items in the list, that means that there were still some matches */
1397         return (lb->first != NULL);
1398 }
1399
1400 /* ======================================================= */
1401 /* Main Tree Building API */
1402
1403 /* Main entry point for building the tree data-structure that the outliner represents */
1404 // TODO: split each mode into its own function?
1405 void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops)
1406 {
1407         Base *base;
1408         Object *ob;
1409         TreeElement *te=NULL, *ten;
1410         TreeStoreElem *tselem;
1411         int show_opened= (soops->treestore==NULL); /* on first view, we open scenes */
1412
1413         /* Are we looking for something - we want to tag parents to filter child matches
1414          * - NOT in datablocks view - searching all datablocks takes way too long to be useful
1415          * - this variable is only set once per tree build */
1416         if (soops->search_string[0]!=0 && soops->outlinevis!=SO_DATABLOCKS)
1417                 soops->search_flags |= SO_SEARCH_RECURSIVE;
1418         else
1419                 soops->search_flags &= ~SO_SEARCH_RECURSIVE;
1420
1421         if (soops->tree.first && (soops->storeflag & SO_TREESTORE_REDRAW))
1422                 return;
1423
1424         outliner_free_tree(&soops->tree);
1425         outliner_storage_cleanup(soops);
1426         
1427         /* clear ob id.new flags */
1428         for (ob= mainvar->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
1429         
1430         /* options */
1431         if (soops->outlinevis == SO_LIBRARIES) {
1432                 Library *lib;
1433                 
1434                 for (lib= mainvar->library.first; lib; lib= lib->id.next) {
1435                         ten= outliner_add_element(soops, &soops->tree, lib, NULL, 0, 0);
1436                         lib->id.newid= (ID *)ten;
1437                 }
1438                 /* make hierarchy */
1439                 ten= soops->tree.first;
1440                 while (ten) {
1441                         TreeElement *nten= ten->next, *par;
1442                         tselem= TREESTORE(ten);
1443                         lib= (Library *)tselem->id;
1444                         if (lib->parent) {
1445                                 BLI_remlink(&soops->tree, ten);
1446                                 par= (TreeElement *)lib->parent->id.newid;
1447                                 BLI_addtail(&par->subtree, ten);
1448                                 ten->parent= par;
1449                         }
1450                         ten= nten;
1451                 }
1452                 /* restore newid pointers */
1453                 for (lib= mainvar->library.first; lib; lib= lib->id.next)
1454                         lib->id.newid= NULL;
1455                 
1456         }
1457         else if (soops->outlinevis == SO_ALL_SCENES) {
1458                 Scene *sce;
1459                 for (sce= mainvar->scene.first; sce; sce= sce->id.next) {
1460                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
1461                         tselem= TREESTORE(te);
1462                         if (sce==scene && show_opened) 
1463                                 tselem->flag &= ~TSE_CLOSED;
1464                         
1465                         for (base= sce->base.first; base; base= base->next) {
1466                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
1467                                 ten->directdata= base;
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 (base= sce->base.first; base; base= base->next) base->object->id.newid= NULL;
1472                 }
1473         }
1474         else if (soops->outlinevis == SO_CUR_SCENE) {
1475                 
1476                 outliner_add_scene_contents(soops, &soops->tree, scene, NULL);
1477                 
1478                 for (base= scene->base.first; base; base= base->next) {
1479                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1480                         ten->directdata= base;
1481                 }
1482                 outliner_make_hierarchy(soops, &soops->tree);
1483         }
1484         else if (soops->outlinevis == SO_VISIBLE) {
1485                 for (base= scene->base.first; base; base= base->next) {
1486                         if (base->lay & scene->lay)
1487                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1488                 }
1489                 outliner_make_hierarchy(soops, &soops->tree);
1490         }
1491         else if (soops->outlinevis == SO_GROUPS) {
1492                 Group *group;
1493                 GroupObject *go;
1494                 
1495                 for (group= mainvar->group.first; group; group= group->id.next) {
1496                         if (group->gobject.first) {
1497                                 te= outliner_add_element(soops, &soops->tree, group, NULL, 0, 0);
1498                                 
1499                                 for (go= group->gobject.first; go; go= go->next) {
1500                                         ten= outliner_add_element(soops, &te->subtree, go->ob, te, 0, 0);
1501                                         ten->directdata= NULL; /* eh, why? */
1502                                 }
1503                                 outliner_make_hierarchy(soops, &te->subtree);
1504                                 /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1505                                 for (go= group->gobject.first; go; go= go->next) go->ob->id.newid= NULL;
1506                         }
1507                 }
1508         }
1509         else if (soops->outlinevis == SO_SAME_TYPE) {
1510                 Object *ob= OBACT;
1511                 if (ob) {
1512                         for (base= scene->base.first; base; base= base->next) {
1513                                 if (base->object->type==ob->type) {
1514                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1515                                         ten->directdata= base;
1516                                 }
1517                         }
1518                         outliner_make_hierarchy(soops, &soops->tree);
1519                 }
1520         }
1521         else if (soops->outlinevis == SO_SELECTED) {
1522                 for (base= scene->base.first; base; base= base->next) {
1523                         if (base->lay & scene->lay) {
1524                                 if (base==BASACT || (base->flag & SELECT)) {
1525                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1526                                         ten->directdata= base;
1527                                 }
1528                         }
1529                 }
1530                 outliner_make_hierarchy(soops, &soops->tree);
1531         }
1532         else if (soops->outlinevis==SO_SEQUENCE) {
1533                 Sequence *seq;
1534                 Editing *ed= seq_give_editing(scene, FALSE);
1535                 int op;
1536
1537                 if (ed==NULL)
1538                         return;
1539
1540                 seq= ed->seqbasep->first;
1541                 if (!seq)
1542                         return;
1543
1544                 while (seq) {
1545                         op= need_add_seq_dup(seq);
1546                         if (op==1) {
1547                                 /* ten= */ outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1548                         }
1549                         else if (op==0) {
1550                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1551                                 outliner_add_seq_dup(soops, seq, ten, 0);
1552                         }
1553                         seq= seq->next;
1554                 }
1555         }
1556         else if (soops->outlinevis==SO_DATABLOCKS) {
1557                 PointerRNA mainptr;
1558
1559                 RNA_main_pointer_create(mainvar, &mainptr);
1560
1561                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1562
1563                 if (show_opened) {
1564                         tselem= TREESTORE(ten);
1565                         tselem->flag &= ~TSE_CLOSED;
1566                 }
1567         }
1568         else if (soops->outlinevis==SO_USERDEF) {
1569                 PointerRNA userdefptr;
1570
1571                 RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &userdefptr);
1572
1573                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1574
1575                 if (show_opened) {
1576                         tselem= TREESTORE(ten);
1577                         tselem->flag &= ~TSE_CLOSED;
1578                 }
1579         }
1580         else if (soops->outlinevis==SO_KEYMAP) {
1581                 wmWindowManager *wm= mainvar->wm.first;
1582                 wmKeyMap *km;
1583                 
1584                 for (km= wm->defaultconf->keymaps.first; km; km= km->next) {
1585                         /* ten= */ outliner_add_element(soops, &soops->tree, (void*)km, NULL, TSE_KEYMAP, 0);
1586                 }
1587         }
1588         else {
1589                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1590                 if (ten) ten->directdata= BASACT;
1591         }
1592
1593         outliner_sort(soops, &soops->tree);
1594         outliner_filter_tree(soops, &soops->tree);
1595 }
1596
1597