diff options
author | Caolán McNamara <caolanm@redhat.com> | 2020-02-13 13:28:47 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2020-02-13 20:55:46 +0100 |
commit | 3b8c42a94b5448364d47daa103b3452f54990524 (patch) | |
tree | 36c281fdcd34355a2c080d4a4aa2f84cdc802fad /svx/source/tbxctrls/tbunosearchcontrollers.cxx | |
parent | 8eab16d46f0c70cf9f7afb307ab4a56c49919ac4 (diff) |
weld FindTextFieldControl
Change-Id: Ib5861855d37c72d2be2b11b77173a141db90444e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88614
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'svx/source/tbxctrls/tbunosearchcontrollers.cxx')
-rw-r--r-- | svx/source/tbxctrls/tbunosearchcontrollers.cxx | 267 |
1 files changed, 165 insertions, 102 deletions
diff --git a/svx/source/tbxctrls/tbunosearchcontrollers.cxx b/svx/source/tbxctrls/tbunosearchcontrollers.cxx index 104355137361..3769273ac3df 100644 --- a/svx/source/tbxctrls/tbunosearchcontrollers.cxx +++ b/svx/source/tbxctrls/tbunosearchcontrollers.cxx @@ -47,6 +47,7 @@ #include <com/sun/star/util/SearchAlgorithms.hpp> #include <com/sun/star/util/SearchAlgorithms2.hpp> +#include <sfx2/InterimItemWindow.hxx> #include <svl/ctloptions.hxx> #include <svl/srchitem.hxx> #include <svtools/acceleratorexecute.hxx> @@ -58,11 +59,12 @@ #include <rtl/instance.hxx> #include <svx/srchdlg.hxx> #include <vcl/button.hxx> -#include <vcl/combobox.hxx> #include <vcl/event.hxx> #include <vcl/fixed.hxx> #include <vcl/window.hxx> +#include <findtextfield.hxx> + using namespace css; namespace { @@ -99,9 +101,9 @@ void impl_executeSearch( const css::uno::Reference< css::uno::XComponentContext OUString sItemCommand = pToolBox->GetItemCommand(id); if ( sItemCommand == COMMAND_FINDTEXT ) { - vcl::Window* pItemWin = pToolBox->GetItemWindow(id); + FindTextFieldControl* pItemWin = static_cast<FindTextFieldControl*>(pToolBox->GetItemWindow(id)); if (pItemWin) - sFindText = pItemWin->GetText(); + sFindText = pItemWin->get_active_text(); } else if ( sItemCommand == COMMAND_MATCHCASE ) { CheckBox* pItemWin = static_cast<CheckBox*>( pToolBox->GetItemWindow(id) ); @@ -144,52 +146,44 @@ void impl_executeSearch( const css::uno::Reference< css::uno::XComponentContext } } -class FindTextFieldControl : public ComboBox -{ -public: - FindTextFieldControl( vcl::Window* pParent, - css::uno::Reference< css::frame::XFrame > const & xFrame, - const css::uno::Reference< css::uno::XComponentContext >& xContext ); - - virtual bool PreNotify( NotifyEvent& rNEvt ) override; - - void Remember_Impl(const OUString& rStr); - void SetTextToSelected_Impl(); - -private: - - css::uno::Reference< css::frame::XFrame > m_xFrame; - css::uno::Reference< css::uno::XComponentContext > m_xContext; - std::unique_ptr<svt::AcceleratorExecute> m_pAcc; -}; +} FindTextFieldControl::FindTextFieldControl( vcl::Window* pParent, css::uno::Reference< css::frame::XFrame > const & xFrame, const css::uno::Reference< css::uno::XComponentContext >& xContext) : - ComboBox(pParent, WB_DROPDOWN | WB_VSCROLL), + InterimItemWindow(pParent, "svx/ui/findbox.ui", "FindBox"), + m_nAsyncGetFocusId(nullptr), + m_xWidget(m_xBuilder->weld_combo_box("find")), m_xFrame(xFrame), m_xContext(xContext), m_pAcc(svt::AcceleratorExecute::createAcceleratorHelper()) { - SetPlaceholderText(SvxResId(RID_SVXSTR_FINDBAR_FIND)); - EnableAutocomplete(true, true); + m_xWidget->set_entry_placeholder_text(SvxResId(RID_SVXSTR_FINDBAR_FIND)); + m_xWidget->set_entry_completion(true, true); m_pAcc->init(m_xContext, m_xFrame); + + m_xWidget->connect_focus_in(LINK(this, FindTextFieldControl, FocusInHdl)); + m_xWidget->connect_key_press(LINK(this, FindTextFieldControl, KeyInputHdl)); + m_xWidget->connect_entry_activate(LINK(this, FindTextFieldControl, ActivateHdl)); + + m_xWidget->set_size_request(250, -1); + SetSizePixel(m_xWidget->get_preferred_size()); } void FindTextFieldControl::Remember_Impl(const OUString& rStr) { - const sal_Int32 nCount = GetEntryCount(); + const sal_Int32 nCount = m_xWidget->get_count(); for (sal_Int32 i=0; i<nCount; ++i) { - if ( rStr == GetEntry(i)) + if (rStr == m_xWidget->get_text(i)) return; } if (nCount == REMEMBER_SIZE) - RemoveEntryAt(REMEMBER_SIZE-1); + m_xWidget->remove(REMEMBER_SIZE-1); - InsertEntry(rStr, 0); + m_xWidget->insert_text(0, rStr); } void FindTextFieldControl::SetTextToSelected_Impl() @@ -214,93 +208,159 @@ void FindTextFieldControl::SetTextToSelected_Impl() if ( !aString.isEmpty() ) { // If something is selected in the document, prepopulate with this - SetText( aString ); - GetModifyHdl().Call(*this); // FIXME why SetText doesn't trigger this? + m_xWidget->set_entry_text(aString); + m_aChangeHdl.Call(*m_xWidget); } - else if (GetEntryCount() > 0) + else if (get_count() > 0) { // Else, prepopulate with last search word (fdo#84256) - SetText(GetEntry(0)); + m_xWidget->set_entry_text(m_xWidget->get_text(0)); } } -bool FindTextFieldControl::PreNotify( NotifyEvent& rNEvt ) +IMPL_LINK(FindTextFieldControl, KeyInputHdl, const KeyEvent&, rKeyEvent, bool) { if (isDisposed()) return true; - bool bRet= ComboBox::PreNotify( rNEvt ); - switch ( rNEvt.GetType() ) + bool bRet = false; + + bool bShift = rKeyEvent.GetKeyCode().IsShift(); + bool bMod1 = rKeyEvent.GetKeyCode().IsMod1(); + sal_uInt16 nCode = rKeyEvent.GetKeyCode().GetCode(); + + // Close the search bar on Escape + if ( KEY_ESCAPE == nCode ) { - case MouseNotifyEvent::KEYINPUT: + bRet = true; + GrabFocusToDocument(); + + // hide the findbar + css::uno::Reference< css::beans::XPropertySet > xPropSet(m_xFrame, css::uno::UNO_QUERY); + if (xPropSet.is()) { - // Clear SearchLabel when altering the search string - #if HAVE_FEATURE_DESKTOP - SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::Empty); - #endif - - const KeyEvent* pKeyEvent = rNEvt.GetKeyEvent(); - bool bShift = pKeyEvent->GetKeyCode().IsShift(); - bool bMod1 = pKeyEvent->GetKeyCode().IsMod1(); - sal_uInt16 nCode = pKeyEvent->GetKeyCode().GetCode(); - - // Close the search bar on Escape - if ( KEY_ESCAPE == nCode ) + css::uno::Reference< css::frame::XLayoutManager > xLayoutManager; + css::uno::Any aValue = xPropSet->getPropertyValue("LayoutManager"); + aValue >>= xLayoutManager; + if (xLayoutManager.is()) { - bRet = true; - GrabFocusToDocument(); - - // hide the findbar - css::uno::Reference< css::beans::XPropertySet > xPropSet(m_xFrame, css::uno::UNO_QUERY); - if (xPropSet.is()) - { - css::uno::Reference< css::frame::XLayoutManager > xLayoutManager; - css::uno::Any aValue = xPropSet->getPropertyValue("LayoutManager"); - aValue >>= xLayoutManager; - if (xLayoutManager.is()) - { - const OUString sResourceURL( "private:resource/toolbar/findbar" ); - xLayoutManager->hideElement( sResourceURL ); - xLayoutManager->destroyElement( sResourceURL ); - } - } + const OUString sResourceURL( "private:resource/toolbar/findbar" ); + xLayoutManager->hideElement( sResourceURL ); + xLayoutManager->destroyElement( sResourceURL ); } - // Select text in the search box when Ctrl-F pressed - else if ( bMod1 && nCode == KEY_F ) - SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) ); + } + } + // Select text in the search box when Ctrl-F pressed + else if ( bMod1 && nCode == KEY_F ) + m_xWidget->select_entry_region(0, -1); - // Execute the search when Return, Ctrl-G or F3 pressed - else if ( KEY_RETURN == nCode || (bMod1 && (KEY_G == nCode)) || (KEY_F3 == nCode) ) - { - Remember_Impl(GetText()); + // Execute the search when Return, Ctrl-G or F3 pressed + else if ( KEY_RETURN == nCode || (bMod1 && (KEY_G == nCode)) || (KEY_F3 == nCode) ) + { + ActivateFind(bShift); + bRet = true; + } + else + { + auto awtKey = svt::AcceleratorExecute::st_VCLKey2AWTKey(rKeyEvent.GetKeyCode()); + const OUString aCommand(m_pAcc->findCommand(awtKey)); + if (aCommand == ".uno:SearchDialog") + bRet = m_pAcc->execute(awtKey); + } - vcl::Window* pWindow = GetParent(); - ToolBox* pToolBox = static_cast<ToolBox*>(pWindow); + return bRet || ChildKeyInput(rKeyEvent); +} - impl_executeSearch( m_xContext, m_xFrame, pToolBox, bShift); - bRet = true; - } - else - { - auto awtKey = svt::AcceleratorExecute::st_VCLKey2AWTKey(pKeyEvent->GetKeyCode()); - const OUString aCommand(m_pAcc->findCommand(awtKey)); - if (aCommand == ".uno:SearchDialog") - bRet = m_pAcc->execute(awtKey); - } - break; - } +void FindTextFieldControl::ActivateFind(bool bShift) +{ + Remember_Impl(m_xWidget->get_active_text()); + + vcl::Window* pWindow = GetParent(); + ToolBox* pToolBox = static_cast<ToolBox*>(pWindow); + + impl_executeSearch(m_xContext, m_xFrame, pToolBox, bShift); +} + +IMPL_LINK_NOARG(FindTextFieldControl, ActivateHdl, weld::ComboBox&, bool) +{ + if (isDisposed()) + return true; + + ActivateFind(false); + + return true; +} + +IMPL_LINK_NOARG(FindTextFieldControl, OnAsyncGetFocus, void*, void) +{ + m_nAsyncGetFocusId = nullptr; + m_xWidget->select_entry_region(0, -1); +} - case MouseNotifyEvent::GETFOCUS: - SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) ); - break; +IMPL_LINK_NOARG(FindTextFieldControl, FocusInHdl, weld::Widget&, void) +{ + if (m_nAsyncGetFocusId) + return; + // do it async to defeat entry in combobox having its own ideas about the focus + m_nAsyncGetFocusId = Application::PostUserEvent(LINK(this, FindTextFieldControl, OnAsyncGetFocus)); +} - default: - break; +void FindTextFieldControl::dispose() +{ + if (m_nAsyncGetFocusId) + { + Application::RemoveUserEvent(m_nAsyncGetFocusId); + m_nAsyncGetFocusId = nullptr; } + m_xWidget.reset(); + InterimItemWindow::dispose(); +} + +FindTextFieldControl::~FindTextFieldControl() +{ + disposeOnce(); +} - return bRet; +void FindTextFieldControl::connect_changed(const Link<weld::ComboBox&, void>& rLink) +{ + m_aChangeHdl = rLink; + m_xWidget->connect_changed(rLink); +} + +int FindTextFieldControl::get_count() const +{ + return m_xWidget->get_count(); } +OUString FindTextFieldControl::get_text(int nIndex) const +{ + return m_xWidget->get_text(nIndex); +} + +OUString FindTextFieldControl::get_active_text() const +{ + return m_xWidget->get_active_text(); +} + +void FindTextFieldControl::append_text(const OUString& rText) +{ + m_xWidget->append_text(rText); +} + +void FindTextFieldControl::set_entry_message_type(weld::EntryMessageType eType) +{ + m_xWidget->set_entry_message_type(eType); +} + +void FindTextFieldControl::GetFocus() +{ + if (!m_xWidget) + return; + m_xWidget->grab_focus(); +} + +namespace { + class SearchToolbarControllersManager { public: @@ -342,11 +402,11 @@ SearchToolbarControllersManager& SearchToolbarControllersManager::createControll void SearchToolbarControllersManager::saveSearchHistory(const FindTextFieldControl* pFindTextFieldControl) { - const sal_Int32 nECount( pFindTextFieldControl->GetEntryCount() ); + const sal_Int32 nECount( pFindTextFieldControl->get_count() ); m_aSearchStrings.resize( nECount ); for( sal_Int32 i=0; i<nECount; ++i ) { - m_aSearchStrings[i] = pFindTextFieldControl->GetEntry(i); + m_aSearchStrings[i] = pFindTextFieldControl->get_text(i); } } @@ -354,7 +414,7 @@ void SearchToolbarControllersManager::loadSearchHistory(FindTextFieldControl* pF { for( size_t i=0; i<m_aSearchStrings.size(); ++i ) { - pFindTextFieldControl->InsertEntry(m_aSearchStrings[i],i); + pFindTextFieldControl->append_text(m_aSearchStrings[i]); } } @@ -443,7 +503,7 @@ public: // XStatusListener virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override; - DECL_LINK(EditModifyHdl, Edit&, void); + DECL_LINK(EditModifyHdl, weld::ComboBox&, void); private: @@ -544,9 +604,7 @@ css::uno::Reference< css::awt::XWindow > SAL_CALL FindTextToolbarController::cre ToolBox* pToolbar = static_cast<ToolBox*>(pParent.get()); m_pFindTextFieldControl = VclPtr<FindTextFieldControl>::Create(pToolbar, m_xFrame, m_xContext); - Size aSize(250, m_pFindTextFieldControl->GetTextHeight() + 200); - m_pFindTextFieldControl->SetSizePixel( aSize ); - m_pFindTextFieldControl->SetModifyHdl(LINK(this, FindTextToolbarController, EditModifyHdl)); + m_pFindTextFieldControl->connect_changed(LINK(this, FindTextToolbarController, EditModifyHdl)); SearchToolbarControllersManager::createControllersManager().loadSearchHistory(m_pFindTextFieldControl); } xItemWindow = VCLUnoHelper::GetInterface( m_pFindTextFieldControl ); @@ -564,14 +622,19 @@ void SAL_CALL FindTextToolbarController::statusChanged( const css::frame::Featur OUString aFeatureURL = rEvent.FeatureURL.Complete; if ( aFeatureURL == "AppendSearchHistory" ) { - m_pFindTextFieldControl->Remember_Impl(m_pFindTextFieldControl->GetText()); + m_pFindTextFieldControl->Remember_Impl(m_pFindTextFieldControl->get_active_text()); } // enable up/down buttons in case there is already text (from the search history) textfieldChanged(); } -IMPL_LINK_NOARG(FindTextToolbarController, EditModifyHdl, Edit&, void) +IMPL_LINK_NOARG(FindTextToolbarController, EditModifyHdl, weld::ComboBox&, void) { + // Clear SearchLabel when search string altered + #if HAVE_FEATURE_DESKTOP + SvxSearchDialogWrapper::SetSearchLabel(SearchLabel::Empty); + #endif + textfieldChanged(); } @@ -581,7 +644,7 @@ void FindTextToolbarController::textfieldChanged() { ToolBox* pToolBox = static_cast<ToolBox*>(pWindow.get()); if ( pToolBox && m_pFindTextFieldControl ) { - bool enableButtons = !m_pFindTextFieldControl->GetText().isEmpty(); + bool enableButtons = !m_pFindTextFieldControl->get_active_text().isEmpty(); pToolBox->EnableItem(m_nDownSearchId, enableButtons); pToolBox->EnableItem(m_nUpSearchId, enableButtons); pToolBox->EnableItem(m_nFindAllId, enableButtons); |