home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / internet / mfcie / mainfrm.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  12.6 KB  |  419 lines

  1. // This is 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. //
  11. // MainFrm.cpp : implementation of the CMainFrame class
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "mfcie.h"
  16. #include <wininet.h>
  17.  
  18. #include "MainFrm.h"
  19. #include "mfcieDoc.h"
  20. #include "mfcieVw.h"
  21.  
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMainFrame
  30.  
  31. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  32.  
  33. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  34.     //{{AFX_MSG_MAP(CMainFrame)
  35.     ON_WM_CREATE()
  36.     //}}AFX_MSG_MAP
  37.     ON_CBN_SELENDOK(AFX_IDW_TOOLBAR + 1,OnNewAddress)
  38.     ON_COMMAND_RANGE(0xe00, 0xfff, OnFavorite)
  39.     ON_COMMAND(IDOK, OnNewAddressEnter)
  40.     ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnDropDown)
  41.     ON_COMMAND(ID_FAVORITES_DROPDOWN, DoNothing)
  42.     ON_COMMAND(ID_FONT_DROPDOWN, DoNothing)
  43. END_MESSAGE_MAP()
  44.  
  45. static UINT indicators[] =
  46. {
  47.     ID_SEPARATOR,           // status line indicator
  48.     ID_INDICATOR_CAPS,
  49.     ID_INDICATOR_NUM,
  50.     ID_INDICATOR_SCRL,
  51. };
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CMainFrame construction/destruction
  55.  
  56. CMainFrame::CMainFrame()
  57. {
  58. }
  59.  
  60. CMainFrame::~CMainFrame()
  61. {
  62. }
  63.  
  64. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  65. {
  66.     CImageList img;
  67.     CString str;
  68.  
  69.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  70.         return -1;
  71.  
  72.     if (!m_wndReBar.Create(this))
  73.     {
  74.         TRACE0("Failed to create rebar\n");
  75.         return -1;      // fail to create
  76.     }
  77.  
  78.     if (!m_wndToolBar.CreateEx(this))
  79.     {
  80.         TRACE0("Failed to create toolbar\n");
  81.         return -1;      // fail to create
  82.     }
  83.     // set up toolbar properties
  84.     m_wndToolBar.GetToolBarCtrl().SetButtonWidth(50, 150);
  85.     m_wndToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
  86.  
  87.     img.Create(IDB_HOTTOOLBAR, 22, 0, RGB(255, 0, 255));
  88.     m_wndToolBar.GetToolBarCtrl().SetHotImageList(&img);
  89.     img.Detach();
  90.     img.Create(IDB_COLDTOOLBAR, 22, 0, RGB(255, 0, 255));
  91.     m_wndToolBar.GetToolBarCtrl().SetImageList(&img);
  92.     img.Detach();
  93.     m_wndToolBar.ModifyStyle(0, TBSTYLE_FLAT | TBSTYLE_TRANSPARENT);
  94.     m_wndToolBar.SetButtons(NULL, 9);
  95.  
  96.     // set up each toolbar button
  97.     m_wndToolBar.SetButtonInfo(0, ID_GO_BACK, TBSTYLE_BUTTON, 0);
  98.     str.LoadString(IDS_BACK);
  99.     m_wndToolBar.SetButtonText(0, str);
  100.     m_wndToolBar.SetButtonInfo(1, ID_GO_FORWARD, TBSTYLE_BUTTON, 1);
  101.     str.LoadString(IDS_FORWARD);
  102.     m_wndToolBar.SetButtonText(1, str);
  103.     m_wndToolBar.SetButtonInfo(2, ID_VIEW_STOP, TBSTYLE_BUTTON, 2);
  104.     str.LoadString(IDS_STOP);
  105.     m_wndToolBar.SetButtonText(2, str);
  106.     m_wndToolBar.SetButtonInfo(3, ID_VIEW_REFRESH, TBSTYLE_BUTTON, 3);
  107.     str.LoadString(IDS_REFRESH);
  108.     m_wndToolBar.SetButtonText(3, str);
  109.     m_wndToolBar.SetButtonInfo(4, ID_GO_START_PAGE, TBSTYLE_BUTTON, 4);
  110.     str.LoadString(IDS_HOME);
  111.     m_wndToolBar.SetButtonText(4, str);
  112.     m_wndToolBar.SetButtonInfo(5, ID_GO_SEARCH_THE_WEB, TBSTYLE_BUTTON, 5);
  113.     str.LoadString(IDS_SEARCH);
  114.     m_wndToolBar.SetButtonText(5, str);
  115.     m_wndToolBar.SetButtonInfo(6, ID_FAVORITES_DROPDOWN, TBSTYLE_BUTTON | TBSTYLE_DROPDOWN, 6);
  116.     str.LoadString(IDS_FAVORITES);
  117.     m_wndToolBar.SetButtonText(6, str);
  118.     m_wndToolBar.SetButtonInfo(7, ID_FILE_PRINT, TBSTYLE_BUTTON, 7);
  119.     str.LoadString(IDS_PRINT);
  120.     m_wndToolBar.SetButtonText(7, str);
  121.     m_wndToolBar.SetButtonInfo(8, ID_FONT_DROPDOWN, TBSTYLE_BUTTON | TBSTYLE_DROPDOWN, 8);
  122.     str.LoadString(IDS_FONT);
  123.     m_wndToolBar.SetButtonText(8, str);
  124.  
  125.     CRect rectToolBar;
  126.  
  127.     // set up toolbar button sizes
  128.     m_wndToolBar.GetItemRect(0, &rectToolBar);
  129.     m_wndToolBar.SetSizes(rectToolBar.Size(), CSize(30,20));
  130.  
  131.     // create a combo box for the address bar
  132.     if (!m_wndAddress.Create(CBS_DROPDOWN | WS_CHILD, CRect(0, 0, 200, 120), this, AFX_IDW_TOOLBAR + 1))
  133.     {
  134.         TRACE0("Failed to create combobox\n");
  135.         return -1;      // fail to create
  136.     }
  137.  
  138.     // create the animation control
  139.     m_wndAnimate.Create(WS_CHILD | WS_VISIBLE, CRect(0, 0, 10, 10), this, AFX_IDW_TOOLBAR + 2);
  140.     m_wndAnimate.Open(IDR_MFCAVI);
  141.  
  142.     // add the toolbar, animation, and address bar to the rebar
  143.     m_wndReBar.AddBar(&m_wndToolBar);
  144.     m_wndReBar.AddBar(&m_wndAnimate, NULL, NULL, RBBS_FIXEDSIZE | RBBS_FIXEDBMP);
  145.     str.LoadString(IDS_ADDRESS);
  146.     m_wndReBar.AddBar(&m_wndAddress, str, NULL, RBBS_FIXEDBMP | RBBS_BREAK);
  147.  
  148.     // set up min/max sizes and ideal sizes for pieces of the rebar
  149.     REBARBANDINFO rbbi;
  150.  
  151.     rbbi.cbSize = sizeof(rbbi);
  152.     rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE;
  153.     rbbi.cxMinChild = rectToolBar.Width();
  154.     rbbi.cyMinChild = rectToolBar.Height();
  155.     rbbi.cx = rbbi.cxIdeal = rectToolBar.Width() * 9;
  156.     m_wndReBar.GetReBarCtrl().SetBandInfo(0, &rbbi);
  157.     rbbi.cxMinChild = 0;
  158.  
  159.     CRect rectAddress;
  160.  
  161.     rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE;
  162.     m_wndAddress.GetEditCtrl()->GetWindowRect(&rectAddress);
  163.     rbbi.cyMinChild = rectAddress.Height() + 10;
  164.     rbbi.cxIdeal = 200;
  165.     m_wndReBar.GetReBarCtrl().SetBandInfo(2, &rbbi);
  166.  
  167.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  168.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED);
  169.  
  170.     if (!m_wndStatusBar.Create(this) ||
  171.         !m_wndStatusBar.SetIndicators(indicators,
  172.           sizeof(indicators)/sizeof(UINT)))
  173.     {
  174.         TRACE0("Failed to create status bar\n");
  175.         return -1;      // fail to create
  176.     }
  177.  
  178. // set up Favorites menu
  179.     TCHAR           sz[MAX_PATH];
  180.     TCHAR           szPath[MAX_PATH];
  181.     HKEY            hKey;
  182.     DWORD           dwSize;
  183.     CMenu*          pMenu;
  184.  
  185.     // first get rid of bogus submenu items.
  186.     pMenu = GetMenu()->GetSubMenu(3);
  187.     while(pMenu->DeleteMenu(0, MF_BYPOSITION));
  188.  
  189.     // find out from the registry where the favorites are located.
  190.     if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS)
  191.     {
  192.         TRACE0("Favorites folder not found\n");
  193.         return 0;
  194.     }
  195.     dwSize = sizeof(sz);
  196.     RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
  197.     ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
  198.     RegCloseKey(hKey);
  199.  
  200.     BuildFavoritesMenu(szPath, 0, pMenu);
  201.     return 0;
  202. }
  203.  
  204. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  205. {
  206.     cs.lpszClass = AfxRegisterWndClass(0);
  207.     return CFrameWnd::PreCreateWindow(cs);
  208. }
  209.  
  210. /////////////////////////////////////////////////////////////////////////////
  211. // CMainFrame diagnostics
  212.  
  213. #ifdef _DEBUG
  214. void CMainFrame::AssertValid() const
  215. {
  216.     CFrameWnd::AssertValid();
  217. }
  218.  
  219. void CMainFrame::Dump(CDumpContext& dc) const
  220. {
  221.     CFrameWnd::Dump(dc);
  222. }
  223.  
  224. #endif //_DEBUG
  225.  
  226. /////////////////////////////////////////////////////////////////////////////
  227. // CMainFrame message handlers
  228.  
  229. int CMainFrame::BuildFavoritesMenu(LPCTSTR pszPath, int nStartPos, CMenu* pMenu)
  230. {
  231.     CString         strPath(pszPath);
  232.     CString         strPath2;
  233.     CString         str;
  234.     WIN32_FIND_DATA wfd;
  235.     HANDLE          h;
  236.     int             nPos;
  237.     int             nEndPos;
  238.     int             nNewEndPos;
  239.     int             nLastDir;
  240.     TCHAR           buf[INTERNET_MAX_PATH_LENGTH];
  241.     CStringArray    astrFavorites;
  242.     CStringArray    astrDirs;
  243.     CMenu*          pSubMenu;
  244.  
  245.     // make sure there's a trailing backslash
  246.     if(strPath[strPath.GetLength() - 1] != _T('\\'))
  247.         strPath += _T('\\');
  248.     strPath2 = strPath;
  249.     strPath += "*.*";
  250.  
  251.     // now scan the directory, first for .URL files and then for subdirectories
  252.     // that may also contain .URL files
  253.     h = FindFirstFile(strPath, &wfd);
  254.     if(h != INVALID_HANDLE_VALUE)
  255.     {
  256.         nEndPos = nStartPos;
  257.         do
  258.         {
  259.             if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
  260.             {
  261.                 str = wfd.cFileName;
  262.                 if(str.Right(4) == _T(".url"))
  263.                 {
  264.                     // an .URL file is formatted just like an .INI file, so we can
  265.                     // use GetPrivateProfileString() to get the information we want
  266.                     ::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
  267.                                               _T(""), buf, INTERNET_MAX_PATH_LENGTH,
  268.                                               strPath2 + str);
  269.                     str = str.Left(str.GetLength() - 4);
  270.  
  271.                     // scan through the array and perform an insertion sort
  272.                     // to make sure the menu ends up in alphabetic order
  273.                     for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
  274.                     {
  275.                         if(str.CompareNoCase(astrFavorites[nPos]) < 0)
  276.                             break;
  277.                     }
  278.                     astrFavorites.InsertAt(nPos, str);
  279.                     m_astrFavoriteURLs.InsertAt(nPos, buf);
  280.                     ++nEndPos;
  281.                 }
  282.             }
  283.         } while(FindNextFile(h, &wfd));
  284.         FindClose(h);
  285.         // Now add these items to the menu
  286.         for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
  287.         {
  288.             pMenu->AppendMenu(MF_STRING | MF_ENABLED, 0xe00 + nPos, astrFavorites[nPos]);
  289.         }
  290.  
  291.  
  292.         // now that we've got all the .URL files, check the subdirectories for more
  293.         nLastDir = 0;
  294.         h = FindFirstFile(strPath, &wfd);
  295.         ASSERT(h != INVALID_HANDLE_VALUE);
  296.         do
  297.         {
  298.             if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  299.             {
  300.                 // ignore the current and parent directory entries
  301.                 if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName, _T("..")) == 0)
  302.                     continue;
  303.  
  304.                 for(nPos = 0 ; nPos < nLastDir ; ++nPos)
  305.                 {
  306.                     if(astrDirs[nPos].CompareNoCase(wfd.cFileName) > 0)
  307.                         break;
  308.                 }
  309.                 pSubMenu = new CMenu;
  310.                 pSubMenu->CreatePopupMenu();
  311.  
  312.                 // call this function recursively.
  313.                 nNewEndPos = BuildFavoritesMenu(strPath2 + wfd.cFileName, nEndPos, pSubMenu);
  314.                 if(nNewEndPos != nEndPos)
  315.                 {
  316.                     // only intert a submenu if there are in fact .URL files in the subdirectory
  317.                     nEndPos = nNewEndPos;
  318.                     pMenu->InsertMenu(nPos, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)pSubMenu->m_hMenu, wfd.cFileName);
  319.                     pSubMenu->Detach();
  320.                     astrDirs.InsertAt(nPos, wfd.cFileName);
  321.                     ++nLastDir;
  322.                 }
  323.                 delete pSubMenu;
  324.             }
  325.         } while(FindNextFile(h, &wfd));
  326.         FindClose(h);
  327.     }
  328.     return nEndPos;
  329. }
  330.  
  331. void CMainFrame::OnFavorite(UINT nID)
  332. {
  333.     ((CMfcieView*)GetActiveView())->Navigate2(m_astrFavoriteURLs[nID-0xe00], 0, NULL);
  334. }
  335.  
  336. void CMainFrame::SetAddress(LPCTSTR lpszUrl)
  337. {
  338.     // This is called when the browser has completely loaded the new location,
  339.     // so make sure the text in the address bar is up to date and stop the
  340.     // animation.
  341.     m_wndAddress.SetWindowText(lpszUrl);
  342.     m_wndAnimate.Stop();
  343.     m_wndAnimate.Seek(0);
  344. }
  345.  
  346. void CMainFrame::StartAnimation()
  347. {
  348.     // Start the animation.  This is called when the browser begins to
  349.     // navigate to a new location
  350.     m_wndAnimate.Play(0, -1, -1);
  351. }
  352.  
  353. void CMainFrame::OnNewAddress()
  354. {
  355.     // gets called when an item in the Address combo box is selected
  356.     // just navigate to the newly selected location.
  357.     CString str;
  358.  
  359.     m_wndAddress.GetLBText(m_wndAddress.GetCurSel(), str);
  360.     ((CMfcieView*)GetActiveView())->Navigate2(str, 0, NULL);
  361. }
  362.  
  363. void CMainFrame::OnNewAddressEnter()
  364. {
  365.     // gets called when an item is entered manually into the edit box portion
  366.     // of the Address combo box.
  367.     // navigate to the newly selected location and also add this address to the
  368.     // list of addresses in the combo box.
  369.     CString str;
  370.  
  371.     m_wndAddress.GetEditCtrl()->GetWindowText(str);
  372.     ((CMfcieView*)GetActiveView())->Navigate2(str, 0, NULL);
  373.  
  374.     COMBOBOXEXITEM item;
  375.  
  376.     item.mask = CBEIF_TEXT;
  377.     item.iItem = -1;
  378.     item.pszText = (LPTSTR)(LPCTSTR)str;
  379.     m_wndAddress.InsertItem(&item);
  380. }
  381.  
  382. void CMainFrame::DoNothing()
  383. {
  384.     // this is here only so that the toolbar buttons for the dropdown menus
  385.     // will have a callback, and thus will not be disabled.
  386. }
  387.  
  388. void CMainFrame::OnDropDown(NMHDR* pNotifyStruct, LRESULT* pResult)
  389. {
  390.     // this function handles the dropdown menus from the toolbar
  391.     NMTOOLBAR* pNMToolBar = (NMTOOLBAR*)pNotifyStruct;
  392.     CRect rect;
  393.  
  394.     // translate the current toolbar item rectangle into screen coordinates
  395.     // so that we'll know where to pop up the menu
  396.     m_wndToolBar.GetToolBarCtrl().GetRect(pNMToolBar->iItem, &rect);
  397.     rect.top = rect.bottom;
  398.     ::ClientToScreen(pNMToolBar->hdr.hwndFrom, &rect.TopLeft());
  399.     if(pNMToolBar->iItem == ID_FONT_DROPDOWN)
  400.     {
  401.         CMenu menu;
  402.         CMenu* pPopup;
  403.  
  404.         // the font popup is stored in a resource
  405.         menu.LoadMenu(IDR_FONT_POPUP);
  406.         pPopup = menu.GetSubMenu(0);
  407.         pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top + 1, AfxGetMainWnd());
  408.     }
  409.     else if(pNMToolBar->iItem == ID_FAVORITES_DROPDOWN)
  410.     {
  411.         CMenu* pPopup;
  412.  
  413.         // for the favorties popup, just steal the menu from the main window
  414.         pPopup = GetMenu()->GetSubMenu(3);
  415.         pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top + 1, AfxGetMainWnd());
  416.     }
  417.     *pResult = TBDDRET_DEFAULT;
  418. }
  419.