summaryrefslogtreecommitdiffstats
path: root/qt/qextscintillamacro.cpp
blob: 5cbc7f0aef5493e5844a3a91dcb8ae5db4b18ff4 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// This module implements the QextScintillaMacro class.
//
// Copyright (c) 2006
// 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
// 
// This file is part of TQScintilla.
// 
// This copy of TQScintilla is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option) any
// later version.
// 
// TQScintilla is supplied in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
// 
// You should have received a copy of the GNU General Public License along with
// TQScintilla; see the file LICENSE.  If not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#include <string.h>

#include <tqstring.h>

#include "qextscintillamacro.h"
#include "qextscintilla.h"


static TQCString extract(const TQCString &asc,int &start);
static int fromHex(unsigned char ch);


// The ctor.
QextScintillaMacro::QextScintillaMacro(QextScintilla *parent,const char *name)
			: TQObject(parent,name), qsci(parent)
{
}


// The ctor that initialises the macro.
QextScintillaMacro::QextScintillaMacro(const TQCString &asc,
				       QextScintilla *parent,const char *name)
			: TQObject(parent,name), qsci(parent)
{
	load(asc);
}


// The dtor.
QextScintillaMacro::~QextScintillaMacro()
{
}


// Clear the contents of the macro.
void QextScintillaMacro::clear()
{
	macro.clear();
}


// Read a macro from a string.
bool QextScintillaMacro::load(const TQCString &asc)
{
	bool rc = TRUE;

	macro.clear();

	int pos = 0;

	while (pos < asc.length())
	{
		TQCString fld;
		Macro cmd;
		unsigned len;

		// Extract the 3 fixed fields.
		fld = extract(asc,pos);
		cmd.msg = fld.toUInt(&rc);

		if (!rc)
			break;

		fld = extract(asc,pos);
		cmd.wParam = fld.toULong(&rc);

		if (!rc)
			break;

		fld = extract(asc,pos);
		len = fld.toUInt(&rc);

		if (!rc)
			break;

		// Extract any text.
		if (len)
		{
			cmd.text.resize(len);
			fld = extract(asc,pos);

			char *dp = cmd.text.data();
			const char *sp = fld;

			if (!sp)
			{
				rc = FALSE;
				break;
			}

			while (len--)
			{
				unsigned char ch;

				ch = *sp++;

				if (ch == '"' || ch <= ' ' || ch >= 0x7f)
				{
					rc = FALSE;
					break;
				}

				if (ch == '\\')
				{
					int b1, b2;

					if ((b1 = fromHex(*sp++)) < 0 ||
					    (b2 = fromHex(*sp++)) < 0)
					{
						rc = FALSE;
						break;
					}

					ch = (b1 << 4) + b2;
				}

				*dp++ = ch;
			}

			if (!rc)
				break;
		}

		macro.append(cmd);
	}
		
	if (!rc)
		macro.clear();

	return rc;
}


// Write a macro to a string.
TQCString QextScintillaMacro::save() const
{
	TQCString ms;

	for (TQValueList<Macro>::const_iterator it = macro.begin(); it != macro.end(); ++it)
	{
		if (!ms.isEmpty())
			ms += ' ';

		unsigned len = (*it).text.size();
		TQCString m;

		m.sprintf("%u %lu %u",(*it).msg,(*it).wParam,len);

		if (len)
		{
			m += ' ';

			const char *cp = (*it).text.data();

			while (len--)
			{
				unsigned char ch = *cp++;

				if (ch == '\\' || ch == '"' || ch <= ' ' || ch >= 0x7f)
				{
					char buf[4];

					sprintf(buf,"\\%02x",ch);
					m += buf;
				}
				else
					m += ch;
			}
		}

		ms += m;
	}

	return ms;
}


// Play the macro.
void QextScintillaMacro::play()
{
	if (!qsci)
		return;

	for (TQValueList<Macro>::const_iterator it = macro.begin(); it != macro.end(); ++it)
		qsci -> SendScintilla((*it).msg,(*it).wParam,(*it).text.data());
}


// Start recording.
void QextScintillaMacro::startRecording()
{
	if (!qsci)
		return;

	macro.clear();

	connect(qsci,
		TQT_SIGNAL(SCN_MACRORECORD(unsigned int,unsigned long,long)),
		TQT_SLOT(record(unsigned int,unsigned long,long)));

	qsci -> SendScintilla(QextScintillaBase::SCI_STARTRECORD);
}


// End recording.
void QextScintillaMacro::endRecording()
{
	if (!qsci)
		return;

	qsci -> SendScintilla(QextScintillaBase::SCI_STOPRECORD);
	qsci -> disconnect(this);
}


// Record a command.
void QextScintillaMacro::record(unsigned int msg,unsigned long wParam,
				long lParam)
{
	Macro m;

	m.msg = msg;
	m.wParam = wParam;

	// Determine commands which need special handling of the parameters.
	switch (msg)
	{
        case QextScintillaBase::SCI_ADDTEXT:
		m.text.duplicate(reinterpret_cast<const char *>(lParam),wParam);
		break;

        case QextScintillaBase::SCI_REPLACESEL:
		if (!macro.isEmpty() && macro.last().msg == QextScintillaBase::SCI_REPLACESEL)
		{
			const char *text = reinterpret_cast<const char *>(lParam);

			// This is the command used for ordinary user input so
			// it's a signifacant space reduction to append it to
			// the previous command.

			TQByteArray &ba = macro.last().text;

			unsigned pos = ba.size() - 1;

			// Make room for the new text.
			ba.resize(ba.size() + strlen(text));

			// Copy it in.
			strcpy(ba.data() + pos,text);

			return;
		}

		/* Drop through. */

        case QextScintillaBase::SCI_INSERTTEXT:
        case QextScintillaBase::SCI_APPENDTEXT:
        case QextScintillaBase::SCI_SEARCHNEXT:
        case QextScintillaBase::SCI_SEARCHPREV:
		{
			const char *text = reinterpret_cast<const char *>(lParam);

			m.text.duplicate(text,strlen(text) + 1);
			break;
		}
	}

	macro.append(m);
}


// Extract a macro field starting at the given position.
static TQCString extract(const TQCString &asc,int &fstart)
{
	TQCString f;

	if (fstart < asc.length())
	{
		int fend = asc.find(' ',fstart);

		if (fend < 0)
		{
			f = asc.mid(fstart);
			fstart = asc.length();
		}
		else
		{
			f = asc.mid(fstart,fend - fstart);
			fstart = fend + 1;
		}
	}

	return f;
}


// Return the given hex character as a binary.
static int fromHex(unsigned char ch)
{
	if (ch >= '0' && ch <= '9')
		return ch - '0';

	if (ch >= 'a' && ch <= 'f')
		return ch - 'a' + 10;

	return -1;
}

#include "qextscintillamacro.moc"