[r5083]: tags / V3_12 / ControlExtns.cpp Maximize Restore History

Download this file

ControlExtns.cpp    321 lines (262 with data), 7.9 kB

  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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (c) 2003-2008 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
// ControlExtns.cpp
//
#include "stdafx.h"
#include "ControlExtns.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Right-click Edit Context Menu
#define MENUSTRING_UNDO _T("&Undo")
// Separator
#define MENUSTRING_CUT _T("Cu&t")
#define MENUSTRING_COPY _T("&Copy")
#define MENUSTRING_PASTE _T("&Paste")
#define MENUSTRING_DELETE _T("&Delete")
// Separator
#define MENUSTRING_SELECTALL _T("Select &All")
// Separator
// Custom menu goes here!
#if defined(UNICODE)
#define EDIT_CLIPBOARD_TEXT_FORMAT CF_UNICODETEXT
#else
#define EDIT_CLIPBOARD_TEXT_FORMAT CF_TEXT
#endif
const COLORREF crefInFocus = (RGB(222, 255, 222)); // Light green
const COLORREF crefNoFocus = (RGB(255, 255, 255)); // White
const COLORREF crefBlack = (RGB( 0, 0, 0)); // Black
/////////////////////////////////////////////////////////////////////////////
// CStaticExtn
CStaticExtn::CStaticExtn()
: m_bUserColour(FALSE)
{
}
CStaticExtn::~CStaticExtn()
{
}
BEGIN_MESSAGE_MAP(CStaticExtn, CStatic)
//{{AFX_MSG_MAP(CStaticExtn)
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
HBRUSH CStaticExtn::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
if (!this->IsWindowEnabled() || !m_bUserColour)
return NULL;
pDC->SetTextColor(m_cfUser);
pDC->SetBkColor(GetSysColor(COLOR_BTNFACE));
return GetSysColorBrush(COLOR_BTNFACE);
}
/////////////////////////////////////////////////////////////////////////////
// CEditExtn
CEditExtn::CEditExtn()
: m_bIsFocused(FALSE), m_lastposition(-1),
m_message_number(-1), m_menustring("")
{
m_brInFocus.CreateSolidBrush(crefInFocus);
m_brNoFocus.CreateSolidBrush(crefNoFocus);
}
CEditExtn::CEditExtn(int message_number, LPCTSTR menustring)
: m_bIsFocused(FALSE), m_lastposition(-1),
m_message_number(message_number), m_menustring(menustring)
{
m_brInFocus.CreateSolidBrush(crefInFocus);
m_brNoFocus.CreateSolidBrush(crefNoFocus);
// Don't allow if menu string is empty.
if (m_menustring.IsEmpty())
m_message_number = -1;
}
CEditExtn::~CEditExtn()
{
}
BEGIN_MESSAGE_MAP(CEditExtn, CEdit)
//{{AFX_MSG_MAP(CEditExtn)
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_CONTEXTMENU()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEditExtn message handlers
void CEditExtn::OnSetFocus(CWnd* pOldWnd)
{
m_bIsFocused = TRUE;
CEdit::OnSetFocus(pOldWnd);
if (m_lastposition >= 0) {
int iLine = LineFromChar(m_lastposition);
LineScroll(iLine);
SetSel(m_nStartChar, m_nEndChar);
}
Invalidate(TRUE);
}
void CEditExtn::OnKillFocus(CWnd* pNewWnd)
{
m_bIsFocused = FALSE;
m_lastposition = LineIndex();
GetSel(m_nStartChar, m_nEndChar);
CEdit::OnKillFocus(pNewWnd);
Invalidate(TRUE);
}
HBRUSH CEditExtn::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
if (!this->IsWindowEnabled())
return NULL;
pDC->SetTextColor(crefBlack);
if (m_bIsFocused == TRUE) {
pDC->SetBkColor(crefInFocus);
return m_brInFocus;
} else {
pDC->SetBkColor(crefNoFocus);
return m_brNoFocus;
}
}
void CEditExtn::OnContextMenu(CWnd* pWnd, CPoint point)
{
if (m_message_number < 0) {
CEdit::OnContextMenu(pWnd, point);
return;
}
SetFocus();
CMenu menu;
menu.CreatePopupMenu();
BOOL bReadOnly = GetStyle() & ES_READONLY;
DWORD flags = CanUndo() && !bReadOnly ? 0 : MF_GRAYED;
menu.InsertMenu(0, MF_BYPOSITION | flags, EM_UNDO, MENUSTRING_UNDO);
menu.InsertMenu(1, MF_BYPOSITION | MF_SEPARATOR);
DWORD sel = GetSel();
flags = LOWORD(sel) == HIWORD(sel) ? MF_GRAYED : 0;
// Add it in position 2 but adding the next will make it 3
menu.InsertMenu(2, MF_BYPOSITION | flags, WM_COPY, MENUSTRING_COPY);
flags = (flags == MF_GRAYED || bReadOnly) ? MF_GRAYED : 0;
menu.InsertMenu(2, MF_BYPOSITION | flags, WM_CUT, MENUSTRING_CUT);
flags = (flags == MF_GRAYED || bReadOnly) ? MF_GRAYED : 0;
// Add it in position 4 but adding the next will make it 5
menu.InsertMenu(4, MF_BYPOSITION | flags, WM_CLEAR, MENUSTRING_DELETE);
flags = IsClipboardFormatAvailable(EDIT_CLIPBOARD_TEXT_FORMAT) &&
!bReadOnly ? 0 : MF_GRAYED;
menu.InsertMenu(4, MF_BYPOSITION | flags, WM_PASTE, MENUSTRING_PASTE);
menu.InsertMenu(6, MF_BYPOSITION | MF_SEPARATOR);
int len = GetWindowTextLength();
flags = (!len || (LOWORD(sel) == 0 && HIWORD(sel) == len)) ? MF_GRAYED : 0;
menu.InsertMenu(7, MF_BYPOSITION | flags, EM_SELECTALL, MENUSTRING_SELECTALL);
menu.InsertMenu(8, MF_BYPOSITION | MF_SEPARATOR);
menu.InsertMenu(9, MF_BYPOSITION , m_message_number, m_menustring);
if (point.x == -1 || point.y == -1) {
CRect rc;
GetClientRect(&rc);
point = rc.CenterPoint();
ClientToScreen(&point);
}
int nCmd = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON |
TPM_RETURNCMD | TPM_RIGHTBUTTON,
point.x, point.y, this);
if (nCmd < 0)
return;
if (nCmd == m_message_number) {
this->GetParent()->SendMessage(nCmd);
return;
}
switch (nCmd) {
case EM_UNDO:
case WM_CUT:
case WM_COPY:
case WM_CLEAR:
case WM_PASTE:
SendMessage(nCmd);
break;
case EM_SELECTALL:
SendMessage(EM_SETSEL, 0, -1);
break;
default:
break;
}
}
/////////////////////////////////////////////////////////////////////////////
// CListBoxExtn
CListBoxExtn::CListBoxExtn() : m_bIsFocused(FALSE)
{
m_brInFocus.CreateSolidBrush(crefInFocus);
m_brNoFocus.CreateSolidBrush(crefNoFocus);
}
CListBoxExtn::~CListBoxExtn()
{
}
BEGIN_MESSAGE_MAP(CListBoxExtn, CListBox)
//{{AFX_MSG_MAP(CListBoxExtn)
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListBoxExtn message handlers
void CListBoxExtn::OnSetFocus(CWnd* pOldWnd)
{
m_bIsFocused = TRUE;
CListBox::OnSetFocus(pOldWnd);
Invalidate(TRUE);
}
void CListBoxExtn::OnKillFocus(CWnd* pNewWnd)
{
m_bIsFocused = FALSE;
CListBox::OnKillFocus(pNewWnd);
Invalidate(TRUE);
}
HBRUSH CListBoxExtn::CtlColor(CDC* pDC, UINT /* nCtlColor */)
{
if (!this->IsWindowEnabled())
return NULL;
if (m_bIsFocused == TRUE) {
pDC->SetBkColor(crefInFocus);
return m_brInFocus;
} else {
pDC->SetBkColor(crefNoFocus);
return m_brNoFocus;
}
}
/////////////////////////////////////////////////////////////////////////////
// CComboBoxExtn
BEGIN_MESSAGE_MAP(CComboBoxExtn, CComboBox)
//{{AFX_MSG_MAP(CComboBoxExtn)
ON_WM_CTLCOLOR()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComboBoxExt message handlers
HBRUSH CComboBoxExtn::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (nCtlColor == CTLCOLOR_EDIT) {
// Extended Edit control
if (m_edit.GetSafeHwnd() == NULL)
m_edit.SubclassWindow(pWnd->GetSafeHwnd());
}
else if (nCtlColor == CTLCOLOR_LISTBOX) {
// Extended ListBox control
if (m_listbox.GetSafeHwnd() == NULL)
m_listbox.SubclassWindow(pWnd->GetSafeHwnd());
}
return CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
}
void CComboBoxExtn::OnDestroy()
{
if (m_edit.GetSafeHwnd() != NULL)
m_edit.UnsubclassWindow();
if (m_listbox.GetSafeHwnd() != NULL)
m_listbox.UnsubclassWindow();
CComboBox::OnDestroy();
}
void CComboBoxExtn::ChangeColour()
{
m_edit.ChangeColour();
m_listbox.ChangeColour();
}