Commit e1bf4514 authored by Sven Strickroth's avatar Sven Strickroth

Allow to change file encoding

Use the encoding comboboxes at the bottom right to reload the file with the specified encoding if the ctrl key is pressed.
The behavior is left as is if the ctrl key is not pressed, i.e. the shown file is converted to the specified encoding.

Based on TortoiseSVN revision #26882 and #26883.
Signed-off-by: Sven Strickroth's avatarSven Strickroth <email@cs-ware.de>
parent da98062c
......@@ -741,6 +741,12 @@ BEGIN
IDS_MOVED_TO_TT "Line moved to line %ld"
END
STRINGTABLE
BEGIN
IDS_ENCODING_COMBO_TOOLTIP
"Encoding\nConvert to the specified encoding\nHold down the Ctrl key to reload the file with the specified encoding."
END
STRINGTABLE
BEGIN
IDS_FIND_COUNT "Count: %u matches."
......
......@@ -52,6 +52,7 @@ UINT64 inline DwordSwapBytes(UINT64 nValue)
CFileTextLines::CFileTextLines(void)
: m_bNeedsConversion(false)
, m_bKeepEncoding(false)
{
m_SaveParams.m_UnicodeType = CFileTextLines::AUTOTYPE;
m_SaveParams.m_LineEndings = EOL_AUTOLINE;
......@@ -209,7 +210,8 @@ BOOL CFileTextLines::Load(const CString& sFilePath, int lengthHint /* = 0*/)
{
WCHAR exceptionError[1000] = {0};
m_SaveParams.m_LineEndings = EOL_AUTOLINE;
m_SaveParams.m_UnicodeType = CFileTextLines::AUTOTYPE;
if (!m_bKeepEncoding)
m_SaveParams.m_UnicodeType = CFileTextLines::AUTOTYPE;
RemoveAll();
if(lengthHint != 0)
{
......@@ -277,9 +279,9 @@ BOOL CFileTextLines::Load(const CString& sFilePath, int lengthHint /* = 0*/)
if (m_SaveParams.m_UnicodeType == CFileTextLines::AUTOTYPE)
{
m_SaveParams.m_UnicodeType = this->CheckUnicodeType((LPVOID)oFile, dwReadBytes);
// enforce conversion for all but ASCII and UTF8 type
m_bNeedsConversion = (m_SaveParams.m_UnicodeType!=CFileTextLines::UTF8)&&(m_SaveParams.m_UnicodeType!=CFileTextLines::ASCII);
}
// enforce conversion for all but ASCII and UTF8 type
m_bNeedsConversion = (m_SaveParams.m_UnicodeType != CFileTextLines::UTF8) && (m_SaveParams.m_UnicodeType != CFileTextLines::ASCII);
// we may have to convert the file content - CString is UTF16LE
try
......
// TortoiseGitMerge - a Diff/Patch program
// Copyright (C) 2006-2007, 2012-2014 - TortoiseSVN
// Copyright (C) 2006-2007, 2012-2015 - TortoiseSVN
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
......@@ -154,6 +154,8 @@ public:
const CString& GetAt(int index) const { return CStdFileLineArray::GetAt(index).sLine; }
EOL GetLineEnding(int index) const { return CStdFileLineArray::GetAt(index).eEnding; }
void SetSaveParams(const SaveParams& sp) { m_SaveParams = sp; }
SaveParams GetSaveParams() const { return m_SaveParams; }
void KeepEncoding(bool bKeep = true) { m_bKeepEncoding = bKeep; }
//void SetLineEnding(int index, EOL ending) { CStdFileLineArray::GetAt(index).eEnding = ending; }
static const wchar_t * GetEncodingName(UnicodeType);
......@@ -177,6 +179,7 @@ private:
private:
CString m_sErrorString;
bool m_bNeedsConversion;
bool m_bKeepEncoding;
SaveParams m_SaveParams;
CString m_sCommentLine;
CString m_sCommentBlockStart;
......
......@@ -185,6 +185,7 @@ static UINT indicators[] =
ID_INDICATOR_BOTTOMVIEW,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL
};
......@@ -296,10 +297,12 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
}
m_wndRibbonStatusBar.AddElement(new CMFCRibbonStatusBarPane(ID_SEPARATOR, CString(MAKEINTRESOURCE(AFX_IDS_IDLEMESSAGE)), TRUE), L"");
CString sTooltip(MAKEINTRESOURCE(IDS_ENCODING_COMBO_TOOLTIP));
std::unique_ptr<CMFCRibbonButtonsGroup> apBtnGroupLeft(new CMFCRibbonButtonsGroup);
apBtnGroupLeft->SetID(ID_INDICATOR_LEFTVIEW);
apBtnGroupLeft->AddButton(new CMFCRibbonStatusBarPane(ID_SEPARATOR, CString(MAKEINTRESOURCE(IDS_STATUSBAR_LEFTVIEW)), TRUE));
CMFCRibbonButton * pButton = new CMFCRibbonButton(ID_INDICATOR_LEFTVIEWCOMBOENCODING, L"");
pButton->SetToolTipText(sTooltip);
FillEncodingButton(pButton, ID_INDICATOR_LEFTENCODINGSTART);
apBtnGroupLeft->AddButton(pButton);
pButton = new CMFCRibbonButton(ID_INDICATOR_LEFTVIEWCOMBOEOL, L"");
......@@ -315,6 +318,7 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
apBtnGroupRight->SetID(ID_INDICATOR_RIGHTVIEW);
apBtnGroupRight->AddButton(new CMFCRibbonStatusBarPane(ID_SEPARATOR, CString(MAKEINTRESOURCE(IDS_STATUSBAR_RIGHTVIEW)), TRUE));
pButton = new CMFCRibbonButton(ID_INDICATOR_RIGHTVIEWCOMBOENCODING, L"");
pButton->SetToolTipText(sTooltip);
FillEncodingButton(pButton, ID_INDICATOR_RIGHTENCODINGSTART);
apBtnGroupRight->AddButton(pButton);
pButton = new CMFCRibbonButton(ID_INDICATOR_RIGHTVIEWCOMBOEOL, L"");
......@@ -3477,12 +3481,58 @@ void CMainFrame::FillTabModeButton(CMFCRibbonButton * pButton, int start)
pButton->AddSubItem(new CMFCRibbonButton(start + ENABLEEDITORCONFIG, L"EditorConfig"));
}
bool CMainFrame::AdjustUnicodeTypeForLoad(CFileTextLines::UnicodeType& type)
{
switch (type)
{
case CFileTextLines::UnicodeType::AUTOTYPE:
case CFileTextLines::UnicodeType::BINARY:
return false;
case CFileTextLines::UnicodeType::ASCII:
case CFileTextLines::UnicodeType::UTF16_LE:
case CFileTextLines::UnicodeType::UTF16_BE:
case CFileTextLines::UnicodeType::UTF32_LE:
case CFileTextLines::UnicodeType::UTF32_BE:
case CFileTextLines::UnicodeType::UTF8:
return true;
case CFileTextLines::UnicodeType::UTF16_LEBOM:
type = CFileTextLines::UnicodeType::UTF16_LE;
return true;
case CFileTextLines::UnicodeType::UTF16_BEBOM:
type = CFileTextLines::UnicodeType::UTF16_BE;
return true;
case CFileTextLines::UnicodeType::UTF8BOM:
type = CFileTextLines::UnicodeType::UTF8;
return true;
}
return false;
}
void CMainFrame::OnEncodingLeft( UINT cmd )
{
if (m_pwndLeftView)
{
m_pwndLeftView->SetTextType(CFileTextLines::UnicodeType(cmd-ID_INDICATOR_LEFTENCODINGSTART));
m_pwndLeftView->RefreshViews();
if (GetKeyState(VK_CONTROL) & 0x8000)
{
// reload with selected encoding
auto saveparams = m_Data.m_arBaseFile.GetSaveParams();
saveparams.m_UnicodeType = CFileTextLines::UnicodeType(cmd - ID_INDICATOR_LEFTENCODINGSTART);
if (AdjustUnicodeTypeForLoad(saveparams.m_UnicodeType))
{
m_Data.m_arBaseFile.SetSaveParams(saveparams);
m_Data.m_arBaseFile.KeepEncoding();
LoadViews();
}
}
else
{
m_pwndLeftView->SetTextType(CFileTextLines::UnicodeType(cmd - ID_INDICATOR_LEFTENCODINGSTART));
m_pwndLeftView->RefreshViews();
}
}
}
......@@ -3490,8 +3540,23 @@ void CMainFrame::OnEncodingRight( UINT cmd )
{
if (m_pwndRightView)
{
m_pwndRightView->SetTextType(CFileTextLines::UnicodeType(cmd-ID_INDICATOR_RIGHTENCODINGSTART));
m_pwndRightView->RefreshViews();
if (GetKeyState(VK_CONTROL) & 0x8000)
{
// reload with selected encoding
auto saveparams = m_Data.m_arYourFile.GetSaveParams();
saveparams.m_UnicodeType = CFileTextLines::UnicodeType(cmd - ID_INDICATOR_RIGHTENCODINGSTART);
if (AdjustUnicodeTypeForLoad(saveparams.m_UnicodeType))
{
m_Data.m_arYourFile.SetSaveParams(saveparams);
m_Data.m_arYourFile.KeepEncoding();
LoadViews();
}
}
else
{
m_pwndRightView->SetTextType(CFileTextLines::UnicodeType(cmd - ID_INDICATOR_RIGHTENCODINGSTART));
m_pwndRightView->RefreshViews();
}
}
}
......@@ -3499,8 +3564,23 @@ void CMainFrame::OnEncodingBottom( UINT cmd )
{
if (m_pwndBottomView)
{
m_pwndBottomView->SetTextType(CFileTextLines::UnicodeType(cmd-ID_INDICATOR_BOTTOMENCODINGSTART));
m_pwndBottomView->RefreshViews();
if (GetKeyState(VK_CONTROL) & 0x8000)
{
// reload with selected encoding
auto saveparams = m_Data.m_arTheirFile.GetSaveParams();
saveparams.m_UnicodeType = CFileTextLines::UnicodeType(cmd - ID_INDICATOR_BOTTOMENCODINGSTART);
if (AdjustUnicodeTypeForLoad(saveparams.m_UnicodeType))
{
m_Data.m_arTheirFile.SetSaveParams(saveparams);
m_Data.m_arTheirFile.KeepEncoding();
LoadViews();
}
}
else
{
m_pwndBottomView->SetTextType(CFileTextLines::UnicodeType(cmd - ID_INDICATOR_BOTTOMENCODINGSTART));
m_pwndBottomView->RefreshViews();
}
}
}
......@@ -3573,7 +3653,7 @@ void CMainFrame::OnUpdateEncodingLeft( CCmdUI *pCmdUI )
if (m_pwndLeftView)
{
pCmdUI->SetCheck(CFileTextLines::UnicodeType(pCmdUI->m_nID - ID_INDICATOR_LEFTENCODINGSTART) == m_pwndLeftView->GetTextType());
pCmdUI->Enable(m_pwndLeftView->IsWritable());
pCmdUI->Enable(m_pwndLeftView->IsWritable() || (GetKeyState(VK_CONTROL)&0x8000));
}
else
pCmdUI->Enable(FALSE);
......@@ -3584,7 +3664,7 @@ void CMainFrame::OnUpdateEncodingRight( CCmdUI *pCmdUI )
if (m_pwndRightView)
{
pCmdUI->SetCheck(CFileTextLines::UnicodeType(pCmdUI->m_nID - ID_INDICATOR_RIGHTENCODINGSTART) == m_pwndRightView->GetTextType());
pCmdUI->Enable(m_pwndRightView->IsWritable());
pCmdUI->Enable(m_pwndRightView->IsWritable() || (GetKeyState(VK_CONTROL) & 0x8000));
}
else
pCmdUI->Enable(FALSE);
......@@ -3595,7 +3675,7 @@ void CMainFrame::OnUpdateEncodingBottom( CCmdUI *pCmdUI )
if (m_pwndBottomView)
{
pCmdUI->SetCheck(CFileTextLines::UnicodeType(pCmdUI->m_nID - ID_INDICATOR_BOTTOMENCODINGSTART) == m_pwndBottomView->GetTextType());
pCmdUI->Enable(m_pwndBottomView->IsWritable());
pCmdUI->Enable(m_pwndBottomView->IsWritable() || (GetKeyState(VK_CONTROL) & 0x8000));
}
else
pCmdUI->Enable(FALSE);
......
......@@ -239,6 +239,7 @@ protected:
static bool HasPrevInlineDiff(CBaseView* view);
static bool HasNextInlineDiff(CBaseView* view);
void BuildRegexSubitems(CMFCPopupMenu* pMenuPopup = nullptr);
bool AdjustUnicodeTypeForLoad(CFileTextLines::UnicodeType& type);
protected:
CMFCStatusBar m_wndStatusBar;
......
......@@ -207,6 +207,7 @@
#define IDS_STATUSBAR_RIGHTVIEW 3004
#define IDS_STATUSBAR_CONFLICTS 3005
#define IDS_STATUSBAR_BOTTOMVIEW 3006
#define IDS_ENCODING_COMBO_TOOLTIP 3010
#define IDS_VIEWCONTEXTMENU_USETHISBLOCK 4000
#define IDS_VIEWCONTEXTMENU_USETHEIRBLOCK 4001
#define IDS_VIEWCONTEXTMENU_USEYOURBLOCK 4002
......@@ -375,7 +376,7 @@
#define ID_REGEXFILTER_REGEXFILTER 32993
// Next default values for new objects
//
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 171
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment