code_edit.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*************************************************************************/
  2. /* code_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "code_edit.h"
  31. void CodeEdit::_notification(int p_what) {
  32. switch (p_what) {
  33. case NOTIFICATION_THEME_CHANGED:
  34. case NOTIFICATION_ENTER_TREE: {
  35. set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0').width);
  36. line_number_color = get_theme_color("line_number_color");
  37. } break;
  38. case NOTIFICATION_DRAW: {
  39. } break;
  40. }
  41. }
  42. /* Line numbers */
  43. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  44. set_gutter_draw(line_number_gutter, p_draw);
  45. }
  46. bool CodeEdit::is_draw_line_numbers_enabled() const {
  47. return is_gutter_drawn(line_number_gutter);
  48. }
  49. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  50. p_zero_padded ? line_number_padding = "0" : line_number_padding = " ";
  51. update();
  52. }
  53. bool CodeEdit::is_line_numbers_zero_padded() const {
  54. return line_number_padding == "0";
  55. }
  56. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  57. String fc = String::num(p_line + 1).lpad(line_number_digits, line_number_padding);
  58. int yofs = region.position.y + (cache.row_height - cache.font->get_height()) / 2;
  59. cache.font->draw(get_canvas_item(), Point2(region.position.x, yofs + cache.font->get_ascent()), fc, line_number_color);
  60. }
  61. void CodeEdit::_bind_methods() {
  62. /* Line numbers */
  63. ClassDB::bind_method(D_METHOD("_line_number_draw_callback"), &CodeEdit::_line_number_draw_callback);
  64. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  65. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  66. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  67. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  68. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  69. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  70. }
  71. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  72. if (p_gutter == line_number_gutter) {
  73. cursor_set_line(p_line);
  74. return;
  75. }
  76. }
  77. void CodeEdit::_line_edited_from(int p_line) {
  78. int line_count = get_line_count();
  79. if (line_count != cached_line_count) {
  80. int lc = line_count;
  81. line_number_digits = 1;
  82. while (lc /= 10) {
  83. line_number_digits++;
  84. }
  85. set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0').width);
  86. cached_line_count = line_count;
  87. }
  88. }
  89. void CodeEdit::_update_gutter_indexes() {
  90. for (int i = 0; i < get_gutter_count(); i++) {
  91. if (get_gutter_name(i) == "line_numbers") {
  92. line_number_gutter = i;
  93. continue;
  94. }
  95. }
  96. }
  97. CodeEdit::CodeEdit() {
  98. /* Line numbers */
  99. add_gutter();
  100. set_gutter_name(0, "line_numbers");
  101. set_gutter_draw(0, false);
  102. set_gutter_type(0, GUTTER_TPYE_CUSTOM);
  103. set_gutter_custom_draw(0, this, "_line_number_draw_callback");
  104. connect("line_edited_from", callable_mp(this, &CodeEdit::_line_edited_from));
  105. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  106. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  107. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  108. _update_gutter_indexes();
  109. }
  110. CodeEdit::~CodeEdit() {
  111. }