1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 using namespace ::com::sun::star::uno
;
32 #define MAX_SAVE_COUNT sal_uInt16(10)
38 SearchDialog::SearchDialog(weld::Window
* pWindow
, const OUString
& rConfigName
)
39 : GenericDialogController(pWindow
, "sfx/ui/searchdialog.ui", "SearchDialog")
40 , m_sConfigName(rConfigName
)
41 , m_xSearchEdit(m_xBuilder
->weld_combo_box("searchterm"))
42 , m_xWholeWordsBox(m_xBuilder
->weld_check_button("wholewords"))
43 , m_xMatchCaseBox(m_xBuilder
->weld_check_button("matchcase"))
44 , m_xWrapAroundBox(m_xBuilder
->weld_check_button("wrap"))
45 , m_xBackwardsBox(m_xBuilder
->weld_check_button("backwards"))
46 , m_xFindBtn(m_xBuilder
->weld_button("ok"))
49 m_xFindBtn
->connect_clicked(LINK(this, SearchDialog
, FindHdl
));
50 // load config: old search strings and the status of the check boxes
52 // the search edit should have the focus
53 m_xSearchEdit
->grab_focus();
56 SearchDialog::~SearchDialog()
61 void SearchDialog::LoadConfig()
63 SvtViewOptions
aViewOpt( EViewType::Dialog
, m_sConfigName
);
64 if ( aViewOpt
.Exists() )
66 Any aUserItem
= aViewOpt
.GetUserItem( "UserItem" );
68 if ( aUserItem
>>= sUserData
)
70 DBG_ASSERT( comphelper::string::getTokenCount(sUserData
, ';') == 5, "invalid config data" );
72 OUString sSearchText
= sUserData
.getToken( 0, ';', nIdx
);
73 m_xWholeWordsBox
->set_active( sUserData
.getToken( 0, ';', nIdx
).toInt32() == 1 );
74 m_xMatchCaseBox
->set_active( sUserData
.getToken( 0, ';', nIdx
).toInt32() == 1 );
75 m_xWrapAroundBox
->set_active( sUserData
.getToken( 0, ';', nIdx
).toInt32() == 1 );
76 m_xBackwardsBox
->set_active( sUserData
.getToken( 0, ';', nIdx
).toInt32() == 1 );
80 m_xSearchEdit
->append_text(sSearchText
.getToken( 0, '\t', nIdx
));
81 m_xSearchEdit
->set_active(0);
85 m_xWrapAroundBox
->set_active(true);
88 void SearchDialog::SaveConfig()
90 SvtViewOptions
aViewOpt( EViewType::Dialog
, m_sConfigName
);
92 int i
= 0, nCount
= std::min(m_xSearchEdit
->get_count(), static_cast<int>(MAX_SAVE_COUNT
));
93 for ( ; i
< nCount
; ++i
)
95 sUserData
+= m_xSearchEdit
->get_text(i
) + "\t";
97 sUserData
= comphelper::string::stripStart(sUserData
, '\t') + ";" +
98 OUString::number( m_xWholeWordsBox
->get_active() ? 1 : 0 ) + ";" +
99 OUString::number( m_xMatchCaseBox
->get_active() ? 1 : 0 ) + ";" +
100 OUString::number( m_xWrapAroundBox
->get_active() ? 1 : 0 ) + ";" +
101 OUString::number( m_xBackwardsBox
->get_active() ? 1 : 0 );
103 Any aUserItem
= makeAny( sUserData
);
104 aViewOpt
.SetUserItem( "UserItem", aUserItem
);
107 IMPL_LINK_NOARG(SearchDialog
, FindHdl
, weld::Button
&, void)
109 OUString sSrchTxt
= m_xSearchEdit
->get_active_text();
110 auto nPos
= m_xSearchEdit
->find_text(sSrchTxt
);
114 m_xSearchEdit
->remove(nPos
);
115 m_xSearchEdit
->insert_text(0, sSrchTxt
);
117 m_aFindHdl
.Call( *this );
120 void SearchDialog::SetFocusOnEdit()
122 m_xSearchEdit
->select_entry_region(0, -1);
123 m_xSearchEdit
->grab_focus();
126 void SearchDialog::runAsync(const std::shared_ptr
<SearchDialog
>& rController
)
128 weld::DialogController::runAsync(rController
, [=](sal_Int32
/*nResult*/){ rController
->m_aCloseHdl
.Call(nullptr); });
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */