| 1 | /**************************************************************************/ |
| 2 | /* link_button.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "link_button.h" |
| 32 | |
| 33 | #include "scene/theme/theme_db.h" |
| 34 | |
| 35 | void LinkButton::_shape() { |
| 36 | Ref<Font> font = theme_cache.font; |
| 37 | int font_size = theme_cache.font_size; |
| 38 | |
| 39 | text_buf->clear(); |
| 40 | text_buf->set_width(-1); |
| 41 | if (text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 42 | text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); |
| 43 | } else { |
| 44 | text_buf->set_direction((TextServer::Direction)text_direction); |
| 45 | } |
| 46 | TS->shaped_text_set_bidi_override(text_buf->get_rid(), structured_text_parser(st_parser, st_args, xl_text)); |
| 47 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 48 | text_buf->add_string(xl_text, font, font_size, lang); |
| 49 | text_buf->set_text_overrun_behavior(overrun_behavior); |
| 50 | text_buf->set_ellipsis_char(el_char); |
| 51 | |
| 52 | queue_accessibility_update(); |
| 53 | } |
| 54 | |
| 55 | void LinkButton::set_text(const String &p_text) { |
| 56 | if (text == p_text) { |
| 57 | return; |
| 58 | } |
| 59 | text = p_text; |
| 60 | xl_text = atr(text); |
| 61 | _shape(); |
| 62 | update_minimum_size(); |
| 63 | queue_redraw(); |
| 64 | } |
| 65 | |
| 66 | String LinkButton::get_text() const { |
| 67 | return text; |
| 68 | } |
| 69 | |
| 70 | void LinkButton::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) { |
| 71 | if (overrun_behavior != p_behavior) { |
| 72 | overrun_behavior = p_behavior; |
| 73 | _shape(); |
| 74 | update_minimum_size(); |
| 75 | queue_redraw(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | TextServer::OverrunBehavior LinkButton::get_text_overrun_behavior() const { |
| 80 | return overrun_behavior; |
| 81 | } |
| 82 | |
| 83 | void LinkButton::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) { |
| 84 | if (st_parser != p_parser) { |
| 85 | st_parser = p_parser; |
| 86 | _shape(); |
| 87 | queue_redraw(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void LinkButton::set_ellipsis_char(const String &p_char) { |
| 92 | String c = p_char; |
| 93 | if (c.length() > 1) { |
| 94 | WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given)."); |
| 95 | c = c.left(1); |
| 96 | } |
| 97 | |
| 98 | if (el_char == c) { |
| 99 | return; |
| 100 | } |
| 101 | el_char = c; |
| 102 | |
| 103 | if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) { |
| 104 | _shape(); |
| 105 | queue_redraw(); |
| 106 | update_minimum_size(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | String LinkButton::get_ellipsis_char() const { |
| 111 | return el_char; |
| 112 | } |
| 113 | |
| 114 | TextServer::StructuredTextParser LinkButton::get_structured_text_bidi_override() const { |
| 115 | return st_parser; |
| 116 | } |
| 117 | |
| 118 | void LinkButton::set_structured_text_bidi_override_options(const Array &p_args) { |
| 119 | st_args = Array(p_args); |
| 120 | _shape(); |
| 121 | queue_redraw(); |
| 122 | } |
| 123 | |
| 124 | Array LinkButton::get_structured_text_bidi_override_options() const { |
| 125 | return Array(st_args); |
| 126 | } |
| 127 | |
| 128 | void LinkButton::set_text_direction(Control::TextDirection p_text_direction) { |
| 129 | ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); |
| 130 | if (text_direction != p_text_direction) { |
| 131 | text_direction = p_text_direction; |
| 132 | _shape(); |
| 133 | queue_redraw(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | Control::TextDirection LinkButton::get_text_direction() const { |
| 138 | return text_direction; |
| 139 | } |
| 140 | |
| 141 | void LinkButton::set_language(const String &p_language) { |
| 142 | if (language != p_language) { |
| 143 | language = p_language; |
| 144 | _shape(); |
| 145 | queue_redraw(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | String LinkButton::get_language() const { |
| 150 | return language; |
| 151 | } |
| 152 | |
| 153 | void LinkButton::set_uri(const String &p_uri) { |
| 154 | if (uri != p_uri) { |
| 155 | uri = p_uri; |
| 156 | queue_accessibility_update(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | String LinkButton::get_uri() const { |
| 161 | return uri; |
| 162 | } |
| 163 | |
| 164 | void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) { |
| 165 | if (underline_mode == p_underline_mode) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | underline_mode = p_underline_mode; |
| 170 | queue_redraw(); |
| 171 | } |
| 172 | |
| 173 | LinkButton::UnderlineMode LinkButton::get_underline_mode() const { |
| 174 | return underline_mode; |
| 175 | } |
| 176 | |
| 177 | Ref<Font> LinkButton::get_button_font() const { |
| 178 | return theme_cache.font; |
| 179 | } |
| 180 | |
| 181 | int LinkButton::get_button_font_size() const { |
| 182 | return theme_cache.font_size; |
| 183 | } |
| 184 | |
| 185 | void LinkButton::pressed() { |
| 186 | if (uri.is_empty()) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | OS::get_singleton()->shell_open(uri); |
| 191 | } |
| 192 | |
| 193 | Size2 LinkButton::get_minimum_size() const { |
| 194 | Size2 minsize = text_buf->get_size(); |
| 195 | if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) { |
| 196 | minsize.width = 0; |
| 197 | } |
| 198 | |
| 199 | return minsize; |
| 200 | } |
| 201 | |
| 202 | Control::CursorShape LinkButton::get_cursor_shape(const Point2 &p_pos) const { |
| 203 | return is_disabled() ? CURSOR_ARROW : get_default_cursor_shape(); |
| 204 | } |
| 205 | |
| 206 | void LinkButton::_notification(int p_what) { |
| 207 | switch (p_what) { |
| 208 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 209 | RID ae = get_accessibility_element(); |
| 210 | ERR_FAIL_COND(ae.is_null()); |
| 211 | |
| 212 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LINK); |
| 213 | const String &ac_name = get_accessibility_name(); |
| 214 | if (!xl_text.is_empty() && ac_name.is_empty()) { |
| 215 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, xl_text); |
| 216 | } else if (!xl_text.is_empty() && !ac_name.is_empty() && ac_name != xl_text) { |
| 217 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, ac_name + ": " + xl_text); |
| 218 | } else if (xl_text.is_empty() && ac_name.is_empty() && !get_tooltip_text().is_empty()) { |
| 219 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_tooltip_text()); // Fall back to tooltip. |
| 220 | } |
| 221 | DisplayServer::get_singleton()->accessibility_update_set_url(ae, uri); |
| 222 | } break; |
| 223 | |
| 224 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 225 | xl_text = atr(text); |
| 226 | _shape(); |
| 227 | update_minimum_size(); |
| 228 | queue_redraw(); |
| 229 | } break; |
| 230 | |
| 231 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { |
| 232 | queue_redraw(); |
| 233 | } break; |
| 234 | |
| 235 | case NOTIFICATION_RESIZED: |
| 236 | case NOTIFICATION_THEME_CHANGED: { |
| 237 | _shape(); |
| 238 | update_minimum_size(); |
| 239 | queue_redraw(); |
| 240 | } break; |
| 241 | |
| 242 | case NOTIFICATION_DRAW: { |
| 243 | RID ci = get_canvas_item(); |
| 244 | Size2 size = get_size(); |
| 245 | Color color; |
| 246 | bool do_underline = false; |
| 247 | |
| 248 | switch (get_draw_mode()) { |
| 249 | case DRAW_NORMAL: { |
| 250 | if (has_focus(true)) { |
| 251 | color = theme_cache.font_focus_color; |
| 252 | } else { |
| 253 | color = theme_cache.font_color; |
| 254 | } |
| 255 | |
| 256 | do_underline = underline_mode == UNDERLINE_MODE_ALWAYS; |
| 257 | } break; |
| 258 | case DRAW_HOVER_PRESSED: |
| 259 | case DRAW_PRESSED: { |
| 260 | if (has_theme_color(SNAME("font_pressed_color"))) { |
| 261 | color = theme_cache.font_pressed_color; |
| 262 | } else { |
| 263 | color = theme_cache.font_color; |
| 264 | } |
| 265 | |
| 266 | do_underline = underline_mode != UNDERLINE_MODE_NEVER; |
| 267 | |
| 268 | } break; |
| 269 | case DRAW_HOVER: { |
| 270 | color = theme_cache.font_hover_color; |
| 271 | do_underline = underline_mode != UNDERLINE_MODE_NEVER; |
| 272 | |
| 273 | } break; |
| 274 | case DRAW_DISABLED: { |
| 275 | color = theme_cache.font_disabled_color; |
| 276 | do_underline = underline_mode == UNDERLINE_MODE_ALWAYS; |
| 277 | |
| 278 | } break; |
| 279 | } |
| 280 | |
| 281 | if (has_focus(true)) { |
| 282 | Ref<StyleBox> style = theme_cache.focus; |
| 283 | style->draw(ci, Rect2(Point2(), size)); |
| 284 | } |
| 285 | |
| 286 | if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) { |
| 287 | text_buf->set_width(MAX(1.0f, size.width)); |
| 288 | } |
| 289 | int width = text_buf->get_line_width(); |
| 290 | |
| 291 | Color font_outline_color = theme_cache.font_outline_color; |
| 292 | int outline_size = theme_cache.outline_size; |
| 293 | if (is_layout_rtl()) { |
| 294 | if (outline_size > 0 && font_outline_color.a > 0) { |
| 295 | text_buf->draw_outline(get_canvas_item(), Vector2(size.width - width, 0), outline_size, font_outline_color); |
| 296 | } |
| 297 | text_buf->draw(get_canvas_item(), Vector2(size.width - width, 0), color); |
| 298 | } else { |
| 299 | if (outline_size > 0 && font_outline_color.a > 0) { |
| 300 | text_buf->draw_outline(get_canvas_item(), Vector2(0, 0), outline_size, font_outline_color); |
| 301 | } |
| 302 | text_buf->draw(get_canvas_item(), Vector2(0, 0), color); |
| 303 | } |
| 304 | |
| 305 | if (do_underline) { |
| 306 | int underline_spacing = theme_cache.underline_spacing + text_buf->get_line_underline_position(); |
| 307 | int y = text_buf->get_line_ascent() + underline_spacing; |
| 308 | int underline_thickness = MAX(1, text_buf->get_line_underline_thickness()); |
| 309 | |
| 310 | if (is_layout_rtl()) { |
| 311 | draw_line(Vector2(size.width - width, y), Vector2(size.width, y), color, underline_thickness); |
| 312 | } else { |
| 313 | draw_line(Vector2(0, y), Vector2(width, y), color, underline_thickness); |
| 314 | } |
| 315 | } |
| 316 | } break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void LinkButton::_bind_methods() { |
| 321 | ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text); |
| 322 | ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text); |
| 323 | ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &LinkButton::set_text_overrun_behavior); |
| 324 | ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &LinkButton::get_text_overrun_behavior); |
| 325 | ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &LinkButton::set_ellipsis_char); |
| 326 | ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &LinkButton::get_ellipsis_char); |
| 327 | ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LinkButton::set_text_direction); |
| 328 | ClassDB::bind_method(D_METHOD("get_text_direction"), &LinkButton::get_text_direction); |
| 329 | ClassDB::bind_method(D_METHOD("set_language", "language"), &LinkButton::set_language); |
| 330 | ClassDB::bind_method(D_METHOD("get_language"), &LinkButton::get_language); |
| 331 | ClassDB::bind_method(D_METHOD("set_uri", "uri"), &LinkButton::set_uri); |
| 332 | ClassDB::bind_method(D_METHOD("get_uri"), &LinkButton::get_uri); |
| 333 | ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode); |
| 334 | ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode); |
| 335 | ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LinkButton::set_structured_text_bidi_override); |
| 336 | ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LinkButton::get_structured_text_bidi_override); |
| 337 | ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LinkButton::set_structured_text_bidi_override_options); |
| 338 | ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LinkButton::get_structured_text_bidi_override_options); |
| 339 | |
| 340 | BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS); |
| 341 | BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER); |
| 342 | BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER); |
| 343 | |
| 344 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text"); |
| 345 | ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode"); |
| 346 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri"), "set_uri", "get_uri"); |
| 347 | |
| 348 | ADD_GROUP("Text Behavior", ""); |
| 349 | ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior"); |
| 350 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char"); |
| 351 | |
| 352 | ADD_GROUP("BiDi", ""); |
| 353 | ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction"); |
| 354 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language"); |
| 355 | ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override"); |
| 356 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options"); |
| 357 | |
| 358 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LinkButton, focus); |
| 359 | |
| 360 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_color); |
| 361 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_focus_color); |
| 362 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_pressed_color); |
| 363 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_color); |
| 364 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_pressed_color); |
| 365 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_disabled_color); |
| 366 | |
| 367 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LinkButton, font); |
| 368 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LinkButton, font_size); |
| 369 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, outline_size); |
| 370 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_outline_color); |
| 371 | |
| 372 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, underline_spacing); |
| 373 | } |
| 374 | |
| 375 | LinkButton::LinkButton(const String &p_text) { |
| 376 | text_buf.instantiate(); |
| 377 | set_focus_mode(FOCUS_ACCESSIBILITY); |
| 378 | set_default_cursor_shape(CURSOR_POINTING_HAND); |
| 379 | |
| 380 | set_text(p_text); |
| 381 | } |