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>
23 #include "sfx2/sfxresid.hxx"
24 #include <sfx2/sfxuno.hxx>
26 #include "srchdlg.hrc"
28 #include <tools/debug.hxx>
29 #include <unotools/viewoptions.hxx>
31 using namespace ::com::sun::star::uno
;
33 // ============================================================================
37 #define MAX_SAVE_COUNT (sal_uInt16)10
39 // ============================================================================
41 // ============================================================================
43 SearchDialog::SearchDialog( Window
* pWindow
, const OUString
& rConfigName
) :
45 ModelessDialog( pWindow
, SfxResId( RID_DLG_SEARCH
) ),
47 m_aSearchLabel ( this, SfxResId( FT_SEARCH
) ),
48 m_aSearchEdit ( this, SfxResId( ED_SEARCH
) ),
49 m_aWholeWordsBox ( this, SfxResId( CB_WHOLEWORDS
) ),
50 m_aMatchCaseBox ( this, SfxResId( CB_MATCHCASE
) ),
51 m_aWrapAroundBox ( this, SfxResId( CB_WRAPAROUND
) ),
52 m_aBackwardsBox ( this, SfxResId( CB_BACKWARDS
) ),
53 m_aFindBtn ( this, SfxResId( PB_FIND
) ),
54 m_aCancelBtn ( this, SfxResId( PB_CANCELFIND
) ),
55 m_sToggleText ( SfxResId( STR_TOGGLE
).toString() ),
56 m_sConfigName ( rConfigName
),
57 m_bIsConstructed ( false )
63 m_aFindBtn
.SetClickHdl( LINK( this, SearchDialog
, FindHdl
) );
64 m_aBackwardsBox
.SetClickHdl( LINK( this, SearchDialog
, ToggleHdl
) );
65 // load config: old search strings and the status of the check boxes
67 // we need to change the text of the WrapAround box, depends on the status of the Backwards box
68 if ( m_aBackwardsBox
.IsChecked() )
69 ToggleHdl( &m_aBackwardsBox
);
70 // the search edit should have the focus
71 m_aSearchEdit
.GrabFocus();
74 SearchDialog::~SearchDialog()
77 m_aCloseHdl
.Call( NULL
);
80 void SearchDialog::LoadConfig()
82 SvtViewOptions
aViewOpt( E_DIALOG
, m_sConfigName
);
83 if ( aViewOpt
.Exists() )
85 m_sWinState
= OUStringToOString(aViewOpt
.GetWindowState(), RTL_TEXTENCODING_ASCII_US
);
86 Any aUserItem
= aViewOpt
.GetUserItem( "UserItem" );
88 if ( aUserItem
>>= aTemp
)
90 String
sUserData( aTemp
);
91 DBG_ASSERT( comphelper::string::getTokenCount(sUserData
, ';') == 5, "invalid config data" );
93 String sSearchText
= sUserData
.GetToken( 0, ';', nIdx
);
94 m_aWholeWordsBox
.Check( sUserData
.GetToken( 0, ';', nIdx
).ToInt32() == 1 );
95 m_aMatchCaseBox
.Check( sUserData
.GetToken( 0, ';', nIdx
).ToInt32() == 1 );
96 m_aWrapAroundBox
.Check( sUserData
.GetToken( 0, ';', nIdx
).ToInt32() == 1 );
97 m_aBackwardsBox
.Check( sUserData
.GetToken( 0, ';', nIdx
).ToInt32() == 1 );
101 m_aSearchEdit
.InsertEntry( sSearchText
.GetToken( 0, '\t', nIdx
) );
102 m_aSearchEdit
.SelectEntryPos(0);
106 m_aWrapAroundBox
.Check( sal_True
);
109 void SearchDialog::SaveConfig()
111 SvtViewOptions
aViewOpt( E_DIALOG
, m_sConfigName
);
112 aViewOpt
.SetWindowState(OStringToOUString(m_sWinState
, RTL_TEXTENCODING_ASCII_US
));
114 sal_uInt16 i
= 0, nCount
= std::min( m_aSearchEdit
.GetEntryCount(), MAX_SAVE_COUNT
);
115 for ( ; i
< nCount
; ++i
)
117 sUserData
+= m_aSearchEdit
.GetEntry(i
);
120 sUserData
= comphelper::string::stripStart(sUserData
, '\t');
122 sUserData
+= OUString::number( m_aWholeWordsBox
.IsChecked() ? 1 : 0 );
124 sUserData
+= OUString::number( m_aMatchCaseBox
.IsChecked() ? 1 : 0 );
126 sUserData
+= OUString::number( m_aWrapAroundBox
.IsChecked() ? 1 : 0 );
128 sUserData
+= OUString::number( m_aBackwardsBox
.IsChecked() ? 1 : 0 );
130 Any aUserItem
= makeAny( OUString( sUserData
) );
131 aViewOpt
.SetUserItem( "UserItem", aUserItem
);
134 IMPL_LINK_NOARG(SearchDialog
, FindHdl
)
136 String sSrchTxt
= m_aSearchEdit
.GetText();
137 sal_uInt16 nPos
= m_aSearchEdit
.GetEntryPos( sSrchTxt
);
138 if ( nPos
> 0 && nPos
!= COMBOBOX_ENTRY_NOTFOUND
)
139 m_aSearchEdit
.RemoveEntry( nPos
);
141 m_aSearchEdit
.InsertEntry( sSrchTxt
, 0 );
142 m_aFindHdl
.Call( this );
146 IMPL_LINK_NOARG(SearchDialog
, ToggleHdl
)
148 String sTemp
= m_aWrapAroundBox
.GetText();
149 m_aWrapAroundBox
.SetText( m_sToggleText
);
150 m_sToggleText
= sTemp
;
154 void SearchDialog::SetFocusOnEdit()
156 Selection
aSelection( 0, m_aSearchEdit
.GetText().getLength() );
157 m_aSearchEdit
.SetSelection( aSelection
);
158 m_aSearchEdit
.GrabFocus();
161 sal_Bool
SearchDialog::Close()
163 sal_Bool bRet
= ModelessDialog::Close();
164 m_aCloseHdl
.Call( this );
168 void SearchDialog::StateChanged( StateChangedType nStateChange
)
170 if ( nStateChange
== STATE_CHANGE_INITSHOW
)
172 if (!m_sWinState
.isEmpty())
173 SetWindowState( m_sWinState
);
174 m_bIsConstructed
= sal_True
;
177 ModelessDialog::StateChanged( nStateChange
);
180 void SearchDialog::Move()
182 ModelessDialog::Move();
183 if ( m_bIsConstructed
&& IsReallyVisible() )
184 m_sWinState
= GetWindowState( WINDOWSTATE_MASK_POS
| WINDOWSTATE_MASK_STATE
);
187 // ============================================================================
191 // ============================================================================
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */