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