Skip to content

Commit

Permalink
Fixed Fl_Input_::index(int) to return a UCS4 character instead of a b…
Browse files Browse the repository at this point in the history
…yte.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@6777 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Matthias Melcher committed Apr 23, 2009
1 parent a8fdff5 commit 813d295
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
35 changes: 23 additions & 12 deletions FL/Fl_Input_.H
Expand Up @@ -77,7 +77,27 @@
#define FL_MULTILINE_OUTPUT_WRAP (FL_MULTILINE_INPUT | FL_INPUT_READONLY | FL_INPUT_WRAP)
\endcode
All variables that represent an index into a text buffer are byte-oriented,
not character oriented. Since utf8 characters can be up to six bytes long,
simply incrementing such an index will not reliably advance to the next character
in the text buffer.
Indices and pointers into the text buffer shoudl always point at an 7 bit ASCII
character or the beginning of a utf8 character sequence. Behavior for false
utf8 sequences and pointers into the middle of a seqeunce are undefined.
\see Fl_Text_Display, Fl_Text_Editor for more powerful text handling widgets
\internal
When porting this widget from ASCII to UTF8, previously legal pointers into
the text of this widget can become illegal by pointing into the middle of
a UTF8 seuence. This is not a big problem for Fl_Input_ because all code
in this module is quite tolerant. It could be problematic though when deriving
from this class because no feedback for illegal pointers is given. Additionaly,
a careless "copy" call can put partial UTF8 sequnces into the clipboard.
None of these issues should be desasterous. Nevertheless, we should
discuss how FLTK should handle false UTF8 suequences and pointers.
*/
class FL_EXPORT Fl_Input_ : public Fl_Widget {

Expand All @@ -90,7 +110,7 @@ class FL_EXPORT Fl_Input_ : public Fl_Widget {
/** \internal Size of text in bytes in the \p value_ field. */
int size_;

/** \internal Please document me! */
/** \internal \todo Please document me! */
int bufsize;

/** \internal Positin of the cursor in the document */
Expand Down Expand Up @@ -225,17 +245,8 @@ public:
*/
const char* value() const {return value_;}

/**
Returns the character at index \p i.
This function returns the utf8 character that is closest to \p i
as a ucs4 character code.
\param [in] i index into the value field
\return the character at index \p i
\todo Not yet utf8 aware
*/
char index(int i) const {return value_[i];}
/* Returns the character at index \p i. */
Fl_Char index(int i) const;

/**
Returns the number of bytes in value().
Expand Down
15 changes: 15 additions & 0 deletions src/Fl_Input_.cxx
Expand Up @@ -1239,6 +1239,21 @@ int Fl_Input_::linesPerPage() {
return n;
}

/**
Returns the character at index \p i.
This function returns the utf8 character at \p i
as a ucs4 character code.
\param [in] i index into the value field
\return the character at index \p i
*/
Fl_Char Fl_Input_::index(int i) const
{
int len = 0;
return fl_utf8decode(value_+i, value_+size_, &len);
}

//
// End of "$Id$".
//
4 changes: 4 additions & 0 deletions test/input.cxx
Expand Up @@ -52,6 +52,10 @@ void toggle_cb(Fl_Widget *o, long v) {
void test(Fl_Input *i) {
if (i->changed()) {
i->clear_changed(); printf("%s '%s'\n",i->label(),i->value());
char utf8buf[10];
int last = fl_utf8encode(i->index(i->position()), utf8buf);
utf8buf[last] = 0;
printf("Symbol at cursor position: %s\n", utf8buf);
}
}

Expand Down

0 comments on commit 813d295

Please sign in to comment.