Garmin Fleet Management Controller  2.19.0
CManageCannedResponseDlg.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * MODULE NAME:
4 * CManageCannedResponseDlg.cpp
5 *
6 * Copyright 2008-2009 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 #include "stdafx.h"
11 #include "CFmiApplication.h"
13 #include "Event.h"
14 using namespace std;
15 
16 IMPLEMENT_DYNAMIC( CManageCannedResponseDlg, CDialog )
17 
18 BEGIN_MESSAGE_MAP( CManageCannedResponseDlg, CDialog )
19  ON_BN_CLICKED( IDC_CANRESP_BTN_DELETE, OnBnClickedDelete )
20  ON_BN_CLICKED( IDC_CANRESP_BTN_SEND, OnBnClickedSend )
21  ON_EN_CHANGE( IDC_CANRESP_EDIT_ID, OnEnChangeRspBoxes )
22  ON_EN_CHANGE( IDC_CANRESP_EDIT_TEXT, OnEnChangeRspBoxes )
23  ON_BN_CLICKED( IDOK, OnBnClickedOk )
24  ON_EN_SETFOCUS( IDC_CANRESP_EDIT_TEXT, OnEnSetfocusResponseEdit )
25  ON_EN_KILLFOCUS( IDC_CANRESP_EDIT_TEXT, OnEnKillfocusResponseEdit )
26  ON_EN_SETFOCUS( IDC_CANRESP_EDIT_ID, OnEnSetfocusResponseEdit )
27  ON_EN_KILLFOCUS( IDC_CANRESP_EDIT_ID, OnEnKillfocusResponseEdit )
28  ON_LBN_SELCHANGE( IDC_CANRESP_LST_RESPONSES, OnLbnSelchangeResponselist )
29  ON_LBN_SETFOCUS( IDC_CANRESP_LST_RESPONSES, OnLbnSetfocusResponselist )
30  ON_LBN_KILLFOCUS( IDC_CANRESP_LST_RESPONSES, OnLbnKillfocusResponselist )
31  ON_MESSAGE( WM_EVENT( EVENT_FMI_CANNED_RESP_LIST_CHANGED ), OnCannedRespListChanged )
32 END_MESSAGE_MAP()
33 
34 //----------------------------------------------------------------------
38 //----------------------------------------------------------------------
39 CManageCannedResponseDlg::CManageCannedResponseDlg
40  (
41  CWnd * aParent,
42  FmiApplicationLayer & aCom
43  )
44  : CDialog( IDD_CANNED_RESPONSE, aParent )
45  , mCom( aCom )
46  , mSelectedResponseIndex( 0 )
47  , mResponseId( _T("") )
48  , mResponseText( _T("") )
49 {
50 }
51 
52 //----------------------------------------------------------------------
54 //----------------------------------------------------------------------
56 {
57 }
58 
59 //----------------------------------------------------------------------
62 //----------------------------------------------------------------------
64  (
65  CDataExchange * aDataExchange
66  )
67 {
68  CDialog::DoDataExchange( aDataExchange );
69  DDX_Control( aDataExchange, IDC_CANRESP_LST_RESPONSES, mListBox );
70  DDX_LBIndex( aDataExchange, IDC_CANRESP_LST_RESPONSES, mSelectedResponseIndex );
71  DDX_Text( aDataExchange, IDC_CANRESP_EDIT_ID, mResponseId );
72  DDX_Text( aDataExchange, IDC_CANRESP_EDIT_TEXT, mResponseText );
73 }
74 
75 //----------------------------------------------------------------------
81 //----------------------------------------------------------------------
83 {
84  CDialog::OnInitDialog();
85 
86  updateListBox();
87 
88  SetWindowPos( NULL, 700, 350, 0, 0, SWP_NOSIZE | SWP_NOZORDER );
89 
90  return TRUE;
91 } /* OnInitDialog() */
92 
93 //----------------------------------------------------------------------
96 //----------------------------------------------------------------------
98 {
99  CString listItem;
100 
101  //must keep track of where the list was scrolled to
102  //since we reset content we must reinitialize these
103  int selectedIndex = mListBox.GetCurSel();
104  int topIndex = mListBox.GetTopIndex();
105 
106  //reset content and then add current canned responses
107  mListBox.ResetContent();
108  FileBackedMap<ClientListItem>::const_iterator iter = mCom.mCannedResponses.begin();
109  for( ; iter != mCom.mCannedResponses.end(); iter++ )
110  {
111  if( iter->second.isValid() )
112  {
113  listItem.Format( _T("%d - %s"), iter->first, iter->second.getCurrentName() );
114  mListBox.AddString( listItem );
115  }
116  }
117  //reset scroll and selection
118  mListBox.SetCurSel( selectedIndex );
119  mListBox.SetTopIndex( topIndex );
120 } /* updateListBox() */
121 
122 //----------------------------------------------------------------------
126 //----------------------------------------------------------------------
128 {
129  UpdateData( TRUE );
130  if( mSelectedResponseIndex >= 0 )
131  {
132  mCom.sendDeleteCannedResponseRequest( mCom.mCannedResponses.getKeyAt( mSelectedResponseIndex) );
133  }
134 } /* OnBnClickedDelete() */
135 
136 //----------------------------------------------------------------------
140 //----------------------------------------------------------------------
142 {
143  updateListBox();
144  return 0;
145 } /* OnCannedRespListChanged() */
146 
147 //----------------------------------------------------------------------
152 //----------------------------------------------------------------------
154 {
155  UpdateData( TRUE );
156  uint32 responseId = _ttoi( mResponseId.GetBuffer() );
157 
158  mCom.sendCannedResponse( responseId, mResponseText );
159 } /* OnBnClickedSend() */
160 
161 //----------------------------------------------------------------------
165 //----------------------------------------------------------------------
167 {
168  UpdateData( TRUE );
169  if( mResponseText != "" && mResponseId != "" )
170  {
171  GetDlgItem( IDC_CANRESP_BTN_SEND )->EnableWindow( TRUE );
172  }
173  else
174  {
175  GetDlgItem( IDC_CANRESP_BTN_SEND )->EnableWindow( FALSE );
176  }
177 } /* OnEnChangeRspBoxes() */
178 
179 
180 //----------------------------------------------------------------------
185 //----------------------------------------------------------------------
187 {
188  CListBox * responseListBox = (CListBox*) GetDlgItem( IDC_CANRESP_LST_RESPONSES );
189  int selectedIndex = responseListBox->GetCurSel();
190 
191  if( selectedIndex >= 0 && selectedIndex < responseListBox->GetCount() )
192  {
193  int index = 0;
194  CString itemText;
195  responseListBox->GetText( selectedIndex, itemText );
196  index = itemText.Find( _T("-"), 0 );
197  if( index > 0 )
198  {
199  TCHAR * itemBuffer = itemText.GetBuffer();
200 
201  TCHAR * responseId = new TCHAR[index];
202  memcpy( responseId, itemBuffer, ( index - 1 ) * sizeof( TCHAR ) );
203  responseId[index-1] = '\0';
204  mResponseId.Format( _T("%s"), responseId );
205 
206  int responseTextLength = itemText.GetLength()-index-1;
207  TCHAR * responseText = new TCHAR[responseTextLength];
208  memcpy( responseText, &itemBuffer[index + 2], ( responseTextLength - 1 ) * sizeof( TCHAR ) );
209  responseText[responseTextLength - 1] = '\0';
210  mResponseText.Format( _T("%s"), responseText );
211 
212  itemText.ReleaseBuffer();
213  delete[] responseId;
214  delete[] responseText;
215  UpdateData( FALSE );
216  }
217  }
218 
219  responseListBox->SetCurSel( selectedIndex );
220 }
221 
222 
223 //----------------------------------------------------------------------
226 //----------------------------------------------------------------------
228 {
229  DestroyWindow();
230  //not modal so don't call OnOK()
231 } /* OnBnClickedOk */
232 
233 //----------------------------------------------------------------------
236 //----------------------------------------------------------------------
238 {
239  DestroyWindow();
240 } /* OnCancel */
241 
242 //----------------------------------------------------------------------
249 //----------------------------------------------------------------------
251 {
252  CDialog::PostNcDestroy();
254 } /* PostNcDestroy() */
255 
256 //----------------------------------------------------------------------
261 //----------------------------------------------------------------------
263 {
264  SendMessage( DM_SETDEFID, IDC_CANRESP_BTN_SEND );
265 } /* OnEnSetfocusResponseEdit */
266 
267 //----------------------------------------------------------------------
272 //----------------------------------------------------------------------
274 {
275  SendMessage( DM_SETDEFID, IDOK );
276 } /* OnEnKillfocusResponseEdit */
277 
278 //----------------------------------------------------------------------
282 //----------------------------------------------------------------------
284 {
285  SendMessage( DM_SETDEFID, IDC_CANRESP_BTN_DELETE );
286 } /* OnLbnSetfocusResponselist */
287 
288 //----------------------------------------------------------------------
292 //----------------------------------------------------------------------
294 {
295  SendMessage( DM_SETDEFID, IDOK );
296 } /* OnLbnKillfocusResponselist */
afx_msg void OnLbnSelchangeResponselist()
Selection Changed handler for the Response List box.
const_iterator begin()
Iterator positioned at the first element in the map.
Definition: FileBackedMap.h:59
afx_msg void OnCancel()
Handler for the Cancel action.
Dialog allowing the user to manage canned responses.
#define IDC_CANRESP_BTN_DELETE
Definition: resource.h:163
afx_msg void OnLbnKillfocusResponselist()
Handles the kill focus event for the Response List box.
STL namespace.
const_iterator end()
Iterator positioned after the last element in the map.
Definition: FileBackedMap.h:67
#define IDC_CANRESP_BTN_SEND
Definition: resource.h:164
#define FALSE
Definition: garmin_types.h:46
void PostNcDestroy()
Called by MFC after the window has been destroyed; performs final termination activities.
#define TRUE
Definition: garmin_types.h:45
afx_msg void OnBnClickedSend()
Button handler for the Send button.
afx_msg void OnEnSetfocusResponseEdit()
Handles the set focus event for the Response ID and Response Text edit boxes.
#define IDD_CANNED_RESPONSE
Definition: resource.h:18
virtual void DoDataExchange(CDataExchange *aDataExchange)
Perform dialog data exchange and validation.
afx_msg void OnEnChangeRspBoxes()
Edit handler for the Response ID and Response Text boxes.
Serial communication controller for Garmin and FMI packets.
void updateListBox()
Update the canned response list box from the canned response map owned by FmiApplicationLayer.
#define IDC_CANRESP_LST_RESPONSES
Definition: resource.h:172
afx_msg void OnBnClickedDelete()
Button handler for the Delete button.
afx_msg void OnEnKillfocusResponseEdit()
Handles the kill focus event for the Response ID and Response Text edit boxes.
virtual ~CManageCannedResponseDlg()
Destructor.
afx_msg void OnLbnSetfocusResponselist()
Handles the set focus event for the Response List.
Map whose contents are also saved to a file.
Definition: FileBackedMap.h:32
BOOL OnInitDialog()
This function is called when the window is created.
#define WM_EVENT(_event)
Translation from an application event to the corresponding Windows message.
afx_msg void OnBnClickedOk()
Button handler for the OK button.
unsigned long int uint32
32-bit unsigned integer
Definition: garmin_types.h:66
#define IDC_CANRESP_EDIT_TEXT
Definition: resource.h:166
afx_msg LPARAM OnCannedRespListChanged(WPARAM, LPARAM)
Handler for FMI_EVENT_CANNED_RESP_LIST_CHANGED event.
#define IDC_CANRESP_EDIT_ID
Definition: resource.h:165
static void post(EventId aEventId, uint32 aEventData=0, void *aEventDataPtr=NULL, BOOL handleNow=FALSE)
Posts a message to all windows that have registered to get events.
Definition: Event.cpp:67