1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (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
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /////////////////////////////////////////////////////////////////////////////
22 * @file PropCodepage.h
24 * @brief Implementation file for PropCodepage propertyheet
27 // ID line follows -- this is updated by SVN
28 // $Id: PropCodepage.cpp 6908 2009-07-11 08:29:49Z kimmov $
31 #include "PropCodepage.h"
32 #include "common/unicoder.h"
33 #include "common/ExConverter.h"
35 #include "OptionsDef.h"
36 #include "OptionsMgr.h"
37 #include "OptionsPanel.h"
39 #include "DDXHelper.h"
44 static char THIS_FILE[] = __FILE__;
47 PropCodepage::PropCodepage(COptionsMgr *optionsMgr)
48 : OptionsPanel(optionsMgr, PropCodepage::IDD)
49 , m_nCodepageSystem(-1)
50 , m_nCustomCodepageValue(0)
51 , m_bDetectCodepage(false)
52 , m_bDetectCodepage2(false)
53 , m_nAutodetectType(50001)
57 void PropCodepage::DoDataExchange(CDataExchange* pDX)
59 CPropertyPage::DoDataExchange(pDX);
60 //{{AFX_DATA_MAP(PropCodepage)
61 DDX_Radio(pDX, IDC_CP_SYSTEM, m_nCodepageSystem);
62 DDX_Text(pDX, IDC_CUSTOM_CP_NUMBER, m_cCustomCodepageValue);
63 DDX_Check(pDX, IDC_DETECT_CODEPAGE, m_bDetectCodepage);
64 DDX_Check(pDX, IDC_DETECT_CODEPAGE2, m_bDetectCodepage2);
65 DDX_Control(pDX, IDC_CUSTOM_CP_NUMBER, m_comboCustomCodepageValue);
66 DDX_Control(pDX, IDC_DETECT_AUTODETECTTYPE, m_comboAutodetectType);
71 BEGIN_MESSAGE_MAP(PropCodepage, CPropertyPage)
72 //{{AFX_MSG_MAP(PropCodepage)
73 ON_BN_CLICKED(IDC_CP_SYSTEM, OnCpSystem)
74 ON_BN_CLICKED(IDC_CP_CUSTOM, OnCpCustom)
75 ON_BN_CLICKED(IDC_DETECT_CODEPAGE2, OnDetectCodepage2)
76 ON_CBN_SELCHANGE(IDC_DETECT_AUTODETECTTYPE, OnDetectAutodetecttype)
77 ON_BN_CLICKED(IDC_CP_UI, OnCpUi)
82 * @brief Reads options values from storage to UI.
84 void PropCodepage::ReadOptions()
86 m_nCodepageSystem = GetOptionsMgr()->GetInt(OPT_CP_DEFAULT_MODE);
87 m_nCustomCodepageValue = GetOptionsMgr()->GetInt(OPT_CP_DEFAULT_CUSTOM);
88 m_cCustomCodepageValue = string_to_str(m_nCustomCodepageValue);
89 m_bDetectCodepage = GetOptionsMgr()->GetInt(OPT_CP_DETECT) & 1;
90 m_bDetectCodepage2 = (GetOptionsMgr()->GetInt(OPT_CP_DETECT) & 2) != 0;
91 m_nAutodetectType = ((unsigned)GetOptionsMgr()->GetInt(OPT_CP_DETECT) >> 16);
92 if (m_nAutodetectType == 0)
93 m_nAutodetectType = 50001;
97 * @brief Writes options values from UI to storage.
99 void PropCodepage::WriteOptions()
101 GetOptionsMgr()->SaveOption(OPT_CP_DEFAULT_MODE, (int)m_nCodepageSystem);
102 GetEncodingCodePageFromNameString();
103 GetOptionsMgr()->SaveOption(OPT_CP_DEFAULT_CUSTOM, (int)m_nCustomCodepageValue);
104 GetOptionsMgr()->SaveOption(OPT_CP_DETECT, (m_bDetectCodepage ? 1 : 0) | (m_bDetectCodepage2 << 1) | (m_nAutodetectType << 16));
107 BOOL PropCodepage::OnInitDialog()
109 theApp.TranslateDialog(m_hWnd);
110 CPropertyPage::OnInitDialog();
112 CEdit * pEdit = (CEdit *) GetDlgItem(IDC_CUSTOM_CP_NUMBER);
114 // Enable/disable "Custom codepage" edit field
115 if (IsDlgButtonChecked(IDC_CP_CUSTOM))
116 pEdit->EnableWindow(TRUE);
118 pEdit->EnableWindow(FALSE);
120 if (IsDlgButtonChecked(IDC_DETECT_CODEPAGE2))
121 m_comboAutodetectType.EnableWindow(TRUE);
123 m_comboAutodetectType.EnableWindow(FALSE);
125 m_comboCustomCodepageValue.SetWindowText(string_to_str(m_nCustomCodepageValue).c_str());
127 IExconverter *pexconv = Exconverter::getInstance();
130 CodePageInfo cpi[256];
131 int count = pexconv->enumCodePages(cpi, sizeof(cpi)/sizeof(CodePageInfo));
133 for (i = 0, j = 0; i < count; i++)
135 if (cpi[i].codepage == 1200 /* UNICODE */)
138 wsprintf(desc, _T("%05d - %ls"), cpi[i].codepage, cpi[i].desc);
139 m_comboCustomCodepageValue.AddString(desc);
140 m_comboCustomCodepageValue.SetItemData(j, cpi[i].codepage);
141 if (cpi[i].codepage == m_nCustomCodepageValue)
142 m_comboCustomCodepageValue.SetCurSel(j);
146 static int autodetectTypeList[] = {50001, 50936, 50950, 50932, 50949};
147 for (i = 0; i < sizeof(autodetectTypeList)/sizeof(int); i++)
150 pexconv->getCodepageDescription(autodetectTypeList[i], desc);
151 m_comboAutodetectType.AddString(desc.c_str());
152 m_comboAutodetectType.SetItemData(i, autodetectTypeList[i]);
153 if (m_nAutodetectType == autodetectTypeList[i])
154 m_comboAutodetectType.SetCurSel(i);
158 return TRUE; // return TRUE unless you set the focus to a control
159 // EXCEPTION: OCX Property Pages should return FALSE
162 void PropCodepage::OnCpSystem()
164 GetDlgItem(IDC_CUSTOM_CP_NUMBER)->EnableWindow(FALSE);
167 void PropCodepage::OnCpCustom()
169 GetDlgItem(IDC_CUSTOM_CP_NUMBER)->EnableWindow(TRUE);
172 void PropCodepage::OnDetectCodepage2()
174 if (IsDlgButtonChecked(IDC_DETECT_CODEPAGE2))
175 m_comboAutodetectType.EnableWindow(TRUE);
177 m_comboAutodetectType.EnableWindow(FALSE);
180 void PropCodepage::OnDetectAutodetecttype()
182 m_nAutodetectType = static_cast<int>(m_comboAutodetectType.GetItemData(m_comboAutodetectType.GetCurSel()));
185 void PropCodepage::OnCpUi()
187 GetDlgItem(IDC_CUSTOM_CP_NUMBER)->EnableWindow(FALSE);
190 void PropCodepage::GetEncodingCodePageFromNameString()
192 int nCustomCodepageValue = _ttol(m_cCustomCodepageValue.c_str());
193 if (nCustomCodepageValue == 0)
194 nCustomCodepageValue = GetEncodingCodePageFromName(ucr::toSystemCP(m_cCustomCodepageValue).c_str());
195 //if found a new codepage valid
196 if (nCustomCodepageValue)
197 m_nCustomCodepageValue = nCustomCodepageValue;