home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / ScalosPrefs / examples / C / big.c < prev    next >
C/C++ Source or Header  |  1980-08-02  |  14KB  |  443 lines

  1. /*  Big example, perhaps something like what you'd use in a program :) */
  2.  
  3. /* Always included first */
  4. #include <exec/types.h>
  5.  
  6. /* So we can use the functions from these libraries */
  7. #include <proto/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/gadtools.h>
  10. #include <proto/intuition.h>
  11. #include <proto/preferences.h>
  12. #include <proto/graphics.h>
  13.  
  14. /* Intuition stuff */
  15. #include <intuition/intuitionbase.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/screens.h>
  18.  
  19. /* Gadtools stuff */
  20. #include <libraries/gadtools.h>
  21.  
  22. /* For the MAKE_ID macro */
  23. #include <libraries/iffparse.h>
  24.  
  25. /* For the PrefsStruct structure */
  26. #include <scalos/preferences.h>
  27.  
  28. /* some standard stuff */
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include <clib/alib_protos.h>
  33.  
  34. /* Library bases */
  35. struct Library          *GadToolsBase;
  36. struct IntuitionBase    *IntuitionBase;
  37. struct Library          *PreferencesBase;
  38. struct GfxBase          *GfxBase;
  39.  
  40. /* Font (TextAttr) to use for the gadtools gadgets, simply hardcoded for this simple example.
  41.  * Pointers to each one of the gadgets, so it is easier to work with them after creation.
  42.  */
  43. struct TextAttr ta = {"topaz.font", 8, 0, 0};
  44. struct Gadget   *gadgets[3] = {0};
  45.  
  46. /* Function Prototypes */
  47. int             OpenLibs(void);
  48. void            CloseLibs(void);
  49. struct Gadget   *CreateGadList(struct Screen *scr, APTR vi, WORD ww, WORD wh);
  50. void            FreeGads(struct Gadget *glist);
  51. struct Window   *openwin(WORD wx, WORD wy, WORD ww, WORD wh);
  52. struct Node     *AllocNode(STRPTR name);
  53. void            FreeNode(struct Node *node);
  54.  
  55.  
  56. int main(void)
  57. {
  58.     struct Screen   *scr;           /* screen we want to open on */
  59.     struct Window   *win;           /* Pointer to window */
  60.     struct Gadget   *gad;           /* Pointer to gadtools gadgetlist */
  61.     APTR            vi;             /* visualinfo for the screen */
  62.  
  63.     /* Window position and sizes */
  64.     WORD    wx = 170;
  65.     WORD    wy = 100;
  66.     WORD    ww = 300;
  67.     WORD    wh = 200;
  68.  
  69.     int             quit = 0;       /* flag to show when to quit program */
  70.  
  71.     struct IntuiMessage *imsg;      /* For communicating with intuition */
  72.     ULONG               cls;        /* class of window event */
  73.     UWORD               evc;        /* eventcode */
  74.     struct Gadget       *gadhit;    /* for gadgetup events */
  75.     UWORD               lasthit;    /* last item in listview hit */
  76.  
  77.     struct List         textlist;
  78.     struct Node         *ptr;       /* working pointer for traversing list */
  79.  
  80.     APTR                prefhandle; /* handle for accessing our preferences */
  81.     char                temp[256];  /* temporary storage for when reading string prefs */
  82.  
  83.     NewList(&textlist);             /* Initialise list */
  84.  
  85.     /* Open all required libraries and exit if any were failed to open */
  86.     if(!OpenLibs())
  87.     {
  88.         CloseLibs();
  89.         return(20);     
  90.     }
  91.  
  92.  
  93.     /* Try to read in some preferences */
  94.     if(NULL != (prefhandle = AllocPrefsHandle("Example")) )
  95.     {
  96.         /* First, try to read the file that the preferences were stored in */
  97.         ReadPrefsHandle(prefhandle, "example.prefs");
  98.  
  99.         /* Read window position preferences. If the tags do not exist (i.e. if they
  100.          * were not in the preferences file, or the preferences file was not loaded)
  101.          * then nothing will be copied into the destination memory. This means that
  102.          * the current contents of the positions/sizes will not be corrupted.
  103.          * We use the tag values for OpenWindowTagList for the window positions/sizes
  104.          * for the simple reason is that it makes it clearer what we are doing - plus it
  105.          * saves us some thinking to create our own tag values (which of course can be
  106.          * anything) :)
  107.          */
  108.         GetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Left, &wx, 2);
  109.         GetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Top, &wy, 2);
  110.         GetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Width, &ww, 2);
  111.         GetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Height, &wh, 2);
  112.  
  113.         /* Read a list of strings from the preferences that have been stored.
  114.          * If we try to read an item from the list which does not exist, we have
  115.          * reached the end of the list (remember that it is a list, not an array and
  116.          * that inserting at position 2000 may not insert at exactly position 2000 -
  117.          * if the list has onoly one entry, then it will get inserted after that
  118.          * and effectively be inserted at position 2
  119.          */
  120.         evc = 0;    /* Treat this as the entry number */
  121.         while(GetEntry(prefhandle, MAKE_ID('T','E','X','T'), 1, temp, 256, evc))
  122.         {
  123.             ptr = AllocNode((STRPTR)temp); if(ptr) AddTail(&textlist, ptr);
  124.             evc++;
  125.         }
  126.     }
  127.  
  128.     /* Get default public screen for our use */
  129.     if(NULL != (scr = LockPubScreen(NULL)) )
  130.     {
  131.         /* Get visual info for that screen (needed for creating gadtools gadgets)
  132.          * and then create the gadgets
  133.          */
  134.         vi = GetVisualInfoA(scr, NULL);
  135.         if(NULL != (gad = CreateGadList(scr, vi, ww, wh)) )
  136.         {
  137.             /* Set the list of text items for the Listview to display and try
  138.              * to open our window
  139.              */
  140.             GT_SetGadgetAttrs(gadgets[0], NULL, NULL, GTLV_Labels, &textlist, TAG_DONE);
  141.             if(NULL != (win = openwin(wx, wy, ww, wh)) )
  142.             {
  143.                 /* Add gadget list to window. Refresh the gadget list (causing it to be
  144.                  * drawn. Refresh the window with special processing for gadtools gadgets
  145.                  */
  146.                 AddGList(win, gad, -1, -1, 0);
  147.                 RefreshGList(gad, win, 0, -1);
  148.                 GT_RefreshWindow(win, 0);
  149.  
  150.                 /* window opened, do stuff */
  151.                 while(!quit)
  152.                 {
  153.                     /* Wait for messages on the window's IDCMP port */
  154.                     Wait(1 << win->UserPort->mp_SigBit);
  155.  
  156.                     /* while messages can be read from the window's IDCMP port */
  157.                     while(NULL != (imsg = GT_GetIMsg(win->UserPort)) )
  158.                     {
  159.                         /* Take a copy of the information we are interested in and the reply the
  160.                          * message
  161.                          */
  162.                         cls = imsg->Class;
  163.                         evc = imsg->Code;
  164.                         gadhit = (struct Gadget *)imsg->IAddress;
  165.                         GT_ReplyIMsg(imsg);
  166.                         switch(cls)
  167.                         {
  168.                             case IDCMP_CLOSEWINDOW:
  169.                                 /* window close gadget pressed, set flag to quit program */
  170.                                 quit = 1;
  171.                                 break;
  172.  
  173.                             case IDCMP_GADGETUP:
  174.                                 /* gadget has been released, do processing depending on what gadget was hit */
  175.                                 switch(gadhit->GadgetID)
  176.                                 {
  177.                                     case 1:
  178.                                         /* listview. take a copy of the number of the item that was pressed.
  179.                                          * move to that item in the list attached to the listview and
  180.                                          * set the string in the string gadget (and yes, I know that I could
  181.                                          * have used GTLV_ShowSelected with the pointer to the string gadget,
  182.                                          * but I didn't OK).
  183.                                          */
  184.                                         lasthit = evc;
  185.                                         ptr = textlist.lh_Head;
  186.                                         while(evc-- && ptr->ln_Succ)
  187.                                         {
  188.                                             ptr = ptr->ln_Succ;
  189.                                         }
  190.                                         if(ptr->ln_Succ) GT_SetGadgetAttrs(gadgets[1], win, NULL, GTST_String, ptr->ln_Name, TAG_DONE);
  191.                                         break;
  192.  
  193.                                     case 2:
  194.                                         /* string gadget. detach the list from the listview and then try to add
  195.                                          * a new node to the list which contains the string in the string gadget.
  196.                                          * re-attach the list to the listview
  197.                                          */
  198.                                         GT_SetGadgetAttrs(gadgets[0], win, NULL, GTLV_Labels, ~0, TAG_DONE);
  199.                                         ptr = AllocNode((STRPTR)((struct StringInfo *)gadhit->SpecialInfo)->Buffer); if(ptr) AddTail(&textlist, ptr);
  200.                                         GT_SetGadgetAttrs(gadgets[0], win, NULL, GTLV_Labels, &textlist, TAG_DONE);
  201.                                         break;
  202.  
  203.                                     case 3:
  204.                                         /* button. detach list from listview, move to position in list for the
  205.                                          * last item that was pressed in the listview. remove item from list
  206.                                          * and free the memory. re-attach the list to the listview
  207.                                          */
  208.                                         GT_SetGadgetAttrs(gadgets[0], win, NULL, GTLV_Labels, ~0, TAG_DONE);
  209.                                         ptr = textlist.lh_Head;
  210.                                         while(lasthit-- && ptr->ln_Succ)
  211.                                         {
  212.                                             ptr = ptr->ln_Succ;
  213.                                         }
  214.                                         if(ptr->ln_Succ)
  215.                                         {
  216.                                             Remove(ptr);
  217.                                             FreeNode(ptr);
  218.                                         }
  219.                                         GT_SetGadgetAttrs(gadgets[0], win, NULL, GTLV_Labels, &textlist, TAG_DONE);
  220.                                         break;
  221.                                 }
  222.                                 break;
  223.  
  224.                             case IDCMP_NEWSIZE:
  225.                                 /* window has been resized. Take a copy of the new window sizes. Remove and free
  226.                                  * the gadgets from the window and try to re-create them with the new sizes.
  227.                                  */
  228.                                 ww = win->Width;
  229.                                 wh = win->Height;
  230.                                 RemoveGList(win, gad, -1);
  231.                                 FreeGads(gad);
  232.                                 if(NULL == (gad = CreateGadList(scr, vi, ww, wh)) )
  233.                                 {
  234.                                     /* Gadgets were not created, so we just set the flag to quit the program */
  235.                                     quit = 1;
  236.                                 }
  237.                                 else
  238.                                 {
  239.                                     /* gadgets were created OK, so attach the list to the listview. add the gadgets
  240.                                      * to the window, clear the inside of the window (not borders). refresh the
  241.                                      * window borders, then the gadgets with special processing for gadtools ones
  242.                                      */
  243.                                     GT_SetGadgetAttrs(gadgets[0], NULL, NULL, GTLV_Labels, &textlist, TAG_DONE);
  244.                                     AddGList(win, gad, -1, -1, 0);
  245.                                     SetAPen(win->RPort, 0);
  246.                                     RectFill(win->RPort, win->BorderLeft, win->BorderTop, win->Width-win->BorderRight, win->Height-win->BorderBottom);
  247.                                     RefreshWindowFrame(win);
  248.                                     RefreshGList(gad, win, 0, -1);
  249.                                     GT_RefreshWindow(win, 0);
  250.                                 }
  251.                                 break;
  252.                         }
  253.                     } /* while get message */
  254.  
  255.                 } /* while !quit */
  256.  
  257.                 /* take copy of final x/y position of window. don't need to take the sizes,
  258.                  * as we already have them from the IDCMP_NEWSIZE messages. Remove gadgets from window
  259.                  * and close the window.
  260.                  */
  261.                 wx = win->LeftEdge;
  262.                 wy = win->TopEdge;
  263.                 RemoveGList(win, gad, -1);
  264.                 CloseWindow(win);
  265.             }
  266.             /* Free gadgets */
  267.             FreeGads(gad);
  268.  
  269.         } /* if opened window */
  270.         /* free visual info for screen and unlock the public screen */
  271.         FreeVisualInfo(vi);
  272.         UnlockPubScreen(NULL, scr);
  273.     } /* if opened default pubscreen */
  274.  
  275.     /* Save prefs. If we do not have a valid PrefsHandle, try to allocate one here. */
  276.     if(!prefhandle) prefhandle = AllocPrefsHandle("Example");
  277.     if(prefhandle)
  278.     {
  279.         /* Set all the new window positions/sizes */
  280.         SetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Left, &wx, 2);
  281.         SetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Top, &wy, 2);
  282.         SetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Width, &ww, 2);
  283.         SetPreferences(prefhandle, MAKE_ID('W','N','D','O'), WA_Height, &wh, 2);
  284.  
  285.         /* Clear the previous list preferences so that we are sure to only save what we add using SetEntry */
  286.         while(RemEntry(prefhandle, MAKE_ID('T','E','X','T'), 1, 0)){;}
  287.  
  288.         /* go through the list that was attached to the listview and store each string
  289.          * as an item in a multiple preference per tag item.
  290.          */
  291.         evc = 0;    /* Treat this as the entry number */
  292.         ptr = textlist.lh_Head;
  293.         while(ptr->ln_Succ)
  294.         {
  295.             if(ptr->ln_Name) SetEntry(prefhandle, MAKE_ID('T','E','X','T'), 1, ptr->ln_Name, strlen(ptr->ln_Name)+1, evc);
  296.             evc++;
  297.             ptr = ptr->ln_Succ;
  298.         }
  299.  
  300.         /* Save the preferences and then free them */
  301.         WritePrefsHandle(prefhandle, "example.prefs");
  302.         FreePrefsHandle(prefhandle);
  303.     }
  304.  
  305.     /* Finally, free all memory for the list */
  306.     while(NULL != (ptr = RemHead(&textlist)) )
  307.     {
  308.         FreeNode(ptr);
  309.     }
  310.  
  311.     CloseLibs();
  312. }
  313.  
  314.  
  315. /* Opens all required libraries for this program and returns true (for success) or
  316.  * false (for failure)
  317.  */
  318. int             OpenLibs(void)
  319. {
  320.     GadToolsBase = OpenLibrary("gadtools.library", 36);
  321.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 36);
  322.     PreferencesBase = OpenLibrary("preferences.library", 39);
  323.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 36);
  324.     if(GadToolsBase && IntuitionBase && PreferencesBase && GfxBase) return(1);
  325.     return(0);
  326. }
  327.  
  328.  
  329. /* closes all opened libraries */
  330. void            CloseLibs(void)
  331. {
  332.     if(GadToolsBase) CloseLibrary(GadToolsBase);
  333.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  334.     if(PreferencesBase) CloseLibrary(PreferencesBase);
  335.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  336. }
  337.  
  338.  
  339. /* creates gadget list */
  340. struct Gadget   *CreateGadList(struct Screen *scr, APTR vi, WORD ww, WORD wh)
  341. {
  342.     struct Gadget       *gad, *glist;
  343.     struct NewGadget    ng[] = {{8,20,0,0,NULL,&ta,1,0,0,0},
  344.                                 {8,0,0,14,NULL,&ta,2,0,0,0},
  345.                                 {0,0,40,14,"Del",&ta,3,16,0,0}};
  346.  
  347.     ng[0].ng_Width = ww - 40;
  348.     ng[0].ng_Height = wh - 60;
  349.     ng[0].ng_VisualInfo = vi;
  350.  
  351.     ng[1].ng_TopEdge = 20 + wh - 60;
  352.     ng[1].ng_Width = ww - 80;
  353.     ng[1].ng_VisualInfo = vi;
  354.  
  355.     ng[2].ng_LeftEdge = 8 + ww - 80;
  356.     ng[2].ng_TopEdge = 20 + wh - 60;
  357.     ng[2].ng_VisualInfo = vi;
  358.  
  359.     if(NULL == (gad = CreateContext(&glist)) ) return(NULL);
  360.  
  361.     if(NULL == (gad = CreateGadget(LISTVIEW_KIND, gad, &ng[0], GTLV_Labels, NULL, TAG_DONE)) )
  362.     {
  363.         FreeGads(glist);
  364.         return(NULL);
  365.     }
  366.     gadgets[0] = gad;
  367.  
  368.     if(NULL == (gad = CreateGadget(STRING_KIND, gad, &ng[1], GTST_MaxChars, 256, TAG_DONE)) )
  369.     {
  370.         FreeGads(glist);
  371.         return(NULL);
  372.     }
  373.     gadgets[1] = gad;
  374.  
  375.     if(NULL == (gad = CreateGadgetA(BUTTON_KIND, gad, &ng[2], NULL)) )
  376.     {
  377.         FreeGads(glist);
  378.         return(NULL);
  379.     }
  380.     gadgets[2] = gad;
  381.  
  382.     return(glist);
  383. }
  384.  
  385.  
  386. /* frees gadget list if it exists */
  387. void            FreeGads(struct Gadget *glist)
  388. {
  389.     if(glist) FreeGadgets(glist);
  390. }
  391.  
  392.  
  393. /* open window (just takes the huge mess of the taglist out of the main function */
  394. struct Window   *openwin(WORD wx, WORD wy, WORD ww, WORD wh)
  395. {
  396.     return(OpenWindowTags(NULL,  WA_Left, wx,
  397.                                  WA_Top, wy,
  398.                                  WA_Width, ww,
  399.                                  WA_Height, wh,
  400.                                  WA_Flags, WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SIZEGADGET|WFLG_ACTIVATE|WFLG_NOCAREREFRESH,
  401.                                  WA_IDCMP, IDCMP_CLOSEWINDOW|IDCMP_NEWSIZE|IDCMP_GADGETUP,
  402.                                  WA_MinWidth, 150,
  403.                                  WA_MinHeight, 150,
  404.                                  WA_MaxWidth, 640,
  405.                                  WA_MaxHeight, 480,
  406.                                  WA_AutoAdjust, 1,
  407.                                  WA_PubScreen, NULL,
  408.                                  TAG_DONE));
  409. }
  410.  
  411.  
  412. /* allocates a Node and some memory for the ln_Name and copies the specified name into it */
  413. struct Node     *AllocNode(STRPTR name)
  414. {
  415.     struct  Node    *node;
  416.  
  417.     if(NULL != (node = AllocVec(sizeof(struct Node), MEMF_PUBLIC|MEMF_CLEAR)) )
  418.     {
  419.         if(name)
  420.         {
  421.             if(NULL != (node->ln_Name = AllocVec(strlen(name)+1, MEMF_PUBLIC|MEMF_CLEAR)) )
  422.             {
  423.                 CopyMem(name, node->ln_Name, strlen(name));
  424.             }
  425.         }
  426.     }
  427.     return(node);
  428. }
  429.  
  430.  
  431. /* Frees a Node and also the name */
  432. void            FreeNode(struct Node *node)
  433. {
  434.     if(node)
  435.     {
  436.         if(node->ln_Name) FreeVec(node->ln_Name);
  437.         FreeVec(node);
  438.     }
  439. }
  440.  
  441.  
  442.  
  443.