76ab56fb5748939e973f7acb7a89a836f7da8781
[blender.git] / source / blender / src / 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
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_action_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_constraint_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_camera_types.h"
40 #include "DNA_image_types.h"
41 #include "DNA_ipo_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_lamp_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_meta_types.h"
47 #include "DNA_modifier_types.h"
48 #include "DNA_nla_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_oops_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_screen_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_texture_types.h"
55 #include "DNA_text_types.h"
56 #include "DNA_world_types.h"
57
58 #include "BLI_blenlib.h"
59
60 #include "BKE_constraint.h"
61 #include "BKE_depsgraph.h"
62 #include "BKE_global.h"
63 #include "BKE_library.h"
64 #include "BKE_main.h"
65 #include "BKE_material.h"
66 #include "BKE_modifier.h"
67 #include "BKE_screen.h"
68 #include "BKE_utildefines.h"
69
70 #include "BIF_butspace.h"
71 #include "BIF_drawscene.h"
72 #include "BIF_drawtext.h"
73 #include "BIF_editaction.h"
74 #include "BIF_editarmature.h"
75 #include "BIF_editdeform.h"
76 #include "BIF_editnla.h"
77 #include "BIF_editview.h"
78 #include "BIF_editconstraint.h"
79 #include "BIF_gl.h"
80 #include "BIF_graphics.h"
81 #include "BIF_interface.h"
82 #include "BIF_mywindow.h"
83 #include "BIF_outliner.h"
84 #include "BIF_language.h"
85 #include "BIF_mainqueue.h"
86 #include "BIF_poseobject.h"
87 #include "BIF_previewrender.h"
88 #include "BIF_resources.h"
89 #include "BIF_screen.h"
90 #include "BIF_space.h"
91 #include "BIF_toolbox.h"
92
93 #ifdef INTERNATIONAL
94 #include "FTF_Api.h"
95 #endif
96
97 #include "BDR_editobject.h"
98 #include "BSE_drawipo.h"
99 #include "BSE_edit.h"
100
101 #include "PIL_time.h" 
102
103 #include "blendef.h"
104 #include "mydevice.h"
105
106 #define OL_H    19
107 #define OL_X    18
108
109 #define TS_CHUNK        128
110
111 #define TREESTORE(a) soops->treestore->data+(a)->store_index
112
113 /* ******************** PERSISTANT DATA ***************** */
114
115 static void outliner_storage_cleanup(SpaceOops *soops)
116 {
117         TreeStore *ts= soops->treestore;
118         
119         if(ts) {
120                 TreeStoreElem *tselem;
121                 int a, unused= 0;
122                 
123                 /* each element used once, for ID blocks with more users to have each a treestore */
124                 for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) tselem->used= 0;
125
126                 /* cleanup only after reading file or undo step */
127                 if(soops->storeflag & SO_TREESTORE_CLEANUP) {
128                         
129                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
130                                 if(tselem->id==NULL) unused++;
131                         }
132
133                         if(unused) {
134                                 if(ts->usedelem == unused) {
135                                         MEM_freeN(ts->data);
136                                         ts->data= NULL;
137                                         ts->usedelem= ts->totelem= 0;
138                                 }
139                                 else {
140                                         TreeStoreElem *tsnewar, *tsnew;
141                                         
142                                         tsnew=tsnewar= MEM_mallocN((ts->usedelem-unused)*sizeof(TreeStoreElem), "new tselem");
143                                         for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
144                                                 if(tselem->id) {
145                                                         *tsnew= *tselem;
146                                                         tsnew++;
147                                                 }
148                                         }
149                                         MEM_freeN(ts->data);
150                                         ts->data= tsnewar;
151                                         ts->usedelem-= unused;
152                                         ts->totelem= ts->usedelem;
153                                 }
154                         }
155                 }
156         }
157 }
158
159 static void check_persistant(SpaceOops *soops, TreeElement *te, ID *id, short type, short nr)
160 {
161         TreeStore *ts;
162         TreeStoreElem *tselem;
163         int a;
164         
165         /* case 1; no TreeStore */
166         if(soops->treestore==NULL) {
167                 ts= soops->treestore= MEM_callocN(sizeof(TreeStore), "treestore");
168         }
169         ts= soops->treestore;
170         
171         /* check if 'te' is in treestore */
172         tselem= ts->data;
173         for(a=0; a<ts->usedelem; a++, tselem++) {
174                 if(tselem->id==id && tselem->used==0) {
175                         if((type==0 && tselem->type==0) ||(tselem->type==type && tselem->nr==nr)) {
176                                 te->store_index= a;
177                                 tselem->used= 1;
178                                 return;
179                         }
180                 }
181         }
182         
183         /* add 1 element to treestore */
184         if(ts->usedelem==ts->totelem) {
185                 TreeStoreElem *tsnew;
186                 
187                 tsnew= MEM_mallocN((ts->totelem+TS_CHUNK)*sizeof(TreeStoreElem), "treestore data");
188                 if(ts->data) {
189                         memcpy(tsnew, ts->data, ts->totelem*sizeof(TreeStoreElem));
190                         MEM_freeN(ts->data);
191                 }
192                 ts->data= tsnew;
193                 ts->totelem+= TS_CHUNK;
194         }
195         
196         tselem= ts->data+ts->usedelem;
197         
198         tselem->type= type;
199         if(type) tselem->nr= nr; // we're picky! :)
200         else tselem->nr= 0;
201         tselem->id= id;
202         tselem->flag= TSE_CLOSED;
203         te->store_index= ts->usedelem;
204         
205         ts->usedelem++;
206 }
207
208 /* ******************** TREE MANAGEMENT ****************** */
209
210 void outliner_free_tree(ListBase *lb)
211 {
212         
213         while(lb->first) {
214                 TreeElement *te= lb->first;
215
216                 outliner_free_tree(&te->subtree);
217                 BLI_remlink(lb, te);
218                 MEM_freeN(te);
219         }
220 }
221
222 static void outliner_height(SpaceOops *soops, ListBase *lb, int *h)
223 {
224         TreeElement *te= lb->first;
225         while(te) {
226                 TreeStoreElem *tselem= TREESTORE(te);
227                 if((tselem->flag & TSE_CLOSED)==0) 
228                         outliner_height(soops, &te->subtree, h);
229                 (*h)++;
230                 te= te->next;
231         }
232 }
233
234 static TreeElement *outliner_find_tree_element(ListBase *lb, int store_index)
235 {
236         TreeElement *te= lb->first, *tes;
237         while(te) {
238                 if(te->store_index==store_index) return te;
239                 tes= outliner_find_tree_element(&te->subtree, store_index);
240                 if(tes) return tes;
241                 te= te->next;
242         }
243         return NULL;
244 }
245
246
247
248 static ID *outliner_search_back(SpaceOops *soops, TreeElement *te, short idcode)
249 {
250         TreeStoreElem *tselem;
251         te= te->parent;
252         
253         while(te) {
254                 tselem= TREESTORE(te);
255                 if(te->idcode==idcode && tselem->type==0) return tselem->id;
256                 te= te->parent;
257         }
258         return NULL;
259 }
260
261 struct treesort {
262         TreeElement *te;
263         ID *id;
264         char *name;
265         short idcode;
266 };
267
268 static int treesort_alpha(const void *v1, const void *v2)
269 {
270         const struct treesort *x1= v1, *x2= v2;
271         int comp;
272         
273         /* first put objects last (hierarchy) */
274         comp= (x1->idcode==ID_OB);
275         if(x2->idcode==ID_OB) comp+=2;
276         
277         if(comp==1) return 1;
278         else if(comp==2) return -1;
279         else if(comp==3) {
280                 int comp= strcmp(x1->name, x2->name);
281                 
282                 if( comp>0 ) return 1;
283                 else if( comp<0) return -1;
284                 return 0;
285         }
286         return 0;
287 }
288
289 /* this is nice option for later? doesnt look too useful... */
290 #if 0
291 static int treesort_obtype_alpha(const void *v1, const void *v2)
292 {
293         const struct treesort *x1= v1, *x2= v2;
294         
295         /* first put objects last (hierarchy) */
296         if(x1->idcode==ID_OB && x2->idcode!=ID_OB) return 1;
297         else if(x2->idcode==ID_OB && x1->idcode!=ID_OB) return -1;
298         else {
299                 /* 2nd we check ob type */
300                 if(x1->idcode==ID_OB && x2->idcode==ID_OB) {
301                         if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return 1;
302                         else if( ((Object *)x1->id)->type > ((Object *)x2->id)->type) return -1;
303                         else return 0;
304                 }
305                 else {
306                         int comp= strcmp(x1->name, x2->name);
307                         
308                         if( comp>0 ) return 1;
309                         else if( comp<0) return -1;
310                         return 0;
311                 }
312         }
313 }
314 #endif
315
316 /* sort happens on each subtree individual */
317 static void outliner_sort(SpaceOops *soops, ListBase *lb)
318 {
319         TreeElement *te;
320         TreeStoreElem *tselem;
321         int totelem=0;
322         
323         te= lb->last;
324         if(te==NULL) return;
325         tselem= TREESTORE(te);
326         
327         /* sorting rules; only object lists or deformgroups */
328         if( (tselem->type==TSE_DEFGROUP) || (tselem->type==0 && te->idcode==ID_OB)) {
329                 
330                 /* count first */
331                 for(te= lb->first; te; te= te->next) totelem++;
332                 
333                 if(totelem>1) {
334                         struct treesort *tear= MEM_mallocN(totelem*sizeof(struct treesort), "tree sort array");
335                         struct treesort *tp=tear;
336                         
337                         for(te= lb->first; te; te= te->next, tp++) {
338                                 tselem= TREESTORE(te);
339                                 tp->te= te;
340                                 tp->name= te->name;
341                                 tp->idcode= te->idcode;
342                                 if(tselem->type && tselem->type!=TSE_DEFGROUP) tp->idcode= 0;   // dont sort this
343                                 tp->id= tselem->id;
344                         }
345                         
346                         qsort(tear, totelem, sizeof(struct treesort), treesort_alpha);
347                         
348                         lb->first=lb->last= NULL;
349                         tp= tear;
350                         while(totelem--) {
351                                 BLI_addtail(lb, tp->te);
352                                 tp++;
353                         }
354                         MEM_freeN(tear);
355                 }
356         }
357         
358         for(te= lb->first; te; te= te->next) {
359                 outliner_sort(soops, &te->subtree);
360         }
361 }
362
363 /* Prototype, see function below */
364 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, 
365                                                           ID *id, Bone *curBone, TreeElement *parent, int *a);
366
367 static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *idv, 
368                                                                                  TreeElement *parent, short type, short index)
369 {
370         TreeElement *te;
371         TreeStoreElem *tselem;
372         ID *id= idv;
373         int a;
374         
375         if(id==NULL) return NULL;
376
377         te= MEM_callocN(sizeof(TreeElement), "tree elem");
378         /* add to the visual tree */
379         BLI_addtail(lb, te);
380         /* add to the storage */
381         check_persistant(soops, te, id, type, index);
382         tselem= TREESTORE(te);  
383         
384         te->parent= parent;
385         te->index= index;       // for data arays
386         te->name= id->name+2; // default, can be overridden by non-ID data
387         te->idcode= GS(id->name);
388         
389         if(type==0) {
390
391                 /* tuck pointer back in object, to construct hierarchy */
392                 if(GS(id->name)==ID_OB) id->newid= (ID *)te;
393                 
394                 /* expand specific data always */
395                 switch(GS(id->name)) {
396                 case ID_SCE:
397                         {
398                                 Scene *sce= (Scene *)id;
399                                 outliner_add_element(soops, &te->subtree, sce->world, te, 0, 0);
400                                 if(sce->scriptlink.scripts) {
401                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, sce, te, TSE_SCRIPT_BASE, 0);
402                                         int a= 0;
403                                         tenla->name= "Scripts";
404                                         for (a=0; a<sce->scriptlink.totscript; a++) {
405                                                 outliner_add_element(soops, &tenla->subtree, sce->scriptlink.scripts[a], tenla, 0, 0);
406                                         }
407                                 }
408                         }
409                         break;
410                 case ID_OB:
411                         {
412                                 Object *ob= (Object *)id;
413                                 
414                                 outliner_add_element(soops, &te->subtree, ob->data, te, 0, 0);
415                                 
416                                 if(ob->pose) {
417                                         bPoseChannel *pchan;
418                                         TreeElement *ten;
419                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_POSE_BASE, 0);
420                                         
421                                         tenla->name= "Pose";
422                                         
423                                         if(ob!=G.obedit) {      // channels undefined in editmode, but we want the 'tenla' pose icon itself
424                                                 int a= 0;
425                                                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next, a++) {
426                                                         ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_POSE_CHANNEL, a);
427                                                         ten->name= pchan->name;
428                                                         ten->directdata= pchan;
429                                                         pchan->prev= (bPoseChannel *)ten;
430                                                         
431                                                         if(pchan->constraints.first) {
432                                                                 Object *target;
433                                                                 bConstraint *con;
434                                                                 TreeElement *ten1;
435                                                                 TreeElement *tenla1= outliner_add_element(soops, &ten->subtree, ob, ten, TSE_CONSTRAINT_BASE, 0);
436                                                                 int a= 0;
437                                                                 char *str;
438                                                                 
439                                                                 tenla1->name= "Constraints";
440                                                                 for(con= pchan->constraints.first; con; con= con->next, a++) {
441                                                                         ten1= outliner_add_element(soops, &tenla1->subtree, ob, tenla1, TSE_CONSTRAINT, a);
442                                                                         target= get_constraint_target(con, &str);
443                                                                         if(str && str[0]) ten1->name= str;
444                                                                         else if(target) ten1->name= target->id.name+2;
445                                                                         else ten1->name= con->name;
446                                                                         ten1->directdata= con;
447                                                                         /* possible add all other types links? */
448                                                                 }
449                                                         }
450                                                 }
451                                                 /* make hierarchy */
452                                                 ten= tenla->subtree.first;
453                                                 while(ten) {
454                                                         TreeElement *nten= ten->next, *par;
455                                                         tselem= TREESTORE(ten);
456                                                         if(tselem->type==TSE_POSE_CHANNEL) {
457                                                                 pchan= (bPoseChannel *)ten->directdata;
458                                                                 if(pchan->parent) {
459                                                                         BLI_remlink(&tenla->subtree, ten);
460                                                                         par= (TreeElement *)pchan->parent->prev;
461                                                                         BLI_addtail(&par->subtree, ten);
462                                                                 }
463                                                         }
464                                                         ten= nten;
465                                                 }
466                                                 /* restore prev pointers */
467                                                 pchan= ob->pose->chanbase.first;
468                                                 if(pchan) pchan->prev= NULL;
469                                                 for(; pchan; pchan= pchan->next) {
470                                                         if(pchan->next) pchan->next->prev= pchan;
471                                                 }
472                                         }
473                                 }
474                                 
475                                 outliner_add_element(soops, &te->subtree, ob->ipo, te, 0, 0);
476                                 outliner_add_element(soops, &te->subtree, ob->action, te, 0, 0);
477                                 
478                                 for(a=0; a<ob->totcol; a++) 
479                                         outliner_add_element(soops, &te->subtree, ob->mat[a], te, 0, a);
480                                 
481                                 if(ob->constraints.first) {
482                                         Object *target;
483                                         bConstraint *con;
484                                         TreeElement *ten;
485                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_CONSTRAINT_BASE, 0);
486                                         int a= 0;
487                                         char *str;
488                                         
489                                         tenla->name= "Constraints";
490                                         for(con= ob->constraints.first; con; con= con->next, a++) {
491                                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_CONSTRAINT, a);
492                                                 target= get_constraint_target(con, &str);
493                                                 if(str && str[0]) ten->name= str;
494                                                 else if(target) ten->name= target->id.name+2;
495                                                 else ten->name= con->name;
496                                                 ten->directdata= con;
497                                                 /* possible add all other types links? */
498                                         }
499                                 }
500                                 
501                                 if(ob->modifiers.first) {
502                                         ModifierData *md;
503                                         TreeElement *temod = outliner_add_element(soops, &te->subtree, ob, te, TSE_MODIFIER_BASE, 0);
504                                         int index;
505
506                                         temod->name = "Modifiers";
507                                         for (index=0,md=ob->modifiers.first; md; index++,md=md->next) {
508                                                 TreeElement *te = outliner_add_element(soops, &temod->subtree, ob, temod, TSE_MODIFIER, index);
509                                                 te->name= md->name;
510
511                                                 if (md->type==eModifierType_Lattice) {
512                                                         outliner_add_element(soops, &te->subtree, ((LatticeModifierData*) md)->object, te, TSE_MODIFIER_OB, 0);
513                                                 } else if (md->type==eModifierType_Curve) {
514                                                         outliner_add_element(soops, &te->subtree, ((CurveModifierData*) md)->object, te, TSE_MODIFIER_OB, 0);
515                                                 } else if (md->type==eModifierType_Armature) {
516                                                         outliner_add_element(soops, &te->subtree, ((ArmatureModifierData*) md)->object, te, TSE_MODIFIER_OB, 0);
517                                                 } else if (md->type==eModifierType_Hook) {
518                                                         outliner_add_element(soops, &te->subtree, ((HookModifierData*) md)->object, te, TSE_MODIFIER_OB, 0);
519                                                 }
520                                         }
521                                 }
522                                 if(ob->defbase.first) {
523                                         bDeformGroup *defgroup;
524                                         TreeElement *ten;
525                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_DEFGROUP_BASE, 0);
526                                         int a= 0;
527                                         
528                                         tenla->name= "Vertex Groups";
529                                         for (defgroup=ob->defbase.first; defgroup; defgroup=defgroup->next, a++) {
530                                                 ten= outliner_add_element(soops, &tenla->subtree, ob, tenla, TSE_DEFGROUP, a);
531                                                 ten->name= defgroup->name;
532                                                 ten->directdata= defgroup;
533                                         }
534                                 }
535                                 if(ob->scriptlink.scripts) {
536                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_SCRIPT_BASE, 0);
537                                         int a= 0;
538                                         
539                                         tenla->name= "Scripts";
540                                         for (a=0; a<ob->scriptlink.totscript; a++) {                                                    /*  ** */
541                                                 outliner_add_element(soops, &tenla->subtree, ob->scriptlink.scripts[a], te, 0, 0);
542                                         }
543                                 }
544                                 if(ob->nlastrips.first) {
545                                         bActionStrip *strip;
546                                         TreeElement *ten;
547                                         TreeElement *tenla= outliner_add_element(soops, &te->subtree, ob, te, TSE_NLA, 0);
548                                         int a= 0;
549                                         
550                                         tenla->name= "NLA strips";
551                                         for (strip=ob->nlastrips.first; strip; strip=strip->next, a++) {
552                                                 ten= outliner_add_element(soops, &tenla->subtree, strip->act, tenla, TSE_NLA_ACTION, a);
553                                                 if(ten) ten->directdata= strip;
554                                         }
555                                 }
556                                 
557                         }
558                         break;
559                 case ID_ME:
560                         {
561                                 Mesh *me= (Mesh *)id;
562                                 outliner_add_element(soops, &te->subtree, me->ipo, te, 0, 0);
563                                 outliner_add_element(soops, &te->subtree, me->key, te, 0, 0);
564                                 for(a=0; a<me->totcol; a++) 
565                                         outliner_add_element(soops, &te->subtree, me->mat[a], te, 0, a);
566                                 /* could do tfaces with image links, but the images are not grouped nicely.
567                                    would require going over all tfaces, sort images in use. etc... */
568                         }
569                         break;
570                 case ID_CU:
571                         {
572                                 Curve *cu= (Curve *)id;
573                                 for(a=0; a<cu->totcol; a++) 
574                                         outliner_add_element(soops, &te->subtree, cu->mat[a], te, 0, a);
575                         }
576                         break;
577                 case ID_MB:
578                         {
579                                 MetaBall *mb= (MetaBall *)id;
580                                 for(a=0; a<mb->totcol; a++) 
581                                         outliner_add_element(soops, &te->subtree, mb->mat[a], te, 0, a);
582                         }
583                         break;
584                 case ID_MA:
585                 {
586                         Material *ma= (Material *)id;
587                         
588                         outliner_add_element(soops, &te->subtree, ma->ipo, te, 0, 0);
589                         for(a=0; a<MAX_MTEX; a++) {
590                                 if(ma->mtex[a]) outliner_add_element(soops, &te->subtree, ma->mtex[a]->tex, te, 0, a);
591                         }
592                 }
593                         break;
594                 case ID_TE:
595                         {
596                                 Tex *tex= (Tex *)id;
597                                 
598                                 outliner_add_element(soops, &te->subtree, tex->ipo, te, 0, 0);
599                                 outliner_add_element(soops, &te->subtree, tex->ima, te, 0, 0);
600                         }
601                         break;
602                 case ID_CA:
603                         {
604                                 Camera *ca= (Camera *)id;
605                                 outliner_add_element(soops, &te->subtree, ca->ipo, te, 0, 0);
606                         }
607                         break;
608                 case ID_LA:
609                         {
610                                 Lamp *la= (Lamp *)id;
611                                 outliner_add_element(soops, &te->subtree, la->ipo, te, 0, 0);
612                                 for(a=0; a<MAX_MTEX; a++) {
613                                         if(la->mtex[a]) outliner_add_element(soops, &te->subtree, la->mtex[a]->tex, te, 0, a);
614                                 }
615                         }
616                         break;
617                 case ID_WO:
618                         {
619                                 World *wrld= (World *)id;
620                                 outliner_add_element(soops, &te->subtree, wrld->ipo, te, 0, 0);
621                                 for(a=0; a<MAX_MTEX; a++) {
622                                         if(wrld->mtex[a]) outliner_add_element(soops, &te->subtree, wrld->mtex[a]->tex, te, 0, a);
623                                 }
624                         }
625                         break;
626                 case ID_KE:
627                         {
628                                 Key *key= (Key *)id;
629                                 outliner_add_element(soops, &te->subtree, key->ipo, te, 0, 0);
630                         }
631                         break;
632                 case ID_AC:
633                         {
634                                 bAction *act= (bAction *)id;
635                                 bActionChannel *chan;
636                                 int a= 0;
637                                 
638                                 tselem= TREESTORE(parent);
639                                 for (chan=act->chanbase.first; chan; chan=chan->next, a++) {
640                                         outliner_add_element(soops, &te->subtree, chan->ipo, te, 0, a);
641                                 }
642                         }
643                         break;
644                 case ID_AR:
645                         {
646                                 bArmature *arm= (bArmature *)id;
647                                 int a= 0;
648                                 
649                                 if(G.obedit && G.obedit->data==arm) {
650                                         EditBone *ebone;
651                                         TreeElement *ten;
652                                         
653                                         for (ebone = G.edbo.first; ebone; ebone=ebone->next, a++) {
654                                                 ten= outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
655                                                 ten->directdata= ebone;
656                                                 ten->name= ebone->name;
657                                                 ebone->temp= ten;
658                                         }
659                                         /* make hierarchy */
660                                         ten= te->subtree.first;
661                                         while(ten) {
662                                                 TreeElement *nten= ten->next, *par;
663                                                 ebone= (EditBone *)ten->directdata;
664                                                 if(ebone->parent) {
665                                                         BLI_remlink(&te->subtree, ten);
666                                                         par= ebone->parent->temp;
667                                                         BLI_addtail(&par->subtree, ten);
668                                                 }
669                                                 ten= nten;
670                                         }
671                                 }
672                                 else {
673                                         Bone *curBone;
674                                         for (curBone=arm->bonebase.first; curBone; curBone=curBone->next){
675                                                 outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
676                                         }
677                                 }
678                         }
679                         break;
680                 }
681         }
682         return te;
683 }
684
685
686 /* special handling of hierarchical non-lib data */
687 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone, 
688                                                           TreeElement *parent, int *a)
689 {
690         TreeElement *te= outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
691         
692         (*a)++;
693         te->name= curBone->name;
694         te->directdata= curBone;
695         
696         for(curBone= curBone->childbase.first; curBone; curBone=curBone->next) {
697                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
698         }
699 }
700
701 static void outliner_make_hierarchy(SpaceOops *soops, ListBase *lb)
702 {
703         TreeElement *te, *ten, *tep;
704         TreeStoreElem *tselem;
705
706         /* build hierarchy */
707         te= lb->first;
708         while(te) {
709                 ten= te->next;
710                 tselem= TREESTORE(te);
711                 
712                 if(tselem->type==0 && te->idcode==ID_OB) {
713                         Object *ob= (Object *)tselem->id;
714                         if(ob->parent && ob->parent->id.newid) {
715                                 BLI_remlink(lb, te);
716                                 tep= (TreeElement *)ob->parent->id.newid;
717                                 BLI_addtail(&tep->subtree, te);
718                                 // set correct parent pointers
719                                 for(te=tep->subtree.first; te; te= te->next) te->parent= tep;
720                         }
721                 }
722                 te= ten;
723         }
724 }
725
726 static void outliner_build_tree(SpaceOops *soops)
727 {
728         Scene *sce;
729         Base *base;
730         Object *ob;
731         TreeElement *te, *ten;
732         TreeStoreElem *tselem;
733         
734         outliner_free_tree(&soops->tree);
735         outliner_storage_cleanup(soops);
736                                                    
737         /* clear ob id.new flags */
738         for(ob= G.main->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
739         
740         /* option 1: all scenes */
741         if(soops->outlinevis == SO_ALL_SCENES) {
742                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
743                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
744                         tselem= TREESTORE(te);
745
746                         for(base= sce->base.first; base; base= base->next) {
747                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
748                                 ten->directdata= base;
749                         }
750                         outliner_make_hierarchy(soops, &te->subtree);
751                         /* clear id.newid, to prevent objects be inserted in wrong scenes (parent in other scene) */
752                         for(base= sce->base.first; base; base= base->next) base->object->id.newid= NULL;
753                 }
754         }
755         else if(soops->outlinevis == SO_CUR_SCENE) {
756                 outliner_add_element(soops, &soops->tree, G.scene->world, NULL, 0, 0);
757                 
758                 for(base= G.scene->base.first; base; base= base->next) {
759                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
760                         ten->directdata= base;
761                 }
762                 outliner_make_hierarchy(soops, &soops->tree);
763         }
764         else if(soops->outlinevis == SO_VISIBLE) {
765                 for(base= G.scene->base.first; base; base= base->next) {
766                         if(base->lay & G.scene->lay)
767                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
768                 }
769                 outliner_make_hierarchy(soops, &soops->tree);
770         }
771         else if(soops->outlinevis == SO_SAME_TYPE) {
772                 Object *ob= OBACT;
773                 if(ob) {
774                         for(base= G.scene->base.first; base; base= base->next) {
775                                 if(base->object->type==ob->type) {
776                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
777                                         ten->directdata= base;
778                                 }
779                         }
780                         outliner_make_hierarchy(soops, &soops->tree);
781                 }
782         }
783         else if(soops->outlinevis == SO_SELECTED) {
784                 for(base= G.scene->base.first; base; base= base->next) {
785                         if(base->lay & G.scene->lay) {
786                                 if(base==BASACT || (base->flag & SELECT)) {
787                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
788                                         ten->directdata= base;
789                                 }
790                         }
791                 }
792                 outliner_make_hierarchy(soops, &soops->tree);
793         }
794         else {
795                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
796                 if(ten) ten->directdata= BASACT;
797         }
798         
799         outliner_sort(soops, &soops->tree);
800 }
801
802 /* **************** INTERACTIVE ************* */
803
804 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
805 {
806         TreeElement *te;
807         int level=curlevel, lev;
808         
809         for(te= lb->first; te; te= te->next) {
810                 
811                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
812                 if(lev>level) level= lev;
813         }
814         return level;
815 }
816
817 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
818 {
819         TreeElement *te;
820         TreeStoreElem *tselem;
821         int level;
822         
823         for(te= lb->first; te; te= te->next) {
824                 tselem= TREESTORE(te);
825                 if(tselem->flag & flag) return curlevel;
826                 
827                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
828                 if(level) return level;
829         }
830         return 0;
831 }
832
833 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
834 {
835         TreeElement *te;
836         TreeStoreElem *tselem;
837         
838         for(te= lb->first; te; te= te->next) {
839                 tselem= TREESTORE(te);
840                 if(set==0) tselem->flag &= ~flag;
841                 else tselem->flag |= flag;
842                 outliner_set_flag(soops, &te->subtree, flag, set);
843         }
844 }
845
846 void outliner_toggle_visible(struct ScrArea *sa)
847 {
848         SpaceOops *soops= sa->spacedata.first;
849         
850         if( outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
851                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
852         else 
853                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
854
855         BIF_undo_push("Outliner toggle visible");
856         scrarea_queue_redraw(sa);
857 }
858
859 void outliner_toggle_selected(struct ScrArea *sa)
860 {
861         SpaceOops *soops= sa->spacedata.first;
862         
863         if( outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
864                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
865         else 
866                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
867         
868         BIF_undo_push("Outliner toggle selected");
869         scrarea_queue_redraw(sa);
870 }
871
872
873 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
874 {
875         TreeElement *te;
876         TreeStoreElem *tselem;
877         
878         for(te= lb->first; te; te= te->next) {
879                 tselem= TREESTORE(te);
880                 
881                 if(open) {
882                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
883                 }
884                 else {
885                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
886                 }
887                 
888                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
889         }
890 }
891
892 void outliner_one_level(struct ScrArea *sa, int add)
893 {
894         SpaceOops *soops= sa->spacedata.first;
895         int level;
896         
897         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
898         if(add==1) {
899                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
900         }
901         else {
902                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
903                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
904         }
905         
906         BIF_undo_push("Outliner show/hide one level");
907         scrarea_queue_redraw(sa);
908 }
909
910 void outliner_page_up_down(ScrArea *sa, int up)
911 {
912         SpaceOops *soops= sa->spacedata.first;
913         int dy= soops->v2d.mask.ymax-soops->v2d.mask.ymin;
914         
915         if(up == -1) dy= -dy;
916         soops->v2d.cur.ymin+= dy;
917         soops->v2d.cur.ymax+= dy;
918         
919         scrarea_queue_redraw(sa);
920 }
921
922 /* **** do clicks on items ******* */
923
924 static void tree_element_active_object(SpaceOops *soops, TreeElement *te)
925 {
926         TreeStoreElem *tselem= TREESTORE(te);
927         Scene *sce;
928         Base *base;
929         Object *ob= NULL;
930         
931         /* if id is not object, we search back */
932         if(te->idcode==ID_OB) ob= (Object *)tselem->id;
933         else {
934                 ob= (Object *)outliner_search_back(soops, te, ID_OB);
935                 if(ob==OBACT) return;
936         }
937         if(ob==NULL) return;
938         
939         sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
940         if(sce && G.scene != sce) {
941                 if(G.obedit) exit_editmode(2);
942                 set_scene(sce);
943         }
944         
945         /* find associated base in current scene */
946         for(base= FIRSTBASE; base; base= base->next) 
947                 if(base->object==ob) break;
948         if(base) {
949                 if(G.qual & LR_SHIFTKEY) {
950                         /* swap select */
951                         if(base->flag & SELECT) base->flag &= ~SELECT;
952                         else base->flag |= SELECT;
953                         base->object->flag= base->flag;
954                 }
955                 else {
956                         Base *b;
957                         /* deleselect all */
958                         for(b= FIRSTBASE; b; b= b->next) {
959                                 b->flag &= ~SELECT;
960                                 b->object->flag= b->flag;
961                         }
962                         base->flag |= SELECT;
963                         base->object->flag |= SELECT;
964                 }
965                 set_active_base(base);  /* editview.c */
966                 
967                 allqueue(REDRAWVIEW3D, 1);
968                 allqueue(REDRAWOOPS, 0);
969                 allqueue(REDRAWINFO, 1);
970         }
971         
972         if(ob!=G.obedit) exit_editmode(2);
973 }
974
975 static int tree_element_active_material(SpaceOops *soops, TreeElement *te, int set)
976 {
977         TreeElement *tes;
978         Object *ob;
979         
980         /* we search for the object parent */
981         ob= (Object *)outliner_search_back(soops, te, ID_OB);
982         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
983         
984         /* searching in ob mat array? */
985         tes= te->parent;
986         if(tes->idcode==ID_OB) {
987                 if(set) {
988                         ob->actcol= te->index+1;
989                         ob->colbits |= (1<<te->index);  // make ob material active too
990                 }
991                 else {
992                         if(ob->actcol == te->index+1) 
993                                 if(ob->colbits & (1<<te->index)) return 1;
994                 }
995         }
996         /* or we search for obdata material */
997         else {
998                 if(set) {
999                         ob->actcol= te->index+1;
1000                         ob->colbits &= ~(1<<te->index); // make obdata material active too
1001                 }
1002                 else {
1003                         if(ob->actcol == te->index+1)
1004                                 if( (ob->colbits & (1<<te->index))==0 ) return 1;
1005                 }
1006         }
1007         if(set) {
1008                 extern_set_butspace(F5KEY);     // force shading buttons
1009                 BIF_all_preview_changed();
1010                 allqueue(REDRAWBUTSSHADING, 1);
1011                 allqueue(REDRAWOOPS, 0);
1012                 allqueue(REDRAWIPO, 0);
1013         }
1014         return 0;
1015 }
1016
1017 static int tree_element_active_texture(SpaceOops *soops, TreeElement *te, int set)
1018 {
1019         TreeElement *tep;
1020         TreeStoreElem *tselem, *tselemp;
1021         Object *ob=OBACT;
1022         ScrArea *sa;
1023         SpaceButs *sbuts=NULL;
1024         
1025         if(ob==NULL) return 0; // no active object
1026         
1027         tselem= TREESTORE(te);
1028         
1029         /* find buttons area (note, this is undefined really still, needs recode in blender) */
1030         sa= G.curscreen->areabase.first;
1031         while(sa) {
1032                 if(sa->spacetype==SPACE_BUTS) break;
1033                 sa= sa->next;
1034         }
1035         if(sa) sbuts= sa->spacedata.first;
1036         
1037         /* where is texture linked to? */
1038         tep= te->parent;
1039         tselemp= TREESTORE(tep);
1040         
1041         if(tep->idcode==ID_WO) {
1042                 World *wrld= (World *)tselemp->id;
1043
1044                 if(set) {
1045                         if(sbuts) {
1046                                 sbuts->tabo= TAB_SHADING_TEX;   // hack from header_buttonswin.c
1047                                 sbuts->texfrom= 1;
1048                         }
1049                         extern_set_butspace(F6KEY);     // force shading buttons texture
1050                         wrld->texact= te->index;
1051                 }
1052                 else if(tselemp->id == (ID *)(G.scene->world)) {
1053                         if(wrld->texact==te->index) return 1;
1054                 }
1055         }
1056         else if(tep->idcode==ID_LA) {
1057                 Lamp *la= (Lamp *)tselemp->id;
1058                 if(set) {
1059                         if(sbuts) {
1060                                 sbuts->tabo= TAB_SHADING_TEX;   // hack from header_buttonswin.c
1061                                 sbuts->texfrom= 2;
1062                         }
1063                         extern_set_butspace(F6KEY);     // force shading buttons texture
1064                         la->texact= te->index;
1065                 }
1066                 else {
1067                         if(tselemp->id == ob->data) {
1068                                 if(la->texact==te->index) return 1;
1069                         }
1070                 }
1071         }
1072         else if(tep->idcode==ID_MA) {
1073                 Material *ma= (Material *)tselemp->id;
1074                 if(set) {
1075                         if(sbuts) {
1076                                 //sbuts->tabo= TAB_SHADING_TEX; // hack from header_buttonswin.c
1077                                 sbuts->texfrom= 0;
1078                         }
1079                         extern_set_butspace(F6KEY);     // force shading buttons texture
1080                         ma->texact= te->index;
1081                         
1082                         /* also set active material */
1083                         ob->actcol= tep->index+1;
1084                 }
1085                 else if(tep->flag & TE_ACTIVE) {        // this is active material
1086                         if(ma->texact==te->index) return 1;
1087                 }
1088         }
1089         
1090         return 0;
1091 }
1092
1093
1094 static int tree_element_active_lamp(SpaceOops *soops, TreeElement *te, int set)
1095 {
1096         Object *ob;
1097         
1098         /* we search for the object parent */
1099         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1100         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1101         
1102         if(set) {
1103                 extern_set_butspace(F5KEY);
1104                 BIF_all_preview_changed();
1105                 allqueue(REDRAWBUTSSHADING, 1);
1106                 allqueue(REDRAWOOPS, 0);
1107                 allqueue(REDRAWIPO, 0);
1108         }
1109         else return 1;
1110         
1111         return 0;
1112 }
1113
1114 static int tree_element_active_world(SpaceOops *soops, TreeElement *te, int set)
1115 {
1116         TreeElement *tep;
1117         TreeStoreElem *tselem=NULL;
1118         Scene *sce=NULL;
1119         
1120         tep= te->parent;
1121         if(tep) {
1122                 tselem= TREESTORE(tep);
1123                 sce= (Scene *)tselem->id;
1124         }
1125         
1126         if(set) {       // make new scene active
1127                 if(sce && G.scene != sce) {
1128                         if(G.obedit) exit_editmode(2);
1129                         set_scene(sce);
1130                 }
1131         }
1132         
1133         if(tep==NULL || tselem->id == (ID *)G.scene) {
1134                 if(set) {
1135                         extern_set_butspace(F8KEY);
1136                 }
1137                 else {
1138                         return 1;
1139                 }
1140         }
1141         return 0;
1142 }
1143
1144 static int tree_element_active_ipo(SpaceOops *soops, TreeElement *te, int set)
1145 {
1146         TreeElement *tes;
1147         TreeStoreElem *tselems=NULL;
1148         Object *ob;
1149         
1150         /* we search for the object parent */
1151         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1152         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1153         
1154         /* the parent of ipo */
1155         tes= te->parent;
1156         tselems= TREESTORE(tes);
1157         
1158         if(set) {
1159                 if(tes->idcode==ID_AC) {
1160                         if(ob->ipoflag & OB_ACTION_OB)
1161                                 ob->ipowin= ID_OB;
1162                         else if(ob->ipoflag & OB_ACTION_KEY)
1163                                 ob->ipowin= ID_KE;
1164                         else 
1165                                 ob->ipowin= ID_PO;
1166                 }
1167                 else ob->ipowin= tes->idcode;
1168                 
1169                 if(ob->ipowin==ID_MA) tree_element_active_material(soops, tes, 1);
1170                 else if(ob->ipowin==ID_AC) {
1171                         bActionChannel *chan;
1172                         short a=0;
1173                         for(chan=ob->action->chanbase.first; chan; chan= chan->next) {
1174                                 if(a==te->index) break;
1175                                 if(chan->ipo) a++;
1176                         }
1177                         deselect_actionchannels(ob->action, 0);
1178                         select_channel(ob->action, chan, SELECT_ADD);
1179                         allqueue(REDRAWACTION, ob->ipowin);
1180                         allqueue(REDRAWVIEW3D, ob->ipowin);
1181                 }
1182                 
1183                 allqueue(REDRAWIPO, ob->ipowin);
1184         }
1185         else {
1186                 if(tes->idcode==ID_AC) {
1187                         if(ob->ipoflag & OB_ACTION_OB)
1188                                 return ob->ipowin==ID_OB;
1189                         else if(ob->ipoflag & OB_ACTION_KEY)
1190                                 return ob->ipowin==ID_KE;
1191                         else if(ob->ipowin==ID_AC) {
1192                                 bActionChannel *chan;
1193                                 short a=0;
1194                                 for(chan=ob->action->chanbase.first; chan; chan= chan->next) {
1195                                         if(a==te->index) break;
1196                                         if(chan->ipo) a++;
1197                                 }
1198                                 if(chan==get_hilighted_action_channel(ob->action)) return 1;
1199                         }
1200                 }
1201                 else if(ob->ipowin==tes->idcode) {
1202                         if(ob->ipowin==ID_MA) {
1203                                 Material *ma= give_current_material(ob, ob->actcol);
1204                                 if(ma==(Material *)tselems->id) return 1;
1205                         }
1206                         else return 1;
1207                 }
1208         }
1209         return 0;
1210 }       
1211
1212 static int tree_element_active_defgroup(TreeElement *te, TreeStoreElem *tselem, int set)
1213 {
1214         Object *ob;
1215         
1216         /* id in tselem is object */
1217         ob= (Object *)tselem->id;
1218         if(set) {
1219                 ob->actdef= te->index+1;
1220                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1221                 allqueue(REDRAWVIEW3D, ob->ipowin);
1222         }
1223         else {
1224                 if(ob==OBACT)
1225                         if(ob->actdef== te->index+1) return 1;
1226         }
1227         return 0;
1228 }
1229
1230 static int tree_element_active_nla_action(TreeElement *te, TreeStoreElem *tselem, int set)
1231 {
1232         if(set) {
1233                 bActionStrip *strip= te->directdata;
1234                 if(strip) {
1235                         deselect_nlachannel_keys(0);
1236                         strip->flag |= ACTSTRIP_SELECT;
1237                         allqueue(REDRAWNLA, 0);
1238                 }
1239         }
1240         else {
1241                 /* id in tselem is action */
1242                 bActionStrip *strip= te->directdata;
1243                 if(strip) {
1244                         if(strip->flag & ACTSTRIP_SELECT) return 1;
1245                 }
1246         }
1247         return 0;
1248 }
1249
1250 static int tree_element_active_posechannel(TreeElement *te, TreeStoreElem *tselem, int set)
1251 {
1252         Object *ob= (Object *)tselem->id;
1253         bPoseChannel *pchan= te->directdata;
1254         
1255         if(set) {
1256                 if(!(pchan->bone->flag & BONE_HIDDEN_P)) {
1257                         
1258                         if(G.qual & LR_SHIFTKEY) deselectall_posearmature(ob, 2);       // 2 = clear active tag
1259                         else deselectall_posearmature(ob, 0);   // 0 = deselect 
1260                         pchan->bone->flag |= BONE_SELECTED|BONE_ACTIVE;
1261                         
1262                         allqueue(REDRAWVIEW3D, 0);
1263                         allqueue(REDRAWOOPS, 0);
1264                         allqueue(REDRAWACTION, 0);
1265                 }
1266         }
1267         else {
1268                 if(ob==OBACT && ob->pose) {
1269                         if (pchan->bone->flag & BONE_SELECTED) return 1;
1270                 }
1271         }
1272         return 0;
1273 }
1274
1275 static int tree_element_active_bone(TreeElement *te, TreeStoreElem *tselem, int set)
1276 {
1277         bArmature *arm= (bArmature *)tselem->id;
1278         Bone *bone= te->directdata;
1279         
1280         if(set) {
1281                 if(!(bone->flag & BONE_HIDDEN_P)) {
1282                         if(G.qual & LR_SHIFTKEY) deselectall_posearmature(OBACT, 2);    // 2 is clear active tag
1283                         else deselectall_posearmature(OBACT, 0);
1284                         bone->flag |= BONE_SELECTED|BONE_ACTIVE;
1285                         
1286                         allqueue(REDRAWVIEW3D, 0);
1287                         allqueue(REDRAWOOPS, 0);
1288                         allqueue(REDRAWACTION, 0);
1289                 }
1290         }
1291         else {
1292                 Object *ob= OBACT;
1293                 
1294                 if(ob && ob->data==arm) {
1295                         if (bone->flag & BONE_SELECTED) return 1;
1296                 }
1297         }
1298         return 0;
1299 }
1300
1301
1302 /* ebones only draw in editmode armature */
1303 static int tree_element_active_ebone(TreeElement *te, TreeStoreElem *tselem, int set)
1304 {
1305         EditBone *ebone= te->directdata;
1306         
1307         if(set) {
1308                 if(!(ebone->flag & BONE_HIDDEN_A)) {
1309                         
1310                         if(G.qual & LR_SHIFTKEY) deselectall_armature(2);       // only clear active tag
1311                         else deselectall_armature(0);   // deselect
1312
1313                         ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL|BONE_ACTIVE;
1314                         // flush to parent?
1315                         if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
1316                         
1317                         allqueue(REDRAWVIEW3D, 0);
1318                         allqueue(REDRAWOOPS, 0);
1319                         allqueue(REDRAWACTION, 0);
1320                 }
1321         }
1322         else {
1323                 if (ebone->flag & BONE_SELECTED) return 1;
1324         }
1325         return 0;
1326 }
1327
1328 static int tree_element_active_modifier(TreeElement *te, TreeStoreElem *tselem, int set)
1329 {
1330         if(set) {
1331                 extern_set_butspace(F9KEY);
1332         }
1333         
1334         return 0;
1335 }
1336
1337 static int tree_element_active_text(SpaceOops *soops, TreeElement *te, int set)
1338 {
1339         ScrArea *sa=NULL;
1340         
1341         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1342                 if(sa->spacetype==SPACE_TEXT) break;
1343         }
1344         if(sa) {
1345                 SpaceText *st= sa->spacedata.first;
1346                 TreeStoreElem *tselem= TREESTORE(te);
1347                 
1348                 if(set) {
1349                         st->text= (Text *)tselem->id;
1350                         st->top= 0;
1351                         scrarea_queue_redraw(sa);
1352                 }
1353                 else if(st->text==(Text *)tselem->id) return 1;
1354         }
1355         return 0;
1356 }
1357
1358 /* generic call for ID data check or make/check active in UI */
1359 static int tree_element_active(SpaceOops *soops, TreeElement *te, int set)
1360 {
1361
1362         switch(te->idcode) {
1363                 case ID_MA:
1364                         return tree_element_active_material(soops, te, set);
1365                 case ID_WO:
1366                         return tree_element_active_world(soops, te, set);
1367                 case ID_LA:
1368                         return tree_element_active_lamp(soops, te, set);
1369                 case ID_IP:
1370                         return tree_element_active_ipo(soops, te, set);
1371                 case ID_TE:
1372                         return tree_element_active_texture(soops, te, set);
1373                 case ID_TXT:
1374                         return tree_element_active_text(soops, te, set);
1375         }
1376         return 0;
1377 }
1378
1379 static int tree_element_active_pose(TreeElement *te, TreeStoreElem *tselem, int set)
1380 {
1381         Object *ob= (Object *)tselem->id;
1382         
1383         if(set) {
1384                 if(G.obedit) exit_editmode(2);
1385                 if(ob->flag & OB_POSEMODE) exit_posemode();
1386                 else enter_posemode();
1387         }
1388         else {
1389                 if(ob->flag & OB_POSEMODE) return 1;
1390         }
1391         return 0;
1392 }
1393
1394 /* generic call for non-id data to make/check active in UI */
1395 static int tree_element_type_active(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
1396 {
1397         
1398         switch(tselem->type) {
1399                 case TSE_NLA_ACTION:
1400                         return tree_element_active_nla_action(te, tselem, set);
1401                 case TSE_DEFGROUP:
1402                         return tree_element_active_defgroup(te, tselem, set);
1403                 case TSE_BONE:
1404                         return tree_element_active_bone(te, tselem, set);
1405                 case TSE_EBONE:
1406                         return tree_element_active_ebone(te, tselem, set);
1407                 case TSE_MODIFIER:
1408                         return tree_element_active_modifier(te, tselem, set);
1409                 case TSE_MODIFIER_OB:
1410                         if(set) tree_element_active_object(soops, te);
1411                         else if(tselem->id==(ID *)OBACT) return 1;
1412                         break;
1413                 case TSE_POSE_BASE:
1414                         return tree_element_active_pose(te, tselem, set);
1415                         break;
1416                 case TSE_POSE_CHANNEL:
1417                         return tree_element_active_posechannel(te, tselem, set);
1418                         break;
1419         }
1420         return 0;
1421 }
1422
1423
1424 static int do_outliner_mouse_event(SpaceOops *soops, TreeElement *te, short event, float *mval)
1425 {
1426         
1427         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
1428                 TreeStoreElem *tselem= TREESTORE(te);
1429                 int openclose= 0;
1430
1431                 /* open close icon, three things to check */
1432                 if(event==RETKEY || event==PADENTER) openclose= 1; // enter opens/closes always
1433                 else if((te->flag & TE_ICONROW)==0) {                           // hidden icon, no open/close
1434                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) openclose= 1;
1435                 }
1436
1437                 if(openclose) {
1438                         
1439                         /* all below close/open? */
1440                         if( (G.qual & LR_SHIFTKEY) ) {
1441                                 tselem->flag &= ~TSE_CLOSED;
1442                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
1443                         }
1444                         else {
1445                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
1446                                 else tselem->flag |= TSE_CLOSED;
1447                                 
1448                         }
1449                         
1450                         return 1;
1451                 }
1452                 /* name and first icon */
1453                 else if(mval[0]>te->xs && mval[0]<te->xend) {
1454                         
1455                         /* activate a name button? */
1456                         if(G.qual & LR_CTRLKEY) {
1457                                 if(ELEM5(tselem->type, TSE_NLA, TSE_DEFGROUP_BASE, TSE_CONSTRAINT_BASE, TSE_MODIFIER_BASE, TSE_SCRIPT_BASE)) 
1458                                         error("Cannot edit builtin name");
1459                                 else {
1460                                         tselem->flag |= TSE_TEXTBUT;
1461                                 }
1462                         }
1463                         else {
1464                                 /* always makes active object */
1465                                 tree_element_active_object(soops, te);
1466                                 
1467                                 if(tselem->type==0) { // the lib blocks
1468                                         /* editmode? */
1469                                         if(te->idcode==ID_SCE) {
1470                                                 if(G.scene!=(Scene *)tselem->id) {
1471                                                         if(G.obedit) exit_editmode(2);
1472                                                         set_scene((Scene *)tselem->id);
1473                                                 }
1474                                         }
1475                                         else if(ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
1476                                                 if(G.obedit) exit_editmode(2);
1477                                                 else {
1478                                                         enter_editmode();
1479                                                         extern_set_butspace(F9KEY);
1480                                                 }
1481                                         }
1482                                         else {  // rest of types
1483                                                 tree_element_active(soops, te, 1);
1484                                         }
1485                                         
1486                                 }
1487                                 else tree_element_type_active(soops, te, tselem, 1);
1488                         }
1489                         return 1;
1490                 }
1491         }
1492         
1493         for(te= te->subtree.first; te; te= te->next) {
1494                 if(do_outliner_mouse_event(soops, te, event, mval)) return 1;
1495         }
1496         return 0;
1497 }
1498
1499 /* event can enterkey, then it opens/closes */
1500 void outliner_mouse_event(ScrArea *sa, short event)
1501 {
1502         SpaceOops *soops= sa->spacedata.first;
1503         TreeElement *te;
1504         float fmval[2];
1505         short mval[2];
1506         
1507         getmouseco_areawin(mval);
1508         areamouseco_to_ipoco(&soops->v2d, mval, fmval, fmval+1);
1509         
1510         for(te= soops->tree.first; te; te= te->next) {
1511                 if(do_outliner_mouse_event(soops, te, event, fmval)) break;
1512         }
1513         
1514         if(te) {
1515                 BIF_undo_push("Outliner click event");
1516                 allqueue(REDRAWOOPS, 0);
1517         }
1518         else 
1519                 outliner_select(sa);
1520
1521 }
1522
1523 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
1524 {
1525         TreeElement *te, *tes;
1526         TreeStoreElem *tselem;
1527         
1528         for(te= lb->first; te; te= te->next) {
1529                 tselem= TREESTORE(te);
1530                 if(tselem->id==id) return te;
1531                 /* only deeper on scene or object */
1532                 if( te->idcode==ID_OB || te->idcode==ID_SCE) { 
1533                         tes= outliner_find_id(soops, &te->subtree, id);
1534                         if(tes) return tes;
1535                 }
1536         }
1537         return NULL;
1538 }
1539
1540 void outliner_show_active(struct ScrArea *sa)
1541 {
1542         SpaceOops *so= sa->spacedata.first;
1543         TreeElement *te;
1544         int ytop;
1545         
1546         if(OBACT == NULL) return;
1547         
1548         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
1549         if(te) {
1550                 /* make te->ys center of view */
1551                 ytop= te->ys + (so->v2d.mask.ymax-so->v2d.mask.ymin)/2;
1552                 if(ytop>0) ytop= 0;
1553                 so->v2d.cur.ymax= ytop;
1554                 so->v2d.cur.ymin= ytop-(so->v2d.mask.ymax-so->v2d.mask.ymin);
1555                 scrarea_queue_redraw(sa);
1556         }
1557 }
1558
1559 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
1560 {
1561         TreeElement *te;
1562         TreeStoreElem *tselem;
1563         
1564         for(te= lb->first; te; te= te->next) {
1565                 tselem= TREESTORE(te);
1566                 if(tselem->type==0 && te->idcode==ID_OB) return 1;
1567                 if( subtree_has_objects(soops, &te->subtree)) return 1;
1568         }
1569         return 0;
1570 }
1571
1572 static void tree_element_show_hierarchy(SpaceOops *soops, ListBase *lb)
1573 {
1574         TreeElement *te;
1575         TreeStoreElem *tselem;
1576
1577         /* open all object elems, close others */
1578         for(te= lb->first; te; te= te->next) {
1579                 tselem= TREESTORE(te);
1580                 
1581                 if(tselem->type==0) {
1582                         if(te->idcode==ID_SCE) {
1583                                 if(tselem->id!=(ID *)G.scene) tselem->flag |= TSE_CLOSED;
1584                                         else tselem->flag &= ~TSE_CLOSED;
1585                         }
1586                         else if(te->idcode==ID_OB) {
1587                                 if(subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
1588                                 else tselem->flag |= TSE_CLOSED;
1589                         }
1590                 }
1591                 else tselem->flag |= TSE_CLOSED;
1592
1593                 if(tselem->flag & TSE_CLOSED); else tree_element_show_hierarchy(soops, &te->subtree);
1594         }
1595         
1596 }
1597
1598 /* show entire object level hierarchy */
1599 void outliner_show_hierarchy(struct ScrArea *sa)
1600 {
1601         SpaceOops *so= sa->spacedata.first;
1602         
1603         tree_element_show_hierarchy(so, &so->tree);
1604         scrarea_queue_redraw(sa);
1605         
1606         BIF_undo_push("Outliner show hierarchy");
1607 }
1608
1609 static void do_outliner_select(SpaceOops *soops, ListBase *lb, float y1, float y2, short *selecting)
1610 {
1611         TreeElement *te;
1612         TreeStoreElem *tselem;
1613         
1614         if(y1>y2) SWAP(float, y1, y2);
1615         
1616         for(te= lb->first; te; te= te->next) {
1617                 tselem= TREESTORE(te);
1618                 
1619                 if(te->ys + OL_H < y1) return;
1620                 if(te->ys < y2) {
1621                         if((te->flag & TE_ICONROW)==0) {
1622                                 if(*selecting == -1) {
1623                                         if( tselem->flag & TSE_SELECTED) *selecting= 0;
1624                                         else *selecting= 1;
1625                                 }
1626                                 if(*selecting) tselem->flag |= TSE_SELECTED;
1627                                 else tselem->flag &= ~TSE_SELECTED;
1628                         }
1629                 }
1630                 if((tselem->flag & TSE_CLOSED)==0) do_outliner_select(soops, &te->subtree, y1, y2, selecting);
1631         }
1632 }
1633
1634 /* its own redraw loop... urm */
1635 void outliner_select(struct ScrArea *sa )
1636 {
1637         SpaceOops *so= sa->spacedata.first;
1638         float fmval[2], y1, y2;
1639         short mval[2], yo=-1, selecting= -1;
1640         
1641         getmouseco_areawin(mval);
1642         areamouseco_to_ipoco(&so->v2d, mval, fmval, fmval+1);
1643         y1= fmval[1];
1644
1645         while (get_mbut() & (L_MOUSE|R_MOUSE)) {
1646                 getmouseco_areawin(mval);
1647                 areamouseco_to_ipoco(&so->v2d, mval, fmval, fmval+1);
1648                 y2= fmval[1];
1649                 if(yo!=mval[1]) {
1650                         do_outliner_select(so, &so->tree, y1, y2, &selecting);
1651                         yo= mval[1];
1652                         scrarea_do_windraw(sa);
1653                         screen_swapbuffers();
1654                 
1655                         y1= y2;
1656                 }
1657                 else PIL_sleep_ms(30);
1658         }
1659         
1660         BIF_undo_push("Outliner selection");
1661
1662 }
1663
1664 /* ************ SELECTION OPERATIONS ********* */
1665
1666 static int scenelevel=0, objectlevel=0, idlevel=0, datalevel=0; // globals, euh... you can do better
1667
1668 static void set_operation_types(SpaceOops *soops, ListBase *lb)
1669 {
1670         TreeElement *te;
1671         TreeStoreElem *tselem;
1672         
1673         for(te= lb->first; te; te= te->next) {
1674                 tselem= TREESTORE(te);
1675                 if(tselem->flag & TSE_SELECTED) {
1676                         if(tselem->type) {
1677                                 if(datalevel==0) datalevel= tselem->type;
1678                                 else if(datalevel!=tselem->type) datalevel= -1;
1679                         }
1680                         else {
1681                                 int idcode= GS(tselem->id->name);
1682                                 switch(idcode) {
1683                                         case ID_SCE:
1684                                                 scenelevel= 1;
1685                                                 break;
1686                                         case ID_OB:
1687                                                 objectlevel= 1;
1688                                                 break;
1689                                                 
1690                                         case ID_ME: case ID_CU: case ID_MB: case ID_LT:
1691                                         case ID_LA: case ID_AR: case ID_CA:
1692                                                 idlevel= -2;
1693                                                 break;
1694                                                 
1695                                         case ID_MA: case ID_TE: case ID_IP: case ID_IM:
1696                                         case ID_SO: case ID_KE: case ID_WO: case ID_AC:
1697                                         case ID_NLA: case ID_TXT:
1698                                                 if(idlevel==0) idlevel= idcode;
1699                                                 else if(idlevel!=idcode) idlevel= -1;
1700                                                         break;
1701                                 }
1702                         }
1703                 }
1704                 if((tselem->flag & TSE_CLOSED)==0) set_operation_types(soops, &te->subtree);
1705         }
1706 }
1707
1708 static void unlink_material_cb(TreeElement *te, TreeStoreElem *tsep)
1709 {
1710         Material **matar=NULL;
1711         int a, totcol=0;
1712         
1713         if( GS(tsep->id->name)==ID_OB) {
1714                 Object *ob= (Object *)tsep->id;
1715                 totcol= ob->totcol;
1716                 matar= ob->mat;
1717         }
1718         else if( GS(tsep->id->name)==ID_ME) {
1719                 Mesh *me= (Mesh *)tsep->id;
1720                 totcol= me->totcol;
1721                 matar= me->mat;
1722         }
1723         else if( GS(tsep->id->name)==ID_CU) {
1724                 Curve *cu= (Curve *)tsep->id;
1725                 totcol= cu->totcol;
1726                 matar= cu->mat;
1727         }
1728         else if( GS(tsep->id->name)==ID_MB) {
1729                 MetaBall *mb= (MetaBall *)tsep->id;
1730                 totcol= mb->totcol;
1731                 matar= mb->mat;
1732         }
1733
1734         for(a=0; a<totcol; a++) {
1735                 if(a==te->index && matar[a]) {
1736                         matar[a]->id.us--;
1737                         matar[a]= NULL;
1738                 }
1739         }
1740 }
1741
1742 static void unlink_texture_cb(TreeElement *te, TreeStoreElem *tsep)
1743 {
1744         MTex **mtex= NULL;
1745         int a;
1746         
1747         if( GS(tsep->id->name)==ID_MA) {
1748                 Material *ma= (Material *)tsep->id;
1749                 mtex= ma->mtex;
1750         }
1751         else if( GS(tsep->id->name)==ID_LA) {
1752                 Lamp *la= (Lamp *)tsep->id;
1753                 mtex= la->mtex;
1754         }
1755         else if( GS(tsep->id->name)==ID_WO) {
1756                 World *wrld= (World *)tsep->id;
1757                 mtex= wrld->mtex;
1758         }
1759         else return;
1760         
1761         for(a=0; a<MAX_MTEX; a++) {
1762                 if(a==te->index && mtex[a]) {
1763                         if(mtex[a]->tex) {
1764                                 mtex[a]->tex->id.us--;
1765                                 mtex[a]->tex= NULL;
1766                         }
1767                 }
1768         }
1769 }
1770
1771 static void outliner_do_libdata_operation(SpaceOops *soops, ListBase *lb, 
1772                                                                                  void (*operation_cb)(TreeElement *, TreeStoreElem *))
1773 {
1774         TreeElement *te;
1775         TreeStoreElem *tselem;
1776         
1777         for(te=lb->first; te; te= te->next) {
1778                 tselem= TREESTORE(te);
1779                 if(tselem->flag & TSE_SELECTED) {
1780                         if(tselem->type==0) {
1781                                 TreeStoreElem *tsep= TREESTORE(te->parent);
1782                                 operation_cb(te, tsep);
1783                         }
1784                 }
1785                 if((tselem->flag & TSE_CLOSED)==0) {
1786                         outliner_do_libdata_operation(soops, &te->subtree, operation_cb);
1787                 }
1788         }
1789 }
1790
1791 /* */
1792
1793 static void object_select_cb(TreeElement *te, TreeStoreElem *tselem)
1794 {
1795         Base *base= (Base *)te->directdata;
1796         
1797         base->flag |= SELECT;
1798         base->object->flag |= SELECT;
1799 }
1800
1801 static void object_deselect_cb(TreeElement *te, TreeStoreElem *tselem)
1802 {
1803         Base *base= (Base *)te->directdata;
1804         
1805         base->flag &= ~SELECT;
1806         base->object->flag &= ~SELECT;
1807 }
1808
1809 static void object_delete_cb(TreeElement *te, TreeStoreElem *tselem)
1810 {
1811         Base *base= (Base *)te->directdata;
1812         
1813         if(base) {
1814                 // check also library later
1815                 if(G.obedit==base->object) exit_editmode(2);
1816                 
1817                 if(base==BASACT) {
1818                         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
1819                         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1820                 }
1821                 
1822                 free_and_unlink_base(base);
1823                 te->directdata= NULL;
1824                 tselem->id= NULL;
1825         }
1826 }
1827
1828
1829 static void outliner_do_object_operation(SpaceOops *soops, ListBase *lb, 
1830                                                                                  void (*operation_cb)(TreeElement *, TreeStoreElem *))
1831 {
1832         TreeElement *te;
1833         TreeStoreElem *tselem;
1834         
1835         for(te=lb->first; te; te= te->next) {
1836                 tselem= TREESTORE(te);
1837                 if(tselem->flag & TSE_SELECTED) {
1838                         if(tselem->type==0 && te->idcode==ID_OB) {
1839                                 // when objects selected in other scenes... dunno if that should be allowed
1840                                 Scene *sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
1841                                 if(sce && G.scene != sce) set_scene(sce);
1842                                 
1843                                 operation_cb(te, tselem);
1844                         }
1845                 }
1846                 if((tselem->flag & TSE_CLOSED)==0) {
1847                         outliner_do_object_operation(soops, &te->subtree, operation_cb);
1848                 }
1849         }
1850 }
1851
1852 static void pchan_cb(int event, TreeElement *te, TreeStoreElem *tselem)
1853 {
1854         bPoseChannel *pchan= (bPoseChannel *)te->directdata;
1855         
1856         if(event==1)
1857                 pchan->bone->flag |= BONE_SELECTED;
1858         else if(event==2)
1859                 pchan->bone->flag &= ~BONE_SELECTED;
1860         else if(event==3) {
1861                 pchan->bone->flag |= BONE_HIDDEN_P;
1862                 pchan->bone->flag &= ~BONE_SELECTED;
1863         }
1864         else if(event==4)
1865                 pchan->bone->flag &= ~BONE_HIDDEN_P;
1866 }
1867
1868 static void bone_cb(int event, TreeElement *te, TreeStoreElem *tselem)
1869 {
1870         Bone *bone= (Bone *)te->directdata;
1871         
1872         if(event==1)
1873                 bone->flag |= BONE_SELECTED;
1874         else if(event==2)
1875                 bone->flag &= ~BONE_SELECTED;
1876         else if(event==3) {
1877                 bone->flag |= BONE_HIDDEN_P;
1878                 bone->flag &= ~BONE_SELECTED;
1879         }
1880         else if(event==4)
1881                 bone->flag &= ~BONE_HIDDEN_P;
1882 }
1883
1884 static void ebone_cb(int event, TreeElement *te, TreeStoreElem *tselem)
1885 {
1886         EditBone *ebone= (EditBone *)te->directdata;
1887         
1888         if(event==1)
1889                 ebone->flag |= BONE_SELECTED;
1890         else if(event==2)
1891                 ebone->flag &= ~BONE_SELECTED;
1892         else if(event==3) {
1893                 ebone->flag |= BONE_HIDDEN_A;
1894                 ebone->flag &= ~BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
1895         }
1896         else if(event==4)
1897                 ebone->flag &= ~BONE_HIDDEN_A;
1898 }
1899
1900 static void outliner_do_data_operation(SpaceOops *soops, int type, int event, ListBase *lb, 
1901                                                                                  void (*operation_cb)(int, TreeElement *, TreeStoreElem *))
1902 {
1903         TreeElement *te;
1904         TreeStoreElem *tselem;
1905         
1906         for(te=lb->first; te; te= te->next) {
1907                 tselem= TREESTORE(te);
1908                 if(tselem->flag & TSE_SELECTED) {
1909                         if(tselem->type==type) {
1910                                 operation_cb(event, te, tselem);
1911                         }
1912                 }
1913                 if((tselem->flag & TSE_CLOSED)==0) {
1914                         outliner_do_data_operation(soops, type, event, &te->subtree, operation_cb);
1915                 }
1916         }
1917 }
1918
1919
1920 void outliner_operation_menu(ScrArea *sa)
1921 {
1922         SpaceOops *soops= sa->spacedata.first;
1923         
1924         // globals
1925         scenelevel= objectlevel= idlevel= datalevel=0;
1926         
1927         set_operation_types(soops, &soops->tree);
1928         
1929         if(scenelevel) {
1930                 if(objectlevel || datalevel || idlevel) error("Mixed selection");
1931                 //else pupmenu("Scene Operations%t|Delete");
1932         }
1933         else if(objectlevel) {
1934                 short event= pupmenu("Object Operations%t|Select%x1|Deselect%x2|Delete%x4");
1935                 if(event>0) {
1936                         char *str="";
1937                         
1938                         if(event==1) {
1939                                 Scene *sce= G.scene;    // to be able to delete, scenes are set...
1940                                 outliner_do_object_operation(soops, &soops->tree, object_select_cb);
1941                                 if(G.scene != sce) set_scene(sce);
1942                                 
1943                                 str= "Select Objects";
1944                         }
1945                         else if(event==2) {
1946                                 outliner_do_object_operation(soops, &soops->tree, object_deselect_cb);
1947                                 str= "Deselect Objects";
1948                         }
1949                         else if(event==4) {
1950                                 outliner_do_object_operation(soops, &soops->tree, object_delete_cb);
1951                                 DAG_scene_sort(G.scene);
1952                                 str= "Delete Objects";
1953                         }
1954                         
1955                         countall();
1956                         
1957                         BIF_undo_push(str);
1958                         allqueue(REDRAWALL, 0); // yah... to be sure :)
1959                 }
1960         }
1961         else if(idlevel) {
1962                 if(idlevel==-1 || datalevel) error("Mixed selection");
1963                 else if(idlevel==-2) error("No operations available");
1964                 else {
1965                         short event= pupmenu("Data Operations%t|Unlink");
1966                         
1967                         if(event==1) {
1968                                 switch(idlevel) {
1969                                         case ID_MA:
1970                                                 outliner_do_libdata_operation(soops, &soops->tree, unlink_material_cb);
1971                                                 BIF_undo_push("Unlink material");
1972                                                 allqueue(REDRAWBUTSSHADING, 1);
1973                                                 break;
1974                                         case ID_TE:
1975                                                 outliner_do_libdata_operation(soops, &soops->tree, unlink_texture_cb);
1976                                                 allqueue(REDRAWBUTSSHADING, 1);
1977                                                 BIF_undo_push("Unlink texture");
1978                                                 break;
1979                                         default:
1980                                                 error("Not yet...");
1981                                 }
1982                                 allqueue(REDRAWOOPS, 0);
1983                                 allqueue(REDRAWBUTSALL, 0);
1984                                 allqueue(REDRAWVIEW3D, 0);
1985                         }
1986                 }
1987         }
1988         else if(datalevel) {
1989                 if(datalevel==-1) error("Mixed selection");
1990                 else {
1991                         if(datalevel==TSE_POSE_CHANNEL) {
1992                                 short event= pupmenu("PoseChannel Operations%t|Select%x1|Deselect%x2|Hide%x3|Unhide%x4");
1993                                 if(event>0) {
1994                                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, pchan_cb);
1995                                         BIF_undo_push("PoseChannel operation");
1996                                 }
1997                         }
1998                         else if(datalevel==TSE_BONE) {
1999                                 short event= pupmenu("Bone Operations%t|Select%x1|Deselect%x2|Hide%x3|Unhide%x4");
2000                                 if(event>0) {
2001                                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, bone_cb);
2002                                         BIF_undo_push("Bone operation");
2003                                 }
2004                         }
2005                         else if(datalevel==TSE_EBONE) {
2006                                 short event= pupmenu("EditBone Operations%t|Select%x1|Deselect%x2|Hide%x3|Unhide%x4");
2007                                 if(event>0) {
2008                                         outliner_do_data_operation(soops, datalevel, event, &soops->tree, ebone_cb);
2009                                         BIF_undo_push("EditBone operation");
2010                                 }
2011                         }
2012                         
2013                         allqueue(REDRAWOOPS, 0);
2014                         allqueue(REDRAWBUTSALL, 0);
2015                         allqueue(REDRAWVIEW3D, 0);
2016                 }
2017         }
2018 }
2019
2020
2021 /* ***************** DRAW *************** */
2022
2023 static void tselem_draw_icon(float x, float y, TreeStoreElem *tselem, TreeElement *te)
2024 {
2025         if(tselem->type) {
2026                 switch( tselem->type) {
2027                         case TSE_NLA:
2028                                 BIF_draw_icon(x, y, ICON_NLA); break;
2029                         case TSE_NLA_ACTION:
2030                                 BIF_draw_icon(x, y, ICON_ACTION); break;
2031                         case TSE_DEFGROUP_BASE:
2032                                 BIF_draw_icon(x, y, ICON_VERTEXSEL); break;
2033                         case TSE_BONE:
2034                         case TSE_EBONE:
2035                                 BIF_draw_icon(x, y, ICON_WPAINT_DEHLT); break;
2036                         case TSE_CONSTRAINT_BASE:
2037                                 BIF_draw_icon(x, y, ICON_CONSTRAINT); break;
2038                         case TSE_MODIFIER_BASE:
2039                                 BIF_draw_icon(x, y, ICON_MODIFIER); break;
2040                         case TSE_MODIFIER_OB:
2041                                 BIF_draw_icon(x, y, ICON_OBJECT); break;
2042                         case TSE_MODIFIER:
2043                         {
2044                                 Object *ob= (Object *)tselem->id;
2045                                 ModifierData *md= BLI_findlink(&ob->modifiers, tselem->nr);
2046                                 switch(md->type) {
2047                                         case eModifierType_Subsurf: 
2048                                                 BIF_draw_icon(x, y, ICON_MOD_SUBSURF); break;
2049                                         case eModifierType_Armature: 
2050                                                 BIF_draw_icon(x, y, ICON_ARMATURE); break;
2051                                         case eModifierType_Lattice: 
2052                                                 BIF_draw_icon(x, y, ICON_LATTICE); break;
2053                                         case eModifierType_Curve: 
2054                                                 BIF_draw_icon(x, y, ICON_CURVE); break;
2055                                         case eModifierType_Build: 
2056                                                 BIF_draw_icon(x, y, ICON_MOD_BUILD); break;
2057                                         case eModifierType_Mirror: 
2058                                                 BIF_draw_icon(x, y, ICON_MOD_MIRROR); break;
2059                                         case eModifierType_Decimate: 
2060                                                 BIF_draw_icon(x, y, ICON_MOD_DECIM); break;
2061                                         case eModifierType_Wave: 
2062                                                 BIF_draw_icon(x, y, ICON_MOD_WAVE); break;
2063                                         case eModifierType_Hook: 
2064                                                 BIF_draw_icon(x, y, ICON_HOOK); break;
2065                                         case eModifierType_Softbody: 
2066                                                 BIF_draw_icon(x, y, ICON_MOD_SOFT); break;
2067                                         case eModifierType_Boolean: 
2068                                                 BIF_draw_icon(x, y, ICON_MOD_BOOLEAN); break;
2069                                         default:
2070                                                 BIF_draw_icon(x, y, ICON_DOT); break;
2071                                 }
2072                                 break;
2073                         }
2074                         case TSE_SCRIPT_BASE:
2075                                 BIF_draw_icon(x, y, ICON_TEXT); break;
2076                         case TSE_POSE_BASE:
2077                                 BIF_draw_icon(x, y, ICON_ARMATURE_DEHLT); break;
2078                         case TSE_POSE_CHANNEL:
2079                                 BIF_draw_icon(x, y, ICON_WPAINT_DEHLT); break;
2080                         default:
2081                                 BIF_draw_icon(x, y, ICON_DOT); break;
2082                 }
2083         }
2084         else {
2085                 switch( GS(tselem->id->name)) {
2086                         case ID_SCE:
2087                                 BIF_draw_icon(x, y, ICON_SCENE_DEHLT); break;
2088                         case ID_OB:
2089                                 BIF_draw_icon(x, y, ICON_OBJECT); break;
2090                         case ID_ME:
2091                                 BIF_draw_icon(x, y, ICON_MESH); break;
2092                         case ID_CU:
2093                                 BIF_draw_icon(x, y, ICON_CURVE); break;
2094                         case ID_MB:
2095                                 BIF_draw_icon(x, y, ICON_MBALL); break;
2096                         case ID_LT:
2097                                 BIF_draw_icon(x, y, ICON_LATTICE); break;
2098                         case ID_LA:
2099                                 BIF_draw_icon(x, y, ICON_LAMP_DEHLT); break;
2100                         case ID_MA:
2101                                 BIF_draw_icon(x, y, ICON_MATERIAL_DEHLT); break;
2102                         case ID_TE:
2103                                 BIF_draw_icon(x, y, ICON_TEXTURE_DEHLT); break;
2104                         case ID_IP:
2105                                 BIF_draw_icon(x, y, ICON_IPO_DEHLT); break;
2106                         case ID_IM:
2107                                 BIF_draw_icon(x, y, ICON_IMAGE_DEHLT); break;
2108                         case ID_SO:
2109                                 BIF_draw_icon(x, y, ICON_SPEAKER); break;
2110                         case ID_AR:
2111                                 BIF_draw_icon(x, y, ICON_ARMATURE); break;
2112                         case ID_CA:
2113                                 BIF_draw_icon(x, y, ICON_CAMERA_DEHLT); break;
2114                         case ID_KE:
2115                                 BIF_draw_icon(x, y, ICON_EDIT_DEHLT); break;
2116                         case ID_WO:
2117                                 BIF_draw_icon(x, y, ICON_WORLD_DEHLT); break;
2118                         case ID_AC:
2119                                 BIF_draw_icon(x, y, ICON_ACTION); break;
2120                         case ID_NLA:
2121                                 BIF_draw_icon(x, y, ICON_NLA); break;
2122                         case ID_TXT:
2123                                 BIF_draw_icon(x, y, ICON_SCRIPT); break;
2124                 }
2125         }
2126 }
2127
2128 static void outliner_draw_iconrow(SpaceOops *soops, ListBase *lb, int level, int *offsx, int ys)
2129 {
2130         TreeElement *te;
2131         TreeStoreElem *tselem;
2132         int active;
2133
2134         for(te= lb->first; te; te= te->next) {
2135                 tselem= TREESTORE(te);
2136                 
2137                 /* object hierarchy always, further constrained on level */
2138                 if(level<1 || (tselem->type==0 && te->idcode==ID_OB)) {
2139
2140                         /* active blocks get white circle */
2141                         active= 0;
2142                         if(tselem->type==0) {
2143                                 if(te->idcode==ID_OB) active= (OBACT==(Object *)tselem->id);
2144                                 else if(G.obedit && G.obedit->data==tselem->id) active= 1;
2145                                 else active= tree_element_active(soops, te, 0);
2146                         }
2147                         else active= tree_element_type_active(soops, te, tselem, 0);
2148                         
2149                         if(active) {
2150                                 uiSetRoundBox(15);
2151                                 glColor4ub(255, 255, 255, 100);
2152                                 uiRoundBox( (float)*offsx-0.5, (float)ys-1.0, (float)*offsx+OL_H-3.0, (float)ys+OL_H-3.0, OL_H/2.0-2.0);
2153                                 glEnable(GL_BLEND);
2154                         }
2155                         
2156                         tselem_draw_icon(*offsx, ys, tselem, te);
2157                         te->xs= *offsx;
2158                         te->ys= ys;
2159                         te->xend= *offsx+OL_X;
2160                         te->flag |= TE_ICONROW; // for click
2161                         
2162                         (*offsx) += OL_X;
2163                 }
2164                 
2165                 outliner_draw_iconrow(soops, &te->subtree, level+1, offsx, ys);
2166         }
2167         
2168 }
2169
2170 static void outliner_draw_tree_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
2171 {
2172         TreeElement *ten;
2173         TreeStoreElem *tselem;
2174         int offsx= 0, active=0; // active=1 active obj, else active data
2175         
2176         tselem= TREESTORE(te);
2177
2178         if(*starty >= soops->v2d.cur.ymin && *starty<= soops->v2d.cur.ymax) {
2179                 
2180                 glEnable(GL_BLEND);
2181
2182                 /* colors for active/selected data */
2183                 if(tselem->type==0) {
2184                         if(te->idcode==ID_SCE) {
2185                                 if(tselem->id == (ID *)G.scene) {
2186                                         glColor4ub(255, 255, 255, 100);
2187                                         active= 2;
2188                                 }
2189                         }
2190                         else if(te->idcode==ID_OB) {
2191                                 Object *ob= (Object *)tselem->id;
2192                                 
2193                                 if(ob==OBACT || (ob->flag & SELECT)) {
2194                                         char col[4];
2195                                         
2196                                         active= 2;
2197                                         if(ob==OBACT) {
2198                                                 BIF_GetThemeColorType4ubv(TH_ACTIVE, SPACE_VIEW3D, col);
2199                                                 active= 1;
2200                                         }
2201                                         else BIF_GetThemeColorType4ubv(TH_SELECT, SPACE_VIEW3D, col);
2202                                         col[3]= 100;
2203                                         glColor4ubv(col);
2204                                 }
2205                         }
2206                         else if(G.obedit && G.obedit->data==tselem->id) {
2207                                 glColor4ub(255, 255, 255, 100);
2208                                 active= 2;
2209                         }
2210                         else {
2211                                 if(tree_element_active(soops, te, 0)) {
2212                                         glColor4ub(220, 220, 255, 100);
2213                                         active= 2;
2214                                 }
2215                         }
2216                 }
2217                 else {
2218                         if( tree_element_type_active(soops, te, tselem, 0) ) active= 2;
2219                         glColor4ub(220, 220, 255, 100);
2220                 }
2221                 
2222                 /* active circle */
2223                 if(active) {
2224                         uiSetRoundBox(15);
2225                         uiRoundBox( (float)startx+OL_H-1.5, (float)*starty+2.0, (float)startx+2*OL_H-4.0, (float)*starty+OL_H-1.0, OL_H/2.0-2.0);
2226                         glEnable(GL_BLEND);
2227                         
2228                         te->flag |= TE_ACTIVE; // for lookup in display hierarchies
2229                 }
2230                 
2231                 /* open/close icon, only when sublevels, except for scene */
2232                 if(te->subtree.first || te->idcode==ID_SCE) {
2233                         int icon_x;
2234                         if(tselem->type==0 && (te->idcode==ID_OB || te->idcode==ID_SCE))
2235                                 icon_x = startx;
2236                         else
2237                                 icon_x = startx+5;
2238
2239                                 // icons a bit higher
2240                         if(tselem->flag & TSE_CLOSED) 
2241                                 BIF_draw_icon(icon_x, *starty+2, ICON_TRIA_RIGHT);
2242                         else
2243                                 BIF_draw_icon(icon_x, *starty+2, ICON_TRIA_DOWN);
2244                 }
2245                 offsx+= OL_X;
2246                 
2247                 /* datatype icon */
2248                 
2249                         // icons a bit higher
2250                 tselem_draw_icon(startx+offsx, *starty+2, tselem, te);
2251                 offsx+= OL_X;
2252                 glDisable(GL_BLEND);
2253
2254                 /* name */
2255                 if(active==1) BIF_ThemeColor(TH_TEXT_HI); 
2256                 else BIF_ThemeColor(TH_TEXT);
2257                 glRasterPos2i(startx+offsx, *starty+5);
2258                 BIF_RasterPos(startx+offsx, *starty+5);
2259                 BIF_DrawString(G.font, te->name, 0);
2260                 
2261                 offsx+= OL_X + BIF_GetStringWidth(G.font, te->name, 0);
2262                 
2263                 /* closed item, we draw the icons, not when it's a scene though */
2264                 if(tselem->flag & TSE_CLOSED) {
2265                         if(te->subtree.first) {
2266                                 if(tselem->type==0 && te->idcode==ID_SCE);
2267                                 else {
2268                                         int tempx= startx+offsx;
2269                                         // divider
2270                                         BIF_ThemeColorShade(TH_BACK, -40);
2271                                         glRecti(tempx -10, *starty+4, tempx -8, *starty+OL_H-4);
2272
2273                                         glEnable(GL_BLEND);
2274                                         glPixelTransferf(GL_ALPHA_SCALE, 0.5);
2275                                         outliner_draw_iconrow(soops, &te->subtree, 0, &tempx, *starty+2);
2276                                         glPixelTransferf(GL_ALPHA_SCALE, 1.0);
2277                                         glDisable(GL_BLEND);
2278                                 }
2279                         }
2280                 }
2281         }       
2282         /* store coord and continue */
2283         te->xs= startx;
2284         te->ys= *starty;
2285         te->xend= startx+offsx;
2286                 
2287         *starty-= OL_H;
2288         
2289         if((tselem->flag & TSE_CLOSED)==0) {
2290                 for(ten= te->subtree.first; ten; ten= ten->next) {
2291                         outliner_draw_tree_element(soops, ten, startx+OL_X, starty);
2292                 }
2293         }
2294 }
2295
2296 static void outliner_draw_hierarchy(SpaceOops *soops, ListBase *lb, int startx, int *starty)
2297 {
2298         TreeElement *te;
2299         TreeStoreElem *tselem;
2300         int y1, y2;
2301         
2302         if(lb->first==NULL) return;
2303         
2304         y1=y2= *starty; /* for vertical lines between objects */
2305         for(te=lb->first; te; te= te->next) {
2306                 y2= *starty;
2307                 tselem= TREESTORE(te);
2308                 
2309                 /* horizontal line? */
2310                 if(tselem->type==0 && (te->idcode==ID_OB || te->idcode==ID_SCE)) 
2311                         glRecti(startx, *starty, startx+OL_X, *starty-1);
2312                         
2313                 *starty-= OL_H;
2314                 
2315                 if((tselem->flag & TSE_CLOSED)==0)
2316                         outliner_draw_hierarchy(soops, &te->subtree, startx+OL_X, starty);
2317         }
2318         
2319         /* vertical line */
2320         te= lb->last;
2321         if(te->parent || lb->first!=lb->last) {
2322                 tselem= TREESTORE(te);
2323                 if(tselem->type==0 && te->idcode==ID_OB) {
2324                         
2325                         glRecti(startx, y1+OL_H, startx+1, y2);
2326                 }
2327         }
2328 }
2329
2330 static void outliner_draw_selection(SpaceOops *soops, ListBase *lb, int *starty) 
2331 {
2332         TreeElement *te;
2333         TreeStoreElem *tselem;
2334         
2335         for(te= lb->first; te; te= te->next) {
2336                 tselem= TREESTORE(te);
2337                 
2338                 /* selection status */
2339                 if(tselem->flag & TSE_SELECTED) {
2340                         glRecti(0, *starty+1, (int)soops->v2d.mask.xmax, *starty+OL_H-1);
2341                 }
2342                 *starty-= OL_H;
2343                 if((tselem->flag & TSE_CLOSED)==0) outliner_draw_selection(soops, &te->subtree, starty);
2344         }
2345 }
2346
2347 static void outliner_draw_tree(SpaceOops *soops)
2348 {
2349         TreeElement *te;
2350         int starty, startx;
2351         char col[4];
2352         
2353 #ifdef INTERNATIONAL
2354         FTF_SetFontSize('l');
2355         BIF_SetScale(1.0);
2356 #endif
2357         
2358         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); // only once
2359
2360         // selection first
2361         BIF_GetThemeColor3ubv(TH_BACK, col);
2362         glColor3ub(col[0]+15, col[1]+20, col[2]+25);
2363         starty= soops->v2d.tot.ymax-OL_H;
2364         outliner_draw_selection(soops, &soops->tree, &starty);
2365         
2366         // grey hierarchy lines
2367         glColor3ub(110,110,110);
2368         BIF_ThemeColorBlend(TH_BACK, TH_TEXT, 0.5);
2369         starty= soops->v2d.tot.ymax-OL_H/2;
2370         startx= 6;
2371         outliner_draw_hierarchy(soops, &soops->tree, startx, &starty);
2372         
2373         // items themselves
2374         starty= soops->v2d.tot.ymax-OL_H;
2375         startx= 0;
2376         for(te= soops->tree.first; te; te= te->next) {
2377                 outliner_draw_tree_element(soops, te, startx, &starty);
2378         }
2379         
2380 }
2381
2382 static void outliner_back(SpaceOops *soops)
2383 {
2384         int ystart;
2385         
2386         BIF_ThemeColorShade(TH_BACK, 6);
2387         ystart= soops->v2d.tot.ymax;
2388         ystart= OL_H*(ystart/(OL_H));
2389         
2390         while(ystart > soops->v2d.cur.ymin) {
2391                 glRecti(0, ystart, (int)soops->v2d.mask.xmax, ystart+OL_H);
2392                 ystart-= 2*OL_H;
2393         }
2394 }
2395
2396 static void namebutton_cb(void *soopsp, void *oldnamep)
2397 {
2398         SpaceOops *soops= soopsp;
2399         TreeStore *ts= soops->treestore;
2400         TreeStoreElem *tselem;
2401         int a;
2402         
2403         if(ts) {
2404                 /* only one namebutton can exist */
2405                 for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
2406                         if(tselem->flag & TSE_TEXTBUT) {
2407                                 if(tselem->type==0) {
2408                                         test_idbutton(tselem->id->name+2);      // library.c, unique name and alpha sort
2409                                 }
2410                                 else {
2411                                         TreeElement *te= outliner_find_tree_element(&soops->tree, a);
2412                                         
2413                                         if(te) {
2414                                                 switch(tselem->type) {
2415                                                 case TSE_DEFGROUP:
2416                                                         unique_vertexgroup_name(te->directdata, (Object *)tselem->id); //       id = object
2417                                                         break;
2418                                                 case TSE_NLA_ACTION:
2419                                                         test_idbutton(tselem->id->name+2);
2420                                                         break;
2421                                                 case TSE_EBONE:
2422                                                         if(G.obedit && G.obedit->data==(ID *)tselem->id) {
2423                                                                 EditBone *ebone= te->directdata;
2424                                                                 char newname[32];
2425                                                                 
2426                                                                 /* restore bone name */
2427                                                                 BLI_strncpy(newname, ebone->name, 32);
2428                                                                 BLI_strncpy(ebone->name, oldnamep, 32);
2429                                                                 armature_bone_rename(G.obedit->data, oldnamep, newname);
2430                                                         }
2431                                                         allqueue(REDRAWOOPS, 0);
2432                                                         allqueue(REDRAWVIEW3D, 1);
2433                                                         allqueue(REDRAWBUTSEDIT, 0);
2434                                                         break;
2435
2436                                                 case TSE_BONE:
2437                                                         {
2438                                                                 Bone *bone= te->directdata;
2439                                                                 Object *ob;
2440                                                                 char newname[32];
2441                                                                 
2442                                                                 // always make current object active
2443                                                                 tree_element_active_object(soops, te);
2444                                                                 ob= OBACT;
2445                                                                 
2446                                                                 /* restore bone name */
2447                                                                 BLI_strncpy(newname, bone->name, 32);
2448                                                                 BLI_strncpy(bone->name, oldnamep, 32);
2449                                                                 armature_bone_rename(ob->data, oldnamep, newname);
2450                                                         }
2451                                                         allqueue(REDRAWOOPS, 0);
2452                                                         allqueue(REDRAWVIEW3D, 1);
2453                                                         allqueue(REDRAWBUTSEDIT, 0);
2454                                                         break;
2455                                                 case TSE_POSE_CHANNEL:
2456                                                         {
2457                                                                 bPoseChannel *pchan= te->directdata;
2458                                                                 Object *ob;
2459                                                                 char newname[32];
2460                                                                 
2461                                                                 // always make current object active
2462                                                                 tree_element_active_object(soops, te);
2463                                                                 ob= OBACT;
2464                                                                 
2465                                                                 /* restore bone name */
2466                                                                 BLI_strncpy(newname, pchan->name, 32);
2467                                                                 BLI_strncpy(pchan->name, oldnamep, 32);
2468                                                                 armature_bone_rename(ob->data, oldnamep, newname);
2469                                                         }
2470                                                         allqueue(REDRAWOOPS, 0);
2471                                                         allqueue(REDRAWVIEW3D, 1);
2472                                                         allqueue(REDRAWBUTSEDIT, 0);
2473                                                         break;
2474                                                         
2475                                                 }
2476                                         }
2477                                 }
2478                                 tselem->flag &= ~TSE_TEXTBUT;
2479                         }
2480                 }
2481                 scrarea_queue_redraw(curarea);
2482         }
2483 }
2484
2485 static void outliner_buttons(uiBlock *block, SpaceOops *soops, ListBase *lb)
2486 {
2487         uiBut *bt;
2488         TreeElement *te;
2489         TreeStoreElem *tselem;
2490         int dx, len;
2491         
2492         for(te= lb->first; te; te= te->next) {
2493                 tselem= TREESTORE(te);
2494                 if(tselem->flag & TSE_TEXTBUT) {
2495                         
2496                         if(tselem->type==TSE_EBONE) len = sizeof(((EditBone*) 0)->name);
2497                         else if (tselem->type==TSE_MODIFIER) len = sizeof(((ModifierData*) 0)->name);
2498                         else len= sizeof(((ID*) 0)->name)-2;
2499                         
2500                         dx= BIF_GetStringWidth(G.font, te->name, 0);
2501                         if(dx<50) dx= 50;
2502                         
2503                         bt= uiDefBut(block, TEX, OL_NAMEBUTTON, "",  te->xs+2*OL_X-4, te->ys, dx+10, OL_H-1, te->name, 1.0, (float)len-1, 0, 0, "");
2504                         uiButSetFunc(bt, namebutton_cb, soops, NULL);
2505
2506                         // signal for button to open
2507                         addqueue(curarea->win, BUT_ACTIVATE, OL_NAMEBUTTON);
2508                 }
2509                 if((tselem->flag & TSE_CLOSED)==0) outliner_buttons(block, soops, &te->subtree);
2510         }
2511 }
2512
2513 void draw_outliner(ScrArea *sa, SpaceOops *soops)
2514 {
2515         uiBlock *block;
2516         int sizey;
2517         short ofsx, ofsy;
2518         
2519         calc_scrollrcts(sa, G.v2d, sa->winx, sa->winy);
2520
2521         if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
2522                 if(G.v2d->scroll) {     
2523                         ofsx= sa->winrct.xmin;  /* because mywin */
2524                         ofsy= sa->winrct.ymin;
2525                         glViewport(ofsx+G.v2d->mask.xmin,  ofsy+G.v2d->mask.ymin, ( ofsx+G.v2d->mask.xmax-1)-(ofsx+G.v2d->mask.xmin)+1, ( ofsy+G.v2d->mask.ymax-1)-( ofsy+G.v2d->mask.ymin)+1); 
2526                         glScissor(ofsx+G.v2d->mask.xmin,  ofsy+G.v2d->mask.ymin, ( ofsx+G.v2d->mask.xmax-1)-(ofsx+G.v2d->mask.xmin)+1, ( ofsy+G.v2d->mask.ymax-1)-( ofsy+G.v2d->mask.ymin)+1);
2527                 }
2528         }
2529         
2530         outliner_build_tree(soops); // always 
2531         sizey= 0;
2532         outliner_height(soops, &soops->tree, &sizey);
2533         
2534         /* we init all tot rect vars, only really needed on window size change tho */
2535         G.v2d->tot.xmin= 0.0;
2536         G.v2d->tot.xmax= (G.v2d->mask.xmax-G.v2d->mask.xmin);
2537         G.v2d->tot.ymax= 0.0;
2538         G.v2d->tot.ymin= -sizey*OL_H;
2539         test_view2d(G.v2d, sa->winx, sa->winy);
2540
2541         // align on top window if cur bigger than tot
2542         if(G.v2d->cur.ymax-G.v2d->cur.ymin > sizey*OL_H) {
2543                 G.v2d->cur.ymax= 0.0;
2544                 G.v2d->cur.ymin= -(G.v2d->mask.ymax-G.v2d->mask.ymin);
2545         }
2546
2547         myortho2(G.v2d->cur.xmin-0.375, G.v2d->cur.xmax-0.375, G.v2d->cur.ymin-0.375, G.v2d->cur.ymax-0.375);
2548
2549         /* draw outliner stuff */
2550         outliner_back(soops);
2551         outliner_draw_tree(soops);
2552
2553         /* restore viewport */
2554         mywinset(sa->win);
2555         
2556         /* ortho corrected */
2557         myortho2(G.v2d->cur.xmin-SCROLLB-0.375, G.v2d->cur.xmax-0.375, G.v2d->cur.ymin-0.375, G.v2d->cur.ymax-0.375);
2558         
2559         block= uiNewBlock(&sa->uiblocks, "outliner buttons", UI_EMBOSS, UI_HELV, sa->win);
2560         outliner_buttons(block, soops, &soops->tree);
2561         uiDrawBlock(block);
2562         
2563         /* drawoopsspace handles sliders */
2564 }
2565
2566