home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 115 / af115sub.adf / yahzee.lzx / yahzee / requesters.c < prev    next >
C/C++ Source or Header  |  1998-06-03  |  5KB  |  171 lines

  1. /*
  2.  * requesters.c
  3.  * ============
  4.  * Some usefull functions for handling requesters.
  5.  *
  6.  * Copyright (C) 1994-1998 Håkan L. Younes (lorens@hem.passagen.se)
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/libraries.h>
  11. #include <intuition/intuition.h>
  12. #include <libraries/gadtools.h>
  13. #include <string.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/gadtools_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <clib/intuition_protos.h>
  19.  
  20.  
  21. extern struct Library  *IntuitionBase;
  22.  
  23.  
  24. BOOL
  25. window_sleep (
  26.    struct Window     *win,
  27.    struct Requester  *req)
  28. {
  29.    InitRequester (req);
  30.    if (Request (req, win))
  31.    {
  32.       if (IntuitionBase->lib_Version >= 39L)
  33.          SetWindowPointer (win, WA_BusyPointer, TRUE, TAG_DONE);
  34.       
  35.       return TRUE;
  36.    }
  37.    
  38.    return FALSE;
  39. }
  40.  
  41. void
  42. window_wakeup (
  43.    struct Window     *win,
  44.    struct Requester  *req)
  45. {
  46.    if (IntuitionBase->lib_Version >= 39L)
  47.       SetWindowPointer (win, WA_Pointer, NULL, TAG_DONE);
  48.    EndRequest (req, win);
  49. }
  50.  
  51. void
  52. msg_requester (
  53.    struct Window  *win,
  54.    char           *title,
  55.    char           *gad_title,
  56.    char           *message)
  57. {
  58.    struct EasyStruct   msg_req;
  59.    struct Requester    req;
  60.    BOOL   win_sleep = FALSE;
  61.    
  62.    msg_req.es_StructSize = sizeof (msg_req);
  63.    msg_req.es_Flags = 0;
  64.    msg_req.es_Title = title;
  65.    msg_req.es_TextFormat = message;
  66.    msg_req.es_GadgetFormat = gad_title;
  67.    
  68.    if (win != NULL)
  69.       win_sleep = window_sleep (win, &req);
  70.    EasyRequest (win, &msg_req, NULL, NULL);
  71.    if (win_sleep)
  72.       window_wakeup (win, &req);
  73. }
  74.  
  75. void
  76. string_requester (
  77.    struct Window  *win,
  78.    APTR            vis_info,
  79.    char           *win_title,
  80.    char           *gad_title,
  81.    char           *buffer,
  82.    UBYTE           buf_size)
  83. {
  84.    struct Window  *req_win;
  85.    struct NewGadget   new_gad;
  86.    struct Gadget  *str_gad, *gad_list;
  87.    struct IntuiMessage  *msg;
  88.    BOOL   done = FALSE;
  89.    struct Requester    req;
  90.    BOOL   win_sleep = FALSE;
  91.  
  92.    win_sleep = window_sleep (win, &req);
  93.    
  94.    str_gad = CreateContext (&gad_list);
  95.    new_gad.ng_TextAttr = win->WScreen->Font;
  96.    new_gad.ng_VisualInfo = vis_info;
  97.    new_gad.ng_LeftEdge = win->WScreen->WBorLeft + 16 +
  98.                          TextLength (&win->WScreen->RastPort,
  99.                                      gad_title, strlen (gad_title));
  100.    new_gad.ng_TopEdge = win->WScreen->WBorTop +
  101.                         win->WScreen->Font->ta_YSize + 5;
  102.    new_gad.ng_Width = 200;
  103.    new_gad.ng_Height = win->WScreen->Font->ta_YSize + 6;
  104.    new_gad.ng_GadgetText = gad_title;
  105.    new_gad.ng_GadgetID = 1;
  106.    new_gad.ng_Flags = 0;
  107.    str_gad = CreateGadget (STRING_KIND, str_gad, &new_gad,
  108.                            GTST_String, buffer,
  109.                            GTST_MaxChars, buf_size,
  110.                            TAG_DONE);
  111.    if (str_gad)
  112.    {
  113.       req_win = OpenWindowTags (NULL,
  114.                                 WA_Left, win->LeftEdge +
  115.                                          (win->Width -
  116.                                           win->BorderLeft -
  117.                                           win->BorderRight -
  118.                                           str_gad->LeftEdge -
  119.                                           str_gad->Width - 14) / 2,
  120.                                 WA_Top, win->TopEdge +
  121.                                         (win->Height -
  122.                                          win->BorderTop -
  123.                                          win->BorderBottom -
  124.                                          str_gad->Height - 14) / 2,
  125.                                 WA_InnerWidth, str_gad->LeftEdge +
  126.                                                str_gad->Width + 14,
  127.                                 WA_InnerHeight, str_gad->Height + 14,
  128.                                 WA_Title, win_title,
  129.                                 WA_ScreenTitle, win->ScreenTitle,
  130.                                 WA_PubScreen, win->WScreen,
  131.                                 WA_Gadgets, gad_list,
  132.                                 WA_IDCMP, STRINGIDCMP |
  133.                                           IDCMP_REFRESHWINDOW,
  134.                                 WA_DragBar, TRUE,
  135.                                 WA_DepthGadget, TRUE,
  136.                                 WA_Activate, TRUE,
  137.                                 TAG_DONE);
  138.       if (req_win)
  139.       {
  140.          GT_RefreshWindow (req_win, NULL);
  141.          ActivateGadget (str_gad, req_win, NULL);
  142.          
  143.          while (!done)
  144.          {
  145.             WaitPort (req_win->UserPort);
  146.             while (msg = GT_GetIMsg (req_win->UserPort))
  147.             {
  148.                switch (msg->Class)
  149.                {
  150.                case IDCMP_GADGETUP:
  151.                   done = TRUE;
  152.                   break;
  153.                case IDCMP_REFRESHWINDOW:
  154.                   GT_BeginRefresh (req_win);
  155.                   GT_EndRefresh (req_win, TRUE);
  156.                   break;
  157.                }
  158.                GT_ReplyIMsg (msg);
  159.             }
  160.          }
  161.          strncpy (buffer,
  162.                   ((struct StringInfo *)str_gad->SpecialInfo)->Buffer,
  163.                   buf_size);
  164.          CloseWindow (req_win);
  165.       }
  166.       FreeGadgets (gad_list);
  167.    }
  168.    if (win_sleep)
  169.       window_wakeup (win, &req);
  170. }
  171.