Added support for characters from the current 'locale' for the floating point input field, so that us poor Europeans can finally enter the "," character instead of that English decimal point.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4452 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Matthias Melcher 2005-07-24 17:42:50 +00:00
parent 9688b0b1c3
commit 96e45cb448
2 changed files with 35 additions and 1 deletions

View File

@ -2,6 +2,8 @@ CHANGES IN FLTK 1.1.7
- Documentation fixes (STR #648, STR #692, STR #730, STR
#744, STR #745, STR #942)
- Floating point input field allows characters from
current locale (STR #903)
- Fixed integration of Fl_Input_Choice into Fluid (STR #879)
- New windows touching the right screen border would be
positioned all the way to the left (STR #898)

View File

@ -32,6 +32,8 @@
// the keybindings.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/fl_draw.H>
@ -62,6 +64,14 @@ int Fl_Input::shift_up_down_position(int p) {
#define ctrl(x) ((x)^0x40)
// List of characters that are leagal in a floating point input field.
// This text string is created at run-time to take the current locale
// into account (for example, continental Europe uses a comma instead
// of a decimal point). For back compatibility reasons, we always
// allow the decimal point.
static char *standard_fp_chars = ".eE+-";
static char *legal_fp_chars = 0L;
int Fl_Input::handle_key() {
char ascii = Fl::event_text()[0];
@ -75,13 +85,35 @@ int Fl_Input::handle_key() {
if (input_type() == FL_FLOAT_INPUT || input_type() == FL_INT_INPUT) {
Fl::compose_reset(); // ignore any foreign letters...
// initialize the list of legal characters inside a floating point number
if (!legal_fp_chars) {
int len = strlen(standard_fp_chars);
struct lconv *lc = localeconv();
if (lc) {
if (lc->decimal_point) len += strlen(lc->decimal_point);
if (lc->mon_decimal_point) len += strlen(lc->mon_decimal_point);
if (lc->positive_sign) len += strlen(lc->positive_sign);
if (lc->negative_sign) len += strlen(lc->negative_sign);
}
// the following line is not a true memory leak because the array is only
// allocated once if required, and automatically freed when the program quits
legal_fp_chars = (char*)malloc(len+1);
strcpy(legal_fp_chars, standard_fp_chars);
if (lc) {
if (lc->decimal_point) strcat(legal_fp_chars, lc->decimal_point);
if (lc->mon_decimal_point) strcat(legal_fp_chars, lc->mon_decimal_point);
if (lc->positive_sign) strcat(legal_fp_chars, lc->positive_sign);
if (lc->negative_sign) strcat(legal_fp_chars, lc->negative_sign);
}
}
// This is complex to allow "0xff12" hex to be typed:
if (!position() && (ascii == '+' || ascii == '-') ||
(ascii >= '0' && ascii <= '9') ||
(position()==1 && index(0)=='0' && (ascii=='x' || ascii == 'X')) ||
(position()>1 && index(0)=='0' && (index(1)=='x'||index(1)=='X')
&& (ascii>='A'&& ascii<='F' || ascii>='a'&& ascii<='f')) ||
input_type()==FL_FLOAT_INPUT && ascii && strchr(".eE+-", ascii)) {
input_type()==FL_FLOAT_INPUT && ascii && strchr(legal_fp_chars, ascii)) {
if (readonly()) fl_beep();
else replace(position(), mark(), &ascii, 1);
}