Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <comphelper/string.hxx>
30 : : #include <sfx2/request.hxx>
31 : : #include <svl/stritem.hxx>
32 : : #include <vcl/msgbox.hxx>
33 : :
34 : :
35 : : #include "view.hxx"
36 : : #include "basesh.hxx"
37 : : #include "wrtsh.hxx" //
38 : : #include "cmdid.h"
39 : : #include "bookmark.hxx" // SwInsertBookmarkDlg
40 : : #include "IMark.hxx"
41 : : #include "bookmark.hrc"
42 : : #include "misc.hrc"
43 : :
44 : 0 : const String BookmarkCombo::aForbiddenChars = rtl::OUString("/\\@:*?\";,.#");
45 : :
46 : 0 : IMPL_LINK( SwInsertBookmarkDlg, ModifyHdl, BookmarkCombo *, pBox )
47 : : {
48 : 0 : sal_Bool bSelEntries = pBox->GetSelectEntryCount() != 0;
49 : : // if a string has been pasted from the clipboard then
50 : : // there may be illegal characters in the box
51 : 0 : if(!bSelEntries)
52 : : {
53 : 0 : String sTmp = pBox->GetText();
54 : 0 : sal_uInt16 nLen = sTmp.Len();
55 : 0 : String sMsg;
56 : 0 : for(sal_uInt16 i = 0; i < BookmarkCombo::aForbiddenChars.Len(); i++)
57 : : {
58 : 0 : sal_uInt16 nTmpLen = sTmp.Len();
59 : 0 : sTmp = comphelper::string::remove(sTmp, BookmarkCombo::aForbiddenChars.GetChar(i));
60 : 0 : if(sTmp.Len() != nTmpLen)
61 : 0 : sMsg += BookmarkCombo::aForbiddenChars.GetChar(i);
62 : : }
63 : 0 : if(sTmp.Len() != nLen)
64 : : {
65 : 0 : pBox->SetText(sTmp);
66 : 0 : String sWarning(sRemoveWarning);
67 : 0 : sWarning += sMsg;
68 : 0 : InfoBox(this, sWarning).Execute();
69 : 0 : }
70 : :
71 : :
72 : : }
73 : :
74 : 0 : aOkBtn.Enable(!bSelEntries); // new text mark
75 : 0 : aDeleteBtn.Enable(bSelEntries); // deletable?
76 : :
77 : 0 : return 0;
78 : : }
79 : :
80 : : /*------------------------------------------------------------------------
81 : : Description: callback to delete a text mark
82 : : -----------------------------------------------------------------------*/
83 : 0 : IMPL_LINK_NOARG(SwInsertBookmarkDlg, DeleteHdl)
84 : : {
85 : : // remove text marks from the ComboBox
86 : :
87 : 0 : for (sal_uInt16 i = aBookmarkBox.GetSelectEntryCount(); i; i-- )
88 : 0 : aBookmarkBox.RemoveEntry(aBookmarkBox.GetSelectEntryPos(i - 1));
89 : :
90 : 0 : aBookmarkBox.SetText(aEmptyStr);
91 : 0 : aDeleteBtn.Enable(sal_False); // no further entries there
92 : :
93 : 0 : aOkBtn.Enable(); // the OK handler deletes
94 : 0 : return 0;
95 : : }
96 : :
97 : : /*------------------------------------------------------------------------
98 : : Description: callback for OKButton. Inserts a new text mark to the
99 : : current position. Deleted text marks are also deleted in the model.
100 : : -----------------------------------------------------------------------*/
101 : 0 : void SwInsertBookmarkDlg::Apply()
102 : : {
103 : : //at first remove deleted bookmarks to prevent multiple bookmarks with the same
104 : : //name
105 : 0 : for (sal_uInt16 nCount = aBookmarkBox.GetRemovedCount(); nCount > 0; nCount--)
106 : : {
107 : 0 : String sRemoved = aBookmarkBox.GetRemovedEntry( nCount -1 ).GetName();
108 : 0 : IDocumentMarkAccess* const pMarkAccess = rSh.getIDocumentMarkAccess();
109 : 0 : pMarkAccess->deleteMark( pMarkAccess->findMark(sRemoved) );
110 : 0 : SfxRequest aReq( rSh.GetView().GetViewFrame(), FN_DELETE_BOOKMARK );
111 : 0 : aReq.AppendItem( SfxStringItem( FN_DELETE_BOOKMARK, sRemoved ) );
112 : 0 : aReq.Done();
113 : 0 : }
114 : :
115 : : // insert text mark
116 : 0 : sal_uInt16 nLen = aBookmarkBox.GetText().Len();
117 : 0 : SwBoxEntry aTmpEntry(aBookmarkBox.GetText(), 0 );
118 : :
119 : 0 : if ( nLen && (aBookmarkBox.GetEntryPos(aTmpEntry) == COMBOBOX_ENTRY_NOTFOUND) )
120 : : {
121 : : String sEntry(comphelper::string::remove(aBookmarkBox.GetText(),
122 : 0 : aBookmarkBox.GetMultiSelectionSeparator()));
123 : :
124 : 0 : rSh.SetBookmark( KeyCode(), sEntry, aEmptyStr );
125 : 0 : rReq.AppendItem( SfxStringItem( FN_INSERT_BOOKMARK, sEntry ) );
126 : 0 : rReq.Done();
127 : : }
128 : :
129 : 0 : if ( !rReq.IsDone() )
130 : 0 : rReq.Ignore();
131 : :
132 : 0 : }
133 : :
134 : : /*------------------------------------------------------------------------
135 : : Description: CTOR
136 : : -----------------------------------------------------------------------*/
137 : 0 : SwInsertBookmarkDlg::SwInsertBookmarkDlg( Window *pParent, SwWrtShell &rS, SfxRequest& rRequest ) :
138 : :
139 : : SvxStandardDialog(pParent,SW_RES(DLG_INSERT_BOOKMARK)),
140 : : aBookmarkFl(this,SW_RES(FL_BOOKMARK)),
141 : : aBookmarkBox(this,SW_RES(CB_BOOKMARK)),
142 : : aOkBtn(this,SW_RES(BT_OK)),
143 : : aCancelBtn(this,SW_RES(BT_CANCEL)),
144 : : aDeleteBtn(this,SW_RES(BT_DELETE)),
145 : : rSh( rS ),
146 : 0 : rReq( rRequest )
147 : : {
148 : 0 : aBookmarkBox.SetModifyHdl(LINK(this, SwInsertBookmarkDlg, ModifyHdl));
149 : 0 : aBookmarkBox.EnableMultiSelection(sal_True);
150 : 0 : aBookmarkBox.EnableAutocomplete( sal_True, sal_True );
151 : :
152 : 0 : aDeleteBtn.SetClickHdl(LINK(this, SwInsertBookmarkDlg, DeleteHdl));
153 : :
154 : : // fill Combobox with existing bookmarks
155 : 0 : IDocumentMarkAccess* const pMarkAccess = rSh.getIDocumentMarkAccess();
156 : 0 : sal_uInt16 nId = 0;
157 : 0 : for( IDocumentMarkAccess::const_iterator_t ppBookmark = pMarkAccess->getBookmarksBegin();
158 : 0 : ppBookmark != pMarkAccess->getBookmarksEnd();
159 : : ppBookmark++)
160 : : {
161 : 0 : if(IDocumentMarkAccess::BOOKMARK == IDocumentMarkAccess::GetType(**ppBookmark))
162 : 0 : aBookmarkBox.InsertEntry( SwBoxEntry( ppBookmark->get()->GetName(), nId++ ) );
163 : : }
164 : 0 : FreeResource();
165 : 0 : sRemoveWarning = String(SW_RES(STR_REMOVE_WARNING));
166 : 0 : }
167 : :
168 : 0 : SwInsertBookmarkDlg::~SwInsertBookmarkDlg()
169 : : {
170 : 0 : }
171 : :
172 : 0 : BookmarkCombo::BookmarkCombo( Window* pWin, const ResId& rResId ) :
173 : 0 : SwComboBox(pWin, rResId)
174 : : {
175 : 0 : }
176 : :
177 : 0 : sal_uInt16 BookmarkCombo::GetFirstSelEntryPos() const
178 : : {
179 : 0 : return GetSelEntryPos(0);
180 : : }
181 : :
182 : 0 : sal_uInt16 BookmarkCombo::GetNextSelEntryPos(sal_uInt16 nPos) const
183 : : {
184 : 0 : return GetSelEntryPos(nPos + 1);
185 : : }
186 : :
187 : 0 : sal_uInt16 BookmarkCombo::GetSelEntryPos(sal_uInt16 nPos) const
188 : : {
189 : 0 : sal_Unicode cSep = GetMultiSelectionSeparator();
190 : :
191 : 0 : sal_uInt16 nCnt = comphelper::string::getTokenCount(GetText(), cSep);
192 : :
193 : 0 : for (; nPos < nCnt; nPos++)
194 : : {
195 : 0 : String sEntry(comphelper::string::strip(GetText().GetToken(nPos, cSep), ' '));
196 : 0 : if (GetEntryPos(sEntry) != COMBOBOX_ENTRY_NOTFOUND)
197 : 0 : return nPos;
198 : 0 : }
199 : :
200 : 0 : return COMBOBOX_ENTRY_NOTFOUND;
201 : : }
202 : :
203 : 0 : sal_uInt16 BookmarkCombo::GetSelectEntryCount() const
204 : : {
205 : 0 : sal_uInt16 nCnt = 0;
206 : :
207 : 0 : sal_uInt16 nPos = GetFirstSelEntryPos();
208 : 0 : while (nPos != COMBOBOX_ENTRY_NOTFOUND)
209 : : {
210 : 0 : nPos = GetNextSelEntryPos(nPos);
211 : 0 : nCnt++;
212 : : }
213 : :
214 : 0 : return nCnt;
215 : : }
216 : :
217 : : /*------------------------------------------------------------------------
218 : : Description: position inside of the listbox (the ComboBox)
219 : : -----------------------------------------------------------------------*/
220 : 0 : sal_uInt16 BookmarkCombo::GetSelectEntryPos( sal_uInt16 nSelIndex ) const
221 : : {
222 : 0 : sal_uInt16 nCnt = 0;
223 : 0 : sal_uInt16 nPos = GetFirstSelEntryPos();
224 : :
225 : 0 : while (nPos != COMBOBOX_ENTRY_NOTFOUND)
226 : : {
227 : 0 : if (nSelIndex == nCnt)
228 : : {
229 : 0 : sal_Unicode cSep = GetMultiSelectionSeparator();
230 : 0 : String sEntry(comphelper::string::strip(GetText().GetToken(nPos, cSep), ' '));
231 : 0 : return GetEntryPos(sEntry);
232 : : }
233 : 0 : nPos = GetNextSelEntryPos(nPos);
234 : 0 : nCnt++;
235 : : }
236 : :
237 : 0 : return COMBOBOX_ENTRY_NOTFOUND;
238 : : }
239 : :
240 : 0 : long BookmarkCombo::PreNotify( NotifyEvent& rNEvt )
241 : : {
242 : 0 : long nHandled = 0;
243 : 0 : if( EVENT_KEYINPUT == rNEvt.GetType() &&
244 : 0 : rNEvt.GetKeyEvent()->GetCharCode() )
245 : : {
246 : 0 : rtl::OUString sKey( rNEvt.GetKeyEvent()->GetCharCode() );
247 : 0 : if(STRING_NOTFOUND != aForbiddenChars.Search(sKey))
248 : 0 : nHandled = 1;
249 : : }
250 : 0 : if(!nHandled)
251 : 0 : nHandled = SwComboBox::PreNotify( rNEvt );
252 : 0 : return nHandled;
253 : 0 : }
254 : :
255 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|