summaryrefslogtreecommitdiff
path: root/win32/makedrop/processpage.cpp
blob: be3951af4477a2e18e118db9acc15d8221396104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * 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: <nielsen@memberwebs.com>
 */

#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;
}