gcsx_list.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* GCSx
  2. ** LIST.H
  3. **
  4. ** Listbox, drop-down, etc. dialog widgets
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_LIST_H_
  24. #define __GCSx_LIST_H_
  25. // A single listbox entry
  26. class ListEntry {
  27. public:
  28. std::string label; // Used to determine equality/collation
  29. int selected; // Not part of equality
  30. int disabled; // Not part of equality
  31. int id; // User-defined- used to determine equality/collation
  32. int code1; // User-defined- not part of equality
  33. int code2; // User-defined- not part of equality
  34. int code3; // User-defined- not part of equality
  35. ListEntry();
  36. ListEntry(const std::string& eLabel, int eId, int eCode1 = 0, int eCode2 = 0, int eCode3 = 0, int eDisabled = 0);
  37. ListEntry(const ListEntry& from);
  38. int operator==(const ListEntry& right) const { return (!myStricmp(label.c_str(), right.label.c_str())) && (id == right.id); }
  39. int operator!=(const ListEntry& right) const { return (myStricmp(label.c_str(), right.label.c_str())) || (id != right.id); }
  40. int operator<(const ListEntry& right) const { return (myStricmp(label.c_str(), right.label.c_str()) < 0) || ((!myStricmp(label.c_str(), right.label.c_str())) && (id < right.id)); }
  41. int operator>(const ListEntry& right) const { return (myStricmp(label.c_str(), right.label.c_str()) > 0) || ((!myStricmp(label.c_str(), right.label.c_str())) && (id > right.id)); }
  42. int operator<=(const ListEntry& right) const { return myStricmp(label.c_str(), right.label.c_str()) <= 0; }
  43. int operator>=(const ListEntry& right) const { return myStricmp(label.c_str(), right.label.c_str()) >= 0; }
  44. ListEntry& operator=(const ListEntry& right);
  45. };
  46. class WListBox : public Widget {
  47. protected:
  48. class WidgetScroll* wparent;
  49. // Height of each value; Width of largest value
  50. int valueHeight;
  51. int valueWidth;
  52. // Values in listbox
  53. std::vector<ListEntry> contents;
  54. // Number of values
  55. int numValues;
  56. // Width in pixels to show
  57. int showWidth;
  58. // Number of lines to show
  59. int showLines;
  60. // Cursor position (focus)
  61. int cursorPos;
  62. // Currently selected item- -1 for none/-2 for multiple items
  63. // (used for speed/simplicity)
  64. int selectedItem;
  65. enum { SELECTED_NONE = -1, SELECTED_MANY = -2 };
  66. // Select/deselect an item; 'alone' determines if we should attempt to 'add' to selection
  67. virtual void selectItem(int id, int select = 1, int alone = 1);
  68. // (can move to outside bounds- position is clipped)
  69. void moveCursor(int id);
  70. // Type of selection allowed
  71. int allowZero;
  72. int allowMany;
  73. // Current drag action- set or unset?
  74. int dragSet;
  75. enum {
  76. // This is the number of X characters to fit in a standard width listbox
  77. GUI_LISTBOX_DEFAULTWIDTH = 20,
  78. // Default number of lines to show
  79. GUI_LISTBOX_DEFAULTLINES = 5,
  80. // Padding on left/right of text
  81. GUI_LISTBOX_HORIZPAD = 3,
  82. };
  83. public:
  84. // Width in pixels, lines to show vertically
  85. // Version that allows exactly one selection, stores id
  86. // Allows zero selection if lAllowZero true, stores 0
  87. WListBox(int lId, int* lSelection, int lAllowZero = 0, int lWidth = -1, int lLines = -1);
  88. ~WListBox();
  89. // Selecting an item selects it solely
  90. // (doesn't call siblingModified unless selectIt true)
  91. virtual void addItem(const ListEntry& item, int selectIt = 0);
  92. // NULL items will not be updated
  93. void modifyItem(int position, const std::string* newLabel = NULL, const int* newDisabled = NULL, const int* newCode1 = NULL, const int* newCode2 = NULL, const int* newCode3 = NULL);
  94. void clear();
  95. void sortItems(); // Case insensitive
  96. void addTo(Dialog* dialog);
  97. // Refuse all if empty
  98. int refuseAll() const;
  99. virtual int event(int hasFocus, const SDL_Event* event);
  100. void load();
  101. void apply();
  102. virtual void display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset);
  103. // Given an id, returns an item in listbox, NULL if none found
  104. const ListEntry* findEntry(int id) const;
  105. };
  106. #endif