home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / histbld.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.1 KB  |  251 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. // histbld.cpp
  20. // This implements the CHistoryMenuBuilder class
  21.  
  22. #include "stdafx.h"
  23. #include "histbld.h"
  24.  
  25. // Function - CHistoryMenuBuilder
  26. //
  27. // Constructor creates the command to history entry map
  28. // 
  29. // Arguments -  none
  30. //
  31. // Returns - nothing
  32. CHistoryMenuBuilder::CHistoryMenuBuilder()
  33. {
  34.  
  35.     m_pMenuMap = new CMapWordToPtr;
  36. }
  37.  
  38. // Function - ~CHistoryMenuBuilder
  39. //
  40. // Destructor destroys the command to history entry map
  41. // 
  42. // Arguments -  none
  43. //
  44. // Returns - nothing
  45. CHistoryMenuBuilder::~CHistoryMenuBuilder()
  46. {
  47.     m_pMenuMap->RemoveAll();
  48.     delete m_pMenuMap;
  49. }
  50.  
  51. // Function - Fill
  52. //
  53. // Fills the menu with up to MAX_HISTORY_MENU_ITEMS entries.  The type of fills
  54. // (everything, everything after the current item, or everything before the current
  55. //  item) is determined by the user 
  56. // 
  57. // Arguments -  hMenu                   the menu we are adding to
  58. //                nMaxMenuItemLength     the length of the longest item that can be stored in the menu.
  59. //                                       Anything longer will be truncated.
  60. //                eFill                   the type of fill to perform
  61. //
  62. // Returns - nothing
  63. void CHistoryMenuBuilder::Fill(HMENU hMenu, int nMaxMenuItemLength, eFillEnum eFill)
  64. {
  65.  
  66.         m_pMenuMap->RemoveAll();
  67.  
  68.         CAbstractCX * pCX = FEU_GetLastActiveFrameContext();
  69.         
  70.         if (!pCX)
  71.             return;
  72.  
  73.  
  74.         // Get the session history list
  75.         XP_List* pList = SHIST_GetList(pCX->GetContext());
  76.  
  77.         // Get the pointer to the current history entry
  78.         History_entry*  pCurrentDoc = pCX->GetContext()->hist.cur_doc_ptr;
  79.  
  80.         XP_List *pCurrentNode = XP_ListFindObject(pList, pCurrentDoc);
  81.  
  82.         ASSERT(pList);
  83.         if (!pList)
  84.             return;
  85.  
  86.         pList = pList->prev;
  87.         
  88.         int nNumItems = 0;
  89.  
  90.         if(eFill == eFILLFORWARD)
  91.         {
  92.             //Start at the one after the current node and pass in TRUE to go forward
  93.             FillItems(hMenu, pCurrentNode->next, TRUE, nMaxMenuItemLength, pCurrentDoc);
  94.         }
  95.         else if(eFill == eFILLBACKWARD)
  96.         {
  97.             //Start at the one before the current node and pass in FALSE to go backwards
  98.             FillItems(hMenu, pCurrentNode->prev, FALSE, nMaxMenuItemLength, pCurrentDoc);
  99.         }
  100.         else //eFill == eALL
  101.         {
  102.             //Start at the last item and pass in FALSE to go backwards
  103.             FillItems(hMenu, pList, FALSE, nMaxMenuItemLength, pCurrentDoc);
  104.         }
  105.         
  106. }
  107.  
  108. // Function - Execute
  109. //
  110. // Given the command id, this function finds the corresponding history entry and changes
  111. // the browser to that page
  112. // 
  113. // Arguments -  nCommandID         the command we want to execute
  114. //
  115. // Returns - TRUE if it could execute the command, FALSE otherwise
  116. BOOL CHistoryMenuBuilder::Execute(UINT nCommandID)
  117. {
  118.         void* nLastSelectedData;
  119.  
  120.         m_pMenuMap->Lookup(nCommandID, nLastSelectedData);
  121.  
  122.         CAbstractCX * pCX = FEU_GetLastActiveFrameContext();
  123.         
  124.         if (!pCX)
  125.             return FALSE;
  126.  
  127.         // Get ready to load
  128.         URL_Struct* pURL = SHIST_CreateURLStructFromHistoryEntry(pCX->GetContext(),
  129.             (History_entry*)nLastSelectedData);
  130.  
  131.         if (!pURL)
  132.             return FALSE;
  133.     
  134.         //      Load up.
  135.         pCX->GetUrl(pURL, FO_CACHE_AND_PRESENT);
  136.  
  137.         return TRUE;
  138. }
  139.  
  140. char * CHistoryMenuBuilder::GetURL(UINT nCommandID)
  141. {
  142.  
  143.         void* pCommandData;
  144.  
  145.         m_pMenuMap->Lookup(nCommandID, pCommandData);
  146.  
  147.         return ((History_entry*)pCommandData)->address;
  148. }
  149.  
  150. //////////////////////////////////////////////////////////////////////////
  151. //                        Helper Functions for CHistoryMenuBuilder
  152. //////////////////////////////////////////////////////////////////////////
  153.  
  154. // Function - AddHistoryToMenu
  155. //
  156. // Adds a history item to the passed in menu.  Also attaches a numerica accelerator to the menu item
  157. // if nPosition is less than 10.
  158. // 
  159. // Arguments -  hMenu                the menu we are adding to
  160. //                nPosition            the position in the menu (0 is at the top).  This is used for creating
  161. //                                    the accelerator.  Items are always appended to the menu
  162. //                bCurrent            Is this the current page that's showing.  If it is, we'll put a checkmark
  163. //                                    by the menu item.
  164. //                pEntry                the history entry we are adding to the menu
  165. //                nMaxMenuItemLength    The maximum length string we can store in the menu. Strings longer than
  166. //                                    this are truncated
  167. //
  168. // Returns - TRUE if it could add the item, FALSE otherwise
  169. BOOL CHistoryMenuBuilder::AddHistoryToMenu(HMENU hMenu, int nPosition, BOOL bCurrent, History_entry*  pEntry,
  170.                                            int nMaxMenuItemLength)
  171. {
  172.  
  173.     char*  lpszText;
  174.  
  175.     
  176.     ASSERT(pEntry->title);
  177.     if (!pEntry->title)
  178.          return FALSE;
  179.  
  180.     // Each of the menu items is numbered
  181.     if (nPosition < 10)
  182.         lpszText = PR_smprintf("&%d %s", nPosition, pEntry->title);
  183.     else
  184.         lpszText = PR_smprintf("  %s", pEntry->title);
  185.  
  186.     // We need to shorten the name to keep the menu width reasonable. TRUE for
  187.     // the third argument means to modify the passed in string and not make a copy
  188.     char*   lpszShortName = fe_MiddleCutString(lpszText, nMaxMenuItemLength, TRUE);
  189.  
  190.     // Append the menu item
  191.     ::AppendMenu(hMenu,MF_STRING | MF_ENABLED | bCurrent ? MF_CHECKED : MF_UNCHECKED, 
  192.                       FIRST_HISTORY_MENU_ID + nPosition, lpszShortName);
  193.  
  194.     // Add the history entry to the map. We need to do this because Windows
  195.     // doesn't allow any client data to be associated with menu items
  196.     m_pMenuMap->SetAt(FIRST_HISTORY_MENU_ID + nPosition, pEntry);
  197.  
  198.     // Free the allocated memory
  199.     XP_FREE(lpszText);
  200.  
  201.     return TRUE;
  202.  
  203. }
  204.  
  205. // Function - FillItems
  206. //
  207. // Adds all of the history entries in the passed in list to the menu up to MAX_NUM_HISTORY_MENU_ITEMS
  208. // 
  209. // Arguments -  hMenu                the menu we are adding to
  210. //                pList                the list that has all of the history entries
  211. //                bForward            if TRUE, iterate over the next link otherwise over the prev link
  212. //                nMaxMenuItemLength    The maximum length string we can store in the menu. Strings longer than
  213. //                                    this are truncated
  214. //                pCurrentDoc            The page we are currently on
  215. //
  216. // Returns - TRUE if it could add the items, FALSE otherwise
  217. BOOL CHistoryMenuBuilder::FillItems(HMENU hMenu, XP_List *pList, BOOL bForward, int nMaxMenuItemLength,
  218.                                            History_entry* pCurrentDoc)
  219. {
  220.  
  221.     int nNumItems = 0;
  222.  
  223.     while(pList)
  224.     {
  225.         History_entry*  pEntry = (History_entry*)pList->object;
  226.  
  227.  
  228.         ASSERT(pEntry);
  229.         if (!pEntry)
  230.             return FALSE;
  231.  
  232.         if(nNumItems >= MAX_NUM_HISTORY_MENU_ITEMS)
  233.             return FALSE;
  234.  
  235.         AddHistoryToMenu(hMenu, nNumItems++, pCurrentDoc == pEntry, pEntry, nMaxMenuItemLength);
  236.  
  237.     
  238.         if(bForward)
  239.         {
  240.             pList = pList->next;
  241.         }
  242.         else
  243.         {
  244.             pList = pList->prev;
  245.         }
  246.     }
  247.  
  248.     return TRUE;
  249. }
  250.  
  251.