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