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