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