CGUIEnumAttribute.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef __C_GUI_ENUM_ATTRIBUTE_H_INCLUDED__
  2. #define __C_GUI_ENUM_ATTRIBUTE_H_INCLUDED__
  3. #include "CGUIAttribute.h"
  4. #include "IGUIComboBox.h"
  5. #include "IGUIEditBox.h"
  6. #include "EGUIEditTypes.h"
  7. namespace irr
  8. {
  9. namespace gui
  10. {
  11. class CGUIEnumAttribute : public CGUIAttribute
  12. {
  13. public:
  14. //
  15. CGUIEnumAttribute(IGUIEnvironment* environment, IGUIElement *parent, s32 myParentID) :
  16. CGUIAttribute(environment, parent, myParentID),
  17. AttribComboBox(0), AttribEditBox(0)
  18. {
  19. }
  20. virtual ~CGUIEnumAttribute()
  21. {
  22. if (AttribComboBox)
  23. AttribComboBox->drop();
  24. if (AttribEditBox)
  25. AttribEditBox->drop();
  26. }
  27. // save the attribute and possibly post the event to its parent
  28. virtual void setAttrib(io::IAttributes *attribs, u32 attribIndex)
  29. {
  30. if (AttribComboBox)
  31. {
  32. AttribComboBox->remove();
  33. AttribComboBox->drop();
  34. AttribComboBox = 0;
  35. }
  36. if (AttribEditBox)
  37. {
  38. AttribEditBox->remove();
  39. AttribEditBox->drop();
  40. AttribEditBox = 0;
  41. }
  42. core::array<core::stringc> outLiterals;
  43. attribs->getAttributeEnumerationLiteralsOfEnumeration(attribIndex, outLiterals);
  44. core::rect<s32> r = getAbsolutePosition();
  45. core::rect<s32> r2(0, Environment->getSkin()->getFont()->getDimension(L"A").Height + 10,
  46. r.getWidth() - 5,
  47. Environment->getSkin()->getFont()->getDimension(L"A").Height*2 + 20 );
  48. if (outLiterals.size() > 0)
  49. {
  50. AttribComboBox = Environment->addComboBox(r2, this, -1);
  51. for (u32 i=0; i<outLiterals.size(); ++i)
  52. AttribComboBox->addItem( core::stringw(outLiterals[i].c_str()).c_str());
  53. AttribComboBox->setSelected( attribs->getAttributeAsInt(attribIndex) );
  54. AttribComboBox->grab();
  55. AttribComboBox->setSubElement(true);
  56. AttribComboBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  57. }
  58. else
  59. {
  60. AttribEditBox = Environment->addEditBox(
  61. attribs->getAttributeAsStringW(attribIndex).c_str(),
  62. r2, true, this, -1);
  63. AttribEditBox->grab();
  64. AttribEditBox->setSubElement(true);
  65. AttribEditBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  66. }
  67. CGUIAttribute::setAttrib(attribs, attribIndex);
  68. }
  69. //! save the attribute and possibly post the event to its parent
  70. virtual bool updateAttrib(bool sendEvent=true)
  71. {
  72. if (!Attribs)
  73. return true;
  74. if (AttribComboBox)
  75. Attribs->setAttribute(Index, AttribComboBox->getText());
  76. else if (AttribEditBox)
  77. Attribs->setAttribute(Index, AttribEditBox->getText());
  78. return CGUIAttribute::updateAttrib(sendEvent);
  79. }
  80. //! Returns the type name of the gui element.
  81. virtual const c8* getTypeName() const
  82. {
  83. return GUIEditElementTypeNames[EGUIEDIT_ENUMATTRIBUTE];
  84. }
  85. private:
  86. IGUIComboBox* AttribComboBox;
  87. IGUIEditBox* AttribEditBox;
  88. };
  89. } // namespace gui
  90. } // namespace irr
  91. #endif