CMakeSetupDialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. m_WhereBuild = startPath;
  86. this->LoadFromRegistry();
  87. m_BuildPathChanged = false;
  88. }
  89. void CMakeSetupDialog::DoDataExchange(CDataExchange* pDX)
  90. {
  91. CDialog::DoDataExchange(pDX);
  92. //{{AFX_DATA_MAP(CMakeSetupDialog)
  93. DDX_Control(pDX, IDC_LIST2, m_CacheEntriesList);
  94. DDX_Text(pDX, IDC_WhereSource, m_WhereSource);
  95. DDX_Text(pDX, IDC_WhereBuild, m_WhereBuild);
  96. //}}AFX_DATA_MAP
  97. }
  98. BEGIN_MESSAGE_MAP(CMakeSetupDialog, CDialog)
  99. //{{AFX_MSG_MAP(CMakeSetupDialog)
  100. ON_WM_SYSCOMMAND()
  101. ON_WM_PAINT()
  102. ON_WM_QUERYDRAGICON()
  103. ON_BN_CLICKED(IDC_BUTTON2, OnBrowseWhereSource)
  104. ON_BN_CLICKED(IDC_BUTTON3, OnBrowseWhereBuild)
  105. ON_BN_CLICKED(IDC_BuildProjects, OnBuildProjects)
  106. ON_EN_CHANGE(IDC_WhereBuild, OnChangeWhereBuild)
  107. ON_EN_CHANGE(IDC_WhereSource, OnChangeWhereSource)
  108. //}}AFX_MSG_MAP
  109. END_MESSAGE_MAP()
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CMakeSetupDialog message handlers
  112. BOOL CMakeSetupDialog::OnInitDialog()
  113. {
  114. CDialog::OnInitDialog();
  115. // Add "About..." menu item to system menu.
  116. // IDM_ABOUTBOX must be in the system command range.
  117. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  118. ASSERT(IDM_ABOUTBOX < 0xF000);
  119. CMenu* pSysMenu = GetSystemMenu(FALSE);
  120. if (pSysMenu != NULL)
  121. {
  122. CString strAboutMenu;
  123. strAboutMenu.LoadString(IDS_ABOUTBOX);
  124. if (!strAboutMenu.IsEmpty())
  125. {
  126. pSysMenu->AppendMenu(MF_SEPARATOR);
  127. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  128. }
  129. }
  130. // Set the icon for this dialog. The framework does this automatically
  131. // when the application's main window is not a dialog
  132. SetIcon(m_hIcon, TRUE); // Set big icon
  133. SetIcon(m_hIcon, FALSE); // Set small icon
  134. this->LoadCacheFromDiskToGUI();
  135. return TRUE; // return TRUE unless you set the focus to a control
  136. }
  137. void CMakeSetupDialog::OnSysCommand(UINT nID, LPARAM lParam)
  138. {
  139. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  140. {
  141. CAboutDlg dlgAbout;
  142. dlgAbout.DoModal();
  143. }
  144. else
  145. {
  146. CDialog::OnSysCommand(nID, lParam);
  147. }
  148. }
  149. // If you add a minimize button to your dialog, you will need the code below
  150. // to draw the icon. For MFC applications using the document/view model,
  151. // this is automatically done for you by the framework.
  152. void CMakeSetupDialog::OnPaint()
  153. {
  154. if (IsIconic())
  155. {
  156. CPaintDC dc(this); // device context for painting
  157. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  158. // Center icon in client rectangle
  159. int cxIcon = GetSystemMetrics(SM_CXICON);
  160. int cyIcon = GetSystemMetrics(SM_CYICON);
  161. CRect rect;
  162. GetClientRect(&rect);
  163. int x = (rect.Width() - cxIcon + 1) / 2;
  164. int y = (rect.Height() - cyIcon + 1) / 2;
  165. // Draw the icon
  166. dc.DrawIcon(x, y, m_hIcon);
  167. }
  168. else
  169. {
  170. CDialog::OnPaint();
  171. }
  172. }
  173. // The system calls this to obtain the cursor to display while the user drags
  174. // the minimized window.
  175. HCURSOR CMakeSetupDialog::OnQueryDragIcon()
  176. {
  177. return (HCURSOR) m_hIcon;
  178. }
  179. void CMakeSetupDialog::OnBrowseWhereSource()
  180. {
  181. this->UpdateData();
  182. Browse(m_WhereSource, "Enter Path to Insight Source");
  183. this->UpdateData(false);
  184. }
  185. bool CMakeSetupDialog::Browse(CString &result, const char *title)
  186. {
  187. // don't know what to do with initial right now...
  188. char szPathName[4096];
  189. BROWSEINFO bi;
  190. bi.hwndOwner = m_hWnd;
  191. bi.pidlRoot = NULL;
  192. bi.pszDisplayName = (LPTSTR)szPathName;
  193. bi.lpszTitle = title;
  194. bi.ulFlags = BIF_BROWSEINCLUDEFILES ;
  195. bi.lpfn = NULL;
  196. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  197. bool bSuccess = (SHGetPathFromIDList(pidl, szPathName) ? true : false);
  198. if(bSuccess)
  199. {
  200. result = szPathName;
  201. }
  202. return bSuccess;
  203. }
  204. void CMakeSetupDialog::OnBrowseWhereBuild()
  205. {
  206. this->UpdateData();
  207. Browse(m_WhereBuild, "Enter Path to Insight Build");
  208. this->UpdateData(false);
  209. }
  210. void CMakeSetupDialog::SaveToRegistry()
  211. {
  212. HKEY hKey;
  213. DWORD dwDummy;
  214. if(RegCreateKeyEx(HKEY_CURRENT_USER,
  215. m_RegistryKey,
  216. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  217. NULL, &hKey, &dwDummy) != ERROR_SUCCESS)
  218. {
  219. return;
  220. }
  221. else
  222. {
  223. RegSetValueEx(hKey, _T("WhereSource"), 0, REG_SZ,
  224. (CONST BYTE *)(const char *)m_WhereSource,
  225. m_WhereSource.GetLength());
  226. RegSetValueEx(hKey, _T("WhereBuild"), 0, REG_SZ,
  227. (CONST BYTE *)(const char *)m_WhereBuild,
  228. m_WhereBuild.GetLength());
  229. }
  230. RegCloseKey(hKey);
  231. }
  232. void CMakeSetupDialog::ReadRegistryValue(HKEY hKey,
  233. CString *val,
  234. const char *key,
  235. const char *adefault)
  236. {
  237. DWORD dwType, dwSize;
  238. char *pb;
  239. dwType = REG_SZ;
  240. pb = val->GetBuffer(MAX_PATH);
  241. dwSize = MAX_PATH;
  242. if(RegQueryValueEx(hKey,_T(key), NULL, &dwType,
  243. (BYTE *)pb, &dwSize) != ERROR_SUCCESS)
  244. {
  245. val->ReleaseBuffer();
  246. *val = _T(adefault);
  247. }
  248. else
  249. {
  250. val->ReleaseBuffer();
  251. }
  252. }
  253. void CMakeSetupDialog::LoadFromRegistry()
  254. {
  255. HKEY hKey;
  256. if(RegOpenKeyEx(HKEY_CURRENT_USER,
  257. m_RegistryKey,
  258. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  259. {
  260. return;
  261. }
  262. else
  263. {
  264. // save some values
  265. this->ReadRegistryValue(hKey, &(m_WhereSource),"WhereSource","C:\\Insight");
  266. this->ReadRegistryValue(hKey, &(m_WhereBuild),"WhereBuild",
  267. "C:\\Insight");
  268. }
  269. RegCloseKey(hKey);
  270. }
  271. void CMakeSetupDialog::OnBuildProjects()
  272. {
  273. if(!cmSystemTools::FileExists(m_WhereBuild))
  274. {
  275. std::string message =
  276. "Build directory does not exist, should I create it?\n\n"
  277. "Directory: ";
  278. message += (const char*)m_WhereBuild;
  279. if(MessageBox(message.c_str(), "Create Directory", MB_OKCANCEL) == IDOK)
  280. {
  281. cmSystemTools::MakeDirectory(m_WhereBuild);
  282. }
  283. else
  284. {
  285. MessageBox("Build Project aborted, nothing done.");
  286. return;
  287. }
  288. }
  289. ::SetCursor(LoadCursor(NULL, IDC_WAIT));
  290. // get all the info from the screen
  291. this->UpdateData();
  292. if(!m_BuildPathChanged)
  293. {
  294. // if the build path has not changed save the
  295. // current GUI values to the cache
  296. this->SaveCacheFromGUI();
  297. }
  298. // Make sure we are working from the cache on disk
  299. this->LoadCacheFromDiskToGUI();
  300. // Create a makefile object
  301. cmMakefile makefile;
  302. makefile.SetMakefileGenerator(new cmMSProjectGenerator);
  303. makefile.SetHomeDirectory(m_WhereSource);
  304. makefile.SetStartOutputDirectory(m_WhereBuild);
  305. makefile.SetHomeOutputDirectory(m_WhereBuild);
  306. makefile.SetStartDirectory(m_WhereSource);
  307. makefile.MakeStartDirectoriesCurrent();
  308. CString makefileIn = m_WhereSource;
  309. makefileIn += "/CMakeLists.txt";
  310. makefile.ReadListFile(makefileIn);
  311. // Generate the project files
  312. makefile.GenerateMakefile();
  313. // Save the cache
  314. cmCacheManager::GetInstance()->SaveCache(&makefile);
  315. // update the GUI with any new values in the caused by the
  316. // generation process
  317. this->LoadCacheFromDiskToGUI();
  318. // save source and build paths to registry
  319. this->SaveToRegistry();
  320. // path is not up-to-date
  321. m_BuildPathChanged = false;
  322. ::SetCursor(LoadCursor(NULL, IDC_ARROW));
  323. }
  324. // copy from the cache manager to the cache edit list box
  325. void CMakeSetupDialog::FillCacheGUIFromCacheManager()
  326. {
  327. // Clear the current GUI
  328. m_CacheEntriesList.RemoveAll();
  329. const cmCacheManager::CacheEntryMap &cache =
  330. cmCacheManager::GetInstance()->GetCacheMap();
  331. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  332. i != cache.end(); ++i)
  333. {
  334. const char* key = i->first.c_str();
  335. const cmCacheManager::CacheEntry& value = i->second;
  336. switch(value.m_Type )
  337. {
  338. case cmCacheManager::BOOL:
  339. if(cmCacheManager::GetInstance()->IsOn(key))
  340. {
  341. m_CacheEntriesList.AddProperty(key,
  342. "ON",
  343. value.m_HelpString.c_str(),
  344. CPropertyList::CHECKBOX,"");
  345. }
  346. else
  347. {
  348. m_CacheEntriesList.AddProperty(key,
  349. "OFF",
  350. value.m_HelpString.c_str(),
  351. CPropertyList::CHECKBOX,"");
  352. }
  353. break;
  354. case cmCacheManager::PATH:
  355. m_CacheEntriesList.AddProperty(key,
  356. value.m_Value.c_str(),
  357. value.m_HelpString.c_str(),
  358. CPropertyList::PATH,"");
  359. break;
  360. case cmCacheManager::FILEPATH:
  361. m_CacheEntriesList.AddProperty(key,
  362. value.m_Value.c_str(),
  363. value.m_HelpString.c_str(),
  364. CPropertyList::FILE,"");
  365. break;
  366. case cmCacheManager::STRING:
  367. m_CacheEntriesList.AddProperty(key,
  368. value.m_Value.c_str(),
  369. value.m_HelpString.c_str(),
  370. CPropertyList::EDIT,"");
  371. break;
  372. case cmCacheManager::INTERNAL:
  373. break;
  374. }
  375. }
  376. this->UpdateData(FALSE);
  377. }
  378. // copy from the list box to the cache manager
  379. void CMakeSetupDialog::FillCacheManagerFromCacheGUI()
  380. {
  381. cmCacheManager::GetInstance()->GetCacheMap();
  382. std::set<CPropertyItem*> items = m_CacheEntriesList.GetItems();
  383. for(std::set<CPropertyItem*>::iterator i = items.begin();
  384. i != items.end(); ++i)
  385. {
  386. CPropertyItem* item = *i;
  387. // check to see if the editor has removed the cache entry
  388. if(item->m_Removed)
  389. {
  390. cmCacheManager::GetInstance()->RemoveCacheEntry((*i)->m_propName);
  391. }
  392. else
  393. {
  394. cmCacheManager::CacheEntry *entry =
  395. cmCacheManager::GetInstance()->GetCacheEntry((const char*)item->m_propName);
  396. if (entry)
  397. {
  398. entry->m_Value = item->m_curValue;
  399. }
  400. }
  401. }
  402. }
  403. void CMakeSetupDialog::OnChangeWhereBuild()
  404. {
  405. this->UpdateData();
  406. std::string cachefile = m_WhereBuild;
  407. cachefile += "/CMakeCache.txt";
  408. if(cmSystemTools::FileExists(cachefile.c_str()))
  409. {
  410. m_CacheEntriesList.ShowWindow(SW_SHOW);
  411. this->LoadCacheFromDiskToGUI();
  412. m_BuildPathChanged = true;
  413. }
  414. else
  415. {
  416. m_CacheEntriesList.RemoveAll();
  417. }
  418. }
  419. void CMakeSetupDialog::OnChangeWhereSource()
  420. {
  421. this->UpdateData();
  422. }
  423. //! Load cache file from m_WhereBuild and display in GUI editor
  424. void CMakeSetupDialog::LoadCacheFromDiskToGUI()
  425. {
  426. if(m_WhereBuild != "")
  427. {
  428. cmCacheManager::GetInstance()->LoadCache(m_WhereBuild);
  429. this->FillCacheGUIFromCacheManager();
  430. }
  431. }
  432. //! Save GUI values to cmCacheManager and then save to disk.
  433. void CMakeSetupDialog::SaveCacheFromGUI()
  434. {
  435. this->FillCacheManagerFromCacheGUI();
  436. if(m_WhereBuild != "")
  437. {
  438. cmCacheManager::GetInstance()->SaveCache(m_WhereBuild);
  439. }
  440. }