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