LibreWeb-Browser/src/mainwindow.cc

99 lines
3.6 KiB
C++
Raw Normal View History

2020-11-12 14:27:47 -08:00
#include "mainwindow.h"
2020-11-30 15:43:52 -08:00
#include <gtkmm/menuitem.h>
2020-12-08 19:44:26 -08:00
#include <gtkmm/image.h>
#ifdef LEGACY_CXX
#include <experimental/filesystem>
namespace n_fs = ::std::experimental::filesystem;
#else
#include <filesystem>
namespace n_fs = ::std::filesystem;
#endif
2020-11-12 14:27:47 -08:00
2020-12-08 19:44:26 -08:00
MainWindow::MainWindow()
: m_vbox(Gtk::ORIENTATION_VERTICAL, 0),
m_hbox_bar(Gtk::ORIENTATION_HORIZONTAL, 0)
2020-11-12 14:27:47 -08:00
{
2020-11-28 14:47:34 -08:00
set_title("Browser");
2020-11-29 20:13:48 -08:00
set_default_size(1000, 800);
2020-11-30 16:02:29 -08:00
set_position(Gtk::WIN_POS_CENTER);
2020-11-14 14:13:00 -08:00
2020-11-30 15:43:52 -08:00
// Connect signals
m_menu.quit.connect(sigc::mem_fun(this, &MainWindow::hide)); /*!< hide main window and therefor closes the app */
2020-12-08 19:44:26 -08:00
m_menu.reload.connect(sigc::mem_fun(this, &MainWindow::demo)); /*!< Menu item for reloading the page */
m_menu.about.connect(sigc::mem_fun(m_about, &About::show_about)); /*!< Display about dialog */
m_about.signal_response().connect(sigc::mem_fun(m_about, &About::hide_about)); /*!< Close about dialog */
m_refreshButton.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::demo)); /*!< Button for reloading the page */
2020-11-30 15:43:52 -08:00
m_vbox.pack_start(m_menu, false, false, 0);
2020-11-14 14:13:00 -08:00
2020-12-08 19:44:26 -08:00
// Horizontal bar
auto styleBack = m_backButton.get_style_context();
styleBack->add_class("circular");
auto styleForward = m_forwardButton.get_style_context();
styleForward->add_class("circular");
auto styleRefresh = m_refreshButton.get_style_context();
styleRefresh->add_class("circular");
m_backButton.set_relief(Gtk::RELIEF_NONE);
m_forwardButton.set_relief(Gtk::RELIEF_NONE);
m_refreshButton.set_relief(Gtk::RELIEF_NONE);
m_homeButton.set_relief(Gtk::RELIEF_NONE);
// Add icons to buttons
backIcon.set_from_icon_name("go-previous", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_backButton.add(backIcon);
forwardIcon.set_from_icon_name("go-next", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_forwardButton.add(forwardIcon);
refreshIcon.set_from_icon_name("view-refresh", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_refreshButton.add(refreshIcon);
homeIcon.set_from_icon_name("go-home", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_homeButton.add(homeIcon);
m_hbox_bar.pack_start(m_backButton, false, false , 0);
m_hbox_bar.pack_start(m_forwardButton, false, false , 0);
m_hbox_bar.pack_start(m_refreshButton, false, false , 0);
m_hbox_bar.pack_start(m_homeButton, false, false , 0);
m_hbox_bar.pack_start(m_inputField, true, true , 8);
m_vbox.pack_start(m_hbox_bar, false, false, 6);
// Main browser rendering area
2020-11-28 14:47:34 -08:00
m_scrolledWindow.add(m_renderArea);
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
2020-11-30 15:43:52 -08:00
m_vbox.pack_end(m_scrolledWindow, true, true, 0);
add(m_vbox);
2020-11-28 20:00:32 -08:00
show_all_children();
2020-12-08 19:44:26 -08:00
// Grap focus to input field by default
m_inputField.grab_focus();
2020-12-07 16:13:21 -08:00
2020-12-04 17:51:40 -08:00
demo();
}
2020-12-04 17:51:40 -08:00
void MainWindow::demo()
{
2020-12-04 17:51:40 -08:00
/*
// From disk
std::string exePath = n_fs::current_path().string();
std::string filePath = exePath.append("/../../test.md");
cmark_node *readDoc = m_file.read(filePath);
2020-12-04 17:51:40 -08:00
if (readDoc != NULL) {
m_renderArea.processDocument(readDoc);
m_file.free(readDoc);
2020-12-04 17:51:40 -08:00
}
*/
// From IPFS
try {
cmark_node *fetchDoc = m_file.fetch("QmQzhn6hEfbYdCfwzYFsSt3eWpubVKA1dNqsgUwci5vHwq");
2020-12-04 17:51:40 -08:00
m_renderArea.processDocument(fetchDoc);
m_file.free(fetchDoc);
} catch (const std::runtime_error &error) {
std::cerr << "IPFS Deamon is most likely down: " << error.what() << std::endl;
// Not found (or any other issue)
m_renderArea.showMessage("Page not found!", "Detailed error message: " + std::string(error.what()));
}
2020-11-12 20:27:47 -08:00
}
2020-12-07 16:13:21 -08:00