You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/controls/combobox_sequence.hpp
  3. *
  4. * Purpose: Contains the combobox_sequence class.
  5. *
  6. * Created: 13th November 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Thanks: To Pablo Aguilar for some patches.
  10. *
  11. * Home: http://stlsoft.org/
  12. *
  13. * Copyright (c) 2002-2009, Matthew Wilson and Synesis Software
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice, this
  20. * list of conditions and the following disclaimer.
  21. * - Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  25. * any contributors may be used to endorse or promote products derived from
  26. * this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * ////////////////////////////////////////////////////////////////////// */
  41. /** \file winstl/controls/combobox_sequence.hpp
  42. *
  43. * \brief [C++ only] Definition of the winstl::combobox_sequence class
  44. * template
  45. * (\ref group__library__windows_controls "Windows Controls" Library).
  46. */
  47. #ifndef WINSTL_INCL_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE
  48. #define WINSTL_INCL_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE
  49. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  50. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE_MAJOR 3
  51. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE_MINOR 4
  52. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE_REVISION 2
  53. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE_EDIT 60
  54. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  55. /* /////////////////////////////////////////////////////////////////////////
  56. * Includes
  57. */
  58. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  59. # include <winstl/winstl.h>
  60. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  61. #ifndef WINSTL_INCL_WINSTL_CONTROLS_HPP_LISTBOX_CONST_ITERATOR
  62. # include <winstl/controls/listbox_const_iterator.hpp>
  63. #endif /* !WINSTL_INCL_WINSTL_CONTROLS_HPP_LISTBOX_CONST_ITERATOR */
  64. #ifndef WINSTL_INCL_WINSTL_CONTROLS_H_FUNCTIONS
  65. # include <winstl/controls/functions.h> // for combobox_addstring(), etc.
  66. #endif /* !WINSTL_INCL_WINSTL_CONTROLS_H_FUNCTIONS */
  67. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  68. # include <stlsoft/collections/util/collections.hpp>
  69. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  70. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  71. # include <stlsoft/util/std/iterator_helper.hpp>
  72. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  73. /* /////////////////////////////////////////////////////////////////////////
  74. * Namespace
  75. */
  76. #ifndef _WINSTL_NO_NAMESPACE
  77. # if defined(_STLSOFT_NO_NAMESPACE) || \
  78. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  79. /* There is no stlsoft namespace, so must define ::winstl */
  80. namespace winstl
  81. {
  82. # else
  83. /* Define stlsoft::winstl_project */
  84. namespace stlsoft
  85. {
  86. namespace winstl_project
  87. {
  88. # endif /* _STLSOFT_NO_NAMESPACE */
  89. #endif /* !_WINSTL_NO_NAMESPACE */
  90. /* /////////////////////////////////////////////////////////////////////////
  91. * Classes
  92. */
  93. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  94. struct combobox_operation_traits
  95. {
  96. public:
  97. static ws_int_t err_constant()
  98. {
  99. return CB_ERR;
  100. }
  101. static ws_int_t get_count(HWND hwnd)
  102. {
  103. return combobox_getcount(hwnd);
  104. }
  105. static ws_int_t get_text_len(HWND hwnd, ws_int_t index)
  106. {
  107. return combobox_gettextlen(hwnd, index);
  108. }
  109. static ws_int_t get_text(HWND hwnd, ws_int_t index, ws_char_a_t *s)
  110. {
  111. return combobox_gettext_a(hwnd, index, s);
  112. }
  113. static ws_int_t get_text(HWND hwnd, ws_int_t index, ws_char_w_t *s)
  114. {
  115. return combobox_gettext_w(hwnd, index, s);
  116. }
  117. };
  118. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  119. /** \brief STL-like sequence for combo-box contents
  120. *
  121. * \ingroup group__library__windows_controls
  122. *
  123. * This class presents an STL-like sequence interface to a combo-box
  124. *
  125. * \param S The string type
  126. */
  127. template <ss_typename_param_k S>
  128. class combobox_sequence
  129. : public stlsoft_ns_qual(stl_collection_tag)
  130. {
  131. /// \name Member Types
  132. /// @{
  133. public:
  134. /// The current parameterisation of the type
  135. typedef combobox_sequence<S> class_type;
  136. /// The non-mutating (const) iterator type
  137. typedef listbox_const_iterator<S, combobox_operation_traits> const_iterator;
  138. /// The character type
  139. typedef ss_typename_type_k const_iterator::char_type char_type;
  140. /// The value type
  141. typedef ss_typename_type_k const_iterator::value_type value_type;
  142. /// The non-mutable (const) reference type
  143. typedef ss_typename_type_k const_iterator::const_reference const_reference;
  144. /// The size type
  145. typedef ss_typename_type_k const_iterator::size_type size_type;
  146. /// The difference type
  147. typedef ss_typename_type_k const_iterator::difference_type difference_type;
  148. #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  149. /// The non-mutating (const) reverse iterator type
  150. typedef ss_typename_type_k const_iterator::const_reverse_iterator_type const_reverse_iterator;
  151. #endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  152. /// The allocator type
  153. typedef ss_typename_type_k const_iterator::allocator_type allocator_type;
  154. private:
  155. typedef ss_typename_type_k const_iterator::buffer_type buffer_type;
  156. typedef combobox_operation_traits control_traits_type;
  157. /// @}
  158. /// \name Construction
  159. /// @{
  160. public:
  161. /// Constructs from the given combo-box
  162. ss_explicit_k combobox_sequence(HWND hwndComboBox)
  163. : m_hwnd(hwndComboBox)
  164. {}
  165. /// @}
  166. /// \name State
  167. /// @{
  168. public:
  169. /// Returns the number of elements in the combo-box
  170. size_type size() const
  171. {
  172. return static_cast<size_type>(control_traits_type::get_count(m_hwnd));
  173. }
  174. /// Indicates whether the combo-box is empty
  175. ws_bool_t empty() const
  176. {
  177. return size() == 0;
  178. }
  179. /// Returns the maximum number of items that the combo-box can contain
  180. static size_type max_size()
  181. {
  182. return static_cast<size_type>(-1) / sizeof(LPCTSTR);
  183. }
  184. /// @}
  185. /// \name Iteration
  186. /// @{
  187. public:
  188. /// Begins the iteration
  189. ///
  190. /// \return An iterator representing the start of the sequence
  191. const_iterator begin() const
  192. {
  193. return const_iterator(m_hwnd, 0);
  194. }
  195. /// Ends the iteration
  196. ///
  197. /// \return An iterator representing the end of the sequence
  198. const_iterator end() const
  199. {
  200. return const_iterator(m_hwnd, static_cast<int>(size()));
  201. }
  202. #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  203. /// Begins the reverse iteration
  204. ///
  205. /// \return An iterator representing the start of the reverse sequence
  206. const_reverse_iterator rbegin() const
  207. {
  208. return const_reverse_iterator(end());
  209. }
  210. /// Ends the reverse iteration
  211. ///
  212. /// \return An iterator representing the end of the reverse sequence
  213. const_reverse_iterator rend() const
  214. {
  215. return const_reverse_iterator(begin());
  216. }
  217. #endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  218. /// @}
  219. /// \name Element Access
  220. /// @{
  221. public:
  222. /// Returns the item at the given index
  223. value_type operator [](difference_type index) const
  224. {
  225. return const_iterator::get_value_at_(m_hwnd, index);
  226. }
  227. /// @}
  228. /// \name Members
  229. /// @{
  230. private:
  231. HWND m_hwnd;
  232. /// @}
  233. };
  234. ////////////////////////////////////////////////////////////////////////////
  235. // Unit-testing
  236. #ifdef STLSOFT_UNITTEST
  237. # include "./unittest/combobox_sequence_unittest_.h"
  238. #endif /* STLSOFT_UNITTEST */
  239. /* ////////////////////////////////////////////////////////////////////// */
  240. #ifndef _WINSTL_NO_NAMESPACE
  241. # if defined(_STLSOFT_NO_NAMESPACE) || \
  242. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  243. } // namespace winstl
  244. # else
  245. } // namespace winstl_project
  246. } // namespace stlsoft
  247. # endif /* _STLSOFT_NO_NAMESPACE */
  248. #endif /* !_WINSTL_NO_NAMESPACE */
  249. /* ////////////////////////////////////////////////////////////////////// */
  250. #endif /* WINSTL_INCL_WINSTL_CONTROLS_HPP_COMBOBOX_SEQUENCE */
  251. /* ///////////////////////////// end of file //////////////////////////// */