| 1 | /* Copyright (C) 2025 by Maxim Iorsh |
| 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 "css_builder.hpp" |
| 29 | |
| 30 | #include <cstring> |
| 31 | #include <functional> |
| 32 | #include <iostream> |
| 33 | #include <map> |
| 34 | #include <vector> |
| 35 | |
| 36 | typedef struct gdisplay GDisplay; |
| 37 | |
| 38 | extern Color histogram_graphcol; |
| 39 | extern "C" Color GDrawGetDefaultForeground(GDisplay*); |
| 40 | extern "C" Color GDrawGetDefaultBackground(GDisplay*); |
| 41 | extern "C" Color GDrawGetWarningForeground(GDisplay*); |
| 42 | |
| 43 | namespace ff::app { |
| 44 | |
| 45 | using CssPropertyEvalCB = |
| 46 | std::function<std::string(const GBox& box_resource, bool enabled)>; |
| 47 | |
| 48 | static std::string css_color(Color col, bool enabled = true) { |
| 49 | // There is no convenient hex formatting in C++17... |
| 50 | char value_string[250] = "\0"; |
| 51 | |
| 52 | // Build semitransparent color expression for disabled elements |
| 53 | const char* format = enabled ? "#%06x" : "alpha(#%06x, 0.5)"; |
| 54 | |
| 55 | sprintf(value_string, format, col); |
| 56 | return value_string; |
| 57 | } |
| 58 | |
| 59 | template <auto GBox::* PROP> |
| 60 | std::string color_property(const GBox& box_resource, bool enabled) { |
| 61 | return css_color(box_resource.*PROP, enabled); |
| 62 | } |
| 63 | |
| 64 | static std::string box_shadow_value(const GBox& box_resource, bool enabled) { |
| 65 | std::string inner_shadow = |
| 66 | (box_resource.flags & box_foreground_border_inner) |
| 67 | ? "inset 0px 0px 0px 1px " + |
| 68 | css_color(box_resource.border_inner, enabled) |
| 69 | : ""; |
| 70 | std::string outer_shadow = |
| 71 | (box_resource.flags & box_foreground_border_outer) |
| 72 | ? "0px 0px 0px 1px " + css_color(box_resource.border_outer, enabled) |
| 73 | : ""; |
| 74 | |
| 75 | if (inner_shadow.empty()) { |
| 76 | return outer_shadow.empty() ? "none" : outer_shadow; |
| 77 | } else { |
| 78 | return outer_shadow.empty() ? inner_shadow |
| 79 | : inner_shadow + "," + outer_shadow; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static std::string gradient_value(const GBox& box_resource, bool enabled) { |
| 84 | if (!(box_resource.flags & box_gradient_bg)) { |
| 85 | return "none"; |
| 86 | } |
| 87 | |
| 88 | Color gradient_bg_start = enabled ? box_resource.main_background |
| 89 | : box_resource.disabled_background; |
| 90 | return "linear-gradient(to bottom, " + |
| 91 | css_color(gradient_bg_start, enabled) + ", " + |
| 92 | css_color(box_resource.gradient_bg_end, enabled) + ")"; |
| 93 | } |
| 94 | |
| 95 | static std::string border_width(const GBox& box_resource, bool enabled) { |
| 96 | if (box_resource.border_type != bt_none) { |
| 97 | // GDraw converts points to pixels with integer rounding. It usually |
| 98 | // doesn't matter, except for 1pt, which we shall round to 1px for |
| 99 | // sharpness. |
| 100 | std::string unit = (box_resource.border_width == 1) ? "px" : "pt"; |
| 101 | return std::to_string(box_resource.border_width) + unit; |
| 102 | } else { |
| 103 | return "0"; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static std::string padding(const GBox& box_resource, bool enabled) { |
| 108 | // The "padding" property mimics the specifics of GDraw border width |
| 109 | // application |
| 110 | return std::to_string(box_resource.padding + |
| 111 | box_resource.border_width / 2) + |
| 112 | "pt"; |
| 113 | } |
| 114 | |
| 115 | static std::string margin(const GBox& box_resource, bool enabled) { |
| 116 | // From GBoxExtraSpace() |
| 117 | return "2pt"; |
| 118 | } |
| 119 | |
| 120 | static std::string border_style(const GBox& box_resource, bool enabled) { |
| 121 | static const std::map<enum border_type, std::string> border_type_map = { |
| 122 | // CSS property values "inset" and "outset" modify colors, so we don't |
| 123 | // use them. |
| 124 | {bt_none, "none"}, {bt_box, "solid"}, {bt_raised, "solid"}, |
| 125 | {bt_lowered, "solid"}, {bt_engraved, "groove"}, {bt_embossed, "ridge"}, |
| 126 | {bt_double, "double"}}; |
| 127 | return border_type_map.at( |
| 128 | static_cast<enum border_type>(box_resource.border_type)); |
| 129 | } |
| 130 | |
| 131 | static std::string border_radius(const GBox& box_resource, bool enabled) { |
| 132 | if (box_resource.border_shape == bs_roundrect) { |
| 133 | if (box_resource.rr_radius < 1) { |
| 134 | // Set the radius to arbitrarily huge value, the CSS renderer will |
| 135 | // decrease it to half-height as necessary |
| 136 | return "1000pt"; |
| 137 | } else { |
| 138 | return std::to_string(box_resource.rr_radius) + "pt"; |
| 139 | } |
| 140 | } else if (box_resource.border_shape == bs_elipse) { |
| 141 | return "50%"; |
| 142 | } |
| 143 | |
| 144 | return "0"; |
| 145 | } |
| 146 | |
| 147 | std::map<std::string, std::string> evaluate_css_properties( |
| 148 | const std::vector<std::pair<std::string, CssPropertyEvalCB>>& property_map, |
| 149 | const GBox& box_resource, bool enabled) { |
| 150 | std::map<std::string, std::string> collection; |
| 151 | |
| 152 | for (const auto& [css_property_name, eval] : property_map) { |
| 153 | std::string css_value = eval(box_resource, enabled); |
| 154 | if (!css_value.empty()) { |
| 155 | collection[css_property_name] = css_value; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return collection; |
| 160 | } |
| 161 | |
| 162 | std::map<std::string, std::string> collect_css_properties( |
| 163 | const GBox& box_resource, const GResFont* font) { |
| 164 | static const std::vector<std::pair<std::string, CssPropertyEvalCB>> |
| 165 | css_property_map = { |
| 166 | {"color", color_property<&GBox::main_foreground>}, |
| 167 | {"background-color", color_property<&GBox::main_background>}, |
| 168 | {"border-left-color", color_property<&GBox::border_brightest>}, |
| 169 | {"border-top-color", color_property<&GBox::border_brighter>}, |
| 170 | {"border-bottom-color", color_property<&GBox::border_darker>}, |
| 171 | {"border-right-color", color_property<&GBox::border_darkest>}, |
| 172 | {"box-shadow", box_shadow_value}, |
| 173 | {"background-image", gradient_value}, |
| 174 | {"border-width", border_width}, |
| 175 | {"padding", padding}, |
| 176 | {"margin", margin}, |
| 177 | {"border-style", border_style}, |
| 178 | {"border-radius", border_radius}, |
| 179 | }; |
| 180 | |
| 181 | std::map<std::string, std::string> collection = |
| 182 | evaluate_css_properties(css_property_map, box_resource, true); |
| 183 | |
| 184 | if (font && font->fi) { |
| 185 | collection["font-family"] = font->fi->rq.utf8_family_name; |
| 186 | collection["font-size"] = |
| 187 | std::to_string(font->fi->rq.point_size) + "pt"; |
| 188 | } |
| 189 | |
| 190 | return collection; |
| 191 | } |
| 192 | |
| 193 | std::map<std::string, std::string> collect_css_properties_disabled( |
| 194 | const GBox& box_resource) { |
| 195 | static const std::vector<std::pair<std::string, CssPropertyEvalCB>> |
| 196 | css_property_map = { |
| 197 | {"color", color_property<&GBox::disabled_foreground>}, |
| 198 | {"background-color", color_property<&GBox::disabled_background>}, |
| 199 | {"border-left-color", color_property<&GBox::border_brightest>}, |
| 200 | {"border-top-color", color_property<&GBox::border_brighter>}, |
| 201 | {"border-bottom-color", color_property<&GBox::border_darker>}, |
| 202 | {"border-right-color", color_property<&GBox::border_darkest>}, |
| 203 | {"box-shadow", box_shadow_value}, |
| 204 | {"background-image", gradient_value}, |
| 205 | }; |
| 206 | |
| 207 | return evaluate_css_properties(css_property_map, box_resource, false); |
| 208 | } |
| 209 | |
| 210 | std::map<std::string, std::string> collect_css_properties_selected( |
| 211 | const GBox& box_resource) { |
| 212 | static const std::vector<std::pair<std::string, CssPropertyEvalCB>> |
| 213 | css_property_map = { |
| 214 | {"color", color_property<&GBox::main_foreground>}, |
| 215 | {"background-color", color_property<&GBox::active_border>}, |
| 216 | }; |
| 217 | |
| 218 | return evaluate_css_properties(css_property_map, box_resource, true); |
| 219 | } |
| 220 | |
| 221 | std::map<std::string, std::string> collect_css_properties_main( |
| 222 | const struct resed* ri_extras) { |
| 223 | std::map<std::string, std::string> collection; |
| 224 | |
| 225 | for (const struct resed* rec = ri_extras; rec && rec->resname; ++rec) { |
| 226 | if (std::strcmp(rec->resname, "Foreground") == 0) { |
| 227 | collection["color"] = css_color(*(Color*)(rec->val)); |
| 228 | } |
| 229 | if (std::strcmp(rec->resname, "Background") == 0) { |
| 230 | collection["background-color"] = css_color(*(Color*)(rec->val)); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return collection; |
| 235 | } |
| 236 | |
| 237 | std::map<std::string, std::string> collect_css_properties_color( |
| 238 | const GBox& box_resource) { |
| 239 | static const std::vector<std::pair<std::string, CssPropertyEvalCB>> |
| 240 | css_property_map = { |
| 241 | {"color", color_property<&GBox::main_foreground>}, |
| 242 | {"background-color", color_property<&GBox::main_background>}, |
| 243 | }; |
| 244 | |
| 245 | return evaluate_css_properties(css_property_map, box_resource, true); |
| 246 | } |
| 247 | |
| 248 | std::string build_style(const std::string& selector, |
| 249 | const std::map<std::string, std::string>& properties) { |
| 250 | std::string property_list; |
| 251 | for (auto& [property, value] : properties) { |
| 252 | property_list += " " + property + ": " + value + ";\n"; |
| 253 | } |
| 254 | |
| 255 | return selector + " {\n" + property_list + "}\n"; |
| 256 | } |
| 257 | |
| 258 | std::string build_unset_style( |
| 259 | const std::string& selector, |
| 260 | const std::map<std::string, std::string>& properties) { |
| 261 | std::string property_list; |
| 262 | for (auto& [property, value] : properties) { |
| 263 | property_list += " " + property + ": unset;\n"; |
| 264 | } |
| 265 | |
| 266 | return selector + " {\n" + property_list + "}\n"; |
| 267 | } |
| 268 | |
| 269 | // Some GTK widgets have substantially different structure from their GDraw |
| 270 | // analogs. For them we collect only color properties. |
| 271 | std::string build_color_only_styles(const GResInfo* gdraw_ri) { |
| 272 | static const std::vector< |
| 273 | std::tuple<std::string /*GDraw resource*/, std::string /*CSS class*/, |
| 274 | std::string /*CSS pseudoclass*/>> |
| 275 | css_selector_map_color = { |
| 276 | {"GList", "treeview", "selected"}, |
| 277 | {"GList", "combobox menuitem", "hover"}, |
| 278 | {"GTabSet", "header", "checked"}, |
| 279 | {"GTabSet", "tab", "checked"}, |
| 280 | }; |
| 281 | |
| 282 | std::string styles; |
| 283 | for (const auto& [gres, css_class, css_pseudoclass] : |
| 284 | css_selector_map_color) { |
| 285 | const GResInfo* ri = gdraw_ri; |
| 286 | for (; ri != NULL && ri->resname != gres; ri = ri->next); |
| 287 | if (ri == NULL || ri->boxdata == NULL) { |
| 288 | std::cerr << "GDraw resource \"" << gres << "\" not found" |
| 289 | << std::endl; |
| 290 | continue; |
| 291 | } |
| 292 | auto props_color = collect_css_properties_color(*(ri->boxdata)); |
| 293 | styles += build_style(css_class, props_color); |
| 294 | |
| 295 | auto props_disabled = collect_css_properties_disabled(*(ri->boxdata)); |
| 296 | styles += build_style(css_class + ":disabled", props_disabled); |
| 297 | |
| 298 | auto props_selected = collect_css_properties_selected(*(ri->boxdata)); |
| 299 | styles += |
| 300 | build_style(css_class + ":" + css_pseudoclass, props_selected); |
| 301 | } |
| 302 | return styles; |
| 303 | } |
| 304 | |
| 305 | std::string build_styles(const GResInfo* gdraw_ri) { |
| 306 | struct Selector { |
| 307 | std::string node_name; |
| 308 | |
| 309 | // Exclude this selector when appears in the following containers, e.g. |
| 310 | // "label" should not be modified when appears inside "button". |
| 311 | std::vector<std::string> excluded_containers; |
| 312 | }; |
| 313 | |
| 314 | static const std::map<std::string, Selector> css_selector_map = { |
| 315 | {"", {"box", {"tooltip"}}}, |
| 316 | {"GLabel", {"label", {"button", "tooltip", "tab"}}}, |
| 317 | {"GButton", {"button", {"spinbutton"}}}, |
| 318 | {"GDefaultButton", {"button#ok", {}}}, |
| 319 | {"GCancelButton", {"button#cancel", {}}}, |
| 320 | {"GNumericField", {"spinbutton", {}}}, |
| 321 | {"GNumericFieldSpinner", {"spinbutton button", {}}}, |
| 322 | {"GTextField", {"entry", {"spinbutton"}}}, |
| 323 | {"GGadget.Popup", {"tooltip", {}}}, |
| 324 | {"GScrollBar", {"scrollbar", {}}}, |
| 325 | }; |
| 326 | |
| 327 | std::string styles; |
| 328 | |
| 329 | for (const GResInfo* ri = gdraw_ri; ri->next != NULL; ri = ri->next) { |
| 330 | auto sel_it = css_selector_map.find(ri->resname); |
| 331 | if (sel_it == css_selector_map.end()) { |
| 332 | continue; |
| 333 | } |
| 334 | const Selector& selector = sel_it->second; |
| 335 | |
| 336 | if ((std::strcmp(ri->resname, "") == 0 || |
| 337 | std::strcmp(ri->resname, "GGadget.Popup") == 0) && |
| 338 | ri->extras) { |
| 339 | // Collect some default properties from the base class. |
| 340 | auto props_main = collect_css_properties_main(ri->extras); |
| 341 | styles += build_style(selector.node_name, props_main); |
| 342 | |
| 343 | // When the node is inside predefined containers, we shall unset the |
| 344 | // affected properties |
| 345 | for (const std::string& container : selector.excluded_containers) { |
| 346 | styles += build_unset_style( |
| 347 | container + " " + selector.node_name, props_main); |
| 348 | } |
| 349 | |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | if (!ri->boxdata) { |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | auto props = collect_css_properties(*(ri->boxdata), ri->font); |
| 358 | styles += build_style(selector.node_name, props); |
| 359 | |
| 360 | auto props_disabled = collect_css_properties_disabled(*(ri->boxdata)); |
| 361 | styles += build_style(selector.node_name + ":disabled", props_disabled); |
| 362 | |
| 363 | // When the node is inside predefined containers, we shall unset the |
| 364 | // affected properties |
| 365 | for (const std::string& container : selector.excluded_containers) { |
| 366 | styles += |
| 367 | build_unset_style(container + " " + selector.node_name, props); |
| 368 | styles += build_unset_style( |
| 369 | container + " " + selector.node_name + ":disabled", |
| 370 | props_disabled); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | styles += build_color_only_styles(gdraw_ri); |
| 375 | styles += "tab { margin-bottom: 1px; }\n"; |
| 376 | |
| 377 | styles += "@define-color ff_histogram_bg " + |
| 378 | css_color(GDrawGetDefaultBackground(nullptr)) + ";\n"; |
| 379 | styles += "@define-color ff_histogram_axis " + |
| 380 | css_color(GDrawGetDefaultForeground(nullptr)) + ";\n"; |
| 381 | styles += "@define-color ff_histogram_bars " + |
| 382 | css_color(histogram_graphcol) + ";\n"; |
| 383 | styles += "@define-color ff_histogram_moving_average " + |
| 384 | css_color(GDrawGetWarningForeground(nullptr)) + ";\n"; |
| 385 | |
| 386 | return styles; |
| 387 | } |
| 388 | |
| 389 | } // namespace ff::app |