359 lines
10 KiB
C++
359 lines
10 KiB
C++
#include <dos.h>
|
||
#include <io.h>
|
||
#include <dir.h>
|
||
#include <string.h>
|
||
#include <common/string.hpp>
|
||
#include <winmus/main.hpp>
|
||
#include <winmus/mididef.hpp>
|
||
#include <winmus/filehelp.hpp>
|
||
|
||
SelectFile::SelectFile(Window &parentWindow,Mode readWrite)
|
||
: mwFileAttribute((WORD)DefaultAttribute), mhParent(parentWindow),
|
||
mMode(readWrite), mhInstance(parentWindow.processInstance()),
|
||
mCommandHandler(this,&SelectFile::commandHandler),
|
||
mInitDialogHandler(this,&SelectFile::initDialogHandler),
|
||
mDrawItemHandler(this,&SelectFile::drawItemHandler),
|
||
mOwnerDraw(OwnerDraw::UseInstance), mhGrayBrush(::CreateSolidBrush(RGB(btnRed,btnGreen,btnBlue)))
|
||
{
|
||
insertHandler(VectorHandler::CommandHandler,&mCommandHandler);
|
||
insertHandler(VectorHandler::InitDialogHandler,&mInitDialogHandler);
|
||
insertHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
|
||
mszFileName[0]='\0';
|
||
::strcpy(mszDefExt,".DAT");
|
||
::strcpy(mszFileSpec,"*.*");
|
||
mLastDrive=::getdisk();
|
||
::getcwd(mszDirectory,sizeof(mszDirectory));
|
||
}
|
||
|
||
SelectFile::~SelectFile()
|
||
{
|
||
char Path[132];
|
||
|
||
if(mhGrayBrush)::DeleteObject(mhGrayBrush);
|
||
::wsprintf(Path,"%c:\\",mLastDrive+65);
|
||
::setdisk(mLastDrive);
|
||
::strcat(Path,mszDirectory);
|
||
::chdir(Path);
|
||
removeHandler(VectorHandler::CommandHandler,&mCommandHandler);
|
||
removeHandler(VectorHandler::InitDialogHandler,&mInitDialogHandler);
|
||
removeHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
|
||
}
|
||
|
||
int SelectFile::GetFileName(char *szNameBuf,WORD nMaxChars)
|
||
{
|
||
WORD returnCode;
|
||
|
||
returnCode=::DialogBoxParam(mhInstance,(LPSTR)"FileSelect",mhParent,(DLGPROC)DWindow::DialogProcedure,(LONG)((DWindow*)this));
|
||
if(returnCode)
|
||
::strncpy(szNameBuf,mszFileName,nMaxChars>sizeof(mszFileName)?sizeof(mszFileName):nMaxChars);
|
||
return returnCode;
|
||
}
|
||
|
||
void SelectFile::Initialize()
|
||
{
|
||
::SendDlgItemMessage((HWND)*this,IDS_FNAME,EM_LIMITTEXT,80,0L);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
waitCursor(TRUE);
|
||
::DlgDirList((HWND)*this,mszFileSpec,IDS_FLIST,IDS_FPATH,mwFileAttribute);
|
||
waitCursor(FALSE);
|
||
SetExtended();
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
::SendMessage(GetDlgItem((HWND)*this,IDS_FLIST),LB_GETTEXT,0,(LONG)mszFileName);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,LB_SETCURSEL,0,0L);
|
||
#if defined(__FLAT__)
|
||
if(DlgDirSelectExA((HWND)*this,mszFileName,sizeof(mszFileName),IDS_FLIST))
|
||
#else
|
||
if(DlgDirSelect((HWND)*this,mszFileName,IDS_FLIST))
|
||
#endif
|
||
::lstrcat(mszFileName,mszFileSpec);
|
||
::SetDlgItemText((HWND)*this,IDS_FNAME,mszFileName);
|
||
}
|
||
|
||
void SelectFile::SelChange()
|
||
{
|
||
#if defined(__FLAT__)
|
||
if(DlgDirSelectExA((HWND)*this,mszFileName,sizeof(mszFileName),IDS_FLIST))
|
||
#else
|
||
if(DlgDirSelect((HWND)*this,mszFileName,IDS_FLIST))
|
||
#endif
|
||
::lstrcat(mszFileName,mszFileSpec);
|
||
::SetDlgItemText((HWND)*this,IDS_FNAME,mszFileName);
|
||
}
|
||
|
||
void SelectFile::DoubleClick()
|
||
{
|
||
waitCursor(TRUE);
|
||
#if defined(__FLAT__)
|
||
if(DlgDirSelectExA((HWND)*this,mszFileName,sizeof(mszFileName),IDS_FLIST))
|
||
#else
|
||
if(DlgDirSelect((HWND)*this,mszFileName,IDS_FLIST))
|
||
#endif
|
||
{
|
||
::lstrcat(mszFileName,mszFileSpec);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
::DlgDirList((HWND)*this,mszFileName,IDS_FLIST,IDS_FPATH,mwFileAttribute);
|
||
SetExtended();
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
::SetDlgItemText((HWND)*this,IDS_FNAME,mszFileSpec);
|
||
}
|
||
else
|
||
{
|
||
::SetDlgItemText((HWND)*this,IDS_FNAME,mszFileName);
|
||
::SendMessage((HWND)*this,WM_COMMAND,IDOK,0L);
|
||
}
|
||
waitCursor(FALSE);
|
||
}
|
||
|
||
int SelectFile::CheckFileNameSpec()
|
||
{
|
||
::GetDlgItemText((HWND)*this,IDS_FNAME,mszFileName,sizeof(mszFileName));
|
||
mnEditLen=::lstrlen(mszFileName);
|
||
mcLastChar=*::AnsiPrev(mszFileName,mszFileName+mnEditLen);
|
||
if(mcLastChar=='\\'||mcLastChar==':')::lstrcat(mszFileName,mszFileSpec);
|
||
if(lstrchr(mszFileName,'*')||lstrchr(mszFileName,'?'))
|
||
{
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
waitCursor(TRUE);
|
||
if(::DlgDirList((HWND)*this,mszFileName,IDS_FLIST,IDS_FPATH,mwFileAttribute))
|
||
{
|
||
SetExtended();
|
||
::strcpy(mszFileSpec,mszFileName);
|
||
::SetDlgItemText((HWND)*this,IDS_FNAME,mszFileSpec);
|
||
}
|
||
else ::MessageBeep(0);
|
||
waitCursor(FALSE);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
return FALSE;
|
||
}
|
||
::lstrcat(lstrcat(mszFileName,"\\"),mszFileSpec);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
waitCursor(TRUE);
|
||
if(::DlgDirList((HWND)*this,mszFileName,IDS_FLIST,IDS_FPATH,mwFileAttribute))
|
||
{
|
||
waitCursor(FALSE);
|
||
SetExtended();
|
||
::strcpy(mszFileSpec,mszFileName);
|
||
::SetDlgItemText((HWND)*this,IDS_FNAME,mszFileSpec);
|
||
return FALSE;
|
||
}
|
||
waitCursor(FALSE);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
mszFileName[mnEditLen]='\0';
|
||
if(-1==(mfd=::OpenFile(mszFileName,&mPof,OF_SHARE_COMPAT|OF_EXIST)))
|
||
{
|
||
if(Write==mMode)
|
||
{
|
||
if(-1==(mfd=::OpenFile(mszFileName,&mPof,OF_WRITE|OF_CREATE)))
|
||
{
|
||
::MessageBeep(0);
|
||
return FALSE;
|
||
}
|
||
::_lclose(mfd);
|
||
::unlink(mszFileName);
|
||
return TRUE;
|
||
}
|
||
::lstrcat(mszFileName,mszDefExt);
|
||
if(-1==(mfd=::OpenFile(mszFileName,&mPof,OF_SHARE_COMPAT|OF_EXIST)))
|
||
{
|
||
::MessageBeep(0);
|
||
return FALSE;
|
||
}
|
||
else ::_lclose(mfd);
|
||
}
|
||
else ::_lclose(mfd);
|
||
::strcpy(mszFileName,mPof.szPathName);
|
||
::OemToAnsi(mszFileName,mszFileName);
|
||
if(Write==mMode)
|
||
{
|
||
::MessageBeep(0);
|
||
switch(
|
||
::MessageBox((HWND)*this,(LPSTR)"Overwrite this file?",(LPSTR)mszFileName,MB_OKCANCEL))
|
||
{
|
||
case IDOK :
|
||
return TRUE;
|
||
case IDCANCEL :
|
||
return FALSE;
|
||
}
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
LPSTR SelectFile::lstrchr(LPSTR str,char ch)
|
||
{
|
||
while(*str)
|
||
{
|
||
if(ch==*str)return str;
|
||
str=::AnsiNext(str);
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
LPSTR SelectFile::lstrrchr(LPSTR str,char ch)
|
||
{
|
||
LPSTR strl=str+lstrlen(str);
|
||
|
||
do
|
||
{
|
||
if(ch==*strl)return strl;
|
||
strl=::AnsiPrev(str,strl);
|
||
}while(strl>str);
|
||
return NULL;
|
||
}
|
||
|
||
void SelectFile::SetExtended()
|
||
{
|
||
HCURSOR hCursor;
|
||
ffblk mffblk;
|
||
char szBuffer[100];
|
||
char szFormat[100];
|
||
char minBuf[5];
|
||
char hrBuf[5];
|
||
char moBuf[5];
|
||
char dayBuf[5];
|
||
unsigned Year;
|
||
unsigned Month;
|
||
unsigned Day;
|
||
unsigned Hour;
|
||
unsigned Minute;
|
||
char cAmPm;
|
||
|
||
int nItems=(int)::SendDlgItemMessage((HWND)*this,IDS_FLIST,LB_GETCOUNT,0,0L);
|
||
if(!nItems)return;
|
||
hCursor=::SetCursor(::LoadCursor(NULL,IDC_WAIT));
|
||
::ShowCursor(TRUE);
|
||
for(int nIndex=0;nIndex<nItems;nIndex++)
|
||
{
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,LB_GETTEXT,nIndex,(LPARAM)(LPCSTR)szBuffer);
|
||
if(!::findfirst(szBuffer,&mffblk,FA_ARCH))
|
||
{
|
||
Year=(mffblk.ff_fdate>>9)+1980;
|
||
Month=(mffblk.ff_fdate>>5)&0x0F;
|
||
Day=mffblk.ff_fdate&0x1F;
|
||
Hour=(mffblk.ff_ftime>>11)&0x1F;
|
||
Minute=(mffblk.ff_ftime>>5)&0x3F;
|
||
if(Hour>=12 && Minute)
|
||
{
|
||
Hour=Hour==12?1:Hour-12;
|
||
if(Hour==12)cAmPm='a';
|
||
else cAmPm='p';
|
||
}
|
||
else
|
||
{
|
||
if(!Hour)Hour=12;
|
||
cAmPm='a';
|
||
}
|
||
::wsprintf(minBuf,"%d",Minute);
|
||
if(1==::lstrlen(minBuf))
|
||
{
|
||
minBuf[2]='\0';
|
||
minBuf[1]=minBuf[0];
|
||
minBuf[0]='0';
|
||
}
|
||
::wsprintf(hrBuf,"%d",Hour);
|
||
if(1==::lstrlen(hrBuf))
|
||
{
|
||
hrBuf[3]='\0';
|
||
hrBuf[2]=hrBuf[0];
|
||
hrBuf[1]=' ';
|
||
hrBuf[0]=' ';
|
||
}
|
||
::wsprintf(moBuf,"%d",Month);
|
||
if(1==::lstrlen(moBuf))
|
||
{
|
||
moBuf[2]='\0';
|
||
moBuf[1]=moBuf[0];
|
||
moBuf[0]='0';
|
||
}
|
||
::wsprintf(dayBuf,"%d",Day);
|
||
if(1==::lstrlen(dayBuf))
|
||
{
|
||
dayBuf[2]='\0';
|
||
dayBuf[1]=dayBuf[0];
|
||
dayBuf[0]='0';
|
||
}
|
||
::wsprintf(szFormat,"\t%s-%s-%d\t%9ld\t%s:%s%c",
|
||
(LPSTR)moBuf,(LPSTR)dayBuf,Year,mffblk.ff_fsize,(LPSTR)hrBuf,(LPSTR)minBuf,cAmPm);
|
||
::lstrcat(szBuffer,szFormat);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,LB_DELETESTRING,nIndex,0L);
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,LB_INSERTSTRING,nIndex,(LPARAM)(LPCSTR)szBuffer);
|
||
}
|
||
}
|
||
::ShowCursor(FALSE);
|
||
::SetCursor(hCursor);
|
||
}
|
||
|
||
CallbackData::ReturnType SelectFile::initDialogHandler(CallbackData &/*someCallbackData*/)
|
||
{
|
||
int TabStopList[3];
|
||
WORD DlgWidthUnits;
|
||
String windowText;
|
||
|
||
DlgWidthUnits=LOWORD(::GetDialogBaseUnits())/4;
|
||
mOwnerDraw.associate(IDOK,
|
||
String(STRING_BITMAPOKFOCUSUP),
|
||
String(STRING_BITMAPOKNOFUP),
|
||
String(STRING_BITMAPOKFOCUSDN),OwnerDraw::NOFOCUS);
|
||
mOwnerDraw.associate(IDCANCEL,
|
||
String(STRING_BITMAPCAFOCUSUP),
|
||
String(STRING_BITMAPCANOFUP),
|
||
String(STRING_BITMAPCAFOCUSDN),OwnerDraw::NOFOCUS);
|
||
Initialize();
|
||
TabStopList[0]=DlgWidthUnits*13*2;
|
||
TabStopList[1]=DlgWidthUnits*28*2;
|
||
TabStopList[2]=DlgWidthUnits*38*2;
|
||
::SendDlgItemMessage((HWND)*this,IDS_FLIST,LB_SETTABSTOPS,3,(LONG)(LPSTR)TabStopList);
|
||
::GetDlgItemText((HWND)*this,(Write==mMode?IDS_FILESAVE:IDS_FILEOPEN),windowText,String::MaxString);
|
||
::SetWindowText((HWND)*this,windowText);
|
||
return (CallbackData::ReturnType)TRUE;
|
||
}
|
||
|
||
CallbackData::ReturnType SelectFile::drawItemHandler(CallbackData &someCallbackData)
|
||
{
|
||
if(IDOK==((LPDRAWITEMSTRUCT)someCallbackData.lParam())->CtlID)
|
||
mOwnerDraw.handleOwnerButton(IDOK,someCallbackData.lParam());
|
||
else if(IDCANCEL==((LPDRAWITEMSTRUCT)someCallbackData.lParam())->CtlID)
|
||
mOwnerDraw.handleOwnerButton(IDCANCEL,someCallbackData.lParam());
|
||
return (CallbackData::ReturnType)TRUE;
|
||
}
|
||
|
||
CallbackData::ReturnType SelectFile::commandHandler(CallbackData &someCallbackData)
|
||
{
|
||
switch(GET_WM_COMMAND_ID(someCallbackData.wParam(),someCallbackData.lParam()))
|
||
{
|
||
case IDS_FLIST :
|
||
switch(GET_WM_COMMAND_CMD(someCallbackData.wParam(),someCallbackData.lParam()))
|
||
{
|
||
case LBN_SELCHANGE :
|
||
SelChange();
|
||
return (CallbackData::ReturnType)TRUE;
|
||
case LBN_DBLCLK :
|
||
DoubleClick();
|
||
return (CallbackData::ReturnType)TRUE;
|
||
}
|
||
break;
|
||
case IDS_FNAME :
|
||
if(EN_CHANGE==GET_WM_COMMAND_CMD(someCallbackData.wParam(),someCallbackData.lParam()))
|
||
::EnableWindow(::GetDlgItem((HWND)*this,IDOK),(BOOL)
|
||
::SendMessage(GET_WM_COMMAND_HWND(someCallbackData.wParam(),someCallbackData.lParam()),WM_GETTEXTLENGTH,0,0L));
|
||
return (CallbackData::ReturnType)TRUE;
|
||
case IDOK :
|
||
if(CheckFileNameSpec())
|
||
{
|
||
mOwnerDraw.freeButton(IDOK);
|
||
mOwnerDraw.freeButton(IDCANCEL);
|
||
endDialog(TRUE);
|
||
}
|
||
return (CallbackData::ReturnType)TRUE;
|
||
case IDCANCEL :
|
||
mOwnerDraw.freeButton(IDOK);
|
||
mOwnerDraw.freeButton(IDCANCEL);
|
||
endDialog(FALSE);
|
||
return (CallbackData::ReturnType)TRUE;
|
||
default :
|
||
return (CallbackData::ReturnType)FALSE;
|
||
}
|
||
return (CallbackData::ReturnType)FALSE;
|
||
}
|
||
|
||
|
||
|