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