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