sd: keep a non-owning pointer to the OverridingShell
[LibreOffice.git] / sfx2 / source / dialog / srchdlg.cxx
blob1ec8a778b04f7dfdd713d73db390ae9c9e5a3587
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <srchdlg.hxx>
22 #include <comphelper/string.hxx>
24 #include <tools/debug.hxx>
25 #include <unotools/viewoptions.hxx>
26 #include <o3tl/string_view.hxx>
27 #include <utility>
29 using namespace ::com::sun::star::uno;
32 namespace sfx2 {
34 #define MAX_SAVE_COUNT sal_uInt16(10)
37 // SearchDialog
40 SearchDialog::SearchDialog(weld::Window* pWindow, OUString aConfigName)
41 : GenericDialogController(pWindow, u"sfx/ui/searchdialog.ui"_ustr, u"SearchDialog"_ustr)
42 , m_sConfigName(std::move(aConfigName))
43 , m_xSearchEdit(m_xBuilder->weld_combo_box(u"searchterm"_ustr))
44 , m_xWholeWordsBox(m_xBuilder->weld_check_button(u"wholewords"_ustr))
45 , m_xMatchCaseBox(m_xBuilder->weld_check_button(u"matchcase"_ustr))
46 , m_xWrapAroundBox(m_xBuilder->weld_check_button(u"wrap"_ustr))
47 , m_xBackwardsBox(m_xBuilder->weld_check_button(u"backwards"_ustr))
48 , m_xFindBtn(m_xBuilder->weld_button(u"ok"_ustr))
50 // set handler
51 m_xFindBtn->connect_clicked(LINK(this, SearchDialog, FindHdl));
52 // load config: old search strings and the status of the check boxes
53 LoadConfig();
54 // the search edit should have the focus
55 m_xSearchEdit->grab_focus();
58 SearchDialog::~SearchDialog()
60 SaveConfig();
63 void SearchDialog::LoadConfig()
65 SvtViewOptions aViewOpt( EViewType::Dialog, m_sConfigName );
66 if ( aViewOpt.Exists() )
68 Any aUserItem = aViewOpt.GetUserItem( u"UserItem"_ustr );
69 OUString sUserData;
70 if ( aUserItem >>= sUserData )
72 DBG_ASSERT( comphelper::string::getTokenCount(sUserData, ';') == 5, "invalid config data" );
73 sal_Int32 nIdx = 0;
74 OUString sSearchText = sUserData.getToken( 0, ';', nIdx );
75 m_xWholeWordsBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
76 m_xMatchCaseBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
77 m_xWrapAroundBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
78 m_xBackwardsBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
80 nIdx = 0;
81 while ( nIdx != -1 )
82 m_xSearchEdit->append_text(sSearchText.getToken( 0, '\t', nIdx));
83 m_xSearchEdit->set_active(0);
86 else
87 m_xWrapAroundBox->set_active(true);
90 void SearchDialog::SaveConfig()
92 SvtViewOptions aViewOpt( EViewType::Dialog, m_sConfigName );
93 OUString sUserData;
94 int i = 0, nCount = std::min(m_xSearchEdit->get_count(), static_cast<int>(MAX_SAVE_COUNT));
95 for ( ; i < nCount; ++i )
97 sUserData += m_xSearchEdit->get_text(i) + "\t";
99 sUserData = comphelper::string::stripStart(sUserData, '\t') + ";" +
100 OUString::number( m_xWholeWordsBox->get_active() ? 1 : 0 ) + ";" +
101 OUString::number( m_xMatchCaseBox->get_active() ? 1 : 0 ) + ";" +
102 OUString::number( m_xWrapAroundBox->get_active() ? 1 : 0 ) + ";" +
103 OUString::number( m_xBackwardsBox->get_active() ? 1 : 0 );
105 Any aUserItem( sUserData );
106 aViewOpt.SetUserItem( u"UserItem"_ustr, aUserItem );
109 IMPL_LINK_NOARG(SearchDialog, FindHdl, weld::Button&, void)
111 OUString sSrchTxt = m_xSearchEdit->get_active_text();
112 auto nPos = m_xSearchEdit->find_text(sSrchTxt);
113 if (nPos != 0)
115 if (nPos != -1)
116 m_xSearchEdit->remove(nPos);
117 m_xSearchEdit->insert_text(0, sSrchTxt);
119 m_aFindHdl.Call( *this );
122 void SearchDialog::SetFocusOnEdit()
124 m_xSearchEdit->select_entry_region(0, -1);
125 m_xSearchEdit->grab_focus();
128 void SearchDialog::runAsync(const std::shared_ptr<SearchDialog>& rController)
130 weld::DialogController::runAsync(rController, [=](sal_Int32 /*nResult*/){ rController->m_aCloseHdl.Call(nullptr); });
133 } // namespace sfx2
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */