home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / DEMOT.ZIP / Src / demo_toolbar / mainfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-23  |  8.1 KB  |  315 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. // 1998 Kirk Stowell ( kstowel@sprynet.com )
  4. // www.geocities.com/SiliconValley/Haven/8230/index.html
  5. //
  6. // You are free to use, modify and distribute this source, as long as
  7. // there is no charge, and this HEADER stays intact. This source is
  8. // supplied "AS-IS", and without WARRANTY OF ANY KIND, and the user
  9. // holds the author blameless for any or all problems that may arise
  10. // from the use of this code.
  11. //
  12. //////////////////////////////////////////////////////////////////////
  13.  
  14. #include "stdafx.h"
  15. #include "demo_toolbar.h"
  16.  
  17. #include "MainFrm.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CMainFrame
  27.  
  28. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  29.  
  30. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  31.     //{{AFX_MSG_MAP(CMainFrame)
  32.     ON_WM_CREATE()
  33.     ON_COMMAND(ID_BACK, OnBack)
  34.     ON_COMMAND(ID_FORWARD, OnForward)
  35.     ON_COMMAND(ID_STOP, OnStop)
  36.     ON_COMMAND(ID_REFRESH, OnRefresh)
  37.     ON_COMMAND(ID_HOME, OnHome)
  38.     //}}AFX_MSG_MAP
  39.  
  40.     // will have to define some message handlers for our controls.
  41.     ON_CBN_SELENDOK( IDC_COMBO, OnSelEndOk )
  42.     ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnToolbarDropDown)
  43.  
  44. END_MESSAGE_MAP()
  45.  
  46. static UINT indicators[] =
  47. {
  48.     ID_SEPARATOR,           // status line indicator
  49.     ID_INDICATOR_CAPS,
  50.     ID_INDICATOR_NUM,
  51.     ID_INDICATOR_SCRL,
  52. };
  53.  
  54. static CString comboText[] =
  55. {
  56.     "Page Width",
  57.     " 50%",
  58.     "100%",
  59.     "200%",
  60.     "300%",
  61.     "400%",
  62. };
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CMainFrame construction/destruction
  66.  
  67. CMainFrame::CMainFrame()
  68. {
  69.     // TODO: add member initialization code here
  70.     
  71. }
  72.  
  73. CMainFrame::~CMainFrame()
  74. {
  75. }
  76.  
  77. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  78. {
  79.     if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  80.         return -1;
  81.  
  82.     if (!m_wndToolBar.Create(this) ||
  83.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  84.     {
  85.         TRACE0("Failed to create toolbar\n");
  86.         return -1;      // fail to create
  87.     }
  88.  
  89.     if (!m_winToolBar.Create(this) ||
  90.         !m_winToolBar.LoadToolBar(IDR_WINDOW))
  91.     {
  92.         TRACE0("Failed to create toolbar\n");
  93.         return -1;      // fail to create
  94.     }
  95.  
  96.     if (!m_hotToolBar.Create(this) ||
  97.         !m_hotToolBar.LoadToolBar(IDR_HOTBAR))
  98.     {
  99.         TRACE0("Failed to create toolbar\n");
  100.         return -1;      // fail to create
  101.     }
  102.  
  103.     if (!m_wndStatusBar.Create(this) ||
  104.         !m_wndStatusBar.SetIndicators(indicators,
  105.           sizeof(indicators)/sizeof(UINT)))
  106.     {
  107.         TRACE0("Failed to create status bar\n");
  108.         return -1;      // fail to create
  109.     }
  110.  
  111.     // TODO: Remove this if you don't want tool tips or a resizeable toolbar
  112.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  113.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  114.     m_winToolBar.SetBarStyle(m_winToolBar.GetBarStyle() |
  115.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  116.     m_hotToolBar.SetBarStyle(m_hotToolBar.GetBarStyle() |
  117.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  118.  
  119.     // TODO: Delete these three lines if you don't want the toolbar to
  120.     //  be dockable
  121.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  122.     m_winToolBar.EnableDocking(CBRS_ALIGN_ANY);
  123.     m_hotToolBar.EnableDocking(CBRS_ALIGN_ANY);
  124.     EnableDocking(CBRS_ALIGN_ANY);
  125.     DockControlBar(&m_wndToolBar);
  126.     DockControlBarLeftOf(&m_winToolBar,&m_wndToolBar);
  127.     DockControlBar(&m_hotToolBar);
  128.  
  129.     LPSTR lpText[] = {"Back","Forward","Stop","Refresh","Home"};
  130.  
  131.     // Create and initialize image list used for child toolbar
  132.     CBitmap bmp;
  133.     bmp.LoadBitmap( IDB_COLOR_IMAGELIST );
  134.     m_pImageColor.Create( 21,20,ILC_COLORDDB|ILC_MASK,5,1);
  135.     m_pImageColor.Add( &bmp, RGB(191,191,191) );
  136.  
  137.     // Create and initialize hot image list used for child toolbar
  138.     CBitmap hotBmp;
  139.     hotBmp.LoadBitmap( IDB_GRAY_IMAGELIST );
  140.     m_pImageNoColor.Create( 21,20,ILC_COLORDDB|ILC_MASK,5,1);
  141.     m_pImageNoColor.Add( &hotBmp, RGB(191,191,191) );
  142.  
  143.     // Set toolbar image lists
  144.     m_hotToolBar.SetImageList( &m_pImageNoColor, TB_SETIMAGELIST );
  145.     m_hotToolBar.SetImageList( &m_pImageColor,   TB_SETHOTIMAGELIST );
  146.  
  147.     // Add text to toolbar buttons.
  148.     for( int i = ID_BACK, x = 0; i <= ID_HOME; i++, x++ )
  149.     {
  150.         m_hotToolBar.SetButtonText(
  151.             m_hotToolBar.CommandToIndex( i ), lpText[x] );
  152.     }
  153.  
  154.     // set button to dropdown list
  155.     m_wndToolBar.SetButtonDropDown( ID_FILE_OPEN );
  156.     m_hotToolBar.SetButtonDropDown( ID_BACK );
  157.     m_hotToolBar.SetButtonDropDown( ID_FORWARD );
  158.  
  159.     // Insert the CEdit control
  160.     m_edit = (CEdit*)m_wndToolBar.InsertControl(
  161.         RUNTIME_CLASS(CEdit), _T(""), CRect(-35,-22,0,0), IDC_EDIT,
  162.         ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP );
  163.  
  164.     // Insert the CComboBox control
  165.     m_combo = (CComboBox*)m_wndToolBar.InsertControl(
  166.         RUNTIME_CLASS(CComboBox), _T(""), CRect(-100,-200,0,0), IDC_COMBO,
  167.         WS_CHILD | CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS );
  168.  
  169.     // Add strings to combo box
  170.     for( i = 0; i < 5; i++ ) {
  171.         m_combo->AddString( comboText[i] );
  172.     }
  173.     
  174.     // Select first index.
  175.     m_combo->SetCurSel(0);
  176.  
  177.     // Install cool menus.
  178.     m_menuManager.Install((CFrameWnd*)this);
  179.     m_menuManager.LoadToolbar(IDR_MAINFRAME);
  180.  
  181.     return 0;
  182. }
  183.  
  184. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  185. {
  186.    cs.lpszClass = AfxRegisterWndClass(
  187.       CS_DBLCLKS,                       // if you need double-clicks
  188.       NULL,                             // no cursor (use default)
  189.       NULL,                             // no background brush
  190.       AfxGetApp()->LoadIcon(IDR_MAINFRAME)); // app icon
  191.    ASSERT(cs.lpszClass);
  192.     return CMDIFrameWnd::PreCreateWindow(cs);
  193. }
  194.  
  195. /////////////////////////////////////////////////////////////////////////////
  196. // CMainFrame diagnostics
  197.  
  198. #ifdef _DEBUG
  199. void CMainFrame::AssertValid() const
  200. {
  201.     CMDIFrameWnd::AssertValid();
  202. }
  203.  
  204. void CMainFrame::Dump(CDumpContext& dc) const
  205. {
  206.     CMDIFrameWnd::Dump(dc);
  207. }
  208.  
  209. #endif //_DEBUG
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CMainFrame message handlers
  213.  
  214. void CMainFrame::OnSelEndOk()
  215. {
  216.     SetComboIndex( m_combo->GetCurSel());
  217.     MessageBox( "Combo box selection made!",
  218.         "CCoolbar Message", MB_ICONINFORMATION );
  219. }
  220.  
  221. void CMainFrame::OnToolbarDropDown(NMTOOLBAR* pnmtb, LRESULT *plr)
  222. {
  223.     CWnd *pWnd;
  224.     UINT nID;
  225.  
  226.     if( pnmtb->iItem == ID_FILE_OPEN ) {
  227.         pWnd = &m_wndToolBar;
  228.         nID = IDR_POPUP;
  229.     }
  230.     
  231.     else if( pnmtb->iItem == ID_BACK ) {
  232.         pWnd = &m_hotToolBar;
  233.         nID = IDR_POPUP;
  234.     }
  235.  
  236.     else if( pnmtb->iItem == ID_FORWARD ) {
  237.         pWnd = &m_hotToolBar;
  238.         nID = IDR_POPUP;
  239.     }
  240.  
  241.     else {
  242.         return;
  243.     }
  244.  
  245.     // load and display popup menu
  246.     CMenu menu;
  247.     menu.LoadMenu(nID);
  248.     CMenu* pPopup = menu.GetSubMenu(0);
  249.     ASSERT(pPopup);
  250.  
  251.     CRect rc;
  252.     pWnd->SendMessage(TB_GETRECT, pnmtb->iItem, (LPARAM)&rc);
  253.     pWnd->ClientToScreen(&rc);
  254.  
  255.     pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
  256.         rc.left, rc.bottom, this, &rc);
  257. }
  258.  
  259. void CMainFrame::DockControlBarLeftOf(CToolBar* Bar, CToolBar* LeftOf)
  260. {
  261.     CRect rect;
  262.     DWORD dw;
  263.     UINT n;
  264.     
  265.     // get MFC to adjust the dimensions of all docked ToolBars
  266.     // so that GetWindowRect will be accurate
  267.     RecalcLayout(TRUE);
  268.     
  269.     LeftOf->GetWindowRect(&rect);
  270.     rect.OffsetRect(1,0);
  271.     dw=LeftOf->GetBarStyle();
  272.     n = 0;
  273.     n = (dw&CBRS_ALIGN_TOP)                 ? AFX_IDW_DOCKBAR_TOP        : n;
  274.     n = (dw&CBRS_ALIGN_BOTTOM    && n==0) ? AFX_IDW_DOCKBAR_BOTTOM    : n;
  275.     n = (dw&CBRS_ALIGN_LEFT        && n==0) ? AFX_IDW_DOCKBAR_LEFT        : n;
  276.     n = (dw&CBRS_ALIGN_RIGHT    && n==0) ? AFX_IDW_DOCKBAR_RIGHT    : n;
  277.     
  278.     // When we take the default parameters on rect, DockControlBar will dock
  279.     // each Toolbar on a seperate line. By calculating a rectangle, we in effect
  280.     // are simulating a Toolbar being dragged to that location and docked.
  281.     DockControlBar(Bar,n,&rect);
  282. }
  283.  
  284.  
  285. void CMainFrame::OnBack() 
  286. {
  287.     // TODO: Add your command handler code here
  288.     
  289. }
  290.  
  291. void CMainFrame::OnForward() 
  292. {
  293.     // TODO: Add your command handler code here
  294.     
  295. }
  296.  
  297. void CMainFrame::OnStop() 
  298. {
  299.     // TODO: Add your command handler code here
  300.     
  301. }
  302.  
  303. void CMainFrame::OnRefresh() 
  304. {
  305.     // TODO: Add your command handler code here
  306.     
  307. }
  308.  
  309. void CMainFrame::OnHome() 
  310. {
  311.     // TODO: Add your command handler code here
  312.     
  313. }
  314.  
  315.