CMakeSetupDialog.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // pcbuilderdialogDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CMakeSetup.h"
  5. #include "CMakeSetupDialog.h"
  6. #include "../cmDSWMakefile.h"
  7. #include "../cmWindowsConfigure.h"
  8. #include "../cmMSProjectGenerator.h"
  9. #include "../cmCacheManager.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. //{{AFX_DATA(CAboutDlg)
  23. enum { IDD = IDD_ABOUTBOX };
  24. //}}AFX_DATA
  25. // ClassWizard generated virtual function overrides
  26. //{{AFX_VIRTUAL(CAboutDlg)
  27. protected:
  28. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  29. //}}AFX_VIRTUAL
  30. // Implementation
  31. protected:
  32. //{{AFX_MSG(CAboutDlg)
  33. //}}AFX_MSG
  34. DECLARE_MESSAGE_MAP()
  35. };
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAboutDlg)
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  48. //{{AFX_MSG_MAP(CAboutDlg)
  49. // No message handlers
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP();
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CMakeSetupDialog dialog
  54. CMakeSetupDialog::CMakeSetupDialog(CWnd* pParent /*=NULL*/)
  55. : CDialog(CMakeSetupDialog::IDD, pParent)
  56. {
  57. CString startPath = _pgmptr;
  58. startPath.Replace('\\', '_');
  59. startPath.Replace(':', '_');
  60. startPath.Replace(".EXE", "");
  61. startPath.Replace(".exe", "");
  62. m_RegistryKey = "Software\\Kitware\\CMakeSetup\\Settings\\";
  63. // _pgmptr should be the directory from which cmake was run from
  64. // use it as the unique key for the dialog
  65. m_RegistryKey += startPath;
  66. //{{AFX_DATA_INIT(CMakeSetupDialog)
  67. m_WhereSource = _T("");
  68. m_WhereBuild = _T("");
  69. //}}AFX_DATA_INIT
  70. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  71. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  72. // Guess the initial source directory based on the location
  73. // of this program, it should be in CMake/Source/
  74. startPath = _pgmptr;
  75. int removePos = startPath.Find("\\CMake\\Source");
  76. if(removePos == -1)
  77. {
  78. removePos = startPath.Find("/CMake/Source");
  79. }
  80. if(removePos != -1)
  81. {
  82. startPath = startPath.Left(removePos);
  83. }
  84. m_WhereSource = startPath;
  85. this->LoadFromRegistry();
  86. }
  87. void CMakeSetupDialog::DoDataExchange(CDataExchange* pDX)
  88. {
  89. CDialog::DoDataExchange(pDX);
  90. //{{AFX_DATA_MAP(CMakeSetupDialog)
  91. DDX_Text(pDX, IDC_WhereSource, m_WhereSource);
  92. DDX_Text(pDX, IDC_WhereBuild, m_WhereBuild);
  93. //}}AFX_DATA_MAP
  94. }
  95. BEGIN_MESSAGE_MAP(CMakeSetupDialog, CDialog)
  96. //{{AFX_MSG_MAP(CMakeSetupDialog)
  97. ON_WM_SYSCOMMAND()
  98. ON_WM_PAINT()
  99. ON_WM_QUERYDRAGICON()
  100. ON_EN_CHANGE(IDC_WhereSource, OnChangeEdit1)
  101. ON_BN_CLICKED(IDC_BUTTON2, OnBrowse)
  102. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  103. //}}AFX_MSG_MAP
  104. END_MESSAGE_MAP()
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CMakeSetupDialog message handlers
  107. BOOL CMakeSetupDialog::OnInitDialog()
  108. {
  109. CDialog::OnInitDialog();
  110. // Add "About..." menu item to system menu.
  111. // IDM_ABOUTBOX must be in the system command range.
  112. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  113. ASSERT(IDM_ABOUTBOX < 0xF000);
  114. CMenu* pSysMenu = GetSystemMenu(FALSE);
  115. if (pSysMenu != NULL)
  116. {
  117. CString strAboutMenu;
  118. strAboutMenu.LoadString(IDS_ABOUTBOX);
  119. if (!strAboutMenu.IsEmpty())
  120. {
  121. pSysMenu->AppendMenu(MF_SEPARATOR);
  122. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  123. }
  124. }
  125. // Set the icon for this dialog. The framework does this automatically
  126. // when the application's main window is not a dialog
  127. SetIcon(m_hIcon, TRUE); // Set big icon
  128. SetIcon(m_hIcon, FALSE); // Set small icon
  129. // TODO: Add extra initialization here
  130. return TRUE; // return TRUE unless you set the focus to a control
  131. }
  132. void CMakeSetupDialog::OnSysCommand(UINT nID, LPARAM lParam)
  133. {
  134. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  135. {
  136. CAboutDlg dlgAbout;
  137. dlgAbout.DoModal();
  138. }
  139. else
  140. {
  141. CDialog::OnSysCommand(nID, lParam);
  142. }
  143. }
  144. // If you add a minimize button to your dialog, you will need the code below
  145. // to draw the icon. For MFC applications using the document/view model,
  146. // this is automatically done for you by the framework.
  147. void CMakeSetupDialog::OnPaint()
  148. {
  149. if (IsIconic())
  150. {
  151. CPaintDC dc(this); // device context for painting
  152. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  153. // Center icon in client rectangle
  154. int cxIcon = GetSystemMetrics(SM_CXICON);
  155. int cyIcon = GetSystemMetrics(SM_CYICON);
  156. CRect rect;
  157. GetClientRect(&rect);
  158. int x = (rect.Width() - cxIcon + 1) / 2;
  159. int y = (rect.Height() - cyIcon + 1) / 2;
  160. // Draw the icon
  161. dc.DrawIcon(x, y, m_hIcon);
  162. }
  163. else
  164. {
  165. CDialog::OnPaint();
  166. }
  167. }
  168. // The system calls this to obtain the cursor to display while the user drags
  169. // the minimized window.
  170. HCURSOR CMakeSetupDialog::OnQueryDragIcon()
  171. {
  172. return (HCURSOR) m_hIcon;
  173. }
  174. void CMakeSetupDialog::OnChangeEdit1()
  175. {
  176. // TODO: If this is a RICHEDIT control, the control will not
  177. // send this notification unless you override the CDialog::OnInitDialog()
  178. // function and call CRichEditCtrl().SetEventMask()
  179. // with the ENM_CHANGE flag ORed into the mask.
  180. // TODO: Add your control notification handler code here
  181. }
  182. void CMakeSetupDialog::OnBrowse()
  183. {
  184. this->UpdateData();
  185. Browse(m_WhereSource, "Enter Path to Insight Source");
  186. this->UpdateData(false);
  187. }
  188. bool CMakeSetupDialog::Browse(CString &result, const char *title)
  189. {
  190. // don't know what to do with initial right now...
  191. char szPathName[4096];
  192. BROWSEINFO bi;
  193. bi.hwndOwner = m_hWnd;
  194. bi.pidlRoot = NULL;
  195. bi.pszDisplayName = (LPTSTR)szPathName;
  196. bi.lpszTitle = title;
  197. bi.ulFlags = BIF_BROWSEINCLUDEFILES ;
  198. bi.lpfn = NULL;
  199. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  200. bool bSuccess = (bool)SHGetPathFromIDList(pidl, szPathName);
  201. if(bSuccess)
  202. {
  203. result = szPathName;
  204. }
  205. return bSuccess;
  206. }
  207. void CMakeSetupDialog::OnOK()
  208. {
  209. // get all the info from the screen
  210. this->UpdateData();
  211. if(m_WhereBuild == "")
  212. {
  213. m_WhereBuild = m_WhereSource;
  214. }
  215. cmMakefile mf;
  216. mf.SetMakefileGenerator(new cmMSProjectGenerator);
  217. mf.SetHomeDirectory(m_WhereSource);
  218. // Set the output directory
  219. mf.SetStartOutputDirectory(m_WhereBuild);
  220. mf.SetHomeOutputDirectory(m_WhereBuild);
  221. // set the directory which contains the CMakeLists.txt
  222. mf.SetStartDirectory(m_WhereSource);
  223. // Create the master DSW file and all children dsp files for ITK
  224. // Set the CMakeLists.txt file
  225. CString makefileIn = m_WhereSource;
  226. makefileIn += "/CMakeLists.txt";
  227. mf.MakeStartDirectoriesCurrent();
  228. // Create a string for the cache file
  229. cmCacheManager::GetInstance()->LoadCache(&mf);
  230. mf.ReadListFile(makefileIn);
  231. // Move this to the cache editor
  232. mf.GenerateMakefile();
  233. CDialog::OnOK();
  234. cmCacheManager::GetInstance()->SaveCache(&mf);
  235. this->SaveToRegistry();
  236. }
  237. void CMakeSetupDialog::OnButton3()
  238. {
  239. this->UpdateData();
  240. Browse(m_WhereBuild, "Enter Path to Insight Build");
  241. this->UpdateData(false);
  242. }
  243. void CMakeSetupDialog::SaveToRegistry()
  244. {
  245. HKEY hKey;
  246. DWORD dwDummy;
  247. if(RegCreateKeyEx(HKEY_CURRENT_USER,
  248. m_RegistryKey,
  249. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  250. NULL, &hKey, &dwDummy) != ERROR_SUCCESS)
  251. {
  252. return;
  253. }
  254. else
  255. {
  256. RegSetValueEx(hKey, _T("WhereSource"), 0, REG_SZ,
  257. (CONST BYTE *)(const char *)m_WhereSource,
  258. m_WhereSource.GetLength());
  259. RegSetValueEx(hKey, _T("WhereBuild"), 0, REG_SZ,
  260. (CONST BYTE *)(const char *)m_WhereBuild,
  261. m_WhereBuild.GetLength());
  262. }
  263. RegCloseKey(hKey);
  264. }
  265. void CMakeSetupDialog::ReadRegistryValue(HKEY hKey,
  266. CString *val,
  267. const char *key,
  268. const char *adefault)
  269. {
  270. DWORD dwType, dwSize;
  271. char *pb;
  272. dwType = REG_SZ;
  273. pb = val->GetBuffer(MAX_PATH);
  274. dwSize = MAX_PATH;
  275. if(RegQueryValueEx(hKey,_T(key), NULL, &dwType,
  276. (BYTE *)pb, &dwSize) != ERROR_SUCCESS)
  277. {
  278. val->ReleaseBuffer();
  279. *val = _T(adefault);
  280. }
  281. else
  282. {
  283. val->ReleaseBuffer();
  284. }
  285. }
  286. void CMakeSetupDialog::LoadFromRegistry()
  287. {
  288. HKEY hKey;
  289. if(RegOpenKeyEx(HKEY_CURRENT_USER,
  290. m_RegistryKey,
  291. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  292. {
  293. return;
  294. }
  295. else
  296. {
  297. // save some values
  298. this->ReadRegistryValue(hKey, &(m_WhereSource),"WhereSource","C:\\Insight");
  299. this->ReadRegistryValue(hKey, &(m_WhereBuild),"WhereBuild",
  300. "C:\\Insight");
  301. }
  302. RegCloseKey(hKey);
  303. }