How to uppercase the first char on an ImGui::InputText() with std::string using imgui_stdlib.h? #6457

Closed
opened 2023-05-25 06:35:13 +00:00 by moebiussurfing · 5 comments
moebiussurfing commented 2023-05-25 06:35:13 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

Version: v1.89.4
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: OpenGL/ openFrameworks
Compiler: VS 2022
Operating System: Windows10

My Issue/Question:

I am using imgui_stdlib.h to simplify the use of an std::string as text target...
I'm trying to do it with an ImGui::InputText but also on the hint version and multiline too.
(not at the same time, just one of the three.)

Standalone, minimal, complete and verifiable example

// Here's some code

//.h
string text = "";

//--

//.cpp and inside a window

const char* label = strLabel.c_str();
label = "##label";//remove

const char* hint = strHint.c_str();

void* user_data = NULL;
static bool isChanged = false;

ImGuiInputTextFlags _flags = ImGuiInputTextFlags_None;
_flags |= ImGuiInputTextFlags_CallbackCharFilter;

auto callback = [](ImGuiInputTextCallbackData* data) {
    //if (text.size() == 1) {
    //	std::cout << "callback" << std::endl;
    //}
    
    //if (data->EventFlag == ImGuiInputTextFlags_CallbackCharFilter) {
    //	// Only capitalize the first character if the buffer is empty
    //	if (data->BufTextLen == 0) {
    //		if (data->Buf && data->Buf[0]) {
    //			data->Buf[0] = toupper(data->Buf[0]);
    //			data->BufDirty = true;
    //			std::cout << "callback" << std::endl;
    //		}
    //	}
    //}

    return 0;
};

isChanged = ImGui::InputText(label, &text, _flags, callback, user_data);
isChanged = ImGui::InputTextWithHint(label, hint, &text, _flags, callback, user_data);
isChanged = ImGui::InputTextMultiline(label, &text, _sz, _flags, callback, user_data);

I am dealing with exceptions that I can't solve.

Any example or idea around this?

**Version/Branch of Dear ImGui:** Version: v1.89.4 Branch: master **Back-end/Renderer/Compiler/OS** Back-ends: OpenGL/ openFrameworks Compiler: VS 2022 Operating System: Windows10 **My Issue/Question:** I am using `imgui_stdlib.h` to simplify the use of an std::string as text target... I'm trying to do it with an ImGui::InputText but also on the hint version and multiline too. (not at the same time, just one of the three.) **Standalone, minimal, complete and verifiable example** ```.cpp // Here's some code //.h string text = ""; //-- //.cpp and inside a window const char* label = strLabel.c_str(); label = "##label";//remove const char* hint = strHint.c_str(); void* user_data = NULL; static bool isChanged = false; ImGuiInputTextFlags _flags = ImGuiInputTextFlags_None; _flags |= ImGuiInputTextFlags_CallbackCharFilter; auto callback = [](ImGuiInputTextCallbackData* data) { //if (text.size() == 1) { // std::cout << "callback" << std::endl; //} //if (data->EventFlag == ImGuiInputTextFlags_CallbackCharFilter) { // // Only capitalize the first character if the buffer is empty // if (data->BufTextLen == 0) { // if (data->Buf && data->Buf[0]) { // data->Buf[0] = toupper(data->Buf[0]); // data->BufDirty = true; // std::cout << "callback" << std::endl; // } // } //} return 0; }; isChanged = ImGui::InputText(label, &text, _flags, callback, user_data); isChanged = ImGui::InputTextWithHint(label, hint, &text, _flags, callback, user_data); isChanged = ImGui::InputTextMultiline(label, &text, _sz, _flags, callback, user_data); ``` I am dealing with exceptions that I can't solve. Any example or idea around this?
ocornut commented 2023-05-25 07:08:32 +00:00 (Migrated from github.com)

The commented code is not how to use ImGuiInputTextFlags_CallbackCharFilter. Please refer to demo code using it.

The commented code is not how to use ImGuiInputTextFlags_CallbackCharFilter. Please refer to demo code using it.
ocornut commented 2023-05-25 09:40:44 +00:00 (Migrated from github.com)

My apologies. This is only partially covered in the demo: ImGuiInputTextFlags_CallbackCharFilter is used but not for character replacement.
You can read data->EventChar and change it if you want, the modified character will then be inserted.

The only source of documentation for this is the flags description:

ImGuiInputTextFlags_CallbackCharFilter  = 1 << 9,   // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.

I will work on amending the demo to showcase replacement along with filtering.

Otherwise you can use ImGuiInputTextFlags_CallbackEdit and then alter Buf[0] and set BufDirty like you tried to above.

My apologies. This is only partially covered in the demo: `ImGuiInputTextFlags_CallbackCharFilter` is used but not for character replacement. You can read `data->EventChar` and change it if you want, the modified character will then be inserted. The only source of documentation for this is the flags description: ```cpp ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. ``` I will work on amending the demo to showcase replacement along with filtering. Otherwise you can use `ImGuiInputTextFlags_CallbackEdit` and then alter Buf[0] and set BufDirty like you tried to above.
ocornut commented 2023-05-25 12:27:41 +00:00 (Migrated from github.com)

I have improved the demo 7947f32

And confirmed that to do what you want, it is best to use ImGuiInputTextFlags_CallbackEdit:

ImGui::InputText("capitalize first", buf6, 32, ImGuiInputTextFlags_CallbackEdit, TextFilters::CapitalizeFirst);
static int CapitalizeFirst(ImGuiInputTextCallbackData* data)
{
    if (data->EventFlag & ImGuiInputTextFlags_CallbackEdit)
    {
        if (data->BufTextLen == 1 && (data->Buf[0] >= 'a' || data->Buf[0] <= 'z'))
        {
            data->Buf[0] = (char)toupper((char)data->Buf[0]);
            data->BufDirty = true;
        }
    }
    return 0;
}
I have improved the demo 7947f32 And confirmed that to do what you want, it is best to use `ImGuiInputTextFlags_CallbackEdit`: ```cpp ImGui::InputText("capitalize first", buf6, 32, ImGuiInputTextFlags_CallbackEdit, TextFilters::CapitalizeFirst); ``` ```cpp static int CapitalizeFirst(ImGuiInputTextCallbackData* data) { if (data->EventFlag & ImGuiInputTextFlags_CallbackEdit) { if (data->BufTextLen == 1 && (data->Buf[0] >= 'a' || data->Buf[0] <= 'z')) { data->Buf[0] = (char)toupper((char)data->Buf[0]); data->BufDirty = true; } } return 0; } ```
moebiussurfing commented 2023-05-25 15:17:55 +00:00 (Migrated from github.com)

Super. Thanks.

Super. Thanks.
moebiussurfing commented 2023-05-28 19:13:14 +00:00 (Migrated from github.com)

It worked fine.

EDIT: converted to lambda

auto callback = [](ImGuiInputTextCallbackData* data) -> int {
	if (data->EventFlag & ImGuiInputTextFlags_CallbackEdit) {
		if (data->BufTextLen == 1 && (data->Buf[0] >= 'a' || data->Buf[0] <= 'z')) {
			data->Buf[0] = (char)toupper((char)data->Buf[0]);
			data->BufDirty = true;
		}
	}
	return 0;
};

A bit hard to me to understand all that callback and pointer data stuff... I need to check deeper.

PS
How could I get when a key is pressed to trig a sound player?
Sorry. Callback is trigged when a pressed key, so it can be used to trig a sound here. Better than listen to key events and so maybe...
But how can I pass [this] or a boolean to flag or trig the sound from inside the function?

It worked fine. EDIT: converted to lambda ```.cpp auto callback = [](ImGuiInputTextCallbackData* data) -> int { if (data->EventFlag & ImGuiInputTextFlags_CallbackEdit) { if (data->BufTextLen == 1 && (data->Buf[0] >= 'a' || data->Buf[0] <= 'z')) { data->Buf[0] = (char)toupper((char)data->Buf[0]); data->BufDirty = true; } } return 0; }; ``` A bit hard to me to understand all that callback and pointer data stuff... I need to check deeper. PS ~~How could I get when a key is pressed to trig a sound player?~~ Sorry. Callback is trigged when a pressed key, so it can be used to trig a sound here. Better than listen to key events and so maybe... But how can I pass [this] or a boolean to flag or trig the sound from inside the function?
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#6457
No description provided.