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