]> icculus.org git repositories - taylor/freespace2.git/blob - src/fred2/addvariabledlg.cpp
Initial revision
[taylor/freespace2.git] / src / fred2 / addvariabledlg.cpp
1 // AddVariableDlg.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "fred.h"
6 #include "addvariabledlg.h"
7 #include "sexp.h"
8
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
14
15 #define NO_RESET_FOCUS  0
16 #define RESET_FOCUS             1
17
18 /////////////////////////////////////////////////////////////////////////////
19 // CAddVariableDlg dialog
20
21
22 CAddVariableDlg::CAddVariableDlg(CWnd* pParent /*=NULL*/)
23         : CDialog(CAddVariableDlg::IDD, pParent)
24 {
25         //{{AFX_DATA_INIT(CAddVariableDlg)
26         m_default_value = _T("");
27         m_variable_name = _T("");
28         //}}AFX_DATA_INIT
29 }
30
31
32 void CAddVariableDlg::DoDataExchange(CDataExchange* pDX)
33 {
34         CDialog::DoDataExchange(pDX);
35         //{{AFX_DATA_MAP(CAddVariableDlg)
36         DDX_Text(pDX, IDC_ADD_VARIABLE_DEFAULT_VALUE, m_default_value);
37         DDV_MaxChars(pDX, m_default_value, 31);
38         DDX_Text(pDX, IDC_ADD_VARIABLE_NAME, m_variable_name);
39         DDV_MaxChars(pDX, m_variable_name, 31);
40         //}}AFX_DATA_MAP
41 }
42
43
44 BEGIN_MESSAGE_MAP(CAddVariableDlg, CDialog)
45         //{{AFX_MSG_MAP(CAddVariableDlg)
46         ON_BN_CLICKED(IDC_TYPE_NUMBER, OnTypeNumber)
47         ON_BN_CLICKED(IDC_TYPE_STRING, OnTypeString)
48         //}}AFX_MSG_MAP
49 END_MESSAGE_MAP()
50
51 /////////////////////////////////////////////////////////////////////////////
52 // CAddVariableDlg message handlers
53
54 void CAddVariableDlg::OnOK() 
55 {
56         // validation name
57         validate_variable_name(RESET_FOCUS);
58
59         // validate data
60         if ( m_name_validated ) {
61                 validate_data(RESET_FOCUS);
62         }
63
64         // both ok, then store results
65         if ( m_name_validated && m_data_validated ) {
66 //              int sexp_add_variable(char *text, char*, int type);
67 //              char temp_name[32];
68 //              char temp_value[32];
69 //              strcpy(temp_name, m_variable_name);
70 //              strcpy(temp_value, m_default_value);
71                 // SEXP_VARIABLE_NUMBER SEXP_VARIABLE_STRING
72 //              int type;
73 //
74 //              if (m_type_number) {
75 //                      type = SEXP_VARIABLE_NUMBER;
76 //              } else {
77 //                      type = SEXP_VARIABLE_STRING;
78 //              }
79
80 //              m_sexp_var_index = sexp_add_variable(temp_value, temp_name, type);
81 //              this get done for free CDialog::OnOk() UpdateData(TRUE);
82                 m_create = true;
83
84                 CDialog::OnOK();
85         }
86 }
87
88 BOOL CAddVariableDlg::OnInitDialog() 
89 {
90         CDialog::OnInitDialog();
91         
92         // TODO: Add extra initialization here
93         m_variable_name = "<Variable Name>";
94         m_default_value = "<Default Value>";
95
96         // Set variable type to number
97         m_type_number = true;
98         set_variable_type();
99
100         m_name_validated = false;
101         m_data_validated = false;
102         m_create = false;
103
104         // Send default name and values into dialog box
105         UpdateData(FALSE);
106         
107         return TRUE;  // return TRUE unless you set the focus to a control
108                       // EXCEPTION: OCX Property Pages should return FALSE
109 }
110
111 bool is_sexp_variable_name(const char* temp_name)
112 {
113         for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
114                 if (Sexp_variables[i].type & SEXP_VARIABLE_SET) {
115                         if ( !strcmp(Sexp_variables[i].text, temp_name) ) {
116                                 return false;
117                         }
118                 }
119         }
120         // name not found
121         return true;
122 }
123
124
125 // Check variable name (1) changed (2) > 0 length (3) does not already exist
126 void CAddVariableDlg::validate_variable_name(int set_focus)
127 {
128         CString temp_name;
129
130         CEdit *edit = (CEdit *) GetDlgItem(IDC_ADD_VARIABLE_NAME);
131         edit->GetWindowText(temp_name);
132
133         // Check if any change and not already in list
134         if ( stricmp(temp_name, "<Variable Name>") ) {
135                 if ( (strlen(temp_name) > 0) && (get_index_sexp_variable_name(LPCTSTR(temp_name)) == -1) ) { //not already in list and length > 0 {
136                         m_name_validated = true;
137                 } else {
138                         // conflicting variable name
139                         if (strlen(temp_name) == 0) {
140                                 edit->SetWindowText("<Variable Name>");
141                         }
142                         m_name_validated = false;
143                         if (set_focus == RESET_FOCUS) {
144                                 MessageBox("Conflicting variable name");
145                                 edit->SetFocus();
146                                 edit->SetSel(0, -1);
147                         }
148                 }
149         } else {
150                 // name unchanged from default
151                 m_name_validated = false;
152                 if (set_focus == RESET_FOCUS) {
153                         MessageBox("Invalid variable name");
154                         edit->SetFocus();
155                         edit->SetSel(0, -1);
156                 }
157         }
158
159 }
160
161 void CAddVariableDlg::validate_data(int set_focus)
162 {
163         CString temp_data;
164
165         CEdit *edit = (CEdit *) GetDlgItem(IDC_ADD_VARIABLE_DEFAULT_VALUE);
166         edit->GetWindowText(temp_data);
167
168         // check for 0 string length
169         if (strlen(temp_data) == 0) {
170                 m_data_validated = false;
171         } else {
172                 if (m_type_number) {
173                         // verify valid number
174                         int temp_num = atoi(temp_data);
175                         char buf[TOKEN_LENGTH];
176                         sprintf(buf, "%d", temp_num);
177
178                         if ( stricmp(buf, temp_data) ) {
179                                 m_data_validated = false;
180                         } else {
181                                 m_data_validated = true;
182                         }
183                 } else {
184                         m_data_validated = true;
185                 }
186         }
187
188         // Display message and reset focus
189         if ( (!m_data_validated) && (set_focus == RESET_FOCUS) ) {
190                 MessageBox("Invalid Default Value.");
191                 edit->SetFocus();
192                 edit->SetSel(0, -1);
193         }
194 }
195
196 // Set type to number
197 void CAddVariableDlg::OnTypeNumber() 
198 {
199         m_type_number = true;
200         set_variable_type();
201 }
202
203 // Set type to string
204 void CAddVariableDlg::OnTypeString() 
205 {
206         m_type_number = false;
207         set_variable_type();
208 }
209
210 // Set type check boxes
211 void CAddVariableDlg::set_variable_type()
212 {
213
214         CButton *button_string = (CButton *) GetDlgItem(IDC_TYPE_STRING);
215         CButton *button_number = (CButton *) GetDlgItem(IDC_TYPE_NUMBER);
216
217         button_number->SetCheck( m_type_number);
218         button_string->SetCheck(!m_type_number);
219 }