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.

465 lines
15 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/controls/treeview_sequence.hpp
  3. *
  4. * Purpose: Contains the treeview sequence classes.
  5. *
  6. * Created: 1st December 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-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/treeview_sequence.hpp
  40. *
  41. * \brief [C++ only] Definition of the winstl::treeview_child_sequence,
  42. * winstl::treeview_peer_sequence and winstl::treeview_visible_sequence
  43. * classes, and their supporting types
  44. * (\ref group__library__windows_controls "Windows Controls" Library).
  45. */
  46. #ifndef WINSTL_INCL_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE
  47. #define WINSTL_INCL_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE
  48. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  49. # define WINSTL_VER_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE_MAJOR 4
  50. # define WINSTL_VER_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE_MINOR 0
  51. # define WINSTL_VER_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE_REVISION 6
  52. # define WINSTL_VER_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE_EDIT 71
  53. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  54. /* /////////////////////////////////////////////////////////////////////////
  55. * Includes
  56. */
  57. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  58. # include <winstl/winstl.h>
  59. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  60. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  61. # include <stlsoft/util/std/iterator_helper.hpp>
  62. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  63. #ifndef WINSTL_INCL_WINSTL_CONTROLS_H_COMMCTRL_FUNCTIONS
  64. # include <winstl/controls/commctrl_functions.h> // for treeview_getnext(), etc.
  65. #endif /* !WINSTL_INCL_WINSTL_CONTROLS_H_COMMCTRL_FUNCTIONS */
  66. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  67. # include <stlsoft/collections/util/collections.hpp>
  68. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  69. /* ////////////////////////////////////////////////////////////////////// */
  70. #ifndef _WINSTL_NO_NAMESPACE
  71. # if defined(_STLSOFT_NO_NAMESPACE) || \
  72. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  73. /* There is no stlsoft namespace, so must define ::winstl */
  74. namespace winstl
  75. {
  76. # else
  77. /* Define stlsoft::winstl_project */
  78. namespace stlsoft
  79. {
  80. namespace winstl_project
  81. {
  82. # endif /* _STLSOFT_NO_NAMESPACE */
  83. #endif /* !_WINSTL_NO_NAMESPACE */
  84. /* ////////////////////////////////////////////////////////////////////// */
  85. /** \brief Iterator for the treeview_child_sequence, treeview_peer_sequence and treeview_visible_sequence classes
  86. *
  87. * \ingroup group__library__windows_controls
  88. *
  89. * \param N The windows message that is used to access the next element in the iteration sequence
  90. * \param P The windows message that is used to access the previous element in the iteration sequence
  91. */
  92. template <UINT N, UINT P>
  93. class treeview_sequence_const_iterator
  94. : public stlsoft_ns_qual(iterator_base)<winstl_ns_qual_std(forward_iterator_tag)
  95. , HTREEITEM
  96. , ws_ptrdiff_t
  97. , void // By-Value Temporary reference category
  98. , HTREEITEM // By-Value Temporary reference category
  99. >
  100. {
  101. /// \name Member Types
  102. /// @{
  103. public:
  104. /// The current parameterisation of the type
  105. typedef treeview_sequence_const_iterator<N, P> class_type;
  106. /// The value type
  107. typedef HTREEITEM value_type;
  108. /// The difference type
  109. typedef ws_ptrdiff_t difference_type;
  110. /// The effective reference type
  111. typedef value_type effective_reference;
  112. /// @}
  113. /// \name Construction
  114. /// @{
  115. private:
  116. #if 0
  117. // friend class treeview_sequence_base<N, P>;
  118. friend struct treeview_sequence_const_iterator_friend;
  119. friend class treeview_visible_sequence;
  120. #else /* ? 0 */
  121. public:
  122. #endif /* 0 */
  123. treeview_sequence_const_iterator(HWND hwndTree, HTREEITEM hitem);
  124. public:
  125. /// Default constructor
  126. treeview_sequence_const_iterator();
  127. /// Copy constructor
  128. treeview_sequence_const_iterator(class_type const& rhs);
  129. /// Copy assignment operator
  130. treeview_sequence_const_iterator& operator =(class_type const& rhs);
  131. /// @}
  132. /// \name Forward Iterator methods
  133. /// @{
  134. public:
  135. /// Derefences and returns the current item
  136. effective_reference operator *() const;
  137. /// Pre-increment
  138. class_type& operator ++();
  139. /// Post-increment
  140. class_type operator ++(int);
  141. /// Evaluates whether \c this and \c rhs are equivalent
  142. ws_bool_t operator ==(class_type const& rhs) const;
  143. /// Evaluates whether \c this and \c rhs are not equivalent
  144. ws_bool_t operator !=(class_type const& rhs) const;
  145. /// @}
  146. /// \name Members
  147. /// @{
  148. private:
  149. HWND m_hwnd;
  150. HTREEITEM m_hitem;
  151. /// @}
  152. };
  153. /** \brief Base class for the treeview_child_sequence,
  154. * treeview_peer_sequence and treeview_visible_sequence classes.
  155. *
  156. * \ingroup group__library__windows_controls
  157. */
  158. template <UINT N, UINT P>
  159. class treeview_sequence_base
  160. : public stlsoft_ns_qual(stl_collection_tag)
  161. {
  162. /// \name Member Types
  163. /// @{
  164. #if defined(STLSOFT_COMPILER_IS_DMC) && \
  165. __DMC__ < 0x0840
  166. public:
  167. #else /* ? __DMC__ < 0x0840 */
  168. protected:
  169. #endif /* __DMC__ < 0x0840 */
  170. /// This class
  171. typedef treeview_sequence_base<N, P> class_type;
  172. /// The non-mutating (const) iterator type
  173. typedef treeview_sequence_const_iterator<N, P> const_iterator;
  174. /// The value type
  175. typedef ss_typename_type_k const_iterator::value_type value_type;
  176. /// The difference type
  177. typedef ss_typename_type_k const_iterator::difference_type difference_type;
  178. /// @}
  179. /// \name Construction
  180. /// @{
  181. protected:
  182. /// Constructs from the given tree and item
  183. treeview_sequence_base(HWND hwndTree, HTREEITEM hitem);
  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. /// Ends the iteration
  193. ///
  194. /// \return An iterator representing the end of the sequence
  195. const_iterator end() const;
  196. /// @}
  197. /// \name Members
  198. /// @{
  199. private:
  200. HWND m_hwnd;
  201. HTREEITEM m_hitem;
  202. /// @}
  203. };
  204. // class treeview_child_sequence
  205. /** \brief Presents an STL-like sequence interface to the children of a given node in a tree-view
  206. *
  207. * \ingroup group__library__windows_controls
  208. */
  209. class treeview_child_sequence
  210. : public treeview_sequence_base<TVGN_NEXT, TVGN_PREVIOUS>
  211. {
  212. /// \name Member Types
  213. /// @{
  214. private:
  215. typedef treeview_sequence_base<TVGN_NEXT, TVGN_PREVIOUS> base_class_type;
  216. public:
  217. /// This class
  218. typedef treeview_child_sequence class_type;
  219. /// The non-mutating (const) iterator type
  220. typedef base_class_type::const_iterator const_iterator;
  221. /// The value type
  222. typedef base_class_type::value_type value_type;
  223. /// The difference type
  224. typedef base_class_type::difference_type difference_type;
  225. /// @}
  226. /// \name Construction
  227. /// @{
  228. public:
  229. /// Create sequence of the children of \c hitem in the given tree
  230. treeview_child_sequence(HWND hwndTree, HTREEITEM hitem);
  231. /// Create sequence of the children of the root in the given tree
  232. ss_explicit_k treeview_child_sequence(HWND hwndTree);
  233. /// @}
  234. };
  235. // class treeview_peer_sequence
  236. /** \brief brief Presents an STL-like sequence interface to the peers of a given node in a tree-view
  237. *
  238. * \ingroup group__library__windows_controls
  239. */
  240. class treeview_peer_sequence
  241. : public treeview_sequence_base<TVGN_NEXT, TVGN_PREVIOUS>
  242. {
  243. /// \name Member Types
  244. /// @{
  245. private:
  246. typedef treeview_sequence_base<TVGN_NEXT, TVGN_PREVIOUS> base_class_type;
  247. public:
  248. /// This class
  249. typedef treeview_child_sequence class_type;
  250. /// The non-mutating (const) iterator type
  251. typedef base_class_type::const_iterator const_iterator;
  252. /// The value type
  253. typedef base_class_type::value_type value_type;
  254. /// The difference type
  255. typedef base_class_type::difference_type difference_type;
  256. /// @}
  257. /// \name Construction
  258. /// @{
  259. public:
  260. /// Create sequence of the peers of \c hitem in the given tree
  261. treeview_peer_sequence(HWND hwndTree, HTREEITEM hitem);
  262. /// @}
  263. };
  264. // class treeview_visible_sequence
  265. /** \brief Presents an STL-like sequence interface to the visible items in a tree-view
  266. *
  267. * \ingroup group__library__windows_controls
  268. */
  269. class treeview_visible_sequence
  270. : public treeview_sequence_base<TVGN_NEXTVISIBLE, TVGN_PREVIOUSVISIBLE>
  271. {
  272. /// \name Member Types
  273. /// @{
  274. private:
  275. typedef treeview_sequence_base<TVGN_NEXTVISIBLE, TVGN_PREVIOUSVISIBLE> base_class_type;
  276. public:
  277. /// This class
  278. typedef treeview_child_sequence class_type;
  279. /// The non-mutating (const) iterator type
  280. typedef base_class_type::const_iterator const_iterator;
  281. /// The value type
  282. typedef base_class_type::value_type value_type;
  283. /// The difference type
  284. typedef base_class_type::difference_type difference_type;
  285. /// @}
  286. /// \name Construction
  287. /// @{
  288. public:
  289. /// Create sequence of the visible items in the given tree
  290. ss_explicit_k treeview_visible_sequence(HWND hwndTree);
  291. /// @}
  292. };
  293. /* /////////////////////////////////////////////////////////////////////////
  294. * Unit-testing
  295. */
  296. #ifdef STLSOFT_UNITTEST
  297. # include "./unittest/treeview_sequence_unittest_.h"
  298. #endif /* STLSOFT_UNITTEST */
  299. /* /////////////////////////////////////////////////////////////////////////
  300. * Implementation
  301. */
  302. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  303. // treeview_sequence_const_iterator
  304. template <UINT N, UINT P>
  305. inline treeview_sequence_const_iterator<N, P>::treeview_sequence_const_iterator(HWND hwndTree, HTREEITEM hitem)
  306. : m_hwnd(hwndTree)
  307. , m_hitem(hitem)
  308. {}
  309. template <UINT N, UINT P>
  310. inline treeview_sequence_const_iterator<N, P>::treeview_sequence_const_iterator()
  311. : m_hwnd(NULL)
  312. , m_hitem(NULL)
  313. {}
  314. template <UINT N, UINT P>
  315. inline treeview_sequence_const_iterator<N, P>::treeview_sequence_const_iterator(treeview_sequence_const_iterator<N, P> const& rhs)
  316. : m_hwnd(rhs.m_hwnd)
  317. , m_hitem(rhs.m_hitem)
  318. {}
  319. template <UINT N, UINT P>
  320. inline treeview_sequence_const_iterator<N, P> &treeview_sequence_const_iterator<N, P>::operator =(treeview_sequence_const_iterator<N, P> const& rhs)
  321. {
  322. m_hwnd = rhs.m_hwnd;
  323. m_hitem = rhs.m_hitem;
  324. return *this;
  325. }
  326. template <UINT N, UINT P>
  327. inline HTREEITEM treeview_sequence_const_iterator<N, P>::operator *() const
  328. {
  329. return m_hitem;
  330. }
  331. template <UINT N, UINT P>
  332. inline ss_typename_type_ret_k treeview_sequence_const_iterator<N, P>::class_type& treeview_sequence_const_iterator<N, P>::operator ++()
  333. {
  334. if(m_hitem != NULL)
  335. {
  336. m_hitem = treeview_getnextitem(m_hwnd, m_hitem, N);
  337. }
  338. return *this;
  339. }
  340. template <UINT N, UINT P>
  341. inline ss_typename_type_ret_k treeview_sequence_const_iterator<N, P>::class_type treeview_sequence_const_iterator<N, P>::operator ++(int)
  342. {
  343. class_type ret(*this);
  344. operator ++();
  345. return ret;
  346. }
  347. template <UINT N, UINT P>
  348. inline ws_bool_t treeview_sequence_const_iterator<N, P>::operator ==(treeview_sequence_const_iterator<N, P> const& rhs) const
  349. {
  350. return m_hitem == rhs.m_hitem;
  351. }
  352. template <UINT N, UINT P>
  353. inline ws_bool_t treeview_sequence_const_iterator<N, P>::operator !=(treeview_sequence_const_iterator<N, P> const& rhs) const
  354. {
  355. return !operator ==(rhs);
  356. }
  357. // treeview_sequence_base
  358. template <UINT N, UINT P>
  359. inline treeview_sequence_base<N, P>::treeview_sequence_base(HWND hwndTree, HTREEITEM hitem)
  360. : m_hwnd(hwndTree)
  361. , m_hitem(hitem)
  362. {}
  363. template <UINT N, UINT P>
  364. inline ss_typename_type_ret_k treeview_sequence_base<N, P>::const_iterator treeview_sequence_base<N, P>::begin() const
  365. {
  366. return const_iterator(m_hwnd, m_hitem);
  367. }
  368. template <UINT N, UINT P>
  369. inline ss_typename_type_ret_k treeview_sequence_base<N, P>::const_iterator treeview_sequence_base<N, P>::end() const
  370. {
  371. return const_iterator();
  372. }
  373. // treeview_child_sequence
  374. inline treeview_child_sequence::treeview_child_sequence(HWND hwndTree, HTREEITEM hitem)
  375. : base_class_type(hwndTree, treeview_getchilditem(hwndTree, hitem))
  376. {}
  377. inline treeview_child_sequence::treeview_child_sequence(HWND hwndTree)
  378. : base_class_type(hwndTree, treeview_getchilditem(hwndTree, treeview_getrootitem(hwndTree)))
  379. {}
  380. // treeview_peer_sequence
  381. inline treeview_peer_sequence::treeview_peer_sequence(HWND hwndTree, HTREEITEM hitem)
  382. : base_class_type(hwndTree, hitem)
  383. {}
  384. // treeview_visible_sequence
  385. inline treeview_visible_sequence::treeview_visible_sequence(HWND hwndTree)
  386. : base_class_type(hwndTree, treeview_getnextitem(hwndTree, NULL, TVGN_FIRSTVISIBLE))
  387. {}
  388. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  389. /* ////////////////////////////////////////////////////////////////////// */
  390. #ifndef _WINSTL_NO_NAMESPACE
  391. # if defined(_STLSOFT_NO_NAMESPACE) || \
  392. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  393. } // namespace winstl
  394. # else
  395. } // namespace winstl_project
  396. } // namespace stlsoft
  397. # endif /* _STLSOFT_NO_NAMESPACE */
  398. #endif /* !_WINSTL_NO_NAMESPACE */
  399. /* ////////////////////////////////////////////////////////////////////// */
  400. #endif /* WINSTL_INCL_WINSTL_CONTROLS_HPP_TREEVIEW_SEQUENCE */
  401. /* ///////////////////////////// end of file //////////////////////////// */