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