1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        MadSearchDialog.cpp
3 // Description:
4 // Author:      madedit@gmail.com
5 // Licence:     GPL
6 ///////////////////////////////////////////////////////////////////////////////
7 
8 #include "MadSearchDialog.h"
9 #include "MadReplaceDialog.h"
10 
11 #include "MadEdit/MadEdit.h"
12 
13 //Do not add custom headers.
14 //wx-dvcpp designer will remove them
15 ////Header Include Start
16 ////Header Include End
17 
18 
19 #include "../images/down.xpm"
20 
21 MadSearchDialog *g_SearchDialog=NULL;
22 
23 //----------------------------------------------------------------------------
24 // MadSearchDialog
25 //----------------------------------------------------------------------------
26      //Add Custom Events only in the appropriate Block.
27     // Code added in  other places will be removed by wx-dvcpp
28     ////Event Table Start
BEGIN_EVENT_TABLE(MadSearchDialog,wxDialog)29 BEGIN_EVENT_TABLE(MadSearchDialog,wxDialog)
30 	////Manual Code Start
31 	EVT_BUTTON(ID_WXBITMAPBUTTONRECENTFINDTEXT, MadSearchDialog::WxBitmapButtonRecentFindTextClick)
32 	EVT_MENU_RANGE(ID_RECENTFINDTEXT1, ID_RECENTFINDTEXT20, MadSearchDialog::OnRecentFindText)
33 	////Manual Code End
34 
35 	EVT_CLOSE(MadSearchDialog::MadSearchDialogClose)
36 	EVT_KEY_DOWN(MadSearchDialog::MadSearchDialogKeyDown)
37 	EVT_ACTIVATE(MadSearchDialog::MadSearchDialogActivate)
38 	EVT_BUTTON(ID_WXBUTTONCLOSE,MadSearchDialog::WxButtonCloseClick)
39 	EVT_BUTTON(ID_WXBUTTONREPLACE,MadSearchDialog::WxButtonReplaceClick)
40 	EVT_BUTTON(ID_WXBUTTONCOUNT,MadSearchDialog::WxButtonCountClick)
41 	EVT_BUTTON(ID_WXBUTTONFINDPREV,MadSearchDialog::WxButtonFindPrevClick)
42 	EVT_BUTTON(ID_WXBUTTONFINDNEXT,MadSearchDialog::WxButtonFindNextClick)
43 	EVT_CHECKBOX(ID_WXCHECKBOXSEARCHINSELECTION,MadSearchDialog::WxCheckBoxSearchInSelectionClick)
44 	EVT_CHECKBOX(ID_WXCHECKBOXFINDHEX,MadSearchDialog::WxCheckBoxFindHexClick)
45 END_EVENT_TABLE()
46     ////Event Table End
47 
48 
49 
50 MadSearchDialog::MadSearchDialog( wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style )
51     : wxDialog( parent, id, title, position, size, style)
52 {
53     CreateGUIControls();
54 }
55 
~MadSearchDialog()56 MadSearchDialog::~MadSearchDialog()
57 {
58 
59 }
60 
61 //static int gs_MinX=0;
62 
ResizeItem(wxBoxSizer * sizer,wxWindow * item,int ax,int ay)63 static void ResizeItem(wxBoxSizer* sizer, wxWindow *item, int ax, int ay)
64 {
65     int x, y;
66     wxString str=item->GetLabel();
67     item->GetTextExtent(str, &x, &y);
68     item->SetSize(x+=ax, y+=ay);
69     sizer->SetItemMinSize(item, x, y);
70 
71     //wxPoint pos=item->GetPosition();
72     //if(pos.x + x > gs_MinX) gs_MinX = pos.x + x;
73 }
74 
CreateGUIControls(void)75 void MadSearchDialog::CreateGUIControls(void)
76 {
77     //do not set FontName, it is not exist on all platforms
78     #define wxFont(p0,p1,p2,p3,p4,p5) wxFont(wxDEFAULT,wxDEFAULT,p2,p3,p4)
79 
80     //Do not add custom Code here
81     //wx-devcpp designer will remove them.
82     //Add the custom code before or after the Blocks
83     ////GUI Items Creation Start
84 
85 	WxBoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
86 	this->SetSizer(WxBoxSizer1);
87 	this->SetAutoLayout(true);
88 
89 	WxBoxSizer2 = new wxBoxSizer(wxVERTICAL);
90 	WxBoxSizer1->Add(WxBoxSizer2, 0, wxALIGN_CENTER | wxALL, 0);
91 
92 	WxBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
93 	WxBoxSizer2->Add(WxBoxSizer4, 0, wxALIGN_CENTER | wxALL, 0);
94 
95 	WxBoxSizer5 = new wxBoxSizer(wxVERTICAL);
96 	WxBoxSizer2->Add(WxBoxSizer5, 0, wxALIGN_LEFT | wxALIGN_TOP | wxALL, 0);
97 
98 	WxCheckBoxMoveFocus = new wxCheckBox(this, ID_WXCHECKBOXMOVEFOCUS, _("&Move Focus to Editor Window"), wxPoint(25, 2), wxSize(300, 22), 0, wxDefaultValidator, _("WxCheckBoxMoveFocus"));
99 	WxCheckBoxMoveFocus->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
100 	WxBoxSizer5->Add(WxCheckBoxMoveFocus,0,wxALIGN_LEFT | wxALL,2);
101 
102 	WxCheckBoxCaseSensitive = new wxCheckBox(this, ID_WXCHECKBOXCASESENSITIVE, _("&Case Sensitive"), wxPoint(25, 28), wxSize(300, 22), 0, wxDefaultValidator, _("WxCheckBoxCaseSensitive"));
103 	WxCheckBoxCaseSensitive->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
104 	WxBoxSizer5->Add(WxCheckBoxCaseSensitive,0,wxALIGN_LEFT | wxALL,2);
105 
106 	WxCheckBoxWholeWord = new wxCheckBox(this, ID_WXCHECKBOXWHOLEWORD, _("&Whole Word Only"), wxPoint(25, 54), wxSize(300, 22), 0, wxDefaultValidator, _("WxCheckBoxWholeWord"));
107 	WxCheckBoxWholeWord->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
108 	WxBoxSizer5->Add(WxCheckBoxWholeWord,0,wxALIGN_LEFT | wxALL,2);
109 
110 	WxCheckBoxRegex = new wxCheckBox(this, ID_WXCHECKBOXREGEX, _("Use Regular E&xpressions"), wxPoint(25, 80), wxSize(300, 22), 0, wxDefaultValidator, _("WxCheckBoxRegex"));
111 	WxCheckBoxRegex->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
112 	WxBoxSizer5->Add(WxCheckBoxRegex,0,wxALIGN_LEFT | wxALL,2);
113 
114 	WxCheckBoxFindHex = new wxCheckBox(this, ID_WXCHECKBOXFINDHEX, _("Find &Hex String (Example: BE 00 3A or BE003A)"), wxPoint(25, 106), wxSize(300, 22), 0, wxDefaultValidator, _("WxCheckBoxFindHex"));
115 	WxCheckBoxFindHex->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
116 	WxBoxSizer5->Add(WxCheckBoxFindHex,0,wxALIGN_LEFT | wxALL,2);
117 
118 	WxBoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
119 	WxBoxSizer5->Add(WxBoxSizer6, 0, wxALIGN_LEFT | wxALL, 0);
120 
121 	WxCheckBoxSearchInSelection = new wxCheckBox(this, ID_WXCHECKBOXSEARCHINSELECTION, _("Search In &Selection"), wxPoint(2, 2), wxSize(120, 22), 0, wxDefaultValidator, _("WxCheckBoxSearchInSelection"));
122 	WxCheckBoxSearchInSelection->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
123 	WxBoxSizer6->Add(WxCheckBoxSearchInSelection,0,wxALIGN_LEFT | wxALL,2);
124 
125 	WxStaticTextFrom = new wxStaticText(this, ID_WXSTATICTEXTFROM, _("From:"), wxPoint(126, 4), wxDefaultSize, 0, _("WxStaticTextFrom"));
126 	WxStaticTextFrom->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
127 	WxBoxSizer6->Add(WxStaticTextFrom,0,wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL,2);
128 
129 	WxEditFrom = new wxTextCtrl(this, ID_WXEDITFROM, _(""), wxPoint(160, 2), wxSize(80, 22), 0, wxDefaultValidator, _("WxEditFrom"));
130 	WxEditFrom->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
131 	WxBoxSizer6->Add(WxEditFrom,0,wxALIGN_LEFT | wxALL,2);
132 
133 	WxStaticTextTo = new wxStaticText(this, ID_WXSTATICTEXTTO, _("To:"), wxPoint(244, 4), wxDefaultSize, 0, _("WxStaticTextTo"));
134 	WxStaticTextTo->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
135 	WxBoxSizer6->Add(WxStaticTextTo,0,wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL,2);
136 
137 	WxEditTo = new wxTextCtrl(this, ID_WXEDITTO, _(""), wxPoint(268, 2), wxSize(80, 22), 0, wxDefaultValidator, _("WxEditTo"));
138 	WxEditTo->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
139 	WxBoxSizer6->Add(WxEditTo,0,wxALIGN_LEFT | wxALL,2);
140 
141 	WxBoxSizer3 = new wxBoxSizer(wxVERTICAL);
142 	WxBoxSizer1->Add(WxBoxSizer3, 0, wxALIGN_TOP | wxALL, 0);
143 
144 	WxButtonFindNext = new wxButton(this, ID_WXBUTTONFINDNEXT, _("Find &Next"), wxPoint(2, 2), wxSize(100, 28), 0, wxDefaultValidator, _("WxButtonFindNext"));
145 	WxButtonFindNext->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
146 	WxBoxSizer3->Add(WxButtonFindNext,0,wxALIGN_CENTER | wxALL,2);
147 
148 	WxButtonFindPrev = new wxButton(this, ID_WXBUTTONFINDPREV, _("Find &Previous"), wxPoint(2, 34), wxSize(100, 28), 0, wxDefaultValidator, _("WxButtonFindPrev"));
149 	WxButtonFindPrev->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
150 	WxBoxSizer3->Add(WxButtonFindPrev,0,wxALIGN_CENTER | wxALL,2);
151 
152 	WxButtonCount = new wxButton(this, ID_WXBUTTONCOUNT, _("C&ount"), wxPoint(2, 66), wxSize(100, 28), 0, wxDefaultValidator, _("WxButtonCount"));
153 	WxButtonCount->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
154 	WxBoxSizer3->Add(WxButtonCount,0,wxALIGN_CENTER | wxALL,2);
155 
156 	WxButtonReplace = new wxButton(this, ID_WXBUTTONREPLACE, _("&Replace >>"), wxPoint(2, 98), wxSize(100, 28), 0, wxDefaultValidator, _("WxButtonReplace"));
157 	WxButtonReplace->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
158 	WxBoxSizer3->Add(WxButtonReplace,0,wxALIGN_CENTER | wxALL,2);
159 
160 	WxButtonClose = new wxButton(this, ID_WXBUTTONCLOSE, _("Close"), wxPoint(2, 128), wxSize(100, 28), 0, wxDefaultValidator, _("WxButtonClose"));
161 	WxButtonClose->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxNORMAL, false, _("MS Sans Serif")));
162 	WxBoxSizer3->Add(WxButtonClose,0,wxALIGN_CENTER | wxALL,2);
163 
164 	WxPopupMenuRecentFindText = new wxMenu(_(""));
165 
166 	SetTitle(_("Search"));
167 	SetIcon(wxNullIcon);
168 
169 	GetSizer()->Layout();
170 	GetSizer()->Fit(this);
171 	GetSizer()->SetSizeHints(this);
172 
173     ////GUI Items Creation End
174 
175     //restore wxFont
176     #undef wxFont
177 
178     this->SetPosition(wxPoint(300,100));
179 
180     int bw, bh;
181     WxButtonFindNext->GetSize(&bw, &bh);
182 
183     m_FindText=new MadEdit(this, ID_MADEDIT, wxPoint(0, 0), wxSize(400, bh));
184     m_FindText->SetSingleLineMode(true);
185     m_FindText->SetEncoding(wxT("UTF-32LE"));
186     m_FindText->SetFixedWidthMode(false);
187     m_FindText->SetRecordCaretMovements(false);
188     m_FindText->SetInsertSpacesInsteadOfTab(false);
189     m_FindText->SetWantTab(false);
190     m_FindText->LoadDefaultSyntaxScheme();
191 
192     WxBoxSizer4->Add(m_FindText,0,wxALIGN_CENTER_HORIZONTAL | wxALL,2);
193     WxBoxSizer4->SetItemMinSize(m_FindText, 400, bh);
194 
195     wxBitmap WxBitmapButtonRecentFindText_BITMAP (down_xpm);
196     WxBitmapButtonRecentFindText = new wxBitmapButton(this, ID_WXBITMAPBUTTONRECENTFINDTEXT, WxBitmapButtonRecentFindText_BITMAP, wxPoint(0,0), wxSize(bh,bh), wxBU_AUTODRAW, wxDefaultValidator, _("WxBitmapButtonRecentFindText"));
197     WxBoxSizer4->Add(WxBitmapButtonRecentFindText,0,wxALIGN_CENTER_HORIZONTAL | wxALL,2);
198 
199     // resize checkbox
200     ResizeItem(WxBoxSizer5, WxCheckBoxMoveFocus, 25, 4);
201     ResizeItem(WxBoxSizer5, WxCheckBoxCaseSensitive, 25, 4);
202     ResizeItem(WxBoxSizer5, WxCheckBoxWholeWord, 25, 4);
203     ResizeItem(WxBoxSizer5, WxCheckBoxRegex, 25, 4);
204     ResizeItem(WxBoxSizer5, WxCheckBoxFindHex, 25, 4);
205     ResizeItem(WxBoxSizer6, WxCheckBoxSearchInSelection, 25, 4);
206     ResizeItem(WxBoxSizer6, WxStaticTextFrom, 2, 2);
207     ResizeItem(WxBoxSizer6, WxStaticTextTo, 2, 2);
208 
209     GetSizer()->Fit(this);
210 
211     // connect to KeyDown event handler
212     m_FindText->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
213     WxBitmapButtonRecentFindText->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
214     WxCheckBoxMoveFocus->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
215     WxCheckBoxCaseSensitive->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
216     WxCheckBoxWholeWord->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
217     WxCheckBoxRegex->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
218     WxCheckBoxFindHex->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
219     WxCheckBoxSearchInSelection->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
220     WxEditFrom->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
221     WxEditTo->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
222     WxButtonFindNext->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
223     WxButtonFindPrev->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
224     WxButtonCount->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
225     WxButtonClose->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MadSearchDialog::MadSearchDialogKeyDown));
226 
227 
228     m_RecentFindText=new wxFileHistory(20, ID_RECENTFINDTEXT1);
229     m_RecentFindText->UseMenu(WxPopupMenuRecentFindText);
230 
231     wxConfigBase *m_Config=wxConfigBase::Get(false);
232     wxString oldpath=m_Config->GetPath();
233     m_Config->SetPath(wxT("/RecentFindText"));
234     m_RecentFindText->Load(*m_Config);
235     m_Config->SetPath(oldpath);
236 
237     if(m_RecentFindText->GetCount()>0)
238     {
239         wxString text=m_RecentFindText->GetHistoryFile(0);
240         if(!text.IsEmpty())
241         {
242             m_FindText->SetText(text);
243         }
244     }
245 
246     SetDefaultItem(WxButtonFindNext);
247 }
248 
MadSearchDialogClose(wxCloseEvent & event)249 void MadSearchDialog::MadSearchDialogClose(wxCloseEvent& event)
250 {
251     // --> Don't use Close with a Dialog,
252     // use Destroy instead.
253 
254     if(event.CanVeto())
255     {
256         event.Veto();
257         Show(false);
258         return;
259     }
260 
261     g_SearchDialog=NULL;
262     Destroy();
263 }
264 
265 
266 /*
267  * WxButtonCloseClick
268  */
WxButtonCloseClick(wxCommandEvent & event)269 void MadSearchDialog::WxButtonCloseClick(wxCommandEvent& event)
270 {
271     Show(false);
272 }
273 
274 /*
275  * WxButtonFindNextClick
276  */
WxButtonFindNextClick(wxCommandEvent & event)277 void MadSearchDialog::WxButtonFindNextClick(wxCommandEvent& event)
278 {
279     extern MadEdit *g_ActiveMadEdit;
280 
281     if(g_ActiveMadEdit==NULL)
282         return;
283 
284     wxString text;
285     m_FindText->GetText(text, true);
286 
287     if(text.Len()>0)
288     {
289         m_RecentFindText->AddFileToHistory(text);
290 
291         MadSearchResult sr;
292         wxFileOffset selend = g_ActiveMadEdit->GetSelectionEndPos();
293 
294         wxInt64 from = 0, to = 0;
295         wxFileOffset rangeFrom = -1, rangeTo = -1;
296         if(WxCheckBoxSearchInSelection->IsChecked())
297         {
298             if(!StrToInt64(WxEditFrom->GetValue(), from))
299             {
300                 wxMessageBox(_("The value of 'From' is incorrect."), wxT("MadEdit"), wxOK|wxICON_WARNING);
301                 return;
302             }
303             if(!StrToInt64(WxEditTo->GetValue(), to))
304             {
305                 wxMessageBox(_("The value of 'To' is incorrect."), wxT("MadEdit"), wxOK|wxICON_WARNING);
306                 return;
307             }
308 
309             rangeTo = to;
310             wxFileOffset caretpos = g_ActiveMadEdit->GetCaretPosition();
311             if(caretpos <= from || caretpos > to)
312                 rangeFrom = from;
313         }
314 
315         for(;;)
316         {
317             if(WxCheckBoxFindHex->GetValue())
318             {
319                 sr=g_ActiveMadEdit->FindHexNext(text, rangeFrom, rangeTo);
320             }
321             else
322             {
323                 sr=g_ActiveMadEdit->FindTextNext(text,
324                     WxCheckBoxRegex->GetValue(),
325                     WxCheckBoxCaseSensitive->GetValue(),
326                     WxCheckBoxWholeWord->GetValue(),
327                     rangeFrom, rangeTo);
328             }
329 
330             if(sr != SR_NO)
331             {
332                 if(sr == SR_YES && g_ActiveMadEdit->GetCaretPosition() == selend)
333                 {
334                     selend = -1;
335                     continue;
336                 }
337                 break;
338             }
339 
340             wxString msg(_("Cannot find the matched string."));
341             msg += wxT("\n\n");
342             msg += WxCheckBoxSearchInSelection->IsChecked()?
343                 _("Do you want to find from begin of selection?"):
344                 _("Do you want to find from begin of file?");
345 
346             if(wxCANCEL == wxMessageBox(msg, _("Find Next"), wxOK|wxCANCEL|wxICON_QUESTION ))
347             {
348                 break;
349             }
350             rangeFrom = WxCheckBoxSearchInSelection->IsChecked()? from : 0;
351         }
352     }
353 
354     if(WxCheckBoxMoveFocus->GetValue())
355     {
356         ((wxFrame*)wxTheApp->GetTopWindow())->Raise();
357         g_ActiveMadEdit->SetFocus();
358     }
359 }
360 
361 /*
362  * WxButtonFindPrevClick
363  */
WxButtonFindPrevClick(wxCommandEvent & event)364 void MadSearchDialog::WxButtonFindPrevClick(wxCommandEvent& event)
365 {
366     extern MadEdit *g_ActiveMadEdit;
367 
368     if(g_ActiveMadEdit==NULL)
369         return;
370 
371     wxString text;
372     m_FindText->GetText(text, true);
373 
374     if(text.Len()>0)
375     {
376         m_RecentFindText->AddFileToHistory(text);
377 
378         MadSearchResult sr;
379         wxFileOffset selbeg = g_ActiveMadEdit->GetSelectionBeginPos();
380 
381         wxInt64 from = 0, to = 0;
382         wxFileOffset rangeFrom = -1, rangeTo = -1;
383         if(WxCheckBoxSearchInSelection->IsChecked())
384         {
385             if(!StrToInt64(WxEditFrom->GetValue(), from))
386             {
387                 wxMessageBox(_("The value of 'From' is incorrect."), wxT("MadEdit"), wxOK|wxICON_WARNING);
388                 return;
389             }
390             if(!StrToInt64(WxEditTo->GetValue(), to))
391             {
392                 wxMessageBox(_("The value of 'To' is incorrect."), wxT("MadEdit"), wxOK|wxICON_WARNING);
393                 return;
394             }
395 
396             rangeFrom = from;
397             wxFileOffset caretpos = g_ActiveMadEdit->GetCaretPosition();
398             if(caretpos < from || caretpos >= to)
399                 rangeTo = to;
400         }
401 
402         for(;;)
403         {
404             if(WxCheckBoxFindHex->GetValue())
405             {
406                 sr=g_ActiveMadEdit->FindHexPrevious(text, rangeTo, rangeFrom);
407             }
408             else
409             {
410                 sr=g_ActiveMadEdit->FindTextPrevious(text,
411                     WxCheckBoxRegex->GetValue(),
412                     WxCheckBoxCaseSensitive->GetValue(),
413                     WxCheckBoxWholeWord->GetValue(),
414                     rangeTo, rangeFrom);
415             }
416 
417             if(sr!=SR_NO)
418             {
419                 if(sr == SR_YES && g_ActiveMadEdit->GetCaretPosition() == selbeg)
420                 {
421                     selbeg = -1;
422                     continue;
423                 }
424                 break;
425             }
426 
427             wxString msg(_("Cannot find the matched string."));
428             msg += wxT("\n\n");
429             msg += WxCheckBoxSearchInSelection->IsChecked()?
430                 _("Do you want to find from end of selection?"):
431                 _("Do you want to find from end of file?");
432 
433             if(wxCANCEL==wxMessageBox(msg, _("Find Previous"), wxOK|wxCANCEL|wxICON_QUESTION ))
434             {
435                 break;
436             }
437             rangeTo = WxCheckBoxSearchInSelection->IsChecked()? to: g_ActiveMadEdit->GetFileSize();
438         }
439     }
440 
441     if(WxCheckBoxMoveFocus->GetValue())
442     {
443         ((wxFrame*)wxTheApp->GetTopWindow())->Raise();
444         g_ActiveMadEdit->SetFocus();
445     }
446 }
447 
448 
449 /*
450  * MadSearchDialogKeyDown
451  */
MadSearchDialogKeyDown(wxKeyEvent & event)452 void MadSearchDialog::MadSearchDialogKeyDown(wxKeyEvent& event)
453 {
454     int key=event.GetKeyCode();
455 
456     //g_SearchDialog->SetTitle(wxString()<<key);
457 
458     switch(key)
459     {
460     case WXK_ESCAPE:
461         g_SearchDialog->Show(false);
462         return;
463     case WXK_RETURN:
464     case WXK_NUMPAD_ENTER:
465         if((wxButton*)this!=g_SearchDialog->WxButtonFindNext &&
466            (wxButton*)this!=g_SearchDialog->WxButtonFindPrev &&
467            (wxButton*)this!=g_SearchDialog->WxButtonClose)
468         {
469             wxCommandEvent e;
470             g_SearchDialog->WxButtonFindNextClick(e);
471             return; // no skip
472         }
473         break;
474     case WXK_DOWN:
475         if((MadEdit*)this==g_SearchDialog->m_FindText)
476         {
477             int x,y,w,h;
478             g_SearchDialog->m_FindText->GetPosition(&x, &y);
479             g_SearchDialog->m_FindText->GetSize(&w, &h);
480             g_SearchDialog->PopupMenu(g_SearchDialog->WxPopupMenuRecentFindText, x, y+h);
481             return;
482         }
483         break;
484     }
485 
486     extern wxAcceleratorEntry g_AccelFindNext, g_AccelFindPrev;
487     int flags=wxACCEL_NORMAL;
488     if(event.m_altDown) flags|=wxACCEL_ALT;
489     if(event.m_shiftDown) flags|=wxACCEL_SHIFT;
490     if(event.m_controlDown) flags|=wxACCEL_CTRL;
491 
492     if(g_AccelFindNext.GetKeyCode()==key && g_AccelFindNext.GetFlags()==flags)
493     {
494         wxCommandEvent e;
495         g_SearchDialog->WxButtonFindNextClick(e);
496         return; // no skip
497     }
498 
499     if(g_AccelFindPrev.GetKeyCode()==key && g_AccelFindPrev.GetFlags()==flags)
500     {
501         wxCommandEvent e;
502         g_SearchDialog->WxButtonFindPrevClick(e);
503         return; // no skip
504     }
505 
506     event.Skip();
507 }
508 
509 /*
510  * WxBitmapButtonRecentFindTextClick
511  */
WxBitmapButtonRecentFindTextClick(wxCommandEvent & event)512 void MadSearchDialog::WxBitmapButtonRecentFindTextClick(wxCommandEvent& event)
513 {
514     PopupMenu(WxPopupMenuRecentFindText);
515 }
516 
OnRecentFindText(wxCommandEvent & event)517 void MadSearchDialog::OnRecentFindText(wxCommandEvent& event)
518 {
519     int idx=event.GetId()-ID_RECENTFINDTEXT1;
520     wxString text=m_RecentFindText->GetHistoryFile(idx);
521     if(!text.IsEmpty())
522     {
523         m_FindText->SetText(text);
524         m_FindText->SetFocus();
525     }
526 }
527 
ReadWriteSettings(bool bRead)528 void MadSearchDialog::ReadWriteSettings(bool bRead)
529 {
530     wxConfigBase *m_Config=wxConfigBase::Get(false);
531     wxString oldpath=m_Config->GetPath();
532 
533     if(bRead)
534     {
535         bool bb;
536         m_Config->Read(wxT("/MadEdit/SearchMoveFocus"), &bb, false);
537         WxCheckBoxMoveFocus->SetValue(bb);
538 
539         m_Config->Read(wxT("/MadEdit/SearchCaseSensitive"), &bb, false);
540         WxCheckBoxCaseSensitive->SetValue(bb);
541 
542         m_Config->Read(wxT("/MadEdit/SearchWholeWord"), &bb, false);
543         WxCheckBoxWholeWord->SetValue(bb);
544 
545         m_Config->Read(wxT("/MadEdit/SearchRegex"), &bb, false);
546         WxCheckBoxRegex->SetValue(bb);
547 
548         m_Config->Read(wxT("/MadEdit/SearchHex"), &bb, false);
549         WxCheckBoxFindHex->SetValue(bb);
550         UpdateCheckBoxByCBHex(bb);
551 
552         m_Config->Read(wxT("/MadEdit/SearchInSelection"), &bb, false);
553         WxCheckBoxSearchInSelection->SetValue(bb);
554         UpdateSearchInSelection(bb);
555 
556         wxString str;
557         m_Config->Read(wxT("/MadEdit/SearchFrom"), &str, wxEmptyString);
558         WxEditFrom->SetValue(str);
559         m_Config->Read(wxT("/MadEdit/SearchTo"), &str, wxEmptyString);
560         WxEditTo->SetValue(str);
561     }
562     else
563     {
564         m_Config->Write(wxT("/MadEdit/SearchMoveFocus"), WxCheckBoxMoveFocus->GetValue());
565         m_Config->Write(wxT("/MadEdit/SearchCaseSensitive"), WxCheckBoxCaseSensitive->GetValue());
566         m_Config->Write(wxT("/MadEdit/SearchWholeWord"), WxCheckBoxWholeWord->GetValue());
567         m_Config->Write(wxT("/MadEdit/SearchRegex"), WxCheckBoxRegex->GetValue());
568         m_Config->Write(wxT("/MadEdit/SearchHex"), WxCheckBoxFindHex->GetValue());
569 
570         m_Config->Write(wxT("/MadEdit/SearchInSelection"), WxCheckBoxSearchInSelection->GetValue());
571         m_Config->Write(wxT("/MadEdit/SearchFrom"), WxEditFrom->GetValue());
572         m_Config->Write(wxT("/MadEdit/SearchTo"), WxEditTo->GetValue());
573     }
574 
575     m_Config->SetPath(oldpath);
576 }
577 
UpdateCheckBoxByCBHex(bool check)578 void MadSearchDialog::UpdateCheckBoxByCBHex(bool check)
579 {
580     if(check)
581     {
582         WxCheckBoxCaseSensitive->Disable();
583         WxCheckBoxWholeWord->Disable();
584         WxCheckBoxRegex->Disable();
585     }
586     else
587     {
588         WxCheckBoxCaseSensitive->Enable();
589         WxCheckBoxWholeWord->Enable();
590         WxCheckBoxRegex->Enable();
591     }
592 }
593 
594 /*
595  * WxCheckBoxFindHexClick
596  */
WxCheckBoxFindHexClick(wxCommandEvent & event)597 void MadSearchDialog::WxCheckBoxFindHexClick(wxCommandEvent& event)
598 {
599     UpdateCheckBoxByCBHex(event.IsChecked());
600 }
601 
602 /*
603  * MadSearchDialogActivate
604  */
MadSearchDialogActivate(wxActivateEvent & event)605 void MadSearchDialog::MadSearchDialogActivate(wxActivateEvent& event)
606 {
607     ReadWriteSettings(event.GetActive());
608 }
609 
610 
611 /*
612  * WxButtonReplaceClick
613  */
WxButtonReplaceClick(wxCommandEvent & event)614 void MadSearchDialog::WxButtonReplaceClick(wxCommandEvent& event)
615 {
616     wxString fname;
617     int fsize;
618     m_FindText->GetFont(fname, fsize);
619     g_ReplaceDialog->m_FindText->SetFont(fname, 14);
620     g_ReplaceDialog->m_ReplaceText->SetFont(fname, 14);
621 
622     wxString text;
623     m_FindText->GetText(text, true);
624     g_ReplaceDialog->m_FindText->SetText(text);
625 
626     this->Show(false);
627     g_ReplaceDialog->Show();
628     g_ReplaceDialog->SetFocus();
629     g_ReplaceDialog->Raise();
630 
631     g_ReplaceDialog->UpdateCheckBoxByCBHex(g_ReplaceDialog->WxCheckBoxFindHex->GetValue());
632 
633     g_ReplaceDialog->m_FindText->SelectAll();
634     g_ReplaceDialog->m_FindText->SetFocus();
635 
636 }
637 
UpdateSearchInSelection(bool check)638 void MadSearchDialog::UpdateSearchInSelection(bool check)
639 {
640     WxEditFrom->Enable(check);
641     WxEditTo->Enable(check);
642 
643     extern MadEdit *g_ActiveMadEdit;
644     if(check && g_ActiveMadEdit!=NULL)
645     {
646         WxEditFrom->SetValue(wxLongLong(g_ActiveMadEdit->GetSelectionBeginPos()).ToString());
647         WxEditTo->SetValue(wxLongLong(g_ActiveMadEdit->GetSelectionEndPos()).ToString());
648     }
649 }
650 
651 
WxCheckBoxSearchInSelectionClick(wxCommandEvent & event)652 void MadSearchDialog::WxCheckBoxSearchInSelectionClick(wxCommandEvent& event)
653 {
654     UpdateSearchInSelection(event.IsChecked());
655 }
656 
WxButtonCountClick(wxCommandEvent & event)657 void MadSearchDialog::WxButtonCountClick(wxCommandEvent& event)
658 {
659     extern MadEdit *g_ActiveMadEdit;
660 
661     if(g_ActiveMadEdit==NULL)
662         return;
663 
664     int count = 0;
665     wxString text;
666     m_FindText->GetText(text, true);
667 
668     if(text.Len()>0)
669     {
670         m_RecentFindText->AddFileToHistory(text);
671 
672         wxInt64 from = 0, to = 0;
673         wxFileOffset rangeFrom = -1, rangeTo = -1;
674         if(WxCheckBoxSearchInSelection->IsChecked())
675         {
676             if(!StrToInt64(WxEditFrom->GetValue(), from))
677             {
678                 wxMessageBox(_("The value of 'From' is incorrect."), wxT("MadEdit"), wxOK|wxICON_WARNING);
679                 return;
680             }
681             if(!StrToInt64(WxEditTo->GetValue(), to))
682             {
683                 wxMessageBox(_("The value of 'To' is incorrect."), wxT("MadEdit"), wxOK|wxICON_WARNING);
684                 return;
685             }
686 
687             rangeTo = to;
688             rangeFrom = from;
689         }
690 
691         if(WxCheckBoxFindHex->GetValue())
692         {
693             count=g_ActiveMadEdit->FindHexAll(text, false, NULL, NULL, rangeFrom, rangeTo);
694         }
695         else
696         {
697             count=g_ActiveMadEdit->FindTextAll(text,
698                 WxCheckBoxRegex->GetValue(),
699                 WxCheckBoxCaseSensitive->GetValue(),
700                 WxCheckBoxWholeWord->GetValue(),
701                 false,
702                 NULL, NULL,
703                 rangeFrom, rangeTo);
704         }
705     }
706 
707     if(count >= 0)
708     {
709         wxString msg;
710         msg.Printf(_("'%s' was found %d times."), text.c_str(), count);
711         wxMessageBox(msg, wxT("MadEdit"), wxOK);
712     }
713 }
714