CMakeSetupDialog.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 %d.%d", cmMakefile::GetMajorVersion(),
  130. cmMakefile::GetMinorVersion());
  131. SetDlgItemText(IDC_CMAKE_VERSION, tmp);
  132. this->UpdateData(FALSE);
  133. return TRUE; // return TRUE unless you set the focus to a control
  134. }
  135. // About dialog invoke
  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. // Browse button
  179. bool CMakeSetupDialog::Browse(CString &result, const char *title)
  180. {
  181. CPathDialog dlg("Select Path", title, result);
  182. if(dlg.DoModal()==IDOK)
  183. {
  184. result = dlg.GetPathName();
  185. return true;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. void CMakeSetupDialog::SaveToRegistry()
  193. {
  194. HKEY hKey;
  195. DWORD dwDummy;
  196. if(RegCreateKeyEx(HKEY_CURRENT_USER,
  197. m_RegistryKey,
  198. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  199. NULL, &hKey, &dwDummy) != ERROR_SUCCESS)
  200. {
  201. return;
  202. }
  203. else
  204. {
  205. // load some values
  206. CString regvalue;
  207. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource","C:\\");
  208. if(m_WhereSource != regvalue)
  209. {
  210. regvalue = "";
  211. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource3","");
  212. RegSetValueEx(hKey, _T("WhereSource4"), 0, REG_SZ,
  213. (CONST BYTE *)(const char *)regvalue,
  214. regvalue.GetLength());
  215. regvalue = "";
  216. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource2","");
  217. RegSetValueEx(hKey, _T("WhereSource3"), 0, REG_SZ,
  218. (CONST BYTE *)(const char *)regvalue,
  219. regvalue.GetLength());
  220. regvalue = "";
  221. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource","");
  222. RegSetValueEx(hKey, _T("WhereSource2"), 0, REG_SZ,
  223. (CONST BYTE *)(const char *)regvalue,
  224. regvalue.GetLength());
  225. RegSetValueEx(hKey, _T("WhereSource"), 0, REG_SZ,
  226. (CONST BYTE *)(const char *)m_WhereSource,
  227. m_WhereSource.GetLength());
  228. }
  229. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild","C:\\");
  230. if(m_WhereBuild != regvalue)
  231. {
  232. regvalue = "";
  233. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild3","");
  234. RegSetValueEx(hKey, _T("WhereBuild4"), 0, REG_SZ,
  235. (CONST BYTE *)(const char *)regvalue,
  236. regvalue.GetLength());
  237. regvalue = "";
  238. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild2","");
  239. RegSetValueEx(hKey, _T("WhereBuild3"), 0, REG_SZ,
  240. (CONST BYTE *)(const char *)regvalue,
  241. regvalue.GetLength());
  242. regvalue = "";
  243. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild","");
  244. RegSetValueEx(hKey, _T("WhereBuild2"), 0, REG_SZ,
  245. (CONST BYTE *)(const char *)regvalue,
  246. regvalue.GetLength());
  247. RegSetValueEx(hKey, _T("WhereBuild"), 0, REG_SZ,
  248. (CONST BYTE *)(const char *)m_WhereBuild,
  249. m_WhereBuild.GetLength());
  250. }
  251. }
  252. RegCloseKey(hKey);
  253. }
  254. void CMakeSetupDialog::ReadRegistryValue(HKEY hKey,
  255. CString *val,
  256. const char *key,
  257. const char *adefault)
  258. {
  259. DWORD dwType, dwSize;
  260. char *pb;
  261. dwType = REG_SZ;
  262. pb = val->GetBuffer(MAX_PATH);
  263. dwSize = MAX_PATH;
  264. if(RegQueryValueEx(hKey,_T(key), NULL, &dwType,
  265. (BYTE *)pb, &dwSize) != ERROR_SUCCESS)
  266. {
  267. val->ReleaseBuffer();
  268. *val = _T(adefault);
  269. }
  270. else
  271. {
  272. val->ReleaseBuffer();
  273. }
  274. }
  275. void CMakeSetupDialog::LoadFromRegistry()
  276. {
  277. HKEY hKey;
  278. if(RegOpenKeyEx(HKEY_CURRENT_USER,
  279. m_RegistryKey,
  280. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  281. {
  282. return;
  283. }
  284. else
  285. {
  286. // load some values
  287. this->ReadRegistryValue(hKey, &(m_WhereSource),"WhereSource","C:\\");
  288. this->ReadRegistryValue(hKey, &(m_WhereBuild),"WhereBuild","C:\\");
  289. m_WhereSourceControl.AddString(m_WhereSource);
  290. m_WhereBuildControl.AddString(m_WhereBuild);
  291. CString regvalue;
  292. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource2","C:\\");
  293. m_WhereSourceControl.AddString(regvalue);
  294. regvalue = "";
  295. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild2","C:\\");
  296. m_WhereBuildControl.AddString(regvalue);
  297. regvalue = "";
  298. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource3","C:\\");
  299. m_WhereSourceControl.AddString(regvalue);
  300. regvalue = "";
  301. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild3","C:\\");
  302. m_WhereBuildControl.AddString(regvalue);
  303. regvalue = "";
  304. this->ReadRegistryValue(hKey, &(regvalue),"WhereSource4","C:\\");
  305. m_WhereSourceControl.AddString(regvalue);
  306. regvalue = "";
  307. this->ReadRegistryValue(hKey, &(regvalue),"WhereBuild4","C:\\");
  308. m_WhereBuildControl.AddString(regvalue);
  309. }
  310. RegCloseKey(hKey);
  311. }
  312. // Callback for browse source button
  313. void CMakeSetupDialog::OnBrowseWhereSource()
  314. {
  315. this->UpdateData();
  316. Browse(m_WhereSource, "Enter Path to Source");
  317. this->UpdateData(false);
  318. this->OnChangeWhereSource();
  319. }
  320. // Callback for browser build button
  321. void CMakeSetupDialog::OnBrowseWhereBuild()
  322. {
  323. this->UpdateData();
  324. Browse(m_WhereBuild, "Enter Path to Build");
  325. this->UpdateData(false);
  326. this->OnChangeWhereBuild();
  327. }
  328. // Callback for build projects button
  329. void CMakeSetupDialog::OnBuildProjects()
  330. {
  331. if(!cmSystemTools::FileExists(m_WhereBuild))
  332. {
  333. std::string message =
  334. "Build directory does not exist, should I create it?\n\n"
  335. "Directory: ";
  336. message += (const char*)m_WhereBuild;
  337. if(MessageBox(message.c_str(), "Create Directory", MB_OKCANCEL) == IDOK)
  338. {
  339. cmSystemTools::MakeDirectory(m_WhereBuild);
  340. }
  341. else
  342. {
  343. MessageBox("Build Project aborted, nothing done.");
  344. return;
  345. }
  346. }
  347. // set the wait cursor
  348. ::SetCursor(LoadCursor(NULL, IDC_WAIT));
  349. // get all the info from the dialog
  350. this->UpdateData();
  351. if(!m_BuildPathChanged)
  352. {
  353. // if the build path has not changed save the
  354. // current GUI values to the cache
  355. this->SaveCacheFromGUI();
  356. }
  357. // Make sure we are working from the cache on disk
  358. this->LoadCacheFromDiskToGUI();
  359. // create a cmake object
  360. cmake make;
  361. // create the arguments for the cmake object
  362. std::vector<std::string> args;
  363. args.push_back((const char*)m_PathToExecutable);
  364. std::string arg;
  365. arg = "-H";
  366. arg += m_WhereSource;
  367. args.push_back(arg);
  368. arg = "-B";
  369. arg += m_WhereBuild;
  370. args.push_back(arg);
  371. // run the generate process
  372. if(make.Generate(args) != 0)
  373. {
  374. cmSystemTools::Error(
  375. "Error in generation process, project files may be invalid");
  376. cmSystemTools::ResetErrorOccuredFlag();
  377. }
  378. // update the GUI with any new values in the caused by the
  379. // generation process
  380. this->LoadCacheFromDiskToGUI();
  381. // save source and build paths to registry
  382. this->SaveToRegistry();
  383. // path is up-to-date now
  384. m_BuildPathChanged = false;
  385. // put the cursor back
  386. ::SetCursor(LoadCursor(NULL, IDC_ARROW));
  387. }
  388. // callback for combo box menu where build selection
  389. void CMakeSetupDialog::OnSelendokWhereBuild()
  390. {
  391. m_WhereBuildControl.GetLBText(m_WhereBuildControl.GetCurSel(),
  392. m_WhereBuild);
  393. this->UpdateData(FALSE);
  394. this->OnChangeWhereBuild();
  395. }
  396. // callback for combo box menu where source selection
  397. void CMakeSetupDialog::OnSelendokWhereSource()
  398. {
  399. m_WhereSourceControl.GetLBText(m_WhereSourceControl.GetCurSel(),
  400. m_WhereSource);
  401. this->UpdateData(FALSE);
  402. this->OnChangeWhereSource();
  403. }
  404. // callback for chaing source directory
  405. void CMakeSetupDialog::OnChangeWhereSource()
  406. {
  407. }
  408. // callback for changing the build directory
  409. void CMakeSetupDialog::OnChangeWhereBuild()
  410. {
  411. this->UpdateData();
  412. std::string cachefile = m_WhereBuild;
  413. cachefile += "/CMakeCache.txt";
  414. m_CacheEntriesList.RemoveAll();
  415. if(cmSystemTools::FileExists(cachefile.c_str()))
  416. {
  417. m_CacheEntriesList.ShowWindow(SW_SHOW);
  418. this->LoadCacheFromDiskToGUI();
  419. m_BuildPathChanged = true;
  420. }
  421. }
  422. // copy from the cache manager to the cache edit list box
  423. void CMakeSetupDialog::FillCacheGUIFromCacheManager()
  424. {
  425. const cmCacheManager::CacheEntryMap &cache =
  426. cmCacheManager::GetInstance()->GetCacheMap();
  427. for(cmCacheManager::CacheEntryMap::const_iterator i = cache.begin();
  428. i != cache.end(); ++i)
  429. {
  430. const char* key = i->first.c_str();
  431. const cmCacheManager::CacheEntry& value = i->second;
  432. switch(value.m_Type )
  433. {
  434. case cmCacheManager::BOOL:
  435. if(cmCacheManager::GetInstance()->IsOn(key))
  436. {
  437. m_CacheEntriesList.AddProperty(key,
  438. "ON",
  439. value.m_HelpString.c_str(),
  440. CPropertyList::CHECKBOX,"");
  441. }
  442. else
  443. {
  444. m_CacheEntriesList.AddProperty(key,
  445. "OFF",
  446. value.m_HelpString.c_str(),
  447. CPropertyList::CHECKBOX,"");
  448. }
  449. break;
  450. case cmCacheManager::PATH:
  451. m_CacheEntriesList.AddProperty(key,
  452. value.m_Value.c_str(),
  453. value.m_HelpString.c_str(),
  454. CPropertyList::PATH,"");
  455. break;
  456. case cmCacheManager::FILEPATH:
  457. m_CacheEntriesList.AddProperty(key,
  458. value.m_Value.c_str(),
  459. value.m_HelpString.c_str(),
  460. CPropertyList::FILE,"");
  461. break;
  462. case cmCacheManager::STRING:
  463. m_CacheEntriesList.AddProperty(key,
  464. value.m_Value.c_str(),
  465. value.m_HelpString.c_str(),
  466. CPropertyList::EDIT,"");
  467. break;
  468. case cmCacheManager::INTERNAL:
  469. break;
  470. }
  471. }
  472. }
  473. // copy from the list box to the cache manager
  474. void CMakeSetupDialog::FillCacheManagerFromCacheGUI()
  475. {
  476. cmCacheManager::GetInstance()->GetCacheMap();
  477. std::set<CPropertyItem*> items = m_CacheEntriesList.GetItems();
  478. for(std::set<CPropertyItem*>::iterator i = items.begin();
  479. i != items.end(); ++i)
  480. {
  481. CPropertyItem* item = *i;
  482. cmCacheManager::CacheEntry *entry =
  483. cmCacheManager::GetInstance()->GetCacheEntry(
  484. (const char*)item->m_propName);
  485. if (entry)
  486. {
  487. entry->m_Value = item->m_curValue;
  488. }
  489. }
  490. }
  491. //! Load cache file from m_WhereBuild and display in GUI editor
  492. void CMakeSetupDialog::LoadCacheFromDiskToGUI()
  493. {
  494. if(m_WhereBuild != "")
  495. {
  496. cmCacheManager::GetInstance()->LoadCache(m_WhereBuild);
  497. this->FillCacheGUIFromCacheManager();
  498. }
  499. }
  500. //! Save GUI values to cmCacheManager and then save to disk.
  501. void CMakeSetupDialog::SaveCacheFromGUI()
  502. {
  503. this->FillCacheManagerFromCacheGUI();
  504. if(m_WhereBuild != "")
  505. {
  506. cmCacheManager::GetInstance()->SaveCache(m_WhereBuild);
  507. }
  508. }