From 19503ef4f8477329bddaaf656bff37d135ec6b3d Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 29 Nov 2011 23:17:40 +0000 Subject: [PATCH] Simplify HelpButton and add some error handling Originally committed to SVN as r5931. --- aegisub/src/help_button.cpp | 113 ++++++++++++------------------------ aegisub/src/help_button.h | 24 +------- 2 files changed, 40 insertions(+), 97 deletions(-) diff --git a/aegisub/src/help_button.cpp b/aegisub/src/help_button.cpp index 432ab056b..b96e2a198 100644 --- a/aegisub/src/help_button.cpp +++ b/aegisub/src/help_button.cpp @@ -34,88 +34,29 @@ /// @ingroup custom_control /// - -/////////// -// Headers #include "config.h" +#include "help_button.h" + #ifndef AGI_PRE +#include +#include + #include -#include #include #endif -#include "help_button.h" +#include + #include "standard_paths.h" #include "utils.h" +static std::map *pages = 0; -/// @brief Constructor -/// @param parent -/// @param _page -/// @param position -/// @param size -/// -HelpButton::HelpButton(wxWindow *parent,wxString _page,wxPoint position,wxSize size) -: wxButton (parent,wxID_HELP,"",position,size) -{ - id = _page; - Connect(GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(HelpButton::OnPressed)); -} - - - -/// @brief Pressed -/// @param event -/// @return -/// -void HelpButton::OnPressed(wxCommandEvent &event) { - // Verify if the page is valid - if (id.IsEmpty()) { - wxLogMessage("TODO"); - return; - } - - // Open - OpenPage(id); -} - - - -/// @brief Open a page -/// @param pageID -/// -void HelpButton::OpenPage(const wxString pageID) { - // Transcode - InitStatic(); - wxString page = (*pages)[pageID]; - - // Get the file type - wxFileType *type = wxTheMimeTypesManager->GetFileTypeFromExtension("html"); - if (type) { - //wxString path = StandardPaths::DecodePath(wxString::Format("http://docs.aegisub.net/%s",page.c_str())); - wxString docsPath = StandardPaths::DecodePath("?data/docs"); -#ifdef __WINDOWS__ - docsPath.Replace("\\","/"); - docsPath = "/" + docsPath; -#endif - wxString path = wxString::Format("file://%s/%s.html",docsPath,page); - wxLaunchDefaultBrowser(path); - } -} - - - -/// DOCME -std::map *HelpButton::pages = NULL; - - -/// @brief DOCME -/// -void HelpButton::InitStatic() { +static void init_static() { if (!pages) { - pages = new std::map; - std::map &page = *pages; + pages = new std::map; + std::map &page = *pages; page["Attachment Manager"] = "Attachment_Manager"; page["Automation Manager"] = "Automation_Manager"; page["Colour Picker"] = "Colour_Picker"; @@ -140,11 +81,33 @@ void HelpButton::InitStatic() { } } - -/// @brief DOCME -/// -void HelpButton::ClearPages() { - if (pages) delete pages; +HelpButton::HelpButton(wxWindow *parent, wxString const& page, wxPoint position, wxSize size) +: wxButton(parent, wxID_HELP, "", position, size) +{ + Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::tr1::bind(&HelpButton::OpenPage, page)); + init_static(); + if (pages->find(page) == pages->end()) + throw agi::InternalError("Invalid help page", 0); } +void HelpButton::OpenPage(wxString const& pageID) { + init_static(); + wxString page = (*pages)[pageID]; + // Get the file type + wxFileType *type = wxTheMimeTypesManager->GetFileTypeFromExtension("html"); + if (type) { + wxString docsPath = StandardPaths::DecodePath("?data/docs"); +#ifdef __WINDOWS__ + docsPath.Replace("\\","/"); + docsPath = "/" + docsPath; +#endif + wxString path = wxString::Format("file://%s/%s.html",docsPath,page); + if (!wxLaunchDefaultBrowser(path)) + wxMessageBox("Documentation files not found.", "Error", wxOK | wxICON_ERROR); + } +} + +void HelpButton::ClearPages() { + delete pages; +} diff --git a/aegisub/src/help_button.h b/aegisub/src/help_button.h index 0dfc497bd..c9b635dae 100644 --- a/aegisub/src/help_button.h +++ b/aegisub/src/help_button.h @@ -34,40 +34,20 @@ /// @ingroup custom_control /// - - - -//////////// -// Includes #ifndef AGI_PRE -#include #include #endif - /// DOCME /// @class HelpButton /// @brief DOCME /// /// DOCME class HelpButton : public wxButton { -private: - - /// DOCME - wxString id; - void OnPressed(wxCommandEvent &event); - - - /// DOCME - static std::map *pages; - static void InitStatic(); - public: - HelpButton(wxWindow *parent,wxString page="",wxPoint position=wxDefaultPosition,wxSize size=wxDefaultSize); + HelpButton(wxWindow *parent, wxString const& page="", wxPoint position=wxDefaultPosition, wxSize size=wxDefaultSize); - static void OpenPage(const wxString page); + static void OpenPage(wxString const& page); static void ClearPages(); }; - -