gpanthera.cc
C++ source, ASCII text
1#include "gpanthera.hh" 2#include <iostream> 3#include <utility> 4#include <libintl.h> 5#include <locale.h> 6#include <filesystem> 7#define _(STRING) gettext(STRING) 8 9namespace gPanthera { 10std::vector<Gtk::Widget*> collect_children(Gtk::Widget &widget) { 11std::vector<Gtk::Widget*> children; 12for(auto *child = widget.get_first_child(); child; child = child->get_next_sibling()) { 13children.push_back(child); 14} 15return children; 16} 17 18Gtk::Image *copy_image(Gtk::Image *image) { 19if(image->get_storage_type() == Gtk::Image::Type::PAINTABLE) { 20return Gtk::make_managed<Gtk::Image>(image->get_paintable()); 21} else if(image->get_storage_type() == Gtk::Image::Type::ICON_NAME) { 22auto new_image = Gtk::make_managed<Gtk::Image>(); 23new_image->set_from_icon_name(image->get_icon_name()); 24return new_image; 25} else { 26return nullptr; 27} 28} 29 30void init() { 31// Set up gettext configuration 32setlocale(LC_ALL, ""); 33 34bindtextdomain("gpanthera", "./locales"); 35textdomain("gpanthera"); 36} 37 38DockablePane::DockablePane(std::shared_ptr<LayoutManager> layout, Gtk::Widget &child, const Glib::ustring &name, const Glib::ustring &label, Gtk::Image *icon, DockStack *stack, Gtk::Widget *custom_header) 39: Gtk::Box(Gtk::Orientation::VERTICAL, 0), name(name) { 40if(icon) { 41this->icon = icon; 42} 43if(stack) { 44this->stack = stack; 45} 46this->layout = std::move(layout); 47this->label.set_text(label); 48header = std::make_unique<Gtk::HeaderBar>(); 49header->set_show_title_buttons(false); 50if(custom_header) { 51header->set_title_widget(*custom_header); 52} else { 53header->set_title_widget(this->label); 54} 55auto header_menu_button = Gtk::make_managed<Gtk::MenuButton>(); 56auto header_menu = Gio::Menu::create(); 57header_menu_button->set_direction(Gtk::ArrowType::NONE); 58 59// Pane menu 60auto action_group = Gio::SimpleActionGroup::create(); 61header_menu_button->insert_action_group("win", action_group); 62 63// Close action 64auto close_action = Gio::SimpleAction::create("close"); 65close_action->signal_activate().connect([this](const Glib::VariantBase&) { 66if(this->stack) { 67this->stack->set_visible_child("empty"); 68} 69}); 70action_group->add_action(close_action); 71header_menu->append(_("Close"), "win.close"); 72 73// Pop out action 74auto pop_out_action = Gio::SimpleAction::create("pop_out"); 75pop_out_action->signal_activate().connect([this](const Glib::VariantBase&) { 76if(this->stack) { 77this->pop_out(); 78} 79}); 80action_group->add_action(pop_out_action); 81header_menu->append(_("Pop out"), "win.pop_out"); 82 83// Switch to traditional (nested) submenus, not sliding 84auto popover_menu = Gtk::make_managed<Gtk::PopoverMenu>(header_menu, Gtk::PopoverMenu::Flags::NESTED); 85header_menu_button->set_popover(*popover_menu); 86 87// Move menu 88auto move_menu = Gio::Menu::create(); 89for(auto &this_stack : this->layout->stacks) { 90auto action_name = "move_" + this_stack->name; 91auto move_action = Gio::SimpleAction::create(action_name); 92move_action->signal_activate().connect([this, this_stack](const Glib::VariantBase&) { 93this_stack->add_pane(*this); 94}); 95action_group->add_action(move_action); 96move_menu->append(this_stack->name, "win." + action_name); 97} 98 99// Add move submenu 100header_menu->append_submenu(_("Move"), move_menu); 101 102header->pack_end(*header_menu_button); 103 104this->prepend(*header); 105this->child = &child; 106this->append(child); 107} 108 109void DockablePane::redock(DockStack *stack) { 110if(this->window != nullptr) { 111this->window->hide(); 112this->window->unset_titlebar(); 113this->window->set_decorated(false); 114this->header->get_style_context()->remove_class("titlebar"); 115this->prepend(*this->header); 116this->window->unset_child(); 117this->window->close(); 118} else if(this->stack == this->get_parent()) { 119this->stack->remove(*this); 120} 121this->stack = stack; 122this->stack->add(*this, this->get_identifier()); 123if(this->window != nullptr) { 124this->window->destroy(); 125delete this->window; 126this->window = nullptr; 127} 128} 129 130void DockablePane::pop_out() { 131if(this->stack != nullptr) { 132this->stack->remove(*this); 133this->stack = nullptr; 134} 135 136if(this->window == nullptr) { 137this->remove(*this->header); 138this->window = new DockWindow(this); 139this->window->set_titlebar(*this->header); 140this->window->set_child(*this); 141this->window->set_decorated(true); 142this->window->present(); 143} 144} 145 146Glib::ustring DockablePane::get_identifier() const { 147return name; 148} 149 150Gtk::Image *DockablePane::get_icon() const { 151return icon; 152} 153 154Gtk::Widget *DockablePane::get_child() const { 155return child; 156} 157 158Gtk::Label *DockablePane::get_label() { 159return &label; 160} 161 162LayoutManager::LayoutManager() : Glib::ObjectBase("LayoutManager") { 163} 164 165void LayoutManager::add_pane(DockablePane *pane) { 166panes.push_back(pane); 167} 168 169void LayoutManager::add_stack(DockStack *stack) { 170stacks.push_back(stack); 171} 172 173DockStack::DockStack(std::shared_ptr<LayoutManager> layout, const Glib::ustring &name) : Gtk::Stack(), layout(layout), name(name) { 174auto *empty_child = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 175add(*empty_child, "empty"); 176// Add the stack to a layout manager 177this->layout->add_stack(this); 178} 179 180DockWindow::DockWindow(DockablePane *pane) { 181this->pane = pane; 182this->set_child(*pane); 183} 184 185DockStackSwitcher::DockStackSwitcher(DockStack *stack) : Gtk::Box(Gtk::Orientation::HORIZONTAL), stack(stack) { 186auto update_callback = [this](Gtk::Widget*) { 187this->update_buttons(); 188}; 189stack->signal_child_added.connect(update_callback); 190stack->signal_child_removed.connect(update_callback); 191} 192 193DockStackSwitcher::~DockStackSwitcher() { 194add_handler.disconnect(); 195remove_handler.disconnect(); 196} 197 198DockButton::DockButton(DockablePane *pane) : Gtk::Button(), pane(pane) { 199if(pane->get_icon()) { 200this->set_child(*copy_image(pane->get_icon())); 201} 202this->set_tooltip_text(pane->get_label()->get_text()); 203this->set_halign(Gtk::Align::CENTER); 204} 205 206void DockStackSwitcher::update_buttons() { 207auto old_buttons = collect_children(*this); 208for(auto *button : old_buttons) { 209remove(*button); 210} 211for(auto *widget = stack->get_first_child(); widget; widget = widget->get_next_sibling()) { 212if(auto pane = dynamic_cast<DockablePane*>(widget)) { 213auto *button = Gtk::make_managed<DockButton>(pane); 214button->signal_clicked().connect([this, pane]() { 215if(stack->get_visible_child_name() == pane->get_identifier()) { 216stack->set_visible_child("empty"); 217} else { 218stack->set_visible_child(pane->get_identifier()); 219} 220}); 221if(pane->get_identifier() != Glib::ustring("empty")) { 222append(*button); 223} 224} 225} 226} 227 228void DockStack::add_pane(DockablePane &child) { 229child.redock(this); 230} 231 232void DockStack::add(Gtk::Widget &child, const Glib::ustring &name) { 233Gtk::Stack::add(child, name); 234signal_child_added.emit(&child); 235} 236 237void DockStack::remove(Gtk::Widget &child) { 238Gtk::Stack::remove(child); 239signal_child_removed.emit(&child); 240} 241} // namespace gPanthera 242