Merged changes in the trunk up to revision 28536.
[blender.git] / source / blender / editors / space_outliner / outliner.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_anim_types.h"
37 #include "DNA_armature_types.h"
38 #include "DNA_constraint_types.h"
39 #include "DNA_camera_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_particle_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_world_types.h"
49 #include "DNA_sequence_types.h"
50
51 #include "BLI_blenlib.h"
52
53 #if defined WIN32 && !defined _LIBC
54 # include "BLI_fnmatch.h" /* use fnmatch included in blenlib */
55 #else
56 # define _GNU_SOURCE
57 # include <fnmatch.h>
58 #endif
59
60 #include "IMB_imbuf_types.h"
61
62 #include "BKE_animsys.h"
63 #include "BKE_constraint.h"
64 #include "BKE_context.h"
65 #include "BKE_deform.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_fcurve.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
70 #include "BKE_library.h"
71 #include "BKE_main.h"
72 #include "BKE_material.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_report.h"
76 #include "BKE_screen.h"
77 #include "BKE_scene.h"
78 #include "BKE_sequencer.h"
79 #include "BKE_utildefines.h"
80
81 #include "ED_armature.h"
82 #include "ED_object.h"
83 #include "ED_screen.h"
84 #include "ED_util.h"
85
86 #include "WM_api.h"
87 #include "WM_types.h"
88
89 #include "BIF_gl.h"
90 #include "BIF_glutil.h"
91
92 #include "UI_interface.h"
93 #include "UI_interface_icons.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99
100 #include "ED_keyframing.h"
101
102 #include "outliner_intern.h"
103
104
105
106 #define OL_H    19
107 #define OL_X    18
108
109 #define OL_TOG_RESTRICT_VIEWX   54
110 #define OL_TOG_RESTRICT_SELECTX 36
111 #define OL_TOG_RESTRICT_RENDERX 18
112
113 #define OL_TOGW OL_TOG_RESTRICT_VIEWX
114
115 #define OL_RNA_COLX                     300
116 #define OL_RNA_COL_SIZEX        150
117 #define OL_RNA_COL_SPACEX       50
118
119 #define TS_CHUNK        128
120
121 #define TREESTORE(a) ((a)?soops->treestore->data+(a)->store_index:NULL)
122
123 /* ************* XXX **************** */
124
125 static void error(const char *dummy, ...) {}
126
127 /* ********************************** */
128
129
130 /* ******************** PROTOTYPES ***************** */
131 static void outliner_draw_tree_element(bContext *C, uiBlock *block, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, int startx, int *starty);
132 static void outliner_do_object_operation(bContext *C, Scene *scene, SpaceOops *soops, ListBase *lb, 
133                                                                                  void (*operation_cb)(bContext *C, Scene *scene, TreeElement *, TreeStoreElem *, TreeStoreElem *));
134
135
136 /* ******************** PERSISTANT DATA ***************** */
137
138 static void outliner_storage_cleanup(SpaceOops *soops)
139 {
140         TreeStore *ts= soops->treestore;
141         
142         if(ts) {
143                 TreeStoreElem *tselem;
144                 int a, unused= 0;
145                 
146                 /* each element used once, for ID blocks with more users to have each a treestore */
147                 for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) tselem->used= 0;
148
149                 /* cleanup only after reading file or undo step, and always for
150                  * RNA datablocks view in order to save memory */
151                 if(soops->storeflag & SO_TREESTORE_CLEANUP) {
152                         
153                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
154                                 if(tselem->id==NULL) unused++;
155                         }
156
157                         if(unused) {
158                                 if(ts->usedelem == unused) {
159                                         MEM_freeN(ts->data);
160                                         ts->data= NULL;
161                                         ts->usedelem= ts->totelem= 0;
162                                 }
163                                 else {
164                                         TreeStoreElem *tsnewar, *tsnew;
165                                         
166                                         tsnew=tsnewar= MEM_mallocN((ts->usedelem-unused)*sizeof(TreeStoreElem), "new tselem");
167                                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
168                                                 if(tselem->id) {
169                                                         *tsnew= *tselem;
170                                                         tsnew++;
171                                                 }
172                                         }
173                                         MEM_freeN(ts->data);
174                                         ts->data= tsnewar;
175                                         ts->usedelem-= unused;
176                                         ts->totelem= ts->usedelem;
177                                 }
178                         }
179                 }
180         }
181 }
182
183 static void check_persistant(SpaceOops *soops, TreeElement *te, ID *id, short type, short nr)
184 {
185         TreeStore *ts;
186         TreeStoreElem *tselem;
187         int a;
188         
189         /* case 1; no TreeStore */
190         if(soops->treestore==NULL) {
191                 ts= soops->treestore= MEM_callocN(sizeof(TreeStore), "treestore");
192         }
193         ts= soops->treestore;
194         
195         /* check if 'te' is in treestore */
196         tselem= ts->data;
197         for(a=0; a<ts->usedelem; a++, tselem++) {
198                 if(tselem->id==id && tselem->used==0) {
199                         if((type==0 && tselem->type==0) ||(tselem->type==type && tselem->nr==nr)) {
200                                 te->store_index= a;
201                                 tselem->used= 1;
202                                 return;
203                         }
204                 }
205         }
206         
207         /* add 1 element to treestore */
208         if(ts->usedelem==ts->totelem) {
209                 TreeStoreElem *tsnew;
210                 
211                 tsnew= MEM_mallocN((ts->totelem+TS_CHUNK)*sizeof(TreeStoreElem), "treestore data");
212                 if(ts->data) {
213                         memcpy(tsnew, ts->data, ts->totelem*sizeof(TreeStoreElem));
214                         MEM_freeN(ts->data);
215                 }
216                 ts->data= tsnew;
217                 ts->totelem+= TS_CHUNK;
218         }
219         
220         tselem= ts->data+ts->usedelem;
221         
222         tselem->type= type;
223         if(type) tselem->nr= nr; // we're picky! :)
224         else tselem->nr= 0;
225         tselem->id= id;
226         tselem->used = 0;
227         tselem->flag= TSE_CLOSED;
228         te->store_index= ts->usedelem;
229         
230         ts->usedelem++;
231 }
232
233 /* ******************** TREE MANAGEMENT ****************** */
234
235 void outliner_free_tree(ListBase *lb)
236 {
237         
238         while(lb->first) {
239                 TreeElement *te= lb->first;
240                 
241                 outliner_free_tree(&te->subtree);
242                 BLI_remlink(lb, te);
243
244                 if(te->flag & TE_FREE_NAME) MEM_freeN(te->name);
245                 MEM_freeN(te);
246         }
247 }
248
249 static void outliner_height(SpaceOops *soops, ListBase *lb, int *h)
250 {
251         TreeElement *te= lb->first;
252         while(te) {
253                 TreeStoreElem *tselem= TREESTORE(te);
254                 if((tselem->flag & TSE_CLOSED)==0) 
255                         outliner_height(soops, &te->subtree, h);
256                 (*h) += OL_H;
257                 te= te->next;
258         }
259 }
260
261 #if 0  // XXX this is currently disabled until te->xend is set correctly
262 static void outliner_width(SpaceOops *soops, ListBase *lb, int *w)
263 {
264         TreeElement *te= lb->first;
265         while(te) {
266 //              TreeStoreElem *tselem= TREESTORE(te);
267                 
268                 // XXX fixme... te->xend is not set yet
269                 if(tselem->flag & TSE_CLOSED) {
270                         if (te->xend > *w)
271                                 *w = te->xend;
272                 }
273                 outliner_width(soops, &te->subtree, w);
274                 te= te->next;
275         }
276 }
277 #endif
278
279 static void outliner_rna_width(SpaceOops *soops, ListBase *lb, int *w, int startx)
280 {
281         TreeElement *te= lb->first;
282         while(te) {
283                 TreeStoreElem *tselem= TREESTORE(te);
284                         // XXX fixme... (currently, we're using a fixed length of 100)!
285                 /*if(te->xend) {
286                         if(te->xend > *w)
287                                 *w = te->xend;
288                 }*/
289                 if(startx+100 > *w)
290                         *w = startx+100;
291
292                 if((tselem->flag & TSE_CLOSED)==0)
293                         outliner_rna_width(soops, &te->subtree, w, startx+OL_X);
294                 te= te->next;
295         }
296 }
297
298 static TreeElement *outliner_find_tree_element(ListBase *lb, int store_index)
299 {
300         TreeElement *te= lb->first, *tes;
301         while(te) {
302                 if(te->store_index==store_index) return te;
303                 tes= outliner_find_tree_element(&te->subtree, store_index);
304                 if(tes) return tes;
305                 te= te->next;
306         }
307         return NULL;
308 }
309
310
311
312 static ID *outliner_search_back(SpaceOops *soops, TreeElement *te, short idcode)
313 {
314         TreeStoreElem *tselem;
315         te= te->parent;
316         
317         while(te) {
318                 tselem= TREESTORE(te);
319                 if(tselem->type==0 && te->idcode==idcode) return tselem->id;
320                 te= te->parent;
321         }
322         return NULL;
323 }
324
325 struct treesort {
326         TreeElement *te;
327         ID *id;
328         char *name;
329         short idcode;
330 };
331
332 static int treesort_alpha(const void *v1, const void *v2)
333 {
334         const struct treesort *x1= v1, *x2= v2;
335         int comp;
336         
337         /* first put objects last (hierarchy) */
338         comp= (x1->idcode==ID_OB);
339         if(x2->idcode==ID_OB) comp+=2;
340         
341         if(comp==1) return 1;
342         else if(comp==2) return -1;
343         else if(comp==3) {
344                 int comp= strcmp(x1->name, x2->name);
345                 
346                 if( comp>0 ) return 1;
347                 else if( comp<0) return -1;
348                 return 0;
349         }
350         return 0;
351 }
352
353 /* this is nice option for later? doesnt look too useful... */
354 #if 0
355 static int treesort_obtype_alpha(const void *v1, const void *v2)
356 {
357         const struct treesort *x1= v1, *x2= v2;
358         
359         /* first put objects last (hierarchy) */
360         if(x1->idcode==ID_OB && x2->idcode!=ID_OB) return 1;
361         else if(x2->idcode==ID_OB && x1->idcode!=ID_OB) return -1;
362         else {
363                 /* 2nd we check ob type */
364                 if(x1->idcode==ID_OB && x2->idcode==ID_OB) {
365                         if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return 1;
366                         else if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
367                         else return 0;
368                 }
369                 else {
370                         int comp= strcmp(x1->name, x2->name);
371                         
372                         if( comp>0 ) return 1;
373                         else if( comp<0) return -1;
374                         return 0;
375                 }
376         }
377 }
378 #endif
379
380 /* sort happens on each subtree individual */
381 static void outliner_sort(SpaceOops *soops, ListBase *lb)
382 {
383         TreeElement *te;
384         TreeStoreElem *tselem;
385         int totelem=0;
386         
387         te= lb->last;
388         if(te==NULL) return;
389         tselem= TREESTORE(te);
390         
391         /* sorting rules; only object lists or deformgroups */
392         if( (tselem->type==TSE_DEFGROUP) || (tselem->type==0 && te->idcode==ID_OB)) {
393                 
394                 /* count first */
395                 for(te= lb->first; te; te= te->next) totelem++;
396                 
397                 if(totelem>1) {
398                         struct treesort *tear= MEM_mallocN(totelem*sizeof(struct treesort), "tree sort array");
399                         struct treesort *tp=tear;
400                         int skip= 0;
401                         
402                         for(te= lb->first; te; te= te->next, tp++) {
403                                 tselem= TREESTORE(te);
404                                 tp->te= te;
405                                 tp->name= te->name;
406                                 tp->idcode= te->idcode;
407                                 if(tselem->type && tselem->type!=TSE_DEFGROUP) tp->idcode= 0;   // dont sort this
408                                 tp->id= tselem->id;
409                         }
410                         /* keep beginning of list */
411                         for(tp= tear, skip=0; skip<totelem; skip++, tp++)
412                                 if(tp->idcode) break;
413                         
414                         if(skip<totelem)
415                                 qsort(tear+skip, totelem-skip, sizeof(struct treesort), treesort_alpha);
416                         
417                         lb->first=lb->last= NULL;
418                         tp= tear;
419                         while(totelem--) {
420                                 BLI_addtail(lb, tp->te);
421                                 tp++;
422                         }
423                         MEM_freeN(tear);
424                 }
425         }
426         
427         for(te= lb->first; te; te= te->next) {
428                 outliner_sort(soops, &te->subtree);
429         }
430 }
431
432 /* Prototype, see functions below */
433 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
434                                                                                  TreeElement *parent, short type, short index);
435
436
437 static void outliner_add_passes(SpaceOops *soops, TreeElement *tenla, ID *id, SceneRenderLayer *srl)
438 {
439         TreeStoreElem *tselem= TREESTORE(tenla);
440         TreeElement *te;
441         
442         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_COMBINED);
443         te->name= "Combined";
444         te->directdata= &srl->passflag;
445         
446         /* save cpu cycles, but we add the first to invoke an open/close triangle */
447         if(tselem->flag & TSE_CLOSED)
448                 return;
449         
450         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_Z);
451         te->name= "Z";
452         te->directdata= &srl->passflag;
453         
454         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_VECTOR);
455         te->name= "Vector";
456         te->directdata= &srl->passflag;
457         
458         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_NORMAL);
459         te->name= "Normal";
460         te->directdata= &srl->passflag;
461         
462         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_UV);
463         te->name= "UV";
464         te->directdata= &srl->passflag;
465         
466         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_MIST);
467         te->name= "Mist";
468         te->directdata= &srl->passflag;
469         
470         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_INDEXOB);
471         te->name= "Index Object";
472         te->directdata= &srl->passflag;
473         
474         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_RGBA);
475         te->name= "Color";
476         te->directdata= &srl->passflag;
477         
478         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_DIFFUSE);
479         te->name= "Diffuse";
480         te->directdata= &srl->passflag;
481         
482         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_SPEC);
483         te->name= "Specular";
484         te->directdata= &srl->passflag;
485         
486         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_SHADOW);
487         te->name= "Shadow";
488         te->directdata= &srl->passflag;
489         
490         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_AO);
491         te->name= "AO";
492         te->directdata= &srl->passflag;
493         
494         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_REFLECT);
495         te->name= "Reflection";
496         te->directdata= &srl->passflag;
497         
498         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_REFRACT);
499         te->name= "Refraction";
500         te->directdata= &srl->passflag;
501         
502         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_INDIRECT);
503         te->name= "Indirect";
504         te->directdata= &srl->passflag;
505
506         /* TODO SCE_PASS_ENVIRONMENT/EMIT overflow short..
507
508         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_ENVIRONMENT);
509         te->name= "Environment";
510         te->directdata= &srl->passflag;
511
512         te= outliner_add_element(soops, &tenla->subtree, id, tenla, TSE_R_PASS, SCE_PASS_EMIT);
513         te->name= "Emit";
514         te->directdata= &srl->passflag;*/
515 }
516
517
518 /* special handling of hierarchical non-lib data */
519 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone, 
520                                                           TreeElement *parent, int *a)
521 {
522         TreeElement *te= outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
523         
524         (*a)++;
525         te->name= curBone->name;
526         te->directdata= curBone;
527         
528         for(curBone= curBone->childbase.first; curBone; curBone=curBone->next) {
529                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
530         }
531 }
532
533 static void outliner_add_scene_contents(SpaceOops *soops, ListBase *lb, Scene *sce, TreeElement *te)
534 {
535         SceneRenderLayer *srl;
536         TreeElement *tenla= outliner_add_element(soops, lb, sce, te, TSE_R_LAYER_BASE, 0);
537         int a;
538         
539         tenla->name= "RenderLayers";
540         for(a=0, srl= sce->r.layers.first; srl; srl= srl->next, a++) {
541                 TreeElement *tenlay= outliner_add_element(soops, &tenla->subtree, sce, te, TSE_R_LAYER, a);
542                 tenlay->name= srl->name;
543                 tenlay->directdata= &srl->passflag;
544                 
545                 if(srl->light_override)
546                         outliner_add_element(soops, &tenlay->subtree, srl->light_override, tenlay, TSE_LINKED_LAMP, 0);
547                 if(srl->mat_override)
548                         outliner_add_element(soops, &tenlay->subtree, srl->mat_override, tenlay, TSE_LINKED_MAT, 0);
549                 
550                 outliner_add_passes(soops, tenlay, &sce->id, srl);
551         }
552         
553         outliner_add_element(soops,  lb, sce->world, te, 0, 0);
554 }
555
556 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
557                                                                                  TreeElement *parent, short type, short index)
558 {
559         TreeElement *te;
560         TreeStoreElem *tselem;
561         ID *id= idv;
562         int a = 0;
563         
564         if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
565                 id= ((PointerRNA*)idv)->id.data;
566                 if(!id) id= ((PointerRNA*)idv)->data;
567         }
568
569         if(id==NULL) return NULL;
570
571         te= MEM_callocN(sizeof(TreeElement), "tree elem");
572         /* add to the visual tree */
573         BLI_addtail(lb, te);
574         /* add to the storage */
575         check_persistant(soops, te, id, type, index);
576         tselem= TREESTORE(te);  
577         
578         te->parent= parent;
579         te->index= index;       // for data arays
580         if(ELEM3(type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP));
581         else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM));
582         else if(type==TSE_ANIM_DATA);
583         else {
584                 te->name= id->name+2; // default, can be overridden by Library or non-ID data
585                 te->idcode= GS(id->name);
586         }
587         
588         if(type==0) {
589
590                 /* tuck pointer back in object, to construct hierarchy */
591                 if(GS(id->name)==ID_OB) id->newid= (ID *)te;
592                 
593                 /* expand specific data always */
594                 switch(GS(id->name)) {
595                 case ID_LI:
596                         te->name= ((Library *)id)->name;
597                         break;
598                 case ID_SCE:
599                         outliner_add_scene_contents(soops, &te->subtree, (Scene *)id, te);
600                         break;
601                 case ID_OB:
602                         {
603                                 Object *ob= (Object *)id;
604                                 
605                                 outliner_add_element(soops, &te->subtree, ob->adt, te, TSE_ANIM_DATA, 0);
606                                 outliner_add_element(soops, &te->subtree, ob->poselib, te, 0, 0); // XXX FIXME.. add a special type for this
607                                 
608                                 if(ob->proxy && ob->id.lib==NULL)
609                                         outliner_add_element(soops, &te->subtree, ob->proxy, te, TSE_PROXY, 0);
610                                 
611                                 outliner_add_element(soops, &te->subtree, ob->data, te, 0, 0);
612                                 
613                                 if(ob->pose) {
614                                         bArmature *arm= ob->data;
615                                         bPoseChannel *pchan;
616                                         TreeElement *ten;
617                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSE_BASE, 0);
618                                         
619                                         tenla->name= "Pose";
620                                         
621                                         if(arm->edbo==NULL && (ob->mode & OB_MODE_POSE)) {      // channels undefined in editmode, but we want the 'tenla' pose icon itself
622                                                 int a= 0, const_index= 1000;    /* ensure unique id for bone constraints */
623                                                 
624                                                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
625                                                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSE_CHANNEL, a);
626                                                         ten->name= pchan->name;
627                                                         ten->directdata= pchan;
628                                                         pchan->prev= (bPoseChannel *)ten;
629                                                         
630                                                         if(pchan->constraints.first) {
631                                                                 //Object *target;
632                                                                 bConstraint *con;
633                                                                 TreeElement *ten1;
634                                                                 TreeElement *tenla1= outliner_add_element(soops, &ten->subtree, ob, ten, TSE_CONSTRAINT_BASE, 0);
635                                                                 //char *str;
636                                                                 
637                                                                 tenla1->name= "Constraints";
638                                                                 for(con= pchan->constraints.first; con; con= con->next, const_index++) {
639                                                                         ten1= outliner_add_element(soops, &tenla1->subtree, ob, tenla1, TSE_CONSTRAINT, const_index);
640 #if 0 /* disabled as it needs to be reworked for recoded constraints system */
641                                                                         target= get_constraint_target(con, &str);
642                                                                         if(str && str[0]) ten1->name= str;
643                                                                         else if(target) ten1->name= target->id.name+2;
644                                                                         else ten1->name= con->name;
645 #endif
646                                                                         ten1->name= con->name;
647                                                                         ten1->directdata= con;
648                                                                         /* possible add all other types links? */
649                                                                 }
650                                                         }
651                                                 }
652                                                 /* make hierarchy */
653                                                 ten= tenla->subtree.first;
654                                                 while(ten) {
655                                                         TreeElement *nten= ten->next, *par;
656                                                         tselem= TREESTORE(ten);
657                                                         if(tselem->type==TSE_POSE_CHANNEL) {
658                                                                 pchan= (bPoseChannel *)ten->directdata;
659                                                                 if(pchan->parent) {
660                                                                         BLI_remlink(&tenla->subtree, ten);
661                                                                         par= (TreeElement *)pchan->parent->prev;
662                                                                         BLI_addtail(&par->subtree, ten);
663                                                                         ten->parent= par;
664                                                                 }
665                                                         }
666                                                         ten= nten;
667                                                 }
668                                                 /* restore prev pointers */
669                                                 pchan= ob->pose->chanbase.first;
670                                                 if(pchan) pchan->prev= NULL;
671                                                 for(; pchan; pchan= pchan->next) {
672                                                         if(pchan->next) pchan->next->prev= pchan;
673                                                 }
674                                         }
675                                         
676                                         /* Pose Groups */
677                                         if(ob->pose->agroups.first) {
678                                                 bActionGroup *agrp;
679                                                 TreeElement *ten;
680                                                 TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSEGRP_BASE, 0);
681                                                 int a= 0;
682                                                 
683                                                 tenla->name= "Bone Groups";
684                                                 for (agrp=ob->pose->agroups.first; agrp; agrp=agrp->next, a++) {
685                                                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSEGRP, a);
686                                                         ten->name= agrp->name;
687                                                         ten->directdata= agrp;
688                                                 }
689                                         }
690                                 }
691                                 
692                                 for(a=0; a<ob->totcol; a++) 
693                                         outliner_add_element(soops, &te->subtree, ob->mat[a], te, 0, a);
694                                 
695                                 if(ob->constraints.first) {
696                                         //Object *target;
697                                         bConstraint *con;
698                                         TreeElement *ten;
699                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_CONSTRAINT_BASE, 0);
700                                         int a= 0;
701                                         //char *str;
702                                         
703                                         tenla->name= "Constraints";
704                                         for(con= ob->constraints.first; con; con= con->next, a++) {
705                                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_CONSTRAINT, a);
706 #if 0 /* disabled due to constraints system targets recode... code here needs review */
707                                                 target= get_constraint_target(con, &str);
708                                                 if(str && str[0]) ten->name= str;
709                                                 else if(target) ten->name= target->id.name+2;
710                                                 else ten->name= con->name;
711 #endif
712                                                 ten->name= con->name;
713                                                 ten->directdata= con;
714                                                 /* possible add all other types links? */
715                                         }
716                                 }
717                                 
718                                 if(ob->modifiers.first) {
719                                         ModifierData *md;
720                                         TreeElement *temod = outliner_add_element(soops, &te->subtree, ob, te, TSE_MODIFIER_BASE, 0);
721                                         int index;
722
723                                         temod->name = "Modifiers";
724                                         for (index=0,md=ob->modifiers.first; md; index++,md=md->next) {
725                                                 TreeElement *te = outliner_add_element(soops, &temod->subtree, ob, temod, TSE_MODIFIER, index);
726                                                 te->name= md->name;
727                                                 te->directdata = md;
728
729                                                 if (md->type==eModifierType_Lattice) {
730                                                         outliner_add_element(soops, &te->subtree, ((LatticeModifierData*) md)->object, te, TSE_LINKED_OB, 0);
731                                                 } else if (md->type==eModifierType_Curve) {
732                                                         outliner_add_element(soops, &te->subtree, ((CurveModifierData*) md)->object, te, TSE_LINKED_OB, 0);
733                                                 } else if (md->type==eModifierType_Armature) {
734                                                         outliner_add_element(soops, &te->subtree, ((ArmatureModifierData*) md)->object, te, TSE_LINKED_OB, 0);
735                                                 } else if (md->type==eModifierType_Hook) {
736                                                         outliner_add_element(soops, &te->subtree, ((HookModifierData*) md)->object, te, TSE_LINKED_OB, 0);
737                                                 } else if (md->type==eModifierType_ParticleSystem) {
738                                                         TreeElement *ten;
739                                                         ParticleSystem *psys= ((ParticleSystemModifierData*) md)->psys;
740                                                         
741                                                         ten = outliner_add_element(soops, &te->subtree, ob, te, TSE_LINKED_PSYS, 0);
742                                                         ten->directdata = psys;
743                                                         ten->name = psys->part->id.name+2;
744                                                 }
745                                         }
746                                 }
747                                 if(ob->defbase.first) {
748                                         bDeformGroup *defgroup;
749                                         TreeElement *ten;
750                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_DEFGROUP_BASE, 0);
751                                         int a= 0;
752                                         
753                                         tenla->name= "Vertex Groups";
754                                         for (defgroup=ob->defbase.first; defgroup; defgroup=defgroup->next, a++) {
755                                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_DEFGROUP, a);
756                                                 ten->name= defgroup->name;
757                                                 ten->directdata= defgroup;
758                                         }
759                                 }
760                                 
761                                 if(ob->dup_group)
762                                         outliner_add_element(soops, &te->subtree, ob->dup_group, te, 0, 0);     
763                                 
764                         }
765                         break;
766                 case ID_ME:
767                         {
768                                 Mesh *me= (Mesh *)id;
769                                 
770                                 //outliner_add_element(soops, &te->subtree, me->adt, te, TSE_ANIM_DATA, 0);
771                                 
772                                 outliner_add_element(soops, &te->subtree, me->key, te, 0, 0);
773                                 for(a=0; a<me->totcol; a++) 
774                                         outliner_add_element(soops, &te->subtree, me->mat[a], te, 0, a);
775                                 /* could do tfaces with image links, but the images are not grouped nicely.
776                                    would require going over all tfaces, sort images in use. etc... */
777                         }
778                         break;
779                 case ID_CU:
780                         {
781                                 Curve *cu= (Curve *)id;
782                                 
783                                 outliner_add_element(soops, &te->subtree, cu->adt, te, TSE_ANIM_DATA, 0);
784                                 
785                                 for(a=0; a<cu->totcol; a++) 
786                                         outliner_add_element(soops, &te->subtree, cu->mat[a], te, 0, a);
787                         }
788                         break;
789                 case ID_MB:
790                         {
791                                 MetaBall *mb= (MetaBall *)id;
792                                 for(a=0; a<mb->totcol; a++) 
793                                         outliner_add_element(soops, &te->subtree, mb->mat[a], te, 0, a);
794                         }
795                         break;
796                 case ID_MA:
797                 {
798                         Material *ma= (Material *)id;
799                         
800                         outliner_add_element(soops, &te->subtree, ma->adt, te, TSE_ANIM_DATA, 0);
801                         
802                         for(a=0; a<MAX_MTEX; a++) {
803                                 if(ma->mtex[a]) outliner_add_element(soops, &te->subtree, ma->mtex[a]->tex, te, 0, a);
804                         }
805                 }
806                         break;
807                 case ID_TE:
808                         {
809                                 Tex *tex= (Tex *)id;
810                                 
811                                 outliner_add_element(soops, &te->subtree, tex->adt, te, TSE_ANIM_DATA, 0);
812                                 outliner_add_element(soops, &te->subtree, tex->ima, te, 0, 0);
813                         }
814                         break;
815                 case ID_CA:
816                         {
817                                 Camera *ca= (Camera *)id;
818                                 outliner_add_element(soops, &te->subtree, ca->adt, te, TSE_ANIM_DATA, 0);
819                         }
820                         break;
821                 case ID_LA:
822                         {
823                                 Lamp *la= (Lamp *)id;
824                                 
825                                 outliner_add_element(soops, &te->subtree, la->adt, te, TSE_ANIM_DATA, 0);
826                                 
827                                 for(a=0; a<MAX_MTEX; a++) {
828                                         if(la->mtex[a]) outliner_add_element(soops, &te->subtree, la->mtex[a]->tex, te, 0, a);
829                                 }
830                         }
831                         break;
832                 case ID_WO:
833                         {
834                                 World *wrld= (World *)id;
835                                 
836                                 outliner_add_element(soops, &te->subtree, wrld->adt, te, TSE_ANIM_DATA, 0);
837                                 
838                                 for(a=0; a<MAX_MTEX; a++) {
839                                         if(wrld->mtex[a]) outliner_add_element(soops, &te->subtree, wrld->mtex[a]->tex, te, 0, a);
840                                 }
841                         }
842                         break;
843                 case ID_KE:
844                         {
845                                 Key *key= (Key *)id;
846                                 
847                                 outliner_add_element(soops, &te->subtree, key->adt, te, TSE_ANIM_DATA, 0);
848                         }
849                         break;
850                 case ID_AC:
851                         {
852                                 // XXX do we want to be exposing the F-Curves here?
853                                 //bAction *act= (bAction *)id;
854                         }
855                         break;
856                 case ID_AR:
857                         {
858                                 bArmature *arm= (bArmature *)id;
859                                 int a= 0;
860                                 
861                                 if(arm->edbo) {
862                                         EditBone *ebone;
863                                         TreeElement *ten;
864                                         
865                                         for (ebone = arm->edbo->first; ebone; ebone=ebone->next, a++) {
866                                                 ten= outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
867                                                 ten->directdata= ebone;
868                                                 ten->name= ebone->name;
869                                                 ebone->temp= ten;
870                                         }
871                                         /* make hierarchy */
872                                         ten= te->subtree.first;
873                                         while(ten) {
874                                                 TreeElement *nten= ten->next, *par;
875                                                 ebone= (EditBone *)ten->directdata;
876                                                 if(ebone->parent) {
877                                                         BLI_remlink(&te->subtree, ten);
878                                                         par= ebone->parent->temp;
879                                                         BLI_addtail(&par->subtree, ten);
880                                                         ten->parent= par;
881                                                 }
882                                                 ten= nten;
883                                         }
884                                 }
885                                 else {
886                                         /* do not extend Armature when we have posemode */
887                                         tselem= TREESTORE(te->parent);
888                                         if( GS(tselem->id->name)==ID_OB && ((Object *)tselem->id)->mode & OB_MODE_POSE);
889                                         else {
890                                                 Bone *curBone;
891                                                 for (curBone=arm->bonebase.first; curBone; curBone=curBone->next){
892                                                         outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
893                                                 }
894                                         }
895                                 }
896                         }
897                         break;
898                 }
899         }
900         else if(type==TSE_ANIM_DATA) {
901                 AnimData *adt= (AnimData *)idv;
902                 
903                 /* this element's info */
904                 te->name= "Animation";
905                 
906                 /* Action */
907                 outliner_add_element(soops, &te->subtree, adt->action, te, 0, 0);
908                 
909                 /* Drivers */
910                 if (adt->drivers.first) {
911                         TreeElement *ted= outliner_add_element(soops, &te->subtree, adt, te, TSE_DRIVER_BASE, 0);
912                         ID *lastadded= NULL;
913                         FCurve *fcu;
914                         
915                         ted->name= "Drivers";
916                 
917                         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
918                                 if (fcu->driver && fcu->driver->variables.first)  {
919                                         ChannelDriver *driver= fcu->driver;
920                                         DriverVar *dvar;
921                                         
922                                         for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
923                                                 /* loop over all targets used here */
924                                                 DRIVER_TARGETS_USED_LOOPER(dvar) 
925                                                 {
926                                                         if (lastadded != dtar->id) {
927                                                                 // XXX this lastadded check is rather lame, and also fails quite badly...
928                                                                 outliner_add_element(soops, &ted->subtree, dtar->id, ted, TSE_LINKED_OB, 0);
929                                                                 lastadded= dtar->id;
930                                                         }
931                                                 }
932                                                 DRIVER_TARGETS_LOOPER_END
933                                         }
934                                 }
935                         }
936                 }
937                 
938                 /* NLA Data */
939                 if (adt->nla_tracks.first) {
940                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, adt, te, TSE_NLA, 0);
941                         NlaTrack *nlt;
942                         int a= 0;
943                         
944                         tenla->name= "NLA Tracks";
945                         
946                         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
947                                 TreeElement *tenlt= outliner_add_element(soops, &tenla->subtree, nlt, tenla, TSE_NLA_TRACK, a);
948                                 NlaStrip *strip;
949                                 TreeElement *ten;
950                                 int b= 0;
951                                 
952                                 tenlt->name= nlt->name;
953                                 
954                                 for (strip=nlt->strips.first; strip; strip=strip->next, b++) {
955                                         ten= outliner_add_element(soops, &tenlt->subtree, strip->act, tenlt, TSE_NLA_ACTION, b);
956                                         if(ten) ten->directdata= strip;
957                                 }
958                         }
959                 }
960         }
961         else if(type==TSE_SEQUENCE) {
962                 Sequence *seq= (Sequence*) idv;
963                 Sequence *p;
964
965                 /*
966                  * The idcode is a little hack, but the outliner
967                  * only check te->idcode if te->type is equal to zero,
968                  * so this is "safe".
969                  */
970                 te->idcode= seq->type;
971                 te->directdata= seq;
972
973                 if(seq->type<7) {
974                         /*
975                          * This work like the sequence.
976                          * If the sequence have a name (not default name)
977                          * show it, in other case put the filename.
978                          */
979                         if(strcmp(seq->name, "SQ"))
980                                 te->name= seq->name;
981                         else {
982                                 if((seq->strip) && (seq->strip->stripdata))
983                                         te->name= seq->strip->stripdata->name;
984                                 else if((seq->strip) && (seq->strip->tstripdata) && (seq->strip->tstripdata->ibuf))
985                                         te->name= seq->strip->tstripdata->ibuf->name;
986                                 else
987                                         te->name= "SQ None";
988                         }
989
990                         if(seq->type==SEQ_META) {
991                                 te->name= "Meta Strip";
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                 else
1002                         te->name= "Effect";
1003         }
1004         else if(type==TSE_SEQ_STRIP) {
1005                 Strip *strip= (Strip *)idv;
1006
1007                 if(strip->dir)
1008                         te->name= strip->dir;
1009                 else
1010                         te->name= "Strip None";
1011                 te->directdata= strip;
1012         }
1013         else if(type==TSE_SEQUENCE_DUP) {
1014                 Sequence *seq= (Sequence*)idv;
1015
1016                 te->idcode= seq->type;
1017                 te->directdata= seq;
1018                 te->name= seq->strip->stripdata->name;
1019         }
1020         else if(ELEM3(type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM)) {
1021                 PointerRNA pptr, propptr, *ptr= (PointerRNA*)idv;
1022                 PropertyRNA *prop, *iterprop;
1023                 PropertyType proptype;
1024                 int a, tot;
1025
1026                 /* we do lazy build, for speed and to avoid infinite recusion */
1027
1028                 if(ptr->data == NULL) {
1029                         te->name= "(empty)";
1030                 }
1031                 else if(type == TSE_RNA_STRUCT) {
1032                         /* struct */
1033                         te->name= RNA_struct_name_get_alloc(ptr, NULL, 0);
1034
1035                         if(te->name)
1036                                 te->flag |= TE_FREE_NAME;
1037                         else
1038                                 te->name= (char*)RNA_struct_ui_name(ptr->type);
1039
1040                         iterprop= RNA_struct_iterator_property(ptr->type);
1041                         tot= RNA_property_collection_length(ptr, iterprop);
1042
1043                         /* auto open these cases */
1044                         if(!parent || (RNA_property_type(parent->directdata)) == PROP_POINTER)
1045                                 if(!tselem->used)
1046                                         tselem->flag &= ~TSE_CLOSED;
1047
1048                         if(!(tselem->flag & TSE_CLOSED)) {
1049                                 for(a=0; a<tot; a++)
1050                                         outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_PROPERTY, a);
1051                         }
1052                         else if(tot)
1053                                 te->flag |= TE_LAZY_CLOSED;
1054
1055                         te->rnaptr= *ptr;
1056                 }
1057                 else if(type == TSE_RNA_PROPERTY) {
1058                         /* property */
1059                         iterprop= RNA_struct_iterator_property(ptr->type);
1060                         RNA_property_collection_lookup_int(ptr, iterprop, index, &propptr);
1061
1062                         prop= propptr.data;
1063                         proptype= RNA_property_type(prop);
1064
1065                         te->name= (char*)RNA_property_ui_name(prop);
1066                         te->directdata= prop;
1067                         te->rnaptr= *ptr;
1068
1069                         if(proptype == PROP_POINTER) {
1070                                 pptr= RNA_property_pointer_get(ptr, prop);
1071
1072                                 if(pptr.data) {
1073                                         if(!(tselem->flag & TSE_CLOSED))
1074                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, -1);
1075                                         else
1076                                                 te->flag |= TE_LAZY_CLOSED;
1077                                 }
1078                         }
1079                         else if(proptype == PROP_COLLECTION) {
1080                                 tot= RNA_property_collection_length(ptr, prop);
1081
1082                                 if(!(tselem->flag & TSE_CLOSED)) {
1083                                         for(a=0; a<tot; a++) {
1084                                                 RNA_property_collection_lookup_int(ptr, prop, a, &pptr);
1085                                                 outliner_add_element(soops, &te->subtree, (void*)&pptr, te, TSE_RNA_STRUCT, -1);
1086                                         }
1087                                 }
1088                                 else if(tot)
1089                                         te->flag |= TE_LAZY_CLOSED;
1090                         }
1091                         else if(ELEM3(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
1092                                 tot= RNA_property_array_length(ptr, prop);
1093
1094                                 if(!(tselem->flag & TSE_CLOSED)) {
1095                                         for(a=0; a<tot; a++)
1096                                                 outliner_add_element(soops, &te->subtree, (void*)ptr, te, TSE_RNA_ARRAY_ELEM, a);
1097                                 }
1098                                 else if(tot)
1099                                         te->flag |= TE_LAZY_CLOSED;
1100                         }
1101                 }
1102                 else if(type == TSE_RNA_ARRAY_ELEM) {
1103                         char c;
1104
1105                         prop= parent->directdata;
1106
1107                         te->directdata= prop;
1108                         te->rnaptr= *ptr;
1109                         te->index= index;
1110
1111                         c= RNA_property_array_item_char(prop, index);
1112
1113                         te->name= MEM_callocN(sizeof(char)*20, "OutlinerRNAArrayName");
1114                         if(c) sprintf(te->name, "  %c", c);
1115                         else sprintf(te->name, "  %d", index+1);
1116                         te->flag |= TE_FREE_NAME;
1117                 }
1118         }
1119         else if(type == TSE_KEYMAP) {
1120                 wmKeyMap *km= (wmKeyMap *)idv;
1121                 wmKeyMapItem *kmi;
1122                 char opname[OP_MAX_TYPENAME];
1123                 
1124                 te->directdata= idv;
1125                 te->name= km->idname;
1126                 
1127                 if(!(tselem->flag & TSE_CLOSED)) {
1128                         a= 0;
1129                         
1130                         for (kmi= km->items.first; kmi; kmi= kmi->next, a++) {
1131                                 const char *key= WM_key_event_string(kmi->type);
1132                                 
1133                                 if(key[0]) {
1134                                         wmOperatorType *ot= NULL;
1135                                         
1136                                         if(kmi->propvalue);
1137                                         else ot= WM_operatortype_find(kmi->idname, 0);
1138                                         
1139                                         if(ot || kmi->propvalue) {
1140                                                 TreeElement *ten= outliner_add_element(soops, &te->subtree, kmi, te, TSE_KEYMAP_ITEM, a);
1141                                                 
1142                                                 ten->directdata= kmi;
1143                                                 
1144                                                 if(kmi->propvalue) {
1145                                                         ten->name= "Modal map, not yet";
1146                                                 }
1147                                                 else {
1148                                                         WM_operator_py_idname(opname, ot->idname);
1149                                                         ten->name= BLI_strdup(opname);
1150                                                         ten->flag |= TE_FREE_NAME;
1151                                                 }
1152                                         }
1153                                 }
1154                         }
1155                 }
1156                 else 
1157                         te->flag |= TE_LAZY_CLOSED;
1158         }
1159
1160         return te;
1161 }
1162
1163 static void outliner_make_hierarchy(SpaceOops *soops, ListBase *lb)
1164 {
1165         TreeElement *te, *ten, *tep;
1166         TreeStoreElem *tselem;
1167
1168         /* build hierarchy */
1169         // XXX also, set extents here...
1170         te= lb->first;
1171         while(te) {
1172                 ten= te->next;
1173                 tselem= TREESTORE(te);
1174                 
1175                 if(tselem->type==0 && te->idcode==ID_OB) {
1176                         Object *ob= (Object *)tselem->id;
1177                         if(ob->parent && ob->parent->id.newid) {
1178                                 BLI_remlink(lb, te);
1179                                 tep= (TreeElement *)ob->parent->id.newid;
1180                                 BLI_addtail(&tep->subtree, te);
1181                                 // set correct parent pointers
1182                                 for(te=tep->subtree.first; te; te= te->next) te->parent= tep;
1183                         }
1184                 }
1185                 te= ten;
1186         }
1187 }
1188
1189 /* Helped function to put duplicate sequence in the same tree. */
1190 int need_add_seq_dup(Sequence *seq)
1191 {
1192         Sequence *p;
1193
1194         if((!seq->strip) || (!seq->strip->stripdata) || (!seq->strip->stripdata->name))
1195                 return(1);
1196
1197         /*
1198          * First check backward, if we found a duplicate
1199          * sequence before this, don't need it, just return.
1200          */
1201         p= seq->prev;
1202         while(p) {
1203                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1204                         p= p->prev;
1205                         continue;
1206                 }
1207
1208                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1209                         return(2);
1210                 p= p->prev;
1211         }
1212
1213         p= seq->next;
1214         while(p) {
1215                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1216                         p= p->next;
1217                         continue;
1218                 }
1219
1220                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1221                         return(0);
1222                 p= p->next;
1223         }
1224         return(1);
1225 }
1226
1227 void add_seq_dup(SpaceOops *soops, Sequence *seq, TreeElement *te, short index)
1228 {
1229         TreeElement *ch;
1230         Sequence *p;
1231
1232         p= seq;
1233         while(p) {
1234                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
1235                         p= p->next;
1236                         continue;
1237                 }
1238
1239                 if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
1240                         ch= outliner_add_element(soops, &te->subtree, (void*)p, te, TSE_SEQUENCE, index);
1241                 p= p->next;
1242         }
1243 }
1244
1245 static int outliner_filter_has_name(TreeElement *te, char *name, int flags)
1246 {
1247 #if 0
1248         int found= 0;
1249         
1250         /* determine if match */
1251         if (flags & SO_FIND_CASE_SENSITIVE) {
1252                 if (flags & SO_FIND_COMPLETE)
1253                         found= strcmp(te->name, name) == 0;
1254                 else
1255                         found= strstr(te->name, name) != NULL;
1256         }
1257         else {
1258                 if (flags & SO_FIND_COMPLETE)
1259                         found= BLI_strcasecmp(te->name, name) == 0;
1260                 else
1261                         found= BLI_strcasestr(te->name, name) != NULL;
1262         }
1263 #else
1264         
1265         int fn_flag= 0;
1266         int found= 0;
1267         
1268         if ((flags & SO_FIND_CASE_SENSITIVE) == 0)
1269                 fn_flag |= FNM_CASEFOLD;
1270
1271         if (flags & SO_FIND_COMPLETE) {
1272                 found= fnmatch(name, te->name, fn_flag)==0;
1273         }
1274         else {
1275                 char fn_name[sizeof(((struct SpaceOops *)NULL)->search_string) + 2];
1276                 sprintf(fn_name, "*%s*", name);
1277                 found= fnmatch(fn_name, te->name, fn_flag)==0;
1278         }
1279         return found;
1280 #endif
1281 }
1282
1283 static int outliner_filter_tree(SpaceOops *soops, ListBase *lb)
1284 {
1285         TreeElement *te, *ten;
1286         TreeStoreElem *tselem;
1287         
1288         /* although we don't have any search string, we return TRUE 
1289          * since the entire tree is ok then...
1290          */
1291         if (soops->search_string[0]==0) 
1292                 return 1;
1293
1294         for (te= lb->first; te; te= ten) {
1295                 ten= te->next;
1296                 
1297                 if (0==outliner_filter_has_name(te, soops->search_string, soops->search_flags)) {
1298                         /* item isn't something we're looking for, but...
1299                          *      - if the subtree is expanded, check if there are any matches that can be easily found
1300                          *              so that searching for "cu" in the default scene will still match the Cube
1301                          *      - otherwise, we can't see within the subtree and the item doesn't match,
1302                          *              so these can be safely ignored (i.e. the subtree can get freed)
1303                          */
1304                         tselem= TREESTORE(te);
1305                         
1306                         if ((tselem->flag & TSE_CLOSED) || outliner_filter_tree(soops, &te->subtree)==0) { 
1307                                 outliner_free_tree(&te->subtree);
1308                                 BLI_remlink(lb, te);
1309                                 
1310                                 if(te->flag & TE_FREE_NAME) MEM_freeN(te->name);
1311                                 MEM_freeN(te);
1312                         }
1313                 }
1314                 else {
1315                         /* filter subtree too */
1316                         outliner_filter_tree(soops, &te->subtree);
1317                 }
1318         }
1319         
1320         /* if there are still items in the list, that means that there were still some matches */
1321         return (lb->first != NULL);
1322 }
1323
1324
1325 static void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops)
1326 {
1327         Base *base;
1328         Object *ob;
1329         TreeElement *te=NULL, *ten;
1330         TreeStoreElem *tselem;
1331         int show_opened= (soops->treestore==NULL); /* on first view, we open scenes */
1332
1333         if(soops->tree.first && (soops->storeflag & SO_TREESTORE_REDRAW))
1334            return;
1335            
1336         outliner_free_tree(&soops->tree);
1337         outliner_storage_cleanup(soops);
1338         
1339         /* clear ob id.new flags */
1340         for(ob= mainvar->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
1341         
1342         /* options */
1343         if(soops->outlinevis == SO_LIBRARIES) {
1344                 Library *lib;
1345                 
1346                 for(lib= mainvar->library.first; lib; lib= lib->id.next) {
1347                         ten= outliner_add_element(soops, &soops->tree, lib, NULL, 0, 0);
1348                         lib->id.newid= (ID *)ten;
1349                 }
1350                 /* make hierarchy */
1351                 ten= soops->tree.first;
1352                 while(ten) {
1353                         TreeElement *nten= ten->next, *par;
1354                         tselem= TREESTORE(ten);
1355                         lib= (Library *)tselem->id;
1356                         if(lib->parent) {
1357                                 BLI_remlink(&soops->tree, ten);
1358                                 par= (TreeElement *)lib->parent->id.newid;
1359                                 BLI_addtail(&par->subtree, ten);
1360                                 ten->parent= par;
1361                         }
1362                         ten= nten;
1363                 }
1364                 /* restore newid pointers */
1365                 for(lib= mainvar->library.first; lib; lib= lib->id.next)
1366                         lib->id.newid= NULL;
1367                 
1368         }
1369         else if(soops->outlinevis == SO_ALL_SCENES) {
1370                 Scene *sce;
1371                 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
1372                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
1373                         tselem= TREESTORE(te);
1374                         if(sce==scene && show_opened) 
1375                                 tselem->flag &= ~TSE_CLOSED;
1376                         
1377                         for(base= sce->base.first; base; base= base->next) {
1378                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
1379                                 ten->directdata= base;
1380                         }
1381                         outliner_make_hierarchy(soops, &te->subtree);
1382                         /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1383                         for(base= sce->base.first; base; base= base->next) base->object->id.newid= NULL;
1384                 }
1385         }
1386         else if(soops->outlinevis == SO_CUR_SCENE) {
1387                 
1388                 outliner_add_scene_contents(soops, &soops->tree, scene, NULL);
1389                 
1390                 for(base= scene->base.first; base; base= base->next) {
1391                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1392                         ten->directdata= base;
1393                 }
1394                 outliner_make_hierarchy(soops, &soops->tree);
1395         }
1396         else if(soops->outlinevis == SO_VISIBLE) {
1397                 for(base= scene->base.first; base; base= base->next) {
1398                         if(base->lay & scene->lay)
1399                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1400                 }
1401                 outliner_make_hierarchy(soops, &soops->tree);
1402         }
1403         else if(soops->outlinevis == SO_GROUPS) {
1404                 Group *group;
1405                 GroupObject *go;
1406                 
1407                 for(group= mainvar->group.first; group; group= group->id.next) {
1408                         if(group->id.us) {
1409                                 te= outliner_add_element(soops, &soops->tree, group, NULL, 0, 0);
1410                                 tselem= TREESTORE(te);
1411                                 
1412                                 for(go= group->gobject.first; go; go= go->next) {
1413                                         ten= outliner_add_element(soops, &te->subtree, go->ob, te, 0, 0);
1414                                         ten->directdata= NULL; /* eh, why? */
1415                                 }
1416                                 outliner_make_hierarchy(soops, &te->subtree);
1417                                 /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1418                                 for(go= group->gobject.first; go; go= go->next) go->ob->id.newid= NULL;
1419                         }
1420                 }
1421         }
1422         else if(soops->outlinevis == SO_SAME_TYPE) {
1423                 Object *ob= OBACT;
1424                 if(ob) {
1425                         for(base= scene->base.first; base; base= base->next) {
1426                                 if(base->object->type==ob->type) {
1427                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1428                                         ten->directdata= base;
1429                                 }
1430                         }
1431                         outliner_make_hierarchy(soops, &soops->tree);
1432                 }
1433         }
1434         else if(soops->outlinevis == SO_SELECTED) {
1435                 for(base= scene->base.first; base; base= base->next) {
1436                         if(base->lay & scene->lay) {
1437                                 if(base==BASACT || (base->flag & SELECT)) {
1438                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1439                                         ten->directdata= base;
1440                                 }
1441                         }
1442                 }
1443                 outliner_make_hierarchy(soops, &soops->tree);
1444         }
1445         else if(soops->outlinevis==SO_SEQUENCE) {
1446                 Sequence *seq;
1447                 Editing *ed= seq_give_editing(scene, FALSE);
1448                 int op;
1449
1450                 if(ed==NULL)
1451                         return;
1452
1453                 seq= ed->seqbasep->first;
1454                 if(!seq)
1455                         return;
1456
1457                 while(seq) {
1458                         op= need_add_seq_dup(seq);
1459                         if(op==1)
1460                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1461                         else if(op==0) {
1462                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1463                                 add_seq_dup(soops, seq, ten, 0);
1464                         }
1465                         seq= seq->next;
1466                 }
1467         }
1468         else if(soops->outlinevis==SO_DATABLOCKS) {
1469                 PointerRNA mainptr;
1470
1471                 RNA_main_pointer_create(mainvar, &mainptr);
1472
1473                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1474
1475                 if(show_opened)  {
1476                         tselem= TREESTORE(ten);
1477                         tselem->flag &= ~TSE_CLOSED;
1478                 }
1479         }
1480         else if(soops->outlinevis==SO_USERDEF) {
1481                 PointerRNA userdefptr;
1482
1483                 RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &userdefptr);
1484
1485                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1486
1487                 if(show_opened)  {
1488                         tselem= TREESTORE(ten);
1489                         tselem->flag &= ~TSE_CLOSED;
1490                 }
1491         }
1492         else if(soops->outlinevis==SO_KEYMAP) {
1493                 wmWindowManager *wm= mainvar->wm.first;
1494                 wmKeyMap *km;
1495                 
1496                 for(km= wm->defaultconf->keymaps.first; km; km= km->next) {
1497                         ten= outliner_add_element(soops, &soops->tree, (void*)km, NULL, TSE_KEYMAP, 0);
1498                 }
1499         }
1500         else {
1501                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1502                 if(ten) ten->directdata= BASACT;
1503         }
1504
1505         outliner_sort(soops, &soops->tree);
1506         outliner_filter_tree(soops, &soops->tree);
1507 }
1508
1509 /* **************** INTERACTIVE ************* */
1510
1511 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
1512 {
1513         TreeElement *te;
1514         int level=curlevel, lev;
1515         
1516         for(te= lb->first; te; te= te->next) {
1517                 
1518                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
1519                 if(lev>level) level= lev;
1520         }
1521         return level;
1522 }
1523
1524 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
1525 {
1526         TreeElement *te;
1527         TreeStoreElem *tselem;
1528         int level;
1529         
1530         for(te= lb->first; te; te= te->next) {
1531                 tselem= TREESTORE(te);
1532                 if(tselem->flag & flag) return curlevel;
1533                 
1534                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
1535                 if(level) return level;
1536         }
1537         return 0;
1538 }
1539
1540 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
1541 {
1542         TreeElement *te;
1543         TreeStoreElem *tselem;
1544         
1545         for(te= lb->first; te; te= te->next) {
1546                 tselem= TREESTORE(te);
1547                 if(set==0) tselem->flag &= ~flag;
1548                 else tselem->flag |= flag;
1549                 outliner_set_flag(soops, &te->subtree, flag, set);
1550         }
1551 }
1552
1553 /* --- */
1554
1555 void object_toggle_visibility_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1556 {
1557         Base *base= (Base *)te->directdata;
1558         
1559         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1560         if(base) {
1561                 base->object->restrictflag^=OB_RESTRICT_VIEW;
1562         }
1563 }
1564
1565 static int outliner_toggle_visibility_exec(bContext *C, wmOperator *op)
1566 {
1567         SpaceOops *soops= CTX_wm_space_outliner(C);
1568         Scene *scene= CTX_data_scene(C);
1569         ARegion *ar= CTX_wm_region(C);
1570         
1571         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_visibility_cb);
1572         
1573         ED_region_tag_redraw(ar);
1574         
1575         return OPERATOR_FINISHED;
1576 }
1577
1578 void OUTLINER_OT_visibility_toggle(wmOperatorType *ot)
1579 {
1580         /* identifiers */
1581         ot->name= "Toggle Visability";
1582         ot->idname= "OUTLINER_OT_visibility_toggle";
1583         ot->description= "Toggle the visibility of selected items";
1584         
1585         /* callbacks */
1586         ot->exec= outliner_toggle_visibility_exec;
1587         ot->poll= ED_operator_outliner_active;
1588         
1589         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1590 }
1591
1592 /* --- */
1593
1594 static void object_toggle_selectability_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1595 {
1596         Base *base= (Base *)te->directdata;
1597         
1598         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1599         if(base) {
1600                 base->object->restrictflag^=OB_RESTRICT_SELECT;
1601         }
1602 }
1603
1604 static int outliner_toggle_selectability_exec(bContext *C, wmOperator *op)
1605 {
1606         SpaceOops *soops= CTX_wm_space_outliner(C);
1607         Scene *scene= CTX_data_scene(C);
1608         ARegion *ar= CTX_wm_region(C);
1609         
1610         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_selectability_cb);
1611         
1612         ED_region_tag_redraw(ar);
1613         
1614         return OPERATOR_FINISHED;
1615 }
1616
1617 void OUTLINER_OT_selectability_toggle(wmOperatorType *ot)
1618 {
1619         /* identifiers */
1620         ot->name= "Toggle Selectability";
1621         ot->idname= "OUTLINER_OT_selectability_toggle";
1622         ot->description= "Toggle the selectability";
1623         
1624         /* callbacks */
1625         ot->exec= outliner_toggle_selectability_exec;
1626         ot->poll= ED_operator_outliner_active;
1627         
1628         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1629 }
1630
1631 /* --- */
1632
1633 void object_toggle_renderability_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1634 {
1635         Base *base= (Base *)te->directdata;
1636         
1637         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1638         if(base) {
1639                 base->object->restrictflag^=OB_RESTRICT_RENDER;
1640         }
1641 }
1642
1643 static int outliner_toggle_renderability_exec(bContext *C, wmOperator *op)
1644 {
1645         SpaceOops *soops= CTX_wm_space_outliner(C);
1646         Scene *scene= CTX_data_scene(C);
1647         ARegion *ar= CTX_wm_region(C);
1648         
1649         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_renderability_cb);
1650         
1651         ED_region_tag_redraw(ar);
1652         
1653         return OPERATOR_FINISHED;
1654 }
1655
1656 void OUTLINER_OT_renderability_toggle(wmOperatorType *ot)
1657 {
1658         /* identifiers */
1659         ot->name= "Toggle Renderability";
1660         ot->idname= "OUTLINER_OT_renderability_toggle";
1661         ot->description= "Toggle the renderbility of selected items";
1662         
1663         /* callbacks */
1664         ot->exec= outliner_toggle_renderability_exec;
1665         ot->poll= ED_operator_outliner_active;
1666         
1667         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1668 }
1669
1670 /* --- */
1671
1672 static int outliner_toggle_expanded_exec(bContext *C, wmOperator *op)
1673 {
1674         SpaceOops *soops= CTX_wm_space_outliner(C);
1675         ARegion *ar= CTX_wm_region(C);
1676         
1677         if (outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
1678                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
1679         else 
1680                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
1681         
1682         ED_region_tag_redraw(ar);
1683         
1684         return OPERATOR_FINISHED;
1685 }
1686
1687 void OUTLINER_OT_expanded_toggle(wmOperatorType *ot)
1688 {
1689         /* identifiers */
1690         ot->name= "Expand/Collapse All";
1691         ot->idname= "OUTLINER_OT_expanded_toggle";
1692         ot->description= "Expand/Collapse all items";
1693         
1694         /* callbacks */
1695         ot->exec= outliner_toggle_expanded_exec;
1696         ot->poll= ED_operator_outliner_active;
1697         
1698         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1699 }
1700
1701 /* --- */
1702
1703 static int outliner_toggle_selected_exec(bContext *C, wmOperator *op)
1704 {
1705         SpaceOops *soops= CTX_wm_space_outliner(C);
1706         ARegion *ar= CTX_wm_region(C);
1707         
1708         if (outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
1709                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
1710         else 
1711                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
1712         
1713         soops->storeflag |= SO_TREESTORE_REDRAW;
1714         
1715         ED_region_tag_redraw(ar);
1716         
1717         return OPERATOR_FINISHED;
1718 }
1719
1720 void OUTLINER_OT_selected_toggle(wmOperatorType *ot)
1721 {
1722         /* identifiers */
1723         ot->name= "Toggle Selected";
1724         ot->idname= "OUTLINER_OT_selected_toggle";
1725         ot->description= "Toggle the Outliner selection of items";
1726         
1727         /* callbacks */
1728         ot->exec= outliner_toggle_selected_exec;
1729         ot->poll= ED_operator_outliner_active;
1730         
1731         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1732 }
1733
1734 /* --- */
1735
1736 /* helper function for Show/Hide one level operator */
1737 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
1738 {
1739         TreeElement *te;
1740         TreeStoreElem *tselem;
1741         
1742         for(te= lb->first; te; te= te->next) {
1743                 tselem= TREESTORE(te);
1744                 
1745                 if(open) {
1746                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
1747                 }
1748                 else {
1749                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
1750                 }
1751                 
1752                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
1753         }
1754 }
1755
1756 static int outliner_one_level_exec(bContext *C, wmOperator *op)
1757 {
1758         SpaceOops *soops= CTX_wm_space_outliner(C);
1759         ARegion *ar= CTX_wm_region(C);
1760         int add= RNA_boolean_get(op->ptr, "open");
1761         int level;
1762         
1763         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
1764         if(add==1) {
1765                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
1766         }
1767         else {
1768                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
1769                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
1770         }
1771         
1772         ED_region_tag_redraw(ar);
1773         
1774         return OPERATOR_FINISHED;
1775 }
1776
1777 void OUTLINER_OT_show_one_level(wmOperatorType *ot)
1778 {
1779         /* identifiers */
1780         ot->name= "Show/Hide One Level";
1781         ot->idname= "OUTLINER_OT_show_one_level";
1782         
1783         /* callbacks */
1784         ot->exec= outliner_one_level_exec;
1785         ot->poll= ED_operator_outliner_active;
1786         
1787         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1788         
1789         /* properties */
1790         RNA_def_boolean(ot->srna, "open", 1, "Open", "Expand all entries one level deep.");
1791 }
1792
1793 /* return 1 when levels were opened */
1794 static int outliner_open_back(SpaceOops *soops, TreeElement *te)
1795 {
1796         TreeStoreElem *tselem;
1797         int retval= 0;
1798         
1799         for (te= te->parent; te; te= te->parent) {
1800                 tselem= TREESTORE(te);
1801                 if (tselem->flag & TSE_CLOSED) { 
1802                         tselem->flag &= ~TSE_CLOSED;
1803                         retval= 1;
1804                 }
1805         }
1806         return retval;
1807 }
1808
1809 /* This is not used anywhere at the moment */
1810 #if 0
1811 static void outliner_open_reveal(SpaceOops *soops, ListBase *lb, TreeElement *teFind, int *found)
1812 {
1813         TreeElement *te;
1814         TreeStoreElem *tselem;
1815         
1816         for (te= lb->first; te; te= te->next) {
1817                 /* check if this tree-element was the one we're seeking */
1818                 if (te == teFind) {
1819                         *found= 1;
1820                         return;
1821                 }
1822                 
1823                 /* try to see if sub-tree contains it then */
1824                 outliner_open_reveal(soops, &te->subtree, teFind, found);
1825                 if (*found) {
1826                         tselem= TREESTORE(te);
1827                         if (tselem->flag & TSE_CLOSED) 
1828                                 tselem->flag &= ~TSE_CLOSED;
1829                         return;
1830                 }
1831         }
1832 }
1833 #endif
1834
1835 // XXX just use View2D ops for this?
1836 void outliner_page_up_down(Scene *scene, ARegion *ar, SpaceOops *soops, int up)
1837 {
1838         int dy= ar->v2d.mask.ymax-ar->v2d.mask.ymin;
1839         
1840         if(up == -1) dy= -dy;
1841         ar->v2d.cur.ymin+= dy;
1842         ar->v2d.cur.ymax+= dy;
1843         
1844         soops->storeflag |= SO_TREESTORE_REDRAW;
1845 }
1846
1847 /* **** do clicks on items ******* */
1848
1849 static int tree_element_active_renderlayer(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
1850 {
1851         Scene *sce;
1852         
1853         /* paranoia check */
1854         if(te->idcode!=ID_SCE)
1855                 return 0;
1856         sce= (Scene *)tselem->id;
1857         
1858         if(set) {
1859                 sce->r.actlay= tselem->nr;
1860                 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, sce);
1861         }
1862         else {
1863                 return sce->r.actlay==tselem->nr;
1864         }
1865         return 0;
1866 }
1867
1868 static void tree_element_set_active_object(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1869 {
1870         TreeStoreElem *tselem= TREESTORE(te);
1871         Scene *sce;
1872         Base *base;
1873         Object *ob= NULL;
1874         
1875         /* if id is not object, we search back */
1876         if(te->idcode==ID_OB) ob= (Object *)tselem->id;
1877         else {
1878                 ob= (Object *)outliner_search_back(soops, te, ID_OB);
1879                 if(ob==OBACT) return;
1880         }
1881         if(ob==NULL) return;
1882         
1883         sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
1884         if(sce && scene != sce) {
1885                 ED_screen_set_scene(C, sce);
1886         }
1887         
1888         /* find associated base in current scene */
1889         for(base= FIRSTBASE; base; base= base->next) 
1890                 if(base->object==ob) break;
1891         if(base) {
1892                 if(set==2) {
1893                         /* swap select */
1894                         if(base->flag & SELECT)
1895                                 ED_base_object_select(base, BA_DESELECT);
1896                         else 
1897                                 ED_base_object_select(base, BA_SELECT);
1898                 }
1899                 else {
1900                         Base *b;
1901                         /* deleselect all */
1902                         for(b= FIRSTBASE; b; b= b->next) {
1903                                 b->flag &= ~SELECT;
1904                                 b->object->flag= b->flag;
1905                         }
1906                         ED_base_object_select(base, BA_SELECT);
1907                 }
1908                 if(C)
1909                         ED_base_object_activate(C, base); /* adds notifier */
1910         }
1911         
1912         if(ob!=scene->obedit) 
1913                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
1914         
1915         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
1916
1917 }
1918
1919 static int tree_element_active_material(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1920 {
1921         TreeElement *tes;
1922         Object *ob;
1923         
1924         /* we search for the object parent */
1925         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1926         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1927         
1928         /* searching in ob mat array? */
1929         tes= te->parent;
1930         if(tes->idcode==ID_OB) {
1931                 if(set) {
1932                         ob->actcol= te->index+1;
1933                         ob->matbits[te->index]= 1;      // make ob material active too
1934                         ob->colbits |= (1<<te->index);
1935                 }
1936                 else {
1937                         if(ob->actcol == te->index+1) 
1938                                 if(ob->matbits[te->index]) return 1;
1939                 }
1940         }
1941         /* or we search for obdata material */
1942         else {
1943                 if(set) {
1944                         ob->actcol= te->index+1;
1945                         ob->matbits[te->index]= 0;      // make obdata material active too
1946                         ob->colbits &= ~(1<<te->index);
1947                 }
1948                 else {
1949                         if(ob->actcol == te->index+1)
1950                                 if(ob->matbits[te->index]==0) return 1;
1951                 }
1952         }
1953         if(set) {
1954                 WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING, NULL);
1955         }
1956         return 0;
1957 }
1958
1959 static int tree_element_active_texture(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1960 {
1961         TreeElement *tep;
1962         TreeStoreElem *tselem, *tselemp;
1963         Object *ob=OBACT;
1964         SpaceButs *sbuts=NULL;
1965         
1966         if(ob==NULL) return 0; // no active object
1967         
1968         tselem= TREESTORE(te);
1969         
1970         /* find buttons area (note, this is undefined really still, needs recode in blender) */
1971         /* XXX removed finding sbuts */
1972         
1973         /* where is texture linked to? */
1974         tep= te->parent;
1975         tselemp= TREESTORE(tep);
1976         
1977         if(tep->idcode==ID_WO) {
1978                 World *wrld= (World *)tselemp->id;
1979
1980                 if(set) {
1981                         if(sbuts) {
1982                                 // XXX sbuts->tabo= TAB_SHADING_TEX;    // hack from header_buttonswin.c
1983                                 // XXX sbuts->texfrom= 1;
1984                         }
1985 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1986                         wrld->texact= te->index;
1987                 }
1988                 else if(tselemp->id == (ID *)(scene->world)) {
1989                         if(wrld->texact==te->index) return 1;
1990                 }
1991         }
1992         else if(tep->idcode==ID_LA) {
1993                 Lamp *la= (Lamp *)tselemp->id;
1994                 if(set) {
1995                         if(sbuts) {
1996                                 // XXX sbuts->tabo= TAB_SHADING_TEX;    // hack from header_buttonswin.c
1997                                 // XXX sbuts->texfrom= 2;
1998                         }
1999 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
2000                         la->texact= te->index;
2001                 }
2002                 else {
2003                         if(tselemp->id == ob->data) {
2004                                 if(la->texact==te->index) return 1;
2005                         }
2006                 }
2007         }
2008         else if(tep->idcode==ID_MA) {
2009                 Material *ma= (Material *)tselemp->id;
2010                 if(set) {
2011                         if(sbuts) {
2012                                 //sbuts->tabo= TAB_SHADING_TEX; // hack from header_buttonswin.c
2013                                 // XXX sbuts->texfrom= 0;
2014                         }
2015 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
2016                         ma->texact= (char)te->index;
2017                         
2018                         /* also set active material */
2019                         ob->actcol= tep->index+1;
2020                 }
2021                 else if(tep->flag & TE_ACTIVE) {        // this is active material
2022                         if(ma->texact==te->index) return 1;
2023                 }
2024         }
2025         
2026         return 0;
2027 }
2028
2029
2030 static int tree_element_active_lamp(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2031 {
2032         Object *ob;
2033         
2034         /* we search for the object parent */
2035         ob= (Object *)outliner_search_back(soops, te, ID_OB);
2036         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
2037         
2038         if(set) {
2039 // XXX          extern_set_butspace(F5KEY, 0);
2040         }
2041         else return 1;
2042         
2043         return 0;
2044 }
2045
2046 static int tree_element_active_world(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2047 {
2048         TreeElement *tep;
2049         TreeStoreElem *tselem=NULL;
2050         Scene *sce=NULL;
2051         
2052         tep= te->parent;
2053         if(tep) {
2054                 tselem= TREESTORE(tep);
2055                 sce= (Scene *)tselem->id;
2056         }
2057         
2058         if(set) {       // make new scene active
2059                 if(sce && scene != sce) {
2060                         ED_screen_set_scene(C, sce);
2061                 }
2062         }
2063         
2064         if(tep==NULL || tselem->id == (ID *)scene) {
2065                 if(set) {
2066 // XXX                  extern_set_butspace(F8KEY, 0);
2067                 }
2068                 else {
2069                         return 1;
2070                 }
2071         }
2072         return 0;
2073 }
2074
2075 static int tree_element_active_defgroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2076 {
2077         Object *ob;
2078         
2079         /* id in tselem is object */
2080         ob= (Object *)tselem->id;
2081         if(set) {
2082                 ob->actdef= te->index+1;
2083                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2084                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
2085         }
2086         else {
2087                 if(ob==OBACT)
2088                         if(ob->actdef== te->index+1) return 1;
2089         }
2090         return 0;
2091 }
2092
2093 static int tree_element_active_posegroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2094 {
2095         Object *ob= (Object *)tselem->id;
2096         
2097         if(set) {
2098                 if (ob->pose) {
2099                         ob->pose->active_group= te->index+1;
2100                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
2101                 }
2102         }
2103         else {
2104                 if(ob==OBACT && ob->pose) {
2105                         if (ob->pose->active_group== te->index+1) return 1;
2106                 }
2107         }
2108         return 0;
2109 }
2110
2111 static int tree_element_active_posechannel(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2112 {
2113         Object *ob= (Object *)tselem->id;
2114         bArmature *arm= ob->data;
2115         bPoseChannel *pchan= te->directdata;
2116         
2117         if(set) {
2118                 if(!(pchan->bone->flag & BONE_HIDDEN_P)) {
2119                         
2120                         if(set==2) ED_pose_deselectall(ob, 2, 0);       // 2 = clear active tag
2121                         else ED_pose_deselectall(ob, 0, 0);     // 0 = deselect 
2122                         
2123                         if(set==2 && (pchan->bone->flag & BONE_SELECTED)) {
2124                                 pchan->bone->flag &= ~BONE_SELECTED;
2125                                 if(arm->act_bone==pchan->bone)
2126                                         arm->act_bone= NULL;
2127                         } else {
2128                                 pchan->bone->flag |= BONE_SELECTED;
2129                                 arm->act_bone= pchan->bone;
2130                         }
2131                         
2132                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, ob);
2133
2134                 }
2135         }
2136         else {
2137                 if(ob==OBACT && ob->pose) {
2138                         if (pchan->bone->flag & BONE_SELECTED) return 1;
2139                 }
2140         }
2141         return 0;
2142 }
2143
2144 static int tree_element_active_bone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2145 {
2146         bArmature *arm= (bArmature *)tselem->id;
2147         Bone *bone= te->directdata;
2148         
2149         if(set) {
2150                 if(!(bone->flag & BONE_HIDDEN_P)) {
2151                         if(set==2) ED_pose_deselectall(OBACT, 2, 0);    // 2 is clear active tag
2152                         else ED_pose_deselectall(OBACT, 0, 0);
2153                         
2154                         if(set==2 && (bone->flag & BONE_SELECTED)) {
2155                                 bone->flag &= ~BONE_SELECTED;
2156                                 if(arm->act_bone==bone)
2157                                         arm->act_bone= NULL;
2158                         } else {
2159                                 bone->flag |= BONE_SELECTED;
2160                                 arm->act_bone= bone;
2161                         }
2162                         
2163                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, OBACT);
2164                 }
2165         }
2166         else {
2167                 Object *ob= OBACT;
2168                 
2169                 if(ob && ob->data==arm) {
2170                         if (bone->flag & BONE_SELECTED) return 1;
2171                 }
2172         }
2173         return 0;
2174 }
2175
2176
2177 /* ebones only draw in editmode armature */
2178 static int tree_element_active_ebone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2179 {
2180         EditBone *ebone= te->directdata;
2181         
2182         if(set) {
2183                 if(!(ebone->flag & BONE_HIDDEN_A)) {
2184                         bArmature *arm= scene->obedit->data;
2185                         if(set==2) ED_armature_deselectall(scene->obedit, 2, 0);        // only clear active tag
2186                         else ED_armature_deselectall(scene->obedit, 0, 0);      // deselect
2187
2188                         ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL;
2189                         arm->act_edbone= ebone;
2190
2191                         // flush to parent?
2192                         if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
2193                         
2194                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, scene->obedit);
2195                 }
2196         }
2197         else {
2198                 if (ebone->flag & BONE_SELECTED) return 1;
2199         }
2200         return 0;
2201 }
2202
2203 static int tree_element_active_modifier(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2204 {
2205         if(set) {
2206                 Object *ob= (Object *)tselem->id;
2207                 
2208                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
2209
2210 // XXX          extern_set_butspace(F9KEY, 0);
2211         }
2212         
2213         return 0;
2214 }
2215
2216 static int tree_element_active_psys(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2217 {
2218         if(set) {
2219                 Object *ob= (Object *)tselem->id;
2220                 
2221                 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE_DATA, ob);
2222                 
2223 // XXX          extern_set_butspace(F7KEY, 0);
2224         }
2225         
2226         return 0;
2227 }
2228
2229 static int tree_element_active_constraint(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2230 {
2231         if(set) {
2232                 Object *ob= (Object *)tselem->id;
2233                 
2234                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
2235 // XXX          extern_set_butspace(F7KEY, 0);
2236         }
2237         
2238         return 0;
2239 }
2240
2241 static int tree_element_active_text(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2242 {
2243         // XXX removed
2244         return 0;
2245 }
2246
2247 /* generic call for ID data check or make/check active in UI */
2248 static int tree_element_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2249 {
2250
2251         switch(te->idcode) {
2252                 case ID_MA:
2253                         return tree_element_active_material(C, scene, soops, te, set);
2254                 case ID_WO:
2255                         return tree_element_active_world(C, scene, soops, te, set);
2256                 case ID_LA:
2257                         return tree_element_active_lamp(C, scene, soops, te, set);
2258                 case ID_TE:
2259                         return tree_element_active_texture(C, scene, soops, te, set);
2260                 case ID_TXT:
2261                         return tree_element_active_text(C, scene, soops, te, set);
2262         }
2263         return 0;
2264 }
2265
2266 static int tree_element_active_pose(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2267 {
2268         Object *ob= (Object *)tselem->id;
2269         Base *base= object_in_scene(ob, scene);
2270         
2271         if(set) {
2272                 if(scene->obedit) 
2273                         ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
2274                 
2275                 if(ob->mode & OB_MODE_POSE) 
2276                         ED_armature_exit_posemode(C, base);
2277                 else 
2278                         ED_armature_enter_posemode(C, base);
2279         }
2280         else {
2281                 if(ob->mode & OB_MODE_POSE) return 1;
2282         }
2283         return 0;
2284 }
2285
2286 static int tree_element_active_sequence(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2287 {
2288         Sequence *seq= (Sequence*) te->directdata;
2289
2290         if(set) {
2291 // XXX          select_single_seq(seq, 1);
2292         }
2293         else {
2294                 if(seq->flag & SELECT)
2295                         return(1);
2296         }
2297         return(0);
2298 }
2299
2300 static int tree_element_active_sequence_dup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2301 {
2302         Sequence *seq, *p;
2303         Editing *ed= seq_give_editing(scene, FALSE);
2304
2305         seq= (Sequence*)te->directdata;
2306         if(set==0) {
2307                 if(seq->flag & SELECT)
2308                         return(1);
2309                 return(0);
2310         }
2311
2312 // XXX  select_single_seq(seq, 1);
2313         p= ed->seqbasep->first;
2314         while(p) {
2315                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
2316                         p= p->next;
2317                         continue;
2318                 }
2319
2320 //              if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
2321 // XXX                  select_single_seq(p, 0);
2322                 p= p->next;
2323         }
2324         return(0);
2325 }
2326
2327 static int tree_element_active_keymap_item(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2328 {
2329         wmKeyMapItem *kmi= te->directdata;
2330         
2331         if(set==0) {
2332                 if(kmi->flag & KMI_INACTIVE) return 0;
2333                 return 1;
2334         }
2335         else {
2336                 kmi->flag ^= KMI_INACTIVE;
2337         }
2338         return 0;
2339 }
2340
2341
2342 /* generic call for non-id data to make/check active in UI */
2343 /* Context can be NULL when set==0 */
2344 static int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
2345 {
2346         
2347         switch(tselem->type) {
2348                 case TSE_DEFGROUP:
2349                         return tree_element_active_defgroup(C, scene, te, tselem, set);
2350                 case TSE_BONE:
2351                         return tree_element_active_bone(C, scene, te, tselem, set);
2352                 case TSE_EBONE:
2353                         return tree_element_active_ebone(C, scene, te, tselem, set);
2354                 case TSE_MODIFIER:
2355                         return tree_element_active_modifier(C, te, tselem, set);
2356                 case TSE_LINKED_OB:
2357                         if(set) tree_element_set_active_object(C, scene, soops, te, set);
2358                         else if(tselem->id==(ID *)OBACT) return 1;
2359                         break;
2360                 case TSE_LINKED_PSYS:
2361                         return tree_element_active_psys(C, scene, te, tselem, set);
2362                 case TSE_POSE_BASE:
2363                         return tree_element_active_pose(C, scene, te, tselem, set);
2364                 case TSE_POSE_CHANNEL:
2365                         return tree_element_active_posechannel(C, scene, te, tselem, set);
2366                 case TSE_CONSTRAINT:
2367                         return tree_element_active_constraint(C, te, tselem, set);
2368                 case TSE_R_LAYER:
2369                         return tree_element_active_renderlayer(C, te, tselem, set);
2370                 case TSE_POSEGRP:
2371                         return tree_element_active_posegroup(C, scene, te, tselem, set);
2372                 case TSE_SEQUENCE:
2373                         return tree_element_active_sequence(C, te, tselem, set);
2374                 case TSE_SEQUENCE_DUP:
2375                         return tree_element_active_sequence_dup(C, scene, te, tselem, set);
2376                 case TSE_KEYMAP_ITEM:
2377                         return tree_element_active_keymap_item(C, te, tselem, set);
2378                         
2379         }
2380         return 0;
2381 }
2382
2383 static int do_outliner_item_activate(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, int extend, float *mval)
2384 {
2385         
2386         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2387                 TreeStoreElem *tselem= TREESTORE(te);
2388                 int openclose= 0;
2389
2390                 /* open close icon */
2391                 if((te->flag & TE_ICONROW)==0) {                                // hidden icon, no open/close
2392                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) 
2393                                 openclose= 1;
2394                 }
2395
2396                 if(openclose) {
2397                         /* all below close/open? */
2398                         if(extend) {
2399                                 tselem->flag &= ~TSE_CLOSED;
2400                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2401                         }
2402                         else {
2403                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2404                                 else tselem->flag |= TSE_CLOSED;
2405                                 
2406                         }
2407
2408                         return 1;
2409                 }
2410                 /* name and first icon */
2411                 else if(mval[0]>te->xs && mval[0]<te->xend) {
2412                         
2413                         /* always makes active object */
2414                         if(tselem->type!=TSE_SEQUENCE && tselem->type!=TSE_SEQ_STRIP && tselem->type!=TSE_SEQUENCE_DUP)
2415                                 tree_element_set_active_object(C, scene, soops, te, 1 + (extend!=0 && tselem->type==0));
2416                         
2417                         if(tselem->type==0) { // the lib blocks
2418                                 /* editmode? */
2419                                 if(te->idcode==ID_SCE) {
2420                                         if(scene!=(Scene *)tselem->id) {
2421                                                 ED_screen_set_scene(C, (Scene *)tselem->id);
2422                                         }
2423                                 }
2424                                 else if(ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
2425                                         Object *obedit= CTX_data_edit_object(C);
2426                                         if(obedit) 
2427                                                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
2428                                         else {
2429                                                 ED_object_enter_editmode(C, EM_WAITCURSOR);
2430                                                 // XXX extern_set_butspace(F9KEY, 0);
2431                                         }
2432                                 } else {        // rest of types
2433                                         tree_element_active(C, scene, soops, te, 1);
2434                                 }
2435                                 
2436                         }
2437                         else tree_element_type_active(C, scene, soops, te, tselem, 1+(extend!=0));
2438
2439                         return 1;
2440                 }
2441         }
2442         
2443         for(te= te->subtree.first; te; te= te->next) {
2444                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, mval)) return 1;
2445         }
2446         return 0;
2447 }
2448
2449 /* event can enterkey, then it opens/closes */
2450 static int outliner_item_activate(bContext *C, wmOperator *op, wmEvent *event)
2451 {
2452         Scene *scene= CTX_data_scene(C);
2453         ARegion *ar= CTX_wm_region(C);
2454         SpaceOops *soops= CTX_wm_space_outliner(C);
2455         TreeElement *te;
2456         float fmval[2];
2457         int extend= RNA_boolean_get(op->ptr, "extend");
2458         
2459         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2460         
2461         for(te= soops->tree.first; te; te= te->next) {
2462                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, fmval)) break;
2463         }
2464         
2465         if(te) {
2466                 ED_undo_push(C, "Outliner click event");
2467         }
2468         else {
2469                 short selecting= -1;
2470                 int row;
2471                 
2472                 /* get row number - 100 here is just a dummy value since we don't need the column */
2473                 UI_view2d_listview_view_to_cell(&ar->v2d, 1000, OL_H, 0.0f, 0.0f, 
2474                                                 fmval[0], fmval[1], NULL, &row);
2475                 
2476                 /* select relevant row */
2477                 outliner_select(soops, &soops->tree, &row, &selecting);
2478                 
2479                 soops->storeflag |= SO_TREESTORE_REDRAW;
2480                 
2481                 ED_undo_push(C, "Outliner selection event");
2482         }
2483         
2484         ED_region_tag_redraw(ar);
2485
2486         return OPERATOR_FINISHED;
2487 }
2488
2489 void OUTLINER_OT_item_activate(wmOperatorType *ot)
2490 {
2491         ot->name= "Activate Item";
2492         ot->idname= "OUTLINER_OT_item_activate";
2493         
2494         ot->invoke= outliner_item_activate;
2495         
2496         ot->poll= ED_operator_outliner_active;
2497         
2498         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection for activation.");
2499 }
2500
2501 /* *********** */
2502
2503 static int do_outliner_item_openclose(bContext *C, SpaceOops *soops, TreeElement *te, int all, float *mval)
2504 {
2505         
2506         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2507                 TreeStoreElem *tselem= TREESTORE(te);
2508
2509                 /* all below close/open? */
2510                 if(all) {
2511                         tselem->flag &= ~TSE_CLOSED;
2512                         outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2513                 }
2514                 else {
2515                         if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2516                         else tselem->flag |= TSE_CLOSED;
2517                 }
2518
2519                 return 1;
2520         }
2521         
2522         for(te= te->subtree.first; te; te= te->next) {
2523                 if(do_outliner_item_openclose(C, soops, te, all, mval)) 
2524                         return 1;
2525         }
2526         return 0;
2527         
2528 }
2529
2530 /* event can enterkey, then it opens/closes */
2531 static int outliner_item_openclose(bContext *C, wmOperator *op, wmEvent *event)
2532 {
2533         ARegion *ar= CTX_wm_region(C);
2534         SpaceOops *soops= CTX_wm_space_outliner(C);
2535         TreeElement *te;
2536         float fmval[2];
2537         int all= RNA_boolean_get(op->ptr, "all");
2538         
2539         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2540         
2541         for(te= soops->tree.first; te; te= te->next) {
2542                 if(do_outliner_item_openclose(C, soops, te, all, fmval)) 
2543                         break;
2544         }
2545
2546         ED_region_tag_redraw(ar);
2547         
2548         return OPERATOR_FINISHED;
2549 }
2550
2551 void OUTLINER_OT_item_openclose(wmOperatorType *ot)
2552 {
2553         ot->name= "Open/Close Item";
2554         ot->idname= "OUTLINER_OT_item_openclose";
2555         
2556         ot->invoke= outliner_item_openclose;
2557         
2558         ot->poll= ED_operator_outliner_active;
2559         
2560         RNA_def_boolean(ot->srna, "all", 1, "All", "Close or open all items.");
2561
2562 }
2563
2564
2565 /* ********************************************** */
2566
2567 static int do_outliner_item_rename(bContext *C, ARegion *ar, SpaceOops *soops, TreeElement *te, float *mval)
2568 {
2569         
2570         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2571                 TreeStoreElem *tselem= TREESTORE(te);
2572                 
2573                 /* name and first icon */
2574                 if(mval[0]>te->xs && mval[0]<te->xend) {
2575                         
2576                         /* can't rename rna datablocks entries */
2577                         if(ELEM3(tselem->type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM))
2578                            ;
2579                         else if(ELEM10(tselem->type, TSE_ANIM_DATA, TSE_NLA, TSE_DEFGROUP_BASE, TSE_CONSTRAINT_BASE, TSE_MODIFIER_BASE, TSE_SCRIPT_BASE, TSE_POSE_BASE, TSE_POSEGRP_BASE, TSE_R_LAYER_BASE, TSE_R_PASS)) 
2580                                         error("Cannot edit builtin name");
2581                         else if(ELEM3(tselem->type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP))
2582                                 error("Cannot edit sequence name");
2583                         else if(tselem->id->lib) {
2584                                 // XXX                                          error_libdata();
2585                         } else if(te->idcode == ID_LI && te->parent) {
2586                                 error("Cannot edit the path of an indirectly linked library");
2587                         } else {
2588                                 tselem->flag |= TSE_TEXTBUT;
2589                                 ED_region_tag_redraw(ar);
2590                         }
2591                 }
2592                 return 1;
2593         }
2594         
2595         for(te= te->subtree.first; te; te= te->next) {
2596                 if(do_outliner_item_rename(C, ar, soops, te, mval)) return 1;
2597         }
2598         return 0;
2599 }
2600
2601 static int outliner_item_rename(bContext *C, wmOperator *op, wmEvent *event)
2602 {
2603         ARegion *ar= CTX_wm_region(C);
2604         SpaceOops *soops= CTX_wm_space_outliner(C);
2605         TreeElement *te;
2606         float fmval[2];
2607         
2608         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2609         
2610         for(te= soops->tree.first; te; te= te->next) {
2611                 if(do_outliner_item_rename(C, ar, soops, te, fmval)) break;
2612         }
2613         
2614         return OPERATOR_FINISHED;
2615 }
2616
2617
2618 void OUTLINER_OT_item_rename(wmOperatorType *ot)
2619 {
2620         ot->name= "Rename Item";
2621         ot->idname= "OUTLINER_OT_item_rename";
2622         
2623         ot->invoke= outliner_item_rename;
2624         
2625         ot->poll= ED_operator_outliner_active;
2626 }
2627
2628
2629
2630 /* recursive helper for function below */
2631 static void outliner_set_coordinates_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
2632 {
2633         TreeStoreElem *tselem= TREESTORE(te);
2634         
2635         /* store coord and continue, we need coordinates for elements outside view too */
2636         te->xs= (float)startx;
2637         te->ys= (float)(*starty);
2638         *starty-= OL_H;
2639         
2640         if((tselem->flag & TSE_CLOSED)==0) {
2641                 TreeElement *ten;
2642                 for(ten= te->subtree.first; ten; ten= ten->next) {
2643                         outliner_set_coordinates_element(soops, ten, startx+OL_X, starty);
2644                 }
2645         }
2646         
2647 }
2648
2649 /* to retrieve coordinates with redrawing the entire tree */
2650 static void outliner_set_coordinates(ARegion *ar, SpaceOops *soops)
2651 {
2652         TreeElement *te;
2653         int starty= (int)(ar->v2d.tot.ymax)-OL_H;
2654         int startx= 0;
2655         
2656         for(te= soops->tree.first; te; te= te->next) {
2657                 outliner_set_coordinates_element(soops, te, startx, &starty);
2658         }
2659 }
2660
2661 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
2662 {
2663         TreeElement *te, *tes;
2664         TreeStoreElem *tselem;
2665         
2666         for(te= lb->first; te; te= te->next) {
2667                 tselem= TREESTORE(te);
2668                 if(tselem->type==0) {
2669                         if(tselem->id==id) return te;
2670                         /* only deeper on scene or object */
2671                         if( te->idcode==ID_OB || te->idcode==ID_SCE) { 
2672                                 tes= outliner_find_id(soops, &te->subtree, id);
2673                                 if(tes) return tes;
2674                         }
2675                 }
2676         }
2677         return NULL;
2678 }
2679
2680 static int outliner_show_active_exec(bContext *C, wmOperator *op)
2681 {
2682         SpaceOops *so= CTX_wm_space_outliner(C);
2683         Scene *scene= CTX_data_scene(C);
2684         ARegion *ar= CTX_wm_region(C);
2685         View2D *v2d= &ar->v2d;
2686         
2687         TreeElement *te;
2688         int xdelta, ytop;
2689         
2690         // TODO: make this get this info from context instead...
2691         if (OBACT == NULL) 
2692                 return OPERATOR_CANCELLED;
2693         
2694         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
2695         if (te) {
2696                 /* make te->ys center of view */
2697                 ytop= (int)(te->ys + (v2d->mask.ymax - v2d->mask.ymin)/2);
2698                 if (ytop>0) ytop= 0;
2699                 
2700                 v2d->cur.ymax= (float)ytop;
2701                 v2d->cur.ymin= (float)(ytop-(v2d->mask.ymax - v2d->mask.ymin));
2702                 
2703                 /* make te->xs ==> te->xend center of view */
2704                 xdelta = (int)(te->xs - v2d->cur.xmin);
2705                 v2d->cur.xmin += xdelta;
2706                 v2d->cur.xmax += xdelta;
2707                 
2708                 so->storeflag |= SO_TREESTORE_REDRAW;
2709         }
2710         
2711         ED_region_tag_redraw(ar);
2712         
2713         return OPERATOR_FINISHED;
2714 }
2715
2716 void OUTLINER_OT_show_active(wmOperatorType *ot)
2717 {
2718         /* identifiers */
2719         ot->name= "Show Active";
2720         ot->idname= "OUTLINER_OT_show_active";
2721         ot->description= "Adjust the view so that the active Object is shown centered";
2722         
2723         /* callbacks */
2724         ot->exec= outliner_show_active_exec;
2725         ot->poll= ED_operator_outliner_active;
2726         
2727         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2728 }
2729
2730 /* find next element that has this name */
2731 static TreeElement *outliner_find_named(SpaceOops *soops, ListBase *lb, char *name, int flags, TreeElement *prev, int *prevFound)
2732 {
2733         TreeElement *te, *tes;
2734         
2735         for (te= lb->first; te; te= te->next) {
2736                 int found = outliner_filter_has_name(te, name, flags);
2737                 
2738                 if(found) {
2739                         /* name is right, but is element the previous one? */
2740                         if (prev) {
2741                                 if ((te != prev) && (*prevFound)) 
2742                                         return te;
2743                                 if (te == prev) {
2744                                         *prevFound = 1;
2745                                 }
2746                         }
2747                         else
2748                                 return te;
2749                 }
2750                 
2751                 tes= outliner_find_named(soops, &te->subtree, name, flags, prev, prevFound);
2752                 if(tes) return tes;
2753         }
2754
2755         /* nothing valid found */
2756         return NULL;
2757 }
2758
2759 /* tse is not in the treestore, we use its contents to find a match */
2760 static TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
2761 {
2762         TreeStore *ts= soops->treestore;
2763         TreeStoreElem *tselem;
2764         int a;
2765         
2766         if(tse->id==NULL) return NULL;
2767         
2768         /* check if 'tse' is in treestore */
2769         tselem= ts->data;
2770         for(a=0; a<ts->usedelem; a++, tselem++) {
2771                 if((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
2772                         if(tselem->id==tse->id) {
2773                                 break;
2774                         }
2775                 }
2776         }
2777         if(tselem) 
2778                 return outliner_find_tree_element(&soops->tree, a);
2779         
2780         return NULL;
2781 }
2782
2783
2784 /* Called to find an item based on name.
2785  */
2786 void outliner_find_panel(Scene *scene, ARegion *ar, SpaceOops *soops, int again, int flags) 
2787 {
2788         TreeElement *te= NULL;
2789         TreeElement *last_find;
2790         TreeStoreElem *tselem;
2791         int ytop, xdelta, prevFound=0;
2792         char name[32];
2793         
2794         /* get last found tree-element based on stored search_tse */
2795         last_find= outliner_find_tse(soops, &soops->search_tse);
2796         
2797         /* determine which type of search to do */
2798         if (again && last_find) {
2799                 /* no popup panel - previous + user wanted to search for next after previous */         
2800                 BLI_strncpy(name, soops->search_string, sizeof(name));
2801                 flags= soops->search_flags;
2802                 
2803                 /* try to find matching element */
2804                 te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2805                 if (te==NULL) {
2806                         /* no more matches after previous, start from beginning again */
2807                         prevFound= 1;
2808                         te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2809                 }
2810         }
2811         else {
2812                 /* pop up panel - no previous, or user didn't want search after previous */
2813                 strcpy(name, "");
2814 // XXX          if (sbutton(name, 0, sizeof(name)-1, "Find: ") && name[0]) {
2815 //                      te= outliner_find_named(soops, &soops->tree, name, flags, NULL, &prevFound);
2816 //              }
2817 //              else return; /* XXX RETURN! XXX */
2818         }
2819
2820         /* do selection and reveal */
2821         if (te) {
2822                 tselem= TREESTORE(te);
2823                 if (tselem) {
2824                         /* expand branches so that it will be visible, we need to get correct coordinates */
2825                         if( outliner_open_back(soops, te))
2826                                 outliner_set_coordinates(ar, soops);
2827                         
2828                         /* deselect all visible, and select found element */
2829                         outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
2830                         tselem->flag |= TSE_SELECTED;
2831                         
2832                         /* make te->ys center of view */
2833                         ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2834                         if(ytop>0) ytop= 0;
2835                         ar->v2d.cur.ymax= (float)ytop;
2836                         ar->v2d.cur.ymin= (float)(ytop-(ar->v2d.mask.ymax-ar->v2d.mask.ymin));
2837                         
2838                         /* make te->xs ==> te->xend center of view */
2839                         xdelta = (int)(te->xs - ar->v2d.cur.xmin);
2840                         ar->v2d.cur.xmin += xdelta;
2841                         ar->v2d.cur.xmax += xdelta;
2842                         
2843                         /* store selection */
2844                         soops->search_tse= *tselem;
2845                         
2846                         BLI_strncpy(soops->search_string, name, 33);
2847                         soops->search_flags= flags;
2848                         
2849                         /* redraw */
2850                         soops->storeflag |= SO_TREESTORE_REDRAW;
2851                 }
2852         }
2853         else {
2854                 /* no tree-element found */
2855                 error("Not found: %s", name);
2856         }
2857 }
2858
2859 /* helper function for tree_element_shwo_hierarchy() - recursively checks whether subtrees have any objects*/
2860 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
2861 {
2862         TreeElement *te;
2863         TreeStoreElem *tselem;
2864         
2865         for(te= lb->first; te; te= te->next) {
2866                 tselem= TREESTORE(te);
2867                 if(tselem->type==0 && te->idcode==ID_OB) return 1;
2868                 if( subtree_has_objects(soops, &te->subtree)) return 1;
2869         }
2870         return 0;
2871 }
2872
2873 /* recursive helper function for Show Hierarchy operator */
2874 static void tree_element_show_hierarchy(Scene *scene, SpaceOops *soops, ListBase *lb)
2875 {
2876         TreeElement *te;
2877         TreeStoreElem *tselem;
2878
2879         /* open all object elems, close others */
2880         for(te= lb->first; te; te= te->next) {
2881                 tselem= TREESTORE(te);
2882                 
2883                 if(tselem->type==0) {
2884                         if(te->idcode==ID_SCE) {
2885                                 if(tselem->id!=(ID *)scene) tselem->flag |= TSE_CLOSED;
2886                                         else tselem->flag &= ~TSE_CLOSED;
2887                         }
2888                         else if(te->idcode==ID_OB) {
2889                                 if(subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
2890                                 else tselem->flag |= TSE_CLOSED;
2891                         }
2892                 }
2893                 else tselem->flag |= TSE_CLOSED;
2894                 
2895                 if(tselem->flag & TSE_CLOSED); else tree_element_show_hierarchy(scene, soops, &te->subtree);
2896         }
2897 }
2898
2899 /* show entire object level hierarchy */
2900 static int outliner_show_hierarchy_exec(bContext *C, wmOperator *op)
2901 {
2902         SpaceOops *soops= CTX_wm_space_outliner(C);
2903         ARegion *ar= CTX_wm_region(C);
2904         Scene *scene= CTX_data_scene(C);
2905         
2906         /* recursively open/close levels */
2907         tree_element_show_hierarchy(scene, soops, &soops->tree);
2908         
2909         ED_region_tag_redraw(ar);
2910         
2911         return OPERATOR_FINISHED;
2912 }
2913
2914 void OUTLINER_OT_show_hierarchy(wmOperatorType *ot)
2915 {
2916         /* identifiers */
2917         ot->name= "Show Hierarchy";
2918         ot->idname= "OUTLINER_OT_show_hierarchy";
2919         ot->description= "Open all object entries and close all others";
2920         
2921         /* callbacks */
2922         ot->exec= outliner_show_hierarchy_exec;
2923         ot->poll= ED_operator_outliner_active; //  TODO: shouldn't be allowed in RNA views...
2924         
2925         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2926 }
2927
2928 void outliner_select(SpaceOops *soops, ListBase *lb, int *index, short *selecting)
2929 {
2930         TreeElement *te;
2931         TreeStoreElem *tselem;
2932         
2933         for (te= lb->first; te && *index >= 0; te=te->next, (*index)--) {
2934                 tselem= TREESTORE(te);
2935                 
2936                 /* if we've encountered the right item, set its 'Outliner' selection status */
2937                 if (*index == 0) {
2938                         /* this should be the last one, so no need to do anything with index */
2939                         if ((te->flag & TE_ICONROW)==0) {
2940                                 /* -1 value means toggle testing for now... */
2941                                 if (*selecting == -1) {
2942                                         if (tselem->flag & TSE_SELECTED) 
2943                                                 *selecting= 0;
2944                                         else 
2945                                                 *selecting= 1;
2946                                 }
2947                                 
2948                                 /* set selection */
2949                                 if (*selecting) 
2950                                         tselem->flag |= TSE_SELECTED;
2951                                 else 
2952                                         tselem->flag &= ~TSE_SELECTED;
2953                         }
2954                 }
2955        &nb