home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / oledb / tablecopy / step3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-12  |  12.5 KB  |  373 lines

  1. //-----------------------------------------------------------------------------
  2. // Microsoft OLE DB TABLECOPY Sample
  3. // Copyright (C) 1995-1998 Microsoft Corporation
  4. //
  5. // @doc
  6. //
  7. // @module STEP3.CPP
  8. //
  9. //-----------------------------------------------------------------------------
  10.  
  11.  
  12.  
  13. /////////////////////////////////////////////////////////////////////
  14. // Includes
  15. //
  16. /////////////////////////////////////////////////////////////////////
  17. #include "wizard.h"
  18. #include "common.h"
  19. #include "Tablecopy.h"
  20. #include "Table.h"
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////
  24. // CS3Dialog::CS3Dialog
  25. //
  26. /////////////////////////////////////////////////////////////////////
  27. CS3Dialog::CS3Dialog(HWND hWnd, HINSTANCE hInst, CTableCopy* pCTableCopy)
  28.     : CDialogBase(hWnd, hInst)
  29. {
  30.     ASSERT(pCTableCopy);
  31.     m_pCTableCopy = pCTableCopy;
  32. }
  33.  
  34.  
  35. /////////////////////////////////////////////////////////////////////
  36. // CS3Dialog::~CS3Dialog
  37. //
  38. /////////////////////////////////////////////////////////////////////
  39. CS3Dialog::~CS3Dialog()
  40. {
  41. }
  42.  
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // ULONG CS3Dialog::Display
  46. //
  47. /////////////////////////////////////////////////////////////////////////////
  48. ULONG CS3Dialog::Display()
  49. {
  50.     //Create a modal dialog box
  51.     return DialogBoxParam(m_hInst, MAKEINTRESOURCE(IDD_TO_INFO), NULL, DlgProc, (LPARAM)this);
  52. }
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////
  56. // CS3Dialog::DlgProc
  57. //
  58. /////////////////////////////////////////////////////////////////////
  59. BOOL WINAPI CS3Dialog::DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  60. {
  61.     switch(msg) 
  62.     {
  63.         case WM_INITDIALOG:
  64.         {
  65.             Busy();
  66.             //Store the "this" pointer, since this is a static method
  67.             CS3Dialog* pThis = (CS3Dialog*)lParam;
  68.             SetWindowLong(hWnd, GWL_USERDATA, (LONG)pThis);
  69.             
  70.             //On INIT we know we have a valid hWnd to store
  71.             CenterDialog(hWnd);
  72.             pThis->m_hWnd = hWnd;
  73.  
  74.             //Init all controls to the default values
  75.             pThis->InitControls();
  76.             
  77.             //Limit the length of User Entered TableName
  78.             SendDlgItemMessage(hWnd, IDE_TO_TABLE, EM_LIMITTEXT, (WPARAM)MAX_NAME_LEN-1, 0L);
  79.  
  80.             pThis->RefreshControls();
  81.             pThis->m_pCTableCopy->m_pCWizard->DestroyPrevStep(WIZ_STEP3);
  82.             return HANDLED_MSG;
  83.         }
  84.         
  85.         case WM_COMMAND:
  86.         {
  87.             //Obtain the "this" pointer
  88.             CS3Dialog* pThis = (CS3Dialog*)GetWindowLong(hWnd, GWL_USERDATA);
  89.  
  90.             CTable* pCFromTable = pThis->m_pCTableCopy->m_pCFromTable;        
  91.             CTable* pCToTable = pThis->m_pCTableCopy->m_pCToTable;        
  92.             CDataSource* pCToDataSource = pCToTable->m_pCDataSource;        
  93.             CDataSource* pCFromDataSource = pCFromTable->m_pCDataSource;        
  94.  
  95.             // All buttons are handled the same way
  96.             switch(GET_WM_COMMAND_ID(wParam, lParam)) 
  97.             {
  98.                 case IDB_TO_CONNECT:
  99.                 {
  100.                     Busy();
  101.                     if(pThis->Connect()) 
  102.                     {
  103.                         Busy();
  104.                         pThis->RefreshControls();
  105.                         SetFocus(GetDlgItem(hWnd, IDE_TO_TABLE));
  106.                         SendMessage(GetDlgItem(hWnd, IDE_TO_TABLE), EM_SETSEL, 0, -1); //Highlight TableName
  107.                     }
  108.                     return HANDLED_MSG;
  109.                 }
  110.  
  111.                 case IDOK:
  112.                 {    
  113.                     //Get the TableName
  114.                     Busy();
  115.                     wSendMessage(GetDlgItem(hWnd, IDE_TO_TABLE), WM_GETTEXT, MAX_NAME_LEN-1, pCToTable->m_TableInfo.wszTableName);
  116.                     
  117.                     //If the TableNames are the same (ignoring case) and the 
  118.                     //DataSource is the same then the copy is worthless (a no-op)
  119.                     if(_wcsicmp(pCToTable->m_TableInfo.wszTableName, pCFromTable->m_TableInfo.wszTableName)==0 &&
  120.                         pCToDataSource->IsEqual(pCFromDataSource))
  121.                     {
  122.                         //Need to enter a different table name from the source
  123.                         wMessageBox(hWnd, MB_TASKMODAL | MB_ICONEXCLAMATION | MB_OK,     
  124.                             wsz_ERROR, wsz_SAME_TABLE_NAME, pCToDataSource->m_pwszTableTerm);
  125.                         SetFocus(GetDlgItem(hWnd, IDE_TO_TABLE));
  126.                         SendMessage(GetDlgItem(hWnd, IDE_TO_TABLE), EM_SETSEL, 0, -1); //Highlight TableName
  127.                         return HANDLED_MSG;
  128.                     }
  129.                     
  130.                     wcscpy(pCToTable->m_wszQualTableName, pCToTable->m_TableInfo.wszTableName);
  131.                     pThis->m_pCTableCopy->m_pCWizard->DisplayStep(WIZ_STEP4);
  132.                     return HANDLED_MSG;
  133.                 }
  134.                 
  135.                 case IDB_PREV:
  136.                     //Get the TableName
  137.                     Busy();
  138.                     wSendMessage(GetDlgItem(hWnd, IDE_TO_TABLE), WM_GETTEXT, MAX_NAME_LEN-1, pCToTable->m_TableInfo.wszTableName);
  139.                     wcscpy(pCToTable->m_wszQualTableName,pCToTable->m_TableInfo.wszTableName);
  140.                     pThis->m_pCTableCopy->m_pCWizard->DisplayStep(WIZ_STEP2);
  141.                     return HANDLED_MSG;
  142.  
  143.                 case IDCANCEL:
  144.                     Busy();
  145.                     EndDialog(hWnd, GET_WM_COMMAND_ID(wParam, lParam));
  146.                     return HANDLED_MSG;
  147.             }
  148.  
  149.             // Now look for notification messages
  150.             switch(GET_WM_COMMAND_CMD(wParam, lParam)) 
  151.             {
  152.                 case LBN_SELCHANGE:
  153.                 {
  154.                     //A Provider Change requires a refresh
  155.                     if(IDC_PROVIDER_NAME == GET_WM_COMMAND_ID(wParam, lParam)) 
  156.                     {
  157.                         //Get new selection
  158.                         Busy();
  159.                         LONG iSel = 0;
  160.                         if((iSel = SendMessage(GetDlgItem(pThis->m_hWnd, IDC_PROVIDER_NAME), CB_GETCURSEL, 0, 0L)) != CB_ERR)
  161.                         {
  162.                             //Since we have the CBS_SORT turned on, the order in the Combo Box does
  163.                             //not match our array, so we pass the array index (lParam) as the item data
  164.                             LONG lParam = SendMessage(GetDlgItem(pThis->m_hWnd, IDC_PROVIDER_NAME), CB_GETITEMDATA, iSel, 0L);
  165.                             if((lParam < (LONG)pCToDataSource->m_cProviderInfo) && (wcscmp(pCToDataSource->m_rgProviderInfo[lParam].wszName, pCToDataSource->m_pwszProviderName)!=0))
  166.                             {
  167.                                 pCToDataSource->Disconnect();
  168.                             }
  169.                         }
  170.                     }
  171.  
  172.                     pThis->RefreshControls();
  173.                 }
  174.                 
  175.                 case EN_CHANGE:
  176.                 {
  177.                     pThis->EnableTable();
  178.                     return HANDLED_MSG;
  179.                 }
  180.             }
  181.         }
  182.     }
  183.  
  184.     return UNHANDLED_MSG;
  185. }
  186.  
  187.  
  188.  
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191. // BOOL CS3Dialog::InitControls
  192. //
  193. /////////////////////////////////////////////////////////////////////////////
  194. BOOL CS3Dialog::InitControls()
  195. {
  196.     //Initialize the Provider List (if not done so already)
  197.     CDataSource* pCDataSource = m_pCTableCopy->m_pCToTable->m_pCDataSource;
  198.     CDataSource* pCFromDataSource = m_pCTableCopy->m_pCFromTable->m_pCDataSource;
  199.     
  200.     if(pCDataSource->m_rgProviderInfo == NULL)
  201.         pCDataSource->GetProviders();
  202.     
  203.     WCHAR wszBuffer[MAX_NAME_LEN*2];
  204.     HWND hWndProv = GetDlgItem(m_hWnd, IDC_PROVIDER_NAME);
  205.  
  206.     //Fill out the provider name combo box.
  207.     for(ULONG i=0; i<pCDataSource->m_cProviderInfo; i++)
  208.     {
  209.         //Add the name to the list
  210.         //Since we have the CBS_SORT turned on, the order in the Combo Box does
  211.         //not match our array, so we pass the array index (lParam) as the item data
  212.         swprintf(wszBuffer, wsz_PROVIDER_INFO_, pCDataSource->m_rgProviderInfo[i].wszName, pCDataSource->m_rgProviderInfo[i].wszDescription);
  213.         LONG iIndex = wSendMessage(hWndProv, CB_ADDSTRING, (WPARAM)0, wszBuffer);
  214.         SendMessage(hWndProv, CB_SETITEMDATA, (WPARAM)iIndex, (LPARAM)i);
  215.     }
  216.     
  217.     //By default, it selects the same provider chossen in Step1
  218.     if(pCDataSource->m_pwszProviderName == NULL)
  219.         pCDataSource->m_pwszProviderName = pCFromDataSource->m_pwszProviderName;
  220.  
  221.     //Try and select the previous selected Provider
  222.     if(CB_ERR == wSendMessage(hWndProv, CB_SELECTSTRING, 0, pCDataSource->m_pwszProviderName))
  223.     {
  224.         //If not found, just select the first one
  225.         SendMessage(hWndProv, CB_SETCURSEL, 0, 0);
  226.     }
  227.  
  228.     //By default, try and Connect to the same DataSource as in Step1
  229.     //if using the same provider as in Step1
  230.     if(!m_pCTableCopy->m_pCToTable->IsConnected() && wcscmp(pCDataSource->m_pwszProviderName, pCFromDataSource->m_pwszProviderName)==0)
  231.     {
  232.         //The Provider must support more than 1 active session as well to do
  233.         //this functionality by default...
  234.         if(pCFromDataSource->m_ulActiveSessions==0 || pCFromDataSource->m_ulActiveSessions>=2)
  235.             Connect(pCFromDataSource);
  236.     }
  237.  
  238.     // Enable Connect button only if there are providers installed.
  239.     EnableWindow(GetDlgItem(m_hWnd, IDB_FROM_CONNECT), SendMessage(hWndProv, CB_GETCURSEL, 0, 0L) != CB_ERR);
  240.  
  241.     // Set the "Source" info
  242.     //CONNECT_STRING
  243.     wSetDlgItemText(m_hWnd, IDT_FROM_CONNECT, wsz_CONNECT_STRING_,
  244.         pCFromDataSource->m_pwszProviderName, 
  245.         pCFromDataSource->m_pwszDataSource, 
  246.         pCFromDataSource->m_pwszDBMS,
  247.         pCFromDataSource->m_pwszDBMSVer,
  248.         pCFromDataSource->m_pwszProviderFileName,
  249.         pCFromDataSource->m_pwszProviderVer);
  250.  
  251.     //TABLE
  252.     wSetDlgItemText(m_hWnd, IDE_FROM_TABLE, m_pCTableCopy->m_pCFromTable->m_wszQualTableName);
  253.     return TRUE;
  254. }
  255.  
  256.  
  257. /////////////////////////////////////////////////////////////////////////////
  258. // BOOL CS3Dialog::RefreshControls
  259. //
  260. /////////////////////////////////////////////////////////////////////////////
  261. BOOL CS3Dialog::RefreshControls()
  262. {
  263.     BOOL        fConnected;
  264.     
  265.     CTable* pCToTable = m_pCTableCopy->m_pCToTable;
  266.     CTable* pCFromTable = m_pCTableCopy->m_pCFromTable;
  267.     CDataSource* pCDataSource = pCToTable->m_pCDataSource;
  268.  
  269.     // Must have a connection to edit other controls
  270.     fConnected = pCToTable->IsConnected();
  271.  
  272.     //Enable certain dialog controls, only if connected
  273.     EnableWindow(GetDlgItem(m_hWnd, IDE_TO_TABLE),        fConnected);
  274.     EnableWindow(GetDlgItem(m_hWnd, IDT_TARGET),        fConnected);
  275.     EnableWindow(GetDlgItem(m_hWnd, IDT_TOTABLEHELP),    fConnected);
  276.     
  277.     //Store the selected ProviderName and ProviderDesc
  278.     LONG iSel = 0;
  279.     if((iSel = SendMessage(GetDlgItem(m_hWnd, IDC_PROVIDER_NAME), CB_GETCURSEL, 0, 0L)) != CB_ERR)
  280.     {
  281.         //Since we have the CBS_SORT turned on, the order in the Combo Box does
  282.         //not match our array, so we pass the array index (lParam) as the item data
  283.         LONG lParam = SendMessage(GetDlgItem(m_hWnd, IDC_PROVIDER_NAME), CB_GETITEMDATA, iSel, 0L);
  284.         ASSERT(lParam < (LONG)pCDataSource->m_cProviderInfo);
  285.         pCDataSource->m_pwszProviderName = pCDataSource->m_rgProviderInfo[lParam].wszName;
  286.         pCDataSource->m_pwszProviderParseName = pCDataSource->m_rgProviderInfo[lParam].wszParseName;
  287.     }
  288.     
  289.     // Show user the connection string, and enable Next that requires connection
  290.     if(fConnected)
  291.     {
  292.         //CONNECTSTATUS
  293.         SetDlgItemText(m_hWnd, IDT_CONNECTSTATUS, "");
  294.         
  295.         //CONNECT_STRING
  296.         wSetDlgItemText(m_hWnd, IDT_TO_CONNECT, wsz_CONNECT_STRING_,
  297.             pCDataSource->m_pwszProviderName, 
  298.             pCDataSource->m_pwszDataSource, 
  299.             pCDataSource->m_pwszDBMS,
  300.             pCDataSource->m_pwszDBMSVer,
  301.             pCDataSource->m_pwszProviderFileName,
  302.             pCDataSource->m_pwszProviderVer);
  303.  
  304.         //TABLEHELPMSG
  305.         wSetDlgItemText(m_hWnd, IDT_TOTABLEHELP, wsz_TOTABLEHELP_, 
  306.             pCDataSource->m_pwszTableTerm);
  307.     }
  308.     else
  309.     {
  310.         //CONNECTSTATUS
  311.         wSetDlgItemText(m_hWnd, IDT_CONNECTSTATUS, wsz_NOT_CONNECTED);
  312.  
  313.         //CONNECT_STRING
  314.         wSetDlgItemText(m_hWnd, IDT_TO_CONNECT, L"");
  315.     }
  316.     
  317.     // If there is already a Table from previous selections, just use that one
  318.     wSetDlgItemText(m_hWnd, IDE_TO_TABLE, pCToTable->m_TableInfo.wszTableName[0] ? pCToTable->m_TableInfo.wszTableName : pCFromTable->m_TableInfo.wszTableName);
  319.     
  320.     // Determine if there is enough information to move on
  321.     return EnableTable();
  322. }
  323.  
  324.         
  325.  
  326.  
  327. /////////////////////////////////////////////////////////////////////////////
  328. // BOOL CS3Dialog::Connect
  329. //
  330. /////////////////////////////////////////////////////////////////////////////
  331. BOOL CS3Dialog::Connect(CDataSource* pCDataSource)
  332. {
  333.     CDataSource* pCToDataSource = m_pCTableCopy->m_pCToTable->m_pCDataSource;
  334.     CDataSource* pCFromDataSource = m_pCTableCopy->m_pCFromTable->m_pCDataSource;
  335.     
  336.     //Connect to the DataSource
  337.     BOOL fConnected = m_pCTableCopy->m_pCToTable->Connect(m_hWnd, pCDataSource);
  338.     
  339.     //If Connected
  340.     if(fConnected)
  341.     {
  342.         // Verify we can use this data source
  343.         // Just give a warning to the user, since the DataSource may actually
  344.         // be updatable, but it is returning the wrong property value.
  345.         if(pCToDataSource->m_fReadOnly) 
  346.         {
  347.             wMessageBox(m_hWnd, MB_TASKMODAL | MB_ICONEXCLAMATION | MB_OK,     wsz_WARNING, 
  348.                     wsz_READONLY_DATASOURCE_, pCToDataSource->m_pwszDataSource);
  349.         }
  350.  
  351.         //See if this is a similar DSN than the Source
  352.         //If DSN's are not similar, then we need to translate
  353.         m_pCTableCopy->m_fTranslate = !pCToDataSource->IsSimilar(pCFromDataSource);
  354.     }
  355.  
  356.     return fConnected;
  357. }
  358.  
  359.  
  360. /////////////////////////////////////////////////////////////////////////////
  361. // BOOL CS3Dialog::EnableTable
  362. //
  363. /////////////////////////////////////////////////////////////////////////////
  364. BOOL CS3Dialog::EnableTable()
  365. {
  366.     BOOL fConnected = m_pCTableCopy->m_pCToTable->IsConnected();
  367.     
  368.     EnableWindow(GetDlgItem(m_hWnd, IDOK), 
  369.             fConnected && SendDlgItemMessage(m_hWnd, IDE_TO_TABLE, WM_GETTEXTLENGTH, 0, 0L));
  370.  
  371.     return TRUE;
  372. }
  373.