| 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 "utils.hpp" |
| 29 | |
| 30 | #include <glib/gprintf.h> |
| 31 | #include <iostream> |
| 32 | |
| 33 | #include "application.hpp" |
| 34 | |
| 35 | namespace ff::ui_utils { |
| 36 | |
| 37 | static Cairo::TextExtents ui_font_extents(const std::string& sample_text) { |
| 38 | Pango::FontDescription font = |
| 39 | ff::app::ColorManager::instance().style_context()->get_font(); |
| 40 | |
| 41 | // Create the toy font face explicitly once and hold it in a static so its |
| 42 | // reference is released cleanly at program exit, preventing the |
| 43 | // FcPatternDuplicate leak that occurs inside cairo_toy_font_face_create(). |
| 44 | // The destructor calls unreference() to balance the implicit reference. |
| 45 | struct ToyFontFace { |
| 46 | Cairo::RefPtr<Cairo::ToyFontFace> face; |
| 47 | explicit ToyFontFace(const std::string& family) |
| 48 | : face(Cairo::ToyFontFace::create( |
| 49 | family, Cairo::FontSlant::FONT_SLANT_NORMAL, |
| 50 | Cairo::FontWeight::FONT_WEIGHT_NORMAL)) {} |
| 51 | ~ToyFontFace() { face->unreference(); } |
| 52 | }; |
| 53 | static ToyFontFace toy_face(font.get_family()); |
| 54 | |
| 55 | Cairo::RefPtr<Cairo::ImageSurface> srf = |
| 56 | Cairo::ImageSurface::create(Cairo::Format::FORMAT_RGB24, 100, 100); |
| 57 | Cairo::RefPtr<Cairo::Context> cairo_context = Cairo::Context::create(srf); |
| 58 | |
| 59 | cairo_context->set_font_face(toy_face.face); |
| 60 | cairo_context->set_font_size(font.get_size() / PANGO_SCALE * |
| 61 | Gdk::Screen::get_default()->get_resolution() / |
| 62 | 72); |
| 63 | |
| 64 | Cairo::TextExtents extents; |
| 65 | cairo_context->get_text_extents(sample_text, extents); |
| 66 | return extents; |
| 67 | } |
| 68 | |
| 69 | double ui_font_em_size() { |
| 70 | Cairo::TextExtents extents = ui_font_extents("m"); |
| 71 | return extents.x_advance; |
| 72 | } |
| 73 | |
| 74 | double ui_font_eX_size() { |
| 75 | Cairo::TextExtents extents = ui_font_extents("X"); |
| 76 | return extents.height; |
| 77 | } |
| 78 | |
| 79 | double get_current_ppi(Gtk::Widget* w) { |
| 80 | Glib::RefPtr<const Gdk::Display> display = w->get_display(); |
| 81 | Glib::RefPtr<Gdk::Window> window = w->get_window(); |
| 82 | Glib::RefPtr<const Gdk::Monitor> monitor = |
| 83 | display->get_monitor_at_window(window); |
| 84 | |
| 85 | Gdk::Rectangle monitor_geom; |
| 86 | monitor->get_geometry(monitor_geom); |
| 87 | int monitor_width_mm = monitor->get_width_mm(); |
| 88 | int monitor_width_px = monitor_geom.get_width(); |
| 89 | |
| 90 | double ppi = 25.4 * monitor_width_px / monitor_width_mm; |
| 91 | return ppi; |
| 92 | } |
| 93 | |
| 94 | void post_error(const char* title, const char* statement, ...) { |
| 95 | va_list ap; |
| 96 | va_start(ap, statement); |
| 97 | |
| 98 | // Format error statement |
| 99 | gchar* result_string = NULL; |
| 100 | gint chars_written = g_vasprintf(&result_string, statement, ap); |
| 101 | if (chars_written >= 0 && result_string != NULL) { |
| 102 | // Passing use_markup=false causes GTK to embolden the text, and we |
| 103 | // don't want it. |
| 104 | Gtk::MessageDialog message_dlg(result_string, true, Gtk::MESSAGE_ERROR, |
| 105 | Gtk::BUTTONS_OK, true); |
| 106 | message_dlg.set_title(title); |
| 107 | message_dlg.run(); |
| 108 | g_free(result_string); |
| 109 | } else { |
| 110 | std::cerr << "Error formatting statement \"" << statement << "\"" |
| 111 | << std::endl; |
| 112 | } |
| 113 | |
| 114 | va_end(ap); |
| 115 | } |
| 116 | |
| 117 | Glib::RefPtr<Gdk::Cursor> set_cursor(Gtk::Widget* widget, |
| 118 | const Glib::ustring& name) { |
| 119 | if (widget == nullptr) return {}; |
| 120 | |
| 121 | Glib::RefPtr<Gdk::Window> gdk_window = widget->get_window(); |
| 122 | if (!gdk_window) return {}; |
| 123 | |
| 124 | auto old_cursor = gdk_window->get_cursor(); |
| 125 | |
| 126 | Glib::RefPtr<Gdk::Cursor> new_cursor = |
| 127 | Gdk::Cursor::create(gdk_window->get_display(), name); |
| 128 | gdk_window->set_cursor(new_cursor); |
| 129 | |
| 130 | return old_cursor; |
| 131 | } |
| 132 | |
| 133 | void unset_cursor(Gtk::Widget* widget, Glib::RefPtr<Gdk::Cursor> old_cursor) { |
| 134 | if (widget == nullptr) return; |
| 135 | |
| 136 | Glib::RefPtr<Gdk::Window> gdk_window = widget->get_window(); |
| 137 | if (!gdk_window) return; |
| 138 | |
| 139 | // old_cursor is allowed to be NULL |
| 140 | gdk_window->set_cursor(old_cursor); |
| 141 | } |
| 142 | |
| 143 | } // namespace ff::ui_utils |