Skip to content

Commit

Permalink
refactor: remove more unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Dec 25, 2024
1 parent 882096b commit 29d4159
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 181 deletions.
147 changes: 8 additions & 139 deletions chewing_tip/TextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ TextService::TextService():
activateFlags_(0),
isKeyboardOpened_(false),
threadMgrEventSinkCookie_(TF_INVALID_COOKIE),
textEditSinkCookie_(TF_INVALID_COOKIE),
compositionSinkCookie_(TF_INVALID_COOKIE),
keyboardOpenEventSinkCookie_(TF_INVALID_COOKIE),
langBarSinkCookie_(TF_INVALID_COOKIE),
activateLanguageProfileNotifySinkCookie_(TF_INVALID_COOKIE),
composition_(NULL),
input_atom_(TF_INVALID_GUIDATOM),
refCount_(1) {
Expand Down Expand Up @@ -86,41 +81,16 @@ TextService::~TextService(void) {
}
}

if(langBarMgr_) {
langBarMgr_->UnadviseEventSink(langBarSinkCookie_);
}
langBarMgr_ = NULL;
}

// public methods

ITfThreadMgr* TextService::threadMgr() const {
return threadMgr_.get();
}

TfClientId TextService::clientId() const {
return clientId_;
}


// language bar
DWORD TextService::langBarStatus() const {
if(langBarMgr_) {
DWORD status;
if(langBarMgr_->GetShowFloatingStatus(&status) == S_OK) {
return status;
}
}
return 0;
}

void TextService::addButton(ITfLangBarItemButton* button) {
if(button) {
winrt::com_ptr<ITfLangBarItemButton> btn;
btn.copy_from(button);

langBarButtons_.emplace_back(btn);
if(isActivated()) {
if (threadMgr_) {
winrt::com_ptr<ITfLangBarItemMgr> langBarItemMgr;
if(threadMgr_->QueryInterface(IID_ITfLangBarItemMgr, langBarItemMgr.put_void()) == S_OK) {
langBarItemMgr->AddItem(button);
Expand All @@ -136,7 +106,7 @@ void TextService::addPreservedKey(UINT keyCode, UINT modifiers, const GUID& guid
preservedKey.uVKey = keyCode;
preservedKey.uModifiers = modifiers;
preservedKeys_.push_back(preservedKey);
if(threadMgr_) { // our text service is activated
if (threadMgr_) { // our text service is activated
ITfKeystrokeMgr *keystrokeMgr;
if (threadMgr_->QueryInterface(IID_ITfKeystrokeMgr, (void **)&keystrokeMgr) == S_OK) {
keystrokeMgr->PreserveKey(clientId_, guid, &preservedKey, NULL, 0);
Expand All @@ -145,25 +115,6 @@ void TextService::addPreservedKey(UINT keyCode, UINT modifiers, const GUID& guid
}
}

void TextService::removePreservedKey(const GUID& guid) {
vector<PreservedKey>::iterator it;
for(it = preservedKeys_.begin(); it != preservedKeys_.end(); ++it) {
PreservedKey& preservedKey = *it;
if(::IsEqualIID(preservedKey.guid, guid)) {
if(threadMgr_) { // our text service is activated
ITfKeystrokeMgr *keystrokeMgr;
if (threadMgr_->QueryInterface(IID_ITfKeystrokeMgr, (void **)&keystrokeMgr) == S_OK) {
keystrokeMgr->UnpreserveKey(preservedKey.guid, &preservedKey);
keystrokeMgr->Release();
}
}
preservedKeys_.erase(it);
break;
}
}
}


// text composition

bool TextService::isComposing() {
Expand All @@ -187,35 +138,6 @@ void TextService::setKeyboardOpen(bool open) {
}
}


// check if current insertion point is in the range of composition.
// if not in range, insertion is now allowed
bool TextService::isInsertionAllowed(EditSession* session) {
TfEditCookie cookie = session->editCookie();
TF_SELECTION selection;
ULONG selectionNum;
if(isComposing()) {
if(session->context()->GetSelection(cookie, TF_DEFAULT_SELECTION, 1, &selection, &selectionNum) == S_OK) {
ITfRange* compositionRange;
if(composition_->GetRange(&compositionRange) == S_OK) {
bool allowed = false;
// check if current selection is covered by composition range
LONG compareResult1;
LONG compareResult2;
if(selection.range->CompareStart(cookie, compositionRange, TF_ANCHOR_START, &compareResult1) == S_OK
&& selection.range->CompareStart(cookie, compositionRange, TF_ANCHOR_END, &compareResult2) == S_OK) {
if(compareResult1 == -1 && compareResult2 == +1)
allowed = true;
}
compositionRange->Release();
}
if(selection.range)
selection.range->Release();
}
}
return false;
}

void TextService::startComposition(ITfContext* context) {
assert(context);
HRESULT sessionResult;
Expand All @@ -232,27 +154,6 @@ void TextService::endComposition(ITfContext* context) {
session->Release();
}

std::wstring TextService::compositionString(EditSession* session) {
if (composition_) {
winrt::com_ptr<ITfRange> compositionRange;
if (composition_->GetRange(compositionRange.put()) == S_OK) {
TfEditCookie editCookie = session->editCookie();
// FIXME: the TSF API is really stupid here and it provides no way to know the size of the range.
// we cannot even get the actual position of start and end to calculate by ourselves.
// So, just use a huge buffer and assume that it's enough. :-(
// this should be quite enough for most of the IME on the earth as most composition strings
// only contain dozens of characters.
wchar_t buf[4096];
ULONG len = 0;
if (compositionRange->GetText(editCookie, 0, buf, 4096, &len) == S_OK) {
buf[len] = '\0';
return std::wstring(buf);
}
}
}
return std::wstring();
}

void TextService::setCompositionString(EditSession* session, const wchar_t* str, int len) {
ITfContext* context = session->context();
if (context) {
Expand Down Expand Up @@ -381,18 +282,6 @@ void TextService::addCompartmentMonitor(const GUID key) {
compartmentMonitors_.push_back(monitor);
}

void TextService::removeCompartmentMonitor(const GUID key) {
vector<CompartmentMonitor>::iterator it;
it = find(compartmentMonitors_.begin(), compartmentMonitors_.end(), key);
if(it != compartmentMonitors_.end()) {
if(threadMgr_) {
winrt::com_ptr<ITfSource> source = threadCompartment(key).as<ITfSource>();
source->UnadviseSink(it->cookie);
}
compartmentMonitors_.erase(it);
}
}

// virtual
void TextService::onCompartmentChanged(const GUID& key) {
// keyboard status changed, this is threadMgr specific
Expand Down Expand Up @@ -556,8 +445,6 @@ STDMETHODIMP TextService::Deactivate() {
}
}
if(langBarMgr_) {
langBarMgr_->UnadviseEventSink(langBarSinkCookie_);
langBarSinkCookie_ = TF_INVALID_COOKIE;
langBarMgr_ = NULL;
}

Expand All @@ -567,9 +454,7 @@ STDMETHODIMP TextService::Deactivate() {
winrt::com_ptr<ITfSource> source = threadMgr_.as<ITfSource>();
if(source) {
source->UnadviseSink(threadMgrEventSinkCookie_);
source->UnadviseSink(activateLanguageProfileNotifySinkCookie_);
threadMgrEventSinkCookie_ = TF_INVALID_COOKIE;
activateLanguageProfileNotifySinkCookie_ = TF_INVALID_COOKIE;
}

// ITfTextEditSink,
Expand All @@ -592,12 +477,12 @@ STDMETHODIMP TextService::Deactivate() {

// ITfCompartmentEventSink
// thread specific compartment
winrt::com_ptr<ITfCompartment> compartment = threadCompartment(GUID_COMPARTMENT_KEYBOARD_OPENCLOSE);
if(compartment) {
winrt::com_ptr<ITfSource> compartmentSource = compartment.as<ITfSource>();
if(compartmentSource)
compartmentSource->UnadviseSink(keyboardOpenEventSinkCookie_);
keyboardOpenEventSinkCookie_ = TF_INVALID_COOKIE;
if(!compartmentMonitors_.empty()) {
vector<CompartmentMonitor>::iterator it;
for(it = compartmentMonitors_.begin(); it != compartmentMonitors_.end(); ++it) {
winrt::com_ptr<ITfSource> source = threadCompartment(it->guid).as<ITfSource>();
source->UnadviseSink(it->cookie);
}
}

threadMgr_ = NULL;
Expand Down Expand Up @@ -894,22 +779,6 @@ ITfContext* TextService::currentContext() {
return context;
}

bool TextService::compositionRect(EditSession* session, RECT* rect) {
bool ret = false;
if(isComposing()) {
winrt::com_ptr<ITfContextView> view;
if(session->context()->GetActiveView(view.put()) == S_OK) {
BOOL clipped;
winrt::com_ptr<ITfRange> range;
if(composition_->GetRange(range.put()) == S_OK) {
if(view->GetTextExt(session->editCookie(), range.get(), rect, &clipped) == S_OK)
ret = true;
}
}
}
return ret;
}

bool TextService::selectionRect(EditSession* session, RECT* rect) {
bool ret = false;
winrt::com_ptr<ITfContextView> view;
Expand Down
42 changes: 0 additions & 42 deletions chewing_tip/TextService.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,49 +55,18 @@ class TextService:

TextService();

// public methods
ITfThreadMgr* threadMgr() const;

TfClientId clientId() const;

ITfContext* currentContext();

bool isActivated() const {
return (threadMgr() != NULL);
}

DWORD activateFlags() const {
return activateFlags_;
}

// running in Windows 8 app mode
bool isImmersive() const {
return (activateFlags_ & TF_TMF_IMMERSIVEMODE) != 0;
}

// alias of isImmersive()
bool isMetroApp() const {
return isImmersive();
}

// UI less mode is enabled (for ex: in fullscreen games)
bool isUiLess() const {
return (activateFlags_ & TF_TMF_UIELEMENTENABLEDONLY) != 0;
}

// is in console mode
bool isConsole() const {
return (activateFlags_ & TF_TMF_CONSOLE) != 0;
}

DWORD langBarStatus() const;

// language bar buttons
void addButton(ITfLangBarItemButton* button);

// preserved keys
void addPreservedKey(UINT keyCode, UINT modifiers, const GUID& guid);
void removePreservedKey(const GUID& guid);

// text composition handling
bool isComposing();
Expand All @@ -109,14 +78,11 @@ class TextService:
bool isKeyboardOpened();
void setKeyboardOpen(bool open);

bool isInsertionAllowed(EditSession* session);
void startComposition(ITfContext* context);
void endComposition(ITfContext* context);
bool compositionRect(EditSession* session, RECT* rect);
bool selectionRect(EditSession* session, RECT* rect);
HWND compositionWindow(EditSession* session);

std::wstring compositionString(EditSession* session);
void setCompositionString(EditSession* session, const wchar_t* str, int len);
void setCompositionCursor(EditSession* session, int pos);

Expand All @@ -131,7 +97,6 @@ class TextService:

// manage sinks to global or thread compartment (context specific compartment is not used)
void addCompartmentMonitor(const GUID key);
void removeCompartmentMonitor(const GUID key);

// virtual functions that IME implementors may need to override
virtual void onActivate() {}
Expand Down Expand Up @@ -162,8 +127,6 @@ class TextService:

// COM related stuff
public:
friend class DisplayAttributeInfoEnum;

// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
STDMETHODIMP_(ULONG) AddRef(void);
Expand Down Expand Up @@ -261,11 +224,6 @@ class TextService:

// event sink cookies
DWORD threadMgrEventSinkCookie_;
DWORD textEditSinkCookie_;
DWORD compositionSinkCookie_;
DWORD keyboardOpenEventSinkCookie_;
DWORD langBarSinkCookie_;
DWORD activateLanguageProfileNotifySinkCookie_;

ITfComposition* composition_; // acquired when starting composition, released when ending composition
winrt::com_ptr<ITfLangBarMgr> langBarMgr_;
Expand Down

0 comments on commit 29d4159

Please sign in to comment.