home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 500-599 / ff589.lza / Term / TermSrc.lha / StringHook.c < prev    next >
C/C++ Source or Header  |  1991-11-24  |  8KB  |  369 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1991 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: StringHook.c
  6.  *    Created ..: Wednesday 06-Feb-91 12:35
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    06-Feb-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "TermGlobal.h"
  16.  
  17.     /* HookEntry():
  18.      *
  19.      *    Call a hook function.
  20.      */
  21.  
  22. ULONG __saveds __asm
  23. HookEntry(register __a0 struct Hook *HookPtr,register __a2 APTR Object,register __a1 APTR Message)
  24. {
  25.     return(HookPtr -> h_SubEntry(HookPtr,Object,Message));
  26. }
  27.  
  28.     /* InitHook(struct Hook *Hook,APTR HookCode):
  29.      *
  30.      *    Set up a call back routine (hook).
  31.      */
  32.  
  33. VOID
  34. InitHook(struct Hook *Hook,APTR HookCode)
  35. {
  36.     Hook -> h_Entry        = HookEntry;
  37.     Hook -> h_SubEntry    = HookCode;
  38.     Hook -> h_Data        = 0;
  39. }
  40.  
  41.     /* ClipServer():
  42.      *
  43.      *    Quiet background process which saves data to the clipboard
  44.      *    or fills a buffer with fresh clipboard data.
  45.      */
  46.  
  47. VOID __saveds
  48. ClipServer()
  49. {
  50.     if(ClipPort = CreateMsgPort())
  51.     {
  52.         ULONG         Mask;
  53.         struct Message    *ClipMsg;
  54.         UBYTE        *Buffer;
  55.         BYTE         Terminated = FALSE;
  56.  
  57.         Signal(ThisProcess,SIGBREAKF_CTRL_C);
  58.  
  59.         while(!Terminated)
  60.         {
  61.             Mask = Wait(SIGBREAKF_CTRL_C | (1 << ClipPort -> mp_SigBit));
  62.  
  63.             if(Mask & SIGBREAKF_CTRL_C)
  64.                 Terminated = TRUE;
  65.  
  66.             if(Mask & (1 << ClipPort -> mp_SigBit))
  67.             {
  68.                 while(ClipMsg = GetMsg(ClipPort))
  69.                 {
  70.                     Buffer = ClipMsg -> mn_Node . ln_Name;
  71.  
  72.                     if(Buffer[0])
  73.                         SaveClip(Buffer,strlen(Buffer));
  74.                     else
  75.                     {
  76.                         WORD Len = LoadClip(Buffer,256);
  77.  
  78.                         Buffer[Len] = 0;
  79.                     }
  80.  
  81.                     ReplyMsg(ClipMsg);
  82.                 }
  83.             }
  84.         }
  85.  
  86.         DeleteMsgPort(ClipPort);
  87.     }
  88.  
  89.     Forbid();
  90.  
  91.     ClipProcess = NULL;
  92.  
  93.     Signal(ThisProcess,SIGBREAKF_CTRL_C);
  94. }
  95.  
  96.     /* CommandKey(struct Hook *Hook,struct SGWork *Work,ULONG *Msg):
  97.      *
  98.      *    This routine is called whenever input passes through
  99.      *    a `term' string gadget. It releases the currently
  100.      *    active string gadget as soon as the Right-Amiga-key is
  101.      *    pressed (user is about to select a menu item).
  102.      */
  103.  
  104. VOID
  105. CommandKey(struct Hook *Hook,struct SGWork *Work,ULONG *Msg)
  106. {
  107.     if(Msg[0] == SGH_KEY)
  108.     {
  109.             /* Simple: activate the next gadget if
  110.              * return is pressed in the current one.
  111.              */
  112.  
  113.         if(Work -> Actions & SGA_END)
  114.             Work -> Actions |= SGA_NEXTACTIVE;
  115.  
  116.             /* This looks like a built-in command. */
  117.  
  118.         if(Work -> IEvent -> ie_Qualifier & IEQUALIFIER_RCOMMAND)
  119.         {
  120.                 /* Amiga+C = Copy contents of string
  121.                  *           gadget to the clipboard.
  122.                  */
  123.  
  124.             if(Work -> Code == 'c')
  125.             {
  126.                 Work -> Actions &= ~SGA_USE;
  127.                 Work -> Actions |= SGA_BEEP;
  128.  
  129.                     /* Valid buffer contents? */
  130.  
  131.                 if(Work -> PrevBuffer[0])
  132.                 {
  133.                     struct MsgPort *ReplyPort;
  134.  
  135.                         /* Post a message... */
  136.  
  137.                     if(ReplyPort = CreateMsgPort())
  138.                     {
  139.                         struct Message ClipMessage;
  140.  
  141.                         ClipMessage . mn_Node . ln_Name    = Work -> PrevBuffer;
  142.                         ClipMessage . mn_ReplyPort    = ReplyPort;
  143.                         ClipMessage . mn_Length        = sizeof(struct Message);
  144.  
  145.                         PutMsg(ClipPort,&ClipMessage);
  146.  
  147.                         WaitPort(ReplyPort);
  148.  
  149.                         GetMsg(ReplyPort);
  150.  
  151.                         DeleteMsgPort(ReplyPort);
  152.  
  153.                         Work -> Actions &= ~SGA_BEEP;
  154.                     }
  155.                 }
  156.  
  157.                 return;
  158.             }
  159.  
  160.                 /* Amiga+V = Insert current clipboard
  161.                  *           contents at cursor position.
  162.                  */
  163.  
  164.             if(Work -> Code == 'v')
  165.             {
  166.                 Work -> Actions &= ~SGA_USE;
  167.                 Work -> Actions |= SGA_BEEP;
  168.  
  169.                     /* Don't paste to integer gadgets
  170.                      * (it could confuse Intuition if
  171.                      *  the buffer contained non-numeric
  172.                      *  symbols...).
  173.                      */
  174.  
  175.                 if(!(Work -> Gadget -> Activation & GACT_LONGINT))
  176.                 {
  177.                     struct MsgPort *ReplyPort;
  178.  
  179.                         /* Post a message... */
  180.  
  181.                     if(ReplyPort = CreateMsgPort())
  182.                     {
  183.                         STATIC UBYTE    Buffer[2048];
  184.                         struct Message    ClipMessage;
  185.  
  186.                         Buffer[0] = 0;
  187.  
  188.                         ClipMessage . mn_Node . ln_Name    = &Buffer[0];
  189.                         ClipMessage . mn_ReplyPort    = ReplyPort;
  190.                         ClipMessage . mn_Length        = sizeof(struct Message);
  191.  
  192.                         PutMsg(ClipPort,&ClipMessage);
  193.  
  194.                         WaitPort(ReplyPort);
  195.  
  196.                         GetMsg(ReplyPort);
  197.  
  198.                         DeleteMsgPort(ReplyPort);
  199.  
  200.                             /* Anything in the buffer? */
  201.  
  202.                         if(Buffer[0])
  203.                         {
  204.                             WORD Len = strlen(Buffer);
  205.  
  206.                                 /* Insert as many characters as we can. */
  207.  
  208.                             while(Len > 0 && Work -> NumChars + Len > Work -> StringInfo -> MaxChars)
  209.                                 Len--;
  210.  
  211.                                 /* Any characters left? */
  212.  
  213.                             if(Len > 0)
  214.                             {
  215.                                 STATIC UBYTE OtherBuffer[2048];
  216.  
  217.                                     /* Provide null-termination. */
  218.  
  219.                                 Buffer[Len] = 0;
  220.  
  221.                                     /* Set up undo buffer correctly. */
  222.  
  223.                                 if(Work -> StringInfo -> UndoBuffer)
  224.                                     strcpy(Work -> StringInfo -> UndoBuffer,Work -> PrevBuffer);
  225.  
  226.                                 Work -> StringInfo -> UndoPos = --Work -> BufferPos;
  227.  
  228.                                     /* Save the characters before the cursor. */
  229.  
  230.                                 if(Work -> BufferPos)
  231.                                     CopyMem(Work -> PrevBuffer,OtherBuffer,Work -> BufferPos);
  232.  
  233.                                     /* Provide null-termination. */
  234.  
  235.                                 OtherBuffer[Work -> BufferPos] = 0;
  236.  
  237.                                     /* Append the clipboard data. */
  238.  
  239.                                 strcat(OtherBuffer,Buffer);
  240.  
  241.                                     /* Append the characters following the cursor. */
  242.  
  243.                                 strcat(OtherBuffer,&Work -> PrevBuffer[Work -> BufferPos]);
  244.  
  245.                                     /* = new work buffer. */
  246.  
  247.                                 strcpy(Work -> WorkBuffer,OtherBuffer);
  248.  
  249.                                     /* Fiddle with some cursor data. */
  250.  
  251.                                 Work -> BufferPos += Len;
  252.                                 Work -> NumChars    += Len;
  253.  
  254.                                     /* And that's it... */
  255.  
  256.                                 Work -> Actions    |= SGA_USE;
  257.                                 Work -> EditOp     = EO_BIGCHANGE;
  258.  
  259.                                 Work -> Actions &= ~SGA_BEEP;
  260.                             }
  261.                         }
  262.                         else
  263.                             Work -> Actions &= ~SGA_BEEP;
  264.                     }
  265.                 }
  266.  
  267.                 return;
  268.             }
  269.         }
  270.  
  271.         if((Work -> IEvent -> ie_Qualifier & AMIGARIGHT) && Work -> IEvent -> ie_Code < 96)
  272.         {
  273.             if(!(Work -> IEvent -> ie_Qualifier & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)) && (Work -> IEvent -> ie_Code == KEYCODE_X || Work -> IEvent -> ie_Code == KEYCODE_Q))
  274.                 return;
  275.             else
  276.             {
  277.                 Work -> Actions |= (SGA_END|SGA_REUSE);
  278.                 Work -> Actions &= ~(SGA_USE|SGA_BEEP);
  279.  
  280.                 CommandWindow = Work -> GadgetInfo -> gi_Window;
  281.                 CommandGadget = Work -> Gadget;
  282.             }
  283.         }
  284.  
  285.             /* The user pressed the cursor-right key to
  286.              * move the cursor to the next word in the buffer.
  287.              */
  288.  
  289.         if(Work -> IEvent -> ie_Code == CURSORRIGHT && (Work -> IEvent -> ie_Qualifier & IEQUALIFIER_CONTROL))
  290.         {
  291.             if(Work -> BufferPos != Work -> NumChars)
  292.             {
  293.                 WORD i,Position = -1;
  294.  
  295.                 for(i = Work -> BufferPos ; i < Work -> NumChars ; i++)
  296.                 {
  297.                     if(Work -> WorkBuffer[i] == ' ')
  298.                     {
  299.                         for( ; i < Work -> NumChars ; i++)
  300.                         {
  301.                             if(Work -> WorkBuffer[i] != ' ')
  302.                             {
  303.                                 Position = i;
  304.                                 break;
  305.                             }
  306.                         }
  307.  
  308.                         break;
  309.                     }
  310.                 }
  311.  
  312.                 if(Position != -1)
  313.                     Work -> BufferPos = Position;
  314.                 else
  315.                     Work -> BufferPos = Work -> NumChars;
  316.  
  317.                 Work -> EditOp = EO_MOVECURSOR;
  318.             }
  319.         }
  320.  
  321.             /* The user pressed the cursor-right key to
  322.              * move the cursor to the previous word in the buffer.
  323.              */
  324.  
  325.         if(Work -> IEvent -> ie_Code == CURSORLEFT && (Work -> IEvent -> ie_Qualifier & IEQUALIFIER_CONTROL))
  326.         {
  327.             if(Work -> BufferPos)
  328.             {
  329.                 WORD i,Position = -1;
  330.  
  331.                 for(i = Work -> BufferPos ; i >= 0 ; i--)
  332.                 {
  333.                     if(Work -> WorkBuffer[i] != ' ')
  334.                     {
  335.                         Position = i;
  336.                         break;
  337.                     }
  338.                 }
  339.  
  340.                 if(Position == -1)
  341.                     Position = 0;
  342.  
  343.                 if(Position)
  344.                 {
  345.                     i = Position;
  346.  
  347.                     Position = -1;
  348.  
  349.                     for( ; i >= 0 ; i--)
  350.                     {
  351.                         if(Work -> WorkBuffer[i] == ' ')
  352.                         {
  353.                             Position = i + 1;
  354.                             break;
  355.                         }
  356.                     }
  357.                 }
  358.  
  359.                 if(Position != -1)
  360.                     Work -> BufferPos = Position;
  361.                 else
  362.                     Work -> BufferPos = 0;
  363.  
  364.                 Work -> EditOp = EO_MOVECURSOR;
  365.             }
  366.         }
  367.     }
  368. }
  369.