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(const bContext *C, ButsContextPath *path, int mainb, int worldtex)
365 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
369 memset(path, 0, sizeof(*path));
370 path->worldtex= worldtex;
372 /* if some ID datablock is pinned, set the root pointer */
376 RNA_id_pointer_create(id, &path->ptr[0]);
380 /* no pinned root, use scene as root */
382 id= (ID*)CTX_data_scene(C);
383 RNA_id_pointer_create(id, &path->ptr[0]);
387 /* now for each buttons context type, we try to construct a path,
388 * tracing back recursively */
391 found= buttons_context_path_scene(path);
394 found= buttons_context_path_world(path);
396 case BCONTEXT_OBJECT:
397 case BCONTEXT_PHYSICS:
398 case BCONTEXT_CONSTRAINT:
399 found= buttons_context_path_object(path);
401 case BCONTEXT_MODIFIER:
402 found= buttons_context_path_modifier(path);
405 found= buttons_context_path_data(path, -1);
407 case BCONTEXT_PARTICLE:
408 found= buttons_context_path_particle(path);
410 case BCONTEXT_MATERIAL:
411 found= buttons_context_path_material(path);
413 case BCONTEXT_TEXTURE:
414 found= buttons_context_path_texture(path);
417 found= buttons_context_path_bone(path);
427 void buttons_context_compute(const bContext *C, SpaceButs *sbuts)
429 ButsContextPath *path;
431 int a, worldtex, flag= 0;
434 sbuts->path= MEM_callocN(sizeof(ButsContextPath), "ButsContextPath");
437 worldtex= (sbuts->flag & SB_WORLD_TEX);
439 /* for each context, see if we can compute a valid path to it, if
440 * this is the case, we know we have to display the button */
441 for(a=0; a<BCONTEXT_TOT; a++) {
442 if(buttons_context_path(C, path, a, worldtex)) {
445 /* setting icon for data context */
446 if(a == BCONTEXT_DATA) {
447 ptr= &path->ptr[path->len-1];
450 sbuts->dataicon= RNA_struct_ui_icon(ptr->type);
452 sbuts->dataicon= ICON_EMPTY_DATA;
457 /* in case something becomes invalid, change */
458 if((flag & (1 << sbuts->mainb)) == 0) {
459 if(flag & BCONTEXT_OBJECT) {
460 sbuts->mainb= BCONTEXT_OBJECT;
463 for(a=0; a<BCONTEXT_TOT; a++) {
464 if(flag & (1 << a)) {
472 buttons_context_path(C, path, sbuts->mainb, worldtex);
474 if(!(flag & (1 << sbuts->mainb))) {
475 if(flag & (1 << BCONTEXT_OBJECT))
476 sbuts->mainb= BCONTEXT_OBJECT;
478 sbuts->mainb= BCONTEXT_SCENE;
481 sbuts->pathflag= flag;
484 /************************* Context Callback ************************/
486 int buttons_context(const bContext *C, const char *member, bContextDataResult *result)
488 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
489 ButsContextPath *path= sbuts?sbuts->path:NULL;
494 /* here we handle context, getting data from precomputed path */
495 if(CTX_data_dir(member)) {
496 static const char *dir[] = {
497 "world", "object", "meshe", "armature", "lattice", "curve",
498 "meta_ball", "lamp", "camera", "material", "material_slot",
499 "texture", "texture_slot", "bone", "edit_bone", "particle_system",
500 "cloth", "soft_body", "fluid", NULL};
502 CTX_data_dir_set(result, dir);
505 else if(CTX_data_equals(member, "world")) {
506 set_pointer_type(path, result, &RNA_World);
509 else if(CTX_data_equals(member, "object")) {
510 set_pointer_type(path, result, &RNA_Object);
513 else if(CTX_data_equals(member, "mesh")) {
514 set_pointer_type(path, result, &RNA_Mesh);
517 else if(CTX_data_equals(member, "armature")) {
518 set_pointer_type(path, result, &RNA_Armature);
521 else if(CTX_data_equals(member, "lattice")) {
522 set_pointer_type(path, result, &RNA_Lattice);
525 else if(CTX_data_equals(member, "curve")) {
526 set_pointer_type(path, result, &RNA_Curve);
529 else if(CTX_data_equals(member, "meta_ball")) {
530 set_pointer_type(path, result, &RNA_MetaBall);
533 else if(CTX_data_equals(member, "lamp")) {
534 set_pointer_type(path, result, &RNA_Lamp);
537 else if(CTX_data_equals(member, "camera")) {
538 set_pointer_type(path, result, &RNA_Camera);
541 else if(CTX_data_equals(member, "material")) {
542 set_pointer_type(path, result, &RNA_Material);
545 else if(CTX_data_equals(member, "texture")) {
546 set_pointer_type(path, result, &RNA_Texture);
549 else if(CTX_data_equals(member, "material_slot")) {
550 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
553 Object *ob= ptr->data;
555 if(ob && ob->type && (ob->type<OB_LAMP))
556 CTX_data_pointer_set(result, &ob->id, &RNA_MaterialSlot, ob->mat+ob->actcol-1);
561 else if(CTX_data_equals(member, "texture_slot")) {
564 if((ptr=get_pointer_type(path, &RNA_Material))) {
565 Material *ma= ptr->data;
568 CTX_data_pointer_set(result, &ma->id, &RNA_MaterialTextureSlot, ma->mtex[(int)ma->texact]);
570 else if((ptr=get_pointer_type(path, &RNA_Lamp))) {
574 CTX_data_pointer_set(result, &la->id, &RNA_LampTextureSlot, la->mtex[(int)la->texact]);
576 else if((ptr=get_pointer_type(path, &RNA_World))) {
577 World *wo= ptr->data;
580 CTX_data_pointer_set(result, &wo->id, &RNA_WorldTextureSlot, wo->mtex[(int)wo->texact]);
582 else if((ptr=get_pointer_type(path, &RNA_Brush))) { /* how to get this into context? */
583 Brush *br= ptr->data;
586 CTX_data_pointer_set(result, &br->id, &RNA_TextureSlot, br->mtex[(int)br->texact]);
591 else if(CTX_data_equals(member, "bone")) {
592 set_pointer_type(path, result, &RNA_Bone);
595 else if(CTX_data_equals(member, "edit_bone")) {
596 set_pointer_type(path, result, &RNA_EditBone);
599 else if(CTX_data_equals(member, "particle_system")) {
600 set_pointer_type(path, result, &RNA_ParticleSystem);
603 else if(CTX_data_equals(member, "cloth")) {
604 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
606 if(ptr && ptr->data) {
607 Object *ob= ptr->data;
608 ModifierData *md= modifiers_findByType(ob, eModifierType_Cloth);
609 CTX_data_pointer_set(result, &ob->id, &RNA_ClothModifier, md);
613 else if(CTX_data_equals(member, "soft_body")) {
614 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
616 if(ptr && ptr->data) {
617 Object *ob= ptr->data;
618 CTX_data_pointer_set(result, &ob->id, &RNA_SoftBodySettings, ob->soft);
622 else if(CTX_data_equals(member, "fluid")) {
623 PointerRNA *ptr= get_pointer_type(path, &RNA_Object);
625 if(ptr && ptr->data) {
626 Object *ob= ptr->data;
627 ModifierData *md= modifiers_findByType(ob, eModifierType_Fluidsim);
628 CTX_data_pointer_set(result, &ob->id, &RNA_FluidSimulationModifier, md);
636 /************************* Drawing the Path ************************/
638 static void pin_cb(bContext *C, void *arg1, void *arg2)
640 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
641 ButsContextPath *path= sbuts->path;
645 if(sbuts->flag & SB_PIN_CONTEXT) {
647 for(a=path->len-1; a>=0; a--) {
651 sbuts->pinid= ptr->id.data;
660 ED_area_tag_redraw(CTX_wm_area(C));
663 void buttons_context_draw(const bContext *C, uiLayout *layout)
665 SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
666 ButsContextPath *path= sbuts->path;
671 PropertyRNA *nameprop;
672 char namebuf[128], *name;
678 row= uiLayoutRow(layout, 0);
679 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_LEFT);
681 block= uiLayoutGetBlock(row);
682 uiBlockSetEmboss(block, UI_EMBOSSN);
683 but= uiDefIconButBitC(block, ICONTOG, SB_PIN_CONTEXT, 0, (sbuts->flag & SB_PIN_CONTEXT)? ICON_PINNED: ICON_UNPINNED, 0, 0, UI_UNIT_X, UI_UNIT_Y, &sbuts->flag, 0, 0, 0, 0, "Follow context or keep fixed datablock displayed.");
684 uiButSetFunc(but, pin_cb, NULL, NULL);
686 for(a=0; a<path->len; a++) {
690 icon= RNA_struct_ui_icon(ptr->type);
691 nameprop= RNA_struct_name_property(ptr->type);
694 if(sbuts->mainb != BCONTEXT_SCENE && ptr->type == &RNA_Scene) {
695 uiItemL(row, "", icon); /* save some space */
700 name= RNA_property_string_get_alloc(ptr, nameprop, namebuf, sizeof(namebuf));
702 uiItemL(row, name, icon);
708 uiItemL(row, "", icon);
713 static void buttons_panel_context(const bContext *C, Panel *pa)
715 buttons_context_draw(C, pa->layout);
718 void buttons_context_register(ARegionType *art)
722 pt= MEM_callocN(sizeof(PanelType), "spacetype buttons panel context");
723 strcpy(pt->idname, "BUTTONS_PT_context");
724 strcpy(pt->label, "Context");
725 pt->draw= buttons_panel_context;
726 BLI_addtail(&art->paneltypes, pt);