- Added search filter in outliner header. Only activates filter on enter,
[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 int outliner_filter_has_name(TreeElement *te, char *name, int flags)
1239 {
1240         int found= 0;
1241         
1242         /* determine if match */
1243         if(flags==OL_FIND)
1244                 found= BLI_strcasestr(te->name, name)!=NULL;
1245         else if(flags==OL_FIND_CASE)
1246                 found= strstr(te->name, name)!=NULL;
1247         else if(flags==OL_FIND_COMPLETE)
1248                 found= BLI_strcasecmp(te->name, name)==0;
1249         else
1250                 found= strcmp(te->name, name)==0;
1251         
1252         return found;
1253 }
1254
1255 static void outliner_filter_tree(SpaceOops *soops, ListBase *lb)
1256 {
1257         TreeElement *te, *ten;
1258         
1259         if(soops->search_string[0]==0) return;
1260
1261         for (te= lb->first; te; te= ten) {
1262                 ten= te->next;
1263                 
1264                 if(0==outliner_filter_has_name(te, soops->search_string, OL_FIND)) {
1265                         
1266                         outliner_free_tree(&te->subtree);
1267                         BLI_remlink(lb, te);
1268                         
1269                         if(te->flag & TE_FREE_NAME) MEM_freeN(te->name);
1270                         MEM_freeN(te);
1271                 }
1272                 else
1273                         outliner_filter_tree(soops, &te->subtree);
1274         }
1275 }
1276
1277
1278 static void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops)
1279 {
1280         Base *base;
1281         Object *ob;
1282         TreeElement *te=NULL, *ten;
1283         TreeStoreElem *tselem;
1284         int show_opened= (soops->treestore==NULL); /* on first view, we open scenes */
1285
1286         if(soops->tree.first && (soops->storeflag & SO_TREESTORE_REDRAW))
1287            return;
1288            
1289         outliner_free_tree(&soops->tree);
1290         outliner_storage_cleanup(soops);
1291         
1292         /* clear ob id.new flags */
1293         for(ob= mainvar->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
1294         
1295         /* options */
1296         if(soops->outlinevis == SO_LIBRARIES) {
1297                 Library *lib;
1298                 
1299                 for(lib= mainvar->library.first; lib; lib= lib->id.next) {
1300                         ten= outliner_add_element(soops, &soops->tree, lib, NULL, 0, 0);
1301                         lib->id.newid= (ID *)ten;
1302                 }
1303                 /* make hierarchy */
1304                 ten= soops->tree.first;
1305                 while(ten) {
1306                         TreeElement *nten= ten->next, *par;
1307                         tselem= TREESTORE(ten);
1308                         lib= (Library *)tselem->id;
1309                         if(lib->parent) {
1310                                 BLI_remlink(&soops->tree, ten);
1311                                 par= (TreeElement *)lib->parent->id.newid;
1312                                 BLI_addtail(&par->subtree, ten);
1313                                 ten->parent= par;
1314                         }
1315                         ten= nten;
1316                 }
1317                 /* restore newid pointers */
1318                 for(lib= mainvar->library.first; lib; lib= lib->id.next)
1319                         lib->id.newid= NULL;
1320                 
1321         }
1322         else if(soops->outlinevis == SO_ALL_SCENES) {
1323                 Scene *sce;
1324                 for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
1325                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
1326                         tselem= TREESTORE(te);
1327                         if(sce==scene && show_opened) 
1328                                 tselem->flag &= ~TSE_CLOSED;
1329                         
1330                         for(base= sce->base.first; base; base= base->next) {
1331                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
1332                                 ten->directdata= base;
1333                         }
1334                         outliner_make_hierarchy(soops, &te->subtree);
1335                         /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1336                         for(base= sce->base.first; base; base= base->next) base->object->id.newid= NULL;
1337                 }
1338         }
1339         else if(soops->outlinevis == SO_CUR_SCENE) {
1340                 
1341                 outliner_add_scene_contents(soops, &soops->tree, scene, NULL);
1342                 
1343                 for(base= scene->base.first; base; base= base->next) {
1344                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1345                         ten->directdata= base;
1346                 }
1347                 outliner_make_hierarchy(soops, &soops->tree);
1348         }
1349         else if(soops->outlinevis == SO_VISIBLE) {
1350                 for(base= scene->base.first; base; base= base->next) {
1351                         if(base->lay & scene->lay)
1352                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1353                 }
1354                 outliner_make_hierarchy(soops, &soops->tree);
1355         }
1356         else if(soops->outlinevis == SO_GROUPS) {
1357                 Group *group;
1358                 GroupObject *go;
1359                 
1360                 for(group= mainvar->group.first; group; group= group->id.next) {
1361                         if(group->id.us) {
1362                                 te= outliner_add_element(soops, &soops->tree, group, NULL, 0, 0);
1363                                 tselem= TREESTORE(te);
1364                                 
1365                                 for(go= group->gobject.first; go; go= go->next) {
1366                                         ten= outliner_add_element(soops, &te->subtree, go->ob, te, 0, 0);
1367                                         ten->directdata= NULL; /* eh, why? */
1368                                 }
1369                                 outliner_make_hierarchy(soops, &te->subtree);
1370                                 /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1371                                 for(go= group->gobject.first; go; go= go->next) go->ob->id.newid= NULL;
1372                         }
1373                 }
1374         }
1375         else if(soops->outlinevis == SO_SAME_TYPE) {
1376                 Object *ob= OBACT;
1377                 if(ob) {
1378                         for(base= scene->base.first; base; base= base->next) {
1379                                 if(base->object->type==ob->type) {
1380                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1381                                         ten->directdata= base;
1382                                 }
1383                         }
1384                         outliner_make_hierarchy(soops, &soops->tree);
1385                 }
1386         }
1387         else if(soops->outlinevis == SO_SELECTED) {
1388                 for(base= scene->base.first; base; base= base->next) {
1389                         if(base->lay & scene->lay) {
1390                                 if(base==BASACT || (base->flag & SELECT)) {
1391                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1392                                         ten->directdata= base;
1393                                 }
1394                         }
1395                 }
1396                 outliner_make_hierarchy(soops, &soops->tree);
1397         }
1398         else if(soops->outlinevis==SO_SEQUENCE) {
1399                 Sequence *seq;
1400                 Editing *ed= seq_give_editing(scene, FALSE);
1401                 int op;
1402
1403                 if(ed==NULL)
1404                         return;
1405
1406                 seq= ed->seqbasep->first;
1407                 if(!seq)
1408                         return;
1409
1410                 while(seq) {
1411                         op= need_add_seq_dup(seq);
1412                         if(op==1)
1413                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1414                         else if(op==0) {
1415                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1416                                 add_seq_dup(soops, seq, ten, 0);
1417                         }
1418                         seq= seq->next;
1419                 }
1420         }
1421         else if(soops->outlinevis==SO_DATABLOCKS) {
1422                 PointerRNA mainptr;
1423
1424                 RNA_main_pointer_create(mainvar, &mainptr);
1425
1426                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1427
1428                 if(show_opened)  {
1429                         tselem= TREESTORE(ten);
1430                         tselem->flag &= ~TSE_CLOSED;
1431                 }
1432         }
1433         else if(soops->outlinevis==SO_USERDEF) {
1434                 PointerRNA userdefptr;
1435
1436                 RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &userdefptr);
1437
1438                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1439
1440                 if(show_opened)  {
1441                         tselem= TREESTORE(ten);
1442                         tselem->flag &= ~TSE_CLOSED;
1443                 }
1444         }
1445         else if(soops->outlinevis==SO_KEYMAP) {
1446                 wmWindowManager *wm= mainvar->wm.first;
1447                 wmKeyMap *km;
1448                 
1449                 for(km= wm->defaultconf->keymaps.first; km; km= km->next) {
1450                         ten= outliner_add_element(soops, &soops->tree, (void*)km, NULL, TSE_KEYMAP, 0);
1451                 }
1452         }
1453         else {
1454                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1455                 if(ten) ten->directdata= BASACT;
1456         }
1457
1458         outliner_sort(soops, &soops->tree);
1459         outliner_filter_tree(soops, &soops->tree);
1460 }
1461
1462 /* **************** INTERACTIVE ************* */
1463
1464 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
1465 {
1466         TreeElement *te;
1467         int level=curlevel, lev;
1468         
1469         for(te= lb->first; te; te= te->next) {
1470                 
1471                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
1472                 if(lev>level) level= lev;
1473         }
1474         return level;
1475 }
1476
1477 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
1478 {
1479         TreeElement *te;
1480         TreeStoreElem *tselem;
1481         int level;
1482         
1483         for(te= lb->first; te; te= te->next) {
1484                 tselem= TREESTORE(te);
1485                 if(tselem->flag & flag) return curlevel;
1486                 
1487                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
1488                 if(level) return level;
1489         }
1490         return 0;
1491 }
1492
1493 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
1494 {
1495         TreeElement *te;
1496         TreeStoreElem *tselem;
1497         
1498         for(te= lb->first; te; te= te->next) {
1499                 tselem= TREESTORE(te);
1500                 if(set==0) tselem->flag &= ~flag;
1501                 else tselem->flag |= flag;
1502                 outliner_set_flag(soops, &te->subtree, flag, set);
1503         }
1504 }
1505
1506 /* --- */
1507
1508 void object_toggle_visibility_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1509 {
1510         Base *base= (Base *)te->directdata;
1511         
1512         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1513         if(base) {
1514                 base->object->restrictflag^=OB_RESTRICT_VIEW;
1515         }
1516 }
1517
1518 static int outliner_toggle_visibility_exec(bContext *C, wmOperator *op)
1519 {
1520         SpaceOops *soops= CTX_wm_space_outliner(C);
1521         Scene *scene= CTX_data_scene(C);
1522         ARegion *ar= CTX_wm_region(C);
1523         
1524         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_visibility_cb);
1525         
1526         ED_region_tag_redraw(ar);
1527         
1528         return OPERATOR_FINISHED;
1529 }
1530
1531 void OUTLINER_OT_visibility_toggle(wmOperatorType *ot)
1532 {
1533         /* identifiers */
1534         ot->name= "Toggle Visability";
1535         ot->idname= "OUTLINER_OT_visibility_toggle";
1536         ot->description= "Toggle the visibility of selected items";
1537         
1538         /* callbacks */
1539         ot->exec= outliner_toggle_visibility_exec;
1540         ot->poll= ED_operator_outliner_active;
1541         
1542         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1543 }
1544
1545 /* --- */
1546
1547 static void object_toggle_selectability_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1548 {
1549         Base *base= (Base *)te->directdata;
1550         
1551         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1552         if(base) {
1553                 base->object->restrictflag^=OB_RESTRICT_SELECT;
1554         }
1555 }
1556
1557 static int outliner_toggle_selectability_exec(bContext *C, wmOperator *op)
1558 {
1559         SpaceOops *soops= CTX_wm_space_outliner(C);
1560         Scene *scene= CTX_data_scene(C);
1561         ARegion *ar= CTX_wm_region(C);
1562         
1563         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_selectability_cb);
1564         
1565         ED_region_tag_redraw(ar);
1566         
1567         return OPERATOR_FINISHED;
1568 }
1569
1570 void OUTLINER_OT_selectability_toggle(wmOperatorType *ot)
1571 {
1572         /* identifiers */
1573         ot->name= "Toggle Selectability";
1574         ot->idname= "OUTLINER_OT_selectability_toggle";
1575         ot->description= "Toggle the selectability";
1576         
1577         /* callbacks */
1578         ot->exec= outliner_toggle_selectability_exec;
1579         ot->poll= ED_operator_outliner_active;
1580         
1581         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1582 }
1583
1584 /* --- */
1585
1586 void object_toggle_renderability_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1587 {
1588         Base *base= (Base *)te->directdata;
1589         
1590         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1591         if(base) {
1592                 base->object->restrictflag^=OB_RESTRICT_RENDER;
1593         }
1594 }
1595
1596 static int outliner_toggle_renderability_exec(bContext *C, wmOperator *op)
1597 {
1598         SpaceOops *soops= CTX_wm_space_outliner(C);
1599         Scene *scene= CTX_data_scene(C);
1600         ARegion *ar= CTX_wm_region(C);
1601         
1602         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_renderability_cb);
1603         
1604         ED_region_tag_redraw(ar);
1605         
1606         return OPERATOR_FINISHED;
1607 }
1608
1609 void OUTLINER_OT_renderability_toggle(wmOperatorType *ot)
1610 {
1611         /* identifiers */
1612         ot->name= "Toggle Renderability";
1613         ot->idname= "OUTLINER_OT_renderability_toggle";
1614         ot->description= "Toggle the renderbility of selected items";
1615         
1616         /* callbacks */
1617         ot->exec= outliner_toggle_renderability_exec;
1618         ot->poll= ED_operator_outliner_active;
1619         
1620         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1621 }
1622
1623 /* --- */
1624
1625 static int outliner_toggle_expanded_exec(bContext *C, wmOperator *op)
1626 {
1627         SpaceOops *soops= CTX_wm_space_outliner(C);
1628         ARegion *ar= CTX_wm_region(C);
1629         
1630         if (outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
1631                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
1632         else 
1633                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
1634         
1635         ED_region_tag_redraw(ar);
1636         
1637         return OPERATOR_FINISHED;
1638 }
1639
1640 void OUTLINER_OT_expanded_toggle(wmOperatorType *ot)
1641 {
1642         /* identifiers */
1643         ot->name= "Expand/Collapse All";
1644         ot->idname= "OUTLINER_OT_expanded_toggle";
1645         ot->description= "Expand/Collapse all items";
1646         
1647         /* callbacks */
1648         ot->exec= outliner_toggle_expanded_exec;
1649         ot->poll= ED_operator_outliner_active;
1650         
1651         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1652 }
1653
1654 /* --- */
1655
1656 static int outliner_toggle_selected_exec(bContext *C, wmOperator *op)
1657 {
1658         SpaceOops *soops= CTX_wm_space_outliner(C);
1659         ARegion *ar= CTX_wm_region(C);
1660         
1661         if (outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
1662                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
1663         else 
1664                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
1665         
1666         soops->storeflag |= SO_TREESTORE_REDRAW;
1667         
1668         ED_region_tag_redraw(ar);
1669         
1670         return OPERATOR_FINISHED;
1671 }
1672
1673 void OUTLINER_OT_selected_toggle(wmOperatorType *ot)
1674 {
1675         /* identifiers */
1676         ot->name= "Toggle Selected";
1677         ot->idname= "OUTLINER_OT_selected_toggle";
1678         ot->description= "Toggle the Outliner selection of items";
1679         
1680         /* callbacks */
1681         ot->exec= outliner_toggle_selected_exec;
1682         ot->poll= ED_operator_outliner_active;
1683         
1684         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1685 }
1686
1687 /* --- */
1688
1689 /* helper function for Show/Hide one level operator */
1690 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
1691 {
1692         TreeElement *te;
1693         TreeStoreElem *tselem;
1694         
1695         for(te= lb->first; te; te= te->next) {
1696                 tselem= TREESTORE(te);
1697                 
1698                 if(open) {
1699                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
1700                 }
1701                 else {
1702                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
1703                 }
1704                 
1705                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
1706         }
1707 }
1708
1709 static int outliner_one_level_exec(bContext *C, wmOperator *op)
1710 {
1711         SpaceOops *soops= CTX_wm_space_outliner(C);
1712         ARegion *ar= CTX_wm_region(C);
1713         int add= RNA_boolean_get(op->ptr, "open");
1714         int level;
1715         
1716         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
1717         if(add==1) {
1718                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
1719         }
1720         else {
1721                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
1722                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
1723         }
1724         
1725         ED_region_tag_redraw(ar);
1726         
1727         return OPERATOR_FINISHED;
1728 }
1729
1730 void OUTLINER_OT_show_one_level(wmOperatorType *ot)
1731 {
1732         /* identifiers */
1733         ot->name= "Show/Hide One Level";
1734         ot->idname= "OUTLINER_OT_show_one_level";
1735         
1736         /* callbacks */
1737         ot->exec= outliner_one_level_exec;
1738         ot->poll= ED_operator_outliner_active;
1739         
1740         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1741         
1742         /* properties */
1743         RNA_def_boolean(ot->srna, "open", 1, "Open", "Expand all entries one level deep.");
1744 }
1745
1746 /* return 1 when levels were opened */
1747 static int outliner_open_back(SpaceOops *soops, TreeElement *te)
1748 {
1749         TreeStoreElem *tselem;
1750         int retval= 0;
1751         
1752         for (te= te->parent; te; te= te->parent) {
1753                 tselem= TREESTORE(te);
1754                 if (tselem->flag & TSE_CLOSED) { 
1755                         tselem->flag &= ~TSE_CLOSED;
1756                         retval= 1;
1757                 }
1758         }
1759         return retval;
1760 }
1761
1762 /* This is not used anywhere at the moment */
1763 #if 0
1764 static void outliner_open_reveal(SpaceOops *soops, ListBase *lb, TreeElement *teFind, int *found)
1765 {
1766         TreeElement *te;
1767         TreeStoreElem *tselem;
1768         
1769         for (te= lb->first; te; te= te->next) {
1770                 /* check if this tree-element was the one we're seeking */
1771                 if (te == teFind) {
1772                         *found= 1;
1773                         return;
1774                 }
1775                 
1776                 /* try to see if sub-tree contains it then */
1777                 outliner_open_reveal(soops, &te->subtree, teFind, found);
1778                 if (*found) {
1779                         tselem= TREESTORE(te);
1780                         if (tselem->flag & TSE_CLOSED) 
1781                                 tselem->flag &= ~TSE_CLOSED;
1782                         return;
1783                 }
1784         }
1785 }
1786 #endif
1787
1788 // XXX just use View2D ops for this?
1789 void outliner_page_up_down(Scene *scene, ARegion *ar, SpaceOops *soops, int up)
1790 {
1791         int dy= ar->v2d.mask.ymax-ar->v2d.mask.ymin;
1792         
1793         if(up == -1) dy= -dy;
1794         ar->v2d.cur.ymin+= dy;
1795         ar->v2d.cur.ymax+= dy;
1796         
1797         soops->storeflag |= SO_TREESTORE_REDRAW;
1798 }
1799
1800 /* **** do clicks on items ******* */
1801
1802 static int tree_element_active_renderlayer(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
1803 {
1804         Scene *sce;
1805         
1806         /* paranoia check */
1807         if(te->idcode!=ID_SCE)
1808                 return 0;
1809         sce= (Scene *)tselem->id;
1810         
1811         if(set) {
1812                 sce->r.actlay= tselem->nr;
1813                 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, sce);
1814         }
1815         else {
1816                 return sce->r.actlay==tselem->nr;
1817         }
1818         return 0;
1819 }
1820
1821 static void tree_element_set_active_object(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1822 {
1823         TreeStoreElem *tselem= TREESTORE(te);
1824         Scene *sce;
1825         Base *base;
1826         Object *ob= NULL;
1827         
1828         /* if id is not object, we search back */
1829         if(te->idcode==ID_OB) ob= (Object *)tselem->id;
1830         else {
1831                 ob= (Object *)outliner_search_back(soops, te, ID_OB);
1832                 if(ob==OBACT) return;
1833         }
1834         if(ob==NULL) return;
1835         
1836         sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
1837         if(sce && scene != sce) {
1838                 ED_screen_set_scene(C, sce);
1839         }
1840         
1841         /* find associated base in current scene */
1842         for(base= FIRSTBASE; base; base= base->next) 
1843                 if(base->object==ob) break;
1844         if(base) {
1845                 if(set==2) {
1846                         /* swap select */
1847                         if(base->flag & SELECT)
1848                                 ED_base_object_select(base, BA_DESELECT);
1849                         else 
1850                                 ED_base_object_select(base, BA_SELECT);
1851                 }
1852                 else {
1853                         Base *b;
1854                         /* deleselect all */
1855                         for(b= FIRSTBASE; b; b= b->next) {
1856                                 b->flag &= ~SELECT;
1857                                 b->object->flag= b->flag;
1858                         }
1859                         ED_base_object_select(base, BA_SELECT);
1860                 }
1861                 if(C)
1862                         ED_base_object_activate(C, base); /* adds notifier */
1863         }
1864         
1865         if(ob!=scene->obedit) 
1866                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
1867         
1868         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
1869
1870 }
1871
1872 static int tree_element_active_material(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1873 {
1874         TreeElement *tes;
1875         Object *ob;
1876         
1877         /* we search for the object parent */
1878         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1879         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1880         
1881         /* searching in ob mat array? */
1882         tes= te->parent;
1883         if(tes->idcode==ID_OB) {
1884                 if(set) {
1885                         ob->actcol= te->index+1;
1886                         ob->matbits[te->index]= 1;      // make ob material active too
1887                         ob->colbits |= (1<<te->index);
1888                 }
1889                 else {
1890                         if(ob->actcol == te->index+1) 
1891                                 if(ob->matbits[te->index]) return 1;
1892                 }
1893         }
1894         /* or we search for obdata material */
1895         else {
1896                 if(set) {
1897                         ob->actcol= te->index+1;
1898                         ob->matbits[te->index]= 0;      // make obdata material active too
1899                         ob->colbits &= ~(1<<te->index);
1900                 }
1901                 else {
1902                         if(ob->actcol == te->index+1)
1903                                 if(ob->matbits[te->index]==0) return 1;
1904                 }
1905         }
1906         if(set) {
1907                 WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING, NULL);
1908         }
1909         return 0;
1910 }
1911
1912 static int tree_element_active_texture(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1913 {
1914         TreeElement *tep;
1915         TreeStoreElem *tselem, *tselemp;
1916         Object *ob=OBACT;
1917         SpaceButs *sbuts=NULL;
1918         
1919         if(ob==NULL) return 0; // no active object
1920         
1921         tselem= TREESTORE(te);
1922         
1923         /* find buttons area (note, this is undefined really still, needs recode in blender) */
1924         /* XXX removed finding sbuts */
1925         
1926         /* where is texture linked to? */
1927         tep= te->parent;
1928         tselemp= TREESTORE(tep);
1929         
1930         if(tep->idcode==ID_WO) {
1931                 World *wrld= (World *)tselemp->id;
1932
1933                 if(set) {
1934                         if(sbuts) {
1935                                 // XXX sbuts->tabo= TAB_SHADING_TEX;    // hack from header_buttonswin.c
1936                                 // XXX sbuts->texfrom= 1;
1937                         }
1938 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1939                         wrld->texact= te->index;
1940                 }
1941                 else if(tselemp->id == (ID *)(scene->world)) {
1942                         if(wrld->texact==te->index) return 1;
1943                 }
1944         }
1945         else if(tep->idcode==ID_LA) {
1946                 Lamp *la= (Lamp *)tselemp->id;
1947                 if(set) {
1948                         if(sbuts) {
1949                                 // XXX sbuts->tabo= TAB_SHADING_TEX;    // hack from header_buttonswin.c
1950                                 // XXX sbuts->texfrom= 2;
1951                         }
1952 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1953                         la->texact= te->index;
1954                 }
1955                 else {
1956                         if(tselemp->id == ob->data) {
1957                                 if(la->texact==te->index) return 1;
1958                         }
1959                 }
1960         }
1961         else if(tep->idcode==ID_MA) {
1962                 Material *ma= (Material *)tselemp->id;
1963                 if(set) {
1964                         if(sbuts) {
1965                                 //sbuts->tabo= TAB_SHADING_TEX; // hack from header_buttonswin.c
1966                                 // XXX sbuts->texfrom= 0;
1967                         }
1968 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1969                         ma->texact= (char)te->index;
1970                         
1971                         /* also set active material */
1972                         ob->actcol= tep->index+1;
1973                 }
1974                 else if(tep->flag & TE_ACTIVE) {        // this is active material
1975                         if(ma->texact==te->index) return 1;
1976                 }
1977         }
1978         
1979         return 0;
1980 }
1981
1982
1983 static int tree_element_active_lamp(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1984 {
1985         Object *ob;
1986         
1987         /* we search for the object parent */
1988         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1989         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1990         
1991         if(set) {
1992 // XXX          extern_set_butspace(F5KEY, 0);
1993         }
1994         else return 1;
1995         
1996         return 0;
1997 }
1998
1999 static int tree_element_active_world(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2000 {
2001         TreeElement *tep;
2002         TreeStoreElem *tselem=NULL;
2003         Scene *sce=NULL;
2004         
2005         tep= te->parent;
2006         if(tep) {
2007                 tselem= TREESTORE(tep);
2008                 sce= (Scene *)tselem->id;
2009         }
2010         
2011         if(set) {       // make new scene active
2012                 if(sce && scene != sce) {
2013                         ED_screen_set_scene(C, sce);
2014                 }
2015         }
2016         
2017         if(tep==NULL || tselem->id == (ID *)scene) {
2018                 if(set) {
2019 // XXX                  extern_set_butspace(F8KEY, 0);
2020                 }
2021                 else {
2022                         return 1;
2023                 }
2024         }
2025         return 0;
2026 }
2027
2028 static int tree_element_active_defgroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2029 {
2030         Object *ob;
2031         
2032         /* id in tselem is object */
2033         ob= (Object *)tselem->id;
2034         if(set) {
2035                 ob->actdef= te->index+1;
2036                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
2037                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
2038         }
2039         else {
2040                 if(ob==OBACT)
2041                         if(ob->actdef== te->index+1) return 1;
2042         }
2043         return 0;
2044 }
2045
2046 static int tree_element_active_posegroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2047 {
2048         Object *ob= (Object *)tselem->id;
2049         
2050         if(set) {
2051                 if (ob->pose) {
2052                         ob->pose->active_group= te->index+1;
2053                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
2054                 }
2055         }
2056         else {
2057                 if(ob==OBACT && ob->pose) {
2058                         if (ob->pose->active_group== te->index+1) return 1;
2059                 }
2060         }
2061         return 0;
2062 }
2063
2064 static int tree_element_active_posechannel(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2065 {
2066         Object *ob= (Object *)tselem->id;
2067         bArmature *arm= ob->data;
2068         bPoseChannel *pchan= te->directdata;
2069         
2070         if(set) {
2071                 if(!(pchan->bone->flag & BONE_HIDDEN_P)) {
2072                         
2073                         if(set==2) ED_pose_deselectall(ob, 2, 0);       // 2 = clear active tag
2074                         else ED_pose_deselectall(ob, 0, 0);     // 0 = deselect 
2075                         
2076                         if(set==2 && (pchan->bone->flag & BONE_SELECTED)) {
2077                                 pchan->bone->flag &= ~BONE_SELECTED;
2078                                 if(arm->act_bone==pchan->bone)
2079                                         arm->act_bone= NULL;
2080                         } else {
2081                                 pchan->bone->flag |= BONE_SELECTED;
2082                                 arm->act_bone= pchan->bone;
2083                         }
2084                         
2085                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, ob);
2086
2087                 }
2088         }
2089         else {
2090                 if(ob==OBACT && ob->pose) {
2091                         if (pchan->bone->flag & BONE_SELECTED) return 1;
2092                 }
2093         }
2094         return 0;
2095 }
2096
2097 static int tree_element_active_bone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2098 {
2099         bArmature *arm= (bArmature *)tselem->id;
2100         Bone *bone= te->directdata;
2101         
2102         if(set) {
2103                 if(!(bone->flag & BONE_HIDDEN_P)) {
2104                         if(set==2) ED_pose_deselectall(OBACT, 2, 0);    // 2 is clear active tag
2105                         else ED_pose_deselectall(OBACT, 0, 0);
2106                         
2107                         if(set==2 && (bone->flag & BONE_SELECTED)) {
2108                                 bone->flag &= ~BONE_SELECTED;
2109                                 if(arm->act_bone==bone)
2110                                         arm->act_bone= NULL;
2111                         } else {
2112                                 bone->flag |= BONE_SELECTED;
2113                                 arm->act_bone= bone;
2114                         }
2115                         
2116                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, OBACT);
2117                 }
2118         }
2119         else {
2120                 Object *ob= OBACT;
2121                 
2122                 if(ob && ob->data==arm) {
2123                         if (bone->flag & BONE_SELECTED) return 1;
2124                 }
2125         }
2126         return 0;
2127 }
2128
2129
2130 /* ebones only draw in editmode armature */
2131 static int tree_element_active_ebone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2132 {
2133         EditBone *ebone= te->directdata;
2134         
2135         if(set) {
2136                 if(!(ebone->flag & BONE_HIDDEN_A)) {
2137                         bArmature *arm= scene->obedit->data;
2138                         if(set==2) ED_armature_deselectall(scene->obedit, 2, 0);        // only clear active tag
2139                         else ED_armature_deselectall(scene->obedit, 0, 0);      // deselect
2140
2141                         ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL;
2142                         arm->act_edbone= ebone;
2143
2144                         // flush to parent?
2145                         if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
2146                         
2147                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, scene->obedit);
2148                 }
2149         }
2150         else {
2151                 if (ebone->flag & BONE_SELECTED) return 1;
2152         }
2153         return 0;
2154 }
2155
2156 static int tree_element_active_modifier(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2157 {
2158         if(set) {
2159                 Object *ob= (Object *)tselem->id;
2160                 
2161                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
2162
2163 // XXX          extern_set_butspace(F9KEY, 0);
2164         }
2165         
2166         return 0;
2167 }
2168
2169 static int tree_element_active_psys(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2170 {
2171         if(set) {
2172                 Object *ob= (Object *)tselem->id;
2173                 
2174                 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE_DATA, ob);
2175                 
2176 // XXX          extern_set_butspace(F7KEY, 0);
2177         }
2178         
2179         return 0;
2180 }
2181
2182 static int tree_element_active_constraint(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2183 {
2184         if(set) {
2185                 Object *ob= (Object *)tselem->id;
2186                 
2187                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
2188 // XXX          extern_set_butspace(F7KEY, 0);
2189         }
2190         
2191         return 0;
2192 }
2193
2194 static int tree_element_active_text(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2195 {
2196         // XXX removed
2197         return 0;
2198 }
2199
2200 /* generic call for ID data check or make/check active in UI */
2201 static int tree_element_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2202 {
2203
2204         switch(te->idcode) {
2205                 case ID_MA:
2206                         return tree_element_active_material(C, scene, soops, te, set);
2207                 case ID_WO:
2208                         return tree_element_active_world(C, scene, soops, te, set);
2209                 case ID_LA:
2210                         return tree_element_active_lamp(C, scene, soops, te, set);
2211                 case ID_TE:
2212                         return tree_element_active_texture(C, scene, soops, te, set);
2213                 case ID_TXT:
2214                         return tree_element_active_text(C, scene, soops, te, set);
2215         }
2216         return 0;
2217 }
2218
2219 static int tree_element_active_pose(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2220 {
2221         Object *ob= (Object *)tselem->id;
2222         Base *base= object_in_scene(ob, scene);
2223         
2224         if(set) {
2225                 if(scene->obedit) 
2226                         ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
2227                 
2228                 if(ob->mode & OB_MODE_POSE) 
2229                         ED_armature_exit_posemode(C, base);
2230                 else 
2231                         ED_armature_enter_posemode(C, base);
2232         }
2233         else {
2234                 if(ob->mode & OB_MODE_POSE) return 1;
2235         }
2236         return 0;
2237 }
2238
2239 static int tree_element_active_sequence(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2240 {
2241         Sequence *seq= (Sequence*) te->directdata;
2242
2243         if(set) {
2244 // XXX          select_single_seq(seq, 1);
2245         }
2246         else {
2247                 if(seq->flag & SELECT)
2248                         return(1);
2249         }
2250         return(0);
2251 }
2252
2253 static int tree_element_active_sequence_dup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2254 {
2255         Sequence *seq, *p;
2256         Editing *ed= seq_give_editing(scene, FALSE);
2257
2258         seq= (Sequence*)te->directdata;
2259         if(set==0) {
2260                 if(seq->flag & SELECT)
2261                         return(1);
2262                 return(0);
2263         }
2264
2265 // XXX  select_single_seq(seq, 1);
2266         p= ed->seqbasep->first;
2267         while(p) {
2268                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
2269                         p= p->next;
2270                         continue;
2271                 }
2272
2273 //              if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
2274 // XXX                  select_single_seq(p, 0);
2275                 p= p->next;
2276         }
2277         return(0);
2278 }
2279
2280 static int tree_element_active_keymap_item(bContext *C, TreeElement *te, TreeStoreElem *tselem, int set)
2281 {
2282         wmKeyMapItem *kmi= te->directdata;
2283         
2284         if(set==0) {
2285                 if(kmi->flag & KMI_INACTIVE) return 0;
2286                 return 1;
2287         }
2288         else {
2289                 kmi->flag ^= KMI_INACTIVE;
2290         }
2291         return 0;
2292 }
2293
2294
2295 /* generic call for non-id data to make/check active in UI */
2296 /* Context can be NULL when set==0 */
2297 static int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
2298 {
2299         
2300         switch(tselem->type) {
2301                 case TSE_DEFGROUP:
2302                         return tree_element_active_defgroup(C, scene, te, tselem, set);
2303                 case TSE_BONE:
2304                         return tree_element_active_bone(C, scene, te, tselem, set);
2305                 case TSE_EBONE:
2306                         return tree_element_active_ebone(C, scene, te, tselem, set);
2307                 case TSE_MODIFIER:
2308                         return tree_element_active_modifier(C, te, tselem, set);
2309                 case TSE_LINKED_OB:
2310                         if(set) tree_element_set_active_object(C, scene, soops, te, set);
2311                         else if(tselem->id==(ID *)OBACT) return 1;
2312                         break;
2313                 case TSE_LINKED_PSYS:
2314                         return tree_element_active_psys(C, scene, te, tselem, set);
2315                 case TSE_POSE_BASE:
2316                         return tree_element_active_pose(C, scene, te, tselem, set);
2317                 case TSE_POSE_CHANNEL:
2318                         return tree_element_active_posechannel(C, scene, te, tselem, set);
2319                 case TSE_CONSTRAINT:
2320                         return tree_element_active_constraint(C, te, tselem, set);
2321                 case TSE_R_LAYER:
2322                         return tree_element_active_renderlayer(C, te, tselem, set);
2323                 case TSE_POSEGRP:
2324                         return tree_element_active_posegroup(C, scene, te, tselem, set);
2325                 case TSE_SEQUENCE:
2326                         return tree_element_active_sequence(C, te, tselem, set);
2327                 case TSE_SEQUENCE_DUP:
2328                         return tree_element_active_sequence_dup(C, scene, te, tselem, set);
2329                 case TSE_KEYMAP_ITEM:
2330                         return tree_element_active_keymap_item(C, te, tselem, set);
2331                         
2332         }
2333         return 0;
2334 }
2335
2336 static int do_outliner_item_activate(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, int extend, float *mval)
2337 {
2338         
2339         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2340                 TreeStoreElem *tselem= TREESTORE(te);
2341                 int openclose= 0;
2342
2343                 /* open close icon */
2344                 if((te->flag & TE_ICONROW)==0) {                                // hidden icon, no open/close
2345                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) 
2346                                 openclose= 1;
2347                 }
2348
2349                 if(openclose) {
2350                         /* all below close/open? */
2351                         if(extend) {
2352                                 tselem->flag &= ~TSE_CLOSED;
2353                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2354                         }
2355                         else {
2356                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2357                                 else tselem->flag |= TSE_CLOSED;
2358                                 
2359                         }
2360
2361                         return 1;
2362                 }
2363                 /* name and first icon */
2364                 else if(mval[0]>te->xs && mval[0]<te->xend) {
2365                         
2366                         /* always makes active object */
2367                         if(tselem->type!=TSE_SEQUENCE && tselem->type!=TSE_SEQ_STRIP && tselem->type!=TSE_SEQUENCE_DUP)
2368                                 tree_element_set_active_object(C, scene, soops, te, 1 + (extend!=0 && tselem->type==0));
2369                         
2370                         if(tselem->type==0) { // the lib blocks
2371                                 /* editmode? */
2372                                 if(te->idcode==ID_SCE) {
2373                                         if(scene!=(Scene *)tselem->id) {
2374                                                 ED_screen_set_scene(C, (Scene *)tselem->id);
2375                                         }
2376                                 }
2377                                 else if(ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
2378                                         Object *obedit= CTX_data_edit_object(C);
2379                                         if(obedit) 
2380                                                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
2381                                         else {
2382                                                 ED_object_enter_editmode(C, EM_WAITCURSOR);
2383                                                 // XXX extern_set_butspace(F9KEY, 0);
2384                                         }
2385                                 } else {        // rest of types
2386                                         tree_element_active(C, scene, soops, te, 1);
2387                                 }
2388                                 
2389                         }
2390                         else tree_element_type_active(C, scene, soops, te, tselem, 1+(extend!=0));
2391
2392                         return 1;
2393                 }
2394         }
2395         
2396         for(te= te->subtree.first; te; te= te->next) {
2397                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, mval)) return 1;
2398         }
2399         return 0;
2400 }
2401
2402 /* event can enterkey, then it opens/closes */
2403 static int outliner_item_activate(bContext *C, wmOperator *op, wmEvent *event)
2404 {
2405         Scene *scene= CTX_data_scene(C);
2406         ARegion *ar= CTX_wm_region(C);
2407         SpaceOops *soops= CTX_wm_space_outliner(C);
2408         TreeElement *te;
2409         float fmval[2];
2410         int extend= RNA_boolean_get(op->ptr, "extend");
2411         
2412         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2413         
2414         for(te= soops->tree.first; te; te= te->next) {
2415                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, fmval)) break;
2416         }
2417         
2418         if(te) {
2419                 ED_undo_push(C, "Outliner click event");
2420         }
2421         else {
2422                 short selecting= -1;
2423                 int row;
2424                 
2425                 /* get row number - 100 here is just a dummy value since we don't need the column */
2426                 UI_view2d_listview_view_to_cell(&ar->v2d, 1000, OL_H, 0.0f, 0.0f, 
2427                                                 fmval[0], fmval[1], NULL, &row);
2428                 
2429                 /* select relevant row */
2430                 outliner_select(soops, &soops->tree, &row, &selecting);
2431                 
2432                 soops->storeflag |= SO_TREESTORE_REDRAW;
2433                 
2434                 ED_undo_push(C, "Outliner selection event");
2435         }
2436         
2437         ED_region_tag_redraw(ar);
2438
2439         return OPERATOR_FINISHED;
2440 }
2441
2442 void OUTLINER_OT_item_activate(wmOperatorType *ot)
2443 {
2444         ot->name= "Activate Item";
2445         ot->idname= "OUTLINER_OT_item_activate";
2446         
2447         ot->invoke= outliner_item_activate;
2448         
2449         ot->poll= ED_operator_outliner_active;
2450         
2451         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection for activation.");
2452 }
2453
2454 /* *********** */
2455
2456 static int do_outliner_item_openclose(bContext *C, SpaceOops *soops, TreeElement *te, int all, float *mval)
2457 {
2458         
2459         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2460                 TreeStoreElem *tselem= TREESTORE(te);
2461
2462                 /* all below close/open? */
2463                 if(all) {
2464                         tselem->flag &= ~TSE_CLOSED;
2465                         outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2466                 }
2467                 else {
2468                         if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2469                         else tselem->flag |= TSE_CLOSED;
2470                 }
2471
2472                 return 1;
2473         }
2474         
2475         for(te= te->subtree.first; te; te= te->next) {
2476                 if(do_outliner_item_openclose(C, soops, te, all, mval)) 
2477                         return 1;
2478         }
2479         return 0;
2480         
2481 }
2482
2483 /* event can enterkey, then it opens/closes */
2484 static int outliner_item_openclose(bContext *C, wmOperator *op, wmEvent *event)
2485 {
2486         ARegion *ar= CTX_wm_region(C);
2487         SpaceOops *soops= CTX_wm_space_outliner(C);
2488         TreeElement *te;
2489         float fmval[2];
2490         int all= RNA_boolean_get(op->ptr, "all");
2491         
2492         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2493         
2494         for(te= soops->tree.first; te; te= te->next) {
2495                 if(do_outliner_item_openclose(C, soops, te, all, fmval)) 
2496                         break;
2497         }
2498
2499         ED_region_tag_redraw(ar);
2500         
2501         return OPERATOR_FINISHED;
2502 }
2503
2504 void OUTLINER_OT_item_openclose(wmOperatorType *ot)
2505 {
2506         ot->name= "Open/Close Item";
2507         ot->idname= "OUTLINER_OT_item_openclose";
2508         
2509         ot->invoke= outliner_item_openclose;
2510         
2511         ot->poll= ED_operator_outliner_active;
2512         
2513         RNA_def_boolean(ot->srna, "all", 1, "All", "Close or open all items.");
2514
2515 }
2516
2517
2518 /* ********************************************** */
2519
2520 static int do_outliner_item_rename(bContext *C, ARegion *ar, SpaceOops *soops, TreeElement *te, float *mval)
2521 {
2522         
2523         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2524                 TreeStoreElem *tselem= TREESTORE(te);
2525                 
2526                 /* name and first icon */
2527                 if(mval[0]>te->xs && mval[0]<te->xend) {
2528                         
2529                         /* can't rename rna datablocks entries */
2530                         if(ELEM3(tselem->type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM))
2531                            ;
2532                         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)) 
2533                                         error("Cannot edit builtin name");
2534                         else if(ELEM3(tselem->type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP))
2535                                 error("Cannot edit sequence name");
2536                         else if(tselem->id->lib) {
2537                                 // XXX                                          error_libdata();
2538                         } else if(te->idcode == ID_LI && te->parent) {
2539                                 error("Cannot edit the path of an indirectly linked library");
2540                         } else {
2541                                 tselem->flag |= TSE_TEXTBUT;
2542                                 ED_region_tag_redraw(ar);
2543                         }
2544                 }
2545                 return 1;
2546         }
2547         
2548         for(te= te->subtree.first; te; te= te->next) {
2549                 if(do_outliner_item_rename(C, ar, soops, te, mval)) return 1;
2550         }
2551         return 0;
2552 }
2553
2554 static int outliner_item_rename(bContext *C, wmOperator *op, wmEvent *event)
2555 {
2556         ARegion *ar= CTX_wm_region(C);
2557         SpaceOops *soops= CTX_wm_space_outliner(C);
2558         TreeElement *te;
2559         float fmval[2];
2560         
2561         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2562         
2563         for(te= soops->tree.first; te; te= te->next) {
2564                 if(do_outliner_item_rename(C, ar, soops, te, fmval)) break;
2565         }
2566         
2567         return OPERATOR_FINISHED;
2568 }
2569
2570
2571 void OUTLINER_OT_item_rename(wmOperatorType *ot)
2572 {
2573         ot->name= "Rename Item";
2574         ot->idname= "OUTLINER_OT_item_rename";
2575         
2576         ot->invoke= outliner_item_rename;
2577         
2578         ot->poll= ED_operator_outliner_active;
2579 }
2580
2581
2582
2583 /* recursive helper for function below */
2584 static void outliner_set_coordinates_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
2585 {
2586         TreeStoreElem *tselem= TREESTORE(te);
2587         
2588         /* store coord and continue, we need coordinates for elements outside view too */
2589         te->xs= (float)startx;
2590         te->ys= (float)(*starty);
2591         *starty-= OL_H;
2592         
2593         if((tselem->flag & TSE_CLOSED)==0) {
2594                 TreeElement *ten;
2595                 for(ten= te->subtree.first; ten; ten= ten->next) {
2596                         outliner_set_coordinates_element(soops, ten, startx+OL_X, starty);
2597                 }
2598         }
2599         
2600 }
2601
2602 /* to retrieve coordinates with redrawing the entire tree */
2603 static void outliner_set_coordinates(ARegion *ar, SpaceOops *soops)
2604 {
2605         TreeElement *te;
2606         int starty= (int)(ar->v2d.tot.ymax)-OL_H;
2607         int startx= 0;
2608         
2609         for(te= soops->tree.first; te; te= te->next) {
2610                 outliner_set_coordinates_element(soops, te, startx, &starty);
2611         }
2612 }
2613
2614 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
2615 {
2616         TreeElement *te, *tes;
2617         TreeStoreElem *tselem;
2618         
2619         for(te= lb->first; te; te= te->next) {
2620                 tselem= TREESTORE(te);
2621                 if(tselem->type==0) {
2622                         if(tselem->id==id) return te;
2623                         /* only deeper on scene or object */
2624                         if( te->idcode==ID_OB || te->idcode==ID_SCE) { 
2625                                 tes= outliner_find_id(soops, &te->subtree, id);
2626                                 if(tes) return tes;
2627                         }
2628                 }
2629         }
2630         return NULL;
2631 }
2632
2633 static int outliner_show_active_exec(bContext *C, wmOperator *op)
2634 {
2635         SpaceOops *so= CTX_wm_space_outliner(C);
2636         Scene *scene= CTX_data_scene(C);
2637         ARegion *ar= CTX_wm_region(C);
2638         View2D *v2d= &ar->v2d;
2639         
2640         TreeElement *te;
2641         int xdelta, ytop;
2642         
2643         // TODO: make this get this info from context instead...
2644         if (OBACT == NULL) 
2645                 return OPERATOR_CANCELLED;
2646         
2647         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
2648         if (te) {
2649                 /* make te->ys center of view */
2650                 ytop= (int)(te->ys + (v2d->mask.ymax - v2d->mask.ymin)/2);
2651                 if (ytop>0) ytop= 0;
2652                 
2653                 v2d->cur.ymax= (float)ytop;
2654                 v2d->cur.ymin= (float)(ytop-(v2d->mask.ymax - v2d->mask.ymin));
2655                 
2656                 /* make te->xs ==> te->xend center of view */
2657                 xdelta = (int)(te->xs - v2d->cur.xmin);
2658                 v2d->cur.xmin += xdelta;
2659                 v2d->cur.xmax += xdelta;
2660                 
2661                 so->storeflag |= SO_TREESTORE_REDRAW;
2662         }
2663         
2664         ED_region_tag_redraw(ar);
2665         
2666         return OPERATOR_FINISHED;
2667 }
2668
2669 void OUTLINER_OT_show_active(wmOperatorType *ot)
2670 {
2671         /* identifiers */
2672         ot->name= "Show Active";
2673         ot->idname= "OUTLINER_OT_show_active";
2674         ot->description= "Adjust the view so that the active Object is shown centered";
2675         
2676         /* callbacks */
2677         ot->exec= outliner_show_active_exec;
2678         ot->poll= ED_operator_outliner_active;
2679         
2680         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2681 }
2682
2683 /* find next element that has this name */
2684 static TreeElement *outliner_find_named(SpaceOops *soops, ListBase *lb, char *name, int flags, TreeElement *prev, int *prevFound)
2685 {
2686         TreeElement *te, *tes;
2687         
2688         for (te= lb->first; te; te= te->next) {
2689                 int found;
2690                 
2691                 /* determine if match */
2692                 if(flags==OL_FIND)
2693                         found= BLI_strcasestr(te->name, name)!=NULL;
2694                 else if(flags==OL_FIND_CASE)
2695                         found= strstr(te->name, name)!=NULL;
2696                 else if(flags==OL_FIND_COMPLETE)
2697                         found= BLI_strcasecmp(te->name, name)==0;
2698                 else
2699                         found= strcmp(te->name, name)==0;
2700                 
2701                 if(found) {
2702                         /* name is right, but is element the previous one? */
2703                         if (prev) {
2704                                 if ((te != prev) && (*prevFound)) 
2705                                         return te;
2706                                 if (te == prev) {
2707                                         *prevFound = 1;
2708                                 }
2709                         }
2710                         else
2711                                 return te;
2712                 }
2713                 
2714                 tes= outliner_find_named(soops, &te->subtree, name, flags, prev, prevFound);
2715                 if(tes) return tes;
2716         }
2717
2718         /* nothing valid found */
2719         return NULL;
2720 }
2721
2722 /* tse is not in the treestore, we use its contents to find a match */
2723 static TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
2724 {
2725         TreeStore *ts= soops->treestore;
2726         TreeStoreElem *tselem;
2727         int a;
2728         
2729         if(tse->id==NULL) return NULL;
2730         
2731         /* check if 'tse' is in treestore */
2732         tselem= ts->data;
2733         for(a=0; a<ts->usedelem; a++, tselem++) {
2734                 if((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
2735                         if(tselem->id==tse->id) {
2736                                 break;
2737                         }
2738                 }
2739         }
2740         if(tselem) 
2741                 return outliner_find_tree_element(&soops->tree, a);
2742         
2743         return NULL;
2744 }
2745
2746
2747 /* Called to find an item based on name.
2748  */
2749 void outliner_find_panel(Scene *scene, ARegion *ar, SpaceOops *soops, int again, int flags) 
2750 {
2751         TreeElement *te= NULL;
2752         TreeElement *last_find;
2753         TreeStoreElem *tselem;
2754         int ytop, xdelta, prevFound=0;
2755         char name[33];
2756         
2757         /* get last found tree-element based on stored search_tse */
2758         last_find= outliner_find_tse(soops, &soops->search_tse);
2759         
2760         /* determine which type of search to do */
2761         if (again && last_find) {
2762                 /* no popup panel - previous + user wanted to search for next after previous */         
2763                 BLI_strncpy(name, soops->search_string, 33);
2764                 flags= soops->search_flags;
2765                 
2766                 /* try to find matching element */
2767                 te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2768                 if (te==NULL) {
2769                         /* no more matches after previous, start from beginning again */
2770                         prevFound= 1;
2771                         te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2772                 }
2773         }
2774         else {
2775                 /* pop up panel - no previous, or user didn't want search after previous */
2776                 strcpy(name, "");
2777 // XXX          if (sbutton(name, 0, sizeof(name)-1, "Find: ") && name[0]) {
2778 //                      te= outliner_find_named(soops, &soops->tree, name, flags, NULL, &prevFound);
2779 //              }
2780 //              else return; /* XXX RETURN! XXX */
2781         }
2782
2783         /* do selection and reveal */
2784         if (te) {
2785                 tselem= TREESTORE(te);
2786                 if (tselem) {
2787                         /* expand branches so that it will be visible, we need to get correct coordinates */
2788                         if( outliner_open_back(soops, te))
2789                                 outliner_set_coordinates(ar, soops);
2790                         
2791                         /* deselect all visible, and select found element */
2792                         outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
2793                         tselem->flag |= TSE_SELECTED;
2794                         
2795                         /* make te->ys center of view */
2796                         ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2797                         if(ytop>0) ytop= 0;
2798                         ar->v2d.cur.ymax= (float)ytop;
2799                         ar->v2d.cur.ymin= (float)(ytop-(ar->v2d.mask.ymax-ar->v2d.mask.ymin));
2800                         
2801                         /* make te->xs ==> te->xend center of view */
2802                         xdelta = (int)(te->xs - ar->v2d.cur.xmin);
2803                         ar->v2d.cur.xmin += xdelta;
2804                         ar->v2d.cur.xmax += xdelta;
2805                         
2806                         /* store selection */
2807                         soops->search_tse= *tselem;
2808                         
2809                         BLI_strncpy(soops->search_string, name, 33);
2810                         soops->search_flags= flags;
2811                         
2812                         /* redraw */
2813                         soops->storeflag |= SO_TREESTORE_REDRAW;
2814                 }
2815         }
2816         else {
2817                 /* no tree-element found */
2818                 error("Not found: %s", name);
2819         }
2820 }
2821
2822 /* helper function for tree_element_shwo_hierarchy() - recursively checks whether subtrees have any objects*/
2823 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
2824 {
2825         TreeElement *te;
2826         TreeStoreElem *tselem;
2827         
2828         for(te= lb->first; te; te= te->next) {
2829                 tselem= TREESTORE(te);
2830                 if(tselem->type==0 && te->idcode==ID_OB) return 1;
2831                 if( subtree_has_objects(soops, &te->subtree)) return 1;
2832         }
2833         return 0;
2834 }
2835
2836 /* recursive helper function for Show Hierarchy operator */
2837 static void tree_element_show_hierarchy(Scene *scene, SpaceOops *soops, ListBase *lb)
2838 {
2839         TreeElement *te;
2840         TreeStoreElem *tselem;
2841
2842         /* open all object elems, close others */
2843         for(te= lb->first; te; te= te->next) {
2844                 tselem= TREESTORE(te);
2845                 
2846                 if(tselem->type==0) {
2847                         if(te->idcode==ID_SCE) {
2848                                 if(tselem->id!=(ID *)scene) tselem->flag |= TSE_CLOSED;
2849                                         else tselem->flag &= ~TSE_CLOSED;
2850                         }
2851                         else if(te->idcode==ID_OB) {
2852                                 if(subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
2853                                 else tselem->flag |= TSE_CLOSED;
2854                         }
2855                 }
2856                 else tselem->flag |= TSE_CLOSED;
2857                 
2858                 if(tselem->flag & TSE_CLOSED); else tree_element_show_hierarchy(scene, soops, &te->subtree);
2859         }
2860 }
2861
2862 /* show entire object level hierarchy */
2863 static int outliner_show_hierarchy_exec(bContext *C, wmOperator *op)
2864 {
2865         SpaceOops *soops= CTX_wm_space_outliner(C);
2866         ARegion *ar= CTX_wm_region(C);
2867         Scene *scene= CTX_data_scene(C);
2868         
2869         /* recursively open/close levels */
2870         tree_element_show_hierarchy(scene, soops, &soops->tree);
2871         
2872         ED_region_tag_redraw(ar);
2873         
2874         return OPERATOR_FINISHED;
2875 }
2876
2877 void OUTLINER_OT_show_hierarchy(wmOperatorType *ot)
2878 {
2879         /* identifiers */
2880         ot->name= "Show Hierarchy";
2881         ot->idname= "OUTLINER_OT_show_hierarchy";
2882         ot->description= "Open all object entries and close all others";
2883         
2884         /* callbacks */
2885         ot->exec= outliner_show_hierarchy_exec;
2886         ot->poll= ED_operator_outliner_active; //  TODO: shouldn't be allowed in RNA views...
2887         
2888         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2889 }
2890
2891 void outliner_select(SpaceOops *soops, ListBase *lb, int *index, short *selecting)
2892 {
2893         TreeElement *te;
2894         TreeStoreElem *tselem;
2895         
2896         for (te= lb->first; te && *index >= 0; te=te->next, (*index)--) {
2897                 tselem= TREESTORE(te);
2898                 
2899                 /* if we've encountered the right item, set its 'Outliner' selection status */
2900                 if (*index == 0) {
2901                         /* this should be the last one, so no need to do anything with index */
2902                         if ((te->flag & TE_ICONROW)==0) {
2903                                 /* -1 value means toggle testing for now... */
2904                                 if (*selecting == -1) {
2905                                         if (tselem->flag & TSE_SELECTED) 
2906                                                 *selecting= 0;
2907                                         else 
2908                                                 *selecting= 1;
2909                                 }
2910                                 
2911                                 /* set selection */
2912                                 if (*selecting) 
2913                                         tselem->flag |= TSE_SELECTED;
2914                                 else 
2915                                         tselem->flag &= ~TSE_SELECTED;
2916                         }
2917                 }
2918                 else if ((tselem->flag & TSE_CLOSED)==0) {
2919                         /* Only try selecting sub-elements if we haven't hit the right element yet
2920                          *
2921                          * Hack warning:
2922                          *      Index must be reduced before supplying it to the sub-tree to try to do
2923                          *      selection, however, we need to increment it again for the next loop to 
2924                          *      function correctly
2925                          */
2926                         (*index)--;
2927                         outliner_select(soops, &te->subtree, index, selecting);
2928                         (*index)++;
2929                 }
2930         }
2931 }
2932
2933 /* ************ SELECTION OPERATIONS ********* */
2934
2935 static void set_operation_types(SpaceOops *soops, ListBase *lb,
2936                                 int *scenelevel,
2937                                 int *objectlevel,
2938                                 int *idlevel,
2939                                 int *datalevel)
2940 {
2941         TreeElement *te;
2942         TreeStoreElem *tselem;
2943         
2944         for(te= lb->first; te; te= te->next) {
2945                 tselem= TREESTORE(te);
2946                 if(tselem->flag & TSE_SELECTED) {
2947                         if(tselem->type) {
2948                                 if(*datalevel==0) 
2949                                         *datalevel= tselem->type;
2950                                 else if(*datalevel!=tselem->type) 
2951                                         *datalevel= -1;
2952                         }