Skip to content

Commit d513f36

Browse files
committed
Fixed non-english input
1 parent 538f2a9 commit d513f36

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

src/xrEngine/edit_actions.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "line_edit_control.h"
1111
#include "xr_input.h"
1212
#include <locale.h>
13+
#include <codecvt>
1314

1415
namespace text_editor
1516
{
@@ -55,39 +56,48 @@ void type_pair::init(int dik, char c, char c_shift, bool b_translate)
5556
m_char_shift = c_shift;
5657
}
5758

59+
xr_string utf8_to_string(const char* utf8str, const std::locale& loc)
60+
{
61+
// UTF-8 to wstring
62+
std::wstring_convert<std::codecvt_utf8<wchar_t>> wconv;
63+
std::wstring wstr = wconv.from_bytes(utf8str);
64+
// wstring to string
65+
std::vector<char> buf(wstr.size());
66+
std::use_facet<std::ctype<wchar_t>>(loc).narrow(wstr.data(), wstr.data() + wstr.size(), '?', buf.data());
67+
return xr_string(buf.data(), buf.size());
68+
}
69+
5870
void type_pair::on_key_press(line_edit_control* const control)
5971
{
6072
char c = 0;
6173
if (m_translate)
6274
{
6375
c = m_char;
6476
char c_shift = m_char_shift;
65-
string128 buff;
66-
buff[0] = 0;
67-
68-
/*
69-
//setlocale( LC_ALL, "" ); // User-default
70-
71-
// The following 3 lines looks useless
72-
73-
LPSTR loc;
74-
STRCONCAT ( loc, ".", xr_itoa( GetACP(), code_page, 10 ) );
75-
setlocale ( LC_ALL, loc );*/
7677

77-
static _locale_t current_locale = _create_locale(LC_ALL, "");
78-
79-
if (pInput->get_dik_name(m_dik, buff, sizeof(buff)))
78+
SDL_Event event;
79+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_TEXTEDITING, SDL_TEXTINPUT))
8080
{
81-
if (_isalpha_l(buff[0], current_locale) || buff[0] == char(-1)) // "я" = -1
81+
switch (event.type)
82+
{
83+
case SDL_TEXTINPUT:
8284
{
83-
_strlwr_l(buff, current_locale);
84-
c = buff[0];
85-
_strupr_l(buff, current_locale);
86-
c_shift = buff[0];
85+
const std::locale locale("");
86+
auto str = utf8_to_string(event.text.text, locale);
87+
88+
if (std::isalpha(str[0], locale) || str[0] == char(-1)) // "я" = -1
89+
{
90+
c = std::tolower(str[0], locale);
91+
c_shift = std::toupper(str[0], locale);
92+
}
93+
break;
8794
}
88-
}
8995

90-
// setlocale( LC_ALL, "C" ); // restore to ANSI
96+
case SDL_TEXTEDITING:
97+
// XXX: use this?
98+
break;
99+
}
100+
}
91101

92102
if (control->get_key_state(ks_Shift) != control->get_key_state(ks_CapsLock))
93103
{

src/xrEngine/line_edit_control.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,6 @@ line_edit_control::~line_edit_control()
9898
delete_data(actions);
9999
}
100100

101-
static inline bool get_caps_lock_state()
102-
{
103-
#if 0
104-
static bool first_time = true;
105-
static bool is_windows_vista_or_later = false;
106-
if (first_time)
107-
{
108-
first_time = false;
109-
OSVERSIONINFO version_info;
110-
ZeroMemory(&version_info, sizeof(version_info));
111-
version_info.dwOSVersionInfoSize = sizeof(version_info);
112-
GetVersionEx(&version_info);
113-
is_windows_vista_or_later = version_info.dwMajorVersion >= 6;
114-
}
115-
116-
if (is_windows_vista_or_later)
117-
return !!(GetKeyState(VK_CAPITAL) & 1);
118-
else
119-
#else // #if 0
120-
return false;
121-
#endif // #if 0
122-
}
123-
124101
void line_edit_control::update_key_states()
125102
{
126103
m_key_state.zero();
@@ -740,7 +717,13 @@ void line_edit_control::compute_positions()
740717
}
741718

742719
void line_edit_control::clamp_cur_pos() { clamp(m_cur_pos, 0, (int)xr_strlen(m_edit_str)); }
743-
void line_edit_control::SwitchKL() { ActivateKeyboardLayout((HKL)HKL_NEXT, 0); }
720+
void line_edit_control::SwitchKL()
721+
{
722+
#ifdef WINDOWS
723+
if (pInput->get_exclusive_mode())
724+
ActivateKeyboardLayout((HKL)HKL_NEXT, 0);
725+
#endif
726+
}
744727
// -------------------------------------------------------------------------------------------------
745728

746729
void remove_spaces(pstr str)

src/xrEngine/line_editor.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010

1111
namespace text_editor
1212
{
13-
line_editor::line_editor(u32 str_buffer_size) : m_control(str_buffer_size) {}
14-
line_editor::~line_editor() {}
13+
line_editor::line_editor(u32 str_buffer_size) : m_control(str_buffer_size)
14+
{
15+
SDL_StartTextInput();
16+
}
17+
line_editor::~line_editor()
18+
{
19+
SDL_StopTextInput();
20+
}
21+
1522
void line_editor::on_frame() { m_control.on_frame(); }
1623
void line_editor::IR_OnKeyboardPress(int dik) { m_control.on_key_press(dik); }
1724
void line_editor::IR_OnKeyboardHold(int dik) { m_control.on_key_hold(dik); }

src/xrEngine/xr_input.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ CInput::CInput(bool exclusive, int deviceForInit)
5858

5959
xrDebug::SetDialogHandler(on_error_dialog);
6060

61+
SDL_StopTextInput(); // sanity
62+
6163
#ifdef ENGINE_BUILD
6264
Device.seqAppActivate.Add(this);
6365
Device.seqAppDeactivate.Add(this, REG_PRIORITY_HIGH);
@@ -171,7 +173,7 @@ void CInput::KeyUpdate()
171173
SDL_PumpEvents();
172174

173175
SDL_Event event;
174-
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYMAPCHANGED))
176+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYUP))
175177
{
176178
switch (event.type)
177179
{

0 commit comments

Comments
 (0)