PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/worldforge-sear-0b70ddb/guichan/adjustingcontainer.hpp

#
C++ Header | 236 lines | 53 code | 22 blank | 161 comment | 0 complexity | 2fc4864875938eb9f8c8df920c22a8ac MD5 | raw file
Possible License(s): GPL-2.0
  1. /* _______ __ __ __ ______ __ __ _______ __ __
  2. * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
  3. * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
  4. * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
  5. * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
  6. * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
  7. * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
  8. *
  9. * Copyright (c) 2007 - 2008 Josh Matthews and Olof Naessén
  10. *
  11. *
  12. * Per Larsson a.k.a finalman
  13. * Olof Naessén a.k.a jansem/yakslem
  14. *
  15. * Visit: http://guichan.sourceforge.net
  16. *
  17. * License: (BSD)
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * 3. Neither the name of Guichan nor the names of its contributors may
  28. * be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  37. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  38. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  39. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. #ifndef GCN_CONTRIB_ADJUSTINGCONTAINER_HPP
  44. #define GCN_CONTRIB_ADJUSTINGCONTAINER_HPP
  45. #include <vector>
  46. #include <guichan.hpp>
  47. namespace gcn
  48. {
  49. namespace contrib
  50. {
  51. /**
  52. * Self-adjusting Container class. AdjustingContainers are an easy way to
  53. * have Guichan position a group of widgets for you. It organizes elements
  54. * in a table layout, with fixed columns and variable rows. The user specifies
  55. *
  56. * @verbitam
  57. * <ul>
  58. * <li>the number of columns</li>
  59. * <li>horizontal spacing between columns</li>
  60. * <li>vertical spacing between rows</li>
  61. * <li>padding around the sides of the container</li>
  62. * <li>each column's alignment</li>
  63. * </ul>
  64. * @endverbitam
  65. *
  66. * These properties give the user a lot of flexibility to make the
  67. * widgets look just right.
  68. * @code
  69. * AdjustingContainer *adjust = new AdjustingContainer;
  70. * adjust->setPadding(5, 5, 5, 5); //left, right, top, bottom
  71. * adjust->setHorizontalSpacing(3);
  72. * adjust->setVerticalSpacing(3);
  73. * adjust->setColumns(3);
  74. * adjust->setColumnAlignment(0, AdjustingContainer::LEFT);
  75. * adjust->setColumnAlignment(1, AdjustingContainer::CENTER);
  76. * adjust->setColumnAlignment(2, AdjustingContainer::RIGHT);
  77. * top->add(adjust);
  78. *
  79. * for(int j = 0; j < 9; j++)
  80. * {
  81. * gcn::Label *l;
  82. * int r = rand() % 3;
  83. * if(r == 0)
  84. * l = new gcn::Label("Short");
  85. * else if(r == 1)
  86. * l = new gcn::Label("A longer phrase");
  87. * else
  88. * l = new gcn::Label("Extravagent and wordy text");
  89. * adjust->add(l);
  90. * @endcode
  91. *
  92. * Output:
  93. * @verbitam
  94. * <pre>
  95. *+---------------------------------------------------------------------------+
  96. *| |
  97. *| A longer phrase Short Extravagent and wordy text |
  98. *| |
  99. *| Short Extravagent and wordy text Short |
  100. *| |
  101. *| Short A longer phrase A longer phrase |
  102. *| |
  103. *+---------------------------------------------------------------------------+
  104. * </pre>
  105. * @endverbitam
  106. * As you can see, each column is only as big as its largest element.
  107. * The AdjustingContainer will resize itself and rearrange its contents
  108. * based on whatever widgets it contains, allowing dynamic addition and
  109. * removal while the program is running. It also plays nicely with ScrollAreas,
  110. * allowing you to show a fixed, maximum size while not limiting the actual
  111. * container.
  112. *
  113. * For more help with using AdjustingContainers, try the Guichan forums
  114. * (http://guichan.sourceforge.net/forum/) or email mrlachatte@gmail.com.
  115. *
  116. * @author Josh Matthews
  117. */
  118. class AdjustingContainer : public gcn::Container
  119. {
  120. public:
  121. /**
  122. * Constructor.
  123. */
  124. AdjustingContainer();
  125. /**
  126. * Destructor.
  127. */
  128. virtual ~AdjustingContainer();
  129. /**
  130. * Set the number of columns to divide the widgets into.
  131. * The number of rows is derived automatically from the number
  132. * of widgets based on the number of columns. Default column
  133. * alignment is left.
  134. *
  135. * @param numberOfColumns the number of columns.
  136. */
  137. virtual void setNumberOfColumns(unsigned int numberOfColumns);
  138. /**
  139. * Set a specific column's alignment.
  140. *
  141. * @param column the column number, starting from 0.
  142. * @param alignment the column's alignment. See enum with alignments.
  143. */
  144. virtual void setColumnAlignment(unsigned int column, unsigned int alignment);
  145. /**
  146. * Set the padding for the sides of the container.
  147. *
  148. * @param paddingLeft left padding.
  149. * @param paddingRight right padding.
  150. * @param paddingTop top padding.
  151. * @param paddingBottom bottom padding.
  152. */
  153. virtual void setPadding(unsigned int paddingLeft,
  154. unsigned int paddingRight,
  155. unsigned int paddingTop,
  156. unsigned int paddingBottom);
  157. /**
  158. * Set the spacing between rows.
  159. *
  160. * @param verticalSpacing spacing in pixels.
  161. */
  162. virtual void setVerticalSpacing(unsigned int verticalSpacing);
  163. /**
  164. * Set the horizontal spacing between columns.
  165. *
  166. * @param horizontalSpacing spacing in pixels.
  167. */
  168. virtual void setHorizontalSpacing(unsigned int horizontalSpacing);
  169. /**
  170. * Rearrange the widgets and resize the container.
  171. */
  172. virtual void adjustContent();
  173. // Inherited from Container
  174. virtual void logic();
  175. virtual void add(Widget *widget);
  176. virtual void add(Widget *widget, int x, int y);
  177. virtual void remove(Widget *widget);
  178. virtual void clear();
  179. /**
  180. * Possible alignment values for each column.
  181. *
  182. * LEFT - Align content to the left of the column.
  183. * MIDDLE - Align content to the middle of the column.
  184. * RIGHT - Align content to the right of the column.
  185. */
  186. enum
  187. {
  188. LEFT = 0,
  189. CENTER,
  190. RIGHT
  191. };
  192. protected:
  193. /**
  194. * Adjust the size of the container to fit all the widgets.
  195. */
  196. virtual void adjustSize();
  197. std::vector<Widget*> mContainedWidgets;
  198. std::vector<unsigned int> mColumnWidths;
  199. std::vector<unsigned int> mColumnAlignment;
  200. std::vector<unsigned int> mRowHeights;
  201. unsigned int mWidth;
  202. unsigned int mHeight;
  203. unsigned int mNumberOfColumns;
  204. unsigned int mNumberOfRows;
  205. unsigned int mPaddingLeft;
  206. unsigned int mPaddingRight;
  207. unsigned int mPaddingTop;
  208. unsigned int mPaddingBottom;
  209. unsigned int mVerticalSpacing;
  210. unsigned int mHorizontalSpacing;
  211. };
  212. }
  213. }
  214. #endif