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
8.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: atlstl/window/about_dialog.hpp
  3. *
  4. * Purpose: Simple 'about' dialog, that shell-executes hyperlinks.
  5. *
  6. * Created: 30th January 2000
  7. * Updated: 9th September 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2000-2010, 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 atlstl/window/about_dialog.hpp
  40. *
  41. * \brief [C++ only; requires ATL library] Definition of the
  42. * atlstl::AboutDialog and atlstl::AboutDialogId dialog implementation
  43. * class templates
  44. * (\ref group__library__windows_window "Windows Window" Library).
  45. */
  46. #ifndef ATLSTL_INCL_ATLSTL_WINDOW_HPP_ABOUT_DIALOG
  47. #define ATLSTL_INCL_ATLSTL_WINDOW_HPP_ABOUT_DIALOG
  48. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  49. # define ATLSTL_VER_ATLSTL_WINDOW_HPP_ABOUT_DIALOG_MAJOR 4
  50. # define ATLSTL_VER_ATLSTL_WINDOW_HPP_ABOUT_DIALOG_MINOR 0
  51. # define ATLSTL_VER_ATLSTL_WINDOW_HPP_ABOUT_DIALOG_REVISION 3
  52. # define ATLSTL_VER_ATLSTL_WINDOW_HPP_ABOUT_DIALOG_EDIT 54
  53. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  54. /* /////////////////////////////////////////////////////////////////////////
  55. * Compatibility
  56. */
  57. /*
  58. [Incompatibilies-start]
  59. STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
  60. [Incompatibilies-end]
  61. */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Includes
  64. */
  65. #ifndef ATLSTL_INCL_ATLSTL_HPP_ATLSTL
  66. # include <atlstl/atlstl.hpp>
  67. #endif /* !ATLSTL_INCL_ATLSTL_HPP_ATLSTL */
  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_MEMORY_HPP_MALLOC_ALLOCATOR
  72. # include <stlsoft/memory/malloc_allocator.hpp>
  73. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_MALLOC_ALLOCATOR */
  74. #ifndef STLSOFT_INCL_SYS_H_ATLWIN
  75. # define STLSOFT_INCL_SYS_H_ATLWIN
  76. # include <atlwin.h> // for CDialogImplBase
  77. #endif /* !STLSOFT_INCL_SYS_H_ATLWIN */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Namespace
  80. */
  81. #ifndef _ATLSTL_NO_NAMESPACE
  82. # if defined(_STLSOFT_NO_NAMESPACE) || \
  83. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  84. /* There is no stlsoft namespace, so must define ::atlstl */
  85. namespace atlstl
  86. {
  87. # else
  88. /* Define stlsoft::atlstl_project */
  89. namespace stlsoft
  90. {
  91. namespace atlstl_project
  92. {
  93. # endif /* _STLSOFT_NO_NAMESPACE */
  94. #endif /* !_ATLSTL_NO_NAMESPACE */
  95. /* /////////////////////////////////////////////////////////////////////////
  96. * Classes
  97. */
  98. /** \brief Turns an integral value into a type
  99. *
  100. * \ingroup group__library__windows_window
  101. *
  102. */
  103. template <int N>
  104. struct IDD_to_type
  105. {
  106. enum { IDD = N };
  107. };
  108. /** \brief About dialog class based on a type that specifies an Id, and a parent window
  109. * type
  110. *
  111. * \ingroup group__library__windows_window
  112. */
  113. // [[synesis:class:ui-window: atlstl::AboutDialog<T<T>, T<B>>]]
  114. template<
  115. class T
  116. , class B = CWindow
  117. >
  118. class AboutDialog
  119. : public CDialogImplBaseT<B>
  120. {
  121. public:
  122. typedef CDialogImplBaseT<B> parent_class_type;
  123. typedef AboutDialog<T, B> class_type;
  124. public:
  125. INT_PTR DoModal()
  126. {
  127. return this->DoModal(::GetActiveWindow());
  128. }
  129. INT_PTR DoModal(HWND hWndParent)
  130. {
  131. ATLASSERT(m_hWnd == NULL);
  132. // Borrow thunking logic from ATL's own CSimpleDialog
  133. parent_class_type* pThis = this;
  134. _Module.AddCreateWndData(&m_thunk.cd, pThis);
  135. int nRet = ::DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(T::IDD), hWndParent, (DLGPROC)StartDialogProc);
  136. m_hWnd = NULL;
  137. return nRet;
  138. }
  139. BEGIN_MSG_MAP(class_type)
  140. MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  141. COMMAND_RANGE_HANDLER(IDOK, IDNO, OnCloseCmd)
  142. MESSAGE_HANDLER(WM_COMMAND, OnCommand)
  143. END_MSG_MAP()
  144. protected:
  145. LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  146. {
  147. CenterWindow(GetParent());
  148. return TRUE;
  149. }
  150. LRESULT OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  151. {
  152. ::EndDialog(m_hWnd, wID);
  153. return 0;
  154. }
  155. LRESULT OnCommand(UINT /* uMsg */, WPARAM /* wParam */, LPARAM lParam, BOOL &bHandled)
  156. {
  157. bHandled = false;
  158. HWND hwndCtrl = (HWND)lParam;
  159. // 1. Must be a child control
  160. if(NULL != hwndCtrl)
  161. {
  162. // 2. Must be a button
  163. LRESULT ctrlCode = ::SendMessage(hwndCtrl, WM_GETDLGCODE, 0, 0L);
  164. if(DLGC_BUTTON & ctrlCode)
  165. {
  166. typedef ::stlsoft::auto_buffer_old<
  167. TCHAR
  168. , ::stlsoft::malloc_allocator<TCHAR>
  169. , 512
  170. > buffer_t;
  171. // 3. Get text
  172. //
  173. // Note that this uses buffer.size(), so that it does not matter, if the buffer
  174. // allocation fails, whether allocator throws exceptions or returns NULL.
  175. buffer_t buffer(1 + ::GetWindowTextLength(hwndCtrl));
  176. const int len = ::GetWindowText(hwndCtrl, &buffer[0], buffer.size());
  177. buffer[len] = '\0';
  178. // 4. Check whether contains a '.'
  179. if( 0 < len &&
  180. NULL != _tcschr(buffer.data(), '.'))
  181. {
  182. SHELLEXECUTEINFO sei;
  183. sei.cbSize = sizeof(sei);
  184. sei.fMask = SEE_MASK_NOCLOSEPROCESS;
  185. sei.hwnd = *this;
  186. sei.lpVerb = _T("open");
  187. sei.lpFile = buffer.data();
  188. sei.lpParameters = NULL;
  189. sei.lpDirectory = NULL;
  190. sei.nShow = SW_SHOWNORMAL;
  191. sei.hInstApp = NULL;
  192. sei.lpIDList = NULL;
  193. sei.lpClass = NULL;
  194. sei.hkeyClass = NULL;
  195. sei.dwHotKey = NULL;
  196. sei.hIcon = NULL;
  197. sei.hProcess = NULL;
  198. // Execute - ignore failures
  199. (void)::ShellExecuteEx(&sei);
  200. bHandled = true;
  201. }
  202. }
  203. }
  204. return 0;
  205. }
  206. };
  207. /** \brief About dialog class based from an Id
  208. *
  209. * \ingroup group__library__windows_window
  210. *
  211. */
  212. // [[synesis:class:ui-window: atlstl::AboutDialogId<UINT, T<B>>]]
  213. template<
  214. UINT ID
  215. , class B = CWindow
  216. >
  217. class AboutDialogId
  218. : public AboutDialog<IDD_to_type<ID>, B>
  219. {
  220. };
  221. /* ////////////////////////////////////////////////////////////////////// */
  222. #ifndef _ATLSTL_NO_NAMESPACE
  223. # if defined(_STLSOFT_NO_NAMESPACE) || \
  224. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  225. } // namespace atlstl
  226. # else
  227. } // namespace atlstl_project
  228. } // namespace stlsoft
  229. # endif /* _STLSOFT_NO_NAMESPACE */
  230. #endif /* !_ATLSTL_NO_NAMESPACE */
  231. /* ////////////////////////////////////////////////////////////////////// */
  232. #endif /* ATLSTL_INCL_ATLSTL_WINDOW_HPP_ABOUT_DIALOG */
  233. /* ///////////////////////////// end of file //////////////////////////// */