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