OSDN Git Service

Allow NUL and \\.\NUL in paths specified as command line arguments (#2056)
[winmerge-jp/winmerge-jp.git] / Src / DirCmpReportDlg.cpp
1 /** 
2  * @file  DirCmpReportDlg.cpp
3  *
4  * @brief Implementation file for DirCmpReport dialog
5  *
6  */
7
8 #include "stdafx.h"
9 #include "DirCmpReportDlg.h"
10 #include "Coretools.h"
11 #include "DirReportTypes.h"
12 #include "paths.h"
13 #include "FileOrFolderSelect.h"
14 #include "OptionsMgr.h"
15 #include "OptionsDef.h"
16
17 IMPLEMENT_DYNAMIC(DirCmpReportDlg, CTrDialog)
18
19 /**
20  * @brief Constructor.
21  */
22 DirCmpReportDlg::DirCmpReportDlg(CWnd* pParent /*= nullptr*/)
23         : CTrDialog(DirCmpReportDlg::IDD, pParent)
24         , m_bCopyToClipboard(false)
25         , m_bIncludeFileCmpReport(false)
26         , m_nReportType(REPORT_TYPE_COMMALIST)
27 {
28 }
29
30 /**
31  * @brief Map dialog controls to member variables.
32  * This function maps dialog controls with member variables so
33  * when UpdateData() is called controls and member variables
34  * get updated.
35  */
36 void DirCmpReportDlg::DoDataExchange(CDataExchange* pDX)
37 {
38         CTrDialog::DoDataExchange(pDX);
39         DDX_Control(pDX, IDC_REPORT_FILE, m_ctlReportFile);
40         DDX_Control(pDX, IDC_REPORT_STYLECOMBO, m_ctlStyle);
41         DDX_Text(pDX, IDC_REPORT_FILE, m_sReportFile);
42         DDX_Check(pDX, IDC_REPORT_COPYCLIPBOARD, m_bCopyToClipboard);
43         DDX_Check(pDX, IDC_REPORT_INCLUDEFILECMPREPORT, m_bIncludeFileCmpReport);
44 }
45
46 BEGIN_MESSAGE_MAP(DirCmpReportDlg, CTrDialog)
47         ON_BN_CLICKED(IDC_REPORT_BROWSEFILE, OnBtnClickReportBrowse)
48         ON_BN_DOUBLECLICKED(IDC_REPORT_COPYCLIPBOARD, OnBtnDblclickCopyClipboard)
49         ON_CBN_SELCHANGE(IDC_REPORT_STYLECOMBO, OnCbnSelchangeReportStylecombo)
50 END_MESSAGE_MAP()
51
52 /**
53  * @brief Definition for structure containing report types.
54  * This struct is used to form a report types list. This list
55  * can be then used to initialize the GUI for reports.
56  */
57 struct ReportTypeInfo
58 {
59         REPORT_TYPE reportType; /**< Report-type ID */
60         const char *idDisplay; /**< Resource-string ID (shown in file-selection dialog) */
61         const char *browseFilter; /**< File-extension filter (resource-string ID) */
62 };
63
64 /**
65  * @brief List of report types.
66  * This list is used to initialize the GUI.
67  */
68 static ReportTypeInfo f_types[] = {
69         { REPORT_TYPE_COMMALIST,
70                 "Comma-separated list",
71                 "Text Files (*.csv;*.asc;*.rpt;*.txt)|*.csv;*.asc;*.rpt;*.txt|All Files (*.*)|*.*||"
72         },
73         { REPORT_TYPE_TABLIST,
74                 "Tab-separated list",
75                 "Text Files (*.csv;*.asc;*.rpt;*.txt)|*.csv;*.asc;*.rpt;*.txt|All Files (*.*)|*.*||"
76         },
77         { REPORT_TYPE_SIMPLEHTML,
78                 "Simple HTML",
79                 "HTML Files (*.htm,*.html)|*.htm;*.html|All Files (*.*)|*.*||"
80         },
81         { REPORT_TYPE_SIMPLEXML,
82                 "Simple XML",
83                 "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||"
84         },
85 };
86
87 void DirCmpReportDlg::LoadSettings()
88 {
89         m_nReportType = static_cast<REPORT_TYPE>(GetOptionsMgr()->GetInt(OPT_REPORTFILES_REPORTTYPE));
90         m_bCopyToClipboard = GetOptionsMgr()->GetBool(OPT_REPORTFILES_COPYTOCLIPBOARD);
91         m_bIncludeFileCmpReport = GetOptionsMgr()->GetBool(OPT_REPORTFILES_INCLUDEFILECMPREPORT);
92 }
93
94 /**
95  * @brief Dialog initializer function.
96  */
97 BOOL DirCmpReportDlg::OnInitDialog()
98 {
99         CTrDialog::OnInitDialog();
100
101         LoadSettings();
102
103         m_ctlReportFile.LoadState(_T("ReportFiles"));
104
105         for (int i = 0; i < sizeof(f_types) / sizeof(f_types[0]); ++i)
106         {
107                 const ReportTypeInfo & info = f_types[i];
108                 int ind = m_ctlStyle.InsertString(i, tr(info.idDisplay).c_str());
109                 m_ctlStyle.SetItemData(ind, info.reportType);
110                 if (info.reportType == m_nReportType)
111                         m_ctlStyle.SetCurSel(m_nReportType);
112         }
113         if (m_ctlStyle.GetCurSel() < 0)
114                 m_ctlStyle.SetCurSel(0);
115
116         OnCbnSelchangeReportStylecombo();
117
118         // Set selected path to variable so file selection dialog shows
119         // correct filename and path.
120         CString cstrReportFile;
121         m_ctlReportFile.GetWindowText(cstrReportFile);
122         m_sReportFile = cstrReportFile;
123
124         UpdateData(FALSE);
125
126         return TRUE;  // return TRUE unless you set the focus to a control
127                       // EXCEPTION: OCX Property Pages should return FALSE
128 }
129
130 /**
131  * @brief Browse for report file.
132  */
133 void DirCmpReportDlg::OnBtnClickReportBrowse()
134 {
135         UpdateData(TRUE);
136
137         String folder = m_sReportFile;
138         String filter = tr(f_types[m_ctlStyle.GetCurSel()].browseFilter);
139
140         String chosenFilepath;
141         if (SelectFile(GetSafeHwnd(), chosenFilepath, false, folder.c_str(), _T(""), filter))
142         {
143                 m_sReportFile = chosenFilepath;
144                 m_ctlReportFile.SetWindowText(chosenFilepath.c_str());
145         }
146 }
147
148 /**
149  * @brief Erase report file name.
150  */
151 void DirCmpReportDlg::OnBtnDblclickCopyClipboard()
152 {
153         m_ctlReportFile.SetWindowText(_T(""));
154 }
155
156 void DirCmpReportDlg::OnCbnSelchangeReportStylecombo()
157 {
158         EnableDlgItem(IDC_REPORT_INCLUDEFILECMPREPORT,
159                 m_ctlStyle.GetItemData(m_ctlStyle.GetCurSel()) == REPORT_TYPE_SIMPLEHTML);
160 }
161
162 /**
163  * @brief Close dialog.
164  */
165 void DirCmpReportDlg::OnOK()
166 {
167         UpdateData(TRUE);
168
169         int sel = m_ctlStyle.GetCurSel();
170         m_nReportType = (REPORT_TYPE)m_ctlStyle.GetItemData(sel);
171
172         if (m_sReportFile.empty() && !m_bCopyToClipboard)
173         {
174                 LangMessageBox(IDS_MUST_SPECIFY_OUTPUT, MB_ICONSTOP);
175                 m_ctlReportFile.SetFocus();
176                 return;
177         }
178
179         if (!m_sReportFile.empty())
180         {
181                 if (paths::DoesPathExist(m_sReportFile) == paths::IS_EXISTING_FILE)
182                 {
183                         int overWrite = LangMessageBox(IDS_REPORT_FILEOVERWRITE,
184                                         MB_YESNO | MB_ICONWARNING | MB_DONT_ASK_AGAIN,
185                                         IDS_REPORT_FILEOVERWRITE);
186                         if (overWrite == IDNO)
187                                 return;
188                 }
189         }
190
191         m_ctlReportFile.SaveState(_T("ReportFiles"));
192         GetOptionsMgr()->SaveOption(OPT_REPORTFILES_REPORTTYPE, static_cast<int>(m_nReportType));
193         GetOptionsMgr()->SaveOption(OPT_REPORTFILES_COPYTOCLIPBOARD, m_bCopyToClipboard);
194         GetOptionsMgr()->SaveOption(OPT_REPORTFILES_INCLUDEFILECMPREPORT, m_bIncludeFileCmpReport);
195
196         CTrDialog::OnOK();
197 }