#include #include #include #define APP_NAME "Jacob's Fast-Lite Browser" #define APP_VERSION "v0.1" class myApp : public wxApp { virtual bool OnInit(); }; class myFrame : public wxFrame { public: myFrame(const wxString &title, const wxPoint &pos, const wxSize &size); void OnQuit(wxCommandEvent &event); void OnAbout(wxCommandEvent &event); void OnBrowserID(wxCommandEvent &event); void OnOpenURL(wxCommandEvent &event); void OnSave(wxCommandEvent &event); void fetchURL(const wxString &url); DECLARE_EVENT_TABLE() private: wxString browserID; wxTextCtrl *text; }; enum { ID_Quit = 1, ID_About, ID_BrowserID, ID_OpenURL, ID_Save, }; BEGIN_EVENT_TABLE(myFrame, wxFrame) EVT_MENU(ID_Quit, myFrame::OnQuit) EVT_MENU(ID_About, myFrame::OnAbout) EVT_MENU(ID_BrowserID, myFrame::OnBrowserID) EVT_MENU(ID_OpenURL, myFrame::OnOpenURL) EVT_MENU(ID_Save, myFrame::OnSave) END_EVENT_TABLE() IMPLEMENT_APP(myApp) bool myApp::OnInit() { wxString title = APP_NAME; title << " " << APP_VERSION; myFrame *frame = new myFrame(title, wxPoint(50,50), wxSize(450,340)); frame->Show(TRUE); SetTopWindow(frame); return TRUE; } myFrame::myFrame(const wxString &title, const wxPoint &pos, const wxSize &size) : wxFrame((wxFrame *)NULL, -1, title, pos, size) { wxMenu *menuFile = new wxMenu; menuFile->Append(ID_OpenURL, "&Open URL"); menuFile->Append(ID_Save, "&Save"); menuFile->AppendSeparator(); menuFile->Append(ID_Quit, "E&xit"); wxMenu *menuSettings = new wxMenu; menuSettings->Append(ID_BrowserID, "&Browser ID"); wxMenu *menuHelp = new wxMenu; menuHelp->Append(ID_About, "&About..."); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File"); menuBar->Append(menuSettings, "&Settings"); menuBar->Append(menuHelp, "&Help"); SetMenuBar(menuBar); browserID = "Fast-Lite v0.1"; CreateStatusBar(); SetStatusText("Select 'File/Open URL' to begin"); text = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY, wxDefaultValidator, "urlText"); } void myFrame::OnQuit(wxCommandEvent &WXUNUSED(event)) { Close(TRUE); } void myFrame::OnAbout(wxCommandEvent &WXUNUSED(event)) { wxString title = APP_NAME; title << " " << APP_VERSION; wxString msg = "This program was written by\n"; msg << "A. Jacob Cord\nfor your browsing convenience."; wxMessageBox(msg, title, wxOK | wxICON_INFORMATION, this); } void myFrame::OnBrowserID(wxCommandEvent &WXUNUSED(event)) { // store old value in case of cancel wxString prevID = browserID; browserID = wxGetTextFromUser("Enter the New Browser Identification String", "Enter Browser ID", browserID, NULL, -1, -1, FALSE); // if cancelled, restore old value if(browserID.empty()) { browserID = prevID; } // report our identification to the status bar wxString msg = "Sorry, this function not yet implemented."; SetStatusText(msg); } void myFrame::OnOpenURL(wxCommandEvent &WXUNUSED(event)) { wxString url; url = wxGetTextFromUser("Open URL:", "Open URL...", "http://", NULL, -1, -1, FALSE); // make sure we didn't just cancel if(url.empty() || url == "http://") { SetStatusText("Open URL Cancelled"); return; } // clear out our text window text->Clear(); fetchURL(url); } void myFrame::OnSave(wxCommandEvent &WXUNUSED(event)) { wxString fileName; wxFileDialog saveDialog(this, "Save As...", "", ".html", "", wxSAVE | wxOVERWRITE_PROMPT, wxDefaultPosition); if(saveDialog.ShowModal() == wxID_OK) { // user hit OK button fileName = saveDialog.GetPath(); // try to save the text to selected file if(text->SaveFile(fileName)) { SetStatusText("File saved."); } else { SetStatusText("Error encountered while saving. Sorry."); } } } void myFrame::fetchURL(const wxString &url) { wxURL client(url); SetStatusText("Fetching URL data..."); wxInputStream *iStream = client.GetInputStream(); // declare our read buffer const int bufSize = 1024; char buf[bufSize]; while(!iStream->Eof()) { // until we get to the end, read 1k at a time and append to text control iStream->Read(buf, bufSize); text->AppendText(buf); memset(buf, '\0', bufSize); } SetStatusText("Finished."); }