OSDN Git Service

Add Expand Different Subfolders menu item (#1964)
[winmerge-jp/winmerge-jp.git] / Src / WMGotoDlg.cpp
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3  * @file  WMGotoDlg.cpp
4  *
5  * @brief Implementation of the WMGotoDlg dialog.
6  */
7
8 #include "stdafx.h"
9 #include "WMGotoDlg.h"
10 #include "TrDialogs.h"
11
12 #ifdef _DEBUG
13 #define new DEBUG_NEW
14 #endif
15
16 /**
17  * @brief Class for Goto-dialog.
18  * This dialog allows user to go to certain line or or difference in the file
19  * compare. As there are two panels with different line numbers, there is a
20  * choice for target panel. When dialog is opened, its values are initialized
21  * for active file's line number.
22  */
23 class WMGotoDlg::Impl : public CTrDialog
24 {
25 // Construction
26 public:
27         explicit Impl(WMGotoDlg *p, CWnd* pParent = nullptr);   // standard constructor
28
29 // Dialog Data
30         //{{AFX_DATA(WMGotoDlg)
31         enum { IDD = IDD_WMGOTO };
32         //}}AFX_DATA
33
34 // Overrides
35         // ClassWizard generated virtual function overrides
36         //{{AFX_VIRTUAL(WMGotoDlg)
37         protected:
38         virtual BOOL OnInitDialog() override;
39         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
40         //}}AFX_VIRTUAL
41
42 // Implementation
43 protected:
44
45         // Generated message map functions
46         //{{AFX_MSG(WMGotoDlg)
47                 // NOTE: the ClassWizard will add member functions here
48         afx_msg void OnChangeParam();
49         afx_msg void OnBnClicked(UINT nID);
50         //}}AFX_MSG
51         DECLARE_MESSAGE_MAP()
52
53 private:
54         WMGotoDlg *m_p;
55         String m_strRange;                      /**< Acceptable range */
56
57         int GetRangeMax();
58         void UpdateRange();
59         void UpdateGoToButton();
60 };
61
62 /////////////////////////////////////////////////////////////////////////////
63 // CGotoDlg dialog
64
65 /**
66  * @brief Constructor.
67  */
68 WMGotoDlg::Impl::Impl(WMGotoDlg *p, CWnd* pParent /*= nullptr*/)
69         : CTrDialog(WMGotoDlg::Impl::IDD, pParent), m_p(p)
70 {
71 }
72
73 /**
74  * @brief Initialize the dialog.
75  * @return Always TRUE.
76  */
77 BOOL WMGotoDlg::Impl::OnInitDialog()
78 {
79         LangTranslateDialog(m_hWnd);
80         CDialog::OnInitDialog();
81
82         if (m_p->m_nFiles < 3)
83                 EnableDlgItem(IDC_WMGOTO_FILEMIDDLE, false);
84         if (m_p->m_nLastDiff == 0)
85                 EnableDlgItem(IDC_WMGOTO_TODIFF, false);
86
87         UpdateRange();
88         UpdateGoToButton();
89         UpdateData(FALSE);
90
91         return TRUE;
92 }
93
94 void WMGotoDlg::Impl::DoDataExchange(CDataExchange* pDX)
95 {
96         CTrDialog::DoDataExchange(pDX);
97         //{{AFX_DATA_MAP(WMGotoDlg)
98         DDX_Text(pDX, IDC_WMGOTO_PARAM, m_p->m_strParam);
99         DDX_Radio(pDX, IDC_WMGOTO_FILELEFT, m_p->m_nFile);
100         DDX_Radio(pDX, IDC_WMGOTO_TOLINE, m_p->m_nGotoWhat);
101         DDX_Text(pDX, IDC_WMGOTO_RANGE, m_strRange);
102         //}}AFX_DATA_MAP
103 }
104
105
106 BEGIN_MESSAGE_MAP(WMGotoDlg::Impl, CTrDialog)
107         //{{AFX_MSG_MAP(WMGotoDlg::Impl)
108                 // NOTE: the ClassWizard will add message map macros here
109         ON_EN_CHANGE(IDC_WMGOTO_PARAM, OnChangeParam)
110         ON_CONTROL_RANGE(BN_CLICKED, IDC_WMGOTO_FILELEFT, IDC_WMGOTO_FILERIGHT, OnBnClicked)
111         ON_CONTROL_RANGE(BN_CLICKED, IDC_WMGOTO_TOLINE, IDC_WMGOTO_TODIFF, OnBnClicked)
112         //}}AFX_MSG_MAP
113 END_MESSAGE_MAP()
114
115 /**
116  * @brief Called when the edit string changes.
117  */
118 void WMGotoDlg::Impl::OnChangeParam()
119 {
120         UpdateData(TRUE);
121         UpdateGoToButton();
122 }
123
124 /**
125  * @brief Called when user selects "File" or "Go to what" radio button.
126  * @param [in] nID Button ID of the selected item
127  */
128 void WMGotoDlg::Impl::OnBnClicked(UINT nID)
129 {
130         bool bIsValidId = (nID >= IDC_WMGOTO_FILELEFT && nID <= IDC_WMGOTO_FILERIGHT) || (nID >= IDC_WMGOTO_TOLINE && nID <= IDC_WMGOTO_TODIFF);
131         assert(bIsValidId);
132         if (!bIsValidId)
133                 return;
134
135         UpdateData(TRUE);
136         UpdateRange();
137         UpdateGoToButton();
138         UpdateData(FALSE);
139 }
140
141 /**
142  * @brief Get upper bound of acceptable range for selected "File" and "Go to what".
143  * @return Upper bound of acceptable range for selected "File" and "Go to what".
144  */
145 int WMGotoDlg::Impl::GetRangeMax()
146 {
147         bool bIsValidState = ((m_p->m_nGotoWhat >= 0 && m_p->m_nGotoWhat <= 1) && (m_p->m_nFile >= 0 && m_p->m_nFile <= 2));
148         assert(bIsValidState);
149         if (!bIsValidState)
150                 return -1;
151
152         return (m_p->m_nGotoWhat == 0) ? m_p->m_nLastLine[m_p->m_nFile] : m_p->m_nLastDiff;
153 }
154
155 /**
156  * @brief Update the acceptable range.
157  */
158 void WMGotoDlg::Impl::UpdateRange()
159 {
160         int nRangeMax = GetRangeMax();
161         m_strRange = (nRangeMax > 0) ? strutils::format(_T("(1-%d)"), nRangeMax) : _T("");
162 }
163
164 /**
165  * @brief Update the enabled state of the "Go to" button.
166  */
167 void WMGotoDlg::Impl::UpdateGoToButton()
168 {
169         int nNum = 0;
170         try
171         {
172                 nNum = std::stoi(m_p->m_strParam);
173         }
174         catch (...)
175         {
176                 nNum = 0;
177         }
178
179         bool bEnable = (nNum > 0 && nNum <= GetRangeMax());
180         EnableDlgItem(IDOK, bEnable);
181 }
182
183 /////////////////////////////////////////////////////////////////////////////
184 // WMGotoDlg message handlers
185
186
187
188 WMGotoDlg::WMGotoDlg()
189         : m_pimpl(new WMGotoDlg::Impl(this)), m_nFile(-1), m_nGotoWhat(-1), m_nFiles(-1), m_nLastLine{-1, -1, -1}, m_nLastDiff(-1) {}
190 WMGotoDlg::~WMGotoDlg() = default;
191 int WMGotoDlg::DoModal() { return static_cast<int>(m_pimpl->DoModal()); }
192