-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClipTreeCtrl.cpp
184 lines (152 loc) · 3.87 KB
/
ClipTreeCtrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// ClipTreeCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "phreeqci2.h"
#include "ClipTreeCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_REDRAW_TREE (WM_USER + 99)
/////////////////////////////////////////////////////////////////////////////
// CClipTreeCtrl
CClipTreeCtrl::CClipTreeCtrl()
{
// accelerator table
m_hAccelTable = ::LoadAccelerators(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_CLIPLISTBOX));
}
CClipTreeCtrl::~CClipTreeCtrl()
{
}
BEGIN_MESSAGE_MAP(CClipTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CClipTreeCtrl)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_COPY, OnCopy)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnTvnItemExpanded)
ON_MESSAGE(WM_REDRAW_TREE, OnRedrawTree)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClipTreeCtrl message handlers
void CClipTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
UNUSED(pWnd);
ASSERT(pWnd == this);
CMenu menu;
VERIFY(menu.LoadMenu(IDR_CLIPLISTBOX));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup);
if (pPopup)
{
if (point.x == -1 && point.y == -1)
{
// keystroke invocation
point.x = point.y = 0;
ClientToScreen(&point);
}
// Note: All commands except copy are grayed by default
// disable copy item if there's no selection
if (GetSelectedItem() == NULL)
{
VERIFY(pPopup->EnableMenuItem((UINT)ID_COPY, MF_BYCOMMAND | MF_GRAYED) != -1);
}
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
}
void CClipTreeCtrl::OnRButtonDown(UINT nFlags, CPoint point)
{
UNUSED(nFlags);
HTREEITEM hItem = HitTest(point);
if (hItem != NULL)
{
SelectItem(hItem);
SetFocus();
}
else
{
// The next line will cause OnContextMenu to NOT be called
CTreeCtrl::OnRButtonDown(nFlags, point);
}
}
void CClipTreeCtrl::OnCopy()
{
HTREEITEM hItem = GetSelectedItem();
if (hItem == NULL) return;
if (::OpenClipboard(m_hWnd))
{
// Retrieve string from ListBox
CString strText = GetItemText(hItem);
// Allocate global memory
HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_DDESHARE, (strText.GetLength() + 1) * sizeof(TCHAR));
if (hglbCopy)
{
// Lock it and get a pointer to it
LPTSTR lptstrCopy = (LPTSTR)::GlobalLock(hglbCopy);
if (lptstrCopy)
{
// Empty existing clipboard contents
VERIFY(::EmptyClipboard());
// Copy string into memory
VERIFY(::lstrcpy(lptstrCopy, (LPCTSTR)strText));
// Unlock memory
VERIFY(::GlobalUnlock(hglbCopy));
// Send memory to clipboard
#ifdef UNICODE
VERIFY(::SetClipboardData(CF_UNICODETEXT, hglbCopy));
#else
VERIFY(::SetClipboardData(CF_TEXT, hglbCopy));
#endif
}
else
{
// Free memory if it couldn't be locked
::GlobalFree(hglbCopy);
}
}
// Close the clipboard.
VERIFY(::CloseClipboard());
}
}
void CClipTreeCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
UNUSED(pNMHDR);
OnCopy();
*pResult = 0;
}
BOOL CClipTreeCtrl::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST)
{
// translate accelerators
if (m_hAccelTable != NULL && ::TranslateAccelerator(m_hWnd, m_hAccelTable, pMsg))
{
return TRUE;
}
}
return CTreeCtrl::PreTranslateMessage(pMsg);
}
void CClipTreeCtrl::OnTvnItemExpanded(NMHDR* pNMHDR, LRESULT* pResult)
{
// see OnRedrawTree
PostMessage(WM_REDRAW_TREE, 0, 0);
UNUSED(pNMHDR);
*pResult = 0;
}
LRESULT CClipTreeCtrl::OnRedrawTree(WPARAM wParam, LPARAM lParam)
{
// This is a hack that forces the tree to be redrawn when
// a branch is collapsed/expanded
UNUSED(wParam);
UNUSED(lParam);
TRACE("CClipTreeCtrl::OnRedrawTree\n");
CRect rc;
GetClientRect(rc);
rc.InflateRect(5, 5, 5, 5);
ClientToScreen(rc);
GetParent()->ScreenToClient(rc);
GetParent()->InvalidateRect(rc);
return 0;
}