home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CHistoryMenu.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  5.5 KB  |  170 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. //    CHistoryMenu.cp
  21. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  22.  
  23. #include "Netscape_Constants.h"
  24.  
  25. #include "CHistoryMenu.h"
  26. #include "CWindowMediator.h"
  27. #include "CBrowserContext.h"
  28. #include "CBrowserWindow.h"
  29. #include "UMenuUtils.h"
  30. #include "cstring.h"
  31.  
  32. #include "xp_list.h"
  33.  
  34. CHistoryMenu* CHistoryMenu::sHistoryMenu = NULL;
  35.  
  36. CHistoryMenu::CHistoryMenu(ResIDT inMenuID) :
  37.     LMenu(inMenuID),
  38.     mCurrentHistoryEntry(NULL)
  39. {
  40.     mLastNonDynamicItem = ::CountMenuItems(GetMacMenuH());
  41.     sHistoryMenu = this;
  42. }
  43.  
  44. CHistoryMenu::~CHistoryMenu()
  45. {
  46.     sHistoryMenu = NULL;
  47. }
  48.  
  49. void CHistoryMenu::Update(void)
  50. {
  51.     CMediatedWindow* topWindow = (CWindowMediator::GetWindowMediator())->FetchTopWindow(WindowType_Any, regularLayerType);
  52.     if (topWindow)
  53.     {
  54.         if (topWindow->GetWindowType() == WindowType_Browser)
  55.         {
  56.             CBrowserWindow* browserWindow = (CBrowserWindow*)topWindow;
  57.             if (browserWindow->GetWindowContext())
  58.             {
  59.                 History_entry* currentEntry = (browserWindow->GetWindowContext())->GetCurrentHistoryEntry();
  60.                 if (GetCurrentHistoryEntry() != currentEntry)
  61.                     SyncMenuToHistory(browserWindow->GetWindowContext());
  62.             }
  63.         }
  64.     }
  65.     else
  66.         // If there are no windows, we default to browser menus, so clear
  67.         // history items from menu
  68.         UMenuUtils::PurgeMenuItems(GetMacMenuH(), GetLastNonDynamicItem());
  69. }
  70.  
  71. // Rebuild history menu
  72. void CHistoryMenu::SyncMenuToHistory(CNSContext* inNSContext)
  73. {
  74.     Int16                menuSize;
  75.     Int16                numHistoryMenuEntries;
  76.     Int16                historyLength;
  77.     
  78.     menuSize = ::CountMItems(GetMacMenuH());
  79.     historyLength = inNSContext->GetHistoryListCount();
  80.     
  81.     // this is total number of history menu items we want in menu
  82.     // add one because we loop from 1 -> num instead of from 0 -> (num - 1)
  83.     numHistoryMenuEntries = MIN(historyLength, cMaxHistoryMenuItems) + 1;
  84.  
  85.     Int16    count = 1,
  86.             // since we want most recent history entries, grab entries from end
  87.             // of history list
  88.             entryIndex = historyLength,
  89.             // this is index of menu item we're going to insert
  90.             currentMenuItemIndex = count + GetLastNonDynamicItem(),
  91.             // index of of current history entry in 
  92.             currentEntryIndex = inNSContext->GetIndexOfCurrentHistoryEntry();
  93.     // Now loop over current history menu items, overwriting current menu items.
  94.     // If the new history is longer than current one, append menu items
  95.     while (count < numHistoryMenuEntries)
  96.     {
  97.         // copy title string because we need to 'unescape' string
  98.         CAutoPtr<cstring> title;
  99.         
  100.         title.reset(inNSContext->GetHistoryEntryTitleByIndex(entryIndex));
  101.         if (title.get())
  102.         {
  103.             if (!title->length())
  104.             {
  105.                 title.reset(new cstring);
  106.                 inNSContext->GetHistoryURLByIndex(*title, entryIndex);
  107.             }
  108.         
  109.             UMenuUtils::AdjustStringForMenuTitle(*title);
  110.             LStr255 pstr(*title);
  111.             // append menu item if we've reached end of menu
  112.             if (currentMenuItemIndex > menuSize)
  113.                 UMenuUtils::AppendMenuItem(GetMacMenuH(), pstr, true);
  114.             else
  115.                 ::SetMenuItemText(GetMacMenuH(), currentMenuItemIndex, pstr);
  116.         }
  117.  
  118. //        NOTE: These command keys are used in the "Window" menu now.
  119. //        if (count < 11)        // Setting of command numbers
  120. //            ::SetItemCmd(GetMacMenuH(), currentMenuItemIndex, '0' + count - 1);
  121. //        else
  122. //            ::SetItemCmd(GetMacMenuH(), currentMenuItemIndex, 0);
  123.  
  124.         if (entryIndex == currentEntryIndex)    // Checkmark next to current entry
  125.             ::SetItemMark(GetMacMenuH(), currentMenuItemIndex, '');
  126.         else
  127.             ::SetItemMark(GetMacMenuH(), currentMenuItemIndex, 0);
  128.         ++count;
  129.         --entryIndex;
  130.         ++currentMenuItemIndex;
  131.     }
  132.     // remove left over menu items just in case new history list is
  133.     // smaller than previous one
  134.     if (currentMenuItemIndex <= menuSize)
  135.         UMenuUtils::PurgeMenuItems(GetMacMenuH(), currentMenuItemIndex - 1);
  136.     SetCurrentHistoryEntry(inNSContext->GetCurrentHistoryEntry());
  137. }
  138.  
  139. CommandT CHistoryMenu::GetFirstSyntheticCommandID(void)
  140. {
  141.     if (sHistoryMenu)
  142.         return (-(((Int32)sHistoryMenu->GetMenuID()) << 16)
  143.                 - sHistoryMenu->GetLastNonDynamicItem()
  144.                 - 1);
  145.     else
  146.         return cmd_Nothing;
  147. }
  148.  
  149. Boolean CHistoryMenu::IsHistoryMenuSyntheticCommandID(CommandT inCommandID)
  150. {
  151.     Boolean result = false;
  152.     CommandT firstCommandID, lastCommandID;
  153.     
  154.     if (sHistoryMenu)
  155.     {
  156.         firstCommandID = -(((Int32)sHistoryMenu->GetMenuID()) << 16)
  157.                          - sHistoryMenu->GetLastNonDynamicItem()
  158.                          - 1;
  159.         lastCommandID = firstCommandID
  160.                         - (::CountMenuItems(sHistoryMenu->GetMacMenuH())
  161.                            - sHistoryMenu->GetLastNonDynamicItem())
  162.                         + 1;
  163.         // check that lastCommandID <= inCommandID <= firstCommandID
  164.         // note: we're checking synthetic commands which are negative
  165.         result = lastCommandID <= inCommandID && inCommandID <= firstCommandID;
  166.     }
  167.     
  168.     return result;
  169. }
  170.