9fans.net/go@v0.0.5/draw/frame/frame.go (about)

     1  // Package frame supports frames of editable text,
     2  // such as in the Plan 9 text editors sam and acme.
     3  package frame
     4  
     5  import (
     6  	"9fans.net/go/draw"
     7  )
     8  
     9  // A Frame is a frame of editable text in a single font on a raster display.
    10  // It may hold any character (even NUL, in contrast to the C original).
    11  // Long lines are folded and tabs are set at fixed intervals.
    12  //
    13  // P0 and p1 may be changed by the application provided the
    14  // selection routines are called afterwards to maintain a consistent display.
    15  // MaxTab determines the size of tab stops.
    16  // The Init method sets MaxTab to 8 times the width of a 0 (zero) character
    17  // in the font; it may be changed before any text is added to
    18  // the frame.
    19  // The other elements of the structure are maintained by the library
    20  // and should not be modified directly.
    21  //
    22  // The text within frames is not directly addressable; instead
    23  // frames are designed to work alongside another structure that
    24  // holds the text.  The typical application is to display a
    25  // section of a longer document such as a text file or terminal
    26  // session.  Usually the program will keep its own copy of the
    27  // text in the window (probably as an array of Runes) and pass
    28  // components of this text to the frame routines to display the
    29  // visible portion.  Only the text that is visible is held by
    30  // the Frame; the application must check maxlines, nlines, and
    31  // lastlinefull to determine, for example, whether new text
    32  // needs to be appended at the end of the Frame after calling
    33  // frdelete.
    34  //
    35  // There are no routines in the library to allocate Frames;
    36  // instead the interface assumes that Frames will be components
    37  // of larger structures and initialized using the Init method.
    38  //
    39  // Note that unlike most Go types, Frames must be explicitly
    40  // freed using the Clear method, in order to release the
    41  // associated images.
    42  //
    43  // Programs that wish to manage the selection themselves have
    44  // several routines to help.  They involve the maintenance of
    45  // the `tick', the vertical line indicating a null selection
    46  // between characters, and the colored region representing a
    47  // non-null selection. See the Tick, Drawsel, Drawsel0, and SelectPaint methods.
    48  type Frame struct {
    49  	P0, P1 int // selection
    50  	MaxTab int // max size of tab, in pixels
    51  
    52  	// Read-only to clients.
    53  	Font         *draw.Font        // of chars in the frame
    54  	Display      *draw.Display     // on which frame appears
    55  	B            *draw.Image       // on which frame appears
    56  	Cols         [NCOL]*draw.Image // text and background colors
    57  	R            draw.Rectangle    // in which text appears
    58  	Entire       draw.Rectangle    // of full frame
    59  	Scroll       func(*Frame, int) // scroll function provided by application
    60  	NumChars     int               // # runes in frame
    61  	NumLines     int               // # lines with text
    62  	MaxLines     int               // total # lines in frame
    63  	LastLineFull bool              // last line fills frame
    64  	Ticked       bool              // flag: is tick onscreen?
    65  	NoRedraw     int               // don't draw on the screen
    66  
    67  	box       []box
    68  	tick      *draw.Image // typing tick
    69  	tickback  *draw.Image // saved image under tick
    70  	tickscale int         // tick scaling factor
    71  	modified  bool        // changed since frselect
    72  }
    73  
    74  // BACK, HIGH, BORD, TEXT, and HTEXT are indices into Frame.Cols/
    75  const (
    76  	BACK  = iota // background color
    77  	HIGH         // highlight background color
    78  	BORD         // border color
    79  	TEXT         // text color
    80  	HTEXT        // highlight text color
    81  	NCOL
    82  )
    83  
    84  const _FRTICKW = 3
    85  
    86  func (b *box) NRUNE() int {
    87  	if b.nrune < 0 {
    88  		return 1
    89  	}
    90  	return b.nrune
    91  }
    92  
    93  func (b *box) NBYTE() int {
    94  	return len(b.bytes)
    95  }