TextField.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #pragma once
  2. #include "Array.h"
  3. #include "Drawing.h"
  4. namespace Framework
  5. {
  6. class Font; //! Font.h
  7. class Text; //! Text.h
  8. class AlphaFeld; //! AlphaField.h
  9. class Border; //! Border.h
  10. class TextField; //! from this file
  11. class VScrollBar; //! Scroll.h
  12. class HScrollBar; //! Scroll.h
  13. class TextRenderer;
  14. struct TextStyle
  15. {
  16. int beginIndex;
  17. unsigned char fontSize;
  18. int fontColor;
  19. int selectedColor;
  20. int selectedBackcroundColor;
  21. bool underlined;
  22. bool selected;
  23. int interactParam;
  24. unsigned char rendererIndex;
  25. DLLEXPORT bool equals(const TextStyle& rhs);
  26. };
  27. //! Manages a text field
  28. class TextField : public DrawableBackground
  29. {
  30. public:
  31. class TextStyleManager : public virtual ReferenceCounter
  32. {
  33. protected:
  34. RCArray<TextRenderer>* renderer;
  35. Array<TextStyle> textStyle;
  36. int index;
  37. int styleIndex;
  38. TextStyle current;
  39. Text* text;
  40. public:
  41. DLLEXPORT TextStyleManager();
  42. DLLEXPORT virtual ~TextStyleManager();
  43. //! Sets the style of a text section
  44. //! \param begin The start position of the section
  45. //! \param end The end position of the section (not included)
  46. DLLEXPORT void setTextStyle(int begin, int end, TextStyle style);
  47. //! Removes a text section
  48. //! \param begin The index of the first affected character
  49. //! \param end The index of the first character after the section
  50. DLLEXPORT void removeText(int begin, int end);
  51. //! Inserts text at a specific position
  52. //! \param pos The position of the new text section
  53. //! \param text The new text
  54. DLLEXPORT void insertText(int pos, const char* text);
  55. //! Removes unneeded identical styles
  56. DLLEXPORT void cleanupStyles();
  57. //! Returns a reference to the style object
  58. DLLEXPORT TextStyle& currentStyle();
  59. //! Returns the current text renderer
  60. DLLEXPORT TextRenderer* zCurrentRenderer();
  61. //! Changes the content of the style object to the style of the
  62. //! next character. Returns 0 if there is no next character
  63. DLLEXPORT bool nextStyle();
  64. //! Changes the content of the style object to the style of the
  65. //! specified character \param index The index of the character
  66. //! to jump to. Returns 0 if the character does not exist
  67. DLLEXPORT bool stepTo(int index);
  68. //! Changes the content of the style object to the style of the
  69. //! first character
  70. DLLEXPORT void resetIteration();
  71. //! Returns the style of a specific character
  72. //! \param index The index of the character
  73. DLLEXPORT TextStyle getTextStyle(int index) const;
  74. friend TextField;
  75. };
  76. class Style : public DrawableBackground::Style
  77. {
  78. public:
  79. //! If this flag is not set, all line breaks are automatically
  80. //! removed from the text
  81. static const __int64 Mehrzeilig = 0x001000;
  82. //! If this flag is set, the text is placed exactly in the
  83. //! horizontal center of the field
  84. static const __int64 HCenter = 0x002000;
  85. //! If this flag is set, the text is placed exactly in the
  86. //! vertical center of the field
  87. static const __int64 VCenter = 0x004000;
  88. //! If this flag is set, the text can be edited by the user
  89. static const __int64 Editierbar = 0x2000000;
  90. //! Automatically inserts line breaks during rendering so that the
  91. //! width of the text field is not exceeded. The text itself is not
  92. //! modified. Requires Style Mehrzeilig
  93. static const __int64 AutoLineBreak = 0x4000000;
  94. //! Combines the flags HCenter and VCenter
  95. static const __int64 Center = HCenter | VCenter;
  96. //! Combines the flags Sichtbar, Erlaubt, Border,
  97. //! Buffered, VCenter
  98. static const __int64 TextField
  99. = Sichtbar | Erlaubt | Border | Buffered | VCenter | Editierbar;
  100. //! Combines the flags Sichtbar,
  101. //! Mehrzeilig
  102. static const __int64 Text = Sichtbar | Mehrzeilig | Erlaubt;
  103. //! Combines the flags Sichtbar, Erlaubt, Border,
  104. //! Hintergrund, Mehrzeilig, VScroll
  105. static const __int64 TextGebiet = Sichtbar | Erlaubt | Border
  106. | Hintergrund | Editierbar
  107. | Mehrzeilig | VScroll;
  108. //! Combines the flags VScroll and HScroll
  109. static const __int64 Scroll = VScroll | HScroll;
  110. };
  111. private:
  112. TextStyleManager* tm;
  113. Text* autoLineBreakSpacing;
  114. unsigned char showChar;
  115. int cpos;
  116. double tickVal;
  117. bool mausKlick;
  118. std::function<void(int, int, MausEreignis me)> charEvent;
  119. int getTextHeight() const;
  120. int getTextWidth() const;
  121. //! Processes mouse messages
  122. //! \param me The event triggered by the mouse input
  123. DLLEXPORT virtual void doMausEreignis(
  124. MausEreignis& me, bool userRet) override;
  125. public:
  126. //! Constructor
  127. DLLEXPORT TextField();
  128. //! Destructor
  129. DLLEXPORT virtual ~TextField();
  130. //! charEvent: a function called when the mouse is on a specific
  131. //! character and the interactParam in the style is != 0
  132. //! \param charEvent charEvent( charIndex, interactParam, mausEreignis );
  133. DLLEXPORT void setCharEvent(
  134. std::function<void(int, int, MausEreignis me)> charEvent);
  135. //! Sets a pointer to the text in the text field
  136. //! \param txt The pointer to the text
  137. DLLEXPORT void setTextZ(Text* txt);
  138. //! Sets the text of the text field
  139. //! \param txt The text
  140. DLLEXPORT void setText(Text* txt);
  141. //! Sets the text of the text field
  142. //! \param txt The text
  143. DLLEXPORT void setText(const char* txt);
  144. // Sets the text with styles
  145. // txt: the text
  146. // format: \x1: enables underline
  147. // \x2FF: sets the font size for the following text.
  148. // FF is a two-digit hex value
  149. // \x3AARRGGBB: sets the font color.
  150. // AARRGGBB is an 8-digit hex value with two characters
  151. // each for alpha, red, green and blue
  152. // \x4AARRGGBB: sets the color of the selected text.
  153. // AARRGGBB is an 8-digit hex value with two characters
  154. // each for alpha, red, green and blue
  155. // \x5AARRGGBB: sets the background color of the selected text.
  156. // AARRGGBB is an 8-digit hex value with two characters
  157. // each for alpha, red, green and blue
  158. // \x6FF: sets text renderer index.
  159. // FF is a two-digit hex value
  160. // \x7: disables underline
  161. // \x8FFFFFFFF: set interact param.
  162. // FFFFFFFF is an 8-digit hex value
  163. DLLEXPORT void setFormattedText(const char* txt);
  164. //! Inserts line breaks so that the text does not exceed the width
  165. //! of the text field \param spacing A text inserted directly after
  166. //! each inserted line break
  167. DLLEXPORT void addLineBreaks(const char* spacing = "");
  168. //! Inserts line breaks into the text txt so that the text does not
  169. //! exceed the width of the text field \param txt The text to insert
  170. //! line breaks into \param spacing A text inserted directly after
  171. //! each inserted line break \return The text with line breaks
  172. DLLEXPORT Text addLineBreaksToText(const char* txt,
  173. const char* spacing = "",
  174. bool includeFormat = 1) const;
  175. //! Sets a character sequence that is inserted after each line break
  176. //! added with the AutoLineBreak style
  177. DLLEXPORT void setAutoLineBreakSpacing(const char* spacing);
  178. //! Sets the style of a text section
  179. //! \param begin The start position of the section
  180. //! \param end The end position of the section (not included)
  181. DLLEXPORT void setTextStyle(int begin, int end, TextStyle style);
  182. //! Appends a line to the text
  183. //! \param zeile The new line
  184. DLLEXPORT void addZeile(const char* zeile);
  185. //! Appends a line to the text
  186. //! \param zeile The new line
  187. //! \param color The color of the line
  188. DLLEXPORT void addZeile(const char* zeile, int color);
  189. //! Deselects all text sections
  190. DLLEXPORT void deselectAuswahl();
  191. //! Sets the selected text section
  192. //! pos1: The cursor position in the text
  193. //! pos2: The position in the text up to which the text should be
  194. //! colored
  195. DLLEXPORT void setAuswahl(int pos1, int pos2);
  196. //! Sets the selected text section
  197. //! \param auswahl A point with x as cursor position and y as the
  198. //! position up to which the text should be colored
  199. DLLEXPORT void setAuswahl(Punkt& auswahl);
  200. //! Sets the selected text section
  201. //! pos1: The cursor position in the text
  202. //! pos2: The position in the text up to which the text should be
  203. //! colored
  204. DLLEXPORT void addAuswahl(int pos1, int pos2);
  205. //! Sets the selected text section
  206. //! \param auswahl A point with x as cursor position and y as the
  207. //! position up to which the text should be colored
  208. DLLEXPORT void addAuswahl(Punkt& auswahl);
  209. //! Sets the selected text section
  210. //! \param begin The cursor position in the text
  211. //! \param end The position in the text up to which the text should be
  212. //! colored
  213. DLLEXPORT void invertAuswahl(int begin, int end);
  214. //! Replaces all selected text sections with a text
  215. //! \param text The new text
  216. DLLEXPORT void replaceAuswahl(const char* text);
  217. //! Sets the TextRenderer to use
  218. //! \param textRd The text renderer
  219. DLLEXPORT void setTextRendererZ(TextRenderer* textRd);
  220. //! Adds a TextRenderer
  221. //! \param textRd The text renderer
  222. DLLEXPORT void addTextRendererZ(TextRenderer* textRd);
  223. //! Sets the TextRenderers to use
  224. //! \param textRd The text renderers
  225. DLLEXPORT void setTextRendererZ(RCArray<TextRenderer>* textRd);
  226. //! Sets a pointer to the font
  227. //! \param schrift The font to use for text drawing.
  228. DLLEXPORT void setFontZ(Font* schrift);
  229. //! Sets a pointer to the font
  230. //! \param rendererIndex The index of the renderer whose font should
  231. //! be set \param schrift The font to use for text drawing.
  232. DLLEXPORT void setFontZ(int rendererIndex, Font* schrift);
  233. //! Sets the font size (default: 12)
  234. //! \param gr The font size to use for text drawing
  235. DLLEXPORT void setSchriftSize(unsigned char gr);
  236. //! Sets the font size (default: 12)
  237. //! \param begin The index of the first affected character
  238. //! \param end The index of the first unaffected character
  239. //! \param gr The font size to use for text drawing
  240. DLLEXPORT void setSchriftSize(int begin, int end, unsigned char gr);
  241. //! Sets the font color
  242. //! \param fc The color to use for text drawing
  243. DLLEXPORT void setSchriftFarbe(int fc);
  244. //! Sets the font color
  245. //! \param begin The index of the first affected character
  246. //! \param end The index of the first unaffected character
  247. //! \param fc The color to use for text drawing
  248. DLLEXPORT void setSchriftFarbe(int begin, int end, int fc);
  249. //! Sets a character to use for drawing, regardless of the text
  250. //! field's text (requires drawing flag: Border)
  251. //! \param c The character to draw
  252. //! Example: setShowChar( '*' ); for password text fields
  253. DLLEXPORT void setSchowChar(unsigned char c);
  254. //! Scrolls to a specific line (requires drawing flag: VScroll)
  255. //! \param zeile The index of the line that should be the top visible line
  256. DLLEXPORT void setVScrollZuZeile(int zeile);
  257. //! Scrolls to a specific position in the text. Without the Erlaubt
  258. //! flag, always scrolls to the bottom. (requires drawing flag:
  259. //! VScroll) \param pos The index of the character to scroll to.
  260. //! By default scrolls to the cursor position
  261. DLLEXPORT void updateVScroll(int pos = -1);
  262. //! Scrolls to a specific position in the text. Requires the Erlaubt
  263. //! flag. (requires drawing flag: HScroll) \param pos The index of
  264. //! the character to scroll to. By default scrolls to the cursor position
  265. DLLEXPORT void updateHScroll(int pos = -1);
  266. //! Returns the width in pixels needed to fully display the current
  267. //! text with the current styles
  268. DLLEXPORT int getNeededWidth();
  269. //! Returns the height in pixels needed to fully display the current
  270. //! text with the current styles
  271. DLLEXPORT int getNeededHeight();
  272. //! Updates the object. Called by the framework
  273. //! \param tickVal The time in seconds since the last call of this
  274. //! function \return 1 if something changed and the image needs to be
  275. //! redrawn. 0 otherwise
  276. DLLEXPORT virtual bool tick(double tickval) override;
  277. //! Processes keyboard messages
  278. //! \param me The event triggered by the keyboard input
  279. DLLEXPORT void doTastaturEreignis(TastaturEreignis& te) override;
  280. //! Draws the object to zRObj if it is visible
  281. //! \param zRObj The image to draw into
  282. DLLEXPORT virtual void render(Image& zRObj) override;
  283. //! Returns the text from the text field
  284. DLLEXPORT Text* getText() const;
  285. //! Returns the text from the text field without increased reference
  286. //! counter
  287. DLLEXPORT Text* zText() const;
  288. //! Returns the font.
  289. //! \return 0 if the font was not set
  290. DLLEXPORT Font* getFont() const;
  291. //! Returns the font without increased reference counter
  292. //! \return 0 if the font was not set
  293. DLLEXPORT Font* zFont() const;
  294. //! Returns the font.
  295. //! \param rendererIndex The index of the renderer whose font should
  296. //! be returned \return 0 if the font was not set
  297. DLLEXPORT Font* getFont(int rendererIndex) const;
  298. //! Returns the font without increased reference counter
  299. //! \param rendererIndex The index of the renderer whose font should
  300. //! be returned \return 0 if the font was not set
  301. DLLEXPORT Font* zFont(int rendererIndex) const;
  302. //! Returns the TextRenderer.
  303. //! \return 0 if the TextRenderer was not set
  304. DLLEXPORT TextRenderer* getTextRenderer() const;
  305. //! Returns the TextRenderer without increased reference counter
  306. //! \return 0 if the TextRenderer was not set
  307. DLLEXPORT TextRenderer* zTextRenderer() const;
  308. //! Returns the TextRenderer.
  309. //! \param index The index of the renderer to return
  310. //! \return 0 if the TextRenderer was not set
  311. DLLEXPORT TextRenderer* getTextRenderer(int index) const;
  312. //! Returns the TextRenderer without increased reference counter
  313. //! \param index The index of the renderer to return
  314. //! \return 0 if the TextRenderer was not set
  315. DLLEXPORT TextRenderer* zTextRenderer(int index) const;
  316. //! Returns the font size
  317. DLLEXPORT unsigned char getSchriftSize() const;
  318. //! Returns the font size
  319. //! \param index The index of the character
  320. DLLEXPORT unsigned char getSchriftSize(int index) const;
  321. //! Returns the font color in A8R8G8B8 format
  322. DLLEXPORT int getSchriftFarbe() const;
  323. //! Returns the font color in A8R8G8B8 format
  324. //! \param index The index of the character
  325. DLLEXPORT int getSchriftFarbe(int index) const;
  326. //! Returns the display character
  327. DLLEXPORT unsigned char getShowChar() const;
  328. //! Returns the cursor position
  329. DLLEXPORT int getCursorPos() const;
  330. //! Returns 1 if the character is selected
  331. //! \param index The index of the character
  332. DLLEXPORT bool isCharSelected(int index) const;
  333. //! Returns the index of the character located under the mouse
  334. //! \param mx The x position of the mouse relative to the text field
  335. //! position \param my The y position of the mouse relative to the
  336. //! text field position \return -1 if no character is at the position
  337. DLLEXPORT int getTextIndexAt(int mx, int my) const;
  338. //! Returns the index of the character before which the cursor is
  339. //! placed when clicking with the mouse \param mx The x position of
  340. //! the mouse relative to the text field position \param my The y
  341. //! position of the mouse relative to the text field position
  342. DLLEXPORT int getCurserPosAt(int mx, int my) const;
  343. //! Returns the style of a specific character
  344. //! \param index The index of the character
  345. DLLEXPORT TextStyle getTextStyle(int index) const;
  346. //! Returns the character sequence inserted after each line break
  347. //! when using the AutoLineBreak style
  348. DLLEXPORT Text getAutoLineBreakSpacing() const;
  349. //! Creates a complete copy of the text field that can be modified
  350. //! without affecting the original
  351. DLLEXPORT Drawable* dublizieren() const override;
  352. };
  353. } // namespace Framework