CMakeSetupDialog.cpp 17 KB

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