| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | // DallasUserTypesDlg.cpp : implementation file |
| 20 | // |
| 21 | |
| 22 | #include "stdafx.h" |
| 23 | #include "cfile.h" |
| 24 | |
| 25 | #ifdef NEWEDITOR |
| 26 | #include "NewEditor.h" |
| 27 | #else |
| 28 | #include "editor.h" |
| 29 | #endif |
| 30 | |
| 31 | #include "DallasMainDlg.h" |
| 32 | #include "DallasGenericPromptDlg.h" |
| 33 | #include "DallasUserTypesDlg.h" |
| 34 | |
| 35 | CDallasMainDlg *GetDallasDialogPtr(void); |
| 36 | |
| 37 | #ifdef _DEBUG |
| 38 | #define new DEBUG_NEW |
| 39 | #undef THIS_FILE |
| 40 | static char THIS_FILE[] = __FILE__; |
| 41 | #endif |
| 42 | |
| 43 | CString last_type_selected; |
| 44 | |
| 45 | ///////////////////////////////////////////////////////////////////////////// |
| 46 | // CDallasUserTypesDlg dialog |
| 47 | |
| 48 | CDallasUserTypesDlg::CDallasUserTypesDlg(CWnd *pParent /*=NULL*/) : CDialog(CDallasUserTypesDlg::IDD, pParent) { |
| 49 | //{{AFX_DATA_INIT(CDallasUserTypesDlg) |
| 50 | // NOTE: the ClassWizard will add member initialization here |
| 51 | //}}AFX_DATA_INIT |
| 52 | } |
| 53 | |
| 54 | void CDallasUserTypesDlg::DoDataExchange(CDataExchange *pDX) { |
| 55 | CDialog::DoDataExchange(pDX); |
| 56 | //{{AFX_DATA_MAP(CDallasUserTypesDlg) |
| 57 | DDX_Control(pDX, IDC_VALUES_LIST, m_ValuesListBox); |
| 58 | DDX_Control(pDX, IDC_UTYPE_COMBO, m_UserTypeCombo); |
| 59 | //}}AFX_DATA_MAP |
| 60 | } |
| 61 | |
| 62 | BEGIN_MESSAGE_MAP(CDallasUserTypesDlg, CDialog) |
| 63 | //{{AFX_MSG_MAP(CDallasUserTypesDlg) |
| 64 | ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton) |
| 65 | ON_BN_CLICKED(IDC_CHANGE_BUTTON, OnChangeButton) |
| 66 | ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton) |
| 67 | ON_CBN_SELCHANGE(IDC_UTYPE_COMBO, OnSelchangeUtypeCombo) |
| 68 | ON_LBN_DBLCLK(IDC_VALUES_LIST, OnDblclkValuesList) |
| 69 | //}}AFX_MSG_MAP |
| 70 | END_MESSAGE_MAP() |
| 71 | |
| 72 | ///////////////////////////////////////////////////////////////////////////// |
| 73 | // CDallasUserTypesDlg message handlers |
| 74 | |
| 75 | BOOL CDallasUserTypesDlg::OnInitDialog() { |
| 76 | CDialog::OnInitDialog(); |
| 77 | |
| 78 | CDallasMainDlg *m_DallasModelessDlgPtr; |
| 79 | m_DallasModelessDlgPtr = GetDallasDialogPtr(); |
| 80 | |
| 81 | // If Dallas is up, fill in the user type and value boxes |
| 82 | if (m_DallasModelessDlgPtr != NULL) { |
| 83 | m_DallasModelessDlgPtr->FillUserTypeBox(&m_UserTypeCombo); |
| 84 | |
| 85 | if (!last_type_selected.IsEmpty()) { |
| 86 | m_UserTypeCombo.SelectString(-1, last_type_selected.GetBuffer(0)); |
| 87 | } |
| 88 | |
| 89 | OnSelchangeUtypeCombo(); |
| 90 | } |
| 91 | |
| 92 | return TRUE; // return TRUE unless you set the focus to a control |
| 93 | // EXCEPTION: OCX Property Pages should return FALSE |
| 94 | } |
| 95 | |
| 96 | void CDallasUserTypesDlg::OnAddButton() { |
| 97 | CDallasGenericPromptDlg dlg; |
| 98 | int index; |
| 99 | CString name; |
| 100 | |
| 101 | CDallasMainDlg *m_DallasModelessDlgPtr; |
| 102 | m_DallasModelessDlgPtr = GetDallasDialogPtr(); |
| 103 | |
| 104 | if (m_DallasModelessDlgPtr == NULL) |
| 105 | return; |
| 106 | |
| 107 | // Get the name of the user type |
| 108 | index = m_UserTypeCombo.GetCurSel(); |
| 109 | if (index == CB_ERR) |
| 110 | return; |
| 111 | |
| 112 | m_UserTypeCombo.GetLBText(index, name); |
| 113 | if (name.IsEmpty()) |
| 114 | return; |
| 115 | |
| 116 | // Display the prompt dialog |
| 117 | dlg.m_DialogTitle = "Value Name Prompt"; |
| 118 | dlg.m_PromptText = "Enter a name for the new value:"; |
| 119 | dlg.m_PromptData = ""; |
| 120 | dlg.m_MaxDataLength = MAX_MESSAGE_NAME_LEN; |
| 121 | |
| 122 | if (dlg.DoModal() == IDCANCEL) |
| 123 | return; |
| 124 | |
| 125 | // Check if the message name is valid |
| 126 | char *valname = dlg.m_PromptData.GetBuffer(0); |
| 127 | if (strlen(valname) == 0) |
| 128 | return; |
| 129 | for (uint32_t j = 0; j < strlen(valname); j++) |
| 130 | if (!isalnum(valname[j]) && valname[j] != '_' && valname[j] != ' ') { |
| 131 | MessageBox("That name is invalid!\n\nA value name may only contain letters and numbers", "Invalid Name Error", |
| 132 | MB_OK | MB_ICONEXCLAMATION); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // Check if the message name already exists in the list |
| 137 | int pos; |
| 138 | if (m_DallasModelessDlgPtr->GetEnumValue(name.GetBuffer(0), valname, pos)) { |
| 139 | MessageBox("That value name is already in use!\n\nYou must enter a UNIQUE name.", "Invalid Value Name", |
| 140 | MB_OK | MB_ICONEXCLAMATION); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Add the value to the enum database |
| 145 | pos = m_DallasModelessDlgPtr->AddUserTypeValue(name.GetBuffer(0), valname); |
| 146 | if (pos == 0) |
| 147 | return; |
| 148 | if (pos == -1) { |
| 149 | MessageBox("The value could not be added.\n\nThe list may be full.", "Value Not Added", MB_OK | MB_ICONEXCLAMATION); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // Add the value to the list |
| 154 | m_ValuesListBox.InsertString(pos - 1, valname); |
| 155 | } |
| 156 | |
| 157 | void CDallasUserTypesDlg::OnChangeButton() { |
| 158 | CDallasGenericPromptDlg dlg; |
| 159 | int index; |
| 160 | CString type_name, value_name; |
| 161 | |
| 162 | CDallasMainDlg *m_DallasModelessDlgPtr; |
| 163 | m_DallasModelessDlgPtr = GetDallasDialogPtr(); |
| 164 | |
| 165 | if (m_DallasModelessDlgPtr == NULL) |
| 166 | return; |
| 167 | |
| 168 | // Get the name of the user type |
| 169 | index = m_UserTypeCombo.GetCurSel(); |
| 170 | if (index == CB_ERR) |
| 171 | return; |
| 172 | |
| 173 | m_UserTypeCombo.GetLBText(index, type_name); |
| 174 | if (type_name.IsEmpty()) |
| 175 | return; |
| 176 | |
| 177 | // Get the name of the selected value |
| 178 | index = m_ValuesListBox.GetCurSel(); |
| 179 | if (index == LB_ERR) |
| 180 | return; |
| 181 | |
| 182 | m_ValuesListBox.GetText(index, value_name); |
| 183 | if (value_name.IsEmpty()) |
| 184 | return; |
| 185 | |
| 186 | // Display the prompt dialog |
| 187 | dlg.m_DialogTitle = "Value Name Prompt"; |
| 188 | dlg.m_PromptText = "Enter a new name for this value:"; |
| 189 | dlg.m_PromptData = value_name; |
| 190 | dlg.m_MaxDataLength = MAX_MESSAGE_NAME_LEN; |
| 191 | |
| 192 | if (dlg.DoModal() == IDCANCEL) |
| 193 | return; |
| 194 | |
| 195 | // Check if the value name is valid |
| 196 | char *valname = dlg.m_PromptData.GetBuffer(0); |
| 197 | if (strlen(valname) == 0) |
| 198 | return; |
| 199 | for (uint32_t j = 0; j < strlen(valname); j++) |
| 200 | if (!isalnum(valname[j]) && valname[j] != '_' && valname[j] != ' ') { |
| 201 | MessageBox("That name is invalid!\n\nA value name may only contain letters and numbers", "Invalid Name Error", |
| 202 | MB_OK | MB_ICONEXCLAMATION); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | // Check if the value name already exists in the list |
| 207 | int pos; |
| 208 | if (m_DallasModelessDlgPtr->GetEnumValue(type_name.GetBuffer(0), valname, pos) && |
| 209 | strcmp(valname, value_name.GetBuffer(0)) != 0) { |
| 210 | MessageBox("That value name is already in use!\n\nYou must enter a UNIQUE name.", "Invalid Value Name", |
| 211 | MB_OK | MB_ICONEXCLAMATION); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // Change the name of the value |
| 216 | if (m_DallasModelessDlgPtr->ChangeValueName(type_name.GetBuffer(0), value_name.GetBuffer(0), valname)) { |
| 217 | m_ValuesListBox.DeleteString(index); |
| 218 | m_ValuesListBox.InsertString(index, valname); |
| 219 | m_ValuesListBox.SetCurSel(index); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void CDallasUserTypesDlg::OnDeleteButton() { |
| 224 | int index; |
| 225 | CString type_name, value_name; |
| 226 | |
| 227 | CDallasMainDlg *m_DallasModelessDlgPtr; |
| 228 | m_DallasModelessDlgPtr = GetDallasDialogPtr(); |
| 229 | |
| 230 | if (m_DallasModelessDlgPtr == NULL) |
| 231 | return; |
| 232 | |
| 233 | // Get the name of the user type |
| 234 | index = m_UserTypeCombo.GetCurSel(); |
| 235 | if (index == CB_ERR) |
| 236 | return; |
| 237 | |
| 238 | m_UserTypeCombo.GetLBText(index, type_name); |
| 239 | if (type_name.IsEmpty()) |
| 240 | return; |
| 241 | |
| 242 | // Get the name of the selected value |
| 243 | index = m_ValuesListBox.GetCurSel(); |
| 244 | if (index == LB_ERR) |
| 245 | return; |
| 246 | |
| 247 | m_ValuesListBox.GetText(index, value_name); |
| 248 | if (value_name.IsEmpty()) |
| 249 | return; |
| 250 | |
| 251 | if (m_DallasModelessDlgPtr->DeleteUserTypeValue(type_name.GetBuffer(0), value_name.GetBuffer(0))) { |
| 252 | m_ValuesListBox.DeleteString(index); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void CDallasUserTypesDlg::OnOK() { |
| 257 | // TODO: Add extra validation here |
| 258 | |
| 259 | CDialog::OnOK(); |
| 260 | } |
| 261 | |
| 262 | void CDallasUserTypesDlg::OnSelchangeUtypeCombo() { |
| 263 | int index; |
| 264 | CString name; |
| 265 | |
| 266 | CDallasMainDlg *m_DallasModelessDlgPtr; |
| 267 | m_DallasModelessDlgPtr = GetDallasDialogPtr(); |
| 268 | |
| 269 | index = m_UserTypeCombo.GetCurSel(); |
| 270 | if (index == CB_ERR) |
| 271 | return; |
| 272 | |
| 273 | m_UserTypeCombo.GetLBText(index, name); |
| 274 | if (name.IsEmpty()) |
| 275 | return; |
| 276 | |
| 277 | last_type_selected = name; |
| 278 | |
| 279 | // If Dallas is up, fill in the user type and value boxes |
| 280 | if (m_DallasModelessDlgPtr != NULL) { |
| 281 | m_DallasModelessDlgPtr->FillValuesBox(&m_ValuesListBox, name.GetBuffer(0)); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void CDallasUserTypesDlg::OnDblclkValuesList() { OnChangeButton(); } |