home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctlcpp.zip / IABSWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  10.1 KB  |  291 lines

  1. /***************************************************************/
  2. /* Filename: iabswin.cpp                                       */
  3. /*                                                             */
  4. /* Purpose: Definition of classes IAbstractWindow and          */
  5. /*          IAbsWinHandler.                                    */
  6. /*                                                             */
  7. /* Program name: cppctl.exe     Title: C++ PM Control Test App */
  8. /* OS/2 Developer Magazine, Issue: Sept. '94, page             */
  9. /* Article title: Writing OS/2 PM Controls in C++              */
  10. /* Authors: Eric Snell and Lori Ruffing                        */
  11. /*                                                             */
  12. /* Description: This example shows how to implement an OS/2 PM */
  13. /*              control window in C++.                         */
  14. /*                                                             */
  15. /* Program Requirements: This example requires the IBM C Set++ */
  16. /*                       compiler and libraries.               */
  17. /***************************************************************/
  18.  
  19. extern "C"
  20.    {
  21.    #define INCL_WINWINDOWMGR
  22.    #define INCL_WINPOINTERS
  23.    #define INCL_WINFRAMEMGR
  24.    #include "os2.h"
  25.    }
  26.  
  27. #ifndef _ITHREAD_
  28.    #include <ithread.hpp>
  29. #endif
  30.  
  31. #include "iabswin.hpp"
  32.  
  33. Boolean       IAbstractWindow::hasBeenRegistered = false;
  34. unsigned long IAbstractWindow::minWindowData     = 4;
  35.  
  36. //----------------------------------------------------------------------------
  37. // Method: IAbstractWindow::IAbstractWindow
  38. //
  39. // Description: Construct an IAbstractWindow from a class name 'winClassName',
  40. //              class styles 'windowClassStyles', amount of extra memory
  41. //              allocated per window 'extraDataBytes', window 'id', 'parent'
  42. //              window, 'owner' window, 'initial' window rectangle, and
  43. //              window 'style'.
  44. //
  45. // Notes: Register the PM class, throw an exception if registration fails.
  46. //----------------------------------------------------------------------------
  47. IAbstractWindow::IAbstractWindow(const IString& winClassName,
  48.                                  unsigned long windowClassStyles,
  49.                                  unsigned long extraDataBytes,
  50.                                  unsigned long id,
  51.                                  IWindow* parent,
  52.                                  IWindow* owner,
  53.                                  const IRectangle& initial,
  54.                                  unsigned long style)
  55.    : clsName(winClassName),
  56.      clsStyle(windowClassStyles),
  57.      exData(extraDataBytes + minWindowData)
  58.    {
  59.    if (!hasBeenRegistered)
  60.       {
  61.       // If class registration fails, throw an exception because
  62.       // we can't survive
  63.       if (WinRegisterClass(IThread::current().anchorBlock(),
  64.                            clsName,
  65.                            WinDefWindowProc,
  66.                            clsStyle,
  67.                            exData))
  68.          {
  69.          hasBeenRegistered = true;
  70.          }
  71.       else
  72.          {
  73.          // You can throw an exception here.
  74.          }
  75.  
  76.       }
  77.  
  78.    // See if the class has already been registered or
  79.    // was registered above.
  80.    if (hasBeenRegistered)
  81.       {
  82.       // already registered, just create the window
  83.       IWindowHandle handle(create(id,
  84.                                   0,
  85.                                   style,
  86.                                   winClassName,
  87.                                   parent->handle(),
  88.                                   owner->handle(),
  89.                                   initial,
  90.                                   0,
  91.                                   0));
  92.  
  93.       if (handle.isValid())
  94.          {
  95.          // successfully created the window, attach our handlers.
  96.          IWindow::startHandlingEventsFor(handle);
  97.  
  98.          IResizeHandler::handleEventsFor(this);
  99.          IPaintHandler::handleEventsFor(this);
  100.          IMouseMoveHandler::handleEventsFor(this);
  101.  
  102.          IAbsWinHandler::handleEventsFor(this);
  103.          }
  104.       else
  105.          {
  106.          // You can throw an exception here.
  107.          }
  108.       }
  109.    }
  110.  
  111. //----------------------------------------------------------------------------
  112. // Method: IAbstractWindow::~IAbstractWindow
  113. //
  114. // Description: Destructor for IAbstractWindow.
  115. //
  116. // Notes: Empty destructor. May place trace information here to detect
  117. //        destruction while debugging.
  118. //----------------------------------------------------------------------------
  119. IAbstractWindow::~IAbstractWindow()
  120.    {
  121.    }
  122.  
  123. //----------------------------------------------------------------------------
  124. // Method: IAbstractWindow::windowClass
  125. //
  126. // Description: 'windowClass' returns the window class name specified
  127. //              during contruction.
  128. //----------------------------------------------------------------------------
  129. IString IAbstractWindow::windowClass() const
  130.    {
  131.    return IString(clsName);
  132.    }
  133.  
  134. //----------------------------------------------------------------------------
  135. // Method: IAbstractWindow::windowClassStyle
  136. //
  137. // Description: 'windowClassStyle' returns the window class style bits
  138. //              specified during construction.
  139. //----------------------------------------------------------------------------
  140. unsigned long IAbstractWindow::windowClassStyle() const
  141.    {
  142.    return clsStyle;
  143.    }
  144.  
  145. //----------------------------------------------------------------------------
  146. // Method: IAbstractWindow::windowDataBytes
  147. //
  148. // Description: 'windowDataBytes' returns the number of extra bytes each
  149. //              window of this class has allocated.
  150. //
  151. // Notes: This number is a combination of the amount requested during
  152. //        contruction plus the minimumWindowData().
  153. //----------------------------------------------------------------------------
  154. unsigned long IAbstractWindow::windowDataBytes() const
  155.    {
  156.    return exData;
  157.    }
  158.  
  159. //----------------------------------------------------------------------------
  160. // Method: IAbstractWindow::minimumWindowData
  161. //
  162. // Description: 'minimumWindowData' returns the constant amount of bytes
  163. //              that are allocated for each window of this class. The
  164. //              constant is 4 so that users of the class have access to
  165. //              the offset at QWL_USER.
  166. //----------------------------------------------------------------------------
  167. unsigned long IAbstractWindow::minimumWindowData()
  168.    {
  169.    return minWindowData;
  170.    }
  171.  
  172. //----------------------------------------------------------------------------
  173. // Method: IAbstractWindow::paintWindow
  174. //
  175. // Description: 'paintWindow' is called in response to a paint event,
  176. //              'evt', and simply clears the background.
  177. //----------------------------------------------------------------------------
  178. Boolean IAbstractWindow::paintWindow(IPaintEvent& evt)
  179.    {
  180.    evt.clearBackground();
  181.    return true;
  182.    }
  183.  
  184. //----------------------------------------------------------------------------
  185. // Method: IAbstractWindow::mouseMove
  186. //
  187. // Description: 'mouseMove' is called in response to a mousemove event,
  188. //              'evt'.
  189. //----------------------------------------------------------------------------
  190. Boolean IAbstractWindow::mouseMove(IMouseMoveEvent &evt)
  191.    {
  192.    IEventResult result(owner()->sendEvent(WM_CONTROLPOINTER,
  193.                                           id(),
  194.                                           WinQuerySysPointer(desktopWindow()->handle(),
  195.                                                              SPTR_ARROW,
  196.                                                              FALSE)));
  197.  
  198.    // If the event was received, change the pointer.
  199.    if ((unsigned long)result)
  200.       WinSetPointer(desktopWindow()->handle(), (unsigned long)result);
  201.  
  202.    return true;
  203.    }
  204.  
  205. //----------------------------------------------------------------------------
  206. // Method: IAbstractWindow::windowResize
  207. //
  208. // Description: 'windowResize' is called in response to a resize event,
  209. //              'evt'.
  210. //----------------------------------------------------------------------------
  211. Boolean IAbstractWindow::windowResize(IResizeEvent& evt)
  212.    {
  213.    evt;
  214.    return false;
  215.    }
  216.  
  217. //----------------------------------------------------------------------------
  218. // Method: IAbstractWindow::presParamChanged
  219. //
  220. // Description: 'presParamChanged' is called in response to a
  221. //              WM_PRESPARAMCHANGED message or event, 'evt', and
  222. //              invalidates the window.
  223. //----------------------------------------------------------------------------
  224. Boolean IAbstractWindow::presParamChanged(IEvent& evt)
  225.    {
  226.    evt;
  227.    refresh();
  228.    return true;
  229.    }
  230.  
  231. Boolean IAbstractWindow::eraseBackground(IEvent& evt)
  232.    {
  233.    return false;
  234.    }
  235.  
  236. Boolean IAbstractWindow::gainingFocus(IEvent& evt)
  237.    {
  238.    evt;
  239.    return false;
  240.    }
  241.  
  242. Boolean IAbstractWindow::losingFocus(IEvent& evt)
  243.    {
  244.    evt;
  245.    return false;
  246.    }
  247.  
  248. //------------------- IAbsWinHandler Definition Start ------------------------
  249. IAbsWinHandler::IAbsWinHandler()
  250.    : IHandler()
  251.    {
  252.    }
  253.  
  254. IAbsWinHandler::~IAbsWinHandler()
  255.    {
  256.    }
  257.  
  258. //----------------------------------------------------------------------------
  259. // Method: IAbsWinHandler::dispatchHandlerEvent
  260. //
  261. // Description: 'dispatchHandlerEvent' receives an IEvent 'evt', examines
  262. //              the contents and invokes a virtual function if the event
  263. //              is recognized.
  264. //----------------------------------------------------------------------------
  265. Boolean IAbsWinHandler::dispatchHandlerEvent(IEvent& evt)
  266.    {
  267.    Boolean        fHandled = false;
  268.  
  269.    switch (evt.eventId())
  270.       {
  271.       case WM_PRESPARAMCHANGED:
  272.          fHandled = presParamChanged(evt);
  273.          break;
  274.  
  275.       case WM_ERASEBACKGROUND:
  276.          fHandled = eraseBackground(evt);
  277.          break;
  278.  
  279.       case WM_SETFOCUS:
  280.          if ((unsigned long)evt.parameter2())
  281.             fHandled = gainingFocus(evt);
  282.          else
  283.             fHandled = losingFocus(evt);
  284.          break;
  285.       }
  286.  
  287.    return fHandled;
  288.    }
  289.  
  290. //-------------------- IAbsWinHandler Definition End -------------------------
  291.