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 / Tools & Goodies / IntlTest / Sources / Part.cpp < prev    next >
Encoding:
Text File  |  1996-09-12  |  16.3 KB  |  528 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                IntlTest.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "IntlTest.hpp"
  11.  
  12. #ifndef PART_H
  13. #include "Part.h"
  14. #endif
  15.  
  16. #ifndef CONTENT_H
  17. #include "Content.h"
  18. #endif
  19.  
  20. #ifndef BINDING_K
  21. #include "Binding.k"
  22. #endif
  23.  
  24. #ifndef FRAME_H
  25. #include "Frame.h"
  26. #endif
  27.  
  28. #ifndef SELECT_H
  29. #include "Select.h"
  30. #endif
  31.  
  32. // for gDefaultFontMenu and gJFontMenu
  33. #ifndef EDITVIEWS_H
  34. #include "EditViews.h"
  35. #endif
  36.  
  37. #ifndef DIALOGS_H
  38. #include "Dialogs.h"
  39. #endif
  40.  
  41. // ----- Framework Layer -----
  42.  
  43. #ifndef FWABOUT_H
  44. #include "FWAbout.h"
  45. #endif
  46.  
  47. #ifndef FWEDVIEW_H
  48. #include "FWEdView.h"
  49. #endif
  50.  
  51. #ifndef FWEVENT_H
  52. #include "FWEvent.h"
  53. #endif
  54.  
  55. #ifndef FWKIND_H
  56. #include "FWKind.h"
  57. #endif
  58.  
  59. #ifndef FWPRESEN_H
  60. #include "FWPresen.h"
  61. #endif
  62.  
  63. // ----- OS Layer -----
  64.  
  65. #ifndef FWMENU_H
  66. #include "FWMenu.h"
  67. #endif
  68.  
  69. #ifndef FWCFMRES_H
  70. #include "FWCFMRes.h"
  71. #endif
  72.  
  73. #ifndef FWRESTYP_H
  74. #include "FWResTyp.h"
  75. #endif
  76.  
  77. #ifndef FWFONTLI_H
  78. #include "FWFontLi.h"
  79. #endif
  80.  
  81. // ----- OpenDoc Includes -----
  82.  
  83. #ifndef SOM_ODMenuBar_xh
  84. #include <MenuBar.xh>
  85. #endif
  86.  
  87. //========================================================================================
  88. // Constants and Globals
  89. //========================================================================================
  90.  
  91. FW_Locale gJapaneseLocale = { smJapanese, langJapanese };
  92.  
  93. short gAppleMenuID;
  94.  
  95. // Function prototypes to satisfy compiler
  96.  
  97. void AddFontsToMenu(Environment* ev, FW_CPullDownMenu* menu, FW_Boolean singleByte);
  98.  
  99. //========================================================================================
  100. //    Runtime info
  101. //========================================================================================
  102.  
  103. #ifdef FW_BUILD_MAC
  104. #pragma segment odfIntlTest
  105. #endif
  106.  
  107. //========================================================================================
  108. //    CIntlTestPart class
  109. //========================================================================================
  110.  
  111. FW_DEFINE_AUTO(CIntlTestPart)
  112.  
  113. //----------------------------------------------------------------------------------------
  114. // CIntlTestPart constructor
  115. //----------------------------------------------------------------------------------------
  116.  
  117. CIntlTestPart::CIntlTestPart(ODPart* odPart) :
  118.     FW_CPart(odPart, FW_gInstance, kPartInfoID),
  119.     fPartContent(NULL),
  120.     fMainPresentation(NULL),
  121.     fOpTestPresentation(NULL),
  122.     fReaderTestPresentation(NULL),
  123.     fNumberTestPresentation(NULL),
  124.     fTestFrame(NULL)
  125. {
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // CIntlTestPart destructor
  130. //----------------------------------------------------------------------------------------
  131.  
  132. CIntlTestPart::~CIntlTestPart()
  133. {
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // CIntlTestPart::Initialize
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void CIntlTestPart::Initialize(Environment* ev, ODStorageUnit* storageUnit, FW_Boolean fromStorage)
  141. {
  142.     FW_CPart::Initialize(ev, storageUnit, fromStorage);
  143.  
  144.     // ----- Register our Presentation
  145.     CIntlTestSelection* selection = FW_NEW(CIntlTestSelection, (ev, this));
  146.     fMainPresentation = RegisterPresentation(ev, kMainPresentation, TRUE, selection);
  147.     fTestSelection = selection;
  148.     fOpTestPresentation = RegisterPresentation(ev, kOpTestPresentation, false, kOpTestDialog, kOpTestDialog, NULL);
  149.     fReaderTestPresentation = RegisterPresentation(ev, kReaderTestPresentation, false, kReaderTestDialog, kReaderTestDialog, NULL);
  150.     fNumberTestPresentation = RegisterPresentation(ev, kNumberTestPresentation, false, kNumberTestDialog, kNumberTestDialog, NULL);
  151.     
  152.     // ----- Register our other kinds -----
  153.     RegisterKind(ev, kODMacIText, FW_kDataInterchangeStorage, FW_kImportExportEnabled);
  154.     RegisterKind(ev, kTEXTOSType, kODPlatformDataType, FW_kAllStorage, FW_kImportExportEnabled);
  155.     RegisterKind(ev, kTEXTOSType, kODPlatformFileType, FW_kDataInterchangeStorage, FW_kImportExportEnabled);
  156.     FW_CKind* partKind = GetPartKind(ev);
  157.     partKind->ChangeStorageKind(ev, FW_kPartStorage | FW_kLinkStorage | FW_kFileStorage);
  158.  
  159.     // ----- Initialize my menus -----
  160.     InitializeMenus(ev);
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // CIntlTestPart::InitializeMenus
  165. //----------------------------------------------------------------------------------------
  166. void CIntlTestPart::InitializeMenus(Environment* ev)
  167. {
  168.     FW_PSharedLibraryResourceFile resFile(ev);
  169.     FW_CMenuBar* menuBar = GetMenuBar(ev);
  170.  
  171.     // We instantiate a submenu item from the stream so we must
  172.     // prevent it from being deadstripped.
  173.     FW_DO_NOT_DEAD_STRIP(FW_CSubMenuItem);
  174.  
  175.     // ----- Create the default Font menu -----
  176.     FW_CString32 title;    // read title from resource
  177.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kFontMenuString, title);
  178.     FW_CPullDownMenu* fontMenu = new FW_CPullDownMenu(ev, title);
  179.     AddFontsToMenu(ev, fontMenu, true);        // single-byte fonts
  180.     gDefaultFontMenu = fontMenu;
  181.  
  182.     // ----- Create the Japanese Font menu -----
  183.     fontMenu = new FW_CPullDownMenu(ev, title);
  184.     AddFontsToMenu(ev, fontMenu, false);    // double-byte fonts
  185.     gJFontMenu = fontMenu;
  186.  
  187.     // ----- Set the Font menu to the default one -----
  188.     menuBar->AdoptMenuLast(ev, gDefaultFontMenu);
  189.     gFontMenu = gDefaultFontMenu;
  190.     gFontCount = gFontMenu->CountItems(ev);
  191.  
  192.     // ----- Create the Size menu -----
  193.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kSizeMenuString, title);
  194.     fontMenu = new FW_CPullDownMenu(ev, title);
  195.  
  196.     // ----- Add the Size menu items -----
  197.     FW_CString32 sizeString;
  198.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kSize9, sizeString);
  199.     fontMenu->AppendTextItem(ev, sizeString, cFontSize9);
  200.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kSize10, sizeString);
  201.     fontMenu->AppendTextItem(ev, sizeString, cFontSize10);
  202.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kSize12, sizeString);
  203.     fontMenu->AppendTextItem(ev, sizeString, cFontSize12);
  204.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kSize14, sizeString);
  205.     fontMenu->AppendTextItem(ev, sizeString, cFontSize14);
  206.     FW_LoadStringByID(ev, resFile, kIntlTestPartStrings, FW_kMultiStringRes, kSize18, sizeString);
  207.     fontMenu->AppendTextItem(ev, sizeString, cFontSize18);
  208.     menuBar->AdoptMenuLast(ev, fontMenu);
  209.     gSizeMenuID = fontMenu->GetMenuID(ev);    // menuID not assigned until menu is added to menuBar
  210.  
  211.     // ----- Try adding an additional item to the Apple menu -----
  212.     short appleMenuID;
  213.     short aboutItemID;
  214.     FW_CAcquiredODMenuBar aqODMenuBar = menuBar->AcquireODMenuBar(ev);
  215.     aqODMenuBar->GetMenuAndItem(ev, kODCommandAbout, &appleMenuID, &aboutItemID);
  216.     ODPlatformMenu platformMenu = aqODMenuBar->GetMenu(ev, appleMenuID);
  217.     ::InsertMenuItem(platformMenu, "\pTesting", aboutItemID);    // insert after About item
  218.     gAppleMenuID = appleMenuID;
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // CIntlTestPart::NewFrame
  223. //----------------------------------------------------------------------------------------
  224.  
  225. FW_CFrame* CIntlTestPart::NewFrame(Environment* ev,
  226.                                 ODFrame* odFrame,
  227.                                 FW_CPresentation* presentation,
  228.                                 FW_Boolean fromStorage)    // Override
  229. {
  230.     FW_UNUSED(fromStorage);
  231.  
  232.     if (presentation == fMainPresentation)
  233.     {
  234.         fTestFrame = FW_NEW(CIntlTestFrame, (ev, odFrame, presentation, this));
  235.         return fTestFrame;
  236.     }
  237.  
  238.     if (presentation == fOpTestPresentation)
  239.     {
  240.         return FW_NEW(COpTestDialogFrame, (ev, odFrame, presentation, this));
  241.     }
  242.  
  243.     if (presentation == fReaderTestPresentation)
  244.     {
  245.         return FW_NEW(CReaderTestDialogFrame, (ev, odFrame, presentation, this));
  246.     }
  247.  
  248.     if (presentation == fNumberTestPresentation)
  249.     {
  250.         return FW_NEW(CNumberTestDialogFrame, (ev, odFrame, presentation, this));
  251.     }
  252.  
  253.     return NULL;
  254. }
  255.  
  256. //------------------------------------------------------------------------------
  257. // CIntlTestPart::NewPartContent
  258. //------------------------------------------------------------------------------
  259.  
  260. FW_CContent* CIntlTestPart::NewPartContent(Environment* ev)
  261. {
  262.     fPartContent = FW_NEW(CIntlTestContent, (ev, this));
  263.     return fPartContent;
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // CIntlTestPart::DoAbout
  268. //----------------------------------------------------------------------------------------
  269. FW_Handled CIntlTestPart::DoAbout(Environment* ev)
  270. {
  271.     ::FW_About(ev, this, kAboutBox);
  272.     
  273.     return FW_kHandled;
  274. }
  275.  
  276. //----------------------------------------------------------------------------------------
  277. // CIntlTestPart::DoMenu
  278. //----------------------------------------------------------------------------------------
  279.  
  280. FW_Handled CIntlTestPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent) // Override
  281. {
  282.     FW_Handled menuHandled = FW_kHandled;
  283.     ODCommandID id = theMenuEvent.GetCommandID(ev);
  284.  
  285.     FW_CAcquiredODMenuBar aqODMenuBar = GetMenuBar(ev)->AcquireODMenuBar(ev);
  286.     if (aqODMenuBar->IsCommandSynthetic(ev, id))
  287.     {
  288.         short menuID, itemID;
  289.         aqODMenuBar->GetMenuAndItem(ev, id, &menuID, &itemID);
  290.         if (menuID == gAppleMenuID && itemID == 2)
  291.         {
  292.             FW_Beep();
  293.             return FW_kHandled;
  294.         }
  295.     }
  296.  
  297.     switch (id)
  298.     {
  299.         case cFirstCommand:
  300.             OpenOperatorTestDialog(ev);
  301.             break;
  302.  
  303.         case cThirdCommand:
  304.             OpenReaderTestDialog(ev);
  305.             break;
  306.  
  307.         case cFifthCommand:
  308.             OpenNumberTestDialog(ev, id);
  309.             break;
  310.  
  311.         default:
  312.             menuHandled = FW_kNotHandled;
  313.     }
  314.     
  315.     return menuHandled;
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------
  319. // CIntlTestPart::DoAdjustMenus
  320. //----------------------------------------------------------------------------------------
  321.  
  322. FW_Handled CIntlTestPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, 
  323.                                      FW_Boolean hasMenuFocus,
  324.                                      FW_Boolean isRoot)
  325. {
  326.     FW_UNUSED(isRoot);
  327.  
  328.     if (hasMenuFocus)
  329.     {
  330.         // Some tests require that an edit view be active
  331.         FW_Boolean isActiveEditView = true;
  332.         ODID id = fTestFrame->GetActiveEditViewId(ev);
  333.         if (id == 0)
  334.             isActiveEditView = false;
  335.         else if (fTestFrame->FindEditView(ev, id) == NULL)
  336.             isActiveEditView = false;
  337.  
  338.         // ----- Set up the IntlTest menu
  339.         menuBar->EnableCommand(ev, cFirstCommand, isActiveEditView);
  340.         menuBar->EnableCommand(ev, cThirdCommand, isActiveEditView);
  341.         menuBar->EnableCommand(ev, cFifthCommand, true);
  342.         menuBar->EnableCommand(ev, cSeventhCommand, true);
  343.     }
  344.     
  345.     return FW_kNotHandled;
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. ODID CIntlTestPart::WhichEditViewIsActive(Environment* ev)
  350. {
  351.     ODID target = fTestFrame->GetActiveEditViewId(ev);
  352.     return target;
  353. }
  354.  
  355. //----------------------------------------------------------------------------------------
  356. FW_CString MyLoadRStringByID(Environment* ev,
  357.                              FW_PResourceFile& file,
  358.                              FW_ResourceID resourceId,
  359.                              FW_ResourceType resourceType)
  360. {
  361.     FW_ASSERT(resourceType == TESTSTRINGRES);
  362.  
  363.     // FW_RString resource header
  364.     struct RStringHeader
  365.     {
  366.         FW_Locale    fLocale;
  367.         short        fLengthWord;
  368.     };
  369.  
  370.     FW_PResource resource(ev, file, resourceId, resourceType);
  371.     FW_CAcquireResourceData data(ev, resource);
  372.     RStringHeader* p = (RStringHeader*) data.GetData();
  373.  
  374.     FW_Locale* pLocale = (FW_Locale*) p;
  375.     FW_CString string(((const FW_Char*)p)+sizeof(RStringHeader), p->fLengthWord, (const FW_Locale&)*pLocale);
  376.     return string;
  377. }
  378.  
  379. //----------------------------------------------------------------------------------------
  380. void CIntlTestPart::UpdateTextData(Environment* ev)
  381. {
  382.     FW_CString s;
  383.  
  384.     if (fTestFrame)        // fTestFrame is NULL when creating stationery
  385.     {
  386.         FW_CEditView* editView;
  387.  
  388.         editView = fTestFrame->FindEditView(ev, kEnglishEditView);
  389.         s = editView->GetText(ev);
  390.         fPartContent->SetTextData(ev, s, kEnglishEditView);
  391.  
  392.         editView = fTestFrame->FindEditView(ev, kJapaneseEditView);
  393.         s = editView->GetText(ev);
  394.         fPartContent->SetTextData(ev, s, kJapaneseEditView);
  395.     }
  396.     else    // creating stationery - load default strings from resources
  397.     {
  398.         FW_PSharedLibraryResourceFile resFile(ev);
  399.         s = MyLoadRStringByID(ev, resFile, kDefaultEngStringID, TESTSTRINGRES);
  400.         fPartContent->SetTextData(ev, s, kEnglishEditView);
  401.         s = MyLoadRStringByID(ev, resFile, kDefaultJStringID, TESTSTRINGRES);
  402.         fPartContent->SetTextData(ev, s, kJapaneseEditView);
  403.     }
  404. }
  405.  
  406. //----------------------------------------------------------------------------------------
  407. void CIntlTestPart::UpdateViewsFromTextData(Environment* ev)
  408. {
  409.     FW_CEditView* editView;
  410.     FW_CString s;
  411.  
  412.     s = fPartContent->GetTextData(kEnglishEditView);
  413.     editView = fTestFrame->FindEditView(ev, kEnglishEditView);
  414.     editView->SetText(ev, s);
  415.  
  416.     s = fPartContent->GetTextData(kJapaneseEditView);
  417.     editView = fTestFrame->FindEditView(ev, kJapaneseEditView);
  418.     editView->SetText(ev, s);
  419. }
  420.  
  421. //----------------------------------------------------------------------------------------
  422. void AddFontsToMenu(Environment* ev, FW_CPullDownMenu* menu, FW_Boolean singleByte)
  423. {
  424.     short count = 0;
  425.  
  426.     // Get a list of all the fonts
  427.     FW_CFontIterator fontIter;    // creates a temporary FW_CFontList
  428.     FW_CString fontName;
  429.     FW_Locale locale;
  430.     ODCommandID commandID = cFirstFontCommand;
  431.     for (fontName = fontIter.First(); fontIter.IsNotComplete(); fontName = fontIter.Next())
  432.     {
  433.         fontName.GetLocale(locale);
  434.         if (FW_LocaleIsSingleByte(locale) == singleByte)
  435.         {
  436.             // Add a menu item for this font
  437.             menu->AppendTextItem(ev, fontName, commandID+count);
  438.             count++;
  439.         }
  440.     }
  441. }
  442.  
  443. //----------------------------------------------------------------------------------------
  444. CMyEditView* CIntlTestPart::GetActiveEditView(Environment* ev)
  445. {
  446.     FW_CEditView* editView = NULL;
  447.     ODID id = fTestFrame->GetActiveEditViewId(ev);
  448.     if (id)
  449.     {
  450.         editView = fTestFrame->FindEditView(ev, id);
  451.     }
  452.     if (editView == NULL) return NULL;    // none of the edit views is active
  453.     return (CMyEditView*)editView;
  454. }
  455.  
  456. //----------------------------------------------------------------------------------------
  457. void CIntlTestPart::OpenOperatorTestDialog(Environment* ev)
  458. {
  459.     // Find the active edit view
  460.     CMyEditView* editView = GetActiveEditView(ev);
  461.  
  462.     FW_CPoint    position(FW_kZeroPoint);
  463.     FW_CPoint    size(FW_IntToFixed(300), FW_IntToFixed(160));
  464.     FW_WindowStyle style = FW_kStandardDialogPosition | FW_kHasCaption;
  465.  
  466.     COpTestDialogFrame* dialog = (COpTestDialogFrame*) FW_CDialogFrame::NewModalDialog(ev, 
  467.                             this,                        // Your part
  468.                             fOpTestPresentation,         // Used in NewFrame
  469.                             size,                         // Window size
  470.                             position,                     // Window position
  471.                             style,                        // Make dialog moveable
  472.                             FW_CString("operator[] Test"));    // Title for moveable dialog
  473.  
  474.     if (dialog != NULL)
  475.     {                        
  476.         dialog->Initialize(ev, editView);        
  477.         dialog->GetWindow(ev)->Show(ev);
  478.     }
  479. }
  480.  
  481. //----------------------------------------------------------------------------------------
  482. void CIntlTestPart::OpenNumberTestDialog(Environment* ev, ODCommandID id)
  483. {
  484.     FW_CString title("Real Number Test");
  485.     FW_CPoint    position(FW_kZeroPoint);
  486.     FW_CPoint    size(FW_IntToFixed(300), FW_IntToFixed(160));
  487.     FW_WindowStyle style = FW_kStandardDialogPosition | FW_kHasCaption;
  488.  
  489.     CNumberTestDialogFrame* dialog = (CNumberTestDialogFrame*) FW_CDialogFrame::NewModalDialog(ev, 
  490.                             this,                        // Your part
  491.                             fNumberTestPresentation,     // Used in NewFrame
  492.                             size,                         // Window size
  493.                             position,                     // Window position
  494.                             style,                        // Make dialog moveable
  495.                             title);                        // Title for moveable dialog
  496.     
  497.     if (dialog != NULL)
  498.     {                        
  499.         dialog->Initialize(ev, (id == cSixthCommand));        
  500.         dialog->GetWindow(ev)->Show(ev);
  501.     }
  502. }
  503.  
  504. //----------------------------------------------------------------------------------------
  505. void CIntlTestPart::OpenReaderTestDialog(Environment* ev)
  506. {
  507.     // Find the active edit view
  508.     CMyEditView* editView = GetActiveEditView(ev);
  509.  
  510.     FW_CPoint    position(FW_kZeroPoint);
  511.     FW_CPoint    size(FW_IntToFixed(300), FW_IntToFixed(200));
  512.     FW_WindowStyle style = FW_kStandardDialogPosition | FW_kHasCaption;
  513.  
  514.     CReaderTestDialogFrame* dialog = (CReaderTestDialogFrame*) FW_CDialogFrame::NewModalDialog(ev, 
  515.                             this,                        // Your part
  516.                             fReaderTestPresentation,     // Used in NewFrame
  517.                             size,                         // Window size
  518.                             position,                     // Window position
  519.                             style,                        // Make dialog moveable
  520.                             FW_CString("TextReader Test"));    // Title for moveable dialog
  521.     
  522.     if (dialog != NULL)
  523.     {                        
  524.         dialog->Initialize(ev, editView);        
  525.         dialog->GetWindow(ev)->Show(ev);
  526.     }
  527. }
  528.