home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / VIEWFORM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  8.4 KB  |  317 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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.     ON_MESSAGE(WM_QUERY3DCONTROLS, OnQuery3dControls)
  32.     //}}AFX_MSG_MAP
  33. #ifndef _AFX_NO_OCC_SUPPORT
  34.     ON_MESSAGE(WM_INITDIALOG, HandleInitDialog)
  35. #endif
  36. END_MESSAGE_MAP()
  37.  
  38. CFormView::CFormView(LPCTSTR lpszTemplateName)
  39. {
  40.     m_lpszTemplateName = lpszTemplateName;
  41.     m_pCreateContext = NULL;
  42.     m_hWndFocus = NULL;     // focus window is unknown
  43. }
  44.  
  45. CFormView::CFormView(UINT nIDTemplate)
  46. {
  47.     ASSERT_VALID_IDR(nIDTemplate);
  48.     m_lpszTemplateName = MAKEINTRESOURCE(nIDTemplate);
  49.     m_pCreateContext = NULL;
  50.     m_hWndFocus = NULL;     // focus window is unknown
  51. }
  52.  
  53. // virtual override of CWnd::Create
  54. BOOL CFormView::Create(LPCTSTR /*lpszClassName*/, LPCTSTR /*lpszWindowName*/,
  55.     DWORD dwRequestedStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
  56.     CCreateContext* pContext)
  57. {
  58.     ASSERT(pParentWnd != NULL);
  59.     ASSERT(m_lpszTemplateName != NULL);
  60.  
  61.     m_pCreateContext = pContext;    // save state for later OnCreate
  62.  
  63. #ifdef _DEBUG
  64.     // dialog template must exist and be invisible with WS_CHILD set
  65.     if (!_AfxCheckDialogTemplate(m_lpszTemplateName, TRUE))
  66.     {
  67.         ASSERT(FALSE);          // invalid dialog template name
  68.         PostNcDestroy();        // cleanup if Create fails too soon
  69.         return FALSE;
  70.     }
  71. #endif //_DEBUG
  72.  
  73. #ifdef _MAC
  74.     HINSTANCE hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
  75.     _AfxStripDialogCaption(hInst, m_lpszTemplateName);
  76. #endif
  77.  
  78.     // initialize common controls
  79.     VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG));
  80.  
  81.     // call PreCreateWindow to get prefered extended style
  82.     CREATESTRUCT cs; memset(&cs, 0, sizeof(CREATESTRUCT));
  83.     if (dwRequestedStyle == 0)
  84.         dwRequestedStyle = AFX_WS_DEFAULT_VIEW;
  85.     cs.style = dwRequestedStyle;
  86.     if (!PreCreateWindow(cs))
  87.         return FALSE;
  88.  
  89.     // create a modeless dialog
  90.     if (!CreateDlg(m_lpszTemplateName, pParentWnd))
  91.         return FALSE;
  92.  
  93.     m_pCreateContext = NULL;
  94.  
  95.     // we use the style from the template - but make sure that
  96.     //  the WS_BORDER bit is correct
  97.     // the WS_BORDER bit will be whatever is in dwRequestedStyle
  98.     ModifyStyle(WS_BORDER|WS_CAPTION, cs.style & (WS_BORDER|WS_CAPTION));
  99.     ModifyStyleEx(WS_EX_CLIENTEDGE, cs.dwExStyle & WS_EX_CLIENTEDGE);
  100.  
  101.     SetDlgCtrlID(nID);
  102.  
  103.     CRect rectTemplate;
  104.     GetWindowRect(rectTemplate);
  105.     SetScrollSizes(MM_TEXT, rectTemplate.Size());
  106.  
  107.     // initialize controls etc
  108.     if (!ExecuteDlgInit(m_lpszTemplateName))
  109.         return FALSE;
  110.  
  111.     // force the size requested
  112.     SetWindowPos(NULL, rect.left, rect.top,
  113.         rect.right - rect.left, rect.bottom - rect.top,
  114.         SWP_NOZORDER|SWP_NOACTIVATE);
  115.  
  116.     // make visible if requested
  117.     if (dwRequestedStyle & WS_VISIBLE)
  118.         ShowWindow(SW_NORMAL);
  119.  
  120.     return TRUE;
  121. }
  122.  
  123. #ifdef _MAC
  124. // Helper function that strips out the caption bit from dialog templates.
  125. // This is done so that WLM creates a true child window instead of a
  126. // top-level child window.
  127. void AFXAPI _AfxStripDialogCaption(HINSTANCE hInst, LPCTSTR lpszResource)
  128. {
  129.     ASSERT(lpszResource != NULL);
  130.  
  131.     HRSRC hResource = ::FindResource(hInst, lpszResource, RT_DIALOG);
  132.     if (hResource == NULL)
  133.         return;
  134.  
  135.     HGLOBAL hTemplate = LoadResource(hInst, hResource);
  136.     if (hTemplate == NULL)
  137.         return;
  138.  
  139.     DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)LockResource(hTemplate);
  140.  
  141.     // strip back to a simple border if the dialog was captioned
  142.     if (((DLGTEMPLATEEX*) pTemplate)->signature == 0xFFFF)
  143.     {
  144.         if ((((DLGTEMPLATEEX*) pTemplate)->style & WS_CAPTION) == WS_CAPTION)
  145.             ((DLGTEMPLATEEX*) pTemplate)->style &= ~WS_DLGFRAME;
  146.     }
  147.     else if ((pTemplate->style & WS_CAPTION) == WS_CAPTION)
  148.         pTemplate->style &= ~WS_DLGFRAME;
  149.  
  150.     // do not free the resource - we want it to remain in memory with our changes
  151. }
  152. #endif
  153.  
  154. void CFormView::OnInitialUpdate()
  155. {
  156.     ASSERT_VALID(this);
  157.  
  158.     if (!UpdateData(FALSE))
  159.         TRACE0("UpdateData failed during formview initial update.\n");
  160.  
  161.     CScrollView::OnInitialUpdate();
  162. }
  163.  
  164. int CFormView::OnCreate(LPCREATESTRUCT lpcs)
  165. {
  166.     // since we can't get the create context parameter passed in
  167.     //  through CreateDialog, we use a temporary member variable
  168.     lpcs->lpCreateParams = (LPVOID)m_pCreateContext;
  169.     return CScrollView::OnCreate(lpcs);
  170. }
  171.  
  172. void CFormView::OnActivateView(
  173.     BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
  174. {
  175.     if (SaveFocusControl())
  176.         return;     // don't call base class when focus is already set
  177.  
  178.     CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  179. }
  180.  
  181. void CFormView::OnActivateFrame(UINT nState, CFrameWnd* /*pFrameWnd*/)
  182. {
  183.     if (nState == WA_INACTIVE)
  184.         SaveFocusControl();     // save focus when frame loses activation
  185. }
  186.  
  187. BOOL CFormView::SaveFocusControl()
  188. {
  189.     // save focus window if focus is on this window's controls
  190.     HWND hWndFocus = ::GetFocus();
  191.     if (hWndFocus != NULL && ::IsChild(m_hWnd, hWndFocus))
  192.     {
  193.         m_hWndFocus = hWndFocus;
  194.         return TRUE;
  195.     }
  196.     return FALSE;
  197. }
  198.  
  199. void CFormView::OnSetFocus(CWnd*)
  200. {
  201.     if (!::IsWindow(m_hWndFocus))
  202.     {
  203.         // invalid or unknown focus window... let windows handle it
  204.         m_hWndFocus = NULL;
  205.         Default();
  206.         return;
  207.     }
  208.     // otherwise, set focus to the last known focus window
  209.     ::SetFocus(m_hWndFocus);
  210. }
  211.  
  212. BOOL CFormView::PreTranslateMessage(MSG* pMsg)
  213. {
  214.     ASSERT(pMsg != NULL);
  215.     ASSERT_VALID(this);
  216.     ASSERT(m_hWnd != NULL);
  217.  
  218.     // allow tooltip messages to be filtered
  219.     if (CView::PreTranslateMessage(pMsg))
  220.         return TRUE;
  221.  
  222.     // don't translate dialog messages when in Shift+F1 help mode
  223.     CFrameWnd* pFrameWnd = GetTopLevelFrame();
  224.     if (pFrameWnd != NULL && pFrameWnd->m_bHelpMode)
  225.         return FALSE;
  226.  
  227.     // since 'IsDialogMessage' will eat frame window accelerators,
  228.     //   we call all frame windows' PreTranslateMessage first
  229.     pFrameWnd = GetParentFrame();   // start with first parent frame
  230.     while (pFrameWnd != NULL)
  231.     {
  232.         // allow owner & frames to translate before IsDialogMessage does
  233.         if (pFrameWnd->PreTranslateMessage(pMsg))
  234.             return TRUE;
  235.  
  236.         // try parent frames until there are no parent frames
  237.         pFrameWnd = pFrameWnd->GetParentFrame();
  238.     }
  239.  
  240.     // filter both messages to dialog and from children
  241.     return PreTranslateInput(pMsg);
  242. }
  243.  
  244. void CFormView::OnDraw(CDC* pDC)
  245. {
  246.     ASSERT_VALID(this);
  247.  
  248.     // do nothing - dialog controls will paint themselves,
  249.     //   and Windows dialog controls do not support printing
  250. #ifdef _DEBUG
  251.     if (pDC->IsPrinting())
  252.         TRACE0("Warning: CFormView does not support printing.\n");
  253. #endif
  254.  
  255.     UNUSED(pDC);     // unused in release build
  256. }
  257.  
  258. #ifndef _AFX_NO_OCC_SUPPORT
  259.  
  260. LRESULT CFormView::HandleInitDialog(WPARAM, LPARAM)
  261. {
  262.     Default();  // allow default to initialize first (common dialogs/etc)
  263.  
  264.     // create OLE controls
  265.     COccManager* pOccManager = afxOccManager;
  266.     if ((pOccManager != NULL) && (m_pOccDialogInfo != NULL))
  267.     {
  268.         if (!pOccManager->CreateDlgControls(this, m_lpszTemplateName,
  269.             m_pOccDialogInfo))
  270.         {
  271.             TRACE0("Warning: CreateDlgControls failed during form view init.\n");
  272.             return FALSE;
  273.         }
  274.     }
  275.  
  276.     return FALSE;   // don't set focus until later
  277. }
  278.  
  279. BOOL CFormView::SetOccDialogInfo(_AFX_OCC_DIALOG_INFO* pOccDialogInfo)
  280. {
  281.     m_pOccDialogInfo = pOccDialogInfo;
  282.     return TRUE;
  283. }
  284.  
  285. #endif //!_AFX_NO_OCC_SUPPORT
  286.  
  287. //////////////////////////////////////////////////////////////////////////
  288. // CFormView diagnostics
  289.  
  290. #ifdef _DEBUG
  291. void CFormView::Dump(CDumpContext& dc) const
  292. {
  293.     CView::Dump(dc);
  294.  
  295.     dc << "m_lpszTemplateName = ";
  296.     if (HIWORD(m_lpszTemplateName) == 0)
  297.         dc << (int)LOWORD((DWORD)m_lpszTemplateName);
  298.     else
  299.         dc << m_lpszTemplateName;
  300.  
  301.     dc << "\n";
  302. }
  303.  
  304. void CFormView::AssertValid() const
  305. {
  306.     CView::AssertValid();
  307. }
  308. #endif
  309.  
  310. #ifdef AFX_INIT_SEG
  311. #pragma code_seg(AFX_INIT_SEG)
  312. #endif
  313.  
  314. IMPLEMENT_DYNAMIC(CFormView, CScrollView)
  315.  
  316. //////////////////////////////////////////////////////////////////////////
  317.