Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[9fe91f]: ItemEdit.cpp Maximize Restore History

Download this file

ItemEdit.cpp    369 lines (330 with data), 9.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// ItemEdit.cpp : implementation file
//
#include "stdafx.h"
#include <fcntl.h>
#include <sys/stat.h>
#include "chitem.h"
#include "chitemDlg.h"
#include "ItemEdit.h"
#include "StrRefDlg.h"
#include "tbg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CItemEdit dialog
CItemEdit::CItemEdit(CWnd* pParent /*=NULL*/)
: CDialog(CItemEdit::IDD, pParent)
{
m_pModelessPropSheet = NULL;
//{{AFX_DATA_INIT(CItemEdit)
//}}AFX_DATA_INIT
}
void CItemEdit::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CItemEdit)
//}}AFX_DATA_MAP
}
void CItemEdit::NewItem()
{
itemname="new item";
the_item.KillExtHeaders();
the_item.KillFeatHeaders();
memset(&the_item.header,0,sizeof(item_header));
memset(&the_item.iwd2header,0,sizeof(iwd2_header));
if(iwd2_structures() ) //iwd2
{
the_item.revision=20;
}
else if(has_xpvar()) //iwd
{
the_item.revision=10;
}
else if(pst_compatible_var()) //pst
{
the_item.revision=11;
}
else //bg1, bg2
{
the_item.revision=1;
}
the_item.header.iddesc=-1;
the_item.header.idref=-1;
the_item.header.uniddesc=-1;
the_item.header.unidref=-1;
the_item.header.extheadoffs=sizeof(item_header);
the_item.header.featureoffs=sizeof(item_header);
the_item.header.itmattr=ATTR_COPYABLE|ATTR_MOVABLE|ATTR_DISPLAY; //these bits are usually set
the_item.m_changed=false;
}
BEGIN_MESSAGE_MAP(CItemEdit, CDialog)
//{{AFX_MSG_MAP(CItemEdit)
ON_BN_CLICKED(IDC_LOAD, OnLoad)
ON_BN_CLICKED(IDC_LOADEX, OnLoadex)
ON_BN_CLICKED(IDC_SAVEAS, OnSaveas)
ON_BN_CLICKED(IDC_NEW, OnNew)
ON_BN_CLICKED(IDC_CHECK, OnCheck)
ON_COMMAND(ID_FILE_SAVE, OnSave)
ON_COMMAND(ID_FILE_TBG, OnFileTbg)
ON_COMMAND(ID_TOOLS_LOOKUPSTRREF, OnToolsLookupstrref)
ON_COMMAND(ID_FILE_NEW, OnNew)
ON_COMMAND(ID_FILE_LOAD, OnLoad)
ON_COMMAND(ID_FILE_LOADEXTERNALSCRIPT, OnLoadex)
ON_COMMAND(ID_FILE_SAVEAS, OnSaveas)
ON_COMMAND(ID_CHECK, OnCheck)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CItemEdit message handlers
void CItemEdit::OnLoad()
{
int res;
pickerdlg.m_restype=REF_ITM;
pickerdlg.m_picked=itemname;
res=pickerdlg.DoModal();
if(res==IDOK)
{
res=read_item(pickerdlg.m_picked);
switch(res)
{
case -3:
MessageBox("Item loaded with errors.","Warning",MB_ICONEXCLAMATION|MB_OK);
itemname=pickerdlg.m_picked;
break;
case 0:
itemname=pickerdlg.m_picked;
break;
default:
MessageBox("Cannot read item!","Error",MB_ICONSTOP|MB_OK);
NewItem();
break;
}
SetWindowText("Edit item: "+itemname);
m_pModelessPropSheet->RefreshDialog();
UpdateData(UD_DISPLAY);
}
}
static char BASED_CODE szFilter[] = "Item files (*.itm)|*.itm|All files (*.*)|*.*||";
void CItemEdit::OnLoadex()
{
CString filepath;
int fhandle;
int res;
res=OFN_FILEMUSTEXIST|OFN_ENABLESIZING|OFN_EXPLORER;
if(readonly) res|=OFN_READONLY;
CFileDialog m_getfiledlg(TRUE, "itm", makeitemname(".itm",0), res, szFilter);
restart:
if( m_getfiledlg.DoModal() == IDOK )
{
filepath=m_getfiledlg.GetPathName();
fhandle=open(filepath, O_RDONLY|O_BINARY);
if(!fhandle)
{
MessageBox("Cannot open file!","Error",MB_ICONSTOP|MB_OK);
goto restart;
}
readonly=m_getfiledlg.GetReadOnlyPref();
res=the_item.ReadItemFromFile(fhandle,-1);
close(fhandle);
lastopenedoverride=filepath.Left(filepath.ReverseFind('\\'));
switch(res)
{
case -4:
MessageBox("Item loaded with serious errors (no extensions).","Warning",MB_ICONEXCLAMATION|MB_OK);
itemname=m_getfiledlg.GetFileTitle();
itemname.MakeUpper();
break;
case -3:
MessageBox("Item loaded with errors (harmless inconsistency).","Warning",MB_ICONEXCLAMATION|MB_OK);
itemname=m_getfiledlg.GetFileTitle();
itemname.MakeUpper();
break;
case 0:
itemname=m_getfiledlg.GetFileTitle();
itemname.MakeUpper();
break;
default:
MessageBox("Cannot read item!","Error",MB_ICONSTOP|MB_OK);
NewItem();
break;
}
SetWindowText("Edit item: "+itemname);
m_pModelessPropSheet->RefreshDialog();
UpdateData(UD_DISPLAY);
}
}
void CItemEdit::OnNew()
{
NewItem();
SetWindowText("Edit item: "+itemname);
m_pModelessPropSheet->RefreshDialog();
UpdateData(UD_DISPLAY);
}
void CItemEdit::OnSave()
{
SaveItem(1);
}
void CItemEdit::OnSaveas()
{
SaveItem(0);
}
void CItemEdit::SaveItem(int save)
{
CString filepath;
CString newname;
CString tmpstr;
int res;
if(readonly)
{
MessageBox("You opened it read only!","Warning",MB_ICONEXCLAMATION|MB_OK);
return;
}
res=OFN_HIDEREADONLY|OFN_ENABLESIZING|OFN_EXPLORER;
CFileDialog m_getfiledlg(FALSE, "itm", makeitemname(".itm",0), res, szFilter);
if(save)
{
newname=itemname;
filepath=makeitemname(".itm",0);
goto gotname;
}
restart:
if( m_getfiledlg.DoModal() == IDOK )
{
filepath=m_getfiledlg.GetPathName();
filepath.MakeLower();
if(filepath.Right(4)!=".itm")
{
filepath+=".itm";
}
newname=m_getfiledlg.GetFileName();
newname.MakeUpper();
if(newname.Right(4)==".ITM") newname=newname.Left(newname.GetLength()-4);
gotname:
if(newname.GetLength()>8 || newname.GetLength()<1 || newname.Find(" ",0)!=-1)
{
tmpstr.Format("The resource name '%s' is bad, it should be 8 characters long and without spaces.",newname);
MessageBox(tmpstr,"Warning",MB_ICONEXCLAMATION|MB_OK);
goto restart;
}
if(newname!=itemname && file_exists(filepath) )
{
res=MessageBox("Do you want to overwrite "+newname+"?","Warning",MB_ICONQUESTION|MB_YESNO);
if(res==IDNO) goto restart;
}
res = write_item(newname, filepath);
lastopenedoverride=filepath.Left(filepath.ReverseFind('\\'));
switch(res)
{
case 0:
itemname=newname;
break; //saved successfully
case -2:
MessageBox("Error while writing file!","Error",MB_ICONSTOP|MB_OK);
break;
case -3:
MessageBox("Internal Error (feature block counter incorrect)!","Error",MB_ICONSTOP|MB_OK);
break;
default:
MessageBox("Unhandled error!","Error",MB_ICONSTOP|MB_OK);
}
}
SetWindowText("Edit item: "+itemname);
m_pModelessPropSheet->RefreshDialog();
UpdateData(UD_DISPLAY);
}
void CItemEdit::OnFileTbg()
{
ExportTBG(this, REF_ITM);
}
void CItemEdit::OnCheck()
{
int ret;
ret=((CChitemDlg *) AfxGetMainWnd())->check_item();
if(ret)
{
MessageBox(lasterrormsg,"Item editor",MB_ICONEXCLAMATION|MB_OK);
}
}
BOOL CItemEdit::OnInitDialog()
{
CString tmpstr, tmpstr1, tmpstr2;
CDialog::OnInitDialog();
SetWindowText("Edit item: "+itemname);
if (m_pModelessPropSheet == NULL)
{
m_pModelessPropSheet = new CItemPropertySheet(this);
//WOW DS_CONTROL
if (!m_pModelessPropSheet->Create(this,
DS_CONTROL | WS_CHILD | WS_VISIBLE ))
{
delete m_pModelessPropSheet;
m_pModelessPropSheet = NULL;
return TRUE;
}
m_pModelessPropSheet->SetWindowPos(0,0,0,0,0,SWP_NOSIZE);
}
if (m_pModelessPropSheet != NULL && !m_pModelessPropSheet->IsWindowVisible())
m_pModelessPropSheet->ShowWindow(SW_SHOW);
//tooltips
{
m_tooltip.Create(this,TTS_NOPREFIX);
m_tooltip.SetMaxTipWidth(200);
m_tooltip.SetTipBkColor(RGB(240,224,160));
m_tooltip.AddTool(GetDlgItem(IDCANCEL), IDS_CANCEL);
tmpstr1.LoadString(IDS_LOAD);
tmpstr2.LoadString(IDS_ITEM);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_LOAD), tmpstr);
tmpstr1.LoadString(IDS_LOADEX);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_LOADEX), tmpstr);
tmpstr1.LoadString(IDS_SAVE);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_SAVEAS), tmpstr);
tmpstr1.LoadString(IDS_NEW);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_NEW), tmpstr);
tmpstr1.LoadString(IDS_CHECK);
tmpstr.Format(tmpstr1, tmpstr2);
m_tooltip.AddTool(GetDlgItem(IDC_CHECK), tmpstr);
}
return TRUE;
}
void CItemEdit::OnToolsLookupstrref()
{
CStrRefDlg dlg;
dlg.DoModal();
m_pModelessPropSheet->RefreshDialog();
UpdateData(UD_DISPLAY);
}
void CItemEdit::PostNcDestroy()
{
if (m_pModelessPropSheet)
{
delete m_pModelessPropSheet;
m_pModelessPropSheet=NULL;
}
CDialog::PostNcDestroy();
}
void CItemEdit::OnCancel()
{
CString tmpstr;
if(the_item.m_changed)
{
tmpstr.Format("Changes have been made to the item.\n"
"Do you want to quit without save?\n");
if(MessageBox(tmpstr,"Warning",MB_YESNO)==IDNO)
{
return;
}
}
CDialog::OnCancel();
}
BOOL CItemEdit::PreTranslateMessage(MSG* pMsg)
{
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}