Debian Codegraph @

editor/D3XStringEditor.cpp

raw symbols SCIP
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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/D3XStringEditor.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:37 $
23 * $Author: kevinb $
24 *
25 * D3XString Editor
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 2 9/09/98 12:48p Samir
30 * added script localizer.
31 *
32 * 1 9/09/98 10:29a Samir
33 * Initial revision.
34 *
35 * $NoKeywords: $
36 */
37
38 #include "stdafx.h"
39 #include "editor.h"
40 #include "D3XStringEditor.h"
41 #include "pserror.h"
42 #include "manage.h"
43 #include "cfile.h"
44 #include "d3x.h"
45
46 #ifdef _DEBUG
47 #define new DEBUG_NEW
48 #undef THIS_FILE
49 static char THIS_FILE[] = __FILE__;
50 #endif
51
52 /////////////////////////////////////////////////////////////////////////////
53 // CD3XStringEditor dialog
54
55 CD3XStringEditor::CD3XStringEditor(CWnd *pParent /*=NULL*/) : CDialog(CD3XStringEditor::IDD, pParent) {
56 //{{AFX_DATA_INIT(CD3XStringEditor)
57 // NOTE: the ClassWizard will add member initialization here
58 //}}AFX_DATA_INIT
59
60 m_modified = false;
61 m_cursel = LB_ERR;
62 }
63
64 void CD3XStringEditor::DoDataExchange(CDataExchange *pDX) {
65 CDialog::DoDataExchange(pDX);
66 //{{AFX_DATA_MAP(CD3XStringEditor)
67 // NOTE: the ClassWizard will add DDX and DDV calls here
68 //}}AFX_DATA_MAP
69 }
70
71 BEGIN_MESSAGE_MAP(CD3XStringEditor, CDialog)
72 //{{AFX_MSG_MAP(CD3XStringEditor)
73 ON_BN_CLICKED(IDC_LOADSCRIPT, OnLoadscript)
74 ON_LBN_SELCHANGE(IDC_STRINGLIST, OnSelchangeStringlist)
75 ON_LBN_DBLCLK(IDC_STRINGLIST, OnDblclkStringlist)
76 ON_EN_KILLFOCUS(IDC_STRINGEDIT, OnKillfocusStringedit)
77 ON_BN_CLICKED(IDC_SAVE, OnSave)
78 //}}AFX_MSG_MAP
79 END_MESSAGE_MAP()
80
81 /////////////////////////////////////////////////////////////////////////////
82 // CD3XStringEditor message handlers
83
84 void CD3XStringEditor::OnLoadscript() {
85 #if 0
86 // open either a level or a .d3x file
87 char openpath[_MAX_PATH+1];
88
89 if (!PromptSave())
90 return;
91
92 if (OpenFileDialog(this, "Descent 3 scripts (*.d3x)|*.d3x||", openpath, LocalLevelsDir, _MAX_PATH+1)) {
93 // load program including string table and fill list box will strings.
94 tD3XProgram *prog;
95 CListBox *list = (CListBox *)GetDlgItem(IDC_STRINGLIST);
96 CFILE *fp = cfopen(openpath, "rb");
97
98 if (!fp) {
99 OutrageMessageBox("Unable to open %s.", openpath);
100 return;
101 }
102
103 prog = D3XLoadProgram(fp);
104 cfclose(fp);
105 if (!prog) {
106 OutrageMessageBox("Illegal d3x file format.");
107 return;
108 }
109
110 list->ResetContent();
111 for (uint16_t i = 0; i < prog->nstr; i++)
112 list->AddString(prog->strlist[i]);
113 list->EnableWindow();
114
115 OnSelchangeStringlist();
116
117 D3XFreeProgram(prog);
118
119 m_pathname = openpath;
120 m_modified = false;
121 }
122 #endif
123 }
124
125 void CD3XStringEditor::OnSelchangeStringlist() {
126 CListBox *list = (CListBox *)GetDlgItem(IDC_STRINGLIST);
127 CEdit *edit = (CEdit *)GetDlgItem(IDC_STRINGEDIT);
128 CString str;
129 int cursel = list->GetCurSel();
130
131 // replace current string in edit box into this listbox
132 if (m_cursel != LB_ERR) {
133 edit->GetWindowText(str);
134 list->DeleteString(m_cursel);
135 list->InsertString(m_cursel, str);
136 m_modified = true;
137 GetDlgItem(IDC_SAVE)->EnableWindow();
138 }
139
140 // put new string into edit box.
141 if (cursel != LB_ERR) {
142 list->GetText(cursel, str);
143 edit->SetWindowText((LPCSTR)str);
144 edit->EnableWindow();
145 }
146 m_cursel = cursel;
147 }
148
149 void CD3XStringEditor::OnDblclkStringlist() {}
150
151 void CD3XStringEditor::OnKillfocusStringedit() {
152 m_modified = true;
153 GetDlgItem(IDC_SAVE)->EnableWindow();
154 }
155
156 void CD3XStringEditor::OnSave() {
157 ASSERT(m_modified);
158 #if 0
159 // save program by loading it in first and reconstructin a new program obhect
160 CFILE *fp = cfopen(m_pathname, "rb");
161 if (fp) {
162 tD3XProgram *old_prog = D3XLoadProgram(fp);
163 tD3XProgram *new_prog;
164
165 cfclose(fp);
166
167 if (old_prog) {
168 CListBox *list = (CListBox *)GetDlgItem(IDC_STRINGLIST);
169 int curlength = 65536;
170 char *strbuf = new char[curlength];
171 char *strptr;
172 int i,n = list->GetCount();
173 for (i = 0, strptr = strbuf; i < n; i++)
174 {
175 CString str;
176 list->GetText(i, str);
177 if ((strptr+str.GetLength()+1) > (strbuf+curlength)) {
178 curlength *= 2;
179 char *newbuf = new char[curlength];
180 strcpy(newbuf, strbuf);
181 delete[] strbuf;
182 strbuf = newbuf;
183 }
184 strcpy(strptr, str);
185 strptr += str.GetLength()+1;
186 }
187
188 new_prog = D3XReallocProgram(NULL, old_prog->len, old_prog->nscripts, old_prog->nstr);
189 if (new_prog) {
190 D3XLoadProgramFromComponents(new_prog, old_prog->code, old_prog->map, strbuf);
191 D3XFreeProgram(old_prog);
192 fp = cfopen(m_pathname, "wb");
193 D3XSaveProgram(fp, new_prog);
194 cfclose(fp);
195 D3XFreeProgram(new_prog);
196 }
197 else {
198 OutrageMessageBox("Failed to reallocate new program.");
199 delete[] strbuf;
200 return;
201 }
202
203 delete[] strbuf;
204 }
205 }
206 else {
207 OutrageMessageBox("Fatal error. Unable to load script file %s.", m_pathname);
208 return;
209 }
210
211
212 m_modified = false;
213 GetDlgItem(IDC_SAVE)->EnableWindow(FALSE);
214 #endif
215 }
216
217 void CD3XStringEditor::OnOK() {
218 // TODO: Add extra validation here
219 if (!PromptSave())
220 return;
221
222 CDialog::OnOK();
223 }
224
225 bool CD3XStringEditor::PromptSave() { return true; }