/* * AUTHOR * N. Nielsen * * LICENSE * This software is in the public domain. * * The software is provided "as is", without warranty of any kind, * express or implied, including but not limited to the warranties * of merchantability, fitness for a particular purpose, and * noninfringement. In no event shall the author(s) be liable for any * claim, damages, or other liability, whether in an action of * contract, tort, or otherwise, arising from, out of, or in connection * with the software or the use or other dealings in the software. * * SUPPORT * Send bug reports to: */ #include "stdafx.h" #include "ProcessPage.h" #include "common/errutil.h" #include "lib/rlib.h" #include "lib/rep.h" // Error Macros: ---------------------------------------------------------- // Context specific macros. Each one jumps to 'cleanup' so any wrapping // up can be performed #define RETURN(v) \ { ret = (v); goto cleanup; } #define WINERR(f) \ RETURN(errorMessage(m_hWnd, HRESULT_FROM_WIN32(::GetLastError()), f)) #define WINERR_1(f, a) \ RETURN(errorMessage(m_hWnd, HRESULT_FROM_WIN32(::GetLastError()), f, a)) #define WINERR_E(e, f) \ RETURN(errorMessage(m_hWnd, e, f)); #define REPERR(c, s) \ RETURN(rlibError(m_hWnd, c, s)); #define CUSTERR(e, s) \ { errorMessage(m_hWnd, 0, s); RETURN(e); } #define CUSTERR_1(e, s, a) \ { errorMessage(m_hWnd, 0, s, a); RETURN(e); } // (Con|De)struction: --------------------------------------------------------- ProcessPage::ProcessPage(Droplet& droplet) : CPropertyPage(IDD_PROCESS, _T("Script")), m_droplet(droplet) { m_inited = false; } // compileScript: ---------------------------------------------------------- // Load and possibly compile a script from a file HRESULT ProcessPage::compileScript(const string& script) { FILE* file = NULL; HRESULT ret = S_OK; int r = R_OK; { file = fopen(script, "rb"); if(!file) CUSTERR_1(E_FAIL, "Couldn't open script file: %s", script.c_str()); r_context& ctx = m_droplet.getContext(); r = repLoad(&ctx, file); if(r < 0) REPERR(r, &(ctx.script)); } cleanup: if(file) fclose(file); return ret; } // loadData: ------------------------------------------------------------- // Load data from the droplet onto page void ProcessPage::loadData() { m_inited = false; size_t buffSize = m_droplet.getBuffSize(); CheckDlgButton(IDC_USEFILE, buffSize == 0); CheckDlgButton(IDC_USECHUNK, buffSize != 0); SetDlgItemInt(IDC_CHUNK, buffSize == 0 ? 2000 : buffSize, FALSE); if(m_droplet.hasScript()) { // By setting both to the same value we prevent rereading // or compiling m_script = _T("[compiled script]"); SetDlgItemText(IDC_SCRIPT, m_script); } m_inited = true; updateControls(); } // onInitDialog: ----------------------------------------------------------- // Dialog initialization LRESULT ProcessPage::onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { loadData(); return 0; } // onInitDialog: ----------------------------------------------------------- // Broadcast by sheet when required to reread droplet info LRESULT ProcessPage::onQuerySiblings(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { loadData(); return 0; } // onInitDialog: ----------------------------------------------------------- // Save droplet data from page LRESULT ProcessPage::onApply(int idCtrl, LPNMHDR pnmh, BOOL& bHandled) { string script; GetDlgItemText(IDC_SCRIPT, script.get_buffer(MAX_PATH), MAX_PATH); script.release_buffer(); // Make sure we have a script if(script.empty()) { errorMessage(m_hWnd, 0, _T("You need to provide the filename of a rep script.")); ::SetFocus(GetDlgItem(IDC_SCRIPT)); SetModified(TRUE); return PSNRET_INVALID; } // If the the script changed if(script != m_script) { // Then recompile it if(SUCCEEDED(compileScript(script))) { m_script = script; } else { // By setting dirty we signal an error to sheet SetModified(TRUE); return PSNRET_INVALID; } } // Do buffer thing if(IsDlgButtonChecked(IDC_USEFILE)) { // Process entire file at once m_droplet.setBuffSize(0); } else { // Get the actual buffer size BOOL translated; UINT buffSize = GetDlgItemInt(IDC_CHUNK, &translated, FALSE); if(buffSize < 16 || !translated) { errorMessage(m_hWnd, 0, _T("The chunk size must be a valid number greater than 16.")); ::SetFocus(GetDlgItem(IDC_CHUNK)); SetModified(TRUE); return PSNRET_INVALID; } m_droplet.setBuffSize(buffSize); } return PSNRET_NOERROR; } // onChange: -------------------------------------------------------------- // Signal dirty to parent sheet LRESULT ProcessPage::onChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { if(m_inited) { SetModified(TRUE); updateControls(); } return 0; } // updateControls: -------------------------------------------------------- // Make sure page remains valid when changes happen void ProcessPage::updateControls() { ::EnableWindow(GetDlgItem(IDC_CHUNK), IsDlgButtonChecked(IDC_USECHUNK)); } // onBrowse: -------------------------------------------------------------- // Browse for a script file LRESULT ProcessPage::onBrowse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { string file; OPENFILENAME ofn; memset(&ofn, 0, sizeof(ofn)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = m_hWnd; ofn.lpstrFilter = _T("Rep Files (*.rep)\0*.rep\0All Files (*.*)\0*.*\0\0"); ofn.lpstrFile = file.get_buffer(MAX_PATH); ofn.nMaxFile = MAX_PATH; ofn.lpstrFileTitle = _T("Choose Rep Script"); ofn.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_PATHMUSTEXIST; ofn.lpstrDefExt = _T("rep");; if(GetOpenFileName(&ofn)) { file.release_buffer(); SetDlgItemText(IDC_SCRIPT, file); SetModified(TRUE); updateControls(); } return 0; }