/ext/ResizableLib/ResizableWndState.cpp

https://gitlab.com/hussinhassan80/tortoisegit · C++ · 150 lines · 75 code · 21 blank · 54 comment · 20 complexity · c9af406d7f56793acfb2d26385853951 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. #include "..\..\src\Utils\DPIAware.h"
  24. //////////////////////////////////////////////////////////////////////
  25. // Construction/Destruction
  26. //////////////////////////////////////////////////////////////////////
  27. CResizableWndState::CResizableWndState()
  28. {
  29. }
  30. CResizableWndState::~CResizableWndState()
  31. {
  32. }
  33. // used to save/restore window's size and position
  34. // either in the registry or a private .INI file
  35. // depending on your application settings
  36. #define PLACEMENT_ENT _T("WindowPlacement")
  37. #define PLACEMENT_FMT _T("%d,%d,%d,%d,%d,%d,%d,%d")
  38. /*!
  39. * This function saves the current window position and size using the base
  40. * class persist method. Minimized and maximized state is also optionally
  41. * preserved.
  42. * @sa CResizableState::WriteState
  43. * @note Window coordinates are in the form used by the system functions
  44. * GetWindowPlacement and SetWindowPlacement.
  45. *
  46. * @param pszName String that identifies stored settings
  47. * @param bRectOnly Flag that specifies wether to ignore min/max state
  48. *
  49. * @return Returns @a TRUE if successful, @a FALSE otherwise
  50. */
  51. BOOL CResizableWndState::SaveWindowRect(LPCTSTR pszName, BOOL bRectOnly)
  52. {
  53. CString data, id;
  54. WINDOWPLACEMENT wp;
  55. SecureZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
  56. wp.length = sizeof(WINDOWPLACEMENT);
  57. if (!GetResizableWnd()->GetWindowPlacement(&wp))
  58. return FALSE;
  59. CDPIAware::Instance().UnscaleWindowPlacement(&wp);
  60. // use workspace coordinates
  61. const RECT& rc = wp.rcNormalPosition;
  62. if (bRectOnly) // save size/pos only (normal state)
  63. {
  64. data.Format(PLACEMENT_FMT, rc.left, rc.top,
  65. rc.right, rc.bottom, SW_SHOWNORMAL, 0, 0, 0);
  66. }
  67. else // save also min/max state
  68. {
  69. data.Format(PLACEMENT_FMT, rc.left, rc.top,
  70. rc.right, rc.bottom, wp.showCmd, wp.flags,
  71. wp.ptMinPosition.x, wp.ptMinPosition.y);
  72. }
  73. id = CString(pszName) + PLACEMENT_ENT;
  74. return WriteState(id, data);
  75. }
  76. /*!
  77. * This function loads and set the current window position and size using
  78. * the base class persist method. Minimized and maximized state is also
  79. * optionally preserved.
  80. * @sa CResizableState::WriteState
  81. * @note Window coordinates are in the form used by the system functions
  82. * GetWindowPlacement and SetWindowPlacement.
  83. *
  84. * @param pszName String that identifies stored settings
  85. * @param bRectOnly Flag that specifies wether to ignore min/max state
  86. *
  87. * @return Returns @a TRUE if successful, @a FALSE otherwise
  88. */
  89. BOOL CResizableWndState::LoadWindowRect(LPCTSTR pszName, BOOL bRectOnly, BOOL bHorzResize, BOOL bVertResize)
  90. {
  91. CString data, id;
  92. WINDOWPLACEMENT wp;
  93. id = CString(pszName) + PLACEMENT_ENT;
  94. if (!ReadState(id, data)) // never saved before
  95. return FALSE;
  96. SecureZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
  97. wp.length = sizeof(WINDOWPLACEMENT);
  98. if (!GetResizableWnd()->GetWindowPlacement(&wp))
  99. return FALSE;
  100. // use workspace coordinates
  101. RECT& rc = wp.rcNormalPosition;
  102. long min_width = rc.right - rc.left;
  103. long min_height = rc.bottom - rc.top;
  104. if (_stscanf(data, PLACEMENT_FMT, &rc.left, &rc.top,
  105. &rc.right, &rc.bottom, &wp.showCmd, &wp.flags,
  106. &wp.ptMinPosition.x, &wp.ptMinPosition.y) == 8)
  107. {
  108. CDPIAware::Instance().ScaleWindowPlacement(&wp);
  109. if ((!bVertResize) || (rc.bottom - rc.top < min_height))
  110. rc.bottom = rc.top + min_height;
  111. if ((!bHorzResize) || (rc.right - rc.left < min_width))
  112. rc.right = rc.left + min_width;
  113. if (bRectOnly) // restore size/pos only
  114. {
  115. wp.showCmd = SW_SHOWNORMAL;
  116. wp.flags = 0;
  117. return GetResizableWnd()->SetWindowPlacement(&wp);
  118. }
  119. else // restore minimized window to normal or maximized state
  120. {
  121. if (wp.showCmd == SW_SHOWMINIMIZED && wp.flags == 0)
  122. wp.showCmd = SW_SHOWNORMAL;
  123. else if (wp.showCmd == SW_SHOWMINIMIZED && wp.flags == WPF_RESTORETOMAXIMIZED)
  124. wp.showCmd = SW_SHOWMAXIMIZED;
  125. return GetResizableWnd()->SetWindowPlacement(&wp);
  126. }
  127. }
  128. return FALSE;
  129. }