2018-08-22 13:43:27 +03:00
|
|
|
// imgui_stl.cpp
|
2018-08-31 11:50:22 +03:00
|
|
|
// Wrappers for C++ standard library (STL) types (std::string, etc.)
|
2018-08-22 13:43:27 +03:00
|
|
|
// This is also an example of how you may wrap your own similar types.
|
|
|
|
|
2018-08-31 11:50:22 +03: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-08-22 13:43:27 +03:00
|
|
|
#include "imgui.h"
|
|
|
|
#include "imgui_stl.h"
|
|
|
|
|
|
|
|
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 11:14:03 +03: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 13:43:27 +03: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-22 22:26:38 +03:00
|
|
|
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
|
2018-08-22 13:43:27 +03: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-22 22:26:38 +03:00
|
|
|
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
|
2018-08-22 13:43:27 +03: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);
|
|
|
|
}
|