doxygen: prevent GPL license block from being parsed as doxygen comment.
[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 static 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 static 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, const 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                                 
1413                                 for(go= group->gobject.first; go; go= go->next) {
1414                                         ten= outliner_add_element(soops, &te->subtree, go->ob, te, 0, 0);
1415                                         ten->directdata= NULL; /* eh, why? */
1416                                 }
1417                                 outliner_make_hierarchy(soops, &te->subtree);
1418                                 /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
1419                                 for(go= group->gobject.first; go; go= go->next) go->ob->id.newid= NULL;
1420                         }
1421                 }
1422         }
1423         else if(soops->outlinevis == SO_SAME_TYPE) {
1424                 Object *ob= OBACT;
1425                 if(ob) {
1426                         for(base= scene->base.first; base; base= base->next) {
1427                                 if(base->object->type==ob->type) {
1428                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1429                                         ten->directdata= base;
1430                                 }
1431                         }
1432                         outliner_make_hierarchy(soops, &soops->tree);
1433                 }
1434         }
1435         else if(soops->outlinevis == SO_SELECTED) {
1436                 for(base= scene->base.first; base; base= base->next) {
1437                         if(base->lay & scene->lay) {
1438                                 if(base==BASACT || (base->flag & SELECT)) {
1439                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
1440                                         ten->directdata= base;
1441                                 }
1442                         }
1443                 }
1444                 outliner_make_hierarchy(soops, &soops->tree);
1445         }
1446         else if(soops->outlinevis==SO_SEQUENCE) {
1447                 Sequence *seq;
1448                 Editing *ed= seq_give_editing(scene, FALSE);
1449                 int op;
1450
1451                 if(ed==NULL)
1452                         return;
1453
1454                 seq= ed->seqbasep->first;
1455                 if(!seq)
1456                         return;
1457
1458                 while(seq) {
1459                         op= need_add_seq_dup(seq);
1460                         if(op==1)
1461                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1462                         else if(op==0) {
1463                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1464                                 add_seq_dup(soops, seq, ten, 0);
1465                         }
1466                         seq= seq->next;
1467                 }
1468         }
1469         else if(soops->outlinevis==SO_DATABLOCKS) {
1470                 PointerRNA mainptr;
1471
1472                 RNA_main_pointer_create(mainvar, &mainptr);
1473
1474                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1475
1476                 if(show_opened)  {
1477                         tselem= TREESTORE(ten);
1478                         tselem->flag &= ~TSE_CLOSED;
1479                 }
1480         }
1481         else if(soops->outlinevis==SO_USERDEF) {
1482                 PointerRNA userdefptr;
1483
1484                 RNA_pointer_create(NULL, &RNA_UserPreferences, &U, &userdefptr);
1485
1486                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1487
1488                 if(show_opened)  {
1489                         tselem= TREESTORE(ten);
1490                         tselem->flag &= ~TSE_CLOSED;
1491                 }
1492         }
1493         else if(soops->outlinevis==SO_KEYMAP) {
1494                 wmWindowManager *wm= mainvar->wm.first;
1495                 wmKeyMap *km;
1496                 
1497                 for(km= wm->defaultconf->keymaps.first; km; km= km->next) {
1498                         ten= outliner_add_element(soops, &soops->tree, (void*)km, NULL, TSE_KEYMAP, 0);
1499                 }
1500         }
1501         else {
1502                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1503                 if(ten) ten->directdata= BASACT;
1504         }
1505
1506         outliner_sort(soops, &soops->tree);
1507         outliner_filter_tree(soops, &soops->tree);
1508 }
1509
1510 /* **************** INTERACTIVE ************* */
1511
1512
1513 static int outliner_scroll_page_exec(bContext *C, wmOperator *op)
1514 {
1515         ARegion *ar= CTX_wm_region(C);
1516         int dy= ar->v2d.mask.ymax - ar->v2d.mask.ymin;
1517         int up= 0;
1518         
1519         if(RNA_boolean_get(op->ptr, "up"))
1520                 up= 1;
1521
1522         if(up == 0) dy= -dy;
1523         ar->v2d.cur.ymin+= dy;
1524         ar->v2d.cur.ymax+= dy;
1525         
1526         ED_region_tag_redraw(ar);
1527         
1528         return OPERATOR_FINISHED;
1529 }
1530
1531
1532 void OUTLINER_OT_scroll_page(wmOperatorType *ot)
1533 {
1534         /* identifiers */
1535         ot->name= "Scroll Page";
1536         ot->idname= "OUTLINER_OT_scroll_page";
1537         ot->description= "Scroll page up or down";
1538         
1539         /* callbacks */
1540         ot->exec= outliner_scroll_page_exec;
1541         ot->poll= ED_operator_outliner_active;
1542         
1543         /* properties */
1544         RNA_def_boolean(ot->srna, "up", 0, "Up", "Scroll up one page.");
1545 }
1546
1547
1548 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
1549 {
1550         TreeElement *te;
1551         int level=curlevel, lev;
1552         
1553         for(te= lb->first; te; te= te->next) {
1554                 
1555                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
1556                 if(lev>level) level= lev;
1557         }
1558         return level;
1559 }
1560
1561 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
1562 {
1563         TreeElement *te;
1564         TreeStoreElem *tselem;
1565         int level;
1566         
1567         for(te= lb->first; te; te= te->next) {
1568                 tselem= TREESTORE(te);
1569                 if(tselem->flag & flag) return curlevel;
1570                 
1571                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
1572                 if(level) return level;
1573         }
1574         return 0;
1575 }
1576
1577 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
1578 {
1579         TreeElement *te;
1580         TreeStoreElem *tselem;
1581         
1582         for(te= lb->first; te; te= te->next) {
1583                 tselem= TREESTORE(te);
1584                 if(set==0) tselem->flag &= ~flag;
1585                 else tselem->flag |= flag;
1586                 outliner_set_flag(soops, &te->subtree, flag, set);
1587         }
1588 }
1589
1590 /* --- */
1591
1592 /* same check needed for both object operation and restrict column button func
1593  * return 0 when in edit mode (cannot restrict view or select)
1594  * otherwise return 1 */
1595 static int common_restrict_check(bContext *C, Object *ob)
1596 {
1597         /* Don't allow hide an object in edit mode,
1598          * check the bug #22153 and #21609, #23977
1599          */
1600         Object *obedit= CTX_data_edit_object(C);
1601         if (obedit && obedit == ob) {
1602                 /* found object is hidden, reset */
1603                 if (ob->restrictflag & OB_RESTRICT_VIEW)
1604                         ob->restrictflag &= ~OB_RESTRICT_VIEW;
1605                 /* found object is unselectable, reset */
1606                 if (ob->restrictflag & OB_RESTRICT_SELECT)
1607                         ob->restrictflag &= ~OB_RESTRICT_SELECT;
1608                 return 0;
1609         }
1610         
1611         return 1;
1612 }
1613
1614 static void object_toggle_visibility_cb(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
1615 {
1616         Base *base= (Base *)te->directdata;
1617         Object *ob = (Object *)tselem->id;
1618         
1619         /* add check for edit mode */
1620         if(!common_restrict_check(C, ob)) return;
1621         
1622         if(base || (base= object_in_scene(ob, scene))) {
1623                 if((base->object->restrictflag ^= OB_RESTRICT_VIEW)) {
1624                         ED_base_object_select(base, BA_DESELECT);
1625                 }
1626         }
1627 }
1628
1629 static int outliner_toggle_visibility_exec(bContext *C, wmOperator *UNUSED(op))
1630 {
1631         SpaceOops *soops= CTX_wm_space_outliner(C);
1632         Scene *scene= CTX_data_scene(C);
1633         ARegion *ar= CTX_wm_region(C);
1634         
1635         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_visibility_cb);
1636         
1637         WM_event_add_notifier(C, NC_SCENE|ND_OB_VISIBLE, scene);
1638         ED_region_tag_redraw(ar);
1639         
1640         return OPERATOR_FINISHED;
1641 }
1642
1643 void OUTLINER_OT_visibility_toggle(wmOperatorType *ot)
1644 {
1645         /* identifiers */
1646         ot->name= "Toggle Visibility";
1647         ot->idname= "OUTLINER_OT_visibility_toggle";
1648         ot->description= "Toggle the visibility of selected items";
1649         
1650         /* callbacks */
1651         ot->exec= outliner_toggle_visibility_exec;
1652         ot->poll= ED_operator_outliner_active_no_editobject;
1653         
1654         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1655 }
1656
1657 /* --- */
1658
1659 static void object_toggle_selectability_cb(bContext *UNUSED(C), Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
1660 {
1661         Base *base= (Base *)te->directdata;
1662         
1663         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1664         if(base) {
1665                 base->object->restrictflag^=OB_RESTRICT_SELECT;
1666         }
1667 }
1668
1669 static int outliner_toggle_selectability_exec(bContext *C, wmOperator *UNUSED(op))
1670 {
1671         SpaceOops *soops= CTX_wm_space_outliner(C);
1672         Scene *scene= CTX_data_scene(C);
1673         ARegion *ar= CTX_wm_region(C);
1674         
1675         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_selectability_cb);
1676         
1677         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1678         ED_region_tag_redraw(ar);
1679         
1680         return OPERATOR_FINISHED;
1681 }
1682
1683 void OUTLINER_OT_selectability_toggle(wmOperatorType *ot)
1684 {
1685         /* identifiers */
1686         ot->name= "Toggle Selectability";
1687         ot->idname= "OUTLINER_OT_selectability_toggle";
1688         ot->description= "Toggle the selectability";
1689         
1690         /* callbacks */
1691         ot->exec= outliner_toggle_selectability_exec;
1692         ot->poll= ED_operator_outliner_active_no_editobject;
1693         
1694         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1695 }
1696
1697 static void object_toggle_renderability_cb(bContext *UNUSED(C), Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem)
1698 {
1699         Base *base= (Base *)te->directdata;
1700         
1701         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1702         if(base) {
1703                 base->object->restrictflag^=OB_RESTRICT_RENDER;
1704         }
1705 }
1706
1707 static int outliner_toggle_renderability_exec(bContext *C, wmOperator *UNUSED(op))
1708 {
1709         SpaceOops *soops= CTX_wm_space_outliner(C);
1710         Scene *scene= CTX_data_scene(C);
1711         ARegion *ar= CTX_wm_region(C);
1712         
1713         outliner_do_object_operation(C, scene, soops, &soops->tree, object_toggle_renderability_cb);
1714         
1715         ED_region_tag_redraw(ar);
1716         
1717         return OPERATOR_FINISHED;
1718 }
1719
1720 void OUTLINER_OT_renderability_toggle(wmOperatorType *ot)
1721 {
1722         /* identifiers */
1723         ot->name= "Toggle Renderability";
1724         ot->idname= "OUTLINER_OT_renderability_toggle";
1725         ot->description= "Toggle the renderbility of selected items";
1726         
1727         /* callbacks */
1728         ot->exec= outliner_toggle_renderability_exec;
1729         ot->poll= ED_operator_outliner_active;
1730         
1731         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1732 }
1733
1734 /* --- */
1735
1736 static int outliner_toggle_expanded_exec(bContext *C, wmOperator *UNUSED(op))
1737 {
1738         SpaceOops *soops= CTX_wm_space_outliner(C);
1739         ARegion *ar= CTX_wm_region(C);
1740         
1741         if (outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
1742                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
1743         else 
1744                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
1745         
1746         ED_region_tag_redraw(ar);
1747         
1748         return OPERATOR_FINISHED;
1749 }
1750
1751 void OUTLINER_OT_expanded_toggle(wmOperatorType *ot)
1752 {
1753         /* identifiers */
1754         ot->name= "Expand/Collapse All";
1755         ot->idname= "OUTLINER_OT_expanded_toggle";
1756         ot->description= "Expand/Collapse all items";
1757         
1758         /* callbacks */
1759         ot->exec= outliner_toggle_expanded_exec;
1760         ot->poll= ED_operator_outliner_active;
1761         
1762         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1763 }
1764
1765 /* --- */
1766
1767 static int outliner_toggle_selected_exec(bContext *C, wmOperator *UNUSED(op))
1768 {
1769         SpaceOops *soops= CTX_wm_space_outliner(C);
1770         ARegion *ar= CTX_wm_region(C);
1771         Scene *scene= CTX_data_scene(C);
1772         
1773         if (outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
1774                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
1775         else 
1776                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
1777         
1778         soops->storeflag |= SO_TREESTORE_REDRAW;
1779         
1780         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1781         ED_region_tag_redraw(ar);
1782         
1783         return OPERATOR_FINISHED;
1784 }
1785
1786 void OUTLINER_OT_selected_toggle(wmOperatorType *ot)
1787 {
1788         /* identifiers */
1789         ot->name= "Toggle Selected";
1790         ot->idname= "OUTLINER_OT_selected_toggle";
1791         ot->description= "Toggle the Outliner selection of items";
1792         
1793         /* callbacks */
1794         ot->exec= outliner_toggle_selected_exec;
1795         ot->poll= ED_operator_outliner_active;
1796         
1797         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1798 }
1799
1800 /* --- */
1801
1802 /* helper function for Show/Hide one level operator */
1803 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
1804 {
1805         TreeElement *te;
1806         TreeStoreElem *tselem;
1807         
1808         for(te= lb->first; te; te= te->next) {
1809                 tselem= TREESTORE(te);
1810                 
1811                 if(open) {
1812                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
1813                 }
1814                 else {
1815                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
1816                 }
1817                 
1818                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
1819         }
1820 }
1821
1822 static int outliner_one_level_exec(bContext *C, wmOperator *op)
1823 {
1824         SpaceOops *soops= CTX_wm_space_outliner(C);
1825         ARegion *ar= CTX_wm_region(C);
1826         int add= RNA_boolean_get(op->ptr, "open");
1827         int level;
1828         
1829         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
1830         if(add==1) {
1831                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
1832         }
1833         else {
1834                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
1835                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
1836         }
1837         
1838         ED_region_tag_redraw(ar);
1839         
1840         return OPERATOR_FINISHED;
1841 }
1842
1843 void OUTLINER_OT_show_one_level(wmOperatorType *ot)
1844 {
1845         /* identifiers */
1846         ot->name= "Show/Hide One Level";
1847         ot->idname= "OUTLINER_OT_show_one_level";
1848         ot->description= "Expand/collapse all entries by 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 static 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);*/ /*UNUSED*/
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_camera(bContext *UNUSED(C), Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2113 {
2114         Object *ob= (Object *)outliner_search_back(soops, te, ID_OB);
2115
2116         if(set)
2117                 return 0;
2118
2119         return scene->camera == ob;
2120 }
2121
2122 static int tree_element_active_world(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2123 {
2124         TreeElement *tep;
2125         TreeStoreElem *tselem=NULL;
2126         Scene *sce=NULL;
2127         
2128         tep= te->parent;
2129         if(tep) {
2130                 tselem= TREESTORE(tep);
2131                 sce= (Scene *)tselem->id;
2132         }
2133         
2134         if(set) {       // make new scene active
2135                 if(sce && scene != sce) {
2136                         ED_screen_set_scene(C, sce);
2137                 }
2138         }
2139         
2140         if(tep==NULL || tselem->id == (ID *)scene) {
2141                 if(set) {
2142 // XXX                  extern_set_butspace(F8KEY, 0);
2143                 }
2144                 else {
2145                         return 1;
2146                 }
2147         }
2148         return 0;
2149 }
2150
2151 static int tree_element_active_defgroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2152 {
2153         Object *ob;
2154         
2155         /* id in tselem is object */
2156         ob= (Object *)tselem->id;
2157         if(set) {
2158                 ob->actdef= te->index+1;
2159                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2160                 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
2161         }
2162         else {
2163                 if(ob==OBACT)
2164                         if(ob->actdef== te->index+1) return 1;
2165         }
2166         return 0;
2167 }
2168
2169 static int tree_element_active_posegroup(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2170 {
2171         Object *ob= (Object *)tselem->id;
2172         
2173         if(set) {
2174                 if (ob->pose) {
2175                         ob->pose->active_group= te->index+1;
2176                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
2177                 }
2178         }
2179         else {
2180                 if(ob==OBACT && ob->pose) {
2181                         if (ob->pose->active_group== te->index+1) return 1;
2182                 }
2183         }
2184         return 0;
2185 }
2186
2187 static int tree_element_active_posechannel(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2188 {
2189         Object *ob= (Object *)tselem->id;
2190         bArmature *arm= ob->data;
2191         bPoseChannel *pchan= te->directdata;
2192         
2193         if(set) {
2194                 if(!(pchan->bone->flag & BONE_HIDDEN_P)) {
2195                         
2196                         if(set==2) ED_pose_deselectall(ob, 2);  // 2 = clear active tag
2197                         else ED_pose_deselectall(ob, 0);        // 0 = deselect 
2198                         
2199                         if(set==2 && (pchan->bone->flag & BONE_SELECTED)) {
2200                                 pchan->bone->flag &= ~BONE_SELECTED;
2201                         } else {
2202                                 pchan->bone->flag |= BONE_SELECTED;
2203                                 arm->act_bone= pchan->bone;
2204                         }
2205                         
2206                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, ob);
2207
2208                 }
2209         }
2210         else {
2211                 if(ob==OBACT && ob->pose) {
2212                         if (pchan->bone->flag & BONE_SELECTED) return 1;
2213                 }
2214         }
2215         return 0;
2216 }
2217
2218 static int tree_element_active_bone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2219 {
2220         bArmature *arm= (bArmature *)tselem->id;
2221         Bone *bone= te->directdata;
2222         
2223         if(set) {
2224                 if(!(bone->flag & BONE_HIDDEN_P)) {
2225                         if(set==2) ED_pose_deselectall(OBACT, 2);       // 2 is clear active tag
2226                         else ED_pose_deselectall(OBACT, 0);
2227                         
2228                         if(set==2 && (bone->flag & BONE_SELECTED)) {
2229                                 bone->flag &= ~BONE_SELECTED;
2230                         } else {
2231                                 bone->flag |= BONE_SELECTED;
2232                                 arm->act_bone= bone;
2233                         }
2234                         
2235                         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, OBACT);
2236                 }
2237         }
2238         else {
2239                 Object *ob= OBACT;
2240                 
2241                 if(ob && ob->data==arm) {
2242                         if (bone->flag & BONE_SELECTED) return 1;
2243                 }
2244         }
2245         return 0;
2246 }
2247
2248
2249 /* ebones only draw in editmode armature */
2250 static void tree_element_active_ebone__sel(bContext *C, Scene *scene, bArmature *arm, EditBone *ebone, short sel)
2251 {
2252         if(sel) {
2253                 ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL;
2254                 arm->act_edbone= ebone;
2255                 // flush to parent?
2256                 if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
2257         }
2258         else {
2259                 ebone->flag &= ~(BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL);
2260                 // flush to parent?
2261                 if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag &= ~BONE_TIPSEL;
2262         }
2263
2264         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, scene->obedit);
2265 }
2266 static int tree_element_active_ebone(bContext *C, Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
2267 {
2268         bArmature *arm= scene->obedit->data;
2269         EditBone *ebone= te->directdata;
2270
2271         if(set==1) {
2272                 if(!(ebone->flag & BONE_HIDDEN_A)) {
2273                         ED_armature_deselect_all(scene->obedit, 0);     // deselect
2274                         tree_element_active_ebone__sel(C, scene, arm, ebone, TRUE);
2275                         return 1;
2276                 }
2277         }
2278         else if (set==2) {
2279                 if(!(ebone->flag & BONE_HIDDEN_A)) {
2280                         if(!(ebone->flag & BONE_SELECTED)) {
2281                                 tree_element_active_ebone__sel(C, scene, arm, ebone, TRUE);
2282                                 return 1;
2283                         }
2284                         else {
2285                                 /* entirely selected, so de-select */
2286                                 tree_element_active_ebone__sel(C, scene, arm, ebone, FALSE);
2287                                 return 0;
2288                         }
2289                 }
2290         }
2291         else if (ebone->flag & BONE_SELECTED) {
2292                 return 1;
2293         }
2294         return 0;
2295 }
2296
2297 static int tree_element_active_modifier(bContext *C, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
2298 {
2299         if(set) {
2300                 Object *ob= (Object *)tselem->id;
2301                 
2302                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, ob);
2303
2304 // XXX          extern_set_butspace(F9KEY, 0);
2305         }
2306         
2307         return 0;
2308 }
2309
2310 static int tree_element_active_psys(bContext *C, Scene *UNUSED(scene), TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
2311 {
2312         if(set) {
2313                 Object *ob= (Object *)tselem->id;
2314                 
2315                 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
2316                 
2317 // XXX          extern_set_butspace(F7KEY, 0);
2318         }
2319         
2320         return 0;
2321 }
2322
2323 static int tree_element_active_constraint(bContext *C, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
2324 {
2325         if(set) {
2326                 Object *ob= (Object *)tselem->id;
2327                 
2328                 WM_event_add_notifier(C, NC_OBJECT|ND_CONSTRAINT, ob);
2329 // XXX          extern_set_butspace(F7KEY, 0);
2330         }
2331         
2332         return 0;
2333 }
2334
2335 static int tree_element_active_text(bContext *UNUSED(C), Scene *UNUSED(scene), SpaceOops *UNUSED(soops), TreeElement *UNUSED(te), int UNUSED(set))
2336 {
2337         // XXX removed
2338         return 0;
2339 }
2340
2341 /* generic call for ID data check or make/check active in UI */
2342 static int tree_element_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2343 {
2344
2345         switch(te->idcode) {
2346                 case ID_MA:
2347                         return tree_element_active_material(C, scene, soops, te, set);
2348                 case ID_WO:
2349                         return tree_element_active_world(C, scene, soops, te, set);
2350                 case ID_LA:
2351                         return tree_element_active_lamp(C, scene, soops, te, set);
2352                 case ID_TE:
2353                         return tree_element_active_texture(C, scene, soops, te, set);
2354                 case ID_TXT:
2355                         return tree_element_active_text(C, scene, soops, te, set);
2356                 case ID_CA:
2357                         return tree_element_active_camera(C, scene, soops, te, set);
2358         }
2359         return 0;
2360 }
2361
2362 static int tree_element_active_pose(bContext *C, Scene *scene, TreeElement *UNUSED(te), TreeStoreElem *tselem, int set)
2363 {
2364         Object *ob= (Object *)tselem->id;
2365         Base *base= object_in_scene(ob, scene);
2366         
2367         if(set) {
2368                 if(scene->obedit) 
2369                         ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO);
2370                 
2371                 if(ob->mode & OB_MODE_POSE) 
2372                         ED_armature_exit_posemode(C, base);
2373                 else 
2374                         ED_armature_enter_posemode(C, base);
2375         }
2376         else {
2377                 if(ob->mode & OB_MODE_POSE) return 1;
2378         }
2379         return 0;
2380 }
2381
2382 static int tree_element_active_sequence(TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
2383 {
2384         Sequence *seq= (Sequence*) te->directdata;
2385
2386         if(set) {
2387 // XXX          select_single_seq(seq, 1);
2388         }
2389         else {
2390                 if(seq->flag & SELECT)
2391                         return(1);
2392         }
2393         return(0);
2394 }
2395
2396 static int tree_element_active_sequence_dup(Scene *scene, TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
2397 {
2398         Sequence *seq, *p;
2399         Editing *ed= seq_give_editing(scene, FALSE);
2400
2401         seq= (Sequence*)te->directdata;
2402         if(set==0) {
2403                 if(seq->flag & SELECT)
2404                         return(1);
2405                 return(0);
2406         }
2407
2408 // XXX  select_single_seq(seq, 1);
2409         p= ed->seqbasep->first;
2410         while(p) {
2411                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
2412                         p= p->next;
2413                         continue;
2414                 }
2415
2416 //              if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
2417 // XXX                  select_single_seq(p, 0);
2418                 p= p->next;
2419         }
2420         return(0);
2421 }
2422
2423 static int tree_element_active_keymap_item(bContext *UNUSED(C), TreeElement *te, TreeStoreElem *UNUSED(tselem), int set)
2424 {
2425         wmKeyMapItem *kmi= te->directdata;
2426         
2427         if(set==0) {
2428                 if(kmi->flag & KMI_INACTIVE) return 0;
2429                 return 1;
2430         }
2431         else {
2432                 kmi->flag ^= KMI_INACTIVE;
2433         }
2434         return 0;
2435 }
2436
2437
2438 /* generic call for non-id data to make/check active in UI */
2439 /* Context can be NULL when set==0 */
2440 static int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
2441 {
2442         switch(tselem->type) {
2443                 case TSE_DEFGROUP:
2444                         return tree_element_active_defgroup(C, scene, te, tselem, set);
2445                 case TSE_BONE:
2446                         return tree_element_active_bone(C, scene, te, tselem, set);
2447                 case TSE_EBONE:
2448                         return tree_element_active_ebone(C, scene, te, tselem, set);
2449                 case TSE_MODIFIER:
2450                         return tree_element_active_modifier(C, te, tselem, set);
2451                 case TSE_LINKED_OB:
2452                         if(set) tree_element_set_active_object(C, scene, soops, te, set);
2453                         else if(tselem->id==(ID *)OBACT) return 1;
2454                         break;
2455                 case TSE_LINKED_PSYS:
2456                         return tree_element_active_psys(C, scene, te, tselem, set);
2457                 case TSE_POSE_BASE:
2458                         return tree_element_active_pose(C, scene, te, tselem, set);
2459                 case TSE_POSE_CHANNEL:
2460                         return tree_element_active_posechannel(C, scene, te, tselem, set);
2461                 case TSE_CONSTRAINT:
2462                         return tree_element_active_constraint(C, te, tselem, set);
2463                 case TSE_R_LAYER:
2464                         return tree_element_active_renderlayer(C, te, tselem, set);
2465                 case TSE_POSEGRP:
2466                         return tree_element_active_posegroup(C, scene, te, tselem, set);
2467                 case TSE_SEQUENCE:
2468                         return tree_element_active_sequence(te, tselem, set);
2469                 case TSE_SEQUENCE_DUP:
2470                         return tree_element_active_sequence_dup(scene, te, tselem, set);
2471                 case TSE_KEYMAP_ITEM:
2472                         return tree_element_active_keymap_item(C, te, tselem, set);
2473                         
2474         }
2475         return 0;
2476 }
2477
2478 static int do_outliner_item_activate(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, int extend, float *mval)
2479 {
2480         
2481         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2482                 TreeStoreElem *tselem= TREESTORE(te);
2483                 int openclose= 0;
2484
2485                 /* open close icon */
2486                 if((te->flag & TE_ICONROW)==0) {                                // hidden icon, no open/close
2487                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) 
2488                                 openclose= 1;
2489                 }
2490
2491                 if(openclose) {
2492                         /* all below close/open? */
2493                         if(extend) {
2494                                 tselem->flag &= ~TSE_CLOSED;
2495                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2496                         }
2497                         else {
2498                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2499                                 else tselem->flag |= TSE_CLOSED;
2500                                 
2501                         }
2502
2503                         return 1;
2504                 }
2505                 /* name and first icon */
2506                 else if(mval[0]>te->xs+OL_X && mval[0]<te->xend) {
2507                         
2508                         /* always makes active object */
2509                         if(tselem->type!=TSE_SEQUENCE && tselem->type!=TSE_SEQ_STRIP && tselem->type!=TSE_SEQUENCE_DUP)
2510                                 tree_element_set_active_object(C, scene, soops, te, 1 + (extend!=0 && tselem->type==0));
2511                         
2512                         if(tselem->type==0) { // the lib blocks
2513                                 /* editmode? */
2514                                 if(te->idcode==ID_SCE) {
2515                                         if(scene!=(Scene *)tselem->id) {
2516                                                 ED_screen_set_scene(C, (Scene *)tselem->id);
2517                                         }
2518                                 }
2519                                 else if(te->idcode==ID_GR) {
2520                                         Group *gr= (Group *)tselem->id;
2521                                         GroupObject *gob;
2522
2523                                         if(extend) {
2524                                                 int sel= BA_SELECT;
2525                                                 for(gob= gr->gobject.first; gob; gob= gob->next) {
2526                                                         if(gob->ob->flag & SELECT) {
2527                                                                 sel= BA_DESELECT;
2528                                                                 break;
2529                                                         }
2530                                                 }
2531
2532                                                 for(gob= gr->gobject.first; gob; gob= gob->next) {
2533                                                         ED_base_object_select(object_in_scene(gob->ob, scene), sel);
2534                                                 }
2535                                         }
2536                                         else {
2537                                                 scene_deselect_all(scene);
2538
2539                                                 for(gob= gr->gobject.first; gob; gob= gob->next) {
2540                                                         if((gob->ob->flag & SELECT) == 0)
2541                                                                 ED_base_object_select(object_in_scene(gob->ob, scene), BA_SELECT);
2542                                                 }
2543                                         }
2544
2545                                         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
2546                                 }
2547                                 else if(ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
2548                                         WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_INVOKE_REGION_WIN, NULL);
2549                                 } else {        // rest of types
2550                                         tree_element_active(C, scene, soops, te, 1);
2551                                 }
2552                                 
2553                         }
2554                         else tree_element_type_active(C, scene, soops, te, tselem, 1+(extend!=0));
2555
2556                         return 1;
2557                 }
2558         }
2559         
2560         for(te= te->subtree.first; te; te= te->next) {
2561                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, mval)) return 1;
2562         }
2563         return 0;
2564 }
2565
2566 /* event can enterkey, then it opens/closes */
2567 static int outliner_item_activate(bContext *C, wmOperator *op, wmEvent *event)
2568 {
2569         Scene *scene= CTX_data_scene(C);
2570         ARegion *ar= CTX_wm_region(C);
2571         SpaceOops *soops= CTX_wm_space_outliner(C);
2572         TreeElement *te;
2573         float fmval[2];
2574         int extend= RNA_boolean_get(op->ptr, "extend");
2575
2576         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2577
2578         if(!ELEM3(soops->outlinevis, SO_DATABLOCKS, SO_USERDEF, SO_KEYMAP) && !(soops->flag & SO_HIDE_RESTRICTCOLS) && fmval[0] > ar->v2d.cur.xmax - OL_TOG_RESTRICT_VIEWX)
2579                 return OPERATOR_CANCELLED;
2580
2581         for(te= soops->tree.first; te; te= te->next) {
2582                 if(do_outliner_item_activate(C, scene, ar, soops, te, extend, fmval)) break;
2583         }
2584         
2585         if(te) {
2586                 ED_undo_push(C, "Outliner click event");
2587         }
2588         else {
2589                 short selecting= -1;
2590                 int row;
2591                 
2592                 /* get row number - 100 here is just a dummy value since we don't need the column */
2593                 UI_view2d_listview_view_to_cell(&ar->v2d, 1000, OL_H, 0.0f, OL_Y_OFFSET, 
2594                                                 fmval[0], fmval[1], NULL, &row);
2595                 
2596                 /* select relevant row */
2597                 outliner_select(soops, &soops->tree, &row, &selecting);
2598                 
2599                 soops->storeflag |= SO_TREESTORE_REDRAW;
2600                 
2601                 ED_undo_push(C, "Outliner selection event");
2602         }
2603         
2604         ED_region_tag_redraw(ar);
2605
2606         return OPERATOR_FINISHED;
2607 }
2608
2609 void OUTLINER_OT_item_activate(wmOperatorType *ot)
2610 {
2611         ot->name= "Activate Item";
2612         ot->idname= "OUTLINER_OT_item_activate";
2613         ot->description= "Handle mouse clicks to activate/select items";
2614         
2615         ot->invoke= outliner_item_activate;
2616         
2617         ot->poll= ED_operator_outliner_active;
2618         
2619         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection for activation.");
2620 }
2621
2622 /* *********** */
2623
2624 static int do_outliner_item_openclose(bContext *C, SpaceOops *soops, TreeElement *te, int all, float *mval)
2625 {
2626         
2627         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2628                 TreeStoreElem *tselem= TREESTORE(te);
2629
2630                 /* all below close/open? */
2631                 if(all) {
2632                         tselem->flag &= ~TSE_CLOSED;
2633                         outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2634                 }
2635                 else {
2636                         if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2637                         else tselem->flag |= TSE_CLOSED;
2638                 }
2639
2640                 return 1;
2641         }
2642         
2643         for(te= te->subtree.first; te; te= te->next) {
2644                 if(do_outliner_item_openclose(C, soops, te, all, mval)) 
2645                         return 1;
2646         }
2647         return 0;
2648         
2649 }
2650
2651 /* event can enterkey, then it opens/closes */
2652 static int outliner_item_openclose(bContext *C, wmOperator *op, wmEvent *event)
2653 {
2654         ARegion *ar= CTX_wm_region(C);
2655         SpaceOops *soops= CTX_wm_space_outliner(C);
2656         TreeElement *te;
2657         float fmval[2];
2658         int all= RNA_boolean_get(op->ptr, "all");
2659         
2660         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2661         
2662         for(te= soops->tree.first; te; te= te->next) {
2663                 if(do_outliner_item_openclose(C, soops, te, all, fmval)) 
2664                         break;
2665         }
2666
2667         ED_region_tag_redraw(ar);
2668         
2669         return OPERATOR_FINISHED;
2670 }
2671
2672 void OUTLINER_OT_item_openclose(wmOperatorType *ot)
2673 {
2674         ot->name= "Open/Close Item";
2675         ot->idname= "OUTLINER_OT_item_openclose";
2676         ot->description= "Toggle whether item under cursor is enabled or closed";
2677         
2678         ot->invoke= outliner_item_openclose;
2679         
2680         ot->poll= ED_operator_outliner_active;
2681         
2682         RNA_def_boolean(ot->srna, "all", 1, "All", "Close or open all items.");
2683
2684 }
2685
2686
2687 /* ********************************************** */
2688
2689 static int do_outliner_item_rename(bContext *C, ARegion *ar, SpaceOops *soops, TreeElement *te, float *mval)
2690 {
2691         
2692         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2693                 TreeStoreElem *tselem= TREESTORE(te);
2694                 
2695                 /* name and first icon */
2696                 if(mval[0]>te->xs+OL_X && mval[0]<te->xend) {
2697                         
2698                         /* can't rename rna datablocks entries */
2699                         if(ELEM3(tselem->type, TSE_RNA_STRUCT, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM))
2700                            ;
2701                         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)) 
2702                                         error("Cannot edit builtin name");
2703                         else if(ELEM3(tselem->type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP))
2704                                 error("Cannot edit sequence name");
2705                         else if(tselem->id->lib) {
2706                                 // XXX                                          error_libdata();
2707                         } 
2708                         else if(te->idcode == ID_LI && te->parent) {
2709                                 error("Cannot edit the path of an indirectly linked library");
2710                         } 
2711                         else {
2712                                 tselem->flag |= TSE_TEXTBUT;
2713                                 ED_region_tag_redraw(ar);
2714                         }
2715                 }
2716                 return 1;
2717         }
2718         
2719         for(te= te->subtree.first; te; te= te->next) {
2720                 if(do_outliner_item_rename(C, ar, soops, te, mval)) return 1;
2721         }
2722         return 0;
2723 }
2724
2725 static int outliner_item_rename(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
2726 {
2727         ARegion *ar= CTX_wm_region(C);
2728         SpaceOops *soops= CTX_wm_space_outliner(C);
2729         TreeElement *te;
2730         float fmval[2];
2731         
2732         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2733         
2734         for(te= soops->tree.first; te; te= te->next) {
2735                 if(do_outliner_item_rename(C, ar, soops, te, fmval)) break;
2736         }
2737         
2738         return OPERATOR_FINISHED;
2739 }
2740
2741
2742 void OUTLINER_OT_item_rename(wmOperatorType *ot)
2743 {
2744         ot->name= "Rename Item";
2745         ot->idname= "OUTLINER_OT_item_rename";
2746         ot->description= "Rename item under cursor";
2747         
2748         ot->invoke= outliner_item_rename;
2749         
2750         ot->poll= ED_operator_outliner_active;
2751 }
2752
2753 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
2754 {
2755         TreeElement *te, *tes;
2756         TreeStoreElem *tselem;
2757         
2758         for(te= lb->first; te; te= te->next) {
2759                 tselem= TREESTORE(te);
2760                 if(tselem->type==0) {
2761                         if(tselem->id==id) return te;
2762                         /* only deeper on scene or object */
2763                         if( te->idcode==ID_OB || te->idcode==ID_SCE || (soops->outlinevis == SO_GROUPS && te->idcode==ID_GR)) {
2764                                 tes= outliner_find_id(soops, &te->subtree, id);
2765                                 if(tes) return tes;
2766                         }
2767                 }
2768         }
2769         return NULL;
2770 }
2771
2772 static int outliner_show_active_exec(bContext *C, wmOperator *UNUSED(op))
2773 {
2774         SpaceOops *so= CTX_wm_space_outliner(C);
2775         Scene *scene= CTX_data_scene(C);
2776         ARegion *ar= CTX_wm_region(C);
2777         View2D *v2d= &ar->v2d;
2778         
2779         TreeElement *te;
2780         int xdelta, ytop;
2781         
2782         // TODO: make this get this info from context instead...
2783         if (OBACT == NULL) 
2784                 return OPERATOR_CANCELLED;
2785         
2786         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
2787         if (te) {
2788                 /* make te->ys center of view */
2789                 ytop= (int)(te->ys + (v2d->mask.ymax - v2d->mask.ymin)/2);
2790                 if (ytop>0) ytop= 0;
2791                 
2792                 v2d->cur.ymax= (float)ytop;
2793                 v2d->cur.ymin= (float)(ytop-(v2d->mask.ymax - v2d->mask.ymin));
2794                 
2795                 /* make te->xs ==> te->xend center of view */
2796                 xdelta = (int)(te->xs - v2d->cur.xmin);
2797                 v2d->cur.xmin += xdelta;
2798                 v2d->cur.xmax += xdelta;
2799                 
2800                 so->storeflag |= SO_TREESTORE_REDRAW;
2801         }
2802         
2803         ED_region_tag_redraw(ar);
2804         
2805         return OPERATOR_FINISHED;
2806 }
2807
2808 void OUTLINER_OT_show_active(wmOperatorType *ot)
2809 {
2810         /* identifiers */
2811         ot->name= "Show Active";
2812         ot->idname= "OUTLINER_OT_show_active";
2813         ot->description= "Adjust the view so that the active Object is shown centered";
2814         
2815         /* callbacks */
2816         ot->exec= outliner_show_active_exec;
2817         ot->poll= ED_operator_outliner_active;
2818 }
2819
2820 /* tse is not in the treestore, we use its contents to find a match */
2821 static TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
2822 {
2823         TreeStore *ts= soops->treestore;
2824         TreeStoreElem *tselem;
2825         int a;
2826         
2827         if(tse->id==NULL) return NULL;
2828         
2829         /* check if 'tse' is in treestore */
2830         tselem= ts->data;
2831         for(a=0; a<ts->usedelem; a++, tselem++) {
2832                 if((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
2833                         if(tselem->id==tse->id) {
2834                                 break;
2835                         }
2836                 }
2837         }
2838         if(tselem) 
2839                 return outliner_find_tree_element(&soops->tree, a);
2840         
2841         return NULL;
2842 }
2843
2844
2845 /* Called to find an item based on name.
2846  */
2847 #if 0
2848
2849 /* recursive helper for function below */
2850 static void outliner_set_coordinates_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
2851 {
2852         TreeStoreElem *tselem= TREESTORE(te);
2853         
2854         /* store coord and continue, we need coordinates for elements outside view too */
2855         te->xs= (float)startx;
2856         te->ys= (float)(*starty);
2857         *starty-= OL_H;
2858         
2859         if((tselem->flag & TSE_CLOSED)==0) {
2860                 TreeElement *ten;
2861                 for(ten= te->subtree.first; ten; ten= ten->next) {
2862                         outliner_set_coordinates_element(soops, ten, startx+OL_X, starty);
2863                 }
2864         }
2865         
2866 }
2867
2868 /* to retrieve coordinates with redrawing the entire tree */
2869 static void outliner_set_coordinates(ARegion *ar, SpaceOops *soops)
2870 {
2871         TreeElement *te;
2872         int starty= (int)(ar->v2d.tot.ymax)-OL_H;
2873         int startx= 0;
2874         
2875         for(te= soops->tree.first; te; te= te->next) {
2876                 outliner_set_coordinates_element(soops, te, startx, &starty);
2877         }
2878 }
2879
2880 /* find next element that has this name */
2881 static TreeElement *outliner_find_named(SpaceOops *soops, ListBase *lb, char *name, int flags, TreeElement *prev, int *prevFound)
2882 {
2883         TreeElement *te, *tes;
2884         
2885         for (te= lb->first; te; te= te->next) {
2886                 int found = outliner_filter_has_name(te, name, flags);
2887                 
2888                 if(found) {
2889                         /* name is right, but is element the previous one? */
2890                         if (prev) {
2891                                 if ((te != prev) && (*prevFound)) 
2892                                         return te;
2893                                 if (te == prev) {
2894                                         *prevFound = 1;
2895                                 }
2896                         }
2897                         else
2898                                 return te;
2899                 }
2900                 
2901                 tes= outliner_find_named(soops, &te->subtree, name, flags, prev, prevFound);
2902                 if(tes) return tes;
2903         }
2904
2905         /* nothing valid found */
2906         return NULL;
2907 }
2908
2909 static void outliner_find_panel(Scene *UNUSED(scene), ARegion *ar, SpaceOops *soops, int again, int flags) 
2910 {
2911         TreeElement *te= NULL;
2912         TreeElement *last_find;
2913         TreeStoreElem *tselem;
2914         int ytop, xdelta, prevFound=0;
2915         char name[32];
2916         
2917         /* get last found tree-element based on stored search_tse */
2918         last_find= outliner_find_tse(soops, &soops->search_tse);
2919         
2920         /* determine which type of search to do */
2921         if (again && last_find) {
2922                 /* no popup panel - previous + user wanted to search for next after previous */         
2923                 BLI_strncpy(name, soops->search_string, sizeof(name));
2924                 flags= soops->search_flags;
2925                 
2926                 /* try to find matching element */
2927                 te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2928                 if (te==NULL) {
2929                         /* no more matches after previous, start from beginning again */
2930                         prevFound= 1;
2931                         te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2932                 }
2933         }
2934         else {
2935                 /* pop up panel - no previous, or user didn't want search after previous */
2936                 strcpy(name, "");
2937 // XXX          if (sbutton(name, 0, sizeof(name)-1, "Find: ") && name[0]) {
2938 //                      te= outliner_find_named(soops, &soops->tree, name, flags, NULL, &prevFound);
2939 //              }
2940 //              else return; /* XXX RETURN! XXX */
2941         }
2942
2943         /* do selection and reveal */
2944         if (te) {
2945                 tselem= TREESTORE(te);
2946                 if (tselem) {
2947                         /* expand branches so that it will be visible, we need to get correct coordinates */
2948                         if( outliner_open_back(soops, te))
2949                                 outliner_set_coordinates(ar, soops);
2950                         
2951                         /* deselect all visible, and select found element */
2952                         outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
2953                         tselem->flag |= TSE_SELECTED;
2954                         
2955                         /* make te->ys center of view */
2956                         ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2957                  &nbs