Exemplo n.º 1
0
        /// <summary>
        /// Sets up the basic unit tests with set strings and controls.
        /// </summary>
        private void SetupTests(
            out EditorViewController controller,
            out Caret caret,
            out LineBuffer lineBuffer)
        {
            // Set up Gtk so we can create the view.
            Application.Init();

            // Set up an editor without a cached renderer and a memory buffer
            // we can easily verify.
            var view = new EditorView();

            caret      = view.Caret;
            lineBuffer = new MemoryLineBuffer();
            controller = view.Controller;
            var renderer = new LineBufferRenderer(view, lineBuffer);

            view.SetRenderer(renderer);

            // Create three lines of text.
            TextActions.InsertText(controller, DefaultLine);
            TextActions.InsertParagraph(controller);
            TextActions.InsertText(controller, DefaultLine);
            TextActions.InsertParagraph(controller);
            TextActions.InsertText(controller, DefaultLine);
        }
        /// <summary>
        /// Handles a key press and performs the appropriate action.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="unicodeKey">The Unicode key.</param>
        /// <param name="modifier">The modifier.</param>
        public bool HandleKeyPress(
            Key key,
            ModifierType modifier,
            uint unicodeKey)
        {
            // Normalize the key code and remove excessive modifiers.
            ModifierType filteredModifiers = modifier
                                             & (ModifierType.ShiftMask | ModifierType.Mod1Mask | ModifierType.ControlMask
                                                | ModifierType.MetaMask | ModifierType.SuperMask);
            int keyCode = GdkUtility.GetNormalizedKeyCode(key, filteredModifiers);

            // Check to see if we have an action for this.
            ModifierType isNormalOrShifted = filteredModifiers & ~ModifierType.ShiftMask;
            bool         isCharacter       = unicodeKey != 0 && isNormalOrShifted == ModifierType.None;
            bool         isAction          = keyBindings.ContainsKey(keyCode);

            if (isAction || isCharacter)
            {
                // Keep track of the original selection.
                TextRange previousSelection = displayContext.Caret.Selection;

                // Mark that we are starting a new action and fire events so
                // other listeners and handle it.
                InAction = true;

                // Perform the appropriate action.
                try
                {
                    if (isAction)
                    {
                        keyBindings[keyCode].Perform(this);
                    }
                    else
                    {
                        TextActions.InsertText(this, (char)unicodeKey);
                    }
                }
                finally
                {
                    InAction = false;
                }

                // Check to see if the selection changed.
                if (previousSelection != displayContext.Caret.Selection)
                {
                    displayContext.Renderer.UpdateSelection(displayContext, previousSelection);
                }

                // We did something, so return processed.
                return(true);
            }

            // No idea what to do, so don't do anything.
            return(false);
        }
        public void InsertTextUsingTextActionString()
        {
            // Setup

            // Operation
            TextActions.InsertText(controller, "Test");

            // Verification
            Assert.AreEqual(1, buffer.LineCount);
            Assert.AreEqual("Test", buffer.GetLineText(0));
        }
        /// <summary>
        /// Inserts patterned text into the buffer.
        /// </summary>
        private void InsertPatternIntoBuffer(int lines)
        {
            for (int i = 0;
                 i < lines;
                 i++)
            {
                if (i > 0)
                {
                    TextActions.InsertParagraph(controller);
                }

                TextActions.InsertText(controller, "Line " + (i + 1));
            }
        }
        public void InsertMultipleParagraphs()
        {
            // Setup

            // Operation
            TextActions.InsertText(controller, "One");
            TextActions.InsertParagraph(controller);
            TextActions.InsertText(controller, "Two");
            TextActions.InsertParagraph(controller);
            TextActions.InsertText(controller, "Three");

            // Verification
            Assert.AreEqual(3, buffer.LineCount);
            Assert.AreEqual("One", buffer.GetLineText(0));
            Assert.AreEqual("Two", buffer.GetLineText(1));
            Assert.AreEqual("Three", buffer.GetLineText(2));
        }