home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / Sources / FWClustr.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  6.6 KB  |  232 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWClustr.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWAROPER_H
  13. #include "FWArOper.h"
  14. #endif
  15.  
  16. #ifndef FWCLUSTR_H
  17. #include "FWClustr.h"
  18. #endif
  19.  
  20. #ifndef FWBUTTON_H
  21. #include "FWButton.h"
  22. #endif
  23.  
  24. #ifndef FWFRAME_H
  25. #include "FWFrame.h"
  26. #endif
  27.  
  28. #ifndef FWCONTXT_H
  29. #include "FWContxt.h"
  30. #endif
  31.  
  32. #ifndef FWNOTDEF_H
  33. #include "FWNotDef.h"
  34. #endif
  35.  
  36. //========================================================================================
  37. // File scope definitions
  38. //========================================================================================
  39.  
  40. #ifdef FW_BUILD_MAC
  41. #pragma segment fwgadgts
  42. #endif
  43.  
  44. //========================================================================================
  45. //    CLASS FW_CRadioCluster
  46. //========================================================================================
  47.  
  48. FW_DEFINE_AUTO(FW_CRadioCluster)
  49. FW_DEFINE_CLASS_M0(FW_CRadioCluster)
  50.  
  51. // This class is archivable, but we provide the archiving implementation in a separate
  52. // translation unit in order to enable deadstripping of the archiving-related code
  53. // in parts that do not use archiving with this class.
  54.  
  55. //----------------------------------------------------------------------------------------
  56. // FW_CRadioCluster::FW_CRadioCluster
  57. //----------------------------------------------------------------------------------------
  58.  
  59. FW_CRadioCluster::FW_CRadioCluster(Environment* ev) :
  60.     FW_MReceiver(),
  61.     fButtonOn(0)
  62. {
  63. FW_UNUSED(ev);
  64.     FW_END_CONSTRUCTOR
  65. }
  66.  
  67. //----------------------------------------------------------------------------------------
  68. // FW_CRadioCluster::~FW_CRadioCluster
  69. //----------------------------------------------------------------------------------------
  70.  
  71. FW_CRadioCluster::~FW_CRadioCluster()
  72. {
  73.     FW_START_DESTRUCTOR
  74. }
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // FW_CRadioCluster::AddRadio
  78. //----------------------------------------------------------------------------------------
  79.  
  80. void FW_CRadioCluster::AddRadio(Environment* ev, FW_CButton* radio)
  81. {
  82.     FW_ASSERT(radio != NULL && radio->GetButtonKind(ev) == FW_kRadioButton);
  83.  
  84.     // This works only if the radio button uses the defaul button message
  85.     // Otherwise the RadioCluster will not catch the notification
  86.     AddInterest(FW_CInterest(radio, FW_kRadioClusterMsg));
  87.  
  88.     // Use the first button added to the cluster as the default one
  89.     if (!fButtonOn)
  90.     {
  91.         fButtonOn = radio;
  92.         radio->SetValue(ev, 1);
  93.     }
  94.     else if (radio->GetValue(ev) != 0)
  95.     {
  96.         fButtonOn->SetValue(ev, 0);
  97.         fButtonOn = radio;
  98.     }
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // FW_CRadioCluster::RemoveRadio
  103. //----------------------------------------------------------------------------------------
  104.  
  105. void FW_CRadioCluster::RemoveRadio(Environment* ev, FW_CButton* radio)
  106. {
  107. FW_UNUSED(ev);
  108.     RemoveInterest(FW_CInterest(radio, FW_kRadioClusterMsg));
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // FW_CRadioCluster::HandleNotification
  113. //----------------------------------------------------------------------------------------
  114.  
  115. void FW_CRadioCluster::HandleNotification(Environment* ev, const FW_CNotification& notification)
  116. {
  117.     switch (notification.GetMessage())
  118.     {
  119.         case FW_kRadioClusterMsg:
  120.         {
  121.             const FW_CControlNotification& controlNotification = 
  122.                                                     (FW_CControlNotification&) notification;
  123.             FW_CButton* radioClicked = (FW_CButton*)controlNotification.GetControl(ev);
  124.             
  125.             // Toggle buttons
  126.             
  127.             FW_CReceiverInterestIterator ite(this);
  128.             for (FW_CInterest* i = ite.First(); ite.IsNotComplete(); i = ite.Next())
  129.             {
  130.                 FW_CButton* radio = FW_DYNAMIC_CAST(FW_CButton, i->GetNotifier());
  131.                 FW_ASSERT(radio);
  132.         
  133.                 if (radio != radioClicked && radio->GetValue(ev) == 1)
  134.                     radio->SetValue(ev, 0);
  135.             }            
  136.             fButtonOn = radioClicked;
  137.         }
  138.         break;
  139.         
  140.         case FW_kNotifierDeletedMsg:
  141.         {
  142.             // Delete radio cluster when the last radio button is being deleted.
  143.             // (we make sure that there is only 1 notifier left in the interest list)
  144.             FW_CReceiverInterestIterator ite(this);
  145.             FW_CInterest* i = ite.First();
  146.             FW_ASSERT(i);
  147.             FW_MNotifier* notifier = i->GetNotifier();
  148.             FW_Boolean deleteIt = TRUE;
  149.             for (; ite.IsNotComplete(); i = ite.Next())
  150.             {
  151.                 if (notifier != i->GetNotifier()) {
  152.                     deleteIt = FALSE;
  153.                     break;
  154.                 }
  155.             }
  156.             if (deleteIt) delete this;
  157.         }
  158.         break;    
  159.         
  160.         default:
  161.             FW_ASSERT("FW_CRadioCluster can't handle this message");
  162.             break;
  163.     }
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. //    FW_CRadioCluster::Flatten
  168. //----------------------------------------------------------------------------------------
  169.  
  170. void FW_CRadioCluster::Flatten(Environment* ev, FW_CWritableStream& stream) const
  171. {
  172.     // If all radio buttons have been removed at runtime the radio-cluster was deleted too
  173.     
  174.     FW_CSuperView* superView = fButtonOn->GetSuperView(ev);
  175.     FW_WRITE_DYNAMIC_OBJECT(stream, superView, FW_CSuperView);
  176.  
  177.     // [LSD] todo: add code to retrieve the list of radio buttons connected    
  178.     short numRadioButtons = 0;  
  179.     long dummy = 0;
  180.     
  181.     stream << dummy, numRadioButtons;
  182. /*
  183.     FW_TOrderedCollectionIterator<FW_CInterest> ite(&fInterestList);
  184.     short numRadioButtons = ite.Count();  
  185.     long dummy = 0;
  186.     
  187.     stream << dummy, numRadioButtons;
  188.  
  189.     FW_CNotifier* nofifier;
  190.     FW_CButton* button;
  191.     for (anInterest = ite.First(); ite.IsNotComplete(); anInterest = ite.Next())
  192.     {
  193.         notifier = anInterest->GetNotifier();
  194.         button = FW_DYNAMIC_CAST(FW_CButton, notifier);
  195.         FW_ASSERT(button != 0);
  196.         
  197.         stream << button->GetViewId(ev);    
  198. */
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. //    FW_CRadioCluster::InitializeFromStream
  203. //----------------------------------------------------------------------------------------
  204.  
  205. void FW_CRadioCluster::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  206. {
  207.     // Read the parent view of the radio buttons 
  208.     FW_CSuperView* parentView;    
  209.     FW_READ_DYNAMIC_OBJECT(stream, &parentView, FW_CSuperView);
  210.     
  211.     long dummy;
  212.     short numRadioButtons;
  213.     stream >> dummy;        // [LSD] dummy field to work around odfrc bug
  214.     stream >> numRadioButtons;
  215.     
  216.     FW_ASSERT(numRadioButtons > 0);
  217.     
  218.     for (short i = 1; i <= numRadioButtons; i++)
  219.     {
  220.         ODID    radioId;
  221.         stream >> radioId;
  222.         
  223.         FW_CView* view = parentView->FindViewById(ev, radioId);
  224.         FW_CButton* button = FW_DYNAMIC_CAST(FW_CButton, view);
  225.         FW_ASSERT(button != NULL);
  226.         
  227.         AddRadio(ev, button);
  228.     }    
  229. }
  230.  
  231.  
  232.