OSDN Git Service

Merge with stable
[winmerge-jp/winmerge-jp.git] / Src / PropCodepage.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //    WinMerge:  an interactive diff/merge utility
3 //    Copyright (C) 1997-2000  Thingamahoochie Software
4 //    Author: Dean Grimm
5 //
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.
10 //
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.
15 //
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.
19 //
20 /////////////////////////////////////////////////////////////////////////////
21 /** 
22  * @file  PropCodepage.h
23  *
24  * @brief Implementation file for PropCodepage propertyheet
25  *
26  */
27 // ID line follows -- this is updated by SVN
28 // $Id: PropCodepage.cpp 6908 2009-07-11 08:29:49Z kimmov $
29
30 #include "stdafx.h"
31 #include "PropCodepage.h"
32 #include "common/unicoder.h"
33 #include "common/ExConverter.h"
34 #include "Merge.h"
35 #include "OptionsDef.h"
36 #include "OptionsMgr.h"
37 #include "OptionsPanel.h"
38 #include "charsets.h"
39 #include "DDXHelper.h"
40
41 #ifdef _DEBUG
42 #define new DEBUG_NEW
43 #undef THIS_FILE
44 static char THIS_FILE[] = __FILE__;
45 #endif
46
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)
54 {
55 }
56
57 void PropCodepage::DoDataExchange(CDataExchange* pDX)
58 {
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);
67         //}}AFX_DATA_MAP
68 }
69
70
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)
78         //}}AFX_MSG_MAP
79 END_MESSAGE_MAP()
80
81 /** 
82  * @brief Reads options values from storage to UI.
83  */
84 void PropCodepage::ReadOptions()
85 {
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;
94 }
95
96 /** 
97  * @brief Writes options values from UI to storage.
98  */
99 void PropCodepage::WriteOptions()
100 {
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));
105 }
106
107 BOOL PropCodepage::OnInitDialog() 
108 {
109         theApp.TranslateDialog(m_hWnd);
110         CPropertyPage::OnInitDialog();
111         
112         CEdit * pEdit = (CEdit *) GetDlgItem(IDC_CUSTOM_CP_NUMBER);
113
114         // Enable/disable "Custom codepage" edit field
115         if (IsDlgButtonChecked(IDC_CP_CUSTOM))
116                 pEdit->EnableWindow(TRUE);
117         else
118                 pEdit->EnableWindow(FALSE);
119
120         if (IsDlgButtonChecked(IDC_DETECT_CODEPAGE2))
121                 m_comboAutodetectType.EnableWindow(TRUE);
122         else
123                 m_comboAutodetectType.EnableWindow(FALSE);
124
125         m_comboCustomCodepageValue.SetWindowText(string_to_str(m_nCustomCodepageValue).c_str());
126
127         IExconverter *pexconv = Exconverter::getInstance();
128         if (pexconv != NULL)
129         {
130                 CodePageInfo cpi[256];
131                 int count = pexconv->enumCodePages(cpi, sizeof(cpi)/sizeof(CodePageInfo));
132                 int i, j;
133                 for (i = 0, j = 0; i < count; i++)
134                 {
135                         if (cpi[i].codepage == 1200 /* UNICODE */)
136                                 continue;
137                         TCHAR desc[256];
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);
143                         j++;
144                 }
145
146                 static int autodetectTypeList[] = {50001, 50936, 50950, 50932, 50949};
147                 for (i = 0; i < sizeof(autodetectTypeList)/sizeof(int); i++)
148                 {
149                         String desc;
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);
155                 }
156         }
157
158         return TRUE;  // return TRUE unless you set the focus to a control
159                       // EXCEPTION: OCX Property Pages should return FALSE
160 }
161
162 void PropCodepage::OnCpSystem() 
163 {
164         GetDlgItem(IDC_CUSTOM_CP_NUMBER)->EnableWindow(FALSE);  
165 }
166
167 void PropCodepage::OnCpCustom() 
168 {
169         GetDlgItem(IDC_CUSTOM_CP_NUMBER)->EnableWindow(TRUE);   
170 }
171
172 void PropCodepage::OnDetectCodepage2() 
173 {
174         if (IsDlgButtonChecked(IDC_DETECT_CODEPAGE2))
175                 m_comboAutodetectType.EnableWindow(TRUE);
176         else
177                 m_comboAutodetectType.EnableWindow(FALSE);
178 }
179
180 void PropCodepage::OnDetectAutodetecttype()
181 {
182         m_nAutodetectType = static_cast<int>(m_comboAutodetectType.GetItemData(m_comboAutodetectType.GetCurSel()));     
183 }
184
185 void PropCodepage::OnCpUi() 
186 {
187         GetDlgItem(IDC_CUSTOM_CP_NUMBER)->EnableWindow(FALSE);  
188 }
189
190 void PropCodepage::GetEncodingCodePageFromNameString()
191 {
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;
198 }