home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / midi / mod2midi / select.c < prev    next >
C/C++ Source or Header  |  1994-06-12  |  10KB  |  419 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include <dos/dos.h>
  5. #include <exec/lists.h>
  6. #include <exec/memory.h>
  7. #include <exec/types.h>
  8. #include <intuition/intuition.h>
  9. #include <libraries/gadtools.h>
  10.  
  11. #include <clib/alib_protos.h>
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/gadtools_protos.h>
  15. #include <clib/intuition_protos.h>
  16.  
  17. #include "mod2midi.h"
  18.  
  19. #define LVNODE_ID 101
  20. #define GAD_MODIFY 1
  21. #define GAD_DONE 2
  22. #define GAD_LISTVIEW 3
  23. #define GAD_OK 4
  24. #define GAD_CANCEL 5
  25. #define GAD_ENTRY 6
  26. #define GAD_TEXT 7
  27.  
  28. /* Function Prototypes */
  29. int HandleGadgetMessages( struct Window *win, UBYTE HandlerMode );
  30. struct lv_Node *NewLVNode( struct List *list, char *name );
  31. void FreeList( struct List *list );
  32. int ScrollChoice( string title, string *sp, int w );
  33. string DialogBox( string *sp, string def );
  34.  
  35. extern char *version;
  36.  
  37. struct Library *IntuitionBase = NULL, *GadToolsBase = NULL;
  38.  
  39. struct lv_Node
  40. {
  41.     struct Node nn_Node;
  42.     char Info[80];
  43. };
  44.  
  45. char EnteredValue[31]; //Hate doing this, but everythings a kludge anyway
  46. char defValue[31];
  47. struct Gadget *gadENTRY;
  48.  
  49. int ScrollChoice( string title, string *sp, int w )
  50. {
  51.     struct Screen *screen;
  52.     struct Window *win;
  53.  
  54.     void *vi = NULL;
  55.  
  56.     struct Gadget *gadContext = NULL;
  57.     struct Gadget *gadList = NULL;
  58.     struct Gadget *gadMODIFY, *gadDONE, *gadLISTVIEW;
  59.     struct NewGadget gadNew;
  60.  
  61.     struct List *LView;
  62.  
  63.     struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0 };
  64.  
  65. //    struct Process *process = NULL;
  66.  
  67.     int TBSize, Selected = -1;
  68.  
  69.     w++; //Maxmium width of entry list in characters
  70.  
  71.     IntuitionBase = OpenLibrary( "intuition.library", 37 );
  72.     GadToolsBase = OpenLibrary( "gadtools.library", 37 );
  73.  
  74.     if ( IntuitionBase != NULL && GadToolsBase != NULL )
  75.     {
  76. //        process = (struct Process *)FindTask( NULL );
  77. //        screen = ( (struct Window *)process->pr_WindowPtr )->WScreen;
  78.  
  79.         screen = LockPubScreen( NULL );
  80.  
  81.         if ( screen != NULL )
  82.         {
  83.             TBSize = screen->WBorTop + screen->Font->ta_YSize + 1;
  84.             vi = GetVisualInfo( screen, TAG_END );
  85.  
  86.             LView = AllocMem( sizeof( struct List ), MEMF_CLEAR );
  87.             if ( LView != NULL )
  88.             {
  89.                 string *t = sp;
  90.  
  91.                 NewList( LView );
  92.  
  93.                 /* Add the names to the listview */
  94.                 while ( *t != NULL )
  95.                 {
  96.                     NewLVNode( LView, *(t++) );
  97.                 }
  98.  
  99.                 gadList = CreateContext( &gadContext );
  100.  
  101.                 gadNew.ng_LeftEdge        =    11;
  102.                 gadNew.ng_TopEdge            =    120 + TBSize;
  103.                 gadNew.ng_Width                =    69;
  104.                 gadNew.ng_Height            =    14;
  105.                 gadNew.ng_GadgetText = "Modify";
  106.                 gadNew.ng_TextAttr        =    &Topaz80;
  107.                 gadNew.ng_VisualInfo    =    vi;
  108.                 gadNew.ng_Flags                =    PLACETEXT_IN;
  109.                 gadNew.ng_GadgetID = GAD_MODIFY;
  110.                 gadMODIFY = gadList = CreateGadget( BUTTON_KIND, gadList, &gadNew,
  111.                                                                      TAG_END );
  112.  
  113.                 gadNew.ng_LeftEdge        =    99 + ( w * 8 - 80 ) - 69;
  114.                 gadNew.ng_GadgetText = "Done";
  115.                 gadNew.ng_GadgetID = GAD_DONE;
  116.                 gadDONE = gadList = CreateGadget( BUTTON_KIND, gadList, &gadNew,
  117.                                                                              TAG_END );
  118.  
  119.                 gadNew.ng_LeftEdge        =    11;
  120.                 gadNew.ng_TopEdge            =    4 + TBSize;
  121.                 gadNew.ng_Width                =    99 + ( w * 8 - 80 );
  122.                 gadNew.ng_Height            =    100;
  123.                 gadNew.ng_Flags                =    PLACETEXT_ABOVE;
  124.                 gadNew.ng_GadgetText = "";
  125.                 gadNew.ng_GadgetID = GAD_LISTVIEW;
  126.                 gadLISTVIEW = gadList = CreateGadget( LISTVIEW_KIND, gadList, &gadNew,
  127.                                                             GTLV_ShowSelected,    NULL,
  128.                                                             GTLV_Selected, 0,
  129.                                                             GTLV_Labels, LView,
  130.                                                             TAG_END );
  131.  
  132.                 win = OpenWindowTags( NULL,
  133.                                 WA_Left, 172,
  134.                                 WA_Top, 28,
  135.                                 WA_Width, 120 + ( w * 8 - 80 ),
  136.                                 WA_Height, 139 + TBSize,
  137.                                 WA_CloseGadget, FALSE,
  138.                                 WA_DepthGadget, TRUE,
  139.                                 WA_Gadgets, gadContext,
  140.                                 WA_DragBar, TRUE,
  141.                                 WA_Activate, TRUE,
  142.                                 WA_CustomScreen, screen,
  143.                                 WA_IDCMP, BUTTONIDCMP | LISTVIEWIDCMP |
  144.                                                     IDCMP_REFRESHWINDOW | IDCMP_CLOSEWINDOW,
  145.                                 WA_Title, title,
  146.                                 WA_ScreenTitle, &version[5],
  147.                                 TAG_DONE, NULL);
  148.  
  149.                 if ( win != NULL )
  150.                 {
  151.                     /* Update gadgets */
  152.                     GT_RefreshWindow( win, NULL );
  153.  
  154.                     Selected = HandleGadgetMessages( win, HM_LISTVIEW );
  155.  
  156.                     CloseWindow( win );
  157.                     FreeGadgets( gadContext );
  158.                 }
  159.                 FreeList( LView );
  160.             }
  161.             UnlockPubScreen( NULL, screen );
  162.         }
  163.  
  164.         if ( vi != NULL ) FreeVisualInfo( vi );
  165.     }
  166.  
  167.     if ( GadToolsBase != NULL ) CloseLibrary( GadToolsBase );
  168.     if ( IntuitionBase != NULL ) CloseLibrary( IntuitionBase );
  169.  
  170.     return Selected;
  171. }
  172.  
  173. int HandleGadgetMessages( struct Window *win, UBYTE HandlerMode )
  174. {
  175.     struct IntuiMessage *msg;
  176.     struct Gadget *gadget;
  177.  
  178.     int Selected = 1; //Default to the lists first entry
  179.     UWORD msgClass, Code;
  180.     int GadgetID;
  181.  
  182.     BOOL terminated = FALSE;
  183.  
  184.     while ( !terminated )
  185.     {
  186.         /* Wait until something happens */
  187.         Wait ( 1L << win->UserPort->mp_SigBit );
  188.  
  189.       while ( ( !terminated ) && ( msg = GT_GetIMsg( win->UserPort ) ) )
  190.         {
  191.           msgClass = msg->Class;
  192.             Code = msg->Code;
  193.           gadget = (struct Gadget *)msg->IAddress;
  194.  
  195.           GT_ReplyIMsg( msg );
  196.  
  197.           switch ( msgClass )
  198.             {
  199.                 case IDCMP_GADGETUP:
  200.                   GadgetID = gadget->GadgetID;
  201.  
  202.                     switch ( HandlerMode )
  203.                     {
  204.                         case HM_LISTVIEW:
  205.                             switch ( GadgetID )
  206.                             {
  207.                                 case GAD_MODIFY:
  208.                                     terminated = TRUE;
  209.                                 break;
  210.  
  211.                                 case GAD_LISTVIEW:
  212.                                     Selected = Code + 1;
  213.                                 break;
  214.  
  215.                                 case GAD_DONE:
  216.                                     Selected = -1;
  217.                                     terminated = TRUE;
  218.                                 break;
  219.                             }
  220.                         break;
  221.  
  222.                         case HM_ENTRY:
  223.                             switch ( GadgetID )
  224.                             {
  225.                                 case GAD_CANCEL:
  226.                                     strcpy( EnteredValue, defValue );
  227.                                     terminated = TRUE;
  228.                                 break;
  229.  
  230.                                 case GAD_OK:
  231.                                     strcpy( EnteredValue, GetString( gadENTRY ) );
  232.                                     terminated = TRUE;
  233.                                 break;
  234.                             }
  235.                         break;
  236.                     }
  237.                 break;
  238.  
  239.                 case IDCMP_RAWKEY:
  240.                 break;
  241.  
  242.                 case IDCMP_REFRESHWINDOW:
  243.                     /* Required by GadTools */
  244.                     GT_BeginRefresh( win );
  245.                     GT_EndRefresh( win, TRUE );
  246.               break;
  247.  
  248.                 case IDCMP_CLOSEWINDOW:
  249.                     terminated = TRUE;
  250.                  break;
  251.  
  252.             }/* switch ( msgClass ) */
  253.         }/* while ( !terminated && GetMsg ) */
  254.     }/* while ( !terminated ) */
  255.  
  256.     return Selected;
  257. }
  258.  
  259. struct lv_Node *NewLVNode( struct List *list, char *name )
  260. {
  261.     struct lv_Node *lvnode = NULL;
  262.  
  263.     /* Allocate memory for a new Node structure */
  264.     if ( lvnode = AllocMem( sizeof( struct lv_Node ), MEMF_CLEAR ) )
  265.     {
  266.         /* Set the Node name, type and priority */
  267.         lvnode->nn_Node.ln_Name = lvnode->Info;
  268.         strcpy( lvnode->Info, name );
  269.         lvnode->nn_Node.ln_Type = LVNODE_ID;
  270.         lvnode->nn_Node.ln_Pri = 0;
  271.  
  272.         /* Add the new Node to the end of the list */
  273.         AddTail( list, (struct Node *)lvnode );
  274.     }
  275.  
  276.     return lvnode;
  277. }
  278.  
  279. void FreeList( struct List *list )
  280. {
  281.     struct lv_Node *worknode, *nextnode;
  282.  
  283.     if ( list != NULL )
  284.     {
  285.         worknode = (struct lv_Node *)( list->lh_Head );
  286.  
  287.         while ( nextnode = (struct lv_Node *)( worknode->nn_Node.ln_Succ ) )
  288.         {
  289.             FreeMem( worknode, sizeof( struct lv_Node ) );
  290.             worknode = nextnode;
  291.         }
  292.  
  293.         FreeMem( list, sizeof( struct List ) );
  294.     }
  295. }
  296.  
  297. string DialogBox( string *sp, string def )
  298. {
  299.     string *t = sp;
  300.  
  301.     struct Screen *screen;
  302.     struct Window *win;
  303.  
  304.     void *vi = NULL;
  305.  
  306.     struct Gadget *gadContext = NULL;
  307.     struct Gadget *gadList = NULL;
  308.     struct Gadget *gadOK, *gadCANCEL, *gadTEXT;
  309.     struct NewGadget gadNew;
  310.  
  311.     struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0 };
  312.  
  313.     int TBSize;
  314.  
  315.     /* Store original value in case of user cancel */
  316.     strcpy( defValue, def );
  317.  
  318.     IntuitionBase = OpenLibrary( "intuition.library", 37 );
  319.     GadToolsBase = OpenLibrary( "gadtools.library", 37 );
  320.  
  321.     if ( IntuitionBase != NULL && GadToolsBase != NULL )
  322.     {
  323.         screen = LockPubScreen( NULL );
  324.  
  325.         if ( screen != NULL )
  326.         {
  327.             TBSize = screen->WBorTop + screen->Font->ta_YSize + 1;
  328.             vi = GetVisualInfo( screen, TAG_END );
  329.  
  330.             {
  331.                 gadList = CreateContext( &gadContext );
  332.  
  333.                 gadNew.ng_LeftEdge        =    11;
  334.                 gadNew.ng_TopEdge            =    125 + TBSize;
  335.                 gadNew.ng_Width                =    69;
  336.                 gadNew.ng_Height            =    14;
  337.                 gadNew.ng_GadgetText = "Ok";
  338.                 gadNew.ng_TextAttr        =    &Topaz80;
  339.                 gadNew.ng_VisualInfo    =    vi;
  340.                 gadNew.ng_Flags                =    PLACETEXT_IN;
  341.                 gadNew.ng_GadgetID = GAD_OK;
  342.                 gadOK = gadList = CreateGadget( BUTTON_KIND, gadList, &gadNew,
  343.                                                                      TAG_END );
  344.  
  345.                 gadNew.ng_LeftEdge        =    270;
  346.                 gadNew.ng_GadgetText = "Cancel";
  347.                 gadNew.ng_Gadg