home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / egs-tools / egs_dev-disk / egsdemos / egsexamples / gadgets / scrollbox / scrollbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  6.0 KB  |  258 lines

  1. /*
  2. *  $
  3. *  $ FILE     : scrollbox.c
  4. *  $ VERSION  : 1
  5. *  $ REVISION : 48
  6. *  $ DATE     : 08-Dec-93 14:59
  7. *  $
  8. *  $ Author   : mvk
  9. *  $
  10. *
  11. *  This is an example for gbscrollbox.library
  12. *
  13. *  It creates a scrollbox with five items, if you click into a item
  14. *  it is delete. If you size the window the all items are displayed
  15. *  again.
  16. *
  17. */
  18. /*
  19.     Related Files: gbscrollbox.doc, gbscrollbox.h <- Our Scrollbox gadget
  20.            egsintui.doc, egsintui.h  <- window stuff
  21.            egsgadbox.doc, egsgadbox.h <- The actual gadbox
  22.  main.c
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <exec/types.h>
  28. #include <dos/dos.h>
  29. #include <exec/memory.h>
  30. #include <proto/dos.h>
  31. #include <proto/exec.h>
  32. #include <egs/egs.h>
  33. #include <egs/egsintui.h>
  34. #include <egs/egsgadbox.h>
  35. #include <egs/egb/gbscrollbox.h>
  36. #include <egs/proto/all.h>
  37.  
  38. /*
  39. **  Protos
  40. */
  41. EI_WindowPtr CreateScrollWindow (EI_WindowPtr win);
  42. APTR My_FindGadget(EB_GadContext GadCon,WORD ID);
  43. void My_AddItems(EB_GadContext GadCon,EI_WindowPtr win);
  44. void Clean_Exit (void);
  45.  
  46. struct Library *EGSIntuiBase=NULL;         // EGSIntuition
  47. struct Library *EGBScrollBase=NULL;        // ScrollBox
  48. struct Library *EGBBase=NULL;              // gadbox
  49.  
  50. EB_GadContext GadCon;
  51. EI_WindowPtr window;
  52. struct Node  item1,item2,item3,item4,item5;
  53.  
  54. void main (void) {
  55.    EI_EIntuiMsgPtr msg;
  56.    int class;
  57.    struct EI_Gadget *iaddress;
  58.  
  59.    if (!(EGSIntuiBase = OpenLibrary ("egsintui.library",0))) {
  60.      printf ("Unable open egsintui.library.\n");
  61.      Clean_Exit ();
  62.    }
  63.    if (!(EGBScrollBase = OpenLibrary ("egb/gbscrollbox.library",0))) {
  64.      printf ("Unable open gbmenuselect.library.\n");
  65.      Clean_Exit ();
  66.    }
  67.    if (!(EGBBase = OpenLibrary ("egsgadbox.library",0))) {
  68.      printf ("Unable open egsgadbox.library.\n");
  69.      Clean_Exit ();
  70.    }
  71.  
  72.    if (!(window = CreateScrollWindow (NULL))) {
  73.      printf ("Unable to open Window.\n");
  74.      Clean_Exit ();
  75.    }
  76.  
  77.    for (;;) {  // Forever
  78.  
  79.      Wait (1<<window->UserPort->mp_SigBit);  // wait for our signals
  80.  
  81.      while ( (msg=(struct EI_EIntuiMsg *) GetMsg (window->UserPort)) != NULL) {
  82.  
  83.        class = msg->Class;
  84.        iaddress = (struct EI_Gadget *)msg->IAddress;
  85.  
  86.        switch (class) {
  87.  
  88.      case EI_iCLOSEWINDOW:
  89.       ReplyMsg ( (struct Message *)msg);
  90.         Clean_Exit ();
  91.      break;
  92.                   // User has begun to resize window
  93.      case EI_iSIZEVERIFY:
  94.          EI_LockIntuition ();
  95.          EI_RemoveGList (window,GadCon->First, GadCon->Num);
  96.          EB_DeleteGadContext (GadCon);
  97.          EI_UnlockIntuition ();
  98.       ReplyMsg ( (struct Message *)msg);
  99.      break;
  100.  
  101.      case EI_iNEWSIZE:
  102.  
  103.          EI_LockIntuition ();
  104.          window = CreateScrollWindow (window);
  105.          EI_AddGList ( window,GadCon->First,GadCon->Num );
  106.          My_AddItems( GadCon,window );                   // Add Items
  107.          EI_UnlockIntuition ();
  108.  
  109.       ReplyMsg ( (struct Message *)msg);
  110.       break;
  111.               // User has pressed a scroll gadget
  112.       case EI_iGADGETDOWN:
  113.  
  114.           printf ("ItemsName = %s\n",
  115.              ((EGB_ScrollGadPtr)iaddress)->ActText->ln_Name);
  116.  
  117.          EI_LockIntuition ();
  118.          EGB_RemItemFromScrollBox(window,iaddress,  // Delete
  119.                  ((EGB_ScrollGadPtr)iaddress)->ActText);
  120.  
  121.          EI_UnlockIntuition ();
  122.  
  123.       ReplyMsg ( (struct Message *)msg);
  124.       break;
  125.        }
  126.      }
  127.    }
  128.  }
  129.  
  130. /*
  131. **
  132. ** CreateScrollGadgets
  133. **
  134. */
  135. EI_WindowPtr CreateScrollWindow (EI_WindowPtr win)
  136. {
  137.  
  138.    EB_GadBoxPtr root;
  139.  
  140.    GadCon = EB_CreateGadContext (NULL,NULL,-1,-1);
  141.  
  142.    root = EB_CreateHorizBox (GadCon);
  143.    EB_AddLastSon (root,EB_CreateHorizFill (GadCon,EB_FILL_ALL,0));
  144.    EB_AddLastSon (root,EGB_CreateLateScrollBox (GadCon,10,20,10,40,0,0x100));
  145.    EB_AddLastSon (root,EB_CreateHorizFill (GadCon,EB_FILL_ALL,0));
  146.  
  147.    root = EB_CreateMasterWindow (GadCon,win,root);
  148.  
  149.    if (EB_ProcessGadBoxes (GadCon,root))
  150.    {
  151.  
  152.     if (win==NULL) {
  153.       GadCon->NewWin->IDCMPFlags        |= EI_iCLOSEWINDOW |
  154.                                                  EI_iSIZEVERIFY  |
  155.                                            EI_iNEWSIZE     |
  156.                                            EI_iGADGETUP    |
  157.                                            EI_iGADGETDOWN;
  158.  
  159.       GadCon->NewWin->Bordef.SysGadgets |= (EI_WINDOWCLOSE)|(EI_WINDOWSIZE);
  160.  
  161.       GadCon->NewWin->Flags             |= EI_SIZEBBOTTOM;
  162.  
  163.       win=EI_OpenWindow (GadCon->NewWin);
  164.  
  165.       My_AddItems(GadCon,win);
  166.  
  167.     }
  168.     return (win);
  169.  
  170.   }else
  171.     return NULL;
  172. }
  173.  
  174. /*
  175. **
  176. **  Make Items
  177. **
  178. */
  179. void My_AddItems(EB_GadContext GadCon,EI_WindowPtr win)
  180. {
  181.       EI_GadgetPtr gad;
  182.       /*
  183.       **  Find Scrollbox  Gadget
  184.       */
  185.       gad=My_FindGadget(GadCon,0x100);
  186.  
  187.       if(gad)
  188.       {
  189.  
  190.        /*
  191.        ** if ln_Pri = 1then item is on the top of the list
  192.        ** and highlight.
  193.        */
  194.  
  195.        item1.ln_Pri=0;
  196.        item2.ln_Pri=0;
  197.        item3.ln_Pri=1;    // First
  198.        item4.ln_Pri=0;
  199.        item5.ln_Pri=0;
  200.  
  201.        /*
  202.        **   Itemnames
  203.        */
  204.  
  205.        item1.ln_Name= "One";
  206.        item2.ln_Name= "Two";
  207.        item3.ln_Name= "Three";
  208.        item4.ln_Name= "Four";
  209.        item5.ln_Name= "Five";
  210.  
  211.        /*
  212.        ** Add Items to Scollbox
  213.        */
  214.        EGB_AddItemToScrollBox(win,gad,&item1);
  215.        EGB_AddItemToScrollBox(win,gad,&item2);
  216.        EGB_AddItemToScrollBox(win,gad,&item3);
  217.        EGB_AddItemToScrollBox(win,gad,&item4);
  218.        EGB_AddItemToScrollBox(win,gad,&item5);
  219.        EGB_ActivateElem(win,gad,&item1);
  220.        EGB_UpdateScrollBox(window,gad);  // Is used to add items, if
  221.                                          // a window is already opened
  222.  
  223.      }
  224. }
  225.  
  226. /*
  227. **
  228. ** This function searched for Subgadget in a MasterGadget,
  229. ** because all Gadgets are in a MasterWindow.
  230. **
  231. */
  232. APTR My_FindGadget(EB_GadContext GadCon,WORD ID)
  233. {
  234.     /*
  235.     ** MasterGadPtr because the window is create with
  236.     ** EB_CreateMasterWindow function
  237.     */
  238.     return( EB_FindGadget(
  239.         ((EI_MasterGadPtr)GadCon->First)->FirstSon,
  240.         ((EI_MasterGadPtr)GadCon->First)->NumSons,ID));
  241. }
  242.  
  243. void Clean_Exit (void) {
  244.  
  245.     if (window)
  246.       EI_CloseWindow (window);
  247.     if (GadCon)
  248.       EB_DeleteGadContext (GadCon);
  249.     if (EGBBase)
  250.       CloseLibrary (EGBBase);
  251.     if (EGBScrollBase)
  252.       CloseLibrary (EGBScrollBase);
  253.     if (EGSIntuiBase)
  254.       CloseLibrary (EGSIntuiBase);
  255.  
  256.     exit (0);
  257. }
  258.