NXWidgets  1.19
ctextbox.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/ctextbox.hxx
3  *
4  * Copyright (C) 2012 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_CTEXTBOX_HXX
71 #define __INCLUDE_CTEXTBOX_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 
84 #include "clabel.hxx"
85 #include "cnxstring.hxx"
86 #include "cwidgetstyle.hxx"
87 #include "cwidgeteventargs.hxx"
88 #include "itextbox.hxx"
89 
90 /****************************************************************************
91  * Pre-Processor Definitions
92  ****************************************************************************/
93 
94 /****************************************************************************
95  * Implementation Classes
96  ****************************************************************************/
97 
98 #if defined(__cplusplus)
99 
100 namespace NXWidgets
101 {
102  /**
103  * Forward references.
104  */
105 
106  class CWidgetControl;
107  class CNxTimer;
108  class CNxString;
109 
110  /**
111  * Single-line textbox widget. Can align text both vertically and
112  * horizontally in different ways. The widget gains this functionality by
113  * inheriting from the CLabel class. However, if the amount of text exceeds
114  * the dimensions of the widget, the widget will ignore its horizontal
115  * alignment settings and switch to left-aligned instead. This ensures that
116  * moving the cursor over the text will scroll through it correctly.
117  */
118 
119  class CTextBox : public ITextBox, public CLabel, public CWidgetEventHandler
120  {
121  protected:
122  int m_cursorPos; /**< Position of the cursor within the string. */
123  uint8_t m_showCursor; /**< Controls cursor visibility. */
124  bool m_wrapCursor; /**< True wrap cursor at the ends of the text */
125 
126  /**
127  * Redraws the widget
128  */
129 
130  inline void onBlur(void);
131 
132  /**
133  * Draw the area of this widget that falls within the clipping region.
134  * Called by the redraw() function to draw all visible regions.
135  *
136  * @param port The CGraphicsPort to draw to.
137  * @see redraw()
138  */
139 
140  virtual void drawContents(CGraphicsPort *port);
141 
142  /**
143  * Moves the cursor without redrawing.
144  *
145  * @param position New cursor position.
146  * @return True if the cursor position changed
147  */
148 
149  virtual bool repositionCursor(const int position);
150 
151  /**
152  * Move the cursor to the specified coordinates. The coordinates
153  * are expected to be the result of a click, and therefore in
154  * world-space rather than widget-space.
155  */
156 
157  void moveCursorToClickLocation(nxgl_coord_t x, nxgl_coord_t y);
158 
159  /**
160  * Draw the area of this widget that falls within the clipping region.
161  * Called by the redraw() function to draw all visible regions.
162  *
163  * @param port The CGraphicsPort to draw to.
164  * @see redraw()
165  */
166 
167  virtual void drawBorder(CGraphicsPort *port);
168 
169  /**
170  * Moves the cursor to the clicked coordinates.
171  *
172  * @param x The x coordinates of the click.
173  * @param y The y coordinates of the click.
174  */
175 
176  virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
177 
178  /**
179  * Does nothing.
180  *
181  * @param x The x coordinates of the click.
182  * @param y The y coordinates of the click.
183  */
184 
185  virtual void onDoubleClick(nxgl_coord_t x, nxgl_coord_t y);
186 
187  /**
188  * Return true if the cursor is visible
189  */
190 
191  virtual bool isCursorVisible(void) const;
192 
193  /**
194  * Get the x coordinate of the cursor in pixels relative
195  * to the left-hand edge of the client rect.
196  *
197  * @return The x coordinate of the cursor in pixels.
198  */
199 
200  virtual const nxgl_coord_t getCursorXPos(void) const;
201 
202  /**
203  * Get the width of the cursor in pixels.
204  *
205  * @return The width of the cursor in pixels.
206  */
207 
208  virtual nxgl_coord_t getCursorWidth(void) const;
209 
210  /**
211  * Calculate the horizontal position of the string based on its length
212  * and the alignment options. Alignment options are overridden if the
213  * width of the string exceeds the width of the textbox.
214  */
215 
216  virtual void calculateTextPositionHorizontal(void);
217 
218  /**
219  * Copy constructor is protected to prevent usage.
220  */
221 
222  inline CTextBox(const CTextBox& textbox) : CLabel(textbox) { };
223 
224  public:
225 
226  /**
227  * Constructor for a textbox containing a string.
228  *
229  * @param pWidgetControl The controlling widget for the window
230  * @param x The x coordinate of the text box, relative to its parent.
231  * @param y The y coordinate of the text box, relative to its parent.
232  * @param width The width of the textbox.
233  * @param height The height of the textbox.
234  * @param text Pointer to a string to display in the textbox.
235  * @param style The style that the button should use. If this is not
236  * specified, the button will use the global default widget
237  * style.
238  */
239 
240  CTextBox(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y,
241  nxgl_coord_t width, nxgl_coord_t height, const CNxString &text,
242  CWidgetStyle *style = (CWidgetStyle *)NULL);
243 
244  /**
245  * Sets the cursor display mode.
246  *
247  * @param cursorMode Determines cursor display mode
248  */
249 
250  virtual void showCursor(EShowCursor cursorMode);
251 
252  /**
253  * Shows the cursor in default mode (only when the TextBox has focus).
254  */
255 
256  virtual inline void showCursor(void)
257  {
259  }
260 
261  /**
262  * Hides the cursor.
263  */
264 
265  virtual inline void hideCursor(void)
266  {
268  }
269 
270  /**
271  * Enables/disables cursor wrapping
272  *
273  * @param wrap True enables cursor wrapping
274  */
275 
276  virtual inline void wrapCursor(bool wrap)
277  {
278  m_wrapCursor = wrap;
279  }
280 
281  /**
282  * Set the text displayed in the label.
283  *
284  * @param text String to display.
285  */
286 
287  virtual void setText(const CNxString &text);
288 
289  /**
290  * Append new text to the end of the current text displayed in the
291  * label.
292  *
293  * @param text String to append.
294  */
295 
296  virtual void appendText(const CNxString &text);
297 
298  /**
299  * Remove all characters from the string from the start index onwards.
300  *
301  * @param startIndex Index to remove from.
302  */
303 
304  virtual void removeText(const unsigned int startIndex);
305 
306  /**
307  * Remove specified number of characters from the string from the
308  * start index onwards.
309  *
310  * @param startIndex Index to remove from.
311  * @param count Number of characters to remove.
312  */
313 
314  virtual void removeText(const unsigned int startIndex, const unsigned int count);
315 
316  /**
317  * Insert text at the specified index.
318  *
319  * @param text The text to insert.
320  * @param index Index at which to insert the text.
321  */
322 
323  virtual void insertText(const CNxString &text, const unsigned int index);
324 
325  /**
326  * Insert text at the current cursor position.
327  *
328  * @param text The text to insert.
329  */
330 
331  virtual void insertTextAtCursor(const CNxString &text);
332 
333  /**
334  * Move the cursor to the text position specified. 0 indicates the
335  * start of the string. If position is greater than the length of the
336  * string, the cursor is moved to the end of the string.
337  *
338  * @param position The new cursor position.
339  */
340 
341  virtual void moveCursorToPosition(const int position);
342 
343  /**
344  * Get the cursor position. This is the index within the string that
345  * the cursor is currently positioned over.
346  *
347  * @return position The cursor position.
348  */
349 
350  virtual inline const int getCursorPosition(void) const
351  {
352  return m_cursorPos;
353  }
354 
355  /**
356  * Handle a keyboard press event. Replaces CWidgetEventHandler method.
357  *
358  * @param e The event data.
359  */
360 
361  void handleKeyPressEvent(const CWidgetEventArgs &e);
362 
363  /**
364  * Handle a cursor control event. Replaces CWidgetEventHandler method.
365  *
366  * @param e The event data.
367  */
368 
370  };
371 }
372 
373 #endif // __cplusplus
374 
375 #endif // __INCLUDE_CTEXTBOX_HXX
virtual void insertText(const CNxString &text, const unsigned int index)
Definition: ctextbox.cxx:219
void moveCursorToClickLocation(nxgl_coord_t x, nxgl_coord_t y)
Definition: ctextbox.cxx:438
virtual void insertTextAtCursor(const CNxString &text)
Definition: ctextbox.cxx:232
virtual void onDoubleClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: ctextbox.cxx:527
virtual const nxgl_coord_t getCursorXPos(void) const
Definition: ctextbox.cxx:549
void onBlur(void)
Definition: ctextbox.cxx:324
virtual void hideCursor(void)
Definition: ctextbox.hxx:265
void handleCursorControlEvent(const CWidgetEventArgs &e)
Definition: ctextbox.cxx:292
virtual void calculateTextPositionHorizontal(void)
Definition: ctextbox.cxx:595
virtual nxgl_coord_t getCursorWidth(void) const
Definition: ctextbox.cxx:573
virtual void drawBorder(CGraphicsPort *port)
Definition: ctextbox.cxx:493
virtual void removeText(const unsigned int startIndex)
Definition: ctextbox.cxx:190
virtual void setText(const CNxString &text)
Definition: ctextbox.cxx:163
virtual void moveCursorToPosition(const int position)
Definition: ctextbox.cxx:245
virtual void wrapCursor(bool wrap)
Definition: ctextbox.hxx:276
virtual const int getCursorPosition(void) const
Definition: ctextbox.hxx:350
virtual void appendText(const CNxString &text)
Definition: ctextbox.cxx:177
virtual void showCursor(void)
Definition: ctextbox.hxx:256
virtual void drawContents(CGraphicsPort *port)
Definition: ctextbox.cxx:337
virtual bool isCursorVisible(void) const
Definition: ctextbox.cxx:536
CTextBox(const CTextBox &textbox)
Definition: ctextbox.hxx:222
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: ctextbox.cxx:515
virtual bool repositionCursor(const int position)
Definition: ctextbox.cxx:417
void handleKeyPressEvent(const CWidgetEventArgs &e)
Definition: ctextbox.cxx:260