home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 September / Macworld (1998-09).dmg / Shareware World / Info / For Developers / MacZoop 1.8.3 / Projects / Small Demo Project / Show Off Classes / ZShowOffApplication.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-10  |  11.0 KB  |  530 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp            -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZShowOffApplication.cpp    -- the demo application
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21. #include    "ZShowOffApplication.h"
  22. #include    "MacZoop.h"
  23. #include    "ZGWorldWindow.h"
  24. #include    "ZDialog.h"
  25. #include    "ZProgress.h"
  26. #include    "ZFolderScanner.h"
  27. #include    "ZSortTestWindow.h"
  28. #include    "ZTextWindow.h"
  29. #include    "ZHexEditor.h"
  30. #include    "ZStdPrefsDialog.h"
  31. #include    "ZPrefsFile.h"
  32. #include    "ZTestInspector.h"
  33.  
  34. #if APPEARANCE_MGR_AWARE
  35.     #include    "ZAMDialog.h"
  36. #endif
  37.  
  38.  
  39. ZInspectorWindow*    gInspector = NULL;
  40.  
  41.  
  42. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  43.  
  44.  
  45. ZShowOffApplication::ZShowOffApplication()
  46.     : ZApplication()
  47. {
  48. }
  49.  
  50.  
  51.  
  52. void    ZShowOffApplication::StartUp()
  53. {
  54.     gPrefsFile = new ZPrefsFile();
  55.     
  56.     if ( gPrefsFile )
  57.         gPrefsFile->OpenResFork();
  58.  
  59.     anFType = 'TEXT';
  60.     gMenuBar->NominateWindowsMenu( 132 );
  61.     gMenuBar->AppendStdItems( 133, appendFontNames );
  62. }
  63.  
  64.  
  65. void    ZShowOffApplication::ShutDown()
  66. {
  67.     if ( gPrefsFile )
  68.         ForgetObject( gPrefsFile );
  69. }
  70.  
  71.  
  72. /*------------------------------***  MAKENEWWINDOW  ***---------------------------------*/
  73. /*    
  74.  
  75. makes a document window of type ZGWorldWindow or ZSortTestWindow according to the filetype
  76. picked.
  77. ----------------------------------------------------------------------------------------*/
  78.  
  79. void    ZShowOffApplication::MakeNewWindow()
  80. {
  81.     FailNIL( mostRecent = new ZHexEditor( this,  kUntitledWindowID ));
  82.     
  83.     try
  84.     {
  85.         mostRecent->InitZWindow();
  86.     }
  87.     catch(OSErr err)
  88.     {
  89.         ForgetObject(mostRecent);
  90.         
  91.         throw err;
  92.     }
  93. }
  94.  
  95.  
  96. /*----------------------------***  MAKENEWWINDOWTYPE  ***-------------------------------*/
  97. /*    
  98. makes one of several kinds of windows. Here, the class ID of the desired type is used as
  99. an way to identify them. This is one way to do it, but there is nothing sacred about this-
  100. you could equally well just enumerate the various types.
  101. ----------------------------------------------------------------------------------------*/
  102.  
  103. ZWindow*    ZShowOffApplication::MakeNewWindowType( OSType aType )
  104. {
  105.     ZWindow*    zw = NULL;
  106.     
  107.     switch( aType )
  108.     {
  109.         case CLASS_ZHexEditor:
  110.             FailNIL( zw = new ZHexEditor( this, kUntitledWindowID ));
  111.             break;    
  112.         
  113.         default:
  114.         case CLASS_ZWindow:
  115.             FailNIL( zw = new ZWindow( this, kUntitledWindowID ));
  116.             break;
  117.             
  118.         case 'TEXT':
  119.             FailNIL( zw = new ZSortTestWindow( this, kUntitledWindowID ));
  120.             break;
  121.             
  122.         case 'PICT':
  123.             FailNIL( zw = new ZGWorldWindow( this, kUntitledWindowID ));
  124.             break;
  125.             
  126.         case CLASS_ZTextWindow:
  127.             FailNIL( zw = new ZTextWindow( this, kUntitledWindowID ));
  128.             break;
  129.             
  130.         case CLASS_ZScroller:
  131.             FailNIL( zw = new ZScroller( this, kUntitledWindowID ));
  132.             break;
  133.     }
  134.     
  135.     if ( zw )
  136.     {
  137.         try
  138.         {
  139.             zw->InitZWindow();
  140.             
  141.             // postprocessing for specific types:
  142.             
  143.             switch ( aType )
  144.             {
  145.                 case CLASS_ZScroller:
  146.                     Rect bounds = { 0, 0, 600, 600 };
  147.                     
  148.                     (( ZScroller* ) zw )->SetBounds( bounds );
  149.                     break;
  150.                     
  151.                 case 'PICT':
  152.                     (( ZGWorldWindow* ) zw )->SetPictureFromResource( 128 );
  153.                     break;
  154.                     
  155.                 default:
  156.                     break;
  157.             }
  158.         }
  159.         catch( OSErr err )
  160.         {
  161.             ForgetObject( zw );
  162.             throw err;
  163.         }
  164.     }
  165.  
  166.     return zw;
  167. }
  168.  
  169.  
  170. /*--------------------------------***  NEWFLOATER  ***----------------------------------*/
  171. /*    
  172. makes a new floating window, to demonstrate this cool new feature!
  173. ----------------------------------------------------------------------------------------*/
  174.  
  175. void    ZShowOffApplication::NewFloater()
  176. {
  177.     FailNIL( mostRecent = new ZWindow( this, kFloaterID ));
  178.     
  179.     try
  180.     {
  181.         mostRecent->InitZWindow();
  182.         
  183.     }
  184.     catch( OSErr err )
  185.     {
  186.         ForgetObject( mostRecent );
  187.         
  188.         throw err;
  189.     }
  190.     
  191.     mostRecent->Select();
  192. }
  193.  
  194.  
  195. /*-------------------------------***  UPDATEMENUS  ***---------------------------------*/
  196. /*    
  197.  
  198. Enable the dialog demo menu items
  199.  
  200. ----------------------------------------------------------------------------------------*/
  201.  
  202. void    ZShowOffApplication::UpdateMenus()
  203. {
  204.     // enable the menus
  205.     
  206.     gMenuBar->EnableCommand( kCmdOpenMoveableModal );
  207.     gMenuBar->EnableCommand( kCmdOpenModal );
  208.     gMenuBar->EnableCommand( kCmdOpenModeless );
  209.     gMenuBar->EnableCommand( kCmdTestProgress );
  210.     gMenuBar->EnableCommand( kCmdScanFolder );
  211.     gMenuBar->EnableCommand( kCmdNewFloater );
  212.     
  213.     gMenuBar->EnableCommand( kCmdOpenTextWindow );
  214.     gMenuBar->EnableCommand( kCmdOpenGWorldWindow );
  215.     gMenuBar->EnableCommand( kCmdOpenPlainWindow );
  216.     gMenuBar->EnableCommand( kCmdOpenScrollerWindow );
  217.     gMenuBar->EnableCommand( kCmdOpenHexEditorWindow );
  218.     gMenuBar->EnableCommand( kCmdOpenSortTestWindow );
  219.     gMenuBar->EnableCommand( kCmdOpenListWindow );
  220.     gMenuBar->EnableCommand( kCmdTestNotification );
  221.     gMenuBar->EnableCommand( kCmdShowInspector );
  222.     
  223.  
  224.     ZApplication::UpdateMenus();
  225. }
  226.  
  227.  
  228.  
  229. /*-------------------------------***  HANDLECOMMAND  ***--------------------------------*/
  230. /*    
  231.  
  232. handle the menu items for dialog demo
  233.  
  234. ----------------------------------------------------------------------------------------*/
  235.  
  236.  
  237. void    ZShowOffApplication::HandleCommand( const long aCmd )
  238. {
  239.     switch ( aCmd )
  240.     {
  241.         case kCmdOpenMoveableModal:
  242.             OpenDialog( kDialog1 );
  243.             break;
  244.         
  245.         case kCmdOpenModal:
  246.             OpenDialog( kDialog2 );
  247.             break;
  248.         
  249.         case kCmdOpenModeless:
  250.             OpenDialog( kDialog3 );
  251.             break;
  252.         
  253.         case kCmdTestProgress:
  254.             TestProgress();
  255.             break;
  256.         
  257.         case kCmdScanFolder:
  258.             TestScan();
  259.             break;
  260.         
  261.         case kCmdNewFloater:
  262.             NewFloater();
  263.             break;
  264.             
  265.         case kCmdOpenHexEditorWindow:
  266.             OpenNewWindowType( CLASS_ZHexEditor );
  267.             break;
  268.             
  269.         case kCmdOpenPlainWindow:
  270.             OpenNewWindowType( CLASS_ZWindow );
  271.             break;
  272.             
  273.         case kCmdOpenTextWindow:
  274.             OpenNewWindowType( CLASS_ZTextWindow );
  275.             break;
  276.             
  277.         case kCmdOpenSortTestWindow:
  278.             OpenNewWindowType( 'TEXT' );
  279.             break;
  280.         
  281.         case kCmdOpenGWorldWindow:
  282.             OpenNewWindowType( 'PICT' );
  283.             break;
  284.             
  285.         case kCmdOpenScrollerWindow:
  286.             OpenNewWindowType( CLASS_ZScroller );
  287.             break;
  288.             
  289.         case kCmdTestNotification:
  290.             TestNotification();
  291.             break;
  292.             
  293.         case kCmdShowInspector:
  294.             OpenInspector();
  295.             break;
  296.             
  297.     }        
  298.     
  299.     // ask base class to handle any other menu commnds
  300.     
  301.     ZApplication::HandleCommand( aCmd );
  302. }
  303.  
  304.  
  305.  
  306. /*-------------------------------***  RECEIVEMESSAGE  ***-------------------------------*/
  307. /*    
  308. listem for dialog item clicks and respond to them
  309. ----------------------------------------------------------------------------------------*/
  310.  
  311. void    ZShowOffApplication::ReceiveMessage( ZComrade* aSender, long aMsg, void* msgData )
  312. {
  313.     // this code illustrates the fact that your dialog can be set up and responded to
  314.     // by the owning commander, avoiding a subclass of the dialog for standard cases. For the
  315.     // case of subclassing a dialog, look at the prefs dialog.
  316.     
  317.     ZDialog*    zd = (ZDialog*) aSender;
  318.     
  319.     if ( aMsg == kMsgDialogItemClicked && HiWord( *(long*) msgData ) == kDialog1 )
  320.     {
  321.         short         iState, item = LoWord( *(long*) msgData );
  322.         
  323.         switch( item )
  324.         {
  325.             case kEnableOKButtonCheckBox:
  326.                 iState = zd->GetValue( kEnableOKButtonCheckBox );
  327.                 if ( iState )
  328.                     zd->EnableItem( ok );
  329.                 else
  330.                     zd->DisableItem( ok );
  331.                 break;
  332.                 
  333.             case kEnableGroupCheckBox:
  334.                 iState = zd->GetValue( kEnableGroupCheckBox );
  335.                 if ( iState )
  336.                     zd->EnableItem( -1 );
  337.                 else
  338.                     zd->DisableItem( -1 );
  339.                 break;
  340.             
  341.             case kEnableFieldCheckBox:
  342.                 zd->EnableItem( kEditField );
  343.                 break;
  344.                 
  345.             case kDisableFieldCheckBox:
  346.                 zd->DisableItem( kEditField );
  347.                 break;
  348.         }
  349.     }
  350.     
  351.     if ( aMsg == kMsgDialogSetUp && HiWord( *(long*) msgData ) == kDialog1 )
  352.     {
  353.         zd->SetValue( kEnableOKButtonCheckBox, 1 );
  354.         zd->SetValue( kEnableGroupCheckBox, 1 );
  355.     }    
  356. }
  357.  
  358.  
  359. /*----------------------------------***  OPENFILE  ***----------------------------------*/
  360. /*    
  361. make sure we create the right sort of window for the file type chosen
  362. ----------------------------------------------------------------------------------------*/
  363.  
  364. void    ZShowOffApplication::OpenFile( const FSSpec& aFile, const OSType fType, Boolean isStationery )
  365. {
  366.     anFType = fType;
  367.     
  368.     ZApplication::OpenFile( aFile, fType, isStationery );
  369. }
  370.  
  371.  
  372. /*--------------------------------***  TESTPROGRESS  ***--------------------------------*/
  373. /*    
  374.  
  375. this shows how to use the progress dialog class (ZProgress)
  376. ----------------------------------------------------------------------------------------*/
  377.  
  378. #define    kLoops    10000
  379.  
  380.  
  381. void    ZShowOffApplication::TestProgress()
  382. {
  383.     // make the progress dialog object on the heap
  384.     
  385.     ZProgress    aPD (this, kStdProgressResID, kLoops, kProportionalProgress, kCancelType);
  386.     
  387.     long        loop;
  388.     
  389.     // wait two seconds before showing the dialog
  390.     
  391.     aPD.SetDelay( kTwoSeconds );
  392.     aPD.SetMessage("\pLooping 10,000 times...");
  393.     
  394.     // loop <kLoops> times, updating the progress and
  395.     // aborting if the user cancelled
  396.     
  397.     for (loop = 0; loop < kLoops; loop++)
  398.     {
  399.         if (! aPD.InformProgress(loop))
  400.             break;
  401.     }
  402.     
  403.     // when the dialog goes out of scope now, it will be automatically deleted
  404.     // and the chain of command maintained accordingly. Neat, huh?
  405. }
  406.  
  407.  
  408.  
  409.  
  410. /*---------------------------------***  OPENDIALOG  ***---------------------------------*/
  411. /*    
  412.  
  413. construct dialog windows with the ID passed- used to demonstrate dialogs
  414.  
  415. ----------------------------------------------------------------------------------------*/
  416.  
  417. void    ZShowOffApplication::OpenDialog(short id)
  418. {
  419.     ZDialog*    aDialog;
  420.     
  421.     #if APPEARANCE_MGR_AWARE
  422.     
  423.     if ( gMacInfo.hasAppearanceMgr )
  424.         FailNIL( aDialog = new ZAMDialog( this, id ));
  425.     else
  426.         FailNIL(aDialog = new ZDialog( this, id ));
  427.     
  428.     #else
  429.         FailNIL(aDialog = new ZDialog( this, id ));
  430.     #endif
  431.     
  432.     try
  433.     {
  434.         aDialog->InitZWindow();
  435.     }
  436.     catch(OSErr err)
  437.     {
  438.         ForgetObject(aDialog);
  439.         
  440.         throw err;
  441.     }
  442.     
  443.     // show the window and make it active
  444.     
  445.     aDialog->Select();
  446. }
  447.  
  448.  
  449.  
  450. void    ZShowOffApplication::OpenInspector()
  451. {
  452.     ZInspectorWindow*    zi;
  453.     
  454.     if ( gInspector == NULL )
  455.     {
  456.         FailNIL( zi = new ZTestInspector( this, 180 ));
  457.         
  458.         try
  459.         {
  460.             zi->InitZWindow();
  461.             gInspector = zi;
  462.         
  463.         }
  464.         catch( OSErr err )
  465.         {
  466.             ForgetObject( zi );
  467.             throw err;
  468.         }
  469.     }
  470.     
  471.     gInspector->Select();
  472. }
  473.  
  474.  
  475. /*-----------------------------------***  TESTSCAN  ***---------------------------------*/
  476. /*    
  477. demonstrate the folder scanner- this scans every file in the selected folder.
  478.  
  479. ----------------------------------------------------------------------------------------*/
  480.  
  481. void    ZShowOffApplication::TestScan()
  482. {
  483.     ZFolderScanner    aScanner;                        // make object on the stack
  484.     
  485.     if ( aScanner.PickFolder())
  486.     {
  487.         aScanner.SetSearchDepth( kScanEveryFolderInHierarchy );
  488.         aScanner.ScanFolder();                        // do the scan
  489.     }
  490. }
  491.  
  492.  
  493. void    ZShowOffApplication::DoPreferences()
  494. {
  495.     ZStdPrefsDialog*    zPD;
  496.     
  497.     FailNIL( zPD = new ZStdPrefsDialog( this, kStdPrefsDialogResID ));
  498.     
  499.     try
  500.     {
  501.         zPD->InitZWindow();
  502.         zPD->Select();
  503.     }
  504.     catch( OSErr err )
  505.     {
  506.         ForgetObject( zPD );
  507.         throw err;
  508.     }
  509. }
  510.  
  511.  
  512. void    ZShowOffApplication::TestNotification()
  513. {
  514.     // wait 5 seconds, then post an alert. If in the background, this should use the
  515.     // notification manager.
  516.     
  517.     (void) Alert( 201, NULL );
  518.     
  519.     long    timer = TickCount();
  520.     
  521.     SetWatchCursor();
  522.     
  523.     while( TickCount() < ( timer + 300 ))
  524.         gApplication->Process1Event();
  525.         
  526.     FailOSErr( 123 );
  527. }
  528.  
  529.  
  530.