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 / FWButton.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  9.2 KB  |  323 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWButton.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 FWBUTTON_H
  13. #include "FWButton.h"
  14. #endif
  15.  
  16. // ----- Framewrk Includes -----
  17.  
  18. #ifndef FWFRAME_H
  19. #include "FWFrame.h"
  20. #endif
  21.  
  22. //#ifndef FWDIALOG_H    [HLX Home]
  23. //#include "FWDialog.h"
  24. //#endif
  25.  
  26. #ifndef FWPART_H
  27. #include "FWPart.h"
  28. #endif
  29.  
  30. #ifndef FWCONTXT_H
  31. #include "FWContxt.h"
  32. #endif
  33.  
  34. // ----- Foundation Includes -----
  35.  
  36. #ifndef FWSTREAM_H
  37. #include "FWStream.h"
  38. #endif
  39.  
  40. #ifndef FWRRECT_H
  41. #include "FWRRcShp.h"
  42. #endif
  43.  
  44. #ifndef FWNOTDEF_H
  45. #include "FWNotDef.h"
  46. #endif
  47.  
  48. // ----- OpenDoc Includes -----
  49.  
  50. #ifndef SOM_ODSession_xh
  51. #include <ODSessn.xh>
  52. #endif
  53.  
  54. #ifndef SOM_ODFacet_xh
  55. #include <Facet.xh>
  56. #endif
  57.  
  58. // ----- Macintosh Includes -----
  59.  
  60. #if defined(FW_BUILD_MAC) && !defined(__CONTROLS__)
  61. #include <Controls.h>
  62. #endif
  63.  
  64. //========================================================================================
  65. // File scope definitions
  66. //========================================================================================
  67.  
  68. #ifdef FW_BUILD_MAC
  69. #pragma segment fwgadgts
  70. #endif
  71.  
  72. //========================================================================================
  73. // CLASS FW_CButton
  74. //========================================================================================
  75.  
  76. FW_DEFINE_CLASS_M1(FW_CButton, FW_CNativeControl)
  77. FW_DEFINE_AUTO(FW_CButton)
  78.  
  79. // This class is archivable, but we provide the archiving implementation in a separate
  80. // translation unit in order to enable deadstripping of the archiving-related code
  81. // in parts that do not use archiving with this class.
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // FW_CButton::FW_CButton
  85. //----------------------------------------------------------------------------------------
  86.  
  87. FW_CButton::FW_CButton(Environment* ev, 
  88.                            FW_CSuperView* container, ODID id,
  89.                         const FW_CRect& bounds,
  90.                         FW_ButtonKind kind,  
  91.                         const FW_CString& label,
  92.                         const FW_CFont& font,
  93.                          FW_Message msg,
  94.                          FW_ControlValue value) :
  95.     FW_CNativeControl(ev, container, id, bounds, msg, value),
  96.     fKind(0)
  97. {
  98.     Initialize(ev, kind, label, font);
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. //    FW_CButton::FW_CButton
  103. //----------------------------------------------------------------------------------------
  104.  
  105. FW_CButton::FW_CButton(Environment* ev) :
  106.     FW_CNativeControl(ev),
  107.     fKind(0)
  108. {    
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // FW_CButton::~FW_CButton
  113. //----------------------------------------------------------------------------------------
  114.  
  115. FW_CButton::~FW_CButton()
  116. {
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // FW_CButton::MacGetProcId
  121. //----------------------------------------------------------------------------------------
  122. #ifdef FW_BUILD_MAC
  123.  
  124. short    FW_CButton::MacGetProcId() 
  125. {
  126.     short procId = pushButProc;    // use this as default
  127.     
  128.     if (fKind == FW_kRadioButton)        procId = radioButProc;
  129.     else if (fKind == FW_kCheckButton)    procId = checkBoxProc;
  130.     
  131.     return procId;
  132. }
  133. #endif
  134.  
  135. //----------------------------------------------------------------------------------------
  136. // FW_CButton::SetButtonKind
  137. //----------------------------------------------------------------------------------------
  138.  
  139. void    FW_CButton::SetButtonKind(Environment* ev, FW_ButtonKind kind)
  140. {
  141. FW_UNUSED(ev);
  142.     // For now this method can only be used to change a push button to a default button
  143.     FW_ASSERT(fKind == FW_kDefaultPushButton || kind == FW_kDefaultPushButton);
  144.  
  145.     // Use a margin to add/remove a default button's outline
  146.     fControlHelper->SetControlMargin(kind == FW_kDefaultPushButton ? 4 : 0);        
  147.     
  148.     fKind = kind;
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // FW_CButton::Draw
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void FW_CButton::Draw(Environment* ev, ODFacet* facet, ODShape* invalidShape)
  156. {
  157. #ifdef FW_BUILD_MAC
  158.     FW_CNativeControl::Draw(ev, facet, invalidShape);
  159.  
  160.     if (fKind == FW_kDefaultPushButton)
  161.     {
  162.         FW_CViewContext gc(ev, this, facet, invalidShape);
  163.         // Draw the standard default button border
  164.         
  165.         FW_Fixed buttonOval = FW_IntToFixed(2) + FW_Half(GetSize(ev).y);
  166.         FW_CRoundRectShape::RenderRoundRect(
  167.             gc,
  168.             FW_CRect(FW_kZeroPoint, GetSize(ev)),
  169.             FW_CPoint(buttonOval, buttonOval),
  170.             FW_kFrame,
  171.             FW_kNormalInk,
  172.             FW_CStyle(FW_IntToFixed(3)));
  173.     }
  174. #endif
  175.  
  176. #ifdef FW_BUILD_WIN
  177.     // Windows handles updating controls automatically
  178. #endif
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // FW_CButton::SimulateButtonPressed
  183. //----------------------------------------------------------------------------------------
  184.  
  185. void FW_CButton::SimulateButtonPressed(Environment* ev)
  186. {
  187.     if (fKind == FW_kPushButton || fKind == FW_kDefaultPushButton) {
  188. #ifdef FW_BUILD_MAC
  189.         FW_CViewContext gc(ev, this, GetFrame(ev)->GetActiveFacet(ev), NULL);
  190.     
  191.         FW_CPlatformPoint qdPoint    = gc.LogicalToDevice(FW_kZeroPoint);
  192.         FW_CPlatformPoint qdSize    = GetSize(ev).AsPlatformPoint();
  193.         fControlHelper->SimulateButtonPressed(qdPoint, qdSize);
  194. #endif
  195.     }
  196.     
  197.     // Send notification
  198.     Notify(ev, FW_CControlNotification(this));
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // FW_CButton::PrivSetValue
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_Boolean FW_CButton::PrivSetValue(Environment *ev, FW_ControlValue value, ODFacet* facet)
  206. {
  207.     // Update the native control.  Return false if value was not changed 
  208.     if (FW_CNativeControl::PrivSetValue(ev, value, facet))
  209.     {
  210.         // If it's a radio button being turned on we must notify its radio-cluster receiver
  211.         // This should be done before any custom message the radio button might be sending
  212.         // to other receivers
  213.         if (fKind == FW_kRadioButton && value == 1)
  214.             Notify(ev, FW_CControlNotification(this, FW_kRadioClusterMsg));
  215.             
  216.         // Send a control message to its receivers
  217.         if (GetMessage(ev) != FW_kNullMsg && GetMessage(ev) != FW_kRadioClusterMsg)
  218.             Notify(ev, FW_CControlNotification(this));
  219.  
  220.         return true;
  221.     }
  222.     else
  223.         return false;
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // FW_CButton::ControlClicked
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void FW_CButton::ControlClicked(Environment *ev, FW_ControlValue oldValue, ODFacet* facet)
  231. {
  232.     // Change radio and checkbox value (this will send a notification too)
  233.     // Send the notification directly for push buttons 
  234.     
  235.     if (GetButtonKind(ev) == FW_kRadioButton)
  236.         PrivSetValue(ev, 1, facet);
  237.     else if (GetButtonKind(ev) == FW_kCheckButton)
  238.         PrivSetValue(ev, 1 - oldValue, facet);
  239.     else
  240.         Notify(ev, FW_CControlNotification(this));
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. // FW_CButton::Initialize
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void FW_CButton::Initialize(Environment* ev, 
  248.                         FW_ButtonKind kind, 
  249.                         const FW_CString& label,
  250.                         const FW_CFont& font)
  251. {
  252.     short max = 1;
  253.     fKind = kind;    
  254.     
  255.     if (fKind == FW_kPushButton || fKind == FW_kDefaultPushButton)
  256.     {
  257.         fValue = 0;                        // Push Button's value cannot change
  258.         max = 0;
  259.     }
  260.     else
  261.     {
  262.         if (fValue != 0)  fValue = 1;    // Radio and Check buttons have 2 values
  263.     }
  264.  
  265. #ifdef FW_BUILD_MAC
  266.     Str255 pascalText;
  267.     label.ExportPascal(pascalText);
  268.     fControlHelper = new FW_CPrivMacControlHelper
  269.                         (ev, this, fValue, 0, max, MacGetProcId(), pascalText, font);
  270. #endif
  271.  
  272. #ifdef FW_BUILD_WIN
  273.     // TO UPDATE
  274.     fControlHelper  = new FW_CPrivWinControlHelper
  275.                             (ev, FW_TYPEID_FROM_TYPE(FW_CPrivWinControlHelper), this);
  276.     fControlHelper->SetText(label);
  277. #endif
  278.     
  279.     // Control message define Default/Cancel buttons inside a FW_CDialogFrame
  280.     FW_CFrame* frame = GetFrame(ev);
  281.     frame->PrivButtonAttached(ev, this);
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. //    FW_CButton::InitializeFromStream
  286. //----------------------------------------------------------------------------------------
  287.  
  288. void FW_CButton::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  289. {
  290.     FW_CNativeControl::InitializeFromStream(ev, stream);
  291.     
  292.     FW_ButtonKind kind;
  293.     FW_CString label;
  294.     FW_CFont font;
  295.  
  296.     stream >> kind;
  297.     stream >> font;
  298.     stream >> label;
  299.  
  300.     FW_ASSERT(kind >= 0 && kind <= FW_kMaxButtonKind);
  301.     
  302.     Initialize(ev, kind, label, font);
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    FW_CButton::Flatten
  307. //----------------------------------------------------------------------------------------
  308.  
  309. void FW_CButton::Flatten(Environment* ev, FW_CWritableStream& stream) const
  310. {
  311.     FW_CNativeControl::Flatten(ev, stream);
  312.  
  313.     FW_CString title;
  314.     fControlHelper->GetText(title);
  315.     
  316.     FW_CFont font(FW_kSystemFont12);    // [LSD] todo: get the button's font
  317.  
  318.     stream << fKind;
  319.     stream << title;    
  320.     stream << font;
  321. }
  322.  
  323.