| 4362 | } |
| 4363 | |
| 4364 | void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end) |
| 4365 | { |
| 4366 | // Accept null ranges |
| 4367 | if (new_text == new_text_end) |
| 4368 | return; |
| 4369 | |
| 4370 | ImGuiContext& g = *Ctx; |
| 4371 | ImGuiInputTextState* obj = &g.InputTextState; |
| 4372 | IM_ASSERT(obj->ID != 0 && g.ActiveId == obj->ID); |
| 4373 | const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0; |
| 4374 | const bool is_readonly = (Flags & ImGuiInputTextFlags_ReadOnly) != 0; |
| 4375 | int new_text_len = new_text_end ? (int)(new_text_end - new_text) : (int)ImStrlen(new_text); |
| 4376 | |
| 4377 | // We support partial insertion (with a mod in stb_textedit.h) |
| 4378 | const int avail = BufSize - 1 - BufTextLen; |
| 4379 | if (!is_resizable && new_text_len > avail) |
| 4380 | new_text_len = (int)(ImTextFindValidUtf8CodepointEnd(new_text, new_text + new_text_len, new_text + avail) - new_text); // Truncate to closest UTF-8 codepoint. Alternative: return 0 to cancel insertion. |
| 4381 | if (new_text_len == 0) |
| 4382 | return; |
| 4383 | |
| 4384 | // Grow internal buffer if needed |
| 4385 | if (new_text_len + BufTextLen + 1 > obj->TextA.Size && is_resizable && !is_readonly) |
| 4386 | { |
| 4387 | IM_ASSERT(Buf == obj->TextA.Data); |
| 4388 | int new_buf_size = BufTextLen + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1; |
| 4389 | obj->TextA.resize(new_buf_size + 1); |
| 4390 | obj->TextSrc = obj->TextA.Data; |
| 4391 | Buf = obj->TextA.Data; |
| 4392 | BufSize = obj->BufCapacity = new_buf_size; |
| 4393 | } |
| 4394 | |
| 4395 | if (BufTextLen != pos) |
| 4396 | memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(BufTextLen - pos)); |
| 4397 | memcpy(Buf + pos, new_text, (size_t)new_text_len * sizeof(char)); |
| 4398 | Buf[BufTextLen + new_text_len] = '\0'; |
| 4399 | |
| 4400 | BufDirty = true; |
| 4401 | BufTextLen += new_text_len; |
| 4402 | if (CursorPos >= pos) |
| 4403 | CursorPos += new_text_len; |
| 4404 | CursorPos = ImClamp(CursorPos, 0, BufTextLen); |
| 4405 | SelectionStart = SelectionEnd = CursorPos; |
| 4406 | } |
| 4407 | |
| 4408 | void ImGui::PushPasswordFont() |
| 4409 | { |
no test coverage detected