CMakeSetupDialog.cpp 12 KB

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