Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/dialog/srchdlg.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
21
#include <srchdlg.hxx>
22
#include <comphelper/string.hxx>
23
24
#include <svtools/viewoptions.hxx>
25
#include <tools/debug.hxx>
26
#include <o3tl/string_view.hxx>
27
#include <vcl/weld/Builder.hxx>
28
#include <utility>
29
30
using namespace ::com::sun::star::uno;
31
32
33
namespace sfx2 {
34
35
0
#define MAX_SAVE_COUNT      sal_uInt16(10)
36
37
38
// SearchDialog
39
40
41
SearchDialog::SearchDialog(weld::Window* pWindow, OUString aConfigName)
42
0
    : GenericDialogController(pWindow, u"sfx/ui/searchdialog.ui"_ustr, u"SearchDialog"_ustr)
43
0
    , m_sConfigName(std::move(aConfigName))
44
0
    , m_xSearchEdit(m_xBuilder->weld_combo_box(u"searchterm"_ustr))
45
0
    , m_xWholeWordsBox(m_xBuilder->weld_check_button(u"wholewords"_ustr))
46
0
    , m_xMatchCaseBox(m_xBuilder->weld_check_button(u"matchcase"_ustr))
47
0
    , m_xWrapAroundBox(m_xBuilder->weld_check_button(u"wrap"_ustr))
48
0
    , m_xBackwardsBox(m_xBuilder->weld_check_button(u"backwards"_ustr))
49
0
    , m_xFindBtn(m_xBuilder->weld_button(u"ok"_ustr))
50
0
{
51
    // set handler
52
0
    m_xFindBtn->connect_clicked(LINK(this, SearchDialog, FindHdl));
53
    // load config: old search strings and the status of the check boxes
54
0
    LoadConfig();
55
    // the search edit should have the focus
56
0
    m_xSearchEdit->grab_focus();
57
0
}
58
59
SearchDialog::~SearchDialog()
60
0
{
61
0
    SaveConfig();
62
0
}
63
64
void SearchDialog::LoadConfig()
65
0
{
66
0
    SvtViewOptions aViewOpt( EViewType::Dialog, m_sConfigName );
67
0
    if ( aViewOpt.Exists() )
68
0
    {
69
0
        Any aUserItem = aViewOpt.GetUserItem( u"UserItem"_ustr );
70
0
        OUString sUserData;
71
0
        if ( aUserItem >>= sUserData )
72
0
        {
73
0
            DBG_ASSERT( comphelper::string::getTokenCount(sUserData, ';') == 5, "invalid config data" );
74
0
            sal_Int32 nIdx = 0;
75
0
            OUString sSearchText = sUserData.getToken( 0, ';', nIdx );
76
0
            m_xWholeWordsBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
77
0
            m_xMatchCaseBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
78
0
            m_xWrapAroundBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
79
0
            m_xBackwardsBox->set_active( o3tl::toInt32(o3tl::getToken(sUserData, 0, ';', nIdx )) == 1 );
80
81
0
            nIdx = 0;
82
0
            while ( nIdx != -1 )
83
0
                m_xSearchEdit->append_text(sSearchText.getToken( 0, '\t', nIdx));
84
0
            m_xSearchEdit->set_active(0);
85
0
        }
86
0
    }
87
0
    else
88
0
        m_xWrapAroundBox->set_active(true);
89
0
}
90
91
void SearchDialog::SaveConfig()
92
0
{
93
0
    SvtViewOptions aViewOpt( EViewType::Dialog, m_sConfigName );
94
0
    OUString sUserData;
95
0
    int i = 0, nCount = std::min(m_xSearchEdit->get_count(), static_cast<int>(MAX_SAVE_COUNT));
96
0
    for ( ; i < nCount; ++i )
97
0
    {
98
0
        sUserData += m_xSearchEdit->get_text(i) + "\t";
99
0
    }
100
0
    sUserData = comphelper::string::stripStart(sUserData, '\t') + ";" +
101
0
        OUString::number( m_xWholeWordsBox->get_active() ? 1 : 0 ) + ";" +
102
0
        OUString::number( m_xMatchCaseBox->get_active() ? 1 : 0 ) + ";" +
103
0
        OUString::number( m_xWrapAroundBox->get_active() ? 1 : 0 ) + ";" +
104
0
        OUString::number( m_xBackwardsBox->get_active() ? 1 : 0 );
105
106
0
    Any aUserItem( sUserData );
107
0
    aViewOpt.SetUserItem( u"UserItem"_ustr, aUserItem );
108
0
}
109
110
IMPL_LINK_NOARG(SearchDialog, FindHdl, weld::Button&, void)
111
0
{
112
0
    OUString sSrchTxt = m_xSearchEdit->get_active_text();
113
0
    auto nPos = m_xSearchEdit->find_text(sSrchTxt);
114
0
    if (nPos != 0)
115
0
    {
116
0
        if (nPos != -1)
117
0
            m_xSearchEdit->remove(nPos);
118
0
        m_xSearchEdit->insert_text(0, sSrchTxt);
119
0
    }
120
0
    m_aFindHdl.Call( *this );
121
0
}
122
123
void SearchDialog::SetFocusOnEdit()
124
0
{
125
0
    m_xSearchEdit->select_entry_region(0, -1);
126
0
    m_xSearchEdit->grab_focus();
127
0
}
128
129
void SearchDialog::runAsync(const std::shared_ptr<SearchDialog>& rController)
130
0
{
131
0
    weld::DialogController::runAsync(rController, [=](sal_Int32 /*nResult*/){ rController->m_aCloseHdl.Call(nullptr); });
132
0
}
133
134
} // namespace sfx2
135
136
137
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */