2.5
[blender.git] / source / blender / editors / space_outliner / outliner.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_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;
1324                 int op;
1325
1326                 ed= scene->ed;
1327                 if(!ed)
1328                         return;
1329
1330                 seq= ed->seqbasep->first;
1331                 if(!seq)
1332                         return;
1333
1334                 while(seq) {
1335                         op= need_add_seq_dup(seq);
1336                         if(op==1)
1337                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE, 0);
1338                         else if(op==0) {
1339                                 ten= outliner_add_element(soops, &soops->tree, (void*)seq, NULL, TSE_SEQUENCE_DUP, 0);
1340                                 add_seq_dup(soops, seq, ten, 0);
1341                         }
1342                         seq= seq->next;
1343                 }
1344         }
1345         else if(soops->outlinevis==SO_DATABLOCKS) {
1346                 PointerRNA mainptr;
1347
1348                 RNA_main_pointer_create(mainvar, &mainptr);
1349
1350                 ten= outliner_add_element(soops, &soops->tree, (void*)&mainptr, NULL, TSE_RNA_STRUCT, -1);
1351
1352                 if(show_opened)  {
1353                         tselem= TREESTORE(ten);
1354                         tselem->flag &= ~TSE_CLOSED;
1355                 }
1356         }
1357         else if(soops->outlinevis==SO_USERDEF) {
1358                 PointerRNA userdefptr;
1359
1360                 RNA_pointer_create(NULL, NULL, &RNA_UserPreferences, &U, &userdefptr);
1361
1362                 ten= outliner_add_element(soops, &soops->tree, (void*)&userdefptr, NULL, TSE_RNA_STRUCT, -1);
1363
1364                 if(show_opened)  {
1365                         tselem= TREESTORE(ten);
1366                         tselem->flag &= ~TSE_CLOSED;
1367                 }
1368         }
1369         else {
1370                 ten= outliner_add_element(soops, &soops->tree, OBACT, NULL, 0, 0);
1371                 if(ten) ten->directdata= BASACT;
1372         }
1373
1374         outliner_sort(soops, &soops->tree);
1375 }
1376
1377 /* **************** INTERACTIVE ************* */
1378
1379 static int outliner_count_levels(SpaceOops *soops, ListBase *lb, int curlevel)
1380 {
1381         TreeElement *te;
1382         int level=curlevel, lev;
1383         
1384         for(te= lb->first; te; te= te->next) {
1385                 
1386                 lev= outliner_count_levels(soops, &te->subtree, curlevel+1);
1387                 if(lev>level) level= lev;
1388         }
1389         return level;
1390 }
1391
1392 static int outliner_has_one_flag(SpaceOops *soops, ListBase *lb, short flag, short curlevel)
1393 {
1394         TreeElement *te;
1395         TreeStoreElem *tselem;
1396         int level;
1397         
1398         for(te= lb->first; te; te= te->next) {
1399                 tselem= TREESTORE(te);
1400                 if(tselem->flag & flag) return curlevel;
1401                 
1402                 level= outliner_has_one_flag(soops, &te->subtree, flag, curlevel+1);
1403                 if(level) return level;
1404         }
1405         return 0;
1406 }
1407
1408 static void outliner_set_flag(SpaceOops *soops, ListBase *lb, short flag, short set)
1409 {
1410         TreeElement *te;
1411         TreeStoreElem *tselem;
1412         
1413         for(te= lb->first; te; te= te->next) {
1414                 tselem= TREESTORE(te);
1415                 if(set==0) tselem->flag &= ~flag;
1416                 else tselem->flag |= flag;
1417                 outliner_set_flag(soops, &te->subtree, flag, set);
1418         }
1419 }
1420
1421 void object_toggle_visibility_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1422 {
1423         Scene *scene= NULL;             // XXX
1424         Base *base= (Base *)te->directdata;
1425         
1426         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1427         if(base) {
1428                 base->object->restrictflag^=OB_RESTRICT_VIEW;
1429         }
1430 }
1431
1432 void outliner_toggle_visibility(Scene *scene, SpaceOops *soops)
1433 {
1434
1435         outliner_do_object_operation(scene, soops, &soops->tree, object_toggle_visibility_cb);
1436         
1437         BIF_undo_push("Outliner toggle selectability");
1438
1439         allqueue(REDRAWVIEW3D, 1);
1440         allqueue(REDRAWOOPS, 0);
1441         allqueue(REDRAWINFO, 1);
1442 }
1443
1444 static void object_toggle_selectability_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1445 {
1446         Scene *scene= NULL; // XXX
1447         Base *base= (Base *)te->directdata;
1448         
1449         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1450         if(base) {
1451                 base->object->restrictflag^=OB_RESTRICT_SELECT;
1452         }
1453 }
1454
1455 void outliner_toggle_selectability(Scene *scene, SpaceOops *soops)
1456 {
1457         
1458         outliner_do_object_operation(scene, soops, &soops->tree, object_toggle_selectability_cb);
1459         
1460         BIF_undo_push("Outliner toggle selectability");
1461
1462         allqueue(REDRAWVIEW3D, 1);
1463         allqueue(REDRAWOOPS, 0);
1464         allqueue(REDRAWINFO, 1);
1465 }
1466
1467 void object_toggle_renderability_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
1468 {
1469         Scene *scene= NULL;     // XXX
1470         Base *base= (Base *)te->directdata;
1471         
1472         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
1473         if(base) {
1474                 base->object->restrictflag^=OB_RESTRICT_RENDER;
1475         }
1476 }
1477
1478 void outliner_toggle_renderability(Scene *scene, SpaceOops *soops)
1479 {
1480
1481         outliner_do_object_operation(scene, soops, &soops->tree, object_toggle_renderability_cb);
1482         
1483         BIF_undo_push("Outliner toggle renderability");
1484
1485         allqueue(REDRAWVIEW3D, 1);
1486         allqueue(REDRAWOOPS, 0);
1487         allqueue(REDRAWINFO, 1);
1488 }
1489
1490 void outliner_toggle_visible(SpaceOops *soops)
1491 {
1492         
1493         if( outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1))
1494                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 0);
1495         else 
1496                 outliner_set_flag(soops, &soops->tree, TSE_CLOSED, 1);
1497
1498         BIF_undo_push("Outliner toggle visible");
1499 }
1500
1501 void outliner_toggle_selected(ARegion *ar, SpaceOops *soops)
1502 {
1503         
1504         if( outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1))
1505                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
1506         else 
1507                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 1);
1508         
1509         BIF_undo_push("Outliner toggle selected");
1510         soops->storeflag |= SO_TREESTORE_REDRAW;
1511 }
1512
1513
1514 static void outliner_openclose_level(SpaceOops *soops, ListBase *lb, int curlevel, int level, int open)
1515 {
1516         TreeElement *te;
1517         TreeStoreElem *tselem;
1518         
1519         for(te= lb->first; te; te= te->next) {
1520                 tselem= TREESTORE(te);
1521                 
1522                 if(open) {
1523                         if(curlevel<=level) tselem->flag &= ~TSE_CLOSED;
1524                 }
1525                 else {
1526                         if(curlevel>=level) tselem->flag |= TSE_CLOSED;
1527                 }
1528                 
1529                 outliner_openclose_level(soops, &te->subtree, curlevel+1, level, open);
1530         }
1531 }
1532
1533 /* return 1 when levels were opened */
1534 static int outliner_open_back(SpaceOops *soops, TreeElement *te)
1535 {
1536         TreeStoreElem *tselem;
1537         int retval= 0;
1538         
1539         for (te= te->parent; te; te= te->parent) {
1540                 tselem= TREESTORE(te);
1541                 if (tselem->flag & TSE_CLOSED) { 
1542                         tselem->flag &= ~TSE_CLOSED;
1543                         retval= 1;
1544                 }
1545         }
1546         return retval;
1547 }
1548
1549 /* This is not used anywhere at the moment */
1550 #if 0
1551 static void outliner_open_reveal(SpaceOops *soops, ListBase *lb, TreeElement *teFind, int *found)
1552 {
1553         TreeElement *te;
1554         TreeStoreElem *tselem;
1555         
1556         for (te= lb->first; te; te= te->next) {
1557                 /* check if this tree-element was the one we're seeking */
1558                 if (te == teFind) {
1559                         *found= 1;
1560                         return;
1561                 }
1562                 
1563                 /* try to see if sub-tree contains it then */
1564                 outliner_open_reveal(soops, &te->subtree, teFind, found);
1565                 if (*found) {
1566                         tselem= TREESTORE(te);
1567                         if (tselem->flag & TSE_CLOSED) 
1568                                 tselem->flag &= ~TSE_CLOSED;
1569                         return;
1570                 }
1571         }
1572 }
1573 #endif
1574
1575 void outliner_one_level(SpaceOops *soops, int add)
1576 {
1577         int level;
1578         
1579         level= outliner_has_one_flag(soops, &soops->tree, TSE_CLOSED, 1);
1580         if(add==1) {
1581                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level, 1);
1582         }
1583         else {
1584                 if(level==0) level= outliner_count_levels(soops, &soops->tree, 0);
1585                 if(level) outliner_openclose_level(soops, &soops->tree, 1, level-1, 0);
1586         }
1587         
1588         BIF_undo_push("Outliner show/hide one level");
1589 }
1590
1591 void outliner_page_up_down(Scene *scene, ARegion *ar, SpaceOops *soops, int up)
1592 {
1593         int dy= ar->v2d.mask.ymax-ar->v2d.mask.ymin;
1594         
1595         if(up == -1) dy= -dy;
1596         ar->v2d.cur.ymin+= dy;
1597         ar->v2d.cur.ymax+= dy;
1598         
1599         soops->storeflag |= SO_TREESTORE_REDRAW;
1600 }
1601
1602 /* **** do clicks on items ******* */
1603
1604 static int tree_element_active_renderlayer(TreeElement *te, TreeStoreElem *tselem, int set)
1605 {
1606         Scene *sce;
1607         
1608         /* paranoia check */
1609         if(te->idcode!=ID_SCE)
1610                 return 0;
1611         sce= (Scene *)tselem->id;
1612         
1613         if(set) {
1614                 sce->r.actlay= tselem->nr;
1615                 allqueue(REDRAWBUTSSCENE, 0);
1616         }
1617         else {
1618                 return sce->r.actlay==tselem->nr;
1619         }
1620         return 0;
1621 }
1622
1623 static void tree_element_set_active_object(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te)
1624 {
1625         TreeStoreElem *tselem= TREESTORE(te);
1626         Scene *sce;
1627         Base *base;
1628         Object *ob= NULL;
1629         int shift= 0; // XXX
1630         
1631         /* if id is not object, we search back */
1632         if(te->idcode==ID_OB) ob= (Object *)tselem->id;
1633         else {
1634                 ob= (Object *)outliner_search_back(soops, te, ID_OB);
1635                 if(ob==OBACT) return;
1636         }
1637         if(ob==NULL) return;
1638         
1639         sce= (Scene *)outliner_search_back(soops, te, ID_SCE);
1640         if(sce && scene != sce) {
1641 // XXX          if(obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
1642                 set_scene(sce);
1643         }
1644         
1645         /* find associated base in current scene */
1646         for(base= FIRSTBASE; base; base= base->next) 
1647                 if(base->object==ob) break;
1648         if(base) {
1649                 if(shift) {
1650                         /* swap select */
1651                         if(base->flag & SELECT)
1652                                 ED_base_object_select(base, BA_DESELECT);
1653                         else 
1654                                 ED_base_object_select(base, BA_SELECT);
1655                 }
1656                 else {
1657                         Base *b;
1658                         /* deleselect all */
1659                         for(b= FIRSTBASE; b; b= b->next) {
1660                                 b->flag &= ~SELECT;
1661                                 b->object->flag= b->flag;
1662                         }
1663                         ED_base_object_select(base, BA_SELECT);
1664                 }
1665                 if(C)
1666                         ED_base_object_activate(C, base); /* adds notifier */
1667         }
1668         
1669 // XXX  if(ob!=obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
1670 //      else countall(); /* exit_editmode calls f() */
1671 }
1672
1673 static int tree_element_active_material(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1674 {
1675         TreeElement *tes;
1676         Object *ob;
1677         
1678         /* we search for the object parent */
1679         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1680         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1681         
1682         /* searching in ob mat array? */
1683         tes= te->parent;
1684         if(tes->idcode==ID_OB) {
1685                 if(set) {
1686                         ob->actcol= te->index+1;
1687                         ob->colbits |= (1<<te->index);  // make ob material active too
1688                 }
1689                 else {
1690                         if(ob->actcol == te->index+1) 
1691                                 if(ob->colbits & (1<<te->index)) return 1;
1692                 }
1693         }
1694         /* or we search for obdata material */
1695         else {
1696                 if(set) {
1697                         ob->actcol= te->index+1;
1698                         ob->colbits &= ~(1<<te->index); // make obdata material active too
1699                 }
1700                 else {
1701                         if(ob->actcol == te->index+1)
1702                                 if( (ob->colbits & (1<<te->index))==0 ) return 1;
1703                 }
1704         }
1705         if(set) {
1706 // XXX          extern_set_butspace(F5KEY, 0);  // force shading buttons
1707                 BIF_preview_changed(ID_MA);
1708                 allqueue(REDRAWBUTSSHADING, 1);
1709                 allqueue(REDRAWNODE, 0);
1710                 allqueue(REDRAWOOPS, 0);
1711                 allqueue(REDRAWIPO, 0);
1712         }
1713         return 0;
1714 }
1715
1716 static int tree_element_active_texture(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1717 {
1718         TreeElement *tep;
1719         TreeStoreElem *tselem, *tselemp;
1720         Object *ob=OBACT;
1721         SpaceButs *sbuts=NULL;
1722         
1723         if(ob==NULL) return 0; // no active object
1724         
1725         tselem= TREESTORE(te);
1726         
1727         /* find buttons area (note, this is undefined really still, needs recode in blender) */
1728         /* XXX removed finding sbuts */
1729         
1730         /* where is texture linked to? */
1731         tep= te->parent;
1732         tselemp= TREESTORE(tep);
1733         
1734         if(tep->idcode==ID_WO) {
1735                 World *wrld= (World *)tselemp->id;
1736
1737                 if(set) {
1738                         if(sbuts) {
1739                                 sbuts->tabo= TAB_SHADING_TEX;   // hack from header_buttonswin.c
1740                                 sbuts->texfrom= 1;
1741                         }
1742 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1743                         wrld->texact= te->index;
1744                 }
1745                 else if(tselemp->id == (ID *)(scene->world)) {
1746                         if(wrld->texact==te->index) return 1;
1747                 }
1748         }
1749         else if(tep->idcode==ID_LA) {
1750                 Lamp *la= (Lamp *)tselemp->id;
1751                 if(set) {
1752                         if(sbuts) {
1753                                 sbuts->tabo= TAB_SHADING_TEX;   // hack from header_buttonswin.c
1754                                 sbuts->texfrom= 2;
1755                         }
1756 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1757                         la->texact= te->index;
1758                 }
1759                 else {
1760                         if(tselemp->id == ob->data) {
1761                                 if(la->texact==te->index) return 1;
1762                         }
1763                 }
1764         }
1765         else if(tep->idcode==ID_MA) {
1766                 Material *ma= (Material *)tselemp->id;
1767                 if(set) {
1768                         if(sbuts) {
1769                                 //sbuts->tabo= TAB_SHADING_TEX; // hack from header_buttonswin.c
1770                                 sbuts->texfrom= 0;
1771                         }
1772 // XXX                  extern_set_butspace(F6KEY, 0);  // force shading buttons texture
1773                         ma->texact= (char)te->index;
1774                         
1775                         /* also set active material */
1776                         ob->actcol= tep->index+1;
1777                 }
1778                 else if(tep->flag & TE_ACTIVE) {        // this is active material
1779                         if(ma->texact==te->index) return 1;
1780                 }
1781         }
1782         
1783         return 0;
1784 }
1785
1786
1787 static int tree_element_active_lamp(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1788 {
1789         Object *ob;
1790         
1791         /* we search for the object parent */
1792         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1793         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1794         
1795         if(set) {
1796 // XXX          extern_set_butspace(F5KEY, 0);
1797                 BIF_preview_changed(ID_LA);
1798                 allqueue(REDRAWBUTSSHADING, 1);
1799                 allqueue(REDRAWOOPS, 0);
1800                 allqueue(REDRAWIPO, 0);
1801         }
1802         else return 1;
1803         
1804         return 0;
1805 }
1806
1807 static int tree_element_active_world(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1808 {
1809         TreeElement *tep;
1810         TreeStoreElem *tselem=NULL;
1811         Scene *sce=NULL;
1812         
1813         tep= te->parent;
1814         if(tep) {
1815                 tselem= TREESTORE(tep);
1816                 sce= (Scene *)tselem->id;
1817         }
1818         
1819         if(set) {       // make new scene active
1820                 if(sce && scene != sce) {
1821 // XXX                  if(obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
1822                         set_scene(sce);
1823                 }
1824         }
1825         
1826         if(tep==NULL || tselem->id == (ID *)scene) {
1827                 if(set) {
1828 // XXX                  extern_set_butspace(F8KEY, 0);
1829                 }
1830                 else {
1831                         return 1;
1832                 }
1833         }
1834         return 0;
1835 }
1836
1837 static int tree_element_active_ipo(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
1838 {
1839         TreeElement *tes;
1840         TreeStoreElem *tselems=NULL;
1841         Object *ob;
1842         
1843         /* we search for the object parent */
1844         ob= (Object *)outliner_search_back(soops, te, ID_OB);
1845         if(ob==NULL || ob!=OBACT) return 0;     // just paranoia
1846         
1847         /* the parent of ipo */
1848         tes= te->parent;
1849         tselems= TREESTORE(tes);
1850         
1851         if(set) {
1852                 if(tes->idcode==ID_AC) {
1853                         if(ob->ipoflag & OB_ACTION_OB)
1854                                 ob->ipowin= ID_OB;
1855                         else if(ob->ipoflag & OB_ACTION_KEY)
1856                                 ob->ipowin= ID_KE;
1857                         else 
1858                                 ob->ipowin= ID_PO;
1859                 }
1860                 else ob->ipowin= tes->idcode;
1861                 
1862                 if(ob->ipowin==ID_MA) tree_element_active_material(scene, soops, tes, 1);
1863                 else if(ob->ipowin==ID_AC) {
1864                         bActionChannel *chan;
1865                         short a=0;
1866                         for(chan=ob->action->chanbase.first; chan; chan= chan->next) {
1867                                 if(a==te->index) break;
1868                                 if(chan->ipo) a++;
1869                         }
1870 // XXX                  deselect_actionchannels(ob->action, 0);
1871 //                      if (chan)
1872 //                              select_channel(ob->action, chan, SELECT_ADD);
1873                         allqueue(REDRAWACTION, ob->ipowin);
1874                         allqueue(REDRAWVIEW3D, ob->ipowin);
1875                 }
1876                 
1877                 allqueue(REDRAWIPO, ob->ipowin);
1878         }
1879         else {
1880                 if(tes->idcode==ID_AC) {
1881                         if(ob->ipoflag & OB_ACTION_OB)
1882                                 return ob->ipowin==ID_OB;
1883                         else if(ob->ipoflag & OB_ACTION_KEY)
1884                                 return ob->ipowin==ID_KE;
1885                         else if(ob->ipowin==ID_AC) {
1886                                 bActionChannel *chan;
1887                                 short a=0;
1888                                 for(chan=ob->action->chanbase.first; chan; chan= chan->next) {
1889                                         if(a==te->index) break;
1890                                         if(chan->ipo) a++;
1891                                 }
1892 // XXX                          if(chan==get_hilighted_action_channel(ob->action)) return 1;
1893                         }
1894                 }
1895                 else if(ob->ipowin==tes->idcode) {
1896                         if(ob->ipowin==ID_MA) {
1897                                 Material *ma= give_current_material(ob, ob->actcol);
1898                                 if(ma==(Material *)tselems->id) return 1;
1899                         }
1900                         else return 1;
1901                 }
1902         }
1903         return 0;
1904 }       
1905
1906 static int tree_element_active_defgroup(Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
1907 {
1908         Object *ob;
1909         
1910         /* id in tselem is object */
1911         ob= (Object *)tselem->id;
1912         if(set) {
1913                 ob->actdef= te->index+1;
1914                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1915                 allqueue(REDRAWVIEW3D, ob->ipowin);
1916         }
1917         else {
1918                 if(ob==OBACT)
1919                         if(ob->actdef== te->index+1) return 1;
1920         }
1921         return 0;
1922 }
1923
1924 static int tree_element_active_nla_action(TreeElement *te, TreeStoreElem *tselem, int set)
1925 {
1926         if(set) {
1927                 bActionStrip *strip= te->directdata;
1928                 if(strip) {
1929 // XXX                  deselect_nlachannel_keys(0);
1930                         strip->flag |= ACTSTRIP_SELECT;
1931                         allqueue(REDRAWNLA, 0);
1932                 }
1933         }
1934         else {
1935                 /* id in tselem is action */
1936                 bActionStrip *strip= te->directdata;
1937                 if(strip) {
1938                         if(strip->flag & ACTSTRIP_SELECT) return 1;
1939                 }
1940         }
1941         return 0;
1942 }
1943
1944 static int tree_element_active_posegroup(Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
1945 {
1946         Object *ob= (Object *)tselem->id;
1947         
1948         if(set) {
1949                 if (ob->pose) {
1950                         ob->pose->active_group= te->index+1;
1951                         allqueue(REDRAWBUTSEDIT, 0);
1952                 }
1953         }
1954         else {
1955                 if(ob==OBACT && ob->pose) {
1956                         if (ob->pose->active_group== te->index+1) return 1;
1957                 }
1958         }
1959         return 0;
1960 }
1961
1962 static int tree_element_active_posechannel(Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
1963 {
1964         Object *ob= (Object *)tselem->id;
1965         bPoseChannel *pchan= te->directdata;
1966         
1967         if(set) {
1968                 if(!(pchan->bone->flag & BONE_HIDDEN_P)) {
1969                         
1970 // XXX                  if(G.qual & LR_SHIFTKEY) deselectall_posearmature(ob, 2, 0);    // 2 = clear active tag
1971 //                      else deselectall_posearmature(ob, 0, 0);        // 0 = deselect 
1972                         pchan->bone->flag |= BONE_SELECTED|BONE_ACTIVE;
1973                         
1974                         allqueue(REDRAWVIEW3D, 0);
1975                         allqueue(REDRAWOOPS, 0);
1976                         allqueue(REDRAWACTION, 0);
1977                 }
1978         }
1979         else {
1980                 if(ob==OBACT && ob->pose) {
1981                         if (pchan->bone->flag & BONE_SELECTED) return 1;
1982                 }
1983         }
1984         return 0;
1985 }
1986
1987 static int tree_element_active_bone(Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
1988 {
1989         bArmature *arm= (bArmature *)tselem->id;
1990         Bone *bone= te->directdata;
1991         
1992         if(set) {
1993                 if(!(bone->flag & BONE_HIDDEN_P)) {
1994 // XXX                  if(G.qual & LR_SHIFTKEY) deselectall_posearmature(OBACT, 2, 0); // 2 is clear active tag
1995 //                      else deselectall_posearmature(OBACT, 0, 0);
1996                         bone->flag |= BONE_SELECTED|BONE_ACTIVE;
1997                         
1998                         allqueue(REDRAWVIEW3D, 0);
1999                         allqueue(REDRAWOOPS, 0);
2000                         allqueue(REDRAWACTION, 0);
2001                 }
2002         }
2003         else {
2004                 Object *ob= OBACT;
2005                 
2006                 if(ob && ob->data==arm) {
2007                         if (bone->flag & BONE_SELECTED) return 1;
2008                 }
2009         }
2010         return 0;
2011 }
2012
2013
2014 /* ebones only draw in editmode armature */
2015 static int tree_element_active_ebone(TreeElement *te, TreeStoreElem *tselem, int set)
2016 {
2017         EditBone *ebone= te->directdata;
2018 //      int shift= 0; // XXX
2019         
2020         if(set) {
2021                 if(!(ebone->flag & BONE_HIDDEN_A)) {
2022                         
2023 // XXX                  if(shift) deselectall_armature(2, 0);   // only clear active tag
2024 //                      else deselectall_armature(0, 0);        // deselect
2025
2026                         ebone->flag |= BONE_SELECTED|BONE_ROOTSEL|BONE_TIPSEL|BONE_ACTIVE;
2027                         // flush to parent?
2028                         if(ebone->parent && (ebone->flag & BONE_CONNECTED)) ebone->parent->flag |= BONE_TIPSEL;
2029                         
2030                         allqueue(REDRAWVIEW3D, 0);
2031                         allqueue(REDRAWOOPS, 0);
2032                         allqueue(REDRAWACTION, 0);
2033                 }
2034         }
2035         else {
2036                 if (ebone->flag & BONE_SELECTED) return 1;
2037         }
2038         return 0;
2039 }
2040
2041 static int tree_element_active_modifier(TreeElement *te, TreeStoreElem *tselem, int set)
2042 {
2043         if(set) {
2044 // XXX          extern_set_butspace(F9KEY, 0);
2045         }
2046         
2047         return 0;
2048 }
2049
2050 static int tree_element_active_psys(TreeElement *te, TreeStoreElem *tselem, int set)
2051 {
2052         if(set) {
2053 //              Object *ob= (Object *)tselem->id;
2054 //              ParticleSystem *psys= te->directdata;
2055                 
2056 // XXX          PE_change_act_psys(ob, psys);
2057 // XXX          extern_set_butspace(F7KEY, 0);
2058         }
2059         
2060         return 0;
2061 }
2062
2063 static int tree_element_active_constraint(TreeElement *te, TreeStoreElem *tselem, int set)
2064 {
2065         if(set) {
2066 // XXX          extern_set_butspace(F7KEY, 0);
2067         }
2068         
2069         return 0;
2070 }
2071
2072 static int tree_element_active_text(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2073 {
2074         // XXX removed
2075         return 0;
2076 }
2077
2078 /* generic call for ID data check or make/check active in UI */
2079 static int tree_element_active(Scene *scene, SpaceOops *soops, TreeElement *te, int set)
2080 {
2081
2082         switch(te->idcode) {
2083                 case ID_MA:
2084                         return tree_element_active_material(scene, soops, te, set);
2085                 case ID_WO:
2086                         return tree_element_active_world(scene, soops, te, set);
2087                 case ID_LA:
2088                         return tree_element_active_lamp(scene, soops, te, set);
2089                 case ID_IP:
2090                         return tree_element_active_ipo(scene, soops, te, set);
2091                 case ID_TE:
2092                         return tree_element_active_texture(scene, soops, te, set);
2093                 case ID_TXT:
2094                         return tree_element_active_text(scene, soops, te, set);
2095         }
2096         return 0;
2097 }
2098
2099 static int tree_element_active_pose(TreeElement *te, TreeStoreElem *tselem, int set)
2100 {
2101         Object *ob= (Object *)tselem->id;
2102         
2103         if(set) {
2104 // XXX          if(obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
2105 //              if(ob->flag & OB_POSEMODE) exit_posemode();
2106 //              else enter_posemode();
2107         }
2108         else {
2109                 if(ob->flag & OB_POSEMODE) return 1;
2110         }
2111         return 0;
2112 }
2113
2114 static int tree_element_active_sequence(TreeElement *te, TreeStoreElem *tselem, int set)
2115 {
2116         Sequence *seq= (Sequence*) te->directdata;
2117
2118         if(set) {
2119 // XXX          select_single_seq(seq, 1);
2120                 allqueue(REDRAWSEQ, 0);
2121         }
2122         else {
2123                 if(seq->flag & SELECT)
2124                         return(1);
2125         }
2126         return(0);
2127 }
2128
2129 static int tree_element_active_sequence_dup(Scene *scene, TreeElement *te, TreeStoreElem *tselem, int set)
2130 {
2131         Sequence *seq, *p;
2132         Editing *ed;
2133
2134         seq= (Sequence*)te->directdata;
2135         if(set==0) {
2136                 if(seq->flag & SELECT)
2137                         return(1);
2138                 return(0);
2139         }
2140
2141 // XXX  select_single_seq(seq, 1);
2142         ed= scene->ed;
2143         p= ed->seqbasep->first;
2144         while(p) {
2145                 if((!p->strip) || (!p->strip->stripdata) || (!p->strip->stripdata->name)) {
2146                         p= p->next;
2147                         continue;
2148                 }
2149
2150 //              if(!strcmp(p->strip->stripdata->name, seq->strip->stripdata->name))
2151 // XXX                  select_single_seq(p, 0);
2152                 p= p->next;
2153         }
2154         allqueue(REDRAWSEQ, 0);
2155         return(0);
2156 }
2157
2158 /* generic call for non-id data to make/check active in UI */
2159 /* Context can be NULL when set==0 */
2160 static int tree_element_type_active(bContext *C, Scene *scene, SpaceOops *soops, TreeElement *te, TreeStoreElem *tselem, int set)
2161 {
2162         
2163         switch(tselem->type) {
2164                 case TSE_NLA_ACTION:
2165                         return tree_element_active_nla_action(te, tselem, set);
2166                 case TSE_DEFGROUP:
2167                         return tree_element_active_defgroup(scene, te, tselem, set);
2168                 case TSE_BONE:
2169                         return tree_element_active_bone(scene, te, tselem, set);
2170                 case TSE_EBONE:
2171                         return tree_element_active_ebone(te, tselem, set);
2172                 case TSE_MODIFIER:
2173                         return tree_element_active_modifier(te, tselem, set);
2174                 case TSE_LINKED_OB:
2175                         if(set) tree_element_set_active_object(C, scene, soops, te);
2176                         else if(tselem->id==(ID *)OBACT) return 1;
2177                         break;
2178                 case TSE_LINKED_PSYS:
2179                         return tree_element_active_psys(te, tselem, set);
2180                         break;
2181                 case TSE_POSE_BASE:
2182                         return tree_element_active_pose(te, tselem, set);
2183                         break;
2184                 case TSE_POSE_CHANNEL:
2185                         return tree_element_active_posechannel(scene, te, tselem, set);
2186                 case TSE_CONSTRAINT:
2187                         return tree_element_active_constraint(te, tselem, set);
2188                 case TSE_R_LAYER:
2189                         return tree_element_active_renderlayer(te, tselem, set);
2190                 case TSE_POSEGRP:
2191                         return tree_element_active_posegroup(scene, te, tselem, set);
2192                 case TSE_SEQUENCE:
2193                         return tree_element_active_sequence(te, tselem, set);
2194                         break;
2195                 case TSE_SEQUENCE_DUP:
2196                         return tree_element_active_sequence_dup(scene, te, tselem, set);
2197                         break;
2198         }
2199         return 0;
2200 }
2201
2202 static int do_outliner_mouse_event(bContext *C, Scene *scene, ARegion *ar, SpaceOops *soops, TreeElement *te, short event, float *mval)
2203 {
2204         int shift= 0, ctrl= 0; // XXX
2205         
2206         if(mval[1]>te->ys && mval[1]<te->ys+OL_H) {
2207                 TreeStoreElem *tselem= TREESTORE(te);
2208                 int openclose= 0;
2209
2210                 /* open close icon, three things to check */
2211                 if(event==RETKEY || event==PADENTER) openclose= 1; // enter opens/closes always
2212                 else if((te->flag & TE_ICONROW)==0) {                           // hidden icon, no open/close
2213                         if( mval[0]>te->xs && mval[0]<te->xs+OL_X) openclose= 1;
2214                 }
2215
2216                 if(openclose) {
2217                         
2218                         /* all below close/open? */
2219                         if(shift) {
2220                                 tselem->flag &= ~TSE_CLOSED;
2221                                 outliner_set_flag(soops, &te->subtree, TSE_CLOSED, !outliner_has_one_flag(soops, &te->subtree, TSE_CLOSED, 1));
2222                         }
2223                         else {
2224                                 if(tselem->flag & TSE_CLOSED) tselem->flag &= ~TSE_CLOSED;
2225                                 else tselem->flag |= TSE_CLOSED;
2226                                 
2227                         }
2228                         
2229                         return 1;
2230                 }
2231                 /* name and first icon */
2232                 else if(mval[0]>te->xs && mval[0]<te->xend) {
2233                         
2234                         /* activate a name button? */
2235                         if(event==LEFTMOUSE) {
2236                         
2237                                 if (ctrl) {
2238                                         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)) 
2239                                                 error("Cannot edit builtin name");
2240                                         else if(ELEM3(tselem->type, TSE_SEQUENCE, TSE_SEQ_STRIP, TSE_SEQUENCE_DUP))
2241                                                 error("Cannot edit sequence name");
2242                                         else if(tselem->id->lib) {
2243 // XXX                                          error_libdata();
2244                                         } else if(te->idcode == ID_LI && te->parent) {
2245                                                 error("Cannot edit the path of an indirectly linked library");
2246                                         } else {
2247                                                 tselem->flag |= TSE_TEXTBUT;
2248                                         }
2249                                 } else {
2250                                         /* always makes active object */
2251                                         if(tselem->type!=TSE_SEQUENCE && tselem->type!=TSE_SEQ_STRIP && tselem->type!=TSE_SEQUENCE_DUP)
2252                                                 tree_element_set_active_object(C, scene, soops, te);
2253                                         
2254                                         if(tselem->type==0) { // the lib blocks
2255                                                 /* editmode? */
2256                                                 if(te->idcode==ID_SCE) {
2257                                                         if(scene!=(Scene *)tselem->id) {
2258 // XXX                                                          if(obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
2259                                                                 set_scene((Scene *)tselem->id);
2260                                                         }
2261                                                 }
2262                                                 else if(ELEM5(te->idcode, ID_ME, ID_CU, ID_MB, ID_LT, ID_AR)) {
2263 // XXX                                                  if(obedit) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
2264 //                                                      else {
2265 //                                                              enter_editmode(EM_WAITCURSOR);
2266 //                                                              extern_set_butspace(F9KEY, 0);
2267 //                                              }
2268                                                 } else {        // rest of types
2269                                                         tree_element_active(scene, soops, te, 1);
2270                                                 }
2271                                                 
2272                                         }
2273                                         else tree_element_type_active(C, scene, soops, te, tselem, 1);
2274                                 }
2275                         }
2276                         else if(event==RIGHTMOUSE) {
2277                                 /* select object that's clicked on and popup context menu */
2278                                 if (!(tselem->flag & TSE_SELECTED)) {
2279                                 
2280                                         if ( outliner_has_one_flag(soops, &soops->tree, TSE_SELECTED, 1) )
2281                                                 outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
2282                                 
2283                                         tselem->flag |= TSE_SELECTED;
2284                                         /* redraw, same as outliner_select function */
2285                                         soops->storeflag |= SO_TREESTORE_REDRAW;
2286 // XXX                                  screen_swapbuffers();
2287                                 }
2288                                 
2289                                 outliner_operation_menu(scene, ar, soops);
2290                         }
2291                         return 1;
2292                 }
2293         }
2294         
2295         for(te= te->subtree.first; te; te= te->next) {
2296                 if(do_outliner_mouse_event(C, scene, ar, soops, te, event, mval)) return 1;
2297         }
2298         return 0;
2299 }
2300
2301 /* event can enterkey, then it opens/closes */
2302 static int outliner_activate_click(bContext *C, wmOperator *op, wmEvent *event)
2303 {
2304         Scene *scene= CTX_data_scene(C);
2305         ARegion *ar= CTX_wm_region(C);
2306         SpaceOops *soops= (SpaceOops*)CTX_wm_space_data(C);
2307         TreeElement *te;
2308         float fmval[2];
2309         
2310         UI_view2d_region_to_view(&ar->v2d, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin, fmval, fmval+1);
2311         
2312         for(te= soops->tree.first; te; te= te->next) {
2313                 if(do_outliner_mouse_event(C, scene, ar, soops, te, event->type, fmval)) break;
2314         }
2315         
2316         if(te) {
2317                 BIF_undo_push("Outliner click event");
2318         }
2319         else 
2320                 outliner_select(ar, soops);
2321         
2322         ED_region_tag_redraw(ar);
2323
2324         return OPERATOR_FINISHED;
2325 }
2326
2327 void OUTLINER_OT_activate_click(wmOperatorType *ot)
2328 {
2329         ot->name= "Activate Click";
2330         ot->idname= "OUTLINER_OT_activate_click";
2331         
2332         ot->invoke= outliner_activate_click;
2333         
2334         ot->poll= ED_operator_outliner_active;
2335 }
2336
2337
2338
2339 /* recursive helper for function below */
2340 static void outliner_set_coordinates_element(SpaceOops *soops, TreeElement *te, int startx, int *starty)
2341 {
2342         TreeStoreElem *tselem= TREESTORE(te);
2343         
2344         /* store coord and continue, we need coordinates for elements outside view too */
2345         te->xs= (float)startx;
2346         te->ys= (float)(*starty);
2347         *starty-= OL_H;
2348         
2349         if((tselem->flag & TSE_CLOSED)==0) {
2350                 TreeElement *ten;
2351                 for(ten= te->subtree.first; ten; ten= ten->next) {
2352                         outliner_set_coordinates_element(soops, ten, startx+OL_X, starty);
2353                 }
2354         }
2355         
2356 }
2357
2358 /* to retrieve coordinates with redrawing the entire tree */
2359 static void outliner_set_coordinates(ARegion *ar, SpaceOops *soops)
2360 {
2361         TreeElement *te;
2362         int starty= (int)(ar->v2d.tot.ymax)-OL_H;
2363         int startx= 0;
2364         
2365         for(te= soops->tree.first; te; te= te->next) {
2366                 outliner_set_coordinates_element(soops, te, startx, &starty);
2367         }
2368 }
2369
2370 static TreeElement *outliner_find_id(SpaceOops *soops, ListBase *lb, ID *id)
2371 {
2372         TreeElement *te, *tes;
2373         TreeStoreElem *tselem;
2374         
2375         for(te= lb->first; te; te= te->next) {
2376                 tselem= TREESTORE(te);
2377                 if(tselem->type==0) {
2378                         if(tselem->id==id) return te;
2379                         /* only deeper on scene or object */
2380                         if( te->idcode==ID_OB || te->idcode==ID_SCE) { 
2381                                 tes= outliner_find_id(soops, &te->subtree, id);
2382                                 if(tes) return tes;
2383                         }
2384                 }
2385         }
2386         return NULL;
2387 }
2388
2389 void outliner_show_active(Scene *scene, ARegion *ar, SpaceOops *so)
2390 {
2391         TreeElement *te;
2392         int xdelta, ytop;
2393         
2394         if(OBACT == NULL) return;
2395         
2396         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
2397         if(te) {
2398                 /* make te->ys center of view */
2399                 ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2400                 if(ytop>0) ytop= 0;
2401                 ar->v2d.cur.ymax= (float)ytop;
2402                 ar->v2d.cur.ymin= (float)(ytop-(ar->v2d.mask.ymax-ar->v2d.mask.ymin));
2403                 
2404                 /* make te->xs ==> te->xend center of view */
2405                 xdelta = (int)(te->xs - ar->v2d.cur.xmin);
2406                 ar->v2d.cur.xmin += xdelta;
2407                 ar->v2d.cur.xmax += xdelta;
2408                 
2409                 so->storeflag |= SO_TREESTORE_REDRAW;
2410         }
2411 }
2412
2413 void outliner_show_selected(Scene *scene, ARegion *ar, SpaceOops *so)
2414 {
2415         TreeElement *te;
2416         int xdelta, ytop;
2417         
2418         te= outliner_find_id(so, &so->tree, (ID *)OBACT);
2419         if(te) {
2420                 /* make te->ys center of view */
2421                 ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2422                 if(ytop>0) ytop= 0;
2423                 ar->v2d.cur.ymax= (float)ytop;
2424                 ar->v2d.cur.ymin= (float)(ytop-(ar->v2d.mask.ymax-ar->v2d.mask.ymin));
2425                 
2426                 /* make te->xs ==> te->xend center of view */
2427                 xdelta = (int)(te->xs - ar->v2d.cur.xmin);
2428                 ar->v2d.cur.xmin += xdelta;
2429                 ar->v2d.cur.xmax += xdelta;
2430                 
2431                 so->storeflag |= SO_TREESTORE_REDRAW;
2432         }
2433 }
2434
2435
2436 /* find next element that has this name */
2437 static TreeElement *outliner_find_named(SpaceOops *soops, ListBase *lb, char *name, int flags, TreeElement *prev, int *prevFound)
2438 {
2439         TreeElement *te, *tes;
2440         
2441         for (te= lb->first; te; te= te->next) {
2442                 int found;
2443                 
2444                 /* determine if match */
2445                 if(flags==OL_FIND)
2446                         found= BLI_strcasestr(te->name, name)!=NULL;
2447                 else if(flags==OL_FIND_CASE)
2448                         found= strstr(te->name, name)!=NULL;
2449                 else if(flags==OL_FIND_COMPLETE)
2450                         found= BLI_strcasecmp(te->name, name)==0;
2451                 else
2452                         found= strcmp(te->name, name)==0;
2453                 
2454                 if(found) {
2455                         /* name is right, but is element the previous one? */
2456                         if (prev) {
2457                                 if ((te != prev) && (*prevFound)) 
2458                                         return te;
2459                                 if (te == prev) {
2460                                         *prevFound = 1;
2461                                 }
2462                         }
2463                         else
2464                                 return te;
2465                 }
2466                 
2467                 tes= outliner_find_named(soops, &te->subtree, name, flags, prev, prevFound);
2468                 if(tes) return tes;
2469         }
2470
2471         /* nothing valid found */
2472         return NULL;
2473 }
2474
2475 /* tse is not in the treestore, we use its contents to find a match */
2476 static TreeElement *outliner_find_tse(SpaceOops *soops, TreeStoreElem *tse)
2477 {
2478         TreeStore *ts= soops->treestore;
2479         TreeStoreElem *tselem;
2480         int a;
2481         
2482         if(tse->id==NULL) return NULL;
2483         
2484         /* check if 'tse' is in treestore */
2485         tselem= ts->data;
2486         for(a=0; a<ts->usedelem; a++, tselem++) {
2487                 if((tse->type==0 && tselem->type==0) || (tselem->type==tse->type && tselem->nr==tse->nr)) {
2488                         if(tselem->id==tse->id) {
2489                                 break;
2490                         }
2491                 }
2492         }
2493         if(tselem) 
2494                 return outliner_find_tree_element(&soops->tree, a);
2495         
2496         return NULL;
2497 }
2498
2499
2500 /* Called to find an item based on name.
2501  */
2502 void outliner_find_panel(Scene *scene, ARegion *ar, SpaceOops *soops, int again, int flags) 
2503 {
2504         TreeElement *te= NULL;
2505         TreeElement *last_find;
2506         TreeStoreElem *tselem;
2507         int ytop, xdelta, prevFound=0;
2508         char name[33];
2509         
2510         /* get last found tree-element based on stored search_tse */
2511         last_find= outliner_find_tse(soops, &soops->search_tse);
2512         
2513         /* determine which type of search to do */
2514         if (again && last_find) {
2515                 /* no popup panel - previous + user wanted to search for next after previous */         
2516                 BLI_strncpy(name, soops->search_string, 33);
2517                 flags= soops->search_flags;
2518                 
2519                 /* try to find matching element */
2520                 te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2521                 if (te==NULL) {
2522                         /* no more matches after previous, start from beginning again */
2523                         prevFound= 1;
2524                         te= outliner_find_named(soops, &soops->tree, name, flags, last_find, &prevFound);
2525                 }
2526         }
2527         else {
2528                 /* pop up panel - no previous, or user didn't want search after previous */
2529                 strcpy(name, "");
2530 // XXX          if (sbutton(name, 0, sizeof(name)-1, "Find: ") && name[0]) {
2531 //                      te= outliner_find_named(soops, &soops->tree, name, flags, NULL, &prevFound);
2532 //              }
2533 //              else return; /* XXX RETURN! XXX */
2534         }
2535
2536         /* do selection and reveil */
2537         if (te) {
2538                 tselem= TREESTORE(te);
2539                 if (tselem) {
2540                         /* expand branches so that it will be visible, we need to get correct coordinates */
2541                         if( outliner_open_back(soops, te))
2542                                 outliner_set_coordinates(ar, soops);
2543                         
2544                         /* deselect all visible, and select found element */
2545                         outliner_set_flag(soops, &soops->tree, TSE_SELECTED, 0);
2546                         tselem->flag |= TSE_SELECTED;
2547                         
2548                         /* make te->ys center of view */
2549                         ytop= (int)(te->ys + (ar->v2d.mask.ymax-ar->v2d.mask.ymin)/2);
2550                         if(ytop>0) ytop= 0;
2551                         ar->v2d.cur.ymax= (float)ytop;
2552                         ar->v2d.cur.ymin= (float)(ytop-(ar->v2d.mask.ymax-ar->v2d.mask.ymin));
2553                         
2554                         /* make te->xs ==> te->xend center of view */
2555                         xdelta = (int)(te->xs - ar->v2d.cur.xmin);
2556                         ar->v2d.cur.xmin += xdelta;
2557                         ar->v2d.cur.xmax += xdelta;
2558                         
2559                         /* store selection */
2560                         soops->search_tse= *tselem;
2561                         
2562                         BLI_strncpy(soops->search_string, name, 33);
2563                         soops->search_flags= flags;
2564                         
2565                         /* redraw */
2566                         soops->storeflag |= SO_TREESTORE_REDRAW;
2567                 }
2568         }
2569         else {
2570                 /* no tree-element found */
2571                 error("Not found: %s", name);
2572         }
2573 }
2574
2575 static int subtree_has_objects(SpaceOops *soops, ListBase *lb)
2576 {
2577         TreeElement *te;
2578         TreeStoreElem *tselem;
2579         
2580         for(te= lb->first; te; te= te->next) {
2581                 tselem= TREESTORE(te);
2582                 if(tselem->type==0 && te->idcode==ID_OB) return 1;
2583                 if( subtree_has_objects(soops, &te->subtree)) return 1;
2584         }
2585         return 0;
2586 }
2587
2588 static void tree_element_show_hierarchy(Scene *scene, SpaceOops *soops, ListBase *lb)
2589 {
2590         TreeElement *te;
2591         TreeStoreElem *tselem;
2592
2593         /* open all object elems, close others */
2594         for(te= lb->first; te; te= te->next) {
2595                 tselem= TREESTORE(te);
2596                 
2597                 if(tselem->type==0) {
2598                         if(te->idcode==ID_SCE) {
2599                                 if(tselem->id!=(ID *)scene) tselem->flag |= TSE_CLOSED;
2600                                         else tselem->flag &= ~TSE_CLOSED;
2601                         }
2602                         else if(te->idcode==ID_OB) {
2603                                 if(subtree_has_objects(soops, &te->subtree)) tselem->flag &= ~TSE_CLOSED;
2604                                 else tselem->flag |= TSE_CLOSED;
2605                         }
2606                 }
2607                 else tselem->flag |= TSE_CLOSED;
2608
2609                 if(tselem->flag & TSE_CLOSED); else tree_element_show_hierarchy(scene, soops, &te->subtree);
2610         }
2611         
2612 }
2613
2614 /* show entire object level hierarchy */
2615 void outliner_show_hierarchy(Scene *scene, SpaceOops *soops)
2616 {
2617         
2618         tree_element_show_hierarchy(scene, soops, &soops->tree);
2619         
2620         BIF_undo_push("Outliner show hierarchy");
2621 }
2622
2623 #if 0
2624 static void do_outliner_select(SpaceOops *soops, ListBase *lb, float y1, float y2, short *selecting)
2625 {
2626         TreeElement *te;
2627         TreeStoreElem *tselem;
2628         
2629         if(y1>y2) SWAP(float, y1, y2);
2630         
2631         for(te= lb->first; te; te= te->next) {
2632                 tselem= TREESTORE(te);
2633                 
2634                 if(te->ys + OL_H < y1) return;
2635                 if(te->ys < y2) {
2636                         if((te->flag & TE_ICONROW)==0) {
2637                                 if(*selecting == -1) {
2638                                         if( tselem->flag & TSE_SELECTED) *selecting= 0;
2639                                         else *selecting= 1;
2640                                 }
2641                                 if(*selecting) tselem->flag |= TSE_SELECTED;
2642                                 else tselem->flag &= ~TSE_SELECTED;
2643                         }
2644                 }
2645                 if((tselem->flag & TSE_CLOSED)==0) do_outliner_select(soops, &te->subtree, y1, y2, selecting);
2646         }
2647 }
2648 #endif
2649
2650 /* its own redraw loop... urm */
2651 void outliner_select(ARegion *ar, SpaceOops *so)
2652 {
2653 #if 0
2654         XXX
2655         float fmval[2], y1, y2;
2656         short yo=-1, selecting= -1;
2657         
2658         UI_view2d_region_to_view(&ar->v2d, event->x, event->y, fmval, fmval+1);
2659         
2660         y1= fmval[1];
2661
2662         while (get_mbut() & (L_MOUSE|R_MOUSE)) {
2663                 UI_view2d_region_to_view(&ar->v2d, event->x, event->y, fmval, fmval+1);
2664                 y2= fmval[1];
2665                 
2666                 if(yo!=mval[1]) {
2667                         /* select the 'ouliner row' */
2668                         do_outliner_select(so, &so->tree, y1, y2, &selecting);
2669                         yo= mval[1];
2670                         
2671                         so->storeflag |= SO_TREESTORE_REDRAW;
2672 // XXX                  screen_swapbuffers();
2673                 
2674                         y1= y2;
2675                 }
2676                 else PIL_sleep_ms(30);
2677         }
2678         
2679         BIF_undo_push("Outliner selection");
2680 #endif
2681 }
2682
2683 /* ************ SELECTION OPERATIONS ********* */
2684
2685 static void set_operation_types(SpaceOops *soops, ListBase *lb,
2686                                 int *scenelevel,
2687                                 int *objectlevel,
2688                                 int *idlevel,
2689                                 int *datalevel)
2690 {
2691         TreeElement *te;
2692         TreeStoreElem *tselem;
2693         
2694         for(te= lb->first; te; te= te->next) {
2695                 tselem= TREESTORE(te);
2696                 if(tselem->flag & TSE_SELECTED) {
2697                         if(tselem->type) {
2698                                 if(tselem->type==TSE_SEQUENCE)
2699                                         *datalevel= TSE_SEQUENCE;
2700                                 else if(tselem->type==TSE_SEQ_STRIP)
2701                                         *datalevel= TSE_SEQ_STRIP;
2702                                 else if(tselem->type==TSE_SEQUENCE_DUP)
2703                                         *datalevel= TSE_SEQUENCE_DUP;
2704                                 else if(*datalevel!=tselem->type) *datalevel= -1;
2705                         }
2706                         else {
2707                                 int idcode= GS(tselem->id->name);
2708                                 switch(idcode) {
2709                                         case ID_SCE:
2710                                                 *scenelevel= 1;
2711                                                 break;
2712                                         case ID_OB:
2713                                                 *objectlevel= 1;
2714                                                 break;
2715                                                 
2716                                         case ID_ME: case ID_CU: case ID_MB: case ID_LT:
2717                                         case ID_LA: case ID_AR: case ID_CA:
2718                                         case ID_MA: case ID_TE: case ID_IP: case ID_IM:
2719                                         case ID_SO: case ID_KE: case ID_WO: case ID_AC:
2720                                         case ID_NLA: case ID_TXT: case ID_GR:
2721                                                 if(*idlevel==0) *idlevel= idcode;
2722                                                 else if(*idlevel!=idcode) *idlevel= -1;
2723                                                         break;
2724                                 }
2725                         }
2726                 }
2727                 if((tselem->flag & TSE_CLOSED)==0) {
2728                         set_operation_types(soops, &te->subtree,
2729                                                                 scenelevel, objectlevel, idlevel, datalevel);
2730                 }
2731         }
2732 }
2733
2734 static void unlink_material_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2735 {
2736         Material **matar=NULL;
2737         int a, totcol=0;
2738         
2739         if( GS(tsep->id->name)==ID_OB) {
2740                 Object *ob= (Object *)tsep->id;
2741                 totcol= ob->totcol;
2742                 matar= ob->mat;
2743         }
2744         else if( GS(tsep->id->name)==ID_ME) {
2745                 Mesh *me= (Mesh *)tsep->id;
2746                 totcol= me->totcol;
2747                 matar= me->mat;
2748         }
2749         else if( GS(tsep->id->name)==ID_CU) {
2750                 Curve *cu= (Curve *)tsep->id;
2751                 totcol= cu->totcol;
2752                 matar= cu->mat;
2753         }
2754         else if( GS(tsep->id->name)==ID_MB) {
2755                 MetaBall *mb= (MetaBall *)tsep->id;
2756                 totcol= mb->totcol;
2757                 matar= mb->mat;
2758         }
2759
2760         for(a=0; a<totcol; a++) {
2761                 if(a==te->index && matar[a]) {
2762                         matar[a]->id.us--;
2763                         matar[a]= NULL;
2764                 }
2765         }
2766 }
2767
2768 static void unlink_texture_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2769 {
2770         MTex **mtex= NULL;
2771         int a;
2772         
2773         if( GS(tsep->id->name)==ID_MA) {
2774                 Material *ma= (Material *)tsep->id;
2775                 mtex= ma->mtex;
2776         }
2777         else if( GS(tsep->id->name)==ID_LA) {
2778                 Lamp *la= (Lamp *)tsep->id;
2779                 mtex= la->mtex;
2780         }
2781         else if( GS(tsep->id->name)==ID_WO) {
2782                 World *wrld= (World *)tsep->id;
2783                 mtex= wrld->mtex;
2784         }
2785         else return;
2786         
2787         for(a=0; a<MAX_MTEX; a++) {
2788                 if(a==te->index && mtex[a]) {
2789                         if(mtex[a]->tex) {
2790                                 mtex[a]->tex->id.us--;
2791                                 mtex[a]->tex= NULL;
2792                         }
2793                 }
2794         }
2795 }
2796
2797 static void unlink_group_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2798 {
2799         Group *group= (Group *)tselem->id;
2800         
2801         if(tsep) {
2802                 if( GS(tsep->id->name)==ID_OB) {
2803                         Object *ob= (Object *)tsep->id;
2804                         ob->dup_group= NULL;
2805                         group->id.us--;
2806                 }
2807         }
2808         else {
2809                 unlink_group(group);
2810         }
2811 }
2812
2813 static void outliner_do_libdata_operation(SpaceOops *soops, ListBase *lb, 
2814                                                                                  void (*operation_cb)(TreeElement *, TreeStoreElem *, TreeStoreElem *))
2815 {
2816         TreeElement *te;
2817         TreeStoreElem *tselem;
2818         
2819         for(te=lb->first; te; te= te->next) {
2820                 tselem= TREESTORE(te);
2821                 if(tselem->flag & TSE_SELECTED) {
2822                         if(tselem->type==0) {
2823                                 TreeStoreElem *tsep= TREESTORE(te->parent);
2824                                 operation_cb(te, tsep, tselem);
2825                         }
2826                 }
2827                 if((tselem->flag & TSE_CLOSED)==0) {
2828                         outliner_do_libdata_operation(soops, &te->subtree, operation_cb);
2829                 }
2830         }
2831 }
2832
2833 /* */
2834
2835 static void object_select_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2836 {
2837         Scene *scene= NULL;     // XXX
2838         Base *base= (Base *)te->directdata;
2839         
2840         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
2841         if(base && ((base->object->restrictflag & OB_RESTRICT_VIEW)==0)) {
2842                 base->flag |= SELECT;
2843                 base->object->flag |= SELECT;
2844         }
2845 }
2846
2847 static void object_deselect_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2848 {
2849         Scene *scene= NULL;
2850         Base *base= (Base *)te->directdata;
2851         
2852         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
2853         if(base) {
2854                 base->flag &= ~SELECT;
2855                 base->object->flag &= ~SELECT;
2856         }
2857 }
2858
2859 static void object_delete_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2860 {
2861         Scene *scene= NULL;
2862         Base *base= (Base *)te->directdata;
2863         
2864         if(base==NULL) base= object_in_scene((Object *)tselem->id, scene);
2865         if(base) {
2866                 // check also library later
2867 // XXX          if(obedit==base->object) exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR);
2868                 
2869                 if(base==BASACT) {
2870                         G.f &= ~(G_VERTEXPAINT+G_TEXTUREPAINT+G_WEIGHTPAINT+G_SCULPTMODE);
2871 // XXX                  setcursor_space(SPACE_VIEW3D, CURSOR_STD);
2872                 }
2873                 
2874 // XXX          free_and_unlink_base(base);
2875                 te->directdata= NULL;
2876                 tselem->id= NULL;
2877         }
2878 }
2879
2880 static void id_local_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2881 {
2882         if(tselem->id->lib && (tselem->id->flag & LIB_EXTERN)) {
2883                 tselem->id->lib= NULL;
2884                 tselem->id->flag= LIB_LOCAL;
2885                 new_id(0, tselem->id, 0);
2886         }
2887 }
2888
2889 static void group_linkobs2scene_cb(TreeElement *te, TreeStoreElem *tsep, TreeStoreElem *tselem)
2890 {
2891         Scene *scene= NULL;
2892         Group *group= (Group *)tselem->id;
2893         GroupObject *gob;
2894         Base *base;
2895         
2896         for(gob=group->gobject.first; gob; gob=gob->next) {
2897                 base= object_in_scene(gob->ob, scene);
2898                 if (base) {
2899                         base->object->flag |= SELECT;
2900                         base->flag |= SELECT;
2901                 } else {
2902                         /* link to scene */
2903                         base= MEM_callocN( sizeof(Base), "add_base");
2904                         BLI_addhead(&scene->base, base);
2905                         base->lay= (1<<20)-1; /*v3d->lay;*/ /* would be nice to use the 3d layer but the include's not here */
2906                         gob->ob->flag |= SELECT;
2907                         base->flag = gob->ob->flag;
2908                         base->object= gob->ob;
2909                         id_lib_extern((ID *)gob->ob); /* incase these are from a linked group */
2910                 }
2911         }
2912 }
2913
2914 static void outliner_do_object_operation(Scene *scene, SpaceOops *soops, ListBase *lb, 
2915                                                                                  void (*operation_cb)(TreeElement *, TreeStoreElem *, TreeStoreElem *))
2916 {
2917         TreeElement *te;
2918         TreeStoreElem *tselem;
2919         
2920         for(te=lb->first; te; te= te->next) {
2921                 tselem= TREESTORE(te);
2922                 if(tselem-&g