home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / daotable / daotdlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  13KB  |  469 lines

  1. // DAOTDlg.cpp : implementation file
  2. //
  3. // Main dialog of DAOTable--most other dialogs are entered
  4. // from the dialog implemented here.
  5. //
  6. // This is a part of the Microsoft Foundation Classes C++ library.
  7. // Copyright (C) 1992-1998 Microsoft Corporation
  8. // All rights reserved.
  9. //
  10. // This source code is only intended as a supplement to the
  11. // Microsoft Foundation Classes Reference and related
  12. // electronic documentation provided with the library.
  13. // See these sources for detailed information regarding the
  14. // Microsoft Foundation Classes product.
  15.  
  16. #include "stdafx.h"
  17. #include "DAOTable.h"
  18. #include "DAOTDlg.h"
  19.  
  20. #include "database.h"
  21. #include "tabledef.h"
  22. #include "querydef.h"
  23.  
  24. #include "addtbdlg.h"
  25. #include "listctrl.h"
  26. #include "addixdlg.h"
  27. #include "AddQyDlg.h"
  28. #include "AddDbDlg.h"
  29.  
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #undef THIS_FILE
  33. static char THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CAboutDlg dialog used for App About
  38.  
  39. class CAboutDlg : public CDialog
  40. {
  41. public:
  42.     CAboutDlg();
  43.  
  44. // Dialog Data
  45.     //{{AFX_DATA(CAboutDlg)
  46.     enum { IDD = IDD_ABOUTBOX };
  47.     //}}AFX_DATA
  48.  
  49.     // ClassWizard generated virtual function overrides
  50.     //{{AFX_VIRTUAL(CAboutDlg)
  51.     protected:
  52.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  53.     //}}AFX_VIRTUAL
  54.  
  55. // Implementation
  56. protected:
  57.     //{{AFX_MSG(CAboutDlg)
  58.     //}}AFX_MSG
  59.     DECLARE_MESSAGE_MAP()
  60. };
  61.  
  62. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  63. {
  64.     //{{AFX_DATA_INIT(CAboutDlg)
  65.     //}}AFX_DATA_INIT
  66. }
  67.  
  68. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  69. {
  70.     CDialog::DoDataExchange(pDX);
  71.     //{{AFX_DATA_MAP(CAboutDlg)
  72.     //}}AFX_DATA_MAP
  73. }
  74.  
  75. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  76.     //{{AFX_MSG_MAP(CAboutDlg)
  77.         // No message handlers
  78.     //}}AFX_MSG_MAP
  79. END_MESSAGE_MAP()
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CDAOTableDlg dialog
  83.  
  84. CDAOTableDlg::CDAOTableDlg(CWnd* pParent /*=NULL*/)
  85.     : CDialog(CDAOTableDlg::IDD, pParent)
  86. {
  87.     //{{AFX_DATA_INIT(CDAOTableDlg)
  88.     m_strDatabaseName = _T("");
  89.     m_strTableName = _T("");
  90.     m_strQueryName = _T("");
  91.     //}}AFX_DATA_INIT
  92.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  93.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  94.  
  95.     // initialize the database pointer
  96.     m_pDatabase = NULL;
  97. }
  98.  
  99. void CDAOTableDlg::DoDataExchange(CDataExchange* pDX)
  100. {
  101.     CDialog::DoDataExchange(pDX);
  102.     //{{AFX_DATA_MAP(CDAOTableDlg)
  103.     DDX_Control(pDX, IDC_COMBO_QUERY_NAME, m_QueryNameComboControl);
  104.     DDX_Control(pDX, IDC_COMBO_TABLE_NAME, m_TableNameComboControl);
  105.     DDX_Text(pDX, IDC_EDIT_DATABASE_NAME, m_strDatabaseName);
  106.     DDX_Text(pDX, IDC_COMBO_TABLE_NAME, m_strTableName);
  107.     DDX_Text(pDX, IDC_COMBO_QUERY_NAME, m_strQueryName);
  108.     //}}AFX_DATA_MAP
  109. }
  110.  
  111. BEGIN_MESSAGE_MAP(CDAOTableDlg, CDialog)
  112.     //{{AFX_MSG_MAP(CDAOTableDlg)
  113.     ON_WM_SYSCOMMAND()
  114.     ON_WM_DESTROY()
  115.     ON_WM_PAINT()
  116.     ON_WM_QUERYDRAGICON()
  117.     ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
  118.     ON_BN_CLICKED(IDC_BUTTON_TABLE_FIELDS, OnButtonTableFields)
  119.     ON_BN_CLICKED(IDC_BUTTON_TABLE_INDEXES, OnButtonTableIndexes)
  120.     ON_BN_CLICKED(IDC_BUTTON_QUERY_DEFINITION, OnButtonQueryDefinition)
  121.     ON_CBN_DROPDOWN(IDC_COMBO_TABLE_NAME, OnDropdownComboTableName)
  122.     ON_CBN_DROPDOWN(IDC_COMBO_QUERY_NAME, OnDropdownComboQueryName)
  123.     ON_BN_CLICKED(IDC_BUTTON_DELETE_TABLE, OnButtonDeleteTable)
  124.     //}}AFX_MSG_MAP
  125. END_MESSAGE_MAP()
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CDAOTableDlg message handlers
  130.  
  131. BOOL CDAOTableDlg::OnInitDialog()
  132. {
  133.     CDialog::OnInitDialog();
  134.  
  135.     // Add "About..." menu item to system menu.
  136.  
  137.     // IDM_ABOUTBOX must be in the system command range.
  138.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  139.     ASSERT(IDM_ABOUTBOX < 0xF000);
  140.  
  141.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  142.     CString strAboutMenu;
  143.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  144.     if (!strAboutMenu.IsEmpty())
  145.     {
  146.         pSysMenu->AppendMenu(MF_SEPARATOR);
  147.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  148.     }
  149.  
  150.     // Set the icon for this dialog.  The framework does this automatically
  151.     //  when the application's main window is not a dialog
  152.     SetIcon(m_hIcon, TRUE);         // Set big icon
  153.     SetIcon(m_hIcon, FALSE);        // Set small icon
  154.  
  155.     // set focus to name-of-database edit box
  156.     CEdit *pEdit;
  157.     pEdit = (CEdit *)GetDlgItem(IDC_EDIT_DATABASE_NAME);
  158.     if (pEdit != NULL)
  159.         pEdit->SetFocus();
  160.  
  161.     return FALSE;  // return TRUE  unless you set the focus to a control
  162. }
  163.  
  164. void CDAOTableDlg::OnSysCommand(UINT nID, LPARAM lParam)
  165. {
  166.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  167.     {
  168.         CAboutDlg dlgAbout;
  169.         dlgAbout.DoModal();
  170.     }
  171.     else
  172.     {
  173.         CDialog::OnSysCommand(nID, lParam);
  174.     }
  175. }
  176.  
  177. void CDAOTableDlg::OnDestroy()
  178. {
  179.     WinHelp(0L, HELP_QUIT);
  180.     CDialog::OnDestroy();
  181. }
  182.  
  183. // If you add a minimize button to your dialog, you will need the code below
  184. //  to draw the icon.  For MFC applications using the document/view model,
  185. //  this is automatically done for you by the framework.
  186.  
  187. void CDAOTableDlg::OnPaint()
  188. {
  189.     if (IsIconic())
  190.     {
  191.         CPaintDC dc(this); // device context for painting
  192.  
  193.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  194.  
  195.         // Center icon in client rectangle
  196.         int cxIcon = GetSystemMetrics(SM_CXICON);
  197.         int cyIcon = GetSystemMetrics(SM_CYICON);
  198.         CRect rect;
  199.         GetClientRect(&rect);
  200.         int x = (rect.Width() - cxIcon + 1) / 2;
  201.         int y = (rect.Height() - cyIcon + 1) / 2;
  202.  
  203.         // Draw the icon
  204.         dc.DrawIcon(x, y, m_hIcon);
  205.     }
  206.     else
  207.     {
  208.         CDialog::OnPaint();
  209.     }
  210. }
  211.  
  212. // The system calls this to obtain the cursor to display while the user drags
  213. //  the minimized window.
  214. HCURSOR CDAOTableDlg::OnQueryDragIcon()
  215. {
  216.     return (HCURSOR) m_hIcon;
  217. }
  218.  
  219.  
  220. // user selected the "Connect" button to
  221. // a) connect to a specified database file
  222. // b) create a new database file
  223. // c) browse for a database file to open
  224. void CDAOTableDlg::OnButtonConnect()
  225. {
  226.     // close any open database
  227.     closeDatabase(&m_pDatabase);
  228.  
  229.     // get the database name (if any) from the dialog control
  230.     UpdateData(TRUE);
  231.  
  232.     // if user specified a name, then open it directly,
  233.     if (m_strDatabaseName != _T(""))
  234.     {
  235.         // now open the database if possible
  236.         int retCode = openDatabase(&m_pDatabase, m_strDatabaseName, FALSE);
  237.         if (SUCCESS == retCode)
  238.         {
  239.             // once a database is opened, other controls are enabled
  240.             enableControlsForOpenDatabase(TRUE);
  241.         }
  242.         else if (FAILURE == retCode) // user has specified a new database to create
  243.         {
  244.             // just to keep this function short, farm rest of
  245.             // process out to helper function--create a new database
  246.             createNewDatabase();
  247.         }
  248.         else // FATAL -- nothing can be done
  249.             // if database not open, other controls are disabled
  250.             enableControlsForOpenDatabase(FALSE);
  251.     }
  252.     else
  253.     {
  254.         // allow user to select database to open
  255.         CFileDialog dlg (TRUE, _T("mdb"), NULL, OFN_HIDEREADONLY,
  256.                          _T("Access Database Files (*.mdb)|*.mdb||"),
  257.                          this);
  258.  
  259.         // if user selected a file then open it as a database
  260.         if (IDOK == dlg.DoModal())
  261.         {
  262.             // get full path to database file
  263.             m_strDatabaseName = dlg.GetPathName();
  264.  
  265.             // display the name of the database on the dialog
  266.             UpdateData(FALSE);
  267.  
  268.             // now open the database if possible
  269.             int retCode = openDatabase(&m_pDatabase, m_strDatabaseName);
  270.             if (SUCCESS == retCode)
  271.             {
  272.                 // once a database is opened, other controls are enabled
  273.                 enableControlsForOpenDatabase(TRUE);
  274.             }
  275.             else    // can't interact with an unopened database
  276.             {
  277.                 // if database not open, other controls are disabled
  278.                 enableControlsForOpenDatabase(FALSE);
  279.             }
  280.         }
  281.     }
  282. }
  283.  
  284. // helper function --  create a new database
  285. void CDAOTableDlg::createNewDatabase()
  286. {
  287.     // see if user wants to create a new database
  288.     if (IDYES == AfxMessageBox(_T("Specified database doesn't exist.  Do you want to create a new database?"), MB_YESNO))
  289.     {
  290.         // display database creation dialog
  291.         CAddDatabaseDlg dlg(&m_pDatabase, m_strDatabaseName);
  292.         dlg.DoModal();
  293.  
  294.         // if the database was opened, then it won't be NULL
  295.         if (m_pDatabase != NULL)
  296.         {
  297.             // once a database is opened, other controls are enabled
  298.             enableControlsForOpenDatabase(TRUE);
  299.         }
  300.         else  // only other option is to let user try again
  301.         {
  302.             // if database not open, other controls are disabled
  303.             enableControlsForOpenDatabase(FALSE);
  304.         }
  305.     }
  306. }
  307.  
  308. // can only interact with tables and queries if you have an open database
  309. // so enable/disable table and query controls conditionally
  310. void CDAOTableDlg::enableControlsForOpenDatabase(BOOL bEnable /*= TRUE*/)
  311. {
  312.     // handle controls subclassed via DDX_Control
  313.     m_QueryNameComboControl.EnableWindow(bEnable);
  314.     m_TableNameComboControl.EnableWindow(bEnable);
  315.  
  316.     // must enable/disable buttons directly
  317.     CButton *pButton;
  318.     pButton = (CButton *) GetDlgItem(IDC_BUTTON_TABLE_FIELDS);
  319.     if (pButton != NULL)
  320.         pButton->EnableWindow(bEnable);
  321.     pButton = (CButton *) GetDlgItem(IDC_BUTTON_TABLE_INDEXES);
  322.     if (pButton != NULL)
  323.         pButton->EnableWindow(bEnable);
  324.     pButton = (CButton *) GetDlgItem(IDC_BUTTON_DELETE_TABLE);
  325.     if (pButton != NULL)
  326.         pButton->EnableWindow(bEnable);
  327.     pButton = (CButton *) GetDlgItem(IDC_BUTTON_QUERY_DEFINITION);
  328.     if (pButton != NULL)
  329.         pButton->EnableWindow(bEnable);
  330. }
  331.  
  332. // user selected to close the window without pressing "Done"
  333. // button
  334. void CDAOTableDlg::OnCancel()
  335. {
  336.     // close the database in case it is open
  337.     closeDatabase(&m_pDatabase);
  338.  
  339.     CDialog::OnCancel();
  340. }
  341.  
  342. // user selected the "done" button
  343. void CDAOTableDlg::OnOK()
  344. {
  345.     // close the database in case it is open
  346.     closeDatabase(&m_pDatabase);
  347.  
  348.     CDialog::OnOK();
  349. }
  350.  
  351. // user selected the "Fields" button to add or view the
  352. // fields that make up the table definition for the specified
  353. // table
  354. void CDAOTableDlg::OnButtonTableFields()
  355. {
  356.     UpdateData(TRUE);
  357.  
  358.     // table specified
  359.     CDlgAddTable dlg(m_pDatabase, m_strTableName);
  360.     dlg.DoModal();
  361. }
  362.  
  363. // user selected the "Indexes" button to add, delete, or view
  364. // indexes for the table specified
  365. void CDAOTableDlg::OnButtonTableIndexes()
  366. {
  367.     UpdateData(TRUE);
  368.  
  369.     // can only continue if this is an existing table
  370.     // new tables must have fields added first
  371.     if (IsExistentTable(m_pDatabase, m_strTableName))
  372.     {
  373.         // existing table specified, so allow interaction
  374.         CAddIndexDlg dlg(m_pDatabase, m_strTableName);
  375.         dlg.DoModal();
  376.     }
  377.     else
  378.         AfxMessageBox(_T("You can only define indexes on existing tables."));
  379. }
  380.  
  381. // user selected "Definition" of query button to view or delete an
  382. // existing querydef or add a new one
  383. void CDAOTableDlg::OnButtonQueryDefinition()
  384. {
  385.     UpdateData(TRUE);
  386.  
  387.     // query specified, so view or create as appropriate
  388.     CAddQueryDlg dlg(m_pDatabase, m_strQueryName);
  389.     dlg.DoModal();
  390.  
  391.     // clear the query name combobox since user may have
  392.     // changed the name
  393.     m_strQueryName = "";
  394.     UpdateData(FALSE);
  395. }
  396.  
  397. // user wants to browse existing tables so fill in the
  398. // combobox
  399. void CDAOTableDlg::OnDropdownComboTableName()
  400. {
  401.     int index = 0;
  402.     CDaoTableDefInfo TableInfo;
  403.  
  404.     // clear out any items in control
  405.     m_TableNameComboControl.ResetContent();
  406.  
  407.     // while there are tables to get information on, continue
  408.     // this is a get info by index, not name and no error
  409.     // reporting is done (specified by FALSE)
  410.     while (getTableInfo(m_pDatabase, &TableInfo, index, FALSE))
  411.     {
  412.         // add to list box if not a system object (i.e. system table)
  413.         // or hidden object
  414.         if (!(TableInfo.m_lAttributes & dbSystemObject)
  415.             && !(TableInfo.m_lAttributes & dbHiddenObject))
  416.             m_TableNameComboControl.AddString(TableInfo.m_strName);
  417.  
  418.         // continue loop
  419.         index += 1;
  420.     }
  421. }
  422.  
  423. // user wants to browse existing queries so fill in the
  424. // combobox
  425. void CDAOTableDlg::OnDropdownComboQueryName()
  426. {
  427.     int index = 0;
  428.     CDaoQueryDefInfo QueryInfo;
  429.  
  430.     // clear out any items in control
  431.     m_QueryNameComboControl.ResetContent();
  432.  
  433.     // while there are tables to get information on, continue
  434.     // this is a get info by index, not name and no error
  435.     // reporting is done (specified by FALSE)
  436.     while (getQueryInfo(m_pDatabase, &QueryInfo, index, FALSE))
  437.     {
  438.         // add to list box
  439.         m_QueryNameComboControl.AddString(QueryInfo.m_strName);
  440.  
  441.         // continue loop
  442.         index += 1;
  443.     }
  444. }
  445.  
  446. // user selected to delete the current tabledef--prompt
  447. // for acceptance before proceeding
  448. void CDAOTableDlg::OnButtonDeleteTable()
  449. {
  450.     UpdateData(TRUE);
  451.  
  452.     // can only delete existing tables
  453.     if (IsExistentTable(m_pDatabase, m_strTableName))
  454.     {
  455.         // is user sure?
  456.         if (IDYES == AfxMessageBox(_T("Delete current table?  This will delete all fields and indexes for this table."), MB_YESNO))
  457.         {
  458.             // delete the table
  459.             deleteTable(m_pDatabase, m_strTableName);
  460.  
  461.             // clear the table name
  462.             m_strTableName = _T("");
  463.  
  464.             // update the dialog controls to erase deleted field
  465.             UpdateData(FALSE);
  466.         }
  467.     }
  468. }
  469.