home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / CBMDevKit3.dms / CBMDevKit3.adf / asl / asltester / edithook.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  2.9 KB  |  125 lines

  1.  
  2. /*
  3. COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1993
  4. Commodore-Amiga, Inc.  All rights reserved.
  5.  
  6. DISCLAIMER: This software is provided "as is".  No representations or
  7. warranties are made with respect to the accuracy, reliability, performance,
  8. currentness, or operation of this software, and all use is at your own risk.
  9. Neither commodore nor the authors assume any responsibility or liability
  10. whatsoever with respect to your use of this software.
  11. */
  12.  
  13.  
  14. /****************************************************************************/
  15.  
  16.  
  17. #include <exec/types.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/gadgetclass.h>
  20. #include <intuition/sghooks.h>
  21. #include <devices/inputevent.h>
  22. #include <utility/hooks.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25.  
  26. #include <clib/utility_protos.h>
  27.  
  28. #include <pragmas/utility_pragmas.h>
  29.  
  30. #include "edithook.h"
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36. #define    RETURN     (0x44)
  37. #define    TAB     (0x42)
  38.  
  39.  
  40. /*****************************************************************************/
  41.  
  42.  
  43. extern struct Library *UtilityBase;
  44.  
  45.  
  46. /*****************************************************************************/
  47.  
  48.  
  49. /* Check whether the buffer only contains a valid hex number... */
  50. BOOL CheckHex(STRPTR buffer, ULONG *value)
  51. {
  52. char  ch;
  53. ULONG num;
  54. UWORD i;
  55.  
  56.     if ((buffer[0] == '0') && (buffer[1] == 'x'))
  57.     {
  58.         num = 0;
  59.         i   = 2;
  60.         while (buffer[i])
  61.         {
  62.             ch  = ToUpper(buffer[i]);
  63.             num = num * 16;
  64.             if ((ch >= 'A') && (ch <= 'F'))
  65.                 num += ch - 'A' + 10;
  66.             else if ((ch >= '0') && (ch <= '9'))
  67.                 num += ch - '0';
  68.             else
  69.                 return(FALSE);
  70.  
  71.             i++;
  72.         }
  73.         *value = num;
  74.         return(TRUE);
  75.     }
  76.  
  77.     return(FALSE);
  78. }
  79.  
  80.  
  81. /*****************************************************************************/
  82.  
  83.  
  84. /* Function used as a hexadecimal editing hook for a string gadget. Only
  85.  * allows hex numbers to be entered in a string gadget. The current value of
  86.  * the hex number is stored in the gadget's UserData field.
  87.  */
  88. ULONG __asm HexHook(register __a0 struct Hook *hook,
  89.                     register __a2 struct SGWork *sgw,
  90.                     register __a1 ULONG *msg)
  91. {
  92. BOOL  exiting = FALSE;
  93. ULONG result;
  94.  
  95.     geta4();
  96.  
  97.     result = 0;
  98.     if (*msg == SGH_KEY)
  99.     {
  100.         result = 1;
  101.         switch (sgw->IEvent->ie_Code)
  102.         {
  103.             case TAB   :
  104.             case RETURN: exiting = TRUE;
  105.         }
  106.  
  107.         if (CheckHex(sgw->WorkBuffer, (ULONG *)&sgw->Gadget->UserData))
  108.         {
  109.             if (exiting)
  110.             {
  111.                 sprintf(sgw->WorkBuffer,"0x%08lX",sgw->Gadget->UserData);
  112.                 sgw->NumChars  = strlen(sgw->WorkBuffer);
  113.                 sgw->BufferPos = 0;
  114.             }
  115.         }
  116.         else
  117.         {
  118.             sgw->EditOp  = EO_BADFORMAT;
  119.             sgw->Actions = SGA_BEEP;
  120.         }
  121.     }
  122.  
  123.     return (result);
  124. }
  125.