CMakeSetupDialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // pcbuilderdialogDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CMakeSetup.h"
  5. #include "CMakeSetupDialog.h"
  6. #include "../cmDSWMakefile.h"
  7. #include "../cmMSProjectGenerator.h"
  8. #include "../cmCacheManager.h"
  9. #include "../cmMakefile.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. //{{AFX_DATA(CAboutDlg)
  23. enum { IDD = IDD_ABOUTBOX };
  24. //}}AFX_DATA
  25. // ClassWizard generated virtual function overrides
  26. //{{AFX_VIRTUAL(CAboutDlg)
  27. protected:
  28. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  29. //}}AFX_VIRTUAL
  30. // Implementation
  31. protected:
  32. //{{AFX_MSG(CAboutDlg)
  33. //}}AFX_MSG
  34. DECLARE_MESSAGE_MAP()
  35. };
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAboutDlg)
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  48. //{{AFX_MSG_MAP(CAboutDlg)
  49. // No message handlers
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP();
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CMakeSetupDialog dialog
  54. CMakeSetupDialog::CMakeSetupDialog(CWnd* pParent /*=NULL*/)
  55. : CDialog(CMakeSetupDialog::IDD, pParent)
  56. {
  57. CString startPath = _pgmptr;
  58. startPath.Replace('\\', '_');
  59. startPath.Replace(':', '_');
  60. startPath.Replace(".EXE", "");
  61. startPath.Replace(".exe", "");
  62. m_RegistryKey = "Software\\Kitware\\CMakeSetup\\Settings\\";
  63. // _pgmptr should be the directory from which cmake was run from
  64. // use it as the unique key for the dialog
  65. m_RegistryKey += startPath;
  66. //{{AFX_DATA_INIT(CMakeSetupDialog)
  67. m_WhereSource = _T("");
  68. m_WhereBuild = _T("");
  69. //}}AFX_DATA_INIT
  70. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  71. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  72. // Guess the initial source directory based on the location
  73. // of this program, it should be in CMake/Source/
  74. startPath = _pgmptr;
  75. int removePos = startPath.Find("\\CMake\\Source");
  76. if(removePos == -1)
  77. {
  78. removePos = startPath.Find("/CMake/Source");
  79. }
  80. if(removePos != -1)
  81. {
  82. startPath = startPath.Left(removePos);
  83. }
  84. m_WhereSource = startPath;
  85. this->LoadFromRegistry();
  86. m_BuildPathChanged = false;
  87. }
  88. void CMakeSetupDialog::DoDataExchange(CDataExchange* pDX)
  89. {
  90. CDialog::DoDataExchange(pDX);
  91. //{{AFX_DATA_MAP(CMakeSetupDialog)
  92. DDX_Control(pDX, IDC_LIST2, m_CacheEntriesList);
  93. DDX_Text(pDX, IDC_WhereSource, m_WhereSource);
  94. DDX_Text(pDX, IDC_WhereBuild, m_WhereBuild);
  95. //}}AFX_DATA_MAP
  96. }
  97. BEGIN_MESSAGE_MAP(CMakeSetupDialog, CDialog)
  98. //{{AFX_MSG_MAP(CMakeSetupDialog)
  99. ON_WM_SYSCOMMAND()
  100. ON_WM_PAINT()
  101. ON_WM_QUERYDRAGICON()
  102. ON_BN_CLICKED(IDC_BUTTON2, OnBrowseWhereSource)
  103. ON_BN_CLICKED(IDC_BUTTON3, OnBrowseWhereBuild)
  104. ON_BN_CLICKED(IDC_BuildProjects, OnBuildProjects)
  105. ON_EN_CHANGE(IDC_WhereBuild, OnChangeWhereBuild)
  106. ON_EN_CHANGE(IDC_WhereSource, OnChangeWhereSource)
  107. //}}AFX_MSG_MAP
  108. END_MESSAGE_MAP()
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CMakeSetupDialog message handlers
  111. BOOL CMakeSetupDialog::OnInitDialog()
  112. {
  113. CDialog::OnInitDialog();
  114. // Add "About..." menu item to system menu.
  115. // IDM_ABOUTBOX must be in the system command range.
  116. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  117. ASSERT(IDM_ABOUTBOX < 0xF000);
  118. CMenu* pSysMenu = GetSystemMenu(FALSE);
  119. if (pSysMenu != NULL)
  120. {
  121. CString strAboutMenu;
  122. strAboutMenu.LoadString(IDS_ABOUTBOX);
  123. if (!strAboutMenu.IsEmpty())
  124. {
  125. pSysMenu->AppendMenu(MF_SEPARATOR);
  126. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  127. }
  128. }
  129. // Set the icon for this dialog. The framework does this automatically
  130. // when the application's main window is not a dialog
  131. SetIcon(m_hIcon, TRUE); // Set big icon
  132. SetIcon(m_hIcon, FALSE); // Set small icon
  133. this->LoadCacheFromDiskToGUI();
  134. return TRUE; // return TRUE unless you set the focus to a control
  135. }
  136. void CMakeSetupDialog::OnSysCommand(UINT nID, LPARAM lParam)
  137. {
  138. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  139. {
  140. CAboutDlg dlgAbout;
  141. dlgAbout.DoModal();
  142. }
  143. else
  144. {
  145. CDialog::OnSysCommand(nID, lParam);
  146. }
  147. }
  148. // If you add a minimize button to your dialog, you will need the code below
  149. // to draw the icon. For MFC applications using the document/view model,
  150. // this is automatically done for you by the framework.
  151. void CMakeSetupDialog::OnPaint()
  152. {
  153. if (IsIconic())
  154. {
  155. CPaintDC dc(this); // device context for painting
  156. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  157. // Center icon in client rectangle
  158. int cxIcon = GetSystemMetrics(SM_CXICON);
  159. int cyIcon = GetSystemMetrics(SM_CYICON);
  160. CRect rect;
  161. GetClientRect(&rect);
  162. int x = (rect.Width() - cxIcon + 1) / 2;
  163. int y = (rect.Height() - cyIcon + 1) / 2;
  164. // Draw the icon
  165. dc.DrawIcon(x, y, m_hIcon);
  166. }
  167. else
  168. {
  169. CDialog::OnPaint();
  170. }
  171. }
  172. // The system calls this to obtain the cursor to display while the user drags
  173. // the minimized window.
  174. HCURSOR CMakeSetupDialog::OnQueryDragIcon()
  175. {
  176. return (HCURSOR) m_hIcon;
  177. }
  178. void CMakeSetupDialog::OnBrowseWhereSource()
  179. {
  180. this->UpdateData();
  181. Browse(m_WhereSource, "Enter Path to Insight Source");
  182. this->UpdateData(false);
  183. }
  184. bool CMakeSetupDialog::Browse(CString &result, const char *title)
  185. {
  186. // don't know what to do with initial right now...
  187. char szPathName[4096];
  188. BROWSEINFO bi;
  189. bi.hwndOwner = m_hWnd;
  190. bi.pidlRoot = NULL;
  191. bi.pszDisplayName = (LPTSTR)szPathName;
  192. bi.lpszTitle = title;
  193. bi.ulFlags = BIF_BROWSEINCLUDEFILES ;
  194. bi.lpfn = NULL;
  195. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  196. bool bSuccess = (SHGetPathFromIDList(pidl, szPathName) ? true : false);
  197. if(bSuccess)
  198. {
  199. result = szPathName;
  200. }
  201. return bSuccess;
  202. }
  203. void CMakeSetupDialog::OnBrowseWhereBuild()
  204. {
  205. this->UpdateData();
  206. Browse(m_WhereBuild, "Enter Path to Insight Build");
  207. this->UpdateData(false);
  208. }
  209. void CMakeSetupDialog::SaveToRegistry()
  210. {
  211. HKEY hKey;
  212. DWORD dwDummy;
  213. if(RegCreateKeyEx(HKEY_CURRENT_USER,
  214. m_RegistryKey,
  215. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  216. NULL, &hKey, &dwDummy) != ERROR_SUCCESS)
  217. {
  218. return;
  219. }
  220. else
  221. {
  222. RegSetValueEx(hKey, _T("WhereSource"), 0, REG_SZ,
  223. (CONST BYTE *)(const char *)m_WhereSource,
  224. m_WhereSource.GetLength());
  225. RegSetValueEx(hKey, _T("WhereBuild"), 0, REG_SZ,
  226. (CONST BYTE *)(const char *)m_WhereBuild,
  227. m_WhereBuild.GetLength());
  228. }
  229. RegCloseKey(hKey);
  230. }
  231. void CMakeSetupDialog::ReadRegistryValue(HKEY hKey,
  232. CString *val,
  233. const char *key,
  234. const char *adefault)
  235. {
  236. DWORD dwType, dwSize;
  237. char *pb;
  238. dwType = REG_SZ;
  239. pb = val->GetBuffer(MAX_PATH);
  240. dwSize = MAX_PATH;
  241. if(RegQueryValueEx(hKey,_T(key), NULL, &dwType,
  242. (BYTE *)pb, &dwSize) != ERROR_SUCCESS)
  243. {
  244. val->ReleaseBuffer();
  245. *val = _T(adefault);
  246. }
  247. else
  248. {
  249. val->ReleaseBuffer();
  250. }
  251. }
  252. void CMakeSetupDialog::LoadFromRegistry()
  253. {
  254. HKEY hKey;
  255. if(RegOpenKeyEx(HKEY_CURRENT_USER,
  256. m_RegistryKey,
  257. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  258. {
  259. return;
  260. }
  261. else
  262. {
  263. // save some values
  264. this->ReadRegistryValue(hKey, &(m_WhereSource),"WhereSource","C:\\Insight");
  265. this->ReadRegistryValue(hKey, &(m_WhereBuild),"WhereBuild",
  266. "C:\\Insight");
  267. }
  268. RegCloseKey(hKey);
  269. }
  270. void CMakeSetupDialog::OnBuildProjects()
  271. {
  272. ::SetCursor(LoadCursor(NULL, IDC_WAIT));
  273. // get all the info from the screen
  274. this->UpdateData();
  275. if(!m_BuildPathChanged)
  276. {
  277. // if the build path has not changed save the
  278. // current GUI values to the cache
  279. this->SaveCacheFromGUI();
  280. }
  281. // Make sure we are working from the cache on disk
  282. this->LoadCacheFromDiskToGUI();
  283. // Create a makefile object
  284. cmMakefile makefile;
  285. makefile.SetMakefileGenerator(new cmMSProjectGenerator);
  286. makefile.SetHomeDirectory(m_WhereSource);
  287. makefile.SetStartOutputDirectory(m_WhereBuild);
  288. makefile.SetHomeOutputDirectory(m_WhereBuild);
  289. makefile.SetStartDirectory(m_WhereSource);
  290. makefile.MakeStartDirectoriesCurrent();
  291. CString makefileIn = m_WhereSource;
  292. makefileIn += "/CMakeLists.txt";
  293. makefile.ReadListFile(makefileIn);
  294. // Generate the project files
  295. makefile.GenerateMakefile();
  296. // Save the cache
  297. cmCacheManager::GetInstance()->SaveCache(&makefile);
  298. // update the GUI with any new values in the caused by the
  299. // generation process
  300. this->LoadCacheFromDiskToGUI();
  301. // save source and build paths to registry
  302. this->SaveToRegistry();
  303. // path is not up-to-date
  304. m_BuildPathChanged = false;
  305. ::SetCursor(LoadCursor(NULL, IDC_ARROW));
  306. }
  307. // copy from the cache manager to the cache edit list box
  308. void CMakeSetupDialog::FillCacheGUIFromCacheManager()
  309. {
  310. // Clear the current GUI
  311. m_CacheEntriesList.RemoveAll();
  312. const cmCacheManager::CacheEntryMap &cache =
  313. cmCacheManager::GetInstance()->GetCacheMap();
  314. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  315. i != cache.end(); ++i)
  316. {
  317. const char* key = i->first.c_str();
  318. const cmCacheManager::CacheEntry& value = i->second;
  319. switch(value.m_Type )
  320. {
  321. case cmCacheManager::BOOL:
  322. if(cmCacheManager::GetInstance()->IsOn(key))
  323. {
  324. m_CacheEntriesList.AddProperty(key,
  325. "ON",
  326. CPropertyList::CHECKBOX,"");
  327. }
  328. else
  329. {
  330. m_CacheEntriesList.AddProperty(key,
  331. "OFF",
  332. CPropertyList::CHECKBOX,"");
  333. }
  334. break;
  335. case cmCacheManager::PATH:
  336. m_CacheEntriesList.AddProperty(key, value.m_Value.c_str(),
  337. CPropertyList::PATH,"");
  338. break;
  339. case cmCacheManager::FILEPATH:
  340. m_CacheEntriesList.AddProperty(key, value.m_Value.c_str(),
  341. CPropertyList::FILE,"");
  342. break;
  343. case cmCacheManager::STRING:
  344. m_CacheEntriesList.AddProperty(key, value.m_Value.c_str(),
  345. CPropertyList::EDIT,"");
  346. break;
  347. case cmCacheManager::INTERNAL:
  348. break;
  349. }
  350. }
  351. this->UpdateData(FALSE);
  352. }
  353. // copy from the list box to the cache manager
  354. void CMakeSetupDialog::FillCacheManagerFromCacheGUI()
  355. {
  356. cmCacheManager::GetInstance()->GetCacheMap();
  357. std::set<CPropertyItem*> items = m_CacheEntriesList.GetItems();
  358. for(std::set<CPropertyItem*>::iterator i = items.begin();
  359. i != items.end(); ++i)
  360. {
  361. CPropertyItem* item = *i;
  362. // check to see if the editor has removed the cache entry
  363. if(item->m_Removed)
  364. {
  365. cmCacheManager::GetInstance()->RemoveCacheEntry((*i)->m_propName);
  366. }
  367. else
  368. {
  369. cmCacheManager::CacheEntry *entry =
  370. cmCacheManager::GetInstance()->GetCacheEntry((const char*)item->m_propName);
  371. if (entry)
  372. {
  373. entry->m_Value = item->m_curValue;
  374. }
  375. }
  376. }
  377. }
  378. void CMakeSetupDialog::OnChangeWhereBuild()
  379. {
  380. this->UpdateData();
  381. std::string cachefile = m_WhereBuild;
  382. cachefile += "/CMakeCache.txt";
  383. if(cmSystemTools::FileExists(cachefile.c_str()))
  384. {
  385. m_CacheEntriesList.ShowWindow(SW_SHOW);
  386. this->LoadCacheFromDiskToGUI();
  387. m_BuildPathChanged = true;
  388. }
  389. else
  390. {
  391. m_CacheEntriesList.RemoveAll();
  392. }
  393. }
  394. void CMakeSetupDialog::OnChangeWhereSource()
  395. {
  396. this->UpdateData();
  397. }
  398. //! Load cache file from m_WhereBuild and display in GUI editor
  399. void CMakeSetupDialog::LoadCacheFromDiskToGUI()
  400. {
  401. if(m_WhereBuild != "")
  402. {
  403. cmCacheManager::GetInstance()->LoadCache(m_WhereBuild);
  404. this->FillCacheGUIFromCacheManager();
  405. }
  406. }
  407. //! Save GUI values to cmCacheManager and then save to disk.
  408. void CMakeSetupDialog::SaveCacheFromGUI()
  409. {
  410. this->FillCacheManagerFromCacheGUI();
  411. if(m_WhereBuild != "")
  412. {
  413. cmCacheManager::GetInstance()->SaveCache(m_WhereBuild);
  414. }
  415. }