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.

550 lines
15 KiB

  1. // mainfrm.cpp : implementation of the CMainFrame class
  2. //
  3. // Copyright (C) 1992-1999 Microsoft Corporation
  4. // All rights reserved.
  5. #include "stdafx.h"
  6. #include "wordpad.h"
  7. #include "mainfrm.h"
  8. #include "wordpdoc.h"
  9. #include "wordpvw.h"
  10. #include "strings.h"
  11. #include "colorlis.h"
  12. #include "filedlg.h"
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMainFrame
  19. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  20. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  21. //{{AFX_MSG_MAP(CMainFrame)
  22. ON_WM_CREATE()
  23. ON_WM_SYSCOLORCHANGE()
  24. ON_WM_SIZE()
  25. ON_WM_MOVE()
  26. ON_COMMAND(ID_HELP, OnHelpFinder)
  27. ON_WM_DROPFILES()
  28. ON_COMMAND(ID_CHAR_COLOR, OnCharColor)
  29. ON_COMMAND(ID_PEN_TOGGLE, OnPenToggle)
  30. ON_WM_FONTCHANGE()
  31. ON_WM_QUERYNEWPALETTE()
  32. ON_WM_PALETTECHANGED()
  33. ON_WM_DEVMODECHANGE()
  34. ON_COMMAND(ID_HELP_INDEX, OnHelpFinder)
  35. //}}AFX_MSG_MAP
  36. // Global help commands
  37. // ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
  38. ON_COMMAND(ID_DEFAULT_HELP, OnHelpFinder)
  39. ON_UPDATE_COMMAND_UI(ID_VIEW_FORMATBAR, OnUpdateControlBarMenu)
  40. ON_UPDATE_COMMAND_UI(ID_VIEW_RULER, OnUpdateControlBarMenu)
  41. ON_MESSAGE(WPM_BARSTATE, OnBarState)
  42. ON_REGISTERED_MESSAGE(CWordPadApp::m_nOpenMsg, OnOpenMsg)
  43. ON_COMMAND_EX(ID_VIEW_STATUS_BAR, OnBarCheck)
  44. ON_COMMAND_EX(ID_VIEW_TOOLBAR, OnBarCheck)
  45. ON_COMMAND_EX(ID_VIEW_FORMATBAR, OnBarCheck)
  46. ON_COMMAND_EX(ID_VIEW_RULER, OnBarCheck)
  47. ON_REGISTERED_MESSAGE(CWordPadApp::m_nOLEHelpMsg, OnOLEHelpMsg)
  48. ON_MESSAGE_VOID(WM_IDLEUPDATECMDUI, OnIdleUpdateCmdUI)
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // arrays of IDs used to initialize control bars
  52. // toolbar buttons - IDs are command buttons
  53. static UINT BASED_CODE toolbar[] =
  54. {
  55. // same order as in the bitmap 'toolbar.bmp'
  56. // (int nBitmap, int nCommand, BYTE byteState, BYTE byteStyle, DWORD dw, int nString)
  57. ID_FILE_NEW,
  58. ID_FILE_OPEN,
  59. ID_FILE_SAVE,
  60. ID_SEPARATOR,
  61. ID_FILE_PRINT_DIRECT,
  62. ID_FILE_PRINT_PREVIEW,
  63. ID_SEPARATOR,
  64. ID_EDIT_FIND,
  65. ID_SEPARATOR,
  66. ID_EDIT_CUT,
  67. ID_EDIT_COPY,
  68. ID_EDIT_PASTE,
  69. ID_EDIT_UNDO,
  70. ID_SEPARATOR,
  71. ID_INSERT_DATE_TIME,
  72. ID_SEPARATOR,
  73. ID_PEN_TOGGLE,
  74. ID_PEN_PERIOD,
  75. ID_PEN_SPACE,
  76. ID_PEN_BACKSPACE,
  77. ID_PEN_NEWLINE,
  78. ID_PEN_LENS
  79. };
  80. #define NUM_PEN_ITEMS 7
  81. #define NUM_PEN_TOGGLE 5
  82. static UINT BASED_CODE format[] =
  83. {
  84. // same order as in the bitmap 'format.bmp'
  85. ID_SEPARATOR, // font name combo box
  86. ID_SEPARATOR,
  87. ID_SEPARATOR, // font size combo box
  88. ID_SEPARATOR,
  89. ID_SEPARATOR, // font script combo box
  90. ID_SEPARATOR,
  91. ID_CHAR_BOLD,
  92. ID_CHAR_ITALIC,
  93. ID_CHAR_UNDERLINE,
  94. ID_CHAR_COLOR,
  95. ID_SEPARATOR,
  96. ID_PARA_LEFT,
  97. ID_PARA_CENTER,
  98. ID_PARA_RIGHT,
  99. ID_SEPARATOR,
  100. ID_INSERT_BULLET,
  101. };
  102. static UINT BASED_CODE indicators[] =
  103. {
  104. ID_SEPARATOR, // status line indicator
  105. ID_INDICATOR_CAPS,
  106. ID_INDICATOR_NUM,
  107. };
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CMainFrame construction/destruction
  110. CMainFrame::CMainFrame()
  111. {
  112. m_hIconDoc = theApp.LoadIcon(IDI_ICON_DOC);
  113. m_hIconText = theApp.LoadIcon(IDI_ICON_TEXT);
  114. m_hIconWrite = theApp.LoadIcon(IDI_ICON_WRITE);
  115. m_inupdate = false;
  116. m_reset = false;
  117. }
  118. CMainFrame::~CMainFrame()
  119. {
  120. }
  121. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  122. {
  123. WNDCLASS wndcls;
  124. BOOL bRes = CFrameWnd::PreCreateWindow(cs);
  125. HINSTANCE hInst = AfxGetInstanceHandle();
  126. // see if the class already exists
  127. if (!::GetClassInfo(hInst, szWordPadClass, &wndcls))
  128. {
  129. // get default stuff
  130. ::GetClassInfo(hInst, cs.lpszClass, &wndcls);
  131. wndcls.style &= ~(CS_HREDRAW|CS_VREDRAW);
  132. // register a new class
  133. wndcls.lpszClassName = szWordPadClass;
  134. wndcls.hIcon = ::LoadIcon(hInst, MAKEINTRESOURCE(IDR_MAINFRAME));
  135. ASSERT(wndcls.hIcon != NULL);
  136. if (!AfxRegisterClass(&wndcls))
  137. AfxThrowResourceException();
  138. }
  139. cs.lpszClass = szWordPadClass;
  140. CRect rect = theApp.m_rectInitialFrame;
  141. if (rect.Width() > 0 && rect.Height() > 0)
  142. {
  143. // make sure window will be visible
  144. CDisplayIC dc;
  145. CRect rectDisplay(0, 0, dc.GetDeviceCaps(HORZRES),
  146. dc.GetDeviceCaps(VERTRES));
  147. if (rectDisplay.PtInRect(rect.TopLeft()) &&
  148. rectDisplay.PtInRect(rect.BottomRight()))
  149. {
  150. cs.x = rect.left;
  151. cs.y = rect.top;
  152. cs.cx = rect.Width();
  153. cs.cy = rect.Height();
  154. }
  155. }
  156. return bRes;
  157. }
  158. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  159. {
  160. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  161. return -1;
  162. if (!CreateToolBar())
  163. return -1;
  164. if (!CreateFormatBar())
  165. return -1;
  166. if (!CreateStatusBar())
  167. return -1;
  168. EnableDocking(CBRS_ALIGN_ANY);
  169. if (!CreateRulerBar())
  170. return -1;
  171. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  172. m_wndFormatBar.EnableDocking(CBRS_ALIGN_TOP|CBRS_ALIGN_BOTTOM);
  173. DockControlBar(&m_wndToolBar);
  174. DockControlBar(&m_wndFormatBar);
  175. CWnd* pView = GetDlgItem(AFX_IDW_PANE_FIRST);
  176. if (pView != NULL)
  177. {
  178. pView->SetWindowPos(&wndBottom, 0, 0, 0, 0,
  179. SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
  180. }
  181. return 0;
  182. }
  183. BOOL CMainFrame::CreateToolBar()
  184. {
  185. int nPen = GetSystemMetrics(SM_PENWINDOWS) ? NUM_PEN_TOGGLE :
  186. NUM_PEN_ITEMS;
  187. UINT nID = theApp.m_bLargeIcons ? IDR_MAINFRAME1_BIG :
  188. IDR_MAINFRAME1;
  189. // If we have Large Icons then we should specify the correct Image size for
  190. // the toolbar before calling LoadBitmap because the number of buttons in the
  191. // toolbar calculated inside MFC (AddReplaceBitmap in BarTool.cpp) as Bitmap width
  192. // divided by ImageSize.cx. and this conflict with mirroring support if you have
  193. // incorrect number of buttons .
  194. if (theApp.m_bLargeIcons)
  195. m_wndToolBar.SetSizes(CSize(31,30), CSize(24,24));
  196. if (!m_wndToolBar.Create(this,
  197. WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||
  198. !m_wndToolBar.LoadBitmap(nID) ||
  199. !m_wndToolBar.SetButtons(toolbar, sizeof(toolbar)/sizeof(UINT) - nPen))
  200. {
  201. TRACE0("Failed to create toolbar\n");
  202. return FALSE; // fail to create
  203. }
  204. if (theApp.m_bLargeIcons)
  205. m_wndToolBar.SetSizes(CSize(31,30), CSize(24,24));
  206. else
  207. m_wndToolBar.SetSizes(CSize(23,22), CSize(16,16));
  208. CString str;
  209. str.LoadString(IDS_TITLE_TOOLBAR);
  210. m_wndToolBar.SetWindowText(str);
  211. return TRUE;
  212. }
  213. BOOL CMainFrame::CreateFormatBar()
  214. {
  215. UINT nID = theApp.m_bLargeIcons ? IDB_FORMATBAR_BIG : IDB_FORMATBAR;
  216. // If we have Large Icons then we should specify the correct Image size for
  217. // the toolbar before calling LoadBitmap because the number of buttons in the
  218. // toolbar calculated inside MFC (AddReplaceBitmap in BarTool.cpp) as Bitmap width
  219. // divided by ImageSize.cx. and this conflict with mirroring support if you have
  220. // incorrect number of buttons .
  221. if (theApp.m_bLargeIcons)
  222. m_wndFormatBar.SetSizes(CSize(31,30), CSize(24,24));
  223. if (!m_wndFormatBar.Create(this,
  224. WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_HIDE_INPLACE|CBRS_SIZE_DYNAMIC,
  225. ID_VIEW_FORMATBAR) ||
  226. !m_wndFormatBar.LoadBitmap(nID) ||
  227. !m_wndFormatBar.SetButtons(format, sizeof(format)/sizeof(UINT)))
  228. {
  229. TRACE0("Failed to create FormatBar\n");
  230. return FALSE; // fail to create
  231. }
  232. if (theApp.m_bLargeIcons)
  233. m_wndFormatBar.SetSizes(CSize(31,30), CSize(24,24));
  234. else
  235. m_wndFormatBar.SetSizes(CSize(23,22), CSize(16,16));
  236. CString str;
  237. str.LoadString(IDS_TITLE_FORMATBAR);
  238. m_wndFormatBar.SetWindowText(str);
  239. m_wndFormatBar.PositionCombos();
  240. return TRUE;
  241. }
  242. BOOL CMainFrame::CreateRulerBar()
  243. {
  244. if (!m_wndRulerBar.Create(this,
  245. WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_HIDE_INPLACE, ID_VIEW_RULER))
  246. {
  247. TRACE0("Failed to create ruler\n");
  248. return FALSE; // fail to create
  249. }
  250. return TRUE;
  251. }
  252. BOOL CMainFrame::CreateStatusBar()
  253. {
  254. if (!m_wndStatusBar.Create(this) ||
  255. !m_wndStatusBar.SetIndicators(indicators,
  256. sizeof(indicators)/sizeof(UINT)))
  257. {
  258. TRACE0("Failed to create status bar\n");
  259. return FALSE; // fail to create
  260. }
  261. return TRUE;
  262. }
  263. /////////////////////////////////////////////////////////////////////////////
  264. // CMainFrame Operations
  265. HICON CMainFrame::GetIcon(int nDocType)
  266. {
  267. switch (nDocType)
  268. {
  269. case RD_WINWORD6:
  270. case RD_WORDPAD:
  271. case RD_EMBEDDED:
  272. case RD_RICHTEXT:
  273. return m_hIconDoc;
  274. case RD_TEXT:
  275. case RD_OEMTEXT:
  276. return m_hIconText;
  277. case RD_WRITE:
  278. return m_hIconWrite;
  279. }
  280. return m_hIconDoc;
  281. }
  282. /////////////////////////////////////////////////////////////////////////////
  283. // CMainFrame diagnostics
  284. #ifdef _DEBUG
  285. void CMainFrame::AssertValid() const
  286. {
  287. CFrameWnd::AssertValid();
  288. }
  289. void CMainFrame::Dump(CDumpContext& dc) const
  290. {
  291. CFrameWnd::Dump(dc);
  292. }
  293. #endif //_DEBUG
  294. /////////////////////////////////////////////////////////////////////////////
  295. // CMainFrame message handlers
  296. void CMainFrame::OnFontChange()
  297. {
  298. m_wndFormatBar.SendMessage(CWordPadApp::m_nPrinterChangedMsg);
  299. }
  300. void CMainFrame::OnDevModeChange(LPTSTR lpDeviceName)
  301. {
  302. theApp.NotifyPrinterChanged();
  303. CFrameWnd::OnDevModeChange(lpDeviceName); //sends message to descendants
  304. }
  305. void CMainFrame::OnSysColorChange()
  306. {
  307. CFrameWnd::OnSysColorChange();
  308. m_wndRulerBar.SendMessage(WM_SYSCOLORCHANGE);
  309. }
  310. void CMainFrame::ActivateFrame(int nCmdShow)
  311. {
  312. WINDOWPLACEMENT wp ;
  313. wp.length = sizeof(WINDOWPLACEMENT) ;
  314. if (GetWindowPlacement(&wp))
  315. {
  316. memcpy(&wp.rcNormalPosition, &theApp.m_rectInitialFrame, sizeof(RECT)) ;
  317. if (-1 != nCmdShow)
  318. wp.showCmd = nCmdShow;
  319. SetWindowPlacement(&wp) ;
  320. }
  321. CFrameWnd::ActivateFrame(nCmdShow);
  322. // make sure and display the toolbar, ruler, etc while loading a document.
  323. OnIdleUpdateCmdUI();
  324. UpdateWindow();
  325. }
  326. void CMainFrame::OnSize(UINT nType, int cx, int cy)
  327. {
  328. CFrameWnd::OnSize(nType, cx, cy);
  329. theApp.m_bMaximized = (nType == SIZE_MAXIMIZED);
  330. if (nType == SIZE_RESTORED)
  331. GetWindowRect(theApp.m_rectInitialFrame);
  332. }
  333. BOOL CMainFrame::OnBarCheck(UINT barID)
  334. {
  335. CDocOptions::CBarState &state = theApp.GetDocOptions().GetBarState();
  336. CControlBar *bar = GetControlBar(barID);
  337. BOOL bVisible = !(bar->GetStyle() & WS_VISIBLE);
  338. switch (barID)
  339. {
  340. case ID_VIEW_STATUS_BAR: state.m_bStatusBar = bVisible; break;
  341. case ID_VIEW_FORMATBAR: state.m_bFormatBar = bVisible; break;
  342. case ID_VIEW_RULER: state.m_bRulerBar = bVisible; break;
  343. case ID_VIEW_TOOLBAR: state.m_bToolBar = bVisible; break;
  344. }
  345. return CFrameWnd::OnBarCheck(barID);
  346. }
  347. LONG CMainFrame::OnBarState(UINT wParam, LONG lParam)
  348. {
  349. if (lParam == -1)
  350. return 0L;
  351. ASSERT(lParam != RD_EMBEDDED);
  352. if (wParam == 0)
  353. {
  354. CDockState& ds = theApp.GetDockState(lParam);
  355. ds.Clear(); // empty out the dock state
  356. GetDockState(ds);
  357. }
  358. else
  359. {
  360. if (IsTextType(lParam))
  361. {
  362. // in text mode hide the ruler and format bar so that it is the default
  363. CControlBar* pBar = GetControlBar(ID_VIEW_FORMATBAR);
  364. if (pBar != NULL)
  365. pBar->ShowWindow(SW_HIDE);
  366. pBar = GetControlBar(ID_VIEW_RULER);
  367. if (pBar != NULL)
  368. pBar->ShowWindow(SW_HIDE);
  369. }
  370. HICON hIcon = GetIcon((int)lParam);
  371. SendMessage(WM_SETICON, TRUE, (LPARAM)hIcon);
  372. SetDockState(theApp.GetDockState(lParam));
  373. }
  374. return 0L;
  375. }
  376. void CMainFrame::OnMove(int x, int y)
  377. {
  378. CFrameWnd::OnMove(x, y);
  379. WINDOWPLACEMENT wp;
  380. wp.length = sizeof(wp);
  381. GetWindowPlacement(&wp);
  382. theApp.m_rectInitialFrame = wp.rcNormalPosition;
  383. CView* pView = GetActiveView();
  384. if (pView != NULL)
  385. pView->SendMessage(WM_MOVE);
  386. }
  387. LONG CMainFrame::OnOpenMsg(UINT, LONG lParam)
  388. {
  389. TCHAR szAtomName[256];
  390. szAtomName[0] = NULL;
  391. GlobalGetAtomName((ATOM)lParam, szAtomName, 256);
  392. CWordPadDoc* pDoc = (CWordPadDoc*)GetActiveDocument();
  393. if (szAtomName[0] != NULL && pDoc != NULL)
  394. {
  395. if (lstrcmpi(szAtomName, pDoc->GetPathName()) == 0)
  396. return TRUE;
  397. }
  398. return FALSE;
  399. }
  400. LONG CMainFrame::OnOLEHelpMsg(UINT, LONG)
  401. {
  402. CWnd* pMainWnd = AfxGetMainWnd();
  403. ASSERT_VALID(pMainWnd);
  404. // return global app help mode state to FALSE (backward compatibility)
  405. m_bHelpMode = FALSE;
  406. pMainWnd->PostMessage(WM_KICKIDLE); // trigger idle update
  407. ::HtmlHelpA( ::GetDesktopWindow(), "wordpad.chm", HH_DISPLAY_TOPIC, 0L );
  408. return TRUE ;
  409. }
  410. void CMainFrame::OnHelpFinder()
  411. {
  412. ::HtmlHelpA( ::GetDesktopWindow(), "wordpad.chm", HH_DISPLAY_TOPIC, 0L );
  413. }
  414. void CMainFrame::OnDropFiles(HDROP hDropInfo)
  415. {
  416. TCHAR szFileName[_MAX_PATH];
  417. ::DragQueryFile(hDropInfo, 0, szFileName, _MAX_PATH);
  418. ::DragFinish(hDropInfo);
  419. theApp.OpenDocumentFile(szFileName);
  420. }
  421. void CMainFrame::OnCharColor()
  422. {
  423. CColorMenu colorMenu;
  424. CRect rc;
  425. int index = m_wndFormatBar.CommandToIndex(ID_CHAR_COLOR);
  426. m_wndFormatBar.GetItemRect(index, &rc);
  427. m_wndFormatBar.ClientToScreen(rc);
  428. colorMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON,rc.left,rc.bottom, this);
  429. }
  430. void CMainFrame::OnPenToggle()
  431. {
  432. static int nPen = 0;
  433. m_wndToolBar.SetButtons(toolbar, sizeof(toolbar)/sizeof(UINT) - nPen);
  434. nPen = (nPen == 0) ? NUM_PEN_TOGGLE : 0;
  435. m_wndToolBar.Invalidate();
  436. m_wndToolBar.GetParentFrame()->RecalcLayout();
  437. }
  438. BOOL CMainFrame::OnQueryNewPalette()
  439. {
  440. CView* pView = GetActiveView();
  441. if (pView != NULL)
  442. return pView->SendMessage(WM_QUERYNEWPALETTE) != 0;
  443. return FALSE;
  444. }
  445. void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd)
  446. {
  447. CView* pView = GetActiveView();
  448. if (pView != NULL)
  449. pView->SendMessage(WM_PALETTECHANGED, (WPARAM)pFocusWnd->GetSafeHwnd());
  450. }
  451. //
  452. // HACKHACK: MFC has a bug where m_nIdleFlags gets clobbered if the flags are
  453. // updated during idle processing (MFC4.2). Workaround it by
  454. // forcing idleMenu and idleLayout after idle processing if
  455. // DelayUpdateFrameMenu gets called during idle processing.
  456. //
  457. void CMainFrame::OnIdleUpdateCmdUI()
  458. {
  459. m_inupdate = true;
  460. CFrameWnd::OnIdleUpdateCmdUI();
  461. if (m_reset)
  462. {
  463. m_nIdleFlags |= idleMenu | idleLayout;
  464. m_reset = false;
  465. }
  466. m_inupdate = false;
  467. }
  468. void CMainFrame::DelayUpdateFrameMenu(HMENU hMenuAlt)
  469. {
  470. if (m_inupdate)
  471. m_reset = true;
  472. CFrameWnd::DelayUpdateFrameMenu(hMenuAlt);
  473. }