1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 2 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 // AddRemoveDialog.cpp : implementation file
30 #include "AddRemoveDialog.h"
34 static char THIS_FILE[] = __FILE__;
37 /////////////////////////////////////////////////////////////////////////////
38 // CAddRemoveDialog dialog
40 void CAddRemoveDialog::AfxDataInit()
42 //{{AFX_DATA_INIT(CAddRemoveDialog)
43 // NOTE: the ClassWizard will add member initialization here
47 CAddRemoveDialog::CAddRemoveDialog(CWnd* pParent /*=NULL*/)
48 : CeCosDialog(IDD, pParent),
52 m_nHorizontalResizingLimit(0)
57 CAddRemoveDialog::CAddRemoveDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
58 : CeCosDialog(nIDTemplate, pParent),
61 m_nHorizontalResizingLimit(0)
66 void CAddRemoveDialog::DoDataExchange(CDataExchange* pDX)
68 CeCosDialog::DoDataExchange(pDX);
69 //{{AFX_DATA_MAP(CAddRemoveDialog)
70 DDX_Control(pDX, IDC_PACKAGE_DESC, m_wndDesc);
71 DDX_Control(pDX, IDC_ADDREMOVE_LIST2, m_List2);
72 DDX_Control(pDX, IDC_ADDREMOVE_LIST1, m_List1);
73 DDX_Control(pDX, IDC_ADDREMOVE_ADD, m_Add);
74 DDX_Control(pDX, IDC_ADDREMOVE_REMOVE,m_Remove);
79 BEGIN_MESSAGE_MAP(CAddRemoveDialog, CeCosDialog)
80 //{{AFX_MSG_MAP(CAddRemoveDialog)
81 ON_BN_CLICKED(IDC_ADDREMOVE_ADD, OnAdd)
82 ON_BN_CLICKED(IDC_ADDREMOVE_REMOVE, OnRemove)
83 ON_LBN_DBLCLK(IDC_ADDREMOVE_LIST1, OnDblclkList1)
84 ON_LBN_DBLCLK(IDC_ADDREMOVE_LIST2, OnDblclkList2)
85 ON_LBN_SELCHANGE(IDC_ADDREMOVE_LIST1, OnSelchangeList1)
86 ON_LBN_SELCHANGE(IDC_ADDREMOVE_LIST2, OnSelchangeList2)
91 /////////////////////////////////////////////////////////////////////////////
92 // CAddRemoveDialog message handlers
94 void CAddRemoveDialog::OnAdd()
97 m_Add.EnableWindow(FALSE);
98 m_Remove.EnableWindow(TRUE);
101 void CAddRemoveDialog::OnRemove()
103 Add(m_List2,m_List1);
104 m_Add.EnableWindow(TRUE);
105 m_Remove.EnableWindow(FALSE);
108 void CAddRemoveDialog::Insert(LPCTSTR pszItem, bool bAdded, LPCTSTR pszDesc/*=NULL*/)
110 m_bHaveDescs|=(NULL!=pszDesc);
111 m_arnItems. Add(bAdded);
112 m_arstrItems.Add(pszItem);
113 m_arstrDescs.Add(pszDesc?pszDesc:_T(""));
116 BOOL CAddRemoveDialog::OnInitDialog()
118 CeCosDialog::OnInitDialog();
120 m_arbSel=new int[GetCount()];
122 SetWindowText(m_strCaption);
123 SetSortMode(m_List1);
124 SetSortMode(m_List2);
127 CDC *pDC=m_List1.GetDC();
128 CFont *pOldFont=pDC->SelectObject(m_List1.GetFont());
129 for(int i=GetCount()-1;i>=0;--i) {
130 const CString &str=m_arstrItems[i];
131 cxExtent=max(cxExtent,pDC->GetTextExtent(str).cx);
132 CListBox &lb=m_arnItems[i]?m_List2:m_List1;
133 lb.SetItemData(lb.AddString(str),(DWORD)i);
135 pDC->SelectObject(pOldFont);
137 SetDlgItemText(IDC_ADDREMOVE_TEXT1,m_strMessage1);
138 SetDlgItemText(IDC_ADDREMOVE_TEXT2,m_strMessage2);
141 m_List1.GetWindowRect(rcLb1);
143 GetWindowRect(rcDialog);
145 int nExpand=cxExtent-(rcLb1.Width()-GetSystemMetrics(SM_CXVSCROLL)-2*GetSystemMetrics(SM_CXBORDER)-5);
146 nExpand=min(nExpand,m_nHorizontalResizingLimit);
148 MoveChild(IDOK,2*nExpand,Move);
149 MoveChild(IDCANCEL,2*nExpand,Move);
150 MoveChild(IDC_ADDREMOVE_ADD,nExpand,Move);
151 MoveChild(IDC_ADDREMOVE_REMOVE,nExpand,Move);
152 MoveChild(IDC_ADDREMOVE_LIST1,nExpand,Stretch);
153 MoveChild(IDC_ADDREMOVE_TEXT2,nExpand,Move);
154 MoveChild(IDC_ADDREMOVE_LIST2,nExpand,Move);
155 MoveChild(IDC_ADDREMOVE_LIST2,nExpand,Stretch);
156 MoveChild(IDC_PACKAGE_DESC,2*nExpand,Stretch);
157 rcDialog.right+=2*nExpand; // MoveWindow call is below
160 // enable horizontal scrolling if necessary, assuming the
161 // listboxes have identical widths and accommodating a
162 // 2 pixel border at each side of each listbox
163 m_List1.SetHorizontalExtent (cxExtent + 4);
164 m_List2.SetHorizontalExtent (cxExtent + 4);
166 m_Add.EnableWindow(m_List1.GetCount()>0);
167 m_Remove.EnableWindow(m_List2.GetCount()>0);
170 // Remove the description pane if not used:
172 m_wndDesc.GetWindowRect(rcDesc);
173 m_wndDesc.ShowWindow(SW_HIDE);
174 int delta=(rcDesc.Height()+(rcDesc.top-rcLb1.bottom));
175 MoveChild(IDOK,delta,Up);
176 MoveChild(IDCANCEL,delta,Up);
177 rcDialog.bottom-=delta;
180 // Finally, adjust dialog size according to calculations:
181 MoveWindow(rcDialog);
183 return TRUE; // return TRUE unless you set the focus to a control
184 // EXCEPTION: OCX Property Pages should return FALSE
187 void CAddRemoveDialog::OnDblclkList1()
189 OnDblclk(m_List1,m_List2);
190 m_Add.EnableWindow(FALSE);
191 m_Remove.EnableWindow(TRUE);
194 void CAddRemoveDialog::OnDblclkList2()
196 OnDblclk(m_List2,m_List1);
197 m_Add.EnableWindow(TRUE);
198 m_Remove.EnableWindow(FALSE);
201 int CAddRemoveDialog::Add(CListBox &lb1,CListBox &lb2)
203 int nSelCount=lb1.GetSelItems(lb1.GetCount(),m_arbSel);
204 for(int i=nSelCount-1;i>=0;--i){
205 Xfer(lb1,lb2,m_arbSel[i]);
212 void CAddRemoveDialog::OnDblclk(CListBox &lb1,CListBox &lb2)
215 DWORD dwPos=GetMessagePos();
216 POINTS pts=MAKEPOINTS(dwPos);
217 CRect rc(pts.x,pts.y,0,0);
218 lb1.ScreenToClient(rc);
219 CPoint pt(rc.left,rc.top);
220 int nItem=lb1.ItemFromPoint(pt,bOutside);
228 void CAddRemoveDialog::ClearSel(CListBox &lb)
230 for(int i=lb.GetCount()-1;i>=0;--i){
235 void CAddRemoveDialog::OnSelchangeList1()
238 m_Add.EnableWindow(m_List1.GetSelCount()>0);
239 UpdateDescription(m_List1);
240 m_Remove.EnableWindow(FALSE);
243 void CAddRemoveDialog::OnSelchangeList2()
246 m_Remove.EnableWindow(m_List2.GetSelCount()>0);
247 UpdateDescription(m_List2);
248 m_Add.EnableWindow(FALSE);
251 void CAddRemoveDialog::Xfer(CListBox &lb1, CListBox &lb2, int nItem)
254 lb1.GetText(nItem,strItem);
255 int nNewItem=lb2.AddString(strItem);
256 lb2.SetSel(nNewItem,TRUE);
257 DWORD nIndex=lb1.GetItemData(nItem);
258 lb2.SetItemData(nNewItem,nIndex);
259 m_arnItems[(int)nIndex]^=1;
260 lb1.DeleteString(nItem);
263 bool CAddRemoveDialog::IsAdded(LPCTSTR pszItem) const
265 for(int i=GetCount()-1;i>=0;--i){
266 if(m_arstrItems[i]==pszItem){
267 return 0!=m_arnItems[i];
275 void CAddRemoveDialog::SetCaption(LPCTSTR pszCaption)
277 m_strCaption=pszCaption;
280 void CAddRemoveDialog::SetMessage(LPCTSTR pszText1,LPCTSTR pszText2)
282 m_strMessage1=pszText1;
283 m_strMessage2=pszText2;
286 void CAddRemoveDialog::OnDestroy()
288 CeCosDialog::OnDestroy();
292 // Add the "sort" property to the listboxes. Unfortunately this entails
294 void CAddRemoveDialog::SetSortMode(CListBox &lb)
296 DWORD dwStyle=lb.GetStyle();
297 if(m_bSort!=(0!=(dwStyle&LBS_SORT))){
303 dwStyle|=WS_HSCROLL; // For some reason this doesn't come through GetStyle()
305 lb.GetWindowRect(rect);
306 ScreenToClient(rect);
307 DWORD dwStyleEx=lb.GetExStyle();
308 CFont *pFont=lb.GetFont();
309 DWORD id=lb.GetDlgCtrlID();
312 // CreateEx not Create to get ourselves a 3D border
313 lb.CreateEx(dwStyleEx,_T("LISTBOX"),NULL,dwStyle,rect,this,id);
318 // Implement ctrl/A on listboxes
319 BOOL CAddRemoveDialog::PreTranslateMessage(MSG* pMsg)
321 if(WM_CHAR==pMsg->message && 1==pMsg->wParam){
322 if(pMsg->hwnd==m_List1.m_hWnd){
323 m_List1.SelItemRange(TRUE,0,m_List1.GetCount()-1);
325 m_Add.EnableWindow(m_List1.GetCount()>0);
326 m_Remove.EnableWindow(FALSE);
328 } else if(pMsg->hwnd==m_List2.m_hWnd){
329 m_List2.SelItemRange(TRUE,0,m_List2.GetCount()-1);
331 m_Remove.EnableWindow(m_List2.GetCount()>0);
332 m_Add.EnableWindow(FALSE);
337 return CeCosDialog::PreTranslateMessage(pMsg);
340 void CAddRemoveDialog::MoveChild(UINT id,int delta,MoveType how)
342 CWnd *pWnd=GetDlgItem(id);
344 pWnd->GetWindowRect(rect);
345 ScreenToClient(rect);
359 pWnd->MoveWindow(rect);
362 void CAddRemoveDialog::UpdateDescription(CListBox &lb)
365 // Set the description if the listbox has exactly one item selected
366 if(1==lb.GetSelCount()){
368 lb.GetSelItems(1,&nIndex);
369 strDesc=m_arstrDescs[lb.GetItemData(nIndex)];
371 m_wndDesc.SetWindowText(strDesc);
374 void CAddRemoveDialog::SetHorizontalResizingLimit(int nPercent)
376 m_nHorizontalResizingLimit=nPercent;