home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OWNDRW.ZIP / OWNDRAW.C next >
C/C++ Source or Header  |  1992-07-03  |  6KB  |  218 lines

  1. /*********************************************************************
  2.  *
  3.  *  COMPANEL.C
  4.  *
  5.  *  These functions add additional functionality to the listbox
  6.  *  class provided by OS/2.  These functions will cause text to be
  7.  *  aligned in columns even when using a porportional spaced font.
  8.  *
  9.  *  Compiled with MS C6.0ax using compiler switches
  10.  *
  11.  *    /W3 /Alfu /Gt64 /G2s /c /FPi87
  12.  *
  13.  *  Copyright (c) 1992 by ASH Software, Inc.
  14.  *
  15.  *  Update History
  16.  *
  17.  *    07/03/92 - Created source code
  18.  *
  19.  *********************************************************************/
  20.  
  21. #define INCL_WIN
  22. #define INCL_GPI
  23.  
  24. #include <os2.h>
  25. #include <string.h>
  26. #include <owndraw.h>
  27.  
  28. /*********************************************************************
  29.  *  ODInitLBWidthHeight
  30.  *
  31.  *  This function should be called to process the WM_MEASUREITEM
  32.  *  message.  The return value from this function should be used
  33.  *  as the return value for the WM_MEASUREITEM message.
  34.  *
  35.  *  Arguments:
  36.  *
  37.  *    hListBox =
  38.  *      Handle to the listbox window
  39.  *
  40.  *    sfCharWidth =
  41.  *      Flag indicating which character spacing to use
  42.  *      (AVERAGE_WIDTH or MAXIMUM_WIDTH)
  43.  *
  44.  *    sCharNax =
  45.  *      Maximum number of characters in any text string for the
  46.  *      particular listbox
  47.  *
  48.  *    *lCharWidth =
  49.  *      Pointer to a long which will contain the character spacing
  50.  *      on return from the function
  51.  *
  52.  *  Returns:
  53.  *
  54.  *    This function returns a MRESULT with the high word containing
  55.  *    the maximum length of a line of text and the low word
  56.  *    containing the character spacing.
  57.  *********************************************************************/
  58.  
  59. MRESULT EXPENTRY ODInitLBWidthHeight(HWND hListBox,SHORT sfCharWidth,
  60.   SHORT sCharMax,LONG *lCharWidth)
  61. {
  62.   SHORT
  63.     sCharWidth;
  64.  
  65.   FONTMETRICS
  66.     fm;
  67.  
  68.   HPS
  69.     hps;
  70.  
  71.   hps=WinGetPS(hListBox);
  72.   GpiQueryFontMetrics(hps,(LONG)sizeof(FONTMETRICS),&fm);
  73.   WinReleasePS(hps);
  74.   if (sfCharWidth == AVERAGE_WIDTH)
  75.     sCharWidth=(SHORT)fm.lAveCharWidth;
  76.   else
  77.     sCharWidth=(SHORT)fm.lMaxCharInc;
  78.   *lCharWidth=(LONG)sCharWidth;
  79.  
  80.   //  Return to the calling program
  81.  
  82.   return(
  83.     MRFROM2SHORT(fm.lMaxBaselineExt,sCharMax*sCharWidth));
  84. }
  85.  
  86. /*********************************************************************
  87.  *  ODDrawLBItems
  88.  *
  89.  *  This function should be called to process the WM_DRAWITEM message.
  90.  *  The return value from this function should be used as the return
  91.  *  value for the WM_DRAWITEM message.  This function will align text
  92.  *  in columns based on the column numbers provided in the tab stop
  93.  *  array.  The function will expand embedded tabs by inserting spaces
  94.  *  to reach the next tab stop.  Expanding tabs will slow the listbox
  95.  *  operations, but may save significant amounts of memory.
  96.  *
  97.  *  Arguments:
  98.  *
  99.  *    pOwnerItem =
  100.  *      Pointer to the OWNERITEM structure which is passed in
  101.  *      the WM_DRAWITEM message as mp2
  102.  *
  103.  *    lCharWidth =
  104.  *      The character spacing which is usually obtained by the
  105.  *      call to ODInitLBWidthHeight
  106.  *
  107.  *    *psTabStops =
  108.  *      Pointer to an array of tab stops.  The last tab stop
  109.  *      must be 0 to indicate the end of the array.  The tab
  110.  *      stops number the character positions from 1 to n.  There
  111.  *      is no limit on the number of tab stops allowed.
  112.  *
  113.  *  Returns:
  114.  *
  115.  *    The function returns TRUE indicating that the function has
  116.  *    drawn the text.  The function does not do the highlighting,
  117.  *    but allows the system to perform the default highlighting.
  118.  *
  119.  *  Caution - Remember to make the tab stop arrary and the character
  120.  *            width static or external so that the values will remain
  121.  *            unchanged.
  122.  *********************************************************************/
  123.  
  124. MRESULT EXPENTRY ODDrawLBItems(POWNERITEM pOwnerItem,
  125.   LONG lCharWidth,SHORT *psTabStops)
  126. {
  127.   SHORT
  128.     sTab,
  129.     sLoop,
  130.     sNumberOfChars,
  131.     sTabCharPos,
  132.     sNextCharPos,sPrevCharPos;
  133.  
  134.   LONG
  135.     lSaveXLeft;
  136.  
  137.   CHAR
  138.     cChar,
  139.     cTempReadString[MAX_TEXT_CHARS],
  140.     cTempString[MAX_TEXT_CHARS];
  141.  
  142.   if (pOwnerItem->fsState == pOwnerItem->fsStateOld)
  143.     {
  144.  
  145.     //  Text needs to be drawn
  146.  
  147.     WinSendMsg(pOwnerItem->hwnd,LM_QUERYITEMTEXT,
  148.       MPFROM2SHORT(pOwnerItem->idItem,MAX_TEXT_CHARS),
  149.       MPFROMP(cTempReadString));
  150.     lSaveXLeft=pOwnerItem->rclItem.xLeft;
  151.  
  152.     //  Prepare the output string by copying characters from the
  153.     //  input string and by expanding any tabs found
  154.  
  155.     sNextCharPos=0;
  156.     for (sLoop=0; sLoop<=(SHORT)strlen(cTempReadString); sLoop++)
  157.       {
  158.       cChar=cTempReadString[sLoop];
  159.       if (cChar == '\t')
  160.         {
  161.  
  162.         // Expand the tab to spaces
  163.  
  164.         sTab=-1;
  165.         do
  166.           {
  167.           sTab++;
  168.           sTabCharPos=psTabStops[sTab];
  169.           } while ((sTabCharPos < sNextCharPos) && (sTabCharPos != 0));
  170.         sTabCharPos--;
  171.         if (sNextCharPos < sTabCharPos)
  172.           for (; sNextCharPos<sTabCharPos; sNextCharPos++)
  173.             cTempString[sNextCharPos]=' ';
  174.         }
  175.       else
  176.         {
  177.  
  178.         // Copy the characters from the input string to the output
  179.         // string
  180.  
  181.         cTempString[sNextCharPos]=cChar;
  182.         sNextCharPos++;
  183.         }
  184.       }
  185.  
  186.     //  Align the text on the tab stops
  187.  
  188.     sTab=-1;
  189.     sNextCharPos=1;
  190.     do
  191.       {
  192.       sTab++;
  193.       sPrevCharPos=sNextCharPos;
  194.       sNextCharPos=psTabStops[sTab];
  195.       if (sNextCharPos > 0)
  196.         sNumberOfChars=sNextCharPos-sPrevCharPos;
  197.       else
  198.         sNumberOfChars=0xFFFF;
  199.       pOwnerItem->rclItem.xLeft += (LONG)sPrevCharPos*lCharWidth;
  200.       WinDrawText(pOwnerItem->hps,sNumberOfChars,
  201.         &cTempString[sPrevCharPos-1],&pOwnerItem->rclItem,
  202.         0,0,DT_LEFT|DT_VCENTER|DT_ERASERECT|DT_TEXTATTRS);
  203.       }
  204.     while (psTabStops[sTab] > 0);
  205.  
  206.     //  Let the system perform default highlighting
  207.  
  208.     pOwnerItem->rclItem.xLeft=lSaveXLeft;
  209.     if (pOwnerItem->fsState)
  210.       pOwnerItem->fsStateOld=FALSE;
  211.     }
  212.  
  213.   //  Return to the calling program indicating that the text has 
  214.   //  been drawn
  215.  
  216.   return((MRESULT) TRUE);
  217. }
  218.