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