Skip to content

Commit

Permalink
Allow to change file encoding
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
csware committed Oct 8, 2015
1 parent da98062 commit e1bf451
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/Resources/TortoiseMergeENG.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
8 changes: 5 additions & 3 deletions src/TortoiseMerge/FileTextLines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/TortoiseMerge/FileTextLines.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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
Expand Down Expand Up @@ -154,6 +154,8 @@ class CFileTextLines : public CStdFileLineArray
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);
Expand All @@ -177,6 +179,7 @@ class CFileTextLines : public CStdFileLineArray
private:
CString m_sErrorString;
bool m_bNeedsConversion;
bool m_bKeepEncoding;
SaveParams m_SaveParams;
CString m_sCommentLine;
CString m_sCommentBlockStart;
Expand Down
98 changes: 89 additions & 9 deletions src/TortoiseMerge/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ static UINT indicators[] =
ID_INDICATOR_BOTTOMVIEW,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL
};


Expand Down Expand Up @@ -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"");
Expand All @@ -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"");
Expand Down Expand Up @@ -3477,30 +3481,106 @@ 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();
}
}
}

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();
}
}
}

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();
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/TortoiseMerge/MainFrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class CMainFrame : public CFrameWndEx, public CPatchFilesDlgCallBack //CFrameWnd
static bool HasPrevInlineDiff(CBaseView* view);
static bool HasNextInlineDiff(CBaseView* view);
void BuildRegexSubitems(CMFCPopupMenu* pMenuPopup = nullptr);
bool AdjustUnicodeTypeForLoad(CFileTextLines::UnicodeType& type);

protected:
CMFCStatusBar m_wndStatusBar;
Expand Down
3 changes: 2 additions & 1 deletion src/TortoiseMerge/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1bf451

Please sign in to comment.