home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / Composer / CRecentEditMenuAttachment.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  5.3 KB  |  211 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. // CRecentEditMenuAttachment.cp
  21. //===========================================================
  22.  
  23. #include "CRecentEditMenuAttachment.h"
  24. #include "CEditView.h"
  25. #include "CBrowserContext.h"                //    operator MWContext*()
  26. #include "resgui.h"
  27. #include "macutil.h"                        //    CMediatedWindow
  28. #include "UMenuUtils.h"
  29. #include "edt.h"
  30. #include "CEditorWindow.h"
  31.  
  32.  
  33.  
  34. LMenu *CRecentEditMenuAttachment::sMenu = NULL;
  35.  
  36. CRecentEditMenuAttachment::CRecentEditMenuAttachment()
  37. {
  38.     UpdateMenu();
  39. }
  40.  
  41. MWContext *CRecentEditMenuAttachment::GetTopWindowContext()
  42. {
  43.     // Ok, ok. I know this is skanky,
  44.     // but there is no common way to get the context from a window: it depends on the window type.
  45.     // So, we look around for a CEditView somewhere in the top window.
  46.     // A CEditView we understand (and get an MWContext from).
  47.     
  48.     CMediatedWindow* topWin = NULL;        // find the top window to use the plugin in
  49.     CWindowIterator iter(WindowType_Any);
  50.     iter.Next(topWin);
  51.     
  52.     if (topWin == NULL
  53.     || ! (topWin->GetWindowType() == WindowType_Editor || topWin->GetWindowType() == WindowType_Compose) )
  54.         return NULL;
  55.     
  56.     CEditView *editView = (CEditView *)(topWin->FindPaneByID(CEditView::pane_ID));
  57.         
  58.     if (editView == NULL || editView->GetNSContext() == NULL)
  59.         return NULL;
  60.         
  61.     return editView->GetNSContext()->operator MWContext*();
  62. }
  63.  
  64.  
  65. // Processes
  66. void CRecentEditMenuAttachment::ExecuteSelf( MessageT inMessage, void* ioParam )
  67. {
  68.     switch ( inMessage )    
  69.     {
  70.         case msg_CommandStatus:
  71.         {
  72.             SCommandStatus*    status = (SCommandStatus*)ioParam;
  73.             
  74.             if ( status->command >= RECENT_EDIT_MENU_BASE && status->command <= RECENT_EDIT_MENU_BASE_LAST )
  75.             {
  76.                 *(status->enabled) = true;
  77.                 mExecuteHost = false;
  78.                 return;
  79.             }
  80.         }
  81.         break;
  82.         
  83.         default:
  84.         {
  85.             if ( inMessage >= RECENT_EDIT_MENU_BASE && inMessage <= RECENT_EDIT_MENU_BASE_LAST )
  86.             {
  87.                 MWContext *cntxt2 = GetTopWindowContext();
  88.                 if ( cntxt2 )
  89.                 {
  90.                     char *aURLtoOpen = NULL;
  91.                     if ( EDT_GetEditHistory( cntxt2, inMessage - RECENT_EDIT_MENU_BASE - 1, &aURLtoOpen, NULL) )
  92.                     {
  93.                         URL_Struct* theURL = NET_CreateURLStruct( aURLtoOpen, NET_NORMAL_RELOAD );
  94.                         if ( theURL )
  95.                             CEditorWindow::MakeEditWindow( NULL, theURL );
  96.  
  97.                         mExecuteHost = false;
  98.                         return;
  99.                      }
  100.                  }
  101.             }
  102.         }
  103.         break;            
  104.     }
  105.     
  106.     mExecuteHost = true;    // Let application handle it
  107. }
  108.  
  109.  
  110. LMenu *CRecentEditMenuAttachment::GetMenu()
  111. {
  112.     if (!sMenu)
  113.         sMenu = new LMenu( menu_ID );
  114.     
  115.     return sMenu;
  116. }
  117.  
  118.  
  119. // build the font menu from the system
  120. void CRecentEditMenuAttachment::UpdateMenu()
  121. {
  122.     if (!GetMenu() || !LMenuBar::GetCurrentMenuBar())
  123.         return;
  124.     
  125.     int i;
  126.     
  127.     // Ñ delete all the menu items after the separator line
  128.     MenuHandle menu = sMenu->GetMacMenuH();
  129.     if ( menu )
  130.     {
  131.         for ( i = ::CountMItems( menu ); i > 0; i-- )
  132.             sMenu->RemoveItem( i );
  133.     }
  134.     
  135.     Try_
  136.     {
  137.         ThrowIfNil_( menu );
  138.     
  139.         // Add recently edited URLs to menu
  140.         
  141.         int i;
  142.         char *urlp = NULL, *titlep = NULL;
  143.         
  144.         for ( i = 0; i < MAX_EDIT_HISTORY_LOCATIONS 
  145.                     && EDT_GetEditHistory( GetTopWindowContext(), i, &urlp, &titlep ); i++ )
  146.         {
  147.             NET_UnEscape( urlp );
  148.             
  149.             // convert string to pascal-string for menu
  150.             CStr255 menuStr(urlp);
  151.             if ( menuStr.IsEmpty() )
  152.                 menuStr = titlep;
  153.             
  154.             if ( !menuStr.IsEmpty() )
  155.             {
  156.                 // Insert a "blank" item first...
  157.                 ::InsertMenuItem( GetMenu()->GetMacMenuH(), "\p ", i );
  158.     
  159.                 // Then change it. We do this so that no interpretation of metacharacters will occur.
  160.                 ::SetMenuItemText( GetMenu()->GetMacMenuH(), i + 1, menuStr );
  161.                 
  162.                 // SetCommand for menu item
  163.                 sMenu->SetCommand( i, RECENT_EDIT_MENU_BASE + i );
  164.                 sMenu->SetUsed( true );
  165.             }
  166.             else
  167.                 break;
  168.         }
  169.     }
  170.     Catch_( inErr )
  171.     {
  172.     }
  173.     EndCatch_
  174. }
  175.  
  176.  
  177. void CRecentEditMenuAttachment::RemoveMenus()
  178. {
  179.     if (sMenu)
  180.     {
  181.         LMenuBar *currentMenuBar = LMenuBar::GetCurrentMenuBar();
  182.         if (currentMenuBar)
  183.             currentMenuBar->RemoveMenu(sMenu);
  184.     }
  185. }
  186.  
  187.  
  188. void CRecentEditMenuAttachment::InstallMenus()
  189. {
  190.     if (sMenu)
  191.     {
  192.         LMenuBar *currentMenuBar = LMenuBar::GetCurrentMenuBar();
  193.         if (currentMenuBar)
  194.         {
  195.             StValueChanger<EDebugAction> okayToFail(gDebugThrow, debugAction_Nothing);
  196.             currentMenuBar->InstallMenu(sMenu, hierMenu);
  197.             
  198.             ResIDT resID;
  199.             MenuHandle menuh;
  200.             Int16 whichItem;
  201.             currentMenuBar->FindMenuItem( cmd_ID_toSearchFor, resID, menuh, whichItem );
  202.             if ( menuh )
  203.             {
  204.                 // make it hierarchical
  205.                 ::SetItemCmd( menuh, whichItem, hMenuCmd );
  206.                 ::SetItemMark( menuh, whichItem, menu_ID );
  207.             }
  208.         }
  209.     }
  210. }
  211.