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.

288 lines
6.4 KiB

  1. // formatta.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. #include "stdafx.h"
  13. #include "wordpad.h"
  14. #include "formatta.h"
  15. #include "ddxm.h"
  16. #include "helpids.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21. const DWORD CFormatTabDlg::m_nHelpIDs[] =
  22. {
  23. IDC_BUTTON_SET, IDH_WORDPAD_TABSET,
  24. IDC_BUTTON_CLEAR, IDH_WORDPAD_TABCLEAR,
  25. IDC_BUTTON_CLEARALL, IDH_WORDPAD_TAB_CLEARALL,
  26. IDC_COMBO1, IDH_WORDPAD_TABSTOPS,
  27. IDC_BOX, (DWORD) -1,
  28. 0, 0
  29. };
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CFormatTabDlg dialog
  32. CFormatTabDlg::CFormatTabDlg(PARAFORMAT& pf, CWnd* pParent /*=NULL*/)
  33. : CCSDialog(CFormatTabDlg::IDD, pParent)
  34. {
  35. m_pf = pf;
  36. m_tabarray = new LONG[MAX_TAB_STOPS];
  37. m_nCount = 0;
  38. if (m_pf.dwMask & PFM_TABSTOPS)
  39. {
  40. m_nCount = m_pf.cTabCount;
  41. ASSERT(m_pf.cTabCount <= MAX_TAB_STOPS);
  42. for (int i=0;i<m_pf.cTabCount;i++)
  43. m_tabarray[i] = m_pf.rgxTabs[i];
  44. }
  45. //{{AFX_DATA_INIT(CFormatTabDlg)
  46. //}}AFX_DATA_INIT
  47. }
  48. CFormatTabDlg::~CFormatTabDlg()
  49. {
  50. delete [] m_tabarray;
  51. }
  52. void CFormatTabDlg::DoDataExchange(CDataExchange* pDX)
  53. {
  54. CCSDialog::DoDataExchange(pDX);
  55. //{{AFX_DATA_MAP(CFormatTabDlg)
  56. DDX_Control(pDX, IDC_BUTTON_CLEARALL, m_buttonClearAll);
  57. DDX_Control(pDX, IDC_BUTTON_SET, m_buttonSet);
  58. DDX_Control(pDX, IDC_BUTTON_CLEAR, m_buttonClear);
  59. DDX_Control(pDX, IDC_COMBO1, m_comboBox);
  60. //}}AFX_DATA_MAP
  61. if (!pDX->m_bSaveAndValidate)
  62. UpdateListBox();
  63. }
  64. BEGIN_MESSAGE_MAP(CFormatTabDlg, CCSDialog)
  65. //{{AFX_MSG_MAP(CFormatTabDlg)
  66. ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnClickedClear)
  67. ON_BN_CLICKED(IDC_BUTTON_CLEARALL, OnClickedClearAll)
  68. ON_BN_CLICKED(IDC_BUTTON_SET, OnClickedSet)
  69. ON_CBN_EDITCHANGE(IDC_COMBO1, OnEditChange)
  70. ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchange)
  71. ON_MESSAGE(WM_HELP, OnHelp)
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CFormatTabDlg message handlers
  76. void CFormatTabDlg::OnClickedClear()
  77. {
  78. int nTab;
  79. int nSel = m_comboBox.GetCurSel();
  80. if (nSel == CB_ERR)
  81. {
  82. CDataExchange dx(this, TRUE);
  83. DDX_Twips(&dx, IDC_COMBO1, nTab);
  84. DDV_MinMaxTwips(&dx, nTab, 0, 31680);
  85. if (nTab != DDXM_BLANK)
  86. {
  87. if (RemoveTabFromArray(nTab))
  88. UpdateListBox();
  89. }
  90. }
  91. else
  92. {
  93. ASSERT(nSel < m_nCount);
  94. RemoveTabFromArrayByIndex(nSel);
  95. UpdateListBox();
  96. }
  97. UpdateButtons();
  98. SetEditFocus();
  99. }
  100. void CFormatTabDlg::OnClickedClearAll()
  101. {
  102. m_nCount = 0;
  103. m_comboBox.ResetContent();
  104. UpdateButtons();
  105. SetEditFocus();
  106. }
  107. void CFormatTabDlg::OnClickedSet()
  108. {
  109. Set();
  110. UpdateButtons();
  111. SetEditFocus();
  112. }
  113. BOOL CFormatTabDlg::Set()
  114. {
  115. int nTab = 0;
  116. CDataExchange dx(this, TRUE);
  117. DDX_Twips(&dx, IDC_COMBO1, nTab);
  118. DDV_MinMaxTwips(&dx, nTab, 0, 31680);
  119. if (nTab != DDXM_BLANK)
  120. {
  121. if (m_nCount == MAX_TAB_STOPS)
  122. {
  123. AfxMessageBox(IDS_NOMORETABS);
  124. m_comboBox.Clear();
  125. return FALSE;
  126. }
  127. if (AddTabToArray(nTab))
  128. UpdateListBox();
  129. return TRUE;
  130. }
  131. return FALSE;
  132. }
  133. void CFormatTabDlg::SetEditFocus()
  134. {
  135. m_comboBox.SetFocus();
  136. m_comboBox.SetEditSel(0,-1);
  137. }
  138. BOOL CFormatTabDlg::RemoveTabFromArray(LONG lTab)
  139. {
  140. int i;
  141. for (i=0;i<m_nCount;i++)
  142. {
  143. if (m_tabarray[i] == lTab)
  144. {
  145. RemoveTabFromArrayByIndex(i);
  146. return TRUE;
  147. }
  148. }
  149. return FALSE;
  150. }
  151. void CFormatTabDlg::RemoveTabFromArrayByIndex(int nIndex)
  152. {
  153. memmove(&m_tabarray[nIndex], &m_tabarray[nIndex+1],
  154. (m_nCount-nIndex-1)*sizeof(LONG));
  155. m_nCount--;
  156. }
  157. BOOL CFormatTabDlg::AddTabToArray(LONG lTab)
  158. {
  159. int i;
  160. BOOL bInsert = FALSE;
  161. LONG lTemp;
  162. for (i=0;i<m_nCount;i++)
  163. {
  164. if (!bInsert && lTab < m_tabarray[i])
  165. bInsert = TRUE;
  166. else if (lTab == m_tabarray[i]) // we don't want repeats
  167. return FALSE;
  168. if (bInsert)
  169. {
  170. lTemp = m_tabarray[i];
  171. m_tabarray[i] = lTab;
  172. lTab = lTemp;
  173. }
  174. }
  175. m_tabarray[m_nCount++] = lTab;
  176. return TRUE;
  177. }
  178. void CFormatTabDlg::UpdateListBox()
  179. {
  180. int i;
  181. TCHAR szT[64];
  182. ASSERT(m_nCount >= 0);
  183. m_comboBox.ResetContent();
  184. for (i=0;i<m_nCount;i++)
  185. {
  186. theApp.PrintTwips(szT, ARRAYSIZE(szT), m_tabarray[i], 2);
  187. m_comboBox.AddString(szT);
  188. }
  189. }
  190. void CFormatTabDlg::OnOK()
  191. {
  192. if (m_buttonSet.IsWindowEnabled())
  193. {
  194. if (!Set())
  195. return;
  196. }
  197. CCSDialog::OnOK();
  198. m_pf.cTabCount = (SHORT) m_nCount;
  199. for (int i=0;i<m_nCount;i++)
  200. m_pf.rgxTabs[i] = m_tabarray[i];
  201. m_pf.dwMask = PFM_TABSTOPS;
  202. }
  203. void CFormatTabDlg::OnEditChange()
  204. {
  205. UpdateButtons();
  206. }
  207. void CFormatTabDlg::UpdateButton(CButton& button, BOOL b)
  208. {
  209. if (b != button.IsWindowEnabled())
  210. button.EnableWindow(b);
  211. }
  212. void CFormatTabDlg::UpdateButtons()
  213. {
  214. UpdateButton(m_buttonClearAll, m_nCount > 0);
  215. BOOL bHasText = (m_comboBox.GetWindowTextLength() > 0);
  216. UpdateButton(m_buttonSet, bHasText);
  217. UpdateButton(m_buttonClear, bHasText);
  218. WORD wID = LOWORD(GetDefID());
  219. if (bHasText && wID != IDC_BUTTON_SET)
  220. SetDefID(IDC_BUTTON_SET);
  221. else if (!bHasText && wID != IDOK)
  222. SetDefID(IDOK);
  223. }
  224. BOOL CFormatTabDlg::OnInitDialog()
  225. {
  226. CCSDialog::OnInitDialog();
  227. UpdateButtons();
  228. return TRUE; // return TRUE unless you set the focus to a control
  229. // EXCEPTION: OCX Property Pages should return FALSE
  230. }
  231. void CFormatTabDlg::OnSelchange()
  232. {
  233. UpdateButton(m_buttonClearAll, m_nCount > 0);
  234. // force these since if the edit control is empty and
  235. // an item in the box is clicked on, the edit control will
  236. // not be filled in first
  237. UpdateButton(m_buttonSet, TRUE);
  238. UpdateButton(m_buttonClear, TRUE);
  239. WORD wID = LOWORD(GetDefID());
  240. if (wID != IDC_BUTTON_SET)
  241. SetDefID(IDC_BUTTON_SET);
  242. }
  243. LONG CFormatTabDlg::OnHelp(WPARAM, LPARAM lParam)
  244. {
  245. LPHELPINFO phi = (LPHELPINFO) lParam ;
  246. HWND hWndCombo = ::GetDlgItem(m_hWnd, IDC_COMBO1) ;
  247. HWND hWndItem = (HWND) phi->hItemHandle ;
  248. if (::GetParent(hWndItem) == hWndCombo)
  249. {
  250. hWndItem = hWndCombo ;
  251. }
  252. ::WinHelp(hWndItem, AfxGetApp()->m_pszHelpFilePath,
  253. HELP_WM_HELP, (DWORD_PTR)GetHelpIDs());
  254. return 0;
  255. }