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