| 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 | // EditLineDialog.cpp : implementation file |
| 20 | // |
| 21 | |
| 22 | #include "stdafx.h" |
| 23 | #include "editor.h" |
| 24 | #include "EditLineDialog.h" |
| 25 | |
| 26 | #ifdef _DEBUG |
| 27 | #define new DEBUG_NEW |
| 28 | #undef THIS_FILE |
| 29 | static char THIS_FILE[] = __FILE__; |
| 30 | #endif |
| 31 | |
| 32 | ///////////////////////////////////////////////////////////////////////////// |
| 33 | // CEditLineDialog dialog |
| 34 | |
| 35 | CEditLineDialog::CEditLineDialog(char *title, char *caption, char *initial, bool numeric, CWnd *pParent) |
| 36 | : CDialog(CEditLineDialog::IDD, pParent) { |
| 37 | //{{AFX_DATA_INIT(CEditLineDialog) |
| 38 | // NOTE: the ClassWizard will add member initialization here |
| 39 | //}}AFX_DATA_INIT |
| 40 | |
| 41 | m_Caption = caption; |
| 42 | m_Initial = initial; |
| 43 | m_Title = title; |
| 44 | |
| 45 | m_Numeric = numeric; |
| 46 | } |
| 47 | |
| 48 | CEditLineDialog::CEditLineDialog(char *caption, CWnd *pParent) : CDialog(CEditLineDialog::IDD, pParent) { |
| 49 | // NOTE: ClassWizard can't handle two DATA_INIT sections, so anything that gets |
| 50 | // added above should be copied down here. |
| 51 | |
| 52 | m_Caption = caption; |
| 53 | m_Initial = NULL; |
| 54 | m_Title = caption; |
| 55 | |
| 56 | m_Numeric = 0; |
| 57 | } |
| 58 | |
| 59 | void CEditLineDialog::DoDataExchange(CDataExchange *pDX) { |
| 60 | CDialog::DoDataExchange(pDX); |
| 61 | //{{AFX_DATA_MAP(CEditLineDialog) |
| 62 | // NOTE: the ClassWizard will add DDX and DDV calls here |
| 63 | //}}AFX_DATA_MAP |
| 64 | } |
| 65 | |
| 66 | BEGIN_MESSAGE_MAP(CEditLineDialog, CDialog) |
| 67 | //{{AFX_MSG_MAP(CEditLineDialog) |
| 68 | //}}AFX_MSG_MAP |
| 69 | END_MESSAGE_MAP() |
| 70 | |
| 71 | ///////////////////////////////////////////////////////////////////////////// |
| 72 | // CEditLineDialog message handlers |
| 73 | |
| 74 | void CEditLineDialog::OnOK() { |
| 75 | CEdit *ctl = (CEdit *)GetDlgItem(IDC_EDIT); |
| 76 | ctl->GetWindowText(m_EditBuf); |
| 77 | CDialog::OnOK(); |
| 78 | } |
| 79 | |
| 80 | BOOL CEditLineDialog::OnInitDialog() { |
| 81 | CEdit *edit_ctl = (CEdit *)GetDlgItem(IDC_EDIT); |
| 82 | |
| 83 | CDialog::OnInitDialog(); |
| 84 | |
| 85 | SetWindowText(m_Title); |
| 86 | |
| 87 | ((CEdit *)GetDlgItem(IDC_PROMPT))->SetWindowText(m_Caption); |
| 88 | |
| 89 | if (m_Initial) |
| 90 | edit_ctl->SetWindowText(m_Initial); |
| 91 | |
| 92 | // Turn on/off numeric-only input |
| 93 | if (m_Numeric) |
| 94 | edit_ctl->ModifyStyle(0, ES_NUMBER); |
| 95 | else |
| 96 | edit_ctl->ModifyStyle(ES_NUMBER, 0); |
| 97 | |
| 98 | return TRUE; // return TRUE unless you set the focus to a control |
| 99 | // EXCEPTION: OCX Property Pages should return FALSE |
| 100 | } |
| 101 | |
| 102 | // Gets a string from the user |
| 103 | // Parameters: buf - buffer the string is written to. should be initialized to default value |
| 104 | // maxsize - the length of buf |
| 105 | // title - the title for the input window |
| 106 | // prompt - the prompt for the input box |
| 107 | // Returns: false if cancel was pressed on the dialog, else true |
| 108 | // If false returned, buf is unchanged |
| 109 | bool InputString(char *buf, int maxsize, char *title, char *prompt, CWnd *wnd) { |
| 110 | CEditLineDialog dlg(title, prompt, buf, 0, wnd); |
| 111 | |
| 112 | if (dlg.DoModal() == IDOK) { |
| 113 | strncpy(buf, (char *)dlg.GetText(), maxsize); |
| 114 | buf[maxsize - 1] = 0; // strncpy doesn't terminate if string is too long |
| 115 | return 1; |
| 116 | } else |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | // Gets a number from the user |
| 121 | // Parameters: n - filled in the with return value |
| 122 | // title - the title for the input window |
| 123 | // prompt - the prompt for the input box |
| 124 | // Returns: false if cancel was pressed on the dialog, else true |
| 125 | // If false returned, n is unchanged |
| 126 | bool InputNumber(int *n, char *title, char *prompt, CWnd *wnd) { |
| 127 | char buf[100] = ""; |
| 128 | CEditLineDialog dlg(title, prompt, buf, 1, wnd); |
| 129 | |
| 130 | if (dlg.DoModal() == IDOK) { |
| 131 | *n = atoi((char *)dlg.GetText()); |
| 132 | return 1; |
| 133 | } else |
| 134 | return 0; |
| 135 | } |