From 0c6f41912c3c08822b1b4b825e7566b0ff749fb7 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 27 Apr 2019 17:53:02 -0600 Subject: [PATCH] apps/graphics/twm4nx: Add compount widget CIconWidget. This will eventually replace the icon Windows with widgets that can be drawn on the Background. --- graphics/nxwidgets/src/cradiobuttongroup.cxx | 4 +- graphics/twm4nx/Makefile | 7 +- graphics/twm4nx/src/ciconwidget.cxx | 277 +++++++++++++++++++ graphics/twm4nx/src/ciconwin.cxx | 4 +- include/graphics/nxwidgets/crect.hxx | 37 ++- include/graphics/twm4nx/cbackground.hxx | 12 +- include/graphics/twm4nx/ciconwidget.hxx | 194 +++++++++++++ 7 files changed, 510 insertions(+), 25 deletions(-) create mode 100644 graphics/twm4nx/src/ciconwidget.cxx create mode 100644 include/graphics/twm4nx/ciconwidget.hxx diff --git a/graphics/nxwidgets/src/cradiobuttongroup.cxx b/graphics/nxwidgets/src/cradiobuttongroup.cxx index d3d35edd9..b6066403f 100644 --- a/graphics/nxwidgets/src/cradiobuttongroup.cxx +++ b/graphics/nxwidgets/src/cradiobuttongroup.cxx @@ -88,7 +88,7 @@ ****************************************************************************/ /**************************************************************************** - * CLabel Method Implementations + * CRadioButtonGroup Method Implementations ****************************************************************************/ using namespace NXWidgets; @@ -154,7 +154,7 @@ CRadioButton *CRadioButtonGroup::newRadioButton(nxgl_coord_t x, nxgl_coord_t y, } /** - * Gets a pointer to the selected widget. + * Gets a pointer to the selectm_widgetControled widget. * * @return Pointer to the selected widget. */ diff --git a/graphics/twm4nx/Makefile b/graphics/twm4nx/Makefile index ede5562f8..e1aa8210d 100644 --- a/graphics/twm4nx/Makefile +++ b/graphics/twm4nx/Makefile @@ -54,9 +54,10 @@ STACKSIZE = $(CONFIG_TWM4NX_STACKSIZE) ASRCS = CSRCS = -CXXSRCS = cwindow.cxx cwindowfactory.cxx ciconwin.cxx cwindowevent.cxx -CXXSRCS += cicon.cxx ciconmgr.cxx cbackground.cxx cfonts.cxx cresize.cxx -CXXSRCS += cmenus.cxx cinput.cxx twm4nx_cursor.cxx +CXXSRCS = cbackground.cxx cfonts.cxx cicon.cxx ciconmgr.cxx +CXXSRCS += ciconwidget.cxx ciconwin.cxx cinput.cxx cmenus.cxx +CXXSRCS += cresize.cxx cwindow.cxx cwindowevent.cxx cwindowfactory.cxx +CXXSRCS += twm4nx_cursor.cxx MAINSRC = ctwm4nx.cxx VPATH = src diff --git a/graphics/twm4nx/src/ciconwidget.cxx b/graphics/twm4nx/src/ciconwidget.cxx new file mode 100644 index 000000000..f903f76a8 --- /dev/null +++ b/graphics/twm4nx/src/ciconwidget.cxx @@ -0,0 +1,277 @@ +///////////////////////////////////////////////////////////////////////////// +// apps/graphics/twm4nx/src/ciconwidget.cxx +// Represents on desktop icon +// +// Copyright (C) 2019 Gregory Nutt. All rights reserved. +// Author: Gregory Nutt +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// 3. Neither the name NuttX nor the names of its contributors may be +// used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////////////////////// +// Included Files +///////////////////////////////////////////////////////////////////////////// + +#include + +#include +#include +#include + +#include + +#include "graphics/nxwidgets/cgraphicsport.hxx" +#include "graphics/nxwidgets/cwidgeteventargs.hxx" +#include "graphics/nxwidgets/crlepalettebitmap.hxx" + +#include "graphics/twm4nx/ctwm4nx.hxx" +#include "graphics/twm4nx/cfonts.hxx" +#include "graphics/twm4nx/ciconwidget.hxx" + +///////////////////////////////////////////////////////////////////////////// +// CIconWidget Definitions +///////////////////////////////////////////////////////////////////////////// + +using namespace Twm4Nx; + +/** + * Constructor. Note that the group determines its width and height + * from the position and dimensions of its children. + * + * @param widgetControl The controlling widget for the display. + * @param x The x coordinate of the group. + * @param y The y coordinate of the group. + * @param style The style that the button should use. If this is not + * specified, the button will use the global default widget + * style. + */ + +CIconWidget::CIconWidget(FAR CTwm4Nx *twm4nx, + FAR NXWidgets::CWidgetControl *widgetControl, + nxgl_coord_t x, nxgl_coord_t y, + FAR NXWidgets::CWidgetStyle *style) +: CNxWidget(widgetControl, x, y, 0, 0, WIDGET_BORDERLESS, style) +{ + m_twm4nx = twm4nx; + m_widgetControl = widgetControl; + + // Configure the widget + + m_flags.borderless = true; +} + +/** + * Perform widget initialization that could fail and so it not appropriate + * for the constructor + * + * @param cbitmp The bitmap image representing the icon + * @param title The icon title string + * @return True is returned if the widget is successfully initialized. + */ + +bool CIconWidget::initialize(FAR NXWidgets::CRlePaletteBitmap *cbitmap, + FAR NXWidgets::CNxString &title) +{ + // Get the size of the Icon bitmap + + struct nxgl_size_s iconImageSize; + iconImageSize.w = cbitmap->getWidth(); + iconImageSize.h = cbitmap->getHeight(); + + // Get the size of the Icon name + + FAR CFonts *fonts = m_twm4nx->getFonts(); + FAR NXWidgets::CNxFont *iconFont = fonts->getIconFont(); + + struct nxgl_size_s iconLabelSize; + iconLabelSize.w = iconFont->getStringWidth(title); + iconLabelSize.h = iconFont->getHeight(); + + // Determine the new size of the containing widget + + struct nxgl_size_s iconWidgetSize; + iconWidgetSize.w = ngl_max(iconImageSize.w, iconLabelSize.w); + iconWidgetSize.h = iconImageSize.h + iconLabelSize.h + 2; + + // Update the widget size + + resize(iconWidgetSize.w, iconWidgetSize.h); + + // Get the position bitmap image, centering horizontally if the text + // width is larger than the image width + + struct nxgl_point_s iconImagePos; + iconImagePos.x = 0; + iconImagePos.y = 0; + + if (iconLabelSize.w > (iconImageSize.w + 1)) + { + iconImagePos.x = (iconLabelSize.w - iconImageSize.w) / 2; + } + + // Create a new CImage to hold the bitmap image + + FAR NXWidgets::CImage *image = + new NXWidgets::CImage(m_widgetControl, iconImagePos.x, + iconImagePos.y, iconImageSize.w, iconImageSize.h, + cbitmap, m_style); + if (image == (FAR NXWidgets::CImage *)0) + { + gerr("ERROR: Failed to create image\n"); + return false; + } + + image->setBorderless(true); + + // Get the position icon text, centering horizontally if the image + // width is larger than the text width + + struct nxgl_point_s iconLabelPos; + iconLabelPos.x = 0; + iconLabelPos.y = iconImageSize.h + 2; + + if (iconImageSize.w > (iconLabelSize.w + 1)) + { + iconLabelPos.x = (iconImageSize.w - iconLabelSize.w) / 2; + } + + // Create a new CLabel to hold the icon text + + FAR NXWidgets::CLabel *label = + new NXWidgets::CLabel(m_widgetControl, iconLabelPos.x, iconLabelPos.y, + iconLabelSize.w, iconLabelSize.h, title); + if (label == (FAR NXWidgets::CLabel *)0) + { + gerr("ERROR: Failed to create icon label\n"); + delete image; + return false; + } + + label->setBorderless(true); + + // Add the CImage to to the containing widget + + image->addWidgetEventHandler(this); + addWidget(image); + + label->addWidgetEventHandler(this); + addWidget(label); + return true; +} + +/** + * Insert the dimensions that this widget wants to have into the rect + * passed in as a parameter. All coordinates are relative to the + * widget's parent. Value is based on the length of the largest string + * in the set of options. + * + * @param rect Reference to a rect to populate with data. + */ + +void CIconWidget::getPreferredDimensions(NXWidgets::CRect &rect) const +{ + struct nxgl_size_s widgetSize; + getSize(widgetSize); + + struct nxgl_point_s widgetPos; + getPos(widgetPos); + + rect.setX(widgetPos.x); + rect.setY(widgetPos.y); + rect.setWidth(widgetSize.w); + rect.setHeight(widgetSize.h); +} + +/** + * Handle a mouse click event. + * + * @param e The event data. + */ + +void CIconWidget::handleClickEvent(const NXWidgets::CWidgetEventArgs &e) +{ + m_widgetEventHandlers->raiseClickEvent(e.getX(), e.getY()); +} + +/** + * Handle a mouse double-click event. + * + * @param e The event data. + */ + +void CIconWidget::handleDoubleClickEvent(const NXWidgets::CWidgetEventArgs &e) +{ + m_widgetEventHandlers->raiseDoubleClickEvent(e.getX(), e.getY()); +} + +/** + * Handle a mouse button release event that occurred within the bounds of + * the source widget. + * @param e The event data. + */ + +void CIconWidget::handleReleaseEvent(const NXWidgets::CWidgetEventArgs &e) +{ + m_widgetEventHandlers->raiseReleaseEvent(e.getX(), e.getY()); +} + +/** + * Handle a mouse button release event that occurred outside the bounds of + * the source widget. + * + * @param e The event data. + */ + +void CIconWidget::handleReleaseOutsideEvent(const NXWidgets::CWidgetEventArgs &e) +{ + // Child raised a release outside event, but we need to raise a different + // event if the release occurred within the bounds of this parent widget + + if (checkCollision(e.getX(), e.getY())) + { + m_widgetEventHandlers->raiseReleaseEvent(e.getX(), e.getY()); + } + else + { + m_widgetEventHandlers->raiseReleaseOutsideEvent(e.getX(), e.getY()); + } +} + +/** + * Draw the area of this widget that falls within the clipping region. + * Called by the redraw() function to draw all visible regions. + * @param port The NXWidgets::CGraphicsPort to draw to. + * @see redraw() + */ + +void CIconWidget::drawContents(NXWidgets::CGraphicsPort *port) +{ + port->drawFilledRect(getX(), getY(), getWidth(), getHeight(), + getBackgroundColor()); +} diff --git a/graphics/twm4nx/src/ciconwin.cxx b/graphics/twm4nx/src/ciconwin.cxx index 4b0fc2633..a17349a1f 100644 --- a/graphics/twm4nx/src/ciconwin.cxx +++ b/graphics/twm4nx/src/ciconwin.cxx @@ -108,13 +108,13 @@ bool CIconWin::initialize(FAR CWindow *parent, { struct nxgl_point_s final; - // Git the size of the Icon Image + // Get the size of the Icon Image struct nxgl_size_s iconImageSize; iconImageSize.h = sbitmap->height; iconImageSize.w = sbitmap->height; - // Git the size of the Icon name + // Get the size of the Icon name FAR CFonts *fonts = m_twm4nx->getFonts(); FAR NXWidgets::CNxFont *iconFont = fonts->getIconFont(); diff --git a/include/graphics/nxwidgets/crect.hxx b/include/graphics/nxwidgets/crect.hxx index 8c51f0f2a..4a323db21 100644 --- a/include/graphics/nxwidgets/crect.hxx +++ b/include/graphics/nxwidgets/crect.hxx @@ -164,6 +164,7 @@ namespace NXWidgets /** * Get the rectangle's top y coordinate. + * * @return The rectangle's y coordinate. */ @@ -172,6 +173,18 @@ namespace NXWidgets return m_pos.y; } + /** + * Get the position of the rectangle + * + * @pos The location to return the rectangle position + */ + + inline void getPos(struct nxgl_point_s &pos) const + { + pos.x = m_pos.x; + pos.y = m_pos.y; + } + /** * Get the rectangle's width. * @@ -183,18 +196,6 @@ namespace NXWidgets return m_size.w; } - /** - * Get the size of the rectangle - * - * @return The rectangle's size - */ - - inline void getSize(struct nxgl_size_s &size) const - { - size.h = m_size.h; - size.w = m_size.w; - } - /** * Get the rectangle's height. * @@ -206,6 +207,18 @@ namespace NXWidgets return m_size.h; } + /** + * Get the size of the rectangle + * + * @param size The location to return the rectangle size + */ + + inline void getSize(struct nxgl_size_s &size) const + { + size.h = m_size.h; + size.w = m_size.w; + } + /** * Get the NX rectangle representation * diff --git a/include/graphics/twm4nx/cbackground.hxx b/include/graphics/twm4nx/cbackground.hxx index 8086b3175..a23038436 100644 --- a/include/graphics/twm4nx/cbackground.hxx +++ b/include/graphics/twm4nx/cbackground.hxx @@ -37,9 +37,9 @@ #ifndef __APPS_INCLUDE_GRAPHICS_TWM4NX_CBACKGROUND_HXX #define __APPS_INCLUDE_GRAPHICS_TWM4NX_CBACKGROUND_HXX -/**************************************************************************** - * Included Files - ****************************************************************************/ +///////////////////////////////////////////////////////////////////////////// +// Included Files +///////////////////////////////////////////////////////////////////////////// #include @@ -49,9 +49,9 @@ #include "graphics/nxwidgets/cwidgeteventhandler.hxx" #include "graphics/nxwidgets/cwidgeteventargs.hxx" -/**************************************************************************** - * Implementation Class Definition - ****************************************************************************/ +///////////////////////////////////////////////////////////////////////////// +// Implementation Class Definition +///////////////////////////////////////////////////////////////////////////// namespace NXWidgets { diff --git a/include/graphics/twm4nx/ciconwidget.hxx b/include/graphics/twm4nx/ciconwidget.hxx new file mode 100644 index 000000000..c4a31bce6 --- /dev/null +++ b/include/graphics/twm4nx/ciconwidget.hxx @@ -0,0 +1,194 @@ +///////////////////////////////////////////////////////////////////////////// +// apps/include/graphics/twm4nx/ciconwidget.hxx +// Represents on desktop icon +// +// Copyright (C) 2019 Gregory Nutt. All rights reserved. +// Author: Gregory Nutt +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in +// the documentation and/or other materials provided with the +// distribution. +// 3. Neither the name NuttX nor the names of its contributors may be +// used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef ___APPS_INCLUDE_GRAPHICS_TWM4NX_CICONWIDGET_HXX +#define ___APPS_INCLUDE_GRAPHICS_TWM4NX_CICONWIDGET_HXX + +///////////////////////////////////////////////////////////////////////////// +// Included Files +///////////////////////////////////////////////////////////////////////////// + +#include + +#include +#include +#include + +#include +#include + +#include "graphics/nxwidgets/cnxwidget.hxx" +#include "graphics/nxwidgets/cbutton.hxx" +#include "graphics/nxwidgets/cwidgetstyle.hxx" +#include "graphics/nxwidgets/cwidgeteventhandler.hxx" + +///////////////////////////////////////////////////////////////////////////// +// Implementation Classes +///////////////////////////////////////////////////////////////////////////// + +namespace NXWidgets +{ + class CNxWidget; // Forward reference + class CWidgetStyle; // Forward reference + class CWidgetControl; // Forward reference + class CGraphicsPort; // Forward reference + class CWidgetEventHandler; // Forward reference + class CWidgetEventArgs; // Forward reference + class CRlePaletteBitmap; // Forward reference + class CImage; // Forward reference + class CLabel; // Forward reference +} + +namespace Twm4Nx +{ + class FAR CTwm4Nx; // Forward reference + + /** + * Container class that holds the Icon image and table widgets + */ + + class CIconWidget : public NXWidgets::CNxWidget, + public NXWidgets::CWidgetEventHandler + { + protected: + FAR CTwm4Nx *m_twm4nx; /**< Cached Twm4Nx session */ + FAR NXWidgets::CWidgetControl *m_widgetControl; /**< The controlling widget */ + FAR NXWidgets::CWidgetStyle *m_style; /**< Widget style */ + + /** + * Draw the area of this widget that falls within the clipping region. + * Called by the redraw() function to draw all visible regions. + * @param port The NXWidgets::CGraphicsPort to draw to. + * @see redraw() + */ + + virtual void drawContents(NXWidgets::CGraphicsPort* port); + + /** + * Copy constructor is protected to prevent usage. + */ + + inline CIconWidget(const CIconWidget &radioButtonGroup) : + CNxWidget(radioButtonGroup) { } + + public: + + /** + * Constructor. Note that the group determines its width and height + * from the position and dimensions of its children. + * + * @param twm4nx The Twm4Nx session object + * @param widgetControl The controlling widget for the display. + * @param x The x coordinate of the group. + * @param y The y coordinate of the group. + * @param style The style that the button should use. If this is not + * specified, the button will use the global default widget + * style. + */ + + CIconWidget(FAR CTwm4Nx *twm4nx, + FAR NXWidgets::CWidgetControl *widgetControl, + nxgl_coord_t x, nxgl_coord_t y, + FAR NXWidgets::CWidgetStyle *style = + (FAR NXWidgets::CWidgetStyle *)0); + + /** + * Destructor. + */ + + ~CIconWidget(void) + { + } + + /** + * Perform widget initialization that could fail and so it not appropriate + * for the constructor + * + * @param cbitmp The bitmap image representing the icon + * @param title The icon title string + * @return True is returned if the widget is successfully initialized. + */ + + bool initialize(FAR NXWidgets::CRlePaletteBitmap *cbitmap, + FAR NXWidgets::CNxString &title); + + /** + * Insert the dimensions that this widget wants to have into the rect + * passed in as a parameter. All coordinates are relative to the + * widget's parent. Value is based on the length of the largest string + * in the set of options. + * + * @param rect Reference to a rect to populate with data. + */ + + virtual void getPreferredDimensions(NXWidgets::CRect &rect) const; + + /** + * Handle a mouse click event. + * + * @param e The event data. + */ + + virtual void handleClickEvent(const NXWidgets::CWidgetEventArgs &e); + + /** + * Handle a mouse double-click event. + * + * @param e The event data. + */ + + virtual void handleDoubleClickEvent(const NXWidgets::CWidgetEventArgs &e); + + /** + * Handle a mouse button release event that occurred within the bounds of + * the source widget. + * @param e The event data. + */ + + virtual void handleReleaseEvent(const NXWidgets::CWidgetEventArgs &e); + + /** + * Handle a mouse button release event that occurred outside the bounds of + * the source widget. + * + * @param e The event data. + */ + + virtual void handleReleaseOutsideEvent(const NXWidgets::CWidgetEventArgs &e); + }; +} + +#endif // ___APPS_INCLUDE_GRAPHICS_TWM4NX_CICONWIDGET_HXX