NXWidgets  1.19
cgraphicsport.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cgraphicsport.hxx
3  *
4  * Copyright (C) 2012, 2015 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 all 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_CGRAPHICSPORT_HXX
71 #define __INCLUDE_CGRAPHICSPORT_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 "nxconfig.hxx"
83 #include "inxwindow.hxx"
84 
85 /****************************************************************************
86  * Pre-Processor Definitions
87  ****************************************************************************/
88 
89 /****************************************************************************
90  * Implementation Classes
91  ****************************************************************************/
92 
93 #if defined(__cplusplus)
94 
95 namespace NXWidgets
96 {
97  class CNxFont;
98  class CNxString;
99  class CRect;
100  struct SBitmap;
101 
102  /**
103  * CGraphicsPort is the interface between a NXwidget and NX layer.
104  */
105 
107  {
108  private:
109  INxWindow *m_pNxWnd; /**< NX window interface. */
110 #ifdef CONFIG_NX_WRITEONLY
111  nxgl_mxpixel_t m_backColor; /**< The background color to use */
112 #endif
113 
114  /**
115  * The underlying implementation for drawText functions
116  * @param pos The window-relative x/y coordinate of the string.
117  * @param bound The window-relative bounds of the string.
118  * @param font The font to draw with.
119  * @param string The string to output.
120  * @param startIndex The start index within the string from which
121  * drawing will commence.
122  * @param length The number of characters to draw.
123  * @param background Color to use for background if transparent is false.
124  * @param transparent Whether to fill the background.
125  */
126 
127  void _drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font,
128  const CNxString &string, int startIndex, int length,
129  nxgl_mxpixel_t background, bool transparent);
130 
131  public:
132  /**
133  * Constructor.
134  *
135  * @param pNxWnd. An instance of the underlying window type.
136  * @param backColor. The background color is only needed if we
137  * cannot read from the graphics device.
138  */
139 
140 #ifdef CONFIG_NX_WRITEONLY
141  CGraphicsPort(INxWindow *pNxWnd,
142  nxgl_mxpixel_t backColor = CONFIG_NXWIDGETS_DEFAULT_BACKGROUNDCOLOR);
143 #else
144  CGraphicsPort(INxWindow *pNxWnd);
145 #endif
146 
147  /**
148  * Destructor.
149  */
150 
151  virtual ~CGraphicsPort();
152 
153  /**
154  * Return the absolute x coordinate of the upper left hand corner of the
155  * underlying window.
156  *
157  * @return The x coordinate of the window.
158  */
159 
160  const nxgl_coord_t getX(void) const;
161 
162  /**
163  * Return the absolute y coordinate of the upper left hand corner of the
164  * underlying window.
165  *
166  * @return The y coordinate of the window.
167  */
168 
169  const nxgl_coord_t getY(void) const;
170 
171  /**
172  * Get the background color that will be used to fill in the spaces
173  * when rendering fonts. This background color is ONLY used if the
174  * LCD device does not support reading GRAM contents.
175  *
176  * @return. The current background color being used.
177  */
178 
179 #ifdef CONFIG_NX_WRITEONLY
180  nxgl_mxpixel_t getBackColor(void) const
181  {
182  return m_backColor;
183  }
184 #endif
185 
186  /**
187  * Set the background color that will be used to fill in the spaces
188  * when rendering fonts. This background color is ONLY used if the
189  * LCD device does not support reading GRAM contents.
190  *
191  * @return. The current background color being used.
192  */
193 
194 #ifdef CONFIG_NX_WRITEONLY
195  void setBackColor(nxgl_mxpixel_t backColor)
196  {
197  m_backColor = backColor;
198  }
199 #endif
200 
201  /**
202  * Draw a pixel into the window.
203  *
204  * @param x The window-relative x coordinate of the pixel.
205  * @param y The window-relative y coordinate of the pixel.
206  * @param color The color of the pixel.
207  */
208 
209  void drawPixel(nxgl_coord_t x, nxgl_coord_t y, nxgl_mxpixel_t color);
210 
211  /**
212  * Draw a horizontal line of the specified start position, width, and
213  * color.
214  *
215  * @param x The x coordinate of the line.
216  * @param y The y coordinate of the top-most end of the line.
217  * @param width The width of the line in pixels.
218  * @param color The color of the line.
219  */
220 
221  void drawHorizLine(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width,
222  nxgl_mxpixel_t color);
223 
224  /**
225  * Draw a vertical line of the specified start position, width, and
226  * color.
227  *
228  * @param x The x coordinate of the left-most end of the line.
229  * @param y The y coordinate of the line.
230  * @param height The height of the line in rows.
231  * @param color The color of the line.
232  */
233 
234  void drawVertLine(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t height,
235  nxgl_mxpixel_t color);
236 
237  /**
238  * Draw a line of a fixed color in the window.
239  *
240  * @param x1 The x coordinate of the start point of the line.
241  * @param y1 The y coordinate of the start point of the line.
242  * @param x2 The x coordinate of the end point of the line.
243  * @param y2 The y coordinate of the end point of the line.
244  * @param color The color of the line.
245  * @param caps Draw a circular cap on the ends of the line to support
246  * better line joins
247  */
248 
249  void drawLine(nxgl_coord_t x1, nxgl_coord_t y1,
250  nxgl_coord_t x2, nxgl_coord_t y2,
251  nxgl_mxpixel_t color, enum INxWindow::ELineCaps caps);
252 
253  /**
254  * Draw a filled rectangle of the specified start position, end position,
255  * width, and color.
256  *
257  * @param x The window-relative x coordinate of the rectangle.
258  * @param y The window-relative y coordinate of the rectangle.
259  * @param width The width of the rectangle in pixels.
260  * @param height The height of the rectangle in rows.
261  * @param color The color of the rectangle.
262  */
263 
264  void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width,
265  nxgl_coord_t height, nxgl_mxpixel_t color);
266 
267  /**
268  * Draw an unfilled rectangle to the window
269  *
270  * @param x The window-relative x coordinate of the rectangle.
271  * @param y The window-relative y coordinate of the rectangle.
272  * @param width The width of the rectangle.
273  * @param height The height of the rectangle.
274  * @param color The color of the rectangle .
275  */
276 
277  void drawRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width,
278  nxgl_coord_t height, nxgl_mxpixel_t color);
279 
280  /**
281  * Draw a bevelled rectangle to the window.
282  *
283  * @param x The x coordinate of the rectangle.
284  * @param y The y coordinate of the rectangle.
285  * @param width The width of the rectangle.
286  * @param height The height of the rectangle.
287  * @param shineColor The color of the top/left sides.
288  * @param shadowColor The color of the bottom/right sides.
289  */
290 
291  void drawBevelledRect(nxgl_coord_t x, nxgl_coord_t y,
292  nxgl_coord_t width, nxgl_coord_t height,
293  nxgl_mxpixel_t shineColor,
294  nxgl_mxpixel_t shadowColor);
295 
296  /**
297  * Draw a filled circle at the specified position, size, and color.
298  *
299  * @param center The window-relative coordinates of the circle center.
300  * @param radius The radius of the rectangle in pixels.
301  * @param color The color of the rectangle.
302  */
303 
304  inline void drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius,
305  nxgl_mxpixel_t color)
306  {
307  (void)m_pNxWnd->drawFilledCircle(center, radius, color);
308  }
309 
310  /**
311  * Draw a string to the window.
312  * @param pos The window-relative x/y coordinate of the string.
313  * @param bound The window-relative bounds of the string.
314  * @param font The font to draw with.
315  * @param string The string to output.
316  */
317 
318  void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font,
319  const CNxString &string);
320 
321  /**
322  * Draw a particular length of a string to the window in a secific color.
323  * @param pos The window-relative x/y coordinate of the string.
324  * @param bound The window-relative bounds of the string.
325  * @param font The font to draw with.
326  * @param string The string to output.
327  * @param startIndex The start index within the string from which
328  * drawing will commence.
329  * @param length The number of characters to draw.
330  * @param color The color of the string.
331  */
332 
333  void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font,
334  const CNxString &string, int startIndex, int length,
335  nxgl_mxpixel_t color);
336 
337  /**
338  * Draw a portion of a string to the window.
339  * @param pos The window-relative x/y coordinate of the string.
340  * @param bound The window-relative bounds of the string.
341  * @param font The font to draw with.
342  * @param string The string to output.
343  * @param startIndex The start index within the string from which
344  * drawing will commence.
345  * @param length The number of characters to draw.
346  */
347 
348  void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font,
349  const CNxString &string, int startIndex, int length);
350 
351  /**
352  * Draw a portion of a string to the window and fill the background
353  * in one go.
354  * @param pos The window-relative x/y coordinate of the string.
355  * @param bound The window-relative bounds of the string.
356  * @param font The font to draw with.
357  * @param string The string to output.
358  * @param startIndex The start index within the string from which
359  * drawing will commence.
360  * @param length The number of characters to draw.
361  * @param color Foreground color
362  * @param background Background color
363  */
364 
365  void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font,
366  const CNxString &string, int startIndex, int length,
367  nxgl_mxpixel_t color, nxgl_mxpixel_t background);
368 
369  /**
370  * Draw an opaque bitmap to the window.
371  *
372  * @param x The window-relative x coordinate to draw the bitmap to.
373  * @param y The window-relative y coordinate to draw the bitmap to.
374  * @param width The width of the bitmap to draw.
375  * @param height The height of the bitmap to draw.
376  * @param bitmap Pointer to the bitmap to draw.
377  * @param bitmapX The window-relative x coordinate within the supplied
378  * bitmap to use as the origin.
379  * @param bitmapY The window-relative y coordinate within the supplied
380  * bitmap to use as the origin.
381  */
382 
383  void drawBitmap(nxgl_coord_t x, nxgl_coord_t y,
384  nxgl_coord_t width, nxgl_coord_t height,
385  const struct SBitmap *bitmap, int bitmapX, int bitmapY);
386 
387  /**
388  * Draw a bitmap to the window, using the supplied transparent
389  * color as an invisible color.
390  *
391  * @param x The window-relative x coordinate to draw the bitmap to.
392  * @param y The window-relative y coordinate to draw the bitmap to.
393  * @param width The width of the bitmap to draw.
394  * @param height The height of the bitmap to draw.
395  * @param bitmap Pointer to the bitmap to draw.
396  * @param bitmapX The window-relative x coordinate within the supplied bitmap to use as
397  * the origin.
398  * @param bitmapY The window-relative y coordinate within the supplied bitmap to use as
399  * the origin.
400  * @param transparentColor The transparent color used in the bitmap.
401  */
402 
403  void drawBitmap(nxgl_coord_t x, nxgl_coord_t y,
404  nxgl_coord_t width, nxgl_coord_t height,
405  const struct SBitmap *bitmap, int bitmapX, int bitmapY,
406  nxgl_mxpixel_t transparentColor);
407 
408  /**
409  * Draw a bitmap to the port in greyscale.
410  *
411  * @param x The window-relative x coordinate to draw the bitmap to.
412  * @param y The window-relative y coordinate to draw the bitmap to.
413  * @param width The width of the bitmap to draw.
414  * @param height The height of the bitmap to draw.
415  * @param bitmap Pointer to the bitmap to draw.
416  * @param bitmapX The window-relative x coordinate within the supplied bitmap to use as
417  * the origin.
418  * @param bitmapY The window-relative y coordinate within the supplied bitmap to use as
419  * the origin.
420  */
421 
422  void drawBitmapGreyScale(nxgl_coord_t x, nxgl_coord_t y,
423  nxgl_coord_t width, nxgl_coord_t height,
424  const struct SBitmap *bitmap, int bitmapX, int bitmapY);
425 
426  /**
427  * Copy a rectangular region from the source coordinateinates to the
428  * destination coordinateinates.
429  *
430  * @param sourceX Source x coordinate.
431  * @param sourceY Source y coordinate.
432  * @param destX Destination x coordinate.
433  * @param destY Destination y coordinate.
434  * @param width Width of the rectangle to copy.
435  * @param height Height of the rectangle to copy.
436  */
437 
438  void copy(nxgl_coord_t sourceX, nxgl_coord_t sourceY,
439  nxgl_coord_t destX, nxgl_coord_t destY,
440  nxgl_coord_t width, nxgl_coord_t height);
441 
442  /**
443  * Move a region by a specified distance in two dimensions.
444  *
445  * @param x X coordinate of the source area to move.
446  * @param y Y coordinate of the source area to move.
447  * @param deltaX Horizontal distance to move.
448  * @param deltaY Vertical distance to move.
449  * @param width Width of the area to move.
450  * @param height Height of the area to move.
451  */
452 
453  void move(nxgl_coord_t x, nxgl_coord_t y,
454  nxgl_coord_t deltaX, nxgl_coord_t deltaY,
455  nxgl_coord_t width, nxgl_coord_t height);
456 
457  /**
458  * Convert the region to greyscale.
459  *
460  * @param x X coordinate of the region to change.
461  * @param y Y coordinate of the region to change.
462  * @param width Width of the region to change.
463  * @param height Height of the region to change.
464  */
465 
466  void greyScale(nxgl_coord_t x, nxgl_coord_t y,
467  nxgl_coord_t width, nxgl_coord_t height);
468 
469  /**
470  * Invert colors in a region. NOTE: This allocates an in-memory
471  * buffer the size of one row in graphic memory. So it may only be
472  * useful for inverting small regions and its only current use of for
473  * the inverted cursor text.
474  *
475  * @param x X coordinate of the region to change.
476  * @param y Y coordinate of the region to change.
477  * @param width Width of the region to change.
478  * @param height Height of the region to change.
479  */
480 
481  void invert(nxgl_coord_t x, nxgl_coord_t y,
482  nxgl_coord_t width, nxgl_coord_t height);
483 
484  };
485 }
486 
487 #endif // __cplusplus
488 
489 #endif // __INCLUDE_CGRAPHICSPORT_HXX
void copy(nxgl_coord_t sourceX, nxgl_coord_t sourceY, nxgl_coord_t destX, nxgl_coord_t destY, nxgl_coord_t width, nxgl_coord_t height)
void drawVertLine(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t height, nxgl_mxpixel_t color)
const nxgl_coord_t getY(void) const
void setBackColor(nxgl_mxpixel_t backColor)
CGraphicsPort(INxWindow *pNxWnd, nxgl_mxpixel_t backColor=CONFIG_NXWIDGETS_DEFAULT_BACKGROUNDCOLOR)
void drawBevelledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t shineColor, nxgl_mxpixel_t shadowColor)
void drawBitmap(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, const struct SBitmap *bitmap, int bitmapX, int bitmapY)
void drawHorizLine(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_mxpixel_t color)
void drawBitmapGreyScale(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, const struct SBitmap *bitmap, int bitmapX, int bitmapY)
void drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius, nxgl_mxpixel_t color)
void greyScale(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height)
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
void drawLine(nxgl_coord_t x1, nxgl_coord_t y1, nxgl_coord_t x2, nxgl_coord_t y2, nxgl_mxpixel_t color, enum INxWindow::ELineCaps caps)
void invert(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height)
nxgl_mxpixel_t getBackColor(void) const
const nxgl_coord_t getX(void) const
void drawPixel(nxgl_coord_t x, nxgl_coord_t y, nxgl_mxpixel_t color)
void move(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t deltaX, nxgl_coord_t deltaY, nxgl_coord_t width, nxgl_coord_t height)
static const NXWidgets::SRlePaletteBitmapEntry bitmap[]
virtual bool drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius, nxgl_mxpixel_t color)=0
void _drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font, const CNxString &string, int startIndex, int length, nxgl_mxpixel_t background, bool transparent)
void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font, const CNxString &string)
void drawRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)