/ext/ResizableLib/ResizableWndState.cpp

https://github.com/murank/TortoiseGitMod · C++ · 137 lines · 64 code · 19 blank · 54 comment · 9 complexity · 408a7d0d6ed915271092422fbbf2a14a MD5 · raw file

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is part of ResizableLib
  4. // http://sourceforge.net/projects/resizablelib
  5. //
  6. // Copyright (C) 2000-2004,2008 by Paolo Messina
  7. // http://www.geocities.com/ppescher - mailto:ppescher@hotmail.com
  8. //
  9. // The contents of this file are subject to the Artistic License (the "License").
  10. // You may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at:
  12. // http://www.opensource.org/licenses/artistic-license.html
  13. //
  14. // If you find this code useful, credits would be nice!
  15. //
  16. /////////////////////////////////////////////////////////////////////////////
  17. /*!
  18. * @file
  19. * @brief Implementation of the CResizableWndState class.
  20. */
  21. #include "stdafx.h"
  22. #include "ResizableWndState.h"
  23. //////////////////////////////////////////////////////////////////////
  24. // Construction/Destruction
  25. //////////////////////////////////////////////////////////////////////
  26. CResizableWndState::CResizableWndState()
  27. {
  28. }
  29. CResizableWndState::~CResizableWndState()
  30. {
  31. }
  32. // used to save/restore window's size and position
  33. // either in the registry or a private .INI file
  34. // depending on your application settings
  35. #define PLACEMENT_ENT _T("WindowPlacement")
  36. #define PLACEMENT_FMT _T("%d,%d,%d,%d,%d,%d,%d,%d")
  37. /*!
  38. * This function saves the current window position and size using the base
  39. * class persist method. Minimized and maximized state is also optionally
  40. * preserved.
  41. * @sa CResizableState::WriteState
  42. * @note Window coordinates are in the form used by the system functions
  43. * GetWindowPlacement and SetWindowPlacement.
  44. *
  45. * @param pszName String that identifies stored settings
  46. * @param bRectOnly Flag that specifies wether to ignore min/max state
  47. *
  48. * @return Returns @a TRUE if successful, @a FALSE otherwise
  49. */
  50. BOOL CResizableWndState::SaveWindowRect(LPCTSTR pszName, BOOL bRectOnly)
  51. {
  52. CString data, id;
  53. WINDOWPLACEMENT wp;
  54. SecureZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
  55. wp.length = sizeof(WINDOWPLACEMENT);
  56. if (!GetResizableWnd()->GetWindowPlacement(&wp))
  57. return FALSE;
  58. // use workspace coordinates
  59. RECT& rc = wp.rcNormalPosition;
  60. if (bRectOnly) // save size/pos only (normal state)
  61. {
  62. data.Format(PLACEMENT_FMT, rc.left, rc.top,
  63. rc.right, rc.bottom, SW_SHOWNORMAL, 0, 0, 0);
  64. }
  65. else // save also min/max state
  66. {
  67. data.Format(PLACEMENT_FMT, rc.left, rc.top,
  68. rc.right, rc.bottom, wp.showCmd, wp.flags,
  69. wp.ptMinPosition.x, wp.ptMinPosition.y);
  70. }
  71. id = CString(pszName) + PLACEMENT_ENT;
  72. return WriteState(id, data);
  73. }
  74. /*!
  75. * This function loads and set the current window position and size using
  76. * the base class persist method. Minimized and maximized state is also
  77. * optionally preserved.
  78. * @sa CResizableState::WriteState
  79. * @note Window coordinates are in the form used by the system functions
  80. * GetWindowPlacement and SetWindowPlacement.
  81. *
  82. * @param pszName String that identifies stored settings
  83. * @param bRectOnly Flag that specifies wether to ignore min/max state
  84. *
  85. * @return Returns @a TRUE if successful, @a FALSE otherwise
  86. */
  87. BOOL CResizableWndState::LoadWindowRect(LPCTSTR pszName, BOOL bRectOnly)
  88. {
  89. CString data, id;
  90. WINDOWPLACEMENT wp;
  91. id = CString(pszName) + PLACEMENT_ENT;
  92. if (!ReadState(id, data)) // never saved before
  93. return FALSE;
  94. SecureZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
  95. wp.length = sizeof(WINDOWPLACEMENT);
  96. if (!GetResizableWnd()->GetWindowPlacement(&wp))
  97. return FALSE;
  98. // use workspace coordinates
  99. RECT& rc = wp.rcNormalPosition;
  100. if (_stscanf(data, PLACEMENT_FMT, &rc.left, &rc.top,
  101. &rc.right, &rc.bottom, &wp.showCmd, &wp.flags,
  102. &wp.ptMinPosition.x, &wp.ptMinPosition.y) == 8)
  103. {
  104. if (bRectOnly) // restore size/pos only
  105. {
  106. wp.showCmd = SW_SHOWNORMAL;
  107. wp.flags = 0;
  108. return GetResizableWnd()->SetWindowPlacement(&wp);
  109. }
  110. else // restore also max state
  111. {
  112. if (wp.showCmd == SW_SHOWMINIMIZED)
  113. wp.showCmd = SW_SHOWNORMAL;
  114. return GetResizableWnd()->SetWindowPlacement(&wp);
  115. }
  116. }
  117. return FALSE;
  118. }