gcsx_popup.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* GCSx
  2. ** POPUP.H
  3. **
  4. ** Popup and drop-down menus and menubar
  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_POPUP_H_
  24. #define __GCSx_POPUP_H_
  25. class PopupItem {
  26. private:
  27. // Ranges covered by the below arrays
  28. enum {
  29. ACCEL_LABEL_LOWER_FIRST = 8,
  30. ACCEL_LABEL_LOWER_LAST = 127,
  31. ACCEL_LABEL_UPPER_FIRST = 273,
  32. ACCEL_LABEL_UPPER_LAST = 296,
  33. // Max size of a popup item
  34. POPUP_MAX_WIDTH = 30,
  35. };
  36. // All possible accellerator keys in English
  37. static const char* accelLabelsLower[ACCEL_LABEL_LOWER_LAST - ACCEL_LABEL_LOWER_FIRST + 1];
  38. static const char* accelLabelsUpper[ACCEL_LABEL_UPPER_LAST - ACCEL_LABEL_UPPER_FIRST + 1];
  39. // Fills accelLabel and accel
  40. void prepAccellabel(Sint32 pKey);
  41. std::string label; // menu item text
  42. public:
  43. enum PopupState {
  44. // Popup states- only 'grayed' applies to menubars
  45. POPUP_NONE = 0,
  46. POPUP_DYNAMIC = 1,
  47. POPUP_CHECKBOX = 2, // Temporary- set each time via supportsCommand
  48. POPUP_RADIO = 4, // Temporary- set each time via supportsCommand
  49. POPUP_HIDDEN = 8, // Temporary- set each time via supportsCommand
  50. POPUP_SEPARATOR = 16,
  51. POPUP_CHECKED = 32, // Temporary- set each time via supportsCommand
  52. POPUP_GRAYED = 64, // Temporary- set each time via supportsCommand
  53. };
  54. const std::string& getLabel() const;
  55. void setLabel(const std::string& newLabel);
  56. int code; // positive value that represents this item uniquely
  57. std::string accelLabel; // accel reminder text
  58. class PopupMenu* submenu; // menu to expand to, NULL for a normal item
  59. Sint32 accel; // accellerator for item
  60. int underline; // underline for shortcut key
  61. char shortcut; // shortcut key
  62. PopupState state; // current state, from above #defines
  63. int pos; // X offset for menubar, Y offset for popups
  64. PopupItem();
  65. // (label of NULL for separator)
  66. // Dynamic popups call supportsCommand to determine grey/hide/check/radio status
  67. // Item with or without accellerator (no submenu allowed with accellerator)
  68. PopupItem(int pCode, const char* pLabel, Sint32 pKey, int dynamic, class PopupMenu* pSubmenu = NULL);
  69. };
  70. class PopupMenu : public Window {
  71. protected:
  72. int numItems;
  73. std::vector<PopupItem> items;
  74. int isMenubar;
  75. int fromMenubar;
  76. int isEmpty; // Temporary- recalculated each time it pops up
  77. int accelOffset; // Margin before accellerator notes- popups only
  78. int currSel;
  79. int submenuLevel;
  80. int checkboxX;
  81. int checkboxY;
  82. void predraw(int xPos, int yPos);
  83. enum {
  84. // height of entire separator and width of line itself
  85. GUI_POPUP_SEPHEIGHT = 7,
  86. GUI_POPUP_SEPLINE = 2,
  87. // padding below each text line
  88. GUI_POPUP_LINEPAD = 1,
  89. // padding on all four edges
  90. GUI_POPUP_EDGEPAD = 4,
  91. // margin on left and right for checkboxes and arrows
  92. GUI_POPUP_MARGIN = 20,
  93. // padding between item and it's accellerator note
  94. GUI_POPUP_GUTTER = 10,
  95. // menubar-
  96. // padding to each side of an item
  97. GUI_MENUBAR_TOPPAD = 2,
  98. GUI_MENUBAR_BOTTOMPAD = 6,
  99. GUI_MENUBAR_SIDEPAD = 6,
  100. // Padding to the left of entire bar
  101. GUI_MENUBAR_LEFTPAD = 2,
  102. };
  103. static const std::string wtSubmenu;
  104. static const std::string emptyItem;
  105. static int checkboxSize;
  106. public:
  107. // Constructor-
  108. //
  109. // 'menubar' determines if this is a menubar or a popup menu
  110. // (menubars do not properly support disabled, checkbox, radio button, separator)
  111. PopupMenu(int menubar);
  112. ~PopupMenu();
  113. // Adds an item; modifies original by adding shortcut key
  114. void add(PopupItem& item);
  115. #ifndef NDEBUG
  116. const char* debugDump() const;
  117. #endif
  118. void resolutionChange(int fromW, int fromH, int fromBpp, int toW, int toH, int toBpp);
  119. void display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset);
  120. // Pops up as close to the given location as possible and returns
  121. // Adds self to gui onscreen
  122. // Adds a SDL_COMMAND event to the queue when complete
  123. void popup(int xPos, int yPos, int newSubmenuLevel = 0, int isFromMenubar = 0);
  124. // Changes status of the menubar
  125. void menubar(int show);
  126. // (close all open submenus at a certain level or higher; return count)
  127. int closeAllSubmenus(int level);
  128. int wantsToBeDeleted() const;
  129. int event(int hasFocus, const SDL_Event* event);
  130. int isDocked() const;
  131. int tempFocus() const;
  132. WindowType windowType() const;
  133. WindowSort windowSort() const;
  134. // Quickly generates a popup menu from an array of structures
  135. // You are responsible for deleting it
  136. struct PopupGen {
  137. int code; // Command, or MENU_SUB / MENU_SEP / 0 (0 to end menu / submenu)
  138. const char* name; // NULL ok for SEP
  139. int dynamic; // 1 if dynamic; for MENU_SUB, a command code if dynamic
  140. };
  141. // (pos is used for recursion and should be left as NULL for initial call)
  142. static PopupMenu* compileMenu(const PopupGen* menuData, int isMenuBar = 0, int* pos = NULL);
  143. };
  144. #endif