home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / navbar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.4 KB  |  317 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // Embedded menu and close box for Aurora (Created by Dave Hyatt)
  20.  
  21. #include "stdafx.h"
  22. #include "navbar.h"
  23. #include "navcntr.h"
  24. #include "navfram.h"
  25. #include "usertlbr.h"
  26. #include "dropmenu.h"
  27. #include "rdfliner.h"
  28. #include "htrdf.h"
  29.  
  30. BEGIN_MESSAGE_MAP(CNavMenuButton, CLinkToolbarButton)
  31.     ON_MESSAGE(NSDRAGMENUOPEN, OnDragMenuOpen) 
  32. END_MESSAGE_MAP()
  33.  
  34. CNavMenuButton::CNavMenuButton()
  35. :m_HTView(NULL)
  36. {
  37.     m_bShouldShowRMMenu = FALSE;
  38. }
  39.  
  40. extern void DrawUpButton(HDC dc, CRect& rect);
  41.  
  42. LRESULT CNavMenuButton::OnDragMenuOpen(WPARAM wParam, LPARAM lParam)
  43. {
  44. // Set the focus to the tree view.
  45.     if (!m_HTView)
  46.         return 1;
  47.  
  48.     CSelectorButton* pSelectorButton = (CSelectorButton*)HT_GetViewFEData(m_HTView);
  49.     if (!pSelectorButton)
  50.         return 1;
  51.  
  52.     // Get the tree view and set focus to it.
  53.     CRDFContentView* pView = pSelectorButton->GetTreeView();
  54.     if (pView)
  55.         pView->SetFocus();
  56.     
  57. // Set our drop menu's user data.
  58.     m_pCachedDropMenu = (CDropMenu *)lParam;  // Set our drop menu
  59.     
  60.     m_MenuCommandMap.Clear();
  61.     HT_View theView = m_HTView;
  62.     HT_Resource node = NULL;
  63.     node = HT_GetNextSelection(theView, node);
  64.     BOOL bg = (node == NULL);
  65.     
  66.     HT_Cursor theCursor = HT_NewContextualMenuCursor(theView, PR_FALSE, (PRBool)bg);
  67.     if (theCursor != NULL)
  68.     {
  69.         // We have a cursor. Attempt to iterate
  70.         HT_MenuCmd theCommand; 
  71.         while (HT_NextContextMenuItem(theCursor, &theCommand))
  72.         {
  73.             char* menuName = HT_GetMenuCmdName(theCommand);
  74.             if (theCommand == HT_CMD_SEPARATOR)
  75.                 m_pCachedDropMenu->AppendMenu(MF_SEPARATOR, 0, TRUE, NULL, NULL);
  76.             else
  77.             {
  78.                 // Add the command to our command map
  79.                 CRDFMenuCommand* rdfCommand = new CRDFMenuCommand(menuName, theCommand);
  80.                 int index = m_MenuCommandMap.AddCommand(rdfCommand);
  81.                 m_pCachedDropMenu->AppendMenu(MF_STRING, index+FIRST_HT_MENU_ID, menuName, TRUE, NULL, NULL, NULL);
  82.             }
  83.         }
  84.         HT_DeleteCursor(theCursor);
  85.     }
  86.     return 1;
  87. }
  88.  
  89. BOOL CNavMenuButton::OnCommand(UINT wParam, LONG lParam)
  90. {
  91.     BOOL bRtn = TRUE;
  92.     
  93.     if (wParam >= FIRST_HT_MENU_ID && wParam <= LAST_HT_MENU_ID)
  94.     {
  95.         // A selection was made from the context menu.
  96.         // Use the menu map to get the HT command value
  97.         CRDFMenuCommand* theCommand = (CRDFMenuCommand*)(m_MenuCommandMap.GetCommand((int)wParam-FIRST_HT_MENU_ID));
  98.         if (theCommand)
  99.         {
  100.             HT_MenuCmd htCommand = theCommand->GetHTCommand();
  101.             HT_DoMenuCmd(HT_GetPane(m_HTView), htCommand);
  102.         }
  103.         
  104.     }
  105.  
  106.     return bRtn;
  107. }
  108.  
  109. void CNavMenuButton::UpdateView(HT_View v)
  110. {
  111.     m_HTView = v;
  112.     if (v == NULL)
  113.         SetText("No View Selected.");
  114.     else SetText(HT_GetNodeName(HT_TopNode(m_HTView)));
  115. }
  116.  
  117. void CNavMenuButton::UpdateButtonText(CRect rect)
  118. {
  119.     CString originalText = "No View Selected.";
  120.     if (m_HTView != NULL)
  121.         originalText = HT_GetNodeName(HT_TopNode(m_HTView));    
  122.     
  123.     int currCount = originalText.GetLength();
  124.         
  125.     // Start off at the maximal string
  126.     CString strTxt = originalText;
  127.  
  128.     CSize theSize = GetButtonSizeFromChars(originalText, currCount);
  129.     int horExtent = theSize.cx;
  130.     int width = horExtent;
  131.     int cutoff = rect.right - NAVBAR_CLOSEBOX - 9;
  132.     if (width > cutoff && cutoff > 0)
  133.             width = cutoff;
  134.         
  135.     while (horExtent > width)
  136.     {
  137.         strTxt = originalText.Left(currCount-3) + "...";
  138.         theSize = GetButtonSizeFromChars(strTxt, currCount);
  139.         horExtent = theSize.cx;
  140.         currCount--;
  141.     }
  142.  
  143.     SetTextWithoutResize(strTxt);
  144.  
  145.     int height = GetRequiredButtonSize().cy;
  146.     MoveWindow(2, (rect.Height()-height)/2, width, height);
  147. }
  148.  
  149. BEGIN_MESSAGE_MAP(CNavMenuBar, CWnd)
  150.     //{{AFX_MSG_MAP(CNavMenuBar)
  151.         // NOTE - the ClassWizard will add and remove mapping macros here.
  152.         //    DO NOT EDIT what you see in these blocks of generated code !
  153.     ON_WM_CREATE()
  154.     ON_WM_SIZE()
  155.     ON_WM_LBUTTONDOWN()
  156.     ON_WM_LBUTTONUP ( )
  157.     ON_WM_MOUSEMOVE()
  158.     ON_WM_PAINT()
  159.     //}}AFX_MSG_MAP
  160. END_MESSAGE_MAP()
  161.  
  162. CNavMenuBar::CNavMenuBar()
  163. :m_pSelectorButton(NULL), m_pMenuButton(NULL), m_bHasFocus(FALSE)
  164. {
  165. }
  166.  
  167. CNavMenuBar::~CNavMenuBar()
  168. {
  169.     delete m_pMenuButton;
  170. }
  171.  
  172. void CNavMenuBar::OnPaint( )
  173. {
  174.     CPaintDC dc(this);
  175.     CRect rect;
  176.     GetClientRect(&rect);
  177.     
  178.     if (m_bHasFocus)
  179.         m_pMenuButton->SetCustomColors(::GetSysColor(COLOR_CAPTIONTEXT), ::GetSysColor(COLOR_ACTIVECAPTION));
  180.     else m_pMenuButton->SetCustomColors(::GetSysColor(COLOR_INACTIVECAPTIONTEXT), ::GetSysColor(COLOR_INACTIVECAPTION));
  181.  
  182.     CBrush faceBrush(::GetSysColor(m_bHasFocus? COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION));
  183.  
  184.     dc.FillRect(&rect, &faceBrush);
  185.     
  186.     // Draw the close box at the edge of the bar.
  187.     
  188.     int left = rect.right - NAVBAR_CLOSEBOX - 5;
  189.     int right = left + NAVBAR_CLOSEBOX;
  190.     int top = rect.top + (rect.Height() - NAVBAR_CLOSEBOX)/2;
  191.     int bottom = top + NAVBAR_CLOSEBOX;
  192.  
  193.     CRect edgeRect(left, top, right, bottom);
  194.     CBrush* closeBoxBrush = CBrush::FromHandle(sysInfo.m_hbrBtnFace);
  195.     dc.FillRect(&edgeRect, closeBoxBrush);
  196.  
  197.     DrawUpButton(dc.m_hDC, edgeRect);
  198.  
  199.     HPEN hPen = (HPEN)::CreatePen(PS_SOLID, 1, RGB(0,0,0));
  200.     HPEN hOldPen = (HPEN)(dc.SelectObject(hPen));
  201.  
  202.     dc.MoveTo(edgeRect.left+4, edgeRect.top+4);
  203.     dc.LineTo(edgeRect.right-5, edgeRect.bottom-4);
  204.     dc.MoveTo(edgeRect.right-6, edgeRect.top+4);
  205.     dc.LineTo(edgeRect.left+3, edgeRect.bottom-4);
  206.     
  207.     HPEN hShadowPen = ::CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
  208.  
  209.     dc.SelectObject(hShadowPen);
  210.     dc.MoveTo(rect.left, rect.bottom-1);
  211.     dc.LineTo(rect.right, rect.bottom-1);
  212.     dc.SelectObject(hOldPen);
  213.  
  214.     VERIFY(::DeleteObject(hPen));
  215.     VERIFY(::DeleteObject(hShadowPen));
  216. }
  217.  
  218. int CNavMenuBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
  219. {
  220.     m_pMenuButton = new CNavMenuButton();
  221.     BOOKMARKITEM dummy;
  222.  
  223.     m_pMenuButton->Create(this, FALSE, CSize(32, 21), CSize(32, 21), "No view selected.", 
  224.                           "Click here to view the options menu.",
  225.                           "Click on the button to view the options menu.", CSize(0,0), 100, 15, dummy,
  226.                           NULL, TB_HAS_IMMEDIATE_MENU | TB_HAS_DRAGABLE_MENU);
  227.     return 0;
  228. }
  229.  
  230. void CNavMenuBar::OnLButtonDown (UINT nFlags, CPoint point )
  231. {
  232.     // Called when the user clicks on the menu bar.  Start a drag or collapse the view.
  233.     CRect rect;
  234.     GetClientRect(&rect);
  235.     
  236.     int left = rect.right - NAVBAR_CLOSEBOX - 5;
  237.     int right = left + NAVBAR_CLOSEBOX;
  238.     int top = rect.top + (rect.Height() - NAVBAR_CLOSEBOX)/2;
  239.     int bottom = top + NAVBAR_CLOSEBOX;
  240.  
  241.     CRect edgeRect(left, top, right, bottom);
  242.     if (edgeRect.PtInRect(point))
  243.     {
  244.         // Collapse the view
  245.         if (m_pSelectorButton)
  246.             m_pSelectorButton->OnAction();
  247.     }
  248.     else
  249.     {
  250.         // Set the focus.
  251.         if (m_pSelectorButton)
  252.         {
  253.             CRDFContentView* pView = m_pSelectorButton->GetTreeView();
  254.             if (pView)
  255.                 pView->SetFocus();
  256.         }
  257.  
  258.         m_PointHit = point;
  259.         SetCapture();
  260.     }
  261. }
  262.  
  263.  
  264. void CNavMenuBar::OnMouseMove(UINT nFlags, CPoint point)
  265. {
  266.     if (GetCapture() == this)
  267.     {
  268.         CNSNavFrame* navFrameParent = (CNSNavFrame*)GetParentFrame();
  269.         
  270.         if (abs(point.x - m_PointHit.x) > 3 ||
  271.             abs(point.y - m_PointHit.y) > 3)
  272.         {
  273.             ReleaseCapture();
  274.  
  275.             // Start a drag
  276.             MapWindowPoints(navFrameParent, &point, 1); 
  277.             navFrameParent->StartDrag(point);
  278.         }
  279.     }
  280. }
  281.  
  282. void CNavMenuBar::OnLButtonUp(UINT nFlags, CPoint point)
  283. {
  284.     if (GetCapture() == this) 
  285.     {
  286.         ReleaseCapture();
  287.     }
  288. }
  289.  
  290. void CNavMenuBar::OnSize( UINT nType, int cx, int cy )
  291. {    
  292.     if (m_pMenuButton)
  293.     {
  294.         CRect rect;
  295.         GetClientRect(&rect);
  296.         m_pMenuButton->UpdateButtonText(rect);
  297.     }
  298. }
  299.  
  300. void CNavMenuBar::UpdateView(CSelectorButton* pButton, HT_View view)
  301.     m_pSelectorButton = pButton; 
  302.  
  303.     if (m_pMenuButton)
  304.     {
  305.         CRect rect;
  306.         GetClientRect(&rect);
  307.         m_pMenuButton->UpdateView(view);
  308.         m_pMenuButton->UpdateButtonText(rect);
  309.     }
  310.  
  311.     if (pButton && pButton->GetTreeView())
  312.     {
  313.         ((CRDFOutliner*)(pButton->GetTreeView()->GetOutlinerParent()->GetOutliner()))->SetDockedMenuBar(this);
  314.     }
  315. }
  316.