Bug found by Bassam; in certain occasions reading a file calls setscreen()
[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==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<MAX_MTEX; 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<MAX_MTEX; 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<MAX_MTEX; a++) {
555                                         if(wrld->mtex[a]) outliner_add_element(soops, &te->subtree, wrld->mtex[a]->tex, te, 0, a);
556                                 }
557                         }
558                         break;
559                 case ID_KE:
560                         {
561                                 Key *key= (Key *)id;
562                                 outliner_add_element(soops, &te->subtree, key->ipo, te, 0, 0);
563                         }
564                         break;
565                 case ID_AC:
566                         {
567                                 bAction *act= (bAction *)id;
568                                 bActionChannel *chan;
569                                 int a= 0;
570                                 
571                                 tselem= TREESTORE(parent);
572                                 for (chan=act->chanbase.first; chan; chan=chan->next, a++) {
573                                         outliner_add_element(soops, &te->subtree, chan->ipo, te, 0, a);
574                                 }
575                         }
576                         break;
577                 case ID_AR:
578                         {
579                                 bArmature *arm= (bArmature *)id;
580                                 int a= 0;
581                                 
582                                 if(G.obedit && G.obedit->data==arm) {
583                                         EditBone *ebone;
584                                         TreeElement *ten;
585                                         
586                                         for (ebone = G.edbo.first; ebone; ebone=ebone->next, a++) {
587                                                 ten= outliner_add_element(soops, &te->subtree, id, te, TSE_EBONE, a);
588                                                 ten->directdata= ebone;
589                                                 ten->name= ebone->name;
590                                                 ebone->temp= ten;
591                                         }
592                                         /* make hierarchy */
593                                         ten= te->subtree.first;
594                                         while(ten) {
595                                                 TreeElement *nten= ten->next, *par;
596                                                 ebone= (EditBone *)ten->directdata;
597                                                 if(ebone->parent) {
598                                                         BLI_remlink(&te->subtree, ten);
599                                                         par= ebone->parent->temp;
600                                                         BLI_addtail(&par->subtree, ten);
601                                                 }
602                                                 ten= nten;
603                                         }
604                                         
605                                 }
606                                 else {
607                                         Bone *curBone;
608                                         for (curBone=arm->bonebase.first; curBone; curBone=curBone->next){
609                                                 outliner_add_bone(soops, &te->subtree, id, curBone, te, &a);
610                                         }
611                                 }
612                         }
613                         break;
614                 }
615         }
616         return te;
617 }
618
619 /* special handling of hierarchical non-lib data */
620 static void outliner_add_bone(SpaceOops *soops, ListBase *lb, ID *id, Bone *curBone, 
621                                                           TreeElement *parent, int *a)
622 {
623         TreeElement *te= outliner_add_element(soops, lb, id, parent, TSE_BONE, *a);
624
625         (*a)++;
626         te->name= curBone->name;
627         te->directdata= curBone;
628
629         for(curBone= curBone->childbase.first; curBone; curBone=curBone->next) {
630                 outliner_add_bone(soops, &te->subtree, id, curBone, te, a);
631         }
632 }
633
634
635 static void outliner_make_hierarchy(SpaceOops *soops, ListBase *lb)
636 {
637         TreeElement *te, *ten, *tep;
638         TreeStoreElem *tselem;
639
640         /* build hierarchy */
641         te= lb->first;
642         while(te) {
643                 ten= te->next;
644                 tselem= TREESTORE(te);
645                 
646                 if(tselem->type==0 && te->idcode==ID_OB) {
647                         Object *ob= (Object *)tselem->id;
648                         if(ob->parent && ob->parent->id.newid) {
649                                 BLI_remlink(lb, te);
650                                 tep= (TreeElement *)ob->parent->id.newid;
651                                 BLI_addtail(&tep->subtree, te);
652                                 // set correct parent pointers
653                                 for(te=tep->subtree.first; te; te= te->next) te->parent= tep;
654                         }
655                 }
656                 te= ten;
657         }
658 }
659
660 static void outliner_build_tree(SpaceOops *soops)
661 {
662         Scene *sce;
663         Base *base;
664         Object *ob;
665         TreeElement *te, *ten;
666         TreeStoreElem *tselem;
667         
668         outliner_free_tree(&soops->tree);
669         outliner_storage_cleanup(soops);
670                                                    
671         /* clear ob id.new flags */
672         for(ob= G.main->object.first; ob; ob= ob->id.next) ob->id.newid= NULL;
673         
674         /* option 1: all scenes */
675         if(soops->outlinevis == SO_ALL_SCENES) {
676                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
677                         te= outliner_add_element(soops, &soops->tree, sce, NULL, 0, 0);
678                         tselem= TREESTORE(te);
679
680                         for(base= sce->base.first; base; base= base->next) {
681                                 ten= outliner_add_element(soops, &te->subtree, base->object, te, 0, 0);
682                                 ten->directdata= base;
683                         }
684                         outliner_make_hierarchy(soops, &te->subtree);
685                 }
686         }
687         else if(soops->outlinevis == SO_CUR_SCENE) {
688                 outliner_add_element(soops, &soops->tree, G.scene->world, NULL, 0, 0);
689                 
690                 for(base= G.scene->base.first; base; base= base->next) {
691                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
692                         ten->directdata= base;
693                 }
694                 outliner_make_hierarchy(soops, &soops->tree);
695         }
696         else if(soops->outlinevis == SO_VISIBLE) {
697                 for(base= G.scene->base.first; base; base= base->next) {
698                         if(base->lay & G.scene->lay)
699                                 outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
700                 }
701                 outliner_make_hierarchy(soops, &soops->tree);
702         }
703         else if(soops->outlinevis == SO_SAME_TYPE) {
704                 Object *ob= OBACT;
705                 if(ob) {
706                         for(base= G.scene->base.first; base; base= base->next) {
707                                 if(base->object->type==ob->type) {
708                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
709                                         ten->directdata= base;
710                                 }
711                         }
712                         outliner_make_hierarchy(soops, &soops->tree);
713                 }
714         }
715         else if(soops->outlinevis == SO_SELECTED) {
716                 for(base= G.scene->base.first; base; base= base->next) {
717                         if(base->lay & G.scene->lay) {
718                                 if(base==BASACT || (base->flag & SELECT)) {
719                                         ten= outliner_add_element(soops, &soops->tree, base->object, NULL, 0, 0);
720                                         ten->directdata= base;
721                                 }
722                         }
723                 }
724                 outliner_make_hierarchy(soops, &soops->tree);
725         }
726         else {
727                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
728                 if(ten) ten->directdata= BASACT;
729         }
730         
731         outliner_sort(soops, &soops->tree);
732 }
733
734 /* **************** INTERACTIVE ************* */
735
736 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
737 {
738         TreeElement *te;
739         int level=curlevel, lev;
740         
741         for(te= lb->first; te; te= te->next) {
742                 
743                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
744                 if(lev>level) level= lev;
745         }
746         return level;
747 }
748
749 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
750 {
751         TreeElement *te;
752         TreeStoreElem *tselem;
753         int level;
754         
755         for(te= lb->first; te; te= te->next) {
756                 tselem= TREESTORE(te);
757                 if(tselem->flag & flag) return curlevel;
758                 
759                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
760                 if(level) return level;
761         }
762         return 0;
763 }
764
765 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
766 {
767         TreeElement *te;
768         TreeStoreElem *tselem;
769         
770         for(te= lb->first; te; te= te->next) {
771                 tselem= TREESTORE(te);
772                 if(set==0) tselem->flag &= ~flag;
773                 else tselem->flag |= flag;
774                 outliner_set_flag(soops, &te->subtree, flag, set);
775         }
776 }
777
778 void outliner_toggle_visible(struct ScrArea *sa)
779 {
780         SpaceOops *soops= sa->spacedata.first;
781         
782         if( outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
783                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
784         else 
785                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
786
787         BIF_undo_push("Outliner toggle visible");
788         scrarea_queue_redraw(sa);
789 }
790
791 void outliner_toggle_selected(struct ScrArea *sa)
792 {
793         SpaceOops *soops= sa->spacedata.first;
794         
795         if( outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
796                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
797         else 
798                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
799         
800         BIF_undo_push("Outliner toggle selected");
801         scrarea_queue_redraw(sa);
802 }
803
804
805 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
806 {
807         TreeElement *te;
808         TreeStoreElem *tselem;
809         
810         for(te= lb->first; te; te= te->next) {
811                 tselem= TREESTORE(te);
812                 
813                 if(open) {
814                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
815                 }
816                 else {
817                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
818                 }
819                 
820                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
821         }
822 }
823
824 void outliner_one_level(struct ScrArea *sa, int add)
825 {
826         SpaceOops *soops= sa->spacedata.first;
827         int level;
828         
829         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
830         if(add==1) {
831                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
832         }
833         else {
834                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
835                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
836         }
837         
838         BIF_undo_push("Outliner show/hide one level");
839         scrarea_queue_redraw(sa);
840 }
841
842 void outliner_page_up_down(ScrArea *sa, int up)
843 {
844         SpaceOops *soops= sa->spacedata.first;
845         int dy= soops->v2d.mask.ymax-soops->v2d.mask.ymin;
846         
847         if(up == -1) dy= -dy;
848         soops->v2d.cur.ymin+= dy;
849         soops->v2d.cur.ymax+= dy;
850         
851         scrarea_queue_redraw(sa);
852 }
853
854 /* **** do clicks on items ******* */
855
856 static void tree_element_active_object(SpaceOops *soops, TreeElement *te)
857 {
858         TreeStoreElem *tselem= TREESTORE(te);
859         Scene *sce;
860         Base *base;
861         Object *ob= NULL;
862         
863         /* if id is not object, we search back */
864         if(te->idcode==ID_OB) ob= (Object *)tselem->id;
865         else {
866                 ob= (Object *)outliner_search_back(soops, te, ID_OB);
867                 if(ob==OBACT) return;
868         }
869         if(ob==NULL) return;
870         
871         sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
872         if(sce && G.scene != sce) {
873                 if(G.obedit) exit_editmode(2);
874                 if(G.obpose) exit_posemode(1);
875                 set_scene(sce);
876         }
877         
878         /* find associated base in current scene */
879         for(base= FIRSTBASE; base; base= base->next) 
880                 if(base->object==ob) break;
881         if(base) {
882                 if(G.qual & LR_SHIFTKEY) {
883                         /* swap select */
884                         if(base->flag & SELECT) base->flag &= ~SELECT;
885                         else base->flag |= SELECT;
886                         base->object->flag= base->flag;
887                 }
888                 else {
889                         Base *b;
890                         /* deleselect all */
891                         for(b= FIRSTBASE; b; b= b->next) {
892                                 b->flag &= ~SELECT;
893                                 b->object->flag= b->flag;
894                         }
895                         base->flag |= SELECT;
896                         base->object->flag |= SELECT;
897                 }
898                 set_active_base(base);  /* editview.c */
899                 
900                 allqueue(REDRAWVIEW3D, 1);
901                 allqueue(REDRAWOOPS, 0);
902                 allqueue(REDRAWINFO, 1);
903         }
904         
905         if(ob!=G.obedit) exit_editmode(2);
906         if(ob!=G.obpose) exit_posemode(1);
907 }
908
909 static int tree_element_active_material(SpaceOops *soops, TreeElement *te, int set)
910 {
911         TreeElement *tes;
912         Object *ob;
913         
914         /* we search for the object parent */
915         ob= (Object *)outliner_search_back(soops, te, ID_OB);
916         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
917         
918         /* searching in ob mat array? */
919         tes= te->parent;
920         if(tes->idcode==ID_OB) {
921                 if(set) {
922                         ob->actcol= te->index+1;
923                         ob->colbits |= (1<<te->index);  // make ob material active too
924                 }
925                 else {
926                         if(ob->actcol == te->index+1) 
927                                 if(ob->colbits & (1<<te->index)) return 1;
928                 }
929         }
930         /* or we search for obdata material */
931         else {
932                 if(set) {
933                         ob->actcol= te->index+1;
934                         ob->colbits &= ~(1<<te->index); // make obdata material active too
935                 }
936                 else {
937                         if(ob->actcol == te->index+1)
938                                 if( (ob->colbits & (1<<te->index))==0 ) return 1;
939                 }
940         }
941         if(set) {
942                 extern_set_butspace(F5KEY);     // force shading buttons
943                 BIF_all_preview_changed();
944                 allqueue(REDRAWBUTSSHADING, 1);
945                 allqueue(REDRAWOOPS, 0);
946                 allqueue(REDRAWIPO, 0);
947         }
948         return 0;
949 }
950
951 static int tree_element_active_texture(SpaceOops *soops, TreeElement *te, int set)
952 {
953         TreeElement *tep;
954         TreeStoreElem *tselem, *tselemp;
955         Object *ob=OBACT;
956         ScrArea *sa;
957         SpaceButs *sbuts=NULL;
958         
959         if(ob==NULL) return 0; // no active object
960         
961         tselem= TREESTORE(te);
962         
963         /* find buttons area (note, this is undefined really still, needs recode in blender) */
964         sa= G.curscreen->areabase.first;
965         while(sa) {
966                 if(sa->spacetype==SPACE_BUTS) break;
967                 sa= sa->next;
968         }
969         if(sa) sbuts= sa->spacedata.first;
970         
971         /* where is texture linked to? */
972         tep= te->parent;
973         tselemp= TREESTORE(tep);
974         
975         if(tep->idcode==ID_WO) {
976                 World *wrld= (World *)tselemp->id;
977
978                 if(set) {
979                         if(sbuts) {
980                                 sbuts->tabo= TAB_SHADING_TEX;   // hack from header_buttonswin.c
981                                 sbuts->texfrom= 1;
982                         }
983                         extern_set_butspace(F6KEY);     // force shading buttons texture
984                         wrld->texact= te->index;
985                 }
986                 else if(tselemp->id == (ID *)(G.scene->world)) {
987                         if(wrld->texact==te->index) return 1;
988                 }
989         }
990         else if(tep->idcode==ID_LA) {
991                 Lamp *la= (Lamp *)tselemp->id;
992                 if(set) {
993                         if(sbuts) {
994                                 sbuts->tabo= TAB_SHADING_TEX;   // hack from header_buttonswin.c
995                                 sbuts->texfrom= 2;
996                         }
997                         extern_set_butspace(F6KEY);     // force shading buttons texture
998                         la->texact= te->index;
999                 }
1000                 else {
1001                         if(tselemp->id == ob->data) {
1002                                 if(la->texact==te->index) return 1;
1003                         }
1004                 }
1005         }
1006         else if(tep->idcode==ID_MA) {
1007                 Material *ma= (Material *)tselemp->id;
1008                 if(set) {
1009                         if(sbuts) {
1010                                 //sbuts->tabo= TAB_SHADING_TEX; // hack from header_buttonswin.c
1011                                 sbuts->texfrom= 0;
1012                         }
1013                         extern_set_butspace(F6KEY);     // force shading buttons texture
1014                         ma->texact= te->index;
1015                         
1016                         /* also set active material */
1017                         ob->actcol= tep->index+1;
1018                 }
1019                 else if(tep->flag & TE_ACTIVE) {        // this is active material
1020                         if(ma->texact==te->index) return 1;
1021                 }
1022         }
1023         
1024         return 0;
1025 }
1026
1027
1028 static int tree_element_active_lamp(SpaceOops *soops, TreeElement *te, int set)
1029 {
1030         Object *ob;
1031         
1032         /* we search for the object parent */
1033         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1034         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1035         
1036         if(set) {
1037                 extern_set_butspace(F5KEY);
1038                 BIF_all_preview_changed();
1039                 allqueue(REDRAWBUTSSHADING, 1);
1040                 allqueue(REDRAWOOPS, 0);
1041                 allqueue(REDRAWIPO, 0);
1042         }
1043         else return 1;
1044         
1045         return 0;
1046 }
1047
1048 static int tree_element_active_world(SpaceOops *soops, TreeElement *te, int set)
1049 {
1050         TreeElement *tep;
1051         TreeStoreElem *tselem=NULL;
1052         Scene *sce=NULL;
1053         
1054         tep= te->parent;
1055         if(tep) {
1056                 tselem= TREESTORE(tep);
1057                 sce= (Scene *)tselem->id;
1058         }
1059         
1060         if(set) {       // make new scene active
1061                 if(sce && G.scene != sce) {
1062                         if(G.obedit) exit_editmode(2);
1063                         if(G.obpose) exit_posemode(1);
1064                         set_scene(sce);
1065                 }
1066         }
1067         
1068         if(tep==NULL || tselem->id == (ID *)G.scene) {
1069                 if(set) {
1070                         extern_set_butspace(F8KEY);
1071                 }
1072                 else {
1073                         return 1;
1074                 }
1075         }
1076         return 0;
1077 }
1078
1079 static int tree_element_active_ipo(SpaceOops *soops, TreeElement *te, int set)
1080 {
1081         TreeElement *tes;
1082         TreeStoreElem *tselems=NULL;
1083         Object *ob;
1084         
1085         /* we search for the object parent */
1086         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1087         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1088         
1089         /* the parent of ipo */
1090         tes= te->parent;
1091         tselems= TREESTORE(tes);
1092         
1093         if(set) {
1094                 ob->ipowin= tes->idcode;
1095                 if(ob->ipowin==ID_MA) tree_element_active_material(soops, tes, 1);
1096                 else if(ob->ipowin==ID_AC) {
1097                         bActionChannel *chan;
1098                         short a=0;
1099                         for(chan=ob->action->chanbase.first; chan; chan= chan->next) {
1100                                 if(a==te->index) break;
1101                                 if(chan->ipo) a++;
1102                         }
1103                         deselect_actionchannels(ob->action, 0);
1104                         select_channel(ob->action, chan, SELECT_ADD);
1105                         allqueue(REDRAWACTION, ob->ipowin);
1106                         if(G.obpose) allqueue(REDRAWVIEW3D, ob->ipowin);
1107                 }
1108                 
1109                 allqueue(REDRAWIPO, ob->ipowin);
1110         }
1111         else if(ob->ipowin==tes->idcode) {
1112                 if(ob->ipowin==ID_MA) {
1113                         Material *ma= give_current_material(ob, ob->actcol);
1114                         if(ma==(Material *)tselems->id) return 1;
1115                 }
1116                 else if(ob->ipowin==ID_AC) {
1117                         bActionChannel *chan;
1118                         short a=0;
1119                         for(chan=ob->action->chanbase.first; chan; chan= chan->next) {
1120                                 if(a==te->index) break;
1121                                 if(chan->ipo) a++;
1122                         }
1123                         if(chan==get_hilighted_action_channel(ob->action)) return 1;
1124                 }
1125                 else return 1;
1126         }
1127         return 0;
1128 }       
1129
1130 static int tree_element_active_defgroup(TreeElement *te, TreeStoreElem *tselem, int set)
1131 {
1132         Object *ob;
1133         
1134         /* id in tselem is object */
1135         ob= (Object *)tselem->id;
1136         if(set) {
1137                 ob->actdef= te->index+1;
1138         }
1139         else {
1140                 if(ob==OBACT)
1141                         if(ob->actdef== te->index+1) return 1;
1142         }
1143         return 0;
1144 }
1145
1146 static int tree_element_active_nla_action(TreeElement *te, TreeStoreElem *tselem, int set)
1147 {
1148         if(set) {
1149                 bActionStrip *strip= te->directdata;
1150                 if(strip) {
1151                         deselect_nlachannel_keys(0);
1152                         strip->flag |= ACTSTRIP_SELECT;
1153                         allqueue(REDRAWNLA, 0);
1154                 }
1155         }
1156         else {
1157                 /* id in tselem is action */
1158                 bActionStrip *strip= te->directdata;
1159                 if(strip) {
1160                         if(strip->flag & ACTSTRIP_SELECT) return 1;
1161                 }
1162         }
1163         return 0;
1164 }
1165
1166
1167 static int tree_element_active_bone(TreeElement *te, TreeStoreElem *tselem, int set)
1168 {
1169         bArmature *arm= (bArmature *)tselem->id;
1170         Bone *bone= te->directdata;
1171
1172         if(set) {
1173                 if(G.qual & LR_SHIFTKEY);
1174                 else deselectall_posearmature(0);
1175                 bone->flag |= BONE_SELECTED;
1176
1177                 allqueue(REDRAWVIEW3D, 0);
1178                 allqueue(REDRAWOOPS, 0);
1179                 allqueue(REDRAWACTION, 0);
1180         }
1181         else {
1182                 Object *ob= OBACT;
1183                 
1184                 if(ob && ob->data==arm) {
1185                         if (bone->flag & BONE_SELECTED) return 1;
1186                 }
1187         }
1188         return 0;
1189 }
1190
1191 /* ebones only draw in editmode armature */
1192 static int tree_element_active_ebone(TreeElement *te, TreeStoreElem *tselem, int set)
1193 {
1194         EditBone *ebone= te->directdata;
1195         
1196         if(set) {
1197                 if(G.qual & LR_SHIFTKEY);
1198                 else {
1199                         ebone->flag |= BONE_SELECTED; // trick deselectall, it toggles... duhh
1200                         deselectall_armature();
1201                 }
1202                 ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL;
1203                 // flush to parent?
1204                 if(ebone->parent && (ebone->flag & BONE_IK_TOPARENT)) ebone->parent->flag |= BONE_TIPSEL;
1205                 
1206                 allqueue(REDRAWVIEW3D, 0);
1207                 allqueue(REDRAWOOPS, 0);
1208                 allqueue(REDRAWACTION, 0);
1209         }
1210         else {
1211                 if (ebone->flag & BONE_SELECTED) return 1;
1212         }
1213         return 0;
1214 }
1215
1216
1217 static int tree_element_active_text(SpaceOops *soops, TreeElement *te, int set)
1218 {
1219         ScrArea *sa=NULL;
1220         
1221         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1222                 if(sa->spacetype==SPACE_TEXT) break;
1223         }
1224         if(sa) {
1225                 SpaceText *st= sa->spacedata.first;
1226                 TreeStoreElem *tselem= TREESTORE(te);
1227                 
1228                 if(set) {
1229                         st->text= (Text *)tselem->id;
1230                         st->top= 0;
1231                         scrarea_queue_redraw(sa);
1232                 }
1233                 else if(st->text==(Text *)tselem->id) return 1;
1234         }
1235         return 0;
1236 }
1237
1238 /* generic call for ID data check or make/check active in UI */
1239 static int tree_element_active(SpaceOops *soops, TreeElement *te, int set)
1240 {
1241
1242         switch(te->idcode) {
1243                 case ID_MA:
1244                         return tree_element_active_material(soops, te, set);
1245                 case ID_WO:
1246                         return tree_element_active_world(soops, te, set);
1247                 case ID_LA:
1248                         return tree_element_active_lamp(soops, te, set);
1249                 case ID_IP:
1250                         return tree_element_active_ipo(soops, te, set);
1251                 case ID_TE:
1252                         return tree_element_active_texture(soops, te, set);
1253                 case ID_TXT:
1254                         return tree_element_active_text(soops, te, set);
1255         }
1256         return 0;
1257 }
1258
1259 /* generic call for non-id data to make/check active in UI */
1260 static int tree_element_type_active(SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
1261 {
1262         
1263         switch(tselem->type) {
1264                 case TSE_NLA_ACTION:
1265                         return tree_element_active_nla_action(te, tselem, set);
1266                 case TSE_DEFGROUP:
1267                         return tree_element_active_defgroup(te, tselem, set);
1268                 case TSE_BONE:
1269                         return tree_element_active_bone(te, tselem, set);
1270                 case TSE_EBONE:
1271                         return tree_element_active_ebone(te, tselem, set);
1272                 case TSE_HOOK: // actually object
1273                         if(set) tree_element_active_object(soops, te);
1274                         else if(tselem->id==(ID *)OBACT) return 1;
1275                         break;
1276         }
1277         return 0;
1278 }
1279
1280
1281 static int do_outliner_mouse_event(SpaceOops *soops, TreeElement *te, short event, float *mval)
1282 {
1283         
1284         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
1285                 TreeStoreElem *tselem= TREESTORE(te);
1286                 int openclose= 0;
1287
1288                 /* open close icon, three things to check */
1289                 if(event==RETKEY || event==PADENTER) openclose= 1; // enter opens/closes always
1290                 else if((te->flag & TE_ICONROW)==0) {                           // hidden icon, no open/close
1291                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) openclose= 1;
1292                 }
1293
1294                 if(openclose) {
1295                         
1296                         /* all below close/open? */
1297                         if( (G.qual & LR_SHIFTKEY) ) {
1298                                 tselem->flag &= ~TSE_CLOSED;
1299                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
1300                         }
1301                         else {
1302                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
1303                                 else tselem->flag |= TSE_CLOSED;
1304                                 
1305                         }
1306                         
1307                         return 1;
1308                 }
1309                 /* name and first icon */
1310                 else if(mval[0]>te->xs && mval[0]<te->xend) {
1311                         
1312                         /* activate a name button? */
1313                         if(G.qual & LR_CTRLKEY) {
1314                                 if(ELEM5(tselem->type, TSE_NLA, TSE_DEFGROUP_BASE, TSE_CONSTRAINT_BASE, TSE_HOOKS_BASE, TSE_SCRIPT_BASE)) 
1315                                         error("Cannot edit builtin name");
1316                                 else {
1317                                         tselem->flag |= TSE_TEXTBUT;
1318                                 }
1319                         }
1320                         else {
1321                                 /* always makes active object */
1322                                 tree_element_active_object(soops, te);
1323                                 
1324                                 if(tselem->type==0) { // the lib blocks
1325                                         /* editmode? */
1326                                         if(te->idcode==ID_SCE) {
1327                                                 if(G.scene!=(Scene *)tselem->id) {
1328                                                         if(G.obedit) exit_editmode(2);
1329                                                         if(G.obpose) exit_posemode(1);
1330                                                         set_scene((Scene *)tselem->id);
1331                                                 }
1332                                         }
1333                                         else if(ELEM4(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT)) {
1334                                                 if(G.obpose) exit_posemode(1);
1335                                                 if(G.obedit) exit_editmode(2);
1336                                                 else {
1337                                                         enter_editmode();
1338                                                         extern_set_butspace(F9KEY);
1339                                                 }
1340                                         }
1341                                         else if(te->idcode==ID_AR) {
1342                                                 if(G.obedit) exit_editmode(2);
1343                                                 if(G.obpose) exit_posemode(1);
1344                                                 else enter_posemode();
1345                                         }
1346                                         else {  // rest of types
1347                                                 tree_element_active(soops, te, 1);
1348                                         }
1349                                         
1350                                 }
1351                                 else tree_element_type_active(soops, te, tselem, 1);
1352                         }
1353                         return 1;
1354                 }
1355         }
1356         
1357         for(te= te->subtree.first; te; te= te->next) {
1358                 if(do_outliner_mouse_event(soops, te, event, mval)) return 1;
1359         }
1360         return 0;
1361 }
1362
1363 /* event can enterkey, then it opens/closes */
1364 void outliner_mouse_event(ScrArea *sa, short event)
1365 {
1366         SpaceOops *soops= sa->spacedata.first;
1367         TreeElement *te;
1368         float fmval[2];
1369         short mval[2];
1370         
1371         getmouseco_areawin(mval);
1372         areamouseco_to_ipoco(&soops->v2d, mval, fmval, fmval+1);
1373         
1374         for(te= soops->tree.first; te; te= te->next) {
1375                 if(do_outliner_mouse_event(soops, te, event, fmval)) break;
1376         }
1377         
1378         if(te) {
1379                 BIF_undo_push("Outliner click event");
1380                 allqueue(REDRAWOOPS, 0);
1381         }
1382
1383 }
1384
1385 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
1386 {
1387         TreeElement *te, *tes;
1388         TreeStoreElem *tselem;
1389         
1390         for(te= lb->first; te; te= te->next) {
1391                 tselem= TREESTORE(te);
1392                 if(tselem->id==id) return te;
1393                 /* only deeper on scene or object */
1394                 if( te->idcode==ID_OB || te->idcode==ID_SCE) { 
1395                         tes= outliner_find_id(soops, &te->subtree, id);
1396                         if(tes) return tes;
1397                 }
1398         }
1399         return NULL;
1400 }
1401
1402 void outliner_show_active(struct ScrArea *sa)
1403 {
1404         SpaceOops *so= sa->spacedata.first;
1405         TreeElement *te;
1406         int ytop;
1407         
1408         if(OBACT == NULL) return;
1409         
1410         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
1411         if(te) {
1412                 /* make te->ys center of view */
1413                 ytop= te->ys + (so->v2d.mask.ymax-so->v2d.mask.ymin)/2;
1414                 if(ytop>0) ytop= 0;
1415                 so->v2d.cur.ymax= ytop;
1416                 so->v2d.cur.ymin= ytop-(so->v2d.mask.ymax-so->v2d.mask.ymin);
1417                 scrarea_queue_redraw(sa);
1418         }
1419 }
1420
1421 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
1422 {
1423         TreeElement *te;
1424         TreeStoreElem *tselem;
1425         
1426         for(te= lb->first; te; te= te->next) {
1427                 tselem= TREESTORE(te);
1428                 if(tselem->type==0 && te->idcode==ID_OB) return 1;
1429                 if( subtree_has_objects(soops, &te->subtree)) return 1;
1430         }
1431         return 0;
1432 }
1433
1434 static void tree_element_show_hierarchy(SpaceOops *soops, ListBase *lb)
1435 {
1436         TreeElement *te;
1437         TreeStoreElem *tselem;
1438
1439         /* open all object elems, close others */
1440         for(te= lb->first; te; te= te->next) {
1441                 tselem= TREESTORE(te);
1442                 
1443                 if(tselem->type==0) {
1444                         if(te->idcode==ID_SCE) {
1445                                 if(tselem->id!=(ID *)G.scene) tselem->flag |= TSE_CLOSED;
1446                                         else tselem->flag &= ~TSE_CLOSED;
1447                         }
1448                         else if(te->idcode==ID_OB) {
1449                                 if(subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
1450                                 else tselem->flag |= TSE_CLOSED;
1451                         }
1452                 }
1453                 else tselem->flag |= TSE_CLOSED;
1454
1455                 if(tselem->flag & TSE_CLOSED); else tree_element_show_hierarchy(soops, &te->subtree);
1456         }
1457         
1458 }
1459
1460 /* show entire object level hierarchy */
1461 void outliner_show_hierarchy(struct ScrArea *sa)
1462 {
1463         SpaceOops *so= sa->spacedata.first;
1464         
1465         tree_element_show_hierarchy(so, &so->tree);
1466         scrarea_queue_redraw(sa);
1467         
1468         BIF_undo_push("Outliner show hierarchy");
1469 }
1470
1471 static void do_outliner_select(SpaceOops *soops, ListBase *lb, float y1, float y2, short *selecting)
1472 {
1473         TreeElement *te;
1474         TreeStoreElem *tselem;
1475         
1476         if(y1>y2) SWAP(float, y1, y2);
1477         
1478         for(te= lb->first; te; te= te->next) {
1479                 tselem= TREESTORE(te);
1480                 
1481                 if(te->ys + OL_H < y1) return;
1482                 if(te->ys < y2) {
1483                         if((te->flag & TE_ICONROW)==0) {
1484                                 if(*selecting == -1) {
1485                                         if( tselem->flag & TSE_SELECTED) *selecting= 0;
1486                                         else *selecting= 1;
1487                                 }
1488                                 if(*selecting) tselem->flag |= TSE_SELECTED;
1489                                 else tselem->flag &= ~TSE_SELECTED;
1490                         }
1491                 }
1492                 if((tselem->flag & TSE_CLOSED)==0) do_outliner_select(soops, &te->subtree, y1, y2, selecting);
1493         }
1494 }
1495
1496 /* its own redraw loop... urm */
1497 void outliner_select(struct ScrArea *sa )
1498 {
1499         SpaceOops *so= sa->spacedata.first;
1500         float fmval[2], y1, y2;
1501         short mval[2], yo=-1, selecting= -1;
1502         
1503         getmouseco_areawin(mval);
1504         areamouseco_to_ipoco(&so->v2d, mval, fmval, fmval+1);
1505         y1= fmval[1];
1506
1507         while (get_mbut() & R_MOUSE) {
1508                 getmouseco_areawin(mval);
1509                 areamouseco_to_ipoco(&so->v2d, mval, fmval, fmval+1);
1510                 y2= fmval[1];
1511                 if(yo!=mval[1]) {
1512                         do_outliner_select(so, &so->tree, y1, y2, &selecting);
1513                         yo= mval[1];
1514                         scrarea_do_windraw(sa);
1515                         screen_swapbuffers();
1516                 
1517                         y1= y2;
1518                 }
1519         }
1520         BIF_undo_push("Outliner selection");
1521
1522 }
1523
1524 /* ************ SELECTION OPERATIONS ********* */
1525
1526 static int scenelevel=0, objectlevel=0, idlevel=0, datalevel=0; // globals, euh... you can do better
1527
1528 static void set_operation_types(SpaceOops *soops, ListBase *lb)
1529 {
1530         TreeElement *te;
1531         TreeStoreElem *tselem;
1532         
1533         for(te= lb->first; te; te= te->next) {
1534                 tselem= TREESTORE(te);
1535                 if(tselem->flag & TSE_SELECTED) {
1536                         if(tselem->type) {
1537                                 if(datalevel==0) datalevel= tselem->type;
1538                                 else if(datalevel!=tselem->type) datalevel= -1;
1539                         }
1540                         else {
1541                                 int idcode= GS(tselem->id->name);
1542                                 switch(idcode) {
1543                                         case ID_SCE:
1544                                                 scenelevel= 1;
1545                                                 break;
1546                                         case ID_OB:
1547                                                 objectlevel= 1;
1548                                                 break;
1549                                                 
1550                                         case ID_ME: case ID_CU: case ID_MB: case ID_LT:
1551                                         case ID_LA: case ID_AR: case ID_CA:
1552                                                 idlevel= -2;
1553                                                 break;
1554                                                 
1555                                         case ID_MA: case ID_TE: case ID_IP: case ID_IM:
1556                                         case ID_SO: case ID_KE: case ID_WO: case ID_AC:
1557                                         case ID_NLA: case ID_TXT:
1558                                                 if(idlevel==0) idlevel= idcode;
1559                                                 else if(idlevel!=idcode) idlevel= -1;
1560                                                         break;
1561                                 }
1562                         }
1563                 }
1564                 if((tselem->flag & TSE_CLOSED)==0) set_operation_types(soops, &te->subtree);
1565         }
1566 }
1567
1568 static void unlink_material_cb(TreeElement *te, TreeStoreElem *tsep)
1569 {
1570         Material **matar=NULL;
1571         int a, totcol=0;
1572         
1573         if( GS(tsep->id->name)==ID_OB) {
1574                 Object *ob= (Object *)tsep->id;
1575                 totcol= ob->totcol;
1576                 matar= ob->mat;
1577         }
1578         else if( GS(tsep->id->name)==ID_ME) {
1579                 Mesh *me= (Mesh *)tsep->id;
1580                 totcol= me->totcol;
1581                 matar= me->mat;
1582         }
1583         else if( GS(tsep->id->name)==ID_CU) {
1584                 Curve *cu= (Curve *)tsep->id;
1585                 totcol= cu->totcol;
1586                 matar= cu->mat;
1587         }
1588         else if( GS(tsep->id->name)==ID_MB) {
1589                 MetaBall *mb= (MetaBall *)tsep->id;
1590                 totcol= mb->totcol;
1591                 matar= mb->mat;
1592         }
1593
1594         for(a=0; a<totcol; a++) {
1595                 if(a==te->index && matar[a]) {
1596                         matar[a]->id.us--;
1597                         matar[a]= NULL;
1598                 }
1599         }
1600 }
1601
1602 static void unlink_texture_cb(TreeElement *te, TreeStoreElem *tsep)
1603 {
1604         MTex **mtex= NULL;
1605         int a;
1606         
1607         if( GS(tsep->id->name)==ID_MA) {
1608                 Material *ma= (Material *)tsep->id;
1609                 mtex= ma->mtex;
1610         }
1611         else if( GS(tsep->id->name)==ID_LA) {
1612                 Lamp *la= (Lamp *)tsep->id;
1613                 mtex= la->mtex;
1614         }
1615         else if( GS(tsep->id->name)==ID_WO) {
1616                 World *wrld= (World *)tsep->id;
1617                 mtex= wrld->mtex;
1618         }
1619         else return;
1620         
1621         for(a=0; a<MAX_MTEX; a++) {
1622                 if(a==te->index && mtex[a]) {
1623                         if(mtex[a]->tex) {
1624                                 mtex[a]->tex->id.us--;
1625                                 mtex[a]->tex= NULL;
1626                         }
1627                 }
1628         }
1629 }
1630
1631 static void outliner_do_libdata_operation(SpaceOops *soops, ListBase *lb, 
1632                                                                                  void (*operation_cb)(TreeElement *, TreeStoreElem *))
1633 {
1634         TreeElement *te;
1635         TreeStoreElem *tselem;
1636         
1637         for(te=lb->first; te; te= te->next) {
1638                 tselem= TREESTORE(te);
1639                 if(tselem->flag & TSE_SELECTED) {
1640                         if(tselem->type==0) {
1641                                 TreeStoreElem *tsep= TREESTORE(te->parent);
1642                                 operation_cb(te, tsep);
1643                         }
1644                 }
1645                 if((tselem->flag & TSE_CLOSED)==0) {
1646                         outliner_do_libdata_operation(soops, &te->subtree, operation_cb);
1647                 }
1648         }
1649 }
1650
1651 /* */
1652
1653 static void object_select_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_deselect_cb(TreeElement *te, TreeStoreElem *tselem)
1662 {
1663         Base *base= (Base *)te->directdata;
1664         
1665         base->flag &= ~SELECT;
1666         base->object->flag &= ~SELECT;
1667 }
1668
1669 static void object_delete_cb(TreeElement *te, TreeStoreElem *tselem)
1670 {
1671         Base *base= (Base *)te->directdata;
1672         
1673         if(base) {
1674                 // check also library later
1675                 if(G.obpose==base->object) exit_posemode(1);
1676                 if(G.obedit==base->object) exit_editmode(2);
1677                 
1678                 if(base==BASACT) {
1679                         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
1680                         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1681                 }
1682                 
1683                 free_and_unlink_base(base);
1684                 te->directdata= NULL;
1685                 tselem->id= NULL;
1686         }
1687 }
1688
1689
1690 static void outliner_do_object_operation(SpaceOops *soops, ListBase *lb, 
1691                                                                                  void (*operation_cb)(TreeElement *, TreeStoreElem *))
1692 {
1693         TreeElement *te;
1694         TreeStoreElem *tselem;
1695         
1696         for(te=lb->first; te; te= te->next) {
1697                 tselem= TREESTORE(te);
1698                 if(tselem->flag & TSE_SELECTED) {
1699                         if(tselem->type==0 && te->idcode==ID_OB) {
1700                                 // when objects selected in other scenes... dunno if that should be allowed
1701                                 Scene *sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
1702                                 if(sce && G.scene != sce) set_scene(sce);
1703                                 
1704                                 operation_cb(te, tselem);
1705                         }
1706                 }
1707                 if((tselem->flag & TSE_CLOSED)==0) {
1708                         outliner_do_object_operation(soops, &te->subtree, operation_cb);
1709                 }
1710         }
1711 }
1712
1713 void outliner_operation_menu(ScrArea *sa)
1714 {
1715         SpaceOops *soops= sa->spacedata.first;
1716         
1717         // globals
1718         scenelevel= objectlevel= idlevel= datalevel=0;
1719         
1720         set_operation_types(soops, &soops->tree);
1721         
1722         if(scenelevel) {
1723                 if(objectlevel || datalevel || idlevel) error("Mixed selection");
1724                 //else pupmenu("Scene Operations%t|Delete");
1725         }
1726         else if(objectlevel) {
1727                 short event= pupmenu("Object Operations%t|Select%x1|Deselect%x2|Delete%x4");
1728                 if(event>0) {
1729                         char *str="";
1730                         
1731                         if(event==1) {
1732                                 Scene *sce= G.scene;    // to be able to delete, scenes are set...
1733                                 outliner_do_object_operation(soops, &soops->tree, object_select_cb);
1734                                 if(G.scene != sce) set_scene(sce);
1735                                 
1736                                 str= "Select Objects";
1737                         }
1738                         else if(event==2) {
1739                                 outliner_do_object_operation(soops, &soops->tree, object_deselect_cb);
1740                                 str= "Deselect Objects";
1741                         }
1742                         else if(event==4) {
1743                                 outliner_do_object_operation(soops, &soops->tree, object_delete_cb);
1744                                 str= "Delete Objects";
1745                         }
1746                         
1747                         countall();
1748                         test_scene_constraints(); // for delete
1749                         
1750                         BIF_undo_push(str);
1751                         allqueue(REDRAWALL, 0); // yah... to be sure :)
1752                 }
1753         }
1754         else if(idlevel) {
1755                 if(idlevel==-1 || datalevel) error("Mixed selection");
1756                 else if(idlevel==-2) error("No operations available");
1757                 else {
1758                         short event= pupmenu("Data Operations%t|Unlink");
1759                         
1760                         if(event==1) {
1761                                 switch(idlevel) {
1762                                         case ID_MA:
1763                                                 outliner_do_libdata_operation(soops, &soops->tree, unlink_material_cb);
1764                                                 BIF_undo_push("Unlink material");
1765                                                 allqueue(REDRAWBUTSSHADING, 1);
1766                                                 break;
1767                                         case ID_TE:
1768                                                 outliner_do_libdata_operation(soops, &soops->tree, unlink_texture_cb);
1769                                                 allqueue(REDRAWBUTSSHADING, 1);
1770                                                 BIF_undo_push("Unlink texture");
1771                                                 break;
1772                                         default:
1773                                                 error("Not yet...");
1774                                 }
1775                                 allqueue(REDRAWOOPS, 0);
1776                         }
1777                 }
1778         }
1779         else if(datalevel) {
1780                 if(datalevel==-1) error("Mixed selection");
1781                 else {
1782                         error("Not yet...");
1783                         //pupmenu("Data Operations%t|Delete");
1784                 }
1785         }
1786         else error("Nothing selected");
1787         
1788 }
1789
1790
1791 /* ***************** DRAW *************** */
1792
1793 static void tselem_draw_icon(TreeStoreElem *tselem)
1794 {
1795         if(tselem->type) {
1796                 switch( tselem->type) {
1797                         case TSE_NLA:
1798                                 BIF_draw_icon(ICON_NLA); break;
1799                         case TSE_NLA_ACTION:
1800                                 BIF_draw_icon(ICON_ACTION); break;
1801                         case TSE_DEFGROUP_BASE:
1802                                 BIF_draw_icon(ICON_VERTEXSEL); break;
1803                         case TSE_BONE:
1804                         case TSE_EBONE:
1805                                 BIF_draw_icon(ICON_WPAINT_DEHLT); break;
1806                         case TSE_CONSTRAINT_BASE:
1807                                 BIF_draw_icon(ICON_CONSTRAINT); break;
1808                         case TSE_HOOKS_BASE:
1809                                 BIF_draw_icon(ICON_HOOK); break;
1810                         case TSE_HOOK:
1811                                 BIF_draw_icon(ICON_OBJECT); break;
1812                         case TSE_SCRIPT_BASE:
1813                                 BIF_draw_icon(ICON_TEXT); break;
1814                         default:
1815                                 BIF_draw_icon(ICON_DOT); break;
1816                 }
1817         }
1818         else {
1819                 switch( GS(tselem->id->name)) {
1820                         case ID_SCE:
1821                                 BIF_draw_icon(ICON_SCENE_DEHLT); break;
1822                         case ID_OB:
1823                                 BIF_draw_icon(ICON_OBJECT); break;
1824                         case ID_ME:
1825                                 BIF_draw_icon(ICON_MESH); break;
1826                         case ID_CU:
1827                                 BIF_draw_icon(ICON_CURVE); break;
1828                         case ID_MB:
1829                                 BIF_draw_icon(ICON_MBALL); break;
1830                         case ID_LT:
1831                                 BIF_draw_icon(ICON_LATTICE); break;
1832                         case ID_LA:
1833                                 BIF_draw_icon(ICON_LAMP_DEHLT); break;
1834                         case ID_MA:
1835                                 BIF_draw_icon(ICON_MATERIAL_DEHLT); break;
1836                         case ID_TE:
1837                                 BIF_draw_icon(ICON_TEXTURE_DEHLT); break;
1838                         case ID_IP:
1839                                 BIF_draw_icon(ICON_IPO_DEHLT); break;
1840                         case ID_IM:
1841                                 BIF_draw_icon(ICON_IMAGE_DEHLT); break;
1842                         case ID_SO:
1843                                 BIF_draw_icon(ICON_SPEAKER); break;
1844                         case ID_AR:
1845                                 BIF_draw_icon(ICON_ARMATURE_DEHLT); break;
1846                         case ID_CA:
1847                                 BIF_draw_icon(ICON_CAMERA_DEHLT); break;
1848                         case ID_KE:
1849                                 BIF_draw_icon(ICON_EDIT_DEHLT); break;
1850                         case ID_WO:
1851                                 BIF_draw_icon(ICON_WORLD_DEHLT); break;
1852                         case ID_AC:
1853                                 BIF_draw_icon(ICON_ACTION); break;
1854                         case ID_NLA:
1855                                 BIF_draw_icon(ICON_NLA); break;
1856                         case ID_TXT:
1857                                 BIF_draw_icon(ICON_SCRIPT); break;
1858                 }
1859         }
1860 }
1861
1862 static void outliner_draw_iconrow(SpaceOops *soops, ListBase *lb, int level, int *offsx, int ys)
1863 {
1864         TreeElement *te;
1865         TreeStoreElem *tselem;
1866         int active;
1867
1868         for(te= lb->first; te; te= te->next) {
1869                 tselem= TREESTORE(te);
1870                 
1871                 /* object hierarchy always, further constrained on level */
1872                 if(level<1 || (tselem->type==0 && te->idcode==ID_OB)) {
1873
1874                         /* active blocks get white circle */
1875                         active= 0;
1876                         if(tselem->type==0) {
1877                                 if(te->idcode==ID_OB) active= (OBACT==(Object *)tselem->id);
1878                                 else if(G.obpose && G.obpose->data==tselem->id) active= 1;
1879                                 else if(G.obedit && G.obedit->data==tselem->id) active= 1;
1880                                 else active= tree_element_active(soops, te, 0);
1881                         }
1882                         else active= tree_element_type_active(soops, te, tselem, 0);
1883                         
1884                         if(active) {
1885                                 uiSetRoundBox(15);
1886                                 glColor4ub(255, 255, 255, 100);
1887                                 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);
1888                                 glEnable(GL_BLEND);
1889                         }
1890                         
1891                         glRasterPos2i(*offsx, ys);
1892                         tselem_draw_icon(tselem);
1893                         te->xs= *offsx;
1894                         te->ys= ys;
1895                         te->xend= *offsx+OL_X;
1896                         te->flag |= TE_ICONROW; // for click
1897                         
1898                         (*offsx) += OL_X;
1899                 }
1900                 
1901                 outliner_draw_iconrow(soops, &te->subtree, level+1, offsx, ys);
1902         }
1903         
1904 }
1905
1906 static void outliner_draw_tree_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
1907 {
1908         TreeElement *ten;
1909         TreeStoreElem *tselem;
1910         int offsx= 0, active=0; // active=1 active obj, else active data
1911         
1912         tselem= TREESTORE(te);
1913
1914         if(*starty >= soops->v2d.cur.ymin && *starty<= soops->v2d.cur.ymax) {
1915                 
1916                 glEnable(GL_BLEND);
1917
1918                 /* colors for active/selected data */
1919                 if(tselem->type==0) {
1920                         if(te->idcode==ID_SCE) {
1921                                 if(tselem->id == (ID *)G.scene) {
1922                                         glColor4ub(255, 255, 255, 100);
1923                                         active= 2;
1924                                 }
1925                         }
1926                         else if(te->idcode==ID_OB) {
1927                                 Object *ob= (Object *)tselem->id;
1928                                 
1929                                 if(ob==OBACT || (ob->flag & SELECT)) {
1930                                         char col[4];
1931                                         
1932                                         active= 2;
1933                                         if(ob==OBACT) {
1934                                                 BIF_GetThemeColorType4ubv(TH_ACTIVE, SPACE_VIEW3D, col);
1935                                                 active= 1;
1936                                         }
1937                                         else BIF_GetThemeColorType4ubv(TH_SELECT, SPACE_VIEW3D, col);
1938                                         col[3]= 100;
1939                                         glColor4ubv(col);
1940                                 }
1941                         }
1942                         else if(G.obpose && G.obpose->data==tselem->id) {
1943                                 glColor4ub(255, 255, 255, 100);
1944                                 active= 2;
1945                         }
1946                         else if(G.obedit && G.obedit->data==tselem->id) {
1947                                 glColor4ub(255, 255, 255, 100);
1948                                 active= 2;
1949                         }
1950                         else {
1951                                 if(tree_element_active(soops, te, 0)) {
1952                                         glColor4ub(220, 220, 255, 100);
1953                                         active= 2;
1954                                 }
1955                         }
1956                 }
1957                 else {
1958                         if( tree_element_type_active(soops, te, tselem, 0) ) active= 2;
1959                         glColor4ub(220, 220, 255, 100);
1960                 }
1961                 
1962                 /* active circle */
1963                 if(active) {
1964                         uiSetRoundBox(15);
1965                         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);
1966                         glEnable(GL_BLEND);
1967                         
1968                         te->flag |= TE_ACTIVE; // for lookup in display hierarchies
1969                 }
1970                 
1971                 /* open/close icon, only when sublevels, except for scene */
1972                 if(te->subtree.first || te->idcode==ID_SCE) {
1973                         if(tselem->type==0 && (te->idcode==ID_OB || te->idcode==ID_SCE))
1974                                 glRasterPos2i(startx, *starty+2); // icons a bit higher
1975                         else
1976                                 glRasterPos2i(startx+5, *starty+2); // icons a bit higher
1977                         if(tselem->flag & TSE_CLOSED) 
1978                                 BIF_draw_icon(ICON_TRIA_CLOSED);
1979                         else
1980                                 BIF_draw_icon(ICON_TRIA_OPEN);
1981                 }
1982                 offsx+= OL_X;
1983                 
1984                 /* datatype icon */
1985                 
1986                 glRasterPos2i(startx+offsx, *starty+2); // icons a bit higher
1987                 tselem_draw_icon(tselem);
1988                 offsx+= OL_X;
1989                 glDisable(GL_BLEND);
1990
1991                 /* name */
1992                 if(active==1) BIF_ThemeColor(TH_TEXT_HI); 
1993                 else BIF_ThemeColor(TH_TEXT);
1994                 glRasterPos2i(startx+offsx, *starty+5);
1995                 BIF_RasterPos(startx+offsx, *starty+5);
1996                 BIF_DrawString(G.font, te->name, 0);
1997                 
1998                 offsx+= OL_X + BIF_GetStringWidth(G.font, te->name, 0);
1999                 
2000                 /* closed item, we draw the icons, not when it's a scene though */
2001                 if(tselem->flag & TSE_CLOSED) {
2002                         if(te->subtree.first) {
2003                                 if(tselem->type==0 && te->idcode==ID_SCE);
2004                                 else {
2005                                         int tempx= startx+offsx;
2006                                         // divider
2007                                         BIF_ThemeColorShade(TH_BACK, -40);
2008                                         glRecti(tempx -10, *starty+4, tempx -8, *starty+OL_H-4);
2009
2010                                         glEnable(GL_BLEND);
2011                                         glPixelTransferf(GL_ALPHA_SCALE, 0.5);
2012                                         outliner_draw_iconrow(soops, &te->subtree, 0, &tempx, *starty+2);
2013                                         glPixelTransferf(GL_ALPHA_SCALE, 1.0);
2014                                         glDisable(GL_BLEND);
2015                                 }
2016                         }
2017                 }
2018         }       
2019         /* store coord and continue */
2020         te->xs= startx;
2021         te->ys= *starty;
2022         te->xend= startx+offsx;
2023                 
2024         *starty-= OL_H;
2025         
2026         if((tselem->flag & TSE_CLOSED)==0) {
2027                 for(ten= te->subtree.first; ten; ten= ten->next) {
2028                         outliner_draw_tree_element(soops, ten, startx+OL_X, starty);
2029                 }
2030         }
2031 }
2032
2033 static void outliner_draw_hierarchy(SpaceOops *soops, ListBase *lb, int startx, int *starty)
2034 {
2035         TreeElement *te;
2036         TreeStoreElem *tselem;
2037         int y1, y2;
2038         
2039         if(lb->first==NULL) return;
2040         
2041         y1=y2= *starty; /* for vertical lines between objects */
2042         for(te=lb->first; te; te= te->next) {
2043                 y2= *starty;
2044                 tselem= TREESTORE(te);
2045                 
2046                 /* horizontal line? */
2047                 if(tselem->type==0 && (te->idcode==ID_OB || te->idcode==ID_SCE)) 
2048                         glRecti(startx, *starty, startx+OL_X, *starty-1);
2049                         
2050                 *starty-= OL_H;
2051                 
2052                 if((tselem->flag & TSE_CLOSED)==0)
2053                         outliner_draw_hierarchy(soops, &te->subtree, startx+OL_X, starty);
2054         }
2055         
2056         /* vertical line */
2057         te= lb->last;
2058         if(te->parent || lb->first!=lb->last) {
2059                 tselem= TREESTORE(te);
2060                 if(tselem->type==0 && te->idcode==ID_OB) {
2061                         
2062                         glRecti(startx, y1+OL_H, startx+1, y2);
2063                 }
2064         }
2065 }
2066
2067 static void outliner_draw_selection(SpaceOops *soops, ListBase *lb, int *starty) 
2068 {
2069         TreeElement *te;
2070         TreeStoreElem *tselem;
2071         
2072         for(te= lb->first; te; te= te->next) {
2073                 tselem= TREESTORE(te);
2074                 
2075                 /* selection status */
2076                 if(tselem->flag & TSE_SELECTED) {
2077                         glRecti(0, *starty+1, (int)soops->v2d.mask.xmax, *starty+OL_H-1);
2078                 }
2079                 *starty-= OL_H;
2080                 if((tselem->flag & TSE_CLOSED)==0) outliner_draw_selection(soops, &te->subtree, starty);
2081         }
2082 }
2083
2084 static void outliner_draw_tree(SpaceOops *soops)
2085 {
2086         TreeElement *te;
2087         int starty, startx;
2088         
2089 #ifdef INTERNATIONAL
2090         FTF_SetFontSize('l');
2091 #endif
2092         
2093         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); // only once
2094
2095         // selection first
2096         glColor3ub(125, 150, 175);
2097         starty= soops->v2d.tot.ymax-OL_H;
2098         outliner_draw_selection(soops, &soops->tree, &starty);
2099         
2100         // black hierarchy lines
2101         glColor3ub(0,0,0);
2102         starty= soops->v2d.tot.ymax-OL_H/2;
2103         startx= 6;
2104         outliner_draw_hierarchy(soops, &soops->tree, startx, &starty);
2105         
2106         // items themselves
2107         starty= soops->v2d.tot.ymax-OL_H;
2108         startx= 0;
2109         for(te= soops->tree.first; te; te= te->next) {
2110                 outliner_draw_tree_element(soops, te, startx, &starty);
2111         }
2112         
2113 }
2114
2115 static void outliner_back(SpaceOops *soops)
2116 {
2117         int ystart;
2118         
2119         BIF_ThemeColorShade(TH_BACK, 6);
2120         ystart= soops->v2d.tot.ymax;
2121         ystart= OL_H*(ystart/(OL_H));
2122         
2123         while(ystart > soops->v2d.cur.ymin) {
2124                 glRecti(0, ystart, (int)soops->v2d.mask.xmax, ystart+OL_H);
2125                 ystart-= 2*OL_H;
2126         }
2127 }
2128
2129 static char bone_newname[40];   // temp storage for bone rename, bones are 32 max.
2130
2131 static void namebutton_cb(void *voidp, void *arg2_unused)
2132 {
2133         SpaceOops *soops= voidp;
2134         TreeStore *ts= soops->treestore;
2135         TreeStoreElem *tselem;
2136         int a;
2137         
2138         if(ts) {
2139                 /* only one namebutton can exist */
2140                 for(a=0, tselem= ts->data; a<ts->usedelem; a++, tselem++) {
2141                         if(tselem->flag & TSE_TEXTBUT) {
2142                                 if(tselem->type==0) {
2143                                         test_idbutton(tselem->id->name+2);      // library.c, unique name and alpha sort
2144                                 }
2145                                 else {
2146                                         TreeElement *te= outliner_find_tree_element(&soops->tree, a);
2147                                         
2148                                         if(te) {
2149                                                 switch(tselem->type) {
2150                                                 case TSE_DEFGROUP:
2151                                                         unique_vertexgroup_name(te->directdata, (Object *)tselem->id); //       id = object
2152                                                         break;
2153                                                 case TSE_NLA_ACTION:
2154                                                         test_idbutton(tselem->id->name+2);
2155                                                         break;
2156                                                 case TSE_EBONE:
2157                                                         if(G.obedit && G.obedit->data==(ID *)tselem->id) {
2158                                                                 extern void validate_editbonebutton(EditBone *);
2159                                                                 EditBone *ebone= te->directdata;
2160                                                                 validate_editbonebutton(ebone);
2161                                                         }
2162                                                         allqueue(REDRAWOOPS, 0);
2163                                                         allqueue(REDRAWVIEW3D, 1);
2164                                                         allqueue(REDRAWBUTSEDIT, 0);
2165                                                         break;
2166                                                 case TSE_BONE:
2167                                                         {
2168                                                                 Bone *bone= te->directdata;
2169                                                                 // always make current object active
2170                                                                 tree_element_active_object(soops, te);
2171
2172                                                                 // dangerous call, it re-allocs the Armature bones, exits editmode too
2173                                                                 rename_bone_ext(bone->name, bone_newname);
2174                                                         }
2175                                                         allqueue(REDRAWOOPS, 0);
2176                                                         allqueue(REDRAWVIEW3D, 1);
2177                                                         allqueue(REDRAWBUTSEDIT, 0);
2178                                                         break;
2179                                                 }
2180                                         }
2181                                 }
2182                                 tselem->flag &= ~TSE_TEXTBUT;
2183                         }
2184                 }
2185                 scrarea_queue_redraw(curarea);
2186         }
2187 }
2188
2189 void outliner_buttons(uiBlock *block, SpaceOops *soops, ListBase *lb)
2190 {
2191         uiBut *bt;
2192         TreeElement *te;
2193         TreeStoreElem *tselem;
2194         int dx, len;
2195         
2196         for(te= lb->first; te; te= te->next) {
2197                 tselem= TREESTORE(te);
2198                 if(tselem->flag & TSE_TEXTBUT) {
2199                         
2200                         // darn bones have complex renaming conventions... cannot edit name itself in button
2201                         if(tselem->type==TSE_BONE) {
2202                                 strncpy(bone_newname, te->name, 32); // bone_newname is global
2203                                 te->name= bone_newname;
2204                                 len= 24;
2205                         }
2206                         else len= 19;
2207                         
2208                         dx= BIF_GetStringWidth(G.font, te->name, 0);
2209                         if(dx<50) dx= 50;
2210                         
2211                         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, "");
2212                         uiButSetFunc(bt, namebutton_cb, soops, NULL);
2213
2214                         // signal for button to open
2215                         addqueue(curarea->win, BUT_ACTIVATE, OL_NAMEBUTTON);
2216                 }
2217                 if((tselem->flag & TSE_CLOSED)==0) outliner_buttons(block, soops, &te->subtree);
2218         }
2219 }
2220
2221 void draw_outliner(ScrArea *sa, SpaceOops *soops)
2222 {
2223         uiBlock *block;
2224         int sizey;
2225         short ofsx, ofsy;
2226         
2227         calc_scrollrcts(sa, G.v2d, sa->winx, sa->winy);
2228
2229         if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
2230                 if(G.v2d->scroll) {     
2231                         ofsx= sa->winrct.xmin;  /* because mywin */
2232                         ofsy= sa->winrct.ymin;
2233                         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); 
2234                         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);
2235                 }
2236         }
2237         
2238         outliner_build_tree(soops); // always 
2239         sizey= 0;
2240         outliner_height(soops, &soops->tree, &sizey);
2241         
2242         /* we init all tot rect vars, only really needed on window size change tho */
2243         G.v2d->tot.xmin= 0.0;
2244         G.v2d->tot.xmax= (G.v2d->mask.xmax-G.v2d->mask.xmin);
2245         G.v2d->tot.ymax= 0.0;
2246         G.v2d->tot.ymin= -sizey*OL_H;
2247         test_view2d(G.v2d, sa->winx, sa->winy);
2248
2249         // align on top window if cur bigger than tot
2250         if(G.v2d->cur.ymax-G.v2d->cur.ymin > sizey*OL_H) {
2251                 G.v2d->cur.ymax= 0.0;
2252                 G.v2d->cur.ymin= -(G.v2d->mask.ymax-G.v2d->mask.ymin);
2253         }
2254
2255         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);
2256
2257         /* draw outliner stuff */
2258         outliner_back(soops);
2259         outliner_draw_tree(soops);
2260
2261         /* restore viewport */
2262         mywinset(sa->win);
2263         
2264         /* ortho corrected */
2265         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);
2266         
2267         block= uiNewBlock(&sa->uiblocks, "outliner buttons", UI_EMBOSS, UI_HELV, sa->win);
2268         outliner_buttons(block, soops, &soops->tree);
2269         uiDrawBlock(block);
2270         
2271         /* drawoopsspace handles sliders */
2272 }
2273
2274