4 * @brief Implementation file for CDirColsDlg
6 * @date Created: 2003-08-19
11 #include "DirColsDlg.h"
18 /////////////////////////////////////////////////////////////////////////////
22 * @brief Default dialog constructor.
23 * @param [in] pParent Dialog's parent component (window).
25 CDirColsDlg::CDirColsDlg(CWnd* pParent /*= nullptr*/)
26 : CTrDialog(CDirColsDlg::IDD, pParent)
32 * @brief Handle dialog data exchange between controls and variables.
34 void CDirColsDlg::DoDataExchange(CDataExchange* pDX)
36 CTrDialog::DoDataExchange(pDX);
37 //{{AFX_DATA_MAP(CDirColsDlg)
38 DDX_Control(pDX, IDC_COLDLG_LIST, m_listColumns);
42 BEGIN_MESSAGE_MAP(CDirColsDlg, CTrDialog)
43 //{{AFX_MSG_MAP(CDirColsDlg)
44 ON_BN_CLICKED(IDC_UP, OnUp)
45 ON_BN_CLICKED(IDC_DOWN, OnDown)
46 ON_BN_CLICKED(IDC_COLDLG_DEFAULTS, OnDefaults)
48 ON_NOTIFY(LVN_ITEMCHANGED, IDC_COLDLG_LIST, OnLvnItemchangedColdlgList)
51 /////////////////////////////////////////////////////////////////////////////
52 // CDirColsDlg message handlers
55 * @brief Initialize listcontrol in dialog.
57 void CDirColsDlg::InitList()
59 const int lpx = CClientDC(this).GetDeviceCaps(LOGPIXELSX);
60 auto pointToPixel = [lpx](int point) { return MulDiv(point, lpx, 72); };
62 // Show selection across entire row.
63 // Also enable infotips.
64 m_listColumns.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
65 m_listColumns.InsertColumn(0, _T(""), LVCFMT_LEFT, pointToPixel(148));
69 * @brief Dialog initialisation. Load column lists.
71 BOOL CDirColsDlg::OnInitDialog()
73 CTrDialog::OnInitDialog();
77 return TRUE; // return TRUE unless you set the focus to a control
78 // EXCEPTION: OCX Property Pages should return FALSE
82 * @brief Load listboxes on screen from column array.
84 void CDirColsDlg::LoadLists()
86 for (ColumnArray::iterator iter = m_cols.begin(); iter != m_cols.end(); ++iter)
88 const column & c = *iter;
89 int x = m_listColumns.InsertItem(m_listColumns.GetItemCount(),
91 m_listColumns.SetItemData(x, c.log_col);
93 m_listColumns.SetCheck(x, TRUE);
95 SortArrayToLogicalOrder();
97 // Set first item to selected state
102 * @brief Select item in list.
103 * @param [in] index Index of item to select.
105 void CDirColsDlg::SelectItem(int index)
107 m_listColumns.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);
111 * @brief Load listboxes on screen from defaults column array
113 void CDirColsDlg::LoadDefLists()
115 for (ColumnArray::iterator iter = m_defCols.begin(); iter != m_defCols.end(); ++iter)
117 const column & c = *iter;
118 int x = m_listColumns.InsertItem(m_listColumns.GetItemCount(),
120 m_listColumns.SetItemData(x, c.log_col);
122 m_listColumns.SetCheck(x, TRUE);
124 SortArrayToLogicalOrder();
127 * @brief Sort m_cols so that it is in logical column order.
129 void CDirColsDlg::SortArrayToLogicalOrder()
131 std::sort(m_cols.begin(), m_cols.end(), &CompareColumnsByLogicalOrder);
135 * @brief Compare column order of two columns.
136 * @param [in] el1 First column to compare.
137 * @param [in] el2 Second column to compare.
138 * @return Column order.
140 bool CDirColsDlg::CompareColumnsByLogicalOrder( const column & el1, const column & el2 )
142 return el1.log_col < el2.log_col;
146 * @brief Move column name in the list.
147 * @param [in] index Index of column to move.
148 * @param [in] newIndex New index for the column to move.
150 void CDirColsDlg::MoveItem(int index, int newIndex)
152 // Get current column data
153 String text = m_listColumns.GetItemText(index, 0);
154 bool checked = !!m_listColumns.GetCheck(index);
155 UINT state = m_listColumns.GetItemState(index, LVIS_SELECTED);
156 DWORD_PTR data = m_listColumns.GetItemData(index);
159 m_listColumns.DeleteItem(index);
161 // Insert column in new index
162 m_listColumns.InsertItem(newIndex, text.c_str());
163 m_listColumns.SetItemState(newIndex, state, LVIS_SELECTED);
164 m_listColumns.SetItemData(newIndex, data);
166 m_listColumns.SetCheck(newIndex);
170 * @brief Move selected items one position up or down.
171 * @param [in] bUp If `true` items are moved up,
172 * if `false` items are moved down.
174 void CDirColsDlg::MoveSelectedItems(bool bUp)
177 POSITION pos = m_listColumns.GetFirstSelectedItemPosition();
179 while (pos != nullptr)
181 int ind = m_listColumns.GetNextSelectedItem(pos);
182 int newInd = bUp ? ind - 1: ind + 1;
184 // Remember first item
187 MoveItem(ind, newInd);
189 m_listColumns.EnsureVisible(firstInd, FALSE);
193 * @brief Move selected items up in list.
195 void CDirColsDlg::OnUp()
197 MoveSelectedItems(true);
201 * @brief Move selected items down (in show list)
203 void CDirColsDlg::OnDown()
205 MoveSelectedItems(false);
209 * @brief Move hidden columns as last items in the list.
211 void CDirColsDlg::SanitizeOrder()
213 // Find last visible column.
214 int i = m_listColumns.GetItemCount() - 1;
217 if (m_listColumns.GetCheck(i))
221 // Move all hidden columns below last visible column.
222 for (int j = i; j >= 0; j--)
224 if (!m_listColumns.GetCheck(j))
233 * @brief User clicked ok, so we update m_cols and close.
235 void CDirColsDlg::OnOK()
239 for (int i = 0; i < m_listColumns.GetItemCount(); i++)
241 bool checked = !!m_listColumns.GetCheck(i);
242 DWORD_PTR data = m_listColumns.GetItemData(i);
243 column & col1 = m_cols[data];
254 * @brief Empty lists and load default columns and order.
256 void CDirColsDlg::OnDefaults()
258 m_listColumns.DeleteAllItems();
264 * @brief Update description when selected item changes.
266 void CDirColsDlg::OnLvnItemchangedColdlgList(NMHDR *pNMHDR, LRESULT *pResult)
268 POSITION pos = m_listColumns.GetFirstSelectedItemPosition();
272 int ind = m_listColumns.GetNextSelectedItem(pos);
273 DWORD_PTR data = m_listColumns.GetItemData(ind);
275 ColumnArray::size_type j;
276 for (j = 0; j < m_cols.size(); j++)
278 if (static_cast<DWORD_PTR>(m_cols[j].log_col) == data)
281 SetDlgItemText(IDC_COLDLG_DESC, m_cols[j].desc);
283 // Disable Up/Down -buttons when first/last items are selected.
284 EnableDlgItem(IDC_UP, ind != 0);
285 EnableDlgItem(IDC_DOWN,
286 ind != m_listColumns.GetItemCount() - static_cast<int>(m_listColumns.GetSelectedCount()));