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 */
363 static int buttons_context_path_game(ButsContextPath *path)
365 /* XXX temporary context. Using material slot instead of ob->game_data */
367 PointerRNA *ptr= &path->ptr[path->len-1];
370 /* if we already have a (pinned) material, we're done */
371 if(RNA_struct_is_a(ptr->type, &RNA_Material)) {
374 /* if we have an object, use the object material slot */
375 else if(buttons_context_path_object(path)) {
376 ob= path->ptr[path->len-1].data;
378 if(ob && ob->type && (ob->type<OB_LAMP)) {
379 ma= give_current_material(ob, ob->actcol);
380 RNA_id_pointer_create(&ma->id, &path->ptr[path->len]);
386 /* no path to a material possible */
389 static int buttons_context_path(const bContext *C, ButsContextPath *path, int mainb, int worldtex)
391 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
395 memset(path, 0, sizeof(*path));
396 path->worldtex= worldtex;
398 /* if some ID datablock is pinned, set the root pointer */
402 RNA_id_pointer_create(id, &path->ptr[0]);
406 /* no pinned root, use scene as root */
408 id= (ID*)CTX_data_scene(C);
409 RNA_id_pointer_create(id, &path->ptr[0]);
413 /* now for each buttons context type, we try to construct a path,
414 * tracing back recursively */
417 found= buttons_context_path_scene(path);
420 found= buttons_context_path_world(path);
422 case BCONTEXT_OBJECT:
423 case BCONTEXT_PHYSICS:
424 case BCONTEXT_CONSTRAINT:
425 found= buttons_context_path_object(path);
427 case BCONTEXT_MODIFIER:
428 found= buttons_context_path_modifier(path);
431 found= buttons_context_path_data(path, -1);
434 found= buttons_context_path_game(path);
436 case BCONTEXT_PARTICLE:
437 found= buttons_context_path_particle(path);
439 case BCONTEXT_MATERIAL:
440 found= buttons_context_path_material(path);
442 case BCONTEXT_TEXTURE:
443 found= buttons_context_path_texture(path);
446 found= buttons_context_path_bone(path);
448 found= buttons_context_path_data(path, OB_ARMATURE);
458 void buttons_context_compute(const bContext *C, SpaceButs *sbuts)
460 ButsContextPath *path;
462 int a, worldtex, flag= 0;
465 sbuts->path= MEM_callocN(sizeof(ButsContextPath), "ButsContextPath");
468 worldtex= (sbuts->flag & SB_WORLD_TEX);
470 /* for each context, see if we can compute a valid path to it, if
471 * this is the case, we know we have to display the button */
472 for(a=0; a<BCONTEXT_TOT; a++) {
473 if(buttons_context_path(C, path, a, worldtex)) {
476 /* setting icon for data context */
477 if(a == BCONTEXT_DATA) {
478 ptr= &path->ptr[path->len-1];
481 sbuts->dataicon= RNA_struct_ui_icon(ptr->type);
483 sbuts->dataicon= ICON_EMPTY_DATA;
488 /* in case something becomes invalid, change */
489 if((flag & (1 << sbuts->mainb)) == 0) {
490 if(flag & BCONTEXT_OBJECT) {
491 sbuts->mainb= BCONTEXT_OBJECT;
494 for(a=0; a<BCONTEXT_TOT; a++) {
495 if(flag & (1 << a)) {
503 buttons_context_path(C, path, sbuts->mainb, worldtex);
505 if(!(flag & (1 << sbuts->mainb))) {
506 if(flag & (1 << BCONTEXT_OBJECT))
507 sbuts->mainb= BCONTEXT_OBJECT;
509 sbuts->mainb= BCONTEXT_SCENE;
512 sbuts->pathflag= flag;
515 /************************* Context Callback ************************/
517 int buttons_context(const bContext *C, const char *member, bContextDataResult *result)
519 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
520 ButsContextPath *path= sbuts?sbuts->path:NULL;
525 /* here we handle context, getting data from precomputed path */
526 if(CTX_data_dir(member)) {
527 static const char *dir[] = {
528 "world", "object", "mesh", "armature", "lattice", "curve",
529 "meta_ball", "lamp", "camera", "material", "material_slot", "game",
530 "texture", "texture_slot", "bone", "edit_bone", "particle_system",
531 "cloth", "soft_body", "fluid", "collision", NULL};
533 CTX_data_dir_set(result, dir);
536 else if(CTX_data_equals(member, "world")) {
537 set_pointer_type(path, result, &RNA_World);
540 else if(CTX_data_equals(member, "object")) {
541 set_pointer_type(path, result, &RNA_Object);
544 else if(CTX_data_equals(member, "mesh")) {
545 set_pointer_type(path, result, &RNA_Mesh);
548 else if(CTX_data_equals(member, "armature")) {
549 set_pointer_type(path, result, &RNA_Armature);
552 else if(CTX_data_equals(member, "lattice")) {
553 set_pointer_type(path, result, &RNA_Lattice);
556 else if(CTX_data_equals(member, "curve")) {
557 set_pointer_type(path, result, &RNA_Curve);
560 else if(CTX_data_equals(member, "meta_ball")) {
561 set_pointer_type(path, result, &RNA_MetaBall);
564 else if(CTX_data_equals(member, "lamp")) {
565 set_pointer_type(path, result, &RNA_Lamp);
568 else if(CTX_data_equals(member, "camera")) {
569 set_pointer_type(path, result, &RNA_Camera);
572 else if(CTX_data_equals(member, "material")) {
573 set_pointer_type(path, result, &RNA_Material);
576 else if(CTX_data_equals(member, "texture")) {
577 set_pointer_type(path, result, &RNA_Texture);
580 else if(CTX_data_equals(member, "material_slot")) {
581 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
584 Object *ob= ptr->data;
586 if(ob && ob->type && (ob->type<OB_LAMP) && ob->totcol)
587 CTX_data_pointer_set(result, &ob->id, &RNA_MaterialSlot, ob->mat+ob->actcol-1);
592 else if(CTX_data_equals(member, "texture_slot")) {
595 if((ptr=get_pointer_type(path, &RNA_Material))) {
596 Material *ma= ptr->data;
599 CTX_data_pointer_set(result, &ma->id, &RNA_MaterialTextureSlot, ma->mtex[(int)ma->texact]);
601 else if((ptr=get_pointer_type(path, &RNA_Lamp))) {
605 CTX_data_pointer_set(result, &la->id, &RNA_LampTextureSlot, la->mtex[(int)la->texact]);
607 else if((ptr=get_pointer_type(path, &RNA_World))) {
608 World *wo= ptr->data;
611 CTX_data_pointer_set(result, &wo->id, &RNA_WorldTextureSlot, wo->mtex[(int)wo->texact]);
613 else if((ptr=get_pointer_type(path, &RNA_Brush))) { /* how to get this into context? */
614 Brush *br= ptr->data;
617 CTX_data_pointer_set(result, &br->id, &RNA_TextureSlot, br->mtex[(int)br->texact]);
622 else if(CTX_data_equals(member, "bone")) {
623 set_pointer_type(path, result, &RNA_Bone);
626 else if(CTX_data_equals(member, "edit_bone")) {
627 set_pointer_type(path, result, &RNA_EditBone);
630 else if(CTX_data_equals(member, "particle_system")) {
631 set_pointer_type(path, result, &RNA_ParticleSystem);
634 else if(CTX_data_equals(member, "cloth")) {
635 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
637 if(ptr && ptr->data) {
638 Object *ob= ptr->data;
639 ModifierData *md= modifiers_findByType(ob, eModifierType_Cloth);
640 CTX_data_pointer_set(result, &ob->id, &RNA_ClothModifier, md);
644 else if(CTX_data_equals(member, "soft_body")) {
645 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
647 if(ptr && ptr->data) {
648 Object *ob= ptr->data;
649 ModifierData *md= modifiers_findByType(ob, eModifierType_Softbody);
650 CTX_data_pointer_set(result, &ob->id, &RNA_SoftBodyModifier, md);
654 else if(CTX_data_equals(member, "fluid")) {
655 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
657 if(ptr && ptr->data) {
658 Object *ob= ptr->data;
659 ModifierData *md= modifiers_findByType(ob, eModifierType_Fluidsim);
660 CTX_data_pointer_set(result, &ob->id, &RNA_FluidSimulationModifier, md);
664 else if(CTX_data_equals(member, "collision")) {
665 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
667 if(ptr && ptr->data) {
668 Object *ob= ptr->data;
669 ModifierData *md= modifiers_findByType(ob, eModifierType_Collision);
670 CTX_data_pointer_set(result, &ob->id, &RNA_CollisionModifier, md);
678 /************************* Drawing the Path ************************/
680 static void pin_cb(bContext *C, void *arg1, void *arg2)
682 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
683 ButsContextPath *path= sbuts->path;
687 if(sbuts->flag & SB_PIN_CONTEXT) {
689 for(a=path->len-1; a>=0; a--) {
693 sbuts->pinid= ptr->id.data;
702 ED_area_tag_redraw(CTX_wm_area(C));
705 void buttons_context_draw(const bContext *C, uiLayout *layout)
707 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
708 ButsContextPath *path= sbuts->path;
713 char namebuf[128], *name;
719 row= uiLayoutRow(layout, 1);
720 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_LEFT);
722 block= uiLayoutGetBlock(row);
723 uiBlockSetEmboss(block, UI_EMBOSSN);
724 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.");
725 uiButSetFunc(but, pin_cb, NULL, NULL);
727 for(a=0; a<path->len; a++) {
731 uiDefIconBut(block, LABEL, 0, VICON_SMALL_TRI_RIGHT, 0, 0, 10, UI_UNIT_Y, NULL, 0, 0, 0, 0, "");
734 icon= RNA_struct_ui_icon(ptr->type);
735 name= RNA_struct_name_get_alloc(ptr, namebuf, sizeof(namebuf));
738 if(sbuts->mainb != BCONTEXT_SCENE && ptr->type == &RNA_Scene)
739 uiItemL(row, "", icon); /* save some space */
741 uiItemL(row, name, icon);
747 uiItemL(row, "", icon);
752 static void buttons_panel_context(const bContext *C, Panel *pa)
754 buttons_context_draw(C, pa->layout);
757 void buttons_context_register(ARegionType *art)
761 pt= MEM_callocN(sizeof(PanelType), "spacetype buttons panel context");
762 strcpy(pt->idname, "BUTTONS_PT_context");
763 strcpy(pt->label, "Context");
764 pt->draw= buttons_panel_context;
765 pt->flag= PNL_NO_HEADER;
766 BLI_addtail(&art->paneltypes, pt);