Exemplo n.º 1
0
        protected override void OnLateUpdate()
        {
            if (!IsDirty)
            {
                IsDirty = LastSize.x != Size.x || LastSize.y != Size.y;
            }

            if (IsDirty)
            {
                Style style = Desktop.GetStyle(Style).Styles[State];
                UpdateText(style);
            }

            if (Desktop.HotControl == this)
            {
                Point m = Gui.MousePosition;
                ActiveHref = null;

                foreach (TextLine line in Lines)
                {
                    foreach (TextElement element in line.Elements)
                    {
                        if (!element.IsLink)
                        {
                            continue;
                        }

                        if (element.Rectangle.Contains(m))
                        {
                            Desktop.CurrentCursor = CursorNames.Link;
                            ActiveHref            = element.Href;
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Open()
        {
            if (OnOpening != null)
            {
                SquidEventArgs args = new SquidEventArgs();
                OnOpening(this, args);
                if (args.Cancel)
                {
                    return;
                }
            }

            if (!WasOpenOnce)
            {
                if (DropdownAutoSize)
                {
                    Dropdown.Size = new Point(Size.x, DropdownSize.y);
                }
                else
                {
                    Dropdown.Size = new Point(DropdownSize.x, DropdownSize.y);
                }

                WasOpenOnce = true;
            }

            Dropdown.Position = Location + new Point(0, Size.y);

            Desktop.ShowDropdown(Dropdown, false);
            IsOpen = true;

            if (OnOpened != null)
            {
                OnOpened(this, null);
            }
        }
Exemplo n.º 3
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;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        void TextBox_MouseDown(Control sender, MouseEventArgs args)
        {
            if (args.Button > 0)
            {
                return;
            }

            if (!HasFocus)
            {
                SavedText = Text;
                HasFocus  = true;
            }

            Style style = Desktop.GetStyle(Style).Styles[State];

            string masked = Text;

            if (IsPassword)
            {
                masked = new string(PasswordChar, masked.Length);
            }

            if (string.IsNullOrEmpty(masked))
            {
                return;
            }

            int font = Gui.Renderer.GetFont(style.Font);

            if (font < 0)
            {
                return;
            }

            Point p     = Gui.MousePosition - Location;
            Point s1    = Gui.Renderer.GetTextSize(masked, font);
            int   carex = p.x + Offset + s1.x;
            int   x     = 0;

            string text = string.Empty;

            for (int i = 1; i <= masked.Length; i++)
            {
                text = masked.Substring(0, i);
                x    = Offset + Gui.Renderer.GetTextSize(text, font).x;
                if (x > p.x)
                {
                    Caret = i - 1;
                    break;
                }
            }

            if (x < p.x)
            {
                Caret = masked.Length;
            }

            if (Gui.CtrlPressed)
            {
                int left  = FindIndexLeft(Caret, masked);
                int right = FindIndexRight(Caret, masked);

                if (char.IsWhiteSpace(masked, left) || char.IsPunctuation(masked, left))
                {
                    left++;
                }

                if (char.IsWhiteSpace(masked, right - 1) || char.IsPunctuation(masked, right - 1))
                {
                    right--;
                }

                SelectStart = left;
                SelectEnd   = right;

                Caret = SelectEnd;
            }
            else if (Gui.ShiftPressed)
            {
                SelectEnd = Caret;
            }
            else
            {
                SelectStart = Caret;
                SelectEnd   = Caret;
            }
        }
Exemplo n.º 5
0
        void TextBox_MousePress(Control sender, MouseEventArgs args)
        {
            if (args.Button > 0)
            {
                return;
            }
            if (Gui.CtrlPressed)
            {
                return;
            }

            Style style = Desktop.GetStyle(Style).Styles[State];

            string masked = Text;

            if (IsPassword)
            {
                masked = new string(PasswordChar, masked.Length);
            }

            if (string.IsNullOrEmpty(masked))
            {
                return;
            }

            int font = Gui.Renderer.GetFont(style.Font);

            if (font < 0)
            {
                return;
            }

            Point p     = Gui.MousePosition - Location;
            Point s1    = Gui.Renderer.GetTextSize(masked, font);
            int   carex = p.x + Offset + s1.x;
            int   x     = 0;

            string text  = string.Empty;
            int    caret = Caret;

            for (int i = 1; i <= masked.Length; i++)
            {
                text = masked.Substring(0, i);
                x    = Offset + Gui.Renderer.GetTextSize(text, font).x;
                if (x > p.x)
                {
                    SelectEnd = i - 1;
                    break;
                }
            }

            if (x < p.x)
            {
                SelectEnd = masked.Length;
            }

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

            if (SelectEnd < SelectStart)
            {
                Caret = start;
            }
            else
            {
                Caret = end;
            }
        }
Exemplo n.º 6
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            Desktop root = Desktop;

            if (root == null)
            {
                return;
            }

            if (AutoScale)
            {
                Scale = Math.Min(1, Math.Max(0, Scale));

                if (Orientation == Orientation.Vertical)
                {
                    int size       = Size.y;// (int)(Size.y - Button.Margin.Top - Button.Margin.Bottom);
                    int actualSize = (int)((float)size * Scale);

                    if (MinHandleSize > 0 && actualSize < MinHandleSize)
                    {
                        Scale = (float)MinHandleSize / (float)size;
                    }

                    Button.Size = new Point(Button.Size.x, (int)(size * Scale));
                }
                else
                {
                    int size       = Size.x;// (int)(Size.y - Button.Margin.Top - Button.Margin.Bottom);
                    int actualSize = (int)((float)size * Scale);

                    if (MinHandleSize > 0 && actualSize < MinHandleSize)
                    {
                        Scale = MinHandleSize / size;
                    }

                    Button.Size = new Point((int)(size * Scale), Button.Size.y);
                }
            }

            Button.Dock = Orientation == Orientation.Vertical ? DockStyle.FillX : DockStyle.FillY;

            if (root.PressedControl == Button)
            {
                if (!(Scale >= 1 && AutoScale))
                {
                    Point position = Gui.MousePosition - Location;

                    if (Orientation == Orientation.Vertical)
                    {
                        position.x  = Button.Position.x;
                        position.y -= Offset.y;
                        position.y  = Math.Max(0, Math.Min(Size.y - Button.Size.y, position.y));

                        Button.Position = position;

                        Value = Minimum + (Maximum - Minimum) * position.y / (Size.y - Button.Size.y);
                    }
                    else
                    {
                        position.y  = Button.Position.y;
                        position.x -= Offset.x;
                        position.x  = Math.Max(0, Math.Min(Size.x - Button.Size.x, position.x));

                        Button.Position = position;

                        Value = Minimum + (Maximum - Minimum) * position.x / (Size.x - Button.Size.x);
                    }

                    Snap();

                    _easeScroll = _value;
                }
            }
            else
            {
                Snap();

                float m = _value;

                if (Ease)
                {
                    _easeScroll += ((_value - _easeScroll) / 8f) * Math.Min(8, Gui.TimeElapsed * 0.1f);
                    m            = (_easeScroll - Minimum) / (Maximum - Minimum);
                }
                else
                {
                    m = (_value - Minimum) / (Maximum - Minimum);
                }

                Point end = Point.Zero;

                if (Orientation == Orientation.Vertical)
                {
                    int size = Size.y;// (int)(Size.y - Button.Margin.Top - Button.Margin.Bottom);
                    int y    = (int)(m * (size - Button.Size.y));
                    end = new Point(Button.Position.x, y);

                    Button.Position = end;
                }
                else
                {
                    int x = (int)(m * (Size.x - Button.Size.x));
                    end = new Point(x, Button.Position.y);

                    Button.Position = end;
                }
            }

            if (Ease)
            {
                EasedValue += ((_value - EasedValue) / 8f) * Math.Min(8, Gui.TimeElapsed * 0.1f);
            }
            else
            {
                EasedValue = _value;
            }
        }
Exemplo n.º 7
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;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        //protected override void Bind(object instance)
        //{
        //    if (string.IsNullOrEmpty(Aspect)) return;
        //    if (instance == null) return;

        //    Type type = instance.GetType();
        //    object value = null;

        //    System.Reflection.PropertyInfo property = type.GetProperty(Aspect);
        //    if (property != null)
        //    {
        //        value = property.GetValue(instance, null);
        //        if (value != null) Text = value.ToString();
        //    }
        //    else
        //    {
        //        System.Reflection.FieldInfo field = type.GetField(Aspect);
        //        if (field != null)
        //        {
        //            value = field.GetValue(instance);
        //            if (value != null) Text = value.ToString();
        //        }
        //    }
        //}

        protected override void OnStateChanged()
        {
            Style style = Desktop.GetStyle(Style).Styles[State];

            UpdateText(style);
        }
Exemplo n.º 9
0
 public override void Show(Desktop target)
 {
     _result = DialogResult.None;
     base.Show(target);
 }