CMakeSetupDialog.cpp 17 KB

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