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