WTL Controls
Back to the WTL Controls Home Page.
EditPlus.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 //--------------------------------------------------------------------------------------
3 //--------------------------------------------------------------------------------------
4 //
5 //
6 // WTL Control Library
7 // by
8 // Bright Ideas Software®
9 // Copyright © 2018 by Bright Ideas Software.
10 //
11 //
12 //
13 //--------------------------------------------------------------------------------------
14 //--------------------------------------------------------------------------------------
15 
16 
17 
18 namespace BIS_WTL_CONTROLS
19 {
20 
22  #define SHIFTED 0x8000
23  //----------------------------------------------------------------------------------------------
25  //----------------------------------------------------------------------------------------------
27  {
28  m_TextColor = ::GetSysColor (COLOR_WINDOWTEXT);
29  m_BackColor = ::GetSysColor (COLOR_WINDOW);
30  m_FocusedBackColor = m_BackColor;
31  m_FocusedTextColor = CLR_RED;
32 
33  m_BackBrush = NULL;
34  m_FocusedBackBrush = NULL;
35  m_bFloatingPointMode = false;
36  m_bNotifyOnCR = true;
37 
38  m_bMsgHandled = FALSE;
39  }
40  //----------------------------------------------------------------------------------------------
42  {
43  ATLASSERT(m_hWnd == NULL);
44  DeleteBrushes();
45  }
46  //----------------------------------------------------------------------------------------------
47  //----------------------------------------------------------------------------------------------
57  void CEditPlus::SubclassEdit(HWND hWnd)
58  {
59 
60  CWindowImpl<CEditPlus, CEdit>::SubclassWindow(hWnd);
61  UpdateBackBrushes();
62 
63  }
64  //----------------------------------------------------------------------------------------------
71  {
72  UnsubclassWindow();
73  DeleteBrushes();
74  }
75  //----------------------------------------------------------------------------------------------
76  //----------------------------------------------------------------------------------------------
86  COLORREF CEditPlus::GetBackColor() const
87  {
88  return(m_BackColor);
89  }
90  //----------------------------------------------------------------------------------------------
100  void CEditPlus::SetBackColor(COLORREF value)
101  {
102 
103  if (m_BackColor != value)
104  {
105  m_BackColor = value;
106  UpdateBackBrushes();
107  }
108  }
109  //----------------------------------------------------------------------------------------------
110  //----------------------------------------------------------------------------------------------
119  COLORREF CEditPlus::GetTextColor() const
120  {
121  return(m_TextColor);
122  }
123  //----------------------------------------------------------------------------------------------
134  void CEditPlus::SetTextColor(COLORREF value)
135  {
136  m_TextColor = value;
137  }
138  //----------------------------------------------------------------------------------------------
139  //----------------------------------------------------------------------------------------------
149  {
150  return(m_FocusedBackColor);
151  }
152  //----------------------------------------------------------------------------------------------
162  void CEditPlus::SetFocusedBackColor(COLORREF value)
163  {
164 
165  if (m_FocusedBackColor != value)
166  {
167  m_FocusedBackColor = value;
168  UpdateBackBrushes();
169  }
170  }
171  //----------------------------------------------------------------------------------------------
172  //----------------------------------------------------------------------------------------------
182  {
183  return(m_FocusedTextColor);
184  }
185  //----------------------------------------------------------------------------------------------
196  void CEditPlus::SetFocusedTextColor(COLORREF value)
197  {
198  m_FocusedTextColor = value;
199  }
200  //----------------------------------------------------------------------------------------------
201  //----------------------------------------------------------------------------------------------
210  {
211  return(m_bFloatingPointMode);
212  }
213  //----------------------------------------------------------------------------------------------
225  {
226  m_bFloatingPointMode = value;
227  }
228  //----------------------------------------------------------------------------------------------
229  //----------------------------------------------------------------------------------------------
241  {
242  return(m_bNotifyOnCR);
243  }
244  //----------------------------------------------------------------------------------------------
254  {
255  m_bNotifyOnCR = value;
256  }
257  //----------------------------------------------------------------------------------------------
258  //----------------------------------------------------------------------------------------------
259  //
260  // Private Methods
261  //
262  //----------------------------------------------------------------------------------------------
263  //----------------------------------------------------------------------------------------------
264  void CEditPlus::UpdateBackBrushes()
265  {
266 
267  DeleteBrushes();
268 
269  m_BackBrush = ::CreateSolidBrush (m_BackColor);
270 
271  m_FocusedBackBrush = ::CreateSolidBrush(m_FocusedBackColor);
272 
273  }
274  //----------------------------------------------------------------------------------------------
275  void CEditPlus::DeleteBrushes()
276  {
277 
278  if (m_BackBrush != NULL)
279  {
280  ::DeleteObject (m_BackBrush);
281  m_BackBrush = NULL;
282  }
283 
284  if (m_FocusedBackBrush != NULL)
285  {
286  ::DeleteObject(m_FocusedBackBrush);
287  m_FocusedBackBrush = NULL;
288  }
289  }
290  //----------------------------------------------------------------------------------------------
291  //----------------------------------------------------------------------------------------------
292  //
293  // Message Handlers
294  //
295  //----------------------------------------------------------------------------------------------
296  //----------------------------------------------------------------------------------------------
297  BOOL CEditPlus::OnEraseBkgnd(CDCHandle dc)
298  {
299  return(TRUE);
300  }
301  //----------------------------------------------------------------------------------------------
302  LRESULT CEditPlus::OnCtlColor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
303  {
304 
305  HWND hWnd = (HWND)lParam;
306 
307  if (m_hWnd == hWnd)
308  {
309 
310  bHandled = TRUE;
311 
312  HDC dc = (HDC)wParam;
313  ::SetBkMode(dc, OPAQUE);
314 
315  if (GetFocus() != m_hWnd)
316  {
317 
318  ::SetTextColor(dc, m_TextColor);
319  ::SetBkColor(dc, m_BackColor);
320 
321  return((long)m_BackBrush);
322  }
323  else
324  {
325  ::SetTextColor(dc, m_FocusedTextColor);
326  ::SetBkColor(dc, m_FocusedBackColor);
327 
328  return((long)m_FocusedBackBrush);
329  }
330  }
331  else
332  {
333  bHandled = FALSE;
334  return(0);
335  }
336  }
337  //----------------------------------------------------------------------------------------------
338  void CEditPlus::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
339  {
340 
341  if (nChar == VK_RETURN)
342  {
343  if (m_bNotifyOnCR == true)
344  {
345  ::SendMessage (GetParent().m_hWnd, WM_EDITP_EDIT_END,
346  ((WPARAM) m_hWnd), ((LPARAM)0));
347  }
348  else
349  SetMsgHandled(FALSE);
350 
351  return;
352  }
353 
354 
355  if (m_bFloatingPointMode == true)
356  {
357  if (nChar == 8) // backspace
358  {
359  SetMsgHandled(FALSE);
360  return;
361  }
362 
363  POINT caret;
364  ::GetCaretPos (&caret);
365  caret.x = LOWORD (CharFromPos (caret));
366 
367  CString text;
368  int chars = ::GetWindowTextLength(m_hWnd);
369  LPTSTR lp = text.GetBufferSetLength(chars + 1);
370  GetWindowText( lp, chars + 1);
371  text.SetAt(chars, _T('\0'));
372  text.ReleaseBuffer();
373 
374 
375  if (isdigit(nChar))
376  {
377  SetMsgHandled(FALSE);
378  return;
379  }
380  else if (nChar == ((TCHAR)'-'))
381  {
382  if (caret.x == 0)
383  {
384  if (((text.GetLength() > 0) && (text[0]!= ((TCHAR)'-'))) || (text.GetLength()==0))
385  SetMsgHandled(FALSE);
386  }
387  else
388  {
389  if (text.GetLength() >= 2)
390  {
391 
392  TCHAR previous_char = (TCHAR)text[caret.x - 1];
393 
394  if (previous_char ==((TCHAR) 'E'))
395  SetMsgHandled(FALSE);
396  }
397  }
398  }
399  else if (nChar == ((TCHAR)'E'))
400  {
401  if ((caret.x == 1) && (text[0] == ((TCHAR)'-')))
402  return ;
403 
404  else
405  {
406  for (int i=0; i<text.GetLength(); i++)
407  {
408  if (text[i] == ((TCHAR)'E'))
409  return ;
410  }
411 
412  SetMsgHandled(FALSE);
413  }
414  }
415  else if (nChar == ((TCHAR)'.'))
416  {
417  for (int i=0; i<text.GetLength(); i++)
418  {
419  if (text[i] == ((TCHAR)'.'))
420  return ;
421  }
422 
423  for (int i=0; i<text.GetLength(); i++)
424  {
425  if ((text[i]==((TCHAR)'E')) && (caret.x > i))
426  return ;
427  }
428 
429  SetMsgHandled(FALSE);
430  }
431 
432  }
433  else
434  SetMsgHandled(FALSE);
435 
436  }
437  //----------------------------------------------------------------------------------------------
438 
439 
440 };
void EnableFloatingPointMode(bool enabled)
Change the Floating Point Mode state.
Definition: EditPlus.cpp:224
COLORREF GetFocusedBackColor() const
Default: COLOR_WINDOW.
Definition: EditPlus.cpp:148
const int WM_EDITP_EDIT_END
Definition: WTL_Controls.h:102
void UnSubclassEdit()
Un-subclass the window and release GDI resources.
Definition: EditPlus.cpp:70
void SetFocusedTextColor(COLORREF value)
Select a new text color used when the control has focus.
Definition: EditPlus.cpp:196
void SetTextColor(COLORREF value)
Select a new text color used when the control does not have focus.
Definition: EditPlus.cpp:134
bool IsNotifyOnCREnabled() const
Default: true.
Definition: EditPlus.cpp:240
void SetFocusedBackColor(COLORREF value)
Select a new background color used when the control has focus.
Definition: EditPlus.cpp:162
void EnableNotifyOnCR(bool value)
Change the &#39;Notify on CR&#39; state.
Definition: EditPlus.cpp:253
#define CLR_RED
Definition: WTL_Controls.h:136
COLORREF GetBackColor() const
Default: COLOR_WINDOW.
Definition: EditPlus.cpp:86
COLORREF GetTextColor() const
Default: COLOR_WINDOWTEXT.
Definition: EditPlus.cpp:119
void SetBackColor(COLORREF value)
Select a new background color used when the control does not have focus.
Definition: EditPlus.cpp:100
void SubclassEdit(HWND hWnd)
Subclass an edit control and create GDI objects.
Definition: EditPlus.cpp:57
bool IsFloatingPointModeEnabled() const
Default: false.
Definition: EditPlus.cpp:209
COLORREF GetFocusedTextColor() const
Default: CLR_RED.
Definition: EditPlus.cpp:181