NXWidgets  1.19
cscrollingtextbox.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cscrollingtextbox.cxx
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 /****************************************************************************
71  * Included Files
72  ****************************************************************************/
73 
74 #include <nuttx/config.h>
75 
76 #include <stdint.h>
77 #include <stdbool.h>
78 
79 #include "cscrollingtextbox.hxx"
80 #include "cscrollbarvertical.hxx"
81 #include "cgraphicsport.hxx"
82 
83 /****************************************************************************
84  * Pre-Processor Definitions
85  ****************************************************************************/
86 
87 /****************************************************************************
88  * Method Implementations
89  ****************************************************************************/
90 
91 using namespace NXWidgets;
92 
93 /**
94  * Constructor.
95  *
96  * @param pWidgetControl The widget control for the display.
97  * @param x The x coordinate of the text box, relative to its parent.
98  * @param y The y coordinate of the text box, relative to its parent.
99  * @param width The width of the textbox.
100  * @param height The height of the textbox.
101  * @param text Pointer to a string to display in the textbox.
102  * @param flags Standard widget flag options.
103  * @param maxRows The maximum number of rows the textbox can track. Adding
104  * text beyond this number will cause rows at the start of the text to be
105  * forgotten; text is essentially stored as a queue, and adding to the back
106  * of a full queue causes the front items to be popped off. Setting this to
107  * 0 will make the textbox track only the visible rows.
108  * @param style The style that the widget should use. If this is not
109  * specified, the widget will use the values stored in the global
110  * g_defaultWidgetStyle object. The widget will copy the properties of
111  * the style into its own internal style object.
112  */
113 
115  nxgl_coord_t x, nxgl_coord_t y,
116  nxgl_coord_t width, nxgl_coord_t height,
117  const CNxString &text, uint32_t flags,
118  nxgl_coord_t maxRows, CWidgetStyle *style)
119 : CNxWidget(pWidgetControl, x, y, width, height, flags, style)
120 {
121  m_scrollbarWidth = 10;
122  setBorderless(true);
123 
124  m_texbox = new CMultiLineTextBox(pWidgetControl,
125  0, 0, width - m_scrollbarWidth, height,
126  text, flags, maxRows, &m_style);
128 
129  // Create scrollbar
130 
131  CRect rect;
132  m_texbox->getClientRect(rect);
133  m_scrollbar = new CScrollbarVertical(pWidgetControl,
134  width - m_scrollbarWidth, 0,
135  m_scrollbarWidth, height, &m_style);
141 
142  // Add children to child array
143 
146 }
147 
148 /**
149  * Set the horizontal alignment of text within the textbox.
150  *
151  * @param alignment The horizontal position of the text.
152  */
153 
155 {
156  m_texbox->setTextAlignmentHoriz(alignment);
157 }
158 
159 /**
160  * Set the vertical alignment of text within the textbox.
161  *
162  * @param alignment The vertical position of the text.
163  */
164 
166 {
167  m_texbox->setTextAlignmentVert(alignment);
168 }
169 
170 /**
171  * Returns the number of "pages" that the text spans. A page
172  * is defined as the amount of text that can be displayed within
173  * the textbox at one time.
174  *
175  * @return The page count.
176  */
177 
178 const uint16_t CScrollingTextBox::getPageCount(void) const
179 {
180  return m_texbox->getPageCount();
181 }
182 
183 /**
184  * Returns the current page.
185  *
186  * @return The current page.
187  * @see getPageCount().
188  */
189 
190 const uint16_t CScrollingTextBox::getCurrentPage(void) const
191 {
192  return m_texbox->getCurrentPage();
193 }
194 
195 /**
196  * Returns a pointer to the Text object that contains the
197  * wrapped text used in the textbox. It is used as the
198  * pre-processed data source for the textbox, and should
199  * not be altered.
200  *
201  * @return Pointer to the Text object.
202  */
203 
205 {
206  return m_texbox->getText();
207 }
208 
209 /**
210  * Set the text displayed in the textbox.
211  *
212  * @param text String to display.
213  */
214 
216 {
217  m_texbox->setText(text);
218  m_scrollbar->redraw();
219 }
220 
221 /**
222  * Append new text to the end of the current text
223  * displayed in the textbox.
224  *
225  * @param text String to append.
226  */
227 
229 {
230  m_texbox->appendText(text);
231  m_scrollbar->redraw();
232 }
233 
234 /**
235  * Remove all characters from the string from the start index onwards.
236  *
237  * @param startIndex Index to remove from.
238  */
239 
240 void CScrollingTextBox::removeText(const unsigned int startIndex)
241 {
242  m_texbox->removeText(startIndex);
243  m_scrollbar->redraw();
244 }
245 
246 /**
247  * Remove specified number of characters from the string from the
248  * start index onwards.
249  *
250  * @param startIndex Index to remove from.
251  * @param count Number of characters to remove.
252  */
253 
254 void CScrollingTextBox::removeText(const unsigned int startIndex,
255  const unsigned int count)
256 {
257  m_texbox->removeText(startIndex, count);
258  m_scrollbar->redraw();
259 }
260 
261 /**
262  * Set the font used in the textbox.
263  *
264  * @param font Pointer to the new font.
265  */
266 
268 {
269  m_style.font = font;
270  m_texbox->setFont(font);
271  m_scrollbar->setFont(font);
272 }
273 
274 /**
275  * Get the length of the text string.
276  *
277  * @return The length of the text string.
278  */
279 
280 const unsigned int CScrollingTextBox::getTextLength(void) const
281 {
282  return m_texbox->getTextLength();
283 }
284 
285 /**
286  * Sets the cursor display mode.
287  *
288  * @param cursorMode Determines cursor display mode
289  */
290 
292 {
293  m_texbox->showCursor(cursorMode);
294 }
295 
296 /**
297  * Enables/disables cursor wrapping
298  *
299  * @param wrap True enables cursor wrapping
300  */
301 
303 {
304  m_texbox->wrapCursor(wrap);
305 }
306 
307 /**
308  * Move the cursor to the text position specified. 0 indicates the start
309  * of the string. If position is greater than the length of the string,
310  * the cursor is moved to the end of the string.
311  *
312  * @param position The new cursor position.
313  */
314 
315 void CScrollingTextBox::moveCursorToPosition(const int32_t position)
316 {
317  m_texbox->moveCursorToPosition(position);
318  m_scrollbar->redraw();
319 }
320 
321 /**
322  * Get the cursor position. This is the index within the string that
323  * the cursor is currently positioned over.
324  *
325  * @return position The cursor position.
326  */
327 
328 const int32_t CScrollingTextBox::getCursorPosition(void) const
329 {
330  return m_texbox->getCursorPosition();
331 }
332 
333 /**
334  * Insert text at the specified index.
335  *
336  * @param text The text to insert.
337  * @param index Index at which to insert the text.
338  */
339 
341  const unsigned int index)
342 {
343  m_texbox->insertText(text, index);
344  m_scrollbar->redraw();
345 }
346 
347 /**
348  * Insert text at the current cursor position.
349  *
350  * @param text The text to insert.
351  */
352 
354 {
356  m_scrollbar->redraw();
357 }
358 
359 /**
360  * Handles events raised by its sub-widgets.
361  *
362  * @param e Event arguments.
363  */
364 
366 {
367  if (e.getSource() != NULL)
368  {
369  if (e.getSource() == m_scrollbar)
370  {
371  if (m_texbox != NULL)
372  {
373  m_texbox->setRaisesEvents(false);
374  m_texbox->jump(0, 0 - m_scrollbar->getValue());
375  m_texbox->setRaisesEvents(true);
376  }
377  }
378  else if (e.getSource() == m_texbox)
379  {
380  if (m_scrollbar != NULL)
381  {
386  }
387  }
388  }
389 }
390 
391 /**
392  * Handles events raised by its sub-widgets.
393  *
394  * @param e Event arguments.
395  */
396 
398 {
399  if (e.getSource() != NULL)
400  {
401  if (e.getSource() == m_texbox)
402  {
403  if (m_scrollbar != NULL)
404  {
408  }
409  }
410  }
411 }
412 
413 /**
414  * Gets the x coordinate of the virtual canvas.
415  *
416  * @return The x coordinate of the virtual canvas.
417  */
418 
419 const int32_t CScrollingTextBox::getCanvasX(void) const
420 {
421  return m_texbox->getCanvasX();
422 }
423 
424 /**
425  * Gets the y coordinate of the virtual canvas.
426  *
427  * @return The y coordinate of the virtual canvas.
428  */
429 
430 const int32_t CScrollingTextBox::getCanvasY(void) const
431 {
432  return m_texbox->getCanvasY();
433 }
434 
435 /**
436  * Gets the width of the virtual canvas.
437  *
438  * @return The width of the virtual canvas.
439  */
440 
441 const int32_t CScrollingTextBox::getCanvasWidth(void) const
442 {
443  return m_texbox->getCanvasWidth();
444 }
445 
446 /**
447  * Gets the height of the virtual canvas.
448  *
449  * @return The height of the virtual canvas.
450  */
451 
452 const int32_t CScrollingTextBox::getCanvasHeight(void) const
453 {
454  return m_texbox->getCanvasHeight();
455 }
456 
457 /**
458  * Scroll the panel by the specified amounts.
459  *
460  * @param dx The horizontal distance to scroll.
461  * @param dy The vertical distance to scroll.
462  */
463 
464 void CScrollingTextBox::scroll(int32_t dx, int32_t dy)
465 {
466  m_texbox->scroll(dx, dy);
467 }
468 
469 /**
470  * Reposition the panel's scrolling region to the specified coordinates.
471  *
472  * @param x The new x coordinate of the scrolling region.
473  * @param y The new y coordinate of the scrolling region.
474  */
475 
476 void CScrollingTextBox::jump(int32_t x, int32_t y)
477 {
478  m_texbox->jump(x, y);
479 }
480 
481 /**
482  * Set whether or not horizontal scrolling is allowed.
483  *
484  * @param allow True to allow horizontal scrolling; false to deny it.
485  */
486 
488 {
490 }
491 
492 /**
493  * Set whether or not horizontal scrolling is allowed.
494  *
495  * @param allow True to allow horizontal scrolling; false to deny it.
496  */
497 
499 {
500  // NOP
501 }
502 
503 /**
504  * Sets the width of the virtual canvas.
505  *
506  * @param width The width of the virtual canvas.
507  */
508 
509 void CScrollingTextBox::setCanvasWidth(const int32_t width)
510 {
511  // NOP
512 }
513 
514 /**
515  * Sets the height of the virtual canvas.
516  *
517  * @param height The height of the virtual canvas.
518  */
519 
520 void CScrollingTextBox::setCanvasHeight(const int32_t height)
521 {
522  // NOP
523 }
524 
525 /**
526  * Returns true if vertical scrolling is allowed.
527  *
528  * @return True if vertical scrolling is allowed.
529  */
530 
532 {
533  return m_texbox->allowsVerticalScroll();
534 }
535 
536 /**
537  * Returns true if horizontal scrolling is allowed.
538  *
539  * @return True if horizontal scrolling is allowed.
540  */
541 
543 {
545 }
546 
547 /**
548  * Draw the area of this widget that falls within the clipping region.
549  * Called by the redraw() function to draw all visible regions.
550  *
551  * @param port The CGraphicsPort to draw to.
552  * @see redraw()
553  */
554 
556 {
558 }
559 
560 /**
561  * Resize the textbox to the new dimensions.
562  *
563  * @param width The new width.
564  * @param height The new height.
565  */
566 
567 void CScrollingTextBox::onResize(nxgl_coord_t width, nxgl_coord_t height)
568 {
569  // Resize the children
570 
571  m_texbox->resize(width - m_scrollbarWidth, height);
573 
574  // Move the scrollbar
575 
576  m_scrollbar->moveTo(width - m_scrollbarWidth, 0);
577 }
virtual const int32_t getCanvasY(void) const
void setMinimumValue(const int value)
bool allowsVerticalScroll(void) const
virtual void appendText(const CNxString &text)
const T & getSource(void) const
Definition: teventargs.hxx:127
virtual const int32_t getCanvasHeight(void) const
virtual void setTextAlignmentVert(CMultiLineTextBox::TextAlignmentVert alignment)
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
virtual const CText * getText(void) const
bool moveTo(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:990
virtual void removeText(const unsigned int startIndex)
virtual void insertText(const CNxString &text, const unsigned int index)
virtual const CText * getText(void) const
void setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
virtual void wrapCursor(bool wrap)
virtual void setCanvasWidth(const int32_t width)
virtual void setFont(CNxFont *font)
virtual const int getCurrentPage(void) const
virtual const int32_t getCanvasX(void) const
virtual void setText(const CNxString &text)
virtual void insertTextAtCursor(const CNxString &text)
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
virtual const int32_t getCanvasWidth(void) const
virtual void scroll(int32_t dx, int32_t dy)
virtual bool allowsVerticalScroll(void) const
virtual void handleScrollEvent(const CWidgetEventArgs &e)
virtual const int32_t getCanvasWidth(void) const
bool allowsHorizontalScroll(void) const
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
virtual void wrapCursor(bool wrap)
virtual void setTextAlignmentHoriz(TextAlignmentHoriz alignment)
void getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
virtual void removeText(const unsigned int startIndex)
virtual const int32_t getCanvasHeight(void) const
virtual const int32_t getCursorPosition(void) const
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
virtual const int getPageCount(void) const
virtual void setTextAlignmentVert(TextAlignmentVert alignment)
virtual void setCanvasHeight(const int32_t height)
virtual void showCursor(EShowCursor cursorMode)
virtual void setFont(CNxFont *font)
Definition: cnxwidget.cxx:474
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
virtual void insertTextAtCursor(const CNxString &ext)
virtual bool allowsHorizontalScroll(void) const
virtual void drawContents(CGraphicsPort *port)
virtual void setAllowsHorizontalScroll(bool allow)
virtual const uint16_t getPageCount(void) const
virtual void setFont(CNxFont *font)
CScrollbarVertical * m_scrollbar
virtual const int getTextLength(void) const
virtual const int32_t getCanvasX(void) const
virtual void appendText(const CNxString &text)
virtual const int32_t getCanvasY(void) const
virtual void setText(const CNxString &text)
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
virtual void scroll(int32_t dx, int32_t dy)
virtual const int getCursorPosition(void) const
virtual void moveCursorToPosition(const int32_t position)
CScrollingTextBox(const CScrollingTextBox &scrollingTextBox)
virtual void insertText(const CNxString &text, const unsigned int index)
void setAllowsVerticalScroll(bool allow)
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
void setMaximumValue(const int value)
virtual void moveCursorToPosition(const int position)
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
virtual void handleValueChangeEvent(const CWidgetEventArgs &e)
virtual void setAllowsVerticalScroll(bool allow)
virtual void setTextAlignmentHoriz(CMultiLineTextBox::TextAlignmentHoriz alignment)
virtual const uint16_t getCurrentPage(void) const
virtual const unsigned int getTextLength(void) const
void setBorderless(bool isBorderless)
Definition: cnxwidget.cxx:461
virtual void jump(int32_t x, int32_t y)
void setPageSize(const nxgl_coord_t pageSize)
virtual void jump(int32_t x, int32_t y)