CategoryNewDlg.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:7k
源码类别:
P2P编程
开发平台:
Visual C++
- // CategoryNewDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "CategoryNewDlg.h"
- #include "CategoryBarWnd.h"
- #include "FileBase.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CCategoryNewDlg dialog
- CCategoryNewDlg::CCategoryNewDlg(CImageList* pctlImage,
- CCategoryBarWnd* pCategoryBarWnd, bool bNewOrMoveto, HTREEITEM hItemSel,
- CWnd* pParent /*=NULL*/)
- : CDialog(CCategoryNewDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CCategoryNewDlg)
- m_strCategoryDir = _T("");
- m_strCategoryName = _T("");
- //}}AFX_DATA_INIT
- m_pctlImage = pctlImage;
- m_pCategoryBarWnd = pCategoryBarWnd;
- m_bNewOrMoveto = bNewOrMoveto;
- m_ItemSel = hItemSel;
- m_bAutoFill = false;
- }
- void CCategoryNewDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCategoryNewDlg)
- DDX_Control(pDX, IDC_BUTTON_FOLDER_SEL, m_btnBrowseDir);
- DDX_Control(pDX, IDCANCEL, m_btnCancel);
- DDX_Control(pDX, IDOK, m_btnOK);
- DDX_Control(pDX, IDC_TREE_CATEGORY, m_treeCatetory);
- DDX_Text(pDX, IDC_EDIT_CATEGORY_DIR, m_strCategoryDir);
- DDX_Text(pDX, IDC_EDIT_CATEGORY_NAME, m_strCategoryName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CCategoryNewDlg, CDialog)
- //{{AFX_MSG_MAP(CCategoryNewDlg)
- ON_EN_CHANGE(IDC_EDIT_CATEGORY_NAME, OnChangeEditCategoryName)
- ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_CATEGORY, OnSelchangedTreeCategory)
- ON_EN_CHANGE(IDC_EDIT_CATEGORY_DIR, OnChangeEditCategoryDir)
- ON_WM_CREATE()
- ON_BN_CLICKED(IDC_BUTTON_FOLDER_SEL, OnButtonFolderSel)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCategoryNewDlg message handlers
- BOOL CCategoryNewDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- //
- // Create BitmapList
- //
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_BITMAP_DLG_ICONS),
- IMAGE_BITMAP,
- 0,0, // cx,cy
- LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
- CBitmap bm;
- bm.Attach(hbm);
- m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 3, 4);
- m_ctlImage.SetBkColor(RGB(255, 255,255));
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- //
- // set button image.
- //
- // m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存路径");
- m_btnOK.Create(m_ctlImage.ExtractIcon(0));
- m_btnCancel.Create(m_ctlImage.ExtractIcon(1));
- //
- //
- //
- assert(m_pCategoryBarWnd && m_ItemSel && m_pctlImage);
- m_bAutoFill = true;
- m_treeCatetory.SetImageList(m_pctlImage);
- CopyTree(m_pCategoryBarWnd->m_curRoot, m_treeCatetory.GetRootItem());
- if (!m_treeCatetory.GetSelectedItem())
- m_treeCatetory.GetRootItem().Select();
- assert(m_treeCatetory.GetSelectedItem());
- OnSelchangedTreeCategory(0, 0);
- if (m_bNewOrMoveto)
- {
- GetDlgItem(IDC_EDIT_CATEGORY_NAME)->SetFocus();
- SetWindowText("创建新类别");
- }
- else
- {
- ((CEdit*)GetDlgItem(IDC_EDIT_CATEGORY_NAME))->SetReadOnly(true);
- ((CEdit*)GetDlgItem(IDC_EDIT_CATEGORY_DIR))->SetReadOnly(true);
- ((CEdit*)GetDlgItem(IDC_BUTTON_FOLDER_SEL))->SetReadOnly(true);
- GetDlgItem(IDC_BUTTON_FOLDER_SEL)->EnableWindow(false);
- GetDlgItem(IDC_TREE_CATEGORY)->SetFocus();
- SetWindowText("移动");
- }
- return FALSE;
- }
- void CCategoryNewDlg::OnChangeEditCategoryName()
- {
- assert(m_bNewOrMoveto);
- if (!m_bAutoFill) return;
- UpdateData();
- // get default directory string and format it.
- CTreeCursor ItemSel = m_treeCatetory.GetSelectedItem();
- assert(ItemSel);
- CCategoryBarWnd::CCategoryDBItem* pItem =
- (CCategoryBarWnd::CCategoryDBItem*)GetTreeData(ItemSel);
- assert(pItem);
- string strDefaultDirectory = pItem->m_strDefaultDirectory;
- if (strDefaultDirectory.size() <= 0)
- {
- assert(false);
- return;
- }
- if (strDefaultDirectory[strDefaultDirectory.size() - 1] != '\')
- strDefaultDirectory += '\';
- // auto fill m_strCategoryDir.
- m_strCategoryDir = strDefaultDirectory.data() + m_strCategoryName;
- UpdateData(false);
- }
- void CCategoryNewDlg::OnChangeEditCategoryDir()
- {
- assert(m_bNewOrMoveto);
- m_bAutoFill = false;
- }
- void CCategoryNewDlg::OnSelchangedTreeCategory(NMHDR* pNMHDR, LRESULT* pResult)
- {
- if (pResult) *pResult = 0;
- CTreeCursor ItemSel = m_treeCatetory.GetSelectedItem();
- assert(ItemSel);
- CCategoryBarWnd::CCategoryDBItem* pItem = (CCategoryBarWnd::CCategoryDBItem*)GetTreeData(ItemSel);
- assert(pItem);
- string strDefaultDirectory = pItem->m_strDefaultDirectory;
- assert(strDefaultDirectory.size() > 0);
- if (m_bNewOrMoveto)
- {
- if (!m_bAutoFill) return;
- if (strDefaultDirectory[strDefaultDirectory.size() - 1] != '\')
- strDefaultDirectory += '\';
- m_strCategoryDir = strDefaultDirectory.data() + m_strCategoryName;
- }
- else
- {
- m_strCategoryName = GetCorrespondingCursor(ItemSel).GetText();
- m_strCategoryDir = strDefaultDirectory.data();
- }
- UpdateData(false);
- }
- void CCategoryNewDlg::OnOK()
- {
- UpdateData();
- if (m_bNewOrMoveto)
- {
- if (m_strCategoryName.IsEmpty())
- return;
- // check directory to create.
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- _splitpath(m_strCategoryDir, drive, dir, fname, ext);
- if (!strlen(drive))
- return;
- if (!MakeDirecotry(m_strCategoryDir.GetBuffer(0)))
- return;
- }
- CTreeCursor curItem = m_treeCatetory.GetSelectedItem();
- m_ItemSel = (HTREEITEM)curItem.GetData();
- assert(m_ItemSel);
- CDialog::OnOK();
- }
- void CCategoryNewDlg::OnButtonFolderSel()
- {
- CString strDef, strFolder;
- if (!SelectFolder(strDef, strFolder))
- return;
- /*
- string strFolder = SelectFolder();
- if (strFolder.empty())
- return;
- //*/
- UpdateData();
- m_strCategoryDir = strFolder;
- UpdateData(false);
- m_bAutoFill = false;
- }
- CTreeCursor CCategoryNewDlg::GetCorrespondingCursor(CTreeCursor& curItem)
- {
- long lData = curItem.GetData();
- assert(lData);
- return CTreeCursor ((HTREEITEM)lData, m_pCategoryBarWnd);
- }
- long CCategoryNewDlg::GetTreeData(CTreeCursor& curItem)
- {
- long lData = curItem.GetData();
- assert(lData);
- CTreeCursor curTrue((HTREEITEM)lData, m_pCategoryBarWnd);
- lData = curTrue.GetData();
- assert(lData);
- return lData;
- }
- void CCategoryNewDlg::CopyTree(CTreeCursor& curSrc, CTreeCursor& curTarget)
- {
- // don't show downloading category.
- if (curSrc == m_pCategoryBarWnd->m_curDownloading)
- return;
- CTreeCursor curTemp = curTarget.AddTail(curSrc.GetText(), curSrc.GetImageID());
- curTemp.SetData((long)(HTREEITEM)curSrc);
- assert(m_ItemSel);
- if (curSrc == m_ItemSel)
- curTemp.Select();
- if (curSrc.HasChildren())
- {
- CTreeCursor curChild = curSrc.GetChild();
- while (curChild)
- {
- CopyTree(curChild, curTemp);
- curChild = curChild.GetNext(TVGN_NEXT);
- }
- }
- }