home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 287.lha / TY_v1.3 / src / req.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  5.7 KB  |  172 lines

  1. /*************************************************************************
  2.  ***                        req.c                        (JJB TEMPLAR) ***
  3.  *** Date begun: 11/8/89.                                              ***
  4.  *** Last modified: 28/8/89.                                           ***
  5.  *************************************************************************/
  6. /*** Requester for string/integer input.                               ***
  7.  *************************************************************************/
  8.  
  9. #include <exec/types.h>
  10. #include <intuition/intuition.h>
  11.  
  12. #include <proto/intuition.h>
  13. #include <proto/exec.h>
  14.  
  15. #include <string.h>
  16.  
  17. extern struct Window    *Window;
  18. extern int              has_resized;
  19.  
  20. extern void sprintf();
  21.  
  22. #define G_OKAY      50
  23. #define G_CANCEL    51
  24. #define G_STRING    52
  25.  
  26. UWORD   rb1_dat[10] = {0,0, 193,0, 193,53, 0,53, 0,0};  /* White */
  27. UWORD   rb2_dat[10] = {2,1, 191,1, 191,52, 2,52, 2,1};  /* Red */
  28. UWORD   rb3_dat[ 4] = {0,0, 178,0};                     /* Line */
  29.  
  30. struct Border r_bd[6] = {
  31.     {7,13,1,2,JAM1,2,rb3_dat,NULL},
  32.     {9,14,2,0,JAM1,2,rb3_dat,&r_bd[0]},
  33.     {0, 0,3,3,JAM1,5,rb2_dat,&r_bd[1]},
  34.     {1, 0,3,3,JAM1,5,rb2_dat,&r_bd[2]},
  35.     {0, 0,1,2,JAM1,5,rb1_dat,&r_bd[3]},
  36.     {1, 0,1,2,JAM1,5,rb1_dat,&r_bd[4]}};
  37.  
  38. struct IntuiText r_txt[4] = {
  39.     {1,2,JAM1,55, 3,NULL,NULL,NULL},
  40.     {2,0,JAM1,57, 4,NULL,NULL,&r_txt[0]},
  41.     {1,2,JAM1,55,21,NULL,NULL,&r_txt[1]},
  42.     {2,0,JAM1,57,22,NULL,NULL,&r_txt[2]}};
  43. struct IntuiText g_txt[2] = {
  44.     {1,2,JAM1,15,2,NULL,"OKAY",NULL},
  45.     {3,3,JAM1, 8,2,NULL,"CANCEL",NULL}};
  46.  
  47. struct Image g_img[8] = {
  48.     {2,1,60,10,0,NULL,0,0,NULL},        /* Grey box */
  49.     {0,0,64,12,0,NULL,0,1,&g_img[0]},   /* White border */
  50.     {2,1,64,12,0,NULL,0,2,&g_img[1]},   /* Black shadow */
  51.     {0,0,64,12,0,NULL,0,3,&g_img[0]},   /* Red border */
  52.     {2,1,64,12,0,NULL,0,2,&g_img[3]},   /* Black shadow */
  53.     {-2,-1,164,10,0,NULL,0,0,NULL},         /* Black box */
  54.     {-4,-2,168,12,0,NULL,0,1,&g_img[5]},    /* White border */
  55.     {-2,-1,168,12,0,NULL,0,2,&g_img[6]}};   /* Black shadow */
  56.  
  57. UBYTE   s_buf[256],u_buf[256];
  58. struct StringInfo g_inf = {s_buf,u_buf,0,255,};
  59.  
  60. struct Gadget r_gad[3] = {
  61.     {NULL,13,36,64,12,GADGHCOMP|GADGIMAGE,RELVERIFY|ENDGADGET,
  62.      BOOLGADGET|REQGADGET,(APTR)&g_img[2],NULL,&g_txt[0],NULL,NULL,G_OKAY,NULL},
  63.     {&r_gad[0],117,36,64,12,GADGHCOMP|GADGIMAGE,RELVERIFY|ENDGADGET,
  64.      BOOLGADGET|REQGADGET,(APTR)&g_img[4],NULL,&g_txt[1],NULL,NULL,G_CANCEL,NULL},
  65.     {&r_gad[1],17,21,160,8,GADGHCOMP|GADGIMAGE,RELVERIFY|ENDGADGET,
  66.      STRGADGET|REQGADGET,(APTR)&g_img[7],NULL,NULL,NULL,(APTR)&g_inf,G_STRING,NULL}};
  67.  
  68. struct Requester req = {
  69.     NULL,200,75,195,54,0,0,&r_gad[2],&r_bd[5],&r_txt[1],0,0,};
  70.  
  71. static int  r_init(title,src,len,type) /*================================*/
  72. char    *title,*src;
  73. int     len,type;                  /* 1 = LONGINT, 0 = STRING */
  74. {
  75. register char   *cp;
  76.  
  77.     cp = s_buf + 84;
  78.     while (cp-- > s_buf) *cp = 0;       /* Clear s_buf */
  79.  
  80.     r_txt[0].LeftEdge = 97 - (strlen(title) * 4);
  81.     r_txt[1].LeftEdge = r_txt[0].LeftEdge + 2;
  82.     r_txt[1].IText = r_txt[0].IText = title;
  83.  
  84.     r_gad[2].Activation = (type)? RELVERIFY|ENDGADGET|LONGINT: RELVERIFY|ENDGADGET;
  85.  
  86.     req.LeftEdge = (Window->Width / 2) - 97;
  87.     req.TopEdge = (Window->Height / 2) - 27;
  88.     req.ReqGadget = &r_gad[2];
  89.     req.ReqText = &r_txt[1];
  90.  
  91.     strcpy(s_buf,src);
  92.     g_inf.MaxChars = len;
  93.  
  94.     ModifyIDCMP(Window,Window->IDCMPFlags | REQSET);
  95.     return(Request(&req,Window));
  96. }
  97.  
  98. static int  r_loop() /*==================================================*/
  99. {
  100. struct IntuiMessage *msg;
  101. int     loop = 1,x,y;
  102.   /* To track NEWSIZE, since I can't get NEWSIZE messages during req. */
  103.     x = Window->Width;      y = Window->Height;
  104.  
  105.     while (loop > 0) {
  106.         Wait(1 << Window->UserPort->mp_SigBit);
  107.         while (msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) {
  108.             if (msg->Class == GADGETUP) {
  109.                 switch (((struct Gadget *)(msg->IAddress))->GadgetID) {
  110.                     case (G_STRING):
  111.                     case (G_OKAY):   loop = 0;  break;
  112.                     case (G_CANCEL): loop = -1; break;
  113.                 }
  114.             }
  115.             else if (msg->Class == REQSET) {
  116.                 ModifyIDCMP(Window,Window->IDCMPFlags & ~REQSET);
  117.                 ActivateGadget(&r_gad[2],Window,&req);
  118.             }
  119.         ReplyMsg((struct Message *)msg);
  120.     }
  121.     }
  122.     if ((x != Window->Width) || (y != Window->Height)) has_resized = 1;
  123.     return(loop);
  124. }
  125.  
  126. int     r_string(title,src,len) /*=======================================*/
  127. char    *title,*src;
  128. int     len;
  129. {
  130.     src[len-1] = 0;
  131.     if (!r_init(title,src,len,0)) return(0);
  132.  
  133.     if ((r_loop() < 0) || !s_buf[0]) return(0);
  134.  
  135.     strcpy(src,s_buf);
  136.     return(1);
  137. }
  138.  
  139. int     r_int(title,src,len) /*==========================================*/
  140. char    *title;
  141. int     *src,len;
  142. {
  143. char    buf[10];
  144.     sprintf(buf,"%d",*src);
  145.     buf[len-1] = 0;    /* Caller was pretty stupid if this was necessary */
  146.     if (!r_init(title,buf,len,1)) return(0);
  147.  
  148.     if ((r_loop() < 0) || !s_buf[0]) return(0);
  149.  
  150.     *src = g_inf.LongInt;
  151.     return(1);
  152. }
  153.  
  154. int     r_bool(cp) /*====================================================*/
  155. char    *cp;
  156. {
  157.     r_txt[0].LeftEdge = 57;         r_txt[1].LeftEdge = 59;
  158.     r_txt[1].IText = r_txt[0].IText = "Aye or Nay";
  159.  
  160.     r_txt[2].LeftEdge = 97 - (strlen(cp) * 4);
  161.     r_txt[3].LeftEdge = r_txt[2].LeftEdge + 2;
  162.     r_txt[3].IText = r_txt[2].IText = cp;
  163.  
  164.     req.LeftEdge = (Window->Width / 2) - 97;
  165.     req.TopEdge = (Window->Height / 2) - 27;
  166.     req.ReqGadget = &r_gad[1];
  167.     req.ReqText = &r_txt[3];
  168.  
  169.     if (Request(&req,Window)) return(r_loop() + 1);
  170.     return(0);          /* Fail if couldn't set requester up. */
  171. }
  172.