Browse Source

InputText: numerical fields automatically accept full-width characters (U+FF01..U+FF5E) by converting them to half-width (U+0021..U+007E).

ocornut 3 years ago
parent
commit
268565079c
2 changed files with 12 additions and 1 deletions
  1. 2 0
      docs/CHANGELOG.txt
  2. 10 1
      imgui_widgets.cpp

+ 2 - 0
docs/CHANGELOG.txt

@@ -65,6 +65,8 @@ Other Changes:
 
 
 - InputText: added experimental io.ConfigInputTextEnterKeepActive feature to make pressing
 - InputText: added experimental io.ConfigInputTextEnterKeepActive feature to make pressing
   Enter keep the input active and select all text.
   Enter keep the input active and select all text.
+- InputText: numerical fields automatically accept full-width characters (U+FF01..U+FF5E)
+  by converting them to half-width (U+0021..U+007E).
 - Tables,Columns: fixed a layout issue where SameLine() prior to a row change would set the
 - Tables,Columns: fixed a layout issue where SameLine() prior to a row change would set the
   next row in such state where subsequent SameLine() would move back to previous row.
   next row in such state where subsequent SameLine() would move back to previous row.
 - Tabs: Fixed a crash when closing multiple windows (possible with docking only) with an
 - Tabs: Fixed a crash when closing multiple windows (possible with docking only) with an

+ 10 - 1
imgui_widgets.cpp

@@ -3923,6 +3923,13 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f
         ImGuiContext& g = *GImGui;
         ImGuiContext& g = *GImGui;
         const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint;
         const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint;
 
 
+        // Full-width -> half-width conversion for numeric fields (https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block)
+        // While this is mostly convenient, this has the side-effect for uninformed users accidentally inputting full-width characters that they may
+        // scratch their head as to why it works in numerical fields vs in generic text fields it would require support in the font.
+        if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | ImGuiInputTextFlags_CharsHexadecimal))
+            if (c >= 0xFF01 && c <= 0xFF5E)
+                c = c - 0xFF01 + 0x21;
+
         // Allow 0-9 . - + * /
         // Allow 0-9 . - + * /
         if (flags & ImGuiInputTextFlags_CharsDecimal)
         if (flags & ImGuiInputTextFlags_CharsDecimal)
             if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/'))
             if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/'))
@@ -3941,11 +3948,13 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f
         // Turn a-z into A-Z
         // Turn a-z into A-Z
         if (flags & ImGuiInputTextFlags_CharsUppercase)
         if (flags & ImGuiInputTextFlags_CharsUppercase)
             if (c >= 'a' && c <= 'z')
             if (c >= 'a' && c <= 'z')
-                *p_char = (c += (unsigned int)('A' - 'a'));
+                c += (unsigned int)('A' - 'a');
 
 
         if (flags & ImGuiInputTextFlags_CharsNoBlank)
         if (flags & ImGuiInputTextFlags_CharsNoBlank)
             if (ImCharIsBlankW(c))
             if (ImCharIsBlankW(c))
                 return false;
                 return false;
+
+        *p_char = c;
     }
     }
 
 
     // Custom callback filter
     // Custom callback filter