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