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.

279 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/controls/edit_line_sequence.hpp
  3. *
  4. * Purpose: Contains the edit_line_sequence class.
  5. *
  6. * Created: 23rd March 2006
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2006-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/controls/edit_line_sequence.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::edit_line_sequence class
  42. * template
  43. * (\ref group__library__windows_controls "Windows Controls" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE
  46. #define WINSTL_INCL_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE_MAJOR 1
  49. # define WINSTL_VER_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE_MINOR 0
  50. # define WINSTL_VER_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE_REVISION 7
  51. # define WINSTL_VER_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE_EDIT 14
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  57. # include <winstl/winstl.h>
  58. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  59. #ifndef WINSTL_INCL_WINSTL_CONTROLS_H_FUNCTIONS
  60. # include <winstl/controls/functions.h> // for combobox_addstring(), etc.
  61. #endif /* !WINSTL_INCL_WINSTL_CONTROLS_H_FUNCTIONS */
  62. #ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
  63. # include <winstl/memory/processheap_allocator.hpp>
  64. #endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
  65. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  66. # include <stlsoft/collections/util/collections.hpp>
  67. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  68. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
  69. # include <stlsoft/memory/auto_buffer.hpp>
  70. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  71. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
  72. # include <stlsoft/string/simple_string.hpp>
  73. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
  74. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  75. # include <stlsoft/util/std/iterator_helper.hpp>
  76. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  77. #ifndef STLSOFT_INCL_VECTOR
  78. # define STLSOFT_INCL_VECTOR
  79. # include <vector>
  80. #endif /* !STLSOFT_INCL_VECTOR */
  81. /* /////////////////////////////////////////////////////////////////////////
  82. * Namespace
  83. */
  84. #ifndef _WINSTL_NO_NAMESPACE
  85. # if defined(_STLSOFT_NO_NAMESPACE) || \
  86. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  87. /* There is no stlsoft namespace, so must define ::winstl */
  88. namespace winstl
  89. {
  90. # else
  91. /* Define stlsoft::winstl_project */
  92. namespace stlsoft
  93. {
  94. namespace winstl_project
  95. {
  96. # endif /* _STLSOFT_NO_NAMESPACE */
  97. #endif /* !_WINSTL_NO_NAMESPACE */
  98. /* /////////////////////////////////////////////////////////////////////////
  99. * Classes
  100. */
  101. /** \brief STL-like sequence for edit control contents
  102. *
  103. * \ingroup group__library__windows_controls
  104. *
  105. * This class presents an STL-like sequence interface to an edit control
  106. *
  107. * \param C The character type
  108. * \param A The allocator type. Defaults to winstl::processheap_allocator
  109. * \param S The string type. Defaults to stlsoft::simple_string
  110. * \param Q The queue container type. Defaults to std::vector
  111. */
  112. template< ss_typename_param_k C
  113. , ss_typename_param_k A = processheap_allocator<C>
  114. , ss_typename_param_k S = stlsoft_ns_qual(basic_simple_string)<C, stlsoft_ns_qual(char_traits)<C>, A>
  115. , ss_typename_param_k Q = stlsoft_ns_qual_std(vector)<S>
  116. >
  117. class edit_line_sequence
  118. : public stlsoft_ns_qual(stl_collection_tag)
  119. {
  120. /// \name Member Types
  121. /// @{
  122. public:
  123. /// \brief The character type
  124. typedef C char_type;
  125. /// \brief The allocator type
  126. typedef A allocator_type;
  127. /// \brief The string type
  128. typedef S string_type;
  129. /// \brief The value type
  130. typedef string_type value_type;
  131. /// \brief The container type
  132. typedef Q queue_type;
  133. /// \brief The current specialisation of this type
  134. typedef edit_line_sequence<S> class_type;
  135. /// \brief The non-mutating (const) iterator type
  136. typedef ss_typename_type_k queue_type::const_iterator const_iterator;
  137. /// \brief The non-mutating (const) reverse iterator type
  138. typedef ss_typename_type_k queue_type::const_reverse_iterator const_reverse_iterator;
  139. /// The non-mutable (const) reference type
  140. typedef ss_typename_type_k const_iterator::const_reference const_reference;
  141. /// @}
  142. /// \name Construction
  143. /// @{
  144. public:
  145. /// \brief Constructs on the given edit control
  146. ss_explicit_k edit_line_sequence(HWND hwndEdit, unsigned = 0);
  147. /// @}
  148. /// \name Iteration methods
  149. /// @{
  150. public:
  151. const_iterator begin() const;
  152. const_iterator end() const;
  153. const_reverse_iterator rbegin() const;
  154. const_reverse_iterator rend() const;
  155. /// @}
  156. /// \name Members
  157. /// @{
  158. private:
  159. HWND m_hwndEdit;
  160. queue_type m_queue;
  161. /// @}
  162. };
  163. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  164. inline ws_bool_t operator ==(ss_typename_type_k edit_line_sequence<C, A, S, Q>::const_iterator const& lhs, ss_typename_type_k edit_line_sequence<C, A, S, Q>::const_iterator const& rhs)
  165. {
  166. return lhs.equal(rhs);
  167. }
  168. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  169. inline ws_bool_t operator !=(ss_typename_type_k edit_line_sequence<C, A, S, Q>::const_iterator const& lhs, ss_typename_type_k edit_line_sequence<C, A, S, Q>::const_iterator const& rhs)
  170. {
  171. return !lhs.equal(rhs);
  172. }
  173. ////////////////////////////////////////////////////////////////////////////
  174. // Unit-testing
  175. #ifdef STLSOFT_UNITTEST
  176. # include "./unittest/edit_line_sequence_unittest_.h"
  177. #endif /* STLSOFT_UNITTEST */
  178. /* /////////////////////////////////////////////////////////////////////////
  179. * Implementation
  180. */
  181. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  182. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  183. inline /* ss_explicit_k */ edit_line_sequence<C, A, S, Q>::edit_line_sequence(HWND hwndEdit, unsigned /* = 0 */)
  184. : m_hwndEdit(hwndEdit)
  185. {
  186. WINSTL_ASSERT(NULL != hwndEdit);
  187. WINSTL_ASSERT(::IsWindow(hwndEdit));
  188. WINSTL_MESSAGE_ASSERT("Edit control must have both ES_MULTILINE and ES_AUTOHSCROLL styles", (ES_MULTILINE | ES_AUTOHSCROLL) == ((ES_MULTILINE | ES_AUTOHSCROLL) & ::GetWindowLong(hwndEdit, GWL_STYLE)));
  189. queue_type queue;
  190. int numLines = edit_getcount(hwndEdit);
  191. { int charTotal = 0;
  192. for(int i = 0; i < numLines; ++i)
  193. {
  194. int charIndex = charTotal + i;
  195. int lineLength = edit_linelength(hwndEdit, charIndex);
  196. stlsoft_ns_qual(auto_buffer)<char> line(1 + lineLength);
  197. lineLength = edit_getline(hwndEdit, i, &line[0], line.size());
  198. line[lineLength] = '\0';
  199. queue.push_back(line.data());
  200. charTotal += lineLength + 2;
  201. }}
  202. m_queue.swap(queue);
  203. }
  204. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  205. inline ss_typename_type_ret_k edit_line_sequence<C, A, S, Q>::const_iterator edit_line_sequence<C, A, S, Q>::begin() const
  206. {
  207. return m_queue.begin();
  208. }
  209. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  210. inline ss_typename_type_ret_k edit_line_sequence<C, A, S, Q>::const_iterator edit_line_sequence<C, A, S, Q>::end() const
  211. {
  212. return m_queue.end();
  213. }
  214. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  215. inline ss_typename_type_ret_k edit_line_sequence<C, A, S, Q>::const_reverse_iterator edit_line_sequence<C, A, S, Q>::rbegin() const
  216. {
  217. return const_reverse_iterator(end());
  218. }
  219. template<ss_typename_param_k C, ss_typename_param_k A, ss_typename_param_k S, ss_typename_param_k Q>
  220. inline ss_typename_type_ret_k edit_line_sequence<C, A, S, Q>::const_reverse_iterator edit_line_sequence<C, A, S, Q>::rend() const
  221. {
  222. return const_reverse_iterator(begin());
  223. }
  224. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  225. /* ////////////////////////////////////////////////////////////////////// */
  226. #ifndef _WINSTL_NO_NAMESPACE
  227. # if defined(_STLSOFT_NO_NAMESPACE) || \
  228. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  229. } // namespace winstl
  230. # else
  231. } // namespace winstl_project
  232. } // namespace stlsoft
  233. # endif /* _STLSOFT_NO_NAMESPACE */
  234. #endif /* !_WINSTL_NO_NAMESPACE */
  235. /* ////////////////////////////////////////////////////////////////////// */
  236. #endif /* WINSTL_INCL_WINSTL_CONTROLS_HPP_EDIT_LINE_SEQUENCE */
  237. /* ///////////////////////////// end of file //////////////////////////// */