home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************/
- /* */
- /* TurboCAD for Windows */
- /* Copyright (c) 1993 - 2001 */
- /* International Microcomputer Software, Inc. */
- /* (IMSI) */
- /* All rights reserved. */
- /* */
- /******************************************************************/
-
- // EventDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "Events.h"
- #include "EventDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- extern CTCEventsApp theApp;
-
- static void DDX_EventMask(CDataExchange* pDX, UINT nIDCFirst, UINT nIDCLast, DWORD& value)
- {
- HWND hWndDlg = pDX->m_pDlgWnd->m_hWnd;
- DWORD dwBit = 1;
- UINT nID;
- HWND hWndButton;
- if (pDX->m_bSaveAndValidate)
- {
- value = 0;
- for (nID = nIDCFirst; nID <= nIDCLast; ++nID, dwBit <<= 1)
- {
- hWndButton = ::GetDlgItem(hWndDlg, nID);
- if (::SendMessage(hWndButton, BM_GETCHECK, 0, 0) != 0)
- value |= dwBit;
- }
- }
- else
- {
- for (nID = nIDCFirst; nID <= nIDCLast; ++nID, dwBit <<= 1)
- {
- hWndButton = ::GetDlgItem(hWndDlg, nID);
- ::SendMessage(hWndButton, BM_SETCHECK, (value & dwBit) ? 1 : 0, 0);
- }
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CTCEventsDlg dialog
-
- CTCEventsDlg::CTCEventsDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTCEventsDlg::IDD, pParent),
- m_dwEventConnection(0),
- m_dwEventMask(~0UL),
- m_pIApplication(NULL)
- {
- //{{AFX_DATA_INIT(CTCEventsDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- // TurboCAD SDK: make sure we release the application
- CTCEventsDlg::~CTCEventsDlg()
- {
- ReleaseApp();
- }
-
- void CTCEventsDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTCEventsDlg)
- //}}AFX_DATA_MAP
- DDX_EventMask(pDX, IDC_EVENT1, IDC_EVENT28, m_dwEventMask);
- }
-
- // Public function to allow event hander to post a message on our edit box
-
- void CTCEventsDlg::AppendText(LPCTSTR lpszText)
- {
- int nStart, nEnd;
- m_edit.SetSel(0, -1);
- m_edit.GetSel(nStart, nEnd);
- if (nEnd > 4000)
- {
- // Dump the buffer if it's too big
- Clear();
- nEnd = 0;
- }
- m_edit.SetSel(nEnd, nEnd);
- m_edit.ReplaceSel(lpszText);
- }
-
- // Clear our edit box
-
- void CTCEventsDlg::Clear()
- {
- m_edit.SetSel(0, -1);
- m_edit.Clear();
- }
-
- // Create an instance of the TurboCAD local server object
-
- IUnknown* CTCEventsDlg::CreateLocalServerObject()
- {
- // Try to create instance from TurboCAD
- CString strStr;
- IUnknown* pUnk = NULL;
- HRESULT hRes = ::CoCreateInstance(CLSID_Application, NULL,
- CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
-
- if (FAILED(hRes))
- {
- strStr.LoadString(IDS_COULDNOT_CREATETCADAPP);
- AfxMessageBox(strStr);
- return NULL;
- }
- return pUnk;
- }
-
- // Get the running instance of the TurboCAD local server object from the ROT.
-
- IUnknown* CTCEventsDlg::GetActiveObject(BOOL* pbCreateInstance)
- {
- // Try to connect to running TurboCAD
- IUnknown* pUnk = NULL;
- CString strStr;
- HRESULT hRes = ::GetActiveObject(CLSID_Application, NULL, &pUnk);
- if (SUCCEEDED(hRes))
- {
- // Did not create instance
- *pbCreateInstance = FALSE;
- return pUnk;
- }
- if (!*pbCreateInstance)
- {
- strStr.LoadString(IDS_TURBOCAD_ISNOT_ACTIVE);
- AfxMessageBox(strStr);
- return NULL;
- }
- return CreateLocalServerObject();
- }
-
- // Release our pointers
-
- void CTCEventsDlg::ReleaseApp()
- {
- DisconnectEvents();
- if (m_pIApplication != NULL)
- {
- m_pIApplication->Release();
- m_pIApplication = NULL;
- }
- }
-
- // Connect our app's event sink to the TurboCAD server.
- // Connect or start up the TurboCAD server if we need to.
-
- void CTCEventsDlg::ConnectEvents()
- {
- CString strStr;
- if (m_dwEventConnection != 0)
- {
- AfxMessageBox("Already connected.");
- return;
- }
- if (m_pIApplication == NULL)
- {
- BOOL bCreateInstance = TRUE;
- IUnknown* pUnk = GetActiveObject(&bCreateInstance);
- if (pUnk == NULL)
- return;
-
- HRESULT hRes = pUnk->QueryInterface(IID_IApplication, (void**)&m_pIApplication);
- pUnk->Release();
-
- if (SUCCEEDED(hRes) && m_pIApplication)
- {
- hRes = m_pIApplication->put_Visible (TRUE);
- }
-
- if (FAILED(hRes))
- {
- strStr.LoadString(IDS_TCADINIT_FAILED);
- AfxMessageBox(strStr); //"Object is not a TurboCAD application!"
- return;
- }
- else
- {
- if (bCreateInstance)
- {
- strStr.LoadString(IDS_CREATED_NEWTCAD_OBJECT);
- AfxMessageBox(strStr);//"Created new Application object."
- }
- else
- {
- strStr.LoadString(IDS_ATTACED_TO_RUNNED_TURBACAD);
- AfxMessageBox(strStr);//"Attached to already running Application object."
- }
- GetDlgItem(IDC_EXIT_APP)->EnableWindow(TRUE);
- }
- }
-
- IDispatch* pUnkEventSink = theApp.GetIDispatch(TRUE);
-
- UpdateData(TRUE);
- COleVariant var((long)m_dwEventMask);
- HRESULT hRes = m_pIApplication->ConnectEvents(pUnkEventSink,
- &var,
- (long*)&m_dwEventConnection);
- if (FAILED(hRes))
- {
- strStr.LoadString(IDS_CONNECTION_NOT_ESTABLISHED);
- AfxMessageBox(strStr);
- return;
- }
- else
- {
- TCHAR msg[256];
- strStr.LoadString(IDS_CONNECTION_ESTABLISHED);
- wsprintf(msg, strStr, m_dwEventMask);
- AfxMessageBox(msg);
- }
-
- CString strButtonText;
- strButtonText.LoadString(IDS_DISCONNECT);
- GetDlgItem(IDC_CONNECT_EVENTS)->SetWindowText(strButtonText);
- }
-
- // Release the event connection
-
- void CTCEventsDlg::DisconnectEvents()
- {
- CString strStr;
- if (m_pIApplication == NULL || m_dwEventConnection == 0)
- return;
-
- HRESULT hRes = m_pIApplication->DisconnectEvents(m_dwEventConnection);
- if (FAILED(hRes))
- {
- strStr.LoadString(IDS_CONNECTION_NOT_BROKEN);
- AfxMessageBox(strStr);
- return;
- }
-
- strStr.LoadString(IDS_CONNECTION_BROKEN);
- AfxMessageBox(strStr);
- m_dwEventConnection = 0;
-
- // DisconnectEvents can be called from destructor after DestroyWindow()...
- if (::IsWindow(m_hWnd))
- {
- CString strButtonText;
- strButtonText.LoadString(IDS_CONNECT);
- GetDlgItem(IDC_CONNECT_EVENTS)->SetWindowText(strButtonText);
- }
- }
-
- BEGIN_MESSAGE_MAP(CTCEventsDlg, CDialog)
- //{{AFX_MSG_MAP(CTCEventsDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CONNECT_EVENTS, OnConnectEvents)
- ON_BN_CLICKED(IDC_EXIT_APP, OnExitApp)
- //}}AFX_MSG_MAP
- ON_CONTROL_RANGE(BN_CLICKED, IDC_EVENT1, IDC_EVENT28, OnEventMask)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CTCEventsDlg message handlers
-
- // TurboCAD SDK: TurboCAD Class Identifier and Interface Identifier
- const CLSID CLSID_Application = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
- const CLSID CLSID_XApplication = {0x6A481801,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
- const IID IID_IApplication = {0x6A481101,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
-
- BOOL CTCEventsDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // Enable the buttons
- GetDlgItem(IDC_CONNECT_EVENTS)->EnableWindow(TRUE);
- GetDlgItem(IDC_EXIT_APP)->EnableWindow(FALSE);
-
- m_edit.SubclassDlgItem(IDC_EDIT_LOG, this);
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void CTCEventsDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CTCEventsDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CTCEventsDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- void CTCEventsDlg::OnConnectEvents()
- {
- if (m_dwEventConnection == 0)
- {
- ConnectEvents();
- }
- else
- {
- DisconnectEvents();
- }
- }
-
- void CTCEventsDlg::OnExitApp()
- {
- if (m_pIApplication == NULL)
- {
- AfxMessageBox("Start application first.");
- return;
- }
- DisconnectEvents();
- m_pIApplication->Quit();
- ReleaseApp();
- GetDlgItem(IDC_EXIT_APP)->EnableWindow(FALSE);
- }
-
- void CTCEventsDlg::OnEventMask(UINT nIDC)
- {
- // Change the connection if we're connected...
- if (m_dwEventConnection != 0)
- {
- DisconnectEvents();
- ConnectEvents();
- }
- }
-