panthera-www.cc
C++ source, ASCII text
1#include "gpanthera.hh" 2#include <gtkmm.h> 3#include <glibmm.h> 4#include <glibmm/ustring.h> 5#include <iostream> 6#include <memory> 7#include <libintl.h> 8#include <locale.h> 9#include <gtk/gtk.h> 10#include <gdk/gdk.h> 11#include <webkit/webkit.h> 12 13class PantheraWww : public Gtk::Application { 14Gtk::Window *window = Gtk::make_managed<Gtk::Window>(); 15protected: 16std::shared_ptr<gPanthera::LayoutManager> layout_manager; 17std::shared_ptr<gPanthera::ContentManager> content_manager; 18 19static void notify_callback(GObject *object, GParamSpec *pspec, gpointer data) { 20auto parent = gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(object))); 21if(auto page = dynamic_cast<gPanthera::ContentPage*>(Glib::wrap(parent))) { 22if(g_strcmp0(pspec->name, "title") == 0) { 23if(auto label = dynamic_cast<Gtk::Label*>(page->tab_widget)) { 24label->set_label(webkit_web_view_get_title(WEBKIT_WEB_VIEW(object))); 25} 26} 27} 28} 29 30void on_new_tab(gPanthera::ContentStack *stack) { 31if(!stack) { 32// Find the current area 33stack = content_manager->get_last_operated_page()->get_stack(); 34} 35 36WebKitWebView *webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); 37gtk_widget_set_hexpand(GTK_WIDGET(webview), true); 38gtk_widget_set_vexpand(GTK_WIDGET(webview), true); 39auto page_content = Gtk::make_managed<Gtk::Box>(); 40gtk_box_append(page_content->gobj(), GTK_WIDGET(webview)); 41auto page_tab = new Gtk::Label("Untitled"); 42auto page = Gtk::make_managed<gPanthera::ContentPage>(content_manager, stack, page_content, page_tab); 43g_signal_connect(webview, "notify", G_CALLBACK(notify_callback), page->gobj()); 44webkit_web_view_load_uri(webview, "about:blank"); 45stack->add_page(*page); 46} 47 48void on_startup() override { 49Gtk::Application::on_startup(); 50add_window(*window); 51window->set_default_size(600, 400); 52layout_manager = std::make_shared<gPanthera::LayoutManager>(); 53auto dock_stack_1 = Gtk::make_managed<gPanthera::DockStack>(layout_manager, "One"); 54auto switcher_1 = Gtk::make_managed<gPanthera::DockStackSwitcher>(dock_stack_1, Gtk::Orientation::HORIZONTAL); 55auto dock_stack_2 = Gtk::make_managed<gPanthera::DockStack>(layout_manager, "Two"); 56auto switcher_2 = Gtk::make_managed<gPanthera::DockStackSwitcher>(dock_stack_2, Gtk::Orientation::VERTICAL); 57auto pane_1_content = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 58auto debug_button = Gtk::make_managed<Gtk::Button>("Debug"); 59pane_1_content->append(*debug_button); 60auto pane_2_content = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 61pane_2_content->append(*Gtk::make_managed<Gtk::Label>("Pane 2 content")); 62auto pane_3_content = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 63pane_3_content->append(*Gtk::make_managed<Gtk::Label>("Pane 3 content")); 64auto pane_1_icon = Gtk::make_managed<Gtk::Image>(); 65pane_1_icon->set_from_icon_name("go-home-symbolic"); 66auto pane_2_icon = Gtk::make_managed<Gtk::Image>(); 67pane_2_icon->set_from_icon_name("folder-symbolic"); 68auto pane_3_icon = Gtk::make_managed<Gtk::Image>(); 69pane_3_icon->set_from_icon_name("network-transmit-receive-symbolic"); 70auto pane_1 = Gtk::make_managed<gPanthera::DockablePane>(layout_manager, *pane_1_content, "pane1", "Pane 1", pane_1_icon); 71auto pane_2 = Gtk::make_managed<gPanthera::DockablePane>(layout_manager, *pane_2_content, "pane2", "Pane 2", pane_2_icon); 72auto pane_3 = Gtk::make_managed<gPanthera::DockablePane>(layout_manager, *pane_3_content, "pane3", "Pane 3", pane_3_icon); 73 74dock_stack_1->set_transition_type(Gtk::StackTransitionType::SLIDE_LEFT_RIGHT); 75dock_stack_1->set_transition_duration(125); 76dock_stack_1->set_expand(true); 77dock_stack_2->set_transition_type(Gtk::StackTransitionType::SLIDE_UP_DOWN); 78dock_stack_2->set_transition_duration(125); 79dock_stack_2->set_expand(true); 80 81dock_stack_1->add_pane(*pane_1); 82dock_stack_1->add_pane(*pane_3); 83dock_stack_2->add_pane(*pane_2); 84 85auto outer_grid = Gtk::make_managed<Gtk::Grid>(); 86outer_grid->attach(*switcher_2, 0, 1, 1, 1); 87outer_grid->attach(*switcher_1, 1, 2, 1, 1); 88auto outer_paned = Gtk::make_managed<Gtk::Paned>(Gtk::Orientation::HORIZONTAL); 89outer_paned->set_start_child(*dock_stack_2); 90auto inner_paned = Gtk::make_managed<Gtk::Paned>(Gtk::Orientation::VERTICAL); 91auto content = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 92content_manager = std::make_shared<gPanthera::ContentManager>(); 93std::function<bool(gPanthera::ContentPage*)> detach_handler; 94detach_handler = [](gPanthera::ContentPage *widget) { 95auto new_stack = Gtk::make_managed<gPanthera::ContentStack>(widget->content_manager, widget->get_stack()->get_detach_handler()); 96auto new_switcher = Gtk::make_managed<gPanthera::ContentTabBar>(new_stack, Gtk::Orientation::HORIZONTAL, dynamic_cast<gPanthera::ContentTabBar*>(widget->get_stack()->get_parent()->get_first_child())->get_extra_child_function()); 97auto new_notebook = Gtk::make_managed<gPanthera::ContentNotebook>(new_stack, new_switcher); 98auto window = new gPanthera::ContentWindow(new_notebook); 99widget->redock(new_stack); 100window->present(); 101new_stack->signal_leave_empty.connect([window]() { 102window->close(); 103delete window; 104}); 105return true; 106}; 107 108auto return_extra_child = [this](gPanthera::ContentTabBar *switcher) { 109auto new_tab_button = Gtk::make_managed<Gtk::Button>(); 110new_tab_button->set_child(*Gtk::make_managed<Gtk::Image>(Gio::Icon::create("list-add-symbolic"))); 111new_tab_button->set_tooltip_text("New tab"); 112new_tab_button->signal_clicked().connect([this, switcher]() { 113on_new_tab(switcher->get_stack()); 114}); 115return new_tab_button; 116}; 117auto content_stack = Gtk::make_managed<gPanthera::ContentStack>(content_manager, detach_handler); 118auto content_stack_switcher = Gtk::make_managed<gPanthera::ContentTabBar>(content_stack, Gtk::Orientation::HORIZONTAL, return_extra_child); 119content_manager->add_stack(content_stack); 120WebKitWebView *webview = WEBKIT_WEB_VIEW(webkit_web_view_new()); 121webkit_web_view_load_uri(webview, "https://www.example.com"); 122gtk_widget_set_hexpand(GTK_WIDGET(webview), true); 123gtk_widget_set_vexpand(GTK_WIDGET(webview), true); 124auto page_1_content = Gtk::make_managed<Gtk::Box>(); 125gtk_box_append(page_1_content->gobj(), GTK_WIDGET(webview)); 126auto page_1_tab = new Gtk::Label("Page 1"); 127auto page_1 = Gtk::make_managed<gPanthera::ContentPage>(content_manager, content_stack, page_1_content, page_1_tab); 128auto page_2_content = Gtk::make_managed<Gtk::Label>("Page 2..."); 129auto page_2_tab = new Gtk::Label("Page 2"); 130auto page_2 = Gtk::make_managed<gPanthera::ContentPage>(content_manager, content_stack, page_2_content, page_2_tab); 131content->set_name("content_box"); 132auto content_notebook = Gtk::make_managed<gPanthera::ContentNotebook>(content_stack, content_stack_switcher, Gtk::PositionType::TOP); 133content->append(*content_notebook); 134inner_paned->set_start_child(*content); 135inner_paned->set_end_child(*dock_stack_1); 136outer_paned->set_end_child(*inner_paned); 137outer_grid->attach(*outer_paned, 1, 1, 1, 1); 138window->set_child(*outer_grid); 139debug_button->signal_clicked().connect([this]() { 140std::cout << "Last operated page: " << content_manager->get_last_operated_page()->get_name() << std::endl; 141}); 142// TODO: Use the last operated page and allow opening tabs next to the last operated page using panes 143} 144 145void on_activate() override { 146window->present(); 147} 148public: 149static Glib::RefPtr<PantheraWww> create() { 150return Glib::make_refptr_for_instance<PantheraWww>(new PantheraWww()); 151} 152}; 153 154int main(int argc, char *argv[]) { 155gPanthera::init(); 156auto app = PantheraWww::create(); 157return app->run(argc, argv); 158} 159