home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / wordpad / mainfrm.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  12KB  |  456 lines

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