STEP3.CPP

//----------------------------------------------------------------------------- 
// Microsoft OLE DB TABLECOPY Sample
// Copyright (C) 1996 By Microsoft Corporation.
//
// @doc
//
// @module STEP3.CPP
//
//-----------------------------------------------------------------------------



/////////////////////////////////////////////////////////////////////
// Includes
//
/////////////////////////////////////////////////////////////////////
#include "wizard.h"
#include "common.h"
#include "Tablecopy.h"
#include "Table.h"


/////////////////////////////////////////////////////////////////////
// CS3Dialog::CS3Dialog
//
/////////////////////////////////////////////////////////////////////
CS3Dialog::CS3Dialog(HWND hWnd, HINSTANCE hInst, CTableCopy* pCTableCopy)
: CDialog(hWnd, hInst)
{
ASSERT(pCTableCopy);
m_pCTableCopy = pCTableCopy;
}


/////////////////////////////////////////////////////////////////////
// CS3Dialog::~CS3Dialog
//
/////////////////////////////////////////////////////////////////////
CS3Dialog::~CS3Dialog()
{
}


/////////////////////////////////////////////////////////////////////////////
// ULONG CS3Dialog::Display
//
/////////////////////////////////////////////////////////////////////////////
ULONG CS3Dialog::Display()
{
return DialogBoxParam(m_hInst, MAKEINTRESOURCE(IDD_TO_INFO), NULL, DlgProc, (LPARAM)this);
}


/////////////////////////////////////////////////////////////////////
// CS3Dialog::DlgProc
//
/////////////////////////////////////////////////////////////////////
BOOL WINAPI CS3Dialog::DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
{
//Store the "this" pointer, since this is a static method
CS3Dialog* pThis = (CS3Dialog*)lParam;
SetWindowLong(hWnd, GWL_USERDATA, (LONG)pThis);

//On INIT we know we have a valid hWnd to store
pThis->m_hWnd = hWnd;

//Limit the length of User Entered CTableName
SendDlgItemMessage(hWnd, IDE_TO_TABLE, EM_LIMITTEXT, (WPARAM)MAX_NAME_LEN-1, 0L);

pThis->RefreshControls();
CenterDialog(hWnd);
return HANDLED_MSG;
}

case WM_COMMAND:
{
//Obtain the "this" pointer
CS3Dialog* pThis = (CS3Dialog*)GetWindowLong(hWnd, GWL_USERDATA);

CTable* pCFromTable = pThis->m_pCTableCopy->m_pCFromTable;
CTable* pCToTable = pThis->m_pCTableCopy->m_pCToTable;

// All buttons are handled the same way
switch(GET_WM_COMMAND_ID(wParam, lParam))
{
case IDB_TO_CONNECT:
{
if(pThis->Connect())
{
pThis->RefreshControls();
SetFocus(GetDlgItem(hWnd, IDE_TO_TABLE));
}
return HANDLED_MSG;
}

case IDOK:
{
//Get the TableName
wSendMessage(GetDlgItem(hWnd, IDE_TO_TABLE), WM_GETTEXT, MAX_NAME_LEN-1, pCToTable->m_wszTableName);

//If the TableNames are the same and the DataSource is the
//same then the copy is worthless (a no-op)
if(wcscmp(pCToTable->m_wszTableName, pCFromTable->m_wszTableName)==0 &&
pCToTable->m_pCDataSource->IsEqual(pCFromTable->m_pCDataSource))
{
//Need to enter a different table name from the source
wMessageBox(hWnd, MB_ICONEXCLAMATION | MB_OK,
wsz_ERROR, wsz_SAME_TABLE_NAME);
SetFocus(GetDlgItem(hWnd, IDE_TO_TABLE));
return HANDLED_MSG;
}

wcscpy(pCToTable->m_wszQualTableName,pCToTable->m_wszTableName);
EndDialog(hWnd, GET_WM_COMMAND_ID(wParam, lParam));
return HANDLED_MSG;
}

case IDB_PREV:
//Get the TableName
wSendMessage(GetDlgItem(hWnd, IDE_TO_TABLE), WM_GETTEXT, MAX_NAME_LEN-1, pCToTable->m_wszTableName);
wcscpy(pCToTable->m_wszQualTableName,pCToTable->m_wszTableName);
EndDialog(hWnd, GET_WM_COMMAND_ID(wParam, lParam));
return HANDLED_MSG;

case IDCANCEL:
EndDialog(hWnd, GET_WM_COMMAND_ID(wParam, lParam));
return HANDLED_MSG;
}

// Now look for notification messages
switch (GET_WM_COMMAND_CMD(wParam, lParam))
{
case EN_CHANGE:
{
pThis->EnableTable();
return HANDLED_MSG;
}
}
}
}

return UNHANDLED_MSG;
}




/////////////////////////////////////////////////////////////////////////////
// BOOL CS3Dialog::RefreshControls
//
/////////////////////////////////////////////////////////////////////////////
BOOL CS3Dialog::RefreshControls()
{
BOOLfConnected;

CTable* pCToTable = m_pCTableCopy->m_pCToTable;
CTable* pCFromTable = m_pCTableCopy->m_pCFromTable;

// Must have a connection to edit other controls
fConnected = pCToTable->IsConnected();

//Enable certain dialog controls, only if connected
EnableWindow(GetDlgItem(m_hWnd, IDT_TO_TITLE),fConnected);
EnableWindow(GetDlgItem(m_hWnd, IDE_TO_TABLE),fConnected);

// Show user the connection string, and enable Next that requires connection
if(fConnected)
{
wSetDlgItemText(m_hWnd, IDT_CONNECT, wsz_CONNECT_STRING_,
pCToTable->m_pCDataSource->m_pwszDataSource,
pCToTable->m_pCDataSource->m_pwszUserName,
pCToTable->m_pCDataSource->m_pwszPassword,
pCToTable->m_pCDataSource->m_pwszDBMS,
pCToTable->m_pCDataSource->m_pwszDBMSVer);
}
else
{
wSetDlgItemText(m_hWnd, IDT_CONNECT, wsz_NOT_CONNECTED);
}

if(pCFromTable->m_pCDataSource->m_pwszProvider)
{
wSetDlgItemText(m_hWnd, IDT_TO_PROVIDER, wsz_PROVIDER_STRING_,
pCFromTable->m_pCDataSource->m_pwszProvider,
pCFromTable->m_pCDataSource->m_pwszProviderVer,
pCFromTable->m_pCDataSource->m_pwszProviderOLEDBVer);
}
else
{
wSetDlgItemText(m_hWnd, IDT_TO_PROVIDER, wsz_NO_PROVIDER);
}

// Set the from info
wSetDlgItemText(m_hWnd, IDT_FROM_INFO, L"%s, %s",
pCFromTable->m_pCDataSource->m_pwszDataSource,
pCFromTable->m_wszQualTableName);

// If there is already a CTable from previous selections, just use that one
wSetDlgItemText(m_hWnd, IDE_TO_TABLE, pCToTable->m_wszTableName[0] ? pCToTable->m_wszTableName :pCFromTable->m_wszTableName);

// Determine if there is enough information to move on
return EnableTable();
}




/////////////////////////////////////////////////////////////////////////////
// BOOL CS3Dialog::Connect
//
/////////////////////////////////////////////////////////////////////////////
BOOL CS3Dialog::Connect()
{
BOOL fConnected = FALSE;

CDataSource* pCToDataSource = m_pCTableCopy->m_pCToTable->m_pCDataSource;
CDataSource* pCFromDataSource = m_pCTableCopy->m_pCFromTable->m_pCDataSource;

//Connect to the DataSource
HRESULT hr = m_pCTableCopy->m_pCToTable->Connect(m_hWnd);

//If Connected
if(SUCCEEDED(hr))
{
fConnected = TRUE;

// Verify we can use this data source
if(pCToDataSource->m_fReadOnly)
{
wMessageBox(m_hWnd, MB_ICONEXCLAMATION | MB_OK, wsz_ERROR,
wsz_READONLY_DATASOURCE_, pCToDataSource->m_pwszDataSource);

fConnected = FALSE;
}

//See if this is a similar DSN than the Source
//If DSN's are not similar, then we need to translate
m_pCTableCopy->m_fTranslate = !pCToDataSource->IsSimilar(pCFromDataSource);
}

return fConnected;
}


/////////////////////////////////////////////////////////////////////////////
// BOOL CS3Dialog::EnableTable
//
/////////////////////////////////////////////////////////////////////////////
BOOL CS3Dialog::EnableTable()
{
BOOL fConnected = m_pCTableCopy->m_pCToTable->IsConnected();

EnableWindow(GetDlgItem(m_hWnd, IDOK),
fConnected && SendDlgItemMessage(m_hWnd, IDE_TO_TABLE, WM_GETTEXTLENGTH, 0, 0L));

return TRUE;
}