Adjust checkbox and radio control widths to required size
[TortoiseGit.git] / src / TortoiseProc / Settings / SettingsPropPage.h
blobb4cb44f1743db72745a123ecab45158bf6412dd2
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2007-2010 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
20 #include "MessageBox.h"
22 /**
23 * \ingroup TortoiseProc
24 * Base class for all the settings property pages
26 class ISettingsPropPage : public CPropertyPage
28 DECLARE_DYNAMIC(ISettingsPropPage)
29 public:
30 // simple construction
31 ISettingsPropPage();
32 explicit ISettingsPropPage(UINT nIDTemplate, UINT nIDCaption = 0, DWORD dwSize = sizeof(PROPSHEETPAGE));
33 explicit ISettingsPropPage(LPCTSTR lpszTemplateName, UINT nIDCaption = 0, DWORD dwSize = sizeof(PROPSHEETPAGE));
35 // extended construction
36 ISettingsPropPage(UINT nIDTemplate, UINT nIDCaption,
37 UINT nIDHeaderTitle, UINT nIDHeaderSubTitle = 0, DWORD dwSize = sizeof(PROPSHEETPAGE));
38 ISettingsPropPage(LPCTSTR lpszTemplateName, UINT nIDCaption,
39 UINT nIDHeaderTitle, UINT nIDHeaderSubTitle = 0, DWORD dwSize = sizeof(PROPSHEETPAGE));
41 virtual ~ISettingsPropPage();
43 enum SettingsRestart
45 Restart_None = 0,
46 Restart_System = 1,
47 Restart_Cache = 2
50 /**
51 * Returns the icon ID
53 virtual UINT GetIconID() = 0;
55 /**
56 * Returns the restart code
58 virtual SettingsRestart GetRestart() {return m_restart;}
60 protected:
62 SettingsRestart m_restart;
64 /**
65 * Adjusts the size of a checkbox or radio button control.
66 * Since we always make the size of those bigger than 'necessary'
67 * for making sure that translated strings can fit in those too,
68 * this method can reduce the size of those controls again to only
69 * fit the text.
71 void AdjustControlSize(UINT nID)
73 CWnd* pwndDlgItem = GetDlgItem(nID);
74 if (!pwndDlgItem)
75 return;
76 // adjust the size of the control to fit its content
77 CString sControlText;
78 pwndDlgItem->GetWindowText(sControlText);
79 // next step: find the rectangle the control text needs to
80 // be displayed
82 CDC* pDC = pwndDlgItem->GetWindowDC();
83 RECT controlrect;
84 RECT controlrectorig;
85 pwndDlgItem->GetWindowRect(&controlrect);
86 ::MapWindowPoints(nullptr, GetSafeHwnd(), (LPPOINT)&controlrect, 2);
87 controlrectorig = controlrect;
88 if (pDC)
90 CFont* font = pwndDlgItem->GetFont();
91 CFont* pOldFont = pDC->SelectObject(font);
92 if (pDC->DrawText(sControlText, -1, &controlrect, DT_WORDBREAK | DT_EDITCONTROL | DT_EXPANDTABS | DT_LEFT | DT_CALCRECT))
94 // now we have the rectangle the control really needs
95 if ((controlrectorig.right - controlrectorig.left) > (controlrect.right - controlrect.left))
97 // we're dealing with radio buttons and check boxes,
98 // which means we have to add a little space for the checkbox
99 // the value of 3 pixels added here is necessary in case certain visual styles have
100 // been disabled. Without this, the width is calculated too short.
101 const int checkWidth = GetSystemMetrics(SM_CXMENUCHECK) + 2 * GetSystemMetrics(SM_CXEDGE) + 3;
102 controlrectorig.right = controlrectorig.left + (controlrect.right - controlrect.left) + checkWidth;
103 pwndDlgItem->MoveWindow(&controlrectorig);
106 pDC->SelectObject(pOldFont);
107 ReleaseDC(pDC);
109 return;
113 * Utility method:
114 * Store the current value of a BOOL, DWORD or CString into the
115 * respective CRegDWORD etc. and check for success.
118 template<class T, class Reg>
119 void Store (const T& value, Reg& registryKey)
121 registryKey = value;
122 if (registryKey.GetLastError() != ERROR_SUCCESS)
123 CMessageBox::Show (m_hWnd, registryKey.getErrorString(), _T("TortoiseGit"), MB_ICONERROR);