home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / COMBO.ZIP / COMBOSUP.C < prev    next >
C/C++ Source or Header  |  1990-01-28  |  3KB  |  95 lines

  1. #include <malloc.h>
  2. #include <string.h>
  3.  
  4. #define  INCL_WINDIALOGS
  5. #define  INCL_WINLISTBOXES
  6. #define  INCL_WINENTRYFIELDS
  7. #define  INCL_WINWINDOWMGR
  8. #include <os2.h>
  9.  
  10. #include "combosup.h"
  11.  
  12.  
  13.  
  14. /* ------------------------------------------------------------------ */
  15.  
  16. USHORT EXPENTRY InitComboBox
  17. (
  18.     HWND hwnd,              // Dialog box or owner window handle
  19.     SHORT idCombo,          // Combo box identifier
  20.     PSZ *apszInit,          // Array of pointers to listbox strings
  21.     SHORT InitialSelection  // Array index of initially selected
  22.                             //  string, or value ICB_NOSELECTION
  23. )
  24. {
  25.     SHORT   cItems = 0;
  26.     PSZ *   apszInitItem = apszInit;
  27.  
  28.     while( *apszInitItem )              // Loop through init array
  29.     {
  30.         ++cItems;                       // Count number of items
  31.         WinSendDlgItemMsg( hwnd,        // Insert item into combo listbox
  32.             idCombo, LM_INSERTITEM,
  33.             MPFROMSHORT( LIT_END ),
  34.             *apszInitItem++ );
  35.     }
  36.  
  37.     if( (InitialSelection != ICB_NOSELECTION)   // If a valid initial
  38.     &&  (InitialSelection >= 0)             //  selection index is provided,
  39.     &&  (InitialSelection < cItems) )       //  select that item
  40.         WinSetDlgItemText( hwnd, idCombo,
  41.             apszInit[ InitialSelection ] );
  42.  
  43.     return 0;
  44. }
  45.  
  46.  
  47.  
  48. /* ------------------------------------------------------------------ */
  49.  
  50. USHORT  EXPENTRY InitComboBoxFromRcStrings
  51. (
  52.     HAB hab,                // Anchor block handle
  53.     HWND hwnd,              // Dialog box or owner window handle
  54.     SHORT idCombo,          // Combo box identifier
  55.     SHORT cStrings,         // Count of strings to insert in combo box
  56.     SHORT aidStrings[],     // Array of resource string identifiers
  57.     SHORT cbStringMax,      // Maximum length of resource string
  58.     HMODULE hmodResource,   // Resource module handle, or null
  59.     SHORT InitialSelection  // Array index of initially selected
  60.                             //  string, or value ICB_NOSELECTION
  61. )
  62. {
  63.     PSZ     *apszInit;      // Combo box initialization structure
  64.     PSZ     pszString;      // Buffer for loading individual strings
  65.     SHORT   cbString;       // String length returned from WinLoadString
  66.     SHORT   i;              // Array index for looping
  67.  
  68.     pszString = halloc( 1L,             // Allocate WinLoadString buffer
  69.         cbStringMax + 1 );
  70.     apszInit = halloc( 1L,              // Allocate array of pointers to
  71.         sizeof (PSZ) * (cStrings + 1) );//  initialization strings
  72.     apszInit[ cStrings ] = 0;           // Set last element to NULL
  73.  
  74.     for( i = 0; i < cStrings; ++i )     // Load all strings
  75.     {
  76.         cbString = WinLoadString( hab,  // Load string from resource file
  77.             hmodResource, aidStrings[ i ],
  78.             cbStringMax, pszString );
  79.         apszInit[ i ] = malloc(         // Allocate buffer for this string
  80.             cbString + 1 );             //  and point array element to it
  81.         strcpy( apszInit[ i ], pszString ); // Copy string into buffer
  82.     }
  83.  
  84.     InitComboBox( hwnd, idCombo,        // Initialize combo box
  85.         apszInit, InitialSelection );
  86.  
  87.     for( i = 1; i <= cStrings; ++i )    // Free all string buffers
  88.         free( apszInit[ i ] );
  89.  
  90.     hfree( apszInit );                  // Free array of string pointers
  91.     hfree( pszString );                 // Free WinLoadString buffer
  92.  
  93.     return 0;
  94. }
  95.