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