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