Esempio n. 1
0
        ///<summary>
        /// Called when an asynchronous Replace All operation finishes.
        ///</summary>
        void ReplaceAllAsyncCallback(IAsyncResult ar)
        {
            ReplaceAllOperation state = (ReplaceAllOperation)ar.AsyncState;

            ThreadedAsyncOperation.OperationResult result = state.Result;
            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();
        }
Esempio n. 2
0
        void ReplaceAllAsyncCallback(IAsyncResult ar)
        {
            ThreadedAsyncResult tar = (ThreadedAsyncResult)ar;

            ReplaceAllOperation state = (ReplaceAllOperation)tar.AsyncState;

            if (state.Result == ReplaceAllOperation.OperationResult.Finished)
            {
                InformationAlert ia = new InformationAlert("Found and replaced " + state.NumReplaced + " occurences.", "", null);
                ia.Run();
                ia.Destroy();
            }

            this.Sensitive = true;
            this.Visible   = false;
            this.Visible   = true;
        }
Esempio n. 3
0
        ///<summary>
        /// Replace all matches with a pattern. Works asynchronously.
        ///</summary>
        public IAsyncResult ReplaceAll(byte[] ba, AsyncCallback ac)
        {
            if (dataBook.NPages == 0 || inUse)
            {
                ReplaceAllOperation op = new ReplaceAllOperation(strategy, null, ReplaceAllAsyncCallback, ba);
                return(HandleProblematicOp(op, ac));
            }

            // DataBookFinder is already in use
            if (inUse)
            {
                return(null);
            }

            inUse = true;

            userFindAsyncCallback = ac;

            DataView dv = ((DataViewDisplay)dataBook.CurrentPageWidget).View;

            // initialize strategy
            strategy.Buffer   = dv.Buffer;
            strategy.Position = 0;

            SetUpReplaceAllProgressReport();

            ReplaceAllOperation rao = new ReplaceAllOperation(strategy, progressCallback, ReplaceAllAsyncCallback, ba);

            findFinishedEvent.Reset();

            // don't allow messing up with the buffer

            // start replace all thread
            Thread findThread = new Thread(rao.OperationThread);

            findThread.IsBackground = true;
            findThread.Start();

            return(new ThreadedAsyncResult(rao, findFinishedEvent, false));
        }