Add delete functionality.

This commit is contained in:
Nicholas Shanks 2002-03-31 17:02:40 +00:00
parent 46a62de11c
commit 92217f05c9
4 changed files with 155 additions and 3 deletions

View File

@ -27,7 +27,7 @@
char *bytes = (char *) [data bytes];
NSMutableString *representation = [NSMutableString string];
// draw bytes
// calculate bytes
for( row = 0; row < rows; row++ )
{
for( addr = 0; addr < bytesPerRow; addr++ )
@ -74,15 +74,17 @@
char *bytes = (char *) [data bytes];
NSMutableString *representation = [NSMutableString string];
// draw bytes
// calculate bytes
for( row = 0; row < rows; row++ )
{
for( addr = 0; addr < bytesPerRow; addr++ )
{
if( currentByte < dataLength )
{
if( bytes[currentByte] >= 0x20 && bytes[currentByte] < 0x7F )
if( bytes[currentByte] > 0x20 && bytes[currentByte] < 0x7F )
buffer[addr] = bytes[currentByte];
else if( bytes[currentByte] == 0x20 )
buffer[addr] = 0xCA; // nbsp to stop maligned wraps
else buffer[addr] = 0x2E; // full stop
// advance current byte
@ -149,6 +151,8 @@
{
byteRange = [self byteRangeFromAsciiRange:newSelectedCharRange];
hexRange = [self hexRangeFromByteRange:byteRange];
if( hexRange.length > 0 )
hexRange.length -= 1;
[hex setSelectedRange:hexRange];
}

View File

@ -1,5 +1,6 @@
#import <Cocoa/Cocoa.h>
#import "HexEditorDelegate.h"
#import "HexWindowController.h"
@interface HexTextView : NSTextView
{

View File

@ -9,6 +9,28 @@
return self;
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
/* if( [[self window] isKeyWindow] && [[self window] firstResponder] == self )
{
NSSetFocusRingStyle( NSFocusRingOnly );
[self setKeyboardFocusRingNeedsDisplayInRect:rect];
}*/
/* [super drawRect:rect];
if( [[self window] isKeyWindow] )
{
NSResponder *responder = [[self window] firstResponder];
if( [responder isKindOfClass:[NSView class]] && [(NSView *)responder isDescendantOf:self])
{
NSSetFocusRingStyle( NSFocusRingOnly );
NSRectFill( rect );
}
}
[self setKeyboardFocusRingNeedsDisplayInRect:rect];*/
}
- (void)setSelectedRange:(NSRange)charRange affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)flag
{
NSRange newRange = charRange;
@ -53,6 +75,130 @@
[super setSelectedRange:newRange affinity:affinity stillSelecting:NO];
}
/* NSResponder overrides */
- (IBAction)insertText:(NSString *)string
{
NSLog( @"Inserting text: %@", string );
/* if( hexWindow->editingHex ) // editing in hexadecimal
{
Boolean deletePrev = false; // delete prev typing to add new one
if( hexWindow->editedHigh ) // edited high bits already
{
// shift typed char into high bits and add new low char
if( charCode >= 0x30 && charCode <= 0x39 ) charCode -= 0x30; // 0 to 9
else if( charCode >= 0x61 && charCode <= 0x66 ) charCode -= 0x57; // a to f
else if( charCode >= 0x93 && charCode <= 0x98 ) charCode -= 0x8A; // A to F
else break;
hexWindow->hexChar <<= 4; // store high bit
hexWindow->hexChar += charCode & 0x0F; // add low bit
hexWindow->selStart += 1;
hexWindow->selEnd = hexWindow->selStart;
hexWindow->editedHigh = false;
deletePrev = true;
}
else // editing low bits
{
// put typed char into low bits
if( charCode >= 0x30 && charCode <= 0x39 ) charCode -= 0x30; // 0 to 9
else if( charCode >= 0x61 && charCode <= 0x66 ) charCode -= 0x57; // a to f
else if( charCode >= 0x93 && charCode <= 0x98 ) charCode -= 0x8A; // A to F
else break;
hexWindow->hexChar = charCode & 0x0F;
hexWindow->editedHigh = true;
}
hexWindow->InsertBytes( nil, hexWindow->selStart - hexWindow->selEnd, hexWindow->selEnd ); // remove selection
hexWindow->selEnd = hexWindow->selStart;
if( deletePrev )
{
hexWindow->InsertBytes( nil, -1, hexWindow->selStart ); // remove previous hex char
hexWindow->InsertBytes( &hexWindow->hexChar, 1, hexWindow->selStart -1 ); // insert typed char (bug fix hack)
}
else hexWindow->InsertBytes( &hexWindow->hexChar, 1, hexWindow->selStart ); // insert typed char
}
else // editing in ascii
{
hexWindow->InsertBytes( nil, hexWindow->selStart - hexWindow->selEnd, hexWindow->selEnd ); // remove selection
hexWindow->selEnd = hexWindow->selStart;
hexWindow->InsertBytes( &charCode, 1, hexWindow->selStart ); // insert typed char
hexWindow->selStart += 1;
hexWindow->selEnd = hexWindow->selStart;
}*/
}
- (IBAction)deleteBackward:(id)sender
{
NSRange selection = [self rangeForUserTextChange], byteSelection;
NSMutableData *data = [[[[self window] windowController] data] mutableCopy];
// get selection range
if( self == (id) [[self delegate] hex] )
byteSelection = [[self delegate] byteRangeFromHexRange:selection];
else byteSelection = [[self delegate] byteRangeFromAsciiRange:selection];
// adjust selection if is insertion point
if( byteSelection.length == 0 )
{
byteSelection.location -= 1;
byteSelection.length = 1;
}
// replace bytes (updates views implicitly)
[data replaceBytesInRange:byteSelection withBytes:nil length:0];
[[(HexWindowController *)[[self window] windowController] resource] setData:data];
[data release];
// set the new selection/insertion point
if( selection.length == 0 )
selection.location -= 1;
else selection.length = 0;
[self setSelectedRange:selection];
}
- (IBAction)deleteForward:(id)sender
{
NSRange selection = [self rangeForUserTextChange], byteSelection;
NSMutableData *data = [[[[self window] windowController] data] mutableCopy];
// get selection range
if( self == (id) [[self delegate] hex] )
byteSelection = [[self delegate] byteRangeFromHexRange:selection];
else byteSelection = [[self delegate] byteRangeFromAsciiRange:selection];
// adjust selection if is insertion point
if( byteSelection.length == 0 )
byteSelection.length = 1;
// replace bytes (updates views implicitly)
[data replaceBytesInRange:byteSelection withBytes:nil length:0];
[[(HexWindowController *)[[self window] windowController] resource] setData:data];
[data release];
// set the new selection/insertion point
selection.length = 0;
[self setSelectedRange:selection];
}
- (IBAction)transpose:(id)sender
{
;
}
- (IBAction)deleteWordBackward:(id)sender
{
[self deleteBackward:sender];
}
- (IBAction)deleteWordForward:(id)sender
{
[self deleteForward:sender];
}
- (IBAction)transposeWords:(id)sender
{
[self transpose:sender];
}
@end
@implementation NSTextView (HexTextView)

View File

@ -94,6 +94,7 @@ NSString *ResourceDidChangeNotification = @"ResourceDidChangeNotification";
NSClipView *asciiClip = [[ascii enclosingScrollView] contentView];
// due to a bug in -[NSView setPostsBoundsChangedNotifications:] (it basically doesn't work), I am having to work around it by removing myself from the notification center and restoring things later on!
// update, Apple say this isn't their bug.
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewBoundsDidChangeNotification object:nil];
// when a view scrolls, update the other two