CMakeSetupDialog.cpp 11 KB

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