1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/string.hxx>
21 #include <sfx2/request.hxx>
22 #include <svl/stritem.hxx>
23 #include <vcl/msgbox.hxx>
28 #include "wrtsh.hxx" //
30 #include "bookmark.hxx" // SwInsertBookmarkDlg
32 #include "globals.hrc"
34 const String
BookmarkCombo::aForbiddenChars
= OUString("/\\@:*?\";,.#");
36 IMPL_LINK( SwInsertBookmarkDlg
, ModifyHdl
, BookmarkCombo
*, pBox
)
38 sal_Bool bSelEntries
= pBox
->GetSelectEntryCount() != 0;
39 // if a string has been pasted from the clipboard then
40 // there may be illegal characters in the box
43 String sTmp
= pBox
->GetText();
44 sal_uInt16 nLen
= sTmp
.Len();
46 for(sal_uInt16 i
= 0; i
< BookmarkCombo::aForbiddenChars
.Len(); i
++)
48 sal_uInt16 nTmpLen
= sTmp
.Len();
49 sTmp
= comphelper::string::remove(sTmp
, BookmarkCombo::aForbiddenChars
.GetChar(i
));
50 if(sTmp
.Len() != nTmpLen
)
51 sMsg
+= BookmarkCombo::aForbiddenChars
.GetChar(i
);
53 if(sTmp
.Len() != nLen
)
56 String
sWarning(sRemoveWarning
);
58 InfoBox(this, sWarning
).Execute();
64 m_pOkBtn
->Enable(!bSelEntries
); // new text mark
65 m_pDeleteBtn
->Enable(bSelEntries
); // deletable?
70 /*------------------------------------------------------------------------
71 Description: callback to delete a text mark
72 -----------------------------------------------------------------------*/
73 IMPL_LINK_NOARG(SwInsertBookmarkDlg
, DeleteHdl
)
75 // remove text marks from the ComboBox
77 for (sal_uInt16 i
= m_pBookmarkBox
->GetSelectEntryCount(); i
; i
-- )
78 m_pBookmarkBox
->RemoveEntry(m_pBookmarkBox
->GetSelectEntryPos(i
- 1));
80 m_pBookmarkBox
->SetText(aEmptyStr
);
81 m_pDeleteBtn
->Enable(sal_False
); // no further entries there
83 m_pOkBtn
->Enable(); // the OK handler deletes
87 /*------------------------------------------------------------------------
88 Description: callback for OKButton. Inserts a new text mark to the
89 current position. Deleted text marks are also deleted in the model.
90 -----------------------------------------------------------------------*/
91 void SwInsertBookmarkDlg::Apply()
93 //at first remove deleted bookmarks to prevent multiple bookmarks with the same
95 for (sal_uInt16 nCount
= m_pBookmarkBox
->GetRemovedCount(); nCount
> 0; nCount
--)
97 String sRemoved
= m_pBookmarkBox
->GetRemovedEntry( nCount
-1 ).GetName();
98 IDocumentMarkAccess
* const pMarkAccess
= rSh
.getIDocumentMarkAccess();
99 pMarkAccess
->deleteMark( pMarkAccess
->findMark(sRemoved
) );
100 SfxRequest
aReq( rSh
.GetView().GetViewFrame(), FN_DELETE_BOOKMARK
);
101 aReq
.AppendItem( SfxStringItem( FN_DELETE_BOOKMARK
, sRemoved
) );
106 SwBoxEntry
aTmpEntry(m_pBookmarkBox
->GetText(), 0 );
108 if ( !m_pBookmarkBox
->GetText().isEmpty() && (m_pBookmarkBox
->GetEntryPos(aTmpEntry
) == COMBOBOX_ENTRY_NOTFOUND
) )
110 String
sEntry(comphelper::string::remove(m_pBookmarkBox
->GetText(),
111 m_pBookmarkBox
->GetMultiSelectionSeparator()));
113 rSh
.SetBookmark( KeyCode(), sEntry
, aEmptyStr
);
114 rReq
.AppendItem( SfxStringItem( FN_INSERT_BOOKMARK
, sEntry
) );
118 if ( !rReq
.IsDone() )
123 SwInsertBookmarkDlg::SwInsertBookmarkDlg( Window
*pParent
, SwWrtShell
&rS
, SfxRequest
& rRequest
) :
124 SvxStandardDialog(pParent
, "InsertBookmarkDialog", "modules/swriter/ui/insertbookmark.ui"),
128 get(m_pBookmarkBox
, "bookmarks");
130 get(m_pDeleteBtn
, "delete");
132 m_pBookmarkBox
->SetModifyHdl(LINK(this, SwInsertBookmarkDlg
, ModifyHdl
));
133 m_pBookmarkBox
->EnableMultiSelection(sal_True
);
134 m_pBookmarkBox
->EnableAutocomplete( sal_True
, sal_True
);
136 m_pDeleteBtn
->SetClickHdl(LINK(this, SwInsertBookmarkDlg
, DeleteHdl
));
138 // fill Combobox with existing bookmarks
139 IDocumentMarkAccess
* const pMarkAccess
= rSh
.getIDocumentMarkAccess();
141 for( IDocumentMarkAccess::const_iterator_t ppBookmark
= pMarkAccess
->getBookmarksBegin();
142 ppBookmark
!= pMarkAccess
->getBookmarksEnd();
145 if(IDocumentMarkAccess::BOOKMARK
== IDocumentMarkAccess::GetType(**ppBookmark
))
146 m_pBookmarkBox
->InsertEntry( SwBoxEntry( ppBookmark
->get()->GetName(), nId
++ ) );
149 sRemoveWarning
= String(SW_RES(STR_REMOVE_WARNING
));
152 SwInsertBookmarkDlg::~SwInsertBookmarkDlg()
156 BookmarkCombo::BookmarkCombo(Window
* pWin
)
161 sal_uInt16
BookmarkCombo::GetFirstSelEntryPos() const
163 return GetSelEntryPos(0);
166 sal_uInt16
BookmarkCombo::GetNextSelEntryPos(sal_uInt16 nPos
) const
168 return GetSelEntryPos(nPos
+ 1);
171 sal_uInt16
BookmarkCombo::GetSelEntryPos(sal_uInt16 nPos
) const
173 sal_Unicode cSep
= GetMultiSelectionSeparator();
175 sal_uInt16 nCnt
= comphelper::string::getTokenCount(GetText(), cSep
);
177 for (; nPos
< nCnt
; nPos
++)
179 OUString
sEntry(comphelper::string::strip(GetText().getToken(nPos
, cSep
), ' '));
180 if (GetEntryPos(sEntry
) != COMBOBOX_ENTRY_NOTFOUND
)
184 return COMBOBOX_ENTRY_NOTFOUND
;
187 sal_uInt16
BookmarkCombo::GetSelectEntryCount() const
191 sal_uInt16 nPos
= GetFirstSelEntryPos();
192 while (nPos
!= COMBOBOX_ENTRY_NOTFOUND
)
194 nPos
= GetNextSelEntryPos(nPos
);
201 /*------------------------------------------------------------------------
202 Description: position inside of the listbox (the ComboBox)
203 -----------------------------------------------------------------------*/
204 sal_uInt16
BookmarkCombo::GetSelectEntryPos( sal_uInt16 nSelIndex
) const
207 sal_uInt16 nPos
= GetFirstSelEntryPos();
209 while (nPos
!= COMBOBOX_ENTRY_NOTFOUND
)
211 if (nSelIndex
== nCnt
)
213 sal_Unicode cSep
= GetMultiSelectionSeparator();
214 OUString
sEntry(comphelper::string::strip(GetText().getToken(nPos
, cSep
), ' '));
215 return GetEntryPos(sEntry
);
217 nPos
= GetNextSelEntryPos(nPos
);
221 return COMBOBOX_ENTRY_NOTFOUND
;
224 long BookmarkCombo::PreNotify( NotifyEvent
& rNEvt
)
227 if( EVENT_KEYINPUT
== rNEvt
.GetType() &&
228 rNEvt
.GetKeyEvent()->GetCharCode() )
230 OUString
sKey( rNEvt
.GetKeyEvent()->GetCharCode() );
231 if(STRING_NOTFOUND
!= aForbiddenChars
.Search(sKey
))
235 nHandled
= SwComboBox::PreNotify( rNEvt
);
239 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeBookmarkCombo(Window
* pParent
)
241 return new BookmarkCombo(pParent
);
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */