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