2018-10-12 17:14:21 +08:00
|
|
|
// imgui_stdlib.cpp
|
2018-08-31 16:50:22 +08:00
|
|
|
// Wrappers for C++ standard library (STL) types (std::string, etc.)
|
2018-08-22 18:43:27 +08:00
|
|
|
// This is also an example of how you may wrap your own similar types.
|
|
|
|
|
2018-08-31 16:50:22 +08:00
|
|
|
// Compatibility:
|
|
|
|
// - std::string support is only guaranteed to work from C++11.
|
|
|
|
// If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture)
|
|
|
|
|
2018-10-12 17:14:21 +08:00
|
|
|
// Changelog:
|
|
|
|
// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
|
|
|
|
|
2018-08-22 18:43:27 +08:00
|
|
|
#include "imgui.h"
|
2018-10-12 17:14:21 +08:00
|
|
|
#include "imgui_stdlib.h"
|
2018-08-22 18:43:27 +08:00
|
|
|
|
|
|
|
struct InputTextCallback_UserData
|
|
|
|
{
|
|
|
|
std::string* Str;
|
|
|
|
ImGuiInputTextCallback ChainCallback;
|
|
|
|
void* ChainCallbackUserData;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int InputTextCallback(ImGuiInputTextCallbackData* data)
|
|
|
|
{
|
|
|
|
InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData;
|
|
|
|
if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
|
|
|
|
{
|
|
|
|
// Resize string callback
|
2018-10-12 16:14:03 +08:00
|
|
|
// If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want.
|
2018-08-22 18:43:27 +08:00
|
|
|
std::string* str = user_data->Str;
|
|
|
|
IM_ASSERT(data->Buf == str->c_str());
|
|
|
|
str->resize(data->BufTextLen);
|
|
|
|
data->Buf = (char*)str->c_str();
|
|
|
|
}
|
|
|
|
else if (user_data->ChainCallback)
|
|
|
|
{
|
|
|
|
// Forward to user callback, if any
|
|
|
|
data->UserData = user_data->ChainCallbackUserData;
|
|
|
|
return user_data->ChainCallback(data);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
|
|
|
|
{
|
2018-08-23 03:26:38 +08:00
|
|
|
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
|
2018-08-22 18:43:27 +08:00
|
|
|
flags |= ImGuiInputTextFlags_CallbackResize;
|
|
|
|
|
|
|
|
InputTextCallback_UserData cb_user_data;
|
|
|
|
cb_user_data.Str = str;
|
|
|
|
cb_user_data.ChainCallback = callback;
|
|
|
|
cb_user_data.ChainCallbackUserData = user_data;
|
|
|
|
return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
|
|
|
|
{
|
2018-08-23 03:26:38 +08:00
|
|
|
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
|
2018-08-22 18:43:27 +08:00
|
|
|
flags |= ImGuiInputTextFlags_CallbackResize;
|
|
|
|
|
|
|
|
InputTextCallback_UserData cb_user_data;
|
|
|
|
cb_user_data.Str = str;
|
|
|
|
cb_user_data.ChainCallback = callback;
|
|
|
|
cb_user_data.ChainCallbackUserData = user_data;
|
|
|
|
return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data);
|
|
|
|
}
|