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