home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / submods / listbox / ListBox.b next >
Encoding:
Text File  |  1996-08-28  |  9.1 KB  |  318 lines

  1. /*
  2. ** A simple ListView requester Subprogram LIBRARY Module.
  3. **
  4. ** Adapted from RKM: Libraries (1992), p 383-385
  5. ** AND NDK example gadget1.c, by David Benn,
  6. ** 18th March, 3rd April, 1st May, 3rd,6th-8th,
  7. ** 29th June, 1st July 1995
  8. **
  9. ** Modified to use the new includes 14th September 1995,
  10. ** by Nils Sjoholm.
  11. ** The fontsensitiv changes was done before that.
  12. ** The date? can't remember. :)
  13. **
  14. ** Changed the source to use the function declarations
  15. ** and my tagmacros.
  16. ** Date 14-Oct-95   Nils Sjoholm
  17. **
  18. ** Created SUBmod from ListBox.b
  19. ** making minor changes to the source.
  20. ** 17th October 1995, David Benn
  21. ** 
  22. **/
  23.  
  24. #include <libraries/gadtools.h>
  25. #include <ace/acedef.h>
  26. #include <funcs/intuition_funcs.h>
  27. #include <funcs/gadtools_funcs.h>
  28. #include <funcs/utility_funcs.h>
  29. #include <funcs/exec_funcs.h>
  30.  
  31. /*  *
  32. ** Constant definitions.
  33. **/
  34. CONST LISTBOX_CANCELLED = 0
  35. CONST LISTBOX_ITEM_STRINGSIZE = 80
  36.  
  37. #define OK_CLICKED -1
  38. #define CANCEL_CLICKED -2
  39. #define UNKNOWN_ACTION -3
  40.  
  41. #define GAD_LISTVIEW  1
  42. #define GAD_BUTTON_OK  2
  43. #define GAD_BUTTON_CANCEL  3
  44.  
  45. /*  *
  46. ** FORWARD SUB declarations.
  47. **/
  48. DECLARE SUB LONGINT gadtools_window(ADDRESS listItemArray, LONGINT items, \
  49.                                     STRING wdwTitle, STRING prompt)
  50. DECLARE SUB handle_gadget(ADDRESS msg, ADDRESS action)
  51. DECLARE SUB do_window_refresh(ADDRESS wdw)
  52.  
  53. /* ami.lib           */
  54. DECLARE FUNCTION NewList(ADDRESS theList) EXTERNAL
  55.  
  56. /*  *
  57. ** Subprogram definitions.
  58. **/
  59. SUB LONGINT gadtools_window(ADDRESS listItemArray, \
  60.                             LONGINT items, STRING wdwTitle, STRING prompt)
  61. /*  *
  62. ** Prepare FOR USING GadTools, set up gadgets,
  63. ** OPEN WINDOW AND process IDCMP events.
  64. **/
  65.  
  66. DECLARE STRUCT _Screen *mysc
  67. DECLARE STRUCT TextAttr *scrFont
  68. DECLARE STRUCT _Window *mywin
  69. DECLARE STRUCT _Gadget *glist, *gad
  70. DECLARE STRUCT NewGadget ng
  71. DECLARE STRUCT TagItem *gadTags, *tag, *wintags
  72. DECLARE STRUCT _List myList
  73. DECLARE STRUCT Node *theNode
  74. DECLARE STRUCT TextAttr gadFont
  75. DECLARE STRUCT IntuiMessage *imsg
  76. DECLARE STRUCT IntuiText IText
  77.  
  78. LONGINT terminated, class, selected
  79. DIM STRING theItems(1) SIZE LISTBOX_ITEM_STRINGSIZE ADDRESS listItemArray
  80. SHORTINT action
  81. ADDRESS vi
  82. LONGINT YSize
  83. LONGINT Win_Width,Win_Height
  84. LONGINT templeft,temptop,tempwidth,tempheight
  85. LONGINT GadgetWidth
  86. LONGINT IDCMP
  87. STRING longname SIZE LISTBOX_ITEM_STRINGSIZE
  88. STRING dummy SIZE LISTBOX_ITEM_STRINGSIZE
  89.   glist = NULL
  90.  
  91.   mysc = SCREEN(1)
  92.   IF mysc <> NULL THEN
  93.  
  94.     vi = GetVisualInfoA(mysc, NULL)
  95.     IF vi <> NULL THEN
  96.       /* GadTools always requires this STEP TO be taken. */
  97.       gad = CreateContext(@glist)
  98.  
  99.      /*  * Create Exec LIST of items TO be displayed in listview GADGET **/
  100.      NewList(myList)
  101.      FOR i=1 TO items
  102.        theNode = ALLOC(SIZEOF(Node))
  103.        theNode->ln_Name = @theItems(i)
  104.        dummy = theItems(i)
  105.        IF LEN(dummy) > LEN(longname) THEN
  106.            longname = dummy
  107.        END IF
  108.        AddTail(myList,theNode)
  109.      NEXT
  110.       scrFont = mysc->_Font
  111.       YSize = scrFont->ta_YSize+5
  112.       IText->ITextFont = scrFont
  113.       IText->IText = @longname
  114.       Win_Width = IntuiTextLength(IText)+40
  115.       IF Win_Width > mysc->_Width THEN
  116.           gadFont->ta_Name  = SADD("topaz.font")
  117.           gadFont->ta_YSize = 8
  118.           gadFont->ta_Style = 0
  119.           gadFont->ta_Flags = 0
  120.           scrFont = gadfont
  121.           IText->ITextFont = scrFont
  122.           IText->IText = @longname
  123.           Win_Width = IntuiTextLength(IText) + 40
  124.       END IF
  125.  
  126.       IText->ITextFont = scrFont
  127.       IText->IText = SADD("Cancel")
  128.       GadgetWidth = IntuiTextLength(IText) + 10
  129.       IF Win_Width < (2 * GadgetWidth) + 40 THEN
  130.           Win_Width = (2 * GadgetWidth) + 40
  131.       END IF
  132.  
  133.      /*  * Create the listview GADGET initialising with above LIST **/
  134.  
  135.      ng->ng_LeftEdge = 10
  136.      ng->ng_TopEdge = YSize + 8
  137.      ng->ng_Width = Win_Width - 20
  138.      ng->ng_Height = YSize * 5
  139.      ng->ng_GadgetText = @prompt
  140.      ng->ng_TextAttr = scrFont
  141.      ng->ng_VisualInfo = vi
  142.      ng->ng_GadgetID = GAD_LISTVIEW
  143.      ng->ng_Flags = NULL
  144.  
  145.      templeft = ng->ng_LeftEdge
  146.      temptop = ng->ng_TopEdge
  147.      tempwidth = ng->ng_Width
  148.      tempheight = ng->ng_Height
  149.  
  150.      SetUpTags(gadtags,3)
  151.      IF gadTags <> NULL THEN
  152.         tag = gadTags
  153.         SetTag(GTLV_Labels,mylist)
  154.         SetTag(GTLV_ShowSelected,NULL)
  155.         SetTagEnd
  156.      END IF
  157.  
  158.      gad = CreateGadgetA(LISTVIEW_KIND, gad, ng, gadTags)
  159.      FreeTagItems(gadTags)
  160.  
  161.      /*  * Create the Ok AND Cancel buttons **/
  162.      ng->ng_LeftEdge    = 10
  163.      ng->ng_TopEdge     = temptop + tempheight + 4
  164.      ng->ng_Width       = GadgetWidth
  165.      ng->ng_Height      = YSize + 2
  166.      ng->ng_GadgetText  = SADD("Ok")
  167.      ng->ng_GadgetID    = GAD_BUTTON_OK
  168.      ng->ng_Flags       = NULL
  169.  
  170.      templeft = ng->ng_LeftEdge
  171.      temptop = ng->ng_TopEdge
  172.      tempwidth = ng->ng_Width
  173.      tempheight = ng->ng_Height
  174.  
  175.      gad = CreateGadgetA(BUTTON_KIND,gad,ng,NULL)
  176.  
  177.      ng->ng_LeftEdge    = 10 + ((Win_Width - 20) - tempwidth)
  178.      ng->ng_TopEdge     = temptop
  179.      ng->ng_Width       = tempwidth
  180.      ng->ng_Height      = tempheight
  181.      ng->ng_GadgetText  = SADD("Cancel")
  182.      ng->ng_GadgetID    = GAD_BUTTON_CANCEL
  183.      ng->ng_Flags       = NULL
  184.  
  185.      temptop = ng->ng_TopEdge
  186.      tempheight = ng->ng_Height
  187.      gad = CreateGadgetA(BUTTON_KIND,gad,ng,NULL)
  188.  
  189.      Win_Height = temptop + tempheight + 5
  190.  
  191.       IDCMP = (IDCMP_CLOSEWINDOW OR IDCMP_GADGETUP OR LISTVIEWIDCMP)
  192.  
  193.      SetUpTags(wintags,13)
  194.      IF wintags <> NULL THEN
  195.         tag = wintags
  196.         SetTag(WA_Left,mysc->MouseX-(Win_Width/2))
  197.         SetTag(WA_Top,mysc->MouseY-(Win_Height/2))
  198.         SetTag(WA_InnerWidth,Win_Width)
  199.         SetTag(WA_InnerHeight,Win_Height)
  200.         SetTag(WA_Title,@wdwTitle)
  201.         SetTag(WA_CustomScreen,mysc)
  202.         SetTag(WA_Gadgets,glist)
  203.         SetTag(WA_IDCMP,IDCMP)
  204.         SetTag(WA_DragBar,TRUE)
  205.         SetTag(WA_CloseGadget,TRUE)
  206.         SetTag(WA_GimmeZeroZero,TRUE)
  207.         SetTag(WA_Activate,TRUE)
  208.         SetTagEnd
  209.      END IF
  210.  
  211.      /* OPEN WINDOW, render gadgets AND enter event-handling loop.  */
  212.      IF gad <> NULL THEN
  213.  
  214.          mywin = OpenWindowTagList(NULL,wintags)
  215.          FreeTagItems(wintags)
  216.          IF mywin <> NULL THEN
  217.           GT_RefreshWindow(mywin,NULL)
  218.  
  219.           /*  *
  220.           ** Standard MESSAGE handling loop with GadTools MESSAGE
  221.           ** handling functions used (GT_GetIMsg() AND GT_ReplyIMsg()).
  222.           **/
  223.           WHILE NOT terminated
  224.             WaitPort(mywin->UserPort)
  225.  
  226.             /* Use GT_GetIMsg() AND GT_ReplyIMsg() FOR handling   */
  227.             /* IntuiMessages with GadTools gadgets.               */
  228.             REPEAT
  229.               imsg = GT_GetIMsg(mywin->UserPort)
  230.               class = imsg->Class
  231.               CASE
  232.                 /* Buttons AND listviews report GADGETUP.                */
  233.                 class = IDCMP_GADGETUP : handle_gadget(imsg, @action)
  234.                 /* Has WINDOW CLOSE GADGET been clicked?                 */
  235.                 class = IDCMP_CLOSEWINDOW : terminated=true : result=LISTBOX_CANCELLED
  236.                 /* This handling is REQUIRED with GadTools.              */
  237.                 class = IDCMP_REFRESHWINDOW : do_window_refresh(mywin)
  238.               END CASE
  239.  
  240.               GT_ReplyIMsg(imsg)
  241.  
  242.               /* Item selected?     */
  243.               IF action > 0 THEN
  244.                 selected = action
  245.               ELSE
  246.                 /* Ok BUTTON clicked?   */
  247.                 IF action = OK_CLICKED THEN
  248.                   terminated = TRUE
  249.                   result = selected
  250.                 ELSE
  251.                   /* Cancel BUTTON clicked?   */
  252.                   IF action = CANCEL_CLICKED THEN
  253.                     terminated = TRUE
  254.                     result = LISTBOX_CANCELLED
  255.                   END IF
  256.                 END IF
  257.               END IF
  258.             UNTIL terminated OR imsg = NULL
  259.           WEND
  260.  
  261.           /* WINDOW CLOSE 1    */
  262.           CloseWindow(mywin)
  263.         ELSE
  264.           result = LISTBOX_CANCELLED
  265.         END IF
  266.      ELSE
  267.        result = LISTBOX_CANCELLED
  268.      END IF
  269.  
  270.      /* FreeGadgets() must be called after the context has been
  271.         created. It does nothing IF glist is NULL.    */
  272.      FreeGadgets(glist)
  273.      FreeVisualInfo(vi)
  274.     ELSE
  275.       result = LISTBOX_CANCELLED
  276.     END IF
  277.   ELSE
  278.     result = LISTBOX_CANCELLED
  279.   END IF
  280.  
  281.   /*  RETURN the result.  */
  282.   gadtools_window = result
  283. END SUB
  284.  
  285. SUB handle_gadget(ADDRESS msg, ADDRESS action)
  286. DECLARE STRUCT IntuiMessage *imsg
  287. DECLARE STRUCT _Gadget *gad
  288.   imsg = msg
  289.   gad = imsg->IAddress
  290.   CASE
  291.     gad->GadgetID = GAD_LISTVIEW : *%action := imsg->Code+1
  292.     gad->GadgetID = GAD_BUTTON_OK : *%action := OK_CLICKED
  293.     gad->GadgetID = GAD_BUTTON_CANCEL : *%action := CANCEL_CLICKED
  294.     default : *%action := UNKNOWN_ACTION
  295.   END CASE
  296. END SUB
  297.  
  298. SUB do_window_refresh(ADDRESS wdw)
  299.   GT_BeginRefresh(mywin)
  300.   GT_EndRefresh(mywin,TRUE)
  301. END SUB
  302.  
  303. SUB LONGINT ListBox(ADDRESS listItemArray, \
  304.                     LONGINT items, STRING wdwTitle, STRING prompt) EXTERNAL
  305. /*  *
  306. ** Invoke the listbox.
  307. **/
  308. LONGINT result
  309.  
  310.   LIBRARY "exec.LIBRARY"
  311.   LIBRARY "utility.LIBRARY"
  312.   LIBRARY "intuition.LIBRARY"
  313.   LIBRARY "gadtools.LIBRARY"
  314.   result = gadtools_window(listItemArray, items, wdwTitle, prompt)
  315.   LIBRARY CLOSE
  316.   ListBox = result
  317. END SUB
  318.