source: trunk/XSUC/DlgConnections.cpp@ 45

Last change on this file since 45 was 11, checked in by marrucho, 12 years ago
File size: 4.7 KB
Line 
1// DlgConnections.cpp : implementation file
2//
3
4#include "stdafx.h"
5#include "Modbucfg.h"
6#include "DlgConnections.h"
7
8#ifdef _DEBUG
9#define new DEBUG_NEW
10#undef THIS_FILE
11static char THIS_FILE[] = __FILE__;
12#endif
13
14/////////////////////////////////////////////////////////////////////////////
15// CDlgConnections dialog
16
17
18CDlgConnections::CDlgConnections(CWnd* pParent /*=NULL*/)
19 : CDialog(CDlgConnections::IDD, pParent)
20{
21 //{{AFX_DATA_INIT(CDlgConnections)
22 // NOTE: the ClassWizard will add member initialization here
23 //}}AFX_DATA_INIT
24}
25
26
27void CDlgConnections::DoDataExchange(CDataExchange* pDX)
28{
29 CDialog::DoDataExchange(pDX);
30 //{{AFX_DATA_MAP(CDlgConnections)
31 DDX_Control(pDX, IDC_LST_CONNECTIOS, m_lstConnections);
32 DDX_Control(pDX, IDC_CMD_REMOVE, m_cmdRemove);
33 DDX_Control(pDX, IDC_CMD_EDIT, m_cmdEdit);
34 DDX_Control(pDX, IDC_CMD_ADD, m_cmdType);
35 DDX_Control(pDX, IDC_CBO_TYPE, m_cboType);
36 //}}AFX_DATA_MAP
37}
38
39
40BEGIN_MESSAGE_MAP(CDlgConnections, CDialog)
41 //{{AFX_MSG_MAP(CDlgConnections)
42 ON_BN_CLICKED(IDC_CMD_ADD, OnCmdAdd)
43 ON_BN_CLICKED(IDC_CMD_EDIT, OnCmdEdit)
44 ON_BN_CLICKED(IDC_CMD_REMOVE, OnCmdRemove)
45 ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
46 ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad)
47 //}}AFX_MSG_MAP
48END_MESSAGE_MAP()
49
50/////////////////////////////////////////////////////////////////////////////
51// CDlgConnections message handlers
52
53void CDlgConnections::LoadControls()
54{
55 POSITION pos;
56 //new connection type
57 m_cboType.ResetContent();
58 m_cboType.AddString(_T("Local/DCom"));
59 m_cboType.AddString(_T("Open Modbus"));
60 m_cboType.SetCurSel(0);
61 CString sKey;
62 CConnection* pConnection;
63
64 pos = gConnections.GetStartPosition();
65
66 m_lstConnections.ResetContent();
67
68 while (NULL!=pos) {
69 gConnections.GetNextAssoc(pos,sKey,pConnection);
70 m_lstConnections.AddString(sKey);
71 }
72
73
74}
75
76BOOL CDlgConnections::OnInitDialog()
77{
78 CDialog::OnInitDialog();
79
80 LoadControls();
81 return TRUE; // return TRUE unless you set the focus to a control
82 // EXCEPTION: OCX Property Pages should return FALSE
83}
84
85void CDlgConnections::OnCmdAdd()
86{
87 int nIndex;
88
89 CConnection* pConnection=NULL;
90 CCommParameters dlgComParameters;
91 CDlgRemoteParameters dlgRemoteParameters;
92
93 nIndex = m_cboType.GetCurSel();
94
95 switch (nIndex) {
96 case 0: //Auto
97
98 dlgComParameters.m_bModeNew=TRUE;
99 dlgComParameters.Connection(NULL);
100 dlgComParameters.DoModal();
101 pConnection = dlgComParameters.Connection();
102
103 break;
104
105 case 1: //Remote
106
107 dlgRemoteParameters.m_bModeNew=TRUE;
108 dlgRemoteParameters.Connection(NULL);
109 dlgRemoteParameters.DoModal();
110 pConnection = dlgRemoteParameters.Connection();
111
112 break;
113 }
114
115
116 if (NULL!=pConnection) {
117
118 m_lstConnections.AddString(pConnection->Key());
119 }
120
121}
122
123void CDlgConnections::OnCmdEdit()
124{
125 int nIndex;
126 CString sKey;
127 CConnection* pConnection =NULL;
128 CCommParameters dlgComParameters;
129 CDlgRemoteParameters dlgRemoteParameters;
130
131 nIndex = m_lstConnections.GetCurSel();
132
133 if (nIndex>=0) {
134 m_lstConnections.GetText(nIndex,sKey);
135
136 if (gConnections.Lookup(sKey,pConnection)) {
137
138 switch (pConnection->Type()) {
139
140 case CConnection::AUTO:
141 dlgComParameters.m_bModeNew=FALSE;
142 dlgComParameters.Connection(pConnection);
143 dlgComParameters.DoModal();
144 break;
145 case CConnection::REMOTE:
146 dlgRemoteParameters.m_bModeNew=FALSE;
147 dlgRemoteParameters.Connection(pConnection);
148 dlgRemoteParameters.DoModal();
149 break;
150 default:
151 ASSERT(FALSE);
152
153 }
154 }
155
156 }
157
158}
159
160void CDlgConnections::OnCmdRemove()
161{
162 int nIndex;
163 CString sKey;
164 int iResp;
165
166 nIndex = m_lstConnections.GetCurSel();
167
168 if (nIndex>=0) {
169 m_lstConnections.GetText(nIndex,sKey);
170 iResp=AfxMessageBox(_T("Remove connection ")+sKey,MB_YESNO);
171 if (IDYES==iResp) {
172 gConnections.Remove(sKey);
173 m_lstConnections.DeleteString(nIndex);
174 }
175
176 }
177
178}
179
180void CDlgConnections::OnButtonSave()
181{
182 CFile fileCfg;
183
184 if (fileCfg.Open("Modbu.cfg",CFile::modeCreate|CFile::modeWrite)){
185
186 CArchive archConfig(&fileCfg,CArchive::store);
187 gConnections.Serialize(archConfig);
188 archConfig.Close();
189 fileCfg.Close();
190 }
191}
192
193void CDlgConnections::OnButtonLoad()
194{
195
196 CFile fileCfg;
197
198 gConnections.RemoveAll();
199
200 if (fileCfg.Open("Modbu.cfg",CFile::modeRead)){
201
202 try {
203 CArchive archConfig(&fileCfg,CArchive::load);
204 gConnections.Serialize(archConfig);
205 archConfig.Close();
206
207 LoadControls();
208 }
209
210 catch( CException* e )
211 {
212 // Handle the exception here.
213 // "e" contains information about the exception.
214 e->Delete();
215
216 }//end catch
217
218 fileCfg.Close();
219 }
220}
Note: See TracBrowser for help on using the repository browser.