349 lines
9.2 KiB
Plaintext
349 lines
9.2 KiB
Plaintext
#include <dos.h>
|
||
#include <io.h>
|
||
#include <dir.h>
|
||
#include <string.h>
|
||
#include <mdiwin/main.hpp>
|
||
#include <mdiwin/string.hpp>
|
||
#include <mdiwin/mdiwin.h>
|
||
#include <mdiwin/filehelp.hpp>
|
||
|
||
SelectFile::SelectFile(HWND hParent,Mode readWrite)
|
||
: mwFileAttribute((WORD)DefaultAttribute), mhParent(hParent),
|
||
mMode(readWrite), mhInstance(Main::processInstance(mhParent))
|
||
{
|
||
mszFileName[0]='\0';
|
||
::strcpy(mszDefExt,".DAT");
|
||
::strcpy(mszFileSpec,"*.*");
|
||
mLastDrive=::getdisk();
|
||
::getcwd(mszDirectory,sizeof(mszDirectory));
|
||
}
|
||
SelectFile::~SelectFile()
|
||
{
|
||
char Path[132];
|
||
::wsprintf(Path,"%c:\\",mLastDrive+65);
|
||
::setdisk(mLastDrive);
|
||
::strcat(Path,mszDirectory);
|
||
::chdir(Path);
|
||
}
|
||
|
||
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(GetHandle(),IDS_FNAME,EM_LIMITTEXT,80,0L);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
waitCursor(TRUE);
|
||
::DlgDirList(GetHandle(),mszFileSpec,IDS_FLIST,IDS_FPATH,mwFileAttribute);
|
||
waitCursor(FALSE);
|
||
SetExtended();
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
::SendMessage(GetDlgItem(GetHandle(),IDS_FLIST),LB_GETTEXT,0,(LONG)mszFileName);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,LB_SETCURSEL,0,0L);
|
||
#if defined(__FLAT__)
|
||
if(DlgDirSelectExA(GetHandle(),mszFileName,sizeof(mszFileName),IDS_FLIST))
|
||
#else
|
||
if(DlgDirSelect(GetHandle(),mszFileName,IDS_FLIST))
|
||
#endif
|
||
::lstrcat(mszFileName,mszFileSpec);
|
||
::SetDlgItemText(GetHandle(),IDS_FNAME,mszFileName);
|
||
}
|
||
|
||
void SelectFile::SelChange()
|
||
{
|
||
#if defined(__FLAT__)
|
||
if(DlgDirSelectExA(GetHandle(),mszFileName,sizeof(mszFileName),IDS_FLIST))
|
||
#else
|
||
if(DlgDirSelect(GetHandle(),mszFileName,IDS_FLIST))
|
||
#endif
|
||
::lstrcat(mszFileName,mszFileSpec);
|
||
::SetDlgItemText(GetHandle(),IDS_FNAME,mszFileName);
|
||
}
|
||
|
||
void SelectFile::DoubleClick()
|
||
{
|
||
waitCursor(TRUE);
|
||
#if defined(__FLAT__)
|
||
if(DlgDirSelectExA(GetHandle(),mszFileName,sizeof(mszFileName),IDS_FLIST))
|
||
#else
|
||
if(DlgDirSelect(GetHandle(),mszFileName,IDS_FLIST))
|
||
#endif
|
||
{
|
||
::lstrcat(mszFileName,mszFileSpec);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
::DlgDirList(GetHandle(),mszFileName,IDS_FLIST,IDS_FPATH,mwFileAttribute);
|
||
SetExtended();
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
::SetDlgItemText(GetHandle(),IDS_FNAME,mszFileSpec);
|
||
}
|
||
else
|
||
{
|
||
::SetDlgItemText(GetHandle(),IDS_FNAME,mszFileName);
|
||
::SendMessage(GetHandle(),WM_COMMAND,IDOK,0L);
|
||
}
|
||
waitCursor(FALSE);
|
||
}
|
||
|
||
int SelectFile::CheckFileNameSpec()
|
||
{
|
||
::GetDlgItemText(GetHandle(),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(GetHandle(),IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
waitCursor(TRUE);
|
||
if(::DlgDirList(GetHandle(),mszFileName,IDS_FLIST,IDS_FPATH,mwFileAttribute))
|
||
{
|
||
SetExtended();
|
||
::strcpy(mszFileSpec,mszFileName);
|
||
::SetDlgItemText(GetHandle(),IDS_FNAME,mszFileSpec);
|
||
}
|
||
else ::MessageBeep(0);
|
||
waitCursor(FALSE);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
return FALSE;
|
||
}
|
||
::lstrcat(lstrcat(mszFileName,"\\"),mszFileSpec);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,FALSE,0L);
|
||
waitCursor(TRUE);
|
||
if(::DlgDirList(GetHandle(),mszFileName,IDS_FLIST,IDS_FPATH,mwFileAttribute))
|
||
{
|
||
waitCursor(FALSE);
|
||
SetExtended();
|
||
::strcpy(mszFileSpec,mszFileName);
|
||
::SetDlgItemText(GetHandle(),IDS_FNAME,mszFileSpec);
|
||
return FALSE;
|
||
}
|
||
waitCursor(FALSE);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,WM_SETREDRAW,TRUE,0L);
|
||
mszFileName[mnEditLen]='\0';
|
||
if(-1==(mfd=::OpenFile(mszFileName,&mPof,OF_READ|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_READ|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(GetHandle(),(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;
|
||
}
|
||
|
||
int SelectFile::DlgProc(UINT message,WPARAM wParam,LPARAM lParam)
|
||
{
|
||
switch(message)
|
||
{
|
||
case WM_INITDIALOG :
|
||
{
|
||
int TabStopList[3];
|
||
WORD DlgWidthUnits;
|
||
String windowText;
|
||
|
||
DlgWidthUnits=LOWORD(::GetDialogBaseUnits())/4;
|
||
Main::smhBitmap.associate(IDOK,
|
||
String(STRING_BITMAPOKFOCUSUP,mhInstance),
|
||
String(STRING_BITMAPOKNOFUP,mhInstance),
|
||
String(STRING_BITMAPOKFOCUSDN,mhInstance),OwnerDraw::NOFOCUS);
|
||
Main::smhBitmap.associate(IDCANCEL,
|
||
String(STRING_BITMAPCAFOCUSUP,mhInstance),
|
||
String(STRING_BITMAPCANOFUP,mhInstance),
|
||
String(STRING_BITMAPCAFOCUSDN,mhInstance),OwnerDraw::NOFOCUS);
|
||
Initialize();
|
||
TabStopList[0]=DlgWidthUnits*13*2;
|
||
TabStopList[1]=DlgWidthUnits*28*2;
|
||
TabStopList[2]=DlgWidthUnits*38*2;
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,LB_SETTABSTOPS,3,(LONG)(LPSTR)TabStopList);
|
||
::GetDlgItemText(GetHandle(),(Write==mMode?IDS_FILESAVE:IDS_FILEOPEN),windowText,String::MaxString);
|
||
::SetWindowText(GetHandle(),windowText);
|
||
}
|
||
return TRUE;
|
||
case WM_DRAWITEM :
|
||
switch(((LPDRAWITEMSTRUCT)lParam)->CtlID)
|
||
{
|
||
case IDOK :
|
||
Main::smhBitmap.handleOwnerButton(IDOK,lParam);
|
||
return TRUE;
|
||
case IDCANCEL :
|
||
Main::smhBitmap.handleOwnerButton(IDCANCEL,lParam);
|
||
return TRUE;
|
||
}
|
||
return TRUE;
|
||
case WM_COMMAND :
|
||
switch(GET_WM_COMMAND_ID(wParam,lParam))
|
||
{
|
||
case IDS_FLIST :
|
||
switch(GET_WM_COMMAND_CMD(wParam,lParam))
|
||
{
|
||
case LBN_SELCHANGE :
|
||
SelChange();
|
||
return TRUE;
|
||
case LBN_DBLCLK :
|
||
DoubleClick();
|
||
return TRUE;
|
||
}
|
||
break;
|
||
case IDS_FNAME :
|
||
if(GET_WM_COMMAND_CMD(wParam,lParam)==EN_CHANGE)
|
||
::EnableWindow(::GetDlgItem(GetHandle(),IDOK),
|
||
(BOOL)::SendMessage(GET_WM_COMMAND_HWND(wParam,lParam),
|
||
WM_GETTEXTLENGTH,0,0L));
|
||
return TRUE;
|
||
case IDOK :
|
||
if(CheckFileNameSpec())
|
||
{
|
||
Main::smhBitmap.freeButton(IDOK);
|
||
Main::smhBitmap.freeButton(IDCANCEL);
|
||
::EndDialog(GetHandle(),TRUE);
|
||
}
|
||
return TRUE;
|
||
case IDCANCEL :
|
||
Main::smhBitmap.freeButton(IDOK);
|
||
Main::smhBitmap.freeButton(IDCANCEL);
|
||
::EndDialog(GetHandle(),FALSE);
|
||
return TRUE;
|
||
}
|
||
}
|
||
return FALSE;
|
||
}
|
||
|
||
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];
|
||
int nIndex=0;
|
||
unsigned Year;
|
||
unsigned Month;
|
||
unsigned Day;
|
||
unsigned Hour;
|
||
unsigned Minute;
|
||
char cAmPm;
|
||
|
||
int nItems=(int)::SendDlgItemMessage(GetHandle(),IDS_FLIST,LB_GETCOUNT,0,0L);
|
||
if(!nItems)return;
|
||
hCursor=::SetCursor(::LoadCursor(NULL,IDC_WAIT));
|
||
::ShowCursor(TRUE);
|
||
for(nIndex=0;nIndex<nItems;nIndex++)
|
||
{
|
||
::SendDlgItemMessage(GetHandle(),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(GetHandle(),IDS_FLIST,LB_DELETESTRING,nIndex,0L);
|
||
::SendDlgItemMessage(GetHandle(),IDS_FLIST,LB_INSERTSTRING,nIndex,(LPARAM)(LPCSTR)szBuffer);
|
||
}
|
||
}
|
||
::ShowCursor(FALSE);
|
||
::SetCursor(hCursor);
|
||
}
|
||
|