Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <AnnotationWin.hxx>
21 :
22 : #include <AnnotationMenuButton.hxx>
23 : #include <PostItMgr.hxx>
24 :
25 : #include <annotation.hrc>
26 : #include <popup.hrc>
27 : #include <cmdid.h>
28 :
29 : #include <vcl/menu.hxx>
30 :
31 : #include <svl/undo.hxx>
32 : #include <unotools/syslocale.hxx>
33 : #include <svl/languageoptions.hxx>
34 :
35 : #include <editeng/postitem.hxx>
36 : #include <editeng/fhgtitem.hxx>
37 : #include <editeng/langitem.hxx>
38 :
39 : #include <editeng/editview.hxx>
40 : #include <editeng/outliner.hxx>
41 : #include <editeng/editeng.hxx>
42 : #include <editeng/editobj.hxx>
43 :
44 : #include <docufld.hxx>
45 : #include <txtfld.hxx>
46 : #include <ndtxt.hxx>
47 : #include <view.hxx>
48 : #include <wrtsh.hxx>
49 : #include <docsh.hxx>
50 : #include <doc.hxx>
51 : #include <IDocumentUndoRedo.hxx>
52 : #include <SwUndoField.hxx>
53 :
54 : #include <boost/scoped_ptr.hpp>
55 :
56 : namespace sw { namespace annotation {
57 :
58 84 : SwAnnotationWin::SwAnnotationWin( SwEditWin& rEditWin,
59 : WinBits nBits,
60 : SwPostItMgr& aMgr,
61 : SwPostItBits aBits,
62 : SwSidebarItem& rSidebarItem,
63 : SwFormatField* aField )
64 : : SwSidebarWin( rEditWin, nBits, aMgr, aBits, rSidebarItem )
65 : , mpFormatField(aField)
66 84 : , mpField( static_cast<SwPostItField*>(aField->GetField()))
67 168 : , mpButtonPopup(0)
68 : {
69 84 : }
70 :
71 252 : SwAnnotationWin::~SwAnnotationWin()
72 : {
73 84 : disposeOnce();
74 168 : }
75 :
76 84 : void SwAnnotationWin::dispose()
77 : {
78 84 : delete mpButtonPopup;
79 84 : sw::sidebarwindows::SwSidebarWin::dispose();
80 84 : }
81 :
82 84 : void SwAnnotationWin::SetPostItText()
83 : {
84 : //If the cursor was visible, then make it visible again after
85 : //changing text, e.g. fdo#33599
86 84 : vcl::Cursor *pCursor = GetOutlinerView()->GetEditView().GetCursor();
87 84 : bool bCursorVisible = pCursor && pCursor->IsVisible();
88 :
89 : //If the new text is the same as the old text, keep the same insertion
90 : //point .e.g. fdo#33599
91 84 : mpField = static_cast<SwPostItField*>(mpFormatField->GetField());
92 84 : OUString sNewText = mpField->GetPar2();
93 84 : bool bTextUnchanged = sNewText.equals(Engine()->GetEditEngine().GetText());
94 84 : ESelection aOrigSelection(GetOutlinerView()->GetEditView().GetSelection());
95 :
96 : // get text from SwPostItField and insert into our textview
97 84 : Engine()->SetModifyHdl( Link<>() );
98 84 : Engine()->EnableUndo( false );
99 84 : if( mpField->GetTextObject() )
100 71 : Engine()->SetText( *mpField->GetTextObject() );
101 : else
102 : {
103 13 : Engine()->Clear();
104 13 : GetOutlinerView()->SetAttribs(DefaultItem());
105 13 : GetOutlinerView()->InsertText(sNewText,false);
106 : }
107 :
108 84 : Engine()->ClearModifyFlag();
109 84 : Engine()->GetUndoManager().Clear();
110 84 : Engine()->EnableUndo( true );
111 84 : Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
112 84 : if (bTextUnchanged)
113 0 : GetOutlinerView()->GetEditView().SetSelection(aOrigSelection);
114 84 : if (bCursorVisible)
115 84 : GetOutlinerView()->ShowCursor();
116 84 : Invalidate();
117 84 : }
118 :
119 0 : void SwAnnotationWin::UpdateData()
120 : {
121 0 : if ( Engine()->IsModified() )
122 : {
123 : IDocumentUndoRedo & rUndoRedo(
124 0 : DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
125 0 : boost::scoped_ptr<SwField> pOldField;
126 0 : if (rUndoRedo.DoesUndo())
127 : {
128 0 : pOldField.reset(mpField->Copy());
129 : }
130 0 : mpField->SetPar2(Engine()->GetEditEngine().GetText());
131 0 : mpField->SetTextObject(Engine()->CreateParaObject());
132 0 : if (rUndoRedo.DoesUndo())
133 : {
134 0 : SwTextField *const pTextField = mpFormatField->GetTextField();
135 0 : SwPosition aPosition( pTextField->GetTextNode() );
136 0 : aPosition.nContent = pTextField->GetStart();
137 : rUndoRedo.AppendUndo(
138 0 : new SwUndoFieldFromDoc(aPosition, *pOldField, *mpField, 0, true));
139 : }
140 : // so we get a new layout of notes (anchor position is still the same and we would otherwise not get one)
141 0 : Mgr().SetLayout();
142 : // #i98686# if we have several views, all notes should update their text
143 0 : mpFormatField->Broadcast(SwFormatFieldHint( 0, SwFormatFieldHintWhich::CHANGED));
144 0 : DocView().GetDocShell()->SetModified();
145 : }
146 0 : Engine()->ClearModifyFlag();
147 0 : Engine()->GetUndoManager().Clear();
148 0 : }
149 :
150 0 : void SwAnnotationWin::Delete()
151 : {
152 0 : if (DocView().GetWrtShellPtr()->GotoField(*mpFormatField))
153 : {
154 0 : SwSidebarWin::Delete();
155 : // we delete the field directly, the Mgr cleans up the PostIt by listening
156 0 : GrabFocusToDocument();
157 0 : DocView().GetWrtShellPtr()->DelRight();
158 : }
159 0 : }
160 :
161 0 : void SwAnnotationWin::GotoPos()
162 : {
163 0 : DocView().GetDocShell()->GetWrtShell()->GotoField(*mpFormatField);
164 0 : }
165 :
166 0 : sal_uInt32 SwAnnotationWin::MoveCaret()
167 : {
168 : // if this is an answer, do not skip over all following ones, but insert directly behind the current one
169 : // but when just leaving a note, skip all following ones as well to continue typing
170 0 : return Mgr().IsAnswer()
171 : ? 1
172 0 : : 1 + CountFollowing();
173 : }
174 :
175 : //returns true, if there is another note right before this note
176 763 : bool SwAnnotationWin::CalcFollow()
177 : {
178 763 : SwTextField* pTextField = mpFormatField->GetTextField();
179 763 : SwPosition aPosition( pTextField->GetTextNode() );
180 763 : aPosition.nContent = pTextField->GetStart();
181 : SwTextAttr * const pTextAttr =
182 763 : pTextField->GetTextNode().GetTextAttrForCharAt(
183 763 : aPosition.nContent.GetIndex() - 1,
184 1526 : RES_TXTATR_ANNOTATION );
185 763 : const SwField* pField = pTextAttr ? pTextAttr->GetFormatField().GetField() : 0;
186 763 : return pField && (pField->Which()== RES_POSTITFLD);
187 : }
188 :
189 : // counts how many SwPostItField we have right after the current one
190 0 : sal_uInt32 SwAnnotationWin::CountFollowing()
191 : {
192 0 : sal_uInt32 aCount = 1; // we start with 1, so we have to subtract one at the end again
193 0 : SwTextField* pTextField = mpFormatField->GetTextField();
194 0 : SwPosition aPosition( pTextField->GetTextNode() );
195 0 : aPosition.nContent = pTextField->GetStart();
196 :
197 0 : SwTextAttr * pTextAttr = pTextField->GetTextNode().GetTextAttrForCharAt(
198 0 : aPosition.nContent.GetIndex() + 1,
199 0 : RES_TXTATR_ANNOTATION );
200 : SwField* pField = pTextAttr
201 0 : ? const_cast<SwField*>(pTextAttr->GetFormatField().GetField())
202 0 : : 0;
203 0 : while ( pField && ( pField->Which()== RES_POSTITFLD ) )
204 : {
205 0 : aCount++;
206 0 : pTextAttr = pTextField->GetTextNode().GetTextAttrForCharAt(
207 0 : aPosition.nContent.GetIndex() + aCount,
208 0 : RES_TXTATR_ANNOTATION );
209 : pField = pTextAttr
210 0 : ? const_cast<SwField*>(pTextAttr->GetFormatField().GetField())
211 0 : : 0;
212 : }
213 0 : return aCount - 1;
214 : }
215 :
216 84 : VclPtr<MenuButton> SwAnnotationWin::CreateMenuButton()
217 : {
218 84 : mpButtonPopup = new PopupMenu(SW_RES(MN_ANNOTATION_BUTTON));
219 84 : OUString aText = mpButtonPopup->GetItemText( FN_DELETE_NOTE_AUTHOR );
220 168 : SwRewriter aRewriter;
221 84 : aRewriter.AddRule(UndoArg1,GetAuthor());
222 84 : aText = aRewriter.Apply(aText);
223 84 : mpButtonPopup->SetItemText(FN_DELETE_NOTE_AUTHOR,aText);
224 168 : VclPtrInstance<AnnotationMenuButton> pMenuButton( *this );
225 84 : pMenuButton->SetPopupMenu( mpButtonPopup );
226 84 : pMenuButton->Show();
227 168 : return pMenuButton;
228 : }
229 :
230 0 : void SwAnnotationWin::InitAnswer(OutlinerParaObject* pText)
231 : {
232 : //collect our old meta data
233 0 : SwSidebarWin* pWin = Mgr().GetNextPostIt(KEY_PAGEUP, this);
234 0 : const SvtSysLocale aSysLocale;
235 0 : const LocaleDataWrapper& rLocalData = aSysLocale.GetLocaleData();
236 0 : SwRewriter aRewriter;
237 0 : aRewriter.AddRule(UndoArg1, pWin->GetAuthor());
238 : const OUString aText = aRewriter.Apply(SW_RESSTR(STR_REPLY))
239 0 : + " (" + rLocalData.getDate( pWin->GetDate())
240 0 : + ", " + rLocalData.getTime( pWin->GetTime(), false)
241 0 : + "): \"";
242 0 : GetOutlinerView()->InsertText(aText,false);
243 :
244 : // insert old, selected text or "..."
245 : // TODO: iterate over all paragraphs, not only first one to find out if it is empty
246 0 : if (!pText->GetTextObject().GetText(0).isEmpty())
247 0 : GetOutlinerView()->GetEditView().InsertText(pText->GetTextObject());
248 : else
249 0 : GetOutlinerView()->InsertText(OUString("..."),false);
250 0 : GetOutlinerView()->InsertText(OUString("\"\n"),false);
251 :
252 0 : GetOutlinerView()->SetSelection(ESelection(0,0,EE_PARA_ALL,EE_TEXTPOS_ALL));
253 0 : SfxItemSet aAnswerSet( DocView().GetDocShell()->GetPool() );
254 0 : aAnswerSet.Put(SvxFontHeightItem(200,80,EE_CHAR_FONTHEIGHT));
255 0 : aAnswerSet.Put(SvxPostureItem(ITALIC_NORMAL,EE_CHAR_ITALIC));
256 0 : GetOutlinerView()->SetAttribs(aAnswerSet);
257 0 : GetOutlinerView()->SetSelection(ESelection(EE_PARA_MAX_COUNT,EE_TEXTPOS_MAX_COUNT,EE_PARA_MAX_COUNT,EE_TEXTPOS_MAX_COUNT));
258 :
259 : //remove all attributes and reset our standard ones
260 0 : GetOutlinerView()->GetEditView().RemoveAttribsKeepLanguages(true);
261 0 : GetOutlinerView()->SetAttribs(DefaultItem());
262 : // lets insert an undo step so the initial text can be easily deleted
263 : // but do not use UpdateData() directly, would set modified state again and reentrance into Mgr
264 0 : Engine()->SetModifyHdl( Link<>() );
265 : IDocumentUndoRedo & rUndoRedo(
266 0 : DocView().GetDocShell()->GetDoc()->GetIDocumentUndoRedo());
267 0 : boost::scoped_ptr<SwField> pOldField;
268 0 : if (rUndoRedo.DoesUndo())
269 : {
270 0 : pOldField.reset(mpField->Copy());
271 : }
272 0 : mpField->SetPar2(Engine()->GetEditEngine().GetText());
273 0 : mpField->SetTextObject(Engine()->CreateParaObject());
274 0 : if (rUndoRedo.DoesUndo())
275 : {
276 0 : SwTextField *const pTextField = mpFormatField->GetTextField();
277 0 : SwPosition aPosition( pTextField->GetTextNode() );
278 0 : aPosition.nContent = pTextField->GetStart();
279 : rUndoRedo.AppendUndo(
280 0 : new SwUndoFieldFromDoc(aPosition, *pOldField, *mpField, 0, true));
281 : }
282 0 : Engine()->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
283 0 : Engine()->ClearModifyFlag();
284 0 : Engine()->GetUndoManager().Clear();
285 0 : }
286 :
287 84 : SvxLanguageItem SwAnnotationWin::GetLanguage()
288 : {
289 : // set initial language for outliner
290 84 : SvtScriptType nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( mpField->GetLanguage() );
291 84 : sal_uInt16 nLangWhichId = 0;
292 84 : switch (nScriptType)
293 : {
294 84 : case SvtScriptType::LATIN : nLangWhichId = EE_CHAR_LANGUAGE ; break;
295 0 : case SvtScriptType::ASIAN : nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
296 0 : case SvtScriptType::COMPLEX : nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
297 : default: OSL_FAIL("GetLanguage: wrong script type");
298 : }
299 84 : return SvxLanguageItem(mpField->GetLanguage(),nLangWhichId);
300 : }
301 :
302 0 : bool SwAnnotationWin::IsProtected()
303 : {
304 0 : return SwSidebarWin::IsProtected() ||
305 0 : GetLayoutStatus() == SwPostItHelper::DELETED ||
306 0 : ( mpFormatField && mpFormatField->IsProtect() );
307 : }
308 :
309 252 : OUString SwAnnotationWin::GetAuthor()
310 : {
311 252 : return mpField->GetPar1();
312 : }
313 :
314 84 : Date SwAnnotationWin::GetDate()
315 : {
316 84 : return mpField->GetDate();
317 : }
318 :
319 161 : tools::Time SwAnnotationWin::GetTime()
320 : {
321 161 : return mpField->GetTime();
322 : }
323 :
324 177 : } } // end of namespace sw::annotation
325 :
326 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|