Laxkit
0.0.7.1
|
A base class for text edits using utf8 encoded character arrays. More...
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 char * | GetCText () |
Return a const pointer to thetext. | |
virtual char * | GetText () |
Return a new char[] copy of thetext. | |
virtual char * | GetSelText () |
Return a new char[] copy of the selected text. | |
virtual char * | CutSelText () |
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 |
char * | thetext |
char * | cutbuffer |
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 |
A base class for text edits using utf8 encoded character arrays.
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.
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.
|
protectedvirtual |
Return 1 for added, or 0 for ignoring add, for instance was in middle of an undo.
|
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[].
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().
|
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().
Find str in thetext, start search at fromcurs, wrap around.
Referenced by Laxkit::MultiLineEdit::Find().
|
virtual |
Find the start and end of a word that includes position pos.
Return 1 for word found, 0 for not found.
|
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().
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().
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().
|
virtual |
Return the number of newlines in range [s,e].
Only counts chars matching the first delimiter.
|
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!
|
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.
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().
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().
Insert a string of text. after!=0 makes cursor jump to after the insert.
Reimplemented in Laxkit::MultiLineEdit.
Referenced by Laxkit::MultiLineEdit::insstring(), and Laxkit::TextXEditBaseUtf8::Paste().
|
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().
|
protectedvirtual |
Return the next utf8 character position after l.
If l happens to be in the middle of a utf8 char, return the start of the next char.
Referenced by Laxkit::LineEdit::CharInput(), Laxkit::TextXEditBaseUtf8::ExtentAndStr(), Laxkit::MultiLineEdit::findline(), Laxkit::MultiLineEdit::findpos(), Laxkit::TextXEditBaseUtf8::GetPos(), Laxkit::MultiLineEdit::LBDblClick(), Laxkit::LineEdit::LBDblClick(), and Laxkit::PromptEdit::ProcessInput().
|
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().
|
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().
Reimplemented in Laxkit::MultiLineEdit.
Referenced by Laxkit::MultiLineEdit::Replace().
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.
|
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().
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.
|
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.
|
virtual |
Return how many newlines before pos.
defaults to count from beginning,first line==0, one line per newline
Reimplemented in Laxkit::MultiLineEdit.
Referenced by Laxkit::TextXEditBaseUtf8::DrawCaret().