-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Growing a vector from InputTextMultiline #1443
Labels
Comments
@crylessdomore : very interesting feature. Thank you very much for sharing your code ! 👍 |
@crylessdomore: Does this trick work when pasting code to it? P.S. Good approach using an optional char vector as last parameter. 👍 |
@Flix01 I had not thought of that, thank you for letting me know. I see what I can do in the next few hours :) --- EDIT --- It seems working now ## but crash with a large clipboard text // Paste
if (const char* clipboard = GetClipboardText())
{
// Filter pasted buffer
const int clipboard_len = (int)strlen(clipboard);
ImWchar* clipboard_filtered = (ImWchar*)ImGui::MemAlloc((clipboard_len+1) * sizeof(ImWchar));
int clipboard_filtered_len = 0;
for (const char* s = clipboard; *s; )
{
unsigned int c;
s += ImTextCharFromUtf8(&c, s, NULL);
if (c == 0)
break;
if (c >= 0x10000 || !InputTextFilterCharacter(&c, flags, callback, user_data))
continue;
clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c;
}
clipboard_filtered[clipboard_filtered_len] = 0;
if (clipboard_filtered_len > 0) // If everything was filtered, ignore the pasting operation
{
// Check whether buf is referencing to v
// and make sure buf is the same as v->Data
if (v && buf == v->Data)
{
// Increase buf_size for future use
buf_size += clipboard_filtered_len;
// Resize both vectors to have enough space to contain clipboard_filtered
edit_state.Text.resize(buf_size + 1);
v->resize(edit_state.Text.Size);
// Update BufSizeA
edit_state.BufSizeA = buf_size; // Needed for STB_TEXTEDIT_INSERTCHARS
}
stb_textedit_paste(&edit_state, &edit_state.StbState, clipboard_filtered, clipboard_filtered_len);
edit_state.CursorFollow = true;
}
ImGui::MemFree(clipboard_filtered);
} |
ocornut
added a commit
that referenced
this issue
Aug 22, 2018
ocornut
added a commit
that referenced
this issue
Aug 22, 2018
ocornut
added a commit
that referenced
this issue
Mar 12, 2019
Closed
ocornut
added a commit
that referenced
this issue
Feb 4, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did search online but could not find anything about it and so I wanted to propose my solution to the problem.
Wait a minute, what am I talking about (O.o")? Let's start from the beginning ...
... Hi! ... oops I think I have gone too far back XD ... excuse me ... Let me retry ... I was playing with InputTextMultiline when I noticed that there is not an official way to use a dynamic-allocated buffer (e.g. vector) for text typing and so you need to specify the size that may be a limit like in my case.
I apologize in advance if I am wrong was not my intention to disturb you.
Below my solution,
I wanted it compatible with the official version so I had not to modify anything other than InputTextMultiline and InputTextEx.
InputTextEx
I added a pointer to the vector, as optional parameter, that will be used only in case you have not a defined size while maintaining the function compatible for all other calls.
I could not know how many characters I would have to add without first filtering them so I push them into a temporary vector that will be used immediately afterwards to reallocate the two vectors. Next, I call OnKeyPressed to append the characters.
InputTextMultiline
I added a second definition of the InputTextMultiline function this time though using a vector. I check the vector size to make it safer to use by guaranteeing the validity of the pointer Data even if not initialized. I pass the vector address as last parameter.
ImVector
I had to make the ImVector class visible to the InputTextMultiline function.
I hope to have been helpful and thank all those who have contributed to realize this masterpiece.
I apologize for my english and I look forward to your opinion on this.
The text was updated successfully, but these errors were encountered: