CMakeSetupDialog.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #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. }
  86. void CMakeSetupDialog::DoDataExchange(CDataExchange* pDX)
  87. {
  88. CDialog::DoDataExchange(pDX);
  89. //{{AFX_DATA_MAP(CMakeSetupDialog)
  90. DDX_Text(pDX, IDC_WhereSource, m_WhereSource);
  91. DDX_Text(pDX, IDC_WhereBuild, m_WhereBuild);
  92. //}}AFX_DATA_MAP
  93. }
  94. BEGIN_MESSAGE_MAP(CMakeSetupDialog, CDialog)
  95. //{{AFX_MSG_MAP(CMakeSetupDialog)
  96. ON_WM_SYSCOMMAND()
  97. ON_WM_PAINT()
  98. ON_WM_QUERYDRAGICON()
  99. ON_EN_CHANGE(IDC_WhereSource, OnChangeEdit1)
  100. ON_BN_CLICKED(IDC_BUTTON2, OnBrowse)
  101. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  102. //}}AFX_MSG_MAP
  103. END_MESSAGE_MAP()
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CMakeSetupDialog message handlers
  106. BOOL CMakeSetupDialog::OnInitDialog()
  107. {
  108. CDialog::OnInitDialog();
  109. // Add "About..." menu item to system menu.
  110. // IDM_ABOUTBOX must be in the system command range.
  111. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  112. ASSERT(IDM_ABOUTBOX < 0xF000);
  113. CMenu* pSysMenu = GetSystemMenu(FALSE);
  114. if (pSysMenu != NULL)
  115. {
  116. CString strAboutMenu;
  117. strAboutMenu.LoadString(IDS_ABOUTBOX);
  118. if (!strAboutMenu.IsEmpty())
  119. {
  120. pSysMenu->AppendMenu(MF_SEPARATOR);
  121. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  122. }
  123. }
  124. // Set the icon for this dialog. The framework does this automatically
  125. // when the application's main window is not a dialog
  126. SetIcon(m_hIcon, TRUE); // Set big icon
  127. SetIcon(m_hIcon, FALSE); // Set small icon
  128. // TODO: Add extra initialization here
  129. return TRUE; // return TRUE unless you set the focus to a control
  130. }
  131. void CMakeSetupDialog::OnSysCommand(UINT nID, LPARAM lParam)
  132. {
  133. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  134. {
  135. CAboutDlg dlgAbout;
  136. dlgAbout.DoModal();
  137. }
  138. else
  139. {
  140. CDialog::OnSysCommand(nID, lParam);
  141. }
  142. }
  143. // If you add a minimize button to your dialog, you will need the code below
  144. // to draw the icon. For MFC applications using the document/view model,
  145. // this is automatically done for you by the framework.
  146. void CMakeSetupDialog::OnPaint()
  147. {
  148. if (IsIconic())
  149. {
  150. CPaintDC dc(this); // device context for painting
  151. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  152. // Center icon in client rectangle
  153. int cxIcon = GetSystemMetrics(SM_CXICON);
  154. int cyIcon = GetSystemMetrics(SM_CYICON);
  155. CRect rect;
  156. GetClientRect(&rect);
  157. int x = (rect.Width() - cxIcon + 1) / 2;
  158. int y = (rect.Height() - cyIcon + 1) / 2;
  159. // Draw the icon
  160. dc.DrawIcon(x, y, m_hIcon);
  161. }
  162. else
  163. {
  164. CDialog::OnPaint();
  165. }
  166. }
  167. // The system calls this to obtain the cursor to display while the user drags
  168. // the minimized window.
  169. HCURSOR CMakeSetupDialog::OnQueryDragIcon()
  170. {
  171. return (HCURSOR) m_hIcon;
  172. }
  173. void CMakeSetupDialog::OnChangeEdit1()
  174. {
  175. // TODO: If this is a RICHEDIT control, the control will not
  176. // send this notification unless you override the CDialog::OnInitDialog()
  177. // function and call CRichEditCtrl().SetEventMask()
  178. // with the ENM_CHANGE flag ORed into the mask.
  179. // TODO: Add your control notification handler code here
  180. }
  181. void CMakeSetupDialog::OnBrowse()
  182. {
  183. this->UpdateData();
  184. Browse(m_WhereSource, "Enter Path to Insight Source");
  185. this->UpdateData(false);
  186. }
  187. bool CMakeSetupDialog::Browse(CString &result, const char *title)
  188. {
  189. // don't know what to do with initial right now...
  190. char szPathName[4096];
  191. BROWSEINFO bi;
  192. bi.hwndOwner = m_hWnd;
  193. bi.pidlRoot = NULL;
  194. bi.pszDisplayName = (LPTSTR)szPathName;
  195. bi.lpszTitle = title;
  196. bi.ulFlags = BIF_BROWSEINCLUDEFILES ;
  197. bi.lpfn = NULL;
  198. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  199. bool bSuccess = (bool)SHGetPathFromIDList(pidl, szPathName);
  200. if(bSuccess)
  201. {
  202. result = szPathName;
  203. }
  204. return bSuccess;
  205. }
  206. void CMakeSetupDialog::OnOK()
  207. {
  208. // get all the info from the screen
  209. this->UpdateData();
  210. if(m_WhereBuild == "")
  211. {
  212. m_WhereBuild = m_WhereSource;
  213. }
  214. cmMakefile mf;
  215. mf.SetMakefileGenerator(new cmMSProjectGenerator);
  216. mf.SetHomeDirectory(m_WhereSource);
  217. // Set the output directory
  218. mf.SetStartOutputDirectory(m_WhereBuild);
  219. mf.SetHomeOutputDirectory(m_WhereBuild);
  220. // set the directory which contains the CMakeLists.txt
  221. mf.SetStartDirectory(m_WhereSource);
  222. // Create the master DSW file and all children dsp files for ITK
  223. // Set the CMakeLists.txt file
  224. CString makefileIn = m_WhereSource;
  225. makefileIn += "/CMakeLists.txt";
  226. mf.MakeStartDirectoriesCurrent();
  227. mf.ReadListFile(makefileIn);
  228. // Move this to the cache editor
  229. mf.GenerateMakefile();
  230. CDialog::OnOK();
  231. this->SaveToRegistry();
  232. }
  233. void CMakeSetupDialog::OnButton3()
  234. {
  235. this->UpdateData();
  236. Browse(m_WhereBuild, "Enter Path to Insight Build");
  237. this->UpdateData(false);
  238. }
  239. void CMakeSetupDialog::SaveToRegistry()
  240. {
  241. HKEY hKey;
  242. DWORD dwDummy;
  243. if(RegCreateKeyEx(HKEY_CURRENT_USER,
  244. m_RegistryKey,
  245. 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
  246. NULL, &hKey, &dwDummy) != ERROR_SUCCESS)
  247. {
  248. return;
  249. }
  250. else
  251. {
  252. RegSetValueEx(hKey, _T("WhereSource"), 0, REG_SZ,
  253. (CONST BYTE *)(const char *)m_WhereSource,
  254. m_WhereSource.GetLength());
  255. RegSetValueEx(hKey, _T("WhereBuild"), 0, REG_SZ,
  256. (CONST BYTE *)(const char *)m_WhereBuild,
  257. m_WhereBuild.GetLength());
  258. }
  259. RegCloseKey(hKey);
  260. }
  261. void CMakeSetupDialog::ReadRegistryValue(HKEY hKey,
  262. CString *val,
  263. const char *key,
  264. const char *adefault)
  265. {
  266. DWORD dwType, dwSize;
  267. char *pb;
  268. dwType = REG_SZ;
  269. pb = val->GetBuffer(MAX_PATH);
  270. dwSize = MAX_PATH;
  271. if(RegQueryValueEx(hKey,_T(key), NULL, &dwType,
  272. (BYTE *)pb, &dwSize) != ERROR_SUCCESS)
  273. {
  274. val->ReleaseBuffer();
  275. *val = _T(adefault);
  276. }
  277. else
  278. {
  279. val->ReleaseBuffer();
  280. }
  281. }
  282. void CMakeSetupDialog::LoadFromRegistry()
  283. {
  284. HKEY hKey;
  285. if(RegOpenKeyEx(HKEY_CURRENT_USER,
  286. m_RegistryKey,
  287. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  288. {
  289. return;
  290. }
  291. else
  292. {
  293. // save some values
  294. this->ReadRegistryValue(hKey, &(m_WhereSource),"WhereSource","C:\\Insight");
  295. this->ReadRegistryValue(hKey, &(m_WhereBuild),"WhereBuild",
  296. "C:\\Insight");
  297. }
  298. RegCloseKey(hKey);
  299. }