Edit InputTextMultiline while active (for Auto Complete e.g.) #968

Closed
opened 2017-01-06 06:58:51 +00:00 by kungfooman · 2 comments
kungfooman commented 2017-01-06 06:58:51 +00:00 (Migrated from github.com)

Hi, is there a way to edit a InputTextMultiline while being active? I tried all kinds of stuff, but I don't understand a lot from all the Unicode/Wchar/ASCII conversions.

I tried to copy the functions from the InputTextMultiline itself, but most times it either did nothing, crashes, or wrong results:

E.g. like

char *buf = str;
int buf_size = 4096;
ImGuiContext *g = ImGui::GetCurrentContext();
auto edit_state = g->InputTextState;
// Start edition
// Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar)
// From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode)
const int prev_len_w = edit_state.CurLenW;
edit_state.Text.resize(buf_size+1);        // wchar count <= UTF-8 count. we use +1 to make sure that .Data isn't NULL so it doesn't crash.
edit_state.InitialText.resize(buf_size+1); // UTF-8. we use +1 to make sure that .Data isn't NULL so it doesn't crash.
ImStrncpy(edit_state.InitialText.Data, buf, edit_state.InitialText.Size);
const char* buf_end = NULL;
//edit_state.CurLenW = ImTextStrFromUtf8(edit_state.Text.Data, edit_state.Text.Size, buf, NULL, &buf_end);
//edit_state.CurLenA = (int)(buf_end - buf); // We can't get the result from ImFormatString() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8.
//edit_state.CursorAnimReset();	

I use it to edit JavaScript ingame and it would be nice to use Ctrl+Space to auto complete :)

Hi, is there a way to edit a InputTextMultiline while being active? I tried all kinds of stuff, but I don't understand a lot from all the Unicode/Wchar/ASCII conversions. I tried to copy the functions from the InputTextMultiline itself, but most times it either did nothing, crashes, or wrong results: E.g. like char *buf = str; int buf_size = 4096; ImGuiContext *g = ImGui::GetCurrentContext(); auto edit_state = g->InputTextState; // Start edition // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar) // From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode) const int prev_len_w = edit_state.CurLenW; edit_state.Text.resize(buf_size+1); // wchar count <= UTF-8 count. we use +1 to make sure that .Data isn't NULL so it doesn't crash. edit_state.InitialText.resize(buf_size+1); // UTF-8. we use +1 to make sure that .Data isn't NULL so it doesn't crash. ImStrncpy(edit_state.InitialText.Data, buf, edit_state.InitialText.Size); const char* buf_end = NULL; //edit_state.CurLenW = ImTextStrFromUtf8(edit_state.Text.Data, edit_state.Text.Size, buf, NULL, &buf_end); //edit_state.CurLenA = (int)(buf_end - buf); // We can't get the result from ImFormatString() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8. //edit_state.CursorAnimReset(); I use it to edit JavaScript ingame and it would be nice to use Ctrl+Space to auto complete :)
ocornut commented 2017-01-06 07:58:02 +00:00 (Migrated from github.com)

Sorry this code is rather messy/complex. The eventual goal is to get rid of the conversions alltogether.

To answer your question, currently the only legal way to modify the active buffer is to use the custom callback to InputTextMultiline(). Check out the docs around that.

Sorry this code is rather messy/complex. The eventual goal is to get rid of the conversions alltogether. To answer your question, currently the only legal way to modify the active buffer is to use the custom callback to InputTextMultiline(). Check out the docs around that.
kungfooman commented 2017-01-06 19:32:03 +00:00 (Migrated from github.com)

Thanks, that works like a charm :)

int repl_callback(ImGuiTextEditCallbackData *data) {
	if (autocomplete) {
		char returnbuffer[256] = {0};
		js_call("get_auto_completion", "siiii", replbuffer, data->CursorPos, data->SelectionStart, data->SelectionEnd, returnbuffer);
		//log("got returnbuffer: %s\n", returnbuffer);
		data->InsertChars(data->CursorPos, returnbuffer);
		autocomplete = 0;
	}
	//log("callback called\n");
	return 0;
}


ImGui::InputTextMultiline("", replbuffer, sizeof replbuffer, ImGui::GetWindowSize() + ImVec2(-15, -35), ImGuiInputTextFlags_CallbackAlways, repl_callback);
Thanks, that works like a charm :) int repl_callback(ImGuiTextEditCallbackData *data) { if (autocomplete) { char returnbuffer[256] = {0}; js_call("get_auto_completion", "siiii", replbuffer, data->CursorPos, data->SelectionStart, data->SelectionEnd, returnbuffer); //log("got returnbuffer: %s\n", returnbuffer); data->InsertChars(data->CursorPos, returnbuffer); autocomplete = 0; } //log("callback called\n"); return 0; } ImGui::InputTextMultiline("", replbuffer, sizeof replbuffer, ImGui::GetWindowSize() + ImVec2(-15, -35), ImGuiInputTextFlags_CallbackAlways, repl_callback);
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#968
No description provided.