home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Code Resources / Arrow CDEF / ArrowCDEF Tester.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-23  |  6.7 KB  |  249 lines  |  [TEXT/KAHL]

  1. /*
  2.     FILE:            ArrowCDEF Tester.c
  3.     DESCRIPTION:    A simple program to test the ArrowCDEF.
  4.     AUTHOR:        David Hay
  5.     CREATED:        March 17, 1994
  6.     
  7.     Copyright © 1994 by David Hay
  8. */
  9.  
  10. #include <ctype.h>
  11. #include "ArrowCDEF.h"
  12.  
  13. #define     kBaseResID        128
  14. #define     kMoveToFront        (WindowPtr)-1L
  15.  
  16. #define    kEnterKey            0x03
  17. #define    kReturnKey        0x0d
  18. #define     kEscapeKey        0x1b
  19.  
  20. #define    kVisualDelay        8
  21. #define    kScrollDelay        15
  22.  
  23. #define    kArrowCntl        2
  24. #define    kOKOutline        3
  25. #define    kTextItem            4
  26. #define    kActiveItem        6
  27.  
  28.  
  29. void    ToolBoxInit( void )
  30. {
  31.     InitGraf(&thePort);
  32.     InitFonts();
  33.     FlushEvents(everyEvent, 0);
  34.     InitWindows();
  35.     InitMenus();
  36.     TEInit();
  37.     InitDialogs(nil);
  38.     MaxApplZone();
  39.     InitCursor();
  40. }
  41.  
  42. pascal Boolean DialogFilter( DialogPtr theDialog, EventRecord* theEvent, short* itemHit )
  43. {
  44.     short        itemType;
  45.     Handle        itemHandle;
  46.     Rect            itemRect;
  47.     char            key;
  48.     long            finalTicks;
  49.     Point            mousePoint;
  50.     
  51.     switch( theEvent->what )
  52.     {
  53.         case keyDown:
  54.         case autoKey:
  55.             key = ( char ) ( ( theEvent->message ) & charCodeMask );
  56.             
  57.             if ( key == kReturnKey || key == kEnterKey )
  58.             {
  59.                     /*        Respond as if the OK button was pressed        */
  60.                 GetDItem( theDialog, ok, &itemType, &itemHandle, &itemRect );
  61.                 HiliteControl( (ControlHandle)itemHandle, inButton );
  62.                 Delay( kVisualDelay, &finalTicks );
  63.                 HiliteControl( (ControlHandle)itemHandle, 0 );
  64.                 *itemHit = ok;
  65.                 return( true );
  66.             }
  67.             else
  68.             {
  69.                     /*        Only allow numbers to be entered            */
  70.                 if ( !isdigit( key ) && !iscntrl( key ) && key != '-')
  71.                 {
  72.                     SysBeep( 10 );
  73.                     *itemHit = kTextItem;
  74.                     return( true );
  75.                 }
  76.             }
  77.             break;
  78.     }
  79.     return( false );
  80. }
  81.  
  82. pascal void ArrowActionProc( ControlHandle theControl, short part )
  83. {
  84.     long            theValue;
  85.     Str255        theString;
  86.     DialogPtr        theDialog;
  87.     Rect            iRect;
  88.     Handle        iHandle;
  89.     short        iType;
  90.     static long        delay = kScrollDelay;
  91.     static long        endTicks = 0;
  92.     static short    oldDir = kPlainArrow;
  93.  
  94.     if ( endTicks < TickCount() && part != kPlainArrow )
  95.     {
  96.         theValue = GetCtlValue( theControl );
  97.         theDialog = (DialogPtr)(*theControl )->contrlOwner;
  98.         GetDItem(theDialog, kTextItem, &iType, &iHandle, &iRect);
  99.         if ( part == inUpButton )            /*    pressed up arrow, increment theValue    */
  100.         {
  101.             if ( theValue < GetCtlMax( theControl ) )
  102.                 theValue++;
  103.             if ( oldDir != kUpArrow )        /*    remember which part was pressed last    */
  104.             {
  105.                 oldDir = kUpArrow;
  106.                 delay = kScrollDelay;
  107.             }
  108.         }
  109.         else if ( part == inDownButton )        /*    pressed down arrow, decrement theValue    */
  110.         {
  111.             if ( theValue > GetCtlMin( theControl ) )
  112.                 theValue--;
  113.             if ( oldDir != kDownArrow )    /*    remember which part was pressed last    */
  114.             {
  115.                 oldDir = kDownArrow;
  116.                 delay = kScrollDelay;
  117.             }
  118.         }
  119.             /*    Set the new value, display it, and hilite it.        */
  120.         SetCtlValue( theControl, theValue );
  121.         NumToString( theValue, theString );
  122.         SetIText(iHandle, theString);
  123.         SelIText(theDialog, kTextItem, 0, 32767);
  124.         
  125.         endTicks = TickCount() + delay;
  126.         if ( delay > 0 )
  127.         {
  128.             delay--;
  129.         }
  130.     }
  131. }
  132.             
  133. Point     PositionDialog( DialogPtr theDialog, Boolean move )
  134. {
  135.     short    width;        /* width of theDialog */
  136.     short    height;        /* height of theDialog */
  137.     Point        newLoc;        /* new location of theDialog */
  138.  
  139.     width = theDialog->portRect.right - theDialog->portRect.left;
  140.     height = theDialog->portRect.bottom - theDialog->portRect.top;
  141.     newLoc.h = ( screenBits.bounds.right - width ) / 2;
  142.     newLoc.v = ( screenBits.bounds.bottom - GetMBarHeight() - height ) / 3
  143.                 + GetMBarHeight();
  144.  
  145.     if ( move )    /*    if we're supposed to move the dialog, do so    */
  146.     {
  147.         MoveWindow( theDialog, newLoc.h, newLoc.v, true );
  148.     }
  149. }
  150.  
  151. pascal void ButtonOutline( DialogPtr theDialog, short itemID )
  152. {
  153.     short         itemType;            /* The type of the item in GetDItem */
  154.     Handle        theItem;            /* handle to the item */
  155.     Rect            itemBox;            /* the box containing the item */
  156.     PenState        savedPenState;        /* old pen state resored on exit */
  157.     GrafPtr        origPort;            /* the current port saved. restored on exit */
  158.     
  159.     GetPort( &origPort );    /* save the old port */
  160.     SetPort( theDialog );
  161.  
  162.     GetDItem( theDialog, itemID, &itemType, &theItem, &itemBox );
  163.  
  164.         /*        Draw the button outline    */
  165.     GetPenState( &savedPenState );
  166.     PenNormal();
  167.     PenSize( 3, 3 );
  168.     FrameRoundRect( &itemBox, 16, 16 );
  169.  
  170.         /*        Restore the old settings    */
  171.     SetPenState( &savedPenState );
  172.     SetPort( origPort );
  173. }
  174.  
  175. main()
  176. {
  177.     GrafPtr            savePort;        /*    The old graphics port            */
  178.     DialogPtr            theDialog;        /*    The dialog to display            */
  179.     short            itemHit;        /*    Last item hit in the dialog        */
  180.     short            itemType;        /*    The type of item            */
  181.     Handle            itemHandle;    /*    handle to the item            */
  182.     Rect                itemRect;        /*    an item's Rect                */
  183.     Str255            theString;        /*    misc. pascal string            */
  184.     long                theValue;        /*    misc value.                */
  185.     Boolean            done;        /*    is the program done yet?        */
  186.     
  187.     ToolBoxInit();
  188.  
  189.     GetPort( &savePort );
  190.     
  191.     theDialog = GetNewDialog( kBaseResID, nil, kMoveToFront );
  192.     if ( theDialog == NULL )
  193.     {
  194.         SysBeep( 10 );
  195.         ExitToShell();
  196.     }
  197.  
  198.         /*        Center the dialog, select it, and make it  the current graphics port.    */
  199.     PositionDialog( theDialog, true );
  200.     SelectWindow( theDialog );
  201.     SetPort( theDialog );
  202.  
  203.         /*        Give the arrow control an action procedure    */
  204.     GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  205.     SetCtlAction( (ControlHandle) itemHandle, ArrowActionProc );
  206.     
  207.         /*        Install a routine to draw the default button outline to a user item.    */
  208.     GetDItem( theDialog, kOKOutline, &itemType, &itemHandle, &itemRect );
  209.     SetDItem( theDialog, kOKOutline, itemType, &ButtonOutline, &itemRect );
  210.  
  211.         /*        Make the arrow activator initially active.        */
  212.     GetDItem( theDialog, kActiveItem, &itemType, &itemHandle, &itemRect );
  213.     SetCtlValue( (ControlHandle)itemHandle, 1 );
  214.     
  215.         /*        Done with initializations.
  216.                 Show the dialog and call ModalDialog until OK is pressed.    */
  217.     ShowWindow( theDialog );
  218.     done = false;
  219.     while( !done )
  220.     {
  221.         ModalDialog( DialogFilter, &itemHit );
  222.         switch ( itemHit )
  223.         {
  224.             case ok:                /*    OK button pressed...exit the program    .        */
  225.                 done = true;
  226.                 break;
  227.             case kTextItem:        /*    Text entered, get the new value            */
  228.                 GetDItem( theDialog, kTextItem, &itemType, &itemHandle, &itemRect );
  229.                 GetIText( itemHandle, theString );
  230.                 StringToNum( theString, &theValue );
  231.                 GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  232.                 SetCtlValue( (ControlHandle)itemHandle, theValue );
  233.                 break;
  234.             case kActiveItem:        /*    Change the active status of the arrow control    */
  235.                 GetDItem( theDialog, kActiveItem, &itemType, &itemHandle, &itemRect );
  236.                 theValue = 1 - GetCtlValue( (ControlHandle)itemHandle );
  237.                 SetCtlValue( (ControlHandle)itemHandle, theValue );
  238.                 GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  239.                 HiliteControl( (ControlHandle)itemHandle, (theValue) ? 0 : 255 );
  240.                 break;
  241.             default:
  242.                 break;
  243.         }
  244.     }
  245.     
  246.     DisposDialog( theDialog );
  247.     SetPort( savePort );
  248.     ExitToShell();
  249. }