home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / mac / mgetline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  6.9 KB  |  364 lines

  1. /*    SCCS Id: @(#)getline.c    3.1    90/22/02
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include "Dialogs.h"
  7. #ifdef THINK_C
  8. #include "pascal.h"
  9. #endif
  10. #include <Packages.h>
  11.  
  12. // void FDECL(getlin,(const char *, char *));
  13. // void FDECL(get_ext_cmd,(char *));
  14. int FDECL ( try_key_queue , ( char * ) ) ;
  15.  
  16. char * FDECL ( PtoCstr , ( unsigned char * ) ) ;
  17. unsigned char * FDECL ( CtoPstr , ( char * ) ) ;
  18. void SetFrameItem ( DialogPtr , short , short ) ;
  19. void FlashButton ( DialogPtr , short ) ;
  20.  
  21. void FDECL ( enter_topl_mode , ( char * ) ) ;
  22. void FDECL ( leave_topl_mode , ( char * ) ) ;
  23. Boolean FDECL ( topl_key , ( unsigned char ) ) ;
  24. Boolean FDECL ( topl_ext_key , ( unsigned char ) ) ;
  25.  
  26. typedef Boolean FDECL ( ( * key_func ) , ( unsigned char ) ) ;
  27.  
  28.  
  29. int
  30. get_line_from_key_queue ( char * bufp )
  31. {
  32.     * bufp = 0 ;
  33.     if ( try_key_queue ( bufp ) ) {
  34.  
  35.         while ( * bufp ) {
  36.  
  37.             if ( * bufp == 10 || * bufp == 13 ) {
  38.  
  39.                 * bufp = 0 ;
  40.             }
  41.             bufp ++ ;
  42.         }
  43.         return true ;
  44.     }
  45.     return false ;
  46. }
  47.  
  48.  
  49. pascal Boolean
  50. getlinFilter ( DialogPtr dp , EventRecord * ev , short * itemHit )
  51. {
  52.     if (ev->what == keyDown) {
  53.         int key = ev->message & keyCodeMask,
  54.             ch    = ev->message & charCodeMask;
  55.  
  56.         if (ch == 0x1b || key == 0x3500 || key == 0x4700) {
  57.             *itemHit = 2;
  58.             FlashButton(dp, 2);
  59.             return true;
  60.  
  61.         } else if (ch == CHAR_CR || ch == CHAR_ENTER) {
  62.             *itemHit = 1;
  63.             FlashButton(dp, 1);
  64.             return true;
  65.         }
  66.     }
  67.  
  68.     return false;
  69. }
  70.  
  71.  
  72. void
  73. popup_getlin(const char *query, char *bufp)
  74. {
  75.     ControlHandle    ctrl;
  76.     DialogPtr        promptDialog;
  77.     short            itemHit, type;
  78.     Rect            box;
  79.     Str255            pasStr;
  80.  
  81.     if ( get_line_from_key_queue ( bufp ) )
  82.         return ;
  83.  
  84.     /*
  85.     ** Make a copy of the prompt string and convert the copy to a Pascal string.
  86.     */
  87.     
  88.     strcpy((char *) pasStr, query);
  89.     CtoPstr((char *) pasStr);
  90.     
  91.     /*
  92.     ** Set the query line as parameter text.
  93.     */
  94.     
  95.     ParamText(pasStr, "\p", "\p", "\p");
  96.     
  97.     promptDialog = mv_get_new_dialog(130);
  98.     ShowWindow(promptDialog);
  99.  
  100.     InitCursor ( ) ;
  101.     SetFrameItem ( promptDialog , 6 , 1 ) ;
  102.     do {
  103.         mv_modal_dialog(&getlinFilter, &itemHit);
  104.     } while ((itemHit != 1) && (itemHit != 2));
  105.     
  106.     if (itemHit != 2) {
  107.         /*
  108.         ** Get the text from the text edit item.
  109.         */
  110.         
  111.         GetDItem(promptDialog, 4, &type, (Handle *) &ctrl, &box);
  112.         GetIText((Handle) ctrl, pasStr);
  113.         
  114.         /*
  115.         ** Convert it to a 'C' string and copy it into the return value.
  116.         */
  117.         
  118.         PtoCstr(pasStr);
  119.         strcpy(bufp, (char *) pasStr);
  120.     } else {
  121.         /*
  122.         ** Return a null-terminated string consisting of a single <ESC>.
  123.         */
  124.         
  125.         bufp[0] = '\033';
  126.         bufp[1] = '\0';
  127.     }
  128.     
  129.     mv_close_dialog(promptDialog);
  130. }
  131.  
  132.  
  133. void
  134. topl_getlin(const char *query, char *bufp, key_func key)
  135. {
  136.     int q_len = strlen(query);
  137.  
  138.     if ( get_line_from_key_queue ( bufp ) )
  139.         return ;
  140.  
  141.     enter_topl_mode(query);
  142.     while ((*key)(nhgetch())) ;
  143.     leave_topl_mode(bufp);
  144. }
  145.  
  146.  
  147. /*
  148.  * Read a line closed with '\n' into the array char bufp[BUFSZ].
  149.  * (The '\n' is not stored. The string is closed with a '\0'.)
  150.  * Reading can be interrupted by an escape ('\033') - now the
  151.  * resulting string is "\033".
  152.  */
  153. void
  154. mac_getlin(const char *query, char *bufp)
  155. {
  156.     if (flags.popup_dialog)
  157.         popup_getlin(query, bufp);
  158.     else
  159.         topl_getlin(query, bufp, &topl_key);
  160. }
  161.  
  162.  
  163. #ifdef COM_COMPL
  164.  
  165. pascal Boolean
  166. ExtendedCommandDialogFilter ( DialogPtr dp , EventRecord * ev , short * item )
  167. {
  168.     int ix ;
  169.     Handle h ;
  170.     Rect r ;
  171.     short k ;
  172.     Str255 s ;
  173.     unsigned char com [ 2 ] ;
  174.  
  175.     if ( ev -> what != keyDown ) {
  176.  
  177.         return 0 ;
  178.     }
  179.     com [ 0 ] = 1 ;
  180.     com [ 1 ] = ev -> message & 0xff ;
  181.  
  182.     if ( com [ 1 ] == 10 || com [ 1 ] == 13 || com [ 1 ] == 32 ||
  183.         com [ 1 ] == 3 ) { // various "OK"
  184.  
  185.         * item = 1 ;
  186.         FlashButton ( dp , 1 ) ;
  187.         return 1 ;
  188.     }
  189.     if ( com [ 1 ] == 27 || ( ev -> message & 0xff00 == 0x3500 ) ) { // escape
  190.  
  191.         * item = 2 ;
  192.         FlashButton ( dp , 2 ) ;
  193.         return 1 ;
  194.     }
  195.     for ( ix = 3 ; ix ; ix ++ ) {
  196.  
  197.         h = ( Handle ) NULL ;
  198.         k = 0 ;
  199.         GetDItem ( dp , ix , & k , & h , & r ) ;
  200.         if ( ! k || ! h ) {
  201.  
  202.             return 0 ;
  203.         }
  204.         if ( k == 6 ) {    //    Radio Button Item
  205.  
  206.             GetCTitle ( ( ControlHandle ) h , s ) ;
  207.             s [ 0 ] = 1 ;
  208.             if ( ! IUEqualString ( com , s ) ) {
  209.  
  210.                 * item = ix ;
  211.                 return 1 ;
  212.             }
  213.         }
  214.     }
  215. /*NOTREACHED*/
  216.     return 0 ;
  217. }
  218.  
  219.  
  220. void
  221. popup_get_ext_cmd(char *bufp)
  222. {
  223.     ControlHandle    ctrl;
  224.     DialogPtr        extendedDialog;
  225.     short            itemHit, type;
  226.     Rect            box;
  227.     char            *extendedCommand;
  228.  
  229.     /*
  230.     ** Default selection is the first item after the "Cancel" button.
  231.     */
  232.     static lastItemSelected = 3;
  233.  
  234.     if ( get_line_from_key_queue ( bufp ) )
  235.         return ;
  236.  
  237.     extendedDialog = mv_get_new_dialog(131);
  238.     ShowWindow(extendedDialog);
  239.  
  240.     /*
  241.     ** Mark the default selection.
  242.     */
  243.     
  244.     GetDItem(extendedDialog, lastItemSelected, &type, (Handle *) &ctrl, &box);
  245.     SetCtlValue(ctrl, 1);
  246.  
  247.     InitCursor ( ) ;
  248.     SetFrameItem ( extendedDialog , 20 , 1 ) ;
  249.     do {
  250.         mv_modal_dialog((ModalFilterProcPtr) ExtendedCommandDialogFilter , &itemHit);
  251.         if ((itemHit != 1) && (itemHit != 2)) {
  252.             /*
  253.             ** If OK and Cancel (items 1 and 2) weren't selected then a radio button 
  254.             ** was pushed.  Unmark the previous selection.
  255.             */
  256.             
  257.             GetDItem(extendedDialog, lastItemSelected, &type, (Handle *) &ctrl, &box);
  258.             SetCtlValue(ctrl, 0);
  259.             
  260.             /*
  261.             ** Mark the current selection.
  262.             */
  263.             
  264.             GetDItem(extendedDialog, itemHit, &type, (Handle *) &ctrl, &box);
  265.             SetCtlValue(ctrl, 1);
  266.  
  267.             /*
  268.             ** Save the item number for use later.
  269.             */
  270.             
  271.             lastItemSelected = itemHit;
  272.         }
  273.     } while ((itemHit != 1) && (itemHit != 2));
  274.     
  275.     if (itemHit == 2) {
  276.         /*
  277.         ** Return a null-terminated string consisting of a single <ESC>.
  278.         */
  279.         
  280.         bufp[0] = '\033';
  281.         bufp[1] = '\0';
  282.     } else {
  283.         switch (lastItemSelected) {
  284.         case 3:
  285.             extendedCommand = "adjust";
  286.             break;
  287.         case 4:
  288.             extendedCommand = "chat";
  289.             break;
  290.         case 5:
  291.             extendedCommand = "dip";
  292.             break;
  293.         case 6:
  294.             extendedCommand = "force";
  295.             break;
  296.         case 7:
  297.             extendedCommand = "jump";
  298.             break;
  299.         case 8:
  300.             extendedCommand = "loot";
  301.             break;
  302.         case 9:
  303.             extendedCommand = "monster";
  304.             break;
  305.         case 10:
  306.             extendedCommand = "name";
  307.             break;
  308.         case 11:
  309.             extendedCommand = "offer";
  310.             break;
  311.         case 12:
  312.             extendedCommand = "pray";
  313.             break;
  314.         case 13:
  315.             extendedCommand = "rub";
  316.             break;
  317.         case 14:
  318.             extendedCommand = "sit";
  319.             break;
  320.         case 15:
  321.             extendedCommand = "turn";
  322.             break;
  323.         case 16:
  324.             extendedCommand = "untrap";
  325.             break;
  326.         case 17:
  327.             extendedCommand = "version";
  328.             break;
  329.         case 18:
  330.             extendedCommand = "window";
  331.             break;
  332.         case 19:
  333.             extendedCommand = "wipe";
  334.             break;
  335.         }
  336.         
  337.         /*
  338.         ** Copy the text representing the last radio button selected into the buffer.
  339.         */
  340.         
  341.         strcpy(bufp, extendedCommand);
  342.     }
  343.     
  344.     mv_close_dialog(extendedDialog);
  345. }
  346.  
  347.  
  348. /* Read in an extended command - doing command line completion for
  349.  * when enough characters have been entered to make a unique command.
  350.  * This is just a modified getlin().   -jsb
  351.  */
  352. void
  353. mac_get_ext_cmd(char *bufp)
  354. {
  355.     if (flags.popup_dialog)
  356.         popup_get_ext_cmd(bufp);
  357.     else
  358.         topl_getlin("# ", bufp, &topl_ext_key);
  359. }
  360.  
  361. #endif /* COM_COMPL /* */
  362.  
  363. /* macgetline.c */
  364.