| 1 | /* Copyright 2025 Maxim Iorsh <[email protected]> |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "application.hpp" |
| 29 | |
| 30 | #include <iostream> |
| 31 | #include <sstream> |
| 32 | |
| 33 | #include "gresource.h" |
| 34 | #include "css_builder.hpp" |
| 35 | |
| 36 | extern GResInfo gdraw_ri; |
| 37 | extern "C" void GResEditDoInit(GResInfo* ri); |
| 38 | |
| 39 | namespace ff::app { |
| 40 | |
| 41 | Glib::RefPtr<Gtk::Application> GtkApp() { |
| 42 | // Unique instance mode doesn't work well as long as the startup sequence is |
| 43 | // handled in the legacy code. It would be possible to enable it once the |
| 44 | // application main loop is started with Gtk::Application::run(). |
| 45 | Gio::ApplicationFlags app_flags = Gio::APPLICATION_NON_UNIQUE; |
| 46 | |
| 47 | static auto app = Gtk::Application::create("org.fontforge", app_flags); |
| 48 | static bool initialized = false; |
| 49 | |
| 50 | if (!initialized) { |
| 51 | if (app) { |
| 52 | // TODO(iorsh): Registration call sometimes freezes or crashes in |
| 53 | // Windows, we shall enable it after the GTK code takes over the |
| 54 | // main loop. |
| 55 | // |
| 56 | // app->register_application(); |
| 57 | } else { |
| 58 | std::cerr << "Error: Failed to create Gtk::Application instance" |
| 59 | << std::endl; |
| 60 | } |
| 61 | load_legacy_style(); |
| 62 | |
| 63 | initialized = true; |
| 64 | } |
| 65 | |
| 66 | return app; |
| 67 | } |
| 68 | |
| 69 | void load_legacy_style() { |
| 70 | static Glib::RefPtr<Gtk::CssProvider> css_provider = |
| 71 | Gtk::CssProvider::create(); |
| 72 | if (!css_provider) |
| 73 | std::cerr << "Error: Failed to create CSS provider" << std::endl; |
| 74 | static bool initialized = false; |
| 75 | |
| 76 | if (!initialized) { |
| 77 | // Add CSS provider to the screen, so that it applies to all windows. |
| 78 | auto screen = Gdk::Screen::get_default(); |
| 79 | if (!screen) |
| 80 | std::cerr << "Error: Failed to retrieve GDK screen" << std::endl; |
| 81 | |
| 82 | // User-defined CSS should usually go with USER priority, but we reduce |
| 83 | // it by 2 so that widget-specific customizations and the GTK inspector, |
| 84 | // which also applies USER, would have priority over it. |
| 85 | if (screen && css_provider) { |
| 86 | Gtk::StyleContext::add_provider_for_screen( |
| 87 | screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER - 2); |
| 88 | } |
| 89 | |
| 90 | initialized = true; |
| 91 | } |
| 92 | |
| 93 | for (GResInfo* re = &gdraw_ri; re != NULL; re = re->next) |
| 94 | GResEditDoInit(re); |
| 95 | std::string style = build_styles(&gdraw_ri); |
| 96 | |
| 97 | // Load CSS styles |
| 98 | try { |
| 99 | css_provider && css_provider->load_from_data(style); |
| 100 | } catch (const Glib::Error& ex) { |
| 101 | std::cerr << "Failed CSS data: " << std::endl << style << std::endl; |
| 102 | std::cerr << "Error loading CSS: " << ex.what() << std::endl; |
| 103 | } catch (...) { |
| 104 | std::cerr << "Unknown error occurred while loading CSS." << std::endl; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | ColorManager::ColorManager() { |
| 109 | style_ctx_ = Gtk::StyleContext::create(); |
| 110 | |
| 111 | Gtk::WidgetPath path; |
| 112 | path.path_append_type(GTK_TYPE_WINDOW); |
| 113 | style_ctx_->set_path(path); |
| 114 | style_ctx_->set_screen(Gdk::Screen::get_default()); |
| 115 | |
| 116 | custom_color_provider_ = Gtk::CssProvider::create(); |
| 117 | style_ctx_->add_provider(custom_color_provider_, |
| 118 | GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 119 | } |
| 120 | |
| 121 | ColorManager& ColorManager::instance() { |
| 122 | static ColorManager manager; |
| 123 | return manager; |
| 124 | } |
| 125 | |
| 126 | void ColorManager::register_colors(const ColorMap& colors) { |
| 127 | std::ostringstream css; |
| 128 | for (const auto& [ff_color_name, theme_color_pair] : colors) { |
| 129 | const std::string& theme_color_name = theme_color_pair.first; |
| 130 | Gdk::RGBA resolved; |
| 131 | if (!style_ctx_->lookup_color(theme_color_name, resolved)) { |
| 132 | // Fallback to hardcoded color if the theme color is not found. |
| 133 | resolved = theme_color_pair.second; |
| 134 | std::cerr << "In the declaration of \"" << ff_color_name |
| 135 | << "\": GTK theme color " << theme_color_name |
| 136 | << " not found, using fallback." << std::endl; |
| 137 | } |
| 138 | css << "@define-color " << ff_color_name << " " << resolved.to_string() |
| 139 | << ";\n"; |
| 140 | } |
| 141 | |
| 142 | custom_color_provider_->load_from_data(css.str()); |
| 143 | } |
| 144 | |
| 145 | void ColorManager::set_color_in_context(const Cairo::RefPtr<Cairo::Context>& cr, |
| 146 | const std::string& color_name) const { |
| 147 | Gdk::RGBA color; |
| 148 | // Fallback to Pantone 448 C, the ugliest color in the world. |
| 149 | color.set_rgba_u(0x4A00, 0x4100, 0x2A00, 0xFFFF); |
| 150 | |
| 151 | bool result = style_ctx_->lookup_color(color_name, color); |
| 152 | if (!result) { |
| 153 | std::cerr << "CSS color " << color_name |
| 154 | << " not registered, using fallback." << std::endl; |
| 155 | } |
| 156 | |
| 157 | cr->set_source_rgba(color.get_red(), color.get_green(), color.get_blue(), |
| 158 | color.get_alpha()); |
| 159 | } |
| 160 | |
| 161 | } // namespace ff::app |