#include "ex.h"
#include <aes.h>
#include <string.h>
#include <naudio\naudio.h>

/* This little example is an example of BAD C-Programming, and BAD
   GEM-Programming, since it does NO error checking. On the other
   hand the NAUDIO code is done properly, what you care about any-
   way. The lack of error checking is to keep the source short,
   as to not obscure the NAUDIO functionality.
 */
#define WTYPE  (NAME | CLOSER | MOVER)


static void join( char *dst, char *path, char * file)
{
   strcpy( dst, path);
   strcpy( strrchr( dst, '\\') + 1, file);
}


main()
{
   OBJECT         *tree, *menu;
   int            id;
   unsigned int   ev;
   int            x, y, w, h, dmy;
   int            mbuf[ 8];
   auto char      path[ 128],
                  filename[ 32],
                  full[ 256];
   n_module       *p = 0;


   appl_init();
   graf_mouse( 0, NULL);
   naudio_init();
   naudio_some();
   if( naudio_engine( NTRACKER) < 0)
   {
      form_alert( 1, "[1][NAUDIO Initialization failed!][ EXIT ]");
      appl_exit();
      return( -1);
   }

   rsrc_load( "ex.rsc");
   rsrc_gaddr( R_TREE, T_DIAL, &tree);
   rsrc_gaddr( R_TREE, T_MENU, &menu);
   menu_bar( menu, 1);
   wind_calc( WC_BORDER, WTYPE, tree->ob_x, tree->ob_y,
                                tree->ob_width, tree->ob_height,
                                &x, &y, &w, &h);
   id = wind_create( WTYPE, x, y, w, h);
   wind_open( id, x, y, w, h);
   wind_set( id, WF_NAME, "NAUDIO-EX2");

   for( ;;)
   {
      ev = evnt_multi( MU_BUTTON | MU_MESAG, 2, 3, 1,
                       0, 0, 0, 0, 0,
                       0, 0, 0, 0, 0,
                       mbuf,
                       0, 0,
                       &x, &y, &dmy, &dmy, &dmy, &dmy);

      if( ev & MU_BUTTON)
      {
         int   no;

         if( (no = objc_find( tree, 0, 16, x, y)) >= 0 &&
             ! form_button( tree, no, 1, &dmy))
         {
            objc_change( tree, no, 0, tree->ob_x, tree->ob_y,
                                      tree->ob_width, tree->ob_height,
                                      0, SELECTED);
            switch( no)
            {
               case D_LOAD :
                  fsel_input( path, filename, &dmy);
                  if( dmy)
                  {
                     nmodule_stop();
                     if( p)
                        nmodule_free( p);
                     join( full, path, filename);
                     if( ! (p = nmodule_load( full)))
                        form_alert( 1, "[2][Load failed][ OK ]");
                  }
                  break;

               case D_PLAY :
                  naudio_stop();
                  naudio_start( 1);    /* superflous, but harmless a 2nd time */
                  if( p)
                     nmodule_play( p, 0, 0, 0, 2);
                  break;

               case D_SAVE :
                  if( p)
                  {
                     fsel_input( path, filename, &dmy);
                     if( dmy)
                     {
                        join( full, path, filename);
                        if( nmodule_save( full, p, 1))
                           form_alert( 1, "[2][Save failed][ OK ]");
                     }
                  }
                  break;
            }
         }
      }

      if( ev & MU_MESAG)
      {
         switch( mbuf[ 0])
         {
            case MN_SELECTED :
            case WM_CLOSED :
               goto done;

            case WM_MOVED :
               wind_set( id, WF_CURRXYWH, mbuf[4], mbuf[5], mbuf[6], mbuf[7]);
               wind_calc( WC_WORK, WTYPE, mbuf[4], mbuf[5], mbuf[6], mbuf[7],
                                          &tree->ob_x, &tree->ob_y,
                                          &tree->ob_width, &tree->ob_height);
               break;

            case WM_REDRAW :
               objc_draw( tree, 0, 16, mbuf[ 4], mbuf[ 5], mbuf[ 6], mbuf[ 7]);
         }
      }
   }
done:
   menu_bar( menu, 0);
   wind_close( id);
   wind_delete( id);
   appl_exit();
   naudio_done();
   return( 0);
}



