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