CMakeSetupDialog.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // pcbuilderdialogDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CMakeSetup.h"
  5. #include "PathDialog.h"
  6. #include "CMakeSetupDialog.h"
  7. #include "../cmCacheManager.h"
  8. #include "../cmake.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16. class CAboutDlg : public CDialog
  17. {
  18. public:
  19. CAboutDlg();
  20. // Dialog Data
  21. //{{AFX_DATA(CAboutDlg)
  22. enum { IDD = IDD_ABOUTBOX };
  23. //}}AFX_DATA
  24. // ClassWizard generated virtual function overrides
  25. //{{AFX_VIRTUAL(CAboutDlg)
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  28. //}}AFX_VIRTUAL
  29. // Implementation
  30. protected:
  31. //{{AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{{AFX_DATA_INIT(CAboutDlg)
  38. //}}AFX_DATA_INIT
  39. }
  40. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CAboutDlg)
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  47. //{{AFX_MSG_MAP(CAboutDlg)
  48. // No message handlers
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP();
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CMakeSetupDialog dialog
  53. CMakeSetupDialog::CMakeSetupDialog(CWnd* pParent /*=NULL*/)
  54. : CDialog(CMakeSetupDialog::IDD, pParent)
  55. {
  56. m_RegistryKey = "Software\\Kitware\\CMakeSetup\\Settings\\StartPath";
  57. //{{AFX_DATA_INIT(CMakeSetupDialog)
  58. m_WhereBuild = _T("");
  59. m_WhereSource = _T("");
  60. //}}AFX_DATA_INIT
  61. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  62. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  63. m_BuildPathChanged = false;
  64. // Find the path to the cmake.exe executable
  65. char fname[1024];
  66. ::GetModuleFileName(NULL,fname,1023);
  67. // extract just the path part
  68. m_PathToExecutable = cmSystemTools::GetProgramPath(fname).c_str();
  69. // add the cmake.exe to the path
  70. m_PathToExecutable += "/cmake.exe";
  71. }
  72. void CMakeSetupDialog::DoDataExchange(CDataExchange* pDX)
  73. {
  74. CDialog::DoDataExchange(pDX);
  75. //{{AFX_DATA_MAP(CMakeSetupDialog)
  76. DDX_Control(pDX, IDC_WhereSource, m_WhereSourceControl);
  77. DDX_Control(pDX, IDC_WhereBuild, m_WhereBuildControl);
  78. DDX_Control(pDX, IDC_LIST2, m_CacheEntriesList);
  79. DDX_CBString(pDX, IDC_WhereBuild, m_WhereBuild);
  80. DDX_CBString(pDX, IDC_WhereSource, m_WhereSource);
  81. //}}AFX_DATA_MAP
  82. }
  83. BEGIN_MESSAGE_MAP(CMakeSetupDialog, CDialog)
  84. //{{AFX_MSG_MAP(CMakeSetupDialog)
  85. ON_WM_SYSCOMMAND()
  86. ON_WM_PAINT()
  87. ON_WM_QUERYDRAGICON()
  88. ON_BN_CLICKED(IDC_BuildProjects, OnBuildProjects)
  89. ON_BN_CLICKED(IDC_BUTTON2, OnBrowseWhereSource)
  90. ON_BN_CLICKED(IDC_BUTTON3, OnBrowseWhereBuild)
  91. ON_CBN_EDITCHANGE(IDC_WhereBuild, OnChangeWhereBuild)
  92. ON_CBN_SELCHANGE(IDC_WhereBuild, OnSelendokWhereBuild)
  93. ON_CBN_SELENDOK(IDC_WhereSource, OnSelendokWhereSource)
  94. ON_CBN_EDITCHANGE(IDC_WhereSource, OnChangeWhereSource)
  95. //}}AFX_MSG_MAP
  96. END_MESSAGE_MAP()
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CMakeSetupDialog message handlers
  99. BOOL CMakeSetupDialog::OnInitDialog()
  100. {
  101. CDialog::OnInitDialog();
  102. // Add "About..." menu item to system menu.
  103. // IDM_ABOUTBOX must be in the system command range.
  104. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  105. ASSERT(IDM_ABOUTBOX < 0xF000);
  106. CMenu* pSysMenu = GetSystemMenu(FALSE);
  107. if (pSysMenu != NULL)
  108. {
  109. CString strAboutMenu;
  110. strAboutMenu.LoadString(IDS_ABOUTBOX);
  111. if (!strAboutMenu.IsEmpty())
  112. {
  113. pSysMenu->AppendMenu(MF_SEPARATOR);
  114. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  115. }
  116. }
  117. // Set the icon for this dialog. The framework does this automatically
  118. // when the application's main window is not a dialog
  119. SetIcon(m_hIcon, TRUE); // Set big icon
  120. SetIcon(m_hIcon, FALSE); // Set small icon
  121. // Load source and build dirs from registry
  122. this->LoadFromRegistry();
  123. // try to load the cmake cache from disk
  124. this->LoadCacheFromDiskToGUI();
  125. m_WhereBuildControl.LimitText(2048);
  126. m_WhereSourceControl.LimitText(2048);
  127. // Set the version number
  128. char tmp[1024];
  129. sprintf(tmp,"Version %s", cmMakefile::GetVersion());
  130. SetDlgItemText(IDC_CMAKE_VERSION, tmp);
  131. this->UpdateData(FALSE);
  132. return TRUE; // return TRUE unless you set the focus to a control
  133. }
  134. // About dialog invoke
  135. void CMakeSetupDialog::OnSysCommand(UINT nID, LPARAM lParam)
  136. {
  137. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  138. {
  139. CAboutDlg dlgAbout;
  140. dlgAbout.DoModal();
  141. }
  142. else
  143. {
  144. CDialog::OnSysCommand(nID, lParam);
  145. }
  146. }
  147. // If you add a minimize button to your dialog, you will need the code below
  148. // to draw the icon. For MFC applications using the document/view model,
  149. // this is automatically done for you by the framework.
  150. void CMakeSetupDialog::OnPaint()
  151. {
  152. if (IsIconic())
  153. {
  154. CPaintDC dc(this); // device context for painting
  155. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  156. // Center icon in client rectangle
  157. int cxIcon = GetSystemMetrics(SM_CXICON);
  158. int cyIcon = GetSystemMetrics(SM_CYICON);
  159. CRect rect;
  160. GetClientRect(&rect);
  161. int x = (rect.Width() - cxIcon + 1) / 2;
  162. int y = (rect.Height() - cyIcon + 1) / 2;
  163. // Draw the icon
  164. dc.DrawIcon(x, y, m_hIcon);
  165. }
  166. else
  167. {
  168. CDialog::OnPaint();
  169. }
  170. }
  171. // The system calls this to obtain the cursor to display while the user drags
  172. // the minimized window.
  173. HCURSOR CMakeSetupDialog::OnQueryDragIcon()
  174. {
  175. return (HCURSOR) m_hIcon;
  176. }
  177. // Browse button
  178. bool CMakeSetupDialog::Browse(CString &result, const char *title)
  179. {
  180. CPathDialog dlg("Select Path", title, result);
  181. if(dlg.DoModal()==IDOK)
  182. {
  183. result = dlg.GetPathName();
  184. return true;
  185. }
  186. else
  187. {
  188. return false;
  189. }
  190. }
  191. void CMakeSetupDialog::SaveToRegistry()
  192. {
  193. HKEY hKey;
  194. DWORD dwDummy;
  195. if(RegCreateKeyEx(HKEY_CURRENT_USER,
  196. m_RegistryKey,
  197. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  198. NULL, &hKey, &dwDummy) != ERROR_SUCCESS)
  199. {
  200. return;
  201. }
  202. else
  203. {
  204. // load some values
  205. CString regvalue;
  206. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource","C:\\");
  207. if(m_WhereSource != regvalue)
  208. {
  209. regvalue = "";
  210. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource3","");
  211. RegSetValueEx(hKey, _T("WhereSource4"), 0, REG_SZ,
  212. (CONST BYTE *)(const char *)regvalue,
  213. regvalue.GetLength());
  214. regvalue = "";
  215. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource2","");
  216. RegSetValueEx(hKey, _T("WhereSource3"), 0, REG_SZ,
  217. (CONST BYTE *)(const char *)regvalue,
  218. regvalue.GetLength());
  219. regvalue = "";
  220. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource","");
  221. RegSetValueEx(hKey, _T("WhereSource2"), 0, REG_SZ,
  222. (CONST BYTE *)(const char *)regvalue,
  223. regvalue.GetLength());
  224. RegSetValueEx(hKey, _T("WhereSource"), 0, REG_SZ,
  225. (CONST BYTE *)(const char *)m_WhereSource,
  226. m_WhereSource.GetLength());
  227. }
  228. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild","C:\\");
  229. if(m_WhereBuild != regvalue)
  230. {
  231. regvalue = "";
  232. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild3","");
  233. RegSetValueEx(hKey, _T("WhereBuild4"), 0, REG_SZ,
  234. (CONST BYTE *)(const char *)regvalue,
  235. regvalue.GetLength());
  236. regvalue = "";
  237. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild2","");
  238. RegSetValueEx(hKey, _T("WhereBuild3"), 0, REG_SZ,
  239. (CONST BYTE *)(const char *)regvalue,
  240. regvalue.GetLength());
  241. regvalue = "";
  242. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild","");
  243. RegSetValueEx(hKey, _T("WhereBuild2"), 0, REG_SZ,
  244. (CONST BYTE *)(const char *)regvalue,
  245. regvalue.GetLength());
  246. RegSetValueEx(hKey, _T("WhereBuild"), 0, REG_SZ,
  247. (CONST BYTE *)(const char *)m_WhereBuild,
  248. m_WhereBuild.GetLength());
  249. }
  250. }
  251. RegCloseKey(hKey);
  252. }
  253. void CMakeSetupDialog::ReadRegistryValue(HKEY hKey,
  254. CString *val,
  255. const char *key,
  256. const char *adefault)
  257. {
  258. DWORD dwType, dwSize;
  259. char *pb;
  260. dwType = REG_SZ;
  261. pb = val->GetBuffer(MAX_PATH);
  262. dwSize = MAX_PATH;
  263. if(RegQueryValueEx(hKey,_T(key), NULL, &dwType,
  264. (BYTE *)pb, &dwSize) != ERROR_SUCCESS)
  265. {
  266. val->ReleaseBuffer();
  267. *val = _T(adefault);
  268. }
  269. else
  270. {
  271. val->ReleaseBuffer();
  272. }
  273. }
  274. void CMakeSetupDialog::LoadFromRegistry()
  275. {
  276. HKEY hKey;
  277. if(RegOpenKeyEx(HKEY_CURRENT_USER,
  278. m_RegistryKey,
  279. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  280. {
  281. return;
  282. }
  283. else
  284. {
  285. // load some values
  286. this->ReadRegistryValue(hKey, &(m_WhereSource),"WhereSource","C:\\");
  287. this->ReadRegistryValue(hKey, &(m_WhereBuild),"WhereBuild","C:\\");
  288. m_WhereSourceControl.AddString(m_WhereSource);
  289. m_WhereBuildControl.AddString(m_WhereBuild);
  290. CString regvalue;
  291. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource2","C:\\");
  292. m_WhereSourceControl.AddString(regvalue);
  293. regvalue = "";
  294. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild2","C:\\");
  295. m_WhereBuildControl.AddString(regvalue);
  296. regvalue = "";
  297. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource3","C:\\");
  298. m_WhereSourceControl.AddString(regvalue);
  299. regvalue = "";
  300. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild3","C:\\");
  301. m_WhereBuildControl.AddString(regvalue);
  302. regvalue = "";
  303. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource4","C:\\");
  304. m_WhereSourceControl.AddString(regvalue);
  305. regvalue = "";
  306. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild4","C:\\");
  307. m_WhereBuildControl.AddString(regvalue);
  308. }
  309. RegCloseKey(hKey);
  310. }
  311. // Callback for browse source button
  312. void CMakeSetupDialog::OnBrowseWhereSource()
  313. {
  314. this->UpdateData();
  315. Browse(m_WhereSource, "Enter Path to Source");
  316. this->UpdateData(false);
  317. this->OnChangeWhereSource();
  318. }
  319. // Callback for browser build button
  320. void CMakeSetupDialog::OnBrowseWhereBuild()
  321. {
  322. this->UpdateData();
  323. Browse(m_WhereBuild, "Enter Path to Build");
  324. this->UpdateData(false);
  325. this->OnChangeWhereBuild();
  326. }
  327. // Callback for build projects button
  328. void CMakeSetupDialog::OnBuildProjects()
  329. {
  330. if(!cmSystemTools::FileExists(m_WhereBuild))
  331. {
  332. std::string message =
  333. "Build directory does not exist, should I create it?\n\n"
  334. "Directory: ";
  335. message += (const char*)m_WhereBuild;
  336. if(MessageBox(message.c_str(), "Create Directory", MB_OKCANCEL) == IDOK)
  337. {
  338. cmSystemTools::MakeDirectory(m_WhereBuild);
  339. }
  340. else
  341. {
  342. MessageBox("Build Project aborted, nothing done.");
  343. return;
  344. }
  345. }
  346. // set the wait cursor
  347. ::SetCursor(LoadCursor(NULL, IDC_WAIT));
  348. // get all the info from the dialog
  349. this->UpdateData();
  350. if(!m_BuildPathChanged)
  351. {
  352. // if the build path has not changed save the
  353. // current GUI values to the cache
  354. this->SaveCacheFromGUI();
  355. }
  356. // Make sure we are working from the cache on disk
  357. this->LoadCacheFromDiskToGUI();
  358. // create a cmake object
  359. cmake make;
  360. // create the arguments for the cmake object
  361. std::vector<std::string> args;
  362. args.push_back((const char*)m_PathToExecutable);
  363. std::string arg;
  364. arg = "-H";
  365. arg += m_WhereSource;
  366. args.push_back(arg);
  367. arg = "-B";
  368. arg += m_WhereBuild;
  369. args.push_back(arg);
  370. // run the generate process
  371. if(make.Generate(args) != 0)
  372. {
  373. cmSystemTools::Error(
  374. "Error in generation process, project files may be invalid");
  375. }
  376. // update the GUI with any new values in the caused by the
  377. // generation process
  378. this->LoadCacheFromDiskToGUI();
  379. // save source and build paths to registry
  380. this->SaveToRegistry();
  381. // path is up-to-date now
  382. m_BuildPathChanged = false;
  383. // put the cursor back
  384. ::SetCursor(LoadCursor(NULL, IDC_ARROW));
  385. }
  386. // callback for combo box menu where build selection
  387. void CMakeSetupDialog::OnSelendokWhereBuild()
  388. {
  389. m_WhereBuildControl.GetLBText(m_WhereBuildControl.GetCurSel(),
  390. m_WhereBuild);
  391. this->UpdateData(FALSE);
  392. this->OnChangeWhereBuild();
  393. }
  394. // callback for combo box menu where source selection
  395. void CMakeSetupDialog::OnSelendokWhereSource()
  396. {
  397. m_WhereSourceControl.GetLBText(m_WhereSourceControl.GetCurSel(),
  398. m_WhereSource);
  399. this->UpdateData(FALSE);
  400. this->OnChangeWhereSource();
  401. }
  402. // callback for chaing source directory
  403. void CMakeSetupDialog::OnChangeWhereSource()
  404. {
  405. }
  406. // callback for changing the build directory
  407. void CMakeSetupDialog::OnChangeWhereBuild()
  408. {
  409. this->UpdateData();
  410. std::string cachefile = m_WhereBuild;
  411. cachefile += "/CMakeCache.txt";
  412. m_CacheEntriesList.RemoveAll();
  413. if(cmSystemTools::FileExists(cachefile.c_str()))
  414. {
  415. m_CacheEntriesList.ShowWindow(SW_SHOW);
  416. this->LoadCacheFromDiskToGUI();
  417. m_BuildPathChanged = true;
  418. }
  419. }
  420. // copy from the cache manager to the cache edit list box
  421. void CMakeSetupDialog::FillCacheGUIFromCacheManager()
  422. {
  423. const cmCacheManager::CacheEntryMap &cache =
  424. cmCacheManager::GetInstance()->GetCacheMap();
  425. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  426. i != cache.end(); ++i)
  427. {
  428. const char* key = i->first.c_str();
  429. const cmCacheManager::CacheEntry& value = i->second;
  430. switch(value.m_Type )
  431. {
  432. case cmCacheManager::BOOL:
  433. if(cmCacheManager::GetInstance()->IsOn(key))
  434. {
  435. m_CacheEntriesList.AddProperty(key,
  436. "ON",
  437. value.m_HelpString.c_str(),
  438. CPropertyList::CHECKBOX,"");
  439. }
  440. else
  441. {
  442. m_CacheEntriesList.AddProperty(key,
  443. "OFF",
  444. value.m_HelpString.c_str(),
  445. CPropertyList::CHECKBOX,"");
  446. }
  447. break;
  448. case cmCacheManager::PATH:
  449. m_CacheEntriesList.AddProperty(key,
  450. value.m_Value.c_str(),
  451. value.m_HelpString.c_str(),
  452. CPropertyList::PATH,"");
  453. break;
  454. case cmCacheManager::FILEPATH:
  455. m_CacheEntriesList.AddProperty(key,
  456. value.m_Value.c_str(),
  457. value.m_HelpString.c_str(),
  458. CPropertyList::FILE,"");
  459. break;
  460. case cmCacheManager::STRING:
  461. m_CacheEntriesList.AddProperty(key,
  462. value.m_Value.c_str(),
  463. value.m_HelpString.c_str(),
  464. CPropertyList::EDIT,"");
  465. break;
  466. case cmCacheManager::INTERNAL:
  467. break;
  468. }
  469. }
  470. }
  471. // copy from the list box to the cache manager
  472. void CMakeSetupDialog::FillCacheManagerFromCacheGUI()
  473. {
  474. cmCacheManager::GetInstance()->GetCacheMap();
  475. std::set<CPropertyItem*> items = m_CacheEntriesList.GetItems();
  476. for(std::set<CPropertyItem*>::iterator i = items.begin();
  477. i != items.end(); ++i)
  478. {
  479. CPropertyItem* item = *i;
  480. cmCacheManager::CacheEntry *entry =
  481. cmCacheManager::GetInstance()->GetCacheEntry(
  482. (const char*)item->m_propName);
  483. if (entry)
  484. {
  485. entry->m_Value = item->m_curValue;
  486. }
  487. }
  488. }
  489. //! Load cache file from m_WhereBuild and display in GUI editor
  490. void CMakeSetupDialog::LoadCacheFromDiskToGUI()
  491. {
  492. if(m_WhereBuild != "")
  493. {
  494. cmCacheManager::GetInstance()->LoadCache(m_WhereBuild);
  495. this->FillCacheGUIFromCacheManager();
  496. }
  497. }
  498. //! Save GUI values to cmCacheManager and then save to disk.
  499. void CMakeSetupDialog::SaveCacheFromGUI()
  500. {
  501. this->FillCacheManagerFromCacheGUI();
  502. if(m_WhereBuild != "")
  503. {
  504. cmCacheManager::GetInstance()->SaveCache(m_WhereBuild);
  505. }
  506. }