Menu

[r22]: / src / styledialog.cpp  Maximize  Restore  History

Download this file

812 lines (719 with data), 26.5 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#include <wx/intl.h>
#include "styledialog.h"
#include "nocasecompare.h"
#define ngettext wxGetTranslation
BEGIN_EVENT_TABLE ( StyleDialog, wxDialog )
EVT_BUTTON ( ID_STYLE_REPORT, StyleDialog::OnReport )
EVT_BUTTON ( ID_STYLE_IGNORE_ALL, StyleDialog::OnStyleIgnoreAll )
EVT_BUTTON ( ID_STYLE_CHANGE_ALL, StyleDialog::OnStyleChangeAll )
EVT_BUTTON ( ID_STYLE_EDIT, StyleDialog::OnStyleEdit )
EVT_BUTTON ( ID_STYLE_WEB_REPORT, StyleDialog::OnStyleWebReport )
EVT_BUTTON ( ID_STYLE_WEB_SUMMARY, StyleDialog::OnStyleWebSummary )
EVT_BUTTON ( wxID_CANCEL, StyleDialog::OnCancel )
EVT_MENU ( ID_MENU_CHANGE_ONCE, StyleDialog::OnMenuChangeOnce )
EVT_MENU ( ID_MENU_CHANGE_ALL, StyleDialog::OnMenuChangeAll )
EVT_MENU ( ID_MENU_IGNORE_ONCE, StyleDialog::OnMenuIgnoreOnce )
EVT_MENU ( ID_MENU_IGNORE_ALL, StyleDialog::OnMenuIgnoreAll )
EVT_MENU ( ID_MENU_NEW_SUGGESTION, StyleDialog::OnMenuNewSuggestion )
EVT_MENU ( ID_MENU_APPLY_SUGGESTION_ALL, StyleDialog::OnMenuApplySuggestionAll )
EVT_LIST_COL_CLICK ( ID_STYLE_TABLE, StyleDialog::OnColumnClick )
EVT_LIST_ITEM_ACTIVATED ( ID_STYLE_TABLE, StyleDialog::OnItemActivated )
EVT_LIST_ITEM_RIGHT_CLICK ( ID_STYLE_TABLE, StyleDialog::OnItemRightClick )
EVT_UPDATE_UI_RANGE ( ID_STYLE_EDIT, ID_STYLE_CHANGE_ALL, StyleDialog::OnUpdateTableRange )
END_EVENT_TABLE()
StyleDialog::StyleDialog (
wxWindow *parent,
wxIcon icon,
const std::string& bufferParameterUtf8,
const wxString& fileNameParameter,
const wxString& ruleSetDirectoryParameter,
const wxString& filterDirectoryParameter,
const wxString& browserParameter,
const wxString& ruleSetPresetParameter,
const wxString& filterPresetParameter,
bool readOnlyParameter,
wxPoint position,
wxSize size )
: wxDialog (
parent,
wxID_ANY,
wxString ( _ ( "Spelling and Style" ) ),
position,
size,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxMAXIMIZE_BOX ),
bufferUtf8 ( bufferParameterUtf8 ),
fileName ( fileNameParameter ),
ruleSetDirectory ( ruleSetDirectoryParameter ),
filterDirectory ( filterDirectoryParameter ),
browser ( browserParameter ),
ruleSetPreset ( ruleSetPresetParameter ),
filterPreset ( filterPresetParameter ),
readOnly ( readOnlyParameter )
{
SetIcon ( icon );
wxSize buttonSize ( 100, wxDefaultCoord );
// top box
ruleSetCombo = new wxComboBox (
this,
ID_STYLE_COMBO_RULESET,
_T ( "" ),
wxDefaultPosition,
wxSize ( 200, -1 )
);
filterCombo = new wxComboBox (
this,
ID_STYLE_COMBO_FILTER,
_T ( "" ),
wxDefaultPosition,
wxSize ( 200, -1 )
);
wxButton *createReportButton = new wxButton (
this,
ID_STYLE_REPORT,
_ ( "&Report" ),
wxDefaultPosition,
buttonSize,
0 );
wxBoxSizer *comboSizer = new wxBoxSizer ( wxHORIZONTAL );
comboSizer->Add ( ruleSetCombo, 0, wxRIGHT, 10 );
comboSizer->Add ( filterCombo, 0, wxRIGHT, 10 );
comboSizer->Add ( createReportButton, 0, wxRIGHT, 10 );
// middle box
wxListCtrl *myTable = new wxListCtrl (
this,
ID_STYLE_TABLE,
wxPoint ( 0, 0 ),
wxSize ( -1, -1 ),
wxLC_REPORT );
int widthUnit = 35;
myTable->InsertColumn ( 0, _ ( "No." ), wxLIST_FORMAT_LEFT, widthUnit * 1 );
myTable->InsertColumn ( 1, _ ( "Context" ), wxLIST_FORMAT_RIGHT, widthUnit * 3 );
myTable->InsertColumn ( 2, _ ( "Match" ), wxLIST_FORMAT_CENTER, widthUnit * 3 );
myTable->InsertColumn ( 3, _ ( "Context" ), wxLIST_FORMAT_LEFT, widthUnit * 3 );
myTable->InsertColumn ( 4, _ ( "Suggestion" ), wxLIST_FORMAT_LEFT, widthUnit * 3 );
myTable->InsertColumn ( 5, _ ( "Rule" ), wxLIST_FORMAT_LEFT, widthUnit * 3 );
myTable->InsertColumn ( 6, _ ( "Action" ), wxLIST_FORMAT_LEFT, widthUnit * 3 );
table = myTable;
// lower box
wxButton *editItemsButton =
new wxButton (
this,
ID_STYLE_EDIT,
_ ( "&Edit" ),
wxDefaultPosition,
buttonSize,
0 );
wxButton *webReportButton =
new wxButton (
this,
ID_STYLE_WEB_REPORT,
_ ( "&Printable report" ),
wxDefaultPosition,
buttonSize,
0 );
wxButton *webSummaryButton =
new wxButton (
this,
ID_STYLE_WEB_SUMMARY,
_ ( "Pr&intable summary" ),
wxDefaultPosition,
buttonSize,
0 );
wxButton *selectAllButton =
new wxButton (
this,
ID_STYLE_CHANGE_ALL,
_ ( "&Change all" ),
wxDefaultPosition,
buttonSize,
0 );
wxButton *deselectAllButton =
new wxButton (
this,
ID_STYLE_IGNORE_ALL,
_ ( "I&gnore all" ),
wxDefaultPosition,
buttonSize,
0 );
wxButton *cancelButton =
new wxButton (
this,
wxID_CANCEL,
_ ( "C&ancel" ),
wxDefaultPosition,
buttonSize,
0 );
wxBoxSizer *reportButtonSizer = new wxBoxSizer ( wxHORIZONTAL );
reportButtonSizer->Add ( editItemsButton, 0, wxRIGHT, 10 );
reportButtonSizer->Add ( webReportButton, 0, wxLEFT | wxRIGHT, 10 );
reportButtonSizer->Add ( webSummaryButton, 0, wxRIGHT, 10 );
reportButtonSizer->Add ( selectAllButton, 0, wxLEFT | wxRIGHT, 10 );
reportButtonSizer->Add ( deselectAllButton, 0, wxRIGHT, 10 );
reportButtonSizer->Add ( cancelButton, 0, wxLEFT, 10 );
// status bar
status = new wxStatusBar ( this, wxID_ANY );
// overall sizer
wxBoxSizer *reportTopSizer = new wxBoxSizer ( wxVERTICAL );
reportTopSizer->Add ( comboSizer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
reportTopSizer->Add ( table, 1, wxEXPAND | wxALL, 5 );
reportTopSizer->Add ( reportButtonSizer, 0, wxALL, 5 );
reportTopSizer->Add ( status, 0 );
this->SetSizer ( reportTopSizer );
// update combo lists
if ( wxFileName::DirExists ( ruleSetDirectory ) )
{
wxString ruleMask, ruleFile;
ruleMask = ruleSetDirectory + wxFileName::GetPathSeparator() + _T ( "*.xml" );
ruleFile = wxFindFirstFile ( ruleMask, wxFILE );
if ( !ruleFile.empty() )
{
ruleFile.Replace ( _T ( ".xml" ), _T ( "" ) );
ruleSetCombo->Append ( wxFileNameFromPath ( ruleFile ) );
for ( ;; )
{
ruleFile = wxFindNextFile();
if ( ruleFile.empty() )
break;
ruleFile.Replace ( _T ( ".xml" ), _T ( "" ) );
ruleSetCombo->Append ( wxFileNameFromPath ( ruleFile ) );
}
}
ruleSetCombo->SetValue ( ruleSetPreset );
}
else
{
ruleSetCombo->Append ( _ ( "(No rule sets found)" ) );
ruleSetCombo->Select ( 0 );
}
if ( wxFileName::DirExists ( filterDirectory ) )
{
filterCombo->Append ( _ ( "(No filter)" ) );
wxString filterMask, filterFile;
filterMask = filterDirectory + wxFileName::GetPathSeparator() + _T ( "*.xml" );
filterFile = wxFindFirstFile ( filterMask, wxFILE );
if ( !filterFile.empty() )
{
filterFile.Replace ( _T ( ".xml" ), _T ( "" ) );
filterCombo->Append ( wxFileNameFromPath ( filterFile ) );
for ( ;; )
{
filterFile = wxFindNextFile();
if ( filterFile.empty() )
break;
filterFile.Replace ( _T ( ".xml" ), _T ( "" ) );
filterCombo->Append ( wxFileNameFromPath ( filterFile ) );
}
}
filterCombo->SetValue ( filterPreset );
}
else
{
filterCombo->Append ( _ ( "(No filters found)" ) );
filterCombo->Select ( 0 );
}
createReportButton->SetFocus();
if ( readOnly )
filterCombo->Enable ( false );
// keyboard shortcuts
wxAcceleratorEntry entries[7];
entries[0].Set ( wxACCEL_ALT, ( int ) 'R', ID_STYLE_REPORT );
entries[1].Set ( wxACCEL_ALT, ( int ) 'E', ID_STYLE_EDIT );
entries[2].Set ( wxACCEL_ALT, ( int ) 'W', ID_STYLE_WEB_REPORT );
entries[3].Set ( wxACCEL_ALT, ( int ) 'B', ID_STYLE_WEB_SUMMARY );
entries[4].Set ( wxACCEL_ALT, ( int ) 'C', ID_STYLE_CHANGE_ALL );
entries[5].Set ( wxACCEL_ALT, ( int ) 'I', ID_STYLE_IGNORE_ALL );
entries[6].Set ( wxACCEL_ALT, ( int ) 'A', wxID_CANCEL );
wxAcceleratorTable accel ( 7, entries );
this->SetAcceleratorTable ( accel );
}
StyleDialog::~StyleDialog()
{
std::set<wxString>::iterator it;
for ( it = tempFiles.begin(); it != tempFiles.end(); it++ )
wxRemoveFile ( *it );
}
void StyleDialog::OnColumnClick ( wxListEvent& event )
{
std::auto_ptr<SortData> data ( new SortData );
data->column = event.GetColumn();
data->table = table;
table->SortItems ( MyCompareFunction, ( long ) data.get() );
long itemCount = table->GetItemCount();
for ( int i = 0; i < itemCount; ++i )
table->SetItemData ( i, i );
}
void StyleDialog::OnItemActivated ( wxListEvent& event )
{
int index = event.GetIndex();
bool ignore = ( getTextByColumn ( table, index, 6 ) == _ ( "Ignore" ) );
setIgnore ( index, ( ignore ) ? false : true );
}
void StyleDialog::OnItemRightClick ( wxListEvent& event )
{
int index = indexForContextMenu = event.GetIndex();
wxString match, suggestion;
match = getTextByColumn ( table, index, 2 );
suggestion = getTextByColumn ( table, index, 4 );
wxMenu contextMenu;
contextMenu.Append (
ID_MENU_IGNORE_ONCE, _ ( "Ignore once" ) );
contextMenu.Append (
ID_MENU_IGNORE_ALL, _ ( "Ignore all" ) );
contextMenu.AppendSeparator();
contextMenu.Append ( ID_MENU_CHANGE_ONCE, _ ( "Change once" ) );
contextMenu.Append ( ID_MENU_CHANGE_ALL, _ ( "Change all" ) );
contextMenu.AppendSeparator();
contextMenu.Append (
ID_MENU_NEW_SUGGESTION, _ ( "New suggestion..." ) );
wxString menuString;
menuString.Printf ( _T ( "Change '%s' to '%s' throughout" ), match.c_str(), suggestion.c_str() );
contextMenu.Append (
ID_MENU_APPLY_SUGGESTION_ALL, menuString );
bool ignore = ( getTextByColumn ( table, index, 6 ) == _ ( "Ignore" ) );
contextMenu.Enable ( ( ignore ) ? ID_MENU_IGNORE_ONCE : ID_MENU_CHANGE_ONCE, false );
PopupMenu ( &contextMenu, wxPoint ( -1, -1 ) );
}
void StyleDialog::OnCancel ( wxCommandEvent& event )
{
updateSizeInformation();
event.Skip();
}
void StyleDialog::OnReport ( wxCommandEvent& event )
{
table->DeleteAllItems();
matchVector.clear();
status->SetStatusText ( _ ( "Creating report..." ) );
// update presets
ruleSetPreset = ruleSetCombo->GetValue();
filterPreset = filterCombo->GetValue();
// reconstitute short filenames
wxString ruleSet, filter;
ruleSet = ruleSetPreset + _T ( ".xml" );
filter = filterPreset + _T ( ".xml" );
std::string ruleSetDirectoryUtf8,
ruleSetUtf8,
filterDirectoryUtf8,
filterUtf8,
pathSeparatorUtf8;
ruleSetDirectoryUtf8 = ruleSetDirectory.mb_str ( wxConvUTF8 );
ruleSetUtf8 = ruleSet.mb_str ( wxConvUTF8 );
filterDirectoryUtf8 = filterDirectory.mb_str ( wxConvUTF8 );
filterUtf8 = filter.mb_str ( wxConvUTF8 );
wxString separator = wxFileName::GetPathSeparator();
pathSeparatorUtf8 = separator.mb_str ( wxConvUTF8 );
std::auto_ptr<HouseStyle> hs ( new HouseStyle (
bufferUtf8,
ruleSetDirectoryUtf8,
ruleSetUtf8,
filterDirectoryUtf8,
filterUtf8,
pathSeparatorUtf8,
5 ) );
status->SetStatusText ( _ ( "Creating report..." ) );
if ( !hs->createReport() )
{
std::string lastError = hs->getLastError();
wxString error = wxString ( lastError.c_str(), wxConvUTF8, lastError.size() );
status->SetStatusText ( _ ( "Cannot create report: " ) + error );
return;
}
matchVector = hs->getMatchVector();
vector<ContextMatch>::iterator it;
std::string prelogUtf8, matchUtf8, postlogUtf8, replaceUtf8, reportUtf8;
wxString matchNo, prelog, match, postlog, replace, report;
int i = 0;
for ( it = matchVector.begin(); it != matchVector.end(); it++ )
{
matchNo.Printf ( _T ( "%i" ), i + 1 ); // display numbers from 1
prelogUtf8 = flatWhiteSpace ( ( *it ).prelog );
matchUtf8 = flatWhiteSpace ( ( *it ).match );
postlogUtf8 = flatWhiteSpace ( ( *it ).postlog );
replaceUtf8 = flatWhiteSpace ( ( *it ).replace );
reportUtf8 = flatWhiteSpace ( ( *it ).report );
prelog = wxString ( prelogUtf8.c_str(), wxConvUTF8, ( *it ).prelog.size() );
match = wxString ( matchUtf8.c_str(), wxConvUTF8, ( *it ).match.size() );
postlog = wxString ( postlogUtf8.c_str(), wxConvUTF8, ( *it ).postlog.size() );
replace = wxString ( replaceUtf8.c_str(), wxConvUTF8, ( *it ).replace.size() );
report = wxString ( reportUtf8.c_str(), wxConvUTF8, ( *it ).report.size() );
table->InsertItem ( i, matchNo, 0 );
table->SetItem ( i, 1, prelog );
table->SetItem ( i, 2, match );
table->SetItem ( i, 3, postlog );
table->SetItem ( i, 4, replace );
table->SetItem ( i, 5, report );
setIgnore ( i, ( *it ).tentative );
table->SetItemData ( i, i );
++i;
}
wxString message;
message.Printf ( ngettext ( L"%i match", L"%i matches", i ), i );
status->SetStatusText ( message );
if ( i )
table->SetFocus();
}
void StyleDialog::OnStyleEdit ( wxCommandEvent& event )
{
updateSizeInformation();
std::vector<ContextMatch> v;
getSelectedMatches ( v );
if ( v.empty() )
{
status->SetStatusText ( _ ( "No items selected" ) );
return;
}
sort ( v.begin(), v.end(), elementAndOffsetCompareFunction );
HouseStyleWriter hsw ( v );
if ( !hsw.parse ( bufferUtf8 ) )
{
std::string error = hsw.getLastError();
wxString wideError = wxString (
error.c_str(),
wxConvUTF8,
error.size() );
status->SetStatusText ( wideError );
return;
}
bufferUtf8 = hsw.getOutput();
wxCommandEvent e;
EndModal ( wxID_OK );//OnOK(e);
}
std::string StyleDialog::getEditedString()
{
return bufferUtf8;
}
void StyleDialog::OnStyleIgnoreAll ( wxCommandEvent& event )
{
styleSetIgnoreAll ( true );
}
void StyleDialog::OnStyleChangeAll ( wxCommandEvent& event )
{
styleSetIgnoreAll ( false );
}
void StyleDialog::OnStyleWebReport ( wxCommandEvent& event )
{
std::vector<ContextMatch> v;
getAllMatches ( v );
sort ( v.begin(), v.end(), reportCompareFunction );
// temporary file should be in default temporary folder
wxString tempNameWide = wxFileName::CreateTempFileName ( _T ( "" ) );
if ( tempNameWide.empty() )
return;
tempNameWide.Replace ( _T ( ".tmp" ), _T ( "_report.html" ), true );
tempFiles.insert ( tempNameWide );
std::string tempNameUtf8 = ( const char * ) tempNameWide.mb_str ( wxConvUTF8 );
std::ofstream ofs ( tempNameUtf8.c_str() );
if ( !ofs )
return;
ofs << XHTML_START;
ofs << "<body><h2>";
ofs << fileName.mb_str ( wxConvUTF8 );
ofs << "</h2><table><tr>";
ofs << "<td><b align=\"right\">No.</b></td>";
ofs << "<td align=\"right\"><b>Context</b></td>";
ofs << "<td align=\"center\"><b>Match</b></td>";
ofs << "<td align=\"left\"><b>Context</b></td>";
ofs << "<td><b>Suggestion</b></td><td><b>Report</b></td></tr>";
std::vector<ContextMatch>::iterator it;
int matchCount = 0;
for ( it = v.begin(); it != v.end(); it++ )
{
ofs << "<tr><td align=\"right\">";
ofs << ++matchCount;
ofs << "</td>";
ofs << "<td align=\"right\">";
ofs << it->prelog;
ofs << "</td><td align=\"center\"><font color=\"red\"><b>";
ofs << it->match;
ofs << "</b></font></td><td align=\"left\">";
ofs << it->postlog;
ofs << "</td><td><font color=\"green\"><b>";
ofs << it->replace;
ofs << "</b></font></td><td>";
ofs << it->report;
ofs << "</td></tr>";
}
ofs << "</table></body>";
ofs << XHTML_END;
ofs.close();
// display file in browser
if ( !wxFileName::FileExists ( tempNameWide ) )
return;
wxString cmd = browser + _T ( " \"" ) + tempNameWide + _T ( "\"" );
wxExecute ( cmd, wxEXEC_SYNC );
}
void StyleDialog::OnStyleWebSummary ( wxCommandEvent& event )
{
std::vector<ContextMatch> v;
getAllMatches ( v );
std::map<std::string, int, NoCaseCompare> matchMap;
std::vector<ContextMatch>::iterator vectorIterator;
for (
vectorIterator = v.begin();
vectorIterator != v.end();
vectorIterator++ )
{
if ( ( matchMap.find ( vectorIterator->match ) ) != matchMap.end() )
++ ( matchMap[vectorIterator->match] );
else
matchMap[vectorIterator->match] = 1;
}
// temporary file should be in default temporary folder
wxString tempNameWide = wxFileName::CreateTempFileName ( _T ( "" ) );
if ( tempNameWide.empty() )
return;
tempNameWide.Replace ( _T ( ".tmp" ), _T ( "_summary.html" ), true );
tempFiles.insert ( tempNameWide );
std::string tempNameUtf8 = ( const char * ) tempNameWide.mb_str ( wxConvUTF8 );
std::ofstream ofs ( tempNameUtf8.c_str() );
if ( !ofs )
return;
ofs << XHTML_START;
ofs << "<body><h2>";
ofs << fileName.mb_str ( wxConvUTF8 );
ofs << "</h2>";
WrapExpat we;
ofs << "<table><tr><th>Match</th><th>Frequency</th></tr>";
std::map<std::string, int>::iterator mapIterator;
int matchTotal = 0;
for (
mapIterator = matchMap.begin();
mapIterator != matchMap.end();
mapIterator++ )
{
ofs << "<tr><td>";
ofs << we.xmliseTextNode ( mapIterator->first );
ofs << "</td><td>";
// handle number of matches
matchTotal += mapIterator->second;
ofs << mapIterator->second;
ofs << "</td></tr>";
}
ofs << "<tr><th>Total</th><th>";
ofs << matchTotal;
ofs << "</th></tr></table>";
ofs << XHTML_END;
ofs.close();
// display file in browser
if ( !wxFileName::FileExists ( tempNameWide ) )
return;
wxString cmd = browser + _T ( " \"" ) + tempNameWide + _T ( "\"" );
wxExecute ( cmd, wxEXEC_SYNC );
}
void StyleDialog::styleSetIgnoreAll ( bool ignore )
{
int count = table->GetItemCount();
for ( int i = 0; i < count; ++i )
setIgnore ( i, ignore );
}
bool StyleDialog::isIgnore ( int item )
{
wxString field = getTextByColumn ( table, item, 6 );
return ( field == _ ( "Ignore" ) ) ? true : false;
}
void StyleDialog::setIgnore ( int item, bool ignore )
{
table->SetItem ( item, 6, ( ignore ) ? _ ( "Ignore" ) : _ ( "Change" ) );
table->SetItemTextColour ( item, ( ignore ) ? *wxBLUE : *wxBLACK );
}
void StyleDialog::getAllMatches ( vector<ContextMatch> &v )
{
v.clear();
int count = table->GetItemCount();
wxString matchNoString;
long matchNo;
for ( int i = 0; i < count; ++i )
{
matchNoString = table->GetItemText ( i );
if ( !matchNoString.ToLong ( &matchNo ) || matchNo < 1 )
continue;
v.push_back ( matchVector.at ( matchNo - 1 ) ); // vector index starts at 0
}
}
void StyleDialog::getSelectedMatches ( vector<ContextMatch> &v )
{
v.clear();
int count = table->GetItemCount();
wxString selectionString, matchNoString;
long matchNo;
for ( int i = 0; i < count; ++i )
{
selectionString = getTextByColumn ( table, i, 6 );
if ( selectionString != _ ( "Change" ) )
continue;
matchNoString = table->GetItemText ( i );
if ( !matchNoString.ToLong ( &matchNo ) || matchNo < 1 )
continue;
v.push_back ( matchVector.at ( matchNo - 1 ) ); // vector index starts at 0
}
}
int wxCALLBACK StyleDialog::MyCompareFunction (
long item1,
long item2,
long sortData )
{
SortData *data = ( SortData * ) sortData;
int column;
wxListCtrl *table;
column = data->column;
table = data->table;
wxString string1 = getTextByColumn ( table, item1, column );
wxString string2 = getTextByColumn ( table, item2, column );
// special case: numerical content
if ( string1.IsNumber() && string2.IsNumber() )
{
long value1, value2;
string1.ToLong ( &value1 );
string2.ToLong ( &value2 );
if ( value1 < value2 )
return -1;
else if ( value1 > value2 )
return 1;
else
return 0;
}
if ( string1.CmpNoCase ( string2 ) < 0 )
return -1;
else if ( string1.CmpNoCase ( string2 ) > 0 )
return 1;
else
return 0;
}
bool StyleDialog::elementAndOffsetCompareFunction (
ContextMatch m1,
ContextMatch m2 )
{
if ( m1.elementCount == m2.elementCount )
return ( m1.offset < m2.offset );
return ( m1.elementCount < m2.elementCount );
}
bool StyleDialog::reportCompareFunction ( ContextMatch m1, ContextMatch m2 )
{
return ( m1.report < m2.report );
}
wxString StyleDialog::getTextByColumn ( wxListCtrl *table, long index, int col )
{
wxListItem Item;
Item.SetId ( index );
Item.SetColumn ( col );
Item.SetMask ( wxLIST_MASK_TEXT );
table->GetItem ( Item );
return Item.GetText();
}
void StyleDialog::OnUpdateTableRange ( wxUpdateUIEvent& event )
{
if ( event.GetId() == ID_STYLE_EDIT && readOnly )
{
event.Enable ( false );
return;
}
event.Enable ( table->GetItemCount() );
}
void StyleDialog::OnMenuChangeOnce ( wxCommandEvent& event )
{
setIgnore ( indexForContextMenu, false );
}
void StyleDialog::OnMenuChangeAll ( wxCommandEvent& event )
{
wxString match, suggestion;
match = getTextByColumn ( table, indexForContextMenu, 2 );
suggestion = getTextByColumn ( table, indexForContextMenu, 4 );
long itemCount = table->GetItemCount();
for ( int i = 0; i < itemCount; ++i )
{
if ( getTextByColumn ( table, i, 2 ) == match )
setIgnore ( i, false );
}
}
void StyleDialog::OnMenuIgnoreOnce ( wxCommandEvent& event )
{
setIgnore ( indexForContextMenu, true );
}
void StyleDialog::OnMenuIgnoreAll ( wxCommandEvent& event )
{
wxString match, suggestion;
match = getTextByColumn ( table, indexForContextMenu, 2 );
suggestion = getTextByColumn ( table, indexForContextMenu, 4 );
long itemCount = table->GetItemCount();
for ( int i = 0; i < itemCount; ++i )
{
if ( getTextByColumn ( table, i, 2 ) == match )
{
table->SetItem ( i, 4, suggestion );
setIgnore ( i, true );
}
}
}
void StyleDialog::OnMenuNewSuggestion ( wxCommandEvent& event )
{
wxString suggestion = getTextByColumn ( table, indexForContextMenu, 4 );
wxTextEntryDialog *dlg = new wxTextEntryDialog (
this,
_ ( "Enter new suggestion:" ),
_ ( "New Suggestion" ),
suggestion );
if ( !dlg )
return;
int ret = dlg->ShowModal();
if ( ret == wxID_CANCEL )
return;
// identify item in match vector
wxString noString = getTextByColumn ( table, indexForContextMenu, 0 );
long no;
if ( !noString.ToLong ( &no ) || no < 1 || no > ( long ) matchVector.size() )
return;
--no; // reverse display offset
wxString wideReplace = dlg->GetValue();
std::string replace = ( const char * ) wideReplace.mb_str ( wxConvUTF8 );
matchVector[no].replace = replace;
table->SetItem ( indexForContextMenu, 4, dlg->GetValue() );
setIgnore ( indexForContextMenu, false );
}
void StyleDialog::OnMenuApplySuggestionAll ( wxCommandEvent& event )
{
wxString match, suggestion;
match = getTextByColumn ( table, indexForContextMenu, 2 );
suggestion = getTextByColumn ( table, indexForContextMenu, 4 );
long itemCount = table->GetItemCount();
for ( int i = 0; i < itemCount; ++i )
{
if ( getTextByColumn ( table, i, 2 ) != match )
continue;
// identify item in match vector
wxString noString = getTextByColumn ( table, i, 0 );
long no;
if ( !noString.ToLong ( &no ) || no < 1 || no > ( long ) matchVector.size() )
continue;
--no; // reverse display offset
std::string replace = ( const char * ) suggestion.mb_str ( wxConvUTF8 );
matchVector[no].replace = replace;
table->SetItem ( i, 4, suggestion );
setIgnore ( i, false );
}
}
std::string StyleDialog::flatWhiteSpace ( std::string& s )
{
std::string::iterator it;
std::string output;
output.reserve ( s.size() );
for ( it = s.begin(); it != s.end(); it++ )
{
if ( *it == '\t' || *it == '\r' || *it == '\n' )
output += ' ';
else
output += *it;
}
return output;
}
void StyleDialog::updateSizeInformation()
{
framePosition = GetPosition();
frameSize = GetSize();
}
wxPoint StyleDialog::getPosition()
{
return framePosition;
}
wxSize StyleDialog::getSize()
{
return frameSize;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.