1 /*---------------------------------------------------------------------------*
2   Project:  HIO2 demos - multi
3   File:     multiView.cpp
4 
5   (C)2005 HUDSON SOFT
6 
7   $Header: /home/cvsroot/SDK/build/demos/hio2demo/vc++/multi/multiView.cpp,v 1.3 2006/03/15 06:31:26 mitu Exp $
8 
9   $NoKeywords: $
10  *---------------------------------------------------------------------------*/
11 
12 // multiView.cpp : implementation of the CMultiView class
13 //
14 
15 #include "stdafx.h"
16 #include "multi.h"
17 
18 #include "multiDoc.h"
19 #include "multiView.h"
20 #include ".\multiview.h"
21 
22 #ifdef _DEBUG
23 #define new DEBUG_NEW
24 #endif
25 
26 
27 // CMultiView
28 
IMPLEMENT_DYNCREATE(CMultiView,CFormView)29 IMPLEMENT_DYNCREATE(CMultiView, CFormView)
30 
31 BEGIN_MESSAGE_MAP(CMultiView, CFormView)
32     ON_MESSAGE(WM_USER, OnMyMessage)
33 	ON_BN_CLICKED(IDC_BTN_SEND, OnBnClickedBtnSend)
34 	ON_BN_CLICKED(IDC_BTN_CONNECT, OnBnClickedBtnConnect)
35 	ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBnClickedBtnDisconnect)
36 	ON_BN_CLICKED(IDC_RADIO_SYNC, OnBnClickedRadioSync)
37 	ON_BN_CLICKED(IDC_RADIO_ASYNC, OnBnClickedRadioAsync)
38 END_MESSAGE_MAP()
39 
40 // CMultiView construction/destruction
41 
42 CMultiView::CMultiView()
43 	: CFormView(CMultiView::IDD)
44 {
45 	// TODO: add construction code here
46 
47 }
48 
~CMultiView()49 CMultiView::~CMultiView()
50 {
51 }
52 
DoDataExchange(CDataExchange * pDX)53 void CMultiView::DoDataExchange(CDataExchange* pDX)
54 {
55 	CFormView::DoDataExchange(pDX);
56 }
57 
PreCreateWindow(CREATESTRUCT & cs)58 BOOL CMultiView::PreCreateWindow(CREATESTRUCT& cs)
59 {
60 	// TODO: Modify the Window class or styles here by modifying
61 	//  the CREATESTRUCT cs
62 
63 	return CFormView::PreCreateWindow(cs);
64 }
65 
OnInitialUpdate()66 void CMultiView::OnInitialUpdate()
67 {
68 	CFormView::OnInitialUpdate();
69 	ResizeParentToFit();
70 
71 	///////// for multiApp /////////
72 	CheckRadioButton(IDC_RADIO_SYNC, IDC_RADIO_ASYNC, IDC_RADIO_SYNC);
73 }
74 
75 
76 // CMultiView diagnostics
77 
78 #ifdef _DEBUG
AssertValid() const79 void CMultiView::AssertValid() const
80 {
81 	CFormView::AssertValid();
82 }
83 
Dump(CDumpContext & dc) const84 void CMultiView::Dump(CDumpContext& dc) const
85 {
86 	CFormView::Dump(dc);
87 }
88 
GetDocument() const89 CMultiDoc* CMultiView::GetDocument() const // non-debug version is inline
90 {
91 	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMultiDoc)));
92 	return (CMultiDoc*)m_pDocument;
93 }
94 #endif //_DEBUG
95 
96 ///////// for multiApp /////////
97 
98 // CMultiView message handlers
99 
OnMyMessage(WPARAM wParam,LPARAM lParam)100 LRESULT	CMultiView::OnMyMessage(WPARAM wParam, LPARAM lParam)
101 {
102 	TCHAR szBuffer[128];
103 
104 	switch ( wParam )
105 	{
106 	case EVENCT_RECEIVED:
107 		{
108 			int nIndex =
109 				GetApp()->m_cInfoList.IndexOf((LPVOID)GetDocument()->m_nHioIfID, MultiCompID);
110 			MultiPacketToString(
111 				szBuffer,
112 				(MULTI_PACKET *)&(GetApp()->m_cInfoList.GetItem(nIndex)->m_stPacket)
113 			);
114 			SetDlgItemText(IDC_RECV_DATA, szBuffer);
115 		}
116 		break;
117 	case EVENT_CONNECT:
118 		GetDocument()->UpdateTitle();
119 		GetDlgItem(IDC_BTN_SEND)->EnableWindow(TRUE);
120 		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);
121 		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);
122 		break;
123 	case EVENT_DISCONNECT:
124 		GetDocument()->UpdateTitle();
125 		GetDlgItem(IDC_BTN_SEND)->EnableWindow(FALSE);
126 		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);
127 		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);
128 		break;
129 	case EVENT_UPDATE_PC_TIME:
130 		{
131 			// PC time
132 			MULTI_PACKET stPacket;
133 			GetApp()->CreatePcTime(&stPacket);
134 			MultiPacketToString(szBuffer, &stPacket);
135 			SetDlgItemText(IDC_PC_TIME, szBuffer);
136 			// Communication status
137 			wsprintf(szBuffer,"ST:%04X",lParam);
138 			SetDlgItemText(IDC_STATUS, szBuffer);
139 			break;
140 		}
141 	case EVENT_WRITE_DONE:
142 		break;
143 	}
144 	return 0;
145 }
146 
OnBnClickedBtnSend()147 void CMultiView::OnBnClickedBtnSend()
148 {
149 	HIO2IF_RESULT result;
150 
151 	GetApp()->CreatePcTime(&m_stPacket);
152 	int nIndex =
153 		GetApp()->m_cInfoList.IndexOf((LPVOID)GetDocument()->m_nHioIfID, MultiCompID);
154 	BOOL bSync = GetApp()->m_cInfoList.GetItem(nIndex)->m_bSync;
155 
156 	GetApp()->m_pHioIf->EnterCriticalSection();
157 
158 #ifdef	PROTOCOL_USED
159 	result = GetApp()->m_pHioIf->Write(
160 					GetDocument()->m_nHioIfID,
161 					MULTI_PC2NNGC_ADDR,
162 					(LPVOID)&m_stPacket,
163 					MULTI_BUFFER_SIZE,
164 					bSync);
165 
166 #else	// PROTOCOL_USED
167 	result = GetApp()->m_pHioIf->WriteFree(
168 					GetDocument()->m_nHioIfID,
169 					MULTI_PC2NNGC_ADDR,
170 					(LPVOID)&m_stPacket,
171 					MULTI_BUFFER_SIZE,
172 					bSync);
173 #endif	// PROTOCOL_USED
174 
175 	GetApp()->m_pHioIf->LeaveCriticalSection();
176 
177 	if ( HIO2IF_FAILED(result) )
178 		MessageBox("Send Error", "ERROR", MB_OK);
179 }
180 
OnBnClickedBtnConnect()181 void CMultiView::OnBnClickedBtnConnect()
182 {
183 	GetApp()->Connect(GetDocument()->m_nHioIfID);
184 }
185 
OnBnClickedBtnDisconnect()186 void CMultiView::OnBnClickedBtnDisconnect()
187 {
188 	GetApp()->Disconnect(GetDocument()->m_nHioIfID);
189 }
190 
OnBnClickedRadioSync()191 void CMultiView::OnBnClickedRadioSync()
192 {
193 	int nIndex =
194 		GetApp()->m_cInfoList.IndexOf((LPVOID)GetDocument()->m_nHioIfID, MultiCompID);
195 	GetApp()->m_cInfoList.GetItem(nIndex)->m_bSync = FALSE;
196 }
197 
OnBnClickedRadioAsync()198 void CMultiView::OnBnClickedRadioAsync()
199 {
200 	int nIndex =
201 		GetApp()->m_cInfoList.IndexOf((LPVOID)GetDocument()->m_nHioIfID, MultiCompID);
202 	GetApp()->m_cInfoList.GetItem(nIndex)->m_bSync = TRUE;
203 }
204 
205 
OnDraw(CDC *)206 void CMultiView::OnDraw(CDC* /*pDC*/)
207 {
208 	CMultiApp* pApp = GetApp();
209 	CMultiDoc* pDoc = GetDocument();
210 
211 	// case when launched earlier from NNGC
212 	if ( (pDoc->m_nHioIfID != HIO2IF_INVALID_ID)
213 		&& (pApp->m_pHioIf->IsConnected(pDoc->m_nHioIfID)) )
214 	{
215 		pDoc->UpdateTitle();
216 		GetDlgItem(IDC_BTN_SEND)->EnableWindow(TRUE);
217 		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);
218 		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);
219 	}
220 }
221