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