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