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