home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / Composer / COpenRecentlyEditedPopup.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  5.7 KB  |  184 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. //    COpenRecentlyEditedPopup.cp
  21. // ===========================================================================
  22.  
  23. #include "COpenRecentlyEditedPopup.h"
  24.         
  25. #include "CWindowMediator.h"
  26. #include "CBrowserContext.h"
  27. #include "CEditorWindow.h"
  28. #include "UMenuUtils.h"
  29. #include "PascalString.h"
  30.  
  31. #include "net.h"        // NET_CreateURLStruct
  32. #include "structs.h"    // TagType which is needed in edt.h
  33. #include "edt.h"
  34.  
  35.         
  36. // ---------------------------------------------------------------------------
  37. //        Ñ CreateNavigationButtonPopupStream [static]
  38. // ---------------------------------------------------------------------------
  39.  
  40. COpenRecentlyEditedPopup*
  41. COpenRecentlyEditedPopup::CreateOpenRecentlyEditedPopupStream( LStream* inStream )
  42. {
  43.     return new COpenRecentlyEditedPopup(inStream);
  44. }
  45.         
  46. // ---------------------------------------------------------------------------
  47. //        Ñ COpenRecentlyEditedPopup
  48. // ---------------------------------------------------------------------------
  49.  
  50. COpenRecentlyEditedPopup::COpenRecentlyEditedPopup( LStream* inStream )
  51.     :    mBrowserContext(nil),
  52.         super(inStream)
  53. {
  54. }
  55.  
  56. // ---------------------------------------------------------------------------
  57. //        Ñ ~COpenRecentlyEditedPopup
  58. // ---------------------------------------------------------------------------
  59.  
  60. COpenRecentlyEditedPopup::~COpenRecentlyEditedPopup()
  61. {
  62. }
  63.     
  64. #pragma mark -
  65.  
  66. // ---------------------------------------------------------------------------
  67. //        Ñ AdjustMenuContents
  68. // ---------------------------------------------------------------------------
  69.  
  70. void
  71. COpenRecentlyEditedPopup::AdjustMenuContents()
  72. {
  73.     if (!GetMenu() || !GetMenu()->GetMacMenuH())
  74.         return;
  75.     
  76.     if (!AssertPreconditions())
  77.         return;
  78.         
  79.     // Purge the menu
  80.     UMenuUtils::PurgeMenuItems(GetMenu()->GetMacMenuH(), PERM_OPEN_ITEMS);
  81.     
  82.     // Fill the menu
  83.     int i;
  84.     char *urlp = NULL, *titlep = NULL;
  85.     for ( i = 0; i < MAX_EDIT_HISTORY_LOCATIONS 
  86.                 && EDT_GetEditHistory( ((MWContext *)(*mBrowserContext)), i, &urlp, &titlep ); i++ )
  87.     {
  88.         // strange logic:  if we have no URL (then how do we go there???) then use title if it exists
  89.         if ( urlp == NULL )
  90.             urlp = titlep;
  91.         
  92.         if ( urlp )
  93.         {
  94.             NET_UnEscape( urlp );
  95.             InsertItemIntoMenu( urlp, PERM_OPEN_ITEMS + i );
  96.         }
  97.         else
  98.             break;
  99.     }
  100.     
  101.     // delete menu break line if we don't have any history items
  102.     if ( i == 0 )
  103.     {
  104.         ::DeleteMenuItem( GetMenu()->GetMacMenuH(), PERM_OPEN_ITEMS );
  105.     }
  106.  
  107.     // Set the min/max values of the control since we populated the menu
  108.     SetPopupMinMaxValues();
  109. }
  110.  
  111. // ---------------------------------------------------------------------------
  112. //        Ñ InsertItemIntoMenu
  113. // ---------------------------------------------------------------------------
  114.  
  115. void
  116. COpenRecentlyEditedPopup::InsertItemIntoMenu( char *urlp, Int16 inAfterItem )
  117. {
  118.     Assert_(GetMenu() && GetMenu()->GetMacMenuH());
  119.     Assert_(mBrowserContext);
  120.     CStr255 thePString( urlp );
  121.  
  122.     // Insert a "blank" item first...
  123.     ::InsertMenuItem( GetMenu()->GetMacMenuH(), "\p ", inAfterItem + 1 );
  124.     
  125.     // Then change it. We do this so that no interpretation of metacharacters will occur.
  126.     ::SetMenuItemText( GetMenu()->GetMacMenuH(), inAfterItem + 1, thePString );
  127. }
  128.     
  129. #pragma mark -
  130.  
  131. // ---------------------------------------------------------------------------
  132. //        Ñ HandleNewValue
  133. // ---------------------------------------------------------------------------
  134.  
  135. Boolean
  136. COpenRecentlyEditedPopup::HandleNewValue( Int32 inNewValue )
  137. {
  138.     if ( inNewValue >= 1 && inNewValue < PERM_OPEN_ITEMS )
  139.     {
  140.         // someone else will handle this
  141.         return false;
  142.     }
  143.     
  144.     if ( AssertPreconditions() && inNewValue )
  145.     {
  146.         MWContext *cntxt = ((MWContext *)(*mBrowserContext));
  147.         if ( cntxt )
  148.         {
  149.             char *aURLtoOpen = NULL;
  150.             // EDT_GetEditHistory is 0-based so deduct 1 from 2nd parameter
  151.             if ( EDT_GetEditHistory( cntxt, inNewValue - PERM_OPEN_ITEMS - 1, &aURLtoOpen, NULL) )
  152.             {
  153.                 URL_Struct* theURL = NET_CreateURLStruct( aURLtoOpen, NET_NORMAL_RELOAD );
  154.                 if ( theURL )
  155.                     CEditorWindow::MakeEditWindow( NULL, theURL );
  156.             }
  157.         }
  158.     }
  159.     
  160.     return true;
  161. }
  162.  
  163. // ---------------------------------------------------------------------------
  164. //        Ñ AssertPreconditions
  165. // ---------------------------------------------------------------------------
  166. //    Assert preconditions and fill in interesting member data
  167.  
  168. Boolean
  169. COpenRecentlyEditedPopup::AssertPreconditions()
  170. {    
  171.     CMediatedWindow* topWindow = CWindowMediator::GetWindowMediator()->FetchTopWindow( WindowType_Any, regularLayerType );
  172.     
  173.     if (!topWindow || topWindow->GetWindowType() != WindowType_Editor)
  174.         return false;
  175.     
  176.     CEditorWindow* composerWindow = dynamic_cast<CEditorWindow*>(topWindow);
  177.     if ( !composerWindow )
  178.         return false;
  179.     
  180.     if ( !(mBrowserContext = (CBrowserContext*)composerWindow->GetWindowContext()) )
  181.         return false;
  182.     
  183.     return true;
  184. }