PageExport.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // PageExport.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "NVAPIExample.h"
  5. #include "NVAPIExampleDlg.h"
  6. #include "PageExport.h"
  7. #include "PageData.h"
  8. #include "Sheet.h"
  9. #include <process.h>
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CPageExport dialog
  17. CPageExport::CPageExport(CSheet * pParent)
  18. : CPage(CPageExport::IDD, pParent)
  19. , m_bActiveState(false)
  20. , m_thread(0)
  21. {
  22. //{{AFX_DATA_INIT(CPageExport)
  23. //}}AFX_DATA_INIT
  24. }
  25. void CPageExport::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. //{{AFX_DATA_MAP(CPageExport)
  29. DDX_Control(pDX, IDC_STATIC_TOTAL_REMAINTIME, c_textTotalRemainTime);
  30. DDX_Control(pDX, IDC_STATIC_TOTAL_PROGRESS, c_textTotalProgress);
  31. DDX_Control(pDX, IDC_STATIC_CURRENT_REMAINTIME, c_textCurrentRemainTime);
  32. DDX_Control(pDX, IDC_STATIC_CURRENT_PROGRESS, m_textCurrentProgress);
  33. DDX_Control(pDX, IDC_PROGRESS_TOTAL, c_ProgressTotal);
  34. DDX_Control(pDX, IDC_PROGRESS_CURRENT, c_progressCurrent);
  35. //}}AFX_DATA_MAP
  36. }
  37. BEGIN_MESSAGE_MAP(CPageExport, CDialog)
  38. //{{AFX_MSG_MAP(CPageExport)
  39. // NOTE: the ClassWizard will add message map macros here
  40. //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CPageExport message handlers
  44. bool CPageExport::Create (void)
  45. {
  46. // This is a CPage virtual function. Just create the dialog and return
  47. // success.
  48. //
  49. return CDialog::Create (m_lpszTemplateName, m_pParentWnd) != FALSE;
  50. }
  51. bool CPageExport::OnBack (void)
  52. {
  53. // go back to first page
  54. CNVAPIExampleDlg * pParentSheet = (CNVAPIExampleDlg *) GetParent();
  55. if (pParentSheet)
  56. pParentSheet->SwitchPage(0);
  57. return true;
  58. }
  59. bool CPageExport::OnNext (void)
  60. {
  61. // If we are in an active state, the button is used for abortion. So,
  62. // abort and eat the button click.
  63. //
  64. if (m_bActiveState)
  65. {
  66. m_IsAborting = true;
  67. return true;
  68. }
  69. else
  70. {
  71. CNVAPIExampleDlg * pParentSheet = (CNVAPIExampleDlg *) GetParent();
  72. if (pParentSheet)
  73. pParentSheet->SwitchPage(0);
  74. return true;
  75. }
  76. }
  77. DWORD WINAPI CPageExport::BeginThreadFunc(void* p)
  78. {
  79. ASSERT(p);
  80. CPageExport* page = (CPageExport*)p;
  81. page->ExportProcess();
  82. return 0;
  83. }
  84. void CPageExport::ExportProcess()
  85. {
  86. CoInitialize(0);
  87. try
  88. {
  89. // prepare the exporter
  90. NeroVisionAPI::IErrorPtr err;
  91. NeroVisionAPI::IMovieExporterPtr exporter;
  92. {
  93. if (FAILED(exporter.CreateInstance(__uuidof(NeroVisionAPI::MovieExporter))))
  94. throw _bstr_t("CreateInstance failed");
  95. err = exporter->LastError;
  96. if (err->ErrCode)
  97. throw err;
  98. }
  99. m_pageData->m_pProject->SetXMLString ((LPSTR)(LPCSTR) m_pageData->m_sXML);
  100. CString str;
  101. str.Format("t%d", m_pageData->m_selectedItemIndex + 1);
  102. wchar_t XMLID[100];
  103. MultiByteToWideChar(CP_UTF8, 0, str, -1, XMLID, 100);
  104. if (!exporter->SetSource(m_pageData->m_pProject, XMLID))
  105. throw err = exporter->LastError;
  106. __int64 s;
  107. if (!exporter->EstimateFileSize(&s))
  108. throw err = exporter->LastError;
  109. if (!exporter->ExportMovie(m_exportFilename, this))
  110. throw err = exporter->LastError;
  111. }
  112. catch (_bstr_t& errstr)
  113. {
  114. m_IsAborting = true;
  115. MessageBox(_T("Error occurred:\n") + errstr, 0, MB_OK | MB_ICONERROR);
  116. }
  117. catch (NeroVisionAPI::IErrorPtr& e)
  118. {
  119. m_IsAborting = true;
  120. if (e->ErrCode != NeroVisionAPI::Canceled)
  121. {
  122. _bstr_t s = "NeroVisionAPI error occurred:\n" + e->ErrText;
  123. _bstr_t s2 = e->XMLID;
  124. if (s2.length())
  125. s += "\nXML-ID: " + s2;
  126. MessageBox(s , 0, MB_OK | MB_ICONERROR);
  127. }
  128. }
  129. catch (_com_error& e)
  130. {
  131. m_IsAborting = true;
  132. _bstr_t s = L"COM error occurred:\n";
  133. if ((wchar_t*)e.Description())
  134. s += e.Description();
  135. if (e.ErrorMessage())
  136. s += e.ErrorMessage();
  137. MessageBox(s, 0, MB_OK | MB_ICONERROR);
  138. }
  139. CoUninitialize();
  140. PostMessage(WM_COMMAND, MAKEWPARAM(IDC_START_PROCESSINGFINISHED, 0), 0);
  141. }
  142. void CPageExport::OnChangeState (bool bActivate, bool bForward)
  143. {
  144. if (bForward)
  145. {
  146. // let user select filename for export
  147. CString sAbort;
  148. sAbort.LoadString (IDS_BUTTON_ABORT);
  149. GetParent ()->GetDlgItem (IDOK)->SetWindowText (sAbort);
  150. GetParent ()->GetDlgItem (IDCANCEL)->EnableWindow (false);
  151. c_ProgressTotal.SetRange(0, 1000);
  152. c_ProgressTotal.SetPos(0);
  153. c_progressCurrent.SetRange(0, 1000);
  154. c_progressCurrent.SetPos(0);
  155. c_textTotalRemainTime.SetWindowText("");
  156. c_textTotalProgress.SetWindowText("");
  157. c_textCurrentRemainTime.SetWindowText("");
  158. m_textCurrentProgress.SetWindowText("");
  159. char fileBuffer[MAX_PATH];
  160. *fileBuffer = 0;
  161. char filterBuffer[] = "MPEG files (*.mpg)\0*.mpg\0\0";
  162. char extBuffer[] = ".mpg";
  163. char titleBuffer[] = "Save as MPEG file";
  164. OPENFILENAME ofn;
  165. ZeroMemory(&ofn, sizeof(ofn));
  166. ofn.lStructSize = sizeof(ofn);
  167. ofn.hwndOwner = GetSafeHwnd();
  168. ofn.lpstrFile = fileBuffer;
  169. ofn.nMaxFile = MAX_PATH;
  170. ofn.lpstrFilter = filterBuffer;
  171. ofn.lpstrDefExt = extBuffer;
  172. ofn.lpstrTitle = titleBuffer;
  173. ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  174. if (GetSaveFileName(&ofn) == IDOK)
  175. {
  176. m_exportFilename = fileBuffer;
  177. m_IsAborting = false;
  178. m_bActiveState = true;
  179. CString sAbort;
  180. sAbort.LoadString (IDS_BUTTON_ABORT);
  181. GetParent ()->GetDlgItem (IDOK)->SetWindowText (sAbort);
  182. GetParent ()->GetDlgItem (IDOK)->EnableWindow (true);
  183. GetParent ()->GetDlgItem (IDCANCEL)->EnableWindow (false);
  184. m_pageData = GetPageData(); // for use inside thread function
  185. m_thread = (HANDLE)CreateThread(0, 0, BeginThreadFunc, this, 0, 0);
  186. }
  187. else
  188. {
  189. PostMessage(WM_COMMAND, MAKEWPARAM(IDC_START_PROCESSINGFINISHED, 0), 0);
  190. }
  191. }
  192. }
  193. STDMETHODIMP CPageExport::raw_OnProgress (enum NeroVisionAPI::ProgressAction action, BSTR itemName,
  194. double currentRemain, double currentTotal, float currentFraction,
  195. double totalRemain, double totalTotal, float totalFraction)
  196. {
  197. // update progress controls
  198. SendDlgItemMessage(IDC_PROGRESS_CURRENT, PBM_SETPOS, WPARAM(currentFraction * 1000), 0);
  199. SendDlgItemMessage(IDC_PROGRESS_TOTAL, PBM_SETPOS, WPARAM(totalFraction * 1000), 0);
  200. // display current action
  201. _bstr_t actionText;
  202. switch (action) {
  203. case NeroVisionAPI::Estimating:
  204. actionText = _T("Estimating"); break;
  205. case NeroVisionAPI::Preparing:
  206. actionText = _T("Preparing"); break;
  207. case NeroVisionAPI::Transcoding:
  208. actionText = _T("Transcoding"); break;
  209. case NeroVisionAPI::Generating:
  210. actionText = _T("Generating"); break;
  211. case NeroVisionAPI::Analyzing:
  212. actionText = _T("Analyzing"); break;
  213. }
  214. if (_bstr_t(itemName).length() > 0)
  215. actionText += _T(": '") + _bstr_t(itemName) + _T("'");
  216. SetDlgItemText(IDC_STATIC_PROGRESS, actionText);
  217. // display total times for current action and total process
  218. TCHAR text[2048];
  219. FormatTimeString(currentTotal, _T("Time needed for current task: %ldh %02ldm %02lds"), text);
  220. SetDlgItemText(IDC_STATIC_CURRENT_PROGRESS, text);
  221. FormatTimeString(totalTotal, _T("Total time needed: %ldh %02ldm %02lds"), text);
  222. SetDlgItemText(IDC_STATIC_TOTAL_PROGRESS, text);
  223. // display times remaining for current action and total process
  224. TCHAR fmtRemain[] = _T("Remaining: %ldh %02ldm %02lds");
  225. FormatTimeString(currentRemain, fmtRemain, text);
  226. SetDlgItemText(IDC_STATIC_CURRENT_REMAINTIME, text);
  227. FormatTimeString(totalRemain, fmtRemain, text);
  228. SetDlgItemText(IDC_STATIC_TOTAL_REMAINTIME, text);
  229. UpdateWindow();
  230. return S_OK;
  231. }
  232. STDMETHODIMP CPageExport::raw_ShouldCancel(VARIANT_BOOL* pbCancel)
  233. {
  234. *pbCancel = (m_IsAborting ? VARIANT_TRUE : VARIANT_FALSE);
  235. return S_OK;
  236. }
  237. void CPageExport::FormatTimeString(double secs, const TCHAR* format, TCHAR* buffer)
  238. {
  239. unsigned int hours = unsigned int(secs / 3600);
  240. unsigned int minutes = (unsigned int(secs) % 3600) / 60;
  241. unsigned int seconds = unsigned int(secs) % 60;
  242. _stprintf(buffer, format, hours, minutes, seconds);
  243. }
  244. BOOL CPageExport::OnCommand(WPARAM wParam, LPARAM lParam)
  245. {
  246. // delay go on back to first page
  247. if (LOWORD(wParam) == IDC_START_PROCESSINGFINISHED)
  248. {
  249. if (m_thread)
  250. {
  251. CloseHandle(m_thread);
  252. m_thread = 0;
  253. }
  254. CNVAPIExampleDlg * pParentSheet = (CNVAPIExampleDlg *) GetParent();
  255. if (pParentSheet)
  256. pParentSheet->SwitchPage(0);
  257. }
  258. return CDialog::OnCommand(wParam, lParam);
  259. }