home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term23_2.lha / Source_Code / termSource / termStringHook.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  6KB  |  292 lines

  1. /*
  2. **    $Id: termStringHook.c,v 1.3 92/08/15 20:15:20 olsen Sta Locker: olsen $
  3. **    $Revision: 1.3 $
  4. **    $Date: 92/08/15 20:15:20 $
  5. **
  6. **    Custom string gadget editing hook routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* CommandKey(struct Hook *Hook,struct SGWork *Work,ULONG *Msg):
  15.      *
  16.      *    This routine is called whenever input passes through
  17.      *    a `term' string gadget. It releases the currently
  18.      *    active string gadget as soon as the Right-Amiga-key is
  19.      *    pressed (user is about to select a menu item).
  20.      */
  21.  
  22. VOID __saveds __asm
  23. CommandKey(register __a0 struct Hook *Hook,register __a1 ULONG *Msg,register __a2 struct SGWork *Work)
  24. {
  25.     if(Msg[0] == SGH_KEY)
  26.     {
  27.             /* Simple: activate the next gadget if
  28.              * return is pressed in the current one.
  29.              */
  30.  
  31.         if(Work -> Actions & SGA_END)
  32.         {
  33.             if(!(Work -> GadgetInfo -> gi_Window -> Flags & WFLG_RMBTRAP))
  34.                 Work -> Actions |= SGA_NEXTACTIVE;
  35.             else
  36.                 ActiveGadget = NULL;
  37.         }
  38.  
  39.             /* This looks like a built-in command. */
  40.  
  41.         if(Work -> IEvent -> ie_Qualifier & IEQUALIFIER_RCOMMAND)
  42.         {
  43.                 /* Amiga+C = Copy contents of string
  44.                  *           gadget to the clipboard.
  45.                  */
  46.  
  47.             if(Work -> Code == 'c')
  48.             {
  49.                 Work -> Actions &= ~SGA_USE;
  50.                 Work -> Actions |= SGA_BEEP;
  51.  
  52.                     /* Valid buffer contents? */
  53.  
  54.                 if(Work -> PrevBuffer[0])
  55.                 {
  56.                     struct MsgPort *ReplyPort;
  57.  
  58.                         /* Post a message... */
  59.  
  60.                     if(ReplyPort = CreateMsgPort())
  61.                     {
  62.                         struct Message ClipMessage;
  63.  
  64.                         ClipMessage . mn_Node . ln_Name    = Work -> PrevBuffer;
  65.                         ClipMessage . mn_ReplyPort    = ReplyPort;
  66.                         ClipMessage . mn_Length        = sizeof(struct Message);
  67.  
  68.                         PutMsg(ClipPort,&ClipMessage);
  69.  
  70.                         WaitPort(ReplyPort);
  71.  
  72.                         GetMsg(ReplyPort);
  73.  
  74.                         DeleteMsgPort(ReplyPort);
  75.  
  76.                         Work -> Actions &= ~SGA_BEEP;
  77.                     }
  78.                 }
  79.  
  80.                 return;
  81.             }
  82.  
  83.                 /* Amiga+V = Insert current clipboard
  84.                  *           contents at cursor position.
  85.                  */
  86.  
  87.             if(Work -> Code == 'v')
  88.             {
  89.                 Work -> Actions &= ~SGA_USE;
  90.                 Work -> Actions |= SGA_BEEP;
  91.  
  92.                     /* Don't paste to integer gadgets
  93.                      * (it could confuse Intuition if
  94.                      *  the buffer contained non-numeric
  95.                      *  symbols...).
  96.                      */
  97.  
  98.                 if(!(Work -> Gadget -> Activation & GACT_LONGINT))
  99.                 {
  100.                     struct MsgPort *ReplyPort;
  101.  
  102.                         /* Post a message... */
  103.  
  104.                     if(ReplyPort = CreateMsgPort())
  105.                     {
  106.                         STATIC UBYTE    Buffer[2048];
  107.                         struct Message    ClipMessage;
  108.  
  109.                         Buffer[0] = 0;
  110.  
  111.                         ClipMessage . mn_Node . ln_Name    = &Buffer[0];
  112.                         ClipMessage . mn_ReplyPort    = ReplyPort;
  113.                         ClipMessage . mn_Length        = sizeof(struct Message);
  114.  
  115.                         PutMsg(ClipPort,&ClipMessage);
  116.  
  117.                         WaitPort(ReplyPort);
  118.  
  119.                         GetMsg(ReplyPort);
  120.  
  121.                         DeleteMsgPort(ReplyPort);
  122.  
  123.                             /* Anything in the buffer? */
  124.  
  125.                         if(Buffer[0])
  126.                         {
  127.                             WORD Len = strlen(Buffer);
  128.  
  129.                                 /* Insert as many characters as we can. */
  130.  
  131.                             while(Len > 0 && Work -> NumChars + Len > Work -> StringInfo -> MaxChars)
  132.                                 Len--;
  133.  
  134.                                 /* Any characters left? */
  135.  
  136.                             if(Len > 0)
  137.                             {
  138.                                 STATIC UBYTE OtherBuffer[2048];
  139.  
  140.                                     /* Provide null-termination. */
  141.  
  142.                                 Buffer[Len] = 0;
  143.  
  144.                                     /* Set up undo buffer correctly. */
  145.  
  146.                                 if(Work -> StringInfo -> UndoBuffer)
  147.                                     strcpy(Work -> StringInfo -> UndoBuffer,Work -> PrevBuffer);
  148.  
  149.                                 Work -> StringInfo -> UndoPos = --Work -> BufferPos;
  150.  
  151.                                     /* Save the characters before the cursor. */
  152.  
  153.                                 if(Work -> BufferPos)
  154.                                     CopyMem(Work -> PrevBuffer,OtherBuffer,Work -> BufferPos);
  155.  
  156.                                     /* Provide null-termination. */
  157.  
  158.                                 OtherBuffer[Work -> BufferPos] = 0;
  159.  
  160.                                     /* Append the clipboard data. */
  161.  
  162.                                 strcat(OtherBuffer,Buffer);
  163.  
  164.                                     /* Append the characters following the cursor. */
  165.  
  166.                                 strcat(OtherBuffer,&Work -> PrevBuffer[Work -> BufferPos]);
  167.  
  168.                                     /* = new work buffer. */
  169.  
  170.                                 strcpy(Work -> WorkBuffer,OtherBuffer);
  171.  
  172.                                     /* Fiddle with some cursor data. */
  173.  
  174.                                 Work -> BufferPos += Len;
  175.                                 Work -> NumChars    += Len;
  176.  
  177.                                     /* And that's it... */
  178.  
  179.                                 Work -> Actions    |= SGA_USE;
  180.                                 Work -> EditOp     = EO_BIGCHANGE;
  181.  
  182.                                 Work -> Actions &= ~SGA_BEEP;
  183.                             }
  184.                         }
  185.                         else
  186.                             Work -> Actions &= ~SGA_BEEP;
  187.                     }
  188.                 }
  189.  
  190.                 return;
  191.             }
  192.         }
  193.  
  194.         if((Work -> IEvent -> ie_Qualifier & AMIGARIGHT) && Work -> IEvent -> ie_Code < 96)
  195.         {
  196.             if(!(Work -> IEvent -> ie_Qualifier & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)) && (Work -> IEvent -> ie_Code == KEYCODE_X || Work -> IEvent -> ie_Code == KEYCODE_Q))
  197.                 return;
  198.             else
  199.             {
  200.                 Work -> Actions |= (SGA_END|SGA_REUSE);
  201.                 Work -> Actions &= ~(SGA_USE|SGA_BEEP);
  202.  
  203.                 CommandWindow = Work -> GadgetInfo -> gi_Window;
  204.                 CommandGadget = Work -> Gadget;
  205.             }
  206.         }
  207.  
  208.             /* The user pressed the cursor-right key to
  209.              * move the cursor to the next word in the buffer.
  210.              */
  211.  
  212.         if(Work -> IEvent -> ie_Code == CURSORRIGHT && (Work -> IEvent -> ie_Qualifier & IEQUALIFIER_CONTROL))
  213.         {
  214.             if(Work -> BufferPos != Work -> NumChars)
  215.             {
  216.                 WORD i,Position = -1;
  217.  
  218.                 for(i = Work -> BufferPos ; i < Work -> NumChars ; i++)
  219.                 {
  220.                     if(Work -> WorkBuffer[i] == ' ')
  221.                     {
  222.                         for( ; i < Work -> NumChars ; i++)
  223.                         {
  224.                             if(Work -> WorkBuffer[i] != ' ')
  225.                             {
  226.                                 Position = i;
  227.                                 break;
  228.                             }
  229.                         }
  230.  
  231.                         break;
  232.                     }
  233.                 }
  234.  
  235.                 if(Position != -1)
  236.                     Work -> BufferPos = Position;
  237.                 else
  238.                     Work -> BufferPos = Work -> NumChars;
  239.  
  240.                 Work -> EditOp = EO_MOVECURSOR;
  241.             }
  242.         }
  243.  
  244.             /* The user pressed the cursor-right key to
  245.              * move the cursor to the previous word in the buffer.
  246.              */
  247.  
  248.         if(Work -> IEvent -> ie_Code == CURSORLEFT && (Work -> IEvent -> ie_Qualifier & IEQUALIFIER_CONTROL))
  249.         {
  250.             if(Work -> BufferPos)
  251.             {
  252.                 WORD i,Position = -1;
  253.  
  254.                 for(i = Work -> BufferPos ; i >= 0 ; i--)
  255.                 {
  256.                     if(Work -> WorkBuffer[i] != ' ')
  257.                     {
  258.                         Position = i;
  259.                         break;
  260.                     }
  261.                 }
  262.  
  263.                 if(Position == -1)
  264.                     Position = 0;
  265.  
  266.                 if(Position)
  267.                 {
  268.                     i = Position;
  269.  
  270.                     Position = -1;
  271.  
  272.                     for( ; i >= 0 ; i--)
  273.                     {
  274.                         if(Work -> WorkBuffer[i] == ' ')
  275.                         {
  276.                             Position = i + 1;
  277.                             break;
  278.                         }
  279.                     }
  280.                 }
  281.  
  282.                 if(Position != -1)
  283.                     Work -> BufferPos = Position;
  284.                 else
  285.                     Work -> BufferPos = 0;
  286.  
  287.                 Work -> EditOp = EO_MOVECURSOR;
  288.             }
  289.         }
  290.     }
  291. }
  292.