[dasher: 9/217] Refactored CModuleSettings into modal dialog & ATL API



commit b76753ff5b08ff9b1aa1d81751e35e5c82df3289
Author: ipomoena <amajorek google com>
Date:   Sun Jul 12 10:32:37 2015 -0700

    Refactored CModuleSettings into modal dialog & ATL API
    
    - CModuleSettings is now based on CDialogImpl and resource.
    - Simplify CControlPage event handling by calling CModuleSettings as
    modal dialog.
    - use MapDialogRect for convenience
    - use ATL for convenience

 Src/Win32/Common/WinCommon.h      |    1 +
 Src/Win32/Dasher.rc               |   27 +++++-
 Src/Win32/ModuleSettings.cpp      |  189 +++++++++----------------------------
 Src/Win32/ModuleSettings.h        |   29 ++----
 Src/Win32/Widgets/ControlPage.cpp |   30 +-----
 Src/Win32/Widgets/ControlPage.h   |    4 -
 Src/Win32/resource.h              |    4 +-
 7 files changed, 82 insertions(+), 202 deletions(-)
---
diff --git a/Src/Win32/Common/WinCommon.h b/Src/Win32/Common/WinCommon.h
index 24e5266..059efdf 100644
--- a/Src/Win32/Common/WinCommon.h
+++ b/Src/Win32/Common/WinCommon.h
@@ -19,6 +19,7 @@
 #define _ATL_ENABLE_PTM_WARNING
 
 #include <atlbase.h>
+#include <atlstr.h>
 #include <atlwin.h>
 
 
diff --git a/Src/Win32/Dasher.rc b/Src/Win32/Dasher.rc
index 09d2f56..1a6db3d 100644
--- a/Src/Win32/Dasher.rc
+++ b/Src/Win32/Dasher.rc
@@ -135,7 +135,7 @@ END
 //
 
 IDD_ABOUTBOX DIALOGEX 0, 0, 242, 151
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
+STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "About"
 FONT 8, "MS Shell Dlg", 0, 0, 0x0
 BEGIN
@@ -150,7 +150,7 @@ BEGIN
 END
 
 IDD_ALPHABETPAGE DIALOGEX 0, 0, 381, 238
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
+STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "Select Alphabet"
 FONT 8, "MS Shell Dlg", 0, 0, 0x0
 BEGIN
@@ -178,7 +178,7 @@ BEGIN
 END
 
 IDD_CONTROLPAGE DIALOGEX 0, 0, 381, 238
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
+STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "Dasher Preferences"
 FONT 8, "MS Shell Dlg", 400, 0, 0x1
 BEGIN
@@ -255,7 +255,7 @@ BEGIN
 END
 
 IDD_LMPAGE DIALOGEX 0, 0, 218, 238
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
+STYLE DS_SETFONT | DS_MODALFRAME |  WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "Dialog"
 FONT 8, "MS Shell Dlg", 400, 0, 0x1
 BEGIN
@@ -283,7 +283,7 @@ BEGIN
 END
 
 IDD_APPPAGE DIALOGEX 0, 0, 381, 238
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
+STYLE DS_SETFONT | DS_MODALFRAME |  WS_POPUP | WS_CAPTION | WS_SYSMENU
 CAPTION "Dialog"
 FONT 8, "MS Shell Dlg", 400, 0, 0x1
 BEGIN
@@ -306,6 +306,15 @@ BEGIN
     CONTROL         "Copy all on stop",IDC_COPYONSTOP,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,204,60,90,10
 END
 
+IDD_MODULESETTINGS DIALOGEX 0, 0, 309, 177
+STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
+CAPTION "Dialog"
+FONT 8, "MS Shell Dlg", 400, 0, 0x1
+BEGIN
+    DEFPUSHBUTTON   "OK",IDOK,198,156,50,14
+    PUSHBUTTON      "Cancel",IDCANCEL,252,156,50,14
+END
+
 
 /////////////////////////////////////////////////////////////////////////////
 //
@@ -391,6 +400,14 @@ BEGIN
         TOPMARGIN, 7
         BOTTOMMARGIN, 231
     END
+
+    IDD_MODULESETTINGS, DIALOG
+    BEGIN
+        LEFTMARGIN, 7
+        RIGHTMARGIN, 302
+        TOPMARGIN, 7
+        BOTTOMMARGIN, 170
+    END
 END
 #endif    // APSTUDIO_INVOKED
 
diff --git a/Src/Win32/ModuleSettings.cpp b/Src/Win32/ModuleSettings.cpp
index 2a00e50..d3ded9a 100644
--- a/Src/Win32/ModuleSettings.cpp
+++ b/Src/Win32/ModuleSettings.cpp
@@ -4,194 +4,91 @@
 #include "ModuleControlString.h"
 #include "ModuleSettings.h"
 
-CONST UINT WM_MS_CLOSE = RegisterWindowMessage(_WM_MS_CLOSE);
-
 CModuleSettings::CModuleSettings(const std::string &strModuleName, SModuleSettings *pSettings, int iCount, 
CAppSettings *pAppSets) {
   m_iCount = iCount;
   m_pControls = new CModuleControl*[m_iCount];
   m_pAppSets = pAppSets;
   m_strModuleName = strModuleName;
 
-  for(int i(0); i < m_iCount; ++i) {
-    switch(pSettings[i].iType) {
-      case T_BOOL:
-        m_pControls[i] = new CModuleControlBool(pSettings + i);
-        break;
-      case T_LONG:
-        m_pControls[i] = new CModuleControlLong(pSettings + i);
-        break;
-      case T_LONGSPIN:
-        m_pControls[i] = new CModuleControlLongSpin(pSettings + i);
-        break;
-      case T_STRING:
-        m_pControls[i] = new CModuleControlString(pSettings + i);
-        break;
+  for (int i(0); i < m_iCount; ++i) {
+    switch (pSettings[i].iType) {
+    case T_BOOL:
+      m_pControls[i] = new CModuleControlBool(pSettings + i);
+      break;
+    case T_LONG:
+      m_pControls[i] = new CModuleControlLong(pSettings + i);
+      break;
+    case T_LONGSPIN:
+      m_pControls[i] = new CModuleControlLongSpin(pSettings + i);
+      break;
+    case T_STRING:
+      m_pControls[i] = new CModuleControlString(pSettings + i);
+      break;
     }
-   
+
   }
 }
 
 CModuleSettings::~CModuleSettings() {
-  for(int i(0); i < m_iCount; ++i)
+  for (int i(0); i < m_iCount; ++i)
     delete m_pControls[i];
 
   delete[] m_pControls;
 }
 
-void CModuleSettings::Create(HWND hWndParent, ATL::_U_RECT rect) {
-  m_hParent = hWndParent;
-
+LRESULT CModuleSettings::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
   std::wstring strWName;
   WinUTF8::UTF8string_to_wstring(m_strModuleName, strWName);
 
-  RECT sRect = {CW_USEDEFAULT, CW_USEDEFAULT, 0, 0};
-  CWindowImpl<CModuleSettings>::Create(hWndParent, sRect, strWName.c_str(), WS_DLGFRAME | WS_SYSMENU | 
WS_CLIPCHILDREN, WS_EX_WINDOWEDGE | WS_EX_CONTEXTHELP);
-
   int iHeight(7);
 
-  for(int i(0); i < m_iCount; ++i) {
+  for (int i(0); i < m_iCount; ++i) {
     m_pControls[i]->Create(m_hWnd);
     m_pControls[i]->Initialise(m_pAppSets);
     iHeight += m_pControls[i]->GetHeightRequest() + 2;
   }
+  iHeight += (-2 + 7 + 14 + 7); // - last item spacing + buttons 
 
-  m_hOk = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("BUTTON"), L"Ok", 
-      WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 50, 14, m_hWnd, NULL, WinHelper::hInstApp, NULL);
-
-  m_hCancel = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("BUTTON"), L"Cancel", 
-      WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 50, 14, m_hWnd, NULL, WinHelper::hInstApp, NULL);
-
-  HGDIOBJ hGuiFont;
-  hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
- 
-  SendMessage(m_hOk, WM_SETFONT, (WPARAM)hGuiFont, (LPARAM)true);
-  SendMessage(m_hCancel, WM_SETFONT, (WPARAM)hGuiFont, (LPARAM)true);
-  
-  iHeight += 26; // Subtract 2 for spacing
-
-  HDC hDC(::GetDC(m_hWnd));
-  SelectObject(hDC, hGuiFont);
-  SIZE sSize;
-  GetTextExtentPoint32(hDC, L"x", 1, &sSize);
-
-  int iBaseX(sSize.cx);
-  int iBaseY(sSize.cy);
-
-  ::ReleaseDC(m_hWnd, hDC);
-  
-  int iWindowWidth(MulDiv(250, iBaseX, 4));
-  int iWindowHeight(MulDiv(iHeight, iBaseY, 8));
-
-  RECT sWindowPos;
-  GetWindowRect(&sWindowPos);
-
-  sWindowPos.right = sWindowPos.left + iWindowWidth;
-  sWindowPos.bottom = sWindowPos.top + iWindowHeight;
-
-  AdjustWindowRect(&sWindowPos, WS_CAPTION, false);
-
-  MoveWindow(&sWindowPos);
+  RECT size = { 0, 0, m_iDlgWidth, iHeight };
+  MapDialogRect(&size);
+  ResizeClient(size.right, size.bottom);
+  return 1;
 }
 
-LRESULT CModuleSettings::OnCommand(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
-  bHandled = false;
-
-  if(HIWORD(wParam) == BN_CLICKED) {
-    if((HWND)lParam == m_hOk) {
-      bHandled = true;
-      // Apply and close 
-      for(int i(0); i < m_iCount; ++i) {
-        m_pControls[i]->Apply(m_pAppSets);
-      }
-      ShowWindow(SW_HIDE);
-      SendMessage(m_hParent, WM_MS_CLOSE, 0, 0);
-    }
-    else if((HWND)lParam == m_hCancel) {
-      bHandled = true;
-      ShowWindow(SW_HIDE);
-      SendMessage(m_hParent, WM_MS_CLOSE, 0, 0);
+LRESULT  CModuleSettings::OnCloseCmd(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled){
+  if (wID == IDOK) {
+    // Apply and close 
+    for (int i(0); i < m_iCount; ++i) {
+      m_pControls[i]->Apply(m_pAppSets);
     }
   }
-
-  return 0;
-}
-
-LRESULT CModuleSettings::OnPaint(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
-  bHandled = true;
-
-  RECT sRect;
-  GetClientRect(&sRect);
-
-  RECT sUpdateRect;
-  if(!GetUpdateRect(&sUpdateRect))
-    return 0;
-
-  PAINTSTRUCT sPS;
-
-  BeginPaint(&sPS);
-
-  HBRUSH hBrush(GetSysColorBrush(COLOR_3DFACE));
-  FillRect(sPS.hdc, &sRect, hBrush);
-
-  EndPaint(&sPS);
-
+  EndDialog(wID);
   return 0;
 }
 
 LRESULT CModuleSettings::OnSize(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
-  HGDIOBJ hGuiFont;
-  hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
-
-  HDC hDC(::GetDC(m_hWnd));
-  SelectObject(hDC, hGuiFont);
-  SIZE sSize;
-  GetTextExtentPoint32(hDC, L"x", 1, &sSize);
-
-  int iBaseX(sSize.cx);
-  int iBaseY(sSize.cy);
-
-  ::ReleaseDC(m_hWnd, hDC);
-
-  RECT sWindowRect;
-  GetClientRect(&sWindowRect);
-
-  int iWindowWidth(sWindowRect.right - sWindowRect.left);
-  int iWindowHeight(sWindowRect.bottom - sWindowRect.top);
-
-  int iDlgWidth(MulDiv(iWindowWidth, 4, iBaseX));
-  int iDlgHeight(MulDiv(iWindowHeight, 8, iBaseY));
-
-  // TODO: Dialogue units rather than pixels
-
   int iCurrentTop(7);
 
-  for(int i(0); i < m_iCount; ++i) {
+  for (int i(0); i < m_iCount; ++i) {
     int iHeight(m_pControls[i]->GetHeightRequest());
 
-    RECT sPosition;
-
-    sPosition.left = MulDiv(7, iBaseX, 4);
-    sPosition.right = MulDiv(243, iBaseX, 4);
-    sPosition.top = MulDiv(iCurrentTop, iBaseY, 8);
-    sPosition.bottom = MulDiv(iCurrentTop + iHeight, iBaseY, 8);
-
+    RECT sPosition = { 7, iCurrentTop, m_iDlgWidth - 7, iCurrentTop + iHeight };
+    MapDialogRect(&sPosition);
     m_pControls[i]->Layout(&sPosition);
 
     iCurrentTop += iHeight + 2;
   }
+  int btnX = m_iDlgWidth - (50 + 7);
+  int btnY = iCurrentTop -2 + 7;
+  MoveButton(IDCANCEL, btnX, btnY);
+  btnX -= 50 + 7;
+  MoveButton(IDOK, btnX, btnY);
+  return 0;
+}
 
-  int iDlgX(iDlgWidth - 57);
-  int iDlgY(iDlgHeight - 21);
-  int iDlgButtonWidth(50);
-  int iDlgButtonHeight(14);
-
-  ::MoveWindow(m_hCancel, MulDiv(iDlgX, iBaseX, 4), MulDiv(iDlgY, iBaseY, 8), 
-    MulDiv(iDlgButtonWidth, iBaseX, 4), MulDiv(iDlgButtonHeight, iBaseY, 8), true);  
-
-  iDlgX = iDlgWidth - 114;
-  
-  ::MoveWindow(m_hOk, MulDiv(iDlgX, iBaseX, 4), MulDiv(iDlgY, iBaseY, 8), 
-    MulDiv(iDlgButtonWidth, iBaseX, 4), MulDiv(iDlgButtonHeight, iBaseY, 8), true);  
+void CModuleSettings::MoveButton(WORD wID, int x, int y){
 
-  return 0;
+  RECT r = { x, y, x + 50, y + 14 };
+  MapDialogRect(&r);
+  GetDlgItem(wID).MoveWindow(&r);
 }
diff --git a/Src/Win32/ModuleSettings.h b/Src/Win32/ModuleSettings.h
index 1fe6f46..1f7b98e 100644
--- a/Src/Win32/ModuleSettings.h
+++ b/Src/Win32/ModuleSettings.h
@@ -3,46 +3,35 @@
 
 #include "AppSettings.h"
 #include "ModuleControl.h"
+#include "resource.h"
 
 #include <atlbase.h>
 #include <atlwin.h>
 #include <string>
 
-extern CONST UINT WM_MS_CLOSE;
-#define _WM_MS_CLOSE (LPCWSTR)"wm_ms_close"
-
-class CModuleSettings : public CWindowImpl<CModuleSettings> {
+class CModuleSettings : public CDialogImpl<CModuleSettings> {
 public:
   CModuleSettings(const std::string &strModuleName, SModuleSettings *pSettings, int iCount, CAppSettings 
*pAppSets);
   ~CModuleSettings();
 
-  void Create(HWND hWndParent, ATL::_U_RECT rect);
-
-  DECLARE_WND_CLASS(NULL);
+  enum { IDD = IDD_MODULESETTINGS };
 
   BEGIN_MSG_MAP(CModuleSettings)
-    MESSAGE_HANDLER(WM_COMMAND, OnCommand)
-    MESSAGE_HANDLER(WM_PAINT, OnPaint)
+    MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
+    COMMAND_RANGE_HANDLER(IDOK, IDCANCEL, OnCloseCmd)
     MESSAGE_HANDLER(WM_SIZE, OnSize)
   END_MSG_MAP()
 
-  LRESULT OnCommand(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
-  LRESULT OnPaint(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+  LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+  LRESULT OnCloseCmd(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
   LRESULT OnSize(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
 
-  virtual void OnFinalMessage(HWND hWnd) {
-    delete this;
-  }
-
 private:
+  void MoveButton(WORD wID, int x, int y);
   int m_iCount;
   CModuleControl **m_pControls;
   std::string m_strModuleName;
-
-  HWND m_hParent;
-
-  HWND m_hOk;
-  HWND m_hCancel;
+  const int m_iDlgWidth = 250;
 
   CAppSettings *m_pAppSets;
 };
diff --git a/Src/Win32/Widgets/ControlPage.cpp b/Src/Win32/Widgets/ControlPage.cpp
index 46f97e5..4eed542 100644
--- a/Src/Win32/Widgets/ControlPage.cpp
+++ b/Src/Win32/Widgets/ControlPage.cpp
@@ -13,6 +13,7 @@
 #include "../Common/StringUtils.h"
 #include "../AppSettings.h"
 #include "../ButtonPrefs.h"
+#include "../ModuleSettings.h"
 
 
 #include <utility>              // for std::pair
@@ -219,17 +220,6 @@ bool CControlPage::Apply()
 }
 
 LRESULT CControlPage::WndProc(HWND Window, UINT message, WPARAM wParam, LPARAM lParam) {
-  // TODO: Why isn't this in case?
-  if(message == WM_MS_CLOSE) {
-    if(m_pModuleSettingsDialogue) {
-      m_pModuleSettingsDialogue->DestroyWindow();
-    }
-
-    m_pModuleSettingsDialogue = NULL;
-    EnableWindow(m_hwnd, true);
-    return 0;
-  }
-
        double NewSpeed;
        switch (message) 
        {       
@@ -303,13 +293,8 @@ LRESULT CControlPage::WndProc(HWND Window, UINT message, WPARAM wParam, LPARAM l
           if(!m_pDasherInterface->GetModuleSettings(strNewValue, &pSettings, &iSettingsCount))
             break;
 
-          RECT sRect;
-
-          m_pModuleSettingsDialogue = new CModuleSettings(strNewValue, pSettings, iSettingsCount, 
m_pAppSettings);
-          m_pModuleSettingsDialogue->Create(m_hwnd, &sRect);
-          m_pModuleSettingsDialogue->ShowWindow(SW_RESTORE);
-        
-          EnableWindow(m_hwnd, false);
+          CModuleSettings dlg(strNewValue, pSettings, iSettingsCount, m_pAppSettings);
+          dlg.DoModal(m_hwnd);
         }
       }
                  break;
@@ -332,13 +317,8 @@ LRESULT CControlPage::WndProc(HWND Window, UINT message, WPARAM wParam, LPARAM l
           if(!m_pDasherInterface->GetModuleSettings(strNewValue, &pSettings, &iSettingsCount))
             break;
 
-          RECT sRect;
-
-          m_pModuleSettingsDialogue = new CModuleSettings(strNewValue, pSettings, iSettingsCount, 
m_pAppSettings);
-          m_pModuleSettingsDialogue->Create(m_hwnd, &sRect);
-          m_pModuleSettingsDialogue->ShowWindow(SW_RESTORE);
-        
-          EnableWindow(m_hwnd, false);
+          CModuleSettings dlg(strNewValue, pSettings, iSettingsCount, m_pAppSettings);
+          dlg.DoModal(m_hwnd);
         }
       }
                  break;
diff --git a/Src/Win32/Widgets/ControlPage.h b/Src/Win32/Widgets/ControlPage.h
index fa53d85..dc27a38 100644
--- a/Src/Win32/Widgets/ControlPage.h
+++ b/Src/Win32/Widgets/ControlPage.h
@@ -11,11 +11,9 @@
 
 #include "PrefsPageBase.h"
 
-#include "../resource.h"
 
 #include "../../DasherCore/DasherInterfaceBase.h"
 #include "../../DasherCore/ColourIO.h"
-#include "../ModuleSettings.h"
 
 class CAppSettings;
 
@@ -41,8 +39,6 @@ private:
   
   void PopulateList();
   bool Apply();
- 
-  CModuleSettings *m_pModuleSettingsDialogue;
 };
 
 #endif  // ndef
diff --git a/Src/Win32/resource.h b/Src/Win32/resource.h
index 4e34c0b..778c471 100644
--- a/Src/Win32/resource.h
+++ b/Src/Win32/resource.h
@@ -91,6 +91,7 @@
 #define IDD_LMPAGE                      175
 #define IDD_APPEARANCEPAGE              178
 #define IDD_APPPAGE                     179
+#define IDD_MODULESETTINGS              180
 #define IDC_LIST1                       1008
 #define IDC_INPUT_LIST                  1008
 #define IDC_LIST_ALPH                   1009
@@ -140,7 +141,6 @@
 #define IDC_OUTLINE                     1091
 #define IDC_STOPIDLE                    1091
 #define IDC_TIMESTAMP                   1092
-//#define IDC_STYLUS                      1092
 #define IDC_COPYALLONSTOP               1093
 #define IDC_DRAWMOUSE                   1094
 #define IDC_SPEECH                      1096
@@ -256,7 +256,7 @@
 // 
 #ifdef APSTUDIO_INVOKED
 #ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE        180
+#define _APS_NEXT_RESOURCE_VALUE        181
 #define _APS_NEXT_COMMAND_VALUE         32820
 #define _APS_NEXT_CONTROL_VALUE         1160
 #define _APS_NEXT_SYMED_VALUE           110


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