From 813d295e8a453fccaf5b7acfb55c7c07388bf731 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 23 Apr 2009 15:32:19 +0000 Subject: Fixed Fl_Input_::index(int) to return a UCS4 character instead of a byte. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@6777 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- FL/Fl_Input_.H | 35 +++++++++++++++++++++++------------ src/Fl_Input_.cxx | 15 +++++++++++++++ test/input.cxx | 4 ++++ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/FL/Fl_Input_.H b/FL/Fl_Input_.H index 960f75afb..2467a476d 100644 --- a/FL/Fl_Input_.H +++ b/FL/Fl_Input_.H @@ -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 { @@ -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 */ @@ -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(). diff --git a/src/Fl_Input_.cxx b/src/Fl_Input_.cxx index 8f5f69e0b..60ec85289 100644 --- a/src/Fl_Input_.cxx +++ b/src/Fl_Input_.cxx @@ -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$". // diff --git a/test/input.cxx b/test/input.cxx index 8fb9f214f..17534c812 100644 --- a/test/input.cxx +++ b/test/input.cxx @@ -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); } } -- cgit v1.2.3