Skip to content

Commit

Permalink
Optionally output simplified Chinese characters on the fly.
Browse files Browse the repository at this point in the history
  • Loading branch information
PCMan committed Sep 25, 2013
1 parent 34d7b48 commit 2d46950
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 0 deletions.
Binary file modified ChewingPreferences/ChewingPreferences.rc
Binary file not shown.
Binary file modified ChewingPreferences/resource.h
Binary file not shown.
23 changes: 23 additions & 0 deletions ChewingTextService/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TextService::TextService(ImeModule* module):
showingCandidates_(false),
langMode_(-1),
shapeMode_(-1),
outputSimpChinese_(false),
lastKeyDownCode_(0),
messageWindow_(NULL),
messageTimerId_(0),
Expand Down Expand Up @@ -354,6 +355,11 @@ bool TextService::onKeyDown(Ime::KeyEvent& keyEvent, Ime::EditSession* session)
char* buf = ::chewing_commit_String(chewingContext_);
std::wstring wbuf = utf8ToUtf16(buf);
::chewing_free(buf);

// FIXME: this should be per-instance rather than a global setting.
if(outputSimpChinese_) // convert output to simplified Chinese
wbuf = tradToSimpChinese(wbuf);

// commit the text, replace currently selected text with our commit string
setCompositionString(session, wbuf.c_str(), wbuf.length());

Expand Down Expand Up @@ -481,6 +487,9 @@ bool TextService::onCommand(UINT id, CommandType type) {
onConfigure(HWND_DESKTOP);
}
break;
case ID_OUTPUT_SIMP_CHINESE: // toggle output traditional or simplified Chinese
toggleSimplifiedChinese();
break;
case ID_ABOUT: // show about dialog
if(!isImmersive()) { // only do this in desktop app mode
// show about dialog
Expand Down Expand Up @@ -584,6 +593,12 @@ void TextService::initChewingContext() {
if(cfg.defaultFullSpace)
::chewing_set_ShapeMode(chewingContext_, FULLSHAPE_MODE);
}

outputSimpChinese_ = config().outputSimpChinese;
// update popup menu to check/uncheck the simplified Chinese item
DWORD checkFlags = outputSimpChinese_ ? MF_CHECKED : MF_UNCHECKED;
::CheckMenuItem(popupMenu_, ID_OUTPUT_SIMP_CHINESE, MF_BYCOMMAND|checkFlags);

applyConfig();
}

Expand Down Expand Up @@ -661,6 +676,14 @@ void TextService::toggleShapeMode() {
}
}

// toggle output traditional or simplified Chinese
void TextService::toggleSimplifiedChinese() {
outputSimpChinese_ = !outputSimpChinese_;
// update popup menu to check/uncheck the simplified Chinese item
DWORD checkFlags = outputSimpChinese_ ? MF_CHECKED : MF_UNCHECKED;
::CheckMenuItem(popupMenu_, ID_OUTPUT_SIMP_CHINESE, MF_BYCOMMAND|checkFlags);
}

void TextService::updateCandidates(Ime::EditSession* session) {
assert(candidateWindow_);
candidateWindow_->clear();
Expand Down
2 changes: 2 additions & 0 deletions ChewingTextService/ChewingTextService.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class TextService: public Ime::TextService {

void toggleLanguageMode(); // toggle between English and Chinese
void toggleShapeMode(); // toggle between full shape and half shape
void toggleSimplifiedChinese(); // toggle output traditional or simplified Chinese

private:
ChewingContext* chewingContext_;
Expand All @@ -114,6 +115,7 @@ class TextService: public Ime::TextService {
Ime::LangBarButton* imeModeIcon_; // IME mode icon, a special language button (Windows 8 only)
HMENU popupMenu_;

bool outputSimpChinese_; // output simplified Chinese
int langMode_;
int shapeMode_;
UINT lastKeyDownCode_;
Expand Down
Binary file modified ChewingTextService/ChewingTextService.rc
Binary file not shown.
Binary file modified ChewingTextService/resource.h
Binary file not shown.
10 changes: 10 additions & 0 deletions libIME/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "Utils.h"
#include <Windows.h>
#include <Winnls.h>

std::wstring utf8ToUtf16(const char* text) {
std::wstring wtext;
Expand All @@ -41,3 +42,12 @@ std::string utf16ToUtf8(const wchar_t* wtext) {
}
return text;
}

std::wstring tradToSimpChinese(const std::wstring& trad) {
int len = ::LCMapStringW(0x0404, LCMAP_SIMPLIFIED_CHINESE, trad.c_str(), trad.length(), NULL, 0);
std::wstring simp;
simp.resize(len);
if(::LCMapStringW(0x0404, LCMAP_SIMPLIFIED_CHINESE, trad.c_str(), trad.length(), &simp[0], len))
return simp;
return trad;
}
3 changes: 3 additions & 0 deletions libIME/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ std::wstring utf8ToUtf16(const char* text);

std::string utf16ToUtf8(const wchar_t* wtext);

// convert traditional Chinese to simplified Chinese
std::wstring tradToSimpChinese(const std::wstring& trad);

#endif

0 comments on commit 2d46950

Please sign in to comment.