2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
9 // ModifyVariableDlg.cpp : implementation file
14 #include "modifyvariabledlg.h"
20 static char THIS_FILE[] = __FILE__;
23 #define NO_RESET_FOCUS 0
27 /////////////////////////////////////////////////////////////////////////////
28 // CModifyVariableDlg dialog
30 CModifyVariableDlg::CModifyVariableDlg(CWnd* pParent /*=NULL*/)
31 : CDialog(CModifyVariableDlg::IDD, pParent)
33 //{{AFX_DATA_INIT(CModifyVariableDlg)
34 m_default_value = _T("");
35 m_cur_variable_name = _T("");
40 void CModifyVariableDlg::DoDataExchange(CDataExchange* pDX)
42 CDialog::DoDataExchange(pDX);
43 //{{AFX_DATA_MAP(CModifyVariableDlg)
44 DDX_Text(pDX, IDC_MODIFY_DEFAULT_VALUE, m_default_value);
45 DDV_MaxChars(pDX, m_default_value, 31);
46 DDX_CBString(pDX, IDC_MODIFY_VARIABLE_NAME, m_cur_variable_name);
47 DDV_MaxChars(pDX, m_cur_variable_name, 31);
52 BEGIN_MESSAGE_MAP(CModifyVariableDlg, CDialog)
53 //{{AFX_MSG_MAP(CModifyVariableDlg)
54 ON_BN_CLICKED(ID_DELETE_VARIABLE, OnDeleteVariable)
55 ON_BN_CLICKED(IDC_TYPE_STRING, OnTypeString)
56 ON_BN_CLICKED(IDC_TYPE_NUMBER, OnTypeNumber)
57 ON_CBN_SELCHANGE(IDC_MODIFY_VARIABLE_NAME, OnSelchangeModifyVariableName)
58 ON_CBN_EDITCHANGE(IDC_MODIFY_VARIABLE_NAME, OnEditchangeModifyVariableName)
59 ON_EN_KILLFOCUS(IDC_MODIFY_DEFAULT_VALUE, OnKillfocusModifyDefaultValue)
60 ON_CBN_DROPDOWN(IDC_MODIFY_VARIABLE_NAME, OnDropdownModifyVariableName)
64 /////////////////////////////////////////////////////////////////////////////
65 // CModifyVariableDlg message handlers
67 // Maybe delete variable
68 void CModifyVariableDlg::OnDeleteVariable()
73 // Check for name change
74 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
75 cbox->GetWindowText(temp_name);
77 // Can't delete. Name has been changed
78 if ( stricmp(Sexp_variables[get_sexp_var_index()].variable_name, temp_name) ) {
79 MessageBox("Can not delete variable. Name has been changed.");
83 int num_counts = m_p_sexp_tree->get_variable_count(Sexp_variables[get_sexp_var_index()].variable_name);
86 sprintf(buffer, "Can not delete variable. Used in %d location(s).", num_counts);
91 // maybe delete variable
92 rval = MessageBox("This will permanantly delete the variable. Do you want to continue?", NULL, MB_OKCANCEL);
95 // delete variable and exit
97 // next statement does UpdataData(TRUE);
102 // Set type to string
103 void CModifyVariableDlg::OnTypeString()
105 // check if type actually modified
106 if (m_type_number == true) {
108 // Don't allow type change if in use.
109 int num_counts = m_p_sexp_tree->get_variable_count(Sexp_variables[get_sexp_var_index()].variable_name);
110 if (num_counts > 0) {
112 sprintf(buffer, "Can not modify variable type. Used in %d location(s).", num_counts);
115 m_type_number = true;
116 m_modified_type = false;
121 // keep track if type is really changed
122 if (m_modified_type == true) {
123 m_modified_type = false;
125 m_modified_type = true;
128 m_type_number = false;
132 // Set type to number
133 void CModifyVariableDlg::OnTypeNumber()
135 // check if type actually modified
136 if (m_type_number == false) {
138 // Don't allow type change if in use.
139 int num_counts = m_p_sexp_tree->get_variable_count(Sexp_variables[get_sexp_var_index()].variable_name);
140 if (num_counts > 0) {
142 sprintf(buffer, "Can not modify variable type. Used in %d location(s).", num_counts);
145 m_type_number = false;
146 m_modified_type = false;
151 // keep track if type is really changed
152 if (m_modified_type == true) {
153 m_modified_type = false;
155 m_modified_type = true;
158 m_type_number = true;
162 #pragma warning (push)
163 #pragma warning( disable : 4800 ) // Disable "forcing value to bool 'true' or 'false'"
165 void CModifyVariableDlg::OnSelchangeModifyVariableName()
167 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
169 // get index of current selection
170 int index = cbox->GetCurSel();
172 // check an item was actually selected, and not outside the box
173 if (index == CB_ERR) {
177 // check if another has been modified
178 if (m_modified_type || m_modified_name || m_modified_value) {
180 // Don't send message if changing to self
181 if (index != m_combo_last_modified_index) {
182 MessageBox("Can only modify one variable.");
185 //reset focus to current
186 cbox->SetCurSel(m_combo_last_modified_index);
190 m_combo_last_modified_index = index;
192 // Get index into sexp_variables
193 int sexp_variable_index = get_sexp_var_index();
195 // Set new type for selection
196 m_type_number = (bool) (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NUMBER);
199 // Set new default value for selection
200 if (sexp_variable_index > -1) {
201 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
202 edit->SetWindowText(Sexp_variables[sexp_variable_index].text);
205 #pragma warning (pop)
207 // Check if variable name has changed from Sexp_variables[].varaible name
208 void CModifyVariableDlg::OnEditchangeModifyVariableName()
210 // Do string compare to check for change
213 // Get current variable name
214 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
215 cbox->GetWindowText(temp_name);
217 // Check if variable name is modified
218 if ( strcmp(Sexp_variables[get_sexp_var_index()].variable_name, temp_name) ) {
219 m_modified_name = true;
221 m_modified_name = false;
226 #pragma warning (push)
227 #pragma warning( disable : 4800 ) // Disable "forcing value to bool 'true' or 'false'"
229 BOOL CModifyVariableDlg::OnInitDialog()
231 CDialog::OnInitDialog();
234 // Init combo box and translation table from combo box to sexp_varaibles
235 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
236 cbox->ResetContent();
238 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
239 m_traslate_combo_to_sexp[i] = -1;
242 // initialize list -- Box is set to *not* sort
243 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
244 if (Sexp_variables[i].type & SEXP_VARIABLE_SET) {
245 box_index = cbox->AddString(Sexp_variables[i].variable_name);
248 if ( !((box_index == CB_ERR) || (box_index == CB_ERRSPACE)) ) {
249 m_traslate_combo_to_sexp[box_index] = i;
254 // Exit gracefully if nothing added to combo box
255 if (cbox->GetCount() == 0) {
256 Int3(); // this should not happen
260 int last_modified = 0;
261 // Set current variable
262 if (m_start_index > -1) {
263 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
264 if (m_traslate_combo_to_sexp[i] == m_start_index) {
271 m_combo_last_modified_index = last_modified;
272 cbox->SetCurSel(last_modified);
274 // Set the default value
275 if (m_traslate_combo_to_sexp[last_modified] > -1) {
276 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
277 edit->SetWindowText(Sexp_variables[m_traslate_combo_to_sexp[last_modified]].text);
280 // Set old variable name
281 m_old_var_name = Sexp_variables[m_traslate_combo_to_sexp[last_modified]].variable_name;
284 m_type_number = (Sexp_variables[last_modified].type & SEXP_VARIABLE_NUMBER);
287 // keep track of changes
288 m_modified_name = false;
289 m_modified_value = false;
290 m_modified_type = false;
294 m_data_validated = false;
295 m_var_name_validated = false;
298 return TRUE; // return TRUE unless you set the focus to a control
299 // EXCEPTION: OCX Property Pages should return FALSE
301 #pragma warning (pop)
304 void CModifyVariableDlg::set_variable_type()
307 CButton *button_string = (CButton *) GetDlgItem(IDC_TYPE_STRING);
308 CButton *button_number = (CButton *) GetDlgItem(IDC_TYPE_NUMBER);
311 button_number->SetCheck( m_type_number);
312 button_string->SetCheck(!m_type_number);
315 void CModifyVariableDlg::OnOK()
320 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
321 edit->GetWindowText(temp_data);
324 validate_data(temp_data, RESET_FOCUS);
325 if (m_data_validated) {
326 // Dont get OnKillfocusModifyDefaultValue when ok
327 if (!m_modified_value) {
328 if ( strcmp(Sexp_variables[get_sexp_var_index()].text, temp_data) ) {
329 m_modified_value = true;
333 // validate variable name
334 validate_var_name(RESET_FOCUS);
335 if (m_var_name_validated) {
337 // maybe set m_do_modify -- this is needed. compare with OnCancel()
338 if (m_modified_name || m_modified_value || m_modified_type) {
346 void CModifyVariableDlg::OnKillfocusModifyDefaultValue()
348 // Do string compare to check for change
351 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
352 edit->GetWindowText(temp_data);
354 if ( strcmp(Sexp_variables[get_sexp_var_index()].text, temp_data) ) {
355 m_modified_value = true;
357 m_modified_value = false;
362 // check (1) zero length (2) invalid chars (3) value if numberf
363 void CModifyVariableDlg::validate_data(CString &temp_data, int set_focus)
365 // display invalid data message
366 bool message = false;
367 char message_text[256];
370 int length = strlen(temp_data);
372 strcpy(message_text, "Invalid Default Value");
375 } else if (m_type_number) {
376 // check if string and str(atoi(stri)) are same
377 int temp_num = atoi(temp_data);
378 char buf[TOKEN_LENGTH];
379 sprintf(buf, "%d", temp_num);
381 if ( stricmp(buf, temp_data) ) {
383 strcpy(message_text, "Invalid Default Value");
389 // check for invalid characters
390 int rval = strcspn(temp_data, "@()");
391 if (rval != length) {
393 sprintf(message_text, "Invalid char '%c' in Default Value", temp_data[rval]);
398 m_data_validated = false;
399 MessageBox(message_text);
402 if (set_focus == RESET_FOCUS) {
403 CEdit *edit = (CEdit *) GetDlgItem(IDC_MODIFY_DEFAULT_VALUE);
409 // string always ok, numbers if no message
410 m_data_validated = !message;
413 // validate variable name
414 // check (1) zero length (2) invalid chars (3) already in use
415 void CModifyVariableDlg::validate_var_name(int set_focus)
418 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
419 cbox->GetWindowText(temp_name);
421 int cur_sel = cbox->GetCurSel();
422 m_old_var_name = Sexp_variables[m_traslate_combo_to_sexp[cur_sel]].variable_name;
424 // display invalid data message
425 bool message = false;
426 char message_text[256];
429 int length = strlen(temp_name);
431 strcpy(message_text, "Invalid Variable Name");
435 // check for invalid characters
436 int rval = strcspn(temp_name, "@()");
437 if (rval != length) {
439 sprintf(message_text, "Invalid char '%c' in Variable Name", temp_name[rval]);
441 int index = get_index_sexp_variable_name(temp_name);
443 // if not a new name and not start name
444 if ( (index != -1) && (index != m_traslate_combo_to_sexp[m_combo_last_modified_index]) ) {
446 strcpy(message_text, "Variable Name already in use");
453 MessageBox(message_text);
456 if (set_focus == RESET_FOCUS) {
461 // set var_name_validated
462 m_var_name_validated = !message;
466 int CModifyVariableDlg::get_sexp_var_index()
468 int index = m_traslate_combo_to_sexp[m_combo_last_modified_index];
469 SDL_assert( (index >= 0) && (index < MAX_SEXP_VARIABLES) );
474 // Reset text in drop down list
475 void CModifyVariableDlg::OnDropdownModifyVariableName()
479 // Get current variable name
480 CComboBox *cbox = (CComboBox *) GetDlgItem(IDC_MODIFY_VARIABLE_NAME);
481 cbox->GetWindowText(temp_name);
483 // Reset combo box text
485 rval = cbox->InsertString(m_combo_last_modified_index, temp_name);
486 if ( (rval == CB_ERR) || (rval == CB_ERRSPACE) ) {
487 AfxMessageBox("An internal error has occured.");
490 cbox->DeleteString(m_combo_last_modified_index+1);
492 cbox->SetCurSel(m_combo_last_modified_index);