#include "interfaccia.h"
HWND CreaFinestraPrincipale( HINSTANCE hInst ) {
HWND hwnd = CreateDialog(
hInst, MAKEINTRESOURCE(kIDFinPrinc), NULL, (DLGPROC)DlgMain );
if( hwnd == NULL ) {
Errore( "Finestra principale non creata." );
}
else {
SetWindowText( hwnd, kStrNomeProgramma );
ComponiInformazioni( hwnd );
}
return hwnd;
}
void AggiornaPulsanti( HWND hwnd, CDF *cdf ) {
HWND ctrlCod = GetDlgItem( hwnd, kIDPulsCodifica );
HWND ctrlDec = GetDlgItem( hwnd, kIDPulsDecodifica );
if( ctrlCod!=NULL && ctrlDec!=NULL ) {
int dd = cdf->tipologia_dati();
bool chiave_valida = cdf->chiave_valida();
EnableWindow( ctrlCod, dd==kCDFInChiaro && chiave_valida );
EnableWindow( ctrlDec, dd==kCDFCodificati && chiave_valida );
}
}
void PopolaListaCaratteriMancanti( HWND hwnd, CDF *cdf ) {
static char buff[24] = "";
HWND ctrlLista = GetDlgItem( hwnd, kIDCampoCarMancanti );
if( ctrlLista != NULL ) {
SendMessage( ctrlLista, LB_RESETCONTENT, 0, 0 );
if( !cdf->chiave_valida() ) {
unsigned int i, nCManc = cdf->totale_caratteri_mancanti();
unsigned char c;
SendMessage( ctrlLista, WM_SETREDRAW, FALSE, 0 );
for( i=0; i<nCManc; ++i ) {
c = cdf->carattere_mancante( i );
wsprintf( buff, "%c\t(ASCII %03d)", c, (int)c );
SendMessage( ctrlLista, LB_ADDSTRING, 0, (LPARAM)buff );
}
SendMessage( ctrlLista, WM_SETREDRAW, TRUE, 0 );
}
else {
if( cdf->chiave_presente() ) {
SendMessage( ctrlLista, LB_ADDSTRING,
0, (LPARAM)"Nessun carattere mancante" );
}
else {
SendMessage( ctrlLista, LB_ADDSTRING,
0, (LPARAM)"Chiave assente" );
}
}
}
}
char *RicavaPrimoFileTrascinato( LPSTR lpszArgs ) {
char *esito, *e, *inizio, *fine;
if( *lpszArgs == '\0' ) {
try {
esito = new char[1];
*esito = '\0';
} catch( ... ) {
return NULL;
}
}
else {
if( *lpszArgs == '\"' ) {
// trova l'inizio e la fine del virgolettato
for( inizio = lpszArgs; *inizio == '\"'; ++inizio );
for( fine = inizio; *fine != '\"'; ++fine );
}
else {
for( inizio=fine=lpszArgs; *fine!=' ' && *fine!='\0'; ++fine );
}
try {
esito = new char[(fine-inizio)+1];
} catch( ... ) {
return NULL;
}
// copia il primo parametro della command line
for( e = esito; fine > inizio; *e++ = *inizio++ );
*e = '\0';
}
return esito;
}
/*==============================================================================
"tipoFile" identifica il tipo di file che sara' riconosciuto dalla finestra di
navigazione e che sarà usato come estensione di default, se del caso. Se e' NULL
saranno accettati file di ogni tipo e non sara' attribuita alcuna estensione di
default.
"nomeFileDef" identifica il nome del file usato come default nella finestra del
tipo "Salva". Se "nomeFileDef" e' un puntatore valido, la finestra usata sara'
una finestra del tipo "Salva"; se e' NULL, la finestra usata sarà una finestra
del tipo "Apri".
==============================================================================*/
char *ScegliFile (
HWND hwnd, const char *tipoFile, const char *nomeFileDef ) {
OPENFILENAME ofn;
char filtro[32];
char *p = filtro;
char *nomeFile;
DWORD esito;
ZeroMemory(&ofn, sizeof(ofn));
if( tipoFile != NULL ) {
p += wsprintf( p, "Tipo file: %s", tipoFile ) + 1;
p += wsprintf( p, "*.%s", tipoFile ) + 1;
*p = '\0';
}
else {
p += wsprintf( p, "Tutti i file (*.*)" ) + 1;
p += wsprintf( p, "*.*" ) + 1;
*p = '\0';
}
try {
nomeFile = new char[4*MAX_PATH];
*nomeFile = '\0';
} catch( ... ) {
return NULL;
}
if( nomeFileDef != NULL )
lstrcpy( nomeFile, nomeFileDef );
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = filtro;
ofn.lpstrFile = nomeFile;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST |
OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY |
OFN_NOCHANGEDIR;
ofn.lpstrDefExt = tipoFile;
if( nomeFileDef != NULL )
esito = GetSaveFileName( &ofn );
else esito = GetOpenFileName( &ofn );
if ( esito == FALSE ) {
if( CommDlgExtendedError() != 0 )
Errore( "Scelta file non valida.", hwnd );
delete[] nomeFile;
return NULL;
}
return nomeFile;
}
////////////////////////////////////////////////////////////////////////////////
// Carica in memoria i dati in formato TEXT contenuti negli appunti //
// riferibili alla finestra "hFin" ("hFin" puo' essere NULL se gli appunti //
// non riguardano una particolare finestra). Il valore restituito e' il //
// puntatore a una stringa di tipo C (zero terminated) allocata //
// dinamicamente. Se non esistono appunti leggibili o se e' impossibile //
// allocare spazio in memoria per la stringa da restituire, la funzione //
// restituisce NULL. //
// Il parametro opzionale "lTesto" (NULL per default) contiene l'indirizzo di //
// un valore di tipo unsigned long. Se "lTesto" esprime un indirizzo diverso //
// da NULL, in esso viene immagazzinata la quantita' di caratteri contenuta //
// nella stringa restituita come valore di ritorno. //
// Richiede che sia incluso l'header standard del C "string.h" //
////////////////////////////////////////////////////////////////////////////////
char *RicavaAppuntiTxt( HWND hFin, unsigned long *lTesto ) {
HANDLE hClip = NULL;
char *pClip = NULL;
char *pEsito = NULL;
unsigned long lung;
if( OpenClipboard(hFin) ) {
if( (hClip = GetClipboardData(CF_TEXT)) != NULL ) {
if( (pClip = (char*)GlobalLock(hClip)) != NULL ) {
lung = lstrlen( pClip );
// alloca spazio per il testo da restituire
try {
pEsito = new char[lung+1];
// copia il testo nello spazio allocato
lstrcpy( pEsito, pClip );
// se richiesto, annota la lunghezza del testo
if( lTesto != NULL ) *lTesto = lung;
} catch( ... ) {
;
}
GlobalUnlock( hClip );
}
}
CloseClipboard();
}
return pEsito;
}
char *ComponiStringaValiditaChiave( CDF *cdf ) {
static char buff[64] = "";
const char *non[2] = { " NON ", " " };
const char *esc[2] = { "!", "." };
int lStr = 0;
bool valida = cdf->chiave_valida();
lStr = wsprintf( buff, "%s%s%s%s", "Gli appunti", non[valida],
"contengono una chiave valida", esc[valida] );
if( valida )
wsprintf( buff+lStr, " (%d caratteri)", cdf->lunghezza_chiave() );
return buff;
}
void AggiornaInformazioniChiave( HWND hwnd, CDF *cdf ) {
HWND ctrlEtInfo = GetDlgItem( hwnd, kIDEtAppuntiValidi );
if( ctrlEtInfo != NULL )
SetWindowText( ctrlEtInfo, ComponiStringaValiditaChiave(cdf) );
}
void ComponiInformazioni( HWND hwnd ) {
HWND ctrlEtInfo = GetDlgItem( hwnd, kIDEtInfo );
if( ctrlEtInfo != NULL ) {
char buff[96] = "";
char *p = buff;
p += wsprintf( p, "%s v%d.%d",
kStrNomeProgramma, kVersione, kSottoversione );
if( kRevisione != 0 ) p += wsprintf( p, ".%d", kRevisione );
p += wsprintf( p, "\ndi Aldo Carpanelli\ngen/feb 2015\n" );
p += wsprintf( p, "\nBuon divertimento! :o)" );
SetWindowText( ctrlEtInfo, buff );
}
}
void Errore( const char *msg, HWND genitrice ) {
MessageBox( genitrice, msg, kStrNomeProgramma, MB_OK | MB_ICONERROR );
}
void Notifica( const char *msg, HWND genitrice ) {
MessageBox( genitrice, msg, kStrNomeProgramma, MB_OK | MB_ICONINFORMATION );
}