home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CPaneEnabler.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  6.5 KB  |  222 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. // CPaneEnabler is a more general form of CButtonEnabler, originally written by John R. McMullen.
  20. // The generalization was done by Mark Young.
  21.  
  22. // CSlaveEnabler was written by John R. McMullen
  23.  
  24. #include "CPaneEnabler.h"
  25. #include "LSharable.h"
  26.  
  27. #include "MPaneEnablerPolicy.h"
  28. #include "CTargetedUpdateMenuRegistry.h"
  29.  
  30. #include <LControl.h>
  31.  
  32. //======================================
  33. #pragma mark --------- class CPaneMaster
  34. //======================================
  35.  
  36. //======================================
  37. class CPaneMaster
  38. //======================================
  39.     :    public LAttachable // CPaneEnablers all attach to this
  40.     ,    public LSharable // shared by all CPaneEnablers.
  41. {
  42. public:
  43.     CPaneMaster();
  44.     virtual ~CPaneMaster();
  45.     static CPaneMaster* sPaneMaster;
  46. }; // class CPaneMaster
  47.  
  48. CPaneMaster* CPaneMaster::sPaneMaster = nil;
  49.  
  50. //-----------------------------------
  51. CPaneMaster::CPaneMaster()
  52. //-----------------------------------
  53. {
  54.     sPaneMaster = this;
  55. } // CPaneMaster::CPaneMaster()
  56.  
  57. //-----------------------------------
  58. CPaneMaster::~CPaneMaster()
  59. //-----------------------------------
  60. {
  61.     sPaneMaster = nil;
  62. } // CPaneMaster::~CPaneMaster()
  63.  
  64. //======================================
  65. #pragma mark --------- class CPaneEnabler
  66. //======================================
  67.  
  68. //-----------------------------------
  69. CPaneEnabler::CPaneEnabler(LStream* inStream)
  70. //-----------------------------------
  71. :    LAttachment(inStream)
  72. ,    mPane(nil)
  73. {
  74.     SetMessage(msg_Event); // execute me only for event messages.
  75.     LAttachable    *host = LAttachable::GetDefaultAttachable();
  76.     mPane = dynamic_cast<LPane*>(host);
  77.     Assert_(mPane);
  78.     try
  79.     {
  80.         if (!CPaneMaster::sPaneMaster)
  81.             new CPaneMaster;
  82.         CPaneMaster::sPaneMaster->AddUser(this);
  83.         CPaneMaster::sPaneMaster->AddAttachment(this, nil, false);
  84.             // don't care where, master is not owner.
  85.     }
  86.     catch (...)
  87.     {
  88.     }
  89.     LAttachable::SetDefaultAttachable(host);
  90.         // Restore, because LAttachable's constructor (called when we made
  91.         // the new pane master) clobbers the default attachable
  92. } // CPaneEnabler::CPaneEnabler
  93.  
  94. //-----------------------------------
  95. CPaneEnabler::~CPaneEnabler()
  96. //-----------------------------------
  97. {
  98.     if (CPaneMaster::sPaneMaster)
  99.     {
  100.         CPaneMaster::sPaneMaster->RemoveAttachment(this);
  101.         CPaneMaster::sPaneMaster->RemoveUser(this);
  102.     }
  103. } // CPaneEnabler::~CPaneEnabler
  104.  
  105. //-----------------------------------
  106. void CPaneEnabler::UpdatePanes()
  107. //-----------------------------------
  108. {
  109.     if (CPaneMaster::sPaneMaster)
  110.         CPaneMaster::sPaneMaster->ExecuteAttachments(msg_Event, nil);    
  111. } // CPaneEnabler::UpdatePanes
  112.  
  113. //-----------------------------------
  114. void CPaneEnabler::ExecuteSelf(MessageT inMessage, void*)
  115. //-----------------------------------
  116. {
  117.     MPaneEnablerPolicy*    thePolicy    = dynamic_cast<MPaneEnablerPolicy*>(mPane);
  118.     LPane*                thePane        = dynamic_cast<LPane*>(mPane);
  119.     LControl*            theControl    = dynamic_cast<LControl*>(mPane);
  120.  
  121.     if (thePolicy)
  122.     {
  123.         // Delegate the policy
  124.         
  125.         thePolicy->HandleEnablingPolicy();
  126.     }
  127.     else if (theControl)
  128.     {
  129.         // Default enabling policy for LControl
  130.         
  131.         LCommander* theTarget        = LCommander::GetTarget();
  132.         MessageT    command            = theControl->GetValueMessage();
  133.         Boolean     enabled            = false;
  134.         Boolean        usesMark        = false;
  135.         Str255        outName;
  136.         Char16        outMark;
  137.         
  138.         if (!CTargetedUpdateMenuRegistry::UseRegistryToUpdateMenus() ||
  139.                 CTargetedUpdateMenuRegistry::CommandInRegistry(command))
  140.         {
  141.             if (!mPane->IsActive() || !mPane->IsVisible())
  142.                 return;
  143.                 
  144.             if (!theTarget)
  145.                 return;
  146.             
  147.             theTarget->ProcessCommandStatus(command, enabled, usesMark, outMark, outName);            
  148.             if (enabled)
  149.                 mPane->Enable();
  150.             else
  151.                 mPane->Disable();
  152.         }
  153.     }
  154.     else if (thePane)
  155.     {
  156.         // Default enabling policy for LPane        
  157.         mPane->Enable();
  158.     }
  159.     else
  160.         throw;
  161. } // CPaneEnabler::ExecuteSelf
  162.  
  163. //======================================
  164. #pragma mark --------- class CSlaveEnabler
  165. //======================================
  166.  
  167. //-----------------------------------
  168. CSlaveEnabler::CSlaveEnabler(LStream* inStream)
  169. //-----------------------------------
  170. :    LAttachment(inStream)
  171. ,    mControllingControl(nil)
  172. ,    mControllingID(0)
  173. ,    mPane(nil)
  174. {
  175.     *inStream >> mControllingID;
  176.     LAttachable    *host = LAttachable::GetDefaultAttachable();
  177.     mPane = dynamic_cast<LPane*>(host);
  178.     Assert_(mPane);    
  179.     SetMessage(msg_DrawOrPrint);
  180. } // CSlaveEnabler::CSlaveEnabler
  181.  
  182. //-----------------------------------
  183. void CSlaveEnabler::Update(SInt32 inValue)
  184. //-----------------------------------
  185. {
  186.     if (inValue != 0)
  187.         mPane->Enable();
  188.     else
  189.         mPane->Disable();
  190. } // CSlaveEnabler::Update
  191.  
  192. //-----------------------------------
  193. void CSlaveEnabler::ExecuteSelf(MessageT inMessage, void*)
  194. // When our pane is to be drawn for the first time, find our controlling control
  195. // and set the enabling accordingly.  Then turn off attachment messages.
  196. //-----------------------------------
  197. {
  198.     if (inMessage == msg_DrawOrPrint && !mControllingControl)
  199.     {                
  200.         mControllingControl = dynamic_cast<LControl*>(
  201.             mPane->GetSuperView()->FindPaneByID(mControllingID));
  202.         Assert_(mControllingControl);
  203.         if (mControllingControl)
  204.         {
  205.             mControllingControl->AddListener(this);
  206.             if (mControllingControl->GetValueMessage() == 0)
  207.                 mControllingControl->SetValueMessage(msg_Click);
  208.             Update(mControllingControl->GetValue());
  209.         }
  210.         SetMessage(msg_Nothing); // don't call me again
  211.     }
  212. } // CSlaveEnabler::ExecuteSelf
  213.  
  214. //-----------------------------------
  215. void CSlaveEnabler::ListenToMessage(MessageT inMessage, void* ioParam)
  216. // Listening to our controlling control for when it does a value broadcast
  217. //-----------------------------------
  218. {
  219.     if (mControllingControl && inMessage == mControllingControl->GetValueMessage())
  220.         Update(*(Int32*)ioParam);
  221. } // CSlaveEnabler::ListenToMessage
  222.