NXWidgets  1.19
cmultilinetextbox.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cmultilinetextbox.hxx
3  *
4  * Copyright (C) 2012, 2016 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18  * me be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************
35  *
36  * Portions of this package derive from Woopsi (http://woopsi.org/) and
37  * portions are original efforts. It is difficult to determine at this
38  * point what parts are original efforts and which parts derive from Woopsi.
39  * However, in any event, the work of Antony Dzeryn will be acknowledged
40  * in most NxWidget files. Thanks Antony!
41  *
42  * Copyright (c) 2007-2011, Antony Dzeryn
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions are met:
47  *
48  * * Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * * Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * * Neither the names "Woopsi", "Simian Zombie" nor the
54  * names of its contributors may be used to endorse or promote products
55  * derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  ****************************************************************************/
69 
70 #ifndef __INCLUDE_CMULTILINETEXTBOX_HXX
71 #define __INCLUDE_CMULTILINETEXTBOX_HXX
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <stdint.h>
80 #include <stdbool.h>
81 
82 #include <nuttx/nx/nxglib.h>
83 #include <nuttx/nx/nx.h>
84 
85 #include "cscrollingpanel.hxx"
86 #include "cwidgetstyle.hxx"
87 #include "cnxstring.hxx"
88 #include "ctext.hxx"
89 #include "cwidgeteventhandler.hxx"
90 #include "itextbox.hxx"
91 
92 /****************************************************************************
93  * Pre-Processor Definitions
94  ****************************************************************************/
95 
96 /****************************************************************************
97  * Implementation Classes
98  ****************************************************************************/
99 
100 #if defined(__cplusplus)
101 
102 namespace NXWidgets
103 {
104  class CNxTimer;
105 
106  /**
107  * Textbox that offers multiple lines of text. Has scrolling
108  * capability and can be dragged using the mouse. The text
109  * it contains can be changed or added to. It can remember more
110  * rows of text than it can display, and these additional
111  * rows can be scrolled through.
112  */
113 
115  public CWidgetEventHandler
116  {
117  public:
118 
119  /**
120  * Enum of horizontal alignment options.
121  */
122 
124  {
125  TEXT_ALIGNMENT_HORIZ_CENTER = 0, /**< Centre the text */
126  TEXT_ALIGNMENT_HORIZ_LEFT = 1, /**< Align left */
127  TEXT_ALIGNMENT_HORIZ_RIGHT = 2 /**< Align right */
128  };
129 
130  /**
131  * Enum of vertical alignment options.
132  */
133 
135  {
136  TEXT_ALIGNMENT_VERT_CENTER = 0, /**< Align to centre of textbox */
137  TEXT_ALIGNMENT_VERT_TOP = 1, /**< Align to top of textbox */
138  TEXT_ALIGNMENT_VERT_BOTTOM = 2 /**< Align to bottom of textbox */
139  };
140 
141  protected:
142  CText *m_text; /**< CText object that manipulates
143  and wraps the raw text string. */
144  uint8_t m_visibleRows; /**< Total number of rows that the
145  textbox can display at once. */
146  nxgl_coord_t m_maxRows; /**< Maximum number of rows that the
147  textbox should buffer. */
148  int32_t m_topRow; /**< Index of the top row of text
149  currently displayed. */
150  TextAlignmentHoriz m_hAlignment; /**< Horizontal alignment of the text. */
151  TextAlignmentVert m_vAlignment; /**< Vertical alignment of the text. */
152  int m_cursorPos; /**< Position of the cursor within
153  the string. */
154  uint8_t m_showCursor; /**< Cursor visibility. */
155  bool m_wrapCursor; /**< True wrap cursor at the ends of the text */
156 
157  /**
158  * Get the coordinates of the cursor relative to the text.
159  *
160  * @param x Will be populated with the x coordinate of the cursor.
161  * @param y Will be populated with the y coordinate of the cursor.
162  */
163 
164  virtual void getCursorCoordinates(nxgl_coord_t& x, nxgl_coord_t& y) const;
165 
166  /**
167  * Gets the index of the character at the specified x coordinate in the
168  * specified row.
169  *
170  * @param x X coordinate of the character.
171  * @param rowIndex Index of the row containing the character.
172  * @return The index of the character at the specified coordinate.
173  */
174 
175  virtual int getCharIndexAtCoordinate(nxgl_coord_t x, int rowIndex) const;
176 
177  /**
178  * Get the index of the character at the specified coordinates.
179  *
180  * @param x X coordinate of the character.
181  * @param y Y coordinate of the character.
182  * @return The index of the character at the specified coordinates.
183  */
184 
185  virtual unsigned int getCharIndexAtCoordinates(nxgl_coord_t x, nxgl_coord_t y) const;
186 
187  /**
188  * Get the row containing the specified Y coordinate.
189  *
190  * @param y Y coordinate to locate.
191  * @return The index of the row containing the specified Y coordinate.
192  */
193 
194  int getRowContainingCoordinate(nxgl_coord_t y) const;
195 
196  /**
197  * Draw the area of this widget that falls within the clipping region.
198  * Called by the redraw() function to draw all visible regions.
199  *
200  * @param port The CGraphicsPort to draw to.
201  * @see redraw()
202  */
203 
204  virtual void drawContents(CGraphicsPort *port);
205 
206  /**
207  * Draw the area of this widget that falls within the clipping region.
208  * Called by the redraw() function to draw all visible regions.
209  *
210  * @param port The CGraphicsPort to draw to.
211  * @see redraw()
212  */
213 
214  virtual void drawBorder(CGraphicsPort *port);
215 
216  /**
217  * Move cursor one character to the left.
218  */
219 
220  virtual void moveCursorLeft(void);
221 
222  /**
223  * Move cursor one character to the right.
224  */
225 
226  virtual void moveCursorRight(void);
227 
228  /**
229  * Move cursor one row upwards.
230  */
231 
232  virtual void moveCursorUp(void);
233 
234  /**
235  * Move cursor one row downwards.
236  */
237 
238  virtual void moveCursorDown(void);
239 
240  /**
241  * Ensures that the textbox only contains the maximum allowed
242  * number of rows by culling any excess rows from the top of
243  * the text.
244  *
245  * @return True if lines were removed from the text; false if not.
246  */
247 
248  virtual bool cullTopLines(void);
249 
250  /**
251  * Ensures that the canvas height is the height of the widget,
252  * if the widget exceeds the size of the text, or the height of
253  * the text if the text exceeds the size of the widget.
254  */
255 
256  virtual void limitCanvasHeight(void);
257 
258  /**
259  * Ensures that the canvas cannot scroll beyond its height.
260  */
261 
262  virtual void limitCanvasY(void);
263 
264  /**
265  * Jumps to the cursor coordinates of the text.
266  */
267 
268  void jumpToCursor(void);
269 
270  /**
271  * Jumps to the bottom of the text.
272  */
273 
274  void jumpToTextBottom(void);
275 
276  /**
277  * Resize the textbox to the new dimensions.
278  *
279  * @param width The new width.
280  * @param height The new height.
281  */
282 
283  virtual void onResize(nxgl_coord_t width, nxgl_coord_t height);
284 
285  /**
286  * Starts the dragging system.
287  *
288  * @param x The x coordinate of the click.
289  * @param y The y coordinate of the click.
290  */
291 
292  virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
293 
294  /**
295  * Opens the keyboard on the bottom display.
296  *
297  * @param x The x coordinates of the click.
298  * @param y The y coordinates of the click.
299  */
300 
301  virtual void onDoubleClick(nxgl_coord_t x, nxgl_coord_t y);
302 
303  /**
304  * Handles physical button presses. Moves the cursor
305  * in the direction pressed.
306  *
307  * @param key The key that was pressed.
308  */
309 
311 
312  /**
313  * Handle a keyboard press event.
314  *
315  * @param e The event data.
316  */
317 
318  void handleKeyPressEvent(const CWidgetEventArgs &e);
319 
320  /**
321  * Handle a cursor control event. Replaces CWidgetEventHandler method.
322  *
323  * @param e The event data.
324  */
325 
327 
328  /**
329  * Gets the x position of a row of text based on the width of the row and the
330  * type of horizontal alignment currently set.
331  *
332  * @param row The index of the row.
333  * @return The x coordinate of the row.
334  */
335 
336  nxgl_coord_t getRowX(int row) const;
337 
338  /**
339  * Gets the y position of the specified row of text based on the type of
340  * vertical alignment currently set.
341  *
342  * @param row The row number to find the y coordinate of.
343  * @return The y coordinate of the specified row of text.
344  */
345 
346  nxgl_coord_t getRowY(int row) const;
347 
348  /**
349  * Return true if the cursor is visible
350  */
351 
352  virtual bool isCursorVisible(void) const;
353 
354  /**
355  * Gets the character under the cursor.
356  *
357  * @return The character under the cursor.
358  */
359 
360  nxwidget_char_t getCursorChar(void) const;
361 
362  /**
363  * Works out the number of visible rows within the textbox.
364  */
365 
366  void calculateVisibleRows(void);
367 
368  /**
369  * Draws text.
370  *
371  * @param port The CGraphicsPort to draw to.
372  */
373 
374  void drawText(CGraphicsPort *port);
375 
376  /**
377  * Draws the cursor.
378  *
379  * @param port The CGraphicsPort to draw to.
380  */
381 
382  void drawCursor(CGraphicsPort *port);
383 
384  /**
385  * Draws a single line of text.
386  *
387  * @param port The CGraphicsPort to draw to.
388  * @param row The index of the row to draw.
389  */
390 
391  void drawRow(CGraphicsPort *port, int row);
392 
393  /**
394  * Destructor.
395  */
396 
397  inline virtual ~CMultiLineTextBox(void)
398  {
399  delete m_text;
400  m_text = (CText *)NULL;
401  }
402 
403  /**
404  * Copy constructor is protected to prevent usage.
405  */
406 
407  inline CMultiLineTextBox(const CMultiLineTextBox &multiLineTextBox)
408  : CScrollingPanel(multiLineTextBox) { }
409 
410  public:
411 
412  /**
413  * Constructor.
414  *
415  * @param pWidgetControl The widget control for the display.
416  * @param x The x coordinate of the text box, relative to its parent.
417  * @param y The y coordinate of the text box, relative to its parent.
418  * @param width The width of the textbox.
419  * @param height The height of the textbox.
420  * @param text Pointer to a string to display in the textbox.
421  * @param flags Standard widget flag options.
422  * @param maxRows The maximum number of rows the textbox can track. Adding
423  * text beyond this number will cause rows at the start of the text to be
424  * forgotten; text is essentially stored as a queue, and adding to the back
425  * of a full queue causes the front items to be popped off. Setting this to
426  * 0 will make the textbox track only the visible rows.
427  * @param style The style that the widget should use. If this is not
428  * specified, the widget will use the values stored in the global
429  * g_defaultWidgetStyle object. The widget will copy the properties of
430  * the style into its own internal style object.
431  */
432 
433  CMultiLineTextBox(CWidgetControl *pWidgetControl,
434  nxgl_coord_t x, nxgl_coord_t y,
435  nxgl_coord_t width, nxgl_coord_t height,
436  const CNxString &text, uint32_t flags,
437  nxgl_coord_t maxRows = 0,
438  CWidgetStyle *style = (CWidgetStyle *)NULL);
439 
440  /**
441  * Set the horizontal alignment of text within the textbox.
442  *
443  * @param alignment The horizontal position of the text.
444  */
445 
446  virtual void setTextAlignmentHoriz(TextAlignmentHoriz alignment);
447 
448  /**
449  * Set the vertical alignment of text within the textbox.
450  *
451  * @param alignment The vertical position of the text.
452  */
453 
454  virtual void setTextAlignmentVert(TextAlignmentVert alignment);
455 
456  /**
457  * Returns the number of "pages" that the text spans. A page
458  * is defined as the amount of text that can be displayed within
459  * the textbox at one time.
460  *
461  * @return The page count.
462  */
463 
464  virtual const int getPageCount(void) const;
465 
466  /**
467  * Returns the current page.
468  *
469  * @return The current page.
470  * @see getPageCount().
471  */
472 
473  virtual const int getCurrentPage(void) const;
474 
475  /**
476  * Returns a pointer to the CText object that contains the
477  * wrapped text used in the textbox. It is used as the
478  * pre-processed data source for the textbox, and should
479  * not be altered.
480  *
481  * @return Pointer to the CText object.
482  */
483 
484  virtual inline const CText *getText(void) const
485  {
486  return m_text;
487  }
488 
489  /**
490  * Set the text displayed in the textbox.
491  *
492  * @param text String to display.
493  */
494 
495  virtual void setText(const CNxString &text);
496 
497  /**
498  * Append new text to the end of the current text
499  * displayed in the textbox.
500  *
501  * @param text String to append.
502  */
503 
504  virtual void appendText(const CNxString &text);
505 
506  /**
507  * Remove all characters from the string from the start index onwards.
508  *
509  * @param startIndex Index to remove from.
510  */
511 
512  virtual void removeText(const unsigned int startIndex);
513 
514  /**
515  * Remove specified number of characters from the string from the
516  * start index onwards.
517  *
518  * @param startIndex Index to remove from.
519  * @param count Number of characters to remove.
520  */
521 
522  virtual void removeText(const unsigned int startIndex, const unsigned int count);
523 
524  /**
525  * Set the font used in the textbox.
526  *
527  * @param font Pointer to the new font.
528  */
529 
530  virtual void setFont(CNxFont *font);
531 
532  /**
533  * Set the line spacing of the text.
534  *
535  * @param spacing Extra space (in pixels) to add between lines.
536  */
537 
538  virtual inline void setLineSpacing(nxgl_coord_t lineSpacing)
539  {
540  m_text->setLineSpacing(lineSpacing);
541  }
542 
543  /**
544  * Get the length of the text string.
545  *
546  * @return The length of the text string.
547  */
548 
549  virtual const int getTextLength(void) const;
550 
551  /**
552  * Sets the cursor display mode.
553  *
554  * @param cursorMode Determines cursor display mode
555  */
556 
557  virtual void showCursor(EShowCursor cursorMode);
558 
559  /**
560  * Shows the cursor in default mode (only when the TextBox has focus).
561  */
562 
563  virtual inline void showCursor(void)
564  {
566  }
567 
568  /**
569  * Hides the cursor.
570  */
571 
572  virtual inline void hideCursor(void)
573  {
575  }
576 
577  /**
578  * Enables/disables cursor wrapping
579  *
580  * @param wrap True enables cursor wrapping
581  */
582 
583  virtual inline void wrapCursor(bool wrap)
584  {
585  m_wrapCursor = wrap;
586  }
587 
588  /**
589  * Move the cursor to the text position specified. 0 indicates the start
590  * of the string. If position is greater than the length of the string,
591  * the cursor is moved to the end of the string.
592  *
593  * @param position The new cursor position.
594  */
595 
596  virtual void moveCursorToPosition(const int position);
597 
598  /**
599  * Get the cursor position. This is the index within the string that
600  * the cursor is currently positioned over.
601  *
602  * @return position The cursor position.
603  */
604 
605  virtual inline const int getCursorPosition(void) const
606  {
607  return m_cursorPos;
608  }
609 
610  /**
611  * Insert text at the specified index.
612  *
613  * @param text The text to insert.
614  * @param index Index at which to insert the text.
615  */
616 
617  virtual void insertText(const CNxString &text,
618  const unsigned int index);
619 
620  /**
621  * Insert text at the current cursor position.
622  *
623  * @param text The text to insert.
624  */
625 
626  virtual void insertTextAtCursor(const CNxString & ext);
627  };
628 }
629 
630 #endif // __cplusplus
631 
632 #endif // __INCLUDE_CMULTILINETEXTBOX_HXX
void drawCursor(CGraphicsPort *port)
virtual void setLineSpacing(nxgl_coord_t lineSpacing)
virtual const CText * getText(void) const
virtual void removeText(const unsigned int startIndex)
virtual void onDoubleClick(nxgl_coord_t x, nxgl_coord_t y)
virtual void limitCanvasHeight(void)
void drawText(CGraphicsPort *port)
virtual void wrapCursor(bool wrap)
virtual void setFont(CNxFont *font)
virtual const int getCurrentPage(void) const
virtual void getCursorCoordinates(nxgl_coord_t &x, nxgl_coord_t &y) const
virtual int getCharIndexAtCoordinate(nxgl_coord_t x, int rowIndex) const
virtual void setText(const CNxString &text)
virtual bool isCursorVisible(void) const
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
virtual unsigned int getCharIndexAtCoordinates(nxgl_coord_t x, nxgl_coord_t y) const
virtual void setTextAlignmentHoriz(TextAlignmentHoriz alignment)
void handleKeyPressEvent(const CWidgetEventArgs &e)
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
virtual const int getPageCount(void) const
virtual void setTextAlignmentVert(TextAlignmentVert alignment)
uint16_t nxwidget_char_t
Definition: nxconfig.hxx:454
virtual void drawBorder(CGraphicsPort *port)
CMultiLineTextBox(const CMultiLineTextBox &multiLineTextBox)
void drawRow(CGraphicsPort *port, int row)
virtual void insertTextAtCursor(const CNxString &ext)
virtual const int getTextLength(void) const
nxwidget_char_t getCursorChar(void) const
int getRowContainingCoordinate(nxgl_coord_t y) const
void processPhysicalKey(nxwidget_char_t key)
virtual void appendText(const CNxString &text)
nxgl_coord_t getRowX(int row) const
virtual const int getCursorPosition(void) const
virtual void insertText(const CNxString &text, const unsigned int index)
virtual void moveCursorToPosition(const int position)
void handleCursorControlEvent(const CWidgetEventArgs &e)
nxgl_coord_t getRowY(int row) const
virtual void drawContents(CGraphicsPort *port)
void setLineSpacing(nxgl_coord_t lineSpacing)
Definition: ctext.cxx:202