home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / Libraries / Intuition / StrDemo / strhooks.c < prev   
Encoding:
C/C++ Source or Header  |  1999-10-27  |  3.8 KB  |  158 lines

  1. /* strhooks.c -- string gadget hooks    */
  2. /* WARNING: This file contains "callback" functions.
  3.  * You must disable stack checking for them to work:
  4.  *         "LC -v strhooks"
  5.  */
  6.  
  7. /*
  8. Copyright (c) 1989-1999 Amiga, Inc.
  9.  
  10. Executables based on this information may be used in software
  11. for Amiga computers. All other rights reserved.
  12. This information is provided "as is"; no warranties are made.
  13. All use is at your own risk, and no liability or responsibility
  14. is assumed.
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <devices/inputevent.h>
  19. #include <utility/hooks.h>
  20. #include <intuition/sghooks.h>
  21.  
  22. #include <clib/intuition_protos.h>
  23. #include <clib/diskfont_protos.h>
  24. #include <clib/exec_protos.h>
  25.  
  26. #include <string.h>
  27. #include <stdlib.h>
  28.  
  29. #include "strgad.h"
  30.  
  31. struct Hook    tabhook;
  32. struct Hook    cyclehook;
  33.  
  34. /* forward in this file    */
  35. VOID    tabh();
  36. void    cycleh();
  37.  
  38. #define TABSCAN    (0x42)
  39.  
  40. #define SHIFTY (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)
  41.  
  42. #define D( x )    ;    /* debugging: on 'x', off ';'    */
  43.  
  44. void
  45. initMyHooks()
  46. {
  47.     void initHook();
  48.  
  49.     initHook( &tabhook, tabh );
  50.     initHook( &cyclehook, cycleh );
  51. }
  52.  
  53. /*
  54.  * This is a function which converts register-parameter
  55.  * hook calling convention into standard C conventions.
  56.  * It only works with Lattice C 5.0+
  57.  * 
  58.  * Without the fancy __asm stuff, you'd probably need to
  59.  * write this in assembler.
  60.  *
  61.  * You could conceivably declare all your C hook functions
  62.  * this way, and eliminate the middleman (you'd initialize
  63.  * the h_Entry field to your C function's address, and not
  64.  * bother with the h_SubEntry field).
  65.  *
  66.  * This is nice and easy, though, and since we're using the
  67.  * small data model, using a single interface routine like this
  68.  * (which does the necessary __saveds), it might
  69.  * actually turn out to be smaller to use a single entry point
  70.  * like this rather than declaring each of many hooks __saveds.
  71.  */
  72. ULONG __saveds __asm
  73. hookEntry( register __a0 struct Hook *hookptr,
  74.     register __a2 void    *object,
  75.     register __a1 void    *message )
  76. {
  77.     return ( (*hookptr->h_SubEntry)( hookptr, object, message ) );
  78. }
  79.  
  80. void
  81. initHook( hook, ccode )
  82. struct Hook    *hook;
  83. VOID        (*ccode)();
  84. {
  85.     hook->h_Entry = hookEntry;
  86.     hook->h_SubEntry = (ULONG (*)()) ccode;
  87.     hook->h_Data = 0;        /* I don't use it        */
  88. }
  89.  
  90.  
  91. void
  92. tabh( hook, sgw, msg )
  93. struct Hook    *hook;    /* which I don't use */
  94. struct SGWork    *sgw;
  95. ULONG        *msg;
  96. {
  97.     D( kprintf("tabh: sgw: %lx\n", sgw ) );
  98.  
  99.     if ( ( *msg == SGH_KEY ) && ( sgw->IEvent->ie_Code == TABSCAN ) )
  100.     {
  101.     sgw->Code = (sgw->IEvent->ie_Qualifier & SHIFTY)?
  102.         MYCODEBACKTAB: MYCODETAB;
  103.     sgw->Actions |= SGA_END;
  104.     sgw->Actions &= ~SGA_USE;
  105.     }
  106. }
  107.  
  108. UBYTE    *choices[] = {
  109.     (UBYTE *) "choice 1",
  110.     (UBYTE *) "choice 2",
  111.     (UBYTE *) "choice 3",
  112.     (UBYTE *) "choice 4",
  113.     (UBYTE *) "choice 5",
  114.     (UBYTE *) "choice 6",
  115. };
  116.  
  117. #define NUMCHOICES    ((sizeof (choices))/sizeof (UBYTE *))
  118. #define UPARROW        (0x4c)
  119. #define DOWNARROW    (0x4d)
  120.  
  121. void
  122. cycleh( hook, sgw, msg )
  123. struct Hook    *hook;    /* which I don't use */
  124. struct SGWork    *sgw;
  125. ULONG        *msg;
  126. {
  127.     static int    choice = 0;
  128.  
  129.     D( kprintf("cycleh: %lx, sgw: %lx\n", sgw ) );
  130.  
  131.     if ( *msg != SGH_KEY ) return;
  132.  
  133.     switch ( sgw->IEvent->ie_Code )
  134.     {
  135.     case TABSCAN:
  136.     sgw->Code = (sgw->IEvent->ie_Qualifier & SHIFTY)?
  137.         MYCODEBACKTAB: MYCODETAB;
  138.     sgw->Actions |= SGA_END;
  139.     sgw->Actions &= ~SGA_USE;
  140.     break;
  141.  
  142.     case UPARROW:
  143.     D( kprintf("cycle hook (up), previous choice %ld\n", choice ));
  144.     sgw->WorkBuffer = choices[ choice = (choice + 1)%NUMCHOICES ];
  145.     sgw->NumChars = strlen( sgw->WorkBuffer );
  146.     D( kprintf("new choice: %ld <%s>\n",choice, sgw->WorkBuffer ) );
  147.     break;
  148.  
  149.     case DOWNARROW:
  150.     D( kprintf("cycle hook (down), previous choice %ld\n", choice ));
  151.     sgw->WorkBuffer = choices[choice=(choice+(NUMCHOICES-1))%NUMCHOICES];
  152.     sgw->NumChars = strlen( sgw->WorkBuffer );
  153.     D( kprintf("new choice: %ld <%s>\n",choice, sgw->WorkBuffer ) );
  154.     break;
  155.     }
  156. }
  157.  
  158.