[80baa6]: StrRefDlg.cpp Maximize Restore History

Download this file

StrRefDlg.cpp    617 lines (562 with data), 14.2 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// StrRefDlg.cpp : implementation file
//
#include "stdafx.h"
#include "chitem.h"
#include "chitemDlg.h"
#include "StrRefDlg.h"
#include "ItemPicker.h"
#include "massclear.h"
#include "options.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStrRefDlg dialog
CStrRefDlg::CStrRefDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStrRefDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStrRefDlg)
m_text = _T("");
m_sound = _T("");
m_tag = FALSE;
m_maxstr = _T("");
//}}AFX_DATA_INIT
m_strref=0;
m_searchdlg=NULL;
m_mode=0;
m_refresh=false;
}
void CStrRefDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
m_maxstr.Format("/ %d",tlk_headerinfo[0].entrynum);
//{{AFX_DATA_MAP(CStrRefDlg)
DDX_Text(pDX, IDC_SOUND, m_sound);
DDV_MaxChars(pDX, m_sound, 8);
DDX_Check(pDX, IDC_TAG, m_tag);
DDX_Text(pDX, IDC_MAX, m_maxstr);
DDV_MaxChars(pDX, m_maxstr, 10);
DDX_Control(pDX, IDC_TEXT, m_text_control);
DDX_Text(pDX, IDC_TEXT, m_text);
DDX_Control(pDX, IDC_SPIN, m_spin_control);
DDX_Control(pDX, IDC_STRREF, m_strref_control);
//}}AFX_DATA_MAP
m_spin_control.SetRange32(0,tlk_headerinfo[0].entrynum);
}
BEGIN_MESSAGE_MAP(CStrRefDlg, CDialog)
//{{AFX_MSG_MAP(CStrRefDlg)
ON_EN_UPDATE(IDC_STRREF, OnUpdateStrref)
ON_EN_KILLFOCUS(IDC_TEXT, OnKillfocusText)
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_EN_KILLFOCUS(IDC_SOUND, OnKillfocusSound)
ON_BN_CLICKED(IDC_TAG, OnTag)
ON_BN_CLICKED(IDC_SEARCH, OnSearch)
ON_BN_CLICKED(IDC_PLAY, OnPlay)
ON_BN_CLICKED(IDC_FIND, OnFind)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_RELOAD, OnReload)
ON_COMMAND(ID_TRIM, OnTrim)
ON_COMMAND(ID_CLEAR, OnClear)
ON_EN_KILLFOCUS(IDC_STRREF, OnKillfocusStrref)
ON_COMMAND(ID_CHECKSOUND, OnChecksound)
ON_COMMAND(ID_CHECKTAG, OnChecktag)
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE( WM_FINDREPLACE, OnFindReplace )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStrRefDlg message handlers
void CStrRefDlg::OnUpdateStrref()
{
CString strref;
if(!m_refresh) return;
GetDlgItem(IDC_STRREF)->GetWindowText(strref);
m_strref=strtonum(strref);
m_tag=resolve_tlk_tag(m_strref);
m_text=resolve_tlk_text(m_strref);
m_sound=resolve_tlk_soundref(m_strref);
strref="Edit TLK ";
if(global_changed[0]==true) strref+="*";
SetWindowText(strref);
UpdateData(UD_DISPLAY);
}
void CStrRefDlg::OnKillfocusText()
{
CString old;
UpdateData(UD_RETRIEVE);
old=resolve_tlk_text(m_strref);
if(old==m_text) return;
if(editflg&TLKCHANGE)
{
if(MessageBox("Do you want to update dialog.tlk?","TLK editor",MB_ICONQUESTION|MB_YESNO)!=IDYES)
{
goto end;
}
}
if(tlk_headerinfo[0].entrynum==-1)
{
memcpy(tlk_headerinfo[0].signature,"TLK V1 ",8);
tlk_headerinfo[0].entrynum=0;
store_tlk_text(0,"<NO TEXT>");
m_strref=1;
}
if(m_strref>0 && m_strref<=tlk_headerinfo[0].entrynum) //allow = so we can add strings
{
m_strref=store_tlk_text(m_strref, m_text);
}
end:
old.Format("%d",m_strref);
GetDlgItem(IDC_STRREF)->SetWindowText(old);
UpdateData(UD_DISPLAY);
}
void CStrRefDlg::OnKillfocusSound()
{
CString old;
UpdateData(UD_RETRIEVE);
old=resolve_tlk_soundref(m_strref);
if(old==m_sound) return;
if(editflg&TLKCHANGE)
{
if(MessageBox("Do you want to update dialog.tlk?","TLK editor",MB_ICONQUESTION|MB_YESNO)!=IDYES)
{
return;
}
}
if(m_strref>0 && m_strref<tlk_headerinfo[0].entrynum)
{
m_strref=store_tlk_soundref(m_strref, m_sound);
}
}
void CStrRefDlg::OnTag()
{
if(editflg&TLKCHANGE)
{
if(MessageBox("Do you want to update dialog.tlk?","TLK editor",MB_ICONQUESTION|MB_YESNO)!=IDYES)
{
return;
}
}
if(m_strref>0 && m_strref<tlk_headerinfo[0].entrynum)
{
toggle_tlk_tag(m_strref);
}
m_tag=resolve_tlk_tag(m_strref);
}
void CStrRefDlg::OnPlay()
{
play_acm(m_sound,false,false);
}
void CStrRefDlg::OnBrowse()
{
pickerdlg.m_restype=REF_WAV;
pickerdlg.m_picked=m_sound;
if(pickerdlg.DoModal()==IDOK)
{
m_sound=pickerdlg.m_picked;
UpdateData(UD_DISPLAY);
OnKillfocusSound();
}
}
void CStrRefDlg::StartFindReplace(int mode)
{
int a,b;
if(!m_searchdlg)
{
m_mode=mode;
m_searchdlg=new CFindReplaceDialog;
if(!m_searchdlg)
{
MessageBox("Not enough memory.","Error",MB_ICONEXCLAMATION|MB_OK);
return;
}
m_text_control.GetSel(a,b);
if(mode)
{
m_searchdlg->Create(readonly,m_text.Mid(a,b-a),NULL, FR_DOWN | FR_MATCHCASE, this);
}
else
{
m_searchdlg->Create(readonly,m_sound,NULL, FR_DOWN | FR_HIDEMATCHCASE|FR_HIDEWHOLEWORD, this);
}
}
}
void CStrRefDlg::OnFind()
{
StartFindReplace(0);
}
void CStrRefDlg::OnSearch()
{
StartFindReplace(1);
}
long CStrRefDlg::OnFindReplace(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
int direction;
int mode; //0 - search, 1 - replace current, 2 - replace all
int match;
CString search;
CString replace;
if(!m_searchdlg) return 0;
if(m_searchdlg->IsTerminating() )
{
m_searchdlg=NULL;
return 0;
}
direction=m_searchdlg->SearchDown();
mode=!!m_searchdlg->ReplaceCurrent()+(!!m_searchdlg->ReplaceAll())*2;
//matchcase is cheaper, therefore it is marked 0
match=!m_searchdlg->MatchCase()+(!!m_searchdlg->MatchWholeWord())*2;
search=m_searchdlg->GetFindString();
replace=m_searchdlg->GetReplaceString();
do_search_and_replace(direction,mode,match,search,replace);
return 0;
}
int perform_search_and_replace_sound(int idx, int mode, CString search, CString replace)
{
CString tmpstr;
int tmpstart;
RetrieveResref(tmpstr,tlk_entries[0][idx].reference.soundref);
tmpstart=tmpstr.Find(search);
if(tmpstart!=-1)
{
if(mode)
{
tmpstr.Replace(search,replace);
StoreResref(tmpstr,tlk_entries[0][idx].reference.soundref);
global_changed[0]=true;
}
return true;
}
return false;
}
int perform_search_and_replace(int idx, int mode, int match, CString search, CString replace, int stringmode)
{
int ch;
int found;
int len;
int tmplen, tmpstart;
CString tmp2;
CString srch;
if(!stringmode)
{
return perform_search_and_replace_sound(idx, mode,search,replace);
}
found=false;
srch=search;
len=srch.GetLength();
switch(mode)
{
case SR_SEARCH:
tmp2=tlk_entries[0][idx].text;
if(match&1) // case insensitive trick
{
tmp2.MakeLower();
}
if(match&2) // full word trick
{
tmp2=" "+tlk_entries[0][idx].text+" "; //in this
}
if(tmp2.Find(srch) !=-1) found=true;
break;
case SR_REPLACE: case SR_ALL:
tmplen=tlk_entries[0][idx].text.GetLength();
if(match&2)
{
tlk_entries[0][idx].text=" "+tlk_entries[0][idx].text+" ";
}
if(match&1)
{
tmp2=tlk_entries[0][idx].text;
tmp2.MakeLower();
tmpstart=tmp2.Find(srch);
if(tmpstart==-1) ch=0;
else
{
tlk_entries[0][idx].text.Delete(tmpstart,len);
tlk_entries[0][idx].text.Insert(tmpstart,replace);
ch=true;
}
}
else
{
ch=tlk_entries[0][idx].text.Replace(srch,replace);
}
if(match&2)
{
tlk_entries[0][idx].text=tlk_entries[0][idx].text.Mid(1,tmplen);
}
if(ch)
{
tlk_entries[0][idx].reference.length=tlk_entries[0][idx].text.GetLength();
if(!tlk_entries[0][idx].reference.length)
{
if(tlk_entries[0][idx].reference.soundref[0]) tlk_entries[0][idx].reference.flags=2; //only sound reference
else
{
tlk_entries[0][idx].reference.flags=0; //deleted entry
tlk_entries[0][idx].reference.pitch=0;
tlk_entries[0][idx].reference.volume=0;
memset(tlk_entries[0][idx].reference.soundref,0,sizeof(resref_t));
}
}
global_changed[0]=true;
found=true;
}
break;
}
return found;
}
int CStrRefDlg::do_search_and_replace(int direction,int mode,int match,CString search,CString replace)
{
int found, count;
int oldidx, idx;
CString operation;
CString what;
idx=m_strref;
if(idx<0 || idx>=tlk_headerinfo[0].entrynum)
{
idx=0;
}
if(!tlk_headerinfo[0].entrynum) return -1;
oldidx=idx;
if(m_mode) //search for string
{
if(match&1) //we can do this here
{
search.MakeLower();
}
if(match&2) //this too
{
search=" "+search+" ";
}
}
else search.MakeUpper(); //search for soundref
found=-1;
count=0;
if(direction)
{
direction=1;
what="bottom";
}
else
{
direction=-1;
what="top";
}
switch(mode)
{
case SR_SEARCH: operation="Search"; break; //0
case SR_REPLACE: operation="Search & replace"; break; //1
case SR_ALL: operation="Replace all"; break; //2
}
if(!mode)
{
idx+=direction;
}
while(direction<1? idx : idx<tlk_headerinfo[0].entrynum)
{
if(perform_search_and_replace(idx,mode,match,search,replace,m_mode))
{
found=idx;
count++;
}
if(found==-1)
{//we found nothing
if(mode==1) mode=0; //couldn't find anything in the actual position, we will search from now
}
else
{//we found something, if it is not replace all then we stop here
if(mode!=2) break;
}
idx+=direction;
}
if((found==-1) || (mode==2)) //replace all should go around even if it has found something
{
if(MessageBox(operation+" hit the "+what+" of the list, do you want to continue?",operation,MB_YESNO)==IDNO)
{
return found;
}
if(direction>0)
{
idx=0;
}
else
{
idx=tlk_headerinfo[0].entrynum-1;
}
while(idx!=oldidx)
{
if(perform_search_and_replace(idx,mode,match,search,replace,m_mode))
{
found=idx;
count++;
}
if(found==-1)
{//we found nothing
if(mode==1) mode=0; //couldn't find anything in the actual position, we will search from now
}
else
{//we found something, if it is not replace all then we stop here
if(mode!=2) break;
}
idx+=direction;
}
}
if(found!=-1)
{
m_strref=found;
what.Format("%d",m_strref);
m_strref_control.SetWindowText(what);
if(count>1)
{
operation.Format("Found %d entries.",count);
MessageBox(operation);
}
}
else
{
MessageBox("Couldn't find any entries containing: "+search);
}
return found;
}
BOOL CStrRefDlg::OnInitDialog()
{
CString tmpstr, tmpstr1, tmpstr2;
CDialog::OnInitDialog();
//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);
}
m_refresh=true;
m_strref_control.SetWindowText("0");
return TRUE;
}
void CStrRefDlg::OnSave()
{
((CChitemDlg *) AfxGetMainWnd())->write_file_progress(0);
if(global_changed[0]==true)
{
MessageBox(((CChitemDlg *) AfxGetMainWnd())->GetTlkFileName(0)+" wasn't saved, exit all other programs that might use it, and try again.","Warning",MB_ICONEXCLAMATION|MB_OK);
}
((CChitemDlg *) AfxGetMainWnd())->write_file_progress(1);
if(global_changed[1]==true)
{
MessageBox(((CChitemDlg *) AfxGetMainWnd())->GetTlkFileName(1)+" wasn't saved, exit all other programs that might use it, and try again.","Warning",MB_ICONEXCLAMATION|MB_OK);
}
OnUpdateStrref();
}
void CStrRefDlg::OnReload()
{
((CChitemDlg *) AfxGetMainWnd())->rescan_dialog(false);
OnUpdateStrref();
}
void CStrRefDlg::OnTrim()
{
CString tmpstr;
OnKillfocusStrref();
if(tlk_headerinfo[0].entrynum<1)
{
MessageBox("Can't truncate an empty file.","Warning",MB_OK);
return;
}
tmpstr.Format("Do you really want to remove all entries above #%ld.",m_strref);
if(MessageBox(tmpstr,"Truncate",MB_YESNO)==IDYES)
{
if(truncate_references(m_strref+1,0)) global_changed[0]=true;
if(truncate_references(m_strref+1,1)) global_changed[1]=true;
UpdateData(UD_DISPLAY);
}
}
void CStrRefDlg::OnClear()
{
massclear mscDlg;
OnKillfocusStrref();
mscDlg.SetRange(1,tlk_headerinfo[0].entrynum-1, m_strref);
mscDlg.SetText("Clearing an interval of entries");
if(mscDlg.DoModal()==IDOK)
{
while(mscDlg.m_from<=mscDlg.m_to)
{
store_tlk_soundref(mscDlg.m_from,"");
store_tlk_text(mscDlg.m_from++,"*!*");
}
}
}
void CStrRefDlg::OnKillfocusStrref()
{
CString old;
if(m_strref>tlk_headerinfo[0].entrynum)
{
m_strref=0;
}
old.Format("%d",m_strref);
GetDlgItem(IDC_STRREF)->SetWindowText(old);
UpdateData(UD_DISPLAY);
}
void CStrRefDlg::OnChecksound()
{
loc_entry tmploc;
CString tmpref,tmpstr;
int i;
int ret;
int chg;
chg=0;
for(i=0;i<tlk_headerinfo[0].entrynum;i++)
{
tmpref=resolve_tlk_soundref(i);
if(tmpref.IsEmpty())
{
tlk_entries[0][i].reference.pitch=tlk_entries[0][i].reference.volume=0;
if(tlk_entries[0][i].text.IsEmpty())
{
if(tlk_entries[0][i].reference.flags)
{
chg=1;
tlk_entries[0][i].reference.flags=0;
}
}
continue;
}
else
{
if(tlk_entries[0][i].reference.flags&2)
{
chg=1;
tlk_entries[0][i].reference.flags|=2;
}
}
if(!sounds.Lookup(tmpref, tmploc) )
{
tmpstr.Format("%d",i);
GetDlgItem(IDC_STRREF)->SetWindowText(tmpstr);
UpdateData(UD_DISPLAY);
tmpstr.Format("Missing sound reference: %s, do you want to clear it?",tmpref);
ret=MessageBox(tmpstr,"Tlk editor",MB_ICONEXCLAMATION|MB_YESNOCANCEL);
if(ret==IDCANCEL) return;
if(ret==IDYES)
{
chg=1;
store_tlk_soundref(i,"");
}
}
}
if(!chg) MessageBox("No problem found!","Tlk editor",MB_OK);
}
void CStrRefDlg::OnChecktag()
{
int i;
int chg;
chg=0;
for(i=0;i<tlk_headerinfo[0].entrynum;i++)
{
}
if(!chg) MessageBox("No problem found!","Tlk editor",MB_OK);
}
BOOL CStrRefDlg::PreTranslateMessage(MSG* pMsg)
{
m_tooltip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}