DlgRunParameters.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:15k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // DlgRunParameters.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "testbt.h"
  5. #include "DlgRunParameters.h"
  6. #include "FileDB.h"
  7. #include "SetupRegister.h"
  8. #include "Download.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDlgRunParameters dialog
  16. CDlgRunParameters::CDlgRunParameters(CFileDBItem* pFileDBItem, CSetupRegister* pSetupRegister, CWnd* pParent /*=NULL*/)
  17. : CDialog(CDlgRunParameters::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CDlgRunParameters)
  20. // NOTE: the ClassWizard will add member initialization here
  21. //}}AFX_DATA_INIT
  22. m_pSetupRegister = pSetupRegister;
  23. m_pFileDBItem = pFileDBItem;
  24. assert(m_pFileDBItem && m_pSetupRegister);
  25. }
  26. void CDlgRunParameters::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. //{{AFX_DATA_MAP(CDlgRunParameters)
  30. DDX_Control(pDX, IDC_SPIN_CONNECT_INTVAL, m_spinConnectIntval);
  31. DDX_Control(pDX, IDC_SPIN_CONNECT_ERR_MAX, m_spinConnectErrMax);
  32. DDX_Control(pDX, IDC_SPIN_PEERS_MAX_SPECIAL, m_spinPeersMax);
  33. DDX_Control(pDX, IDC_SPIN_PEERS_MIN_SPECIAL, m_spinPeersMin);
  34. DDX_Control(pDX, IDC_SPIN_COMPLETE_KEEP_SEED_HOUR, m_spinCompleteKeepSeedHour);
  35. DDX_Control(pDX, IDC_GROUP_UPSPEED, m_gpUpSpeed);
  36. DDX_Control(pDX, IDC_GROUP_GERNERAL, m_gpGeneral);
  37. DDX_Control(pDX, IDCANCEL, m_btnCancel);
  38. DDX_Control(pDX, IDOK, m_btnOK);
  39. DDX_Control(pDX, IDC_STATIC_SPEED_MAX, m_stSpeedMax);
  40. DDX_Control(pDX, IDC_STATIC_SPEED_MIN, m_stSpeedMin);
  41. DDX_Control(pDX, IDC_STATIC_PEERS_MAX, m_stPeersMax);
  42. DDX_Control(pDX, IDC_STATIC_PEERS_MIN, m_stPeersMin);
  43. DDX_Control(pDX, IDC_COMBO_GLOBAL_SETUP, m_cbGlobalSetup);
  44. DDX_Control(pDX, IDC_SLIDER_UPLOAD_SPEED_MAX, m_sliderUploadSpeedMax);
  45. DDX_Control(pDX, IDC_SLIDER_UPLOAD_PEERS_MAX, m_sliderUploadPeersMax);
  46. DDX_Control(pDX, IDC_SPIN_UPLOAD_SPEED_MAX, m_spinUploadSpeedMax);
  47. DDX_Control(pDX, IDC_SPIN_UPLOAD_PEERS_MAX, m_spinUploadPeersMax);
  48. DDX_Control(pDX, IDC_COMBO_DOWN_SPEED, m_cbDownSpeed);
  49. //}}AFX_DATA_MAP
  50. }
  51. BEGIN_MESSAGE_MAP(CDlgRunParameters, CDialog)
  52. //{{AFX_MSG_MAP(CDlgRunParameters)
  53. ON_CBN_SELCHANGE(IDC_COMBO_GLOBAL_SETUP, OnSelchangeComboGlobalSetup)
  54. ON_CBN_SELCHANGE(IDC_COMBO_DOWN_SPEED, OnSelchangeComboDownSpeed)
  55. ON_WM_HSCROLL()
  56. ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_UPLOAD_SPEED_MAX, OnDeltaposSpinUploadSpeedMax)
  57. ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_UPLOAD_PEERS_MAX, OnDeltaposSpinUploadPeersMax)
  58. ON_WM_VSCROLL()
  59. //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CDlgRunParameters message handlers
  63. BOOL CDlgRunParameters::OnInitDialog() 
  64. {
  65. CDialog::OnInitDialog();
  66. ///////////////////////////////////////////////////////////////////////
  67. // Create BitmapList
  68. //
  69. HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
  70. MAKEINTRESOURCE(IDB_BITMAP_DLG_ICONS),
  71. IMAGE_BITMAP,
  72. 0,0, // cx,cy
  73. LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
  74. CBitmap bm;
  75. bm.Attach(hbm);
  76. m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 3, 4);
  77. m_ctlImage.SetBkColor(RGB(255, 255,255)); 
  78. m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
  79. //
  80. // set button image.
  81. //
  82. // m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存路径");
  83. m_btnOK.Create(m_ctlImage.ExtractIcon(0));
  84. m_btnCancel.Create(m_ctlImage.ExtractIcon(1));
  85. /////////////////////////////////////////////////////////////////////
  86. // init data
  87. if (!m_pFileDBItem->m_bSpecialRunParameter)
  88. {
  89. m_pFileDBItem->m_lUploadSpeedMode = m_pSetupRegister->m_lDownSpeed;
  90. m_pFileDBItem->m_lUploadSpeedMax = CSpeedParameters::m_lArrSpeedVal[m_pSetupRegister->m_lDownSpeed];
  91. m_pFileDBItem->m_lUploadPeerMax = CSpeedParameters::m_lArrPeerVal[m_pSetupRegister->m_lDownSpeed];
  92. m_pFileDBItem->m_lPeersMin = m_pSetupRegister->m_lPeersMin;
  93. m_pFileDBItem->m_lPeersMax = m_pSetupRegister->m_lPeersMax;
  94. m_pFileDBItem->m_lConnectErrorMax = m_pSetupRegister->m_lConnectErrorMax;
  95. m_pFileDBItem->m_lConnectIntval = m_pSetupRegister->m_lConnectIntval;
  96. m_pFileDBItem->m_lCompleteKeepSeedHour = m_pSetupRegister->m_lCompleteKeepSeedHour;
  97. }
  98. /////////////////////////////////////////////////////////////////////
  99. // upspeed.
  100. // init contronls data.
  101. m_cbGlobalSetup.AddString("所有参数使用全局设置");
  102. m_cbGlobalSetup.AddString("使用特殊设置");
  103. for (int i=0; i<CSpeedParameters::eSpeedModeAll; i++)
  104. {
  105. m_cbDownSpeed.AddString(CSpeedParameters::m_strArrSpeedMode[i]);
  106. m_cbDownSpeed.SetItemData(m_cbDownSpeed.GetCount()-1, i);
  107. }
  108. m_spinUploadSpeedMax.SetBuddy(GetDlgItem(IDC_EDIT_UPLOAD_SPEED_MAX));
  109. m_spinUploadPeersMax.SetBuddy(GetDlgItem(IDC_EDIT_UPLOAD_PEERS_MAX));
  110. // show cur state.
  111. m_cbGlobalSetup.SetCurSel(m_pFileDBItem->m_bSpecialRunParameter);
  112. // m_cbDownSpeed.SetCurSel(m_pFileDBItem->m_lUploadSpeedMode);
  113. // OnSelchangeComboDownSpeed();
  114. // SetValue(m_pFileDBItem->m_lUploadSpeedMax, m_pFileDBItem->m_lUploadPeerMax);
  115. /////////////////////////////////////////////////////////////////////
  116. // general setup.
  117. //
  118. m_spinCompleteKeepSeedHour.SetBuddy(GetDlgItem(IDC_EDIT_COMPLETE_KEEP_SEED_HOUR));
  119. m_spinPeersMin.SetBuddy(GetDlgItem(IDC_EDIT_PEERS_MIN_SPECIAL));
  120. m_spinPeersMax.SetBuddy(GetDlgItem(IDC_EDIT_PEERS_MAX_SPECIAL));
  121. m_spinConnectErrMax.SetBuddy(GetDlgItem(IDC_EDIT_CONNECT_ERR_MAX));
  122. m_spinConnectIntval.SetBuddy(GetDlgItem(IDC_EDIT_CONNECT_INTVAL));
  123. m_spinPeersMin.SetRange(10, 60);
  124. m_spinPeersMax.SetRange(10, 60);
  125. m_spinCompleteKeepSeedHour.SetRange(0, 1000);
  126. m_spinConnectIntval.SetRange(0, 500);
  127. m_spinConnectErrMax.SetRange(1, 10000);
  128. ///////////////////////////////////////////////////////////////////////
  129. // set all values.
  130. //
  131. OnSelchangeComboGlobalSetup();
  132. return TRUE;  // return TRUE unless you set the focus to a control
  133.               // EXCEPTION: OCX Property Pages should return FALSE
  134. }
  135. void CDlgRunParameters::OnOK() 
  136. {
  137. m_pFileDBItem->m_bSpecialRunParameter = m_cbGlobalSetup.GetCurSel() > 0;
  138. ////////////////////////////////////////////
  139. // Get data from ctrl.
  140. //
  141. // upspeed.
  142. //
  143. m_pFileDBItem->m_lUploadSpeedMode = m_cbDownSpeed.GetCurSel(); //m_cbDownSpeed.GetItemData(iCurSel);
  144. m_pFileDBItem->m_lUploadSpeedMax = m_sliderUploadSpeedMax.GetPos();
  145. m_pFileDBItem->m_lUploadPeerMax = m_sliderUploadPeersMax.GetPos();
  146. //
  147. // gerneral.
  148. //
  149. m_pFileDBItem->m_lPeersMin = GetSpinValue(m_spinPeersMin);
  150. m_pFileDBItem->m_lPeersMax = GetSpinValue(m_spinPeersMax);
  151. m_pFileDBItem->m_lConnectErrorMax = GetSpinValue(m_spinConnectErrMax);
  152. m_pFileDBItem->m_lConnectIntval = GetSpinValue(m_spinConnectIntval);
  153. m_pFileDBItem->m_lCompleteKeepSeedHour = GetSpinValue(m_spinCompleteKeepSeedHour);
  154. if (m_pFileDBItem->m_lPeersMin >= m_pFileDBItem->m_lPeersMax)
  155. {
  156. int lower=0, upper=0;
  157. m_spinPeersMin.GetRange(lower, upper);
  158. m_pFileDBItem->m_lPeersMin = lower;
  159. }
  160. if (!m_pFileDBItem->m_bSpecialRunParameter)
  161. {
  162. //
  163. // upspeed.
  164. //
  165. m_pFileDBItem->m_lUploadSpeedMode = m_pSetupRegister->m_lDownSpeed;
  166. m_pFileDBItem->m_lUploadSpeedMax = CSpeedParameters::m_lArrSpeedVal[m_pSetupRegister->m_lDownSpeed];
  167. m_pFileDBItem->m_lUploadPeerMax = CSpeedParameters::m_lArrPeerVal[m_pSetupRegister->m_lDownSpeed];
  168. //
  169. // gerneral.
  170. //
  171. m_pFileDBItem->m_lPeersMin = m_pSetupRegister->m_lPeersMin;
  172. m_pFileDBItem->m_lPeersMax = m_pSetupRegister->m_lPeersMax;
  173. m_pFileDBItem->m_lConnectErrorMax = m_pSetupRegister->m_lConnectErrorMax;
  174. m_pFileDBItem->m_lConnectIntval = m_pSetupRegister->m_lConnectIntval;
  175. m_pFileDBItem->m_lCompleteKeepSeedHour = m_pSetupRegister->m_lCompleteKeepSeedHour;
  176. }
  177. ////////////////////////////////////////////
  178. // Implete change if item is running..
  179. if (m_pFileDBItem->m_pDownload && 
  180. m_pFileDBItem->m_pDownload->IsDownloading() && 
  181. !m_pFileDBItem->m_pDownload->IsBad() && 
  182. m_pFileDBItem->m_pDownload->CanSetRunParameters())
  183. {
  184. m_pFileDBItem->m_pDownload->SetRunParameters(
  185. m_pFileDBItem->m_lUploadSpeedMax, 
  186. m_pFileDBItem->m_lUploadPeerMax,
  187. m_pFileDBItem->m_lCompleteKeepSeedHour,
  188. m_pFileDBItem->m_lConnectErrorMax,
  189. m_pFileDBItem->m_lConnectIntval, 
  190. m_pFileDBItem->m_lPeersMin,
  191. m_pFileDBItem->m_lPeersMax);
  192. }
  193. CDialog::OnOK();
  194. }
  195. void CDlgRunParameters::OnSelchangeComboGlobalSetup() 
  196. {
  197. UpdateCommandUI();
  198. }
  199. void CDlgRunParameters::UpdateCommandUI()
  200. {
  201. bool bSpecial = m_cbGlobalSetup.GetCurSel() != 0;
  202. m_cbDownSpeed.EnableWindow(bSpecial);
  203. //
  204. // upspeed 
  205. //
  206. GetDlgItem(IDC_EDIT_UPLOAD_SPEED_MAX)->EnableWindow(bSpecial);
  207. GetDlgItem(IDC_EDIT_UPLOAD_PEERS_MAX)->EnableWindow(bSpecial);
  208. m_spinUploadSpeedMax.EnableWindow(bSpecial);
  209. m_spinUploadPeersMax.EnableWindow(bSpecial);
  210. m_sliderUploadSpeedMax.EnableWindow(bSpecial);
  211. m_sliderUploadPeersMax.EnableWindow(bSpecial);
  212. //
  213. // gerneral.
  214. //
  215. GetDlgItem(IDC_EDIT_COMPLETE_KEEP_SEED_HOUR)->EnableWindow(bSpecial);
  216. GetDlgItem(IDC_EDIT_PEERS_MIN_SPECIAL)->EnableWindow(bSpecial);
  217. GetDlgItem(IDC_EDIT_PEERS_MAX_SPECIAL)->EnableWindow(bSpecial);
  218. GetDlgItem(IDC_EDIT_CONNECT_ERR_MAX)->EnableWindow(bSpecial);
  219. GetDlgItem(IDC_EDIT_CONNECT_INTVAL)->EnableWindow(bSpecial);
  220. m_spinCompleteKeepSeedHour.EnableWindow(bSpecial);
  221. m_spinPeersMin.EnableWindow(bSpecial);
  222. m_spinPeersMax.EnableWindow(bSpecial);
  223. m_spinConnectErrMax.EnableWindow(bSpecial);
  224. m_spinConnectIntval.EnableWindow(bSpecial);
  225. // set parameter to global setup.
  226. if (!bSpecial)
  227. {
  228. //
  229. // upspeed 
  230. //
  231. m_cbDownSpeed.SetCurSel(m_pSetupRegister->m_lDownSpeed);
  232. OnSelchangeComboDownSpeed();
  233. //
  234. // gerneral.
  235. //
  236. m_spinConnectErrMax.SetPos(m_pSetupRegister->m_lConnectErrorMax);
  237. m_spinConnectIntval.SetPos(m_pSetupRegister->m_lConnectIntval);
  238. m_spinCompleteKeepSeedHour.SetPos(m_pSetupRegister->m_lCompleteKeepSeedHour);
  239. m_spinPeersMin.SetPos(m_pSetupRegister->m_lPeersMin);
  240. m_spinPeersMax.SetPos(m_pSetupRegister->m_lPeersMax);
  241. }
  242. else
  243. {
  244. //
  245. // upspeed 
  246. //
  247. m_cbDownSpeed.SetCurSel(m_pFileDBItem->m_lUploadSpeedMode);
  248. OnSelchangeComboDownSpeed();
  249. SetValue(m_pFileDBItem->m_lUploadSpeedMax, m_pFileDBItem->m_lUploadPeerMax);
  250. //
  251. // gerneral.
  252. //
  253. m_spinConnectErrMax.SetPos(m_pFileDBItem->m_lConnectErrorMax);
  254. m_spinConnectIntval.SetPos(m_pFileDBItem->m_lConnectIntval);
  255. m_spinCompleteKeepSeedHour.SetPos(m_pFileDBItem->m_lCompleteKeepSeedHour);
  256. m_spinPeersMin.SetPos(m_pFileDBItem->m_lPeersMin);
  257. m_spinPeersMax.SetPos(m_pFileDBItem->m_lPeersMax);
  258. }
  259. }
  260. void CDlgRunParameters::OnSelchangeComboDownSpeed()
  261. {
  262. long lCurSel = m_cbDownSpeed.GetCurSel();
  263. //
  264. // Set range.
  265. //
  266. m_spinUploadSpeedMax.SetRange(CSpeedParameters::m_lArrSpeedRange[lCurSel].cx, 
  267. CSpeedParameters::m_lArrSpeedRange[lCurSel].cy);
  268. m_sliderUploadSpeedMax.SetRange(CSpeedParameters::m_lArrSpeedRange[lCurSel].cx, 
  269. CSpeedParameters::m_lArrSpeedRange[lCurSel].cy, true);
  270. m_spinUploadPeersMax.SetRange(CSpeedParameters::m_lArrPeerRange[lCurSel].cx, 
  271. CSpeedParameters::m_lArrPeerRange[lCurSel].cy);
  272. m_sliderUploadPeersMax.SetRange(CSpeedParameters::m_lArrPeerRange[lCurSel].cx, 
  273. CSpeedParameters::m_lArrPeerRange[lCurSel].cy, true);
  274. // temp set their pos.
  275. m_sliderUploadSpeedMax.SetPos(CSpeedParameters::m_lArrSpeedRange[lCurSel].cx);
  276. m_sliderUploadPeersMax.SetPos(CSpeedParameters::m_lArrPeerRange[lCurSel].cx);
  277. // true set pos.
  278. SetValue(CSpeedParameters::m_lArrSpeedVal[lCurSel], CSpeedParameters::m_lArrPeerVal[lCurSel]);
  279. //
  280. // show text.
  281. //
  282. CString strText;
  283. strText.Format("%d", CSpeedParameters::m_lArrSpeedRange[lCurSel].cx);
  284. m_stSpeedMin.SetWindowText(strText);
  285. strText.Format("%d", CSpeedParameters::m_lArrSpeedRange[lCurSel].cy);
  286. m_stSpeedMax.SetWindowText(strText);
  287. strText.Format("%d", CSpeedParameters::m_lArrPeerRange[lCurSel].cx);
  288. m_stPeersMin.SetWindowText(strText);
  289. strText.Format("%d", CSpeedParameters::m_lArrPeerRange[lCurSel].cy);
  290. m_stPeersMax.SetWindowText(strText);
  291. }
  292. void CDlgRunParameters::SetValue(long lSpeedMax, long lPeersMax)
  293. {
  294. // m_sliderUploadSpeedMax.VerifyPos();
  295. // m_sliderUploadPeersMax.VerifyPos();
  296. m_sliderUploadSpeedMax.SetPos(lSpeedMax);
  297. m_spinUploadSpeedMax.SetPos(lSpeedMax);
  298. m_sliderUploadPeersMax.SetPos(lPeersMax);
  299. m_spinUploadPeersMax.SetPos(lPeersMax);
  300. #ifdef _DEBUG
  301. int lMin = 0, lMax = 0;
  302. m_sliderUploadSpeedMax.GetRange(lMin, lMax);
  303. ASSERT(lSpeedMax >= lMin &&  lSpeedMax <= lMax);
  304. int lSpeedMaxtrue = m_sliderUploadSpeedMax.GetPos();
  305. ASSERT(lSpeedMaxtrue == lSpeedMax);
  306. m_sliderUploadPeersMax.GetRange(lMin, lMax);
  307. ASSERT(lPeersMax >= lMin &&  lPeersMax <= lMax);
  308. int lPeersMaxtrue = m_spinUploadPeersMax.GetPos();
  309. ASSERT(lPeersMaxtrue == lPeersMax);
  310. #endif
  311. // m_sliderUploadSpeedMax.VerifyPos();
  312. // m_sliderUploadPeersMax.VerifyPos();
  313. }
  314. void CDlgRunParameters::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  315. {
  316. if (nSBCode == SB_ENDSCROLL)
  317. {
  318. if ((CSliderCtrl*)pScrollBar == &m_sliderUploadSpeedMax)
  319. {
  320. m_spinUploadSpeedMax.SetPos(m_sliderUploadSpeedMax.GetPos());
  321. }
  322. else if ((CSliderCtrl*)pScrollBar == &m_sliderUploadPeersMax)
  323. {
  324. m_spinUploadPeersMax.SetPos(m_sliderUploadPeersMax.GetPos());
  325. }
  326. }
  327. CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  328. }
  329. void CDlgRunParameters::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  330. {
  331. if (nSBCode == SB_ENDSCROLL)
  332. {
  333. if (pScrollBar == (CScrollBar *)&m_spinUploadSpeedMax)
  334. {
  335. long lPos = LOWORD(m_spinUploadSpeedMax.GetPos());
  336. m_sliderUploadSpeedMax.SetPos(lPos);
  337. #ifdef _DEBUG
  338. int lMin = 0, lMax = 0;
  339. m_sliderUploadSpeedMax.GetRange(lMin, lMax);
  340. ASSERT(lPos >= lMin &&  lPos <= lMax);
  341. #endif
  342. }
  343. else if (pScrollBar == (CScrollBar *)&m_spinUploadPeersMax)
  344. {
  345. long lPos = LOWORD(m_spinUploadPeersMax.GetPos());
  346. m_sliderUploadPeersMax.SetPos(lPos);
  347. #ifdef _DEBUG
  348. int lMin = 0, lMax = 0;
  349. m_sliderUploadPeersMax.GetRange(lMin, lMax);
  350. ASSERT(lPos >= lMin &&  lPos <= lMax);
  351. #endif
  352. }
  353. }
  354. CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
  355. }
  356. void CDlgRunParameters::OnDeltaposSpinUploadSpeedMax(NMHDR* pNMHDR, LRESULT* pResult) 
  357. {
  358. NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  359. *pResult = 0;
  360. // m_sliderUploadSpeedMax.SetPos(pNMUpDown->iPos);
  361. }
  362. void CDlgRunParameters::OnDeltaposSpinUploadPeersMax(NMHDR* pNMHDR, LRESULT* pResult) 
  363. {
  364. NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  365. *pResult = 0;
  366. // m_sliderUploadPeersMax.SetPos(pNMUpDown->iPos);
  367. }
  368. long CDlgRunParameters::GetSpinValue(CSpinButtonCtrl& SpinCtrl)
  369. {
  370. long lPos = LOWORD(SpinCtrl.GetPos());
  371. int lower=0, upper=0;
  372. SpinCtrl.GetRange(lower, upper);
  373. if (lPos < 0)
  374. {
  375. lPos = upper;
  376. }
  377. else
  378. {
  379. lPos = max(lower, lPos);
  380. lPos = min(upper, lPos);
  381. }
  382. return lPos;
  383. }