Return false to discard a character.
| 3799 | |
| 3800 | // Return false to discard a character. |
| 3801 | static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source) |
| 3802 | { |
| 3803 | IM_ASSERT(input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Clipboard); |
| 3804 | unsigned int c = *p_char; |
| 3805 | |
| 3806 | // Filter non-printable (NB: isprint is unreliable! see #2467) |
| 3807 | bool apply_named_filters = true; |
| 3808 | if (c < 0x20) |
| 3809 | { |
| 3810 | bool pass = false; |
| 3811 | pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); // Note that an Enter KEY will emit \r and be ignored (we poll for KEY in InputText() code) |
| 3812 | pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); |
| 3813 | if (!pass) |
| 3814 | return false; |
| 3815 | apply_named_filters = false; // Override named filters below so newline and tabs can still be inserted. |
| 3816 | } |
| 3817 | |
| 3818 | if (input_source != ImGuiInputSource_Clipboard) |
| 3819 | { |
| 3820 | // We ignore Ascii representation of delete (emitted from Backspace on OSX, see #2578, #2817) |
| 3821 | if (c == 127) |
| 3822 | return false; |
| 3823 | |
| 3824 | // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME) |
| 3825 | if (c >= 0xE000 && c <= 0xF8FF) |
| 3826 | return false; |
| 3827 | } |
| 3828 | |
| 3829 | // Filter Unicode ranges we are not handling in this build |
| 3830 | if (c > IM_UNICODE_CODEPOINT_MAX) |
| 3831 | return false; |
| 3832 | |
| 3833 | // Generic named filters |
| 3834 | if (apply_named_filters && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific))) |
| 3835 | { |
| 3836 | // The libc allows overriding locale, with e.g. 'setlocale(LC_NUMERIC, "de_DE.UTF-8");' which affect the output/input of printf/scanf to use e.g. ',' instead of '.'. |
| 3837 | // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. |
| 3838 | // We don't really intend to provide widespread support for it, but out of empathy for people stuck with using odd API, we support the bare minimum aka overriding the decimal point. |
| 3839 | // Change the default decimal_point with: |
| 3840 | // ImGui::GetCurrentContext()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; |
| 3841 | // Users of non-default decimal point (in particular ',') may be affected by word-selection logic (is_word_boundary_from_right/is_word_boundary_from_left) functions. |
| 3842 | ImGuiContext& g = *GImGui; |
| 3843 | const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint; |
| 3844 | |
| 3845 | // Full-width -> half-width conversion for numeric fields (https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block) |
| 3846 | // While this is mostly convenient, this has the side-effect for uninformed users accidentally inputting full-width characters that they may |
| 3847 | // scratch their head as to why it works in numerical fields vs in generic text fields it would require support in the font. |
| 3848 | if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | ImGuiInputTextFlags_CharsHexadecimal)) |
| 3849 | if (c >= 0xFF01 && c <= 0xFF5E) |
| 3850 | c = c - 0xFF01 + 0x21; |
| 3851 | |
| 3852 | // Allow 0-9 . - + * / |
| 3853 | if (flags & ImGuiInputTextFlags_CharsDecimal) |
| 3854 | if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/')) |
| 3855 | return false; |
| 3856 | |
| 3857 | // Allow 0-9 . - + * / e E |
| 3858 | if (flags & ImGuiInputTextFlags_CharsScientific) |
no test coverage detected