-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolsDialog.cpp
251 lines (183 loc) · 6.57 KB
/
ToolsDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
////////////////////////////////////////////////////////////////////////////////
//! \file ToolsDialog.cpp
//! \brief The ToolsDialog class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "ToolsDialog.hpp"
#include "EditToolDialog.hpp"
//! The maximum number of tool definitions supported.
const size_t MAX_TOOLS = ID_HOST_INVOKE_TOOL_19 - ID_HOST_INVOKE_TOOL_1 + 1;
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
ToolsDialog::ToolsDialog()
: CDialog(IDD_HOST_TOOLS)
{
DEFINE_CTRL_TABLE
CTRL(IDC_TOOLS, &m_view)
END_CTRL_TABLE
DEFINE_CTRLMSG_TABLE
CMD_CTRLMSG(IDC_ADD, BN_CLICKED, &ToolsDialog::onAddTool)
CMD_CTRLMSG(IDC_COPY, BN_CLICKED, &ToolsDialog::onCopyTool)
CMD_CTRLMSG(IDC_EDIT, BN_CLICKED, &ToolsDialog::onEditTool)
CMD_CTRLMSG(IDC_DELETE, BN_CLICKED, &ToolsDialog::onDeleteTool)
CMD_CTRLMSG(IDC_UP, BN_CLICKED, &ToolsDialog::onMoveToolUp)
CMD_CTRLMSG(IDC_DOWN, BN_CLICKED, &ToolsDialog::onMoveToolDown)
NFY_CTRLMSG(IDC_TOOLS, LVN_ITEMCHANGED, &ToolsDialog::onToolSelected)
NFY_CTRLMSG(IDC_TOOLS, NM_DBLCLK, &ToolsDialog::onToolDoubleClicked)
END_CTRLMSG_TABLE
}
////////////////////////////////////////////////////////////////////////////////
//! Dialog initialisation handler.
void ToolsDialog::OnInitDialog()
{
m_view.InsertColumn(TOOL_NAME, TXT("Tool"), m_view.StringWidth(25));
m_view.InsertColumn(COMMAND_LINE, TXT("Command Line"), m_view.StringWidth(45));
for (Tools::const_iterator it = m_tools.begin(); it != m_tools.end(); ++it)
addItemToView(*it, (it == m_tools.begin()));
updateUi();
}
////////////////////////////////////////////////////////////////////////////////
//! OK button handler.
bool ToolsDialog::OnOk()
{
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Tools view selection change handler.
LRESULT ToolsDialog::onToolSelected(NMHDR& header)
{
const NMLISTVIEW& message = reinterpret_cast<const NMLISTVIEW&>(header);
if (message.uChanged & LVIF_STATE)
updateUi();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//! Double-clicked tool.
LRESULT ToolsDialog::onToolDoubleClicked(NMHDR& /*header*/)
{
if (m_view.IsSelection())
onEditTool();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//! Add button handler.
void ToolsDialog::onAddTool()
{
EditToolDialog dialog;
for (Tools::const_iterator it = m_tools.begin(); it != m_tools.end(); ++it)
dialog.m_usedNames.insert((*it)->m_name);
if (dialog.RunModal(*this) == IDOK)
{
ConstToolPtr tool = makeTool(dialog.m_tool);
m_tools.append(tool);
addItemToView(tool, true);
updateUi();
}
}
////////////////////////////////////////////////////////////////////////////////
//! Copy button handler.
void ToolsDialog::onCopyTool()
{
ASSERT(m_view.IsSelection());
const size_t selection = m_view.Selection();
const ConstToolPtr source = m_tools.tool(selection);
EditToolDialog dialog;
dialog.m_tool = *source;
for (Tools::const_iterator it = m_tools.begin(); it != m_tools.end(); ++it)
dialog.m_usedNames.insert((*it)->m_name);
if (dialog.RunModal(*this) == IDOK)
{
ConstToolPtr tool = makeTool(dialog.m_tool);
m_tools.append(tool);
addItemToView(tool, true);
updateUi();
}
}
////////////////////////////////////////////////////////////////////////////////
//! Edit button handler.
void ToolsDialog::onEditTool()
{
ASSERT(m_view.IsSelection());
const size_t selection = m_view.Selection();
const ConstToolPtr original = m_tools.tool(selection);
EditToolDialog dialog;
dialog.m_tool = *original;
for (Tools::const_iterator it = m_tools.begin(); it != m_tools.end(); ++it)
{
if ((*it)->m_name != original->m_name)
dialog.m_usedNames.insert((*it)->m_name);
}
if (dialog.RunModal(*this) == IDOK)
{
const ConstToolPtr edited = makeTool(dialog.m_tool);
m_tools.replace(selection, edited);
updateViewItem(selection, edited);
updateUi();
}
}
////////////////////////////////////////////////////////////////////////////////
//! Delete button handler.
void ToolsDialog::onDeleteTool()
{
ASSERT(m_view.IsSelection());
const size_t selection = m_view.Selection();
m_view.DeleteItem(selection, true);
m_tools.remove(selection);
updateUi();
}
////////////////////////////////////////////////////////////////////////////////
//! Up button handler.
void ToolsDialog::onMoveToolUp()
{
ASSERT(m_view.IsSelection());
ASSERT(m_view.Selection() != 0);
const size_t selection = m_view.Selection();
m_tools.swap(selection, selection-1);
updateViewItem(selection, m_tools.tool(selection));
updateViewItem(selection-1, m_tools.tool(selection-1));
m_view.Select(selection-1);
}
////////////////////////////////////////////////////////////////////////////////
//! Down button handler.
void ToolsDialog::onMoveToolDown()
{
ASSERT(m_view.IsSelection());
ASSERT(m_view.Selection() != (m_view.ItemCount()-1));
const size_t selection = m_view.Selection();
m_tools.swap(selection, selection+1);
updateViewItem(selection, m_tools.tool(selection));
updateViewItem(selection+1, m_tools.tool(selection+1));
m_view.Select(selection+1);
}
////////////////////////////////////////////////////////////////////////////////
//! Update the state of the UI.
void ToolsDialog::updateUi()
{
const bool isSelection = m_view.IsSelection();
const bool maxDefined = (m_view.ItemCount() == MAX_TOOLS);
const bool isMoveable = (m_view.ItemCount() > 1);
const bool isFirstSelected = isSelection && (m_view.Selection() == 0);
const bool isLastSelected = isSelection && (m_view.Selection() == (m_view.ItemCount()-1));
Control(IDC_ADD).Enable(!maxDefined);
Control(IDC_COPY).Enable(!maxDefined && isSelection);
Control(IDC_EDIT).Enable(isSelection);
Control(IDC_DELETE).Enable(isSelection);
Control(IDC_UP).Enable(isMoveable && isSelection && !isFirstSelected);
Control(IDC_DOWN).Enable(isMoveable && isSelection && !isLastSelected);
}
////////////////////////////////////////////////////////////////////////////////
//! Add an item to the view.
void ToolsDialog::addItemToView(ConstToolPtr tool, bool select)
{
const size_t row = m_view.AppendItem(tool->m_name);
updateViewItem(row, tool);
if (select)
m_view.Select(row);
}
////////////////////////////////////////////////////////////////////////////////
//! Update an item in the view.
void ToolsDialog::updateViewItem(size_t row, ConstToolPtr tool)
{
m_view.ItemText(row, TOOL_NAME, tool->m_name);
m_view.ItemText(row, COMMAND_LINE, tool->m_commandLine);
}