home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OGraphicsButton.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  9KB  |  305 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OGraphicsButton.cpp
  5.  
  6.  
  7. /*
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  14.  *    endorse or promote products derived from this software
  15.  *    without specific prior written permission.
  16.  * 3. See OCL.INF for a detailed copyright notice.
  17.  *
  18.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  19.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28.  * SUCH DAMAGE.
  29.  */
  30.  
  31.  
  32. // $Header: W:/Projects/OCL/Source/rcs/OGraphicsButton.cpp 1.50 1996/08/11 23:49:16 B.STEIN Release $
  33.  
  34. #define __OCL_SOURCE__
  35.  
  36. #define OINCL_OSTRING
  37. #define OINCL_BASE
  38.  
  39. #include <ocl.hpp>
  40. #include <OGraphicsButton.hpp>
  41. #include <OMessage.hpp>
  42.  
  43. OGraphicsButton::OGraphicsButton (ULONG    id,
  44.                                   HWND     Parent,
  45.                                   pOIcon   ico,  
  46.                                   PSZ      help,  
  47.                                   ULONG    x,
  48.                                   ULONG    y,
  49.                                   ULONG    cx,
  50.                                   ULONG    cy)
  51.   : OUserButton(id, Parent, x, y, cx, cy),
  52.     helpText(help),
  53.     pIco(ico),
  54.     imageType(OGraphicsButton::useIcon),
  55.     isEnabled(TRUE) 
  56.   {}
  57.  
  58. OGraphicsButton::OGraphicsButton (ULONG    id,
  59.                                   HWND     Parent,
  60.                                   pOBitmap bmp,    
  61.                                   PSZ      help,  
  62.                                   ULONG    x,
  63.                                   ULONG    y,
  64.                                   ULONG    cx,
  65.                                   ULONG    cy)
  66.   : OUserButton(id, Parent, x, y, cx, cy),
  67.     helpText(help),
  68.     pBmp(bmp),
  69.     imageType(OGraphicsButton::useBitmap),
  70.     isEnabled(TRUE) 
  71.   {}
  72.  
  73. OGraphicsButton::OGraphicsButton (ULONG    id,
  74.                                   OFrame&  Parent,
  75.                                   pOIcon   ico,    
  76.                                   PSZ      help,  
  77.                                   ULONG    x,
  78.                                   ULONG    y,
  79.                                   ULONG    cx,
  80.                                   ULONG    cy)
  81.   : OUserButton(id, Parent, x, y, cx, cy),
  82.     helpText(help),
  83.     pIco(ico),
  84.     imageType(OGraphicsButton::useIcon),
  85.     isEnabled(TRUE) 
  86.   {}
  87.  
  88. OGraphicsButton::OGraphicsButton (ULONG    id,
  89.                                   OFrame&  Parent,
  90.                                   pOBitmap bmp,    
  91.                                   PSZ      help,  
  92.                                   ULONG    x,
  93.                                   ULONG    y,
  94.                                   ULONG    cx,
  95.                                   ULONG    cy)
  96.   : OUserButton(id, Parent, x, y, cx, cy),
  97.     helpText(help),
  98.     pBmp(bmp),
  99.     imageType(OGraphicsButton::useBitmap), 
  100.     isEnabled(TRUE) 
  101.   {}
  102.  
  103.  
  104. OGraphicsButton::OGraphicsButton (ULONG    id,
  105.                                   pOFrame  Parent,
  106.                                   pOIcon   ico,    
  107.                                   PSZ      help,  
  108.                                   ULONG    x,
  109.                                   ULONG    y,
  110.                                   ULONG    cx,
  111.                                   ULONG    cy)
  112.   : OUserButton(id, Parent, x, y, cx, cy),
  113.     helpText(help),
  114.     pIco(ico),
  115.     imageType(OGraphicsButton::useIcon),
  116.     isEnabled(TRUE) 
  117.   {}
  118.  
  119. OGraphicsButton::OGraphicsButton (ULONG    id,
  120.                                   pOFrame  Parent,
  121.                                   pOBitmap bmp,    
  122.                                   PSZ      help,  
  123.                                   ULONG    x,
  124.                                   ULONG    y,
  125.                                   ULONG    cx,
  126.                                   ULONG    cy)
  127.   : OUserButton(id, Parent, x, y, cx, cy),
  128.     helpText(help),
  129.     pBmp(bmp),
  130.     imageType(OGraphicsButton::useBitmap),
  131.     isEnabled(TRUE) 
  132.   {}
  133.  
  134.  
  135. OGraphicsButton::~OGraphicsButton()
  136.   {}
  137.  
  138.  
  139. PSZ OGraphicsButton::isOfType() const
  140. {
  141.  return("OGraphicsButton");
  142. }
  143.  
  144.  
  145. MRESULT OGraphicsButton::UBDefaultCall(ULONG  msg,  
  146.                                        MPARAM mp1,
  147.                                        MPARAM mp2)
  148. {
  149.  return((MRESULT)PM_ButtonProc(hwnd, msg, mp1, mp2));
  150. }
  151.  
  152.  
  153. OButton& OGraphicsButton::createButton()
  154. {
  155.  CLASSINFO clsi ;
  156.    
  157.  WinQueryClassInfo(OApp::current().anchor(),
  158.                    WC_BUTTON, &clsi);
  159.  
  160.  PM_ButtonProc = clsi.pfnWindowProc;
  161.  
  162.  if (!WinRegisterClass(OApp::current().anchor(),
  163.                        "OGraphicsButton",
  164.                        (PFNWP) OGraphicsButtonProc,
  165.                        0,
  166.                        clsi.cbWindowData + sizeof (PVOID)))
  167.    throw OPMException(OCL::error(60), 0, OException::fatal);
  168.  
  169.  hwnd = WinCreateWindow(parent, "OGraphicsButton",
  170.                         "", BS_PUSHBUTTON | BS_USERBUTTON | BS_NOPOINTERFOCUS | WS_VISIBLE,
  171.                         sizepos.x, sizepos.y, sizepos.cx, sizepos.cy,
  172.                         owner, HWND_TOP, res, this, NULL);
  173.  
  174.  if (!hwnd)
  175.    throw OPMException(OCL::error(61), 0, OException::fatal);
  176.  
  177.  return(*this);
  178. }
  179.  
  180.  
  181. OGraphicsButton& OGraphicsButton::drawButton(HPS hPS)
  182. {
  183.  POINTL ptl;
  184.  
  185.  WinQueryWindowRect(hwnd, &rcl);
  186.  GpiCreateLogColorTable(hPS, LCOL_RESET, LCOLF_RGB, 0L, 0L, NULL);
  187.  WinFillRect(hPS, &rcl, SYSCLR_BUTTONMIDDLE);
  188.  if (pushState)
  189.    WinDrawBorder(hPS, &rcl, 2, 2, 0, 0, DB_PATCOPY | DB_DEPRESSED);
  190.  else
  191.    WinDrawBorder(hPS, &rcl, 2, 2, 0, 0, DB_PATCOPY | DB_RAISED);
  192.  
  193.  WinQueryWindowRect(hwnd, &rcl);
  194.  if (imageType == OGraphicsButton::useIcon)
  195.   {
  196.    ptl.x = (rcl.xRight - rcl.xLeft - pIco->width  + pushState * 3)  / 2;
  197.    ptl.y = (rcl.yTop - rcl.yBottom - pIco->height - pushState * 3) / 2;
  198.    WinDrawPointer(hPS, ptl.x, ptl.y, pIco->hptr, isEnabled ? DP_NORMAL : DP_HALFTONED);
  199.   }
  200.  else
  201.   {
  202.    ptl.x = (rcl.xRight - rcl.xLeft - pBmp->width  + pushState * 3)  / 2;
  203.    ptl.y = (rcl.yTop - rcl.yBottom - pBmp->height - pushState * 3) / 2;
  204.    WinDrawBitmap(hPS, pBmp->hbm, NULL, &ptl, 0, 0, isEnabled ? DBM_NORMAL : DBM_HALFTONE);
  205.   }
  206.  return(*this);
  207. }
  208.  
  209.  
  210. BOOL OGraphicsButton::OCommand(ULONG msg, MPARAM mp1, MPARAM mp2)
  211. {
  212.  switch(msg)
  213.   {
  214.    case WM_CREATE:
  215.      pushState = FALSE;
  216.      break;
  217.  
  218.    case BM_SETHILITE:
  219.      WinSendMsg(parent, WM_CONTROL,
  220.                 MPFROM2SHORT(OGRBTN_SETHILITE, res), NULL);
  221.      pushState = ((BOOL)SHORT1FROMMP(mp1));
  222.      hps = WinGetPS(hwnd);
  223.      drawButton(hps);
  224.      WinReleasePS(hps);
  225.      return(FALSE);
  226.  
  227.    case WM_PRESPARAMCHANGED:
  228.      getFont(); 
  229.      WinSendMsg(parent, WM_CONTROL, MPFROM2SHORT(msg, res), MPFROMP(this));
  230.      break;
  231.  
  232.    case WM_WINDOWENTER:
  233.      WinSendMsg(parent, WM_CONTROL,
  234.                 MPFROM2SHORT(OGRBTN_WINDOWENTER, res),
  235.                 MPFROMP(helpText.getText()));
  236.      break;
  237.  
  238.    case WM_WINDOWLEAVE:
  239.      WinSendMsg(parent, WM_CONTROL,
  240.                 MPFROM2SHORT(OGRBTN_WINDOWLEAVE, res),
  241.                 MPFROMP((PSZ)""));
  242.      break;
  243.  
  244.    case WM_ENABLE:
  245.      isEnabled = (BOOL)SHORT1FROMMP(mp1);
  246.      return(FALSE);
  247.  
  248.    case WM_PAINT:
  249.      hps = WinBeginPaint(hwnd, 0, NULL);
  250.      drawButton(hps);
  251.      WinEndPaint(hps);
  252.      break;
  253.  
  254.    default:
  255.      return(FALSE);
  256.   }
  257.  return(TRUE); 
  258. }
  259.  
  260.  
  261.  
  262. // The following function is strictly for internal use only!
  263.  
  264.  
  265. MRESULT EXPENTRY OGraphicsButtonProc(HWND hwnd,
  266.                                      ULONG msg,
  267.                                      MPARAM mp1,
  268.                                      MPARAM mp2)
  269. {
  270.  pOGraphicsButton ub_current = (pOGraphicsButton) WinQueryWindowPtr(hwnd, QWL_USER);
  271.  
  272.  if (ub_current)
  273.    ub_current->hwnd = hwnd;
  274.  
  275.  switch(msg)
  276.   {
  277.    case WM_CREATE:
  278.      if (!mp1)
  279.        return(MRESULT(TRUE));
  280.      ub_current = (pOGraphicsButton) mp1;
  281.      WinSetWindowPtr(hwnd, QWL_USER, ub_current);
  282.      ub_current->hwnd = hwnd;
  283.      ub_current->UBDefaultCall(msg, mp1, mp2);
  284.      ub_current->OCommand(msg, mp1, mp2);
  285.      break ;
  286.  
  287.    case WM_DESTROY:
  288.      WinSetWindowPtr(hwnd, QWL_USER, NULL);
  289.      if(ub_current) {
  290.        ub_current->OCommand(msg, mp1, mp2); 
  291.        return(ub_current->UBDefaultCall(msg, mp1, mp2)); }
  292.      break;
  293.  
  294.    default:
  295.      if ((ub_current) &&
  296.         (!ub_current->OCommand(msg, mp1, mp2)))
  297.        return(ub_current->UBDefaultCall(msg, mp1, mp2));
  298.      break;
  299.   }
  300.  return((MRESULT)FALSE);
  301. }
  302.  
  303.  
  304. // end of source
  305.