Work Place Shell Programming - Part 2

Written by Chris Palchak

Overview
This, the second article in this series, will show you:
 * the code that needs to be added to the C++ program that was generated in the first article,
 * how to write a settings page dialogs in C++.

Adding the Necessary Code to the Generated C++ Stub
In the stub [chFile.cpp ChFile.cpp] file there are two important variables of which you need to be aware:
 * somSelf
 * points to the object on which the program is operating. In a SOM generated C++ program, use somSelf to call member functions (e.g., somSelf->wpInsertSettingsPage(hwndNotebook, &pageinfo)


 * somThis
 * points to the data within the object. In a SOM generated C++ program, use somThis to reference data members (e.g., somThis->lastYYYY = 1997).

In addition to the generated function headers, the following statements are generated for each function:

All other code is added manually.

Writing a Settings Page Dialog Class
The classes created to handle these settings pages are all so similar that only one of the three dialog classes will be included in this article. All settings classes include the same functions but are coded to handle the specific fields on their individual settings pages.

StockDlg.hpp listing
class ChartFile; // ChFile.xih is included in StockDlg.cpp //********************************************************** // Stock Settings Page Dialog //********************************************************** class StockDlg : public IFrameWindow , public ICommandHandler { public: ~StockDlg; StockDlg (ChartFile *PChartFile,             const IWindowHandle& Hwnd); private: IEntryField stockSymbol, lastMonth, lastDay, lastYear; IComboBox   exchange; ChartFile  *pChartFile;   // WPS object Environment *pSomEnv;     // SOM environment // virtual function overrides Boolean command(ICommandEvent& cmdevt); Boolean dispatchHandlerEvent(IEvent& evt); // custom functions void saveState; void restoreState; };
 * 1) ifndef STOCKDLG_HPP
 * 2) define STOCKDLG_HPP
 * 3) include 
 * 4) include 
 * 5) include 
 * 1) include    // Required SOM definitions
 * 1) include      // The basics
 * 2) include    // IFrameWindow
 * 3) include   // ICommandHandler
 * 4) include  // IEntryField
 * 5) include   // IString
 * 6) include  // IComboBox
 * 1) endif

StockDlg.cpp listing
//--- Destructor --- // Remove this program from the list of programs handling // events for this frame window. //-- StockDlg::~StockDlg {   ICommandHandler::stopHandlingEventsFor(this); } //--- Contructor --- // // Note that the frame window is constructed from the PM // window handle. This tells IFrameWindow that it is a // wrapper for a PM generated Frame Window. //-- StockDlg::StockDlg (ChartFile *PChartFile,                   const IWindowHandle& Hwnd) : IFrameWindow(Hwnd) , pChartFile(PChartFile) , stockSymbol(DID_TICKER_SYMBOL, this) , lastMonth(DID_MM, this) , lastDay(DID_DD, this) , lastYear(DID_YYYY, this) , exchange(DID_EXCHANGE, this) { // Limit the characters that can be entered in each field lastMonth.setLimit(2); lastDay.setLimit(2); lastYear.setLimit(4); stockSymbol.setLimit(8); // Populate the "Exchange" drop-down box exchange.setText(" "); exchange.addAsLast("NYSE"); exchange.addAsLast("AMEX"); exchange.addAsLast("NASDAQ"); exchange.addAsLast("INDEX"); // Save the SOM Environment. It is used through out this // program. pSomEnv = somGetGlobalEnvironment; // Set the fields on the screen to their current values. restoreState; // Put this program on top of the list of programs handling // events for this window. ICommandHandler::handleEventsFor(this); } //-- // Override ICommandHandler::command to know when the user // clicks on the UNDO button on this settings page. //-- Boolean StockDlg::command(ICommandEvent& cmdEvent) {   switch (cmdEvent.commandId) {          case DID_UNDO: restoreState; return true; default: break; }   return false; } //-- // Override IHandler::dispatchHandlerEvent to intercept PM // messages so that when the settings notebook is closed, // the current state of the object can be saved. //-- Boolean StockDlg::dispatchHandlerEvent(IEvent& evt) {  if (evt.eventId == WM_DESTROY) saveState; return ICommandHandler::dispatchHandlerEvent(evt); } //- // Set the screen fields to the values stored in the // ChartFile object //- void StockDlg::restoreState {  IString tStr; short tShort; tStr = pChartFile->getStockSymbol(pSomEnv); stockSymbol.setText(tStr); tStr = pChartFile->getExchange(pSomEnv); exchange.setText(tStr); tShort = pChartFile->getLastDD(pSomEnv); tStr = IString(tShort); lastDay.setText(tStr); tShort = pChartFile->getLastMM(pSomEnv); tStr = IString(tShort); lastMonth.setText(tStr); tShort = pChartFile->getLastYYYY(pSomEnv); tStr = IString(tShort); lastYear.setText(tStr); stockSymbol.setFocus; return; } //- // Copy data from the screen to the WPS object //- void StockDlg::saveState {    IString tStr; short  tShort; tStr = stockSymbol.text; pChartFile->setStockSymbol(pSomEnv, tStr); tStr = exchange.text; pChartFile->setExchange(pSomEnv, tStr); tShort = lastDay.text.asInt; pChartFile->setLastDD(pSomEnv, tShort); tShort = lastMonth.text.asInt; pChartFile->setLastMM(pSomEnv, tShort); tShort = lastYear.text.asInt; pChartFile->setLastYYYY(pSomEnv, tShort); // Tell the ChartFile object that stock data has been // updated and call wpSaveDeferred to save this data // during idle or shut down time. pChartFile->setStockDataSaved(pSomEnv, 1); pChartFile->wpSaveDeferred; return; } //-Create a pop-up menu resource // // This is how the pop-up menu items that are added to the // object's pop-up menu look in ChFile.rc. Note that the old // dialog editor (not the visual builder) is used to create // the settings page's dialog window. //--
 * 1) include 
 * 2) include 
 * 1) include "ChFile.xih"
 * 2) include "stockdlg.h"
 * 3) include "stockdlg.hpp"

ChFile.rc listing
ICON WPICONID_CHFILE chfile.ico /* your icon file      */ MENU WPMENUID_CHFILE BEGIN MENUITEM "~Bar Chart", WPMENUID_CHFILE_BARCHART MENUITEM "~P&F Chart", WPMENUID_CHFILE_PFCHART END rcinclude "stockdlg.DLG" /* generated with dlgedit.exe */
 * 1) include 
 * 2) define WINWORKPLACE
 * 3) include 
 * 4) include "chfile.h"             /* listed below        */
 * 5) include "stockdlg.h"           /* dialog include file */

ChFile.h

 * 1) ifndef CHFILE_H
 * 2) define CHFILE_H
 * 3) define WPICONID_CHFILE         (WPMENUID_USER + 100)
 * 4) define WPMENUID_CHFILE         (WPMENUID_USER + 101)
 * 5) define WPMENUID_CHFILE_BARCHART (WPMENUID_USER + 102)
 * 6) define WPMENUID_CHFILE_PFCHART (WPMENUID_USER + 103)
 * 1) define WPOPEN_BARCHART         (OPEN_USER     + 1)
 * 2) endif

That's all until next month.