home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / Text Processing / HexEdit Release.sit / HexEdit Release / Project / Source / UtilFuncs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-30  |  9.2 KB  |  414 lines  |  [TEXT/CWIE]

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is Copyright 1993 Jim Bumgardner.
  13.  * 
  14.  * The Initial Developer of the Original Code is Jim Bumgardner
  15.  * Portions created by Lane Roathe are
  16.  * Copyright (C) Copyright © 1996-2002.
  17.  * All Rights Reserved.
  18.  *
  19.  * Modified: $Date: 2004/09/09 00:23:30 $
  20.  * Revision: $Id: UtilFuncs.c,v 1.13 2004/09/09 00:23:30 raving Exp $
  21.  *
  22.  * Contributor(s):
  23.  *        Lane Roathe
  24.  *        Nick Shanks
  25.  */
  26.  
  27. // 05/10/01 - GAB: MPW environment support
  28. #ifdef __MPW__
  29. #include "MPWIncludes.h"
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #if __MWERKS__
  34.     #define DEBUG 1
  35.     #include <debugging.h>    // LR: v1.6.5, via Max Horn - safe on systems w/o debugger
  36. #else
  37. //    #include <CarbonCore/Debugging.h>
  38. #endif
  39.  
  40. #include "Utility.h"
  41.  
  42. /*** SET CONTROL ***/
  43. void SetControl( DialogPtr dialog, short item, short value )
  44. {
  45.     Handle handle;
  46.     short type;
  47.     Rect r;
  48.  
  49.     GetDialogItem( dialog, item, &type, &handle, &r );
  50.     SetControlValue( ( ControlHandle ) handle, value );
  51. }
  52.  
  53. // Copy a pascal string to a dialog's text item ( static text or edit text )
  54.  
  55. /*** SET TEXT ***/
  56. void SetText( DialogPtr dialog, short item, StringPtr text )
  57. {
  58.     Handle    handle;
  59.     short    type;
  60.     Rect    r;
  61.  
  62.     GetDialogItem( dialog, item, &type, &handle, &r );
  63.     SetDialogItemText( handle, text );
  64. }
  65.  
  66. // Copy a dialog's text item to a pascal string.
  67.  
  68. /*** GET TEXT ***/
  69. void GetText( DialogPtr dialog, short item, StringPtr text )
  70. {
  71.     Handle    handle;
  72.     short    type;
  73.     Rect    r;
  74.  
  75.     GetDialogItem( dialog, item, &type, &handle, &r );
  76.     GetDialogItemText( handle, (StringPtr) text );
  77. }
  78.  
  79. /*** GET RECT ***/
  80. void GetRect( DialogPtr dialog, short item, Rect *r )
  81. {
  82.     Handle    handle;
  83.     short    type;
  84.  
  85.     GetDialogItem( dialog, item, &type, &handle, r );
  86. }
  87.  
  88. /*** SET DRAW PROC ***/
  89. void SetDraw( DialogPtr dialog, short item, Handle proc )
  90. {
  91.     Handle    handle;
  92.     short    type;
  93.     Rect    r;
  94.  
  95.     GetDialogItem( dialog, item, &type, &handle, &r );
  96.     SetDialogItem( dialog, item, type, proc, &r );
  97. }
  98.  
  99. /*** DISABLE BUTTON ***/
  100. void DisableButton( DialogPtr dialog, short bid )
  101. {
  102.     Handle    h;
  103.     short    t;
  104.     Rect    r;
  105.  
  106. // LR: v1.6.5,    SetDialogDefaultItem( dialog, bid );
  107.     GetDialogItem( dialog, bid, &t, &h, &r );
  108.     HiliteControl( ( ControlHandle ) h, kControlDisabledPart );
  109. }
  110.  
  111. /*** ENABLE BUTTON ***/
  112. void EnableButton( DialogPtr dialog, short bid )
  113. {
  114.     Handle    h;
  115.     short    t;
  116.     Rect    r;
  117.  
  118. // LR: v1.6.5,    SetDialogDefaultItem( dialog, bid );
  119.     GetDialogItem( dialog, bid, &t, &h, &r );
  120.     HiliteControl( ( ControlHandle ) h, kControlNoPart );
  121. }
  122.  
  123. /*    *** Check for an abort keypress (ESC or Command .) ***
  124.  
  125.     LR: 1.66 - added to allow any routine to check for an abort keypress.
  126.  
  127.     EXIT: true == user pressed abort key(s)
  128. */
  129.  
  130. Boolean CheckForAbort( void )
  131. {
  132.     EventRecord event;
  133.  
  134.     if( EventAvail( keyDownMask, &event ) )    // GetNextEvent will NOT work (doesn't see the key press!)
  135.     {
  136.         if( keyDown == event.what )        // should always be true, but ...
  137.         {
  138.             do    //LR 186 -- check avail and all other events for abort press (other no longer worked!)
  139.             {
  140.                 char c = (event.message & charCodeMask);
  141.  
  142.                 // We allow both ESC and Cmd-'.' to abort
  143.             if( 27 == c || ('.' == c && (event.modifiers & cmdKey)) )
  144.                 {
  145.                     FlushEvents( everyEvent, NULL );    // don't leave any events on queue if aborting!
  146.                     return( true );
  147.                 }
  148.             }while( GetNextEvent( everyEvent, &event ) );
  149.         }
  150.     }
  151.     return( false );
  152. }
  153.  
  154. // Simulate the user pressing a button.    This is used to give the user some
  155. // visual feedback when they use the keyboard as a shortcut to press a dialog button.
  156.  
  157. /*** SIMULATE BUTTON PRESS ***/
  158. void SimulateButtonPress( DialogPtr dialog, short bid )
  159. {
  160.     Handle    h;
  161.     short    t;
  162.     Rect    r;
  163.     unsigned long delay;
  164.     GrafPtr    gp;
  165.  
  166.     GetPort( &gp );
  167.     SetPortDialogPort( dialog );
  168.  
  169.     GetDialogItem( dialog, bid, &t, &h, &r );
  170.     HiliteControl( ( ControlHandle ) h, kControlButtonPart );
  171.     Delay( 10, &delay );
  172.     HiliteControl( ( ControlHandle ) h, kControlNoPart );
  173.     SetPort( gp );
  174. }
  175.  
  176. /*** ERROR ALERT ***/
  177. // LR: v1.6.5, modified to accept STR# index for localization
  178. short ErrorAlert( short severity, short strid, ... )
  179. {
  180.     char     tbuf[256], str[256];
  181.     short    itemHit;
  182.     va_list args;
  183.     AlertType inAlertType;
  184.  
  185.     GetIndString( (StringPtr)str, strError, strid );
  186.     CopyPascalStringToC( (StringPtr)str, str );
  187.  
  188.     va_start( args, strid );
  189.     vsprintf( tbuf, str, args );
  190.     va_end( args );
  191.  
  192.     CopyCStringToPascal( tbuf, (StringPtr)tbuf );
  193.  
  194.     if (g.appearanceAvailable)
  195.     {
  196.         AlertStdAlertParamRec params;
  197.  
  198.         switch (severity)
  199.         {
  200.         case ES_Note:
  201.             inAlertType  = kAlertNoteAlert;
  202.             break;
  203.  
  204.         case ES_Caution:
  205.             inAlertType  = kAlertCautionAlert;
  206.             break;
  207.  
  208.         case ES_Stop:
  209.         case ES_Fatal:
  210.         default:
  211.             inAlertType  = kAlertStopAlert;
  212.             break;
  213.         }
  214.  
  215.         //LR 180 -- I want alerts to belong to the error window!
  216.         memset( ¶ms, 0, sizeof(params) );
  217.         params.defaultText = "\pBummer!";
  218.         params.defaultButton = ok;
  219.         params.position = kWindowAlertPositionParentWindow;
  220.         
  221.         StandardAlert( inAlertType, (StringPtr)tbuf, NULL, ¶ms, &itemHit );
  222.     }
  223.     else
  224.     {
  225.         ParamText( (StringPtr) tbuf, NULL, NULL, NULL );
  226.         InitCursor();
  227.         switch( severity )
  228.         {
  229.         case ES_Note:
  230.             itemHit = NoteAlert( alertError, NULL );
  231.             break;
  232.             
  233.         case ES_Caution:
  234.             itemHit = CautionAlert( alertError, NULL );
  235.             break;
  236.  
  237.         case ES_Stop:
  238.         case ES_Fatal:
  239.             itemHit = StopAlert( alertError, NULL );
  240.             break;
  241.         }
  242.     }
  243.  
  244.     // LR: v1.6.5, -- No "Debug" buttons, but OPTION on any goes into debugger now!
  245.     {
  246.         KeyMap keys;
  247.  
  248.         GetKeys( keys );
  249.         if( keys[1] & (1<<2) )
  250.  
  251. // 05/10/01 - GAB: DEBUGSTR not defined for non-Carbon builds
  252. #if __MWERKS__ && TARGET_API_MAC_CARBON
  253.             debug_string( (StringPtr) tbuf );    //LR -- 1.76 DEBUGSTR is out, this is now in. Sigh.
  254. //            DEBUGSTR( (StringPtr) tbuf );
  255. #else
  256.             DebugStr( (StringPtr) tbuf );
  257. #endif
  258.     }
  259.  
  260.     if(  severity == ES_Fatal )
  261.         ExitToShell();
  262.     
  263.     return itemHit;
  264. }
  265.  
  266. /*** OS ERROR ALERT ***/
  267. /* LR: v1.6.5, removed due to redundancy and localization
  268. short OSErrorAlert( short severity, short strid, short error )
  269. {
  270.     Str255 s1, s2;
  271.  
  272.  
  273.     GetIndString( s, strError, errOS );
  274.     return ErrorAlert( severity, s, strid, error );
  275. }
  276. */
  277.  
  278. /*** MY RANDOM ***/
  279. short MyRandom( short limit )
  280. {
  281. /*
  282.     unsigned long r;
  283.     r = ( unsigned ) Random();
  284.     r = ( r* (long) limit )/65536L;
  285.     return (short) r;
  286. */
  287.     return Random() % limit;
  288. }
  289.  
  290. /*** MY SET CURSOR ***/
  291. void MySetCursor( short n )
  292. {
  293.     static short lastN=-1;
  294.     if( n != lastN )
  295.     {
  296.         lastN = n;
  297.         switch( n )
  298.         {
  299. #if TARGET_API_MAC_CARBON
  300.             case C_Arrow:
  301.             {
  302.                 Cursor arrow;
  303.  
  304.                 GetQDGlobalsArrow( &arrow );
  305.                 SetCursor( &arrow );
  306.                 break;
  307.             }
  308. #else
  309.             case C_Arrow:    SetCursor( &qd.arrow );        break;    // LR: qd.
  310. #endif
  311.             // NS: v1.5, changed Curs to CursHandle
  312.             case C_Watch:    SetCursor( *g.watch );        break;
  313.             case C_IBeam:    SetCursor( *g.iBeam );        break;
  314.         }
  315.     }
  316. }
  317.  
  318. //#if false    // NS: v1.6.6, now in CarbonAccessors.o            !TARGET_API_MAC_CARBON
  319. #if !TARGET_API_MAC_CARBON    //LR -- not if you compile all the projects it's not!!!
  320.  
  321. /*** COPY PASCAL STRING TO C ***/
  322. void CopyPascalStringToC( ConstStr255Param source, char* dest )
  323. {
  324.     if( source != NULL )
  325.     {
  326.         SInt16 length  = *source++;
  327.         while (length > 0) 
  328.         {
  329.             *dest++ = *(char*) source++;
  330.             --length;
  331.         }
  332.     }
  333.     *dest = '\0';
  334. }
  335.  
  336. //     CopyCStringToPascal converts the source C string to a destination
  337. //     pascal string as it copies. The dest string will
  338. //     be truncated to fit into an Str255 if necessary.
  339. //  If the C String pointer is NULL,the pascal string's length is set to zero
  340.  
  341. /*** COPY C STRING TO PASCAL ***/
  342. void CopyCStringToPascal( const char* source, Str255 dest )
  343. {
  344.     SInt16     length  = 0;
  345.     
  346.     // handle case of overlapping strings
  347.     if( (void*) source == (void*) dest)
  348.     {
  349.         unsigned char*        curdst = &dest[1];
  350.         unsigned char        thisChar;
  351.                 
  352.         thisChar = *(const unsigned char*) source++;
  353.         while (thisChar != '\0') 
  354.         {
  355.             unsigned char nextChar;
  356.             
  357.             // use nextChar so we don't overwrite what we are about to read
  358.             nextChar = *(const unsigned char*) source++;
  359.             *curdst++ = thisChar;
  360.             thisChar = nextChar;
  361.             
  362.             if (++length >= 255)
  363.                 break;
  364.         }
  365.     }
  366.     else if (source != NULL)
  367.     {
  368.         unsigned char*        curdst = &dest[1];
  369.         SInt16                 overflow = 255;        // count down so test it loop is faster
  370.         register char        temp;
  371.     
  372.         // Can't do the K&R C thing of “while (*s++ = *t++)” because it will copy trailing zero
  373.         // which might overrun pascal buffer.  Instead we use a temp variable.
  374.         while( (temp = *source++) != 0 ) 
  375.         {
  376.             *(char*) curdst++ = temp;
  377.                 
  378.             if (--overflow <= 0)
  379.                 break;
  380.         }
  381.         length = 255 - overflow;
  382.     }
  383.     dest[0] = length;
  384. }
  385.  
  386. #endif
  387.  
  388. /*** EQUAL PASCAL STRINGS ***/
  389. Boolean EqualPStrings( UInt8 *source, UInt8 *dest )
  390. {
  391.     UInt8 length = *source, count;
  392.     for( count = 0; count <= length; count++ )
  393.         if( *source++ != *dest++ ) return false;
  394.     return true;
  395. }
  396.  
  397. #ifndef __MC68K__
  398.  
  399. /*** LAUNCH WEB BROWSER ***/
  400. OSStatus LaunchURL( StringPtr url )
  401. {
  402.     OSStatus error = noErr;
  403.  
  404.     if( ICLaunchURL )    //LR weak linked!
  405.     {
  406.         long start = 1, end = 1, length = (long)url[0];
  407.  
  408.         error = ICLaunchURL( g.icRef, NULL, url+1, length, &start, &end );
  409.     }
  410.     return error;
  411. }
  412.  
  413. #endif
  414.