/****************************************************************************** License: GNU General Public License v3.0 only Copyright: (C) 2008 - 2021 Alexander Shaduri ******************************************************************************/ /// \file /// \author Alexander Shaduri /// \ingroup applib /// \weakgroup applib /// @{ #include "gui_utils.h" #include #include namespace { /// Show a message dialog inline void show_dialog(const std::string& message, const std::string& sec_message, Gtk::Window* parent, Gtk::MessageType type, bool sec_msg_markup) { // no markup, modal Gtk::MessageDialog dialog("\n" + message + (sec_message.empty() ? "\n" : ""), false, type, Gtk::BUTTONS_OK, true); if (!sec_message.empty()) dialog.set_secondary_text(sec_message, sec_msg_markup); if (parent) { dialog.set_transient_for(*parent); dialog.set_position(Gtk::WIN_POS_CENTER_ON_PARENT); } else { dialog.set_position(Gtk::WIN_POS_MOUSE); } dialog.run(); // blocks until the dialog is closed } } void gui_show_error_dialog(const std::string& message, Gtk::Window* parent) { show_dialog(message, "", parent, Gtk::MESSAGE_ERROR, false); } void gui_show_error_dialog(const std::string& message, const std::string& sec_message, Gtk::Window* parent, bool sec_msg_markup) { show_dialog(message, sec_message, parent, Gtk::MESSAGE_ERROR, sec_msg_markup); } void gui_show_warn_dialog(const std::string& message, Gtk::Window* parent) { show_dialog(message, "", parent, Gtk::MESSAGE_WARNING, false); } void gui_show_warn_dialog(const std::string& message, const std::string& sec_message, Gtk::Window* parent, bool sec_msg_markup) { show_dialog(message, sec_message, parent, Gtk::MESSAGE_WARNING, sec_msg_markup); } void gui_show_info_dialog(const std::string& message, Gtk::Window* parent) { show_dialog(message, "", parent, Gtk::MESSAGE_INFO, false); } void gui_show_info_dialog(const std::string& message, const std::string& sec_message, Gtk::Window* parent, bool sec_msg_markup) { show_dialog(message, sec_message, parent, Gtk::MESSAGE_INFO, sec_msg_markup); } // Returns false if Cancel was clicked. Puts the user-entered string into result otherwise. bool gui_show_text_entry_dialog(const std::string& title, const std::string& message, std::string& result, const std::string& default_str, Gtk::Window* parent) { return gui_show_text_entry_dialog(title, message, "", result, default_str, parent, false); } bool gui_show_text_entry_dialog(const std::string& title, const std::string& message, const std::string& sec_message, std::string& result, const std::string& default_str, Gtk::Window* parent, bool sec_msg_markup) { int response = 0; std::string input_str; { // the dialog hides at the end of scope Gtk::Dialog dialog(title, true); // modal dialog.set_resizable(false); dialog.set_skip_taskbar_hint(true); dialog.set_border_width(5); if (parent) { dialog.set_transient_for(*parent); dialog.set_position(Gtk::WIN_POS_CENTER_ON_PARENT); } else { dialog.set_position(Gtk::WIN_POS_MOUSE); } Gtk::Label main_label; main_label.set_markup("" + Glib::Markup::escape_text(message) + (sec_message.empty() ? "\n" : "") + ""); main_label.set_line_wrap(true); main_label.set_selectable(true); main_label.set_alignment(Gtk::ALIGN_START); Gtk::Label sec_label; if (sec_msg_markup) { sec_label.set_markup(sec_message); } else { sec_label.set_text(sec_message); } sec_label.set_line_wrap(true); sec_label.set_selectable(true); sec_label.set_alignment(Gtk::ALIGN_START); Gtk::Entry input_entry; input_entry.set_activates_default(true); input_entry.set_text(default_str); Gtk::Box vbox; vbox.set_spacing(12); vbox.pack_start(main_label, false, false, 0); vbox.pack_start(sec_label, true, true, 0); vbox.pack_start(input_entry, true, true, 0); vbox.show_all(); dialog.get_action_area()->set_border_width(5); dialog.get_action_area()->set_spacing(6); dialog.get_vbox()->set_spacing(14); // as in MessageDialog dialog.get_vbox()->pack_start(vbox, false, false, 0); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); Gtk::Button ok_button(Gtk::Stock::OK); ok_button.set_can_default(true); ok_button.show_all(); dialog.add_action_widget(ok_button, Gtk::RESPONSE_OK); ok_button.grab_default(); // make it the default widget response = dialog.run(); // blocks until the dialog is closed input_str = input_entry.get_text(); } if (response == Gtk::RESPONSE_OK) { result = input_str; return true; } return false; } bool gui_is_dark_theme_active() { // Try to get the GTK settings to check for dark theme preference. // If GTK is not available or not initialized, get_default() will return null. const Glib::RefPtr settings = Gtk::Settings::get_default(); if (settings) { // Check if the application prefers dark theme if (settings->property_gtk_application_prefer_dark_theme().get_value()) { return true; } // Check theme name for common dark theme identifiers Glib::ustring theme_name; settings->get_property("gtk-theme-name", theme_name); const std::string theme_str = theme_name.lowercase(); if (theme_str.find("dark") != std::string::npos || theme_str.find("black") != std::string::npos) { return true; } } return false; } namespace { /// Storage for Windows fractional scaling percentage (0 if no fractional scaling) int g_windows_fractional_scaling_percent = 0; } bool app_is_fractional_scaling(int full_scale_percent) { // Scales below 100% are not handled (GTK3 never scales below 1x). return full_scale_percent > 100 && full_scale_percent % 100 != 0; } int app_get_fractional_scaling_part_percent(int full_scale_percent) { if (!app_is_fractional_scaling(full_scale_percent)) { return 0; } return full_scale_percent % 100; // 250 -> 50, 125 -> 25 } double app_compute_fractional_scaling_correction(int full_scale_percent) { if (!app_is_fractional_scaling(full_scale_percent)) { return 0.0; } const double system_scale = static_cast(full_scale_percent) / 100.0; const int integer_ui_scale = full_scale_percent / 100; // GTK3 uses floor (integer) scaling; >= 1 here return system_scale / static_cast(integer_ui_scale); } std::pair app_apply_fractional_scaling_to_size(int base_w, int base_h, double correction) { if (correction <= 0.0 || base_w <= 0 || base_h <= 0) { return {base_w, base_h}; } return { static_cast(std::lround(base_w * correction)), static_cast(std::lround(base_h * correction)) }; } int app_compute_scaling_percent_from_dpi(double h_ppi) { if (h_ppi <= 0.0) { return 0; } return static_cast(std::lround(h_ppi / 96.0 * 100.0)); } int app_get_windows_fractional_scaling_percent() { return g_windows_fractional_scaling_percent; } void app_set_windows_fractional_scaling_percent(int percent) { g_windows_fractional_scaling_percent = percent; } void app_apply_fractional_scaling_to_default_size(Gtk::Window* window, int config_size_w, int config_size_h) { if (!window) { return; } int size_w = config_size_w; int size_h = config_size_h; // Apply fractional scaling adjustment on Windows if no custom size is configured // This compensates for GTK3's lack of fractional scaling support const int full_percent = g_windows_fractional_scaling_percent; if (full_percent > 0 && size_w == 0 && size_h == 0) { // Get the default size from glade and scale it int glade_w = 0, glade_h = 0; window->get_default_size(glade_w, glade_h); const double correction = app_compute_fractional_scaling_correction(full_percent); std::tie(size_w, size_h) = app_apply_fractional_scaling_to_size(glade_w, glade_h, correction); } if (size_w > 0 && size_h > 0) { window->set_default_size(size_w, size_h); } } /// @}