Line data Source code
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 : #include <sfx2/sfxresid.hxx>
24 : #include <sfx2/sfxuno.hxx>
25 :
26 : #include "dialog.hrc"
27 : #include <tools/debug.hxx>
28 : #include <unotools/viewoptions.hxx>
29 :
30 : using namespace ::com::sun::star::uno;
31 :
32 :
33 :
34 : namespace sfx2 {
35 :
36 : #define MAX_SAVE_COUNT (sal_uInt16)10
37 :
38 :
39 : // SearchDialog
40 :
41 :
42 0 : SearchDialog::SearchDialog(vcl::Window* pWindow, const OUString& rConfigName)
43 : : ModelessDialog(pWindow, "SearchDialog", "sfx/ui/searchdialog.ui")
44 : , m_sConfigName(rConfigName)
45 0 : , m_bIsConstructed(false)
46 :
47 : {
48 0 : get(m_pSearchEdit, "searchterm");
49 0 : get(m_pWholeWordsBox, "wholewords");
50 0 : get(m_pMatchCaseBox, "matchcase");
51 0 : get(m_pWrapAroundBox, "wrap");
52 0 : get(m_pBackwardsBox, "backwards");
53 0 : get(m_pFindBtn, "search");
54 :
55 : // set handler
56 0 : m_pFindBtn->SetClickHdl( LINK( this, SearchDialog, FindHdl ) );
57 : // load config: old search strings and the status of the check boxes
58 0 : LoadConfig();
59 : // the search edit should have the focus
60 0 : m_pSearchEdit->GrabFocus();
61 0 : }
62 :
63 0 : SearchDialog::~SearchDialog()
64 : {
65 0 : disposeOnce();
66 0 : }
67 :
68 0 : void SearchDialog::dispose()
69 : {
70 0 : SaveConfig();
71 0 : m_aCloseHdl.Call( NULL );
72 0 : m_pSearchEdit.clear();
73 0 : m_pWholeWordsBox.clear();
74 0 : m_pMatchCaseBox.clear();
75 0 : m_pWrapAroundBox.clear();
76 0 : m_pBackwardsBox.clear();
77 0 : m_pFindBtn.clear();
78 0 : ModelessDialog::dispose();
79 0 : }
80 :
81 0 : void SearchDialog::LoadConfig()
82 : {
83 0 : SvtViewOptions aViewOpt( E_DIALOG, m_sConfigName );
84 0 : if ( aViewOpt.Exists() )
85 : {
86 0 : m_sWinState = OUStringToOString(aViewOpt.GetWindowState(), RTL_TEXTENCODING_ASCII_US);
87 0 : Any aUserItem = aViewOpt.GetUserItem( "UserItem" );
88 0 : OUString aTemp;
89 0 : if ( aUserItem >>= aTemp )
90 : {
91 0 : OUString sUserData( aTemp );
92 : DBG_ASSERT( comphelper::string::getTokenCount(sUserData, ';') == 5, "invalid config data" );
93 0 : sal_Int32 nIdx = 0;
94 0 : OUString sSearchText = sUserData.getToken( 0, ';', nIdx );
95 0 : m_pWholeWordsBox->Check( sUserData.getToken( 0, ';', nIdx ).toInt32() == 1 );
96 0 : m_pMatchCaseBox->Check( sUserData.getToken( 0, ';', nIdx ).toInt32() == 1 );
97 0 : m_pWrapAroundBox->Check( sUserData.getToken( 0, ';', nIdx ).toInt32() == 1 );
98 0 : m_pBackwardsBox->Check( sUserData.getToken( 0, ';', nIdx ).toInt32() == 1 );
99 :
100 0 : nIdx = 0;
101 0 : while ( nIdx != -1 )
102 0 : m_pSearchEdit->InsertEntry( sSearchText.getToken( 0, '\t', nIdx ) );
103 0 : m_pSearchEdit->SelectEntryPos(0);
104 0 : }
105 : }
106 : else
107 0 : m_pWrapAroundBox->Check( true );
108 0 : }
109 :
110 0 : void SearchDialog::SaveConfig()
111 : {
112 0 : SvtViewOptions aViewOpt( E_DIALOG, m_sConfigName );
113 0 : aViewOpt.SetWindowState(OStringToOUString(m_sWinState, RTL_TEXTENCODING_ASCII_US));
114 0 : OUString sUserData;
115 0 : sal_Int32 i = 0, nCount = std::min( m_pSearchEdit->GetEntryCount(), static_cast<sal_Int32>(MAX_SAVE_COUNT) );
116 0 : for ( ; i < nCount; ++i )
117 : {
118 0 : sUserData += m_pSearchEdit->GetEntry(i);
119 0 : sUserData += "\t";
120 : }
121 0 : sUserData = comphelper::string::stripStart(sUserData, '\t');
122 0 : sUserData += ";";
123 0 : sUserData += OUString::number( m_pWholeWordsBox->IsChecked() ? 1 : 0 );
124 0 : sUserData += ";";
125 0 : sUserData += OUString::number( m_pMatchCaseBox->IsChecked() ? 1 : 0 );
126 0 : sUserData += ";";
127 0 : sUserData += OUString::number( m_pWrapAroundBox->IsChecked() ? 1 : 0 );
128 0 : sUserData += ";";
129 0 : sUserData += OUString::number( m_pBackwardsBox->IsChecked() ? 1 : 0 );
130 :
131 0 : Any aUserItem = makeAny( OUString( sUserData ) );
132 0 : aViewOpt.SetUserItem( "UserItem", aUserItem );
133 0 : }
134 :
135 0 : IMPL_LINK_NOARG(SearchDialog, FindHdl)
136 : {
137 0 : OUString sSrchTxt = m_pSearchEdit->GetText();
138 0 : sal_Int32 nPos = m_pSearchEdit->GetEntryPos( sSrchTxt );
139 0 : if ( nPos > 0 && nPos != COMBOBOX_ENTRY_NOTFOUND )
140 0 : m_pSearchEdit->RemoveEntryAt(nPos);
141 0 : if ( nPos > 0 )
142 0 : m_pSearchEdit->InsertEntry( sSrchTxt, 0 );
143 0 : m_aFindHdl.Call( this );
144 0 : return 0;
145 : }
146 :
147 0 : void SearchDialog::SetFocusOnEdit()
148 : {
149 0 : Selection aSelection( 0, m_pSearchEdit->GetText().getLength() );
150 0 : m_pSearchEdit->SetSelection( aSelection );
151 0 : m_pSearchEdit->GrabFocus();
152 0 : }
153 :
154 0 : bool SearchDialog::Close()
155 : {
156 0 : bool bRet = ModelessDialog::Close();
157 0 : m_aCloseHdl.Call( this );
158 0 : return bRet;
159 : }
160 :
161 0 : void SearchDialog::StateChanged( StateChangedType nStateChange )
162 : {
163 0 : if ( nStateChange == StateChangedType::InitShow )
164 : {
165 0 : if (!m_sWinState.isEmpty())
166 0 : SetWindowState( m_sWinState );
167 0 : m_bIsConstructed = true;
168 : }
169 :
170 0 : ModelessDialog::StateChanged( nStateChange );
171 0 : }
172 :
173 0 : void SearchDialog::Move()
174 : {
175 0 : ModelessDialog::Move();
176 0 : if ( m_bIsConstructed && IsReallyVisible() )
177 0 : m_sWinState = GetWindowState( WINDOWSTATE_MASK_POS | WINDOWSTATE_MASK_STATE );
178 0 : }
179 :
180 :
181 :
182 648 : } // namespace sfx2
183 :
184 :
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|