|
| 1 | +// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) |
| 2 | +// This is also an example of how you may wrap your own similar types. |
| 3 | + |
| 4 | +// Changelog: |
| 5 | +// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string |
| 6 | + |
| 7 | +// See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki: |
| 8 | +// https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness |
| 9 | + |
| 10 | +#include "imgui.h" |
| 11 | +#include "imgui_stdlib.h" |
| 12 | + |
| 13 | +// Clang warnings with -Weverything |
| 14 | +#if defined(__clang__) |
| 15 | +#pragma clang diagnostic push |
| 16 | +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness |
| 17 | +#endif |
| 18 | + |
| 19 | +struct InputTextCallback_UserData |
| 20 | +{ |
| 21 | + std::string* Str; |
| 22 | + ImGuiInputTextCallback ChainCallback; |
| 23 | + void* ChainCallbackUserData; |
| 24 | +}; |
| 25 | + |
| 26 | +static int InputTextCallback(ImGuiInputTextCallbackData* data) |
| 27 | +{ |
| 28 | + InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData; |
| 29 | + if (data->EventFlag == ImGuiInputTextFlags_CallbackResize) |
| 30 | + { |
| 31 | + // Resize string callback |
| 32 | + // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want. |
| 33 | + std::string* str = user_data->Str; |
| 34 | + IM_ASSERT(data->Buf == str->c_str()); |
| 35 | + str->resize(data->BufTextLen); |
| 36 | + data->Buf = (char*)str->c_str(); |
| 37 | + } |
| 38 | + else if (user_data->ChainCallback) |
| 39 | + { |
| 40 | + // Forward to user callback, if any |
| 41 | + data->UserData = user_data->ChainCallbackUserData; |
| 42 | + return user_data->ChainCallback(data); |
| 43 | + } |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) |
| 48 | +{ |
| 49 | + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); |
| 50 | + flags |= ImGuiInputTextFlags_CallbackResize; |
| 51 | + |
| 52 | + InputTextCallback_UserData cb_user_data; |
| 53 | + cb_user_data.Str = str; |
| 54 | + cb_user_data.ChainCallback = callback; |
| 55 | + cb_user_data.ChainCallbackUserData = user_data; |
| 56 | + return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data); |
| 57 | +} |
| 58 | + |
| 59 | +bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) |
| 60 | +{ |
| 61 | + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); |
| 62 | + flags |= ImGuiInputTextFlags_CallbackResize; |
| 63 | + |
| 64 | + InputTextCallback_UserData cb_user_data; |
| 65 | + cb_user_data.Str = str; |
| 66 | + cb_user_data.ChainCallback = callback; |
| 67 | + cb_user_data.ChainCallbackUserData = user_data; |
| 68 | + return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data); |
| 69 | +} |
| 70 | + |
| 71 | +bool ImGui::InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) |
| 72 | +{ |
| 73 | + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); |
| 74 | + flags |= ImGuiInputTextFlags_CallbackResize; |
| 75 | + |
| 76 | + InputTextCallback_UserData cb_user_data; |
| 77 | + cb_user_data.Str = str; |
| 78 | + cb_user_data.ChainCallback = callback; |
| 79 | + cb_user_data.ChainCallbackUserData = user_data; |
| 80 | + return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data); |
| 81 | +} |
| 82 | + |
| 83 | +#if defined(__clang__) |
| 84 | +#pragma clang diagnostic pop |
| 85 | +#endif |
0 commit comments