gpanthera.cc
C++ source, ASCII text
1#include "gpanthera.hh" 2 3#include <cassert> 4#include <iostream> 5#include <utility> 6#include <libintl.h> 7#include <locale.h> 8#include <filesystem> 9#include <ranges> 10#include <nlohmann/json.hpp> 11#include <unordered_set> 12 13#define _(STRING) dgettext("gpanthera", STRING) 14 15namespace gPanthera { 16sigc::signal<void(double, double)> add_context_menu(Gtk::Widget &widget) { 17sigc::signal<void(double, double)> signal; 18// Add a context menu to a widget 19auto gesture_click = Gtk::GestureClick::create(); 20gesture_click->set_button(3); 21auto gesture_keyboard = Gtk::EventControllerKey::create(); 22gesture_click->signal_pressed().connect([&widget, signal](int n_press, double x, double y) { 23if(n_press == 1) { 24signal.emit(x, y); 25} 26}, false); 27gesture_keyboard->signal_key_pressed().connect([&widget, signal](int keyval, int keycode, Gdk::ModifierType state) { 28if((keyval == GDK_KEY_F10) && (state == Gdk::ModifierType::SHIFT_MASK) || keyval == GDK_KEY_Menu) { 29// auto popover = Gtk::make_managed<Gtk::PopoverMenu>(); 30// popover->set_menu_model(menu); 31// popover->set_has_arrow(false); 32// popover->set_halign(Gtk::Align::START); 33// popover->set_pointing_to(Gdk::Rectangle(0, 0, 1, 1)); 34// popover->set_parent(widget); 35// popover->popup(); 36signal.emit(0, 0); 37} 38return true; 39}, false); 40widget.add_controller(gesture_click); 41widget.add_controller(gesture_keyboard); 42return signal; 43} 44 45std::vector<Gtk::Widget*> collect_children(Gtk::Widget &widget) { 46// Get a vector of the children of a GTK widget, since the container API was removed in GTK 4 47std::vector<Gtk::Widget*> children; 48for(auto *child = widget.get_first_child(); child; child = child->get_next_sibling()) { 49children.push_back(child); 50} 51return children; 52} 53 54Gtk::Image *copy_image(Gtk::Image *image) { 55// Generate a new Gtk::Image with the same contents as an existing one 56if(image->get_storage_type() == Gtk::Image::Type::PAINTABLE) { 57return Gtk::make_managed<Gtk::Image>(image->get_paintable()); 58} else if(image->get_storage_type() == Gtk::Image::Type::ICON_NAME) { 59auto new_image = Gtk::make_managed<Gtk::Image>(); 60new_image->set_from_icon_name(image->get_icon_name()); 61return new_image; 62} else { 63return nullptr; 64} 65} 66 67void init() { 68g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL); 69// Set up gettext configurationIsn't 70bindtextdomain("gpanthera", "./locales"); 71} 72 73DockablePane::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) 74: Gtk::Box(Gtk::Orientation::VERTICAL, 0), name(name) { 75if(icon) { 76this->icon = icon; 77} 78if(stack) { 79this->stack = stack; 80} 81this->layout = layout; 82this->layout->add_pane(this); 83this->label.set_text(label); 84// This should be replaced with a custom class in the future 85header = std::make_unique<Gtk::HeaderBar>(); 86header->set_show_title_buttons(false); 87if(custom_header) { 88header->set_title_widget(*custom_header); 89} else { 90header->set_title_widget(this->label); 91} 92header->add_css_class("gpanthera-dock-titlebar"); 93auto header_menu_button = Gtk::make_managed<Gtk::MenuButton>(); 94header_menu_button->set_direction(Gtk::ArrowType::NONE); 95 96// Pane menu 97this->action_group = Gio::SimpleActionGroup::create(); 98header_menu_button->insert_action_group("win", action_group); 99 100// Close action 101auto close_action = Gio::SimpleAction::create("close"); 102close_action->signal_activate().connect([this](const Glib::VariantBase&) { 103if(this->stack) { 104this->stack->set_visible_child(""); 105} 106}); 107action_group->add_action(close_action); 108header_menu->append(_("Close"), "win.close"); 109 110// Pop out action 111auto pop_out_action = Gio::SimpleAction::create("pop_out"); 112pop_out_action->signal_activate().connect([this](const Glib::VariantBase&) { 113if(this->stack) { 114this->pop_out(); 115} 116}); 117action_group->add_action(pop_out_action); 118header_menu->append(_("Pop out"), "win.pop_out"); 119 120// Move menu 121auto move_menu = Gio::Menu::create(); 122for(auto &this_stack : this->layout->stacks) { 123auto action_name = "move_" + this_stack->name; 124auto move_action = Gio::SimpleAction::create(action_name); 125move_action->signal_activate().connect([this, this_stack](const Glib::VariantBase&) { 126this_stack->add_pane(*this); 127}); 128action_group->add_action(move_action); 129move_menu->append(this_stack->name, "win." + action_name); 130} 131 132// Add move submenu 133header_menu->append_submenu(_("Move"), move_menu); 134 135// Switch to traditional (nested) submenus, not sliding 136auto popover_menu = Gtk::make_managed<Gtk::PopoverMenu>(header_menu, Gtk::PopoverMenu::Flags::NESTED); 137popover_menu->set_has_arrow(false); 138header_menu_button->set_popover(*popover_menu); 139 140// TODO: Add a context menu as well 141 142header->pack_end(*header_menu_button); 143 144this->prepend(*header); 145this->child = &child; 146this->append(child); 147} 148 149Gtk::Stack *DockablePane::get_stack() const { 150return stack; 151} 152 153void DockablePane::redock(DockStack *stack) { 154if(this->window != nullptr) { 155this->window->hide(); 156// Put the titlebar back 157this->window->unset_titlebar(); 158this->window->set_decorated(false); 159this->header->get_style_context()->remove_class("titlebar"); 160this->prepend(*this->header); 161this->window->unset_child(); 162this->window->close(); 163} else if(this->get_parent() && this->stack == this->get_parent()) { 164this->stack->remove(*this); 165} 166this->stack = stack; 167this->last_stack = stack; 168this->stack->add(*this, this->get_identifier()); 169if(this->window != nullptr) { 170this->window->destroy(); 171delete this->window; 172this->window = nullptr; 173// Re-enable the pop out option 174auto action = std::dynamic_pointer_cast<Gio::SimpleAction>(this->action_group->lookup_action("pop_out")); 175action->set_enabled(true); 176this->header->get_style_context()->remove_class("gpanthera-dock-titlebar-popout"); 177} 178layout->signal_pane_moved.emit(this); 179} 180 181void DockablePane::pop_out() { 182if(this->stack != nullptr) { 183this->stack->remove(*this); 184this->stack = nullptr; 185} 186 187if(this->window == nullptr) { 188// Remove the header bar from the pane, so it can be used as the titlebar of the window 189this->remove(*this->header); 190this->window = new DockWindow(this); 191this->window->set_titlebar(*this->header); 192this->window->set_child(*this); 193this->window->set_decorated(true); 194// Grey out the pop-out option 195auto action = std::dynamic_pointer_cast<Gio::SimpleAction>(this->action_group->lookup_action("pop_out")); 196action->set_enabled(false); 197this->header->get_style_context()->add_class("gpanthera-dock-titlebar-popout"); 198} 199this->window->present(); 200layout->signal_pane_moved.emit(this); 201} 202 203Glib::ustring DockablePane::get_identifier() const { 204return name; 205} 206 207Gtk::Image *DockablePane::get_icon() const { 208return icon; 209} 210 211Gtk::Widget *DockablePane::get_child() const { 212return child; 213} 214 215Gtk::Label *DockablePane::get_label() { 216return &label; 217} 218 219LayoutManager::LayoutManager() : Glib::ObjectBase("LayoutManager") { 220} 221 222void LayoutManager::add_pane(DockablePane *pane) { 223panes.push_back(pane); 224} 225 226void LayoutManager::add_stack(DockStack *stack) { 227stacks.push_back(stack); 228} 229 230void LayoutManager::remove_pane(DockablePane *pane) { 231panes.erase(std::ranges::remove(panes, pane).begin(), panes.end()); 232} 233 234void LayoutManager::remove_stack(DockStack *stack) { 235stacks.erase(std::ranges::remove(stacks, stack).begin(), stacks.end()); 236} 237 238BaseStack::BaseStack() : Gtk::Stack() { 239} 240 241DockStack::DockStack(std::shared_ptr<LayoutManager> layout, const Glib::ustring &name, const std::string &id) : BaseStack(), layout(layout), name(name), id(id) { 242auto empty_child = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 0); 243this->add(*empty_child, ""); 244// Add the stack to a layout manager 245this->layout->add_stack(this); 246 247// Hide the stack when no child is visible 248this->property_visible_child_name().signal_changed().connect([this]() { 249if(this->get_visible_child_name() == "") { 250this->hide(); 251} else { 252this->show(); 253} 254}); 255 256// Also hide when the visible child is removed 257this->signal_child_removed.connect([this](Gtk::Widget* const &child) { 258if(this->get_visible_child_name() == "") { 259this->hide(); 260} 261}); 262 263this->set_visible_child(""); 264this->hide(); 265} 266 267DockWindow::DockWindow(DockablePane *pane) { 268this->pane = pane; 269this->set_child(*pane); 270// Attempting to close the window should redock the pane so it doesn't vanish 271this->signal_close_request().connect([this]() { 272this->pane->redock(this->pane->last_stack); 273return true; 274}, false); 275} 276 277DockStackSwitcher::DockStackSwitcher(DockStack *stack, Gtk::Orientation orientation) : Gtk::Box(orientation), stack(stack) { 278auto update_callback = [this](Gtk::Widget*) { 279this->update_buttons(); 280}; 281this->property_accessible_role().set_value(Gtk::Accessible::Role::TAB_LIST); 282stack->signal_child_added.connect(update_callback); 283stack->signal_child_removed.connect(update_callback); 284this->get_style_context()->add_class("gpanthera-dock-switcher"); 285drop_target = Gtk::DropTarget::create(DockablePane::get_type(), Gdk::DragAction::MOVE); 286this->add_controller(drop_target); 287// Process dropped buttons 288drop_target->signal_drop().connect([this](const Glib::ValueBase& value, double x, double y) { 289const auto &widget = static_cast<const Glib::Value<DockablePane*>&>(value).get(); 290 291if(widget) { 292if(auto pane = dynamic_cast<DockablePane*>(widget)) { 293this->stack->add_pane(*pane); 294} 295} 296 297return true; // Drop OK 298}, false); 299} 300 301DockStack *DockStackSwitcher::get_stack() const { 302return stack; 303} 304 305DockStackSwitcher::~DockStackSwitcher() { 306add_handler.disconnect(); 307remove_handler.disconnect(); 308} 309 310DockButton::DockButton(DockablePane *pane) : Gtk::ToggleButton(), pane(pane) { 311if(pane->get_icon()) { 312this->set_child(*copy_image(pane->get_icon())); 313} 314this->property_accessible_role().set_value(Gtk::Accessible::Role::TAB); 315this->set_tooltip_text(pane->get_label()->get_text()); 316this->set_halign(Gtk::Align::CENTER); 317this->set_valign(Gtk::Align::CENTER); 318this->get_style_context()->add_class("toggle"); 319this->get_style_context()->add_class("gpanthera-dock-button"); 320// Add/remove CSS classes when the pane is shown/hidden 321active_style_handler = this->pane->get_stack()->property_visible_child_name().signal_changed().connect([this]() { 322this->update_active_style(); 323}); 324drag_source = Gtk::DragSource::create(); 325drag_source->set_exclusive(false); 326// This is to prevent the click handler from taking over grabbing the button 327drag_source->set_propagation_phase(Gtk::PropagationPhase::CAPTURE); 328value.init(DockablePane::get_type()); 329value.set(pane); 330// Add the drag source to the button 331this->add_controller(drag_source); 332this->signal_clicked().connect([this, pane]() { 333if(pane->get_stack()->get_visible_child_name() == pane->get_identifier()) { 334pane->get_stack()->set_visible_child(""); 335} else { 336pane->get_stack()->set_visible_child(pane->get_identifier()); 337} 338}); 339// Provide the drag data 340drag_source->signal_prepare().connect([this](double, double) { 341drag_source->set_actions(Gdk::DragAction::MOVE); 342auto const paintable = Gtk::WidgetPaintable::create(); 343paintable->set_widget(*this); 344drag_source->set_icon(paintable->get_current_image(), 0, 0); 345return Gdk::ContentProvider::create(value); 346}, false); 347drag_source->signal_drag_begin().connect([this](const Glib::RefPtr<Gdk::Drag>&) { 348this->set_opacity(0); 349}, false); 350// Pop out if dragged to an external location 351drag_source->signal_drag_cancel().connect([this](const Glib::RefPtr<Gdk::Drag>&, Gdk::DragCancelReason reason) { 352if(reason == Gdk::DragCancelReason::NO_TARGET) { 353this->pane->pop_out(); 354return true; 355} 356this->set_opacity(1); 357return false; 358}, false); 359// Add a drop target to the button 360auto drop_target = Gtk::DropTarget::create(DockablePane::get_type(), Gdk::DragAction::MOVE); 361drop_target->set_actions(Gdk::DragAction::MOVE); 362drop_target->set_propagation_phase(Gtk::PropagationPhase::CAPTURE); 363this->add_controller(drop_target); 364// Process dropped buttons by inserting them after the current button 365drop_target->signal_drop().connect([this](const Glib::ValueBase& value, double x, double y) { 366const auto &widget = static_cast<const Glib::Value<DockablePane*>&>(value).get(); 367 368if(widget) { 369if(auto pane = dynamic_cast<DockablePane*>(widget)) { 370if(pane->layout != this->pane->layout) { 371// If the pane is not in the same layout manager, reject 372return false; 373} 374auto switcher = dynamic_cast<DockStackSwitcher*>(this->get_parent()); 375if(switcher) { 376auto *stack = switcher->get_stack(); 377// Move the button to the new position 378pane->redock(stack); 379if(switcher->get_orientation() == Gtk::Orientation::HORIZONTAL) { 380if(x < static_cast<double>(this->get_allocated_width()) / 2) { 381pane->insert_before(*stack, *this->pane); 382} else { 383pane->insert_after(*stack, *this->pane); 384} 385} else if(switcher->get_orientation() == Gtk::Orientation::VERTICAL) { 386if(y < static_cast<double>(this->get_allocated_height()) / 2) { 387pane->insert_before(*stack, *this->pane); 388} else { 389pane->insert_after(*stack, *this->pane); 390} 391} 392 393switcher->update_buttons(); 394} 395} 396} 397 398return true; // Drop OK 399}, false); 400this->update_active_style(); 401} 402 403void DockButton::update_active_style() { 404if(this->pane->get_stack()->get_visible_child_name() == this->pane->get_identifier()) { 405this->add_css_class("checked"); 406this->add_css_class("gpanthera-dock-button-active"); 407this->set_active(true); 408} else { 409this->remove_css_class("checked"); 410this->remove_css_class("gpanthera-dock-button-active"); 411this->set_active(false); 412} 413} 414 415DockButton::~DockButton() { 416active_style_handler.disconnect(); 417} 418 419void DockStackSwitcher::update_buttons() { 420// Clear the old buttons 421auto old_buttons = collect_children(*this); 422for(auto *button : old_buttons) { 423remove(*button); 424} 425DockButton* first_child = nullptr; 426for(auto *widget = stack->get_first_child(); widget; widget = widget->get_next_sibling()) { 427if(auto pane = dynamic_cast<DockablePane*>(widget)) { 428auto *button = Gtk::make_managed<DockButton>(pane); 429if(!first_child) { 430first_child = button; 431} else { 432button->set_group(*first_child); 433} 434if(pane->get_identifier() != Glib::ustring("")) { 435append(*button); 436} 437} 438} 439} 440 441void DockStack::add_pane(DockablePane &child) { 442child.redock(this); 443} 444 445void ContentManager::add_stack(ContentStack *stack) { 446this->stacks.push_back(stack); 447} 448 449void ContentManager::remove_stack(ContentStack *stack) { 450this->stacks.erase(std::ranges::remove(this->stacks, stack).begin(), this->stacks.end()); 451} 452 453void BaseStack::add(Gtk::Widget &child, const Glib::ustring &name) { 454Gtk::Stack::add(child, name); 455signal_child_added.emit(&child); 456} 457 458void BaseStack::add(Gtk::Widget &child) { 459Gtk::Stack::add(child); 460signal_child_added.emit(&child); 461} 462 463void BaseStack::remove(Gtk::Widget &child) { 464Gtk::Stack::remove(child); 465signal_child_removed.emit(&child); 466} 467 468void ContentStack::disable_children() { 469auto children = collect_children(*this); 470for(auto *child : children) { 471child->set_can_target(false); 472} 473} 474 475void ContentStack::enable_children() { 476auto children = collect_children(*this); 477for(auto *child : children) { 478child->set_can_target(true); 479} 480} 481 482ContentStack::ContentStack(std::shared_ptr<ContentManager> content_manager, std::function<bool(ContentPage*)> detach_handler) 483: BaseStack(), content_manager(content_manager) { 484this->set_name("gpanthera_content_stack"); 485this->content_manager->add_stack(this); 486if(detach_handler) { 487this->detach_handler = detach_handler; 488this->signal_detach.connect([this](ContentPage *widget) { 489return this->detach_handler(widget); 490}); 491} 492 493drop_target = Gtk::DropTarget::create(ContentPage::get_type(), Gdk::DragAction::MOVE); 494this->add_controller(drop_target); 495// Some widgets (such as webviews) will consume anything dropped on their ancestors. We have 496// to disable all child widgets to ensure the drop can be caught by this widget. 497drop_target->signal_enter().connect([this](double x, double y) { 498disable_children(); 499return Gdk::DragAction::MOVE; 500}, false); 501drop_target->signal_leave().connect([this]() { 502enable_children(); 503}, false); 504// Process dropped buttons 505drop_target->signal_drop().connect([this, content_manager](const Glib::ValueBase &value, double x, double y) { 506enable_children(); 507const auto &widget = static_cast<const Glib::Value<ContentPage*>&>(value).get(); 508 509if(auto page = dynamic_cast<ContentPage*>(widget)) { 510if(page->get_stack() == this && !this->get_first_child()->get_next_sibling()) { 511// Don't allow splitting if there are no more pages 512return false; 513} 514if(!(page->content_manager == this->content_manager)) { 515// If the page is not in the same content manager, reject 516return false; 517} 518double width = this->get_allocated_width(), height = this->get_allocated_height(); 519// Split based on the drop position 520if(!(x < width / 4 || x > width * 3 / 4 || y < height / 4 || y > height * 3 / 4)) { 521if(page->get_stack() == this) { 522return false; 523} 524page->lose_visibility(); 525// If the drop position is not at a quarter to the edges, move to the same stack 526this->add_page(*page); 527return true; 528} 529page->lose_visibility(); 530auto new_stack = Gtk::make_managed<ContentStack>(this->content_manager, this->detach_handler); 531auto this_notebook = dynamic_cast<ContentNotebook*>(this->get_parent()); 532auto new_switcher = Gtk::make_managed<ContentTabBar>(new_stack, this_notebook ? this_notebook->get_switcher()->get_orientation() : Gtk::Orientation::HORIZONTAL, this_notebook->get_switcher()->get_extra_child_function()); 533auto new_notebook = Gtk::make_managed<ContentNotebook>(new_stack, new_switcher, this_notebook ? this_notebook->get_tab_position() : Gtk::PositionType::TOP); 534new_stack->add_page(*page); 535new_stack->set_visible_child(*page); 536content_manager->set_last_operated_page(page); 537if(x < width / 4) { 538this->make_paned(Gtk::Orientation::HORIZONTAL, Gtk::PackType::START); 539if(auto paned = dynamic_cast<Gtk::Paned*>(this->get_parent()->get_parent())) { 540paned->set_start_child(*new_notebook); 541} 542} else if(x > width * 3 / 4) { 543this->make_paned(Gtk::Orientation::HORIZONTAL, Gtk::PackType::END); 544if(auto paned = dynamic_cast<Gtk::Paned*>(this->get_parent()->get_parent())) { 545paned->set_end_child(*new_notebook); 546} 547} else if(y < height / 4) { 548this->make_paned(Gtk::Orientation::VERTICAL, Gtk::PackType::START); 549if(auto paned = dynamic_cast<Gtk::Paned*>(this->get_parent()->get_parent())) { 550paned->set_start_child(*new_notebook); 551} 552} else if(y > height * 3 / 4) { 553this->make_paned(Gtk::Orientation::VERTICAL, Gtk::PackType::END); 554if(auto paned = dynamic_cast<Gtk::Paned*>(this->get_parent()->get_parent())) { 555paned->set_end_child(*new_notebook); 556} 557} 558} 559 560return true; // Drop OK 561}, false); 562} 563 564std::function<bool(ContentPage*)> ContentStack::get_detach_handler() const { 565return this->detach_handler; 566} 567 568ContentNotebook::ContentNotebook(ContentStack *stack, ContentTabBar *switcher, Gtk::PositionType tab_position) : Gtk::Box(), stack(stack), switcher(switcher), tab_position(tab_position) { 569this->append(*stack); 570if(tab_position == Gtk::PositionType::TOP || tab_position == Gtk::PositionType::BOTTOM) { 571this->set_orientation(Gtk::Orientation::VERTICAL); 572if(tab_position == Gtk::PositionType::TOP) { 573this->prepend(*switcher); 574} else if(tab_position == Gtk::PositionType::BOTTOM) { 575this->append(*switcher); 576} 577} else if(tab_position == Gtk::PositionType::LEFT || tab_position == Gtk::PositionType::RIGHT) { 578this->set_orientation(Gtk::Orientation::HORIZONTAL); 579if(tab_position == Gtk::PositionType::LEFT) { 580this->prepend(*switcher); 581} else if(tab_position == Gtk::PositionType::RIGHT) { 582this->append(*switcher); 583} 584} 585} 586 587void ContentStack::make_paned(Gtk::Orientation orientation, Gtk::PackType pack_type) { 588auto *parent = this->get_parent(); 589if(auto notebook = dynamic_cast<ContentNotebook*>(parent)) { 590auto *paned = Gtk::make_managed<Gtk::Paned>(orientation); 591if(auto parent_paned = dynamic_cast<Gtk::Paned*>(notebook->get_parent())) { 592if(parent_paned->get_start_child() == notebook) { 593notebook->unparent(); 594parent_paned->set_start_child(*paned); 595} else if(parent_paned->get_end_child() == notebook) { 596notebook->unparent(); 597parent_paned->set_end_child(*paned); 598} 599} else if(auto box = dynamic_cast<Gtk::Box*>(notebook->get_parent())) { 600auto previous_child = notebook->get_prev_sibling(); 601box->remove(*notebook); 602if(previous_child) { 603paned->insert_after(*box, *previous_child); 604} else { 605box->prepend(*paned); 606} 607} 608if(pack_type == Gtk::PackType::START) { 609paned->set_end_child(*notebook); 610} else if(pack_type == Gtk::PackType::END) { 611paned->set_start_child(*notebook); 612} 613} 614} 615 616ContentTabBar::ContentTabBar(ContentStack *stack, Gtk::Orientation orientation, std::function<Gtk::Widget*(gPanthera::ContentTabBar*)> extra_child_function) : Gtk::Box(orientation), stack(stack), extra_child_function(extra_child_function) { 617this->get_style_context()->add_class("gpanthera-content-tab-bar"); 618this->set_margin_top(0); 619this->set_margin_bottom(0); 620this->set_margin_start(0); 621this->set_margin_end(0); 622 623auto update_callback = [this](Gtk::Widget*) { 624this->update_buttons(); 625}; 626this->property_accessible_role().set_value(Gtk::Accessible::Role::TAB_LIST); 627stack->signal_child_added.connect(update_callback); 628stack->signal_child_removed.connect(update_callback); 629drop_target = Gtk::DropTarget::create(ContentPage::get_type(), Gdk::DragAction::MOVE); 630this->add_controller(drop_target); 631// Process dropped buttons 632drop_target->signal_drop().connect([this](const Glib::ValueBase &value, double x, double y) { 633if(const auto &widget = static_cast<const Glib::Value<ContentPage*>&>(value).get()) { 634if(auto page = dynamic_cast<ContentPage*>(widget)) { 635if(page->get_stack() == this->stack) { 636this->stack->remove(*page); 637this->stack->add(*page); 638} else { 639this->stack->add_page(*page); 640} 641} 642} 643 644return true; // Drop OK 645}, false); 646 647scrolled_window = Gtk::make_managed<Gtk::ScrolledWindow>(); 648auto viewport = Gtk::make_managed<Gtk::Viewport>(nullptr, nullptr); 649tab_box = Gtk::make_managed<Gtk::Box>(orientation); 650this->prepend(*scrolled_window); 651scrolled_window->set_child(*viewport); 652viewport->set_child(*tab_box); 653 654this->set_orientation(orientation); 655 656if(this->extra_child_function) { 657this->append(*this->extra_child_function(this)); 658} 659} 660 661std::function<Gtk::Widget*(gPanthera::ContentTabBar*)> ContentTabBar::get_extra_child_function() const { 662return this->extra_child_function; 663} 664 665void ContentTabBar::set_extra_child_function(std::function<Gtk::Widget*(gPanthera::ContentTabBar*)> extra_child_function) { 666// Note that this doesn't remove the existing extra child 667this->extra_child_function = extra_child_function; 668} 669 670void ContentTabBar::set_orientation(Gtk::Orientation orientation) { 671this->Gtk::Box::set_orientation(orientation); 672if(orientation == Gtk::Orientation::HORIZONTAL) { 673scrolled_window->set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::NEVER); 674tab_box->set_orientation(Gtk::Orientation::HORIZONTAL); 675scrolled_window->set_hexpand(true); 676scrolled_window->set_vexpand(false); 677} else if(orientation == Gtk::Orientation::VERTICAL) { 678scrolled_window->set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC); 679tab_box->set_orientation(Gtk::Orientation::VERTICAL); 680scrolled_window->set_vexpand(true); 681scrolled_window->set_hexpand(false); 682} 683} 684 685ContentTabBar::~ContentTabBar() { 686add_handler.disconnect(); 687remove_handler.disconnect(); 688} 689 690ContentStack *ContentTabBar::get_stack() const { 691return stack; 692} 693 694void ContentTabBar::update_buttons() { 695// Clear the old buttons 696auto old_buttons = collect_children(*this->tab_box); 697for(auto *button : old_buttons) { 698if(auto *button_button = dynamic_cast<Gtk::Button*>(button)) { 699button_button->unset_child(); 700tab_box->remove(*button); 701} 702} 703ContentTab* first_child = nullptr; 704for(auto *widget = stack->get_first_child(); widget; widget = widget->get_next_sibling()) { 705if(auto page = dynamic_cast<ContentPage*>(widget)) { 706auto *button = Gtk::make_managed<ContentTab>(page); 707if(!first_child) { 708first_child = button; 709} else { 710button->set_group(*first_child); 711} 712this->tab_box->append(*button); 713} 714} 715} 716 717void ContentStack::add_page(ContentPage &child) { 718child.redock(this); 719} 720 721void ContentStack::remove_with_paned() { 722if(auto paned = dynamic_cast<Gtk::Paned*>(this->get_parent()->get_parent())) { 723Gtk::Widget *child = nullptr; 724if(this->get_parent() == paned->get_start_child()) { 725child = paned->get_end_child(); 726} else if(this->get_parent() == paned->get_end_child()) { 727child = paned->get_start_child(); 728} else { 729return; 730} 731 732g_object_ref(child->gobj()); // Prevent the child from being automatically deleted 733paned->property_start_child().reset_value(); // Some hacks because the Gtkmm API isn't complete; it doesn't have an unset_start_child() or unset_end_child() 734paned->property_end_child().reset_value(); 735 736if(child) { 737if(auto parent_paned = dynamic_cast<Gtk::Paned*>(paned->get_parent())) { 738if(parent_paned->get_start_child() == paned) { 739parent_paned->set_start_child(*child); 740} else if(parent_paned->get_end_child() == paned) { 741parent_paned->set_end_child(*child); 742} 743} else if(auto box = dynamic_cast<Gtk::Box*>(paned->get_parent())) { 744child->insert_after(*box, *paned); 745paned->unparent(); 746g_object_unref(child->gobj()); 747} 748} 749} 750} 751 752ContentTab::ContentTab(ContentPage *page) : Gtk::ToggleButton(), page(page) { 753this->set_child(*page->get_tab_widget()); 754this->property_accessible_role().set_value(Gtk::Accessible::Role::TAB); 755this->set_halign(Gtk::Align::CENTER); 756this->set_valign(Gtk::Align::CENTER); 757this->get_style_context()->add_class("toggle"); 758this->get_style_context()->add_class("gpanthera-content-tab"); 759// Add/remove CSS classes when the pane is shown/hidden 760active_style_handler = this->page->get_stack()->property_visible_child().signal_changed().connect([this]() { 761this->update_active_style(); 762}); 763drag_source = Gtk::DragSource::create(); 764drag_source->set_exclusive(false); 765// This is to prevent the click handler from taking over grabbing the button 766drag_source->set_propagation_phase(Gtk::PropagationPhase::CAPTURE); 767value.init(ContentPage::get_type()); 768value.set(page); 769// Add the drag source to the button 770this->add_controller(drag_source); 771this->signal_clicked().connect([this, page]() { 772page->get_stack()->set_visible_child(*page); 773page->content_manager->set_last_operated_page(page); 774update_active_style(); 775}); 776// Switch tabs on depress 777auto gesture_click = Gtk::GestureClick::create(); 778gesture_click->set_button(1); 779this->add_controller(gesture_click); 780gesture_click->signal_pressed().connect([this, page](int num_presses, double x, double y) { 781page->get_stack()->set_visible_child(*page); 782page->content_manager->set_last_operated_page(page); 783update_active_style(); 784}); 785// Provide the drag data 786drag_source->signal_prepare().connect([this](double, double) { 787drag_source->set_actions(Gdk::DragAction::MOVE); 788auto const paintable = Gtk::WidgetPaintable::create(); 789paintable->set_widget(*this); 790drag_source->set_icon(paintable->get_current_image(), 0, 0); 791return Gdk::ContentProvider::create(value); 792}, false); 793update_active_style(); 794drag_source->signal_drag_begin().connect([this](const Glib::RefPtr<Gdk::Drag>&) { 795this->set_opacity(0); 796}, false); 797drop_target = Gtk::DropTarget::create(ContentPage::get_type(), Gdk::DragAction::MOVE); 798// Process dropped buttons by inserting them after the current button 799drop_target->signal_drop().connect([this](const Glib::ValueBase &value, double x, double y) { 800const auto &widget = static_cast<const Glib::Value<ContentPage*>&>(value).get(); 801 802if(widget) { 803if(auto page = dynamic_cast<ContentPage*>(widget)) { 804if(page->content_manager != this->page->content_manager) { 805// If the pane is not in the same layout manager, reject 806return false; 807} 808auto switcher = dynamic_cast<ContentTabBar*>(this->get_parent()->get_parent()->get_parent()->get_parent()); 809if(switcher) { 810auto *stack = switcher->get_stack(); 811// Move the button to the new position 812page->redock(stack); 813if(switcher->get_orientation() == Gtk::Orientation::HORIZONTAL) { 814if(x < static_cast<double>(this->get_allocated_width()) / 2) { 815page->insert_before(*stack, *this->page); 816} else { 817page->insert_after(*stack, *this->page); 818} 819} else if(switcher->get_orientation() == Gtk::Orientation::VERTICAL) { 820if(y < static_cast<double>(this->get_allocated_height()) / 2) { 821page->insert_before(*stack, *this->page); 822} else { 823page->insert_after(*stack, *this->page); 824} 825} 826 827switcher->update_buttons(); 828} 829} 830} 831 832return true; // Drop OK 833}, false); 834this->add_controller(drop_target); 835// Pop out if dragged to an external location 836drag_cancel_handler = drag_source->signal_drag_cancel().connect([this](const Glib::RefPtr<Gdk::Drag>&, Gdk::DragCancelReason reason) { 837if(reason == Gdk::DragCancelReason::NO_TARGET) { 838auto stack = dynamic_cast<ContentStack*>(this->page->get_stack()); 839bool result = stack->signal_detach.emit(this->page); 840if(!result) { 841this->set_opacity(1); 842} 843return result; 844} 845this->set_opacity(1); 846return false; 847}, false); 848drag_end_handler = drag_source->signal_drag_end().connect([this](const Glib::RefPtr<Gdk::Drag>&, bool drop_ok) { 849if(drop_ok) { 850this->set_opacity(1); 851} 852}, false); 853 854// Provide a context menu 855context_menu = Gio::Menu::create(); 856auto action_group = Gio::SimpleActionGroup::create(); 857this->insert_action_group("win", action_group); 858 859auto close_action = Gio::SimpleAction::create("close"); 860close_action->signal_activate().connect([this](const Glib::VariantBase&) { 861this->close(); 862}); 863action_group->add_action(close_action); 864context_menu->append(_("Close"), "win.close"); 865 866auto close_all_action = Gio::SimpleAction::create("close-all"); 867close_all_action->signal_activate().connect([this](const Glib::VariantBase&) { 868for(auto page : collect_children(*this->page->get_stack())) { 869if(auto content_page = dynamic_cast<ContentPage*>(page)) { 870if(!content_page->signal_close.emit()) { 871content_page->redock(nullptr); 872} 873} 874} 875}); 876action_group->add_action(close_all_action); 877context_menu->append(_("Close all"), "win.close-all"); 878 879auto close_others_action = Gio::SimpleAction::create("close-others"); 880close_others_action->signal_activate().connect([this](const Glib::VariantBase&) { 881for(auto page : collect_children(*this->page->get_stack())) { 882if(auto content_page = dynamic_cast<ContentPage*>(page)) { 883if(content_page != this->page && !content_page->signal_close.emit()) { 884content_page->redock(nullptr); 885} 886} 887} 888}); 889action_group->add_action(close_others_action); 890context_menu->append(_("Close others"), "win.close-others"); 891 892auto detach_action = Gio::SimpleAction::create("detach"); 893detach_action->signal_activate().connect([this](const Glib::VariantBase&) { 894auto stack = dynamic_cast<ContentStack*>(this->page->get_stack()); 895bool result = stack->signal_detach.emit(this->page); 896}); 897action_group->add_action(detach_action); 898context_menu->append(_("New window"), "win.detach"); 899 900// TODO: Add more actions: "New window", "Split left", "Split right", "Split top", "Split bottom", "Close all", "Close others", "Close to the right", "Close to the left" 901this->insert_action_group("win", action_group); 902// Attach the context menu to the button 903auto context_menu_signal = add_context_menu(*this); 904context_menu_signal.connect([this, action_group](double x, double y) { 905if(this->context_menu) { 906auto popover = Gtk::make_managed<Gtk::PopoverMenu>(); 907popover->set_menu_model(context_menu); 908popover->set_parent(*this); 909popover->set_has_arrow(false); 910popover->set_halign(Gtk::Align::START); 911popover->set_pointing_to(Gdk::Rectangle(x, y, 1, 1)); 912popover->popup(); 913} 914}); 915auto middle_click_controller = Gtk::GestureClick::create(); 916middle_click_controller->set_button(2); 917middle_click_controller->signal_released().connect([this](int num_presses, double x, double y) { 918this->close(); 919}); 920this->add_controller(middle_click_controller); 921control_status_handler = this->page->signal_control_status_changed.connect([this](bool operating) { 922if(operating) { 923this->add_css_class("gpanthera-selected-tab"); 924} else { 925this->remove_css_class("gpanthera-selected-tab"); 926} 927}); 928} 929 930void ContentTab::close() { 931page->close(); 932} 933 934void ContentPage::close() { 935if(!this->signal_close.emit()) { 936if(this->get_next_sibling()) { 937this->content_manager->set_last_operated_page(static_cast<ContentPage*>(this->get_next_sibling())); 938this->get_stack()->set_visible_child(*this->get_next_sibling()); 939} else if(this->get_prev_sibling()) { 940this->content_manager->set_last_operated_page(static_cast<ContentPage*>(this->get_prev_sibling())); 941this->get_stack()->set_visible_child(*this->get_prev_sibling()); 942} 943this->redock(nullptr); 944} 945} 946 947void ContentTab::update_active_style() { 948if(this->page->get_stack()->get_visible_child() == this->page) { 949this->add_css_class("checked"); 950this->add_css_class("gpanthera-dock-button-active"); 951this->set_active(true); 952} else { 953this->remove_css_class("checked"); 954this->remove_css_class("gpanthera-dock-button-active"); 955this->set_active(false); 956} 957} 958 959ContentTab::~ContentTab() { 960active_style_handler.disconnect(); 961drag_end_handler.disconnect(); 962drag_cancel_handler.disconnect(); 963control_status_handler.disconnect(); 964} 965 966Gtk::Widget *ContentPage::get_tab_widget() const { 967return this->tab_widget; 968} 969 970ContentStack *ContentPage::get_stack() const { 971return this->stack; 972} 973 974void ContentPage::lose_visibility() { 975if(this->get_next_sibling()) { 976this->content_manager->set_last_operated_page(static_cast<ContentPage*>(this->get_next_sibling())); 977this->get_stack()->set_visible_child(*this->get_next_sibling()); 978} else if(this->get_prev_sibling()) { 979this->content_manager->set_last_operated_page(static_cast<ContentPage*>(this->get_prev_sibling())); 980this->get_stack()->set_visible_child(*this->get_prev_sibling()); 981} 982} 983 984void ContentPage::redock(ContentStack *stack) { 985if(stack == nullptr) { 986if(this->stack) { 987this->stack->remove(*this); 988if(dynamic_cast<ContentNotebook*>(this->stack->get_parent()) && !this->stack->get_first_child()) { 989this->stack->remove_with_paned(); 990} 991} 992auto old_stack = this->stack; 993if(old_stack) { 994if(!old_stack->get_first_child()) { 995old_stack->signal_leave_empty.emit(); 996} 997} 998this->stack = nullptr; 999this->last_stack = nullptr; 1000return; 1001} 1002// Check if the stack is now empty, in which case we should remove it 1003if(this->stack == stack) { 1004return; 1005} 1006if(this->stack != nullptr) { 1007if(dynamic_cast<ContentNotebook*>(this->stack->get_parent()) && (!this->stack->get_first_child() || (this->stack->get_first_child() == this && !this->stack->get_first_child()->get_next_sibling()))) { 1008this->stack->remove(*this); 1009this->stack->remove_with_paned(); 1010} else if(this->get_parent() == this->stack) { 1011this->stack->remove(*this); 1012} 1013} 1014auto old_stack = this->stack; 1015this->stack = stack; 1016this->last_stack = stack; 1017this->stack->add(*this); 1018if(old_stack) { 1019if(!old_stack->get_first_child()) { 1020old_stack->signal_leave_empty.emit(); 1021} 1022} 1023} 1024 1025ContentPage::ContentPage(std::shared_ptr<ContentManager> content_manager, ContentStack *stack, Gtk::Widget *child, Gtk::Widget *tab_widget) : 1026Gtk::Overlay(), content_manager(std::move(content_manager)), child(child), tab_widget(tab_widget) { 1027this->set_name("gpanthera_content_page"); 1028this->set_child(*child); 1029this->set_tab_widget(tab_widget); 1030this->set_margin_top(0); 1031this->set_margin_bottom(0); 1032this->set_margin_start(0); 1033this->set_margin_end(0); 1034if(stack) { 1035stack->add_page(*this); 1036this->content_manager->add_stack(this->stack); 1037} 1038auto click_controller = Gtk::GestureClick::create(); 1039click_controller->set_button(0); 1040click_controller->signal_pressed().connect([this](int num_presses, double x, double y) { 1041this->content_manager->set_last_operated_page(this); 1042}); 1043this->property_has_focus().signal_changed().connect([this]() { 1044if(this->property_has_focus()) { 1045this->content_manager->set_last_operated_page(this); 1046} 1047}); 1048click_controller->set_propagation_phase(Gtk::PropagationPhase::CAPTURE); 1049this->add_controller(click_controller); 1050} 1051 1052ContentManager::ContentManager() : Glib::ObjectBase("ContentManager") { 1053} 1054 1055void ContentPage::set_tab_widget(Gtk::Widget *tab_widget) { 1056this->tab_widget = tab_widget; 1057} 1058 1059ContentWindow::ContentWindow(ContentNotebook *notebook) : notebook(notebook) { 1060this->set_child(*notebook); 1061this->set_decorated(true); 1062this->set_resizable(true); 1063} 1064 1065Gtk::PositionType ContentNotebook::get_tab_position() const { 1066return this->tab_position; 1067} 1068 1069ContentTabBar *ContentNotebook::get_switcher() const { 1070return this->switcher; 1071} 1072 1073ContentStack *ContentNotebook::get_stack() const { 1074return this->stack; 1075} 1076 1077ContentPage *ContentManager::get_last_operated_page() const { 1078return this->last_operated_page; 1079} 1080 1081void ContentManager::set_last_operated_page(ContentPage *page) { 1082if(this->last_operated_page) { 1083this->last_operated_page->signal_control_status_changed.emit(false); 1084} 1085this->last_operated_page = page; 1086page->signal_control_status_changed.emit(true); 1087this->signal_page_operated.emit(page); 1088} 1089 1090DockWindow *DockablePane::get_window() const { 1091return this->window; 1092} 1093 1094std::string LayoutManager::get_layout_as_json() const { 1095nlohmann::json json; 1096std::unordered_set<DockablePane*> panes_set; 1097for(auto pane: panes) { 1098panes_set.insert(pane); 1099} 1100for(auto stack: stacks) { 1101for(auto pane: collect_children(*stack)) { 1102if(dynamic_cast<DockablePane*>(pane)) { 1103panes_set.erase(dynamic_cast<DockablePane*>(pane)); 1104json[stack->id].push_back(dynamic_cast<DockablePane*>(pane)->get_identifier()); 1105} 1106} 1107} 1108for(auto pane: panes_set) { 1109if(pane->get_window()) { 1110json[""].push_back(pane->get_identifier()); 1111} 1112} 1113return json.dump(); 1114} 1115 1116void LayoutManager::restore_json_layout(const std::string &json_string) { 1117nlohmann::json json = nlohmann::json::parse(json_string); 1118for(auto stack: stacks) { 1119if(json.contains(stack->id)) { 1120for(auto const &pane_id: json[stack->id]) { 1121for(auto pane: panes) { 1122// TODO: could probably be done with a better complexity if there are 1123// many panes 1124if(pane->get_identifier() == static_cast<Glib::ustring>(pane_id.get<std::string>())) { 1125stack->add_pane(*pane); 1126break; 1127} 1128} 1129} 1130} 1131} 1132// Process popped-out panes 1133for(auto const &pane_id: json[""]) { 1134for(auto pane: panes) { 1135// TODO: could probably be done with a better complexity if there are 1136// many panes 1137if(pane->get_identifier() == static_cast<Glib::ustring>(pane_id.get<std::string>())) { 1138pane->pop_out(); 1139break; 1140} 1141} 1142} 1143} 1144} // namespace gPanthera 1145