TextBox.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // TODO: Make a theme for the horizontal scroll-bar and activate it.
  30. // TODO: At least make a stub implementation for cutting, copying and pasting text within the same application.
  31. // Then make it optional to integrate the clipboard with the external operating system using a window handle from somewhere.
  32. // Copying within the same application is still useful if one has multiple files open at the same time or just need to move things around.
  33. namespace dsr {
  34. struct LineIndex {
  35. // Exclusive interval of characters in the line.
  36. int64_t lineStartIndex = 0;
  37. int64_t lineEndIndex = 0;
  38. LineIndex(int64_t lineStartIndex, int64_t lineEndIndex)
  39. : lineStartIndex(lineStartIndex), lineEndIndex(lineEndIndex) {}
  40. };
  41. struct BeamLocation {
  42. int64_t rowIndex; // Index of the current row.
  43. int64_t characterIndex; // Index of the character in the entire text.
  44. BeamLocation(int64_t rowIndex, int64_t characterIndex)
  45. : rowIndex(rowIndex), characterIndex(characterIndex) {}
  46. };
  47. class TextBox : public VisualComponent {
  48. PERSISTENT_DECLARATION(TextBox)
  49. public:
  50. // Value allocated sub-components
  51. ScrollBarImpl verticalScrollBar = ScrollBarImpl(true, true);
  52. ScrollBarImpl horizontalScrollBar = ScrollBarImpl(false, true);
  53. void updateScrollRange();
  54. void limitScrolling(bool keepBeamVisible);
  55. // Attributes
  56. PersistentColor foreColor;
  57. PersistentColor backColor;
  58. PersistentString text;
  59. PersistentBoolean multiLine;
  60. int64_t borderX = 6; // Empty pixels left and right of text.
  61. int64_t borderY = 4; // Empty pixels above and below text.
  62. // TODO: A setting for monospace?
  63. void declareAttributes(StructureDefinition &target) const override;
  64. Persistent* findAttribute(const ReadableString &name) override;
  65. // Temporary
  66. bool mousePressed = false;
  67. uint32_t combinationKeys = 0;
  68. // Selection goes from selectionStart to beamLocation using bi-directional exclusive character indices.
  69. // Empty with selectionStart = beamLocation.
  70. // From the left with selectionStart < beamLocation.
  71. // From the right with beamLocation < selectionStart.
  72. int64_t selectionStart = 0;
  73. int64_t beamLocation = 0;
  74. // Pre-splitted version of text for fast rendering of large documents.
  75. List<LineIndex> lines;
  76. int64_t indexedAtLength = -1; // Length of the last indexed text to detect changes. Assign to -1 to force an update.
  77. int64_t worstCaseLineMonospaces = 0; // An approximation of the worst case line length in monospaces, counting tabs as full length to keep it simple.
  78. void indexLines();
  79. void limitSelection();
  80. LVector2D getTextOrigin(bool includeVerticalScroll);
  81. int64_t findBeamLocationInLine(int64_t rowIndex, int64_t pixelX);
  82. BeamLocation findBeamLocation(const LVector2D &pixelLocation);
  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 completeAssets();
  93. void generateGraphics();
  94. // Generated
  95. bool hasImages = false;
  96. bool drawnAsFocused = false;
  97. OrderedImageRgbaU8 image;
  98. public:
  99. TextBox();
  100. public:
  101. bool isContainer() const override;
  102. void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
  103. void receiveMouseEvent(const MouseEvent& event) override;
  104. void receiveKeyboardEvent(const KeyboardEvent& event) override;
  105. bool pointIsInside(const IVector2D& pixelPosition) override;
  106. void changedTheme(VisualTheme newTheme) override;
  107. void changedLocation(const IRect &oldLocation, const IRect &newLocation) override;
  108. void changedAttribute(const ReadableString &name) override;
  109. };
  110. }
  111. #endif