home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432b.lha / EzLib / src / getstring.c < prev    next >
C/C++ Source or Header  |  1990-11-10  |  7KB  |  232 lines

  1. #include "ezlib.h"
  2.  
  3. /* this file contains a function called getstring() which will prompt
  4.  * a user for a string, and the return it to you if they clicked o.k.
  5.  * or hit return in the string gadget
  6.  */
  7.  
  8. /*    Yes, if you haven't already noticed, some of these structures were
  9.  * generated by PowerWindows.  Also note that I had to mess around with
  10.  * the generated code a fair amount before it worked properly.
  11.  *
  12.  *    One other thing.    The reason these structures are declared global
  13.  * and then copied in down below is so that the routine will be completely
  14.  * re-entrant and won't modify any static data on multiple calls through
  15.  * the same code!  This comes in handy when you have several children
  16.  * tasks using this code.
  17.  */
  18. void *AllocMem();
  19.  
  20. struct StringInfo ez_string_gadget_SInfo = {
  21.     NULL,  /* buffer where text will be edited - FILLED IN LATER*/
  22.     NULL,    /* optional undo buffer */
  23.     0,    /* character position in buffer */
  24.     255,    /* maximum number of characters to allow */
  25.     0,    /* first displayed character buffer position */
  26.     0,0,0,0,0,    /* Intuition initialized and maintained variables */
  27.     0,    /* Rastport of gadget */
  28.     0,    /* initial value for integer gadgets */
  29.     NULL    /* alternate keymap (fill in if you set the flag) */
  30. };
  31.  
  32. SHORT ez_BorderVectors1[] = {
  33.     0,0,
  34.     292,0,
  35.     292,11,
  36.     0,11,
  37.     0,0
  38. };
  39. struct Border ez_Border1 = {
  40.     -2,-2,    /* XY origin relative to container TopLeft */
  41.     3,0,JAM1,    /* front pen, back pen and drawmode */
  42.     5,    /* number of XY vectors */
  43.     ez_BorderVectors1,    /* pointer to XY vectors */
  44.     NULL    /* next border in list */
  45. };
  46.  
  47. struct Gadget ez_string_gadget_global = {
  48.     NULL,    /* next gadget */
  49.     13,18,    /* origin XY of hit box relative to window TopLeft */
  50.     291,10, /* hit box width and height */
  51.     NULL,    /* gadget flags */
  52.     SELECTED+RELVERIFY,   /* activation flags */
  53.     STRGADGET,    /* gadget type flags */
  54.     (APTR)&ez_Border1,    /* gadget border or image to be rendered */
  55.     NULL,    /* alternate imagery for selection */
  56.     NULL,    /* first IntuiText structure */
  57.     NULL,    /* gadget mutual-exclude long word */
  58.     NULL,     /* SpecialInfo structure */
  59.     NULL,    /* user-definable data */
  60.     NULL    /* pointer to user-definable data */
  61. };
  62.  
  63. SHORT ez_BorderVectors2[] = {
  64.     0,0,
  65.     75,0,
  66.     75,11,
  67.     0,11,
  68.     0,0
  69. };
  70. struct Border ez_Border2 = {
  71.     -1,-1,    /* XY origin relative to container TopLeft */
  72.     3,0,JAM1,    /* front pen, back pen and drawmode */
  73.     5,    /* number of XY vectors */
  74.     ez_BorderVectors2,    /* pointer to XY vectors */
  75.     NULL    /* next border in list */
  76. };
  77.  
  78. struct IntuiText ez_IText = {
  79.     3,0,JAM1,    /* front and back text pens, drawmode and fill byte */
  80.     10,1,    /* XY origin relative to container TopLeft */
  81.     NULL,    /* font pointer or NULL for default */
  82.     (UBYTE *)"  OK ",      /* pointer to text */
  83.     NULL    /* next IntuiText structure */
  84. };
  85.  
  86. struct Gadget ez_ok_gadget_global = {
  87.     NULL,     /* next gadget */
  88.     230,40, /* origin XY of hit box relative to window TopLeft */
  89.     74,10,    /* hit box width and height */
  90.     NULL,    /* gadget flags */
  91.     RELVERIFY,    /* activation flags */
  92.     BOOLGADGET,    /* gadget type flags */
  93.     (APTR)&ez_Border2,    /* gadget border or image to be rendered */
  94.     NULL,    /* alternate imagery for selection */
  95.     &ez_IText,   /* first IntuiText structure */
  96.     NULL,    /* gadget mutual-exclude long word */
  97.     NULL,    /* SpecialInfo structure */
  98.     NULL,    /* user-definable data */
  99.     NULL    /* pointer to user-definable data */
  100. };
  101.  
  102. struct IntuiText ez_IText2 = {
  103.     3,0,JAM1,    /* front and back text pens, drawmode and fill byte */
  104.     10,1,    /* XY origin relative to container TopLeft */
  105.     NULL,    /* font pointer or NULL for default */
  106.     (UBYTE *)"Cancel",      /* pointer to text */
  107.     NULL    /* next IntuiText structure */
  108. };
  109.  
  110. struct Gadget ez_cancel_gadget_global = {
  111.     NULL,     /* next gadget - filled in below */
  112.     10,40,    /* origin XY of hit box relative to window TopLeft */
  113.     74,10,    /* hit box width and height */
  114.     NULL,    /* gadget flags */
  115.     RELVERIFY,    /* activation flags */
  116.     BOOLGADGET,    /* gadget type flags */
  117.     (APTR)&ez_Border2,    /* gadget border or image to be rendered */
  118.     NULL,    /* alternate imagery for selection */
  119.     &ez_IText2,   /* first IntuiText structure */
  120.     NULL,    /* gadget mutual-exclude long word */
  121.     NULL,    /* SpecialInfo structure */
  122.     NULL,    /* user-definable data */
  123.     NULL    /* pointer to user-definable data */
  124. };
  125.  
  126.  
  127. /* some defines for our gadget id's for this window... */
  128. #define CANCEL_GADG 351
  129. #define SEARCH_GADG 352
  130. #define STRING_GADG 353
  131.  
  132.  
  133. char *getstring(screen, title, def_string)
  134.  struct Screen *screen;
  135.  UBYTE *title;
  136.  char *def_string;
  137. {
  138.  char buff[255], *string = NULL;
  139.  struct Window *win, *OpenWindow();
  140.  struct IntuiMessage *msg, *GetMsg();
  141.  struct Gadget *gadg, string_gadget, search_gadget, cancel_gadget;
  142.  struct StringInfo string_gadgetSInfo;
  143.  USHORT len;
  144.  LONG go_on = TRUE;
  145.  ULONG    idcmp = GADGETUP+CLOSEWINDOW+ACTIVEWINDOW,   /* IDCMP flags */
  146.     flags = WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE+NOCAREREFRESH+WINDOWACTIVE;
  147.  
  148.  if (def_string != NULL)
  149.   strcpy(buff, def_string);
  150.  else
  151.   buff[0] = 0;
  152.  
  153.  /* this part uses the structure assignment feature of Manx which I
  154.   * believe is part of ANSI C, so this should be o.k.
  155.   */
  156.  cancel_gadget = ez_cancel_gadget_global;
  157.  search_gadget = ez_ok_gadget_global;
  158.  string_gadget = ez_string_gadget_global;
  159.  
  160.  string_gadgetSInfo = ez_string_gadget_SInfo;
  161.  string_gadget.SpecialInfo = (APTR) &string_gadgetSInfo;
  162.  
  163.  cancel_gadget.NextGadget = &search_gadget;
  164.  search_gadget.NextGadget = &string_gadget;
  165.  
  166.  cancel_gadget.GadgetID = CANCEL_GADG;
  167.  search_gadget.GadgetID = SEARCH_GADG;
  168.  string_gadget.GadgetID = STRING_GADG;
  169.  string_gadgetSInfo.Buffer = (UBYTE *)buff;
  170.  
  171.  win = createwindow(screen, flags, idcmp, 148, 46, 316, 55);
  172.  if ( win == NULL)
  173.    { return NULL; }
  174.  
  175.  /* put the window title in */
  176.  SetWindowTitles(win, title, -1L);
  177.  
  178.  /* add the gadgets to the window and make them appear */
  179.  AddGList(win, &cancel_gadget, 1, 3, NULL);
  180.  RefreshGadgets(&cancel_gadget, win, NULL);
  181.  
  182.  while (go_on)
  183.   {
  184.    Wait(1L << win->UserPort->mp_SigBit);
  185.  
  186.    while( go_on && (msg = GetMsg(win->UserPort)) )
  187.     {
  188.      switch(msg->Class)
  189.       {
  190.     case GADGETUP : gadg = (struct Gadget *)msg->IAddress;
  191.             switch( gadg->GadgetID )
  192.              {
  193.                case CANCEL_GADG : go_on = FALSE;
  194.                           break;
  195.  
  196.                case SEARCH_GADG : /* fall through here...  */
  197.  
  198.                case STRING_GADG : go_on = FALSE;
  199.                           len = strlen(buff) + 1;
  200.                           if (len == 1)   /* empty gadget */
  201.                          break;
  202.                           string = (char *)AllocMem(len, 0L);
  203.                           if (string == NULL)
  204.                          break;
  205.                           strcpy(string, buff);
  206.                           break;
  207.  
  208.               default        : break;
  209.              }
  210.             break;
  211.  
  212.     case ACTIVEWINDOW : ActivateGadget(&string_gadget, win, NULL);
  213.                 break;
  214.  
  215.     case CLOSEWINDOW : go_on = FALSE;
  216.                break;
  217.  
  218.     default : break;
  219.  
  220.       } /* end of switch(msg->Class) */
  221.      ReplyMsg(msg);
  222.  
  223.     } /* end of while(msg....) */
  224.  
  225.   } /* end of while (go_on) */
  226.  
  227.  CloseWindow(win);
  228.  
  229.  return string;
  230. }
  231.  
  232.