protected override void DoOperation() { Util.Range m; match = new Util.Range(); firstMatch = null; numReplaced = 0; strategy.Buffer.BeginActionChaining(); strategy.Buffer.ModifyAllowed = false; strategy.Buffer.FileOperationsAllowed = false; strategy.Buffer.EmitEvents = false; while ((m = strategy.FindNext()) != null) { if (firstMatch == null) { firstMatch = new Util.Range(m); } match.Start = m.Start; match.End = m.End; lock (strategy.Buffer.LockObj) { strategy.Buffer.ModifyAllowed = true; strategy.Buffer.Replace(m.Start, m.End, replacePattern); strategy.Buffer.ModifyAllowed = false; } // start next search after the replaced pattern strategy.Position = m.Start + replacePattern.Length; numReplaced++; } }
///<summary> /// Called when an asynchronous Find Next/Previous operation finishes. ///</summary> void FindAsyncCallback(IAsyncResult ar) { GenericFindOperation state = (GenericFindOperation)ar.AsyncState; ThreadedAsyncOperation.OperationResult result = state.Result; Util.Range match = state.Match; DataView dv = null; // find DataView that owns bb foreach (DataViewDisplay dvtemp in dataBook.Children) { if (dvtemp.View.Buffer == strategy.Buffer) { dv = dvtemp.View; break; } } // decide what to do based on the result of the find operation switch (result) { case ThreadedAsyncOperation.OperationResult.Finished: if (match != null) { lastFound = match; dv.SetSelection(match.Start, match.End); //System.Console.WriteLine("Found at {0}-{1}", r.Start, r.End); dv.MoveCursor(match.End + 1, 0); dv.Display.MakeOffsetVisible(match.Start, DataViewDisplay.ShowType.Closest); } else { lastFound.Clear(); } break; case ThreadedAsyncOperation.OperationResult.Cancelled: dv.MoveCursor(strategy.Position, 0); dv.Display.MakeOffsetVisible(strategy.Position, DataViewDisplay.ShowType.Closest); break; case ThreadedAsyncOperation.OperationResult.CaughtException: break; default: break; } inUse = false; // if user provided a callback, call it now if (userFindAsyncCallback != null) { userFindAsyncCallback(ar); } // notify that the find operation has finished findFinishedEvent.Set(); }
public BitwiseOperation(ByteBuffer bb, byte[] ba, Util.Range range, BitwiseOperationsWidget.OperationType ot, ProgressCallback pc, AsyncCallback ac, bool glibIdle) : base(pc, ac, glibIdle) { byteBuffer = bb; byteArray = ba; this.range = range; operationType = ot; currentOffset = range.Start; }
///<summary> /// Returns as a SegmentCollection the data contained in /// the specified range in the buffer. ///</summary> public SegmentCollection RangeToSegmentCollection(Util.Range range) { if (range.Size == 0) { return(null); } return(segCol.GetRange(range.Start, range.End)); }
/// <summary> /// Gets the atomic highlight ranges of the current view. /// (Non-overlapping ranges that describe the highlighting of the whole view) /// </summary> private IntervalTree <AtomicHighlight> GetAtomicHighlights() { int nrows; Util.Range clip = GetViewRange(out nrows); Highlight view = new Highlight(clip, Drawer.HighlightType.Normal); // get all highlights in current view IList <Highlight> viewableHighlights = highlights.SearchOverlap(view); return(BreakDownHighlights(view, viewableHighlights)); }
private void SetupBufferCache() { int nrows; Util.Range view = GetViewRange(out nrows); if (view.Size != bufferCache.Length) { bufferCache = new byte[view.Size]; } for (int i = 0; i < view.Size; i++) { bufferCache[i] = byteBuffer[view.Start + i]; } }
void OnDoOperationClicked(object o, EventArgs args) { if (dataBook.NPages == 0) { return; } DataView dv = ((DataViewDisplay)dataBook.CurrentPageWidget).View; // get the operands as a byte array byte[] byteArray = null; try { byteArray = ParseOperand(); } catch (FormatException e) { ErrorAlert ea = new ErrorAlert(Catalog.GetString("Error in Operand"), e.Message, null); ea.Run(); ea.Destroy(); return; } /// get the range to apply the operation to Util.Range range = dv.Selection; if (range.IsEmpty()) { Util.Range bbRange = dv.Buffer.Range; range.Start = dv.CursorOffset; range.End = range.Start + byteArray.Length - 1; range.Intersect(bbRange); } // don't allow buffer modification while the operation is perfoming dv.Buffer.ModifyAllowed = false; dv.Buffer.FileOperationsAllowed = false; BitwiseOperation bo = new BitwiseOperation(dv.Buffer, byteArray, range, (OperationType)OperationComboBox.Active, Services.UI.Progress.NewCallback(), BitwiseOperationAsyncCallback, true); // start operation thread Thread boThread = new Thread(bo.OperationThread); boThread.IsBackground = true; boThread.Start(); }
///<summary> /// Called when an asynchronous Replace All operation finishes. ///</summary> void ReplaceAllAsyncCallback(IAsyncResult ar) { ReplaceAllOperation state = (ReplaceAllOperation)ar.AsyncState; ThreadedAsyncOperation.OperationResult result = state.Result; Util.Range firstMatch = state.FirstMatch; DataView dv = null; // find DataView that owns bb foreach (DataViewDisplay dvtemp in dataBook.Children) { if (dvtemp.View.Buffer == strategy.Buffer) { dv = dvtemp.View; break; } } // decide what to do based on the result of the Replace All operation if (result == ThreadedAsyncOperation.OperationResult.Cancelled) { dv.Buffer.Undo(); } // if we have replaced at least one occurence, else if (result == ThreadedAsyncOperation.OperationResult.Finished && firstMatch != null) { lastFound = state.Match; // save the cursor state for undo/redo dv.CursorUndoDeque.AddFront(new CursorState(firstMatch.Start, 0, lastFound.Start + state.ReplacePattern.Length, 0)); // move cursor after final replacement dv.SetSelection(-1, -1); dv.MoveCursor(lastFound.Start + state.ReplacePattern.Length, 0); dv.Display.MakeOffsetVisible(lastFound.Start + state.ReplacePattern.Length, DataViewDisplay.ShowType.Closest); } inUse = false; // if user provided a callback, call it now if (userFindAsyncCallback != null) { userFindAsyncCallback(ar); } // notify that the replace all operation has finished findFinishedEvent.Set(); }
private void DeleteSelectionInternal() { AreaGroup areaGroup = dvDisplay.Layout.AreaGroup; // // set cursor and selection before deleting so that the view remains in a consistent state // (eg the selection isn't beyond the EOF) // Util.Range prevSelection = this.Selection; AddUndoCursorState(new CursorState(areaGroup.CursorOffset, 0, areaGroup.Selection.Start, 0)); cursorRedoDeque.Clear(); this.MoveCursor(areaGroup.Selection.Start, 0); this.SetSelection(-1, -1); byteBuffer.Delete(prevSelection.Start, prevSelection.End); }
/// <summary> /// Adds pattern match highlights to an area group before it is rendered /// </summary> protected virtual void BeforeRender(AreaGroup ag) { if (!active) { return; } Util.Range sel = ag.Selection; if (sel.IsEmpty() || sel.Size > 512) { return; } int nrows; Util.Range view = ag.GetViewRange(out nrows); findStrategy.Buffer = ag.Buffer; findStrategy.Position = view.Start; findStrategy.Pattern = ag.Buffer.RangeToByteArray(sel); // Merge overlapping matches Util.Range match; Util.Range currentHighlight = new Util.Range(); while ((match = findStrategy.FindNext(view.End)) != null) { if (currentHighlight.End >= match.Start) { currentHighlight.End = match.End; } else { ag.AddHighlight(currentHighlight.Start, currentHighlight.End, Drawer.HighlightType.PatternMatch); currentHighlight = match; } } ag.AddHighlight(currentHighlight.Start, currentHighlight.End, Drawer.HighlightType.PatternMatch); }
/// <summary> /// Adds pattern match highlights to an area group before it is rendered /// </summary> protected override void BeforeRender(AreaGroup ag) { if (!active) { return; } int nrows; Util.Range view = ag.GetViewRange(out nrows); if (view.Start < 0 || view.End < 0) { return; } findStrategy.Buffer = ag.Buffer; findStrategy.Position = view.Start; // Merge overlapping matches Util.Range match; Util.Range currentHighlight = new Util.Range(); while ((match = findStrategy.FindNext(view.End)) != null) { if (currentHighlight.End >= match.Start) { currentHighlight.End = match.End; } else { ag.AddHighlight(currentHighlight.Start, currentHighlight.End, Drawer.HighlightType.Unfocus); currentHighlight = match; } } ag.AddHighlight(currentHighlight.Start, currentHighlight.End, Drawer.HighlightType.Unfocus); }
void OnDialogResponse(object o, Gtk.ResponseArgs args) { lock (LockObj) { if (args.ResponseId == ResponseType.Ok && dataBook != null && dataBook.NPages > 0) { DataView dv = ((DataViewDisplay)dataBook.CurrentPageWidget).View; IExportBuilder builder = null; TreeIter iter; ExportAsCombo.GetActiveIter(out iter); ExportPlugin plugin = (ExportPlugin)ExportAsCombo.Model.GetValue(iter, 1); Util.Range range; try { range = GetCurrentRange(dv); } catch (FormatException ex) { ErrorAlert ea = new ErrorAlert(Catalog.GetString("Error in custom range"), ex.Message, mainWindow); ea.Run(); ea.Destroy(); return; } Util.Range bufferRange; if (dv.Buffer.Size == 0) { bufferRange = new Util.Range(); } else { bufferRange = new Util.Range(0, dv.Buffer.Size - 1); } if (!bufferRange.Contains(range.Start) || !bufferRange.Contains(range.End)) { ErrorAlert ea = new ErrorAlert(Catalog.GetString("Error in range"), Catalog.GetString("Range is out of file's limits"), mainWindow); ea.Run(); ea.Destroy(); return; } Stream stream = null; try { stream = new FileStream(ExportFileEntry.Text, FileMode.Create, FileAccess.Write); builder = plugin.CreateBuilder(stream); InterpretedPatternExporter exporter = new InterpretedPatternExporter(builder); exporter.Pattern = ExportPatternComboEntry.Entry.Text; cancelClicked = false; BeginExport(exporter, dv.Buffer, range.Start, range.End); } catch (Exception ex) { if (stream != null) { stream.Close(); } ErrorAlert ea = new ErrorAlert(Catalog.GetString("Error saving to file"), ex.Message, mainWindow); ea.Run(); ea.Destroy(); return; } } else if (args.ResponseId == ResponseType.Close) { this.Hide(); } } }
void OnKeyDefault(Gdk.EventKey e, ref Position cur, ref Position next, out bool specialKey) { if (!dataView.Buffer.ModifyAllowed) { specialKey = true; return; } // if the buffer isn't resizable, ignore non-overwriting keypresses if (!dataView.Buffer.IsResizable && !dataView.Overwrite) { specialKey = true; return; } if (dataView.Selection.IsEmpty()) { if (imContext.FilterKeypress(e) && okp_focusArea.HandleKey(e.Key, dataView.Overwrite) == true) { OnKeyRight(ref cur, ref next); dataView.CursorUndoDeque.AddFront(new CursorState(cur.Second, cur.Digit, next.Second, next.Digit)); dataView.CursorRedoDeque.Clear(); selStartPos = selEndPos = next; specialKey = false; } else { specialKey = true; } } else { dataView.Buffer.BeginActionChaining(); // Insert the new data and delete the old if (imContext.FilterKeypress(e) && okp_focusArea.HandleKey(e.Key, false) == true) { Util.Range curSel = dataView.Selection; long curOffset = dataView.CursorOffset; // move cursor to the right OnKeyRight(ref cur, ref next); next.First = next.Second = curSel.Start; selEndPos = selStartPos = next; // the new data could have been inserted either just after the end // or at the beginning of the selection. Handle each case // and delete the old data. if (curOffset > curSel.End) // new data just after end { dataView.Delete(); } else // curOffset == curSel.Start, new data at the beginning // shift selection one position to the right { dataView.SetSelection(curSel.Start + 1, curSel.End + 1); dataView.Delete(); } specialKey = false; } else { specialKey = true; } dataView.Buffer.EndActionChaining(); } // any other key pass it to focused area // if area handled it move one position right }
/// <summary>Determines if another range is inside the bounds of this range.</summary> /// <param name="Range">The child range to test</param> /// <returns>True if range is inside, else false</returns> public bool ContainsRange(Util.Range <T> range) => this.IsValid && range.IsValid && this.ContainsValue(range.min) && this.ContainsValue(range.max);
/// <summary>Determines if this Range is inside the bounds of another range.</summary> /// <param name="Range">The parent range to test on</param> /// <returns>True if range is inclusive, else false</returns> public bool IsInsideRange(Util.Range <T> range) => this.IsValid && range.IsValid && range.ContainsValue(this.min) && range.ContainsValue(this.max);