TextBox.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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: Prevent collisions between multiple scroll-bars in the corner by reserving a square in the corner when both are enabled.
  31. // TODO: At least make a stub implementation for cutting, copying and pasting text within the same application.
  32. // Then make it optional to integrate the clipboard with the external operating system using a window handle from somewhere.
  33. // 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.
  34. namespace dsr {
  35. struct LineIndex {
  36. // Exclusive interval of characters in the line.
  37. int64_t lineStartIndex = 0;
  38. int64_t lineEndIndex = 0;
  39. LineIndex(int64_t lineStartIndex, int64_t lineEndIndex)
  40. : lineStartIndex(lineStartIndex), lineEndIndex(lineEndIndex) {}
  41. };
  42. struct BeamLocation {
  43. int64_t rowIndex; // Index of the current row.
  44. int64_t characterIndex; // Index of the character in the entire text.
  45. BeamLocation(int64_t rowIndex, int64_t characterIndex)
  46. : rowIndex(rowIndex), characterIndex(characterIndex) {}
  47. };
  48. class TextBox : public VisualComponent {
  49. PERSISTENT_DECLARATION(TextBox)
  50. public:
  51. // Value allocated sub-components
  52. // TODO: Let them know about each other to avoid overlap in the shared corner.
  53. ScrollBarImpl verticalScrollBar = ScrollBarImpl(true);
  54. ScrollBarImpl horizontalScrollBar = ScrollBarImpl(false);
  55. void updateScrollRange();
  56. void limitScrolling(bool keepBeamVisible);
  57. // Attributes
  58. PersistentColor foreColor;
  59. PersistentColor backColor;
  60. PersistentString text;
  61. PersistentBoolean multiLine;
  62. int64_t borderX = 6; // Empty pixels left and right of text.
  63. int64_t borderY = 4; // Empty pixels above and below text.
  64. // TODO: A setting for monospace?
  65. void declareAttributes(StructureDefinition &target) const override;
  66. Persistent* findAttribute(const ReadableString &name) override;
  67. // Temporary
  68. bool mousePressed = false;
  69. uint32_t combinationKeys = 0;
  70. // Selection goes from selectionStart to beamLocation using bi-directional exclusive character indices.
  71. // Empty with selectionStart = beamLocation.
  72. // From the left with selectionStart < beamLocation.
  73. // From the right with beamLocation < selectionStart.
  74. int64_t selectionStart = 0;
  75. int64_t beamLocation = 0;
  76. // Pre-splitted version of text for fast rendering of large documents.
  77. List<LineIndex> lines;
  78. int64_t indexedAtLength = -1; // Length of the last indexed text to detect changes. Assign to -1 to force an update.
  79. int64_t worstCaseLineMonospaces = 0; // An approximation of the worst case line length in monospaces, counting tabs as full length to keep it simple.
  80. void indexLines();
  81. void limitSelection();
  82. LVector2D getTextOrigin(bool includeVerticalScroll);
  83. int64_t findBeamLocationInLine(int64_t rowIndex, int64_t pixelX);
  84. BeamLocation findBeamLocation(const LVector2D &pixelLocation);
  85. void replaceSelection(const ReadableString &replacingText);
  86. void replaceSelection(DsrChar replacingCharacter);
  87. void placeBeamAtCharacter(int64_t characterIndex, bool removeSelection);
  88. void moveBeamVertically(int64_t rowIndexOffset, bool removeSelection);
  89. private:
  90. // Given from the style
  91. MediaMethod textBox;
  92. RasterFont font;
  93. void loadFont();
  94. void completeAssets();
  95. void generateGraphics();
  96. // Generated
  97. bool hasImages = false;
  98. bool drawnAsFocused = false;
  99. OrderedImageRgbaU8 image;
  100. public:
  101. TextBox();
  102. public:
  103. bool isContainer() const override;
  104. void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
  105. void receiveMouseEvent(const MouseEvent& event) override;
  106. void receiveKeyboardEvent(const KeyboardEvent& event) override;
  107. bool pointIsInside(const IVector2D& pixelPosition) override;
  108. void changedTheme(VisualTheme newTheme) override;
  109. void changedLocation(const IRect &oldLocation, const IRect &newLocation) override;
  110. void changedAttribute(const ReadableString &name) override;
  111. };
  112. }
  113. #endif