Skip to content

Commit

Permalink
Fixed non-english input
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jul 26, 2018
1 parent 538f2a9 commit d513f36
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
52 changes: 31 additions & 21 deletions src/xrEngine/edit_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "line_edit_control.h"
#include "xr_input.h"
#include <locale.h>
#include <codecvt>

namespace text_editor
{
Expand Down Expand Up @@ -55,39 +56,48 @@ void type_pair::init(int dik, char c, char c_shift, bool b_translate)
m_char_shift = c_shift;
}

xr_string utf8_to_string(const char* utf8str, const std::locale& loc)
{
// UTF-8 to wstring
std::wstring_convert<std::codecvt_utf8<wchar_t>> wconv;
std::wstring wstr = wconv.from_bytes(utf8str);
// wstring to string
std::vector<char> buf(wstr.size());
std::use_facet<std::ctype<wchar_t>>(loc).narrow(wstr.data(), wstr.data() + wstr.size(), '?', buf.data());
return xr_string(buf.data(), buf.size());
}

void type_pair::on_key_press(line_edit_control* const control)
{
char c = 0;
if (m_translate)
{
c = m_char;
char c_shift = m_char_shift;
string128 buff;
buff[0] = 0;

/*
//setlocale( LC_ALL, "" ); // User-default
// The following 3 lines looks useless
LPSTR loc;
STRCONCAT ( loc, ".", xr_itoa( GetACP(), code_page, 10 ) );
setlocale ( LC_ALL, loc );*/

static _locale_t current_locale = _create_locale(LC_ALL, "");

if (pInput->get_dik_name(m_dik, buff, sizeof(buff)))
SDL_Event event;
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_TEXTEDITING, SDL_TEXTINPUT))
{
if (_isalpha_l(buff[0], current_locale) || buff[0] == char(-1)) // "я" = -1
switch (event.type)
{
case SDL_TEXTINPUT:
{
_strlwr_l(buff, current_locale);
c = buff[0];
_strupr_l(buff, current_locale);
c_shift = buff[0];
const std::locale locale("");
auto str = utf8_to_string(event.text.text, locale);

if (std::isalpha(str[0], locale) || str[0] == char(-1)) // "я" = -1
{
c = std::tolower(str[0], locale);
c_shift = std::toupper(str[0], locale);
}
break;
}
}

// setlocale( LC_ALL, "C" ); // restore to ANSI
case SDL_TEXTEDITING:
// XXX: use this?
break;
}
}

if (control->get_key_state(ks_Shift) != control->get_key_state(ks_CapsLock))
{
Expand Down
31 changes: 7 additions & 24 deletions src/xrEngine/line_edit_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,6 @@ line_edit_control::~line_edit_control()
delete_data(actions);
}

static inline bool get_caps_lock_state()
{
#if 0
static bool first_time = true;
static bool is_windows_vista_or_later = false;
if (first_time)
{
first_time = false;
OSVERSIONINFO version_info;
ZeroMemory(&version_info, sizeof(version_info));
version_info.dwOSVersionInfoSize = sizeof(version_info);
GetVersionEx(&version_info);
is_windows_vista_or_later = version_info.dwMajorVersion >= 6;
}

if (is_windows_vista_or_later)
return !!(GetKeyState(VK_CAPITAL) & 1);
else
#else // #if 0
return false;
#endif // #if 0
}

void line_edit_control::update_key_states()
{
m_key_state.zero();
Expand Down Expand Up @@ -740,7 +717,13 @@ void line_edit_control::compute_positions()
}

void line_edit_control::clamp_cur_pos() { clamp(m_cur_pos, 0, (int)xr_strlen(m_edit_str)); }
void line_edit_control::SwitchKL() { ActivateKeyboardLayout((HKL)HKL_NEXT, 0); }
void line_edit_control::SwitchKL()
{
#ifdef WINDOWS
if (pInput->get_exclusive_mode())
ActivateKeyboardLayout((HKL)HKL_NEXT, 0);
#endif
}
// -------------------------------------------------------------------------------------------------

void remove_spaces(pstr str)
Expand Down
11 changes: 9 additions & 2 deletions src/xrEngine/line_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@

namespace text_editor
{
line_editor::line_editor(u32 str_buffer_size) : m_control(str_buffer_size) {}
line_editor::~line_editor() {}
line_editor::line_editor(u32 str_buffer_size) : m_control(str_buffer_size)
{
SDL_StartTextInput();
}
line_editor::~line_editor()
{
SDL_StopTextInput();
}

void line_editor::on_frame() { m_control.on_frame(); }
void line_editor::IR_OnKeyboardPress(int dik) { m_control.on_key_press(dik); }
void line_editor::IR_OnKeyboardHold(int dik) { m_control.on_key_hold(dik); }
Expand Down
4 changes: 3 additions & 1 deletion src/xrEngine/xr_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ CInput::CInput(bool exclusive, int deviceForInit)

xrDebug::SetDialogHandler(on_error_dialog);

SDL_StopTextInput(); // sanity

#ifdef ENGINE_BUILD
Device.seqAppActivate.Add(this);
Device.seqAppDeactivate.Add(this, REG_PRIORITY_HIGH);
Expand Down Expand Up @@ -171,7 +173,7 @@ void CInput::KeyUpdate()
SDL_PumpEvents();

SDL_Event event;
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYMAPCHANGED))
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_KEYDOWN, SDL_KEYUP))
{
switch (event.type)
{
Expand Down

0 comments on commit d513f36

Please sign in to comment.