xref: /aoo42x/main/sw/source/ui/misc/bookmark.cxx (revision efeef26f)
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_sw.hxx"
26 
27 #ifdef SW_DLLIMPLEMENTATION
28 #undef SW_DLLIMPLEMENTATION
29 #endif
30 
31 
32 #include <sfx2/request.hxx>
33 #include <svl/stritem.hxx>
34 #include <vcl/msgbox.hxx>
35 
36 
37 #include "view.hxx"
38 #include "basesh.hxx"
39 #include "wrtsh.hxx"		//
40 #include "cmdid.h"
41 #include "bookmark.hxx"		// SwInsertBookmarkDlg
42 #include "IMark.hxx"
43 #include "bookmark.hrc"
44 #include "misc.hrc"
45 
46 
47 const String BookmarkCombo::aForbiddenChars = String::CreateFromAscii("/\\@:*?\";,.#");
48 
49 
IMPL_LINK(SwInsertBookmarkDlg,ModifyHdl,BookmarkCombo *,pBox)50 IMPL_LINK( SwInsertBookmarkDlg, ModifyHdl, BookmarkCombo *, pBox )
51 {
52 	sal_Bool bSelEntries = pBox->GetSelectEntryCount() != 0;
53 	// if a string has been pasted from the clipboard then
54 	// there may be illegal characters in the box
55 	if(!bSelEntries)
56 	{
57 		String sTmp = pBox->GetText();
58 		sal_uInt16 nLen = sTmp.Len();
59 		String sMsg;
60 		for(sal_uInt16 i = 0; i < BookmarkCombo::aForbiddenChars.Len(); i++)
61 		{
62 			sal_uInt16 nTmpLen = sTmp.Len();
63 			sTmp.EraseAllChars(BookmarkCombo::aForbiddenChars.GetChar(i));
64 			if(sTmp.Len() != nTmpLen)
65 				sMsg += BookmarkCombo::aForbiddenChars.GetChar(i);
66 		}
67 		if(sTmp.Len() != nLen)
68 		{
69 			pBox->SetText(sTmp);
70 			String sWarning(sRemoveWarning);
71 			sWarning += sMsg;
72 			InfoBox(this, sWarning).Execute();
73 		}
74 
75 
76 	}
77 
78 	aOkBtn.Enable(!bSelEntries);  	// neue Textmarke
79 	aDeleteBtn.Enable(bSelEntries);	// loeschbar?
80 
81 	return 0;
82 }
83 
84 /*------------------------------------------------------------------------
85 	 Beschreibung: Callback zum Loeschen einer Textmarke
86  -----------------------------------------------------------------------*/
87 
IMPL_LINK(SwInsertBookmarkDlg,DeleteHdl,Button *,EMPTYARG)88 IMPL_LINK( SwInsertBookmarkDlg, DeleteHdl, Button *, EMPTYARG )
89 {
90 	// Textmarken aus der ComboBox entfernen
91 
92 	for (sal_uInt16 i = aBookmarkBox.GetSelectEntryCount(); i; i-- )
93 		aBookmarkBox.RemoveEntry(aBookmarkBox.GetSelectEntryPos(i - 1));
94 
95 	aBookmarkBox.SetText(aEmptyStr);
96 	aDeleteBtn.Enable(sal_False);	// keine weiteren Eintraege vorhanden
97 	// aBookmarkBox.SetText(aEmptyStr);
98 
99 	aOkBtn.Enable();			// Im OK Handler wird geloescht
100 	return 0;
101 }
102 
103 /*------------------------------------------------------------------------
104 	 Beschreibung: Callback fuer OKButton. Fuegt eine neue Textmarke
105 	 an die akt. Position ein. Geloeschte Textmarken werden auch am Modell
106 	 entfernt.
107  -----------------------------------------------------------------------*/
108 
109 
Apply()110 void SwInsertBookmarkDlg::Apply()
111 {
112 	//at first remove deleted bookmarks to prevent multiple bookmarks with the same
113 	//name
114 	for (sal_uInt16 nCount = aBookmarkBox.GetRemovedCount(); nCount > 0; nCount--)
115 	{
116         String sRemoved = aBookmarkBox.GetRemovedEntry( nCount -1 ).GetName();
117         IDocumentMarkAccess* const pMarkAccess = rSh.getIDocumentMarkAccess();
118         pMarkAccess->deleteMark( pMarkAccess->findMark(sRemoved) );
119         SfxRequest aReq( rSh.GetView().GetViewFrame(), FN_DELETE_BOOKMARK );
120         aReq.AppendItem( SfxStringItem( FN_DELETE_BOOKMARK, sRemoved ) );
121         aReq.Done();
122 	}
123 
124 	// Textmarke einfuegen
125 	sal_uInt16		nLen = aBookmarkBox.GetText().Len();
126 	SwBoxEntry	aTmpEntry(aBookmarkBox.GetText(), 0 );
127 
128     if ( nLen && (aBookmarkBox.GetEntryPos(aTmpEntry) == COMBOBOX_ENTRY_NOTFOUND) )
129 	{
130 		String sEntry(aBookmarkBox.GetText());
131 		sEntry.EraseAllChars(aBookmarkBox.GetMultiSelectionSeparator());
132 
133 		rSh.SetBookmark( KeyCode(), sEntry, aEmptyStr );
134         rReq.AppendItem( SfxStringItem( FN_INSERT_BOOKMARK, sEntry ) );
135         rReq.Done();
136 	}
137 
138     if ( !rReq.IsDone() )
139         rReq.Ignore();
140 
141 }
142 
143 /*------------------------------------------------------------------------
144 	 Beschreibung: CTOR
145  -----------------------------------------------------------------------*/
146 
147 
SwInsertBookmarkDlg(Window * pParent,SwWrtShell & rS,SfxRequest & rRequest)148 SwInsertBookmarkDlg::SwInsertBookmarkDlg( Window *pParent, SwWrtShell &rS, SfxRequest& rRequest ) :
149 
150     SvxStandardDialog(pParent,SW_RES(DLG_INSERT_BOOKMARK)),
151     aBookmarkFl(this,SW_RES(FL_BOOKMARK)),
152     aBookmarkBox(this,SW_RES(CB_BOOKMARK)),
153     aOkBtn(this,SW_RES(BT_OK)),
154     aCancelBtn(this,SW_RES(BT_CANCEL)),
155     aDeleteBtn(this,SW_RES(BT_DELETE)),
156     rSh( rS ),
157     rReq( rRequest )
158 {
159     aBookmarkBox.SetModifyHdl(LINK(this, SwInsertBookmarkDlg, ModifyHdl));
160     aBookmarkBox.EnableMultiSelection(sal_True);
161     aBookmarkBox.EnableAutocomplete( sal_True, sal_True );
162 
163     aDeleteBtn.SetClickHdl(LINK(this, SwInsertBookmarkDlg, DeleteHdl));
164 
165     // Combobox mit vorhandenen Bookmarks fuellen
166     IDocumentMarkAccess* const pMarkAccess = rSh.getIDocumentMarkAccess();
167     sal_uInt16 nId = 0;
168     for( IDocumentMarkAccess::const_iterator_t ppBookmark = pMarkAccess->getBookmarksBegin();
169         ppBookmark != pMarkAccess->getBookmarksEnd();
170         ppBookmark++)
171     {
172         if(IDocumentMarkAccess::BOOKMARK == IDocumentMarkAccess::GetType(**ppBookmark))
173             aBookmarkBox.InsertEntry( SwBoxEntry( ppBookmark->get()->GetName(), nId++ ) );
174     }
175     FreeResource();
176     sRemoveWarning = String(SW_RES(STR_REMOVE_WARNING));
177 }
178 
179 /*------------------------------------------------------------------------
180 	 Beschreibung:
181  -----------------------------------------------------------------------*/
182 
~SwInsertBookmarkDlg()183 SwInsertBookmarkDlg::~SwInsertBookmarkDlg()
184 {
185 }
186 
187 /*------------------------------------------------------------------------
188 	 Beschreibung:
189  -----------------------------------------------------------------------*/
190 
BookmarkCombo(Window * pWin,const ResId & rResId)191 BookmarkCombo::BookmarkCombo( Window* pWin, const ResId& rResId ) :
192 	SwComboBox(pWin, rResId)
193 {
194 }
195 
196 /*------------------------------------------------------------------------
197 	 Beschreibung:
198  -----------------------------------------------------------------------*/
199 
GetFirstSelEntryPos() const200 sal_uInt16 BookmarkCombo::GetFirstSelEntryPos() const
201 {
202 	return GetSelEntryPos(0);
203 }
204 
205 /*------------------------------------------------------------------------
206 	 Beschreibung:
207  -----------------------------------------------------------------------*/
208 
GetNextSelEntryPos(sal_uInt16 nPos) const209 sal_uInt16 BookmarkCombo::GetNextSelEntryPos(sal_uInt16 nPos) const
210 {
211 	return GetSelEntryPos(nPos + 1);
212 }
213 
214 /*------------------------------------------------------------------------
215 	 Beschreibung:
216  -----------------------------------------------------------------------*/
217 
GetSelEntryPos(sal_uInt16 nPos) const218 sal_uInt16 BookmarkCombo::GetSelEntryPos(sal_uInt16 nPos) const
219 {
220     sal_Unicode cSep = GetMultiSelectionSeparator();
221 
222 	sal_uInt16 nCnt = GetText().GetTokenCount(cSep);
223 
224 	for (; nPos < nCnt; nPos++)
225 	{
226 		String sEntry(GetText().GetToken(nPos, cSep));
227 		sEntry.EraseLeadingChars();
228 		sEntry.EraseTrailingChars();
229 		if (GetEntryPos(sEntry) != COMBOBOX_ENTRY_NOTFOUND)
230 			return nPos;
231 	}
232 
233 	return COMBOBOX_ENTRY_NOTFOUND;
234 }
235 
236 /*------------------------------------------------------------------------
237 	 Beschreibung:
238  -----------------------------------------------------------------------*/
239 
GetSelectEntryCount() const240 sal_uInt16 BookmarkCombo::GetSelectEntryCount() const
241 {
242 	sal_uInt16 nCnt = 0;
243 
244 	sal_uInt16 nPos = GetFirstSelEntryPos();
245 	while (nPos != COMBOBOX_ENTRY_NOTFOUND)
246 	{
247 		nPos = GetNextSelEntryPos(nPos);
248 		nCnt++;
249 	}
250 
251 	return nCnt;
252 }
253 
254 /*------------------------------------------------------------------------
255 	 Beschreibung: Position in der Listbox (der ComboBox)
256  -----------------------------------------------------------------------*/
257 
GetSelectEntryPos(sal_uInt16 nSelIndex) const258 sal_uInt16 BookmarkCombo::GetSelectEntryPos( sal_uInt16 nSelIndex ) const
259 {
260 	sal_uInt16 nCnt = 0;
261 	sal_uInt16 nPos = GetFirstSelEntryPos();
262 
263 	while (nPos != COMBOBOX_ENTRY_NOTFOUND)
264 	{
265 		if (nSelIndex == nCnt)
266 		{
267             sal_Unicode cSep = GetMultiSelectionSeparator();
268 			String sEntry(GetText().GetToken(nPos, cSep));
269 			sEntry.EraseLeadingChars();
270 			sEntry.EraseTrailingChars();
271 
272 			return GetEntryPos(sEntry);
273 		}
274 		nPos = GetNextSelEntryPos(nPos);
275 		nCnt++;
276 	}
277 
278 	return COMBOBOX_ENTRY_NOTFOUND;
279 }
280 /* -----------------05.02.99 08:39-------------------
281  *
282  * --------------------------------------------------*/
PreNotify(NotifyEvent & rNEvt)283 long BookmarkCombo::PreNotify( NotifyEvent& rNEvt )
284 {
285 	long nHandled = 0;
286 	if( EVENT_KEYINPUT == rNEvt.GetType() &&
287 		 rNEvt.GetKeyEvent()->GetCharCode() )
288 	{
289 		String sKey( rNEvt.GetKeyEvent()->GetCharCode() );
290 		if(STRING_NOTFOUND != aForbiddenChars.Search(sKey))
291 			nHandled = 1;
292 	}
293 	if(!nHandled)
294 		nHandled = SwComboBox::PreNotify( rNEvt );
295 	return nHandled;
296 }
297 
298 
299 
300