RangeDialog.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // RangeDialog.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "SimpleGraph.h"
  22. #include "RangeDialog.h"
  23. #include "SimpleGraphView.h"
  24. #include "mainfrm.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. //
  32. // fnEditToFloatProc
  33. //
  34. /////////////////////////////////////////////////////////////////////////////
  35. LRESULT CALLBACK
  36. fnEditToFloatProc
  37. (
  38. HWND hwnd,
  39. UINT message,
  40. WPARAM wparam,
  41. LPARAM lparam
  42. )
  43. {
  44. WNDPROC old_proc = (WNDPROC)::GetProp (hwnd, "OLD_WND_PROC");
  45. LRESULT result = 0L;
  46. if (message == WM_SETTEXT) {
  47. //
  48. // Convert the textual value to a long, convert
  49. // the long to a float, and conver the float to
  50. // a string.
  51. //
  52. LPCTSTR string = (LPCTSTR)lparam;
  53. if (::strchr (string, '.') != 0) {
  54. result = ::CallWindowProc (old_proc, hwnd, message, wparam, lparam);
  55. } else {
  56. long value = ::atol ((LPCTSTR)lparam);
  57. float float_value = value / 100.0F;
  58. CString new_text;
  59. new_text.Format ("%.2f", float_value);
  60. result = ::CallWindowProc (old_proc, hwnd, message, wparam, (LPARAM)(LPCTSTR)new_text);
  61. }
  62. } else if (message == WM_GETTEXT) {
  63. //
  64. // Get the value (as text) from the control,
  65. // convert it to a float, convert the float
  66. // to a long, then convert the long back to
  67. // a string.
  68. //
  69. result = ::CallWindowProc (old_proc, hwnd, message, wparam, lparam);
  70. LPCTSTR string = (LPCTSTR)lparam;
  71. if (::strchr (string, '.') != 0) {
  72. float float_value = ::atof (string);
  73. long int_value = long(float_value * 100);
  74. ::itoa (int_value, (LPTSTR)lparam, 10);
  75. } else {
  76. long int_value = ::atol (string) * 100;
  77. ::itoa (int_value, (LPTSTR)lparam, 10);
  78. }
  79. result = ::lstrlen ((LPTSTR)lparam);
  80. } else if (message == WM_CHAR) {
  81. //
  82. // Check to see if this is one of the characters we allow
  83. // the user to type
  84. //
  85. if ( (wparam >= '0' && wparam <= '9') ||
  86. wparam == '.' ||
  87. wparam == VK_BACK ||
  88. wparam == '-')
  89. {
  90. result = ::CallWindowProc (old_proc, hwnd, message, wparam, lparam);
  91. }
  92. } else if (old_proc != NULL) {
  93. result = ::CallWindowProc (old_proc, hwnd, message, wparam, lparam);
  94. }
  95. return result;
  96. }
  97. /////////////////////////////////////////////////////////////////////////////
  98. //
  99. // Make_Edit_Float_Ctrl
  100. //
  101. /////////////////////////////////////////////////////////////////////////////
  102. void
  103. Make_Edit_Float_Ctrl (HWND edit_wnd)
  104. {
  105. LONG old_proc = ::SetWindowLong (edit_wnd, GWL_WNDPROC, (LONG)fnEditToFloatProc);
  106. SetProp (edit_wnd, "OLD_WND_PROC", (HANDLE)old_proc);
  107. return ;
  108. }
  109. ////////////////////////////////////////////////////////////////////////////
  110. //
  111. // SetDlgItemFloat
  112. //
  113. ////////////////////////////////////////////////////////////////////////////
  114. void
  115. SetDlgItemFloat
  116. (
  117. HWND hdlg,
  118. UINT child_id,
  119. float value
  120. )
  121. {
  122. // Convert the float to a string
  123. CString text;
  124. text.Format ("%.2f", value);
  125. // Pass the string onto the dialog control
  126. ::SetDlgItemText (hdlg, child_id, text);
  127. return ;
  128. }
  129. ////////////////////////////////////////////////////////////////////////////
  130. //
  131. // GetDlgItemFloat
  132. //
  133. ////////////////////////////////////////////////////////////////////////////
  134. float
  135. GetDlgItemFloat
  136. (
  137. HWND hdlg,
  138. UINT child_id
  139. )
  140. {
  141. // Get the string from the window
  142. TCHAR string_value[20];
  143. ::GetDlgItemText (hdlg, child_id, string_value, sizeof (string_value));
  144. // Convert the string to a float and return the value
  145. return ::atof (string_value);
  146. }
  147. /////////////////////////////////////////////////////////////////////////////
  148. //
  149. // CRangeDialog
  150. //
  151. /////////////////////////////////////////////////////////////////////////////
  152. CRangeDialog::CRangeDialog(CWnd* pParent /*=NULL*/)
  153. : CDialog(CRangeDialog::IDD, pParent)
  154. {
  155. //{{AFX_DATA_INIT(CRangeDialog)
  156. // NOTE: the ClassWizard will add member initialization here
  157. //}}AFX_DATA_INIT
  158. return ;
  159. }
  160. void CRangeDialog::DoDataExchange(CDataExchange* pDX)
  161. {
  162. CDialog::DoDataExchange(pDX);
  163. //{{AFX_DATA_MAP(CRangeDialog)
  164. // NOTE: the ClassWizard will add DDX and DDV calls here
  165. //}}AFX_DATA_MAP
  166. }
  167. BEGIN_MESSAGE_MAP(CRangeDialog, CDialog)
  168. //{{AFX_MSG_MAP(CRangeDialog)
  169. //}}AFX_MSG_MAP
  170. END_MESSAGE_MAP()
  171. /////////////////////////////////////////////////////////////////////////////
  172. //
  173. // CRangeDialog
  174. //
  175. /////////////////////////////////////////////////////////////////////////////
  176. BOOL
  177. CRangeDialog::OnInitDialog (void)
  178. {
  179. CDialog::OnInitDialog ();
  180. CSimpleGraphView *view = (CSimpleGraphView *)((CMainFrame *)::AfxGetMainWnd ())->GetActiveView ();
  181. Vector2 range_min;
  182. Vector2 range_max;
  183. view->Get_Ranges (range_min, range_max);
  184. /*::Make_Edit_Float_Ctrl (::GetDlgItem (m_hWnd, IDC_MIN_X));
  185. ::Make_Edit_Float_Ctrl (::GetDlgItem (m_hWnd, IDC_MIN_Y));
  186. ::Make_Edit_Float_Ctrl (::GetDlgItem (m_hWnd, IDC_MAX_X));
  187. ::Make_Edit_Float_Ctrl (::GetDlgItem (m_hWnd, IDC_MAX_Y));*/
  188. SetDlgItemFloat (m_hWnd, IDC_MIN_X, range_min.X);
  189. SetDlgItemFloat (m_hWnd, IDC_MIN_Y, range_min.Y);
  190. SetDlgItemFloat (m_hWnd, IDC_MAX_X, range_max.X);
  191. SetDlgItemFloat (m_hWnd, IDC_MAX_Y, range_max.Y);
  192. return TRUE;
  193. }
  194. /////////////////////////////////////////////////////////////////////////////
  195. //
  196. // OnOK
  197. //
  198. /////////////////////////////////////////////////////////////////////////////
  199. void
  200. CRangeDialog::OnOK (void)
  201. {
  202. Vector2 range_min;
  203. Vector2 range_max;
  204. range_min.X = ::GetDlgItemFloat (m_hWnd, IDC_MIN_X);
  205. range_min.Y = ::GetDlgItemFloat (m_hWnd, IDC_MIN_Y);
  206. range_max.X = ::GetDlgItemFloat (m_hWnd, IDC_MAX_X);
  207. range_max.Y = ::GetDlgItemFloat (m_hWnd, IDC_MAX_Y);
  208. CSimpleGraphView *view = (CSimpleGraphView *)((CMainFrame *)::AfxGetMainWnd ())->GetActiveView ();
  209. view->Set_Ranges (range_min, range_max);
  210. view->InvalidateRect (NULL, TRUE);
  211. view->UpdateWindow ();
  212. CDialog::OnOK ();
  213. return ;
  214. }