summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/debugger/osx/debugview.mm
blob: 540fa41815d0c69c05a077b7c0577d36c9eb7305 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
//============================================================
//
//  debugview.m - MacOS X Cocoa debug window handling
//
//============================================================

#import "debugview.h"

#include "emu.h"
#include "debugger.h"
#include "debug/debugcon.h"
#include "debug/debugcpu.h"

#include "modules/lib/osdobj_common.h"

#include "util/xmlfile.h"

#include <cstring>


static NSColor *DefaultForeground;
static NSColor *ChangedForeground;
static NSColor *InvalidForeground;
static NSColor *CommentForeground;
static NSColor *DisabledChangedForeground;
static NSColor *DisabledInvalidForeground;
static NSColor *DisabledCommentForeground;

static NSColor *DefaultBackground;
static NSColor *VisitedBackground;
static NSColor *AncillaryBackground;
static NSColor *SelectedBackground;
static NSColor *CurrentBackground;
static NSColor *SelectedCurrentBackground;
static NSColor *InactiveSelectedBackground;
static NSColor *InactiveSelectedCurrentBackground;

static NSCharacterSet *NonWhiteCharacters;


static void debugwin_view_update(debug_view &view, void *osdprivate)
{
	NSAutoreleasePool *const pool = [[NSAutoreleasePool alloc] init];
	[(MAMEDebugView *)osdprivate update];
	[pool release];
}


@implementation MAMEDebugView

+ (void)initialize {
	// 10.15 and better get full adaptive Dark Mode support
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
	DefaultForeground = [[NSColor textColor] retain];
	ChangedForeground = [[NSColor systemRedColor] retain];
	CommentForeground = [[NSColor systemGreenColor] retain];
	// DCA_INVALID and DCA_DISABLED currently are not set by the core, so these 4 are unused
	InvalidForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:1.0] retain];
	DisabledChangedForeground = [[NSColor colorWithCalibratedRed:0.5 green:0.125 blue:0.125 alpha:1.0] retain];
	DisabledInvalidForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.5 alpha:1.0] retain];
	DisabledCommentForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.25 blue:0.0 alpha:1.0] retain];

	DefaultBackground = [[NSColor textBackgroundColor] retain];
	VisitedBackground = [[NSColor systemTealColor] retain];
	AncillaryBackground = [[NSColor unemphasizedSelectedContentBackgroundColor] retain];
	SelectedBackground = [[NSColor selectedContentBackgroundColor] retain];
	CurrentBackground = [[NSColor selectedControlColor] retain];
	SelectedCurrentBackground = [[NSColor unemphasizedSelectedContentBackgroundColor] retain];
	InactiveSelectedBackground = [[NSColor unemphasizedSelectedContentBackgroundColor] retain];
	InactiveSelectedCurrentBackground = [[NSColor systemGrayColor] retain];
#else
	DefaultForeground = [[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] retain];
	ChangedForeground = [[NSColor colorWithCalibratedRed:0.875 green:0.0 blue:0.0 alpha:1.0] retain];
	InvalidForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:1.0] retain];
	CommentForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.375 blue:0.0 alpha:1.0] retain];
	DisabledChangedForeground = [[NSColor colorWithCalibratedRed:0.5 green:0.125 blue:0.125 alpha:1.0] retain];
	DisabledInvalidForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.5 alpha:1.0] retain];
	DisabledCommentForeground = [[NSColor colorWithCalibratedRed:0.0 green:0.25 blue:0.0 alpha:1.0] retain];

	DefaultBackground = [[NSColor colorWithCalibratedWhite:1.0 alpha:1.0] retain];
	VisitedBackground = [[NSColor colorWithCalibratedRed:0.75 green:1.0 blue:0.75 alpha:1.0] retain];
	AncillaryBackground = [[NSColor colorWithCalibratedWhite:0.75 alpha:1.0] retain];
	SelectedBackground = [[NSColor colorWithCalibratedRed:0.75 green:0.875 blue:1.0 alpha:1.0] retain];
	CurrentBackground = [[NSColor colorWithCalibratedRed:1.0 green:0.75 blue:0.75 alpha:1.0] retain];
	SelectedCurrentBackground = [[NSColor colorWithCalibratedRed:0.875 green:0.625 blue:0.875 alpha:1.0] retain];
	InactiveSelectedBackground = [[NSColor colorWithCalibratedWhite:0.875 alpha:1.0] retain];
	InactiveSelectedCurrentBackground = [[NSColor colorWithCalibratedRed:0.875 green:0.5 blue:0.625 alpha:1.0] retain];
#endif

	NonWhiteCharacters = [[[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet] retain];
}


- (NSColor *)foregroundForAttribute:(uint8_t)attrib {
	if (attrib & DCA_COMMENT)
		return (attrib & DCA_DISABLED) ? DisabledCommentForeground : CommentForeground;
	else if (attrib & DCA_INVALID)
		return (attrib & DCA_DISABLED) ? DisabledInvalidForeground : InvalidForeground;
	else if (attrib & DCA_CHANGED)
		return (attrib & DCA_DISABLED) ? DisabledChangedForeground : ChangedForeground;
	else
		return DefaultForeground;
}


- (NSColor *)backgroundForAttribute:(uint8_t)attrib {
	BOOL const active = [[self window] isKeyWindow] && ([[self window] firstResponder] == self);
	if ((attrib & DCA_SELECTED) && (attrib & DCA_CURRENT))
		return active ? SelectedCurrentBackground : InactiveSelectedCurrentBackground;
	else if (attrib & DCA_CURRENT)
		return CurrentBackground;
	else if (attrib & DCA_SELECTED)
		return active ? SelectedBackground : InactiveSelectedBackground;
	else if (attrib & DCA_ANCILLARY)
		return AncillaryBackground;
	else if (attrib & DCA_VISITED)
		return VisitedBackground;
	else
		return DefaultBackground;
}


- (debug_view_xy)convertLocation:(NSPoint)location {
	debug_view_xy position;

	position.y = lround(floor(location.y / fontHeight));
	if (position.y < 0)
		position.y = 0;
	else if (position.y >= totalHeight)
		position.y = totalHeight - 1;

	debug_view_xy const origin = view->visible_position();
	debug_view_xy const size = view->visible_size();
	debug_view_char const *data = view->viewdata();
	if (!data || (position.y < origin.y) || (position.y >= origin.y + size.y))
	{
		// y coordinate outside visible area, x will be a guess
		position.x = lround(floor((location.x - [textContainer lineFragmentPadding]) / fontWidth));
	}
	else
	{
		data += ((position.y - view->visible_position().y) * view->visible_size().x);
		int         attr = -1;
		NSUInteger  start = 0, length = 0;
		for (uint32_t col = origin.x; col < origin.x + size.x; col++)
		{
			[[text mutableString] appendFormat:@"%c", data[col - origin.x].byte];
			if ((start < length) && (attr != data[col - origin.x].attrib))
			{
				NSRange const run = NSMakeRange(start, length - start);
				[text addAttribute:NSFontAttributeName
							 value:font
							 range:NSMakeRange(0, length)];
				[text addAttribute:NSForegroundColorAttributeName
							 value:[self foregroundForAttribute:attr]
							 range:run];
				start = length;
			}
			attr = data[col - origin.x].attrib;
			length = [text length];
		}
		if (start < length)
		{
			NSRange const run = NSMakeRange(start, length - start);
			[text addAttribute:NSFontAttributeName
						 value:font
						 range:NSMakeRange(0, length)];
			[text addAttribute:NSForegroundColorAttributeName
						 value:[self foregroundForAttribute:attr]
						 range:run];
		}
		CGFloat fraction;
		NSUInteger const glyph = [layoutManager glyphIndexForPoint:NSMakePoint(location.x, fontHeight / 2)
												   inTextContainer:textContainer
									fractionOfDistanceThroughGlyph:&fraction];
		position.x = [layoutManager characterIndexForGlyphAtIndex:glyph]; // FIXME: assumes 1:1 character mapping
		[text deleteCharactersInRange:NSMakeRange(0, length)];
	}
	if (position.x < 0)
		position.x = 0;
	else if (position.x >= totalWidth)
		position.x = totalWidth - 1;

	return position;
}


- (void)convertBounds:(NSRect)b toFirstAffectedLine:(int32_t *)f count:(int32_t *)c {
	*f = lround(floor(b.origin.y / fontHeight));
	*c = lround(ceil((b.origin.y + b.size.height) / fontHeight)) - *f;
}


- (void)recomputeVisible {
	// this gets all the lines that are at least partially visible
	debug_view_xy origin(0, 0), size(totalWidth, totalHeight);
	[self convertBounds:[self visibleRect] toFirstAffectedLine:&origin.y count:&size.y];
	size.y = std::min(size.y, totalHeight - origin.y);

	// tell the underlying view how much real estate is available
	view->set_visible_size(size);
	view->set_visible_position(origin);
	originTop = origin.y;
}


- (void)typeCharacterAndScrollToCursor:(char)ch {
	debug_view_xy const oldPos = view->cursor_position();
	view->process_char(ch);
	if (view->cursor_supported())
	{
		debug_view_xy const newPos = view->cursor_position();
		if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
		{
			// FIXME - use proper font metrics
			[self scrollRectToVisible:NSMakeRect((newPos.x * fontWidth) + [textContainer lineFragmentPadding],
												 newPos.y * fontHeight,
												 fontWidth,
												 fontHeight)];
		}
	}
}


- (void)adjustSizeAndRecomputeVisible {
	NSScrollView *const scroller = [self enclosingScrollView];
	NSSize const clip = [[scroller contentView] bounds].size;
	NSSize content = NSMakeSize((fontWidth * totalWidth) + (2 * [textContainer lineFragmentPadding]),
								fontHeight * totalHeight);
	if (wholeLineScroll)
		content.height += (fontHeight * 2) - 1;
	[self setFrameSize:NSMakeSize(ceil(std::max(clip.width, content.width)),
								  ceil(std::max(clip.height, content.height)))];
	[self recomputeVisible];
	[scroller reflectScrolledClipView:[scroller contentView]];
}


+ (NSFont *)defaultFontForMachine:(running_machine &)m {
	osd_options const &options = downcast<osd_options const &>(m.options());
	char const *const face = options.debugger_font();
	float const size = options.debugger_font_size();

	NSFont *const result = (('\0' != *face) && (0 != strcmp(OSDOPTVAL_AUTO, face)))
						 ? [NSFont fontWithName:[NSString stringWithUTF8String:face] size:MAX(0, size)]
						 : nil;

	return (nil != result) ? result : [NSFont userFixedPitchFontOfSize:MAX(0, size)];
}


- (id)initWithFrame:(NSRect)f type:(debug_view_type)t machine:(running_machine &)m wholeLineScroll:(BOOL)w {
	if (!(self = [super initWithFrame:f]))
		return nil;
	type = t;
	machine = &m;
	view = machine->debug_view().alloc_view((debug_view_type)type, debugwin_view_update, self);
	if (view == nil) {
		[self release];
		return nil;
	}
	wholeLineScroll = w;
	debug_view_xy const size = view->total_size();
	totalWidth = size.x;
	totalHeight = size.y;
	originTop = 0;

	text = [[NSTextStorage alloc] init];
	textContainer = [[NSTextContainer alloc] init];
	layoutManager = [[NSLayoutManager alloc] init];
	[layoutManager addTextContainer:textContainer];
	[textContainer release];
	[text addLayoutManager:layoutManager];
	[layoutManager release];

	[self setFont:[[self class] defaultFontForMachine:m]];

	NSMenu *contextMenu = [[NSMenu alloc] initWithTitle:@"Context"];
	[self addContextMenuItemsToMenu:contextMenu];
	[self setMenu:contextMenu];
	[contextMenu release];

	return self;
}


- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	if (view != nullptr) machine->debug_view().free_view(*view);
	if (font != nil) [font release];
	if (text != nil) [text release];
	[super dealloc];
}


- (void)update {
	// resize our frame if the total size has changed
	debug_view_xy const newSize = view->total_size();
	BOOL const resized = (newSize.x != totalWidth) || (newSize.y != totalHeight);
	if (resized)
	{
		NSScrollView *const scroller = [self enclosingScrollView];
		if (scroller)
		{
			NSSize const clip = [[scroller contentView] bounds].size;
			NSSize content = NSMakeSize((fontWidth * newSize.x) + (2 * [textContainer lineFragmentPadding]),
										fontHeight * newSize.y);
			if (wholeLineScroll)
				content.height += (fontHeight * 2) - 1;
			[self setFrameSize:NSMakeSize(ceil(std::max(clip.width, content.width)),
										  ceil(std::max(clip.height, content.height)))];
			[scroller reflectScrolledClipView:[scroller contentView]];
		}
		totalWidth = newSize.x;
		totalHeight = newSize.y;
	}

	// scroll the view if we're being told to
	debug_view_xy const newOrigin = view->visible_position();
	if (newOrigin.y != originTop)
	{
		NSRect const visible = [self visibleRect];
		NSPoint scroll = NSMakePoint(visible.origin.x, newOrigin.y * fontHeight);
		[self scrollPoint:scroll];
		originTop = newOrigin.y;
	}

	// mark as dirty
	[self setNeedsDisplay:YES];
}


- (NSSize)maximumFrameSize {
	debug_view_xy const max = view->total_size();
	return NSMakeSize(ceil((max.x * fontWidth) + (2 * [textContainer lineFragmentPadding])),
					  ceil((max.y + (wholeLineScroll ? 1 : 0)) * fontHeight));
}


- (NSFont *)font {
	return [[font retain] autorelease];
}


- (void)setFont:(NSFont *)f {
	[font autorelease];
	font = [f retain];
	fontWidth = [font maximumAdvancement].width;
	fontHeight = ceil([font ascender] - [font descender]);
	fontAscent = [font ascender];
	[[self enclosingScrollView] setLineScroll:fontHeight];
	totalWidth = totalHeight = 0;
	[self update];
}


- (BOOL)cursorSupported {
	return view->cursor_supported();
}


- (BOOL)cursorVisible {
	return view->cursor_visible();
}


- (debug_view_xy)cursorPosition {
	return view->cursor_position();
}


- (IBAction)copyVisible:(id)sender {
	debug_view_xy const size = view->visible_size();
	debug_view_char const *data = view->viewdata();
	if (!data)
	{
		NSBeep();
		return;
	}

	for (uint32_t row = 0; row < size.y; row++, data += size.x)
	{
		// add content for the line and set colours
		int         attr = -1;
		NSUInteger  start = [text length], length = start;
		for (uint32_t col = 0; col < size.x; col++)
		{
			[[text mutableString] appendFormat:@"%c", data[col].byte];
			if ((start < length) && (attr != (data[col].attrib & ~DCA_SELECTED)))
			{
				NSRange const run = NSMakeRange(start, length - start);
				[text addAttribute:NSForegroundColorAttributeName
							 value:[self foregroundForAttribute:attr]
							 range:run];
				[text addAttribute:NSBackgroundColorAttributeName
							 value:[self backgroundForAttribute:attr]
							 range:run];
				start = length;
			}
			attr = data[col].attrib & ~DCA_SELECTED;
			length = [text length];
		}

		// clean up trailing whitespace
		NSRange trim = [[text string] rangeOfCharacterFromSet:NonWhiteCharacters
													  options:NSBackwardsSearch
														range:NSMakeRange(start, length - start)];
		if (trim.location != NSNotFound)
			trim = [[text string] rangeOfComposedCharacterSequenceAtIndex:(trim.location + trim.length - 1)];
		else if (start > 0)
			trim = [[text string] rangeOfComposedCharacterSequenceAtIndex:(start - 1)];
		else
			trim = NSMakeRange(start, 0);
		trim.location += trim.length;
		trim.length = length - trim.location;
		[text deleteCharactersInRange:trim];

		// add the line ending and set colours
		[[text mutableString] appendString:@"\n"];
		NSRange const run = NSMakeRange(start, [text length] - start);
		[text addAttribute:NSForegroundColorAttributeName
					 value:[self foregroundForAttribute:attr]
					 range:run];
		[text addAttribute:NSBackgroundColorAttributeName
					 value:[self backgroundForAttribute:attr]
					 range:run];
	}

	// set the font and send it to the pasteboard
	NSRange const run = NSMakeRange(0, [text length]);
	[text addAttribute:NSFontAttributeName value:font range:run];
	NSPasteboard *const board = [NSPasteboard generalPasteboard];
	[board declareTypes:[NSArray arrayWithObject:NSRTFPboardType] owner:nil];
	[board setData:[text RTFFromRange:run documentAttributes:[NSDictionary dictionary]] forType:NSRTFPboardType];
	[text deleteCharactersInRange:run];
}


- (IBAction)paste:(id)sender {
	NSPasteboard *const board = [NSPasteboard generalPasteboard];
	NSString *const avail = [board availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]];
	if (avail == nil)
	{
		NSBeep();
		return;
	}

	NSData *const data = [[board stringForType:avail] dataUsingEncoding:NSASCIIStringEncoding
												   allowLossyConversion:YES];
	char const *const bytes = (char const *)[data bytes];
	debug_view_xy const oldPos = view->cursor_position();
	for (NSUInteger i = 0, l = [data length]; i < l; i++)
		view->process_char(bytes[i]);
	if (view->cursor_supported() && view->cursor_visible())
	{
		debug_view_xy const newPos = view->cursor_position();
		if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
		{
			// FIXME - use proper font metrics
			[self scrollRectToVisible:NSMakeRect((newPos.x * fontWidth) + [textContainer lineFragmentPadding],
												 newPos.y * fontHeight,
												 fontWidth,
												 fontHeight)];
		}
	}
}


- (void)viewBoundsDidChange:(NSNotification *)notification {
	NSView *const changed = [notification object];
	if (changed == [[self enclosingScrollView] contentView])
		[self adjustSizeAndRecomputeVisible];
}


- (void)viewFrameDidChange:(NSNotification *)notification {
	NSView *const changed = [notification object];
	if (changed == [[self enclosingScrollView] contentView])
		[self adjustSizeAndRecomputeVisible];
}


- (void)windowDidBecomeKey:(NSNotification *)notification {
	NSWindow *const win = [notification object];
	if ((win == [self window]) && ([win firstResponder] == self) && view->cursor_supported())
		[self setNeedsDisplay:YES];
}


- (void)windowDidResignKey:(NSNotification *)notification {
	NSWindow *const win = [notification object];
	if ((win == [self window]) && ([win firstResponder] == self) && view->cursor_supported())
		[self setNeedsDisplay:YES];
}


- (void)addContextMenuItemsToMenu:(NSMenu *)menu {
	NSMenuItem  *item;

	item = [menu addItemWithTitle:@"Copy Visible"
						   action:@selector(copyVisible:)
					keyEquivalent:@""];
	[item setTarget:self];

	item = [menu addItemWithTitle:@"Paste"
						   action:@selector(paste:)
					keyEquivalent:@""];
	[item setTarget:self];
}


- (void)saveConfigurationToNode:(util::xml::data_node *)node {
	if (view->cursor_supported())
	{
		util::xml::data_node *const selection = node->add_child(osd::debugger::NODE_WINDOW_SELECTION, nullptr);
		if (selection)
		{
			debug_view_xy const pos = view->cursor_position();
			selection->set_attribute_int(osd::debugger::ATTR_SELECTION_CURSOR_VISIBLE, view->cursor_visible() ? 1 : 0);
			selection->set_attribute_int(osd::debugger::ATTR_SELECTION_CURSOR_X, pos.x);
			selection->set_attribute_int(osd::debugger::ATTR_SELECTION_CURSOR_Y, pos.y);
		}
	}

	util::xml::data_node *const scroll = node->add_child(osd::debugger::NODE_WINDOW_SCROLL, nullptr);
	if (scroll)
	{
		NSRect const visible = [self visibleRect];
		scroll->set_attribute_float(osd::debugger::ATTR_SCROLL_ORIGIN_X, visible.origin.x);
		scroll->set_attribute_float(osd::debugger::ATTR_SCROLL_ORIGIN_Y, visible.origin.y);
	}
}


- (void)restoreConfigurationFromNode:(util::xml::data_node const *)node {
	if (view->cursor_supported())
	{
		util::xml::data_node const *const selection = node->get_child(osd::debugger::NODE_WINDOW_SELECTION);
		if (selection)
		{
			debug_view_xy pos = view->cursor_position();
			view->set_cursor_visible(0 != selection->get_attribute_int(osd::debugger::ATTR_SELECTION_CURSOR_VISIBLE, view->cursor_visible() ? 1 : 0));
			pos.x = selection->get_attribute_int(osd::debugger::ATTR_SELECTION_CURSOR_X, pos.x);
			pos.y = selection->get_attribute_int(osd::debugger::ATTR_SELECTION_CURSOR_Y, pos.y);
			view->set_cursor_position(pos);
		}
	}

	util::xml::data_node const *const scroll = node->get_child(osd::debugger::NODE_WINDOW_SCROLL);
	if (scroll)
	{
		NSRect visible = [self visibleRect];
		visible.origin.x = scroll->get_attribute_float(osd::debugger::ATTR_SCROLL_ORIGIN_X, visible.origin.x);
		visible.origin.y = scroll->get_attribute_float(osd::debugger::ATTR_SCROLL_ORIGIN_Y, visible.origin.y);
		[self scrollRectToVisible:visible];
	}
}


- (BOOL)acceptsFirstResponder {
	return view->cursor_supported();
}


- (BOOL)becomeFirstResponder {
	if (view->cursor_supported())
	{
		view->set_cursor_visible(true);
		[self setNeedsDisplay:YES];
		return [super becomeFirstResponder];
	}
	else
	{
		return NO;
	}
}


- (BOOL)resignFirstResponder {
	if (view->cursor_supported())
		[self setNeedsDisplay:YES];
	return [super resignFirstResponder];
}


- (void)viewDidMoveToSuperview {
	[super viewDidMoveToSuperview];

	[[NSNotificationCenter defaultCenter] removeObserver:self
													name:NSViewBoundsDidChangeNotification
												  object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self
													name:NSViewFrameDidChangeNotification
												  object:nil];

	NSScrollView *const scroller = [self enclosingScrollView];
	if (scroller != nil)
	{
		[scroller setLineScroll:fontHeight];
		[[scroller contentView] setPostsBoundsChangedNotifications:YES];
		[[scroller contentView] setPostsFrameChangedNotifications:YES];
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(viewBoundsDidChange:)
													 name:NSViewBoundsDidChangeNotification
												   object:[scroller contentView]];
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(viewFrameDidChange:)
													 name:NSViewFrameDidChangeNotification
												   object:[scroller contentView]];
		[self adjustSizeAndRecomputeVisible];
	}
}


- (void)viewDidMoveToWindow {
	[super viewDidMoveToWindow];

	[[NSNotificationCenter defaultCenter] removeObserver:self
													name:NSWindowDidBecomeKeyNotification
												  object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self
													name:NSWindowDidResignKeyNotification
												  object:nil];

	if ([self window] != nil)
	{
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(windowDidBecomeKey:)
													 name:NSWindowDidBecomeKeyNotification
												   object:[self window]];
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(windowDidResignKey:)
													 name:NSWindowDidResignKeyNotification
												   object:[self window]];
		[self setNeedsDisplay:YES];
	}
}


- (BOOL)isFlipped {
	return YES;
}


- (BOOL)isOpaque {
	return YES;
}


- (NSRect)adjustScroll:(NSRect)proposedVisibleRect {
	if (wholeLineScroll)
	{
		CGFloat const clamp = [self bounds].size.height - fontHeight - proposedVisibleRect.size.height;
		proposedVisibleRect.origin.y = std::min(proposedVisibleRect.origin.y, std::max(clamp, CGFloat(0)));
		proposedVisibleRect.origin.y -= fmod(proposedVisibleRect.origin.y, fontHeight);
	}
	return proposedVisibleRect;
}


- (void)drawRect:(NSRect)dirtyRect {
	// work out what's available
	[self recomputeVisible];
	debug_view_xy const origin = view->visible_position();
	debug_view_xy const size = view->visible_size();

	// work out how much we need to draw
	int32_t row, clip;
	[self convertBounds:dirtyRect toFirstAffectedLine:&row count:&clip];
	clip += row;
	row = std::max(row, origin.y);
	clip = std::min(clip, origin.y + size.y);

	// this gets the text for the whole visible area
	debug_view_char const *data = view->viewdata();
	if (!data)
		return;

	// clear any space above the available content
	data += ((row - origin.y) * size.x);
	if (dirtyRect.origin.y < (row * fontHeight))
	{
		[DefaultBackground set];
		[NSBezierPath fillRect:NSMakeRect(0,
										  dirtyRect.origin.y,
										  [self bounds].size.width,
										  (row * fontHeight) - dirtyRect.origin.y)];
	}

	// render entire lines to get character alignment right
	for ( ; row < clip; row++, data += size.x)
	{
		int         attr = -1;
		NSUInteger  start = 0, length = 0;
		for (uint32_t col = origin.x; col < origin.x + size.x; col++)
		{
			[[text mutableString] appendFormat:@"%C", unichar(data[col - origin.x].byte)];
			if ((start < length) && (attr != data[col - origin.x].attrib))
			{
				NSRange const run = NSMakeRange(start, length - start);
				[text addAttribute:NSFontAttributeName
							 value:font
							 range:NSMakeRange(0, length)];
				[text addAttribute:NSForegroundColorAttributeName
							 value:[self foregroundForAttribute:attr]
							 range:run];
				NSRange const glyphs = [layoutManager glyphRangeForCharacterRange:run
															 actualCharacterRange:NULL];
				NSRect box = [layoutManager boundingRectForGlyphRange:glyphs
													  inTextContainer:textContainer];
				if (start == 0)
				{
					box.size.width += box.origin.x;
					box.origin.x = 0;
				}
				[[self backgroundForAttribute:attr] set];
				[NSBezierPath fillRect:NSMakeRect(box.origin.x,
												  row * fontHeight,
												  box.size.width,
												  fontHeight)];
				start = length;
			}
			attr = data[col - origin.x].attrib;
			length = [text length];
		}
		NSRange const run = NSMakeRange(start, length - start);
		[text addAttribute:NSFontAttributeName
					 value:font
					 range:NSMakeRange(0, length)];
		[text addAttribute:NSForegroundColorAttributeName
					 value:[self foregroundForAttribute:attr]
					 range:run];
		NSRange const glyphs = [layoutManager glyphRangeForCharacterRange:run
													 actualCharacterRange:NULL];
		NSRect box = [layoutManager boundingRectForGlyphRange:glyphs
											  inTextContainer:textContainer];
		if (start == 0)
			box.origin.x = 0;
		box.size.width = std::max([self bounds].size.width - box.origin.x, CGFloat(0));
		[[self backgroundForAttribute:attr] set];
		[NSBezierPath fillRect:NSMakeRect(box.origin.x,
										  row * fontHeight,
										  box.size.width,
										  fontHeight)];
		[layoutManager drawGlyphsForGlyphRange:[layoutManager glyphRangeForTextContainer:textContainer]
									   atPoint:NSMakePoint(0, row * fontHeight)];
		[text deleteCharactersInRange:NSMakeRange(0, length)];
	}

	// clear any space below the available content
	if ((dirtyRect.origin.y + dirtyRect.size.height) > (row * fontHeight))
	{
		[DefaultBackground set];
		[NSBezierPath fillRect:NSMakeRect(0,
										  row * fontHeight,
										  [self bounds].size.width,
										  (dirtyRect.origin.y + dirtyRect.size.height) - (row * fontHeight))];
	}
}


- (void)mouseDown:(NSEvent *)event {
	NSPoint const location = [self convertPoint:[event locationInWindow] fromView:nil];
	NSUInteger const modifiers = [event modifierFlags];
	view->process_click(((modifiers & NSCommandKeyMask) && [[self window] isMainWindow]) ? DCK_RIGHT_CLICK
					  : (modifiers & NSAlternateKeyMask) ? DCK_MIDDLE_CLICK
					  : DCK_LEFT_CLICK,
						[self convertLocation:location]);
}


- (void)mouseDragged:(NSEvent *)event {
	[self autoscroll:event];
	NSPoint const location = [self convertPoint:[event locationInWindow] fromView:nil];
	NSUInteger const modifiers = [event modifierFlags];
	if (view->cursor_supported()
	 && !(modifiers & NSAlternateKeyMask)
	 && (!(modifiers & NSCommandKeyMask) || ![[self window] isMainWindow]))
	{
		view->set_cursor_position([self convertLocation:location]);
		view->set_cursor_visible(true);
		[self setNeedsDisplay:YES];
	}
}


- (void)rightMouseDown:(NSEvent *)event {
	NSPoint const location = [self convertPoint:[event locationInWindow] fromView:nil];
	if (view->cursor_supported())
	{
		view->set_cursor_position([self convertLocation:location]);
		view->set_cursor_visible(true);
		[self setNeedsDisplay:YES];
	}
	[super rightMouseDown:event];
}


- (void)keyDown:(NSEvent *)event {
	NSUInteger  modifiers = [event modifierFlags];
	NSString    *str = [event charactersIgnoringModifiers];

	if ([str length] == 1)
	{
		if (modifiers & NSNumericPadKeyMask)
		{
			switch ([str characterAtIndex:0])
			{
			case NSUpArrowFunctionKey:
				if (modifiers & NSCommandKeyMask)
					view->process_char(DCH_CTRLHOME);
				else
					view->process_char(DCH_UP);
				return;
			case NSDownArrowFunctionKey:
				if (modifiers & NSCommandKeyMask)
					view->process_char(DCH_CTRLEND);
				else
					view->process_char(DCH_DOWN);
				return;
			case NSLeftArrowFunctionKey:
				if (modifiers & NSCommandKeyMask)
					[self typeCharacterAndScrollToCursor:DCH_HOME];
				else if (modifiers & NSAlternateKeyMask)
					[self typeCharacterAndScrollToCursor:DCH_CTRLLEFT];
				else
					[self typeCharacterAndScrollToCursor:DCH_LEFT];
				return;
			case NSRightArrowFunctionKey:
				if (modifiers & NSCommandKeyMask)
					[self typeCharacterAndScrollToCursor:DCH_END];
				else if (modifiers & NSAlternateKeyMask)
					[self typeCharacterAndScrollToCursor:DCH_CTRLRIGHT];
				else
					[self typeCharacterAndScrollToCursor:DCH_RIGHT];
				return;
			default:
				[self interpretKeyEvents:[NSArray arrayWithObject:event]];
				return;
			}
		}
		else if (modifiers & NSFunctionKeyMask)
		{
			switch ([str characterAtIndex:0])
			{
				case NSPageUpFunctionKey:
					if (modifiers & NSAlternateKeyMask)
					{
						view->process_char(DCH_PUP);
						return;
					}
				case NSPageDownFunctionKey:
					if (modifiers & NSAlternateKeyMask)
					{
						view->process_char(DCH_PDOWN);
						return;
					}
				default:
					;
			}
			[super keyDown:event];
			return;
		}
	}
	[self interpretKeyEvents:[NSArray arrayWithObject:event]];
}


- (void)keyUp:(NSEvent *)event {
	if (view->cursor_supported() && view->cursor_visible())
	{
		debug_view_xy const pos = view->cursor_position();
		[self scrollRectToVisible:NSMakeRect((pos.x * fontWidth) + [textContainer lineFragmentPadding],
											 pos.y * fontHeight,
											 fontWidth,
											 fontHeight)]; // FIXME: metrics
	}
}


- (void)insertTab:(id)sender {
	if ([[self window] firstResponder] == self)
		[[self window] selectNextKeyView:self];
}


- (void)insertBacktab:(id)sender {
	if ([[self window] firstResponder] == self)
		[[self window] selectPreviousKeyView:self];
}


- (void)insertNewline:(id)sender {
	machine->debugger().console().get_visible_cpu()->debug()->single_step();
}


- (void)insertText:(id)string {
	NSUInteger  len;
	NSRange     found;
	if ([string isKindOfClass:[NSAttributedString class]])
		string = [string string];
	for (len = [string length], found = NSMakeRange(0, 0);
		 found.location < len;
		 found.location += found.length) {
		found = [string rangeOfComposedCharacterSequenceAtIndex:found.location];
		if (found.length == 1) {
			unichar ch = [string characterAtIndex:found.location];
			if ((ch >= 32) && (ch < 127))
				[self typeCharacterAndScrollToCursor:ch];
		}
	}
}


- (BOOL)validateMenuItem:(NSMenuItem *)item {
	SEL action = [item action];

	if (action == @selector(paste:))
	{
		NSPasteboard *const board = [NSPasteboard generalPasteboard];
		return [board availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]] != nil;
	}
	else
	{
		return YES;
	}
}

@end