home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / srcos2 / vcmd.cpp < prev    next >
C/C++ Source or Header  |  1999-02-18  |  7KB  |  200 lines

  1. //===============================================================
  2. // vcmd.cxx - vCmd class
  3. //
  4. // Copyright (C) 1995,1996,1997,1998  Bruce E. Wampler
  5. //
  6. // This file is part of the V C++ GUI Framework, and is covered
  7. // under the terms of the GNU Library General Public License,
  8. // Version 2. This library has NO WARRANTY. See the source file
  9. // vapp.cxx for more complete information about license terms.
  10. //===============================================================
  11. #include <v/vos2.h>        // for OS/2 stuff
  12. #include <v/vapp.h>
  13. #include <v/vcmd.h>
  14. #include <v/vcmdprnt.h>
  15. #include <v/vutil.h>
  16.  
  17. //===================>>> vCmd::vCmd <<<=======================
  18.   vCmd::vCmd(vCmdParent* dp, CommandObject* co)
  19.   {
  20.     // We had to make this a non-inline to get access
  21.     // to the vcmdprnt definitons.
  22.     _parentWin = dp;            // dialog class parent
  23.     dlgCmd = co;            // static data structure
  24.     wParent = 0;            // parent window
  25.     _origVal = co->retVal;        // save orignal state
  26.     _validLblHeight = 0;                // label height not known yet
  27.     _labelHeight = 0;
  28.   }
  29.  
  30. //===================>>> vCmd::CopyToLocal <<<=======================
  31.   void vCmd::CopyToLocal(void)
  32.   {
  33.     // Copy static values to local copy
  34.     _title = dlgCmd->title;
  35.     if (!_title || *_title == 0)
  36.       _title = "";
  37.     _cmdId = dlgCmd->cmdId;
  38.     _retVal = dlgCmd->retVal;
  39.     _Sensitive = dlgCmd->Sensitive;
  40.     _itemList = dlgCmd->itemList;
  41.   }
  42.  
  43. //===================>>> vCmd::vCmdCallback <<<=======================
  44.   void vCmd::CmdCallback(UINT uMsg, MPARAM mp1, MPARAM mp2)
  45.   {
  46.     _parentWin->ProcessCmd(dlgCmd->cmdId, dlgCmd->retVal, dlgCmd->cmdType);
  47.   }
  48.  
  49. //===================>>> vCmd::GetMyHwnd <<<=======================
  50.   HWND vCmd::GetMyHwnd(ItemVal id) VCONST
  51.   {
  52.     return WinWindowFromID(_parentWin->getParent(), id);
  53.   }
  54.  
  55. //===================>>> vCmd::LabelWidth <<<=======================
  56.   int vCmd::LabelWidth(char* lbl) VCONST
  57.   {
  58.     // We create a temporary and invisible client window here to
  59.     // gather font metric info for use in sizing labels in dialogs.
  60.     // Since dialogs are sized in dialog-units relative to
  61.     // the default dialog font, we first have to compute
  62.     // the number of pixels in a dialog-unit.  Once we have
  63.     // that info we compute the length in pixels of the label using
  64.     // the desired font (8.Helv) and then convert to dialog-units.
  65.     // We need to do this because every display driver uses different
  66.     // default font sizes and so there is no simple way to map text
  67.     // to dialog-units.
  68.  
  69.     int len = 0;
  70.  
  71.     // register the font probe window class
  72.     WinRegisterClass(theApp->AppHab(),
  73.       "MyProbeClass",     // window-class name
  74.       0L,                 // no window-procedure required
  75.       0L,                 // default window style
  76.       4L);                // reserved storage
  77.  
  78.     HWND hProbe = WinCreateWindow(HWND_DESKTOP,
  79.                     "MyProbeClass",
  80.                     "probe",
  81.                     0L,
  82.                     0,0,
  83.                     1000,200,
  84.                     NULLHANDLE,
  85.                     HWND_BOTTOM,
  86.                     0,
  87.                     NULL,
  88.                     NULL);
  89.  
  90.  
  91.     // first, we get the default font width to compute
  92.     // pixels/dialog-unit ratio.  We make use of the
  93.     // fact that 4 dialog-units = average-font-width of the
  94.     // default font.
  95.  
  96.     // create a cached-micro PS
  97.     HPS probeDC = WinGetPS(hProbe);
  98.     FONTMETRICS _fm;
  99.     GpiQueryFontMetrics(probeDC, sizeof(_fm), &_fm);
  100.     double pxr = _fm.lAveCharWidth / 4.0;
  101.  
  102.     // we get the default font height to compute vertical
  103.     // pixels/dialog-unit ratio.  We make use of the
  104.     // fact that 8 dialog-units = font-height of the
  105.     // default font.
  106.     double pyr = (_fm.lMaxAscender+_fm.lMaxDescender) / 8.0;
  107.     WinReleasePS(probeDC);
  108.  
  109.     // next we set the font to 8.Helv and compute length
  110.     // in pixels
  111.     char fns[] = "8.Helv";
  112.     WinSetPresParam(hProbe, PP_FONTNAMESIZE, sizeof(fns), fns);
  113.  
  114.     probeDC = WinGetPS(hProbe);
  115.     POINTL box[TXTBOX_COUNT];
  116.     GpiQueryTextBox(probeDC, strlen(lbl), lbl, TXTBOX_COUNT, box);
  117.     int pixux = box[TXTBOX_TOPRIGHT].x - box[TXTBOX_TOPLEFT].x;
  118.     double dlgux = pixux / pxr;
  119.     len = (int) (dlgux + 0.5);   // make sure we handle roundoff properly here
  120.  
  121.     // now compute label height
  122.     GpiQueryFontMetrics(probeDC, sizeof(_fm), &_fm);
  123.     double pixuy = _fm.lMaxAscender+_fm.lMaxDescender;
  124.     double dlguy = pixuy / pyr;
  125.     _labelHeight = (int) (dlguy + 0.5);   // make sure we handle roundoff properly here
  126.     _validLblHeight = TRUE;
  127.  
  128.     // cleanup
  129.     WinReleasePS(probeDC);
  130.     WinDestroyWindow(hProbe);
  131.  
  132. //    printf("vCmd::LabelWidth pxr=%lf pixu=%u dlgu=%lf len=%u \n", pxr, pixu, dlgu, len);
  133. //    printf("                 lbl=%s \n", lbl);
  134.  
  135.     return len;
  136.   }
  137.  
  138.  
  139. //===================>>> vCmd::LabelHeight <<<=======================
  140.   int vCmd::LabelHeight(void) VCONST
  141.   {
  142.     if (_validLblHeight )
  143.     {
  144.       return (_labelHeight);
  145.     }
  146.     else
  147.     {
  148.       // register the font probe window class
  149.       WinRegisterClass(theApp->AppHab(),
  150.         "MyProbeClass",     // window-class name
  151.         0L,                 // no window-procedure required
  152.         0L,                 // default window style
  153.         4L);                // reserved storage
  154.  
  155.       HWND hProbe = WinCreateWindow(HWND_DESKTOP,
  156.                       "MyProbeClass",
  157.                       "probe",
  158.                       0L,
  159.                       0,0,
  160.                       1000,200,
  161.                       NULLHANDLE,
  162.                       HWND_BOTTOM,
  163.                       0,
  164.                       NULL,
  165.                       NULL);
  166.  
  167.  
  168.       // create a cached-micro PS
  169.       HPS probeDC = WinGetPS(hProbe);
  170.       FONTMETRICS _fm;
  171.       GpiQueryFontMetrics(probeDC, sizeof(_fm), &_fm);
  172.  
  173.       // we get the default font height to compute vertical
  174.       // pixels/dialog-unit ratio.  We make use of the
  175.       // fact that 8 dialog-units = average-font-height of the
  176.       // default font.
  177.       double pyr = (_fm.lMaxAscender+_fm.lMaxDescender) / 8.0;
  178.       WinReleasePS(probeDC);
  179.  
  180.       // next we set the font to 8.Helv and compute height
  181.       // in pixels
  182.       char fns[] = "8.Helv";
  183.       WinSetPresParam(hProbe, PP_FONTNAMESIZE, sizeof(fns), fns);
  184.  
  185.       probeDC = WinGetPS(hProbe);
  186.  
  187.       GpiQueryFontMetrics(probeDC, sizeof(_fm), &_fm);
  188.       double pixuy = _fm.lMaxAscender+_fm.lMaxDescender;
  189.       double dlguy = pixuy / pyr;
  190.       _labelHeight = (int) (dlguy + 0.5);   // make sure we handle roundoff properly here
  191.       _validLblHeight = TRUE;
  192.  
  193.       // cleanup
  194.       WinReleasePS(probeDC);
  195.       WinDestroyWindow(hProbe);
  196.  
  197.       return (_labelHeight);
  198.     }
  199.   }
  200.