//
// Copyright (c) 2008, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 28 Jul 08 Brian Frank Creation
//
**
** RichText is used to view and edit text styled with
** different fonts and colors.
**
class RichText : Widget
{
**
** Callback when the text is modified.
**
|Event| onModify
**
** Backing data model of text document.
**
RichTextModel model
**
** Draw a border around the text area. Default is true. This
** field cannot be changed once the widget is constructed.
**
const Bool border := true
**
** False for text fields and true for multi-line text areas.
** Default is true. This field cannot be changed once the widget
** is constructed.
**
const Bool multiLine := true
**
** True to make wrap the text of a multiLine text widget.
** Default is false. This field cannot be changed once the
** widget is constructed.
**
const Bool wrap := false
**
** True use a horizontal scrollbar for multiLine text widget.
** Default is true. This field cannot be changed once the
** widget is constructed.
**
const Bool hscroll := true
**
** True use a vertical scrollbar for multiLine text widget.
** Default is true. This field cannot be changed once the
** widget is constructed.
**
const Bool vscroll := true
**
** Convenience for 'model.text' (model must be installed).
**
Str text
{
get { return model.text }
set { model.text = val }
}
**
** Default font for text absense explicit styling.
** Defaults to null (system default).
**
Font font := null { set { @font = val; sync(fontId) } }
internal static const Str fontId := "font"
}
**************************************************************************
** RichTextModel
**************************************************************************
**
** RichTextModel models the document and styling of a `RichText` document.
**
mixin RichTextModel
{
**
** Get or set the entire text document.
**
abstract Str text
**
** Return the number of characters in the content.
**
abstract Int charCount()
**
** Return the number of lines.
**
abstract Int lineCount()
**
** Return the line at the given zero based line index without delimiters.
**
abstract Str line(Int lineIndex)
**
** Return the zero based line index at the given character offset.
**
abstract Int lineAtOffset(Int offset)
**
** Return the character offset of the first character of the
** given zero based line index.
**
abstract Int offsetAtLine(Int lineIndex)
**
** Return the line delimiter that should be used when inserting
** new lines. The default is "\n".
**
virtual Str lineDelimiter() { return "\n" }
**
** Returns a string representing the content at the given range.
**
abstract Str (Int start, Int len)
**
** Replace the text with 'newText' starting at position 'start'
** for a length of 'replaceLen'.
**
abstract Void (Int start, Int replaceLen, Str newText)
**
** Return the styled segments for the given zero based line index.
** The result is a list of Int/RichTextStyle pairs where the Int
** specifies a zero based char offset of the line.
**
virtual Obj[] lineStyles(Int lineIndex) { return null }
}
**************************************************************************
** RichTextStyle
**************************************************************************
**
** Defines the font and color styling of a text
** segment in a `RichTextModel`.
**
const class RichTextStyle
{
** Foreground color
const Color fg
** Background color or null
const Color bg
** Font of text segment
const Font font
/* Waiting for Eclipse 3.4...
** Underline style: `underlineNone`, `underlineSingle`,
** `underlineDouble`, `underlineError`, underlineSquiggle`
const Int underline := underlineNone
static const Int underlineNone := 0
static const Int underlineSingle := 1
static const Int underlineDouble := 2
static const Int underlineError := 3
static const Int underlineSquiggle := 4
** Underline color
const Color underlineColor
*/
override Str toStr()
{
s := StrBuf()
if (fg != null) s.add("fg=$fg")
if (bg != null) s.add(" bg=$bg")
if (font != null) s.add(" font=$font")
return s.toStr.trim
}
}