TextBox.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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;
  53. PersistentColor backColor;
  54. PersistentString text;
  55. PersistentBoolean multiLine;
  56. int64_t borderX = 6; // Empty pixels left and right of text.
  57. int64_t borderY = 4; // Empty pixels above and below text.
  58. // TODO: A setting for monospace?
  59. void declareAttributes(StructureDefinition &target) const override;
  60. Persistent* findAttribute(const ReadableString &name) override;
  61. // Temporary
  62. bool mousePressed = false;
  63. uint32_t combinationKeys = 0;
  64. // Selection goes from selectionStart to beamLocation using bi-directional exclusive character indices.
  65. // Empty with selectionStart = beamLocation.
  66. // From the left with selectionStart < beamLocation.
  67. // From the right with beamLocation < selectionStart.
  68. int64_t selectionStart = 0;
  69. int64_t beamLocation = 0;
  70. // Pre-splitted version of text for fast rendering of large documents.
  71. List<LineIndex> lines;
  72. int64_t indexedAtLength = -1; // Length of the last indexed text to detect changes. Assign to -1 to force an update.
  73. int64_t worstCaseLineMonospaces = 0; // An approximation of the worst case line length in monospaces, counting tabs as full length to keep it simple.
  74. void indexLines();
  75. void limitSelection();
  76. LVector2D getTextOrigin(bool includeVerticalScroll);
  77. int64_t findBeamLocationInLine(int64_t rowIndex, int64_t pixelX);
  78. BeamLocation findBeamLocation(const LVector2D &pixelLocation);
  79. ReadableString getSelectedText();
  80. void replaceSelection(const ReadableString &replacingText);
  81. void replaceSelection(DsrChar replacingCharacter);
  82. void placeBeamAtCharacter(int64_t characterIndex, bool removeSelection);
  83. void moveBeamVertically(int64_t rowIndexOffset, bool removeSelection);
  84. private:
  85. // Given from the style
  86. MediaMethod textBox;
  87. RasterFont font;
  88. void loadFont();
  89. void completeAssets();
  90. void generateGraphics();
  91. // Generated
  92. bool hasImages = false;
  93. bool drawnAsFocused = false;
  94. OrderedImageRgbaU8 image;
  95. public:
  96. TextBox();
  97. public:
  98. bool isContainer() const override;
  99. void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
  100. void receiveMouseEvent(const MouseEvent& event) override;
  101. void receiveKeyboardEvent(const KeyboardEvent& event) override;
  102. bool pointIsInside(const IVector2D& pixelPosition) override;
  103. void changedTheme(VisualTheme newTheme) override;
  104. void changedLocation(const IRect &oldLocation, const IRect &newLocation) override;
  105. void changedAttribute(const ReadableString &name) override;
  106. };
  107. }
  108. #endif