4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2009 Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation
25 * ***** END GPL LICENSE BLOCK *****
31 #include "MEM_guardedalloc.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_lamp_types.h"
35 #include "DNA_material_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_particle_types.h"
42 #include "DNA_texture_types.h"
43 #include "DNA_world_types.h"
45 #include "BLI_listbase.h"
47 #include "BKE_context.h"
48 #include "BKE_material.h"
49 #include "BKE_modifier.h"
50 #include "BKE_particle.h"
51 #include "BKE_screen.h"
52 #include "BKE_utildefines.h"
53 #include "BKE_world.h"
55 #include "RNA_access.h"
57 #include "ED_armature.h"
58 #include "ED_screen.h"
60 #include "UI_interface.h"
61 #include "UI_resources.h"
63 #include "buttons_intern.h" // own include
65 typedef struct ButsContextPath {
71 static int set_pointer_type(ButsContextPath *path, bContextDataResult *result, StructRNA *type)
76 for(a=0; a<path->len; a++) {
79 if(RNA_struct_is_a(ptr->type, type)) {
80 CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data);
88 static PointerRNA *get_pointer_type(ButsContextPath *path, StructRNA *type)
93 for(a=0; a<path->len; a++) {
96 if(RNA_struct_is_a(ptr->type, type))
103 /************************* Creating the Path ************************/
105 static int buttons_context_path_scene(ButsContextPath *path)
107 PointerRNA *ptr= &path->ptr[path->len-1];
109 /* this one just verifies */
110 return RNA_struct_is_a(ptr->type, &RNA_Scene);
113 static int buttons_context_path_world(ButsContextPath *path)
116 PointerRNA *ptr= &path->ptr[path->len-1];
118 /* if we already have a (pinned) world, we're done */
119 if(RNA_struct_is_a(ptr->type, &RNA_World)) {
122 /* if we have a scene, use the scene's world */
123 else if(buttons_context_path_scene(path)) {
124 scene= path->ptr[path->len-1].data;
126 RNA_id_pointer_create(&scene->world->id, &path->ptr[path->len]);
132 /* no path to a world possible */
137 static int buttons_context_path_object(ButsContextPath *path)
141 PointerRNA *ptr= &path->ptr[path->len-1];
143 /* if we already have a (pinned) object, we're done */
144 if(RNA_struct_is_a(ptr->type, &RNA_Object)) {
147 /* if we have a scene, use the scene's active object */
148 else if(buttons_context_path_scene(path)) {
149 scene= path->ptr[path->len-1].data;
150 ob= (scene->basact)? scene->basact->object: NULL;
153 RNA_id_pointer_create(&ob->id, &path->ptr[path->len]);
160 /* no path to a object possible */
164 static int buttons_context_path_data(ButsContextPath *path, int type)
167 PointerRNA *ptr= &path->ptr[path->len-1];
169 /* if we already have a data, we're done */
170 if(RNA_struct_is_a(ptr->type, &RNA_Mesh) && (type == -1 || type == OB_MESH)) return 1;
171 else if(RNA_struct_is_a(ptr->type, &RNA_Curve) && (type == -1 || ELEM3(type, OB_CURVE, OB_SURF, OB_FONT))) return 1;
172 else if(RNA_struct_is_a(ptr->type, &RNA_Armature) && (type == -1 || type == OB_ARMATURE)) return 1;
173 else if(RNA_struct_is_a(ptr->type, &RNA_MetaBall) && (type == -1 || type == OB_MBALL)) return 1;
174 else if(RNA_struct_is_a(ptr->type, &RNA_Lattice) && (type == -1 || type == OB_LATTICE)) return 1;
175 else if(RNA_struct_is_a(ptr->type, &RNA_Camera) && (type == -1 || type == OB_CAMERA)) return 1;
176 else if(RNA_struct_is_a(ptr->type, &RNA_Lamp) && (type == -1 || type == OB_LAMP)) return 1;
177 /* try to get an object in the path, no pinning supported here */
178 else if(buttons_context_path_object(path)) {
179 ob= path->ptr[path->len-1].data;
181 if(ob && (type == -1 || type == ob->type)) {
182 RNA_id_pointer_create(ob->data, &path->ptr[path->len]);
189 /* no path to data possible */
193 static int buttons_context_path_modifier(ButsContextPath *path)
197 if(buttons_context_path_object(path)) {
198 ob= path->ptr[path->len-1].data;
200 if(ob && ELEM4(ob->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF))
207 static int buttons_context_path_material(ButsContextPath *path)
210 PointerRNA *ptr= &path->ptr[path->len-1];
213 /* if we already have a (pinned) material, we're done */
214 if(RNA_struct_is_a(ptr->type, &RNA_Material)) {
217 /* if we have an object, use the object material slot */
218 else if(buttons_context_path_object(path)) {
219 ob= path->ptr[path->len-1].data;
221 if(ob && ob->type && (ob->type<OB_LAMP)) {
222 ma= give_current_material(ob, ob->actcol);
223 RNA_id_pointer_create(&ma->id, &path->ptr[path->len]);
229 /* no path to a material possible */
233 static Bone *find_active_bone(Bone *bone)
237 for(; bone; bone=bone->next) {
238 if(bone->flag & BONE_ACTIVE)
241 active= find_active_bone(bone->childbase.first);
249 static int buttons_context_path_bone(ButsContextPath *path)
255 /* if we have an armature, get the active bone */
256 if(buttons_context_path_data(path, OB_ARMATURE)) {
257 arm= path->ptr[path->len-1].data;
260 for(edbo=arm->edbo->first; edbo; edbo=edbo->next) {
261 if(edbo->flag & BONE_ACTIVE) {
262 RNA_pointer_create(&arm->id, &RNA_EditBone, edbo, &path->ptr[path->len]);
269 bone= find_active_bone(arm->bonebase.first);
272 RNA_pointer_create(&arm->id, &RNA_Bone, bone, &path->ptr[path->len]);
279 /* no path to a bone possible */
283 static int buttons_context_path_particle(ButsContextPath *path)
286 ParticleSystem *psys;
288 /* if we have an object, get the active particle system */
289 if(buttons_context_path_object(path)) {
290 ob= path->ptr[path->len-1].data;
292 if(ob && ob->type == OB_MESH) {
293 psys= psys_get_current(ob);
295 RNA_pointer_create(&ob->id, &RNA_ParticleSystem, psys, &path->ptr[path->len]);
301 /* no path to a particle system possible */
305 static int buttons_context_path_texture(ButsContextPath *path)
312 PointerRNA *ptr= &path->ptr[path->len-1];
314 /* if we already have a (pinned) texture, we're done */
315 if(RNA_struct_is_a(ptr->type, &RNA_Texture)) {
319 else if(path->worldtex && buttons_context_path_world(path)) {
320 wo= path->ptr[path->len-1].data;
323 mtex= wo->mtex[(int)wo->texact];
324 tex= (mtex)? mtex->tex: NULL;
326 RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
332 else if(buttons_context_path_material(path)) {
333 ma= path->ptr[path->len-1].data;
336 mtex= ma->mtex[(int)ma->texact];
337 tex= (mtex)? mtex->tex: NULL;
339 RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
345 else if(buttons_context_path_data(path, OB_LAMP)) {
346 la= path->ptr[path->len-1].data;
349 mtex= la->mtex[(int)la->texact];
350 tex= (mtex)? mtex->tex: NULL;
352 RNA_id_pointer_create(&tex->id, &path->ptr[path->len]);
357 /* TODO: material nodes, brush */
359 /* no path to a particle system possible */
364 static int buttons_context_path(const bContext *C, ButsContextPath *path, int mainb, int worldtex)
366 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
370 memset(path, 0, sizeof(*path));
371 path->worldtex= worldtex;
373 /* if some ID datablock is pinned, set the root pointer */
377 RNA_id_pointer_create(id, &path->ptr[0]);
381 /* no pinned root, use scene as root */
383 id= (ID*)CTX_data_scene(C);
384 RNA_id_pointer_create(id, &path->ptr[0]);
388 /* now for each buttons context type, we try to construct a path,
389 * tracing back recursively */
392 found= buttons_context_path_scene(path);
395 found= buttons_context_path_world(path);
397 case BCONTEXT_OBJECT:
398 case BCONTEXT_PHYSICS:
399 case BCONTEXT_CONSTRAINT:
400 found= buttons_context_path_object(path);
402 case BCONTEXT_MODIFIER:
403 found= buttons_context_path_modifier(path);
406 found= buttons_context_path_data(path, -1);
408 case BCONTEXT_PARTICLE:
409 found= buttons_context_path_particle(path);
411 case BCONTEXT_MATERIAL:
412 found= buttons_context_path_material(path);
414 case BCONTEXT_TEXTURE:
415 found= buttons_context_path_texture(path);
418 found= buttons_context_path_bone(path);
420 found= buttons_context_path_data(path, OB_ARMATURE);
430 void buttons_context_compute(const bContext *C, SpaceButs *sbuts)
432 ButsContextPath *path;
434 int a, worldtex, flag= 0;
437 sbuts->path= MEM_callocN(sizeof(ButsContextPath), "ButsContextPath");
440 worldtex= (sbuts->flag & SB_WORLD_TEX);
442 /* for each context, see if we can compute a valid path to it, if
443 * this is the case, we know we have to display the button */
444 for(a=0; a<BCONTEXT_TOT; a++) {
445 if(buttons_context_path(C, path, a, worldtex)) {
448 /* setting icon for data context */
449 if(a == BCONTEXT_DATA) {
450 ptr= &path->ptr[path->len-1];
453 sbuts->dataicon= RNA_struct_ui_icon(ptr->type);
455 sbuts->dataicon= ICON_EMPTY_DATA;
460 /* always try to use the tab that was explicitly
461 * set to the user, so that once that context comes
462 * back, the tab is activated again */
463 sbuts->mainb= sbuts->mainbuser;
465 /* in case something becomes invalid, change */
466 if((flag & (1 << sbuts->mainb)) == 0) {
467 if(flag & BCONTEXT_OBJECT) {
468 sbuts->mainb= BCONTEXT_OBJECT;
471 for(a=0; a<BCONTEXT_TOT; a++) {
472 if(flag & (1 << a)) {
480 buttons_context_path(C, path, sbuts->mainb, worldtex);
482 if(!(flag & (1 << sbuts->mainb))) {
483 if(flag & (1 << BCONTEXT_OBJECT))
484 sbuts->mainb= BCONTEXT_OBJECT;
486 sbuts->mainb= BCONTEXT_SCENE;
489 sbuts->pathflag= flag;
492 /************************* Context Callback ************************/
494 int buttons_context(const bContext *C, const char *member, bContextDataResult *result)
496 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
497 ButsContextPath *path= sbuts?sbuts->path:NULL;
502 /* here we handle context, getting data from precomputed path */
503 if(CTX_data_dir(member)) {
504 static const char *dir[] = {
505 "world", "object", "mesh", "armature", "lattice", "curve",
506 "meta_ball", "lamp", "camera", "material", "material_slot",
507 "texture", "texture_slot", "bone", "edit_bone", "particle_system",
508 "cloth", "soft_body", "fluid", "collision", NULL};
510 CTX_data_dir_set(result, dir);
513 else if(CTX_data_equals(member, "world")) {
514 set_pointer_type(path, result, &RNA_World);
517 else if(CTX_data_equals(member, "object")) {
518 set_pointer_type(path, result, &RNA_Object);
521 else if(CTX_data_equals(member, "mesh")) {
522 set_pointer_type(path, result, &RNA_Mesh);
525 else if(CTX_data_equals(member, "armature")) {
526 set_pointer_type(path, result, &RNA_Armature);
529 else if(CTX_data_equals(member, "lattice")) {
530 set_pointer_type(path, result, &RNA_Lattice);
533 else if(CTX_data_equals(member, "curve")) {
534 set_pointer_type(path, result, &RNA_Curve);
537 else if(CTX_data_equals(member, "meta_ball")) {
538 set_pointer_type(path, result, &RNA_MetaBall);
541 else if(CTX_data_equals(member, "lamp")) {
542 set_pointer_type(path, result, &RNA_Lamp);
545 else if(CTX_data_equals(member, "camera")) {
546 set_pointer_type(path, result, &RNA_Camera);
549 else if(CTX_data_equals(member, "material")) {
550 set_pointer_type(path, result, &RNA_Material);
553 else if(CTX_data_equals(member, "texture")) {
554 set_pointer_type(path, result, &RNA_Texture);
557 else if(CTX_data_equals(member, "material_slot")) {
558 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
561 Object *ob= ptr->data;
563 if(ob && ob->type && (ob->type<OB_LAMP) && ob->totcol)
564 CTX_data_pointer_set(result, &ob->id, &RNA_MaterialSlot, ob->mat+ob->actcol-1);
569 else if(CTX_data_equals(member, "texture_slot")) {
572 if((ptr=get_pointer_type(path, &RNA_Material))) {
573 Material *ma= ptr->data;
576 CTX_data_pointer_set(result, &ma->id, &RNA_MaterialTextureSlot, ma->mtex[(int)ma->texact]);
578 else if((ptr=get_pointer_type(path, &RNA_Lamp))) {
582 CTX_data_pointer_set(result, &la->id, &RNA_LampTextureSlot, la->mtex[(int)la->texact]);
584 else if((ptr=get_pointer_type(path, &RNA_World))) {
585 World *wo= ptr->data;
588 CTX_data_pointer_set(result, &wo->id, &RNA_WorldTextureSlot, wo->mtex[(int)wo->texact]);
590 else if((ptr=get_pointer_type(path, &RNA_Brush))) { /* how to get this into context? */
591 Brush *br= ptr->data;
594 CTX_data_pointer_set(result, &br->id, &RNA_TextureSlot, br->mtex[(int)br->texact]);
599 else if(CTX_data_equals(member, "bone")) {
600 set_pointer_type(path, result, &RNA_Bone);
603 else if(CTX_data_equals(member, "edit_bone")) {
604 set_pointer_type(path, result, &RNA_EditBone);
607 else if(CTX_data_equals(member, "particle_system")) {
608 set_pointer_type(path, result, &RNA_ParticleSystem);
611 else if(CTX_data_equals(member, "cloth")) {
612 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
614 if(ptr && ptr->data) {
615 Object *ob= ptr->data;
616 ModifierData *md= modifiers_findByType(ob, eModifierType_Cloth);
617 CTX_data_pointer_set(result, &ob->id, &RNA_ClothModifier, md);
621 else if(CTX_data_equals(member, "soft_body")) {
622 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
624 if(ptr && ptr->data) {
625 Object *ob= ptr->data;
626 ModifierData *md= modifiers_findByType(ob, eModifierType_Softbody);
627 CTX_data_pointer_set(result, &ob->id, &RNA_SoftBodyModifier, md);
631 else if(CTX_data_equals(member, "fluid")) {
632 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
634 if(ptr && ptr->data) {
635 Object *ob= ptr->data;
636 ModifierData *md= modifiers_findByType(ob, eModifierType_Fluidsim);
637 CTX_data_pointer_set(result, &ob->id, &RNA_FluidSimulationModifier, md);
641 else if(CTX_data_equals(member, "collision")) {
642 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
644 if(ptr && ptr->data) {
645 Object *ob= ptr->data;
646 ModifierData *md= modifiers_findByType(ob, eModifierType_Collision);
647 CTX_data_pointer_set(result, &ob->id, &RNA_CollisionModifier, md);
655 /************************* Drawing the Path ************************/
657 static void pin_cb(bContext *C, void *arg1, void *arg2)
659 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
660 ButsContextPath *path= sbuts->path;
664 if(sbuts->flag & SB_PIN_CONTEXT) {
666 for(a=path->len-1; a>=0; a--) {
670 sbuts->pinid= ptr->id.data;
679 ED_area_tag_redraw(CTX_wm_area(C));
682 void buttons_context_draw(const bContext *C, uiLayout *layout)
684 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
685 ButsContextPath *path= sbuts->path;
690 char namebuf[128], *name;
696 row= uiLayoutRow(layout, 1);
697 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_LEFT);
699 block= uiLayoutGetBlock(row);
700 uiBlockSetEmboss(block, UI_EMBOSSN);
701 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.");
702 uiButSetFunc(but, pin_cb, NULL, NULL);
704 for(a=0; a<path->len; a++) {
708 uiDefIconBut(block, LABEL, 0, VICON_SMALL_TRI_RIGHT, 0, 0, 10, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
711 icon= RNA_struct_ui_icon(ptr->type);
712 name= RNA_struct_name_get_alloc(ptr, namebuf, sizeof(namebuf));
715 if(sbuts->mainb != BCONTEXT_SCENE && ptr->type == &RNA_Scene)
716 uiItemL(row, "", icon); /* save some space */
718 uiItemL(row, name, icon);
724 uiItemL(row, "", icon);
729 static void buttons_panel_context(const bContext *C, Panel *pa)
731 buttons_context_draw(C, pa->layout);
734 void buttons_context_register(ARegionType *art)
738 pt= MEM_callocN(sizeof(PanelType), "spacetype buttons panel context");
739 strcpy(pt->idname, "BUTTONS_PT_context");
740 strcpy(pt->label, "Context");
741 pt->draw= buttons_panel_context;
742 pt->flag= PNL_NO_HEADER;
743 BLI_addtail(&art->paneltypes, pt);