openMSX
imgui_stdlib.cc
Go to the documentation of this file.
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#ifndef IMGUI_DISABLE
12#include "imgui_stdlib.h"
13
14// Clang warnings with -Weverything
15#if defined(__clang__)
16#pragma clang diagnostic push
17#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
18#endif
19
21{
22 std::string* Str;
23 ImGuiInputTextCallback ChainCallback;
25};
26
27static int InputTextCallback(ImGuiInputTextCallbackData* data)
28{
29 InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData;
30 if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
31 {
32 // Resize string callback
33 // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want.
34 std::string* str = user_data->Str;
35 IM_ASSERT(data->Buf == str->c_str());
36 str->resize(data->BufTextLen);
37 data->Buf = (char*)str->c_str();
38 }
39 else if (user_data->ChainCallback)
40 {
41 // Forward to user callback, if any
42 data->UserData = user_data->ChainCallbackUserData;
43 return user_data->ChainCallback(data);
44 }
45 return 0;
46}
47
48bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
49{
50 IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
51 flags |= ImGuiInputTextFlags_CallbackResize;
52
53 InputTextCallback_UserData cb_user_data;
54 cb_user_data.Str = str;
55 cb_user_data.ChainCallback = callback;
56 cb_user_data.ChainCallbackUserData = user_data;
57 return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
58}
59
60bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
61{
62 IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
63 flags |= ImGuiInputTextFlags_CallbackResize;
64
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
72bool ImGui::InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
73{
74 IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
75 flags |= ImGuiInputTextFlags_CallbackResize;
76
77 InputTextCallback_UserData cb_user_data;
78 cb_user_data.Str = str;
79 cb_user_data.ChainCallback = callback;
80 cb_user_data.ChainCallbackUserData = user_data;
81 return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
82}
83
84#if defined(__clang__)
85#pragma clang diagnostic pop
86#endif
87
88#endif // #ifndef IMGUI_DISABLE
bool InputText(Setting &setting)
ImGuiInputTextCallback ChainCallback