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.

382 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/controls/commctrl_functionals.hpp
  3. *
  4. * Purpose: Functionals for application to common controls.
  5. *
  6. * Created: 8th October 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/commctrl_functionals.hpp
  40. *
  41. * \brief [C++ only] Functionals for application to common controls
  42. * (\ref group__library__windows_controls "Windows Controls" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS
  45. #define WINSTL_INCL_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS_MAJOR 4
  48. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS_MINOR 1
  49. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS_REVISION 4
  50. # define WINSTL_VER_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS_EDIT 75
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Compatibility
  54. */
  55. /*
  56. [Incompatibilies-start]
  57. STLSOFT_COMPILER_IS_DMC: __DMC__<0x0850
  58. STLSOFT_COMPILER_IS_GCC: __GNUC__<3
  59. [Incompatibilies-end]
  60. */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Includes
  63. */
  64. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  65. # include <winstl/winstl.h>
  66. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  67. #if defined(STLSOFT_COMPILER_IS_GCC) && \
  68. __GNUC__ < 3
  69. # error winstl_commctrl_functionals.h is not compatible with GNU C++ prior to 3.0
  70. #endif /* compiler */
  71. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  72. # include <stlsoft/shims/access/string.hpp>
  73. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  74. #ifndef STLSOFT_INCL_STLSOFT_ITERATORS_HPP_UNARY_FUNCTION_OUTPUT_ITERATOR_ADAPTOR
  75. # include <stlsoft/iterators/unary_function_output_iterator_adaptor.hpp>
  76. #endif /* !STLSOFT_INCL_STLSOFT_ITERATORS_HPP_UNARY_FUNCTION_OUTPUT_ITERATOR_ADAPTOR */
  77. #ifndef WINSTL_INCL_WINSTL_SHIMS_ACCESS_HPP_STRING
  78. # include <winstl/shims/access/string.hpp>
  79. #endif /* !WINSTL_INCL_WINSTL_SHIMS_ACCESS_HPP_STRING */
  80. #ifndef STLSOFT_INCL_H_COMMCTRL
  81. # define STLSOFT_INCL_H_COMMCTRL
  82. # include <commctrl.h>
  83. #endif /* !STLSOFT_INCL_H_COMMCTRL */
  84. /* /////////////////////////////////////////////////////////////////////////
  85. * Namespace
  86. */
  87. #ifndef _WINSTL_NO_NAMESPACE
  88. # if defined(_STLSOFT_NO_NAMESPACE) || \
  89. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  90. /* There is no stlsoft namespace, so must define ::winstl */
  91. namespace winstl
  92. {
  93. # else
  94. /* Define stlsoft::winstl_project */
  95. namespace stlsoft
  96. {
  97. namespace winstl_project
  98. {
  99. # endif /* _STLSOFT_NO_NAMESPACE */
  100. #endif /* !_WINSTL_NO_NAMESPACE */
  101. /* /////////////////////////////////////////////////////////////////////////
  102. * Classes
  103. */
  104. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  105. template <ws_bool_t BACK>
  106. struct listview_inserter;
  107. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  108. /** \brief A function class used to insert items into list views
  109. *
  110. * \ingroup group__library__windows_controls
  111. */
  112. template <ws_bool_t BACK = true>
  113. // [[synesis:class:unary-functor: listview_inserter]]
  114. struct listview_inserter
  115. #if !defined(STLSOFT_COMPILER_IS_DMC) || \
  116. __DMC__ > 0x0850
  117. : public stlsoft_ns_qual(unary_function_output_iterator_adaptor)<listview_inserter<BACK> >
  118. #endif /* compiler */
  119. {
  120. public:
  121. /// This type
  122. typedef listview_inserter class_type;
  123. enum
  124. {
  125. #ifdef I_IMAGENONE
  126. image_none = I_IMAGENONE
  127. #else
  128. image_none = -2
  129. #endif /* I_IMAGENONE */
  130. };
  131. public:
  132. /// Construct with the target list-view window
  133. ss_explicit_k listview_inserter(HWND hwndListview, ws_int_t iImage = image_none)
  134. : m_hwndListview(hwndListview)
  135. , m_iImage(iImage)
  136. {}
  137. listview_inserter(class_type const& rhs)
  138. : m_hwndListview(rhs.m_hwndListview)
  139. , m_iImage(rhs.m_iImage)
  140. {}
  141. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  142. /// Function call operator taking the item string
  143. template <ss_typename_param_k S>
  144. ws_int_t operator ()(S const& s)
  145. {
  146. return insert_item(stlsoft_ns_qual(c_str_ptr)(s));
  147. }
  148. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  149. /// Function call operator taking the item string
  150. ws_int_t operator ()(ws_char_a_t const* s)
  151. {
  152. return insert_item(s);
  153. }
  154. /// Function call operator taking the item string
  155. ws_int_t operator ()(ws_char_w_t const* s)
  156. {
  157. return insert_item(s);
  158. }
  159. private:
  160. ws_int_t insert_index() const;
  161. ws_int_t insert_item(ws_char_a_t const* s)
  162. {
  163. LV_ITEMA item;
  164. item.mask = LVIF_TEXT;
  165. item.pszText = const_cast<ws_char_a_t *>(s);
  166. item.iItem = insert_index();
  167. item.iSubItem = 0;
  168. if(image_none != m_iImage)
  169. {
  170. item.mask |= LVIF_IMAGE;
  171. item.iImage = m_iImage;
  172. }
  173. return (int)::SendMessage(m_hwndListview, LVM_INSERTITEMA, 0, reinterpret_cast<LPARAM>(&item));
  174. }
  175. ws_int_t insert_item(ws_char_w_t const* s)
  176. {
  177. LV_ITEMW item;
  178. item.mask = LVIF_TEXT;
  179. item.pszText = const_cast<ws_char_w_t *>(s);
  180. item.iItem = insert_index();
  181. item.iSubItem = 0;
  182. if(image_none != m_iImage)
  183. {
  184. item.mask |= LVIF_IMAGE;
  185. item.iImage = m_iImage;
  186. }
  187. return (int)::SendMessage(m_hwndListview, LVM_INSERTITEMW, 0, reinterpret_cast<LPARAM>(&item));
  188. }
  189. private:
  190. HWND m_hwndListview;
  191. ws_int_t m_iImage;
  192. };
  193. STLSOFT_TEMPLATE_SPECIALISATION
  194. inline ws_int_t listview_inserter<false>::insert_index() const
  195. {
  196. return 0; // Front insertion
  197. }
  198. STLSOFT_TEMPLATE_SPECIALISATION
  199. inline ws_int_t listview_inserter<true>::insert_index() const
  200. {
  201. return static_cast<ws_int_t>(::SendMessage(m_hwndListview, LVM_GETITEMCOUNT, 0, 0L)); // Back insertion
  202. }
  203. /** \brief A function class used to insert items at the front of list-views
  204. *
  205. * \ingroup group__library__windows_controls
  206. */
  207. typedef listview_inserter<false> listview_front_inserter;
  208. /** \brief A function class used to insert items at the back of list-views
  209. *
  210. * \ingroup group__library__windows_controls
  211. */
  212. typedef listview_inserter<true> listview_back_inserter;
  213. /** \brief A function class used to insert items into tree views
  214. *
  215. * \ingroup group__library__windows_controls
  216. */
  217. // [[synesis:class:unary-functor: treeview_inserter]]
  218. struct treeview_inserter
  219. #if !defined(STLSOFT_COMPILER_IS_DMC) || \
  220. __DMC__ > 0x0850
  221. : public stlsoft_ns_qual(unary_function_output_iterator_adaptor)<treeview_inserter>
  222. #endif /* compiler */
  223. {
  224. enum
  225. {
  226. #ifdef I_IMAGENONE
  227. image_none = I_IMAGENONE
  228. #else
  229. image_none = -2
  230. #endif /* I_IMAGENONE */
  231. };
  232. public:
  233. typedef void argument_type;
  234. typedef HTREEITEM result_type;
  235. public:
  236. ss_explicit_k treeview_inserter(HWND hwndTree, HTREEITEM hParent)
  237. : m_hwndTree(hwndTree)
  238. , m_hParent(hParent)
  239. , m_hInsertAfter(TVI_LAST)
  240. , m_iImage(image_none)
  241. {}
  242. ss_explicit_k treeview_inserter(HWND hwndTree, HTREEITEM hParent, HTREEITEM hInsertAfter)
  243. : m_hwndTree(hwndTree)
  244. , m_hParent(hParent)
  245. , m_hInsertAfter(hInsertAfter)
  246. , m_iImage(image_none)
  247. {}
  248. ss_explicit_k treeview_inserter(HWND hwndTree, HTREEITEM hParent, int iImage)
  249. : m_hwndTree(hwndTree)
  250. , m_hParent(hParent)
  251. , m_hInsertAfter(TVI_LAST)
  252. , m_iImage(iImage)
  253. {}
  254. public:
  255. template <ss_typename_param_k S>
  256. HTREEITEM operator () (S const& s)
  257. {
  258. return insert_item(stlsoft_ns_qual(c_str_ptr)(s));
  259. }
  260. HTREEITEM operator () (char const* s)
  261. {
  262. return insert_item(stlsoft_ns_qual(c_str_ptr)(s));
  263. }
  264. HTREEITEM operator () (wchar_t const* s)
  265. {
  266. return insert_item(stlsoft_ns_qual(c_str_ptr)(s));
  267. }
  268. private:
  269. HTREEITEM insert_item(char const* s)
  270. {
  271. TV_INSERTSTRUCTA tvis;
  272. tvis.hParent = m_hParent;
  273. tvis.hInsertAfter = m_hInsertAfter;
  274. tvis.item.mask = TVIF_TEXT;
  275. tvis.item.pszText = const_cast<char*>(s);
  276. if(image_none != m_iImage)
  277. {
  278. tvis.item.mask |= TVIF_IMAGE;
  279. tvis.item.iImage = m_iImage;
  280. }
  281. return (HTREEITEM)::SendMessage(m_hwndTree, TVM_INSERTITEMA, 0, (LPARAM)&tvis);
  282. }
  283. HTREEITEM insert_item(wchar_t const* s)
  284. {
  285. TV_INSERTSTRUCTW tvis;
  286. tvis.hParent = m_hParent;
  287. tvis.hInsertAfter = m_hInsertAfter;
  288. tvis.item.mask = TVIF_TEXT;
  289. tvis.item.pszText = const_cast<wchar_t*>(s);
  290. if(image_none != m_iImage)
  291. {
  292. tvis.item.mask |= TVIF_IMAGE;
  293. tvis.item.iImage = m_iImage;
  294. }
  295. return (HTREEITEM)::SendMessage(m_hwndTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
  296. }
  297. private:
  298. HWND m_hwndTree;
  299. HTREEITEM m_hParent;
  300. HTREEITEM m_hInsertAfter;
  301. int m_iImage;
  302. };
  303. ////////////////////////////////////////////////////////////////////////////
  304. // Unit-testing
  305. #ifdef STLSOFT_UNITTEST
  306. # include "./unittest/commctrl_functionals_unittest_.h"
  307. #endif /* STLSOFT_UNITTEST */
  308. /* ////////////////////////////////////////////////////////////////////// */
  309. #ifndef _WINSTL_NO_NAMESPACE
  310. # if defined(_STLSOFT_NO_NAMESPACE) || \
  311. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  312. } // namespace winstl
  313. # else
  314. } // namespace winstl_project
  315. } // namespace stlsoft
  316. # endif /* _STLSOFT_NO_NAMESPACE */
  317. #endif /* !_WINSTL_NO_NAMESPACE */
  318. /* ////////////////////////////////////////////////////////////////////// */
  319. #endif /* WINSTL_INCL_WINSTL_CONTROLS_HPP_COMMCTRL_FUNCTIONALS */
  320. /* ///////////////////////////// end of file //////////////////////////// */