soc-2008-mxcurioni: merged changes to revision 23516
[blender.git] / source / blender / editors / space_buttons / buttons_context.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) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_armature_types.h"
34 #include "DNA_brush_types.h"
35 #include "DNA_lamp_types.h"
36 #include "DNA_material_types.h"
37 #include "DNA_modifier_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_particle_types.h"
43 #include "DNA_texture_types.h"
44 #include "DNA_world_types.h"
45
46 #include "BLI_listbase.h"
47
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_material.h"
51 #include "BKE_modifier.h"
52 #include "BKE_paint.h"
53 #include "BKE_particle.h"
54 #include "BKE_screen.h"
55 #include "BKE_utildefines.h"
56 #include "BKE_world.h"
57
58 #include "RNA_access.h"
59
60 #include "ED_armature.h"
61 #include "ED_screen.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65
66 #include "buttons_intern.h"     // own include
67
68 typedef struct ButsContextPath {
69         PointerRNA ptr[8];
70         int len;
71         int flag;
72 } ButsContextPath;
73
74 static int set_pointer_type(ButsContextPath *path, bContextDataResult *result, StructRNA *type)
75 {
76         PointerRNA *ptr;
77         int a;
78
79         for(a=0; a<path->len; a++) {
80                 ptr= &path->ptr[a];
81
82                 if(RNA_struct_is_a(ptr->type, type)) {
83                         CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
84                         return 1;
85                 }
86         }
87
88         return 0;
89 }
90
91 static PointerRNA *get_pointer_type(ButsContextPath *path, StructRNA *type)
92 {
93         PointerRNA *ptr;
94         int a;
95
96         for(a=0; a<path->len; a++) {
97                 ptr= &path->ptr[a];
98
99                 if(RNA_struct_is_a(ptr->type, type))
100                         return ptr;
101         }
102
103         return NULL;
104 }
105
106 /************************* Creating the Path ************************/
107
108 static int buttons_context_path_scene(ButsContextPath *path)
109 {
110         PointerRNA *ptr= &path->ptr[path->len-1];
111
112         /* this one just verifies */
113         return RNA_struct_is_a(ptr->type, &RNA_Scene);
114 }
115
116 static int buttons_context_path_world(ButsContextPath *path)
117 {
118         Scene *scene;
119         PointerRNA *ptr= &path->ptr[path->len-1];
120
121         /* if we already have a (pinned) world, we're done */
122         if(RNA_struct_is_a(ptr->type, &RNA_World)) {
123                 return 1;
124         }
125         /* if we have a scene, use the scene's world */
126         else if(buttons_context_path_scene(path)) {
127                 scene= path->ptr[path->len-1].data;
128
129                 RNA_id_pointer_create(&scene->world->id, &path->ptr[path->len]);
130                 path->len++;
131
132                 return 1;
133         }
134
135         /* no path to a world possible */
136         return 0;
137 }
138
139
140 static int buttons_context_path_object(ButsContextPath *path)
141 {
142         Scene *scene;
143         Object *ob;
144         PointerRNA *ptr= &path->ptr[path->len-1];
145
146         /* if we already have a (pinned) object, we're done */
147         if(RNA_struct_is_a(ptr->type, &RNA_Object)) {
148                 return 1;
149         }
150         /* if we have a scene, use the scene's active object */
151         else if(buttons_context_path_scene(path)) {
152                 scene= path->ptr[path->len-1].data;
153                 ob= (scene->basact)? scene->basact->object: NULL;
154
155                 if(ob) {
156                         RNA_id_pointer_create(&ob->id, &path->ptr[path->len]);
157                         path->len++;
158
159                         return 1;
160                 }
161         }
162
163         /* no path to a object possible */
164         return 0;
165 }
166
167 static int buttons_context_path_data(ButsContextPath *path, int type)
168 {
169         Object *ob;
170         PointerRNA *ptr= &path->ptr[path->len-1];
171
172         /* if we already have a data, we're done */
173         if(RNA_struct_is_a(ptr->type, &RNA_Mesh) && (type == -1 || type == OB_MESH)) return 1;
174         else if(RNA_struct_is_a(ptr->type, &RNA_Curve) && (type == -1 || ELEM3(type, OB_CURVE, OB_SURF, OB_FONT))) return 1;
175         else if(RNA_struct_is_a(ptr->type, &RNA_Armature) && (type == -1 || type == OB_ARMATURE)) return 1;
176         else if(RNA_struct_is_a(ptr->type, &RNA_MetaBall) && (type == -1 || type == OB_MBALL)) return 1;
177         else if(RNA_struct_is_a(ptr->type, &RNA_Lattice) && (type == -1 || type == OB_LATTICE)) return 1;
178         else if(RNA_struct_is_a(ptr->type, &RNA_Camera) && (type == -1 || type == OB_CAMERA)) return 1;
179         else if(RNA_struct_is_a(ptr->type, &RNA_Lamp) && (type == -1 || type == OB_LAMP)) return 1;
180         /* try to get an object in the path, no pinning supported here */
181         else if(buttons_context_path_object(path)) {
182                 ob= path->ptr[path->len-1].data;
183
184                 if(ob && (type == -1 || type == ob->type)) {
185                         RNA_id_pointer_create(ob->data, &path->ptr[path->len]);
186                         path->len++;
187
188                         return 1;
189                 }
190         }
191
192         /* no path to data possible */
193         return 0;
194 }
195
196 static int buttons_context_path_modifier(ButsContextPath *path)
197 {
198         Object *ob;
199
200         if(buttons_context_path_object(path)) {
201                 ob= path->ptr[path->len-1].data;
202
203                 if(ob && ELEM5(ob->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_LATTICE))
204                         return 1;
205         }
206
207         return 0;
208 }
209
210 static int buttons_context_path_material(ButsContextPath *path)
211 {
212         Object *ob;
213         PointerRNA *ptr= &path->ptr[path->len-1];
214         Material *ma;
215
216         /* if we already have a (pinned) material, we're done */
217         if(RNA_struct_is_a(ptr->type, &RNA_Material)) {
218                 return 1;
219         }
220         /* if we have an object, use the object material slot */
221         else if(buttons_context_path_object(path)) {
222                 ob= path->ptr[path->len-1].data;
223
224                 if(ob && ob->type && (ob->type<OB_LAMP)) {
225                         ma= give_current_material(ob, ob->actcol);
226                         RNA_id_pointer_create(&ma->id, &path->ptr[path->len]);
227                         path->len++;
228                         return 1;
229                 }
230         }
231
232         /* no path to a material possible */
233         return 0;
234 }
235
236 static Bone *find_active_bone(Bone *bone)
237 {
238         Bone *active;
239
240         for(; bone; bone=bone->next) {
241                 if(bone->flag & BONE_ACTIVE)
242                         return bone;
243
244                 active= find_active_bone(bone->childbase.first);
245                 if(active)
246                         return active;
247         }
248
249         return NULL;
250 }
251
252 static int buttons_context_path_bone(ButsContextPath *path)
253 {
254         bArmature *arm;
255         Bone *bone;
256         EditBone *edbo;
257
258         /* if we have an armature, get the active bone */
259         if(buttons_context_path_data(path, OB_ARMATURE)) {
260                 arm= path->ptr[path->len-1].data;
261
262                 if(arm->edbo) {
263                         for(edbo=arm->edbo->first; edbo; edbo=edbo->next) {
264                                 if(edbo->flag & BONE_ACTIVE) {
265                                         RNA_pointer_create(&arm->id, &RNA_EditBone, edbo, &path->ptr[path->len]);
266                                         path->len++;
267                                         return 1;
268                                 }
269                         }
270                 }
271                 else {
272                         bone= find_active_bone(arm->bonebase.first);
273
274                         if(bone) {
275                                 RNA_pointer_create(&arm->id, &RNA_Bone, bone, &path->ptr[path->len]);
276                                 path->len++;
277                                 return 1;
278                         }
279                 }
280         }
281
282         /* no path to a bone possible */
283         return 0;
284 }
285
286 static int buttons_context_path_particle(ButsContextPath *path)
287 {
288         Object *ob;
289         ParticleSystem *psys;
290
291         /* if we have an object, get the active particle system */
292         if(buttons_context_path_object(path)) {
293                 ob= path->ptr[path->len-1].data;
294
295                 if(ob && ob->type == OB_MESH) {
296                         psys= psys_get_current(ob);
297
298                         RNA_pointer_create(&ob->id, &RNA_ParticleSystem, psys, &path->ptr[path->len]);
299                         path->len++;
300                         return 1;
301                 }
302         }
303
304         /* no path to a particle system possible */
305         return 0;
306 }
307
308 static int buttons_context_path_brush(const bContext *C, ButsContextPath *path)
309 {
310         Scene *scene;
311         ToolSettings *ts;
312         Brush *br= NULL;
313         PointerRNA *ptr= &path->ptr[path->len-1];
314
315         /* if we already have a (pinned) brush, we're done */
316         if(RNA_struct_is_a(ptr->type, &RNA_Brush)) {
317                 return 1;
318         }
319         /* if we have a scene, use the toolsettings brushes */
320         else if(buttons_context_path_scene(path)) {
321                 scene= path->ptr[path->len-1].data;
322                 ts= scene->toolsettings;
323
324                 if(scene)
325                         br= paint_brush(paint_get_active(scene));
326
327                 if(br) {
328                         RNA_id_pointer_create(&br->id, &path->ptr[path->len]);
329                         path->len++;
330
331                         return 1;
332                 }
333         }
334
335         /* no path to a brush possible */
336         return 0;
337 }
338
339 static int buttons_context_path_texture(const bContext *C, ButsContextPath *path)
340 {
341         Material *ma;
342         Lamp *la;
343         Brush *br;
344         World *wo;
345         MTex *mtex;
346         Tex *tex;
347         PointerRNA *ptr= &path->ptr[path->len-1];
348
349         /* if we already have a (pinned) texture, we're done */
350         if(RNA_struct_is_a(ptr->type, &RNA_Texture)) {
351                 return 1;
352         }
353         /* try brush */
354         else if((path->flag & SB_BRUSH_TEX) && buttons_context_path_brush(C, path)) {
355                 br= path->ptr[path->len-1].data;
356
357                 if(br) {
358                         mtex= br->mtex[(int)br->texact];
359                         tex= (mtex)? mtex->tex: NULL;
360
361                         RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
362                         path->len++;
363                         return 1;
364                 }
365         }
366         /* try world */
367         else if((path->flag & SB_WORLD_TEX) && buttons_context_path_world(path)) {
368                 wo= path->ptr[path->len-1].data;
369
370                 if(wo) {
371                         mtex= wo->mtex[(int)wo->texact];
372                         tex= (mtex)? mtex->tex: NULL;
373
374                         RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
375                         path->len++;
376                         return 1;
377                 }
378         }
379         /* try material */
380         else if(buttons_context_path_material(path)) {
381                 ma= path->ptr[path->len-1].data;
382
383                 if(ma) {
384                         mtex= ma->mtex[(int)ma->texact];
385                         tex= (mtex)? mtex->tex: NULL;
386
387                         RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
388                         path->len++;
389                         return 1;
390                 }
391         }
392         /* try lamp */
393         else if(buttons_context_path_data(path, OB_LAMP)) {
394                 la= path->ptr[path->len-1].data;
395
396                 if(la) {
397                         mtex= la->mtex[(int)la->texact];
398                         tex= (mtex)? mtex->tex: NULL;
399
400                         RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
401                         path->len++;
402                         return 1;
403                 }
404         }
405         /* TODO: material nodes */
406
407         /* no path to a texture possible */
408         return 0;
409 }
410
411
412 static int buttons_context_path(const bContext *C, ButsContextPath *path, int mainb, int flag)
413 {
414         SpaceButs *sbuts= CTX_wm_space_buts(C);
415         ID *id;
416         int found;
417
418         memset(path, 0, sizeof(*path));
419         path->flag= flag;
420
421         /* if some ID datablock is pinned, set the root pointer */
422         if(sbuts->pinid) {
423                 id= sbuts->pinid;
424
425                 RNA_id_pointer_create(id, &path->ptr[0]);
426                 path->len++;
427         }
428
429         /* no pinned root, use scene as root */
430         if(path->len == 0) {
431                 id= (ID*)CTX_data_scene(C);
432                 RNA_id_pointer_create(id, &path->ptr[0]);
433                 path->len++;
434         }
435
436         /* now for each buttons context type, we try to construct a path,
437          * tracing back recursively */
438         switch(mainb) {
439                 case BCONTEXT_SCENE:
440                         found= buttons_context_path_scene(path);
441                         break;
442                 case BCONTEXT_WORLD:
443                         found= buttons_context_path_world(path);
444                         break;
445                 case BCONTEXT_OBJECT:
446                 case BCONTEXT_PHYSICS:
447                 case BCONTEXT_CONSTRAINT:
448                         found= buttons_context_path_object(path);
449                         break;
450                 case BCONTEXT_MODIFIER:
451                         found= buttons_context_path_modifier(path);
452                         break;
453                 case BCONTEXT_DATA:
454                         found= buttons_context_path_data(path, -1);
455                         break;
456                 case BCONTEXT_PARTICLE:
457                         found= buttons_context_path_particle(path);
458                         break;
459                 case BCONTEXT_MATERIAL:
460                         found= buttons_context_path_material(path);
461                         break;
462                 case BCONTEXT_TEXTURE:
463                         found= buttons_context_path_texture(C, path);
464                         break;
465                 case BCONTEXT_BONE:
466                 case BCONTEXT_BONE_CONSTRAINT:
467                         found= buttons_context_path_bone(path);
468                         if(!found)
469                                 found= buttons_context_path_data(path, OB_ARMATURE);
470                         break;
471                 default:
472                         found= 0;
473                         break;
474         }
475
476         return found;
477 }
478
479 void buttons_context_compute(const bContext *C, SpaceButs *sbuts)
480 {
481         ButsContextPath *path;
482         PointerRNA *ptr;
483         int a, pflag, flag= 0;
484
485         if(!sbuts->path)
486                 sbuts->path= MEM_callocN(sizeof(ButsContextPath), "ButsContextPath");
487         
488         path= sbuts->path;
489         pflag= (sbuts->flag & (SB_WORLD_TEX|SB_BRUSH_TEX));
490         
491         /* for each context, see if we can compute a valid path to it, if
492          * this is the case, we know we have to display the button */
493         for(a=0; a<BCONTEXT_TOT; a++) {
494                 if(buttons_context_path(C, path, a, pflag)) {
495                         flag |= (1<<a);
496
497                         /* setting icon for data context */
498                         if(a == BCONTEXT_DATA) {
499                                 ptr= &path->ptr[path->len-1];
500
501                                 if(ptr->type)
502                                         sbuts->dataicon= RNA_struct_ui_icon(ptr->type);
503                                 else
504                                         sbuts->dataicon= ICON_EMPTY_DATA;
505                         }
506                 }
507         }
508
509         /* always try to use the tab that was explicitly
510          * set to the user, so that once that context comes
511          * back, the tab is activated again */
512         sbuts->mainb= sbuts->mainbuser;
513
514         /* in case something becomes invalid, change */
515         if((flag & (1 << sbuts->mainb)) == 0) {
516                 if(flag & BCONTEXT_OBJECT) {
517                         sbuts->mainb= BCONTEXT_OBJECT;
518                 }
519                 else {
520                         for(a=0; a<BCONTEXT_TOT; a++) {
521                                 if(flag & (1 << a)) {
522                                         sbuts->mainb= a;
523                                         break;
524                                 }
525                         }
526                 }
527         }
528
529         buttons_context_path(C, path, sbuts->mainb, pflag);
530
531         if(!(flag & (1 << sbuts->mainb))) {
532                 if(flag & (1 << BCONTEXT_OBJECT))
533                         sbuts->mainb= BCONTEXT_OBJECT;
534                 else
535                         sbuts->mainb= BCONTEXT_SCENE;
536         }
537
538         sbuts->pathflag= flag;
539 }
540
541 /************************* Context Callback ************************/
542
543 int buttons_context(const bContext *C, const char *member, bContextDataResult *result)
544 {
545         SpaceButs *sbuts= CTX_wm_space_buts(C);
546         ButsContextPath *path= sbuts?sbuts->path:NULL;
547
548         if(!path)
549                 return 0;
550
551         /* here we handle context, getting data from precomputed path */
552         if(CTX_data_dir(member)) {
553                 static const char *dir[] = {
554                         "world", "object", "mesh", "armature", "lattice", "curve",
555                         "meta_ball", "lamp", "camera", "material", "material_slot",
556                         "texture", "texture_slot", "bone", "edit_bone", "particle_system",
557                         "cloth", "soft_body", "fluid", "smoke", "collision", "brush", NULL};
558
559                 CTX_data_dir_set(result, dir);
560                 return 1;
561         }
562         else if(CTX_data_equals(member, "world")) {
563                 set_pointer_type(path, result, &RNA_World);
564                 return 1;
565         }
566         else if(CTX_data_equals(member, "object")) {
567                 set_pointer_type(path, result, &RNA_Object);
568                 return 1;
569         }
570         else if(CTX_data_equals(member, "mesh")) {
571                 set_pointer_type(path, result, &RNA_Mesh);
572                 return 1;
573         }
574         else if(CTX_data_equals(member, "armature")) {
575                 set_pointer_type(path, result, &RNA_Armature);
576                 return 1;
577         }
578         else if(CTX_data_equals(member, "lattice")) {
579                 set_pointer_type(path, result, &RNA_Lattice);
580                 return 1;
581         }
582         else if(CTX_data_equals(member, "curve")) {
583                 set_pointer_type(path, result, &RNA_Curve);
584                 return 1;
585         }
586         else if(CTX_data_equals(member, "meta_ball")) {
587                 set_pointer_type(path, result, &RNA_MetaBall);
588                 return 1;
589         }
590         else if(CTX_data_equals(member, "lamp")) {
591                 set_pointer_type(path, result, &RNA_Lamp);
592                 return 1;
593         }
594         else if(CTX_data_equals(member, "camera")) {
595                 set_pointer_type(path, result, &RNA_Camera);
596                 return 1;
597         }
598         else if(CTX_data_equals(member, "material")) {
599                 set_pointer_type(path, result, &RNA_Material);
600                 return 1;
601         }
602         else if(CTX_data_equals(member, "texture")) {
603                 set_pointer_type(path, result, &RNA_Texture);
604                 return 1;
605         }
606         else if(CTX_data_equals(member, "material_slot")) {
607                 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
608
609                 if(ptr) {
610                         Object *ob= ptr->data;
611
612                         if(ob && ob->type && (ob->type<OB_LAMP) && ob->totcol)
613                                 CTX_data_pointer_set(result, &ob->id, &RNA_MaterialSlot, ob->mat+ob->actcol-1);
614                 }
615
616                 return 1;
617         }
618         else if(CTX_data_equals(member, "texture_slot")) {
619                 PointerRNA *ptr;
620
621                 if((ptr=get_pointer_type(path, &RNA_Material))) {
622                         Material *ma= ptr->data;
623
624                         if(ma)
625                                 CTX_data_pointer_set(result, &ma->id, &RNA_MaterialTextureSlot, ma->mtex[(int)ma->texact]);
626                 }
627                 else if((ptr=get_pointer_type(path, &RNA_Lamp))) {
628                         Lamp *la= ptr->data;
629
630                         if(la)
631                                 CTX_data_pointer_set(result, &la->id, &RNA_LampTextureSlot, la->mtex[(int)la->texact]);
632                 }
633                 else if((ptr=get_pointer_type(path, &RNA_World))) {
634                         World *wo= ptr->data;
635
636                         if(wo)
637                                 CTX_data_pointer_set(result, &wo->id, &RNA_WorldTextureSlot, wo->mtex[(int)wo->texact]);
638                 }
639                 else if((ptr=get_pointer_type(path, &RNA_Brush))) { /* how to get this into context? */
640                         Brush *br= ptr->data;
641
642                         if(br)
643                                 CTX_data_pointer_set(result, &br->id, &RNA_TextureSlot, br->mtex[(int)br->texact]);
644                 }
645
646                 return 1;
647         }
648         else if(CTX_data_equals(member, "bone")) {
649                 set_pointer_type(path, result, &RNA_Bone);
650                 return 1;
651         }
652         else if(CTX_data_equals(member, "edit_bone")) {
653                 set_pointer_type(path, result, &RNA_EditBone);
654                 return 1;
655         }
656         else if(CTX_data_equals(member, "particle_system")) {
657                 set_pointer_type(path, result, &RNA_ParticleSystem);
658                 return 1;
659         }
660         else if(CTX_data_equals(member, "cloth")) {
661                 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
662
663                 if(ptr && ptr->data) {
664                         Object *ob= ptr->data;
665                         ModifierData *md= modifiers_findByType(ob, eModifierType_Cloth);
666                         CTX_data_pointer_set(result, &ob->id, &RNA_ClothModifier, md);
667                         return 1;
668                 }
669         }
670         else if(CTX_data_equals(member, "soft_body")) {
671                 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
672
673                 if(ptr && ptr->data) {
674                         Object *ob= ptr->data;
675                         ModifierData *md= modifiers_findByType(ob, eModifierType_Softbody);
676                         CTX_data_pointer_set(result, &ob->id, &RNA_SoftBodyModifier, md);
677                         return 1;
678                 }
679         }
680         else if(CTX_data_equals(member, "fluid")) {
681                 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
682
683                 if(ptr && ptr->data) {
684                         Object *ob= ptr->data;
685                         ModifierData *md= modifiers_findByType(ob, eModifierType_Fluidsim);
686                         CTX_data_pointer_set(result, &ob->id, &RNA_FluidSimulationModifier, md);
687                         return 1;
688                 }
689         }
690         
691         else if(CTX_data_equals(member, "smoke")) {
692                 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
693
694                 if(ptr && ptr->data) {
695                         Object *ob= ptr->data;
696                         ModifierData *md= modifiers_findByType(ob, eModifierType_Smoke);
697                         CTX_data_pointer_set(result, &ob->id, &RNA_SmokeModifier, md);
698                         return 1;
699                 }
700         }
701         else if(CTX_data_equals(member, "collision")) {
702                 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
703
704                 if(ptr && ptr->data) {
705                         Object *ob= ptr->data;
706                         ModifierData *md= modifiers_findByType(ob, eModifierType_Collision);
707                         CTX_data_pointer_set(result, &ob->id, &RNA_CollisionModifier, md);
708                         return 1;
709                 }
710         }
711         else if(CTX_data_equals(member, "brush")) {
712                 set_pointer_type(path, result, &RNA_Brush);
713                 return 1;
714         }
715
716         return 0;
717 }
718
719 /************************* Drawing the Path ************************/
720
721 static void pin_cb(bContext *C, void *arg1, void *arg2)
722 {
723         SpaceButs *sbuts= CTX_wm_space_buts(C);
724         ButsContextPath *path= sbuts->path;
725         PointerRNA *ptr;
726         int a;
727
728         if(sbuts->flag & SB_PIN_CONTEXT) {
729                 if(path->len) {
730                         for(a=path->len-1; a>=0; a--) {
731                                 ptr= &path->ptr[a];
732
733                                 if(ptr->id.data) {
734                                         sbuts->pinid= ptr->id.data;
735                                         break;
736                                 }
737                         }
738                 }
739         }
740         else
741                 sbuts->pinid= NULL;
742         
743         ED_area_tag_redraw(CTX_wm_area(C));
744 }
745
746 void buttons_context_draw(const bContext *C, uiLayout *layout)
747 {
748         SpaceButs *sbuts= CTX_wm_space_buts(C);
749         ButsContextPath *path= sbuts->path;
750         uiLayout *row;
751         uiBlock *block;
752         uiBut *but;
753         PointerRNA *ptr;
754         char namebuf[128], *name;
755         int a, icon;
756
757         if(!path)
758                 return;
759
760         row= uiLayoutRow(layout, 1);
761         uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_LEFT);
762
763         block= uiLayoutGetBlock(row);
764         uiBlockSetEmboss(block, UI_EMBOSSN);
765         but= uiDefIconButBitC(block, ICONTOG, SB_PIN_CONTEXT, 0, ICON_UNPINNED, 0, 0, UI_UNIT_X, UI_UNIT_Y, &sbuts->flag, 0, 0, 0, 0, "Follow context or keep fixed datablock displayed.");
766         uiButSetFunc(but, pin_cb, NULL, NULL);
767
768         for(a=0; a<path->len; a++) {
769                 ptr= &path->ptr[a];
770
771                 if(a != 0)
772                         uiDefIconBut(block, LABEL, 0, VICON_SMALL_TRI_RIGHT, 0, 0, 10, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
773
774                 if(ptr->data) {
775                         icon= RNA_struct_ui_icon(ptr->type);
776                         name= RNA_struct_name_get_alloc(ptr, namebuf, sizeof(namebuf));
777
778                         if(name) {
779                                 if(sbuts->mainb != BCONTEXT_SCENE && ptr->type == &RNA_Scene)
780                                         uiItemL(row, "", icon); /* save some space */
781                                 else
782                                         uiItemL(row, name, icon);
783
784                                 if(name != namebuf)
785                                         MEM_freeN(name);
786                         }
787                         else
788                                 uiItemL(row, "", icon);
789                 }
790         }
791 }
792
793 static void buttons_panel_context(const bContext *C, Panel *pa)
794 {
795         buttons_context_draw(C, pa->layout);
796 }
797
798 void buttons_context_register(ARegionType *art)
799 {
800         PanelType *pt;
801
802         pt= MEM_callocN(sizeof(PanelType), "spacetype buttons panel context");
803         strcpy(pt->idname, "BUTTONS_PT_context");
804         strcpy(pt->label, "Context");
805         pt->draw= buttons_panel_context;
806         pt->flag= PNL_NO_HEADER;
807         BLI_addtail(&art->paneltypes, pt);
808 }