home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / FWPopup.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  10.6 KB  |  360 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPopup.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWPOPUP_H
  13. #include "FWPopup.h"
  14. #endif
  15.  
  16. #ifndef FWFRAME_H
  17. #include "FWFrame.h"
  18. #endif
  19.  
  20. #ifndef FWPART_H
  21. #include "FWPart.h"
  22. #endif
  23.  
  24. #ifndef FWNOTDEF_H
  25. #include "FWNotDef.h"
  26. #endif
  27.  
  28. #ifndef FWCONTXT_H
  29. #include "FWContxt.h"
  30. #endif
  31.  
  32. // ----- OS Layer -----
  33.  
  34. #ifndef FWEVENT_H
  35. #include "FWEvent.h"
  36. #endif
  37.  
  38. #ifndef FWRRCSHP_H
  39. #include "FWRRcShp.h"
  40. #endif
  41.  
  42. #ifndef FWODGEOM_H
  43. #include "FWODGeom.h"
  44. #endif
  45.  
  46. // ----- Foundation Includes -----
  47.  
  48. #ifndef FWCFMRES_H
  49. #include "FWCFMRes.h"
  50. #endif
  51.  
  52. #ifndef FWSTREAM_H
  53. #include "FWStream.h"
  54. #endif
  55.  
  56. #ifndef FWNOTIFN_H
  57. #include "FWNotifn.h"
  58. #endif
  59.  
  60. #ifndef FWNOTDEF_H
  61. #include "FWNotDef.h"
  62. #endif
  63.  
  64. #ifndef FWSOMENV_H
  65. #include "FWSOMEnv.h"
  66. #endif
  67.  
  68. // ----- OpenDoc Includes -----
  69.  
  70. #ifndef SOM_ODSession_xh
  71. #include <ODSessn.xh>
  72. #endif
  73.  
  74. #ifndef SOM_ODFacet_xh
  75. #include <Facet.xh>
  76. #endif
  77.  
  78. #ifndef SOM_ODTransform_xh
  79. #include <Trnsform.xh>
  80. #endif
  81.  
  82. //========================================================================================
  83. // File scope definitions
  84. //========================================================================================
  85.  
  86. #ifdef FW_BUILD_MAC
  87. #pragma segment fwgadgts
  88. #endif
  89.  
  90. //========================================================================================
  91. // CLASS FW_CPopupMenu
  92. //========================================================================================
  93.  
  94. FW_DEFINE_AUTO(FW_CPopupMenu)
  95. FW_DEFINE_CLASS_M1(FW_CPopupMenu, FW_CNativeControl)
  96.  
  97. // This class is archivable, but we provide the archiving implementation in a separate
  98. // translation unit in order to enable deadstripping of the archiving-related code
  99. // in parts that do not use archiving with this class.
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // FW_CPopupMenu::FW_CPopupMenu
  103. //----------------------------------------------------------------------------------------
  104.  
  105. FW_CPopupMenu::FW_CPopupMenu(Environment* ev, 
  106.                                 FW_CSuperView* container, 
  107.                                 const FW_CRect& bounds,
  108.                                 ODID viewID,
  109.                                 FW_ResourceID menuID,
  110.                                 const FW_CString& title,
  111.                                    short titleWidth,
  112.                                   short initialMenuItem,
  113.                                   FW_PopupMenuStyle popupVariation,
  114.                                 const FW_CFont& font,
  115.                                 long popupRefCon) :
  116.     FW_CNativeControl(ev, container, bounds, viewID, FW_kPopupClickedMsg)
  117. {
  118.     Initialize(ev, menuID, title, titleWidth, initialMenuItem, popupVariation, font, popupRefCon);
  119.     FW_END_CONSTRUCTOR
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    FW_CPopupMenu::FW_CPopupMenu
  124. //----------------------------------------------------------------------------------------
  125.  
  126. FW_CPopupMenu::FW_CPopupMenu(Environment* ev) :
  127.     FW_CNativeControl(ev)
  128. {
  129.     FW_END_CONSTRUCTOR
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // FW_CPopupMenu::~FW_CPopupMenu
  134. //----------------------------------------------------------------------------------------
  135.  
  136. FW_CPopupMenu::~FW_CPopupMenu()
  137. {
  138.     FW_START_DESTRUCTOR
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. //    FW_CPopupMenu::Initialize
  143. //----------------------------------------------------------------------------------------
  144.  
  145. void FW_CPopupMenu::Initialize(Environment* ev,
  146.                                 FW_ResourceID menuID,
  147.                                 const FW_CString& title,
  148.                                    short titleWidth,
  149.                                   short initialMenuItem,
  150.                                   FW_PopupMenuStyle popupVariation,
  151.                                 const FW_CFont& font,
  152.                                 long popupRefCon) 
  153. {
  154. #ifdef FW_BUILD_MAC
  155.     Str255 pascalText;
  156.     title.ExportPascal(pascalText);
  157.     fControlHelper = new FW_CPrivMacControlHelper (ev, this, 0, (short)menuID, titleWidth, 
  158.                                 popupMenuProc + popupVariation, pascalText, font, popupRefCon);
  159.  
  160.     if (GetPlatformMenuHandle() == 0)
  161.     {
  162.         // Menu wasn't loaded correctly
  163.         FW_DEBUG_MESSAGE("Missing MENU resource or incorrect menuID");
  164.     }
  165.     fControlHelper->SetValue(initialMenuItem, FALSE);
  166.     fValue = fControlHelper->GetValue();
  167. #endif
  168.  
  169. #ifdef FW_BUILD_WIN
  170.     // [TODO]
  171.     fControlHelper = new FW_CPrivWinPopupMenuHelper
  172.                                 (ev, FW_TYPEID_FROM_TYPE(FW_CPrivWinPopupMenuHelper), this);
  173.     fControlHelper->SetText(title);
  174. #endif
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // FW_CPopupMenu::GetPlatformMenuHandle
  179. //----------------------------------------------------------------------------------------
  180.  
  181. FW_PlatformHandle    FW_CPopupMenu::GetPlatformMenuHandle() const
  182. {
  183. #ifdef FW_BUILD_MAC
  184.     PopupPrivateDataHandle    h = 
  185.         (PopupPrivateDataHandle) (**(fControlHelper->GetControlHandle())).contrlData;
  186.     return (FW_PlatformHandle)(**h).mHandle;
  187. #endif
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. // FW_CPopupMenu::PrivSetValue
  192. //----------------------------------------------------------------------------------------
  193.  
  194. FW_Boolean FW_CPopupMenu::PrivSetValue(Environment *ev, FW_ControlValue value, ODFacet* facet)
  195. {
  196.     // Update the native control.  Return false if value was not changed 
  197.     if (FW_CNativeControl::PrivSetValue(ev, value, facet))
  198.     {
  199.         // Send a popup menu message to its receivers
  200.         if (GetMessage(ev) != FW_kNullMsg)
  201.             Notify(ev, FW_CPopupMenuNotification(this));
  202.  
  203.         return true;
  204.     }
  205.     else
  206.         return false;
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // FW_CPopupMenu::ControlClicked
  211. //----------------------------------------------------------------------------------------
  212.  
  213. void FW_CPopupMenu::ControlClicked(Environment *ev, FW_ControlValue value, ODFacet* facet)
  214. {
  215.     PrivSetValue(ev, value, facet);
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. //    FW_CPopupMenu::GetMenuString
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void    FW_CPopupMenu::GetMenuString(Environment* ev, short index, FW_CString& str) const
  223. {
  224.     // [LSD] todo: add checking for valid index
  225.     
  226. #ifdef FW_BUILD_MAC
  227.     FW_CAcquireCFMResourceAccess resAccess(ev);    // open res file for MENU resource
  228.     
  229.     // force an udpate of the control handle to get the correct menu handle...
  230.     Point zero = { 0, 0};
  231.     ::TestControl(fControlHelper->GetControlHandle(), zero);
  232.     
  233.     MenuHandle macMenu = (MenuHandle)GetPlatformMenuHandle();
  234.     Str255 pascalStr;
  235.     
  236.     ::GetMenuItemText(macMenu, index, pascalStr);
  237.     str.ReplaceAll(pascalStr);
  238. #endif    
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. //    FW_CPopupMenu::Flatten
  243. //----------------------------------------------------------------------------------------
  244.  
  245. void FW_CPopupMenu::Flatten(Environment* ev, FW_CWritableStream& stream) const
  246. {
  247.     FW_CNativeControl::Flatten(ev, stream);
  248.     
  249.     ControlHandle controlH = fControlHelper->GetControlHandle();
  250.     FW_CString title;
  251.     fControlHelper->GetText(title);
  252.     
  253.     // [LSD] we use default values for some fields for now.
  254.     PopupPrivateDataHandle    h = 
  255.         (PopupPrivateDataHandle) ((*controlH)->contrlData);
  256.     FW_ASSERT(h);
  257.     FW_ResourceID menuID = (**h).mID;
  258.  
  259.     stream << menuID;
  260.     stream << (short)0;    // 0 titleWidth. It will be calculated dynamically when the view is read in
  261.     stream << GetValue(ev);
  262.     stream << ::GetControlVariant(controlH);
  263.     stream << (*controlH)->contrlRfCon;
  264.     stream << fControlHelper->GetFont(ev);    
  265.     stream << title;
  266. }
  267.  
  268. //----------------------------------------------------------------------------------------
  269. //    FW_CPopupMenu::Initialize
  270. //----------------------------------------------------------------------------------------
  271.  
  272. void FW_CPopupMenu::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  273. {
  274.     FW_CNativeControl::InitializeFromStream(ev, stream);
  275.  
  276.     FW_ResourceID menuID;
  277.     FW_CString title;
  278.     short titleWidth;
  279.     short initialMenuItem;
  280.     FW_PopupMenuStyle popupVariation;
  281.     FW_CFont font;
  282.     long popupRefCon;
  283.     
  284.     stream >> menuID;
  285.     stream >> titleWidth;
  286.     stream >> initialMenuItem;
  287.     stream >> popupVariation;
  288.     stream >> popupRefCon;
  289.     stream >> font;    
  290.     stream >> title;
  291.  
  292.     Initialize(ev, menuID, title, titleWidth, initialMenuItem, popupVariation, font, popupRefCon);
  293. }
  294.  
  295. //========================================================================================
  296. //    CLASS FW_CPopupMenuNotification
  297. //========================================================================================
  298.  
  299. FW_DEFINE_CLASS_M1(FW_CPopupMenuNotification, FW_CControlNotification)
  300.  
  301. //----------------------------------------------------------------------------------------
  302. // FW_CPopupMenuNotification::FW_CPopupMenuNotification
  303. //----------------------------------------------------------------------------------------
  304.  
  305. FW_CPopupMenuNotification::FW_CPopupMenuNotification(FW_CPopupMenu* popup) :
  306.     FW_CControlNotification(popup)
  307. {
  308.     FW_SOMEnvironment ev;
  309.     fMenuIndex = (short)popup->GetValue(ev);
  310.     popup->GetMenuString(ev, fMenuIndex, fMenuString);
  311. }
  312.  
  313. //----------------------------------------------------------------------------------------
  314. // FW_CPopupMenuNotification::FW_CPopupMenuNotification
  315. //----------------------------------------------------------------------------------------
  316.  
  317. FW_CPopupMenuNotification::FW_CPopupMenuNotification(const FW_CPopupMenuNotification& other) :
  318.     FW_CControlNotification(other)
  319. {
  320.     FW_SOMEnvironment ev;
  321.     fMenuIndex = other.GetMenuIndex(ev);
  322.     other.GetMenuString(ev, fMenuString);
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // FW_CPopupMenuNotification::~FW_CPopupMenuNotification
  327. //----------------------------------------------------------------------------------------
  328.  
  329. FW_CPopupMenuNotification::~FW_CPopupMenuNotification()
  330. {
  331. }
  332.  
  333. //----------------------------------------------------------------------------------------
  334. // FW_CPopupMenuNotification::operator=
  335. //----------------------------------------------------------------------------------------
  336.  
  337. FW_CPopupMenuNotification& FW_CPopupMenuNotification::operator=(const FW_CPopupMenuNotification& other)
  338. {
  339.     if (this != &other)
  340.     {
  341.         FW_CControlNotification::operator=(other);
  342.         fMenuIndex = other.fMenuIndex;
  343.         fMenuString = other.fMenuString;
  344.     }
  345.     
  346.     return *this;
  347. }
  348.  
  349. //----------------------------------------------------------------------------------------
  350. // FW_CPopupMenuNotification::operator==
  351. //----------------------------------------------------------------------------------------
  352.  
  353. FW_Boolean FW_CPopupMenuNotification::operator==(const FW_CPopupMenuNotification& other) const
  354. {
  355.     return FW_CControlNotification::operator==(other) &&
  356.                 fMenuIndex == other.fMenuIndex &&
  357.                 fMenuString == other.fMenuString;
  358. }
  359.  
  360.