+// count utf8 chars in text which occur before cp
+int BC_TextBox::wcpos(const char *text, const char *cp)
+{
+ int len = 0;
+ const unsigned char *bp = (const unsigned char *)text;
+ const unsigned char *ep = (const unsigned char *)cp;
+ while( bp < ep && *bp != 0 ) {
+ ++len;
+ int ch = *bp++;
+ if( ch < 0x80 ) continue;
+ int i = ch - 0x0c0;
+ int n = i<0? 0 : i<32? 1 : i<48? 2 : i<56? 3 : i<60? 4 : 5;
+ for( i=n; bp < ep && --i>=0 && (*bp&0xc0) == 0x80; ++bp );
+ }
+ return len;
+}
+
+void BC_TextBox::set_selection(int char1, int char2, int ibeam)
+{
+ const char *cp = get_text();
+ wset_selection(wcpos(cp, cp+char1), wcpos(cp, cp+char2), wcpos(cp, cp+ibeam));
+}
+