Antares Simulator
Power System Simulator
wx-wrapper.hxx
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #ifndef __ANTARES_TOOLBOX_WX_WIDGETS_HXX__
22 #define __ANTARES_TOOLBOX_WX_WIDGETS_HXX__
23 
24 inline wxString wxStringFromUTF8(const std::string& s)
25 {
26 #if wxUSE_UNICODE
27  return wxString::FromUTF8(s.c_str(), s.size());
28 #else
29  return wxString(s.c_str(), s.size());
30 #endif
31 }
32 
33 inline wxString wxStringFromUTF8(const char* s)
34 {
35 #if wxUSE_UNICODE
36  return wxString::FromUTF8(s);
37 #else
38  return wxString(s);
39 #endif
40 }
41 
42 inline wxString wxStringFromUTF8(const Yuni::String& s)
43 {
44 #if wxUSE_UNICODE
45  return wxString::FromUTF8(s.c_str(), s.size());
46 #else
47  return wxString(s.c_str(), s.size());
48 #endif
49 }
50 
51 inline wxString wxStringFromUTF8(const char* const s, uint length)
52 {
53 #if wxUSE_UNICODE
54  return wxString::FromUTF8(s, length);
55 #else
56  return wxString(s, length);
57 #endif
58 }
59 
60 template<uint ChunkT, bool FixedT>
61 inline wxString wxStringFromUTF8(const Yuni::CString<ChunkT, FixedT>& s)
62 {
63 #if wxUSE_UNICODE
64  return wxString::FromUTF8(s.c_str(), s.size());
65 #else
66  return wxString(s.c_str(), s.size());
67 #endif
68 }
69 
70 template<class StringT>
71 inline void wxStringToString(const wxString& s, StringT& out)
72 {
73 #if wxUSE_UNICODE
74  const wxCharBuffer buffer(s.mb_str(wxConvUTF8));
75  out.assign((const char*)buffer, (uint)strlen((const char*)buffer));
76 #else
77  out = (const char*)s.mb_str();
78 #endif
79 }
80 
81 template<int PrecisionT>
82 wxString DoubleToWxStringS(double f)
83 {
84  Yuni::CString<128, false> tmp;
85  switch (PrecisionT)
86  {
87  case 0:
88  tmp.format("%.0f", f);
89  break;
90  case 1:
91  tmp.format("%.1f", f);
92  break;
93  case 2:
94  tmp.format("%.2f", f);
95  break;
96  case 3:
97  tmp.format("%.3f", f);
98  break;
99  case 4:
100  tmp.format("%.4f", f);
101  break;
102  case 5:
103  tmp.format("%.5f", f);
104  break;
105  case 6:
106  tmp.format("%.6f", f);
107  break;
108  case 7:
109  tmp.format("%.7f", f);
110  break;
111  case 8:
112  tmp.format("%.8f", f);
113  break;
114  case 9:
115  tmp.format("%.9f", f);
116  break;
117  default:
118  tmp.format("%.f", f);
119  break;
120  }
121  if (tmp.contains('.'))
122  {
123  tmp.trimRight('0');
124  if (tmp.last() == '.')
125  tmp.removeLast();
126  }
127  return wxStringFromUTF8(tmp);
128 }
129 
130 template<class StringT1, class StringT2>
131 void AppendWithQuotes(StringT1& out, const StringT2& text)
132 {
133  if (!text.empty())
134  {
135  if (text.contains('"'))
136  {
137  Yuni::String copy = text;
138  copy.replace("\"", "\\\"");
139  out << '"' << copy << '"';
140  }
141  else
142  out << '"' << text << '"';
143  }
144  else
145  out << "\"\"";
146 }
147 
148 #endif // __ANTARES_TOOLBOX_WX_WIDGETS_HXX__