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