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