Fixed issue #3770: TortoiseGitMerge can't apply NonANSI (e.g., UTF-8) patch
[TortoiseGit.git] / src / TortoiseMerge / SetMainPage.cpp
blob3175e36d33a517216958e87a1ec12b9b0f7246e0
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2013-2014, 2019 - TortoiseGit
4 // Copyright (C) 2006-2010, 2012-2014, 2016, 2018, 2020 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseMerge.h"
22 #include "DirFileEnum.h"
23 #include "AppUtils.h"
24 #include "PathUtils.h"
25 #include "SetMainPage.h"
26 #include "MainFrm.h"
27 #include "Theme.h"
29 // CSetMainPage dialog
31 IMPLEMENT_DYNAMIC(CSetMainPage, CPropertyPage)
32 CSetMainPage::CSetMainPage()
33 : CPropertyPage(CSetMainPage::IDD)
34 , m_bBackup(FALSE)
35 , m_bFirstDiffOnLoad(FALSE)
36 , m_bFirstConflictOnLoad(FALSE)
37 , m_bUseSpaces(FALSE)
38 , m_bSmartTabChar(FALSE)
39 , m_nTabSize(0)
40 , m_bEnableEditorConfig(FALSE)
41 , m_nContextLines(-1)
42 , m_bIgnoreEOL(FALSE)
43 , m_bOnePane(FALSE)
44 , m_bViewLinenumbers(FALSE)
45 , m_bReloadNeeded(FALSE)
46 , m_bCaseInsensitive(FALSE)
47 , m_bUTF8Default(FALSE)
48 , m_bAutoAdd(TRUE)
49 , m_nMaxInline(3000)
50 , m_dwFontSize(0)
51 , m_themeCallbackId(0)
53 m_regBackup = CRegDWORD(L"Software\\TortoiseGitMerge\\Backup");
54 m_regFirstDiffOnLoad = CRegDWORD(L"Software\\TortoiseGitMerge\\FirstDiffOnLoad", TRUE);
55 m_regFirstConflictOnLoad = CRegDWORD(L"Software\\TortoiseGitMerge\\FirstConflictOnLoad", TRUE);
56 m_regTabMode = CRegDWORD(L"Software\\TortoiseGitMerge\\TabMode", 0);
57 m_regTabSize = CRegDWORD(L"Software\\TortoiseGitMerge\\TabSize", 4);
58 m_regEnableEditorConfig = CRegDWORD(L"Software\\TortoiseGitMerge\\EnableEditorConfig", FALSE);
59 m_regIgnoreEOL = CRegDWORD(L"Software\\TortoiseGitMerge\\IgnoreEOL", TRUE);
60 m_regOnePane = CRegDWORD(L"Software\\TortoiseGitMerge\\OnePane");
61 m_regViewLinenumbers = CRegDWORD(L"Software\\TortoiseGitMerge\\ViewLinenumbers", 1);
62 m_regFontName = CRegString(L"Software\\TortoiseGitMerge\\LogFontName", L"Consolas");
63 m_regFontSize = CRegDWORD(L"Software\\TortoiseGitMerge\\LogFontSize", 10);
64 m_regCaseInsensitive = CRegDWORD(L"Software\\TortoiseGitMerge\\CaseInsensitive", FALSE);
65 m_regUTF8Default = CRegDWORD(L"Software\\TortoiseGitMerge\\UseUTF8", FALSE);
66 m_regAutoAdd = CRegDWORD(L"Software\\TortoiseGitMerge\\AutoAdd", TRUE);
67 m_regMaxInline = CRegDWORD(L"Software\\TortoiseGitMerge\\InlineDiffMaxLineLength", 3000);
68 m_regUseRibbons = CRegDWORD(L"Software\\TortoiseGitMerge\\UseRibbons", TRUE);
69 m_regContextLines = CRegDWORD(L"Software\\TortoiseGitMerge\\ContextLines", static_cast<DWORD>(-1));
71 m_bBackup = m_regBackup;
72 m_bFirstDiffOnLoad = m_regFirstDiffOnLoad;
73 m_bFirstConflictOnLoad = m_regFirstConflictOnLoad;
74 m_bUseSpaces = (m_regTabMode & TABMODE_USESPACES) ? TRUE : FALSE;
75 m_bSmartTabChar = (m_regTabMode & TABMODE_SMARTINDENT) ? TRUE : FALSE;
76 m_nTabSize = m_regTabSize;
77 m_bEnableEditorConfig = m_regEnableEditorConfig;
78 m_nContextLines = m_regContextLines;
79 m_bIgnoreEOL = m_regIgnoreEOL;
80 m_bOnePane = m_regOnePane;
81 m_bViewLinenumbers = m_regViewLinenumbers;
82 m_bCaseInsensitive = m_regCaseInsensitive;
83 m_bUTF8Default = m_regUTF8Default;
84 m_bAutoAdd = m_regAutoAdd;
85 m_nMaxInline = m_regMaxInline;
86 m_bUseRibbons = m_regUseRibbons;
89 CSetMainPage::~CSetMainPage()
91 CTheme::Instance().RemoveRegisteredCallback(m_themeCallbackId);
94 void CSetMainPage::DoDataExchange(CDataExchange* pDX)
96 CPropertyPage::DoDataExchange(pDX);
97 DDX_Check(pDX, IDC_BACKUP, m_bBackup);
98 DDX_Check(pDX, IDC_FIRSTDIFFONLOAD, m_bFirstDiffOnLoad);
99 DDX_Check(pDX, IDC_FIRSTCONFLICTONLOAD, m_bFirstConflictOnLoad);
100 DDX_Check(pDX, IDC_USESPACES, m_bUseSpaces);
101 DDX_Check(pDX, IDC_SMARTTABCHAR, m_bSmartTabChar);
102 DDX_Text(pDX, IDC_TABSIZE, m_nTabSize);
103 DDV_MinMaxInt(pDX, m_nTabSize, 1, 1000);
104 DDX_Check(pDX, IDC_ENABLEEDITORCONFIG, m_bEnableEditorConfig);
105 DDX_Text(pDX, IDC_CONTEXTLINES, m_nContextLines);
106 DDV_MinMaxInt(pDX, m_nContextLines, -1, 1000);
107 DDX_Check(pDX, IDC_IGNORELF, m_bIgnoreEOL);
108 DDX_Check(pDX, IDC_ONEPANE, m_bOnePane);
109 DDX_Control(pDX, IDC_FONTSIZES, m_cFontSizes);
110 m_dwFontSize = static_cast<DWORD>(m_cFontSizes.GetItemData(m_cFontSizes.GetCurSel()));
111 if ((m_dwFontSize==0)||(m_dwFontSize == -1))
113 CString t;
114 m_cFontSizes.GetWindowText(t);
115 m_dwFontSize = _wtoi(t);
117 DDX_Control(pDX, IDC_FONTNAMES, m_cFontNames);
118 DDX_Check(pDX, IDC_LINENUMBERS, m_bViewLinenumbers);
119 DDX_Check(pDX, IDC_CASEINSENSITIVE, m_bCaseInsensitive);
120 DDX_Check(pDX, IDC_UTF8DEFAULT, m_bUTF8Default);
121 DDX_Check(pDX, IDC_AUTOADD, m_bAutoAdd);
122 DDX_Text(pDX, IDC_MAXINLINE, m_nMaxInline);
123 DDX_Check(pDX, IDC_USERIBBONS, m_bUseRibbons);
126 void CSetMainPage::SaveData()
128 m_regBackup = m_bBackup;
129 m_regFirstDiffOnLoad = m_bFirstDiffOnLoad;
130 m_regFirstConflictOnLoad = m_bFirstConflictOnLoad;
131 m_regTabMode = (m_bUseSpaces ? TABMODE_USESPACES : TABMODE_NONE) | (m_bSmartTabChar ? TABMODE_SMARTINDENT : TABMODE_NONE);
132 m_regTabSize = m_nTabSize;
133 m_regEnableEditorConfig = m_bEnableEditorConfig;
134 m_regContextLines = m_nContextLines;
135 m_regIgnoreEOL = m_bIgnoreEOL;
136 m_regOnePane = m_bOnePane;
137 m_regFontName = m_sFontName;
138 m_regFontSize = m_dwFontSize;
139 m_regViewLinenumbers = m_bViewLinenumbers;
140 m_regCaseInsensitive = m_bCaseInsensitive;
141 m_regUTF8Default = m_bUTF8Default;
142 m_regAutoAdd = m_bAutoAdd;
143 m_regMaxInline = m_nMaxInline;
144 m_regUseRibbons = m_bUseRibbons;
147 BOOL CSetMainPage::OnApply()
149 UpdateData();
150 if (m_cFontNames.GetSelFont())
151 m_sFontName = m_cFontNames.GetSelFont()->m_strName;
152 else
153 m_sFontName = m_regFontName;
154 SaveData();
155 SetModified(FALSE);
156 return CPropertyPage::OnApply();
159 BOOL CSetMainPage::OnInitDialog()
161 CMFCFontComboBox::m_bDrawUsingFont = true;
163 CPropertyPage::OnInitDialog();
165 m_bBackup = m_regBackup;
166 m_bFirstDiffOnLoad = m_regFirstDiffOnLoad;
167 m_bFirstConflictOnLoad = m_regFirstConflictOnLoad;
168 m_bUseSpaces = (m_regTabMode & TABMODE_USESPACES) ? TRUE : FALSE;
169 m_bSmartTabChar = (m_regTabMode & TABMODE_SMARTINDENT) ? TRUE : FALSE;
170 m_nTabSize = m_regTabSize;
171 m_bEnableEditorConfig = m_regEnableEditorConfig;
172 m_nContextLines = m_regContextLines;
173 m_bIgnoreEOL = m_regIgnoreEOL;
174 m_bOnePane = m_regOnePane;
175 m_sFontName = m_regFontName;
176 m_dwFontSize = m_regFontSize;
177 m_bViewLinenumbers = m_regViewLinenumbers;
178 m_bCaseInsensitive = m_regCaseInsensitive;
179 m_bUTF8Default = m_regUTF8Default;
180 m_bAutoAdd = m_regAutoAdd;
181 m_nMaxInline = m_regMaxInline;
182 m_bUseRibbons = m_regUseRibbons;
184 DialogEnableWindow(IDC_FIRSTCONFLICTONLOAD, m_bFirstDiffOnLoad);
186 CString temp;
187 int count = 0;
188 for (int i=6; i<32; i=i+2)
190 temp.Format(L"%d", i);
191 m_cFontSizes.AddString(temp);
192 m_cFontSizes.SetItemData(count++, i);
194 BOOL foundfont = FALSE;
195 for (int i=0; i<m_cFontSizes.GetCount(); i++)
197 if (m_cFontSizes.GetItemData(i) == m_dwFontSize)
199 m_cFontSizes.SetCurSel(i);
200 foundfont = TRUE;
203 if (!foundfont)
205 temp.Format(L"%lu", m_dwFontSize);
206 m_cFontSizes.SetWindowText(temp);
208 m_cFontNames.Setup(DEVICE_FONTTYPE|RASTER_FONTTYPE|TRUETYPE_FONTTYPE, 1, FIXED_PITCH);
209 m_cFontNames.SelectFont(m_sFontName);
211 m_cFontNames.SendMessage(CB_SETITEMHEIGHT, static_cast<WPARAM>(-1), m_cFontSizes.GetItemHeight(-1));
213 m_themeCallbackId = CTheme::Instance().RegisterThemeChangeCallback([this]() { CTheme::Instance().SetThemeForDialog(GetSafeHwnd(), CTheme::Instance().IsDarkTheme()); });
214 CTheme::Instance().SetThemeForDialog(GetSafeHwnd(), CTheme::Instance().IsDarkTheme());
216 UpdateData(FALSE);
217 return TRUE; // return TRUE unless you set the focus to a control
218 // EXCEPTION: OCX Property Pages should return FALSE
221 BEGIN_MESSAGE_MAP(CSetMainPage, CPropertyPage)
222 ON_BN_CLICKED(IDC_BACKUP, &CSetMainPage::OnModified)
223 ON_BN_CLICKED(IDC_IGNORELF, &CSetMainPage::OnModifiedWithReload)
224 ON_BN_CLICKED(IDC_ONEPANE, &CSetMainPage::OnModified)
225 ON_BN_CLICKED(IDC_FIRSTDIFFONLOAD, &CSetMainPage::OnModified)
226 ON_BN_CLICKED(IDC_FIRSTCONFLICTONLOAD, &CSetMainPage::OnModified)
227 ON_BN_CLICKED(IDC_LINENUMBERS, &CSetMainPage::OnModified)
228 ON_BN_CLICKED(IDC_USESPACES, &CSetMainPage::OnModified)
229 ON_BN_CLICKED(IDC_SMARTTABCHAR, &CSetMainPage::OnModified)
230 ON_EN_CHANGE(IDC_TABSIZE, &CSetMainPage::OnModified)
231 ON_BN_CLICKED(IDC_ENABLEEDITORCONFIG, &CSetMainPage::OnModified)
232 ON_EN_CHANGE(IDC_CONTEXTLINES, &CSetMainPage::OnModified)
233 ON_CBN_SELCHANGE(IDC_FONTSIZES, &CSetMainPage::OnModifiedWithReload)
234 ON_CBN_SELCHANGE(IDC_FONTNAMES, &CSetMainPage::OnModifiedWithReload)
235 ON_BN_CLICKED(IDC_CASEINSENSITIVE, &CSetMainPage::OnModified)
236 ON_BN_CLICKED(IDC_UTF8DEFAULT, &CSetMainPage::OnModified)
237 ON_BN_CLICKED(IDC_AUTOADD, &CSetMainPage::OnModified)
238 ON_EN_CHANGE(IDC_MAXINLINE, &CSetMainPage::OnModifiedWithReload)
239 ON_BN_CLICKED(IDC_USERIBBONS, &CSetMainPage::OnModified)
240 ON_WM_MEASUREITEM()
241 END_MESSAGE_MAP()
244 // CSetMainPage message handlers
246 void CSetMainPage::OnModified()
248 UpdateData();
249 SetModified();
250 DialogEnableWindow(IDC_FIRSTCONFLICTONLOAD, m_bFirstDiffOnLoad);
253 void CSetMainPage::OnModifiedWithReload()
255 m_bReloadNeeded = TRUE;
256 SetModified();
259 BOOL CSetMainPage::DialogEnableWindow(UINT nID, BOOL bEnable)
261 CWnd * pwndDlgItem = GetDlgItem(nID);
262 if (!pwndDlgItem)
263 return FALSE;
264 if (bEnable)
265 return pwndDlgItem->EnableWindow(bEnable);
266 if (GetFocus() == pwndDlgItem)
268 SendMessage(WM_NEXTDLGCTL, 0, FALSE);
270 return pwndDlgItem->EnableWindow(bEnable);
273 void CSetMainPage::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
275 CFont* pFont = GetFont();
276 if (pFont)
278 CDC* pDC = GetDC();
279 CFont* pFontPrev = pDC->SelectObject(pFont);
280 int iborder = ::GetSystemMetrics(SM_CYBORDER);
281 CSize sz = pDC->GetTextExtent(L"0");
282 lpMeasureItemStruct->itemHeight = sz.cy + 2 * iborder;
283 pDC->SelectObject(pFontPrev);
284 ReleaseDC(pDC);
286 CPropertyPage::OnMeasureItem(nIDCtl, lpMeasureItemStruct);