[dasher: 11/217] Refactoring of ModuleControls, merge long controls



commit 90373662f804ac9bf7861ee285056d4530213290
Author: ipomoena <amajorek google com>
Date:   Thu Jul 16 23:20:06 2015 -0700

    Refactoring of ModuleControls, merge long controls
    
    No container control unless needed
    Simplify SetFont
    Changes in long edit move slider
    Win32 -> ATL
    TODO- hide slider for LongSpin settings

 Src/Win32/Dasher_vc2013.vcxproj     |    2 -
 Src/Win32/ModuleControl.cpp         |   25 +++---------
 Src/Win32/ModuleControl.h           |   21 +---------
 Src/Win32/ModuleControlBool.cpp     |   18 ++------
 Src/Win32/ModuleControlBool.h       |    2 +-
 Src/Win32/ModuleControlLong.cpp     |   75 +++++++++++++++++++++-------------
 Src/Win32/ModuleControlLong.h       |   22 ++++++++--
 Src/Win32/ModuleControlLongSpin.cpp |   34 ----------------
 Src/Win32/ModuleControlLongSpin.h   |   20 ---------
 Src/Win32/ModuleControlString.cpp   |   24 ++++-------
 Src/Win32/ModuleControlString.h     |    2 +-
 Src/Win32/ModuleSettings.cpp        |    5 +-
 12 files changed, 90 insertions(+), 160 deletions(-)
---
diff --git a/Src/Win32/Dasher_vc2013.vcxproj b/Src/Win32/Dasher_vc2013.vcxproj
index d1ba926..f101a72 100644
--- a/Src/Win32/Dasher_vc2013.vcxproj
+++ b/Src/Win32/Dasher_vc2013.vcxproj
@@ -214,7 +214,6 @@
     <ClCompile Include="ModuleControl.cpp" />
     <ClCompile Include="ModuleControlBool.cpp" />
     <ClCompile Include="ModuleControlLong.cpp" />
-    <ClCompile Include="ModuleControlLongSpin.cpp" />
     <ClCompile Include="ModuleControlString.cpp" />
     <ClCompile Include="ModuleSettings.cpp" />
     <ClCompile Include="Widgets\AdvancedPage.cpp" />
@@ -243,7 +242,6 @@
     <ClInclude Include="ModuleControl.h" />
     <ClInclude Include="ModuleControlBool.h" />
     <ClInclude Include="ModuleControlLong.h" />
-    <ClInclude Include="ModuleControlLongSpin.h" />
     <ClInclude Include="ModuleControlString.h" />
     <ClInclude Include="ModuleSettings.h" />
     <ClInclude Include="resource.h" />
diff --git a/Src/Win32/ModuleControl.cpp b/Src/Win32/ModuleControl.cpp
index d02ed35..67be8ae 100644
--- a/Src/Win32/ModuleControl.cpp
+++ b/Src/Win32/ModuleControl.cpp
@@ -11,31 +11,18 @@ CModuleControl::CModuleControl(SModuleSettings *pSetting) {
 };
 
 void CModuleControl::Create(HWND hParent) {
-  m_hParent = hParent;
-
-  CWindowImpl<CModuleControl>::Create(m_hParent);
-  
-  m_hWndCaption = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("STATIC"), m_strCaption.c_str(), 
-    WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, m_hWnd, NULL, WinHelper::hInstApp, NULL);
-
-  HGDIOBJ hGuiFont;
-  hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
- 
-  SendMessage(m_hWndCaption, WM_SETFONT, (WPARAM)hGuiFont, (LPARAM)true);
-
-  CreateChild(m_hWnd);
+  m_hCaption.Create(TEXT("STATIC"), hParent, 0, m_strCaption.c_str(), WS_CHILD | WS_VISIBLE);
+  CreateChild(hParent);
 };
 
 void CModuleControl::Layout(RECT *pRect) {
-  MoveWindow(pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
-  
-  ::MoveWindow(m_hWndCaption, 0, 0, (pRect->right - pRect->left) / 2, pRect->bottom - pRect->top, TRUE);
+  m_hCaption.MoveWindow(pRect->left, pRect->top, (pRect->right - pRect->left) / 2, pRect->bottom - 
pRect->top);
 
   RECT sRect;
-  sRect.left = (pRect->right - pRect->left) / 2;
+  sRect.left = (pRect->right + pRect->left) / 2;
   sRect.right = pRect->right;
-  sRect.top = 0;
-  sRect.bottom = pRect->bottom - pRect->top;
+  sRect.top = pRect->top;
+  sRect.bottom = pRect->bottom;
 
   LayoutChild(sRect);
 };
\ No newline at end of file
diff --git a/Src/Win32/ModuleControl.h b/Src/Win32/ModuleControl.h
index 370dead..9571f35 100644
--- a/Src/Win32/ModuleControl.h
+++ b/Src/Win32/ModuleControl.h
@@ -8,26 +8,10 @@
 #include <atlwin.h>
 #include <string>
 
-class CModuleControl : public CWindowImpl<CModuleControl> {
+class CModuleControl {
 public:
   CModuleControl(SModuleSettings *pSetting);
 
-  DECLARE_WND_SUPERCLASS(NULL, L"STATIC")
-
-  BEGIN_MSG_MAP(CModuleControl)
-    MESSAGE_HANDLER(WM_NOTIFY, OnNotify)
-    MESSAGE_HANDLER(WM_HSCROLL, OnScroll)
-  END_MSG_MAP()
-
-  virtual LRESULT OnNotify(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
-    bHandled = false;
-    return 0;
-  };
-
-  virtual LRESULT OnScroll(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
-    bHandled = false;
-    return 0;
-  }; 
  
   void Create(HWND hParent);
   void Layout(RECT *pRect);
@@ -40,7 +24,6 @@ public:
   virtual void LayoutChild(RECT &sRect) = 0;
 
 protected:
-  HWND m_hParent;
   std::wstring m_strCaption;
 
   int m_iId;
@@ -49,7 +32,7 @@ protected:
   int m_iDivisor;
   int m_iStep;
 
-  HWND m_hWndCaption;
+  CWindow m_hCaption;
 };
 
 #endif
diff --git a/Src/Win32/ModuleControlBool.cpp b/Src/Win32/ModuleControlBool.cpp
index 6af684c..700e615 100644
--- a/Src/Win32/ModuleControlBool.cpp
+++ b/Src/Win32/ModuleControlBool.cpp
@@ -5,26 +5,18 @@ int CModuleControlBool::GetHeightRequest() {
 }
 
 void CModuleControlBool::Initialise(CAppSettings *pAppSets) {
-  if(pAppSets->GetBoolParameter(m_iId))
-    SendMessage(m_hCheckbox, BM_SETCHECK, BST_CHECKED, 0);
-  else
-    SendMessage(m_hCheckbox, BM_SETCHECK, BST_UNCHECKED, 0);
+  m_hCheckbox.SendMessage(BM_SETCHECK, pAppSets->GetBoolParameter(m_iId) ? BST_CHECKED : BST_UNCHECKED);
 }
 
 void CModuleControlBool::Apply(CAppSettings *pAppSets) {
-  pAppSets->SetBoolParameter(m_iId, SendMessage(m_hCheckbox, BM_GETCHECK, 0, 0) == BST_CHECKED);
+  pAppSets->SetBoolParameter(m_iId, m_hCheckbox.SendMessage(BM_GETCHECK) == BST_CHECKED);
 }
 
 void CModuleControlBool::CreateChild(HWND hParent) {
-  m_hCheckbox = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("BUTTON"), NULL, 
-    BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, hParent, NULL, WinHelper::hInstApp, 
NULL);
-
-  HGDIOBJ hGuiFont;
-  hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
- 
-  SendMessage(m_hCheckbox, WM_SETFONT, (WPARAM)hGuiFont, (LPARAM)true);
+  m_hCheckbox.Create(TEXT("BUTTON"), hParent, 0,0,
+    BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP);
 }
 
 void CModuleControlBool::LayoutChild(RECT &sRect) {
-  ::MoveWindow(m_hCheckbox, sRect.left, sRect.top, sRect.right - sRect.left, sRect.bottom - sRect.top, TRUE);
+  m_hCheckbox.MoveWindow(&sRect);
 }
diff --git a/Src/Win32/ModuleControlBool.h b/Src/Win32/ModuleControlBool.h
index 5a4e9d6..bd01704 100644
--- a/Src/Win32/ModuleControlBool.h
+++ b/Src/Win32/ModuleControlBool.h
@@ -14,7 +14,7 @@ public:
   virtual void LayoutChild(RECT &sRect);
 
 private:
-  HWND m_hCheckbox;
+  CWindow m_hCheckbox;
 };
 
 #endif
\ No newline at end of file
diff --git a/Src/Win32/ModuleControlLong.cpp b/Src/Win32/ModuleControlLong.cpp
index 4dce4d7..a7dccfd 100644
--- a/Src/Win32/ModuleControlLong.cpp
+++ b/Src/Win32/ModuleControlLong.cpp
@@ -1,15 +1,17 @@
 #include "ModuleControlLong.h"
 
-#include <cstring>
-
-LRESULT CModuleControlLong::OnScroll(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
-  int iValue = SendMessage(m_hSlider, TBM_GETPOS, 0, 0);
+LRESULT CModuleControlLong::OnSliderScroll(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
+  UpdateValue(GetSliderValue());
+  return 0;
+}
 
-  WCHAR tcBuffer[256];  
-  _sntprintf(tcBuffer, 100, TEXT("%0.2f"), iValue / static_cast<double>(m_iDivisor));
-  SendMessage(m_hEntry, WM_SETTEXT, 0, (LPARAM) tcBuffer);
+LRESULT CModuleControlLong::OnEditChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) {
+  UpdateValue(GetEditValue());
+  return 0;
+}
 
-  bHandled = true;
+LRESULT CModuleControlLong::OnEditLeft(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) {
+  UpdateValue(GetSliderValue());
   return 0;
 }
 
@@ -18,36 +20,51 @@ int CModuleControlLong::GetHeightRequest() {
 }
 
 void CModuleControlLong::Initialise(CAppSettings *pAppSets) {
-  int iValue(pAppSets->GetLongParameter(m_iId));
-  SendMessage(m_hSlider, TBM_SETPOS, (WPARAM)false, (LPARAM)iValue);
-
-  WCHAR tcBuffer[256];
-  _sntprintf(tcBuffer, 100, TEXT("%0.2f"), iValue / static_cast<double>(m_iDivisor));
-  SendMessage(m_hEntry, WM_SETTEXT, 0, (LPARAM) tcBuffer);
+  UpdateValue(pAppSets->GetLongParameter(m_iId));
 }
 
 void CModuleControlLong::Apply(CAppSettings *pAppSets) {
-  int iValue = SendMessage(m_hSlider, TBM_GETPOS, 0, 0);
-  pAppSets->SetLongParameter(m_iId, iValue);
+  pAppSets->SetLongParameter(m_iId, GetSliderValue());
 }
 
 void CModuleControlLong::CreateChild(HWND hParent) {
-  m_hSlider = CreateWindowEx(WS_EX_CONTROLPARENT, TRACKBAR_CLASS, NULL, 
-    TBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, hParent, NULL, WinHelper::hInstApp, NULL);
-
-  SendMessage(m_hSlider, TBM_SETRANGE, (WPARAM)true, (LPARAM)MAKELONG(m_iMin, m_iMax));
-  SendMessage(m_hSlider, TBM_SETPAGESIZE, 0, (LPARAM)m_iStep);
+  CWindowImpl<CModuleControlLong>::Create(hParent);
 
-  m_hEntry = CreateWindowEx(WS_EX_CONTROLPARENT | WS_EX_CLIENTEDGE, TEXT("EDIT"), NULL, 
-    WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, hParent, NULL, WinHelper::hInstApp, NULL);
+  m_hSlider.Create(TRACKBAR_CLASS, *this, 0, 0, TBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP);
+  m_hSlider.SendMessage(TBM_SETPAGESIZE, 0, m_iStep);
+  m_hSlider.SendMessage(TBM_SETRANGEMIN, true, m_iMin);
+  m_hSlider.SendMessage(TBM_SETRANGEMAX, true, m_iMax);
 
-  HGDIOBJ hGuiFont;
-  hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
- 
-  SendMessage(m_hEntry, WM_SETFONT, (WPARAM)hGuiFont, (LPARAM)true);
+  m_hEdit.Create(TEXT("EDIT"), *this, 0, 0, 
+    WS_CHILD | WS_VISIBLE | WS_TABSTOP, WS_EX_CLIENTEDGE, 1);
 }
 
 void CModuleControlLong::LayoutChild(RECT &sRect) {
-  ::MoveWindow(m_hSlider, sRect.left + 32, sRect.top, sRect.right - sRect.left - 32, sRect.bottom - 
sRect.top, TRUE);
-  ::MoveWindow(m_hEntry, sRect.left, sRect.top, 32, sRect.bottom - sRect.top, TRUE);
+  MoveWindow(&sRect);
+  m_hEdit.MoveWindow(0, 0, 32, sRect.bottom - sRect.top);
+  m_hSlider.MoveWindow(32, 0, sRect.right - sRect.left - 32, sRect.bottom - sRect.top);
+}
+
+void CModuleControlLong::UpdateValue(long lValue) {
+  if (GetSliderValue() != lValue)
+  {
+    m_hSlider.SendMessage(TBM_SETPOS, true, lValue);
+  }
+  if (GetEditValue() != lValue)
+  {
+    double dValue = lValue*1.0 / m_iDivisor;
+    CString sValue;
+    sValue.Format(TEXT("%g"), dValue);
+    m_hEdit.SetWindowText(sValue);
+  }
+}
+
+long CModuleControlLong::GetEditValue() {
+  CString sValue;
+  m_hEdit.GetWindowText(sValue);
+  return wcstod(sValue, 0)*m_iDivisor;
+}
+
+long CModuleControlLong::GetSliderValue() {
+  return m_hSlider.SendMessage(TBM_GETPOS);
 }
diff --git a/Src/Win32/ModuleControlLong.h b/Src/Win32/ModuleControlLong.h
index 09d3253..a60205d 100644
--- a/Src/Win32/ModuleControlLong.h
+++ b/Src/Win32/ModuleControlLong.h
@@ -3,12 +3,20 @@
 
 #include "ModuleControl.h"
 
-class CModuleControlLong : public CModuleControl {
+class CModuleControlLong : public CModuleControl, public CWindowImpl<CModuleControlLong>{
 public:
   CModuleControlLong(SModuleSettings *pSetting) : CModuleControl(pSetting) {};
+  DECLARE_WND_SUPERCLASS(NULL, L"STATIC")
+
+  BEGIN_MSG_MAP(CModuleControlLong)
+    COMMAND_HANDLER(1, EN_CHANGE, OnEditChange)
+    COMMAND_HANDLER(1, EN_KILLFOCUS, OnEditLeft)
+    MESSAGE_HANDLER(WM_HSCROLL, OnSliderScroll)
+  END_MSG_MAP()
+  LRESULT OnEditChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+  LRESULT OnEditLeft(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+  LRESULT OnSliderScroll(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
 
-  virtual LRESULT OnScroll(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
- 
   virtual int GetHeightRequest();
   virtual void Initialise(CAppSettings *pAppSets);
   virtual void Apply(CAppSettings *pAppSets);
@@ -16,8 +24,12 @@ public:
   virtual void LayoutChild(RECT &sRect);
 
 private:
-  HWND m_hSlider;
-  HWND m_hEntry;
+  void UpdateValue(long lValue);
+  long GetEditValue();
+  long GetSliderValue();
+  CWindow m_hEdit;
+  CWindow m_hSlider;
+
 };
 
 #endif
\ No newline at end of file
diff --git a/Src/Win32/ModuleControlString.cpp b/Src/Win32/ModuleControlString.cpp
index 807a781..5ee2335 100644
--- a/Src/Win32/ModuleControlString.cpp
+++ b/Src/Win32/ModuleControlString.cpp
@@ -5,31 +5,25 @@ int CModuleControlString::GetHeightRequest() {
 }
 
 void CModuleControlString::Initialise(CAppSettings *pAppSets) {
-  std::wstring strText;
-  WinUTF8::UTF8string_to_wstring(pAppSets->GetStringParameter(m_iId), strText);
-  SendMessage(m_hEntry, WM_SETTEXT, 0, (LPARAM)strText.c_str());
+  std::wstring wideText;
+  WinUTF8::UTF8string_to_wstring(pAppSets->GetStringParameter(m_iId), wideText);
+  m_hEntry.SetWindowText(wideText.c_str());
 }
 
 void CModuleControlString::Apply(CAppSettings *pAppSets) {
-  TCHAR tcBuffer[256];
-  SendMessage(m_hEntry, WM_GETTEXT, 100, (LPARAM)tcBuffer);
-  
+  CString wideText;
+  m_hEntry.GetWindowText(wideText);
   std::string strUTF8Text;
-  WinUTF8::wstring_to_UTF8string(tcBuffer, strUTF8Text);
+  WinUTF8::wstring_to_UTF8string(std::wstring(wideText), strUTF8Text);
 
   pAppSets->SetStringParameter(m_iId, strUTF8Text);
 }
 
 void CModuleControlString::CreateChild(HWND hParent) {
-  m_hEntry = CreateWindowEx(WS_EX_CONTROLPARENT | WS_EX_CLIENTEDGE, TEXT("EDIT"), NULL, 
-    WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, hParent, NULL, WinHelper::hInstApp, NULL);
-
-  HGDIOBJ hGuiFont;
-  hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
- 
-  SendMessage(m_hEntry, WM_SETFONT, (WPARAM)hGuiFont, (LPARAM)true);
+  m_hEntry.Create(TEXT("EDIT"), hParent, 0, 0,
+    WS_CHILD | WS_VISIBLE | WS_TABSTOP, WS_EX_CLIENTEDGE);
 }
 
 void CModuleControlString::LayoutChild(RECT &sRect) {
-  ::MoveWindow(m_hEntry, sRect.left, sRect.top, sRect.right - sRect.left, sRect.bottom - sRect.top, TRUE);
+  m_hEntry.MoveWindow(&sRect);
 }
diff --git a/Src/Win32/ModuleControlString.h b/Src/Win32/ModuleControlString.h
index 8a51197..a5915c0 100644
--- a/Src/Win32/ModuleControlString.h
+++ b/Src/Win32/ModuleControlString.h
@@ -14,7 +14,7 @@ public:
   virtual void LayoutChild(RECT &sRect);
 
 private:
-  HWND m_hEntry;
+  CWindow m_hEntry;
 };
 
 #endif
\ No newline at end of file
diff --git a/Src/Win32/ModuleSettings.cpp b/Src/Win32/ModuleSettings.cpp
index d3ded9a..b806864 100644
--- a/Src/Win32/ModuleSettings.cpp
+++ b/Src/Win32/ModuleSettings.cpp
@@ -1,6 +1,5 @@
 #include "ModuleControlBool.h"
 #include "ModuleControlLong.h"
-#include "ModuleControlLongSpin.h"
 #include "ModuleControlString.h"
 #include "ModuleSettings.h"
 
@@ -19,7 +18,7 @@ CModuleSettings::CModuleSettings(const std::string &strModuleName, SModuleSettin
       m_pControls[i] = new CModuleControlLong(pSettings + i);
       break;
     case T_LONGSPIN:
-      m_pControls[i] = new CModuleControlLongSpin(pSettings + i);
+      m_pControls[i] = new CModuleControlLong(pSettings + i);
       break;
     case T_STRING:
       m_pControls[i] = new CModuleControlString(pSettings + i);
@@ -52,6 +51,8 @@ LRESULT CModuleSettings::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, B
   RECT size = { 0, 0, m_iDlgWidth, iHeight };
   MapDialogRect(&size);
   ResizeClient(size.right, size.bottom);
+
+  SendMessageToDescendants(WM_SETFONT, (WPARAM)GetFont(), true);
   return 1;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]