TextBox.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GUI_COMPONENT_TEXTBOX
  24. #define DFPSR_GUI_COMPONENT_TEXTBOX
  25. #include "../VisualComponent.h"
  26. #include "helpers/ScrollBarImpl.h"
  27. #include "../../../api/fontAPI.h"
  28. #include "../../../math/LVector.h"
  29. namespace dsr {
  30. struct LineIndex {
  31. // Exclusive interval of characters in the line.
  32. int64_t lineStartIndex = 0;
  33. int64_t lineEndIndex = 0;
  34. LineIndex(int64_t lineStartIndex, int64_t lineEndIndex)
  35. : lineStartIndex(lineStartIndex), lineEndIndex(lineEndIndex) {}
  36. };
  37. struct BeamLocation {
  38. int64_t rowIndex; // Index of the current row.
  39. int64_t characterIndex; // Index of the character in the entire text.
  40. BeamLocation(int64_t rowIndex, int64_t characterIndex)
  41. : rowIndex(rowIndex), characterIndex(characterIndex) {}
  42. };
  43. class TextBox : public VisualComponent {
  44. PERSISTENT_DECLARATION(TextBox)
  45. public:
  46. // Value allocated sub-components
  47. ScrollBarImpl verticalScrollBar = ScrollBarImpl(true, true);
  48. ScrollBarImpl horizontalScrollBar = ScrollBarImpl(false, true);
  49. void updateScrollRange();
  50. void limitScrolling(bool keepBeamVisible);
  51. // Attributes
  52. PersistentColor foreColor = PersistentColor(0, 0, 0);
  53. PersistentColor backColor = PersistentColor(200, 200, 200);
  54. PersistentString text;
  55. // Name of theme class used to draw the background.
  56. // "TextBox" is used if backgroundClass is empty or not found.
  57. PersistentString backgroundClass;
  58. PersistentBoolean multiLine;
  59. int64_t borderX = 6; // Empty pixels left and right of text.
  60. int64_t borderY = 4; // Empty pixels above and below text.
  61. // TODO: A setting for monospace?
  62. void declareAttributes(StructureDefinition &target) const override;
  63. Persistent* findAttribute(const ReadableString &name) override;
  64. // Temporary
  65. bool mousePressed = false;
  66. uint32_t combinationKeys = 0;
  67. // Selection goes from selectionStart to beamLocation using bi-directional exclusive character indices.
  68. // Empty with selectionStart = beamLocation.
  69. // From the left with selectionStart < beamLocation.
  70. // From the right with beamLocation < selectionStart.
  71. int64_t selectionStart = 0;
  72. int64_t beamLocation = 0;
  73. // Pre-splitted version of text for fast rendering of large documents.
  74. List<LineIndex> lines;
  75. int64_t indexedAtLength = -1; // Length of the last indexed text to detect changes. Assign to -1 to force an update.
  76. int64_t worstCaseLineMonospaces = 0; // An approximation of the worst case line length in monospaces, counting tabs as full length to keep it simple.
  77. void indexLines();
  78. void limitSelection();
  79. LVector2D getTextOrigin(bool includeVerticalScroll);
  80. int64_t findBeamLocationInLine(int64_t rowIndex, int64_t pixelX);
  81. BeamLocation findBeamLocation(const LVector2D &pixelLocation);
  82. ReadableString getSelectedText();
  83. void replaceSelection(const ReadableString &replacingText);
  84. void replaceSelection(DsrChar replacingCharacter);
  85. void placeBeamAtCharacter(int64_t characterIndex, bool removeSelection);
  86. void moveBeamVertically(int64_t rowIndexOffset, bool removeSelection);
  87. private:
  88. // Given from the style
  89. MediaMethod textBox;
  90. RasterFont font;
  91. void loadFont();
  92. void loadTheme(const VisualTheme &theme);
  93. void completeAssets();
  94. void generateGraphics();
  95. // Settings fetched from the theme
  96. String finalBackgroundClass; // The selected BackgroundClass/Class from layout settings or the component's default theme class "TextBox".
  97. int background_filter = 0; // 0 for solid, 1 for alpha filter.
  98. // Generated
  99. bool hasImages = false;
  100. bool drawnAsFocused = false;
  101. OrderedImageRgbaU8 image;
  102. public:
  103. TextBox();
  104. public:
  105. bool isContainer() const override;
  106. void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
  107. void receiveMouseEvent(const MouseEvent& event) override;
  108. void receiveKeyboardEvent(const KeyboardEvent& event) override;
  109. bool pointIsInside(const IVector2D& pixelPosition) override;
  110. void changedTheme(VisualTheme newTheme) override;
  111. void changedLocation(const IRect &oldLocation, const IRect &newLocation) override;
  112. void changedAttribute(const ReadableString &name) override;
  113. };
  114. }
  115. #endif