Skip to content

Commit

Permalink
* Make selKey in candidate window colorful.
Browse files Browse the repository at this point in the history
* Insert bopomofo at current insertion point rather than the at the tail.
  • Loading branch information
PCMan committed Sep 13, 2013
1 parent 5df8845 commit 3972cca
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
14 changes: 10 additions & 4 deletions ChewingTextService/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ bool TextService::onKeyDown(Ime::KeyEvent& keyEvent, Ime::EditSession* session)
int len;
wchar_t* wbuf = ::utf8ToUtf16(buf, &len);
::chewing_free(buf);
compositionBuf += wbuf;
// put bopomofo symbols at insertion point
// FIXME: alternatively, should we show it in an additional floating window?
int pos = ::chewing_cursor_Current(chewingContext_);
compositionBuf.insert(pos, wbuf);
delete []wbuf;
}
}
Expand Down Expand Up @@ -421,14 +424,17 @@ void TextService::updateCandidates(Ime::EditSession* session) {
candidateWindow_->clear();

::chewing_cand_Enumerate(chewingContext_);
int n = ::chewing_cand_ChoicePerPage(chewingContext_);
for(; n > 0 && ::chewing_cand_hasNext(chewingContext_); --n) {
int* selKeys = ::chewing_get_selKey(chewingContext_); // keys used to select candidates
int n = ::chewing_cand_ChoicePerPage(chewingContext_); // candidate string shown per page
int i;
for(i = 0; i < n && ::chewing_cand_hasNext(chewingContext_); ++i) {
char* str = ::chewing_cand_String(chewingContext_);
wchar_t* wstr = utf8ToUtf16(str);
::chewing_free(str);
candidateWindow_->add(wstr);
candidateWindow_->add(wstr, (wchar_t)selKeys[i]);
delete []wstr;
}
::chewing_free(selKeys);
candidateWindow_->recalculateSize();
candidateWindow_->refresh();

Expand Down
68 changes: 47 additions & 21 deletions libIME/CandidateWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void CandidateWindow::updateFont() {

CandidateWindow::CandidateWindow(TextService* service, EditSession* session):
fontSize_(16),
selKeyWidth_(0),
isImmersive_(service->isImmersive()) {

if(isImmersive_) { // windows 8 app mode
Expand Down Expand Up @@ -121,46 +122,71 @@ void CandidateWindow::onPaint(WPARAM wp, LPARAM lp) {
RECT textRect = {margin_, margin_, rc.right - margin_, 0};
vector<wstring>::const_iterator it;
for(int i = 0, n = items_.size(); i < n; ++i) {
SIZE selKeySize;
int lineHeight = 0;
// the selection key string
wchar_t selKey[] = L"?. ";
selKey[0] = selKeys_[i];
::GetTextExtentPoint32W(hDC, selKey, 3, &selKeySize); // calculate size
textRect.left = margin_;
textRect.bottom = textRect.top + selKeySize.cy;
textRect.right = textRect.left + selKeyWidth_;
// FIXME: make the color of strings configurable.
COLORREF oldColor = ::SetTextColor(hDC, RGB(0, 0, 255));
// paint the selection key
::ExtTextOut(hDC, textRect.left, textRect.top, ETO_OPAQUE, &textRect, selKey, 3, NULL);
::SetTextColor(hDC, oldColor);

// the candidate string
SIZE candidateSize;
wstring& item = items_.at(i);
wchar_t numStr[10];
wstring line = _itow(i + 1, numStr, 10);
line += L". ";
line += item;

SIZE size;
::GetTextExtentPoint32W(hDC, line.c_str(), line.length(), &size);
textRect.bottom = textRect.top + size.cy;
::ExtTextOut(hDC, textRect.left, textRect.top, ETO_OPAQUE, &textRect, line.c_str(), line.length(), NULL);
textRect.top = textRect.bottom + spacing_;
::GetTextExtentPoint32W(hDC, item.c_str(), item.length(), &candidateSize);
textRect.left += selKeyWidth_;
textRect.bottom = textRect.top + candidateSize.cy;
// paint the candidate string
::ExtTextOut(hDC, textRect.left, textRect.top, ETO_OPAQUE, &textRect, item.c_str(), item.length(), NULL);

textRect.top += max(candidateSize.cy, selKeySize.cy);
textRect.top += spacing_;
}

SelectObject(hDC, oldFont);
EndPaint(hwnd_, &ps);
}

void CandidateWindow::recalculateSize() {
if(items_.empty()) {
resize(margin_ * 2, margin_ * 2);
}

HDC hDC = ::GetWindowDC(hwnd());
int height = 0;
int width = 0;
selKeyWidth_ = 0;

HGDIOBJ oldFont = ::SelectObject(hDC, font_);
vector<wstring>::const_iterator it;
for(int i = 0, n = items_.size(); i < n; ++i) {
SIZE selKeySize;
int lineHeight = 0;
// the selection key string
wchar_t selKey[] = L"?. ";
selKey[0] = selKeys_[i];
::GetTextExtentPoint32W(hDC, selKey, 3, &selKeySize);
if(selKeySize.cx > selKeyWidth_)
selKeyWidth_ = selKeySize.cx;

// the candidate string
SIZE candidateSize;
wstring& item = items_.at(i);
wchar_t numStr[10];
wstring line = _itow(i + 1, numStr, 10);
line += L". ";
line += item;

SIZE size;
::GetTextExtentPoint32W(hDC, line.c_str(), line.length(), &size);
height += size.cy;
::GetTextExtentPoint32W(hDC, item.c_str(), item.length(), &candidateSize);
height += max(candidateSize.cy, selKeySize.cy);
height += spacing_;

if(size.cx > width)
width = size.cx;
if(candidateSize.cx > width)
width = candidateSize.cx;
}
width += margin_ * 2;
width += (margin_ * 2 + selKeyWidth_);
height += margin_ * 2;
::SelectObject(hDC, oldFont);
::ReleaseDC(hwnd(), hDC);
Expand Down
9 changes: 7 additions & 2 deletions libIME/CandidateWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ class CandidateWindow : public ImeWindow {
return items_;
}

void setItems(const std::vector<std::wstring>& items) {
void setItems(const std::vector<std::wstring>& items, const std::vector<wchar_t>& sekKeys) {
items_ = items;
selKeys_ = selKeys_;
recalculateSize();
refresh();
}

void add(std::wstring item) {
void add(std::wstring item, wchar_t selKey) {
items_.push_back(item);
selKeys_.push_back(selKey);
}

void clear() {
items_.clear();
selKeys_.clear();
}

void recalculateSize();
Expand All @@ -49,7 +52,9 @@ class CandidateWindow : public ImeWindow {
int fontSize_;
int margin_;
int spacing_;
int selKeyWidth_;

std::vector<wchar_t> selKeys_;
std::vector<std::wstring> items_;
bool isImmersive_;
};
Expand Down

0 comments on commit 3972cca

Please sign in to comment.