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} 55header->add_css_class("gpanthera-dock-titlebar"); 56auto header_menu_button = Gtk::make_managed<Gtk::MenuButton>(); 57auto header_menu = Gio::Menu::create(); 58header_menu_button->set_direction(Gtk::ArrowType::NONE); 59 60// Pane menu 61this->action_group = Gio::SimpleActionGroup::create(); 62header_menu_button->insert_action_group("win", action_group); 63 64// Close action 65auto close_action = Gio::SimpleAction::create("close"); 66close_action->signal_activate().connect([this](const Glib::VariantBase&) { 67if(this->stack) { 68this->stack->set_visible_child("empty"); 69} 70}); 71action_group->add_action(close_action); 72header_menu->append(_("Close"), "win.close"); 73 74// Pop out action 75auto pop_out_action = Gio::SimpleAction::create("pop_out"); 76pop_out_action->signal_activate().connect([this](const Glib::VariantBase&) { 77if(this->stack) { 78this->pop_out(); 79} 80}); 81action_group->add_action(pop_out_action); 82header_menu->append(_("Pop out"), "win.pop_out"); 83 84// Move menu 85auto move_menu = Gio::Menu::create(); 86for(auto &this_stack : this->layout->stacks) { 87auto action_name = "move_" + this_stack->name; 88auto move_action = Gio::SimpleAction::create(action_name); 89move_action->signal_activate().connect([this, this_stack](const Glib::VariantBase&) { 90this_stack->add_pane(*this); 91}); 92action_group->add_action(move_action); 93move_menu->append(this_stack->name, "win." + action_name); 94} 95 96// Add move submenu 97header_menu->append_submenu(_("Move"), move_menu); 98 99// Switch to traditional (nested) submenus, not sliding 100auto popover_menu = Gtk::make_managed<Gtk::PopoverMenu>(header_menu, Gtk::PopoverMenu::Flags::NESTED); 101popover_menu->set_has_arrow(false); 102header_menu_button->set_popover(*popover_menu); 103 104// TODO: Add a context menu as well 105 106header->pack_end(*header_menu_button); 107 108this->prepend(*header); 109this->child = &child; 110this->append(child); 111} 112 113void DockablePane::redock(DockStack *stack) { 114if(this->window != nullptr) { 115this->window->hide(); 116this->window->unset_titlebar(); 117this->window->set_decorated(false); 118this->header->get_style_context()->remove_class("titlebar"); 119this->prepend(*this->header); 120this->window->unset_child(); 121this->window->close(); 122} else if(this->stack == this->get_parent()) { 123this->stack->remove(*this); 124} 125this->stack = stack; 126this->last_stack = stack; 127this->stack->add(*this, this->get_identifier()); 128if(this->window != nullptr) { 129this->window->destroy(); 130delete this->window; 131this->window = nullptr; 132auto action = std::dynamic_pointer_cast<Gio::SimpleAction>(this->action_group->lookup_action("pop_out")); 133action->set_enabled(true); 134this->header->get_style_context()->remove_class("gpanthera-dock-titlebar-popout"); 135} 136} 137 138void DockablePane::pop_out() { 139if(this->stack != nullptr) { 140this->stack->remove(*this); 141this->stack = nullptr; 142} 143 144if(this->window == nullptr) { 145this->remove(*this->header); 146this->window = new DockWindow(this); 147this->window->set_titlebar(*this->header); 148this->window->set_child(*this); 149this->window->set_decorated(true); 150auto action = std::dynamic_pointer_cast<Gio::SimpleAction>(this->action_group->lookup_action("pop_out")); 151action->set_enabled(false); 152this->header->get_style_context()->add_class("gpanthera-dock-titlebar-popout"); 153} 154this->window->present(); 155} 156 157Glib::ustring DockablePane::get_identifier() const { 158return name; 159} 160 161Gtk::Image *DockablePane::get_icon() const { 162return icon; 163} 164 165Gtk::Widget *DockablePane::get_child() const { 166return child; 167} 168 169Gtk::Label *DockablePane::get_label() { 170return &label; 171} 172 173LayoutManager::LayoutManager() : Glib::ObjectBase("LayoutManager") { 174} 175 176void LayoutManager::add_pane(DockablePane *pane) { 177panes.push_back(pane); 178} 179 180void LayoutManager::add_stack(DockStack *stack) { 181stacks.push_back(stack); 182} 183 184DockStack::DockStack(std::shared_ptr<LayoutManager> layout, const Glib::ustring &name) : Gtk::Stack(), layout(layout), name(name) { 185auto *empty_child = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 186add(*empty_child, "empty"); 187// Add the stack to a layout manager 188this->layout->add_stack(this); 189 190// Hide the stack when the empty child is visible 191this->property_visible_child_name().signal_changed().connect([this]() { 192if(this->get_visible_child_name() == "empty") { 193this->hide(); 194} else { 195this->show(); 196} 197}); 198 199this->set_visible_child("empty"); 200this->hide(); 201} 202 203DockWindow::DockWindow(DockablePane *pane) { 204this->pane = pane; 205this->set_child(*pane); 206// Attempting to close the window should redock the pane so it doesn't vanish 207this->signal_close_request().connect([this]() { 208this->pane->redock(this->pane->last_stack); 209return true; 210}, false); 211} 212 213DockStackSwitcher::DockStackSwitcher(DockStack *stack, Gtk::Orientation orientation) : Gtk::Box(orientation), stack(stack) { 214auto update_callback = [this](Gtk::Widget*) { 215this->update_buttons(); 216}; 217stack->signal_child_added.connect(update_callback); 218stack->signal_child_removed.connect(update_callback); 219} 220 221DockStackSwitcher::~DockStackSwitcher() { 222add_handler.disconnect(); 223remove_handler.disconnect(); 224} 225 226DockButton::DockButton(DockablePane *pane) : Gtk::Button(), pane(pane) { 227if(pane->get_icon()) { 228this->set_child(*copy_image(pane->get_icon())); 229} 230this->set_tooltip_text(pane->get_label()->get_text()); 231this->set_halign(Gtk::Align::CENTER); 232this->set_valign(Gtk::Align::CENTER); 233this->get_style_context()->add_class("toggle"); 234this->get_style_context()->add_class("gpanthera-dock-button"); 235this->pane->last_stack->property_visible_child_name().signal_changed().connect([this]() { 236if(this->pane->last_stack->get_visible_child_name() == this->pane->get_identifier()) { 237this->get_style_context()->add_class("checked"); 238this->get_style_context()->add_class("gpanthera-dock-button-active"); 239} else { 240this->get_style_context()->remove_class("checked"); 241this->get_style_context()->remove_class("gpanthera-dock-button-active"); 242} 243}); 244} 245 246void DockStackSwitcher::update_buttons() { 247auto old_buttons = collect_children(*this); 248for(auto *button : old_buttons) { 249remove(*button); 250} 251for(auto *widget = stack->get_first_child(); widget; widget = widget->get_next_sibling()) { 252if(auto pane = dynamic_cast<DockablePane*>(widget)) { 253auto *button = Gtk::make_managed<DockButton>(pane); 254button->signal_clicked().connect([this, pane]() { 255if(stack->get_visible_child_name() == pane->get_identifier()) { 256stack->set_visible_child("empty"); 257} else { 258stack->set_visible_child(pane->get_identifier()); 259} 260}); 261if(pane->get_identifier() != Glib::ustring("empty")) { 262append(*button); 263} 264} 265} 266} 267 268void DockStack::add_pane(DockablePane &child) { 269child.redock(this); 270} 271 272void DockStack::add(Gtk::Widget &child, const Glib::ustring &name) { 273Gtk::Stack::add(child, name); 274signal_child_added.emit(&child); 275} 276 277void DockStack::remove(Gtk::Widget &child) { 278Gtk::Stack::remove(child); 279signal_child_removed.emit(&child); 280} 281} // namespace gPanthera 282