home *** CD-ROM | disk | FTP | other *** search
/ Kyūkyoku!! X68000 Emulator / X68000Book.dat / mac / OLS / X68000 / Ko-Window / kow142s.lzh / parts / input.c < prev    next >
C/C++ Source or Header  |  1994-11-29  |  6KB  |  284 lines

  1. /*
  2.  *    âEâCâôâhâEé┼é╠âCâôâvâbâg
  3.  *
  4.  *    1990.7.7
  5.  *    Copyright    T.Kobayashi
  6.  */
  7. /*
  8.  *    1991.7.10  H.Ogasawara    ò╥ÅWï@ö\é╠Æ╟ë┴, Emacsâéü[âhÆ╟ë┴
  9.  *    1991.8.19  H.Ogasawara     ôⁿù═âéü[âhé╔è┬ï½ò╧Éöé≡ÄQÅ╞é╖éΘéµéñëⁿù╟
  10.  *    1991.9.22  H.Ogasawara    Command.win é┼âoâXâGâëü[é╔é╚éΘâoâOé╠âfâoâbâO
  11.  *    1991.12.23 H.Ogasawara    âfâoâbâO
  12.  *    1992.3.2   H.Ogasawara    æSèpö╗ò╩é╠âoâOÅCÉ│&éQâoâCâgö╝èpæ╬ë₧ôÖ,æSû╩Åæè╖
  13.  *  1993.5.10  H.Ogasawara  force-insert é╞ InputGetMode() Æ╟ë┴
  14.  */
  15. #include     <stdio.h>
  16. #include    <sys_doslib.h>
  17. #include    <iocslib.h>
  18.  
  19. #include     "wlib.h"
  20. #include     "parts.h"
  21.  
  22. #define    Ctrl(a)        ((a)&0x1f)
  23.  
  24. static void    DrawSetCursor( DrawBuf*, InputClass* );
  25. int        _kk_EmacsMode= FALSE;
  26. int        _kk_force_insert= FALSE;
  27.  
  28. void
  29. InputSet( ip, x, y, str, max, attr, font )
  30. InputClass    *ip;
  31. int    x, y;
  32. char    *str;
  33. int    max;
  34. int    attr, font;
  35. {
  36.     ClipSet( (ClipClass*)ip, x, y, font/2*max, font );
  37.     ip->str=str;
  38.     ip->max=max;
  39.     ip->attr=attr;
  40.     ip->font=font;
  41.     ip->cur= 0;
  42.     ip->curflag=TRUE;
  43.     {
  44.         unsigned char    envbuf[256];
  45. /*        _kk_EmacsMode= FALSE;*/
  46.         if( GETENV( "KOWINKEY", 0, envbuf ) >= 0 ){
  47.             if( *envbuf == 'i' ){
  48.                 _kk_force_insert= TRUE;
  49.                 _kk_EmacsMode= envbuf[2] == 'm';
  50.             }else
  51.                 _kk_EmacsMode= envbuf[1] == 'm';
  52.         }
  53.     }
  54. }
  55.  
  56. #if    0    /* move to parts.h */
  57. void    InputSetCursor( ip, cur )
  58. InputClass    *ip ;
  59. int            cur ;
  60. {
  61.     ip->cur = cur ;
  62. }
  63.  
  64. int
  65. InputGetCursor( ip )
  66. InputClass    *ip;
  67. {
  68.     return    ip->cur;
  69. }
  70. #endif
  71.  
  72. int
  73. InputSetCursorVisible( buf, ip, sw )
  74. DrawBuf        *buf;
  75. InputClass    *ip;
  76. int        sw;
  77. {
  78.     if( ip->curflag != sw ){
  79.         ip->curflag= sw;
  80.         if( buf ){
  81.             DrawSetCursor( buf, ip );
  82.             return    1;
  83.         }
  84.     }
  85.     return    0;
  86. }
  87.  
  88. int
  89. InputSetDraw( buf, ip )
  90. DrawBuf        *buf;
  91. InputClass    *ip;
  92. {
  93.     DrawSetSymbol( buf, ip->x1, ip->y1, ip->str, ip->attr, ip->font );
  94.     if( ip->curflag ){
  95.         DrawSetCursor( buf+1, ip );
  96.         return    2;
  97.     }
  98.     return    1;
  99. }
  100.  
  101. static unsigned char    emacsmap[]={
  102. /* @ */        Ctrl('@'),
  103. /* a */        Ctrl('q'),
  104. /* b */        Ctrl('s'),
  105. /* c */        Ctrl('c'),
  106. /* d */        Ctrl('g'),
  107. /* e */        Ctrl('p'),
  108. /* f */        Ctrl('d'),
  109. /* g */        Ctrl('b'),
  110. /* h */        Ctrl('h'),
  111. /* i */        Ctrl('i'),
  112. /* j */        Ctrl('j'),
  113. /* k */        Ctrl('k'),
  114. /* l */        Ctrl('l'),
  115. /* m */        Ctrl('m'),
  116. /* n */        Ctrl('f'),
  117. /* o */        Ctrl('o'),
  118. /* p */        Ctrl('a'),
  119. /* q */        Ctrl('v')
  120. };
  121.  
  122. int
  123. InputKey( buf, ip, keycode, shift )
  124. DrawBuf        *buf;
  125. InputClass    *ip;
  126. int        keycode,
  127.         shift;
  128. {
  129.     DrawBuf    *top=    buf;
  130.     short    cur=    ip->cur;
  131.     short    px=    -1;
  132.     unsigned char    *str= (unsigned char*)ip->str;
  133.     static short    Kanji,
  134.             Cntrl,
  135.             ChangeMode;
  136.  
  137.     if( cur >= ip->max )
  138.         cur= ip->max-1;
  139.  
  140.     if( ip->curflag )
  141.         DrawSetCursor( buf++, ip );    /* cursor off */
  142.     if( Cntrl ){
  143.         ChangeMode= Cntrl= FALSE;
  144.         goto    default_;
  145.     }else if( ChangeMode ){        /*[CTRL]+[_],[e] emacs*/
  146.         ChangeMode= Cntrl= FALSE;
  147.         _kk_EmacsMode= (keycode == 'e');
  148.         keycode= 0;
  149.     }else if( _kk_EmacsMode && keycode<=17 )
  150.         keycode= emacsmap[keycode];
  151.  
  152.     switch( keycode ){
  153.     case Ctrl('h'):    /* back space */
  154.         if( cur ){
  155.             cur--;
  156.             if( KanjiTestPosition( str, cur ) )
  157.                 cur--;
  158.         }else
  159.             break;
  160.     case Ctrl('k'): /* delete from cursor position */
  161.     case Ctrl('g'): /* delete 1 char */
  162.     case FuncKeyDel:
  163.     deletechar:
  164.         if( str[cur] ){
  165.             short    i,
  166.                 xx;
  167.             if( keycode == Ctrl('k') ){
  168.                 i= KanjiGetPosition( str+cur, -1 );
  169.                 str[cur]= '\0';
  170.             }else{
  171.                 i= KanjiIsDbcs( str[cur] ) ? 2 : 1;
  172.                 strcpy( str+cur, str+cur+i );
  173.                 px= cur;
  174.             }
  175.             xx= ip->x1+ KanjiGetPosition( str, -1 )* (ip->font >>1);
  176.             DrawSetLine( buf++, xx, ip->y1,
  177.                 xx+(i+1)*(ip->font>>1)-1,ip->y1+ip->font-1,
  178.                 (ip->attr & AttrReverse) ? ip->attr & 3 : 0,
  179.                 OptionFill );
  180.         }
  181.         break;
  182.     case Ctrl('s'):    /* cursor left */
  183.     case FuncKeyLeft:
  184.         if( cur ){
  185.             cur--;
  186.             if( KanjiTestPosition( str, cur ) )
  187.                 cur--;
  188.         }
  189.         break;
  190.     case FuncKeyUp:
  191.     case Ctrl('a'):
  192.         if( cur ){
  193.             for( cur-- ; cur && isspace( str[cur] ) ; cur-- );
  194.             for(; cur && !isspace( str[cur] ) ; cur-- );
  195.             if( isspace( str[cur] ) )
  196.                 cur++;
  197.         }
  198.         break;
  199.     case Ctrl('d'): /* cursor right */
  200.     case FuncKeyRight:
  201.         if( cur < ip->max-1 && str[cur] ){
  202.             cur++;
  203.             if( KanjiTestPosition( str, cur ) )
  204.                 cur++;
  205.         }
  206.         break;
  207.     case FuncKeyClr:
  208.     case Ctrl('b'):    /* exchange cursor position */
  209.         if( !cur )
  210.             goto    end_of_line;
  211.     case Ctrl('q'):    /* top of line */
  212.         cur= 0;
  213.         break;
  214.     case Ctrl('p'):    /* end of line */
  215.     end_of_line:
  216.         cur= strlen( str );
  217.         break;
  218.     case FuncKeyHome:
  219.     case Ctrl('u'):    /* kill line */
  220.         cur= 0;
  221.         keycode= Ctrl('k');
  222.         goto    deletechar;
  223.     case FuncKeyDown:
  224.     case Ctrl('f'):
  225.         for(; str[cur] && !isspace( str[cur] ) ; cur++ );
  226.         for(; str[cur] &&  isspace( str[cur] ) ; cur++ );
  227.         break;
  228.     case Ctrl('v'):
  229.         Cntrl= TRUE;
  230.         break;
  231.     case Ctrl('o'):
  232.         LEDMOD( 4, !(K_SFTSNS() & 0x100) );
  233.         break;
  234.     case Ctrl('_'):
  235.         ChangeMode= TRUE;
  236.     case 0:
  237.         break;
  238.     default:
  239.         if( ( keycode & 0xff ) >= ' ' ){
  240.     default_:
  241.             /* INS mode */
  242.             if( (_kk_force_insert || (shift & 0x1000)) &&
  243.                                                     strlen(str) < ip->max ){
  244.                 memcpy( str+cur+1, str+cur, strlen(str)-cur+1 );
  245.             }else{
  246.                 if( !str[cur] )
  247.                     str[cur+1]= '\0';
  248.             }
  249.             str[cur]= keycode;
  250.             if( !Kanji ){
  251.                 if( KanjiIsDbcs( keycode ) ){
  252.                     Kanji= TRUE;
  253.                 }else
  254.                     px= cur;
  255.             }else{
  256.                 px= cur-1;
  257.                 Kanji= FALSE;
  258.             }
  259.             if( cur < ip->max-1 )
  260.                 cur++;
  261.         }
  262.     } /* end of switch */
  263.     if( px >= 0 ){
  264.         DrawSetSymbol( buf++,
  265.             ip->x1+KanjiGetPosition( str, px )*(ip->font>>1), ip->y1,
  266.             (char*)str+px, ip->attr, ip->font );
  267.     }
  268.     ip->cur= cur;
  269.     if( ip->curflag )
  270.         DrawSetCursor( buf++, ip );    /* cursor on */
  271.     return    buf-top;
  272. }
  273.  
  274. static void
  275. DrawSetCursor( buf, ip )
  276. DrawBuf        *buf;
  277. InputClass    *ip;
  278. {
  279.     short    font= ip->font >>1,
  280.         x= ip->x1 + KanjiGetPosition( ip->str, ip->cur )*font,
  281.         y= ip->y1 + font;
  282.     DrawSetLine( buf, x, y, x+font-1, y+font-1, (ip->attr&3)|OptionXor, OptionFill );
  283. }
  284.