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