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