LibreOffice Module sw (master) 1
AnnotationWin.cxx
Go to the documentation of this file.
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 <config_wasm_strip.h>
21
22#include <AnnotationWin.hxx>
23
24#include <PostItMgr.hxx>
25
26#include <strings.hrc>
27
28#include <uiobject.hxx>
29
30#include <vcl/svapp.hxx>
31#include <vcl/uitest/logger.hxx>
33
34#include <svl/undo.hxx>
38#include <osl/diagnose.h>
39
40#include <editeng/eeitem.hxx>
41#include <editeng/postitem.hxx>
42#include <editeng/fhgtitem.hxx>
43#include <editeng/langitem.hxx>
44
45#include <editeng/editview.hxx>
46#include <editeng/outliner.hxx>
47#include <editeng/editeng.hxx>
48#include <editeng/editobj.hxx>
49#include <editeng/outlobj.hxx>
50
51#include <comphelper/lok.hxx>
52#include <comphelper/random.hxx>
53#include <docufld.hxx>
54#include <txtfld.hxx>
55#include <ndtxt.hxx>
56#include <view.hxx>
57#include <viewopt.hxx>
58#include <wrtsh.hxx>
59#include <docsh.hxx>
60#include <doc.hxx>
61#include <IDocumentUndoRedo.hxx>
62#include <SwUndoField.hxx>
63#include <edtwin.hxx>
66#include "OverlayRanges.hxx"
67#include "SidebarTxtControl.hxx"
68#include "SidebarWinAcc.hxx"
69
70#include <memory>
71
72namespace{
73
74void collectUIInformation( const OUString& aevent , const OUString& aID )
75{
76 EventDescription aDescription;
77 aDescription.aID = aID;
78 aDescription.aParameters = {{"" , ""}};
79 aDescription.aAction = aevent;
80 aDescription.aParent = "MainWindow";
81 aDescription.aKeyWord = "SwEditWinUIObject";
82 UITestLogger::getInstance().logEvent(aDescription);
83}
84
85}
86
87namespace sw::annotation {
88
89// see AnnotationContents in sd for something similar
91 SwPostItMgr& aMgr,
92 SwSidebarItem& rSidebarItem,
93 SwFormatField* aField )
94 : InterimItemWindow(&rEditWin, "modules/swriter/ui/annotation.ui", "Annotation")
95 , mrMgr(aMgr)
96 , mrView(rEditWin.GetView())
97 , mnDeleteEventId(nullptr)
98 , meSidebarPosition(sw::sidebarwindows::SidebarPosition::NONE)
99 , mPageBorder(0)
100 , mbAnchorRectChanged(false)
101 , mbResolvedStateUpdated(false)
102 , mbMouseOver(false)
103 , mLayoutStatus(SwPostItHelper::INVISIBLE)
104 , mbReadonly(false)
105 , mbIsFollow(false)
106 , mrSidebarItem(rSidebarItem)
107 , mpAnchorFrame(rSidebarItem.maLayoutInfo.mpAnchorFrame)
108 , mpFormatField(aField)
109 , mpField( static_cast<SwPostItField*>(aField->GetField()))
110{
111 set_id("Comment"+OUString::number(mpField->GetPostItId()));
112
113 m_xContainer->connect_mouse_move(LINK(this, SwAnnotationWin, MouseMoveHdl));
114
116 if ( mpShadow )
117 {
118 mpShadow->setVisible(false);
119 }
120
121#if !ENABLE_WASM_STRIP_ACCESSIBILITY
124 *this );
125#endif
126
128 // When double-buffering, allow parents to paint on our area. That's
129 // necessary when parents paint the complete buffer.
130 SetParentClipMode(ParentClipMode::NoClip);
131}
132
134{
135 disposeOnce();
136}
137
139{
140#if !ENABLE_WASM_STRIP_ACCESSIBILITY
142 *this );
143#endif
144 Disable();
145
147 mxSidebarTextControl.reset();
148
149 mxMetadataAuthor.reset();
150 mxMetadataResolved.reset();
151 mxMetadataDate.reset();
152 mxVScrollbar.reset();
153
154 mpAnchor.reset();
155 mpShadow.reset();
156
157 mpTextRangeOverlay.reset();
158
159 mxMenuButton.reset();
160
161 if (mnDeleteEventId)
163
164 mpOutliner.reset();
165 mpOutlinerView.reset();
166
168}
169
171{
172 //If the cursor was visible, then make it visible again after
173 //changing text, e.g. fdo#33599
175 bool bCursorVisible = pCursor && pCursor->IsVisible();
176
177 //If the new text is the same as the old text, keep the same insertion
178 //point .e.g. fdo#33599
179 mpField = static_cast<SwPostItField*>(mpFormatField->GetField());
180 OUString sNewText = mpField->GetPar2();
181 bool bTextUnchanged = sNewText == mpOutliner->GetEditEngine().GetText();
182 ESelection aOrigSelection(GetOutlinerView()->GetEditView().GetSelection());
183
184 // get text from SwPostItField and insert into our textview
185 mpOutliner->SetModifyHdl( Link<LinkParamNone*,void>() );
186 mpOutliner->EnableUndo( false );
187 if( mpField->GetTextObject() )
188 mpOutliner->SetText( *mpField->GetTextObject() );
189 else
190 {
191 mpOutliner->Clear();
192 GetOutlinerView()->SetStyleSheet(SwResId(STR_POOLCOLL_COMMENT));
193 GetOutlinerView()->InsertText(sNewText);
194 }
195
196 mpOutliner->ClearModifyFlag();
197 mpOutliner->GetUndoManager().Clear();
198 mpOutliner->EnableUndo( true );
199 mpOutliner->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
200 if (bTextUnchanged)
201 GetOutlinerView()->GetEditView().SetSelection(aOrigSelection);
202 if (bCursorVisible)
204 Invalidate();
205}
206
208{
209 bool oldState = IsResolved();
210 static_cast<SwPostItField*>(mpFormatField->GetField())->SetResolved(resolved);
211 if (SwWrtShell* pWrtShell = mrView.GetWrtShellPtr())
212 {
213 const SwViewOption* pVOpt = pWrtShell->GetViewOptions();
215 }
216
217 mpTextRangeOverlay.reset();
218
219 if(IsResolved())
220 mxMetadataResolved->show();
221 else
222 mxMetadataResolved->hide();
223
224 if(IsResolved() != oldState)
226 UpdateData();
227 Invalidate();
228 collectUIInformation("SETRESOLVED",get_id());
229}
230
232{
234}
235
237{
238 auto pTop = GetTopReplyNote();
239 pTop->ToggleResolved();
242}
243
245{
246 auto pField = static_cast<SwPostItField*>(mpFormatField->GetField());
247 auto nParaId = pField->GetParaId();
248 if (nParaId == 0)
249 {
250 // The parent annotation does not have a paraId. This happens when the annotation was just
251 // created, and not imported. paraIds are regenerated upon export, thus this new paraId
252 // is only generated so that children annotations can refer to it
253 nParaId = CreateUniqueParaId();
254 pField->SetParaId(nParaId);
255 }
256 return nParaId;
257}
258
260{
261 return comphelper::rng::uniform_uint_distribution(0, std::numeric_limits<sal_uInt32>::max());
262}
263
265{
266 // Go to the top and delete each comment one by one
267 SwAnnotationWin* topNote = GetTopReplyNote();
268 for (SwAnnotationWin* current = topNote;;)
269 {
271 current->mnDeleteEventId = Application::PostUserEvent( LINK( current, SwAnnotationWin, DeleteHdl), nullptr, true );
272 if (!next || next->GetTopReplyNote() != topNote)
273 return;
274 current = next;
275 }
276}
277
279{
280 return static_cast<SwPostItField*>(mpFormatField->GetField())->GetResolved();
281}
282
284{
286 // then iterate downwards checking resolved status
287 SwAnnotationWin* topNote = GetTopReplyNote();
288 for (SwAnnotationWin* current = topNote;;)
289 {
290 if (!current->IsResolved())
291 return false;
292 current = mrMgr.GetNextPostIt(KEY_PAGEDOWN, current);
293 if (!current || current->GetTopReplyNote() != topNote)
294 return true;
295 }
296}
297
299{
300 if ( mpOutliner->IsModified() || mbResolvedStateUpdated )
301 {
302 IDocumentUndoRedo & rUndoRedo(
304 std::unique_ptr<SwField> pOldField;
305 if (rUndoRedo.DoesUndo())
306 {
307 pOldField = mpField->Copy();
308 }
309 mpField->SetPar2(mpOutliner->GetEditEngine().GetText());
310 mpField->SetTextObject(mpOutliner->CreateParaObject());
311 if (rUndoRedo.DoesUndo())
312 {
313 SwTextField *const pTextField = mpFormatField->GetTextField();
314 SwPosition aPosition( pTextField->GetTextNode(), pTextField->GetStart() );
315 rUndoRedo.AppendUndo(
316 std::make_unique<SwUndoFieldFromDoc>(aPosition, *pOldField, *mpField, true));
317 }
318 // so we get a new layout of notes (anchor position is still the same and we would otherwise not get one)
320 // #i98686# if we have several views, all notes should update their text
323 else
326 }
327 mpOutliner->ClearModifyFlag();
328 mpOutliner->GetUndoManager().Clear();
330}
331
333{
334 collectUIInformation("DELETE",get_id());
335 SwWrtShell* pWrtShell = mrView.GetWrtShellPtr();
336 if (!(pWrtShell && pWrtShell->GotoField(*mpFormatField)))
337 return;
338
339 if ( mrMgr.GetActiveSidebarWin() == this)
340 {
341 mrMgr.SetActiveSidebarWin(nullptr);
342 // if the note is empty, the previous line will send a delete event, but we are already there
343 if (mnDeleteEventId)
344 {
346 mnDeleteEventId = nullptr;
347 }
348 }
349 // we delete the field directly, the Mgr cleans up the PostIt by listening
351 pWrtShell->ClearMark();
352 pWrtShell->DelRight();
353}
354
356{
358}
359
361{
362 // if this is an answer, do not skip over all following ones, but insert directly behind the current one
363 // but when just leaving a note, skip all following ones as well to continue typing
364 return mrMgr.IsAnswer()
365 ? 1
366 : 1 + CountFollowing();
367}
368
369// returns a non-zero postit parent id, if exists, otherwise 0 for root comments
371{
372 SwTextField* pTextField = mpFormatField->GetTextField();
373 if (SwPosition aPosition(pTextField->GetTextNode(), pTextField->GetStart());
374 aPosition.GetContentIndex() > 0)
375 if (const SwTextAttr* pTextAttr = pTextField->GetTextNode().GetTextAttrForCharAt(
376 aPosition.GetContentIndex() - 1, RES_TXTATR_ANNOTATION))
377 if (const SwField* pField = pTextAttr->GetFormatField().GetField())
378 if (pField->Which() == SwFieldIds::Postit)
379 return static_cast<const SwPostItField*>(pField)->GetPostItId();
380 return 0;
381}
382
383// counts how many SwPostItField we have right after the current one
385{
386 SwTextField* pTextField = mpFormatField->GetTextField();
387 SwPosition aPosition( pTextField->GetTextNode(), pTextField->GetStart() );
388
389 for (sal_Int32 n = 1;; ++n)
390 {
391 SwTextAttr * pTextAttr = pTextField->GetTextNode().GetTextAttrForCharAt(
392 aPosition.GetContentIndex() + n,
394 if (!pTextAttr)
395 return n - 1;
396 const SwField* pField = pTextAttr->GetFormatField().GetField();
397 if (!pField || pField->Which() != SwFieldIds::Postit)
398 return n - 1;
399 }
400}
401
403{
404 // If tiled annotations is off in lok case, skip adding additional reply text.
406 return;
407
408 //collect our old meta data
410 if (!pWin)
411 return;
412
413 const SvtSysLocale aSysLocale;
414 const LocaleDataWrapper& rLocalData = aSysLocale.GetLocaleData();
415 SwRewriter aRewriter;
416 aRewriter.AddRule(UndoArg1, pWin->GetAuthor());
417 const OUString aText = aRewriter.Apply(SwResId(STR_REPLY))
418 + " (" + rLocalData.getDate( pWin->GetDate())
419 + ", " + rLocalData.getTime( pWin->GetTime(), false)
420 + "): \"";
421 GetOutlinerView()->InsertText(aText);
422
423 // insert old, selected text or "..."
424 // TODO: iterate over all paragraphs, not only first one to find out if it is empty
425 if (!rText.GetTextObject().GetText(0).isEmpty())
427 else
428 GetOutlinerView()->InsertText("...");
429 GetOutlinerView()->InsertText("\"\n");
430
432 SfxItemSet aAnswerSet( mrView.GetDocShell()->GetPool() );
433 aAnswerSet.Put(SvxFontHeightItem(200,80,EE_CHAR_FONTHEIGHT));
435 GetOutlinerView()->SetAttribs(aAnswerSet);
437
438 //remove all attributes and reset our standard ones
440 // lets insert an undo step so the initial text can be easily deleted
441 // but do not use UpdateData() directly, would set modified state again and reentrance into Mgr
442 mpOutliner->SetModifyHdl( Link<LinkParamNone*,void>() );
443 IDocumentUndoRedo & rUndoRedo(
445 std::unique_ptr<SwField> pOldField;
446 if (rUndoRedo.DoesUndo())
447 {
448 pOldField = mpField->Copy();
449 }
450 mpField->SetPar2(mpOutliner->GetEditEngine().GetText());
451 mpField->SetTextObject(mpOutliner->CreateParaObject());
452 if (rUndoRedo.DoesUndo())
453 {
454 SwTextField *const pTextField = mpFormatField->GetTextField();
455 SwPosition aPosition( pTextField->GetTextNode(), pTextField->GetStart() );
456 rUndoRedo.AppendUndo(
457 std::make_unique<SwUndoFieldFromDoc>(aPosition, *pOldField, *mpField, true));
458 }
459 mpOutliner->SetModifyHdl( LINK( this, SwAnnotationWin, ModifyHdl ) );
460 mpOutliner->ClearModifyFlag();
461 mpOutliner->GetUndoManager().Clear();
462}
463
464void SwAnnotationWin::UpdateText(const OUString& aText)
465{
466 mpOutliner->Clear();
467 GetOutlinerView()->InsertText(aText);
468 UpdateData();
469}
470
472{
473 return mbReadonly ||
476}
477
479{
480 return mpField->GetPar1();
481}
482
484{
485 return mpField->GetDate();
486}
487
489{
490 return mpField->GetTime();
491}
492
494{
496}
497
498} // end of namespace sw::annotation
499
500/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SwFrame * mpAnchorFrame
SlideSorterView & mrView
@ UndoArg1
Definition: SwRewriter.hxx:29
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
static std::unique_ptr< UIObject > create(vcl::Window *pWindow)
Definition: uiobject.cxx:210
virtual OUString GetText(sal_Int32 nPara) const=0
void SetSelection(const ESelection &rNewSel)
void InsertText(const OUString &rNew, bool bSelect=false, bool bLOKShowSelect=true)
vcl::Cursor * GetCursor() const
void RemoveAttribsKeepLanguages(bool bRemoveParaAttribs)
virtual void dispose() override
std::unique_ptr< weld::Container > m_xContainer
OUString getDate(const Date &rDate) const
OUString getTime(const tools::Time &rTime, bool bSec=true, bool b100Sec=false) const
const EditTextObject & GetTextObject() const
void SetStyleSheet(const OUString &rStyleName)
void SetSelection(const ESelection &)
void InsertText(const OUString &rNew, bool bSelect=false)
void SetAttribs(const SfxItemSet &)
EditView & GetEditView() const
void ShowCursor(bool bGotoCursor=true, bool bActivate=false)
void Broadcast(const SfxHint &rHint)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
SfxItemPool & GetPool() const
const LocaleDataWrapper & GetLocaleData() const
void ClearMark()
Definition: crsrsh.cxx:1225
SwWrtShell * GetWrtShell()
Access to the SwWrtShell belonging to SwView.
Definition: docsh.hxx:225
virtual void SetModified(bool=true) override
Definition: docsh2.cxx:1437
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:204
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:158
Window class for the Writer edit area, this is the one handling mouse and keyboard events and doing t...
Definition: edtwin.hxx:61
Base class of all fields.
Definition: fldbas.hxx:296
SwFieldIds Which() const
ResId.
Definition: fldbas.cxx:263
const SwField * GetField() const
Definition: fmtfld.hxx:131
bool IsProtect() const
Definition: atrfld.cxx:467
const SwTextField * GetTextField() const
Definition: fmtfld.hxx:149
sal_uInt32 GetPostItId() const
Definition: docufld.hxx:487
virtual OUString GetPar1() const override
Author.
Definition: docufld.cxx:1818
sal_uInt32 GetParaId() const
Definition: docufld.hxx:491
virtual std::unique_ptr< SwField > Copy() const override
Definition: docufld.cxx:1799
const OutlinerParaObject * GetTextObject() const
Definition: docufld.hxx:506
Date GetDate() const
Definition: docufld.hxx:485
tools::Time GetTime() const
Definition: docufld.hxx:486
void SetTextObject(std::optional< OutlinerParaObject > pText)
Definition: docufld.cxx:1842
virtual void SetPar2(const OUString &rStr) override
set the PostIt's text
Definition: docufld.cxx:1824
virtual OUString GetPar2() const override
Text.
Definition: docufld.cxx:1830
void SetLayout()
Definition: PostItMgr.hxx:201
void ConnectSidebarWinToFrame(const SwFrame &rFrame, const SwFormatField &rFormatField, sw::annotation::SwAnnotationWin &rSidebarWin)
Definition: PostItMgr.cxx:2424
OutlinerParaObject * IsAnswer()
Definition: PostItMgr.hxx:259
void LayoutPostIts()
Definition: PostItMgr.cxx:696
void SetActiveSidebarWin(sw::annotation::SwAnnotationWin *p)
Definition: PostItMgr.cxx:2200
sw::annotation::SwAnnotationWin * GetNextPostIt(sal_uInt16 aDirection, sw::annotation::SwAnnotationWin *aPostIt)
Definition: PostItMgr.cxx:1808
void DisconnectSidebarWinFromFrame(const SwFrame &rFrame, sw::annotation::SwAnnotationWin &rSidebarWin)
Definition: PostItMgr.cxx:2441
sw::annotation::SwAnnotationWin * GetActiveSidebarWin()
Definition: PostItMgr.hxx:237
void UpdateResolvedStatus(const sw::annotation::SwAnnotationWin *topNote)
Definition: PostItMgr.cxx:2505
void AddRule(SwUndoArg eWhat, const OUString &rWith)
Definition: SwRewriter.cxx:25
OUString Apply(const OUString &rStr) const
Definition: SwRewriter.cxx:39
SwLayoutInfo maLayoutInfo
virtual const SwFormatField & GetFormatField() const =0
A wrapper around SfxPoolItem to store the start position of (usually) a text portion,...
Definition: txatbase.hxx:44
sal_Int32 GetStart() const
Definition: txatbase.hxx:88
const SwFormatField & GetFormatField() const
Definition: txatbase.hxx:199
SwTextNode & GetTextNode() const
Definition: txtfld.hxx:53
SwTextAttr * GetTextAttrForCharAt(const sal_Int32 nIndex, const sal_uInt16 nWhich=RES_TXTATR_END) const
get the text attribute at position nIndex which owns the dummy character CH_TXTATR_* at that position...
Definition: ndtxt.cxx:3155
bool IsResolvedPostIts() const
Definition: viewopt.hxx:435
SwWrtShell * GetWrtShellPtr() const
Definition: view.hxx:424
SwDocShell * GetDocShell()
Definition: view.cxx:1193
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
bool GotoField(const SwFormatField &rField)
Definition: wrtsh3.cxx:76
bool DelRight(bool isReplaceHeuristic=false)
Definition: delete.cxx:285
static UITestLogger & getInstance()
void logEvent(const EventDescription &rDescription)
SwPostItHelper::SwLayoutStatus GetLayoutStatus() const
std::unique_ptr< sw::overlay::OverlayRanges > mpTextRangeOverlay
virtual void dispose() override
std::unique_ptr< weld::Label > mxMetadataResolved
void UpdateText(const OUString &aText)
std::unique_ptr< Outliner > mpOutliner
SwAnnotationWin * GetTopReplyNote()
Find the first annotation for the thread which this annotation is in.
std::unique_ptr< sw::sidebarwindows::AnchorOverlayObject > mpAnchor
std::unique_ptr< OutlinerView > mpOutlinerView
std::unique_ptr< weld::CustomWeld > mxSidebarTextControlWin
static sal_uInt32 CreateUniqueParaId()
sal_uInt32 CalcParent()
Calculate parent postit id of current annotation window.
std::unique_ptr< weld::MenuButton > mxMenuButton
std::unique_ptr< sw::sidebarwindows::ShadowOverlayObject > mpShadow
SwAnnotationWin(SwEditWin &rEditWin, SwPostItMgr &aMgr, SwSidebarItem &rSidebarItem, SwFormatField *aField)
virtual ~SwAnnotationWin() override
std::unique_ptr< weld::ScrolledWindow > mxVScrollbar
std::unique_ptr< weld::Label > mxMetadataDate
std::unique_ptr< sw::sidebarwindows::SidebarTextControl > mxSidebarTextControl
std::unique_ptr< weld::Label > mxMetadataAuthor
virtual FactoryFunction GetUITestFactory() const override
void InitAnswer(OutlinerParaObject const &rText)
static std::unique_ptr< ShadowOverlayObject > CreateShadowOverlayObject(SwView const &rDocView)
bool IsVisible() const
const OUString & get_id() const
bool SupportsDoubleBuffering() const
void SetParentClipMode(ParentClipMode nMode=ParentClipMode::NONE)
void set_id(const OUString &rID)
void Disable(bool bChild=true)
void GrabFocusToDocument()
void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
#define EE_PARA_ALL
#define EE_TEXTPOS_MAX_COUNT
#define EE_PARA_MAX_COUNT
#define EE_TEXTPOS_ALL
constexpr TypedWhichId< SvxFontHeightItem > EE_CHAR_FONTHEIGHT(EE_CHAR_START+2)
constexpr TypedWhichId< SvxPostureItem > EE_CHAR_ITALIC(EE_CHAR_START+7)
ITALIC_NORMAL
constexpr TypedWhichId< SwFormatField > RES_TXTATR_ANNOTATION(60)
std::function< std::unique_ptr< UIObject >(vcl::Window *)> FactoryFunction
sal_Int64 n
constexpr sal_uInt16 KEY_PAGEDOWN
constexpr sal_uInt16 KEY_PAGEUP
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
NONE
Dialog to specify the properties of date form field.
std::map< OUString, OUString > aParameters
const SwFrame * mpAnchorFrame
Marks a position in the document model.
Definition: pam.hxx:38
sal_Int32 GetContentIndex() const
Definition: pam.hxx:85
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:168
void GetSelection(struct ESelection &rSel, SvxTextForwarder const *pForwarder) noexcept