273 lines
6.6 KiB
C++
273 lines
6.6 KiB
C++
#include "stdafx.h"
|
|
#include "XMLTreeFileDlg.h"
|
|
// IDs of controls in Explorer-style dialog, from Spy
|
|
#define ID_FILESOFTYPE 0x0441
|
|
#define ID_FILENAME 0x0442
|
|
#define ID_LOOKIN 0x0443
|
|
|
|
IMPLEMENT_DYNAMIC(CXMLTreeFileDlg, CFileDialog)
|
|
BEGIN_MESSAGE_MAP(CXMLTreeFileDlg, CFileDialog)
|
|
// ON_COMMAND(ID_SELECT_ALL,OnSelectAll)
|
|
// ON_UPDATE_COMMAND_UI(ID_SELECT_ALL,OnUpdateSelectAll)
|
|
ON_WM_SIZE()
|
|
END_MESSAGE_MAP()
|
|
|
|
//////////////////
|
|
// does path ends in ".txt" ?
|
|
//
|
|
static BOOL IsTextFileName(CString fn)
|
|
{
|
|
fn.MakeLower();
|
|
return fn.Right(4)==_T(".xml");
|
|
}
|
|
CXMLTreeFileDlg::CXMLTreeFileDlg(void) :CFileDialog(TRUE,
|
|
NULL, // no default extension
|
|
NULL, // ..or file name
|
|
OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,
|
|
_T("xml Files (*.xml)|*.xml|All Files (*.*)|*.*||"))
|
|
{
|
|
|
|
}
|
|
|
|
CXMLTreeFileDlg::~CXMLTreeFileDlg(void)
|
|
{
|
|
}
|
|
|
|
//////////////////
|
|
// Initialize dialog.
|
|
// For Explorer, this is actually a child of the main dialog.
|
|
//
|
|
BOOL CXMLTreeFileDlg::OnInitDialog()
|
|
{
|
|
// subclass controls...
|
|
//m_edit1.SubclassDlgItem(IDC_MYDLGINFO1, this);
|
|
//m_edit2.SubclassDlgItem(IDC_MYDLGINFO2, this);
|
|
m_tree.SubclassDlgItem(IDC_XMLTREE,this);
|
|
// initialize helper
|
|
m_dlghelper.Init(this);
|
|
|
|
return CFileDialog::OnInitDialog();
|
|
}
|
|
|
|
//////////////////
|
|
// Select all text files. This won't work if user has checked "Hide
|
|
// extensions for known file types" because then the item name in the list
|
|
// box doesn't end in .txt !!
|
|
//
|
|
//void CXMLTreeFileDlg::OnSelectAll()
|
|
//{
|
|
// CXMLTreeFileDlgHelper& fdh = m_dlghelper;
|
|
// CListCtrl* plc = fdh.GetListCtrl();
|
|
// for (int i=0; i<plc->GetItemCount(); i++) {
|
|
// CString fn = fdh.GetItemName(i);
|
|
// if (IsTextFileName(fn)) {
|
|
// plc->SetItemState(i,LVIS_SELECTED,LVIS_SELECTED);
|
|
// }
|
|
// }
|
|
// plc->SetFocus();
|
|
//}
|
|
|
|
//////////////////
|
|
// Update "Select All" button: disable if no .txt files. This won't work if
|
|
// user has checked "Hide extensions for known file types" because then the
|
|
// item name in the list box doesn't end in .txt !!
|
|
//
|
|
//void CXMLTreeFileDlg::OnUpdateSelectAll(CCmdUI* pCmdUI)
|
|
//{
|
|
// CXMLTreeFileDlgHelper& fdh = m_dlghelper;
|
|
// CListCtrl* plc = fdh.GetListCtrl();
|
|
// for (int i=0; i<plc->GetItemCount(); i++) {
|
|
// CString fn = fdh.GetItemName(i);
|
|
// if (IsTextFileName(fn)) {
|
|
// pCmdUI->Enable(TRUE);
|
|
// return;
|
|
// }
|
|
// }
|
|
// pCmdUI->Enable(FALSE);
|
|
//}
|
|
|
|
//////////////////
|
|
// DoModal override: use my template
|
|
//
|
|
// Zyu modify on 100721,支持64位
|
|
//int CXMLTreeFileDlg::DoModal()
|
|
INT_PTR CXMLTreeFileDlg::DoModal()
|
|
{
|
|
// Add my custom dialog to bottom
|
|
m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_XMLTREEDIALOG);
|
|
m_ofn.Flags |= OFN_ENABLETEMPLATE;
|
|
return CFileDialog::DoModal();
|
|
}
|
|
|
|
//////////////////
|
|
// User selected new file (CDN_SELCHANGE)
|
|
//
|
|
void CXMLTreeFileDlg::OnFileNameChange()
|
|
{
|
|
ShowFileInfo();
|
|
}
|
|
|
|
//////////////////
|
|
// User selected new folder (CDN_SELCHANGE)
|
|
//
|
|
void CXMLTreeFileDlg::OnFolderChange()
|
|
{
|
|
ShowFileInfo();
|
|
}
|
|
|
|
//////////////////
|
|
// User selected new file type (from drop-down)
|
|
//
|
|
void CXMLTreeFileDlg::OnTypeChange()
|
|
{
|
|
ShowFileInfo();
|
|
}
|
|
|
|
////////////////// CDM_GETFILEPATH
|
|
// Common helper: show information in the preview and debug panes
|
|
//
|
|
void CXMLTreeFileDlg::ShowFileInfo()
|
|
{
|
|
CXMLTreeFileDlgHelper& fdh = m_dlghelper;
|
|
CString path = GetPathName();
|
|
CString fldr = GetFolderPath();
|
|
|
|
// Create debug message
|
|
//
|
|
/*CString s;
|
|
s.Format("GetPathName=%s\nGetFolderPath=%s\n",
|
|
(LPCTSTR)path, (LPCTSTR)fldr);
|
|
|
|
int nSelected = 0;
|
|
s += "选中:\n";
|
|
CListCtrl* plc = fdh.GetListCtrl();
|
|
POSITION pos = plc->GetFirstSelectedItemPosition();
|
|
while (pos) {
|
|
int i = plc->GetNextSelectedItem(pos);
|
|
CString name = fdh.GetItemName(i);
|
|
CString type;
|
|
if (fdh.IsItemFolder(i))
|
|
type = "(此为文件夹)";
|
|
CString s2;
|
|
s2.Format(" %s %s\n", (LPCTSTR)name, (LPCTSTR)type);
|
|
s += s2;
|
|
nSelected++;
|
|
}*/
|
|
//AddText(m_edit2, s, TRUE);
|
|
|
|
// Create preview text
|
|
//
|
|
/*s.Empty();
|
|
if (nSelected==1 && IsTextFileName(path)) {
|
|
s = GetTextPreview(path);
|
|
} else if (nSelected>1) {
|
|
s = "[选中多个文件]";
|
|
} else if (nSelected==0) {
|
|
s = "[没有选择]";
|
|
}*/
|
|
//AddText(m_edit1,s,TRUE);
|
|
}
|
|
|
|
//////////////////
|
|
// Preview: Read first 256 characters of text file
|
|
//
|
|
CString CXMLTreeFileDlg::GetTextPreview(LPCTSTR pszPath)
|
|
{
|
|
CString buf;
|
|
CFile f;
|
|
if (f.Open(pszPath,CFile::modeRead)) {
|
|
int len = f.Read(buf.GetBuffer(256),256);
|
|
buf.ReleaseBuffer();
|
|
if (len==256)
|
|
buf += "...";
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
//////////////////
|
|
// Helper adds text to an edit control that I've added to the open dialog
|
|
//
|
|
void CXMLTreeFileDlg::AddText(CEdit& wndEdit, LPCTSTR lpText, BOOL bClear)
|
|
{
|
|
if (wndEdit) {
|
|
// Convert \n to \n\r for Windows brain-damaged edit control !&#%!
|
|
// It's 2001, and I'm still writing code like this!
|
|
//
|
|
LPCTSTR src = lpText;
|
|
|
|
TCHAR buf[1024];
|
|
TCHAR* dst = buf;
|
|
TCHAR* endbuf = buf + sizeof(buf) - 1;
|
|
|
|
while (*src && dst < endbuf) {
|
|
if (*src == '\n')
|
|
*dst++ = '\r';
|
|
*dst++ = *src++;
|
|
}
|
|
*dst = 0;
|
|
|
|
wndEdit.SetSel(bClear ? 0 : -1, -1); // end of edit text
|
|
wndEdit.ReplaceSel(buf); // append string..
|
|
wndEdit.SendMessage(EM_SCROLLCARET); // ..and show caret
|
|
}
|
|
}
|
|
|
|
//////////////////
|
|
// Dialog was sized: reposition my controls. Zzzzz.
|
|
//
|
|
void CXMLTreeFileDlg::OnSize(UINT nType, int cx, int cy)
|
|
{
|
|
CWnd* pDlg = GetParent();
|
|
CRect rcDlg;
|
|
pDlg->GetWindowRect(&rcDlg);
|
|
|
|
/*CWnd* pCancel = pDlg->GetDlgItem(IDCANCEL);
|
|
ASSERT(pCancel);
|
|
CRect rcCancel;
|
|
pCancel->GetWindowRect(&rcCancel);
|
|
|
|
int cxRightMargin = rcDlg.right-rcCancel.right;*/
|
|
CWnd *pList = pDlg->GetDlgItem(2);
|
|
ASSERT(pList);
|
|
CRect rectList;
|
|
pList->GetWindowRect(&rectList);
|
|
|
|
CWnd *pTree = GetDlgItem(IDC_XMLTREE);
|
|
ASSERT(pTree);
|
|
CRect rectTree;
|
|
pTree->GetWindowRect(&rectTree);
|
|
int nLen = rectTree.Width();
|
|
|
|
CRect rect;
|
|
POINT bottomRight;
|
|
POINT topLeft;
|
|
topLeft.x = rectList.TopLeft().x;
|
|
topLeft.y = rectList.TopLeft().y;
|
|
bottomRight.y = rectList.BottomRight().y;
|
|
bottomRight.x = rectList.BottomRight().x-nLen - 10;
|
|
rect.SetRect(topLeft,bottomRight);
|
|
ScreenToClient(&rect);
|
|
pList->MoveWindow(&rect);
|
|
// pList->setwi
|
|
//pList->MoveWindow()
|
|
/*CWnd* pSelAll = GetDlgItem(ID_SELECT_ALL);
|
|
ASSERT(pSelAll);
|
|
CRect rc;
|
|
pSelAll->GetWindowRect(&rc);
|
|
rc.left = rcDlg.right - cxRightMargin - rcCancel.Width();
|
|
rc.right = rc.left + rcCancel.Width();
|
|
rc.bottom= rc.top + rcCancel.Height();
|
|
ScreenToClient(&rc);
|
|
pSelAll->SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),0);
|
|
|
|
for (int i = IDC_MYDLGINFO1; i<= IDC_MYDLGINFO2; i++) {
|
|
CWnd* pInfoWnd = GetDlgItem(i);
|
|
ASSERT(pInfoWnd);
|
|
pInfoWnd->GetWindowRect(&rc);
|
|
rc.right = rcDlg.right - cxRightMargin;
|
|
ScreenToClient(&rc);
|
|
pInfoWnd->SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),0);
|
|
}*/
|
|
}
|
|
|