Scope
ScopeEditCtrl.h
1 #pragma once
2 
3 #include "helpers/ScopeValue.h"
4 #include "helpers/ScopeString.h"
5 #include "ScopeDefines.h"
6 
7 namespace scope {
8  namespace gui {
9 
12 template <class ValT>
14  : public CWindowImpl<CScopeEditCtrl<ValT>, CEdit> {
15 
16 protected:
18  bool created;
19 
22 
24  boost::signals2::connection stateconnection;
25 
27  boost::signals2::connection valueconnection;
28 
30  bool shiftstate;
31 
32 public:
33  BEGIN_MSG_MAP_EX(CScopeEditCtrl)
34  MESSAGE_HANDLER(WM_CHAR, OnChar)
35  MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
36  MESSAGE_HANDLER(WM_KEYUP, OnKeyUp)
37  MESSAGE_HANDLER(WM_KILLFOCUS, OnKillFocus)
38  MESSAGE_HANDLER(WM_UPDATECONTROL, OnUpdateControl)
39  DEFAULT_REFLECTION_HANDLER()
40  END_MSG_MAP()
41 
42  /*CScopeEditImpl() Default constructor is dangerous!
43  : m_shiftstate(false)
44  , m_ScopeValPtr(nullptr)
45  {}*/
46 
51  CScopeEditCtrl(ScopeValue<ValT>* _scopeval, const bool& _connectback = false, const bool& _connectcontrolstate = false)
52  : created(false)
53  , scope_val(_scopeval)
54  , shiftstate(false){
55  if ( _connectback )
56  valueconnection = scope_val->ConnectGUI(std::bind(&CScopeEditCtrl<ValT>::UpdateControl, this));
57  if ( _connectcontrolstate )
58  stateconnection = scope_val->ConnectState(std::bind(&CScopeEditCtrl<ValT>::SetState, this, std::placeholders::_1));
59  }
60 
63  valueconnection.disconnect(); // Does not throw if not connected
64  stateconnection.disconnect();
65  }
66 
68  bool AttachToDlgItem(HWND hWnd) {
69  if ( SubclassWindow(hWnd) ) {
70  created = true;
71  UpdateControl();
72  return true;
73  }
74  return false;
75  }
76 
81  LRESULT OnChar(UINT uMsg, WPARAM wParam,LPARAM lParam, BOOL& bHandled) {
82  //ignore without a beep
83  switch (wParam) {
84  case '\t': // tab
85  case '\r': //Carriage return
86  return 0;
87  break;
88  }
89  return DefWindowProc(uMsg, wParam, lParam);
90  }
91 
94  LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
95  switch (wParam) {
96  case VK_RETURN:
97  case VK_TAB:
98  UpdateValue();
99  ::PostMessage(GetParent(), WM_NEXTDLGCTL, shiftstate, 0L);
100  return FALSE;
101  case VK_SHIFT:
102  shiftstate = true;
103  }
104  return DefWindowProc(uMsg, wParam, lParam);
105  }
106 
108  LRESULT OnKeyUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
109  switch (wParam) {
110  case VK_SHIFT:
111  shiftstate = false;
112  }
113  return DefWindowProc(uMsg, wParam, lParam);
114  }
115 
117  LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
118  UpdateValue();
119  return DefWindowProc(uMsg, wParam, lParam);
120  }
121 
123  LRESULT OnUpdateControl(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
124  if ( scope_val != nullptr )
125  SetWindowText(scope_val->ToChar().c_str());
126  return 0;
127  }
132  void UpdateControl() {
133  ::PostMessage(m_hWnd, WM_UPDATECONTROL, NULL, NULL);
134  }
135 
137  void UpdateValue() {
138  if ( scope_val != nullptr ) {
139  const int32_t length = GetWindowTextLength()+1; // +1 since terminating \0 is not counter
140  std::vector<WCHAR> buff(length, L' ');
141  GetWindowText(buff.data(), length); // empty control gives only a \0
142  scope_val->SetFromString(buff.data());
143  }
144  }
145 
147  void SetState(const bool& state) {
148  if ( created )
149  EnableWindow(state);
150  }
151 };
152 
153 
154 }
155 
156 }
LRESULT OnUpdateControl(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Worker for UpdateControl (explicit specialization for ScopeValue below). ...
boost::signals2::connection ConnectGUI(signalchange_t::slot_type slot)
Connect signal to slot to GUI.
void SetState(const bool &state)
Sets the enabled/disabled state of the control.
bool created
true after window creation
Definition: ScopeEditCtrl.h:18
bool AttachToDlgItem(HWND hWnd)
Attach the control to a dialog item.
Definition: ScopeEditCtrl.h:68
LRESULT OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
For ignoring tab and return without beep.
Definition: ScopeEditCtrl.h:81
void UpdateControl()
Updates the string inside the control from the ScopeValues' value.
An adapted CEdit control around a ScopeValue.
Definition: ScopeEditCtrl.h:13
LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Update scope_val when leaving focus.
bool shiftstate
stores state of shift key
Definition: ScopeEditCtrl.h:30
ScopeValue< ValT > *const scope_val
pointer to underlying ScopeValue
Definition: ScopeEditCtrl.h:21
LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Switches to next control on return or tab and updates ScopeValue scope_val Also sets state of shift k...
Definition: ScopeEditCtrl.h:94
std::wstring ToChar() const
Converts value into a wstring.
boost::signals2::connection ConnectState(signalstate_t::slot_type slot)
Connect signal to slot for state changes.
~CScopeEditCtrl()
Disconnects everything.
Definition: ScopeEditCtrl.h:62
LRESULT OnKeyUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Resets state of shift key.
void UpdateValue()
Updates the ScopeValue from the string inside the control.
void SetFromString(const std::wstring &str)
Parses a wstring and sets the value accordingly.
boost::signals2::connection valueconnection
The connection object for the control state (connection to the scope_val value change) ...
Definition: ScopeEditCtrl.h:27
boost::signals2::connection stateconnection
The connection object for the control state (connection to the scope_val rw state change) ...
Definition: ScopeEditCtrl.h:24