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