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