From 92217f05c925bb97df5cfbf90d0c92834771c2bd Mon Sep 17 00:00:00 2001 From: Nicholas Shanks Date: Sun, 31 Mar 2002 17:02:40 +0000 Subject: [PATCH] Add delete functionality. --- Cocoa/Plug-Ins/Hex Editor/HexEditorDelegate.m | 10 +- Cocoa/Plug-Ins/Hex Editor/HexTextView.h | 1 + Cocoa/Plug-Ins/Hex Editor/HexTextView.m | 146 ++++++++++++++++++ .../Plug-Ins/Hex Editor/HexWindowController.m | 1 + 4 files changed, 155 insertions(+), 3 deletions(-) diff --git a/Cocoa/Plug-Ins/Hex Editor/HexEditorDelegate.m b/Cocoa/Plug-Ins/Hex Editor/HexEditorDelegate.m index edb8845..1fcc707 100644 --- a/Cocoa/Plug-Ins/Hex Editor/HexEditorDelegate.m +++ b/Cocoa/Plug-Ins/Hex Editor/HexEditorDelegate.m @@ -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]; } diff --git a/Cocoa/Plug-Ins/Hex Editor/HexTextView.h b/Cocoa/Plug-Ins/Hex Editor/HexTextView.h index 1fd65a9..0efbcaa 100644 --- a/Cocoa/Plug-Ins/Hex Editor/HexTextView.h +++ b/Cocoa/Plug-Ins/Hex Editor/HexTextView.h @@ -1,5 +1,6 @@ #import #import "HexEditorDelegate.h" +#import "HexWindowController.h" @interface HexTextView : NSTextView { diff --git a/Cocoa/Plug-Ins/Hex Editor/HexTextView.m b/Cocoa/Plug-Ins/Hex Editor/HexTextView.m index 819e642..db5f1cb 100644 --- a/Cocoa/Plug-Ins/Hex Editor/HexTextView.m +++ b/Cocoa/Plug-Ins/Hex Editor/HexTextView.m @@ -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) diff --git a/Cocoa/Plug-Ins/Hex Editor/HexWindowController.m b/Cocoa/Plug-Ins/Hex Editor/HexWindowController.m index 57faf2d..836cbcc 100644 --- a/Cocoa/Plug-Ins/Hex Editor/HexWindowController.m +++ b/Cocoa/Plug-Ins/Hex Editor/HexWindowController.m @@ -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