viewform.cpp 7.5 KB

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