//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//	Config.c
//	Luiz Marques Afonso 	Agosto/95
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#include "laycab.h"

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Prototypes
BOOL CALLBACK _export	AboutProc( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK _export	ConfigDlgProc( HWND, UINT, WPARAM, LPARAM );
static BOOL		EscreveConfig( LPCSTR, LPCSTR );


//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#pragma argsused
BOOL CALLBACK _export
	AboutProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HICON
		hIcone;
	HDC
		hdc;
	PAINTSTRUCT
		ps;

	switch( msg )
	{
		case WM_INITDIALOG:
			SetFocus( GetDlgItem( hDlg, IDOK ) );
			break;

		case WM_PAINT:
			hdc    = BeginPaint( hDlg, &ps );
			hIcone = LoadIcon( hInstance, "ICONE_SOBRE" );
			if( hIcone && hdc )
			{
				SetMapMode( hdc, MM_TEXT );
				DrawIcon( hdc, 95, 45, hIcone );
				ReleaseDC( hDlg, hdc );
				DestroyIcon( hIcone );
			}
			EndPaint( hDlg, &ps );
			break;

		case WM_COMMAND:
			if( wParam == IDOK || wParam == IDCANCEL )
				EndDialog( hDlg, TRUE );
			break;

		default:
			return( FALSE );
	}

	return( TRUE );
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#pragma argsused
BOOL CALLBACK _export
	ConfigDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HWND
		hScroll;
	static WORD
		posAtual;
	char
		strAux[ 5 ];
	//int veloc;
    WORD
        semente;

	switch( msg )
	{
		case WM_INITDIALOG:
		{
            //[LMA 17/07/99]
            PegaSementeConfig();

			hScroll = GetDlgItem( hDlg, IDC_HSCROLL1 );
			SetFocus( hScroll );
            //[LMA 17/07/99] - Novo ajuste da semente
			//SetScrollRange( hScroll, SB_CTL, VELOCIDADE_MIN, VELOCIDADE_MAX, FALSE );
			//SetScrollPos( hScroll, SB_CTL, Velocidade, TRUE );
			//posAtual = Velocidade;
			SetScrollRange( hScroll, SB_CTL, SementeMin, SementeMax, FALSE );
			SetScrollPos( hScroll, SB_CTL, Semente, TRUE );
			posAtual = Semente;
			itoa( posAtual, strAux, 10 );
			SetDlgItemText( hDlg, IDC_SPEED, strAux );
			CheckRadioButton( hDlg, IDC_LPT1, IDC_LPT2, IDC_LPT1 + Lpt );
			CheckRadioButton( hDlg, IDC_SINC, IDC_ASSINC, Sincrono ? IDC_SINC : IDC_ASSINC );

			// Se o driver está em modo assíncrono, força habilitação de interrupções
			if( !Sincrono )
			{
				EnableWindow( GetDlgItem( hDlg, IDC_CHECKINT ), FALSE );
				HabilitaIntr = TRUE;
			}

			CheckDlgButton( hDlg, IDC_CHECKINT, HabilitaIntr );
			if( !TemLPT2 )
				EnableWindow( GetDlgItem( hDlg, IDC_LPT2 ), FALSE );
			break;
		}


		case WM_COMMAND:
		{
			switch( wParam )
			{
				case IDOK:
#if 0
					// Velocidade
					Velocidade = posAtual;
					itoa( posAtual, strAux, 10 );
					EscreveConfig( VarVelocidade, strAux );
#else
                    Semente = posAtual;
					itoa( posAtual, strAux, 10 );
					EscreveConfig( VarSemente, strAux );

                    itoa( SementeMin, strAux, 10 );
                    EscreveConfig( VarSementeMin, strAux );

                    itoa( SementeMax, strAux, 10 );
                    EscreveConfig( VarSementeMax, strAux );
#endif
					// Porta paralela
					Lpt = (IsDlgButtonChecked( hDlg, IDC_LPT1 ))
						? LPT1
						: LPT2;
					EscreveConfig( VarPorta, (Lpt == LPT1) ? StrLPT1
									       : StrLPT2 );
					// Habilita interrupcoes
					HabilitaIntr = IsDlgButtonChecked( hDlg, IDC_CHECKINT );
					itoa( HabilitaIntr, strAux, 10 );
					EscreveConfig( VarInterrupcoes, strAux );

					// Modo de funcionamento
					Sincrono = IsDlgButtonChecked( hDlg, IDC_SINC );
					itoa( Sincrono, strAux, 10 );
					EscreveConfig( VarSincrono, strAux );

					WritePrivateProfileString( NULL, NULL, NULL, ArquivoSystemIni );
					EndDialog( hDlg, TRUE );
					break;

				case IDCANCEL:
					EndDialog( hDlg, FALSE );
					break;

				case IDC_ASSINC:
					CheckDlgButton( hDlg, IDC_CHECKINT, TRUE );
					EnableWindow( GetDlgItem( hDlg, IDC_CHECKINT ), FALSE );
					break;

				case IDC_SINC:
					EnableWindow( GetDlgItem( hDlg, IDC_CHECKINT ), TRUE );
					break;
                    
				case ID_SOBRE:
					DialogBox( hInstance, "SOBRE_DLG", hDlg, AboutProc );
					break;
                    
				case ID_AJUSTE:
                    //[LMA 17/07/99] - Alteracao do calculo da semente
					//veloc = AjustaVelocidade();
                    semente = AjustaSemente();

					//if( veloc )
					if( semente )
					{
						hScroll = GetDlgItem( hDlg, IDC_HSCROLL1 );
						//SetScrollPos( hScroll, SB_CTL, veloc, TRUE );
						//posAtual = veloc;
                        SetScrollPos( hScroll, SB_CTL, semente, TRUE );
                        posAtual = semente;
						itoa( posAtual, strAux, 10 );
						SetDlgItemText( hDlg, IDC_SPEED, strAux );
						UpdateWindow( hScroll );
					}
					break;

				default:
					return( FALSE );
			}
			return( TRUE );
		}


		case WM_HSCROLL:
		{
			hScroll = HIWORD( lParam );

			switch( wParam )
			{
				case SB_TOP:
					//posAtual = VELOCIDADE_MIN;
                    posAtual = SementeMin;
					break;

				case SB_BOTTOM:
					//posAtual = VELOCIDADE_MAX;
                    posAtual = SementeMax;
					break;

				case SB_LINEUP:
					//if( posAtual > VELOCIDADE_MIN )
                    if( posAtual > SementeMin )
						posAtual--;
					break;

				case SB_LINEDOWN:
					//if( posAtual < VELOCIDADE_MAX )
                    if( posAtual < SementeMax )
						posAtual++;
					break;

				case SB_PAGEUP:
					//posAtual = max( (posAtual-10), VELOCIDADE_MIN );
                    posAtual = max( (posAtual-10), SementeMin );
					break;

				case SB_PAGEDOWN:
					//posAtual = min( (posAtual+10), VELOCIDADE_MAX );
                    posAtual = min( (posAtual+10), SementeMax );
					break;

				case SB_THUMBPOSITION:
				case SB_THUMBTRACK:
					posAtual = LOWORD( lParam );
					break;

				default:
					return( FALSE );
			}

			itoa( posAtual, strAux, 10 );
			SetScrollPos( hScroll, SB_CTL, posAtual, TRUE );
			SetDlgItemText( hDlg, IDC_SPEED, strAux );
			break;
		}

		default:
			return( FALSE );

	}

	return( TRUE );
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
static BOOL
	EscreveConfig( LPCSTR lpszEntry, LPCSTR lpszString )
{
	return( WritePrivateProfileString( NomeSecaoSystemIni,
					   lpszEntry,
					   lpszString,
					   ArquivoSystemIni ) );
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void
	RemoveConfig( void )
{
	// Apaga a secao
	WritePrivateProfileString( NomeSecaoSystemIni, NULL, NULL, ArquivoSystemIni );
	// Flush buffer
	WritePrivateProfileString( NULL, NULL, NULL, ArquivoSystemIni );
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
