⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exporttreedlg.hpp

📁 j2me is based on j2mepolish, client & server for mobile application.
💻 HPP
字号:

//         Copyright E骾n O'Callaghan 2008 - 2008.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include "StdAfx.hpp"
#include "Resource.h"

#include "SvcTreeView.hpp"

class ExportTreeView :
	public ATL::CWindowImpl<ExportTreeView, WTL::CTreeViewCtrlEx>,
	public WTLx::update_lockable<ExportTreeView>,
	private boost::noncopyable
{
protected:
	typedef ExportTreeView thisClass;
	typedef ATL::CWindowImpl<thisClass, WTL::CTreeViewCtrlEx> treeClass;

	friend class treeClass;

	BEGIN_MSG_MAP_EX(thisClass)
		MSG_WM_CHAR(OnChar)
		REFLECTED_NOTIFY_CODE_HANDLER(NM_CLICK, OnLButtonDown)

		DEFAULT_REFLECTION_HANDLER()
	END_MSG_MAP()

public:
	bool SubclassWindow(HWND hwnd)
	{
		if(!treeClass::SubclassWindow(hwnd))
			return false;
			
		ModifyStyle(0, TVS_CHECKBOXES|TVS_HASLINES);
		
		return true;
	}	

	void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
	{
		SetMsgHandled(false);	

		if (32 == nChar)
		{
			item_checked(GetSelectedItem(), GetCheckState(GetSelectedItem()));
		}
	}
	
	LRESULT OnLButtonDown(int, LPNMHDR pnmh, BOOL&)
	{
		SetMsgHandled(false);		
		
		WTL::CPoint point;
		GetCursorPos(&point);
		ScreenToClient(&point);

		UINT flags;
		WTL::CTreeItem ti = HitTest(point, &flags);

		if (flags & TVHT_ONITEMSTATEICON)
			item_checked(ti, !GetCheckState(ti)); // this is just before it's toggled.	

		return 0;
	}

	void clone_items(const SvcTreeViewCtrl& source_tree)
	{
		clone_self_and_siblings(source_tree.GetRootItem(), TVI_ROOT, TVI_FIRST);
	}

private:
	void clone_self_and_siblings(WTL::CTreeItem item, HTREEITEM parent, HTREEITEM pre_sibling)
	{
		while (!item.IsNull())
		{
			aux::win_c_str<std::wstring> str(MAX_PATH);
			item.GetText(str, str.size());

			WTL::CTreeItem cur_item = InsertItem(str, parent, pre_sibling);
			cur_item.SetData(item.GetData());
			cur_item.GetParent().Expand();

			clone_self_and_siblings(item.GetChild(), cur_item, TVI_FIRST);

			pre_sibling = cur_item;
			item = item.GetNextSibling();
		}
	}

	void item_checked(WTL::CTreeItem ti, BOOL state)
	{
		if (state)
		{
			ti = ti.GetParent();
			while (!ti.IsNull())
			{
				SetCheckState(ti, true);

				ti = ti.GetParent();
			}
		}
		else
			uncheck_self_and_children(ti.GetChild());
	}

	void uncheck_self_and_children(WTL::CTreeItem item)
	{
		while (!item.IsNull())
		{
			SetCheckState(item, false);

			uncheck_self_and_children(item.GetChild());

			item = item.GetNextSibling();
		}
	}

	std::map<size_t, WTL::CTreeItem> tree_items_;
};


class ExportTreeDlg :
	public ATL::CDialogImpl<ExportTreeDlg>,
	public WTL::CDialogResize<ExportTreeDlg>
{
protected:
	typedef ExportTreeDlg thisClass;
	typedef ATL::CDialogImpl<thisClass> baseClass;
	typedef WTL::CDialogResize<thisClass> resizeClass;

public:
	enum { IDD = IDD_TREEEXPORT };

	ExportTreeDlg(const SvcTreeViewCtrl& source_tree) :
		source_tree_(source_tree)
	{}

    BEGIN_MSG_MAP_EX(thisClass)
        MSG_WM_INITDIALOG(OnInitDialog)
		MSG_WM_CLOSE(OnClose)
		
        COMMAND_ID_HANDLER_EX(IDOK, OnOk)
        COMMAND_ID_HANDLER_EX(IDCANCEL, OnCancel)

		REFLECT_NOTIFICATIONS()
		CHAIN_MSG_MAP(resizeClass)
    END_MSG_MAP()

    BEGIN_DLGRESIZE_MAP(thisClass)
		DLGRESIZE_CONTROL(IDC_EXPORTTREE, DLSZ_SIZE_X|DLSZ_SIZE_Y)
		DLGRESIZE_CONTROL(IDOK, DLSZ_MOVE_Y|DLSZ_MOVE_X)
		DLGRESIZE_CONTROL(IDCANCEL, DLSZ_MOVE_Y|DLSZ_MOVE_X)
	END_DLGRESIZE_MAP()
	
	LRESULT OnInitDialog(HWND, LPARAM)
	{
		resizeClass::DlgResize_Init(false, true, WS_CLIPCHILDREN);

		tree_.SubclassWindow(GetDlgItem(IDC_EXPORTTREE));
		tree_.clone_items(source_tree_);

		set_.clear();

		CenterWindow();
		
		return 0;
	}

    void OnOk(UINT uCode, int nID, HWND hwndCtrl)
    {
		self_and_siblings(tree_.GetRootItem(), set_);
		EndDialog(1);
    }
	    
	void OnCancel(UINT uCode, int nID, HWND hwndCtrl)
    {
		EndDialog(0);
    }

	std::set<size_t> get_data_set()
	{
		return set_;
	}

	void self_and_siblings(WTL::CTreeItem item, std::set<size_t>& set)
	{
		while (!item.IsNull())
		{
			if (tree_.GetCheckState(item)) 
				set.insert(item.GetData());

			self_and_siblings(item.GetChild(), set);

			item = item.GetNextSibling();
		}
	}
	
	void OnClose()
	{
		EndDialog(0);
	}
	
private:
	ExportTreeView tree_;
	const SvcTreeViewCtrl& source_tree_;
	std::set<size_t> set_;

};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -