Return false to discard a character.
| 3916 | |
| 3917 | // Return false to discard a character. |
| 3918 | static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source) |
| 3919 | { |
| 3920 | IM_ASSERT(input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Clipboard); |
| 3921 | unsigned int c = *p_char; |
| 3922 | |
| 3923 | // Filter non-printable (NB: isprint is unreliable! see #2467) |
| 3924 | bool apply_named_filters = true; |
| 3925 | if (c < 0x20) |
| 3926 | { |
| 3927 | bool pass = false; |
| 3928 | pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); // Note that an Enter KEY will emit \r and be ignored (we poll for KEY in InputText() code) |
| 3929 | pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); |
| 3930 | if (!pass) |
| 3931 | return false; |
| 3932 | apply_named_filters = false; // Override named filters below so newline and tabs can still be inserted. |
| 3933 | } |
| 3934 | |
| 3935 | if (input_source != ImGuiInputSource_Clipboard) |
| 3936 | { |
| 3937 | // We ignore Ascii representation of delete (emitted from Backspace on OSX, see #2578, #2817) |
| 3938 | if (c == 127) |
| 3939 | return false; |
| 3940 | |
| 3941 | // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME) |
| 3942 | if (c >= 0xE000 && c <= 0xF8FF) |
| 3943 | return false; |
| 3944 | } |
| 3945 | |
| 3946 | // Filter Unicode ranges we are not handling in this build |
| 3947 | if (c > IM_UNICODE_CODEPOINT_MAX) |
| 3948 | return false; |
| 3949 | |
| 3950 | // Generic named filters |
| 3951 | if (apply_named_filters && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific))) |
| 3952 | { |
| 3953 | // 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 '.'. |
| 3954 | // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. |
| 3955 | // 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. |
| 3956 | // Change the default decimal_point with: |
| 3957 | // ImGui::GetIO()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; |
| 3958 | // 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. |
| 3959 | ImGuiContext& g = *ctx; |
| 3960 | const unsigned c_decimal_point = (unsigned int)g.IO.PlatformLocaleDecimalPoint; |
| 3961 | if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific)) |
| 3962 | if (c == '.' || c == ',') |
| 3963 | c = c_decimal_point; |
| 3964 | |
| 3965 | // Full-width -> half-width conversion for numeric fields (https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block) |
| 3966 | // While this is mostly convenient, this has the side-effect for uninformed users accidentally inputting full-width characters that they may |
| 3967 | // scratch their head as to why it works in numerical fields vs in generic text fields it would require support in the font. |
| 3968 | if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | ImGuiInputTextFlags_CharsHexadecimal)) |
| 3969 | if (c >= 0xFF01 && c <= 0xFF5E) |
| 3970 | c = c - 0xFF01 + 0x21; |
| 3971 | |
| 3972 | // Allow 0-9 . - + * / |
| 3973 | if (flags & ImGuiInputTextFlags_CharsDecimal) |
| 3974 | if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/')) |
| 3975 | return false; |
no test coverage detected