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