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