CGUIListBox.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __C_GUI_LIST_BOX_H_INCLUDED__
  5. #define __C_GUI_LIST_BOX_H_INCLUDED__
  6. #include "IrrCompileConfig.h"
  7. #ifdef _IRR_COMPILE_WITH_GUI_
  8. #include "IGUIListBox.h"
  9. #include "irrArray.h"
  10. namespace irr
  11. {
  12. namespace gui
  13. {
  14. class IGUIFont;
  15. class IGUIScrollBar;
  16. class CGUIListBox : public IGUIListBox
  17. {
  18. public:
  19. //! constructor
  20. CGUIListBox(IGUIEnvironment* environment, IGUIElement* parent,
  21. s32 id, core::rect<s32> rectangle, bool clip=true,
  22. bool drawBack=false, bool moveOverSelect=false);
  23. //! destructor
  24. virtual ~CGUIListBox();
  25. //! returns amount of list items
  26. virtual u32 getItemCount() const;
  27. //! returns string of a list item. the id may be a value from 0 to itemCount-1
  28. virtual const wchar_t* getListItem(u32 id) const;
  29. //! adds an list item, returns id of item
  30. virtual u32 addItem(const wchar_t* text);
  31. //! clears the list
  32. virtual void clear();
  33. //! returns id of selected item. returns -1 if no item is selected.
  34. virtual s32 getSelected() const;
  35. //! sets the selected item. Set this to -1 if no item should be selected
  36. virtual void setSelected(s32 id);
  37. //! sets the selected item. Set this to -1 if no item should be selected
  38. virtual void setSelected(const wchar_t *item);
  39. //! called if an event happened.
  40. virtual bool OnEvent(const SEvent& event);
  41. //! draws the element and its children
  42. virtual void draw();
  43. //! adds an list item with an icon
  44. //! \param text Text of list entry
  45. //! \param icon Sprite index of the Icon within the current sprite bank. Set it to -1 if you want no icon
  46. //! \return
  47. //! returns the id of the new created item
  48. virtual u32 addItem(const wchar_t* text, s32 icon);
  49. //! Returns the icon of an item
  50. virtual s32 getIcon(u32 id) const;
  51. //! removes an item from the list
  52. virtual void removeItem(u32 id);
  53. //! get the the id of the item at the given absolute coordinates
  54. virtual s32 getItemAt(s32 xpos, s32 ypos) const;
  55. //! Sets the sprite bank which should be used to draw list icons. This font is set to the sprite bank of
  56. //! the built-in-font by default. A sprite can be displayed in front of every list item.
  57. //! An icon is an index within the icon sprite bank. Several default icons are available in the
  58. //! skin through getIcon
  59. virtual void setSpriteBank(IGUISpriteBank* bank);
  60. //! set whether the listbox should scroll to newly selected items
  61. virtual void setAutoScrollEnabled(bool scroll);
  62. //! returns true if automatic scrolling is enabled, false if not.
  63. virtual bool isAutoScrollEnabled() const;
  64. //! Update the position and size of the listbox, and update the scrollbar
  65. virtual void updateAbsolutePosition();
  66. //! Writes attributes of the element.
  67. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
  68. //! Reads attributes of the element
  69. virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
  70. //! set all item colors at given index to color
  71. virtual void setItemOverrideColor(u32 index, video::SColor color);
  72. //! set all item colors of specified type at given index to color
  73. virtual void setItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType, video::SColor color);
  74. //! clear all item colors at index
  75. virtual void clearItemOverrideColor(u32 index);
  76. //! clear item color at index for given colortype
  77. virtual void clearItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType);
  78. //! has the item at index its color overwritten?
  79. virtual bool hasItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) const;
  80. //! return the overwrite color at given item index.
  81. virtual video::SColor getItemOverrideColor(u32 index, EGUI_LISTBOX_COLOR colorType) const;
  82. //! return the default color which is used for the given colorType
  83. virtual video::SColor getItemDefaultColor(EGUI_LISTBOX_COLOR colorType) const;
  84. //! set the item at the given index
  85. virtual void setItem(u32 index, const wchar_t* text, s32 icon);
  86. //! Insert the item at the given index
  87. //! Return the index on success or -1 on failure.
  88. virtual s32 insertItem(u32 index, const wchar_t* text, s32 icon);
  89. //! Swap the items at the given indices
  90. virtual void swapItems(u32 index1, u32 index2);
  91. //! set global itemHeight
  92. virtual void setItemHeight( s32 height );
  93. //! Sets whether to draw the background
  94. virtual void setDrawBackground(bool draw);
  95. private:
  96. struct ListItem
  97. {
  98. ListItem() : icon(-1)
  99. {}
  100. core::stringw text;
  101. s32 icon;
  102. // A multicolor extension
  103. struct ListItemOverrideColor
  104. {
  105. ListItemOverrideColor() : Use(false) {}
  106. bool Use;
  107. video::SColor Color;
  108. };
  109. ListItemOverrideColor OverrideColors[EGUI_LBC_COUNT];
  110. };
  111. void recalculateItemHeight();
  112. void selectNew(s32 ypos, bool onlyHover=false);
  113. void recalculateScrollPos();
  114. // extracted that function to avoid copy&paste code
  115. void recalculateItemWidth(s32 icon);
  116. // get labels used for serialization
  117. bool getSerializationLabels(EGUI_LISTBOX_COLOR colorType, core::stringc & useColorLabel, core::stringc & colorLabel) const;
  118. core::array< ListItem > Items;
  119. s32 Selected;
  120. s32 ItemHeight;
  121. s32 ItemHeightOverride;
  122. s32 TotalItemHeight;
  123. s32 ItemsIconWidth;
  124. gui::IGUIFont* Font;
  125. gui::IGUISpriteBank* IconBank;
  126. gui::IGUIScrollBar* ScrollBar;
  127. u32 selectTime;
  128. u32 LastKeyTime;
  129. core::stringw KeyBuffer;
  130. bool Selecting;
  131. bool DrawBack;
  132. bool MoveOverSelect;
  133. bool AutoScroll;
  134. bool HighlightWhenNotFocused;
  135. };
  136. } // end namespace gui
  137. } // end namespace irr
  138. #endif // _IRR_COMPILE_WITH_GUI_
  139. #endif