home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / HexEdit 1.21 / ~Project / Source / UtilFuncs.c < prev   
Encoding:
C/C++ Source or Header  |  1997-08-03  |  3.5 KB  |  184 lines  |  [TEXT/CWIE]

  1. /*********************************************************************
  2.  * UtilFuncs.c
  3.  *
  4.  * HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *********************************************************************/
  7.  
  8. #include "HexEdit.h"
  9.  
  10. #include <stdio.h>
  11.  
  12. void SetControl(DialogPtr dial, int item, int value)
  13. {
  14.     Handle handle;
  15.     short type;
  16.     Rect r;
  17.  
  18.     GetDialogItem(dial,item,&type,&handle,&r);
  19.     SetControlValue((ControlHandle) handle,value);
  20. }
  21.  
  22. // Copy a pascal string to a dialog's text item (static text or edit text)
  23. //
  24. void SetText(DialogPtr dial, int item, StringPtr text)
  25. {
  26.     Handle    handle;
  27.     short    type;
  28.     Rect    r;
  29.  
  30.     GetDialogItem(dial,item,&type,&handle,&r);
  31.     SetDialogItemText(handle,text);
  32. }
  33.  
  34. // Copy a dialog's text item to a pascal string.
  35. //
  36. void GetText(DialogPtr dial, int item, StringPtr text)
  37. {
  38.     Handle    handle;
  39.     short    type;
  40.     Rect    r;
  41.  
  42.     GetDialogItem(dial,item,&type,&handle,&r);
  43.     GetDialogItemText(handle,(StringPtr) text);
  44. }
  45.  
  46. void MyOutlineButton(DialogPtr dp, int bid, Pattern pat)
  47. {
  48.     Handle    h;
  49.     short    t;
  50.     Rect    r;
  51.     GrafPtr    gp;
  52.  
  53.     GetPort(&gp);
  54.     SetPort(dp);
  55.     GetDialogItem(dp,bid,&t,&h,&r);
  56.     PenSize(3,3);
  57.     PenPat(&pat);    //LR :fix bad type coersion
  58.     InsetRect(&r,-4,-4);
  59.     FrameRoundRect(&r,16,16);
  60.     PenNormal();
  61.     SetPort(gp);
  62. }
  63.  
  64. // Gray out a disabled button
  65. //
  66. void MyDisableButton(DialogPtr dp, int bid)
  67. {
  68.     Handle    h;
  69.     short    t;
  70.     Rect    r;
  71.     GrafPtr    gp;
  72.     
  73.     MyOutlineButton(dp,bid,qd.white);    //LR :qd.
  74.     GetPort(&gp);
  75.     SetPort(dp);
  76.     GetDialogItem(dp,bid,&t,&h,&r);
  77.     HiliteControl((ControlHandle) h, 255);
  78.     PenPat(&qd.gray);    //LR :qd. & fix bad type coersion
  79.     PenMode(patBic);
  80.     PaintRect(&r);
  81.     PenNormal();
  82.     SetPort(gp);
  83. }
  84.  
  85. // Restore a previously disabled button
  86. //
  87. void MyEnableButton(DialogPtr dp, int bid)
  88. {
  89.     Handle    h;
  90.     short    t;
  91.     Rect    r;
  92.     GrafPtr    gp;
  93.     
  94.     GetPort(&gp);
  95.     SetPort(dp);
  96.     GetDialogItem(dp,bid,&t,&h,&r);
  97.     HiliteControl((ControlHandle) h,0);
  98.     InvalRect(&r);
  99.     SetPort(gp);
  100. }
  101.  
  102. // Simulate the user pressing a button.  This is used to give the user some
  103. // visual feedback when they use the keyboard as a shortcut to press a dialog button.
  104. //
  105. void MySimulateButtonPress(DialogPtr dp, int bid)
  106. {
  107.     Handle    h;
  108.     short    t;
  109.     Rect    r;
  110.     GrafPtr    gp;
  111.     long    dmy;
  112.  
  113.     GetPort(&gp);
  114.     SetPort(dp);
  115.     GetDialogItem(dp,bid,&t,&h,&r);
  116.     InvertRoundRect(&r,4,4);
  117.     Delay(10,&dmy);
  118.     InvertRoundRect(&r,4,4);
  119.     Delay(10,&dmy);
  120.     SetPort(gp);
  121. }
  122.  
  123. short ErrorAlert(short severity, char *str,...)
  124. {
  125.     char     tbuf[128];
  126.     short    itemHit;
  127.     va_list args;
  128.  
  129.     va_start(args,str);
  130.     vsprintf(tbuf,str,args);
  131.     va_end(args);
  132.     CtoPstr(tbuf);
  133.     ParamText((StringPtr) tbuf,"\p","\p","\p");
  134.     InitCursor();
  135.     switch (severity) {
  136.     case ES_Message:
  137.         itemHit = Alert(StdMessageALRT,NULL);
  138.         break;
  139.     case ES_Note:
  140.         itemHit = NoteAlert(StdErrorALRT,NULL);
  141.         break;
  142.     case ES_Caution:
  143.         itemHit = CautionAlert(StdErrorALRT,NULL);
  144.         break;
  145.     case ES_Stop:
  146.         itemHit = StopAlert(StdErrorALRT,NULL);
  147.         break;
  148.     }
  149.  
  150.     if (itemHit == 2)
  151.         DebugStr((StringPtr) tbuf);
  152.  
  153.     if (severity == ES_Stop)
  154.         ExitToShell();
  155.  
  156.     return itemHit;
  157. }
  158.  
  159. short OSErrorAlert(short severity, char *str, short oe)
  160. {
  161.     return ErrorAlert(severity, "OS Error in %s: %d", str, oe);
  162. }
  163.  
  164. short MyRandom(short limit)
  165. {
  166.     unsigned long r;
  167.     r = (unsigned) Random();
  168.     r = (r*(long)limit)/65536L;
  169.     return (short) r;
  170. }
  171.  
  172. void MySetCursor(short n)
  173. {
  174.     extern Cursor gWatchCursor,gIBeamCursor;
  175.     static short lastN=-1;
  176.     if (n != lastN) {
  177.         lastN = n;
  178.         switch (n) {
  179.         case C_Arrow:    SetCursor(&qd.arrow);            break;    //LR :qd.
  180.         case C_Watch:    SetCursor(&gWatchCursor);    break;
  181.         case C_IBeam:    SetCursor(&gIBeamCursor);    break;
  182.         }
  183.     }
  184. }