home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / CGuidePopupMenu.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  6.7 KB  |  254 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. //    CGuidePopupMenu.cp
  21. // ===========================================================================
  22.  
  23. #include "CGuidePopupMenu.h"
  24.         
  25. #include <algorithm>
  26.         
  27. #include "prefapi.h"        
  28. #include "CApplicationEventAttachment.h"
  29. #include "uapp.h"
  30. #include "CAutoPtrXP.h"
  31.  
  32. // ---------------------------------------------------------------------------
  33. //        Ñ [static member data]
  34. // ---------------------------------------------------------------------------
  35.  
  36. CAutoPtr<LMenu>    CGuidePopupMenu::sMenu;
  37. Boolean            CGuidePopupMenu::sOwnsMenu = true;
  38. Boolean            CGuidePopupMenu::sMenuIsSetup = false;
  39. vector<string>    CGuidePopupMenu::sURLs;        
  40.  
  41. // ---------------------------------------------------------------------------
  42. //        Ñ CGuidePopupMenu
  43. // ---------------------------------------------------------------------------
  44.  
  45. CGuidePopupMenu::CGuidePopupMenu(
  46.     LStream* inStream)
  47.  
  48.     :    super(inStream)
  49. {
  50. }
  51.  
  52. // ---------------------------------------------------------------------------
  53. //        Ñ ~CGuidePopupMenu
  54. // ---------------------------------------------------------------------------
  55.  
  56. CGuidePopupMenu::~CGuidePopupMenu()
  57. {
  58. }
  59.     
  60. #pragma mark -
  61.     
  62. // ---------------------------------------------------------------------------
  63. //        Ñ FinishCreateSelf
  64. // ---------------------------------------------------------------------------
  65.                         
  66. void
  67. CGuidePopupMenu::FinishCreateSelf()
  68. {
  69.     super::FinishCreateSelf();
  70.     
  71.     SetupMenu();
  72. }
  73.     
  74. #pragma mark -
  75.     
  76. // ---------------------------------------------------------------------------
  77. //        Ñ SetupMenu
  78. // ---------------------------------------------------------------------------
  79.  
  80. void
  81. CGuidePopupMenu::SetupMenu()
  82. {
  83.     ThrowIfNil_(GetMenu());
  84.     ThrowIfNil_(GetMenu()->GetMacMenuH());
  85.     
  86.     if (!sMenuIsSetup)
  87.     {
  88.         // Populate the URL vector with the toolbar places items using
  89.         // the javascript preferences mechanism
  90.         
  91.         for (int i = 0, rc = PREF_NOERROR; rc == PREF_NOERROR; i++)
  92.         {
  93.             CAutoPtrXP<char>    label;
  94.             CAutoPtrXP<char>    url;
  95.             char*                tempLabel = 0;
  96.             char*                tempURL = 0;        
  97.             
  98.             rc = PREF_CopyIndexConfigString("toolbar.places.item", i, "label", &tempLabel);
  99.             label.reset(tempLabel);
  100.             
  101.             if (rc == PREF_NOERROR)
  102.             {
  103.                 PREF_CopyIndexConfigString("toolbar.places.item", i, "url", &tempURL);
  104.                 url.reset(tempURL);
  105.                 
  106.                 CtoPstr(label.get());
  107.                 
  108.                 // Insert a "blank" item first...
  109.                 
  110.                 GetMenu()->InsertCommand("\p ", cmd_Nothing, i);
  111.                 
  112.                 // Then change it. We do this so that no interpretation of metacharacters will occur.
  113.                 
  114.                 ::SetMenuItemText(GetMenu()->GetMacMenuH(), i + 1, (StringPtr)label.get());
  115.  
  116.                 sURLs.push_back(url.get());                            
  117.             }
  118.         }
  119.         
  120.         sMenuIsSetup = true;
  121.     }
  122. }
  123.     
  124. #pragma mark -
  125.                         
  126. // ---------------------------------------------------------------------------
  127. //        Ñ GetMenu
  128. // ---------------------------------------------------------------------------
  129.     
  130. LMenu*
  131. CGuidePopupMenu::GetMenu() const
  132. {
  133.     return sMenu.get();
  134.     
  135. }    
  136.     
  137. // ---------------------------------------------------------------------------
  138. //        Ñ OwnsMenu
  139. // ---------------------------------------------------------------------------
  140.     
  141. Boolean
  142. CGuidePopupMenu::OwnsMenu() const
  143. {
  144.     return sOwnsMenu;
  145.     
  146. }    
  147.                     
  148. // ---------------------------------------------------------------------------
  149. //        Ñ SetMenu
  150. // ---------------------------------------------------------------------------
  151.     
  152. void
  153. CGuidePopupMenu::SetMenu(LMenu* inMenu)
  154. {
  155.     ThrowIfNot_(GetMenu() == nil);
  156.     ThrowIfNot_(inMenu == nil);
  157.     
  158.     sMenu.reset(nil);
  159. }
  160.  
  161. // ---------------------------------------------------------------------------
  162. //        Ñ AdoptMenu
  163. // ---------------------------------------------------------------------------
  164.  
  165. void
  166. CGuidePopupMenu::AdoptMenu(
  167.     LMenu* inMenuToAdopt)
  168. {
  169.     ThrowIfNot_(GetMenu() == nil);
  170.  
  171.     EliminatePreviousMenu();
  172.     
  173.     sMenu.reset(inMenuToAdopt);
  174.     sOwnsMenu = true;
  175. }
  176.  
  177. // ---------------------------------------------------------------------------
  178. //        Ñ MakeNewMenu
  179. // ---------------------------------------------------------------------------
  180. //    We are caching the menu in a static CAutoPtr. So we guard instantiations
  181. //    and only allow one.
  182.  
  183. void
  184. CGuidePopupMenu::MakeNewMenu()
  185. {
  186.     if (!GetMenu())
  187.     {
  188.         super::MakeNewMenu();
  189.     }
  190. }
  191.  
  192. // ---------------------------------------------------------------------------
  193. //        Ñ EliminatePreviousMenu
  194. // ---------------------------------------------------------------------------
  195. //    This is intentionally empty. The lifetime of the menu is controlled by
  196. //    the static CAutoPtr.
  197.  
  198. void
  199. CGuidePopupMenu::EliminatePreviousMenu()
  200. {
  201. }                        
  202.  
  203. #pragma mark -
  204.  
  205. // ---------------------------------------------------------------------------
  206. //        Ñ GetItem
  207. // ---------------------------------------------------------------------------
  208.         
  209. short
  210. CGuidePopupMenu::GetItem(const string& inURL) const
  211. {
  212.     short item = 0;
  213.     
  214.     vector<string>::const_iterator theItem = find(sURLs.begin(), sURLs.end(), inURL.c_str());
  215.     
  216.     if (theItem != sURLs.end())
  217.     {
  218.         item = (theItem - sURLs.begin()) + 1;
  219.     }
  220.     
  221.     return item;
  222. }
  223.     
  224. // ---------------------------------------------------------------------------
  225. //        Ñ GetURL
  226. // ---------------------------------------------------------------------------
  227.         
  228. string
  229. CGuidePopupMenu::GetURL(short item) const
  230. {
  231.     ThrowIfNot_(item >= 0 && item <= sURLs.size() - 1);
  232.     
  233.     return sURLs[item];
  234. }
  235.  
  236. #pragma mark -
  237.  
  238. // ---------------------------------------------------------------------------
  239. //        Ñ HandleNewValue
  240. // ---------------------------------------------------------------------------
  241.  
  242. Boolean
  243. CGuidePopupMenu::HandleNewValue(Int32 inNewValue)
  244. {
  245.     if (inNewValue)
  246.     {
  247.         CFrontApp& theApp = dynamic_cast<CFrontApp&>(CApplicationEventAttachment::GetApplication());
  248.         
  249.         theApp.DoGetURL(GetURL(inNewValue - 1).c_str());
  250.     }
  251.     
  252.     return true;
  253. }
  254.