Work Place Shell Programming - Part 2

From EDM2
Revision as of 17:11, 27 December 2016 by Ak120 (Talk | contribs)

Jump to: navigation, search
Work Place Shell Programming
Part: 1 / 2 / 3 / 4

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:

  ChartFileGetData(...)      // not modified
  ChartFileMethodDebug(...)  // not modified
  return ...                 // sometimes modified

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

#ifndef STOCKDLG_HPP
#define STOCKDLG_HPP
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h> 

#include <SOMCORBA.H>    // Required SOM definitions 

#include <iapp.hpp>      // The basics
#include <iframe.hpp>    // IFrameWindow
#include <icmdhdr.hpp>   // ICommandHandler
#include <ientryfd.hpp>  // IEntryField
#include <istring.hpp>   // IString
#include <icombobx.hpp>  // IComboBox

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();
};
#endif

StockDlg.cpp listing

#include <idate.hpp>
#include <imsgbox.hpp>

#include "ChFile.xih"
#include "stockdlg.h"
#include "stockdlg.hpp"

//----------------------- 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.
//--

ChFile.rc listing

#include <wpobject.h>
#define WINWORKPLACE
#include <os2.h>
#include "chfile.h"              /* listed below        */
#include "stockdlg.h"            /* dialog include file */ 

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 */

ChFile.h

#ifndef CHFILE_H
#define CHFILE_H
#define WPICONID_CHFILE          (WPMENUID_USER + 100)
#define WPMENUID_CHFILE          (WPMENUID_USER + 101)
#define WPMENUID_CHFILE_BARCHART (WPMENUID_USER + 102)
#define WPMENUID_CHFILE_PFCHART  (WPMENUID_USER + 103)

#define WPOPEN_BARCHART          (OPEN_USER     + 1)
#endif

That's all until next month.