Пример #1
0
        /// <summary>
        /// Updates this instance.
        /// </summary>
        public new void Update()
        {
            if (!DesignMode)
            {
                ProcessDragDrop();

                if (Gui.GetButton(0) == ButtonState.Up)
                {
                    EndDragDrop();
                }
            }

            int pressed = -1;
            int down    = -1;

            for (int i = 0; i < Gui.Buttons.Length; i++)
            {
                if (Gui.GetButton(i) == ButtonState.Press)
                {
                    pressed = i;
                }

                if (Gui.GetButton(i) == ButtonState.Down)
                {
                    down = i;
                }
            }

            if (pressed == -1)
            {
                if (TooltipControl != null)
                {
                    TooltipControl.Visible = false;
                }
                hot = GetControlAt(Gui.MousePosition.x, Gui.MousePosition.y);
                if (TooltipControl != null)
                {
                    TooltipControl.Visible = true;
                }

                if (!DesignMode && hot != null && ModalQueue.Count > 0)
                {
                    Control check = ModalQueue[ModalQueue.Count - 1];
                    bool    found = check == hot || hot.IsChildOf(check);

                    if (!found && Dropdowns.Count > 0)
                    {
                        for (int i = Dropdowns.Count - 1; i >= 0; i--)
                        {
                            if (Dropdowns[i].Contains(hot))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found && hot.Owner != null)
                    {
                        found = hot.Owner.IsChildOf(check);
                    }

                    if (!found)
                    {
                        hot = this;
                    }
                }

                if (hot != HotControl)
                {
                    if (HotControl != null)
                    {
                        HotControl.OnMouseLeave();
                    }

                    if (hot != null)
                    {
                        CurrentCursor = hot.Cursor;
                        hot.OnMouseEnter();
                    }

                    HotControl = hot;
                }
            }
            else if (pressed > 1)
            {
                hot = null;
            }

            for (int i = 0; i < Gui.Buttons.Length; i++)
            {
                if (Gui.GetButton(i) == ButtonState.Up)
                {
                    if (MouseDownControl != null)
                    {
                        MouseDownControl.OnMouseUp(i);
                        break;
                    }
                }
            }

            if (!DesignMode && down > -1)
            {
                if (ModalQueue.Count == 0)
                {
                    Window w = GetWindowAt(Gui.MousePosition.x, Gui.MousePosition.y);
                    if (w != null && w != window && w.Dock == DockStyle.None)
                    {
                        w.BringToFront();
                        w.Focus();
                        window = w;
                    }
                }

                if (hot != null)
                {
                    if (hot.AllowFocus)
                    {
                        FocusedControl = hot;
                    }
                    else if (!hot.PreventFocusChange)
                    {
                        FocusedControl = null;
                    }
                }
                else
                {
                    FocusedControl = null;
                }

                //if(OnClick != null)
                //    OnClick(hot);

                if (Dropdowns.Count > 0)
                {
                    if (hot == null)
                    {
                        CloseDropdowns();
                    }
                    else
                    {
                        for (int i = Dropdowns.Count - 1; i >= 0; i--)
                        {
                            if (hot != Dropdowns[i])
                            {
                                if (!Dropdowns[i].Contains(hot))
                                {
                                    Dropdowns[i].Parent = null;
                                    Dropdowns.RemoveAt(i);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            if (!DesignMode)
            {
                if (hot != null)
                {
                    hot.DoEvents();
                }

                DoKeyEvents();

                if (FocusedControl != null)
                {
                    FocusedControl.DoKeyEvents();
                }

                if (IsDragging)
                {
                    SetTooltip(dropTarget);
                }
                else
                {
                    SetTooltip((down > -1 || pressed > -1) ? null : hot);
                }
            }

            PerformUpdate();
            PerformLayout();
            PerformLateUpdate();

            foreach (KeyData data in Gui.KeyBuffer)
            {
                if (data.Pressed && data.Key == Keys.TAB)
                {
                    if (Gui.ShiftPressed)
                    {
                        TabPrevious();
                    }
                    else
                    {
                        TabNext();
                    }
                }
            }
        }
Пример #2
0
        protected override void OnKeyDown(KeyEventArgs args)
        {
            // UnityEngine.Debug.Log(args.Key);

            Desktop root = Desktop;

            if (root == null)
            {
                return;
            }

            BlinkTime = 0; DoBlink = 1;

            if (ReadOnly)
            {
                return;
            }

            if (args.Key == Keys.HOME && !ReadOnly) // home
            {
                if (Gui.ShiftPressed)
                {
                    if (SelectStart == Caret)
                    {
                        Caret = SelectStart = 0;
                    }
                    else if (SelectEnd == Caret)
                    {
                        if (0 < SelectStart)
                        {
                            SelectEnd   = SelectStart;
                            SelectStart = 0;
                            Caret       = 0;
                        }
                        else
                        {
                            Caret = SelectEnd = 0;
                        }
                    }
                }
                else
                {
                    Caret       = 0;
                    SelectStart = SelectEnd = Caret;
                }
            }
            else if (args.Key == Keys.END && !ReadOnly) // end
            {
                if (Gui.ShiftPressed)
                {
                    if (SelectEnd == Caret)
                    {
                        Caret = SelectEnd = Text.Length;
                    }
                    else if (SelectStart == Caret)
                    {
                        if (Text.Length > SelectEnd)
                        {
                            SelectStart = SelectEnd;
                            SelectEnd   = Text.Length;
                            Caret       = Text.Length;
                        }
                        else
                        {
                            Caret = SelectStart = Text.Length;
                        }
                    }
                }
                else
                {
                    Caret       = Text.Length;
                    SelectStart = SelectEnd = Caret;
                }
            }
            else if (args.Key == Keys.RIGHTARROW && !ReadOnly) // right arrow
            {
                HandleRightArrow();
            }
            else if (args.Key == Keys.LEFTARROW && !ReadOnly) // left arrow
            {
                HandleLeftArrow();
            }
            else if (args.Key == Keys.BACKSPACE && !ReadOnly) // backspace
            {
                if (IsSelection)
                {
                    int start = Math.Min(SelectStart, SelectEnd);
                    int end   = Math.Max(SelectStart, SelectEnd);

                    Text   = Text.Remove(start, end - start);
                    Caret  = start;
                    Offset = 0;
                }
                else
                {
                    if (Caret > 0)
                    {
                        Text = Text.Remove(Caret - 1, 1);
                        if (Caret > 0)
                        {
                            Caret--;
                        }
                    }
                }

                SelectStart = SelectEnd = Caret;
            }
            else if (args.Key == Keys.DELETE && !ReadOnly) // delete
            {
                if (IsSelection)
                {
                    int start = Math.Min(SelectStart, SelectEnd);
                    int end   = Math.Max(SelectStart, SelectEnd);

                    Text   = Text.Remove(start, end - start);
                    Caret  = start;
                    Offset = 0;
                }
                else
                {
                    if (Text.Length > Caret)
                    {
                        Text = Text.Remove(Caret, 1);
                    }
                }

                SelectStart = SelectEnd = Caret;
            }
            else if (args.Key == Keys.RETURN || args.Key == Keys.NUMPADENTER) // return/enter
            {
                LostFocus -= TextBox_LostFocus;

                root.FocusedControl = null;
                Caret = 0;

                SelectStart = SelectEnd = Caret;

                LostFocus += TextBox_LostFocus;

                if (TextCommit != null)
                {
                    TextCommit(this, null);
                }
            }
            else if (args.Key == Keys.ESCAPE)
            {
                LostFocus -= TextBox_LostFocus;

                Text = SavedText;
                root.FocusedControl = null;
                Caret       = 0;
                HasFocus    = false;
                SelectStart = SelectEnd = Caret;

                LostFocus += TextBox_LostFocus;

                if (TextCancel != null)
                {
                    TextCancel(this, null);
                }
            }
            else
            {
                if (Gui.CtrlPressed && !Gui.AltPressed)
                {
                    if (args.Key == Keys.A) // select all
                    {
                        SelectStart = 0;
                        SelectEnd   = Text.Length;
                        Caret       = Text.Length;
                    }
                    else if (args.Key == Keys.C) // copy
                    {
                        if (IsSelection)
                        {
                            Gui.SetClipboard(Selection);
                        }
                    }
                    else if (args.Key == Keys.X) // copy
                    {
                        if (IsSelection)
                        {
                            Gui.SetClipboard(Selection);

                            int start = Math.Min(SelectStart, SelectEnd);
                            int end   = Math.Max(SelectStart, SelectEnd);

                            Text   = Text.Remove(start, end - start);
                            Caret  = start;
                            Offset = 0;
                        }
                    }
                    else if (args.Key == Keys.V && !ReadOnly) // paste
                    {
                        string paste = Gui.GetClipboard();
                        if (!string.IsNullOrEmpty(paste))
                        {
                            if (IsSelection)
                            {
                                int start = Math.Min(SelectStart, SelectEnd);
                                int end   = Math.Max(SelectStart, SelectEnd);

                                Text  = Text.Remove(start, end - start);
                                Caret = start;
                            }

                            Text = Text.Insert(Caret, paste.ToString());
                            if (Caret < Text.Length)
                            {
                                Caret += paste.Length;
                            }

                            SelectStart = SelectEnd = Caret;
                        }
                    }
                }
                else
                {
                    if (args.Key != Keys.TAB)
                    {
                        if (args.Char.HasValue)
                        {
                            bool valid = true;
                            char c     = args.Char.Value;

                            if (Mode == TextBoxMode.Numeric)
                            {
                                valid = char.IsNumber(c) || char.IsDigit(c) || (c.ToString() == ".") || (c.ToString() == ",");
                            }

                            if (valid)
                            {
                                if (IsSelection)
                                {
                                    int start = Math.Min(SelectStart, SelectEnd);
                                    int end   = Math.Max(SelectStart, SelectEnd);

                                    Text  = Text.Remove(start, end - start);
                                    Caret = start;
                                }

                                Text = Text.Insert(Caret, c.ToString());
                                if (Caret < Text.Length)
                                {
                                    Caret++;
                                }

                                SelectStart = SelectEnd = Caret;
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        protected override void OnKeyDown(KeyEventArgs args)
        {
            Desktop root = Desktop;

            if (root == null)
            {
                return;
            }

            BlinkTime = 0; DoBlink = 1;

            if (args.Key == Keys.HOME && !ReadOnly) // home
            {
                if (Gui.ShiftPressed)
                {
                    if (_selectStart == Caret)
                    {
                        Caret = _selectStart = 0;
                    }
                    else if (_selectEnd == Caret)
                    {
                        if (0 < _selectStart)
                        {
                            _selectEnd   = _selectStart;
                            _selectStart = 0;
                            Caret        = 0;
                        }
                        else
                        {
                            Caret = _selectEnd = 0;
                        }
                    }
                }
                else
                {
                    Caret        = 0;
                    _selectStart = _selectEnd = Caret;
                }
            }
            else if (args.Key == Keys.END && !ReadOnly) // end
            {
                if (Gui.ShiftPressed)
                {
                    if (_selectEnd == Caret)
                    {
                        Caret = _selectEnd = _text.Length;
                    }
                    else if (_selectStart == Caret)
                    {
                        if (_text.Length > _selectEnd)
                        {
                            _selectStart = _selectEnd;
                            _selectEnd   = _text.Length;
                            Caret        = _text.Length;
                        }
                        else
                        {
                            Caret = _selectStart = _text.Length;
                        }
                    }
                }
                else
                {
                    Caret        = _text.Length;
                    _selectStart = _selectEnd = Caret;
                }
            }
            else if (args.Key == Keys.RIGHTARROW && !ReadOnly) // right arrow
            {
                HandleRightArrow();
            }
            else if (args.Key == Keys.LEFTARROW && !ReadOnly) // left arrow
            {
                HandleLeftArrow();
            }
            else if (args.Key == Keys.UPARROW && !ReadOnly) // up arrow
            {
                HandleUpArrow();
            }
            else if (args.Key == Keys.DOWNARROW && !ReadOnly) // down arrow
            {
                HandleDownArrow();
            }
            else if (args.Key == Keys.BACKSPACE && !ReadOnly) // backspace
            {
                //if (Caret > 0)
                //{
                //    HandleLeftArrow();

                //    if (_text.Length > Caret)
                //        SetText(_text.Remove(Caret, 1));
                //}

                if (IsSelection)
                {
                    int start = SelectionStart;
                    int end   = SelectionEnd;

                    SetText(_text.Remove(start, end - start));
                    Caret = start;
                }
                else
                {
                    if (Caret > 0)
                    {
                        SetText(_text.Remove(Caret - 1, 1));
                        if (Caret > 0)
                        {
                            Caret--;
                        }
                    }
                }

                _selectStart = _selectEnd = Caret;
            }
            else if (args.Key == Keys.DELETE && !ReadOnly) // delete
            {
                //if (_text.Length > Caret)
                //    SetText(_text.Remove(Caret, 1));

                if (IsSelection)
                {
                    int start = SelectionStart;
                    int end   = SelectionEnd;

                    SetText(_text.Remove(start, end - start));
                    Caret = start;
                }
                else
                {
                    if (_text.Length > Caret)
                    {
                        SetText(_text.Remove(Caret, 1));
                    }
                }

                _selectStart = _selectEnd = Caret;
            }
            else if (args.Key == Keys.RETURN || args.Key == Keys.NUMPADENTER) // return/enter
            {
                SetText(_text.Insert(Caret, "\n"));
                Caret++;
            }
            else if (args.Key == Keys.ESCAPE)
            {
                LostFocus -= TextBox_LostFocus;

                SetText(SavedText);
                Caret = 0;

                root.FocusedControl = null;

                LostFocus += TextBox_LostFocus;

                if (TextCancel != null)
                {
                    TextCancel(this, null);
                }
            }
            else
            {
                if (Gui.CtrlPressed && !Gui.AltPressed)
                {
                    if (args.Key == Keys.A) // select all
                    {
                        _selectStart = 0;
                        _selectEnd   = _text.Length;
                        Caret        = _text.Length;
                    }
                    else if (args.Key == Keys.C) // copy
                    {
                        if (IsSelection)
                        {
                            Gui.SetClipboard(Selection.Replace("\n", "\r\n"));
                        }
                    }
                    else if (args.Key == Keys.X) // copy
                    {
                        if (IsSelection)
                        {
                            Gui.SetClipboard(Selection);

                            int start = SelectionStart;
                            int end   = SelectionEnd;

                            SetText(_text.Remove(start, end - start));
                            //Caret = start;
                            _selectStart = _selectEnd = Caret = start;
                        }
                    }
                    else if (args.Key == Keys.V && !ReadOnly) // pasteb
                    {
                        string paste = Gui.GetClipboard().Replace("\r\n", "\n");
                        if (!string.IsNullOrEmpty(paste))
                        {
                            if (IsSelection)
                            {
                                int start = SelectionStart;
                                int end   = SelectionEnd;

                                SetText(_text.Remove(start, end - start));
                                Caret = start;
                            }

                            SetText(_text.Insert(Caret, paste.ToString()));

                            if (Caret < _text.Length)
                            {
                                Caret += paste.Length;
                            }

                            _selectStart = _selectEnd = Caret;
                        }
                    }
                }
                else
                {
                    if (args.Key != Keys.TAB)
                    {
                        if (args.Char.HasValue)
                        {
                            bool valid = true;
                            char c     = args.Char.Value;

                            if (valid)
                            {
                                if (IsSelection)
                                {
                                    int start = SelectionStart;
                                    int end   = SelectionEnd;

                                    SetText(_text.Remove(start, end - start));
                                    Caret = start;
                                }

                                SetText(_text.Insert(Caret, c.ToString()));
                                if (Caret < _text.Length)
                                {
                                    Caret++;
                                }

                                _selectStart = _selectEnd = Caret;
                            }
                        }
                    }
                }
            }
        }