home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mltcol.zip / FILLLIST.C < prev    next >
C/C++ Source or Header  |  1993-03-12  |  10KB  |  217 lines

  1. static char szRevision[] = "$Revision:   4.0  $     filllist.c";
  2.  
  3. /*----------------------------------------------------------------------*/
  4. /*                                                                      */
  5. /*      A set up routines to fill list boxes. The only one of true      */
  6. /*      value is ADSFillAnyListBox I would like to see all the others   */
  7. /*      deleted.                                                        */
  8. /*      (May 4 1992 - Old methods deleted )                             */
  9. /*----------------------------------------------------------------------*/
  10. #define     INCL_WIN
  11. #define     INCL_DOSMODULEMGR
  12. #include   <os2.h>
  13.  
  14. #define     DBMSOS2
  15. #include   <sybfront.h>
  16. #include   <sybdb.h>
  17.  
  18. #include   <string.h>
  19. #include   <ctype.h>
  20. #include   <stdlib.h>
  21.  
  22. #include   "adslen.h"
  23. #include   "ads.h"
  24. #include   "adsstr.h"
  25. #include   "errors.h"
  26.  
  27. /*----------------------------------------------------------------------*/
  28. /*   ADSFillAnyListBox                                                  */
  29. /*      Any list box can be filled with this function.                  */
  30. /*                                                                      */
  31. /*          dbproc  -   Pointer to our DBPROCESS structure              */
  32. /*                                                                      */
  33. /*            hwnd  -   Window handle to the list box                   */
  34. /*                                                                      */
  35. /*       asBindCol  -   An array containing the order in which the      */
  36. /*                      returned columns are to be displayed            */
  37. /*                                                                      */
  38. /*   pszStoredProc  -   The SQL command to use to get the rows we want  */
  39. /*                                                                      */
  40. /*----------------------------------------------------------------------*/
  41. USHORT  ADSFillAnyListBox ( DBPROCESS *dbproc,   HWND hwnd,
  42.                          SHORT     asBindCol[],  CHAR *pszStoredProc )  {
  43.  
  44.     char         *apCol[ 60 + 1 ], *pszLine, szMsg[60], szMoney[30], *pszName;
  45.     int           TotLen, ColLen, ColType[60], i;
  46.     USHORT        cnt = 0;
  47.     BOOL          fAlloc = FALSE;
  48.     HMODULE       hmodADS;
  49.     USERATTR     *pShare;
  50.  
  51.     pShare = ADSGetShrSeg ( );
  52.     /*------------------------------------------------------------------*/
  53.     /*  Display painting of the List Box and empty it out.              */
  54.     /*------------------------------------------------------------------*/
  55.     WinEnableWindowUpdate ( hwnd, FALSE );
  56.     ADSSetWait ( );
  57.     /*------------------------------------------------------------------*/
  58.     /*  Execute the SQL code                                            */
  59.     /*------------------------------------------------------------------*/
  60.     dbcmd     ( dbproc, pszStoredProc );
  61.     dbsqlexec ( dbproc );
  62.  
  63.     while ( dbresults ( dbproc ) == SUCCEED )  {
  64.  
  65.         fAlloc = TRUE;
  66.         /*--------------------------------------------------------------*/
  67.         /*  For each column to be returned allocate a buffer and bind   */
  68.         /*  this buffer to the appropriate column.                      */
  69.         /*--------------------------------------------------------------*/
  70.         for ( TotLen = i = 0; asBindCol[i]; i++ )  {
  71.  
  72.             ColType[i]  = dbcoltype ( dbproc, abs ( asBindCol[i] ) );
  73.             switch ( ColType[i] )  {
  74.             case   SQLDATETIMN  :
  75.             case   SQLDATETIME  :   ColLen   = L_DATE_TIME; break;
  76.  
  77.             case   SQLINTN      :
  78.             case   SQLINT1      :
  79.             case   SQLBIT       :
  80.             case   SQLINT2      :
  81.             case   SQLINT4      :   ColLen   = 12;          break;
  82.  
  83.             case   SQLMONEY     :
  84.             case   SQLMONEYN    :   ColLen   = L_AMOUNT;    break;
  85.  
  86.             case   SQLFLT8      :
  87.             case   SQLFLTN      :   ColLen   = 12;          break;
  88.  
  89.             default             :
  90.             case   SQLCHAR      :
  91.                 ColLen   = (int) dbcollen  ( dbproc, abs ( asBindCol[i] ) ) + 1;
  92.                 pszName  = dbcolname ( dbproc, abs ( asBindCol[i] ) );
  93.                 /*------------------------------------------------------*/
  94.                 /*  If date                                             */
  95.                 /*------------------------------------------------------*/
  96.                 if ( pszName[0] == 0 && ColLen == 8+1 )
  97.                     ColType[i] = SQLDATETIME;
  98.                 break;
  99.             }
  100.             if ( ( apCol[i] = malloc ( ColLen ) ) == NULL )  {
  101.                 dbcanquery ( dbproc );
  102.                 break;
  103.             }
  104.  
  105.             TotLen  += ColLen + 2;
  106.             dbbind ( dbproc, abs ( asBindCol[i] ), NTBSTRINGBIND, ColLen, apCol[i] );
  107.         }
  108.         /*--------------------------------------------------------------*/
  109.         /*  Allocate a buffer big enough to hold the combinded columns  */
  110.         /*--------------------------------------------------------------*/
  111.         if ( ( pszLine = malloc ( TotLen ) ) == NULL )  {
  112.             dbcanquery ( dbproc );
  113.             break;
  114.          }
  115.  
  116.         while ( dbnextrow ( dbproc ) != NO_MORE_ROWS )  {
  117.             pszLine[0] = 0;
  118.             /*----------------------------------------------------------*/
  119.             /*  If the asBindCol number is less than 0 this indicates   */
  120.             /*  that this column is to be right justified. So           */
  121.             /*  concatenate a \r on to the string                       */
  122.             /*----------------------------------------------------------*/
  123.             if ( asBindCol[0] < 0 )
  124.                 strcat ( pszLine, "\r" );
  125.             strcat ( pszLine, apCol[0] );
  126.             for ( i = 1; asBindCol[i]; i++ )  {
  127.  
  128.                 strcat ( pszLine, "\t" );
  129.                 if ( asBindCol[i] < 0 )
  130.                     strcat ( pszLine, "\r" );
  131.                 if ( ColType[i] == SQLDATETIME || ColType[i] == SQLDATETIMN )  {
  132.                     char    szDate[L_DATE];
  133.                     if ( apCol[i][2] == '/' )  {
  134.                         ADSConvertDateFromMDY ( pShare->szDateFmtCode, szDate, apCol[i] );
  135.                         strcpy ( apCol[i], szDate );
  136.                     }
  137.                 }
  138.                 if ( ColType[i] == SQLMONEY || ColType[i] == SQLMONEYN )  {
  139.         /*          DBMONEY   mMoney;                                   */
  140.         /*          ADSFormatMoney ( NULL, pShare->szDefaultCurrCode,   */
  141.         /*          DBMONEY   *pmMoney, apCol[i] );                     */
  142.         /*                                                              */
  143.         /*          strcat ( pszLine, "$ " );                           */
  144.                     strcat ( pszLine, apCol[i] );
  145.                 }
  146.                 else  {
  147.                     strcat ( pszLine, apCol[i] );
  148.                 }
  149.             }
  150.             if ( SHORT1FROMMR ( WinSendMsg ( hwnd, LM_SEARCHSTRING,
  151.                             MPFROM2SHORT ( LSS_CASESENSITIVE, LIT_FIRST ),
  152.                             MPFROMP      ( pszLine ) ) )
  153.                                       == LIT_NONE )  {
  154.                 cnt++;
  155.                 if ( WinSendMsg ( hwnd, LM_INSERTITEM,
  156.                         MPFROMSHORT ( LIT_END ), MPFROMP( pszLine ) )
  157.                                          == ( MRESULT ) LIT_MEMERROR )  {
  158.                     dbcanquery (dbproc);
  159.                     DosLoadModule ( szMsg, sizeof ( szMsg ), "ADS", &hmodADS );
  160.  
  161.                     WinLoadString ( WinQueryAnchorBlock ( hwnd ), hmodADS,
  162.                                     IDS_LIST_BOX_FULL, sizeof( szMsg ), szMsg );
  163.                     DosFreeModule ( hmodADS );
  164.                     ADSMsgBox ( dbproc, TOO_MANY_ROWS, szMsg );
  165.                     /*--------------------------------------------------*/
  166.                     /*  cancel extra rows if the List Box exploded      */
  167.                     /*--------------------------------------------------*/
  168.                     goto EMERGENCY;
  169.                 }                           /* Insert Failed check      */
  170.             }                               /* Duplicate search check   */
  171.         }                                   /* while next row loop      */
  172.     }                                       /* while results  loop      */
  173. EMERGENCY:
  174.     ADSSetArrow ( );
  175.     WinShowWindow ( hwnd, TRUE );
  176.  
  177.     /*------------------------------------------------------------------*/
  178.     /*  Free all buffers                                                */
  179.     /*------------------------------------------------------------------*/
  180.     if ( fAlloc )
  181.     {
  182.         for ( i = 0; asBindCol[i]; i++ )
  183.             free ( apCol[i] );
  184.  
  185.         free ( pszLine );
  186.     }
  187.     return  cnt;
  188. }
  189.  
  190. void ADSSetMCHeading ( HWND hwndLB, LONG *alColWidth, USHORT  usMaxCols )
  191. {
  192.  
  193.     char    Buf[80];
  194.  
  195.     memset ( alColWidth, 0, usMaxCols * sizeof (LONG) );
  196.  
  197.     /*------------------------------------------------------------*/
  198.     /* Disable the list box, delete everything from the list box  */
  199.     /* and then either insert a dummy row and delete the dummy      */
  200.     /* row.  Then enable the list box. The heading won't show up  */
  201.     /* on an empty multi column list box              */
  202.     /*------------------------------------------------------------*/
  203.     WinEnableWindowUpdate ( hwndLB, FALSE );
  204.  
  205.     /*----------------------------------------------------------*/
  206.     /* Need to input a line or the column lengths are incorrect */
  207.     /*----------------------------------------------------------*/
  208.     Buf[0] = ' ';
  209.     Buf[1] =  0 ;
  210.     for ( usMaxCols--; usMaxCols; usMaxCols-- )
  211.         strcat ( Buf, "\t " );
  212.  
  213.     WinSendMsg ( hwndLB, LM_INSERTITEM, ( MPARAM ) 0, ( MPARAM ) Buf );
  214.     WinSendMsg ( hwndLB, LM_DELETEITEM, 0, 0 );
  215.     WinShowWindow ( hwndLB, TRUE );
  216. }
  217.