summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/debugger/qt/debugqtview.c
blob: 34146860f616407aed3676956b5fe12e60d2caa2 (plain) (blame)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#define NO_MEM_TRACKING

#include "debugqtview.h"

DebuggerView::DebuggerView(const debug_view_type& type,
							running_machine* machine,
							QWidget* parent) :
	QAbstractScrollArea(parent),
	m_preferBottom(false),
	m_view(NULL),
	m_machine(machine)
{
	// I like setting the font per-view since it doesn't override the menuing fonts.
	QFont viewFontRequest("Courier New");
	viewFontRequest.setFixedPitch(true);
	viewFontRequest.setPointSize(11);
	setFont(viewFontRequest);

	m_view = m_machine->debug_view().alloc_view(type,
												DebuggerView::debuggerViewUpdate,
												this);

	connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
			this, SLOT(verticalScrollSlot(int)));
	connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
			this, SLOT(horizontalScrollSlot(int)));
}


DebuggerView::~DebuggerView()
{
	if (m_machine && m_view)
		m_machine->debug_view().free_view(*m_view);
}

// TODO: remove this version no later than January 1, 2015
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
void DebuggerView::paintEvent(QPaintEvent* event)
{
	// Tell the MAME debug view how much real estate is available
	QFontMetrics actualFont = fontMetrics();
	const int fontWidth = MAX(1, actualFont.width('_'));
	const int fontHeight = MAX(1, actualFont.height());
	m_view->set_visible_size(debug_view_xy(width()/fontWidth, height()/fontHeight));


	// Handle the scroll bars
	const int horizontalScrollCharDiff = m_view->total_size().x - m_view->visible_size().x;
	const int horizontalScrollSize = horizontalScrollCharDiff < 0 ? 0 : horizontalScrollCharDiff;
	horizontalScrollBar()->setRange(0, horizontalScrollSize);

	// If the horizontal scroll bar appears, make sure to adjust the vertical scrollbar accordingly
	const int verticalScrollAdjust = horizontalScrollSize > 0 ? 1 : 0;

	const int verticalScrollCharDiff = m_view->total_size().y - m_view->visible_size().y;
	const int verticalScrollSize = verticalScrollCharDiff < 0 ? 0 : verticalScrollCharDiff+verticalScrollAdjust;
	bool atEnd = false;
	if (verticalScrollBar()->value() == verticalScrollBar()->maximum())
	{
		atEnd = true;
	}
	verticalScrollBar()->setRange(0, verticalScrollSize);
	if (m_preferBottom && atEnd)
	{
		verticalScrollBar()->setValue(verticalScrollSize);
	}


	// Draw the viewport widget
	QPainter painter(viewport());
	painter.fillRect(0, 0, width(), height(), QBrush(Qt::white));
	painter.setBackgroundMode(Qt::OpaqueMode);
	painter.setBackground(QColor(255,255,255));

	// Background control
	QBrush bgBrush;
	bgBrush.setStyle(Qt::SolidPattern);
	painter.setPen(QPen(QColor(0,0,0)));

	size_t viewDataOffset = 0;
	const debug_view_xy& visibleCharDims = m_view->visible_size();
	for (int y = 0; y < visibleCharDims.y; y++)
	{
		for (int x = 0; x < visibleCharDims.x; x++)
		{
			const unsigned char textAttr = m_view->viewdata()[viewDataOffset].attrib;

			if (x == 0 || textAttr != m_view->viewdata()[viewDataOffset-1].attrib)
			{
				// Text color handling
				QColor fgColor(0,0,0);
				QColor bgColor(255,255,255);

				if(textAttr & DCA_VISITED)
				{
					bgColor.setRgb(0xc6, 0xe2, 0xff);
				}
				if(textAttr & DCA_ANCILLARY)
				{
					bgColor.setRgb(0xe0, 0xe0, 0xe0);
				}
				if(textAttr & DCA_SELECTED)
				{
					bgColor.setRgb(0xff, 0x80, 0x80);
				}
				if(textAttr & DCA_CURRENT)
				{
					bgColor.setRgb(0xff, 0xff, 0x00);
				}
				if ((textAttr & DCA_SELECTED) && (textAttr & DCA_CURRENT))
				{
					bgColor.setRgb(0xff,0xc0,0x80);
				}
				if(textAttr & DCA_CHANGED)
				{
					fgColor.setRgb(0xff, 0x00, 0x00);
				}
				if(textAttr & DCA_INVALID)
				{
					fgColor.setRgb(0x00, 0x00, 0xff);
				}
				if(textAttr & DCA_DISABLED)
				{
					fgColor.setRgb((fgColor.red()   + bgColor.red())   >> 1,
									(fgColor.green() + bgColor.green()) >> 1,
									(fgColor.blue()  + bgColor.blue())  >> 1);
				}
				if(textAttr & DCA_COMMENT)
				{
					fgColor.setRgb(0x00, 0x80, 0x00);
				}

				bgBrush.setColor(bgColor);
				painter.setBackground(bgBrush);
				painter.setPen(QPen(fgColor));
			}

			// Your character is not guaranteed to take up the entire fontWidth x fontHeight, so fill before.
			painter.fillRect(x*fontWidth, y*fontHeight, fontWidth, fontHeight, bgBrush);

			// There is a touchy interplay between font height, drawing difference, visible position, etc
			// Fonts don't get drawn "down and to the left" like boxes, so some wiggling is needed.
			painter.drawText(x*fontWidth,
								(y*fontHeight + (fontHeight*0.80)),
								QString(m_view->viewdata()[viewDataOffset].byte));
			viewDataOffset++;
		}
	}
}
#else
void DebuggerView::paintEvent(QPaintEvent* event)
{
	// Tell the MAME debug view how much real estate is available
	QFontMetrics actualFont = fontMetrics();
	const int fontWidth = MAX(1, actualFont.width('_'));
	const int fontHeight = MAX(1, actualFont.height());
	m_view->set_visible_size(debug_view_xy(width()/fontWidth, height()/fontHeight));


	// Handle the scroll bars
	const int horizontalScrollCharDiff = m_view->total_size().x - m_view->visible_size().x;
	const int horizontalScrollSize = horizontalScrollCharDiff < 0 ? 0 : horizontalScrollCharDiff;
	horizontalScrollBar()->setRange(0, horizontalScrollSize);

	// If the horizontal scroll bar appears, make sure to adjust the vertical scrollbar accordingly
	const int verticalScrollAdjust = horizontalScrollSize > 0 ? 1 : 0;

	const int verticalScrollCharDiff = m_view->total_size().y - m_view->visible_size().y;
	const int verticalScrollSize = verticalScrollCharDiff < 0 ? 0 : verticalScrollCharDiff+verticalScrollAdjust;
	bool atEnd = false;
	if (verticalScrollBar()->value() == verticalScrollBar()->maximum())
	{
		atEnd = true;
	}
	verticalScrollBar()->setRange(0, verticalScrollSize);
	if (m_preferBottom && atEnd)
	{
		verticalScrollBar()->setValue(verticalScrollSize);
	}


	// Draw the viewport widget
	QPainter painter(viewport());
	painter.fillRect(0, 0, width(), height(), QBrush(Qt::white));
	painter.setBackgroundMode(Qt::OpaqueMode);
	painter.setBackground(QColor(255,255,255));

	// Background control
	QBrush bgBrush;
	bgBrush.setStyle(Qt::SolidPattern);
	painter.setPen(QPen(QColor(0,0,0)));

	size_t viewDataOffset = 0;
	const debug_view_xy& visibleCharDims = m_view->visible_size();
	const debug_view_char* viewdata = m_view->viewdata();
	for (int y = 0; y < visibleCharDims.y; y++)
	{
		int width = 1;
		for (int x = 0; x < visibleCharDims.x; viewDataOffset += width, x += width)
		{
			const unsigned char textAttr = viewdata[viewDataOffset].attrib;

			// Text color handling
			QColor fgColor(0,0,0);
			QColor bgColor(255,255,255);

			if(textAttr & DCA_VISITED)
			{
				bgColor.setRgb(0xc6, 0xe2, 0xff);
			}
			if(textAttr & DCA_ANCILLARY)
			{
				bgColor.setRgb(0xe0, 0xe0, 0xe0);
			}
			if(textAttr & DCA_SELECTED)
			{
				bgColor.setRgb(0xff, 0x80, 0x80);
			}
			if(textAttr & DCA_CURRENT)
			{
				bgColor.setRgb(0xff, 0xff, 0x00);
			}
			if ((textAttr & DCA_SELECTED) && (textAttr & DCA_CURRENT))
			{
				bgColor.setRgb(0xff,0xc0,0x80);
			}
			if(textAttr & DCA_CHANGED)
			{
				fgColor.setRgb(0xff, 0x00, 0x00);
			}
			if(textAttr & DCA_INVALID)
			{
				fgColor.setRgb(0x00, 0x00, 0xff);
			}
			if(textAttr & DCA_DISABLED)
			{
				fgColor.setRgb((fgColor.red()   + bgColor.red())   >> 1,
								(fgColor.green() + bgColor.green()) >> 1,
								(fgColor.blue()  + bgColor.blue())  >> 1);
			}
			if(textAttr & DCA_COMMENT)
			{
				fgColor.setRgb(0x00, 0x80, 0x00);
			}

			bgBrush.setColor(bgColor);
			painter.setBackground(bgBrush);
			painter.setPen(QPen(fgColor));

			QString text(QChar(viewdata[viewDataOffset].byte));
			for (width = 1; x + width < visibleCharDims.x; width++)
			{
				if (textAttr != viewdata[viewDataOffset + width].attrib)
					break;
				text.append(QChar(viewdata[viewDataOffset + width].byte));
			}

			// Your characters are not guaranteed to take up the entire length x fontWidth x fontHeight, so fill before.
			painter.fillRect(x*fontWidth, y*fontHeight, width*fontWidth, fontHeight, bgBrush);

			// There is a touchy interplay between font height, drawing difference, visible position, etc
			// Fonts don't get drawn "down and to the left" like boxes, so some wiggling is needed.
			painter.drawText(x*fontWidth, (y*fontHeight + (fontHeight*0.80)), text);
		}
	}
}
#endif

void DebuggerView::keyPressEvent(QKeyEvent* event)
{
	if (m_view == NULL)
		return QWidget::keyPressEvent(event);

	Qt::KeyboardModifiers keyMods = QApplication::keyboardModifiers();
	const bool ctrlDown = keyMods.testFlag(Qt::ControlModifier);

	int keyPress = -1;
	switch (event->key())
	{
		case Qt::Key_Up:
			keyPress = DCH_UP;
			break;
		case Qt::Key_Down:
			keyPress = DCH_DOWN;
			break;
		case Qt::Key_Left:
			keyPress = DCH_LEFT;
			if (ctrlDown) keyPress = DCH_CTRLLEFT;
			break;
		case Qt::Key_Right:
			keyPress = DCH_RIGHT;
			if (ctrlDown) keyPress = DCH_CTRLRIGHT;
			break;
		case Qt::Key_PageUp:
			keyPress = DCH_PUP;
			break;
		case Qt::Key_PageDown:
			keyPress = DCH_PDOWN;
			break;
		case Qt::Key_Home:
			keyPress = DCH_HOME;
			if (ctrlDown) keyPress = DCH_CTRLHOME;
			break;
		case Qt::Key_End:
			keyPress = DCH_END;
			if (ctrlDown) keyPress = DCH_CTRLEND;
			break;
		case Qt::Key_0: keyPress = '0'; break;
		case Qt::Key_1: keyPress = '1'; break;
		case Qt::Key_2: keyPress = '2'; break;
		case Qt::Key_3: keyPress = '3'; break;
		case Qt::Key_4: keyPress = '4'; break;
		case Qt::Key_5: keyPress = '5'; break;
		case Qt::Key_6: keyPress = '6'; break;
		case Qt::Key_7: keyPress = '7'; break;
		case Qt::Key_8: keyPress = '8'; break;
		case Qt::Key_9: keyPress = '9'; break;
		case Qt::Key_A: keyPress = 'a'; break;
		case Qt::Key_B: keyPress = 'b'; break;
		case Qt::Key_C: keyPress = 'c'; break;
		case Qt::Key_D: keyPress = 'd'; break;
		case Qt::Key_E: keyPress = 'e'; break;
		case Qt::Key_F: keyPress = 'f'; break;
		default:
			return QWidget::keyPressEvent(event);
	}

	m_view->set_cursor_visible(true);
	m_view->process_char(keyPress);

	// Catch the view up with the cursor
	verticalScrollBar()->setValue(m_view->visible_position().y);

	viewport()->update();
	update();
}


void DebuggerView::mousePressEvent(QMouseEvent* event)
{
	if (m_view == NULL)
		return;

	if (event->button() == Qt::LeftButton)
	{
		QFontMetrics actualFont = fontMetrics();
		const int fontWidth = MAX(1, actualFont.width('_'));
		const int fontHeight = MAX(1, actualFont.height());

		debug_view_xy topLeft = m_view->visible_position();
		debug_view_xy clickViewPosition;
		clickViewPosition.x = topLeft.x + (event->x() / fontWidth);
		clickViewPosition.y = topLeft.y + (event->y() / fontHeight);
		m_view->process_click(DCK_LEFT_CLICK, clickViewPosition);

		viewport()->update();
		update();
	}
}


void DebuggerView::verticalScrollSlot(int value)
{
	m_view->set_visible_position(debug_view_xy(horizontalScrollBar()->value(), value));
}


void DebuggerView::horizontalScrollSlot(int value)
{
	m_view->set_visible_position(debug_view_xy(value, verticalScrollBar()->value()));
}


void DebuggerView::debuggerViewUpdate(debug_view& debugView, void* osdPrivate)
{
	// Get a handle to the DebuggerView being updated & redraw
	DebuggerView* dView = (DebuggerView*)osdPrivate;
	dView->verticalScrollBar()->setValue(dView->view()->visible_position().y);
	dView->horizontalScrollBar()->setValue(dView->view()->visible_position().x);
	dView->viewport()->update();
	dView->update();
}