home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / commodity / newedit18b / newedit_source.lha / Hook.c < prev    next >
C/C++ Source or Header  |  1994-05-30  |  9KB  |  280 lines

  1. #include <ctype.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <devices/inputevent.h>
  6. #include <exec/memory.h>
  7. #include <exec/semaphores.h>
  8. #include <intuition/intuition.h>
  9. #include <intuition/sghooks.h>
  10.  
  11. #include <clib/exec_protos.h>
  12. #include <clib/utility_protos.h>
  13.  
  14. #include <pragmas/exec_pragmas.h>
  15. #include <pragmas/utility_pragmas.h>
  16.  
  17. extern ULONG read_clip( UBYTE *, ULONG );
  18. extern void write_clip( UBYTE *, ULONG );
  19. extern char *IncrementNum( char *target, char *string );
  20. extern LONG next_word( UBYTE *buffer, LONG pos, LONG max );
  21. extern LONG prev_word( UBYTE *buffer, LONG pos );
  22. extern void ToggleCase( UBYTE *character );
  23.  
  24. #define IECODE_UNUSED 0
  25. #define IECODE_C 51
  26. #define IECODE_G 36
  27. #define IECODE_V 52
  28. #define IECODE_I 23
  29. #define IECODE_RETURN 68
  30. #define IECODE_ESC 69
  31. #define IECODE_BACKSP 65
  32. #define IECODE_DEL 70
  33. #define IECODE_CRSRUP 76
  34. #define IECODE_CRSRDWN 77
  35. #define IECODE_CRSRRIGHT 78
  36. #define IECODE_CRSRLEFT 79
  37. #define IECODE_HELP 95
  38. #ifndef SGA_NEXTACTIVE
  39.     #define SGA_NEXTACTIVE (0x20L) /* Make next possible gadget active */
  40.     #define SGA_PREVACTIVE (0x40L) /* Make previous possible gadget active */
  41. #endif
  42.  
  43. extern struct Library *SysBase, *DOSBase, *UtilityBase;
  44. extern struct Hook *OldHook;
  45. extern char *SemaphoreName;
  46. extern struct SignalSemaphore *Sem;
  47. extern BOOL Disabled;
  48.  
  49. /****** string_hook() *******************************************************
  50. *
  51. *   NAME
  52. *        string_hook -- general EditHook for Gadtools Stringgadgets
  53. *   SYNOPSIS
  54. *        Known = string_hook ( Hook, Object, Message )
  55. *        D0                    A0    A2      A1
  56. *        ULONG   string_hook ( struct Hook *, struct SGWork *, APTR * );
  57. *   FUNCTION
  58. *        Normally set as new global Hook with SetEditHook().
  59. *        The following functions will be supported:
  60. *        SHIFT CURSOR RIGHT  next word
  61. *        SHIFT CURSOR LEFT   previous word
  62. *        ALT BACKSPACE       delete previous word
  63. *        ALT DEL             delete next word
  64. *        RALT CURSOR UP      go to prev gadget (with GADGETUP)
  65. *        RALT CURSOR DOWN    go to next gadget (with GADGETUP)
  66. *        ESC                 leave gadget (with GADGETUP)
  67. *        RCommand C          copy gadget contents to clipboard 0
  68. *        RCommand V          paste contents of clip 0 to gadget
  69. *        sgw->EditOp will be set to EO_BIGCHANGE, if some text has been
  70. *        deleted or inserted from the clipboard.
  71. *
  72. *  Added by Paul Huxham, 4th February 1994:
  73. *        RCommand I          Increments a numbered filename by one
  74. *        RALT I              Increments a numbered filename by one
  75. *
  76. *  Added by Paul Huxham, 29th May 1994:
  77. *        RALT C              copy gadget contents to clipboard 0
  78. *        RALT V              paste contents of clip 0 to gadget
  79. *        RALT G              toggle the case of selected character
  80. *        RCommand G          toggle the case of selected character
  81. *        SHIFT RALT G        toggle the case of characters until EOL or
  82. *                            non alphabetic character is found
  83. *        SHIFT RCommand G    toggle the case of characters until EOL or
  84. *                            non alphabetic character is found
  85. *
  86. *        sgw->IEvent->ie_Code will be modified to 0x00 if we moved to the
  87. *        next/previous word/gadget or if AMIGA-C or AMIGA-V was pressed. The
  88. *        keycode 0 is unused by the system and therefore will be ignored by
  89. *        further stringhooks.
  90. *
  91. *   INPUTS
  92. *        Hook   - pointer to own hook-structure
  93. *        Object - struct SGWork for Stringgadgets
  94. *        Message- ???
  95. *
  96. *   RESULT
  97. *        ~0 - command supported, else 0
  98. *        sgw->EditOp and sgw->IEvent->ie_Code may be motified (see above)
  99. *
  100. ***************************************************************************/
  101. ULONG __saveds __asm  string_hook( register __a0 struct Hook   *hook,
  102.                                    register __a2 struct SGWork *sgw,
  103.                                    register __a1 unsigned long *msg  )
  104. {
  105.     struct InputEvent *ie;
  106.     ULONG length;
  107.     ULONG return_code = ~0;
  108.  
  109.     ObtainSemaphoreShared( Sem );
  110.  
  111.     if ( *msg == SGH_KEY )
  112.     {
  113.         /* If Commodities says we are enabled */
  114.         if ( !Disabled )
  115.         {
  116.             ie = sgw->IEvent;
  117.  
  118.             if ( ie->ie_Class == IECLASS_RAWKEY )
  119.             {
  120.                 switch ( ie->ie_Code )
  121.                 {
  122.                     case IECODE_ESC:
  123.                         sgw->Actions |= SGA_END;
  124.                     break;
  125.  
  126.                     case IECODE_CRSRUP:
  127.                         if ( ie->ie_Qualifier & IEQUALIFIER_RALT )
  128.                         {
  129.                             sgw->Actions |= SGA_PREVACTIVE | SGA_END;
  130.                             ie->ie_Code = IECODE_UNUSED;
  131.                         }
  132.                     break;
  133.  
  134.                     case IECODE_CRSRDWN:
  135.                         if ( ie->ie_Qualifier & IEQUALIFIER_RALT )
  136.                         {
  137.                             sgw->Actions |= SGA_NEXTACTIVE | SGA_END;
  138.                             ie->ie_Code = IECODE_UNUSED;
  139.                         }
  140.                     break;
  141.  
  142.                     case IECODE_CRSRRIGHT:
  143.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_LALT | IEQUALIFIER_RALT ) )
  144.                         {
  145.                             sgw->BufferPos = next_word( sgw->WorkBuffer, sgw->BufferPos, sgw->StringInfo->NumChars );
  146.                             ie->ie_Code = IECODE_UNUSED;
  147.                             sgw->Actions |= SGA_REDISPLAY;
  148.                         }
  149.                     break;
  150.  
  151.                     case IECODE_CRSRLEFT:
  152.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_LALT | IEQUALIFIER_RALT ) )
  153.                         {
  154.                           sgw->BufferPos= prev_word( sgw->WorkBuffer, sgw->BufferPos );
  155.                             ie->ie_Code = IECODE_UNUSED;
  156.                             sgw->Actions |= SGA_REDISPLAY;
  157.                         }
  158.                     break;
  159.  
  160.                     case IECODE_BACKSP:
  161.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_LALT | IEQUALIFIER_RALT ) )
  162.                         {
  163.                           sgw->BufferPos = prev_word( sgw->WorkBuffer, sgw->BufferPos ) + 1;
  164.  
  165.                             if ( sgw->BufferPos < sgw->StringInfo->BufferPos )
  166.                                 strcpy( &sgw->WorkBuffer[ sgw->BufferPos ], &sgw->WorkBuffer[ sgw->StringInfo->BufferPos ] );
  167.  
  168.                             sgw->Actions |= SGA_REDISPLAY;
  169.                             sgw->EditOp = EO_BIGCHANGE;
  170.                         }
  171.                     break;
  172.  
  173.                     case IECODE_DEL:
  174.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_LALT | IEQUALIFIER_RALT ) )
  175.                         {
  176.                             length = next_word( sgw->WorkBuffer, sgw->BufferPos, sgw->StringInfo->NumChars );
  177.                             strcpy ( &sgw->WorkBuffer[ sgw->BufferPos + 1 ], &sgw->WorkBuffer[ length ] );
  178.  
  179.                             sgw->Actions |= SGA_REDISPLAY;
  180.                             sgw->EditOp = EO_BIGCHANGE;
  181.                         }
  182.                     break;
  183.  
  184.                     case IECODE_C:
  185.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_RALT | IEQUALIFIER_RCOMMAND ) )
  186.                         {
  187.                             write_clip( sgw->StringInfo->Buffer, sgw->NumChars );
  188.                             sgw->Code = IECODE_UNUSED;
  189.                         }
  190.                     break;
  191.  
  192.                     case IECODE_G:
  193.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_RALT | IEQUALIFIER_RCOMMAND ) )
  194.                         {
  195.                             if ( ie->ie_Qualifier & ( IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT ) )
  196.                             {
  197.                                 while( isalpha( sgw->WorkBuffer[ sgw->BufferPos ] ) != 0 )
  198.                                 {
  199.                                   ToggleCase( &sgw->WorkBuffer[ sgw->BufferPos ] );
  200.                                     sgw->BufferPos++;
  201.                                 }
  202.  
  203.                                 sgw->Actions |= SGA_REDISPLAY;
  204.                                 sgw->Code = IECODE_UNUSED;
  205.                             }
  206.                             else
  207.                             {
  208.                               ToggleCase( &sgw->WorkBuffer[ sgw->BufferPos ] );
  209.                                 sgw->BufferPos++;
  210.  
  211.                                 sgw->Actions |= SGA_REDISPLAY;
  212.                                 sgw->Code = IECODE_UNUSED;
  213.                             }
  214.                         }
  215.                     break;
  216.  
  217.                     case IECODE_I:
  218.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_RALT | IEQUALIFIER_RCOMMAND ) )
  219.                         {
  220.                             char *newstr;
  221.  
  222.                             newstr = AllocMem( 200, MEMF_PUBLIC | MEMF_CLEAR );
  223.                             if ( newstr != NULL )
  224.                             {
  225.                                 if ( IncrementNum( newstr, sgw->StringInfo->Buffer ) != NULL )
  226.                                 {
  227.                                     strcpy( sgw->WorkBuffer, newstr );
  228.  
  229.                                     sgw->BufferPos = strlen( newstr );
  230.                                     sgw->NumChars = strlen( newstr );
  231.                                 }
  232.                                 else sgw->Actions |= SGA_BEEP;
  233.  
  234.                                 sgw->EditOp = EO_BIGCHANGE;
  235.                                 sgw->Code = IECODE_UNUSED;
  236.  
  237.                                 FreeMem( newstr, 200 );
  238.                             }
  239.                         }
  240.                     break;
  241.  
  242.                     case IECODE_V:
  243.                         if ( ie->ie_Qualifier & ( IEQUALIFIER_RALT | IEQUALIFIER_RCOMMAND ) )
  244.                         {
  245.                             WORD maxchars, pos;
  246.  
  247.                             maxchars = sgw->StringInfo->MaxChars - 1;
  248.                             pos = sgw->NumChars;
  249.  
  250.                             while ( --pos >= sgw->BufferPos )
  251.                                 sgw->WorkBuffer[ maxchars - sgw->NumChars + pos ] = sgw->WorkBuffer[ pos ];
  252.  
  253.                             length = read_clip( &sgw->WorkBuffer[ sgw->BufferPos ], maxchars - sgw->NumChars );
  254.  
  255.                             if ( length > 0 )
  256.                             {
  257.                                 strcpy( &sgw->WorkBuffer[ sgw->BufferPos + length ],
  258.                                     &sgw->WorkBuffer[ maxchars - sgw->NumChars + sgw->BufferPos ] );
  259.  
  260.                                 sgw->BufferPos += length;
  261.                                 sgw->NumChars += length;
  262.                             }
  263.                             else sgw->Actions |= SGA_BEEP;
  264.  
  265.                             sgw->EditOp = EO_BIGCHANGE;
  266.                             sgw->Code = IECODE_UNUSED;
  267.                         }
  268.                     break;
  269.                 }
  270.             }
  271.         }
  272.         CallHookPkt( OldHook, (APTR)sgw, msg );
  273.     }
  274.     else return_code = 0;
  275.  
  276.     ReleaseSemaphore( Sem );
  277.  
  278.     return return_code;
  279. }
  280.