home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / ansicdemo.lha / ANSI-C / gadtools / gadget2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  12.6 KB  |  594 lines

  1. /*
  2. ** gadget2.c:  Font-sensitive GadTools gadgets.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/gadtools.h>
  16.  
  17. #include <pragma/exec_lib.h>
  18. #include <pragma/graphics_lib.h>
  19. #include <pragma/intuition_lib.h>
  20. #include <pragma/diskfont_lib.h>
  21. #include <pragma/gadtools_lib.h>
  22.  
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. /*------------------------------------------------------------------------*/
  28.  
  29. BOOL check1 = TRUE;
  30. BOOL check2 = FALSE;
  31. STRPTR settinga = "Bananas";
  32. STRPTR settingb = "Lemons";
  33.  
  34. STRPTR LeftLabels[] =
  35. {
  36.     "Button One",
  37.     "Button Two",
  38.     "Button Three",
  39.     "Button Four",
  40. };
  41.  
  42. STRPTR RightLabels[] =
  43. {
  44.     "Setting A:",
  45.     "Setting B:",
  46.     "Check 1:",
  47.     "Check 2:",
  48. };
  49.  
  50. UWORD WinWidth, WinHeight;
  51.  
  52. /*------------------------------------------------------------------------*/
  53.  
  54. int main(int, char *[]);
  55. struct Window *initWindow(struct Screen *, struct TextAttr *);
  56. void uninitWindow(struct Window *);
  57. void bail_out(int);
  58. struct Gadget *CreateAllGadgets(struct Gadget **, void *,
  59.     struct Screen *, struct TextAttr *);
  60. BOOL HandleEvent(struct Window *);
  61. void Hit_Button1(struct Window *, struct Gadget *);
  62. void Hit_Button2(struct Window *, struct Gadget *);
  63. void Hit_Button3(struct Window *, struct Gadget *);
  64. void Hit_Button4(struct Window *, struct Gadget *);
  65. void Hit_Check1(struct Window *, struct Gadget *);
  66. void Hit_Check2(struct Window *, struct Gadget *);
  67.  
  68. /*------------------------------------------------------------------------*/
  69.  
  70. /*  These are all the things to be allocated/opened, and later
  71.     freed/closed: */
  72.  
  73. struct GfxBase *GfxBase = NULL;
  74. struct IntuitionBase *IntuitionBase = NULL;
  75. struct Library *GadToolsBase = NULL;
  76. struct Library *DiskfontBase = NULL;
  77. struct Screen *mysc = NULL;
  78. struct Window *win = NULL;
  79. struct TextFont *customfont = NULL;
  80. struct Gadget *glist = NULL;
  81. void *vi = NULL;
  82.  
  83. /*------------------------------------------------------------------------*/
  84.  
  85. struct TextAttr customtattr;
  86. struct TextAttr *tattr;
  87.  
  88. /*------------------------------------------------------------------------*/
  89.  
  90. int main( int argc, char *argv[])
  91. {
  92.     /*  Open all libraries: */
  93.  
  94.     if (argc == 2)
  95.     {
  96.     printf("Usage:\n\tgadget2\nor\n\tgadget2 fontname.font fontsize\n");
  97.     printf("Example:\n\tgadget2 courier.font 15\n");
  98.     bail_out(0);
  99.     }
  100.  
  101.     if (!(GfxBase = (struct GfxBase *)
  102.     OpenLibrary("graphics.library", 36L)))
  103.     bail_out(20);
  104.  
  105.     if (!(IntuitionBase = (struct IntuitionBase *)
  106.     OpenLibrary("intuition.library", 36L)))
  107.     bail_out(20);
  108.  
  109.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  110.     bail_out(20);
  111.  
  112.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 36L)))
  113.     bail_out(20);
  114.  
  115.     if (!(mysc = LockPubScreen(NULL)))
  116.     bail_out(20);
  117.  
  118.     customtattr.ta_Style = 0;
  119.     customtattr.ta_Flags = 0;
  120.     if (argc < 3)
  121.     {
  122.     /*  Default to screen's font */
  123.     tattr = mysc->Font;
  124.     }
  125.     else
  126.     {
  127.     LONG longval;
  128.  
  129.     /*  Attempt to use the font specified on the command line: */
  130.     customtattr.ta_Name = argv[1];
  131.     /*  Convert decimal size to long */
  132.     longval = atol( argv[2]);
  133.     customtattr.ta_YSize = longval;
  134.     tattr = &customtattr;
  135.     if (!(customfont = OpenDiskFont(tattr)))
  136.     {
  137.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  138.         customtattr.ta_YSize);
  139.         bail_out(20);
  140.     }
  141.     }
  142.  
  143.     win = initWindow(mysc, tattr);
  144.  
  145.     if (win)
  146.     {
  147.     while (!HandleEvent(win))
  148.         ;
  149.     bail_out(0);
  150.     }
  151.     else
  152.     bail_out(20);
  153. return 0;
  154. }
  155.  
  156. /*------------------------------------------------------------------------*/
  157.  
  158. /*/ initWindow()
  159.  *
  160.  *  Open a window, allocate and add all the gadgets.
  161.  *
  162.  */
  163.  
  164. struct Window *initWindow( struct Screen *screen, struct TextAttr *tattr)
  165. {
  166.     BOOL ok;
  167.     struct Window *win = NULL;
  168.  
  169.     ok = ((vi = GetVisualInfo(screen,
  170.     TAG_DONE)) != NULL);
  171.  
  172.     if (ok)
  173.     {
  174.     ok = (CreateAllGadgets(&glist, vi, screen, tattr) != NULL);
  175.     }
  176.  
  177.     if (ok)
  178.     {
  179.     /*  Open the window: */
  180.     if (!(win = OpenWindowTags(NULL,
  181.     WA_InnerWidth, WinWidth,
  182.     WA_Height, WinHeight,
  183.     WA_AutoAdjust, TRUE,
  184.     WA_PubScreen, screen,
  185.     WA_Activate, TRUE,
  186.     WA_DragBar, TRUE,
  187.     WA_DepthGadget, TRUE,
  188.     WA_CloseGadget, TRUE,
  189.     WA_SizeGadget, TRUE,
  190.     WA_SimpleRefresh, TRUE,
  191.  
  192.     WA_IDCMP, CLOSEWINDOW | REFRESHWINDOW | BUTTONIDCMP | CHECKBOXIDCMP,
  193.  
  194.     WA_MinWidth, 50,
  195.     WA_MinHeight, 50,
  196.     WA_Title, "GadTools Font-Sensitive Gadgets",
  197.  
  198.     TAG_DONE)))
  199.     bail_out(20);
  200.     }
  201.  
  202.     if (ok)
  203.     {
  204.     /*  Add gadgets, refresh them, and special-refresh them.
  205.         GT_RefreshWindow() does the refreshing that RefreshGList()
  206.         will take care of when we switch over to a custom-gadget
  207.         implementation: */
  208.     AddGList(win, glist, ((UWORD) -1), ((UWORD) -1), NULL);
  209.     RefreshGList(glist, win, NULL, ((UWORD) -1));
  210.     GT_RefreshWindow(win, NULL);
  211.     return(win);
  212.     }
  213.     else
  214.     {
  215.     uninitWindow(win);
  216.     win = NULL;
  217.     return(NULL);
  218.     }    
  219. }
  220.  
  221. /*------------------------------------------------------------------------*/
  222.  
  223. /*/ uninitWindow()
  224.  *
  225.  * Take down a window and free stuff associated with it.
  226.  * It's ok to call this with a NULL pointer.
  227.  *
  228.  */
  229.  
  230. void uninitWindow( struct Window *win)
  231. {
  232.     if (win)
  233.     {
  234.     CloseWindow(win);
  235.     }
  236.     if (glist)
  237.     {
  238.     FreeGadgets(glist);
  239.     glist = NULL;
  240.     }
  241.     if (vi)
  242.     {
  243.     FreeVisualInfo(vi);
  244.     vi = NULL;
  245.     }
  246. }
  247.  
  248.  
  249. /*------------------------------------------------------------------------*/
  250.  
  251. /*/ bail_out()
  252.  *
  253.  * Function to close down or free any opened or allocated stuff, and then
  254.  * exit.
  255.  *
  256.  */
  257.  
  258. void bail_out( int code)
  259. {
  260.  
  261.     uninitWindow(win);
  262.     win = NULL;
  263.  
  264.     if (customfont)
  265.     {
  266.     CloseFont(customfont);
  267.     }
  268.  
  269.     if (mysc)
  270.     {
  271.     UnlockPubScreen(NULL, mysc);
  272.     }
  273.  
  274.     if (GadToolsBase)
  275.     {
  276.     CloseLibrary(GadToolsBase);
  277.     }
  278.  
  279.     if (DiskfontBase)
  280.     {
  281.     CloseLibrary(DiskfontBase);
  282.     }
  283.  
  284.     if (IntuitionBase)
  285.     {
  286.     CloseLibrary( (struct Library *)IntuitionBase);
  287.     }
  288.  
  289.     if (GfxBase)
  290.     {
  291.     CloseLibrary( (struct Library *)GfxBase);
  292.     }
  293.  
  294.     exit(code);
  295. }
  296.  
  297.  
  298. /*------------------------------------------------------------------------*/
  299.  
  300. /*/ CreateAllGadgets()
  301.  *
  302.  * Here is where all the initialization and creation of toolkit gadgets
  303.  * take place.  This function requires a pointer to a NULL-initialized
  304.  * gadget list pointer.  It returns a pointer to the last created gadget,
  305.  * which can be checked for success/failure.
  306.  *
  307.  */
  308.  
  309. struct Gadget *CreateAllGadgets( struct Gadget **glistptr, void *vi, struct Screen *screen, struct TextAttr *tattr)
  310. {
  311.     struct NewGadget ng;
  312.     struct Gadget *gad;
  313.     WORD row1, row2, row3, row4;
  314.     WORD LeftWidth = 0;
  315.     WORD RightWidth = 0;
  316.     WORD FarRightWidth = 0;
  317.     WORD width;
  318.     struct RastPort TextRP;
  319.     WORD i;
  320.     UWORD topborder;
  321.     struct TextFont *font;
  322.  
  323.     topborder = screen->WBorTop + (screen->Font->ta_YSize + 1);
  324.  
  325.  
  326.     if (!(font = OpenFont(tattr)))
  327.     {
  328.     return(NULL);
  329.     }
  330.  
  331.     InitRastPort(&TextRP);
  332.     SetFont(&TextRP, font);
  333.  
  334.     /*  Need to know widest label in left and right columns */
  335.     for (i = 0; i < 3; i++)
  336.     {
  337.     if ( (width = TextLength(&TextRP, LeftLabels[i],
  338.         (WORD)strlen(LeftLabels[i]))) > LeftWidth)
  339.     {
  340.         LeftWidth = width;
  341.     }
  342.     if ( (width = TextLength(&TextRP, RightLabels[i],
  343.         (WORD)strlen(RightLabels[i]))) > RightWidth)
  344.     {
  345.         RightWidth = width;
  346.     }
  347.     }
  348.  
  349.     FarRightWidth = TextLength(&TextRP, settinga, strlen(settinga));
  350.     i = TextLength(&TextRP, settingb, strlen(settingb));
  351.     if (i > FarRightWidth)
  352.     {
  353.     FarRightWidth = i;
  354.     }
  355.  
  356.     /*  We obligingly perform the following operation, required of
  357.     any program that uses the toolkit.  It gives the toolkit a
  358.     place to stuff context data: */
  359.     gad = CreateContext(glistptr);
  360.  
  361.     /*  Fill out a NewGadget structure for each gadget we want
  362.     to create: */
  363.  
  364.     ng.ng_LeftEdge = 10;
  365.     row1 = ng.ng_TopEdge = 6+topborder;
  366.     ng.ng_Width = LeftWidth+16;
  367.     ng.ng_Height = font->tf_YSize+4;
  368.     ng.ng_GadgetText = LeftLabels[0];
  369.     ng.ng_TextAttr = tattr;
  370.     ng.ng_Flags = 0;
  371.     ng.ng_UserData = (APTR) Hit_Button1;
  372.     ng.ng_VisualInfo = vi;
  373.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  374.     TAG_DONE);
  375.  
  376.     if (gad)
  377.     {
  378.     row2 = ng.ng_TopEdge += gad->Height + 4;
  379.     }
  380.     ng.ng_GadgetText = LeftLabels[1];
  381.     ng.ng_UserData = (APTR) Hit_Button2;
  382.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  383.     TAG_DONE);
  384.  
  385.     if (gad)
  386.     {
  387.     row3 = ng.ng_TopEdge += gad->Height + 4;
  388.     }
  389.     ng.ng_GadgetText = LeftLabels[2];
  390.     ng.ng_UserData = (APTR) Hit_Button3;
  391.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  392.     TAG_DONE);
  393.  
  394.     if (gad)
  395.     {
  396.     row4 = ng.ng_TopEdge += gad->Height + 4;
  397.     }
  398.     ng.ng_GadgetText = LeftLabels[3];
  399.     ng.ng_UserData = (APTR) Hit_Button4;
  400.     gad = CreateGadget(BUTTON_KIND, gad, &ng,
  401.     TAG_DONE);
  402.  
  403.     if (gad)
  404.     {
  405.     WinHeight = ng.ng_TopEdge += gad->Height + 8;
  406.     }
  407.  
  408.     ng.ng_GadgetText = RightLabels[0];
  409.     ng.ng_UserData = NULL;
  410.     /*  The text in a Text gadget will be two pixels beneath a button
  411.     gadget's text. */
  412.     ng.ng_TopEdge = row1+2;
  413.     ng.ng_LeftEdge += ng.ng_Width + 26 + RightWidth;
  414.     ng.ng_Height = font->tf_YSize;
  415.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  416.     GTTX_Text, settinga,
  417.     TAG_DONE);
  418.  
  419.     if (gad)
  420.     {
  421.     WinWidth = ng.ng_LeftEdge + FarRightWidth + 2;
  422.     }
  423.  
  424.     ng.ng_GadgetText = RightLabels[1];
  425.     ng.ng_TopEdge = row2+2;
  426.     gad = CreateGadget(TEXT_KIND, gad, &ng,
  427.     GTTX_Text, settingb,
  428.     TAG_DONE);
  429.  
  430.     ng.ng_GadgetText = RightLabels[2];
  431.     ng.ng_TopEdge = row3;
  432.     ng.ng_UserData = (APTR) Hit_Check1;
  433.     gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  434.     GTCB_Checked, check1,
  435.     TAG_DONE);
  436.  
  437.     ng.ng_GadgetText = RightLabels[3];
  438.     ng.ng_TopEdge = row4;
  439.     ng.ng_UserData = (APTR) Hit_Check2;
  440.     gad = CreateGadget(CHECKBOX_KIND, gad, &ng,
  441.     GTCB_Checked, check2,
  442.     TAG_DONE);
  443.  
  444.     CloseFont(font);
  445.     return(gad);
  446. }
  447.  
  448.  
  449. /*------------------------------------------------------------------------*/
  450.  
  451. /*/ HandleEvent()
  452.  *
  453.  * Handle an Intuition event.  Returns TRUE if a terminating
  454.  * event was found.
  455.  *
  456.  */
  457.  
  458. BOOL HandleEvent( struct Window *win)
  459. {
  460.     BOOL terminated = FALSE;
  461.     void (*fptr)(struct Window *, struct Gadget *);
  462.     struct IntuiMessage *imsg;
  463.     ULONG imsgClass;
  464.     struct Gadget *gad;
  465.  
  466.     Wait (1 << win->UserPort->mp_SigBit);
  467.     /*  GT_GetIMsg() returns a cooked-up IntuiMessage with
  468.     more friendly information for complex gadget classes.  Use
  469.     it wherever you get IntuiMessages: */
  470.     while (imsg = GT_GetIMsg(win->UserPort))
  471.     {
  472.     imsgClass = imsg->Class;
  473.     /*  Presuming a gadget, of course, but no harm... */
  474.     gad = (struct Gadget *)imsg->IAddress;
  475.     GT_ReplyIMsg(imsg);
  476.     switch (imsgClass)
  477.     {
  478.         case GADGETUP:
  479.         if (fptr = (void (*)(struct Window *, struct Gadget *))gad->UserData)
  480.         {
  481.             (*fptr)(win, gad);
  482.         }
  483.         break;
  484.  
  485.         case CLOSEWINDOW:
  486.         terminated = TRUE;
  487.         break;
  488.  
  489.         case REFRESHWINDOW:
  490.         /*  You must use GT_BeginRefresh() where you would
  491.             normally have your first BeginRefresh() */
  492.         GT_BeginRefresh(win);
  493.         GT_EndRefresh(win, TRUE);
  494.         break;
  495.     }
  496.     }
  497.     return(terminated);
  498. }
  499.  
  500.  
  501. /*------------------------------------------------------------------------*/
  502.  
  503. /*/ Hit_Button1()
  504.  *
  505.  * Button1 gadget handler.
  506.  *
  507.  */
  508.  
  509. void Hit_Button1( struct Window *win, struct Gadget *gad)
  510. {
  511.     printf("Clicked on Button1\n");
  512. }
  513.  
  514.  
  515. /*------------------------------------------------------------------------*/
  516.  
  517. /*/ Hit_Button2()
  518.  *
  519.  * Button2 gadget handler.
  520.  *
  521.  */
  522.  
  523. void Hit_Button2( struct Window *win, struct Gadget *gad)
  524. {
  525.     printf("Clicked on Button2\n");
  526. }
  527.  
  528.  
  529. /*------------------------------------------------------------------------*/
  530.  
  531. /*/ Hit_Button3()
  532.  *
  533.  * Button3 gadget handler.
  534.  *
  535.  */
  536.  
  537. void Hit_Button3( struct Window *win, struct Gadget *gad)
  538. {
  539.     printf("Clicked on Button3\n");
  540. }
  541.  
  542.  
  543. /*------------------------------------------------------------------------*/
  544.  
  545. /*/ Hit_Button4()
  546.  *
  547.  * Button4 gadget handler.
  548.  *
  549.  */
  550.  
  551. void Hit_Button4( struct Window *win, struct Gadget *gad)
  552. {
  553.     printf("Clicked on Button4\n");
  554. }
  555.  
  556.  
  557. /*------------------------------------------------------------------------*/
  558.  
  559. /*/ Hit_Check1()
  560.  *
  561.  * Check1 gadget handler.
  562.  *
  563.  */
  564.  
  565. void Hit_Check1( struct Window *win, struct Gadget *gad)
  566. {
  567.     printf("Check1 now ");
  568.     if (gad->Flags & SELECTED)
  569.     printf("on.\n");
  570.     else
  571.     printf("off.\n");
  572. }
  573.  
  574.  
  575. /*------------------------------------------------------------------------*/
  576.  
  577. /*/ Hit_Check2()
  578.  *
  579.  * Check2 gadget handler.
  580.  *
  581.  */
  582.  
  583. void Hit_Check2( struct Window *win, struct Gadget *gad)
  584. {
  585.     printf("Check2 now ");
  586.     if (gad->Flags & SELECTED)
  587.     printf("on.\n");
  588.     else
  589.     printf("off.\n");
  590. }
  591.  
  592.  
  593. /*------------------------------------------------------------------------*/
  594.