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