StkUI.cpp
上传用户:zhanglf88
上传日期:2013-11-19
资源大小:6036k
文件大小:25k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. // StkUI.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "StkUI.h"
  5. #include <io.h>
  6. #include <afxsock.h>
  7. #include "MainFrm.h"
  8. #include "ChildFrm.h"
  9. #include "StaticDoc.h"
  10. #include "View/SimuView.h"
  11. #include "View/InfoView.h"
  12. #include "Dialog/SetPrpt.h"
  13. #include "Dialog/SelectStk.h"
  14. #include "Dialog/SetRate.h"
  15. #include "Dialog/SetRule.h"
  16. #include "Dialog/SimuReport.h"
  17. #include "Dialog/SimuRealOp.h"
  18. #include "Dialog/CloseStrategyDlg.h"
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CStkUIApp
  21. BEGIN_MESSAGE_MAP(CStkUIApp, CWinApp)
  22. //{{AFX_MSG_MAP(CStkUIApp)
  23. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  24. ON_COMMAND(ID_FILE_NEW, OnFileNew)
  25. ON_COMMAND(ID_STRATEGY_NEW, OnStrategyNew)
  26. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  27. ON_COMMAND(ID_STRATEGY_SAVE, OnStrategySave)
  28. ON_UPDATE_COMMAND_UI(ID_STRATEGY_SAVE, OnUpdateStrategySave)
  29. ON_COMMAND(ID_STRATEGY_SAVE_AS, OnStrategySaveAs)
  30. ON_UPDATE_COMMAND_UI(ID_STRATEGY_SAVE_AS, OnUpdateStrategySaveAs)
  31. ON_COMMAND(ID_STRATEGY_CLOSE, OnStrategyClose)
  32. ON_UPDATE_COMMAND_UI(ID_STRATEGY_CLOSE, OnUpdateStrategyClose)
  33. ON_COMMAND(ID_STRATEGY_SAVEALL, OnStrategySaveall)
  34. ON_UPDATE_COMMAND_UI(ID_STRATEGY_SAVEALL, OnUpdateStrategySaveall)
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CStkUIApp construction
  39. CStkUIApp::CStkUIApp()
  40. {
  41. m_pStaticDoc = NULL;
  42. m_bAutoUpdateViews = TRUE;
  43. }
  44. UINT LoadKDataCacheMain(LPVOID pParam)
  45. {
  46. AfxGetDB().LoadKDataCache(AfxGetStockContainer(), NULL, NULL, 0, STKLIB_MAX_PROGRESS);
  47. AfxGetStockContainer().OnDataChanged();
  48. AfxGetStkReceiver().RefreshStockContainer(AfxGetStockContainer(), TRUE);
  49. AfxReloadStockMain();
  50. ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_USER_UPDATESLISTVIEW,0, 0);
  51. ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_USER_INITDATES, 0, 0);
  52. AfxEndThread(0, TRUE);
  53. return 0;
  54. }
  55. BOOL CALLBACK LoadProgram(HWND hWnd, int nMinProgress, int nMaxProgress)
  56. {
  57. int nMin = nMinProgress;
  58. int nMax = nMaxProgress;
  59. int nRange = nMaxProgress-nMinProgress;
  60. CStkUIApp * pApp = AfxGetStkUIApp();
  61. if (NULL == pApp)
  62. return FALSE;
  63. CStaticDoc * pStaticDoc = pApp->GetStaticDoc();
  64. CMainFrame * pMainFrame = AfxGetMainFrame();
  65. HINSTANCE hInstanceOld = AfxGetResourceHandle();
  66. AfxSetResourceHandle(AfxGetInstanceHandle());
  67. SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin, 0);
  68. AfxGetStockContainer().Load(&AfxGetDB(), NULL, NULL);
  69. AfxGetDomainContainer().Load(AfxGetProfile().GetDomainFile());
  70. AfxGetGroupContainer().Load(AfxGetProfile().GetGroupFile());
  71. SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*4/30, 0);
  72. pMainFrame->m_SearchBox.InitStocks(TRUE, TRUE, TRUE);
  73. SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*8/30, 0);
  74. // Open Simulation Strategy
  75. if (NULL == pApp->GetFirstStrategyPosition())
  76. {
  77. if (0 == pApp->OpenLastOpenedStrategy())
  78. pApp->OpenDefaultStrategy();
  79. }
  80. SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*12/30, 0);
  81. // Show Views
  82. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CWizardView)))
  83. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CWizardView), TRUE);
  84. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*14/30, 0);
  85. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CSimuView)))
  86. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CSimuView), TRUE);
  87. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*16/30, 0);
  88. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CSListView)))
  89. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CSListView), TRUE);
  90. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*18/30, 0);
  91. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CRealTimeView)))
  92. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CRealTimeView), TRUE);
  93. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*20/30, 0);
  94. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CMultiSortView)))
  95. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CMultiSortView), TRUE);
  96. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*22/30, 0);
  97. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CGraphView)))
  98. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CGraphView), TRUE);
  99. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*24/30, 0);
  100. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CInfoView)))
  101. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CInfoView), TRUE);
  102. //SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMin+nRange*28/30, 0);
  103. //if (!pStaticDoc->GetViewIfExist(RUNTIME_CLASS(CSelectorView)))
  104. // pStaticDoc->ShowStaticView(RUNTIME_CLASS(CSelectorView), TRUE);
  105. //AfxSwitchToStaticView(RUNTIME_CLASS(CSListView));
  106. AfxGetProfile().SetCurrentStock(STKLIB_CODE_SZZS, FALSE);
  107. pMainFrame->m_SearchBox.SetCurrentWindowText();
  108. AfxBeginThread(LoadKDataCacheMain, NULL, THREAD_PRIORITY_NORMAL);
  109. SendMessage(hWnd, WM_USER_STARTUP_PROGRESS, nMax, 0);
  110. AfxSetResourceHandle(hInstanceOld);
  111. return TRUE;
  112. }
  113. /////////////////////////////////////////////////////////////////////////////
  114. // The one and only CStkUIApp object
  115. CStkUIApp theApp;
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CStkUIApp initialization
  118. TCHAR szRegKeyCompanyOld[] = _T("Balang Software");
  119. TCHAR szRegKeyAppOld[] = _T("StockAna");
  120. #ifdef CLKLAN_ENGLISH_US
  121. TCHAR szRegKeyCompany[] = _T("Ninebulls-Stock Software");
  122. TCHAR szRegKeyApp[] = _T("Ninebulls China Stock");
  123. #elif defined CLKVER_OEM
  124. TCHAR szRegKeyCompany[] = OEM_REGKEY_COMPANY;
  125. TCHAR szRegKeyApp[] = OEM_REGKEY_APP;
  126. #else
  127. TCHAR szRegKeyCompany[] = _T("Ninebulls-Stock Software");
  128. TCHAR szRegKeyApp[] = _T("CLKing");
  129. #endif
  130. BOOL CStkUIApp::InitInstance()
  131. {
  132. // Standard initialization
  133. // If you are not using these features and wish to reduce the size
  134. //  of your final executable, you should remove from the following
  135. //  the specific initialization routines you do not need.
  136. AfxSocketInit();
  137. AfxOleInit();
  138. AfxGetProfile().LoadProfile();
  139. ::SetCurrentDirectory(AfxGetProfile().GetWorkDirectory());
  140. AfxGetSView().Load(AfxGetProfile().GetSViewFile());
  141. // Change the registry key under which our settings are stored.
  142. // TODO: You should modify this string to be something appropriate
  143. // such as the name of your company or organization.
  144. // SetRegistryKey(szRegKeyCompany);
  145. AfxRegSetValue(szRegKeyCompany, szRegKeyApp, "Install", "Path", AfxGetProfile().GetWorkDirectory());
  146. AfxRegSetValue(szRegKeyCompany, szRegKeyApp, "Install", "Version", AfxGetProfile().GetVersion());
  147. // change profile (INI file)
  148. BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  149. if (m_pszProfileName) free((void*)m_pszProfileName);
  150. m_pszProfileName = _tcsdup(AfxGetProfile().GetWorkDirectory()+m_pszExeName+".ini");
  151. AfxEnableMemoryTracking(bEnable);
  152. LoadStdProfileSettings(64);  // Load standard INI file options (including MRU)
  153. // Register the application's document templates.  Document templates
  154. //  serve as the connection between documents, frame windows and views.
  155. // Enable DDE Execute open
  156. EnableShellOpen();
  157. AfxUnregisterShellFileTypes();
  158. // AfxRegisterShellFileTypes(FALSE);
  159. // Parse command line for standard shell commands, DDE, file open
  160. CCommandLineInfo cmdInfo;
  161. ParseCommandLine(cmdInfo);
  162. // Dispatch commands specified on the command line
  163. if (CCommandLineInfo::AppUnregister == cmdInfo.m_nShellCommand)
  164. {
  165. AfxUnregisterShellFileTypes();
  166. if (!cmdInfo.m_bRunEmbedded)
  167. AfxMessageBox(AFX_IDP_UNREG_DONE, MB_OK | MB_ICONINFORMATION);
  168. if (m_pCmdInfo == NULL)
  169. {
  170. m_pCmdInfo = new CCommandLineInfo;
  171. m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
  172. }
  173. return FALSE;
  174. }
  175. {
  176. // Empty Temp Directory
  177. CNetDatabase netdb;
  178. if (netdb.SetRootPath(AfxGetProfile().GetSelfDBPath(), IStStore::dbtypeSelfDB))
  179. netdb.EmptyTempDirectory();
  180. }
  181. // Load Data
  182. AfxSetDB(&AfxGetNetDB());
  183. char szErr[1024];
  184. if (!AfxInitializeDB(szErr, sizeof(szErr))) AfxMessageBox(szErr, MB_OK|MB_ICONINFORMATION);
  185. // Create MainFrame and Static Doc and child frames and static views
  186. CStaticDoc * pStaticDoc = CStaticDoc::OpenDocumentFile(NULL);
  187. if (NULL == pStaticDoc)
  188. {
  189. AfxMessageBox(IDS_STATICDOC_FAILED, MB_OK | MB_ICONINFORMATION);
  190. return FALSE;
  191. }
  192. pStaticDoc->m_bAutoDelete = FALSE;
  193. CString strAppTitle;
  194. strAppTitle.LoadString(AFX_IDS_APP_TITLE);
  195. pStaticDoc->SetTitle(strAppTitle);
  196. SetStaticDoc(pStaticDoc);
  197. // 建立主窗口
  198. CMainFrame* pMainFrame = CMainFrame::CreateNewFrame();
  199. if (pMainFrame == NULL)
  200. {
  201. AfxMessageBox(IDS_STATICDOC_FAILED, MB_OK | MB_ICONINFORMATION);
  202. delete pStaticDoc;
  203. SetStaticDoc(NULL);
  204. return FALSE;
  205. }
  206. SetMainWnd(pMainFrame);
  207. pStaticDoc->ShowStaticView(RUNTIME_CLASS(CSListView), TRUE);
  208. AfxSwitchToStaticView(RUNTIME_CLASS(CSListView));
  209. // 装载主窗口最后状态
  210. WINDOWPLACEMENT wp;
  211. if (AfxGetProfile().GetWindowPlacement(&wp))
  212. m_pMainWnd->SetWindowPlacement(&wp);
  213. else
  214. m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
  215. // 股票行情接收
  216. AfxGetStkReceiver().CreateReceiver(pMainFrame);
  217. AfxGetStkReceiver().NetEngineSetAutoReport(TRUE);
  218. // 显示启动窗口
  219. CStartupDlg startup;
  220. CBitmap bmp;
  221. bmp.LoadBitmap(IDB_DLGLEFTLOGO);
  222. startup.SetBitmap((HBITMAP)bmp.GetSafeHandle());
  223. startup.SetLoadProgramFunc(LoadProgram);
  224. if (IDOK != startup.DoModal())
  225. {
  226. //pMainFrame->SendMessage(WM_CLOSE);
  227. //return FALSE;
  228. }
  229. if (CCommandLineInfo::FileOpen == cmdInfo.m_nShellCommand)
  230. {
  231. OpenStrategyFile(cmdInfo.m_strFileName);
  232. AfxSwitchToStaticView(RUNTIME_CLASS(CSimuView));
  233. }
  234. return TRUE;
  235. }
  236. BOOL CStkUIApp::OnDBChanged(BOOL bRecalculate)
  237. {
  238. CWaitCursor waitcursor;
  239. // Reload DB
  240. AfxGetStockContainer().Load(&AfxGetDB(), NULL, NULL);
  241. AfxGetDomainContainer().Load(AfxGetProfile().GetDomainFile());
  242. AfxGetGroupContainer().Load(AfxGetProfile().GetGroupFile());
  243. AfxGetDB().LoadKDataCache(AfxGetStockContainer(), NULL, NULL, 0, STKLIB_MAX_PROGRESS);
  244. AfxGetStockContainer().OnDataChanged();
  245. AfxGetStkReceiver().RefreshStockContainer(AfxGetStockContainer(), TRUE);
  246. // Reload AfxGetStockMain()
  247. AfxReloadStockMain();
  248. // Recalcuate Yield
  249. if (bRecalculate && IDYES == AfxMessageBox(IDS_INSTALLPAC_IFRECALCULATE, MB_YESNO|MB_ICONINFORMATION))
  250. {
  251. AfxRecalculateYield(AfxGetProfile().GetYieldAverageDays(), TRUE);
  252. AfxGetStockContainer().ReloadBase(&AfxGetDB());
  253. }
  254. // Update SListBar
  255. CMainFrame * pMainFrame = AfxGetMainFrame();
  256. if (pMainFrame)
  257. {
  258. pMainFrame->m_SearchBox.InitStocks(TRUE, TRUE, TRUE);
  259. pMainFrame->m_SearchBox.SetCurrentWindowText();
  260. }
  261. // Update Views
  262. AfxGetSListStockContainer().ReRetrieveFromStatic(AfxGetActiveStrategy(), TRUE);
  263. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_SLISTVIEW, NULL);
  264. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_GRAPHVIEW, NULL);
  265. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_BASEVIEW, NULL);
  266. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_SIMUVIEW_REREALRUN, NULL);
  267. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_SELECTORVIEW, NULL);
  268. return TRUE;
  269. }
  270. void CStkUIApp::SaveLastOpenedStrategy()
  271. {
  272. CSPStringArray astr;
  273. POSITION pos = GetFirstStrategyPosition();
  274. while(pos != NULL)
  275. {
  276. CStrategy* pStrategy = GetNextStrategy(pos);
  277. if (pStrategy)
  278. astr.Add(pStrategy->GetPathName());
  279. }
  280. AfxGetProfile().SetLastOpenedStrategy(astr);
  281. }
  282. int CStkUIApp::OpenLastOpenedStrategy()
  283. {
  284. CSPStringArray & astrStrategyToOpen = AfxGetProfile().GetLastOpenedStrategy();
  285. CSPString strLastActive = AfxGetProfile().GetLastActiveStrategy();
  286. if (0 != access(strLastActive,0))
  287. strLastActive.Empty();
  288. int nCount = 0;
  289. for(int i=0; i<astrStrategyToOpen.GetSize(); i++)
  290. {
  291. if (0 != access(astrStrategyToOpen.ElementAt(i),0))
  292. continue;
  293. if (NULL != OpenStrategyFile(astrStrategyToOpen.ElementAt(i)))
  294. {
  295. nCount ++;
  296. if (strLastActive.IsEmpty())
  297. strLastActive = astrStrategyToOpen.ElementAt(i);
  298. }
  299. }
  300. if (!strLastActive.IsEmpty())
  301. OpenStrategyFile(strLastActive);
  302. return nCount;
  303. }
  304. int CStkUIApp::OpenDefaultStrategy()
  305. {
  306. CSPStringArray astrStrategyToOpen;
  307. // Recent
  308. if (m_pRecentFileList)
  309. {
  310. for(int i=m_pRecentFileList->GetSize()-1; i>=0; i--)
  311. {
  312. CString strPath = (*m_pRecentFileList)[i];
  313. if (!strPath.IsEmpty())
  314. astrStrategyToOpen.Add(strPath);
  315. }
  316. }
  317. // projects
  318. CString strExt = AfxGetStrategyFileExt();
  319. if (!strExt.IsEmpty())
  320. {
  321. CFileFind finder;
  322. BOOL bWorking = finder.FindFile(AfxGetFilePath((LPCTSTR)AfxGetProfile().GetProjectPath(), LPCTSTR("*" + strExt)));
  323. while(bWorking)
  324. {
  325. bWorking = finder.FindNextFile();
  326. CString strPath = finder.GetFilePath();
  327. for(int i=0; i<astrStrategyToOpen.GetSize(); i++)
  328. {
  329. if (0 == strPath.Compare(astrStrategyToOpen.ElementAt(i)))
  330. break;
  331. }
  332. if (i == astrStrategyToOpen.GetSize())
  333. astrStrategyToOpen.Add(strPath);
  334. }
  335. finder.Close();
  336. }
  337. AfxGetProfile().SetLastOpenedStrategy(astrStrategyToOpen);
  338. return OpenLastOpenedStrategy();
  339. }
  340. CStrategy * CStkUIApp::GetActiveStrategy()
  341. {
  342. CStrategy * pStrategy = NULL;
  343. //CStrategyView * pStrategyView = AfxGetStrategyView();
  344. //if (pStrategyView)
  345. // pStrategy = pStrategyView->GetActiveStrategy();
  346. return pStrategy;
  347. }
  348. void CStkUIApp::AddStrategy(CStrategy* pStrategy)
  349. {
  350. if (NULL == pStrategy)
  351. return;
  352. ASSERT(m_strategyList.Find(pStrategy, NULL) == NULL); // must not be in list
  353. m_strategyList.AddTail(pStrategy);
  354. if (m_bAutoUpdateViews)
  355. {
  356. //::SendMessage(AfxGetStrategyView()->GetSafeHwnd(), WM_USER_ADDSTRATEGY, 0, 0);
  357. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_WIZARDVIEW, NULL);
  358. // AfxGetMainFrame()->ShowWorkspBar();
  359. }
  360. }
  361. void CStkUIApp::RemoveStrategy(CStrategy* pStrategy)
  362. {
  363. POSITION pos = m_strategyList.Find(pStrategy);
  364. ASSERT(NULL != pos);
  365. if (pos)
  366. m_strategyList.RemoveAt(pos);
  367. if (m_bAutoUpdateViews)
  368. {
  369. //::SendMessage(AfxGetStrategyView()->GetSafeHwnd(), WM_USER_REMOVESTRATEGY, 0, 0);
  370. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_WIZARDVIEW, NULL);
  371. }
  372. }
  373. POSITION CStkUIApp::GetFirstStrategyPosition() const
  374. {
  375. return m_strategyList.GetHeadPosition();
  376. }
  377. CStrategy* CStkUIApp::GetNextStrategy(POSITION& rPos) const
  378. {
  379. return (CStrategy*)m_strategyList.GetNext(rPos);
  380. }
  381. BOOL CStkUIApp::ExistStrategy(CStrategy * pStrategy)
  382. {
  383. POSITION pos = GetFirstStrategyPosition();
  384. while(NULL != pos)
  385. {
  386. CStrategy * p = GetNextStrategy(pos);
  387. if (p == pStrategy)
  388. return TRUE;
  389. }
  390. return FALSE;
  391. }
  392. BOOL CStkUIApp::IsFileOpened(LPCTSTR lpszFileName)
  393. {
  394. POSITION pos = GetFirstStrategyPosition();
  395. while (pos != NULL)
  396. {
  397. CStrategy* pStrategy = GetNextStrategy(pos);
  398. if (0 == pStrategy->GetPathName().CompareNoCase(lpszFileName))
  399. {
  400. return TRUE;
  401. }
  402. }
  403. return FALSE;
  404. }
  405. CString CStkUIApp::GetLastStrategyDirectory()
  406. {
  407. CString strLastStrategyDirectory;
  408. if (m_pRecentFileList && m_pRecentFileList->GetSize() > 0)
  409. {
  410. strLastStrategyDirectory = (*m_pRecentFileList)[0];
  411. strLastStrategyDirectory = AfxGetFileDirectoryExist(strLastStrategyDirectory, "");
  412. }
  413. if (strLastStrategyDirectory.IsEmpty() || 0 != access(strLastStrategyDirectory,0))
  414. {
  415. strLastStrategyDirectory = AfxGetProfile().GetProjectPath();
  416. }
  417. return strLastStrategyDirectory;
  418. }
  419. CString CStkUIApp::GetNextNewStrategyTitle(CString & strExtBuffer, CString strPath)
  420. {
  421. CString string;
  422. VERIFY(string.LoadString(IDS_STRATEGY_NONAME));
  423. CString strExt = AfxGetStrategyFileExt();
  424. if (!strExt.IsEmpty())
  425. strExtBuffer = strExt;
  426. CStringArray astrExistTitle;
  427. POSITION pos = GetFirstStrategyPosition();
  428. while(NULL != pos)
  429. {
  430. CStrategy * pStrategy = GetNextStrategy(pos);
  431. astrExistTitle.Add(AfxGetFileTitleNoExt(pStrategy->GetPathName()));
  432. }
  433. CFileFind finder;
  434. BOOL bWorking = finder.FindFile(AfxGetFilePath((LPCTSTR)strPath, LPCTSTR("*" + strExt)));
  435. while(bWorking)
  436. {
  437. bWorking = finder.FindNextFile();
  438. astrExistTitle.Add(finder.GetFileTitle());
  439. }
  440. finder.Close();
  441. for(int i=1; ; i++)
  442. {
  443. CString strTemp;
  444. strTemp.Format("%s(%d)", string, i);
  445. BOOL bHas = FALSE;
  446. for(int k=0; k<astrExistTitle.GetSize(); k++)
  447. {
  448. if (0 == strTemp.CompareNoCase(astrExistTitle.ElementAt(k)))
  449. {
  450. bHas = TRUE;
  451. break;
  452. }
  453. }
  454. if (!bHas)
  455. {
  456. string = strTemp;
  457. break;
  458. }
  459. }
  460. return string;
  461. }
  462. CStrategy* CStkUIApp::OpenStrategyFile(LPCTSTR lpszPathName)
  463. {
  464. // Resolve File Name
  465. TCHAR szPath[_MAX_PATH];
  466. ASSERT(lstrlen(lpszPathName) < sizeof(szPath));
  467. if (NULL != lpszPathName)
  468. {
  469. TCHAR szTemp[_MAX_PATH];
  470. if (lpszPathName[0] == '"')
  471. ++lpszPathName;
  472. lstrcpyn(szTemp, lpszPathName, _MAX_PATH);
  473. LPTSTR lpszLast = _tcsrchr(szTemp, '"');
  474. if (lpszLast != NULL)
  475. *lpszLast = 0;
  476. AfxFullPath(szPath, szTemp);
  477. TCHAR szLinkName[_MAX_PATH];
  478. if (AfxResolveShortcut(AfxGetMainWnd(), szPath, szLinkName, _MAX_PATH))
  479. lstrcpy(szPath, szLinkName);
  480. }
  481. if (NULL != lpszPathName)
  482. {
  483. POSITION pos = GetFirstStrategyPosition();
  484. while (pos != NULL)
  485. {
  486. CStrategy* pStrategy = GetNextStrategy(pos);
  487. if (0 == pStrategy->GetPathName().CompareNoCase(szPath))
  488. {
  489. if (m_bAutoUpdateViews)
  490. {
  491. //::SendMessage(AfxGetStrategyView()->GetSafeHwnd(), WM_USER_ACTIVATESTRATEGY, DWORD(pStrategy), 0);
  492. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_WIZARDVIEW, NULL);
  493. // AfxGetMainFrame()->ShowWorkspBar();
  494. }
  495. return pStrategy;
  496. }
  497. }
  498. }
  499. CStrategy   * pStrategy = new CStrategy();
  500. if (NULL == pStrategy)
  501. return NULL;
  502. char szErr[256];
  503. if (!pStrategy->OpenStrategyFile(szPath, szErr, sizeof(szErr)-1))
  504. {
  505. AfxMessageBox(szErr, MB_OK|MB_ICONINFORMATION);
  506. delete pStrategy;
  507. return NULL;
  508. }
  509. AddStrategy(pStrategy);
  510. return pStrategy;
  511. }
  512. void CStkUIApp::CloseAllStrategy(BOOL bEndSession)
  513. {
  514. POSITION pos = GetFirstStrategyPosition();
  515. while (pos != NULL)
  516. {
  517. CStrategy* pStrategy = GetNextStrategy(pos);
  518. pStrategy->OnClose();
  519. delete pStrategy;
  520. }
  521. m_strategyList.RemoveAll();
  522. if (! bEndSession && m_bAutoUpdateViews)
  523. {
  524. //::SendMessage(AfxGetStrategyView()->GetSafeHwnd(), WM_USER_REMOVESTRATEGY, 0, 0);
  525. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_WIZARDVIEW, NULL);
  526. }
  527. }
  528. CRecentFileList * CStkUIApp::GetRecentFileList()
  529. {
  530. return m_pRecentFileList;
  531. }
  532. /////////////////////////////////////////////////////////////////////////////
  533. // CAboutDlg dialog used for App About
  534. class CAboutDlg : public CDialog
  535. {
  536. public:
  537. CAboutDlg();
  538. // Dialog Data
  539. //{{AFX_DATA(CAboutDlg)
  540. enum { IDD = IDD_ABOUTBOX };
  541. //}}AFX_DATA
  542. CButton m_btnOK;
  543. protected:
  544. //{{AFX_VIRTUAL(CAboutDlg)
  545. virtual void DoDataExchange(CDataExchange* pDX);
  546. //}}AFX_VIRTUAL
  547. // Implementation
  548. protected:
  549. //{{AFX_MSG(CAboutDlg)
  550. virtual BOOL OnInitDialog();
  551. //}}AFX_MSG
  552. DECLARE_MESSAGE_MAP()
  553. };
  554. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  555. {
  556. }
  557. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  558. {
  559. CDialog::DoDataExchange(pDX);
  560. DDX_Control(pDX, IDOK, m_btnOK);
  561. }
  562. BOOL CAboutDlg::OnInitDialog() 
  563. {
  564. return TRUE;
  565. }
  566. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  567. END_MESSAGE_MAP()
  568. // App command to run the dialog
  569. void CStkUIApp::OnAppAbout()
  570. {
  571. CAboutDlg aboutDlg;
  572. aboutDlg.DoModal();
  573. }
  574. /////////////////////////////////////////////////////////////////////////////
  575. // CStkUIApp message handlers
  576. int CStkUIApp::ExitInstance() 
  577. {
  578. // TODO: Add your specialized code here and/or call the base class
  579. SaveLastOpenedStrategy();
  580. CloseAllStrategy(TRUE);
  581. AfxGetDB().StoreCodetable(AfxGetStockContainer());
  582. AfxGetProfile().StoreProfile();
  583. AfxGetSView().Store(AfxGetProfile().GetSViewFile());
  584. AfxReleaseDB();
  585. if (m_pStaticDoc)
  586. {
  587. m_pStaticDoc->OnCloseDocument();
  588. delete m_pStaticDoc;
  589. m_pStaticDoc = NULL;
  590. }
  591. return CWinApp::ExitInstance();
  592. }
  593. void CStkUIApp::OnFileNew() 
  594. {
  595. // TODO: Add your command handler code here
  596. // OpenStrategyFile(NULL);
  597. }
  598. void CStkUIApp::OnFileOpen() 
  599. {
  600. // TODO: Add your command handler code here
  601. CString newName;
  602. if (m_pRecentFileList && m_pRecentFileList->GetSize() > 0)
  603. newName = (*m_pRecentFileList)[0];
  604. if (!AfxDoPromptFileName(newName, IDS_OPENFILE,
  605.   OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLESIZING, TRUE))
  606. return; // open cancelled
  607. if (NULL != OpenStrategyFile(newName))
  608. AfxSwitchToStaticView(RUNTIME_CLASS(CSimuView));
  609. // if returns NULL, the user has already been alerted
  610. }
  611. BOOL CStkUIApp::SaveAllModified() 
  612. {
  613. // TODO: Add your specialized code here and/or call the base class
  614. CStrategy * pActiveStrategy = AfxGetActiveStrategy();
  615. if (pActiveStrategy)
  616. pActiveStrategy->DoFileSave();
  617. /*
  618. POSITION pos = GetFirstStrategyPosition();
  619. while (pos != NULL)
  620. {
  621. CStrategy* pStrategy = GetNextStrategy(pos);
  622. if (!pStrategy->DoFileSave()) // >SaveModified())
  623. return FALSE;
  624. }
  625. */
  626. return TRUE;
  627. }
  628. void CStkUIApp::OnStrategyNew() 
  629. {
  630. // TODO: Add your command handler code here
  631. CStrategy * pStrategy = new CStrategy();
  632. if (NULL == pStrategy)
  633. {
  634. AfxMessageBox(IDS_STRATEGY_FAILED, MB_OK | MB_ICONINFORMATION);
  635. return;
  636. }
  637. CSetPrpt firstPage(NULL, IDS_STRATEGY_SETPRPT, NULL);
  638. firstPage.m_dwButtonFlags = PSWIZB_NEXT | PSWIZB_DISABLEDFINISH;
  639. firstPage.SetStrategy(pStrategy);
  640. CSelectStk selectStk(NULL, IDS_STRATEGY_SELECTSTK, NULL);
  641. selectStk.m_dwButtonFlags = PSWIZB_BACK | PSWIZB_NEXT | PSWIZB_DISABLEDFINISH;
  642. CSetRule setrule(NULL, IDS_STRATEGY_SETRULE, NULL);
  643. setrule.m_dwButtonFlags = PSWIZB_BACK | PSWIZB_NEXT | PSWIZB_DISABLEDFINISH;
  644. setrule.SetTechParameters(&(pStrategy->GetTechParametersBuy()), &(pStrategy->GetSelectedTechsBuy()));
  645. CSetRate setrate(NULL, IDS_STRATEGY_SETRATE, NULL);
  646. setrate.m_dwButtonFlags = PSWIZB_BACK | PSWIZB_FINISH;
  647. setrate.SetStrategy(pStrategy);
  648. CPropertySheet sht;
  649. sht.AddPage(&firstPage);
  650. sht.AddPage(&selectStk);
  651. sht.AddPage(&setrule);
  652. sht.AddPage(&setrate);
  653. sht.SetWizardMode();
  654. sht.m_psh.dwFlags |= (PSH_WIZARD97 | PSH_WIZARDHASFINISH /* | PSH_HEADER /*Header Bitmap*/);
  655. //CBitmap bmp;
  656. //bmp.LoadBitmap(IDB_BALANG);
  657. //sht.SetBitmap((HBITMAP)bmp.GetSafeHandle());
  658. if (ID_WIZFINISH == sht.DoModal())
  659. {
  660. pStrategy->SetStocks(selectStk.m_domainTemp);
  661. pStrategy->SaveStrategyFile(firstPage.GetPathName());
  662. AddStrategy(pStrategy);
  663. AfxSwitchToStaticView(RUNTIME_CLASS(CSimuView));
  664. }
  665. else
  666. {
  667. delete pStrategy;
  668. }
  669. }
  670. void CStkUIApp::OnStrategySave() 
  671. {
  672. // TODO: Add your command handler code here
  673. // CStrategy * pStrategy = GetActiveStrategy();
  674. // if (pStrategy)
  675. // pStrategy->DoFileSave();
  676. }
  677. void CStkUIApp::OnUpdateStrategySave(CCmdUI* pCmdUI) 
  678. {
  679. // TODO: Add your command update UI handler code here
  680. CStrategy * pActive = GetActiveStrategy();
  681. pCmdUI->Enable(FALSE); // NULL != pActiveStrategy);
  682. }
  683. void CStkUIApp::OnStrategySaveAs() 
  684. {
  685. // TODO: Add your command handler code here
  686. CStrategy * pStrategy = GetActiveStrategy();
  687. if (pStrategy)
  688. {
  689. CString newName = AfxGetFileTitleNoExt(pStrategy->GetPathName());
  690. // check for dubious filename
  691. int iBad = newName.FindOneOf(_T(" #%;/\"));
  692. if (iBad != -1)
  693. newName.ReleaseBuffer(iBad);
  694. // append the default suffix if there is one
  695. newName += AfxGetStrategyFileExt();
  696. if (!AfxDoPromptFileName(newName, IDS_SAVEFILE,
  697. OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_ENABLESIZING, FALSE))
  698. return;
  699. pStrategy->SetPathName(newName);
  700. pStrategy->SetName(AfxGetFileTitleNoExt(newName));
  701. pStrategy->DoFileSave();
  702. //AfxOnStrategyUpdated(pStrategy, FALSE);
  703. }
  704. }
  705. void CStkUIApp::OnUpdateStrategySaveAs(CCmdUI* pCmdUI) 
  706. {
  707. // TODO: Add your command update UI handler code here
  708. pCmdUI->Enable(NULL != GetActiveStrategy());
  709. }
  710. void CStkUIApp::OnStrategySaveall() 
  711. {
  712. // TODO: Add your command handler code here
  713. SaveAllModified();
  714. }
  715. void CStkUIApp::OnUpdateStrategySaveall(CCmdUI* pCmdUI) 
  716. {
  717. // TODO: Add your command update UI handler code here
  718. pCmdUI->Enable(NULL != GetFirstStrategyPosition());
  719. }
  720. void CStkUIApp::OnStrategyClose() 
  721. {
  722. // TODO: Add your command handler code here
  723. CSimuView * pSimuView = AfxGetSimuView();
  724. CStrategy * pActive = AfxGetActiveStrategy();
  725. //CStrategyView * pStrategyView = AfxGetStrategyView();
  726. CCloseStrategyDlg dlg;
  727. if (IDOK == dlg.DoModal())
  728. {
  729. BOOL bOldValue = m_bAutoUpdateViews;
  730. m_bAutoUpdateViews = FALSE;
  731. POSITION pos = dlg.GetFirstCloseStrategyPosition();
  732. while(NULL != pos)
  733. {
  734. CStrategy * pStrategy = dlg.GetNextCloseStrategy(pos);
  735. if (pSimuView && pStrategy == pActive && !pSimuView->CanChangeActiveStrategy())
  736. continue;
  737. //if (pStrategy == pActive && pStrategyView)
  738. //{
  739. // pStrategyView->SetActiveStrategy(NULL);
  740. // if (pSimuView)
  741. // pSimuView->OnActiveStrategyChanged();
  742. //}
  743. //RemoveStrategy(pStrategy);
  744. pStrategy->OnClose();
  745. delete pStrategy;
  746. }
  747. m_bAutoUpdateViews = bOldValue;
  748. if (m_bAutoUpdateViews)
  749. {
  750. //::SendMessage(AfxGetStrategyView()->GetSafeHwnd(), WM_USER_REMOVESTRATEGY, 0, 0);
  751. AfxGetStaticDoc()->UpdateAllViews(NULL, UPDATE_HINT_WIZARDVIEW, NULL);
  752. }
  753. }
  754. }
  755. void CStkUIApp::OnUpdateStrategyClose(CCmdUI* pCmdUI) 
  756. {
  757. // TODO: Add your command update UI handler code here
  758. pCmdUI->Enable(NULL != GetFirstStrategyPosition());
  759. }
  760. void CStkUIApp::WinHelp(DWORD dwData, UINT nCmd) 
  761. {
  762. // TODO: Add your specialized code here and/or call the base class
  763. // CWinApp::WinHelp(dwData, nCmd);
  764. }