// CrapFile MakerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CrapFile Maker.h"
#include "CrapFile MakerDlg.h"

#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <time.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCrapFileMakerDlg dialog

CCrapFileMakerDlg::CCrapFileMakerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCrapFileMakerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCrapFileMakerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon     = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  m_hFileIcon = AfxGetApp()->LoadIcon(IDI_FILEOPEN );
  m_hDiceIcon = AfxGetApp()->LoadIcon(IDI_DICE     );
  m_bmAbout.LoadBitmap( IDBMP_IMAGE );

  m_szFileName = "";
}

void CCrapFileMakerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCrapFileMakerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCrapFileMakerDlg, CDialog)
	//{{AFX_MSG_MAP(CCrapFileMakerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDB_CREATE, OnCreateFile)
	ON_EN_CHANGE(IDC_FILE, OnChangeFile)
	ON_BN_CLICKED(IDC_CREDITS, OnCredits)
	ON_BN_CLICKED(IDB_FILESELECT, OnFileselect)
	ON_BN_CLICKED(IDB_DICE, OnDice)
	ON_EN_UPDATE(IDC_SEED, OnUpdateSeed)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCrapFileMakerDlg message handlers

BOOL CCrapFileMakerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
  // TODO: Add extra initialization here
  ((CButton *)GetDlgItem( IDB_FILESELECT ) )->SetIcon( m_hFileIcon );	
  ((CButton *)GetDlgItem( IDB_DICE       ) )->SetIcon( m_hDiceIcon );	
	((CButton *)GetDlgItem( IDC_CREDITS ))->SetBitmap( (HBITMAP) m_bmAbout );

  OnDice();

  return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCrapFileMakerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCrapFileMakerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CCrapFileMakerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}




void CCrapFileMakerDlg::OnFileselect() 
{
  CFileDialog	  oFileDlg( TRUE );

  oFileDlg.DoModal();

  m_szFileName = oFileDlg.GetPathName();
  GetDlgItem( IDC_FILE )->SetWindowText( m_szFileName );
}




void CCrapFileMakerDlg::OnChangeFile() 
{
  ((CEdit*)GetDlgItem( IDC_FILE ))->GetWindowText( m_szFileName );
}


//--- Some Prime Numbers used as key generators
#define KEY_5 93179
#define KEY_6 212369
#define KEY_7 2735129
#define KEY_8 32452843
#define KEY_14 29996224274821


void CCrapFileMakerDlg::OnCreateFile() 
{
  FILE            * pFile;
  CProgressCtrl   * pProgress;
  char            szSize[ 30 ];
  long            lSize, lCpt, lProgCpt;
  unsigned char   ucVal;
  double          dVal;

  int             iSeedDim;
  CString         szSeed;

  //--- Init Random ---
  srand( (int) m_iSeed );
  ((CEdit *)GetDlgItem(IDC_SEED))->GetWindowText(szSeed);
  iSeedDim = szSeed.GetLength() + 1; //+1 for '#'

  //--- Retrieve File Size ---
  ((CEdit *) GetDlgItem( IDC_SIZEFILE ))->GetWindowText( szSize, 30); 
  lSize = atoi( szSize ) - iSeedDim;

  //--- Init progress bar ---
  pProgress = ((CProgressCtrl *) GetDlgItem( IDC_PROGRESS ));
  lProgCpt = (lSize / 1000000);
  pProgress->SetRange( 0, lProgCpt );
  lProgCpt = 0;
  
  if( m_szFileName != "" )
  {
    pFile = fopen( m_szFileName, "wb" );
    if( pFile && lSize )
    {
      fprintf( pFile, "%s#", szSeed );
      
      for( lCpt = 0 ; lCpt < lSize ; lCpt ++ )
      {
        dVal = ((double) rand()) / ((double)0x7fff );
        dVal = ( dVal * ((double)KEY_14) ) - floor( ( dVal * ((double)KEY_14) ) );
        ucVal = ((unsigned char)(255.0 * dVal));
        fputc( ucVal, pFile );
        if( (lCpt % 1000000 ) == 0 )
        {
          MSG					stMessage;		
          
          pProgress->SetPos( lProgCpt ++ );
          _flushall();
          if ( PeekMessage( &stMessage, NULL, 0, 0, PM_REMOVE ) )
            DispatchMessage( &stMessage );
        }
      }
      fclose( pFile  );
      pProgress->SetPos( 0 );
      MessageBox( "File successfully created.", "Info", MB_OK | MB_ICONINFORMATION );
    }
    else
      MessageBox( "Wrong size or file name.", "Error", MB_OK | MB_ICONERROR );
  }
  else
    MessageBox( "Please enter a file name .", "Error", MB_OK | MB_ICONERROR );
}


void CCrapFileMakerDlg::OnCredits() 
{
  MessageBox( "(c) Marc Fascia - 1998", "Credits ...", MB_OK | MB_ICONINFORMATION );
}


void CCrapFileMakerDlg::OnDice() 
{
  time_t          lTime;  
  char            szValue[255];

  time( &lTime );
  m_iSeed = _rotl( lTime, (lTime % 31) );

  sprintf( szValue, "%u", m_iSeed );
  ((CEdit *)GetDlgItem( IDC_SEED))->SetWindowText( szValue );
}

void CCrapFileMakerDlg::OnUpdateSeed() 
{
  CString         szValue;

  ((CEdit *)GetDlgItem( IDC_SEED))->GetWindowText( szValue );
  m_iSeed = atoi( szValue );
}
