apps/graphics/twm4nx: Add an IApplicationFactory interface.

This commit is contained in:
Gregory Nutt 2019-05-11 15:48:46 -06:00
parent c6802efc38
commit 77d10f9028
3 changed files with 355 additions and 344 deletions

View File

@ -125,15 +125,15 @@ using namespace Twm4Nx;
/**
* CNxTerm constructor
*
* @param window. The application window
* @param cwin. The application window
*/
CNxTerm::CNxTerm(CTwm4Nx *twm4nx, CApplicationWindow *window)
CNxTerm::CNxTerm(CTwm4Nx *twm4nx, CWindow *cwin)
{
// Save the constructor data
m_twm4nx = twm4nx;
m_window = window;
m_appWindow = cwin;
// The NxTerm is not runing
@ -143,17 +143,15 @@ CNxTerm::CNxTerm(CTwm4Nx *twm4nx, CApplicationWindow *window)
// Add our personalized window label
NXWidgets::CNxString myName = getName();
window->setWindowLabel(myName);
cwin->setWindowLabel(myName);
// Add our callbacks with the application window
window->registerCallbacks(static_cast<IApplicationCallback *>(this));
cwin->registerCallbacks(static_cast<IApplicationCallback *>(this));
}
/**
* CNxTerm destructor
*
* @param window. The application window
*/
CNxTerm::~CNxTerm(void)
@ -166,17 +164,17 @@ CNxTerm::~CNxTerm(void)
// Although we didn't create it, we are responsible for deleting the
// application window
delete m_window;
delete m_appWindow;
}
/**
* Each implementation of IApplication must provide a method to recover
* the contained CApplicationWindow instance.
* the contained CWindow instance.
*/
IApplicationWindow *CNxTerm::getWindow(void) const
{
return static_cast<IApplicationWindow*>(m_window);
return static_cast<IApplicationWindow*>(m_appWindow);
}
/**
@ -223,11 +221,11 @@ bool CNxTerm::run(void)
// Recover the NXTK window instance contained in the application window
NXWidgets::INxWindow *window = m_window->getWindow();
NXWidgets::INxWindow *inxWindow = m_appWindow->getWindow();
// Get the widget control associated with the NXTK window
NXWidgets::CWidgetControl *control = window->getWidgetControl();
NXWidgets::CWidgetControl *control = inxWindow->getWidgetControl();
// Get the window handle from the widget control
@ -245,7 +243,7 @@ bool CNxTerm::run(void)
// Get the size of the window
(void)window->getSize(&g_nxtermvars.wndo.wsize);
(void)inxWindow->getSize(&g_nxtermvars.wndo.wsize);
// Start the NxTerm task
@ -283,7 +281,7 @@ bool CNxTerm::run(void)
DEBUGASSERT(g_nxtermvars.nxterm != 0);
#ifdef CONFIG_NXTERM_NXKBDIN
window->redirectNxTerm(g_nxtermvars.nxterm);
inxWindow->redirectNxTerm(g_nxtermvars.nxterm);
#endif
// Save the handle to use in the stop method
@ -336,8 +334,8 @@ void CNxTerm::stop(void)
// Re-store NX keyboard input routing
#ifdef CONFIG_NXTERM_NXKBDIN
NXWidgets::INxWindow *window = m_window->getWindow();
window->redirectNxTerm((NXTERM)0);
NXWidgets::INxWindow *inxWindow = m_appWindow->getWindow();
inxWindow->redirectNxTerm((NXTERM)0);
#endif
// Unlink the NxTerm driver
@ -364,7 +362,7 @@ void CNxTerm::destroy(void)
{
// Block any further window messages
m_window->block(this);
m_appWindow->block(this);
// Make sure that the application is stopped
@ -391,12 +389,12 @@ void CNxTerm::redraw(void)
{
// Recover the NXTK window instance contained in the application window
NXWidgets::INxWindow *window = m_window->getWindow();
NXWidgets::INxWindow *inxWindow = m_appWindow->getWindow();
// Get the size of the window
struct nxgl_size_s windowSize;
(void)window->getSize(&windowSize);
(void)inxWindow->getSize(&windowSize);
// Redraw the entire NxTerm window
@ -422,7 +420,7 @@ void CNxTerm::redraw(void)
bool CNxTerm::isFullScreen(void) const
{
return m_window->isFullScreen();
return m_appWindow->isFullScreen();
}
/**
@ -546,6 +544,28 @@ errout:
return EXIT_FAILURE;
}
/**
* Handle Twm4Nx events. This overrides a method from CTwm4NXEvent
*
* @param eventmsg. The received NxWidget WINDOW event message.
* @return True if the message was properly handled. false is
* return on any failure.
*/
bool CNxTerm::event(FAR struct SEventMsg *eventmsg)
{
bool success = true;
switch (eventmsg->eventID)
{
default:
success = false;
break;
}
return success;
}
/**
* This is the NxTerm task exit handler. It registered with on_exit()
* and called automatically when the nxterm task exits.
@ -590,87 +610,36 @@ void CNxTerm::close(void)
m_twm4nx->stopApplication(static_cast<IApplication*>(this));
}
/////////////////////////////////////////////////////////////////////////////
// CNxTermFactory Method Implementations
/////////////////////////////////////////////////////////////////////////////
/**
* CNxTermFactory Constructor
* CNxTermFactory Initializer. Performs parts of the instance
* construction that may fail. In this implemenation, it will
* initialize the NSH library and register an menu item in the
* Main Menu.
*
* @param twm4nx. The twm4nx instance used to terminate the console
* @param twm4nx. The Twm4Nx session instance
*/
CNxTermFactory::CNxTermFactory(CTwm4Nx *twm4nx)
bool CNxTermFactory::initialize(FAR CTwm4Nx *twm4nx)
{
m_twm4nx = twm4nx;
// Initialize the NSH library
if (!nshlibInitialize())
{
twmerr("ERROR: Failed to initialize the NSH library\n");
return false;
}
/**
* CNxTermFactory Initializer. Performs parts of the instance construction
* that may fail
*
* @param twm4nx. The twm4nx instance used to terminate the console
*/
bool CNxTermFactory::initialize(void)
{
// Register an entry with the Main menu. When selected, this will
// Case the start
FAR CMainMenu *cmain = m_twm4nx->getMainMenu();
FAR CMainMenu *cmain = twm4nx->getMainMenu();
return cmain->addApplication(this);
}
/**
* Create and start a new instance of an CNxTerm.
*/
bool *CNxTermFactory::startFunction(CTwm4Nx *twm4n)
{
// Call CTaskBar::openFullScreenWindow to create a full screen window for
// the NxTerm application
CApplicationWindow *window = m_twm4nx->openApplicationWindow();
if (!window)
{
twmerr("ERROR: Failed to create CApplicationWindow\n");
return (IApplication *)0;
}
// Open the window (it is hot in here)
if (!window->open())
{
twmerr("ERROR: Failed to open CApplicationWindow\n");
delete window;
return (IApplication *)0;
}
// Instantiate the application, providing the window to the application's
// constructor
CNxTerm *nxterm = new CNxTerm(m_twm4nx, window);
if (!nxterm)
{
twmerr("ERROR: Failed to instantiate CNxTerm\n");
delete window;
return (IApplication *)0;
}
return true;
}
/**
* Get the icon associated with the application
*
* @return An instance if IBitmap that may be used to rend the
* application's icon. This is an new IBitmap instance that must
* be deleted by the caller when it is no long needed.
*/
NXWidgets::IBitmap *CNxTermFactory::getIcon(void)
{
NXWidgets::CRlePaletteBitmap *bitmap =
new NXWidgets::CRlePaletteBitmap(&CONFIG_TWM4NX_NXTERM_ICON);
return bitmap;
}
/**
* One time NSH initialization. This function must be called exactly
* once during the boot-up sequence to initialize the NSH library.
@ -678,7 +647,7 @@ NXWidgets::IBitmap *CNxTermFactory::getIcon(void)
* @return True on successful initialization
*/
bool Twm4Nx::nshlibInitialize(void)
bool CNxTermFactory::nshlibInitialize(void)
{
// Initialize the global data structure
@ -705,3 +674,44 @@ bool Twm4Nx::nshlibInitialize(void)
return true;
}
/**
* Create and start a new instance of CNxTerm.
*
* @param twm4nx. The Twm4Nx session instance
*/
bool CNxTermFactory::startFunction(FAR CTwm4Nx *twm4nx)
{
// Call CTaskBar::openFullScreenWindow to create a full screen window for
// the NxTerm application
FAR CWindowFactory *factory = twm4nx->getWindowFactory();
CWindow *cwin = factory->createWindow(twm4nx);
if (!cwin )
{
twmerr("ERROR: Failed to create CWindow\n");
return false;
}
// Initialize the window
if (!cwin->initialize(..arguments...))
{
twmerr("ERROR: Failed to open CWindow\n");
delete cwin;
return false;
}
// Instantiate the application, providing the session and the window to
// the application's constructor
CNxTerm *nxterm = new CNxTerm(twm4nx, cwin);
if (!nxterm)
{
twmerr("ERROR: Failed to instantiate CNxTerm\n");
delete cwin;
return (IApplication *)0;
}
return true;
}

View File

@ -48,6 +48,7 @@
#include <nuttx/nx/nxterm.h>
#include "graphics/twm4nx/ctwm4nx.hxx"
#include "graphics/twm4nx/ctwm4nxevent.hxx"
#include "graphics/twm4nx/iapplication.hxx"
/////////////////////////////////////////////////////////////////////////////
@ -56,23 +57,14 @@
namespace Twm4Nx
{
/**
* One time NSH initialization. This function must be called exactly
* once during the boot-up sequence to initialize the NSH library.
*
* @return True on successful initialization
*/
bool nshlibInitialize(void);
/**
* This class implements the NxTerm application.
*/
class CNxTerm
class CNxTerm : public CTwm4NxEvent
{
private:
CTaskbar *m_twm4nx; /**< Reference to the "parent" twm4nx */
CTaskbar *m_twm4nx; /**< Reference to the Twm4Nx session instance */
CApplicationWindow *m_window; /**< Reference to the application window */
NXTERM m_nxterm; /**< NxTerm handle */
pid_t m_pid; /**< Task ID of the NxTerm thread */
@ -85,6 +77,16 @@ namespace Twm4Nx
static int nxterm(int argc, char *argv[]);
/**
* Handle Twm4Nx events. This overrides a method from CTwm4NXEvent
*
* @param eventmsg. The received NxWidget WINDOW event message.
* @return True if the message was properly handled. false is
* return on any failure.
*/
bool event(FAR struct SEventMsg *eventmsg);
/**
* This is the NxTerm task exit handler. It is registered with on_exit()
* and called automatically when the nxterm task exits.
@ -158,7 +160,23 @@ namespace Twm4Nx
class CNxTermFactory : public IApplication, public IApplicationFactory
{
private:
CTaskbar *m_twm4nx; /**< The twm4nx */
/**
* One time NSH initialization. This function must be called exactly
* once during the boot-up sequence to initialize the NSH library.
*
* @return True on successful initialization
*/
bool nshlibInitialize(void);
/**
* Create and start a new instance of an CNxTerm.
*
* @param twm4nx. The Twm4Nx session instance
*/
static bool startFunction(CTwm4Nx *twm4n);
/**
* Return the Main Menu item string. This overrides the method from
@ -169,7 +187,7 @@ namespace Twm4Nx
inline NXWidgets::CNxString getName(void)
{
return NXWidgets::CNxString("NxTerm");
return NXWidgets::CNxString("NuttShell");
}
/**
@ -218,87 +236,36 @@ namespace Twm4Nx
}
public:
/**
* CNxTerm constructor
*
* @param window. The application window
*
* @param twm4nx. A pointer to the parent task bar instance
* @param window. The window to be used by this application.
*/
CNxTerm(CTaskbar *twm4nx, CApplicationWindow *window);
/**
* CNxTerm destructor
*/
~CNxTerm(void);
/**
* Each implementation of IApplication must provide a method to recover
* the contained CApplicationWindow instance.
*/
IApplicationWindow *getWindow(void) const;
/**
* Get the icon associated with the application
*
* @return An instance if IBitmap that may be used to rend the
* application's icon. This is an new IBitmap instance that must
* be deleted by the caller when it is no long needed.
*/
NXWidgets::IBitmap *getIcon(void);
/**
* Get the name string associated with the application
*
* @return A copy if CNxString that contains the name of the application.
*/
NXWidgets::CNxString getName(void);
public:
/**
* CNxTermFactory Constructor
*
* @param twm4nx. The twm4nx instance used to terminate calibration
*/
CNxTermFactory(CTaskbar *twm4nx);
inline CNxTermFactory(void)
{
}
/**
* CNxTermFactory Destructor
*/
inline ~CNxTermFactory(void) { }
inline ~CNxTermFactory(void)
{
// REVISIT: Would need to remove Main Menu item
}
/**
* CNxTermFactory Initializer. Performs parts of the instance
* construction that may fail
* construction that may fail. In this implemenation, it will
* initialize the NSH library and register an menu item in the
* Main Menu.
*
* @param twm4nx. The twm4nx instance used to terminate the console
* @param twm4nx. The Twm4Nx session instance
*/
bool CNxTermFactory::initialize(void);
/**
* Create a new instance of an CNxTerm (as IApplication).
*/
IApplication *create(void);
/**
* Get the icon associated with the application
*
* @return An instance if IBitmap that may be used to rend the
* application's icon. This is an new IBitmap instance that must
* be deleted by the caller when it is no long needed.
*/
NXWidgets::IBitmap *getIcon(void);
bool CNxTermFactory::initialize(FAR CTwm4Nx *twm4nx);
};
}

View File

@ -79,6 +79,7 @@ namespace Twm4Nx
class IApplication
{
public:
/**
* A virtual destructor is required in order to override the
* IApplication destructor. We do this because if we delete
@ -219,6 +220,39 @@ namespace Twm4Nx
virtual uint16_t getEvent(void) = 0;
};
/**
* IAppliation Factory provides a standard interface for adding multiple
* copies of an application.
*/
class IApplicationFactory
{
public:
/**
* A virtual destructor is required in order to override the
* IApplication destructor. We do this because if we delete
* IApplication, we want the destructor of the class that inherits from
* IApplication to run, not this one.
*/
virtual ~IApplicationFactory(void)
{
}
/**
* CNxTermFactory Initializer. Performs parts of the instance
* construction that may fail. Aside from general initialization, the
* main responsibility of this function is to register the factory
* IApplication interface with the Main Menu by calling the
* CMainMenu::addApplication() method.
*
* @param twm4nx. The Twm4Nx session instance.
*/
virtual bool initialize(FAR CTwm4Nx *twm4nx) = 0;
};
}
#endif // __APPS_INCLUDE_GRAPHICS_TWM4NX_IAPPLICATION_HXX