/// <summary> /// Move scrollbar to show caret position. /// </summary> public void ScrollToCaret() { // skip if no scrollbar if (_scrollbar == null) { return; } // make sure caret position is legal if (_caret >= _value.Length) { _caret = -1; } // if caret is at end of text jump to it if (_caret == -1) { _scrollbar.Value = (int)_scrollbar.Max; } // if not try to find the right pos else { TextParagraph.Text = _value; TextParagraph.PrepareForDraw(); string processedValueText = TextParagraph.GetProcessedText(); int currLine = processedValueText.Substring(0, _caret).Split('\n').Length; _scrollbar.Value = currLine - 1; } }
/// <summary> /// When list is resized (also called on init), create the labels to show item values and init graphical stuff. /// </summary> /// <param name="recalcDestRect">If true, will also recalculate destination rectangle.</param> protected virtual void OnResize(bool recalcDestRect = true) { // store current size _prevSize = _size; // remove all children before re-creating them ClearChildren(); // remove previous paragraphs list _paragraphs.Clear(); // calculate self destination rect if (recalcDestRect) { _destRect = CalcDestRect(); } // calculate paragraphs quantity int i = 0; while (true) { // create and add new paragraph Paragraph paragraph = new Paragraph(".", Anchor.Auto, new Vector2(0, 40)); // note: we set negative padding for the selected mark size so it won't be negative size paragraph.WrapWords = false; paragraph.UpdateStyle(DefaultParagraphStyle); paragraph.Scale = paragraph.Scale * ItemsScale; paragraph.SpaceAfter = paragraph.SpaceAfter + new Vector2(0, ExtraSpaceBetweenLines); paragraph.AttachedData = new ParagraphData(this, i++); paragraph.UseActualSizeForCollision = false; AddChild(paragraph); _paragraphs.Add(paragraph); OnCreatedListParagraph(paragraph); // add callback to selection paragraph.OnClick = (Entity entity) => { ParagraphData data = (ParagraphData)entity.AttachedData; if (!data.list.LockSelection) { data.list.Select(data.relativeIndex, true); } }; // to calculate paragraph actual bottom paragraph.CalcDestRect(); paragraph.PrepareForDraw(); // if out of list bounderies remove this paragraph and stop if ((paragraph.GetActualDestRect().Bottom > _destRect.Bottom - Padding.Y) || i > _list.Count) { RemoveChild(paragraph); _paragraphs.Remove(paragraph); break; } } // add scrollbar last, but only if needed if (_paragraphs.Count < _list.Count) { // add scrollbar to list AddChild(_scrollbar, false); // calc max scroll value _scrollbar.Max = (uint)(_list.Count - _paragraphs.Count); if (_scrollbar.Max < 2) { _scrollbar.Max = 2; } _scrollbar.StepsCount = _scrollbar.Max; _scrollbar.Visible = true; } // if no scrollbar is needed, hide it else { _scrollbar.Visible = false; if (_scrollbar.Value > 0) { _scrollbar.Value = 0; } } }