xref: /aoo42x/main/sfx2/source/dialog/srchdlg.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "srchdlg.hxx"
32*cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
33*cdf0e10cSrcweir #include <sfx2/sfxuno.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "srchdlg.hrc"
36*cdf0e10cSrcweir #include "dialog.hrc"
37*cdf0e10cSrcweir #include <tools/debug.hxx>
38*cdf0e10cSrcweir #include <unotools/viewoptions.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // ============================================================================
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace sfx2 {
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #define USERITEM_NAME		DEFINE_CONST_OUSTRING("UserItem")
47*cdf0e10cSrcweir #define MAX_SAVE_COUNT		(sal_uInt16)10
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir // ============================================================================
50*cdf0e10cSrcweir // SearchDialog
51*cdf0e10cSrcweir // ============================================================================
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir SearchDialog::SearchDialog( Window* pWindow, const ::rtl::OUString& rConfigName ) :
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 	ModelessDialog( pWindow, SfxResId( RID_DLG_SEARCH ) ),
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	m_aSearchLabel		( this, SfxResId( FT_SEARCH ) ),
58*cdf0e10cSrcweir 	m_aSearchEdit		( this, SfxResId( ED_SEARCH ) ),
59*cdf0e10cSrcweir 	m_aWholeWordsBox	( this, SfxResId( CB_WHOLEWORDS ) ),
60*cdf0e10cSrcweir 	m_aMatchCaseBox		( this, SfxResId( CB_MATCHCASE ) ),
61*cdf0e10cSrcweir 	m_aWrapAroundBox	( this, SfxResId( CB_WRAPAROUND ) ),
62*cdf0e10cSrcweir 	m_aBackwardsBox		( this, SfxResId( CB_BACKWARDS ) ),
63*cdf0e10cSrcweir 	m_aFindBtn			( this, SfxResId( PB_FIND ) ),
64*cdf0e10cSrcweir 	m_aCancelBtn		( this, SfxResId( PB_CANCELFIND ) ),
65*cdf0e10cSrcweir 	m_sToggleText		(		SfxResId( STR_TOGGLE ) ),
66*cdf0e10cSrcweir 	m_sConfigName		( rConfigName ),
67*cdf0e10cSrcweir 	m_bIsConstructed	( false )
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir 	FreeResource();
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	// set handler
73*cdf0e10cSrcweir 	m_aFindBtn.SetClickHdl( LINK( this, SearchDialog, FindHdl ) );
74*cdf0e10cSrcweir 	m_aBackwardsBox.SetClickHdl( LINK( this, SearchDialog, ToggleHdl ) );
75*cdf0e10cSrcweir 	// load config: old search strings and the status of the check boxes
76*cdf0e10cSrcweir 	LoadConfig();
77*cdf0e10cSrcweir 	// we need to change the text of the WrapAround box, depends on the status of the Backwards box
78*cdf0e10cSrcweir 	if ( m_aBackwardsBox.IsChecked() )
79*cdf0e10cSrcweir 		ToggleHdl( &m_aBackwardsBox );
80*cdf0e10cSrcweir 	// the search edit should have the focus
81*cdf0e10cSrcweir 	m_aSearchEdit.GrabFocus();
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir SearchDialog::~SearchDialog()
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir 	SaveConfig();
87*cdf0e10cSrcweir     m_aCloseHdl.Call( NULL );
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir void SearchDialog::LoadConfig()
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	SvtViewOptions aViewOpt( E_DIALOG, m_sConfigName );
93*cdf0e10cSrcweir 	if ( aViewOpt.Exists() )
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 		m_sWinState = ByteString( aViewOpt.GetWindowState().getStr(), RTL_TEXTENCODING_ASCII_US );
96*cdf0e10cSrcweir 		Any aUserItem = aViewOpt.GetUserItem( USERITEM_NAME );
97*cdf0e10cSrcweir 		::rtl::OUString aTemp;
98*cdf0e10cSrcweir 		if ( aUserItem >>= aTemp )
99*cdf0e10cSrcweir 		{
100*cdf0e10cSrcweir 			String sUserData( aTemp );
101*cdf0e10cSrcweir 			DBG_ASSERT( sUserData.GetTokenCount() == 5, "invalid config data" );
102*cdf0e10cSrcweir 		    xub_StrLen nIdx = 0;
103*cdf0e10cSrcweir 			String sSearchText = sUserData.GetToken( 0, ';', nIdx );
104*cdf0e10cSrcweir 			m_aWholeWordsBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
105*cdf0e10cSrcweir 			m_aMatchCaseBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
106*cdf0e10cSrcweir 			m_aWrapAroundBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
107*cdf0e10cSrcweir 			m_aBackwardsBox.Check( sUserData.GetToken( 0, ';', nIdx ).ToInt32() == 1 );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 			nIdx = 0;
110*cdf0e10cSrcweir 			while ( nIdx != STRING_NOTFOUND )
111*cdf0e10cSrcweir 				m_aSearchEdit.InsertEntry( sSearchText.GetToken( 0, '\t', nIdx ) );
112*cdf0e10cSrcweir 			m_aSearchEdit.SelectEntryPos(0);
113*cdf0e10cSrcweir 		}
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir 	else
116*cdf0e10cSrcweir 		m_aWrapAroundBox.Check( sal_True );
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir void SearchDialog::SaveConfig()
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	SvtViewOptions aViewOpt( E_DIALOG, m_sConfigName );
122*cdf0e10cSrcweir 	aViewOpt.SetWindowState( rtl::OUString::createFromAscii( m_sWinState.GetBuffer() ) );
123*cdf0e10cSrcweir 	String sUserData;
124*cdf0e10cSrcweir 	sal_uInt16 i = 0, nCount = Min( m_aSearchEdit.GetEntryCount(), MAX_SAVE_COUNT );
125*cdf0e10cSrcweir 	for ( ; i < nCount; ++i )
126*cdf0e10cSrcweir 	{
127*cdf0e10cSrcweir 		sUserData += m_aSearchEdit.GetEntry(i);
128*cdf0e10cSrcweir 		sUserData += '\t';
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir 	sUserData.EraseTrailingChars( '\t' );
131*cdf0e10cSrcweir 	sUserData += ';';
132*cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aWholeWordsBox.IsChecked() ? 1 : 0 );
133*cdf0e10cSrcweir 	sUserData += ';';
134*cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aMatchCaseBox.IsChecked() ? 1 : 0 );
135*cdf0e10cSrcweir 	sUserData += ';';
136*cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aWrapAroundBox.IsChecked() ? 1 : 0 );
137*cdf0e10cSrcweir 	sUserData += ';';
138*cdf0e10cSrcweir 	sUserData += String::CreateFromInt32( m_aBackwardsBox.IsChecked() ? 1 : 0 );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	Any aUserItem = makeAny( ::rtl::OUString( sUserData ) );
141*cdf0e10cSrcweir 	aViewOpt.SetUserItem( USERITEM_NAME, aUserItem );
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir IMPL_LINK( SearchDialog, FindHdl, PushButton*, EMPTYARG )
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	String sSrchTxt = m_aSearchEdit.GetText();
147*cdf0e10cSrcweir 	sal_uInt16 nPos = m_aSearchEdit.GetEntryPos( sSrchTxt );
148*cdf0e10cSrcweir 	if ( nPos > 0 && nPos != COMBOBOX_ENTRY_NOTFOUND )
149*cdf0e10cSrcweir 		m_aSearchEdit.RemoveEntry( nPos );
150*cdf0e10cSrcweir 	if ( nPos > 0 )
151*cdf0e10cSrcweir 		m_aSearchEdit.InsertEntry( sSrchTxt, 0 );
152*cdf0e10cSrcweir 	m_aFindHdl.Call( this );
153*cdf0e10cSrcweir 	return 0;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir IMPL_LINK( SearchDialog, ToggleHdl, CheckBox*, EMPTYARG )
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	String sTemp = m_aWrapAroundBox.GetText();
159*cdf0e10cSrcweir 	m_aWrapAroundBox.SetText( m_sToggleText );
160*cdf0e10cSrcweir 	m_sToggleText = sTemp;
161*cdf0e10cSrcweir 	return 0;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir void SearchDialog::SetFocusOnEdit()
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir 	Selection aSelection( 0, m_aSearchEdit.GetText().Len() );
167*cdf0e10cSrcweir 	m_aSearchEdit.SetSelection( aSelection );
168*cdf0e10cSrcweir 	m_aSearchEdit.GrabFocus();
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir sal_Bool SearchDialog::Close()
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir 	sal_Bool bRet = ModelessDialog::Close();
174*cdf0e10cSrcweir     m_aCloseHdl.Call( this );
175*cdf0e10cSrcweir 	return bRet;
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir void SearchDialog::StateChanged( StateChangedType nStateChange )
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir 	if ( nStateChange == STATE_CHANGE_INITSHOW )
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         if ( m_sWinState.Len() )
183*cdf0e10cSrcweir             SetWindowState( m_sWinState );
184*cdf0e10cSrcweir         m_bIsConstructed = sal_True;
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	ModelessDialog::StateChanged( nStateChange );
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir void SearchDialog::Move()
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     ModelessDialog::Move();
193*cdf0e10cSrcweir     if ( m_bIsConstructed && IsReallyVisible() )
194*cdf0e10cSrcweir         m_sWinState = GetWindowState( WINDOWSTATE_MASK_POS | WINDOWSTATE_MASK_STATE );
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir // ============================================================================
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir } // namespace sfx2
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir // ============================================================================
202*cdf0e10cSrcweir 
203