1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Application to test regular expression (wxRegEx)
7 // Copyright: (c) 2003 Ryan Norton <wxprojects@comcast.net>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 //===========================================================================
13 //===========================================================================
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
22 // for all others, include the necessary headers (this file is usually all you
23 // need because it includes almost all "standard" wxWindows headers)
29 // This is the required header for wxRegEx
33 //===========================================================================
35 //===========================================================================
37 //---------------------------------------------------------------------------
39 //---------------------------------------------------------------------------
41 class MyDialog
: public wxFrame
45 //IDs for our controls
64 MyDialog() : wxFrame( NULL
, -1, _("regextest - wxRegEx Testing App"),
65 wxPoint(20,20), wxSize(300,300) )
67 //Set the background to something light gray-ish
68 SetBackgroundColour(wxColour(150,150,150));
71 // Create the menus (Exit & About)
74 wxMenu
*FileMenu
= new wxMenu
;
75 OptionsMenu
= new wxMenu
;
76 wxMenu
*HelpMenu
= new wxMenu
;
78 FileMenu
->Append(wxID_EXIT
, _T("&Exit"), _("Quit this program"));
80 OptionsMenu
->AppendCheckItem(ExtendedID
, _T("wxRE_EXTENDED"), _("Extended Regular Expressions?"));
81 OptionsMenu
->AppendCheckItem(ICaseID
, _T("wxRE_ICASE"), _("Enable case-insensitive matching?"));
82 OptionsMenu
->AppendCheckItem(NewLineID
, _T("wxRE_NEWLINE"), _("Treat \n as special?"));
83 OptionsMenu
->AppendSeparator();
84 OptionsMenu
->AppendCheckItem(NotBolID
, _T("wxRE_NOTBOL"), _("Use functionality of ^ character?"));
85 OptionsMenu
->AppendCheckItem(NotEolID
, _T("wxRE_NOTEOL"), _("Use functionality of $ character?"));
86 OptionsMenu
->Check(ExtendedID
, true);
88 HelpMenu
->Append(wxID_ABOUT
, _T("&About...\tF1"), _("Show about dialog"));
90 wxMenuBar
*MenuBar
= new wxMenuBar();
91 MenuBar
->Append(FileMenu
, _T("&File"));
92 MenuBar
->Append(OptionsMenu
, _T("&Options"));
93 MenuBar
->Append(HelpMenu
, _T("&Help"));
99 PatternText
.Create(this, PatternTextID
, _(""), wxPoint(5, 30));
100 SearchText
.Create(this, SearchTextID
, _(""), wxPoint(5, 75));
101 //reset size of text controls
102 wxSize TextSize
= PatternText
.GetSize();
103 TextSize
.SetWidth(200);
104 PatternText
.SetSize(TextSize
);
105 SearchText
.SetSize(TextSize
);
108 PatternHeader
.Create(this, -1, _("Regular Expression:"), wxPoint(5, 10));
109 SearchHeader
.Create(this, -1, _("String to Search:"), wxPoint(5, 55));
110 ResultText
.Create(this, -1, _(""), wxPoint(5, 150));
113 OkButton
.Create(this, OkButtonID
, _("OK"), wxPoint(20, 120));
116 void OnAbout(wxCommandEvent
& WXUNUSED(evt
))
118 wxMessageBox(wxString::Format(
120 _("Regular Expression Test Application\n"),
121 _("(c) 2003 Ryan Norton <wxprojects@comcast.net>\n"),
127 void OnMatch(wxCommandEvent
& WXUNUSED(evt
))
129 wxString szPattern
= PatternText
.GetValue();
130 wxString szSearch
= SearchText
.GetValue();
131 wxString szResult
= _("Result:\n"); //Will be displayed in ResultText
133 int nCompileFlags
= 0;
136 if (!(OptionsMenu
->IsChecked(ExtendedID
)))
137 nCompileFlags
|= wxRE_BASIC
;
139 if (OptionsMenu
->IsChecked(ICaseID
))
140 nCompileFlags
|= wxRE_ICASE
;
142 if (OptionsMenu
->IsChecked(NewLineID
))
143 nCompileFlags
|= wxRE_NEWLINE
;
145 if (OptionsMenu
->IsChecked(NotBolID
))
146 nMatchFlags
|= wxRE_NOTBOL
;
148 if (OptionsMenu
->IsChecked(NotEolID
))
149 nMatchFlags
|= wxRE_NOTEOL
;
151 //Our regular expression object
154 //We're going to go ahead and time the match
155 //for fun (clock() is a c library routine that
156 //returns the current time since a certian point
158 time_t dwStartTime
= clock();
160 //Regular Expressions must be compiled before
161 //you can search a string with them, or
162 //at least most implementations do.
163 //(Boost and Microsoft have templated
164 //versions that don't require compilation)
165 //Basically compilation breaks it down into
166 //something that's easier to parse, due
167 //to the syntax of regular expressions
168 if (!Regex
.Compile(szPattern
, nCompileFlags
))
169 szResult
+= _("\nCompile Failed!\n");
172 //Here's where we actually search our string
173 if (!Regex
.Matches(szSearch
, nMatchFlags
))
174 szResult
+= _("\nExecution/Matching Failed!\n");
177 //Success! Here we'll display some
178 //information to the user
179 size_t dwStartIndex
, dwEndIndex
;
180 Regex
.GetMatch(&dwStartIndex
, &dwEndIndex
);
182 szResult
+= wxString::Format(
183 _("Start Index:%i\nMatch Length:%i\nString Matched:%s\n"),
184 dwStartIndex
, dwEndIndex
,
185 szSearch
.Mid(dwStartIndex
, dwEndIndex
)
190 //This line gets the difference in time between now
191 //and when we first initialized dwStartTime.
192 time_t dwEndTime
= clock() - dwStartTime
;
194 szResult
+= wxString::Format(_("Match Time:%u Milliseconds"), dwEndTime
);
196 ResultText
.SetLabel(szResult
);
199 void OnQuit(wxCommandEvent
& WXUNUSED(evt
))
204 wxTextCtrl PatternText
, SearchText
;
205 wxStaticText PatternHeader
, SearchHeader
, ResultText
;
210 DECLARE_EVENT_TABLE()
213 BEGIN_EVENT_TABLE(MyDialog
, wxFrame
)
216 EVT_MENU(wxID_EXIT
, MyDialog::OnQuit
)
217 EVT_MENU(wxID_ABOUT
, MyDialog::OnAbout
)
220 EVT_TEXT_ENTER(MyDialog::PatternTextID
, MyDialog::OnMatch
)
221 EVT_TEXT_ENTER(MyDialog::SearchTextID
, MyDialog::OnMatch
)
224 EVT_BUTTON(MyDialog::OkButtonID
, MyDialog::OnMatch
)
228 //---------------------------------------------------------------------------
230 //---------------------------------------------------------------------------
232 class MyApp
: public wxApp
237 MyDialog
* dialog
= new MyDialog();