xref: /aoo42x/main/sfx2/source/dialog/srchdlg.cxx (revision d119d52d)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "srchdlg.hxx"
28cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
29cdf0e10cSrcweir #include <sfx2/sfxuno.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "srchdlg.hrc"
32cdf0e10cSrcweir #include "dialog.hrc"
33cdf0e10cSrcweir #include <tools/debug.hxx>
34cdf0e10cSrcweir #include <unotools/viewoptions.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::com::sun::star::uno;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // ============================================================================
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace sfx2 {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define USERITEM_NAME		DEFINE_CONST_OUSTRING("UserItem")
43cdf0e10cSrcweir #define MAX_SAVE_COUNT		(sal_uInt16)10
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // ============================================================================
46cdf0e10cSrcweir // SearchDialog
47cdf0e10cSrcweir // ============================================================================
48cdf0e10cSrcweir 
SearchDialog(Window * pWindow,const::rtl::OUString & rConfigName)49cdf0e10cSrcweir SearchDialog::SearchDialog( Window* pWindow, const ::rtl::OUString& rConfigName ) :
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	ModelessDialog( pWindow, SfxResId( RID_DLG_SEARCH ) ),
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	m_aSearchLabel		( this, SfxResId( FT_SEARCH ) ),
54cdf0e10cSrcweir 	m_aSearchEdit		( this, SfxResId( ED_SEARCH ) ),
55cdf0e10cSrcweir 	m_aWholeWordsBox	( this, SfxResId( CB_WHOLEWORDS ) ),
56cdf0e10cSrcweir 	m_aMatchCaseBox		( this, SfxResId( CB_MATCHCASE ) ),
57cdf0e10cSrcweir 	m_aWrapAroundBox	( this, SfxResId( CB_WRAPAROUND ) ),
58cdf0e10cSrcweir 	m_aBackwardsBox		( this, SfxResId( CB_BACKWARDS ) ),
59cdf0e10cSrcweir 	m_aFindBtn			( this, SfxResId( PB_FIND ) ),
60cdf0e10cSrcweir 	m_aCancelBtn		( this, SfxResId( PB_CANCELFIND ) ),
61cdf0e10cSrcweir 	m_sToggleText		(		SfxResId( STR_TOGGLE ) ),
62cdf0e10cSrcweir 	m_sConfigName		( rConfigName ),
63cdf0e10cSrcweir 	m_bIsConstructed	( false )
64cdf0e10cSrcweir 
65cdf0e10cSrcweir {
66cdf0e10cSrcweir 	FreeResource();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	// set handler
69cdf0e10cSrcweir 	m_aFindBtn.SetClickHdl( LINK( this, SearchDialog, FindHdl ) );
70cdf0e10cSrcweir 	m_aBackwardsBox.SetClickHdl( LINK( this, SearchDialog, ToggleHdl ) );
71cdf0e10cSrcweir 	// load config: old search strings and the status of the check boxes
72cdf0e10cSrcweir 	LoadConfig();
73cdf0e10cSrcweir 	// we need to change the text of the WrapAround box, depends on the status of the Backwards box
74cdf0e10cSrcweir 	if ( m_aBackwardsBox.IsChecked() )
75cdf0e10cSrcweir 		ToggleHdl( &m_aBackwardsBox );
76cdf0e10cSrcweir 	// the search edit should have the focus
77cdf0e10cSrcweir 	m_aSearchEdit.GrabFocus();
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
~SearchDialog()80cdf0e10cSrcweir SearchDialog::~SearchDialog()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 	SaveConfig();
83cdf0e10cSrcweir     m_aCloseHdl.Call( NULL );
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
LoadConfig()86cdf0e10cSrcweir void SearchDialog::LoadConfig()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	SvtViewOptions aViewOpt( E_DIALOG, m_sConfigName );
89cdf0e10cSrcweir 	if ( aViewOpt.Exists() )
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir 		m_sWinState = ByteString( aViewOpt.GetWindowState().getStr(), RTL_TEXTENCODING_ASCII_US );
92cdf0e10cSrcweir 		Any aUserItem = aViewOpt.GetUserItem( USERITEM_NAME );
93cdf0e10cSrcweir 		::rtl::OUString aTemp;
94cdf0e10cSrcweir 		if ( aUserItem >>= aTemp )
95cdf0e10cSrcweir 		{
96cdf0e10cSrcweir 			String sUserData( aTemp );
97cdf0e10cSrcweir 			DBG_ASSERT( sUserData.GetTokenCount() == 5, "invalid config data" );
98cdf0e10cSrcweir 		    xub_StrLen nIdx = 0;
99cdf0e10cSrcweir 			String sSearchText = sUserData.GetToken( 0, ';', nIdx );
100cdf0e10cSrcweir 			m_aWholeWordsBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
101cdf0e10cSrcweir 			m_aMatchCaseBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
102cdf0e10cSrcweir 			m_aWrapAroundBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
103cdf0e10cSrcweir 			m_aBackwardsBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 			nIdx = 0;
106cdf0e10cSrcweir 			while ( nIdx != STRING_NOTFOUND )
107cdf0e10cSrcweir 				m_aSearchEdit.InsertEntry( sSearchText.GetToken( 0, '\t', nIdx ) );
108cdf0e10cSrcweir 			m_aSearchEdit.SelectEntryPos(0);
109cdf0e10cSrcweir 		}
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir 	else
112cdf0e10cSrcweir 		m_aWrapAroundBox.Check( sal_True );
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
SaveConfig()115cdf0e10cSrcweir void SearchDialog::SaveConfig()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir 	SvtViewOptions aViewOpt( E_DIALOG, m_sConfigName );
118cdf0e10cSrcweir 	aViewOpt.SetWindowState( rtl::OUString::createFromAscii( m_sWinState.GetBuffer() ) );
119cdf0e10cSrcweir 	String sUserData;
120cdf0e10cSrcweir 	sal_uInt16 i = 0, nCount = Min( m_aSearchEdit.GetEntryCount(), MAX_SAVE_COUNT );
121cdf0e10cSrcweir 	for ( ; i < nCount; ++i )
122cdf0e10cSrcweir 	{
123cdf0e10cSrcweir 		sUserData += m_aSearchEdit.GetEntry(i);
124cdf0e10cSrcweir 		sUserData += '\t';
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 	sUserData.EraseTrailingChars( '\t' );
127cdf0e10cSrcweir 	sUserData += ';';
128cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aWholeWordsBox.IsChecked() ? 1 : 0 );
129cdf0e10cSrcweir 	sUserData += ';';
130cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aMatchCaseBox.IsChecked() ? 1 : 0 );
131cdf0e10cSrcweir 	sUserData += ';';
132cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aWrapAroundBox.IsChecked() ? 1 : 0 );
133cdf0e10cSrcweir 	sUserData += ';';
134cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aBackwardsBox.IsChecked() ? 1 : 0 );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	Any aUserItem = makeAny( ::rtl::OUString( sUserData ) );
137cdf0e10cSrcweir 	aViewOpt.SetUserItem( USERITEM_NAME, aUserItem );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
IMPL_LINK(SearchDialog,FindHdl,PushButton *,EMPTYARG)140cdf0e10cSrcweir IMPL_LINK( SearchDialog, FindHdl, PushButton*, EMPTYARG )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	String sSrchTxt = m_aSearchEdit.GetText();
143cdf0e10cSrcweir 	sal_uInt16 nPos = m_aSearchEdit.GetEntryPos( sSrchTxt );
144cdf0e10cSrcweir 	if ( nPos > 0 && nPos != COMBOBOX_ENTRY_NOTFOUND )
145cdf0e10cSrcweir 		m_aSearchEdit.RemoveEntry( nPos );
146cdf0e10cSrcweir 	if ( nPos > 0 )
147cdf0e10cSrcweir 		m_aSearchEdit.InsertEntry( sSrchTxt, 0 );
148cdf0e10cSrcweir 	m_aFindHdl.Call( this );
149cdf0e10cSrcweir 	return 0;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
IMPL_LINK(SearchDialog,ToggleHdl,CheckBox *,EMPTYARG)152cdf0e10cSrcweir IMPL_LINK( SearchDialog, ToggleHdl, CheckBox*, EMPTYARG )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 	String sTemp = m_aWrapAroundBox.GetText();
155cdf0e10cSrcweir 	m_aWrapAroundBox.SetText( m_sToggleText );
156cdf0e10cSrcweir 	m_sToggleText = sTemp;
157cdf0e10cSrcweir 	return 0;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
SetFocusOnEdit()160cdf0e10cSrcweir void SearchDialog::SetFocusOnEdit()
161cdf0e10cSrcweir {
162cdf0e10cSrcweir 	Selection aSelection( 0, m_aSearchEdit.GetText().Len() );
163cdf0e10cSrcweir 	m_aSearchEdit.SetSelection( aSelection );
164cdf0e10cSrcweir 	m_aSearchEdit.GrabFocus();
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
Close()167cdf0e10cSrcweir sal_Bool SearchDialog::Close()
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	sal_Bool bRet = ModelessDialog::Close();
170cdf0e10cSrcweir     m_aCloseHdl.Call( this );
171cdf0e10cSrcweir 	return bRet;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
StateChanged(StateChangedType nStateChange)174cdf0e10cSrcweir void SearchDialog::StateChanged( StateChangedType nStateChange )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	if ( nStateChange == STATE_CHANGE_INITSHOW )
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         if ( m_sWinState.Len() )
179cdf0e10cSrcweir             SetWindowState( m_sWinState );
180cdf0e10cSrcweir         m_bIsConstructed = sal_True;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	ModelessDialog::StateChanged( nStateChange );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
Move()186cdf0e10cSrcweir void SearchDialog::Move()
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     ModelessDialog::Move();
189cdf0e10cSrcweir     if ( m_bIsConstructed && IsReallyVisible() )
190cdf0e10cSrcweir         m_sWinState = GetWindowState( WINDOWSTATE_MASK_POS | WINDOWSTATE_MASK_STATE );
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir // ============================================================================
194cdf0e10cSrcweir 
195cdf0e10cSrcweir } // namespace sfx2
196cdf0e10cSrcweir 
197cdf0e10cSrcweir // ============================================================================
198cdf0e10cSrcweir 
199