home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / viewform.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  8KB  |  288 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #include "occimpl.h"
  13.  
  14. #ifdef AFX_CORE2_SEG
  15. #pragma code_seg(AFX_CORE2_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #define new DEBUG_NEW
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26.  
  27. BEGIN_MESSAGE_MAP(CFormView, CScrollView)
  28.     //{{AFX_MSG_MAP(CFormView)
  29.     ON_WM_SETFOCUS()
  30.     ON_WM_CREATE()
  31.     //}}AFX_MSG_MAP
  32. #ifndef _AFX_NO_CTL3D_SUPPORT
  33.     ON_MESSAGE(WM_QUERY3DCONTROLS, OnQuery3dControls)
  34. #endif
  35. #ifndef _AFX_NO_OCC_SUPPORT
  36.     ON_MESSAGE(WM_INITDIALOG, HandleInitDialog)
  37. #endif
  38. END_MESSAGE_MAP()
  39.  
  40. CFormView::CFormView(LPCTSTR lpszTemplateName)
  41. {
  42.     m_lpszTemplateName = lpszTemplateName;
  43.     m_pCreateContext = NULL;
  44.     m_hWndFocus = NULL;     // focus window is unknown
  45. }
  46.  
  47. CFormView::CFormView(UINT nIDTemplate)
  48. {
  49.     ASSERT_VALID_IDR(nIDTemplate);
  50.     m_lpszTemplateName = MAKEINTRESOURCE(nIDTemplate);
  51.     m_pCreateContext = NULL;
  52.     m_hWndFocus = NULL;     // focus window is unknown
  53. }
  54.  
  55. // virtual override of CWnd::Create
  56. BOOL CFormView::Create(LPCTSTR /*lpszClassName*/, LPCTSTR /*lpszWindowName*/,
  57.     DWORD dwRequestedStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
  58.     CCreateContext* pContext)
  59. {
  60.     ASSERT(pParentWnd != NULL);
  61.     ASSERT(m_lpszTemplateName != NULL);
  62.  
  63.     m_pCreateContext = pContext;    // save state for later OnCreate
  64.  
  65. #ifdef _DEBUG
  66.     // dialog template must exist and be invisible with WS_CHILD set
  67.     if (!_AfxCheckDialogTemplate(m_lpszTemplateName, TRUE))
  68.     {
  69.         ASSERT(FALSE);          // invalid dialog template name
  70.         PostNcDestroy();        // cleanup if Create fails too soon
  71.         return FALSE;
  72.     }
  73. #endif //_DEBUG
  74.  
  75.     // initialize common controls
  76.     VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG));
  77.     AfxDeferRegisterClass(AFX_WNDCOMMCTLSNEW_REG);
  78.  
  79.     // call PreCreateWindow to get prefered extended style
  80.     CREATESTRUCT cs; memset(&cs, 0, sizeof(CREATESTRUCT));
  81.     if (dwRequestedStyle == 0)
  82.         dwRequestedStyle = AFX_WS_DEFAULT_VIEW;
  83.     cs.style = dwRequestedStyle;
  84.     if (!PreCreateWindow(cs))
  85.         return FALSE;
  86.  
  87.     // create a modeless dialog
  88.     if (!CreateDlg(m_lpszTemplateName, pParentWnd))
  89.         return FALSE;
  90.  
  91.     m_pCreateContext = NULL;
  92.  
  93.     // we use the style from the template - but make sure that
  94.     //  the WS_BORDER bit is correct
  95.     // the WS_BORDER bit will be whatever is in dwRequestedStyle
  96.     ModifyStyle(WS_BORDER|WS_CAPTION, cs.style & (WS_BORDER|WS_CAPTION));
  97.     ModifyStyleEx(WS_EX_CLIENTEDGE, cs.dwExStyle & WS_EX_CLIENTEDGE);
  98.  
  99.     SetDlgCtrlID(nID);
  100.  
  101.     CRect rectTemplate;
  102.     GetWindowRect(rectTemplate);
  103.     SetScrollSizes(MM_TEXT, rectTemplate.Size());
  104.  
  105.     // initialize controls etc
  106.     if (!ExecuteDlgInit(m_lpszTemplateName))
  107.         return FALSE;
  108.  
  109.     // force the size requested
  110.     SetWindowPos(NULL, rect.left, rect.top,
  111.         rect.right - rect.left, rect.bottom - rect.top,
  112.         SWP_NOZORDER|SWP_NOACTIVATE);
  113.  
  114.     // make visible if requested
  115.     if (dwRequestedStyle & WS_VISIBLE)
  116.         ShowWindow(SW_NORMAL);
  117.  
  118.     return TRUE;
  119. }
  120.  
  121. void CFormView::OnInitialUpdate()
  122. {
  123.     ASSERT_VALID(this);
  124.  
  125.     if (!UpdateData(FALSE))
  126.         TRACE0("UpdateData failed during formview initial update.\n");
  127.  
  128.     CScrollView::OnInitialUpdate();
  129. }
  130.  
  131. int CFormView::OnCreate(LPCREATESTRUCT lpcs)
  132. {
  133.     // since we can't get the create context parameter passed in
  134.     //  through CreateDialog, we use a temporary member variable
  135.     lpcs->lpCreateParams = (LPVOID)m_pCreateContext;
  136.     return CScrollView::OnCreate(lpcs);
  137. }
  138.  
  139. void CFormView::OnActivateView(
  140.     BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
  141. {
  142.     if (SaveFocusControl())
  143.         return;     // don't call base class when focus is already set
  144.  
  145.     CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  146. }
  147.  
  148. void CFormView::OnActivateFrame(UINT nState, CFrameWnd* /*pFrameWnd*/)
  149. {
  150.     if (nState == WA_INACTIVE)
  151.         SaveFocusControl();     // save focus when frame loses activation
  152. }
  153.  
  154. BOOL CFormView::SaveFocusControl()
  155. {
  156.     // save focus window if focus is on this window's controls
  157.     HWND hWndFocus = ::GetFocus();
  158.     if (hWndFocus != NULL && ::IsChild(m_hWnd, hWndFocus))
  159.     {
  160.         m_hWndFocus = hWndFocus;
  161.         return TRUE;
  162.     }
  163.     return FALSE;
  164. }
  165.  
  166. void CFormView::OnSetFocus(CWnd*)
  167. {
  168.     if (!::IsWindow(m_hWndFocus) || !::IsChild(m_hWnd, m_hWndFocus))
  169.     {
  170.         // invalid or unknown focus window... let windows handle it
  171.         m_hWndFocus = NULL;
  172.         Default();
  173.         return;
  174.     }
  175.     // otherwise, set focus to the last known focus window
  176.     ::SetFocus(m_hWndFocus);
  177. }
  178.  
  179. BOOL CFormView::PreTranslateMessage(MSG* pMsg)
  180. {
  181.     ASSERT(pMsg != NULL);
  182.     ASSERT_VALID(this);
  183.     ASSERT(m_hWnd != NULL);
  184.  
  185.     // allow tooltip messages to be filtered
  186.     if (CView::PreTranslateMessage(pMsg))
  187.         return TRUE;
  188.  
  189.     // don't translate dialog messages when in Shift+F1 help mode
  190.     CFrameWnd* pFrameWnd = GetTopLevelFrame();
  191.     if (pFrameWnd != NULL && pFrameWnd->m_bHelpMode)
  192.         return FALSE;
  193.  
  194.     // since 'IsDialogMessage' will eat frame window accelerators,
  195.     //   we call all frame windows' PreTranslateMessage first
  196.     pFrameWnd = GetParentFrame();   // start with first parent frame
  197.     while (pFrameWnd != NULL)
  198.     {
  199.         // allow owner & frames to translate before IsDialogMessage does
  200.         if (pFrameWnd->PreTranslateMessage(pMsg))
  201.             return TRUE;
  202.  
  203.         // try parent frames until there are no parent frames
  204.         pFrameWnd = pFrameWnd->GetParentFrame();
  205.     }
  206.  
  207.     // don't call IsDialogMessage if form is empty
  208.     if (::GetWindow(m_hWnd, GW_CHILD) == NULL)
  209.         return FALSE;
  210.  
  211.     // filter both messages to dialog and from children
  212.     return PreTranslateInput(pMsg);
  213. }
  214.  
  215. void CFormView::OnDraw(CDC* pDC)
  216. {
  217.     ASSERT_VALID(this);
  218.  
  219.     // do nothing - dialog controls will paint themselves,
  220.     //   and Windows dialog controls do not support printing
  221. #ifdef _DEBUG
  222.     if (pDC->IsPrinting())
  223.         TRACE0("Warning: CFormView does not support printing.\n");
  224. #endif
  225.  
  226.     UNUSED(pDC);     // unused in release build
  227. }
  228.  
  229. #ifndef _AFX_NO_OCC_SUPPORT
  230.  
  231. LRESULT CFormView::HandleInitDialog(WPARAM, LPARAM)
  232. {
  233.     Default();  // allow default to initialize first (common dialogs/etc)
  234.  
  235.     // create OLE controls
  236.     COccManager* pOccManager = afxOccManager;
  237.     if ((pOccManager != NULL) && (m_pOccDialogInfo != NULL))
  238.     {
  239.         if (!pOccManager->CreateDlgControls(this, m_lpszTemplateName,
  240.             m_pOccDialogInfo))
  241.         {
  242.             TRACE0("Warning: CreateDlgControls failed during form view init.\n");
  243.             return FALSE;
  244.         }
  245.     }
  246.  
  247.     return FALSE;   // don't set focus until later
  248. }
  249.  
  250. BOOL CFormView::SetOccDialogInfo(_AFX_OCC_DIALOG_INFO* pOccDialogInfo)
  251. {
  252.     m_pOccDialogInfo = pOccDialogInfo;
  253.     return TRUE;
  254. }
  255.  
  256. #endif //!_AFX_NO_OCC_SUPPORT
  257.  
  258. //////////////////////////////////////////////////////////////////////////
  259. // CFormView diagnostics
  260.  
  261. #ifdef _DEBUG
  262. void CFormView::Dump(CDumpContext& dc) const
  263. {
  264.     CView::Dump(dc);
  265.  
  266.     dc << "m_lpszTemplateName = ";
  267.     if (HIWORD(m_lpszTemplateName) == 0)
  268.         dc << (int)LOWORD((DWORD)m_lpszTemplateName);
  269.     else
  270.         dc << m_lpszTemplateName;
  271.  
  272.     dc << "\n";
  273. }
  274.  
  275. void CFormView::AssertValid() const
  276. {
  277.     CView::AssertValid();
  278. }
  279. #endif
  280.  
  281. #ifdef AFX_INIT_SEG
  282. #pragma code_seg(AFX_INIT_SEG)
  283. #endif
  284.  
  285. IMPLEMENT_DYNAMIC(CFormView, CScrollView)
  286.  
  287. //////////////////////////////////////////////////////////////////////////
  288.