#include "SRLexiWidget.h" #include "SRFramework\SRApp.h" #include "swmodule.h" #include "swkey.h" using namespace SRFramework; BOOL SRLexiWidget::s_fRegistered = false; SRLexiWidget::SRLexiWidget(void) :m_cmbDictEntry(NULL) ,m_viewLexi(NULL) ,m_keyDummy("") { m_wcsClassName = "SRLexiWidget"; m_wcsWindowName = ""; m_hInstance = SRFramework::SRApp::GetInstanceHandle(); } SRLexiWidget::~SRLexiWidget(void) { if(m_lblEntry) delete m_lblEntry; if(m_cmbDictEntry) delete m_cmbDictEntry; if(m_viewLexi) delete m_viewLexi; } BOOL SRLexiWidget::Create(SRWnd *pParentWnd, RECT bounds) { if(!Register()) return FALSE; if(!SRWnd::Create(m_wcsClassName,m_wcsWindowName,WS_CHILD | WS_VISIBLE, bounds, pParentWnd, NULL, m_hInstance)) return FALSE; return TRUE; } WCString SRLexiWidget::GetWindowTitle() { if(m_viewLexi) return m_viewLexi->GetWindowTitle(); return m_wcsWindowName; } VOID SRLexiWidget::MoveWindow(LPCRECT lpRect,BOOL bRepaint) { SRWnd::MoveWindow(lpRect, bRepaint); RECT clientRect = GetBounds(); RECT lblRect = clientRect; RECT cmbRect = clientRect; RECT viewRect = clientRect; lblRect.top = 4; lblRect.left = 4; lblRect.right = 60; lblRect.bottom = 20; cmbRect.top = 0; cmbRect.left = 60; cmbRect.bottom = 20; viewRect.top = 20; if(m_viewLexi) m_viewLexi->MoveWindow(&viewRect, bRepaint); if(m_cmbDictEntry) m_cmbDictEntry->MoveWindow(&cmbRect, bRepaint); if(m_lblEntry) m_lblEntry->MoveWindow(&lblRect, bRepaint); } BOOL SRLexiWidget::Register() { // Register window class... WNDCLASS wc; if(s_fRegistered) return TRUE; wc.style = CS_HREDRAW | CS_VREDRAW | CS_PARENTDC; wc.lpfnWndProc = (WNDPROC) MessageRoute; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = m_hInstance; wc.hIcon = NULL; wc.hCursor = 0; wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = m_wcsClassName.w_str(); if(RegisterClass(&wc) == 0) return FALSE; s_fRegistered = TRUE; return TRUE; } BOOL SRLexiWidget::Init() { RECT clientRect = GetBounds(); m_lblEntry = new SRLabel("Lookup:"); if(!m_lblEntry || !m_lblEntry->Create(this, clientRect)) return FALSE; m_lblEntry->Show(); m_cmbDictEntry = new SRComboBox(); if(!m_cmbDictEntry || !m_cmbDictEntry->Create(this, clientRect)) return FALSE; m_cmbDictEntry->Show(); m_viewLexi = new SRLexiView(); if(!m_viewLexi || !m_viewLexi->Create(this, clientRect)) return FALSE; m_viewLexi->Show(); MoveWindow(&clientRect, TRUE); return TRUE; } /* Partially fills the ComboBox drop down with entries based upon the current text. */ VOID SRLexiWidget::FillDictEntries() { INT nCount = 0; INT nMaxCount = 10; INT nCurSel = 0; if(!m_cmbDictEntry) return; SWModule *pModule = m_viewLexi->GetModule(); if(!pModule) return; pModule->setKey(m_wcsCurEntry.c_str()); pModule->Error(); // Back up halfway or as far as we can for(nCount = 0; nCount < nMaxCount/2; nCount++){ (*pModule)--; if(pModule->Error()) break; } nCurSel = nCount; m_cmbDictEntry->Clear(); for(nCount = 0; nCount < nMaxCount; nCount++){ m_cmbDictEntry->AddItem(pModule->getKeyText()); (*pModule)++; if(pModule->Error()) break; } m_cmbDictEntry->SetCurSel(nCurSel); } const SWKey &SRLexiWidget::GetKey() { if(!m_viewLexi) return m_keyDummy; return m_viewLexi->GetKey(); } const SWModule *SRLexiWidget::GetModule() { if(!m_viewLexi) return NULL; return m_viewLexi->GetModule(); } VOID SRLexiWidget::SetModule(sword::SWModule *pModule) { if(m_viewLexi){ m_viewLexi->SetModule(pModule); FillDictEntries(); } } VOID SRLexiWidget::SetSwordReady() { if(m_viewLexi) m_viewLexi->SetSwordReady(); } VOID SRLexiWidget::RefreshWindow() { if(m_viewLexi) m_viewLexi->RefreshWindow(); } VOID SRLexiWidget::RefreshScreen(BOOL fReloadText) { if(m_viewLexi) m_viewLexi->RefreshScreen(fReloadText); } VOID SRLexiWidget::SetKey(const sword::StrKey &key) { if(m_viewLexi){ m_viewLexi->SetKey(key); m_wcsCurEntry = key.getText(); m_cmbDictEntry->SetText(m_wcsCurEntry); } } BOOL SRLexiWidget::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { BOOL retval = FALSE; if(m_viewLexi){ retval = m_viewLexi->OnKeyDown(nChar, nRepCnt, nFlags); if(m_cmbDictEntry){ m_wcsCurEntry = m_viewLexi->GetKey().getText(); m_cmbDictEntry->SetText(m_wcsCurEntry); } } return retval; } BOOL SRLexiWidget::OnCommand(WORD wNotifyCode, WORD wID, HWND hWndCtl) { INT nCurSel = -1; if(m_cmbDictEntry && m_cmbDictEntry->IshWnd(hWndCtl)){ switch(wNotifyCode) { case CBN_ERRSPACE: return FALSE; case CBN_SELCHANGE: nCurSel = m_cmbDictEntry->GetCurSel(); m_wcsCurEntry = m_cmbDictEntry->GetItemAt(nCurSel); m_cmbDictEntry->SetText(m_wcsCurEntry); m_viewLexi->SetEntry(m_wcsCurEntry); break; case CBN_DBLCLK: return FALSE; case CBN_SETFOCUS: return FALSE; case CBN_KILLFOCUS: return FALSE; case CBN_EDITCHANGE: m_wcsCurEntry = m_cmbDictEntry->GetText(); m_viewLexi->SetEntry(m_wcsCurEntry); break; case CBN_EDITUPDATE: return FALSE; case CBN_DROPDOWN: FillDictEntries(); break; case CBN_CLOSEUP: m_pParentWnd->SetFocus(); break; case CBN_SELENDOK: m_wcsCurEntry = m_cmbDictEntry->GetText(); m_viewLexi->SetEntry(m_wcsCurEntry); break; case CBN_SELENDCANCEL: return FALSE; default: return FALSE; } } return TRUE; } BOOL SRLexiWidget::OnPaint() { PAINTSTRUCT ps; HDC hdc = BeginPaint(m_hWnd, &ps); FillRect(hdc, &ps.rcPaint,(HBRUSH)GetStockObject(WHITE_BRUSH)); EndPaint(m_hWnd, &ps); return TRUE; }