2008-03-23 20:25:49 +01:00
|
|
|
/* -*- fill-column: 77; c-basic-offset: 4; compile-command: "make TARGET_OS=wince DEBUG=TRUE" -*- */
|
|
|
|
/*
|
|
|
|
* Copyright 2004-2008 by Eric House (xwords@eehouse.org). All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windowsx.h>
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include <commdlg.h>
|
|
|
|
|
|
|
|
#include "cemain.h"
|
|
|
|
#include "cesvdgms.h"
|
|
|
|
#include "ceutil.h"
|
|
|
|
#include "cedebug.h"
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
typedef struct CeSaveGameNameState {
|
2008-03-23 20:25:49 +01:00
|
|
|
CEAppGlobals* globals;
|
|
|
|
wchar_t* buf;
|
|
|
|
XP_U16 buflen;
|
|
|
|
XP_Bool cancelled;
|
|
|
|
XP_Bool inited;
|
2008-04-13 19:14:16 +02:00
|
|
|
} CeSaveGameNameState;
|
2008-03-23 20:25:49 +01:00
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
/* static void */
|
|
|
|
/* notImpl( CEAppGlobals* globals ) */
|
|
|
|
/* { */
|
|
|
|
/* messageBoxChar( globals, "To be implemented soon....", */
|
|
|
|
/* L"Notice", MB_OK ); */
|
|
|
|
/* } */
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
static XP_Bool
|
|
|
|
ceFileExists( const wchar_t* name )
|
|
|
|
{
|
|
|
|
wchar_t buf[128];
|
|
|
|
DWORD attributes;
|
|
|
|
swprintf( buf, DEFAULT_DIR_NAME L"\\%s.xwg", name );
|
|
|
|
|
|
|
|
attributes = GetFileAttributes( buf );
|
|
|
|
return attributes != 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
makeUniqueName( wchar_t* buf, XP_U16 bufLen )
|
|
|
|
{
|
2008-03-31 02:26:18 +02:00
|
|
|
XP_U16 ii;
|
|
|
|
for ( ii = 1; ii < 100; ++ii ) {
|
2008-04-13 19:14:16 +02:00
|
|
|
int len = swprintf( buf, L"Untitled%d", ii );
|
|
|
|
XP_ASSERT( len < bufLen );
|
2008-03-23 20:25:49 +01:00
|
|
|
if ( !ceFileExists( buf ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If we fall out of the loop, the user will be asked to confirm delete
|
|
|
|
of Untitled99 or somesuch. That's ok.... */
|
|
|
|
} /* makeUniqueName */
|
|
|
|
|
|
|
|
static LRESULT CALLBACK
|
|
|
|
SaveNameDlg( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
|
|
|
|
{
|
2008-04-13 19:14:16 +02:00
|
|
|
CeSaveGameNameState* state;
|
2008-03-23 20:25:49 +01:00
|
|
|
XP_U16 wid;
|
|
|
|
BOOL result = FALSE;
|
|
|
|
|
|
|
|
if ( message == WM_INITDIALOG ) {
|
|
|
|
SetWindowLong( hDlg, GWL_USERDATA, lParam );
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
state = (CeSaveGameNameState*)lParam;
|
2008-03-23 20:25:49 +01:00
|
|
|
state->cancelled = XP_TRUE;
|
|
|
|
state->inited = XP_FALSE;
|
|
|
|
|
2008-03-31 02:26:18 +02:00
|
|
|
ceDlgSetup( state->globals, hDlg );
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
2008-04-13 19:14:16 +02:00
|
|
|
state = (CeSaveGameNameState*)GetWindowLong( hDlg, GWL_USERDATA );
|
2008-03-23 20:25:49 +01:00
|
|
|
if ( !!state ) {
|
|
|
|
if ( !state->inited ) {
|
|
|
|
state->inited = XP_TRUE;
|
2008-04-13 19:14:16 +02:00
|
|
|
(void)SetDlgItemText( hDlg, IDC_SVGN_EDIT, state->buf );
|
2008-03-23 20:25:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (message) {
|
|
|
|
case WM_COMMAND:
|
|
|
|
wid = LOWORD(wParam);
|
|
|
|
switch( wid ) {
|
|
|
|
case IDOK: {
|
|
|
|
wchar_t buf[128];
|
|
|
|
(void)GetDlgItemText( hDlg, IDC_SVGN_EDIT, buf,
|
|
|
|
VSIZE(buf) );
|
|
|
|
if ( ceFileExists( buf ) ) {
|
|
|
|
messageBoxChar( state->globals, "File exists",
|
|
|
|
L"Oops!", MB_OK );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
swprintf( state->buf, DEFAULT_DIR_NAME L"\\%s.xwg", buf );
|
|
|
|
XP_LOGW( __func__, state->buf );
|
|
|
|
/* fallthru */
|
|
|
|
state->cancelled = XP_FALSE;
|
|
|
|
}
|
|
|
|
case IDCANCEL:
|
|
|
|
EndDialog(hDlg, wid);
|
|
|
|
result = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2008-04-13 19:14:16 +02:00
|
|
|
} /* SaveNameDlg */
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
XP_Bool
|
|
|
|
ceConfirmUniqueName( CEAppGlobals* globals, wchar_t* buf, XP_U16 buflen )
|
|
|
|
{
|
2008-04-13 19:14:16 +02:00
|
|
|
CeSaveGameNameState state;
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
LOG_FUNC();
|
|
|
|
|
|
|
|
makeUniqueName( buf, buflen );
|
|
|
|
|
|
|
|
XP_MEMSET( &state, 0, sizeof(state) );
|
|
|
|
state.globals = globals;
|
|
|
|
state.buf = buf;
|
|
|
|
state.buflen = buflen;
|
|
|
|
(void)DialogBoxParam( globals->hInst, (LPCTSTR)IDD_SAVENAMEDLG,
|
|
|
|
globals->hWnd,
|
|
|
|
(DLGPROC)SaveNameDlg, (long)&state );
|
|
|
|
XP_LOGW( __func__, buf );
|
|
|
|
return !state.cancelled;
|
|
|
|
} /* ceConfirmUniqueName */
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
typedef struct CeSavedGamesState {
|
2008-03-23 20:25:49 +01:00
|
|
|
CEAppGlobals* globals;
|
|
|
|
HWND hDlg;
|
|
|
|
wchar_t* buf;
|
|
|
|
XP_U16 buflen;
|
|
|
|
XP_S16 sel;
|
|
|
|
wchar_t curName[128];
|
2008-04-13 19:14:16 +02:00
|
|
|
XP_U16 nItems;
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
/* wchar_t** names; */
|
|
|
|
/* XP_U16 nNamesUsed; */
|
|
|
|
/* XP_U16 nNamesAllocd; */
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
XP_Bool opened;
|
2008-03-23 20:25:49 +01:00
|
|
|
XP_Bool inited;
|
2008-04-13 19:14:16 +02:00
|
|
|
} CeSavedGamesState;
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
/* Probably belongs as a utility */
|
|
|
|
static void
|
|
|
|
getCBText( HWND hDlg, XP_U16 id, XP_U16 sel, wchar_t* buf, XP_U16* lenp )
|
|
|
|
{
|
|
|
|
XP_U16 len = SendDlgItemMessage( hDlg, id, GETLBTEXTLEN, sel, 0L );
|
|
|
|
if ( len < *lenp ) {
|
|
|
|
(void)SendDlgItemMessage( hDlg, id, GETLBTEXT, sel, (LPARAM)buf );
|
|
|
|
} else {
|
|
|
|
XP_ASSERT( 0 );
|
|
|
|
}
|
|
|
|
*lenp = len;
|
|
|
|
} /* getCBText */
|
|
|
|
|
|
|
|
static void
|
2008-04-13 19:14:16 +02:00
|
|
|
setEditFromSel( CeSavedGamesState* XP_UNUSED(state) )
|
2008-03-23 20:25:49 +01:00
|
|
|
{
|
2008-04-13 19:14:16 +02:00
|
|
|
/* wchar_t buf[64]; */
|
|
|
|
/* XP_U16 len = VSIZE(buf); */
|
|
|
|
/* getCBText( state->hDlg, IDC_SVGM_GAMELIST, state->sel, buf, &len ); */
|
|
|
|
/* if ( len <= VSIZE(buf) ) { */
|
|
|
|
/* (void)SetDlgItemText( state->hDlg, IDC_SVGM_EDIT, buf ); */
|
|
|
|
/* } */
|
2008-03-23 20:25:49 +01:00
|
|
|
} /* */
|
|
|
|
|
|
|
|
static void
|
2008-04-13 19:14:16 +02:00
|
|
|
setButtons( CeSavedGamesState* state )
|
|
|
|
{
|
|
|
|
/* Open button disabled by default in case no games */
|
|
|
|
ceEnOrDisable( state->hDlg, IDC_SVGM_OPEN, state->nItems > 0 );
|
|
|
|
/* ceEnOrDisable( state->hDlg, IDC_SVGM_DEL, state->nItems > 0 ); */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initSavedGamesData( CeSavedGamesState* state )
|
2008-03-23 20:25:49 +01:00
|
|
|
{
|
|
|
|
HANDLE fileH;
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
wchar_t path[256];
|
|
|
|
XP_U16 curSel = 0, ii;
|
|
|
|
|
|
|
|
XP_MEMSET( &data, 0, sizeof(data) );
|
|
|
|
lstrcpy( path, DEFAULT_DIR_NAME L"\\" );
|
|
|
|
lstrcat( path, L"*.xwg" );
|
|
|
|
|
|
|
|
fileH = FindFirstFile( path, &data );
|
|
|
|
for ( ii = 0; fileH != INVALID_HANDLE_VALUE; ++ii ) {
|
|
|
|
XP_U16 len = wcslen( data.cFileName );
|
|
|
|
|
|
|
|
XP_LOGW( "comp1", state->curName );
|
|
|
|
XP_LOGW( "comp2", data.cFileName );
|
|
|
|
if ( curSel == 0 && 0 == wcscmp( state->curName, data.cFileName ) ) {
|
|
|
|
curSel = ii;
|
|
|
|
}
|
|
|
|
|
|
|
|
XP_ASSERT( data.cFileName[len-4] == '.');
|
|
|
|
data.cFileName[len-4] = 0;
|
|
|
|
SendDlgItemMessage( state->hDlg, IDC_SVGM_GAMELIST, ADDSTRING,
|
|
|
|
0, (LPARAM)data.cFileName );
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
++state->nItems;
|
|
|
|
|
2008-03-23 20:25:49 +01:00
|
|
|
if ( !FindNextFile( fileH, &data ) ) {
|
|
|
|
XP_ASSERT( GetLastError() == ERROR_NO_MORE_FILES );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SendDlgItemMessage( state->hDlg, IDC_SVGM_GAMELIST, SETCURSEL, curSel, 0 );
|
|
|
|
state->sel = curSel;
|
|
|
|
setEditFromSel( state );
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
setButtons( state );
|
|
|
|
|
2008-03-23 20:25:49 +01:00
|
|
|
LOG_RETURN_VOID();
|
|
|
|
} /* initSavedGamesData */
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
/* static void */
|
|
|
|
/* deleteSelected( CeSavedGamesState* state ) */
|
|
|
|
/* { */
|
|
|
|
/* wchar_t buf[128]; */
|
|
|
|
/* wchar_t path[128]; */
|
|
|
|
/* XP_U16 len = VSIZE(buf); */
|
|
|
|
|
|
|
|
/* /\* confirm first!!!! *\/ */
|
|
|
|
|
|
|
|
/* getCBText( state->hDlg, IDC_SVGM_GAMELIST, state->sel, buf, &len ); */
|
|
|
|
/* swprintf( path, DEFAULT_DIR_NAME L"\\%s.xwg", buf ); */
|
|
|
|
/* DeleteFile( path ); */
|
|
|
|
|
|
|
|
/* SendDlgItemMessage( state->hDlg, IDC_SVGM_GAMELIST, DELETESTRING, */
|
|
|
|
/* state->sel, 0L ); */
|
|
|
|
|
|
|
|
/* --state->nItems; */
|
|
|
|
|
|
|
|
/* setButtons( state ); */
|
|
|
|
/* } */
|
|
|
|
|
2008-03-23 20:25:49 +01:00
|
|
|
static LRESULT CALLBACK
|
|
|
|
SavedGamesDlg( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
|
|
|
|
{
|
2008-04-13 19:14:16 +02:00
|
|
|
CeSavedGamesState* state;
|
2008-03-23 20:25:49 +01:00
|
|
|
XP_U16 wid;
|
|
|
|
BOOL result = FALSE;
|
|
|
|
|
|
|
|
if ( message == WM_INITDIALOG ) {
|
|
|
|
SetWindowLong( hDlg, GWL_USERDATA, lParam );
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
state = (CeSavedGamesState*)lParam;
|
2008-03-23 20:25:49 +01:00
|
|
|
state->hDlg = hDlg;
|
|
|
|
state->inited = XP_FALSE;
|
|
|
|
|
2008-03-31 02:26:18 +02:00
|
|
|
ceDlgSetup( state->globals, hDlg );
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
2008-04-13 19:14:16 +02:00
|
|
|
state = (CeSavedGamesState*)GetWindowLong( hDlg, GWL_USERDATA );
|
2008-03-23 20:25:49 +01:00
|
|
|
if ( !!state ) {
|
|
|
|
|
|
|
|
if ( !state->inited ) {
|
|
|
|
state->inited = XP_TRUE;
|
|
|
|
initSavedGamesData( state );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (message) {
|
|
|
|
|
|
|
|
case WM_VSCROLL:
|
2008-05-10 17:42:52 +02:00
|
|
|
ceDoDlgScroll( state->globals, hDlg, wParam );
|
2008-03-23 20:25:49 +01:00
|
|
|
break;
|
|
|
|
|
2008-03-31 02:26:18 +02:00
|
|
|
case WM_NEXTDLGCTL:
|
2008-05-10 17:42:52 +02:00
|
|
|
ceDoDlgFocusScroll( state->globals, hDlg, wParam, lParam );
|
2008-03-31 02:26:18 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_COMMAND:
|
2008-03-23 20:25:49 +01:00
|
|
|
wid = LOWORD(wParam);
|
|
|
|
switch( wid ) {
|
|
|
|
|
|
|
|
case IDC_SVGM_GAMELIST:
|
|
|
|
if ( HIWORD(wParam) == CBN_SELCHANGE ) {
|
|
|
|
XP_S16 sel = SendDlgItemMessage( state->hDlg,
|
|
|
|
IDC_SVGM_GAMELIST,
|
2008-03-31 02:26:18 +02:00
|
|
|
GETCURSEL, 0, 0L);
|
2008-03-23 20:25:49 +01:00
|
|
|
if ( sel >= 0 ) {
|
|
|
|
state->sel = sel;
|
|
|
|
setEditFromSel( state );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
/* case IDC_SVGM_DUP: */
|
|
|
|
/* case IDC_SVGM_CHANGE: */
|
|
|
|
/* notImpl( state->globals ); */
|
|
|
|
/* break; */
|
|
|
|
/* case IDC_SVGM_DEL: */
|
|
|
|
/* deleteSelected( state ); */
|
|
|
|
/* break; */
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
case IDC_SVGM_OPEN: {
|
|
|
|
wchar_t buf[128];
|
2008-04-13 19:14:16 +02:00
|
|
|
XP_U16 len = VSIZE(buf);
|
2008-03-23 20:25:49 +01:00
|
|
|
getCBText( state->hDlg, IDC_SVGM_GAMELIST, state->sel,
|
|
|
|
buf, &len );
|
|
|
|
swprintf( state->buf, DEFAULT_DIR_NAME L"\\%s.xwg", buf );
|
|
|
|
XP_LOGW( "returning", state->buf );
|
2008-04-13 19:14:16 +02:00
|
|
|
state->opened = XP_TRUE;
|
2008-03-23 20:25:49 +01:00
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
case IDOK:
|
|
|
|
/* fallthrough */
|
|
|
|
case IDCANCEL:
|
|
|
|
EndDialog(hDlg, wid);
|
|
|
|
result = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} /* SavedGamesDlg */
|
|
|
|
|
|
|
|
XP_Bool
|
|
|
|
ceSavedGamesDlg( CEAppGlobals* globals, const XP_UCHAR* curPath,
|
|
|
|
wchar_t* buf, XP_U16 buflen )
|
|
|
|
{
|
2008-04-13 19:14:16 +02:00
|
|
|
CeSavedGamesState state;
|
2008-03-23 20:25:49 +01:00
|
|
|
|
|
|
|
LOG_FUNC();
|
|
|
|
|
|
|
|
XP_MEMSET( &state, 0, sizeof(state) ); /* sets cancelled */
|
|
|
|
state.globals = globals;
|
|
|
|
state.buf = buf;
|
|
|
|
state.buflen = buflen;
|
|
|
|
|
|
|
|
if ( !!curPath ) {
|
|
|
|
wchar_t shortName[128];
|
|
|
|
XP_U16 len;
|
|
|
|
XP_LOGF( curPath );
|
|
|
|
|
|
|
|
len = (XP_U16)XP_STRLEN( curPath );
|
|
|
|
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, curPath, len + 1,
|
|
|
|
shortName, len + 1 );
|
|
|
|
len = wcslen( DEFAULT_DIR_NAME L"\\" );
|
|
|
|
lstrcpy( state.curName, shortName+len );
|
|
|
|
XP_LOGW( "shortName", state.curName );
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)DialogBoxParam( globals->hInst, (LPCTSTR)IDD_SAVEDGAMESDLG,
|
|
|
|
globals->hWnd,
|
|
|
|
(DLGPROC)SavedGamesDlg, (long)&state );
|
|
|
|
|
|
|
|
XP_LOGW( __func__, buf );
|
|
|
|
|
2008-04-13 19:14:16 +02:00
|
|
|
return state.opened;
|
2008-03-23 20:25:49 +01:00
|
|
|
} /*ceSavedGamesDlg */
|