Laxkit  0.0.7.1
Public Member Functions | Protected Member Functions | Protected Attributes | Friends
Laxkit::TextEditBaseUtf8 Class Reference

A base class for text edits using utf8 encoded character arrays. More...

Inheritance diagram for Laxkit::TextEditBaseUtf8:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 TextEditBaseUtf8 (const char *newtext=NULL, unsigned long nstyle=0, unsigned int ncntlchar=0)
 Constructor.
virtual ~TextEditBaseUtf8 ()
 delete[] thetext and cutbuffer.
virtual int charwidth (unsigned int usc, int actual=0)
virtual int GetTabChar (int atpix)
 For CHAR_TABS, returns the character to center on, or 0 if no such tab there.
virtual int GetNextTab (int atpix)
 Redefine for custom tabstops, default is even spacing with tabwidth pixels.
virtual int GetNextTab (int atpix, int &tabtype)
 Redefine for custom tabstops, default is even spacing with tabwidth pixels.
virtual int inschar (unsigned int ucs, char a=1)
 Insert character at curpos. Afterward, make curpos on it (a==0) or after (a==1) it. Ignores selection, always at curpos.
virtual int insstring (const char *blah, int after=0)
 Insert a string of text. after!=0 makes cursor jump to after the insert.
virtual int delsel ()
 Delete [selstart,curpos], but only if sellen>0.
virtual int delchar (int bksp)
 Delete: bksp==1 means delete curpos-1, 0 del curpos.
virtual int replacesel (unsigned int ucs)
 Replace the selection with character ch.
virtual int replacesel (const char *newt=NULL, int after=0)
 Replace the selection with utf8 encoded newt, placing cursor at beginning of newt (after=0) or after it.
virtual int onlf (long pos=-1)
 Return whether pos is on a delimiter. 0==no, 1==first delimiter, 2==second delimeter.
virtual int findword (long pos, long &start, long &end)
 Find the start and end of a word that includes position pos.
virtual int isword (long pos=-1)
 Return if character at pos is alphanumeric character.
virtual long findlinestart (long pos=-1)
 Return first place after the first newline before pos.
virtual int readonly (long pos=-1)
 Returns if the text is readonly at the given position.
virtual int SetText (const char *newtext)
 Set thetext equal to newtext (copies it).
virtual const charGetCText ()
 Return a const pointer to thetext.
virtual charGetText ()
 Return a new char[] copy of thetext.
virtual charGetSelText ()
 Return a new char[] copy of the selected text.
virtual charCutSelText ()
 Cut out the selected text, and return a copy to a new char[] containing the text.
virtual long GetSelection (long &sels, long &sele)
 Get information about the selection positions.
virtual int SetSelection (long newss=-2, long newse=-2)
 Sets selstart to be newss and curpos to newse.
virtual long Getnumlines ()
 Return the number of newlines in the whole buffer.
virtual long Getnlines (long s=-1, long e=-1)
 Return the number of newlines in range [s,e].
virtual long Getcharswide ()
 Returns most characters wide in all of lines of thetext.
virtual int Getpixwide (long linenum)
 Return pix wide of line from p to newline.
virtual int Modified (int m=1)
virtual int Cut ()
 Cut and copy selection to cutbuffer.
virtual int Copy ()
 Copy selection to cutbuffer.
virtual int Paste ()
 Paste cutbuffer to curpos or selection location.
virtual int Undo ()
virtual int Redo ()
virtual long WhichLine (long pos)
 Return how many newlines before pos.
virtual long GetCurLine ()
virtual long SetCurLine (long nline)
virtual long GetCurpos ()
virtual long SetCurpos (long ncurpos)
 Returns new curpos.
virtual int SetDelimiter (char n1, char n2=0)
 Set the newline delimiter to "n1n2". n2==0 means use single character newline.
virtual int Find (const char *str, int fromcurs=1)
 Find str in thetext, start search at fromcurs, wrap around.
virtual int Replace (const char *newstr, const char *, int which)
virtual int Replace (const char *newstr, int start, int end)
 Replace a range of text with a new string.

Protected Member Functions

virtual int extendtext ()
 Extend the memory allocated for thetext.
virtual long nextpos (long l)
 Return the next utf8 character position after l.
virtual long prevpos (long l)
 Return the utf8 character position befor l.
virtual void makevalidpos (long &l)
 Make l point only to 1st byte of a utf8 char or 2-char delimiter.
virtual int Undo (UndoData *data)
virtual int Redo (UndoData *data)
virtual int AddUndo (int type, const char *str, long len, long start, long end)

Protected Attributes

long curline
long curpos
long selstart
long sellen
int cntlmovedist
int tabwidth
charthetext
charcutbuffer
long textlen
long maxtextmem
char newline
char newline2
char cntlchar
char modified
long maxtextlen
long mintextlen
long maxcharswide
long mincharswide
long maxlines
long minlines
unsigned long textstyle
int undomode
UndoManager undomanager

Friends

class UndoManager

Detailed Description

A base class for text edits using utf8 encoded character arrays.

Todo:

might be good idea to somehow define the encoding? Latin-1, utf8, ascii? probably much more hassle than it's worth.. simpler to do all in utf8.

*** thinks all utf8 chars are actually distinct chars... is this true? are some utf8 actually tags for vowel marks over a previous char? at times like these, pango is looking pretty good..

implement max/min textlen/numlines

This class provides the basic memory management functions for manipulating a big character array of text. These include inserting and deleting single characters and strings of characters, and finding words and lines near a given point. Actual formating, displaying, and such are not handled in this class. TextXEditBase and classes derived from in handle displaying.

It can handle any of the 3 main systems of line delimiting commonly found in text files: '\n' for unix/linux, '\r' for macs, and '\r\n' for windows text files. The cursor should always be on the first byte.

Really the point of the Laxkit text edits setup with TextEditBaseUtf8–TextXEditBase–ActualEdit is to provide a common text manipulation scheme, most of which I wouldn't have to reprogram whenever I wanted to make a text edit in other window systems, like ncurses for instance. In terms of code generated, and speed of action, it adds a couple of function calls per action, but that probably slows things down only just slightly. If that seems to really be noticable, then at some point I would probably not bother with any *EditBase, and just reimplement the scheme for single LineEdits, and then again for GoodEditWW, since they both have rather different needs for text lookup.

The following defines are kind of an all-in-one source for text edit related flags. Not all of them are actually accessed in TextEditBaseUtf8. These are stored in textstyle rather than win_style.

#define LEFT_TAB (1<<0)
#define CENTER_TAB (1<<1)
#define RIGHT_TAB (1<<2)
#define CHAR_TAB (1<<3)
#define NUMERIC_TAB (1<<3)
#define TEXT_LEFT (1<<0)
#define TEXT_CENTER (1<<1)
#define TEXT_RIGHT (1<<2)
#define TEXT_JUSTIFY (1<<3)
#define TEXT_JUSTIFY_RIGHT (1<<2 | 1<<3)
#define TEXT_JUSTIFY_CENTER (1<<1 | 1<<3)
#define TEXT_FORCEJUSTIFY (1<<4)
#define TEXT_WORDWRAP (1<<5)
#define TEXT_READONLY (1<<6)
#define TEXT_NLONLY (1<<7)
#define TEXT_CRLF (1<<9)
#define TEXT_LF (1<<7)
#define TEXT_CR (1<<8)
// write non-chars as cntl char (such as space, '.','\' etc), cntlchar+hex, none
#define TEXT_CNTL_BANG (1<<10)
#define TEXT_CNTL_HEX (1<<11)
#define TEXT_CNTL_NONE (1<<12)
// SHOWTABLINE means have a line at top of edit showing the tab stops
#define TEXT_TABS_SPACES (1<<13)
#define TEXT_TABS_EVEN (1<<14)
#define TEXT_TABS_STOPS (1<<15)
#define TEXT_TABS_NONE (1<<16)
#define TEXT_SHOWTABLINE (1<<17)
// put little divits where space/newline/tabs are
#define TEXT_SHOW_WHITESPACE (1<<18)
// default scrollers is for transient scrollers, special check for ALWAYS must be made in classes
#define TEXT_NOSCROLLERS (1<<19)
#define TEXT_SCROLLERS (1<<20 | 1<<21)
#define TEXT_TRANSIENT_SCROLL (1<<20 | 1<<21)
#define TEXT_TRANSIENT_X (1<<20)
#define TEXT_TRANSIENT_Y (1<<21)
#define TEXT_XSCROLL (1<<20)
#define TEXT_YSCROLL (1<<21)
#define TEXT_ALWAYS_SCROLLERS (1<<20 | 1<<21 | 1<<22 | 1<<23)
#define TEXT_ALWAYS_X (1<<22 | 1<<20)
#define TEXT_ALWAYS Y (1<<23 | 1<<21)
// Set this if you want to be able to move the cursor in regions where
// there would normally not be anything, like after the newline of a short line.
#define TEXT_CURS_PAST_NL (1<<24)
Todo:
***finish putting in Modified() to whatever modifies!!!

Constructor & Destructor Documentation

Laxkit::TextEditBaseUtf8::TextEditBaseUtf8 ( const char newtext = NULL,
unsigned long  nstyle = 0,
unsigned int  ncntlchar = 0 
)

Constructor.

If there is no justification set, then TEXT_LEFT is applied. If no control character scheme is set, then TEXT_CNTL_BANG is applied. The default delimiter is a newline ('\n'==ascii 0x0A). Default tabs is to allow them, and have them evenly spaced at tabwidth=4.

ncntlchar is the UCS code for the character that shows for character that is not printable. For TEXT_CNTL_BANG, this is '!'.

newtext is copied, and is assumed to be utf8 encoded.


Member Function Documentation

int Laxkit::TextEditBaseUtf8::AddUndo ( int  type,
const char str,
long  len,
long  start,
long  end 
)
protectedvirtual

Return 1 for added, or 0 for ignoring add, for instance was in middle of an undo.

char * Laxkit::TextEditBaseUtf8::CutSelText ( )
virtual

Cut out the selected text, and return a copy to a new char[] containing the text.

The calling code is responsible for calling delete[] on the returned char[].

int Laxkit::TextEditBaseUtf8::delchar ( int  bksp)
virtual

Delete: bksp==1 means delete curpos-1, 0 del curpos.

Return nonzero if nothing changed, and 0 if something changed.

Ignores the selection.

Reimplemented in Laxkit::MultiLineEdit.

Referenced by Laxkit::MultiLineEdit::delchar().

int Laxkit::TextEditBaseUtf8::delsel ( )
virtual

Delete [selstart,curpos], but only if sellen>0.

Return nonzero for nothing changed, or 0 for selection deleted.

Reimplemented in Laxkit::MultiLineEdit.

Referenced by Laxkit::TextXEditBaseUtf8::Cut(), and Laxkit::MultiLineEdit::delsel().

int Laxkit::TextEditBaseUtf8::Find ( const char str,
int  fromcurs = 1 
)
virtual

Find str in thetext, start search at fromcurs, wrap around.

Todo:
*** should optionally do case insensitive search

Referenced by Laxkit::MultiLineEdit::Find().

int Laxkit::TextEditBaseUtf8::findword ( long  pos,
long &  start,
long &  end 
)
virtual

Find the start and end of a word that includes position pos.

Return 1 for word found, 0 for not found.

const char * Laxkit::TextEditBaseUtf8::GetCText ( )
virtual

Return a const pointer to thetext.

Useful for when you just want to scan the text for some reason, and not copy the whole thing.

Referenced by Laxkit::InputDialog::Event(), Laxkit::SimplePrint::Event(), Laxkit::ImageDialog::Event(), Laxkit::LineInput::GetCText(), Laxkit::LineEdit::Modified(), Laxkit::SimplePrint::Print(), and Laxkit::ImageDialog::updateImageInfo().

int Laxkit::TextEditBaseUtf8::GetNextTab ( int  atpix)
virtual

Redefine for custom tabstops, default is even spacing with tabwidth pixels.

Returns the next tab stop which is at a pixel position greater than atpix.

Referenced by Laxkit::TextXEditBaseUtf8::DrawTabLine(), Laxkit::TextXEditBaseUtf8::GetExtent(), Laxkit::TextXEditBaseUtf8::GetPos(), and Laxkit::TextXEditBaseUtf8::TextOut().

int Laxkit::TextEditBaseUtf8::GetNextTab ( int  atpix,
int tabtype 
)
virtual

Redefine for custom tabstops, default is even spacing with tabwidth pixels.

This is a more detailed version of GetNextTab. The return value is still the next tab stop at pixel position greater than atpix, and also sets tabtype to the type of tab returned. The default is LEFT_TAB, but the other defined types are RIGHT_TAB, CENTER_TAB, NUMERIC_TAB, and CHAR_TAB. Note that tab types are not relevant to TextEditBaseUtf8. They are more or less implemented in TextXEditBase, in conjunction with the function GetTabChar().

long Laxkit::TextEditBaseUtf8::Getnlines ( long  s = -1,
long  e = -1 
)
virtual

Return the number of newlines in range [s,e].

Only counts chars matching the first delimiter.

int Laxkit::TextEditBaseUtf8::Getpixwide ( long  p)
virtual

Return pix wide of line from p to newline.

NOTE that this assumes that each character takes up a certain width, which is added on to the summed widths of previous characters, which is a false assumption for various non-english languages!

long Laxkit::TextEditBaseUtf8::GetSelection ( long &  sels,
long &  sele 
)
virtual

Get information about the selection positions.

sels gets assigned to the selstart. sele gets curpos. Returns the length of the selection. Note that this return value is bytes long, not characters long.

int Laxkit::TextEditBaseUtf8::GetTabChar ( int  atpix)
virtual

For CHAR_TABS, returns the character to center on, or 0 if no such tab there.

The number returned is the UCS code for the character.

Referenced by Laxkit::TextXEditBaseUtf8::GetExtent(), Laxkit::TextXEditBaseUtf8::GetPos(), and Laxkit::TextXEditBaseUtf8::TextOut().

int Laxkit::TextEditBaseUtf8::inschar ( unsigned int  ucs,
char  a = 1 
)
virtual

Insert character at curpos. Afterward, make curpos on it (a==0) or after (a==1) it. Ignores selection, always at curpos.

if ch=='\n' then put in the delimiter.

Return nonzero if nothing changed, and 0 if something changed.

References Laxkit::utf8bytes(), and Laxkit::utf8encode().

Referenced by Laxkit::MultiLineEdit::inschar().

int Laxkit::TextEditBaseUtf8::insstring ( const char blah,
int  after = 0 
)
virtual

Insert a string of text. after!=0 makes cursor jump to after the insert.

Todo:
should probably validate blah. currently it is assumed that blah is valid utf8.

Reimplemented in Laxkit::MultiLineEdit.

Referenced by Laxkit::MultiLineEdit::insstring(), and Laxkit::TextXEditBaseUtf8::Paste().

int Laxkit::TextEditBaseUtf8::isword ( long  pos = -1)
virtual

Return if character at pos is alphanumeric character.

This is used in cntl-left/right to hop over words.

Reimplemented in Laxkit::MultiLineEdit.

References Laxkit::utf8decode().

Referenced by Laxkit::LineEdit::CharInput(), and Laxkit::LineEdit::LBDblClick().

long Laxkit::TextEditBaseUtf8::nextpos ( long  l)
protectedvirtual
long Laxkit::TextEditBaseUtf8::prevpos ( long  l)
protectedvirtual

Return the utf8 character position befor l.

If l happens to be in the middle of a utf8 char, return the start of the character it is in.

Referenced by Laxkit::LineEdit::CharInput(), Laxkit::MultiLineEdit::findline(), Laxkit::MultiLineEdit::findlinestart(), Laxkit::MultiLineEdit::findpos(), Laxkit::MultiLineEdit::LBDblClick(), Laxkit::LineEdit::LBDblClick(), and Laxkit::MultiLineEdit::makelinestart().

int Laxkit::TextEditBaseUtf8::readonly ( long  pos = -1)
virtual

Returns if the text is readonly at the given position.

If pos<0, then curpos should be assumed.

Default is to return (textstyle&TEXT_READONLY).

Reimplemented in Laxkit::PromptEdit.

Referenced by Laxkit::LineEdit::CharInput().

int Laxkit::TextEditBaseUtf8::Replace ( const char newstr,
const char ,
int  which 
)
virtual
Todo:
*** this is basically unimplemented currently

Reimplemented in Laxkit::MultiLineEdit.

Referenced by Laxkit::MultiLineEdit::Replace().

int Laxkit::TextEditBaseUtf8::replacesel ( const char newt = NULL,
int  after = 0 
)
virtual

Replace the selection with utf8 encoded newt, placing cursor at beginning of newt (after=0) or after it.

Return 0 for thetext changed, else nonzero.

long Laxkit::TextEditBaseUtf8::SetCurpos ( long  newcurpos)
virtual

Returns new curpos.

If the requested position is on the second character of a two character delimiter, then curpos is set to the first character.

If newcurpos<0 or >textlen, then set to textlen.

Referenced by Laxkit::PromptEdit::ProcessInput().

int Laxkit::TextEditBaseUtf8::SetDelimiter ( char  n1,
char  n2 = 0 
)
virtual

Set the newline delimiter to "n1n2". n2==0 means use single character newline.

Return 0 for not changed, and 1 for changed.

The delimiter can be 1 or 2 consecutive ASCII characters.

int Laxkit::TextEditBaseUtf8::SetSelection ( long  newss = -2,
long  newse = -2 
)
virtual

Sets selstart to be newss and curpos to newse.

Like SetCurpos, the positions are never set to the second character of a 2 character delimiter, nor to anything but the first byte of a utf8 char.

If newss or newse <0, then set them to textlen.

Returns sellen.

Reimplemented in Laxkit::MultiLineEdit.

long Laxkit::TextEditBaseUtf8::WhichLine ( long  pos)
virtual

Return how many newlines before pos.

defaults to count from beginning,first line==0, one line per newline

Todo:
*** perhaps remove this from this class, it is not used here, and a 'line' is perhaps too much a many splendored thing. besides as implemented here is very inefficient.

Reimplemented in Laxkit::MultiLineEdit.

Referenced by Laxkit::TextXEditBaseUtf8::DrawCaret().


The documentation for this class was generated from the following files:

Mon Feb 17 2014 11:53:00, Laxkit