home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CTabSwitcher.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  7.6 KB  |  276 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. #ifdef PowerPlant_PCH
  20. #include PowerPlant_PCH
  21. #endif
  22.  
  23. #include <LArrayIterator.h>
  24. #include <LCommander.h>
  25. #include <LStream.h>
  26. #include <PP_Messages.h>
  27. #include <UMemoryMgr.h>
  28. #include <UReanimator.h>
  29.  
  30. #include "CTabSwitcher.h"
  31. #include "CTabControl.h"
  32.  
  33. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  34. //    Ñ    
  35. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  36.  
  37. CTabSwitcher::CTabSwitcher(LStream* inStream)
  38.     :    LView(inStream),
  39.         mCachedPages(sizeof(LView*))
  40. {
  41.     *inStream >> mTabControlID;
  42.     *inStream >> mContainerID;
  43.     *inStream >> mIsCachingPages;
  44.  
  45.     mCurrentPage = NULL;
  46.     mSavedValue = 0;
  47. }
  48.  
  49. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  50. //    Ñ    
  51. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  52.  
  53. CTabSwitcher::~CTabSwitcher()
  54. {
  55.     FlushCachedPages();
  56. }
  57.  
  58. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  59. //    Ñ    
  60. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  61.  
  62. void CTabSwitcher::FinishCreateSelf(void)
  63. {
  64.     LView::FinishCreateSelf();
  65.     
  66.     CTabControl* theTabControl = (CTabControl*)FindPaneByID(mTabControlID);
  67.     Assert_(theTabControl != NULL);
  68.     theTabControl->AddListener(this);
  69.  
  70.     MessageT theTabMessage = theTabControl->GetValueMessage();
  71.     if (theTabMessage != msg_Nothing)
  72.         {
  73.         SwitchToPage(theTabControl->GetValueMessage());
  74.         mSavedValue = theTabControl->GetValue();
  75.         }
  76. }
  77.  
  78. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  79. //    Ñ    
  80. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  81.  
  82. void CTabSwitcher::ListenToMessage(
  83.     MessageT        inMessage,
  84.     void*            ioParam)
  85. {
  86.     if (inMessage == msg_TabSwitched)
  87.         {
  88.         ResIDT thePageResID = (ResIDT)(*(MessageT*)ioParam);
  89.         SwitchToPage(thePageResID);
  90.         }
  91.     else if (inMessage == msg_GrowZone)
  92.         {
  93.         FlushCachedPages();
  94.         // 1998.01.12 pchen -- replicate fix for bug #85275
  95.         // We don't know how much memory was freed, so just set bytes freed to 0
  96.         *((Int32 *)ioParam) = 0;
  97.         }
  98. }
  99.  
  100. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  101. //    Ñ    
  102. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  103.  
  104. void CTabSwitcher::SwitchToPage(ResIDT inPageResID)
  105. {
  106.     CTabControl* theTabControl = (CTabControl*)FindPaneByID(mTabControlID);
  107.     Assert_(theTabControl != NULL);
  108.  
  109.     LView* thePageContainer = (LView*)FindPaneByID(mContainerID);
  110.     Assert_(thePageContainer != NULL);
  111.     
  112.     LView* thePage = NULL;
  113.     Boolean    isFromCache = true;
  114.         
  115.     if (mIsCachingPages)
  116.         thePage = FetchPageFromCache(inPageResID);
  117.  
  118.     if (thePage == NULL)            // we need to reanimate
  119.         {
  120.         try
  121.             {
  122. #if 0
  123. /*
  124.             // We temporarily disable signaling since we're reanimating into a NULL
  125.             // view container.  FocusDraw() signals if it cant establish a port.
  126.             StValueChanger<EDebugAction> okayToSignal(gDebugSignal, debugAction_Nothing);
  127.             thePage = UReanimator::CreateView(inPageResID, NULL, this);
  128. */
  129. #endif
  130.             thePage = UReanimator::CreateView(inPageResID, this, this);
  131.             isFromCache = false;
  132.             }
  133.         catch (...)
  134.             {
  135.             // something went wrong
  136.             delete thePage;
  137.             thePage = NULL;
  138.             }
  139.         }
  140.     else
  141.         RemovePageFromCache(thePage);
  142.     
  143.     // Sanity check in case we mad a page without setting this up.
  144.     Assert_(thePage->GetPaneID() == inPageResID);
  145.         
  146.     if ((thePage != NULL) && SwitchTarget(this))
  147.         {
  148.         if (mCurrentPage != NULL)
  149.             {
  150.             mCurrentPage->Hide();
  151.             DoPreDispose(mCurrentPage, mIsCachingPages);
  152.             if (mIsCachingPages)
  153.                 {
  154.                 mCurrentPage->PutInside(NULL, false);
  155.                 mCachedPages.InsertItemsAt(1, LArray::index_Last, &mCurrentPage);
  156.                 }
  157.             else
  158.                 delete mCurrentPage;
  159.             }
  160.         
  161.         thePage->PutInside(thePageContainer);
  162.         thePageContainer->ExpandSubPane(thePage, true, true);
  163.  
  164.         DoPostLoad(thePage, isFromCache);
  165.  
  166.         thePage->Show();
  167.         thePage->Refresh();
  168.         mCurrentPage = thePage;
  169.         mSavedValue = theTabControl->GetValue();
  170.  
  171. //        RestoreTarget();            // walk the latent subs
  172. //        Don't restore the target otherwise you can't
  173. //        switch to another target in DoPostLoad()
  174.  
  175.         }
  176.     else                             // we falied to reanimate
  177.         {
  178.         theTabControl->SetValue(mSavedValue);
  179.         }
  180. }
  181.  
  182. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  183. //    Ñ    
  184. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  185.  
  186. LView* CTabSwitcher::FindPageByID(ResIDT inPageResID)
  187. {
  188.     LView* theFoundPage = NULL;
  189.     
  190.     if ((mCurrentPage != NULL) && (mCurrentPage->GetPaneID() == inPageResID))
  191.         theFoundPage = mCurrentPage;
  192.     else    
  193.         theFoundPage = FetchPageFromCache(inPageResID);
  194.     
  195.     return theFoundPage;
  196. }
  197.  
  198. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  199. //    Ñ    
  200. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  201.  
  202. void CTabSwitcher::DoPostLoad(
  203.     LView*             /* inLoadedPage */,
  204.     Boolean            /* inFromCache  */)
  205. {
  206. }
  207.  
  208. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  209. //    Ñ    
  210. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  211.  
  212. void CTabSwitcher::DoPreDispose(
  213.     LView*             /* inLeavingPage */,
  214.     Boolean            /* inWillCache   */)
  215. {
  216. }
  217.  
  218. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  219. //    Ñ    
  220. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  221.  
  222. LView* CTabSwitcher::FetchPageFromCache(ResIDT inPageResID)
  223. {
  224.     LView* theCachedPage = NULL;
  225.     LView* theIndexPage = NULL;
  226.     
  227.     LArrayIterator theIter(mCachedPages, LArrayIterator::from_Start);
  228.     while (theIter.Next(&theIndexPage))
  229.         {
  230.         if (theIndexPage->GetPaneID() == inPageResID)
  231.             {
  232.             theCachedPage = theIndexPage;
  233.             break;
  234.             }
  235.         }
  236.  
  237.     return theCachedPage;
  238. }
  239.  
  240. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  241. //    Ñ    
  242. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  243.  
  244. void CTabSwitcher::RemovePageFromCache(LView* inPage)
  245. {
  246.     Assert_(inPage != NULL);
  247.     
  248.     ArrayIndexT thePageIndex = mCachedPages.FetchIndexOf(&inPage);
  249.     Assert_(thePageIndex != LArray::index_Bad);
  250.  
  251.     mCachedPages.RemoveItemsAt(1, thePageIndex);
  252. }
  253.  
  254. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  255. //    Ñ    
  256. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  257.  
  258. void CTabSwitcher::FlushCachedPages(void)
  259. {
  260.     LView* thePage;
  261.     LArrayIterator theIter(mCachedPages, LArrayIterator::from_Start);
  262.     while (theIter.Next(&thePage))
  263.     {
  264.         // We need to put the page back into the view hierarchy otherwise
  265.         // FocusDraw() signals a nil GrafPort and LScroller may crash.
  266.         LView* thePageContainer = (LView*)FindPaneByID(mContainerID);
  267.         Assert_(thePageContainer != NULL);
  268.         thePage->PutInside(thePageContainer);
  269.         // Ok, we can delete the page now.
  270.         delete thePage;
  271.     }
  272.  
  273.     mCachedPages.RemoveItemsAt(LArray::index_First, mCachedPages.GetCount());
  274. }
  275.  
  276.