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