From b5679a3f828b1a2379653c611b6ad359b844f990 Mon Sep 17 00:00:00 2001 From: David Capello Date: Wed, 7 Dec 2016 01:12:54 -0300 Subject: [PATCH] Add navigation through .md files on BrowserView --- data/gui.xml | 2 +- src/CMakeLists.txt | 12 ++++++--- src/app/commands/cmd_about.cpp | 2 +- src/app/ui/browser_view.cpp | 47 ++++++++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/data/gui.xml b/data/gui.xml index 31c7d1a96..d5507c7b9 100644 --- a/data/gui.xml +++ b/data/gui.xml @@ -795,7 +795,7 @@ - + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 44fba3eba..7ef57ad6e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -133,10 +133,16 @@ foreach(fn ${src_data_files}) endforeach() add_custom_command( - OUTPUT ${CMAKE_BINARY_DIR}/bin/data/docs/README.md - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/README.md ${CMAKE_BINARY_DIR}/bin/data/docs/README.md + OUTPUT ${CMAKE_BINARY_DIR}/bin/data/README.md + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/README.md ${CMAKE_BINARY_DIR}/bin/data/README.md MAIN_DEPENDENCY ${CMAKE_SOURCE_DIR}/README.md) -list(APPEND out_data_files ${CMAKE_BINARY_DIR}/bin/data/docs/README.md) +list(APPEND out_data_files ${CMAKE_BINARY_DIR}/bin/data/README.md) + +add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/bin/data/EULA.txt + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/EULA.txt ${CMAKE_BINARY_DIR}/bin/data/EULA.txt + MAIN_DEPENDENCY ${CMAKE_SOURCE_DIR}/EULA.txt) +list(APPEND out_data_files ${CMAKE_BINARY_DIR}/bin/data/EULA.txt) add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/bin/data/docs/LICENSES.md diff --git a/src/app/commands/cmd_about.cpp b/src/app/commands/cmd_about.cpp index 349b8bc44..2e01d64d7 100644 --- a/src/app/commands/cmd_about.cpp +++ b/src/app/commands/cmd_about.cpp @@ -100,7 +100,7 @@ void AboutCommand::onExecute(Context* context) author5->Click.connect( [&window]{ window->closeWindow(nullptr); - App::instance()->mainWindow()->showBrowser("data/docs/LICENSES.md"); + App::instance()->mainWindow()->showBrowser("docs/LICENSES.md"); }); window->openWindowInForeground(); diff --git a/src/app/ui/browser_view.cpp b/src/app/ui/browser_view.cpp index 982c52f01..be1cb3003 100644 --- a/src/app/ui/browser_view.cpp +++ b/src/app/ui/browser_view.cpp @@ -8,9 +8,11 @@ #include "config.h" #endif +#include "app/app.h" #include "app/app_menus.h" #include "app/resource_finder.h" #include "app/ui/browser_view.h" +#include "app/ui/main_window.h" #include "app/ui/skin/skin_style_property.h" #include "app/ui/skin/skin_theme.h" #include "app/ui/workspace.h" @@ -18,6 +20,7 @@ #include "base/fs.h" #include "base/split_string.h" #include "she/font.h" +#include "ui/alert.h" #include "ui/link_label.h" #include "ui/menu.h" #include "ui/message.h" @@ -39,6 +42,25 @@ namespace app { using namespace ui; using namespace app::skin; +namespace { + +RegisterMessage kLoadFileMessage; + +class LoadFileMessage : public Message { +public: + LoadFileMessage(const std::string& file) + : Message(kLoadFileMessage) + , m_file(file) { + } + + const std::string& file() const { return m_file; } + +private: + std::string m_file; +}; + +} // annonymous namespace + // TODO This is not the best implementation, but it's "good enough" // for a first version. class BrowserView::CMarkBox : public Widget { @@ -63,13 +85,19 @@ public: void loadFile(const std::string& inputFile) { std::string file = inputFile; - if (file.size() >= 5 && file.substr(0, 5) == "data/") { + { ResourceFinder rf; - rf.includeDataDir(file.substr(5).c_str()); + rf.includeDataDir(file.c_str()); if (rf.findFirst()) file = rf.filename(); } + if (!base::is_file(file)) { + Alert::show("Error< not found||&Close", + file.c_str()); + return; + } + cmark_parser* parser = cmark_parser_new(CMARK_OPT_DEFAULT); FILE* fp = base::open_file_raw(file, "rb"); if (fp) { @@ -184,6 +212,11 @@ private: } bool onProcessMessage(Message* msg) override { + if (msg->type() == kLoadFileMessage) { + loadFile(static_cast(msg)->file()); + return true; + } + switch (msg->type()) { case kMouseWheelMessage: { @@ -429,6 +462,16 @@ private: void addLink(const std::string& url, const std::string& text) { auto label = new LinkLabel(url, text); + if (url.find(':') == std::string::npos) { + label->setUrl(""); + label->Click.connect( + [this, url]{ + Message* msg = new LoadFileMessage(url); + msg->addRecipient(this); + Manager::getDefault()->enqueueMessage(msg); + }); + } + // Uncomment this line to debug labels //label->setBgColor(gfx::rgba((rand()%128)+128, 128, 128));