home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / CButtonEnabler.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  4.4 KB  |  156 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. // Written by John R. McMullen
  20.  
  21. #include "CButtonEnabler.h"
  22. #include "LSharable.h"
  23.  
  24. #include "MButtonEnablerPolicy.h"
  25.  
  26. //======================================
  27. #pragma mark --------- class CButtonMaster
  28. //======================================
  29.  
  30. //======================================
  31. class CButtonMaster
  32. //======================================
  33.     :    public LAttachable // CButtonEnablers all attach to this
  34.     ,    public LSharable // shared by all CButtonEnablers.
  35. {
  36. public:
  37.     CButtonMaster();
  38.     virtual ~CButtonMaster();
  39.     static CButtonMaster* sButtonMaster;
  40. }; // class CButtonMaster
  41.  
  42. CButtonMaster* CButtonMaster::sButtonMaster = nil;
  43.  
  44. //-----------------------------------
  45. CButtonMaster::CButtonMaster()
  46. //-----------------------------------
  47. {
  48.     sButtonMaster = this;
  49. } // CButtonMaster::CButtonMaster()
  50.  
  51. //-----------------------------------
  52. CButtonMaster::~CButtonMaster()
  53. //-----------------------------------
  54. {
  55.     sButtonMaster = nil;
  56. } // CButtonMaster::~CButtonMaster()
  57.  
  58. //======================================
  59. #pragma mark --------- class CButtonEnabler
  60. //======================================
  61.  
  62. //-----------------------------------
  63. CButtonEnabler* CButtonEnabler::CreateButtonEnablerStream(LStream* inStream)
  64. //-----------------------------------
  65. {
  66.     return new CButtonEnabler(inStream);
  67. }
  68.  
  69. //-----------------------------------
  70. CButtonEnabler::CButtonEnabler(LStream* inStream)
  71. //-----------------------------------
  72. :    LAttachment(inStream)
  73. ,    mControl(nil)
  74. {
  75.     SetMessage(msg_Event); // execute me only for event messages.
  76.     LAttachable    *host = LAttachable::GetDefaultAttachable();
  77.     mControl = dynamic_cast<LControl*>(host);
  78.     Assert_(mControl);
  79.     Try_
  80.     {
  81.         if (!CButtonMaster::sButtonMaster)
  82.             new CButtonMaster;
  83.         CButtonMaster::sButtonMaster->AddUser(this);
  84.         CButtonMaster::sButtonMaster->AddAttachment(this, nil, false);
  85.             // don't care where, master is not owner.
  86.     }
  87.     Catch_ (inErr)
  88.     {
  89.     }
  90.     EndCatch_
  91.     LAttachable::SetDefaultAttachable(host);
  92.         // Restore, because LAttachable's constructor (called when we made
  93.         // the new button master) clobbers the default attachable
  94. } // CButtonEnabler::CButtonEnabler
  95.  
  96. //-----------------------------------
  97. CButtonEnabler::~CButtonEnabler()
  98. //-----------------------------------
  99. {
  100.     if (CButtonMaster::sButtonMaster)
  101.     {
  102.         CButtonMaster::sButtonMaster->RemoveAttachment(this);
  103.         CButtonMaster::sButtonMaster->RemoveUser(this);
  104.     }
  105. } // CButtonEnabler::~CButtonEnabler
  106.  
  107. //-----------------------------------
  108. void CButtonEnabler::UpdateButtons()
  109. //-----------------------------------
  110. {
  111.     if (CButtonMaster::sButtonMaster)
  112.         CButtonMaster::sButtonMaster->ExecuteAttachments(msg_Event, nil);    
  113. } // CButtonEnabler::UpdateButtons
  114.  
  115. //-----------------------------------
  116. void CButtonEnabler::ExecuteSelf(MessageT inMessage, void*)
  117. //-----------------------------------
  118. {
  119.     MButtonEnablerPolicy*    thePolicy = dynamic_cast<MButtonEnablerPolicy*>(mControl);
  120.  
  121.     if (thePolicy)
  122.     {
  123.         // Delegate the policy
  124.         
  125.         thePolicy->HandleButtonEnabling();
  126.     }
  127.     else
  128.     {
  129.         // Default enabling policy for LControl
  130.         
  131.         LCommander* theTarget        = LCommander::GetTarget();
  132.         MessageT    command            = mControl->GetValueMessage();
  133.         Boolean     enabled            = false;
  134.         Boolean        usesMark        = false;
  135.         Str255        outName;
  136.         Char16        outMark;
  137.  
  138.         if (!mControl->IsActive() || !mControl->IsVisible())
  139.             return;
  140.             
  141.         if (!theTarget)
  142.             return;
  143.         
  144.         theTarget->ProcessCommandStatus(command, enabled, usesMark, outMark, outName);
  145.         
  146.         if (enabled)
  147.         {
  148.             mControl->Enable();
  149.         }
  150.         else
  151.         {
  152.             mControl->Disable();
  153.         }
  154.     }
  155. } // CButtonEnabler::ExecuteSelf
  156.