home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CNavigationButtonPopup.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  5.8 KB  |  218 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. // ===========================================================================
  20. //    CNavigationButtonPopup.cp
  21. //
  22. // ===========================================================================
  23.  
  24. #include "CNavigationButtonPopup.h"
  25.         
  26. #include "prefapi.h"        
  27. #include "xp_mem.h"
  28. #include "shist.h"
  29. #include "Netscape_Constants.h"
  30. #include "CWindowMediator.h"
  31. #include "CBrowserContext.h"
  32. #include "CBrowserWindow.h"
  33. #include "UMenuUtils.h"
  34. #include "CAutoPtr.h"
  35. #include "macutil.h"
  36. #include "net.h"
  37.  
  38. #include <string>
  39.         
  40. // ---------------------------------------------------------------------------
  41. //        Ñ CNavigationButtonPopup
  42. // ---------------------------------------------------------------------------
  43.  
  44. CNavigationButtonPopup::CNavigationButtonPopup(
  45.     LStream* inStream)
  46.  
  47.     :    mBrowserContext(nil),
  48.         mHistory(nil),
  49.         mCurrentEntry(nil),
  50.         mCurrentEntryIndex(0),
  51.         mNumItemsInHistory(0),
  52.     
  53.         super(inStream)
  54. {
  55. }
  56.  
  57. // ---------------------------------------------------------------------------
  58. //        Ñ ~CNavigationButtonPopup
  59. // ---------------------------------------------------------------------------
  60.  
  61. CNavigationButtonPopup::~CNavigationButtonPopup()
  62. {
  63. }
  64.     
  65. #pragma mark -
  66.  
  67. // ---------------------------------------------------------------------------
  68. //        Ñ AdjustMenuContents
  69. // ---------------------------------------------------------------------------
  70.  
  71. void
  72. CNavigationButtonPopup::AdjustMenuContents()
  73. {
  74.     if (!GetMenu() || !GetMenu()->GetMacMenuH())
  75.     {
  76.         return;
  77.     }
  78.     
  79.     if (!AssertPreconditions())
  80.     {
  81.         return;
  82.     }
  83.         
  84.     // Purge the menu
  85.  
  86.     UMenuUtils::PurgeMenuItems(GetMenu()->GetMacMenuH());
  87.     
  88.     // Fill the menu
  89.     
  90.     if (GetQuickClickValueOrCommand() == cmd_GoBack)
  91.     {
  92.         for (int insertAfterItem = 0, i = mCurrentEntryIndex - 1; i >= 1; i--, insertAfterItem++)
  93.         {
  94.             InsertHistoryItemIntoMenu(i, insertAfterItem);
  95.         }
  96.     }
  97.     else if (GetQuickClickValueOrCommand() == cmd_GoForward)
  98.     {
  99.         for (int insertAfterItem = 0, i = mCurrentEntryIndex + 1; i <= mNumItemsInHistory; i++, insertAfterItem++)
  100.         {
  101.             InsertHistoryItemIntoMenu(i, insertAfterItem);
  102.         }
  103.     }
  104.     
  105.     // Set the min/max values of the control since we populated the menu
  106.     
  107.     SetPopupMinMaxValues();
  108. }
  109.  
  110. // ---------------------------------------------------------------------------
  111. //        Ñ InsertHistoryItemIntoMenu
  112. // ---------------------------------------------------------------------------
  113.  
  114. void
  115. CNavigationButtonPopup::InsertHistoryItemIntoMenu(
  116.     Int32                inHistoryItemIndex,
  117.     Int16                inAfterItem)
  118. {
  119.     Assert_(GetMenu() && GetMenu()->GetMacMenuH());
  120.     Assert_(mBrowserContext);
  121.  
  122.     CAutoPtr<cstring> theTitle = mBrowserContext->GetHistoryEntryTitleByIndex(inHistoryItemIndex);
  123.     
  124.     if (!theTitle.get() || !theTitle->length())
  125.     {
  126.         theTitle.reset(new cstring);
  127.         mBrowserContext->GetHistoryURLByIndex(*theTitle, inHistoryItemIndex);
  128.     }
  129.     
  130.     UMenuUtils::AdjustStringForMenuTitle(*theTitle);
  131.     
  132.     LStr255 thePString(*theTitle);
  133.  
  134.     // Insert a "blank" item first...
  135.     
  136.     ::InsertMenuItem(GetMenu()->GetMacMenuH(), "\p ", inAfterItem + 1);
  137.     
  138.     // Then change it. We do this so that no interpretation of metacharacters will occur.
  139.     
  140.     ::SetMenuItemText(GetMenu()->GetMacMenuH(), inAfterItem + 1, thePString);
  141. }
  142.     
  143. #pragma mark -
  144.  
  145. // ---------------------------------------------------------------------------
  146. //        Ñ HandleNewValue
  147. // ---------------------------------------------------------------------------
  148.  
  149. Boolean
  150. CNavigationButtonPopup::HandleNewValue(
  151.     Int32    inNewValue)
  152. {
  153.     if (AssertPreconditions() && inNewValue)
  154.     {
  155.         Int32 historyIndex = 0;
  156.  
  157.         if (GetQuickClickValueOrCommand() == cmd_GoBack)
  158.         {
  159.             historyIndex = SHIST_GetIndex(mHistory, mCurrentEntry) - inNewValue;
  160.         }
  161.         else if (GetQuickClickValueOrCommand() == cmd_GoForward)
  162.         {
  163.             historyIndex = SHIST_GetIndex(mHistory, mCurrentEntry) + inNewValue;
  164.         }
  165.  
  166.         if (historyIndex)
  167.         {
  168.             mBrowserContext->LoadHistoryEntry(historyIndex);
  169.         }
  170.     }
  171.     
  172.     return true;
  173. }
  174.  
  175. // ---------------------------------------------------------------------------
  176. //        Ñ AssertPreconditions
  177. // ---------------------------------------------------------------------------
  178. //    Assert preconditions and fill in interesting member data
  179.  
  180. Boolean
  181. CNavigationButtonPopup::AssertPreconditions()
  182. {    
  183.     CMediatedWindow* topWindow = CWindowMediator::GetWindowMediator()->FetchTopWindow(WindowType_Any, regularLayerType);
  184.     
  185.     if (!topWindow || topWindow->GetWindowType() != WindowType_Browser)
  186.     {
  187.         return false;
  188.     }
  189.     
  190.     CBrowserWindow* browserWindow = dynamic_cast<CBrowserWindow*>(topWindow);
  191.  
  192.     if (!browserWindow)
  193.     {
  194.         return false;
  195.     }
  196.     
  197.     if (!(mBrowserContext = (CBrowserContext*)browserWindow->GetWindowContext()))
  198.     {
  199.         return false;
  200.     }
  201.  
  202.     if (!(mHistory = &((MWContext*)(*mBrowserContext))->hist))
  203.     {
  204.         return false;
  205.     }
  206.     
  207.     if (!(mCurrentEntry = mBrowserContext->GetCurrentHistoryEntry()))
  208.     {
  209.         return false;
  210.     }
  211.  
  212.     mCurrentEntryIndex = SHIST_GetIndex(mHistory, mCurrentEntry);
  213.     
  214.     mNumItemsInHistory = mBrowserContext->GetHistoryListCount();
  215.     
  216.     return true;
  217. }
  218.