2
0

imgui_stdlib.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // TL;DR; this is using the ImGuiInputTextFlags_CallbackResize facility,
  4. // which also demonstrated in 'Dear ImGui Demo->Widgets->Text Input->Resize Callback'.
  5. // Changelog:
  6. // - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
  7. // Usage:
  8. // {
  9. // #include "misc/cpp/imgui_stdlib.h"
  10. // #include "misc/cpp/imgui_stdlib.cpp" // <-- If you want to include implementation without messing with your project/build.
  11. // [...]
  12. // std::string my_string;
  13. // ImGui::InputText("my string", &my_string);
  14. // }
  15. // See more C++ related extension (fmt, RAII, syntactic sugar) on Wiki:
  16. // https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness
  17. #include "imgui.h"
  18. #ifndef IMGUI_DISABLE
  19. #include "imgui_stdlib.h"
  20. // Clang warnings with -Weverything
  21. #if defined(__clang__)
  22. #pragma clang diagnostic push
  23. #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
  24. #endif
  25. struct InputTextCallback_UserData
  26. {
  27. std::string* Str;
  28. ImGuiInputTextCallback ChainCallback;
  29. void* ChainCallbackUserData;
  30. };
  31. static int InputTextCallback(ImGuiInputTextCallbackData* data)
  32. {
  33. InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData;
  34. if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
  35. {
  36. // Resize string callback
  37. // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want.
  38. std::string* str = user_data->Str;
  39. IM_ASSERT(data->Buf == str->c_str());
  40. str->resize(data->BufTextLen);
  41. data->Buf = (char*)str->c_str();
  42. }
  43. else if (user_data->ChainCallback)
  44. {
  45. // Forward to user callback, if any
  46. data->UserData = user_data->ChainCallbackUserData;
  47. return user_data->ChainCallback(data);
  48. }
  49. return 0;
  50. }
  51. bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
  52. {
  53. IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
  54. flags |= ImGuiInputTextFlags_CallbackResize;
  55. InputTextCallback_UserData cb_user_data;
  56. cb_user_data.Str = str;
  57. cb_user_data.ChainCallback = callback;
  58. cb_user_data.ChainCallbackUserData = user_data;
  59. return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
  60. }
  61. bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
  62. {
  63. IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
  64. flags |= ImGuiInputTextFlags_CallbackResize;
  65. InputTextCallback_UserData cb_user_data;
  66. cb_user_data.Str = str;
  67. cb_user_data.ChainCallback = callback;
  68. cb_user_data.ChainCallbackUserData = user_data;
  69. return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data);
  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. InputTextCallback_UserData cb_user_data;
  76. cb_user_data.Str = str;
  77. cb_user_data.ChainCallback = callback;
  78. cb_user_data.ChainCallbackUserData = user_data;
  79. return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
  80. }
  81. #if defined(__clang__)
  82. #pragma clang diagnostic pop
  83. #endif
  84. #endif // #ifndef IMGUI_DISABLE