Skip to content

Commit

Permalink
#31 fixed CanInsertEverywhere property
Browse files Browse the repository at this point in the history
  • Loading branch information
abbaye committed Feb 11, 2021
1 parent 731249a commit 9af1d1d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Margin="10,50,10,10"
AllowAutoHighLightSelectionByte="False"
AllowAutoSelectSameByteAtDoubleClick="False"
CanInsertEverywhere="True"
PreloadByteInEditorMode="MaxScreenVisibleLineAtDataLoad" />

<Button
Expand All @@ -39,9 +40,9 @@
Width="218"
Height="26"
Margin="89,10,0,0"
Click="AddByteButton_Click"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="AddByteButton_Click"
Content="Insert 1 byte at SelectionStart position"
IsEnabled="{Binding ElementName=HexEditor, Path=IsFileOrStreamLoaded, UpdateSourceTrigger=PropertyChanged}" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ private void OpenButton_Click(object sender, RoutedEventArgs e) =>
if (o.ShowDialog() ?? false)
HexEditor.FileName = o.FileName;
});



private void AddByteButton_Click(object sender, RoutedEventArgs e)
{
HexEditor.InsertByte(224, 15);
}
}
}
42 changes: 26 additions & 16 deletions Sources/WPFHexaEditor/Core/Bytes/ByteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public sealed class ByteProvider : IDisposable
private string _fileName = string.Empty;
private Stream _stream;
private bool _readOnlyMode;
private bool _canInsertEverywhere;
private bool _canInsertEverywhere = false;
private double _longProcessProgress;
private string _newfilename = string.Empty;
#endregion Globals variable

#region Events


//Somes other events can be added in futures

public event EventHandler DataCopiedToClipboard;
public event EventHandler ReadOnlyChanged;
public event EventHandler Closed;
Expand Down Expand Up @@ -74,16 +76,21 @@ public ByteProvider() { }
/// <summary>
/// Construct new ByteProvider with filename and try to open file
/// </summary>
public ByteProvider(string fileName, bool readOnlyMode)
public ByteProvider(string fileName, bool readOnlyMode, bool canInsertEverywhere)
{
CanInsertEverywhere = canInsertEverywhere;
ReadOnlyMode = readOnlyMode;
FileName = fileName;
}

/// <summary>
/// Constuct new ByteProvider with stream
/// </summary>
public ByteProvider(Stream stream) => Stream = stream;
public ByteProvider(Stream stream, bool canInsertEverywhere)
{
CanInsertEverywhere = canInsertEverywhere;
Stream = stream;
}

#endregion Constructors

Expand Down Expand Up @@ -573,26 +580,26 @@ public void SubmitChanges()
/// <summary>
/// Add/Modifiy a ByteModifed in the list of byte have changed
/// </summary>
public void AddByteModified(byte? @byte, long BytePositionInStream, long undoLength = 1)
public void AddByteModified(byte? @byte, long bytePositionInStream, long undoLength = 1)
{
if (ReadOnlyMode) return;

var (success, _) = CheckIfIsByteModified(BytePositionInStream);
var (success, _) = CheckIfIsByteModified(bytePositionInStream);

if (success)
_byteModifiedDictionary.Remove(BytePositionInStream);
_byteModifiedDictionary.Remove(bytePositionInStream);

var byteModified = new ByteModified
{
Byte = @byte,
Length = undoLength,
BytePositionInStream = BytePositionInStream,
BytePositionInStream = bytePositionInStream,
Action = ByteAction.Modified
};

try
{
_byteModifiedDictionary.Add(BytePositionInStream, byteModified);
_byteModifiedDictionary.Add(bytePositionInStream, byteModified);
UndoStack.Push(byteModified);
}
catch
Expand All @@ -604,27 +611,27 @@ public void AddByteModified(byte? @byte, long BytePositionInStream, long undoLen
/// <summary>
/// Add new byte a ByteModifed in the list of byte have changed
/// </summary>
public void AddByteAdded(byte? @byte, long BytePositionInStream, long undoLength = 1)
public void AddByteAdded(byte? @byte, long bytePositionInStream, long undoLength = 1)
{
if (ReadOnlyMode) return;
if (!CanInsertEverywhere) return;

var (success, _) = CheckIfIsByteModified(BytePositionInStream, ByteAction.Added);
var (success, _) = CheckIfIsByteModified(bytePositionInStream, ByteAction.Added);

if (success)
_byteModifiedDictionary.Remove(BytePositionInStream);
_byteModifiedDictionary.Remove(bytePositionInStream);

var byteModified = new ByteModified
{
Byte = @byte,
Length = undoLength,
BytePositionInStream = BytePositionInStream,
BytePositionInStream =bytePositionInStream,
Action = ByteAction.Added
};

try
{
_byteModifiedDictionary.Add(BytePositionInStream, byteModified);
_byteModifiedDictionary.Add(bytePositionInStream, byteModified);
UndoStack.Push(byteModified);
}
catch
Expand Down Expand Up @@ -711,7 +718,6 @@ public void FillWithByte(long startPosition, long length, byte b)
Position = startPosition + i;
if (GetByte(Position).singleByte != b)
AddByteModified(b, Position - 1);

}

FillWithByteCompleted?.Invoke(this, new EventArgs());
Expand Down Expand Up @@ -919,7 +925,11 @@ public void CopyToClipboard(CopyPasteMode copypastemode,
/// <summary>
/// Copy selection to clipboard in code block
/// </summary>
private void CopyToClipboard_Language(long selectionStart, long selectionStop, bool copyChange, DataObject da, CodeLanguage language)
private void CopyToClipboard_Language(long selectionStart,
long selectionStop,
bool copyChange,
DataObject da,
CodeLanguage language)
{
if (!CanCopy(selectionStart, selectionStop)) return;

Expand Down
45 changes: 42 additions & 3 deletions Sources/WPFHexaEditor/HexEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,7 @@ private void OpenFile(string filename)
if (PreloadByteInEditorMode == PreloadByteInEditor.MaxScreenVisibleLineAtDataLoad)
BuildDataLines(MaxScreenVisibleLine, false);

_provider = new ByteProvider(filename, ReadOnlyMode);
_provider = new ByteProvider(filename, ReadOnlyMode, CanInsertEverywhere);

_provider.With(p =>
{
Expand Down Expand Up @@ -2293,7 +2293,7 @@ private void OpenStream(Stream stream)
if (PreloadByteInEditorMode == PreloadByteInEditor.MaxScreenVisibleLineAtDataLoad)
BuildDataLines(MaxScreenVisibleLine, false);

_provider = new ByteProvider(stream);
_provider = new ByteProvider(stream, CanInsertEverywhere);

_provider.With(p =>
{
Expand Down Expand Up @@ -5609,7 +5609,7 @@ public IEnumerable<ByteDifference> Compare(HexEditor hexeditor, bool compareChan

#endregion

#region Commands implementation (In early stage of developpement)
#region Commands implementation (In early stage of development)

public static DependencyProperty RefreshViewCommandProperty =
DependencyProperty.Register (
Expand Down Expand Up @@ -5638,5 +5638,44 @@ public ICommand SubmitChangesCommand
}

#endregion

#region Insert byte everywhere support (In early stage of development)

/// <summary>
/// Give the possibility to inserts byte everywhere.
/// </summary>
public bool CanInsertEverywhere
{
get { return (bool)GetValue(CanInsertEverywhereProperty); }
set { SetValue(CanInsertEverywhereProperty, value); }
}

public static readonly DependencyProperty CanInsertEverywhereProperty =
DependencyProperty.Register(nameof(CanInsertEverywhere), typeof(bool), typeof(HexEditor),
new FrameworkPropertyMetadata(false, Control_CanInsertEverywhereChanged));

private static void Control_CanInsertEverywhereChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not HexEditor ctrl || e.NewValue == e.OldValue) return;

if (CheckIsOpen(ctrl._provider))
ctrl._provider.CanInsertEverywhere = (bool)e.NewValue;

ctrl.RefreshView();
}

/// <summary>
/// Insert byte at the specified position
/// </summary>
public void InsertByte(byte @byte, long bytePositionInStream)
{
if (!CheckIsOpen(_provider)) return;
if (!CanInsertEverywhere) return;

_provider.AddByteAdded(@byte, bytePositionInStream);

RefreshView();
}
#endregion
}
}

0 comments on commit 9af1d1d

Please sign in to comment.