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