1 // ModifyVariableDlg.cpp : implementation file
6 #include "modifyvariabledlg.h"
12 static char THIS_FILE[] = __FILE__;
15 #define NO_RESET_FOCUS 0
19 /////////////////////////////////////////////////////////////////////////////
20 // CModifyVariableDlg dialog
22 CModifyVariableDlg::CModifyVariableDlg(CWnd* pParent /*=NULL*/)
23 : CDialog(CModifyVariableDlg::IDD, pParent)
25 //{{AFX_DATA_INIT(CModifyVariableDlg)
26 m_default_value = _T("");
27 m_cur_variable_name = _T("");
32 void CModifyVariableDlg::DoDataExchange(CDataExchange* pDX)
34 CDialog::DoDataExchange(pDX);
35 //{{AFX_DATA_MAP(CModifyVariableDlg)
36 DDX_Text(pDX, IDC_MODIFY_DEFAULT_VALUE, m_default_value);
37 DDV_MaxChars(pDX, m_default_value, 31);
38 DDX_CBString(pDX, IDC_MODIFY_VARIABLE_NAME, m_cur_variable_name);
39 DDV_MaxChars(pDX, m_cur_variable_name, 31);
44 BEGIN_MESSAGE_MAP(CModifyVariableDlg, CDialog)
45 //{{AFX_MSG_MAP(CModifyVariableDlg)
46 ON_BN_CLICKED(ID_DELETE_VARIABLE, OnDeleteVariable)
47 ON_BN_CLICKED(IDC_TYPE_STRING, OnTypeString)
48 ON_BN_CLICKED(IDC_TYPE_NUMBER, OnTypeNumber)
49 ON_CBN_SELCHANGE(IDC_MODIFY_VARIABLE_NAME, OnSelchangeModifyVariableName)
50 ON_CBN_EDITCHANGE(IDC_MODIFY_VARIABLE_NAME, OnEditchangeModifyVariableName)
51 ON_EN_KILLFOCUS(IDC_MODIFY_DEFAULT_VALUE, OnKillfocusModifyDefaultValue)
52 ON_CBN_DROPDOWN(IDC_MODIFY_VARIABLE_NAME, OnDropdownModifyVariableName)
56 /////////////////////////////////////////////////////////////////////////////
57 // CModifyVariableDlg message handlers
59 // Maybe delete variable
60 void CModifyVariableDlg::OnDeleteVariable()
65 // Check for name change
66 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
67 cbox->GetWindowText(temp_name);
69 // Can't delete. Name has been changed
70 if ( stricmp(Sexp_variables[get_sexp_var_index()].variable_name, temp_name) ) {
71 MessageBox("Can not delete variable. Name has been changed.");
75 int num_counts = m_p_sexp_tree->get_variable_count(Sexp_variables[get_sexp_var_index()].variable_name);
78 sprintf(buffer, "Can not delete variable. Used in %d location(s).", num_counts);
83 // maybe delete variable
84 rval = MessageBox("This will permanantly delete the variable. Do you want to continue?", NULL, MB_OKCANCEL);
87 // delete variable and exit
89 // next statement does UpdataData(TRUE);
95 void CModifyVariableDlg::OnTypeString()
97 // check if type actually modified
98 if (m_type_number == true) {
100 // Don't allow type change if in use.
101 int num_counts = m_p_sexp_tree->get_variable_count(Sexp_variables[get_sexp_var_index()].variable_name);
102 if (num_counts > 0) {
104 sprintf(buffer, "Can not modify variable type. Used in %d location(s).", num_counts);
107 m_type_number = true;
108 m_modified_type = false;
113 // keep track if type is really changed
114 if (m_modified_type == true) {
115 m_modified_type = false;
117 m_modified_type = true;
120 m_type_number = false;
124 // Set type to number
125 void CModifyVariableDlg::OnTypeNumber()
127 // check if type actually modified
128 if (m_type_number == false) {
130 // Don't allow type change if in use.
131 int num_counts = m_p_sexp_tree->get_variable_count(Sexp_variables[get_sexp_var_index()].variable_name);
132 if (num_counts > 0) {
134 sprintf(buffer, "Can not modify variable type. Used in %d location(s).", num_counts);
137 m_type_number = false;
138 m_modified_type = false;
143 // keep track if type is really changed
144 if (m_modified_type == true) {
145 m_modified_type = false;
147 m_modified_type = true;
150 m_type_number = true;
154 #pragma warning (push)
155 #pragma warning( disable : 4800 ) // Disable "forcing value to bool 'true' or 'false'"
157 void CModifyVariableDlg::OnSelchangeModifyVariableName()
159 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
161 // get index of current selection
162 int index = cbox->GetCurSel();
164 // check an item was actually selected, and not outside the box
165 if (index == CB_ERR) {
169 // check if another has been modified
170 if (m_modified_type || m_modified_name || m_modified_value) {
172 // Don't send message if changing to self
173 if (index != m_combo_last_modified_index) {
174 MessageBox("Can only modify one variable.");
177 //reset focus to current
178 cbox->SetCurSel(m_combo_last_modified_index);
182 m_combo_last_modified_index = index;
184 // Get index into sexp_variables
185 int sexp_variable_index = get_sexp_var_index();
187 // Set new type for selection
188 m_type_number = (bool) (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NUMBER);
191 // Set new default value for selection
192 if (sexp_variable_index > -1) {
193 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
194 edit->SetWindowText(Sexp_variables[sexp_variable_index].text);
197 #pragma warning (pop)
199 // Check if variable name has changed from Sexp_variables[].varaible name
200 void CModifyVariableDlg::OnEditchangeModifyVariableName()
202 // Do string compare to check for change
205 // Get current variable name
206 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
207 cbox->GetWindowText(temp_name);
209 // Check if variable name is modified
210 if ( strcmp(Sexp_variables[get_sexp_var_index()].variable_name, temp_name) ) {
211 m_modified_name = true;
213 m_modified_name = false;
218 #pragma warning (push)
219 #pragma warning( disable : 4800 ) // Disable "forcing value to bool 'true' or 'false'"
221 BOOL CModifyVariableDlg::OnInitDialog()
223 CDialog::OnInitDialog();
226 // Init combo box and translation table from combo box to sexp_varaibles
227 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
228 cbox->ResetContent();
230 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
231 m_traslate_combo_to_sexp[i] = -1;
234 // initialize list -- Box is set to *not* sort
235 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
236 if (Sexp_variables[i].type & SEXP_VARIABLE_SET) {
237 box_index = cbox->AddString(Sexp_variables[i].variable_name);
240 if ( !((box_index == CB_ERR) || (box_index == CB_ERRSPACE)) ) {
241 m_traslate_combo_to_sexp[box_index] = i;
246 // Exit gracefully if nothing added to combo box
247 if (cbox->GetCount() == 0) {
248 Int3(); // this should not happen
252 int last_modified = 0;
253 // Set current variable
254 if (m_start_index > -1) {
255 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
256 if (m_traslate_combo_to_sexp[i] == m_start_index) {
263 m_combo_last_modified_index = last_modified;
264 cbox->SetCurSel(last_modified);
266 // Set the default value
267 if (m_traslate_combo_to_sexp[last_modified] > -1) {
268 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
269 edit->SetWindowText(Sexp_variables[m_traslate_combo_to_sexp[last_modified]].text);
272 // Set old variable name
273 m_old_var_name = Sexp_variables[m_traslate_combo_to_sexp[last_modified]].variable_name;
276 m_type_number = (Sexp_variables[last_modified].type & SEXP_VARIABLE_NUMBER);
279 // keep track of changes
280 m_modified_name = false;
281 m_modified_value = false;
282 m_modified_type = false;
286 m_data_validated = false;
287 m_var_name_validated = false;
290 return TRUE; // return TRUE unless you set the focus to a control
291 // EXCEPTION: OCX Property Pages should return FALSE
293 #pragma warning (pop)
296 void CModifyVariableDlg::set_variable_type()
299 CButton *button_string = (CButton *) GetDlgItem(IDC_TYPE_STRING);
300 CButton *button_number = (CButton *) GetDlgItem(IDC_TYPE_NUMBER);
303 button_number->SetCheck( m_type_number);
304 button_string->SetCheck(!m_type_number);
307 void CModifyVariableDlg::OnOK()
312 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
313 edit->GetWindowText(temp_data);
316 validate_data(temp_data, RESET_FOCUS);
317 if (m_data_validated) {
318 // Dont get OnKillfocusModifyDefaultValue when ok
319 if (!m_modified_value) {
320 if ( strcmp(Sexp_variables[get_sexp_var_index()].text, temp_data) ) {
321 m_modified_value = true;
325 // validate variable name
326 validate_var_name(RESET_FOCUS);
327 if (m_var_name_validated) {
329 // maybe set m_do_modify -- this is needed. compare with OnCancel()
330 if (m_modified_name || m_modified_value || m_modified_type) {
338 void CModifyVariableDlg::OnKillfocusModifyDefaultValue()
340 // Do string compare to check for change
343 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
344 edit->GetWindowText(temp_data);
346 if ( strcmp(Sexp_variables[get_sexp_var_index()].text, temp_data) ) {
347 m_modified_value = true;
349 m_modified_value = false;
354 // check (1) zero length (2) invalid chars (3) value if numberf
355 void CModifyVariableDlg::validate_data(CString &temp_data, int set_focus)
357 // display invalid data message
358 bool message = false;
359 char message_text[256];
362 int length = strlen(temp_data);
364 strcpy(message_text, "Invalid Default Value");
367 } else if (m_type_number) {
368 // check if string and str(atoi(stri)) are same
369 int temp_num = atoi(temp_data);
370 char buf[TOKEN_LENGTH];
371 sprintf(buf, "%d", temp_num);
373 if ( stricmp(buf, temp_data) ) {
375 strcpy(message_text, "Invalid Default Value");
381 // check for invalid characters
382 int rval = strcspn(temp_data, "@()");
383 if (rval != length) {
385 sprintf(message_text, "Invalid char '%c' in Default Value", temp_data[rval]);
390 m_data_validated = false;
391 MessageBox(message_text);
394 if (set_focus == RESET_FOCUS) {
395 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
401 // string always ok, numbers if no message
402 m_data_validated = !message;
405 // validate variable name
406 // check (1) zero length (2) invalid chars (3) already in use
407 void CModifyVariableDlg::validate_var_name(int set_focus)
410 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
411 cbox->GetWindowText(temp_name);
413 int cur_sel = cbox->GetCurSel();
414 m_old_var_name = Sexp_variables[m_traslate_combo_to_sexp[cur_sel]].variable_name;
416 // display invalid data message
417 bool message = false;
418 char message_text[256];
421 int length = strlen(temp_name);
423 strcpy(message_text, "Invalid Variable Name");
427 // check for invalid characters
428 int rval = strcspn(temp_name, "@()");
429 if (rval != length) {
431 sprintf(message_text, "Invalid char '%c' in Variable Name", temp_name[rval]);
433 int index = get_index_sexp_variable_name(temp_name);
435 // if not a new name and not start name
436 if ( (index != -1) && (index != m_traslate_combo_to_sexp[m_combo_last_modified_index]) ) {
438 strcpy(message_text, "Variable Name already in use");
445 MessageBox(message_text);
448 if (set_focus == RESET_FOCUS) {
453 // set var_name_validated
454 m_var_name_validated = !message;
458 int CModifyVariableDlg::get_sexp_var_index()
460 int index = m_traslate_combo_to_sexp[m_combo_last_modified_index];
461 Assert( (index >= 0) && (index < MAX_SEXP_VARIABLES) );
466 // Reset text in drop down list
467 void CModifyVariableDlg::OnDropdownModifyVariableName()
471 // Get current variable name
472 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
473 cbox->GetWindowText(temp_name);
475 // Reset combo box text
477 rval = cbox->InsertString(m_combo_last_modified_index, temp_name);
478 if ( (rval == CB_ERR) || (rval == CB_ERRSPACE) ) {
479 AfxMessageBox("An internal error has occured.");
482 cbox->DeleteString(m_combo_last_modified_index+1);
484 cbox->SetCurSel(m_combo_last_modified_index);