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