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