Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.0 KiB

  1. // filedlg.cpp : header file
  2. //
  3. // Copyright (C) 1992-1999 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // This file is to support an extended file save dialog with a
  7. // "Use this format by default" checkbox
  8. #include "stdafx.h"
  9. #include "wordpad.h"
  10. #include "filedlg.h"
  11. #include "ddxm.h"
  12. #include "helpids.h"
  13. #include <dlgs.h>
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char BASED_CODE THIS_FILE[] = __FILE__;
  17. #endif
  18. DWORD const CWordpadFileDialog::m_nHelpIDs[] =
  19. {
  20. IDC_DEFAULT_FORMAT, IDH_WORDPAD_DEFAULT_FORMAT,
  21. 0, 0
  22. };
  23. int CWordpadFileDialog::m_defaultDoctype = RD_DEFAULT;
  24. BEGIN_MESSAGE_MAP(CWordpadFileDialog, CFileDialog)
  25. //{{AFX_MSG_MAP(CWordpadFileDialog)
  26. ON_BN_CLICKED(IDC_DEFAULT_FORMAT, OnDefaultFormatClicked)
  27. ON_MESSAGE(WM_HELP, OnHelp)
  28. ON_MESSAGE(WM_CONTEXTMENU, OnHelpContextMenu)
  29. //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31. IMPLEMENT_DYNAMIC(CWordpadFileDialog, CFileDialog)
  32. CWordpadFileDialog::CWordpadFileDialog(BOOL bOpenFileDialog)
  33. : CFileDialog(bOpenFileDialog)
  34. {
  35. m_ofn.Flags |= OFN_ENABLESIZING;
  36. if (!m_bOpenFileDialog)
  37. {
  38. m_ofn.Flags |= OFN_ENABLETEMPLATE;
  39. m_ofn.lpTemplateName = TEXT("DefaultFormatDialog");
  40. }
  41. m_doctype = GetDefaultFileType();
  42. }
  43. void CWordpadFileDialog::OnDefaultFormatClicked()
  44. {
  45. if (m_bOpenFileDialog)
  46. return;
  47. m_doctype = GetTypeFromIndex(
  48. m_openfilename.nFilterIndex - 1,
  49. m_bOpenFileDialog);
  50. GetDlgItem(IDC_DEFAULT_FORMAT)->EnableWindow(FALSE);
  51. // Move the focus to the filename combobox.
  52. GetParent()->GetDlgItem(cmb13)->SetFocus();
  53. }
  54. BOOL CWordpadFileDialog::OnFileNameOK()
  55. {
  56. BOOL ret = CFileDialog::OnFileNameOK();
  57. if (!m_bOpenFileDialog)
  58. {
  59. // returns 0 if ok, 1 if not ok...
  60. if (0 == ret)
  61. SetDefaultFileType(m_doctype);
  62. }
  63. return ret;
  64. }
  65. void CWordpadFileDialog::OnTypeChange()
  66. {
  67. CFileDialog::OnTypeChange();
  68. if (m_bOpenFileDialog)
  69. return;
  70. int type = GetTypeFromIndex(
  71. m_openfilename.nFilterIndex - 1,
  72. m_bOpenFileDialog);
  73. CWnd *checkbox = GetDlgItem(IDC_DEFAULT_FORMAT);
  74. checkbox->SendMessage(
  75. BM_SETCHECK,
  76. (type == m_doctype)
  77. ? BST_CHECKED
  78. : BST_UNCHECKED,
  79. 0);
  80. checkbox->EnableWindow(!(type == m_doctype));
  81. //
  82. // Change the extension of the filename presented to the user to match
  83. // the new type
  84. //
  85. // If the user has "Hide extensions of known types" set all this is moot
  86. SHELLFLAGSTATE flags;
  87. SHGetSettings(&flags, SSF_SHOWEXTENSIONS);
  88. if (!flags.fShowExtensions)
  89. return;
  90. CString filespec;
  91. CommDlg_OpenSave_GetSpec(
  92. GetParent()->GetSafeHwnd(),
  93. filespec.GetBufferSetLength(MAX_PATH),
  94. MAX_PATH);
  95. filespec.ReleaseBuffer();
  96. if (filespec.IsEmpty())
  97. return;
  98. CString extension = GetExtFromType(type);
  99. int extstart = filespec.ReverseFind(TEXT('.'));
  100. if (-1 == extstart)
  101. extstart = filespec.GetLength();
  102. if (filespec.Mid(extstart) == extension)
  103. return;
  104. filespec = filespec.Mid(0, extstart) + extension;
  105. CommDlg_OpenSave_SetControlText(
  106. GetParent()->GetSafeHwnd(),
  107. edt1,
  108. (LPCTSTR) filespec);
  109. }
  110. void CWordpadFileDialog::OnInitDone()
  111. {
  112. CFileDialog::OnInitDone();
  113. if (m_bOpenFileDialog)
  114. return;
  115. OnTypeChange();
  116. }
  117. LONG CWordpadFileDialog::OnHelp(WPARAM, LPARAM lParam)
  118. {
  119. ::WinHelp( (HWND)((LPHELPINFO)lParam)->hItemHandle,
  120. AfxGetApp()->m_pszHelpFilePath,
  121. HELP_WM_HELP, (DWORD_PTR)GetHelpIDs());
  122. return 0;
  123. }
  124. LONG CWordpadFileDialog::OnHelpContextMenu(WPARAM wParam, LPARAM)
  125. {
  126. ::WinHelp((HWND)wParam, AfxGetApp()->m_pszHelpFilePath,
  127. HELP_CONTEXTMENU, (DWORD_PTR)GetHelpIDs());
  128. return 0;
  129. }
  130. //
  131. // MFC assumes that when a common dialog is created, the first message that
  132. // gets sent to it's common message handing procedure will be to that window.
  133. // This isn't always true and although it attempts to handle this doesn't get
  134. // it right every time. Fix it, at least for file dialogs, by making a
  135. // private message procedure that does the necessary initialization and then
  136. // calls the original
  137. //
  138. UINT_PTR CALLBACK CWordpadFileDialog::FileDialogHookProc(
  139. HWND hWnd,
  140. UINT message,
  141. WPARAM wParam,
  142. LPARAM lParam)
  143. {
  144. CWordpadFileDialog *_this;
  145. if (WM_INITDIALOG == message)
  146. {
  147. _this = (CWordpadFileDialog *) ((OPENFILENAME *) lParam)->lCustData;
  148. _this->SubclassWindow(hWnd);
  149. }
  150. _this = (CWordpadFileDialog *) CWnd::FromHandlePermanent(hWnd);
  151. if (NULL != _this)
  152. return (_this->m_original_hook)(hWnd, message, wParam, lParam);
  153. else
  154. return 0;
  155. }
  156. INT_PTR CWordpadFileDialog::DoModal()
  157. {
  158. ASSERT_VALID(this);
  159. ASSERT(m_ofn.Flags & OFN_ENABLEHOOK);
  160. ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook
  161. #ifdef _MAC
  162. ASSERT((m_ofn.Flags & OFN_ALLOWMULTISELECT) == 0);
  163. #endif
  164. // WINBUG: This is a special case for the file open/save dialog,
  165. // which sometimes pumps while it is coming up but before it has
  166. // disabled the main window.
  167. HWND hWndFocus = ::GetFocus();
  168. BOOL bEnableParent = FALSE;
  169. m_ofn.hwndOwner = PreModal();
  170. AfxUnhookWindowCreate();
  171. if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner))
  172. {
  173. bEnableParent = TRUE;
  174. ::EnableWindow(m_ofn.hwndOwner, FALSE);
  175. }
  176. _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
  177. ASSERT(pThreadState->m_pAlternateWndInit == NULL);
  178. m_original_hook = m_ofn.lpfnHook;
  179. m_ofn.lpfnHook = FileDialogHookProc;
  180. m_ofn.lCustData = (LPARAM) this;
  181. ZeroMemory(&m_openfilename, sizeof(m_openfilename));
  182. CopyMemory(&m_openfilename, &m_ofn, sizeof(m_ofn));
  183. m_openfilename.lStructSize = sizeof(m_openfilename);
  184. int nResult;
  185. if (m_bOpenFileDialog)
  186. nResult = ::GetOpenFileName((OPENFILENAME*) &m_openfilename);
  187. else
  188. nResult = ::GetSaveFileName((OPENFILENAME*) &m_openfilename);
  189. CopyMemory(&m_ofn, &m_openfilename, sizeof(m_ofn));
  190. m_ofn.lStructSize = sizeof(m_ofn);
  191. m_ofn.lpfnHook = m_original_hook;
  192. if (nResult)
  193. ASSERT(pThreadState->m_pAlternateWndInit == NULL);
  194. pThreadState->m_pAlternateWndInit = NULL;
  195. // WINBUG: Second part of special case for file open/save dialog.
  196. if (bEnableParent)
  197. ::EnableWindow(m_ofn.hwndOwner, TRUE);
  198. if (::IsWindow(hWndFocus))
  199. ::SetFocus(hWndFocus);
  200. PostModal();
  201. return nResult ? nResult : IDCANCEL;
  202. }