CMakeSetupDialog.cpp 13 KB

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