rsvs3D  0.0.0
Codes for the c++ implementation of the 3D RSVS
imgui_stdlib.cpp
1 // Code from IMGUI for support of std::string
2 // dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.)
3 // This is also an example of how you may wrap your own similar types.
4 
5 // Compatibility:
6 // - std::string support is only guaranteed to work from C++11.
7 // If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture)
8 
9 // Changelog:
10 // - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
11 
12 #include "imgui_stdlib.h"
13 #include "imgui.h"
14 
16 {
17  std::string *Str;
18  ImGuiInputTextCallback ChainCallback;
19  void *ChainCallbackUserData;
20 };
21 
22 static int InputTextCallback(ImGuiInputTextCallbackData *data)
23 {
24  InputTextCallback_UserData *user_data = (InputTextCallback_UserData *)data->UserData;
25  if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
26  {
27  // Resize string callback
28  // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back
29  // to what we want.
30  std::string *str = user_data->Str;
31  IM_ASSERT(data->Buf == str->c_str());
32  str->resize(data->BufTextLen);
33  data->Buf = (char *)str->c_str();
34  }
35  else if (user_data->ChainCallback)
36  {
37  // Forward to user callback, if any
38  data->UserData = user_data->ChainCallbackUserData;
39  return user_data->ChainCallback(data);
40  }
41  return 0;
42 }
43 
44 bool ImGui::InputText(const char *label, std::string *str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback,
45  void *user_data)
46 {
47  IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
48  flags |= ImGuiInputTextFlags_CallbackResize;
49 
50  InputTextCallback_UserData cb_user_data;
51  cb_user_data.Str = str;
52  cb_user_data.ChainCallback = callback;
53  cb_user_data.ChainCallbackUserData = user_data;
54  return InputText(label, (char *)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
55 }
56 
57 bool ImGui::InputTextMultiline(const char *label, std::string *str, const ImVec2 &size, ImGuiInputTextFlags flags,
58  ImGuiInputTextCallback callback, void *user_data)
59 {
60  IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
61  flags |= ImGuiInputTextFlags_CallbackResize;
62 
63  InputTextCallback_UserData cb_user_data;
64  cb_user_data.Str = str;
65  cb_user_data.ChainCallback = callback;
66  cb_user_data.ChainCallbackUserData = user_data;
67  return InputTextMultiline(label, (char *)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback,
68  &cb_user_data);
69 }
70 
71 bool ImGui::InputTextWithHint(const char *label, const char *hint, std::string *str, ImGuiInputTextFlags flags,
72  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,
82  &cb_user_data);
83 }