LibreOffice Module sc (master)  1
futext3.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 <svx/svdocapt.hxx>
21 #include <svx/svdundo.hxx>
22 #include <vcl/cursor.hxx>
23 #include <osl/diagnose.h>
24 
25 #include <global.hxx>
26 #include <drwlayer.hxx>
27 #include <userdat.hxx>
28 #include <tabvwsh.hxx>
29 #include <document.hxx>
30 #include <futext.hxx>
31 #include <docsh.hxx>
32 #include <postit.hxx>
33 #include <globstr.hrc>
34 #include <scresid.hxx>
35 #include <drawview.hxx>
36 #include <undocell.hxx>
37 
38 // Editing of Note-Key-Objects has to be stopped always via StopEditMode,
39 // so that changes are taken over into the document!
40 // (Fontwork-Execute in drawsh and drtxtob does not happen for Key-Objects)
41 
43 {
45  if( !pObject ) return;
46 
47  // relock the internal layer that has been unlocked in FuText::SetInEditMode()
48  if ( pObject->GetLayer() == SC_LAYER_INTERN )
50 
51  ScViewData& rViewData = rViewShell.GetViewData();
52  ScDocument& rDoc = rViewData.GetDocument();
53  ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
54  OSL_ENSURE( pDrawLayer && (pDrawLayer == pDrDoc), "FuText::StopEditMode - missing or different drawing layers" );
55 
56  ScAddress aNotePos;
57  ScPostIt* pNote = nullptr;
58  if( const ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( pObject, rViewData.GetTabNo() ) )
59  {
60  aNotePos = pCaptData->maStart;
61  pNote = rDoc.GetNote( aNotePos );
62  OSL_ENSURE( pNote && (pNote->GetCaption() == pObject), "FuText::StopEditMode - missing or invalid cell note" );
63  }
64 
65  ScDocShell* pDocShell = rViewData.GetDocShell();
66  SfxUndoManager* pUndoMgr = rDoc.IsUndoEnabled() ? pDocShell->GetUndoManager() : nullptr;
67  bool bNewNote = false;
68  if( pNote && pUndoMgr )
69  {
70  /* Put all undo actions already collected (e.g. create caption object)
71  and all following undo actions (text changed) together into a ListAction. */
72  std::unique_ptr<SdrUndoGroup> pCalcUndo = pDrawLayer->GetCalcUndo();
73 
74  if(pCalcUndo)
75  {
76  const OUString aUndoStr = ScResId( STR_UNDO_EDITNOTE );
77  pUndoMgr->EnterListAction( aUndoStr, aUndoStr, 0, rViewShell.GetViewShellId() );
78 
79  /* Note has been created before editing, if first undo action is
80  an insert action. Needed below to decide whether to drop the
81  undo if editing a new note has been cancelled. */
82  bNewNote = (pCalcUndo->GetActionCount() > 0) && dynamic_cast< SdrUndoNewObj* >(pCalcUndo->GetAction( 0 ));
83 
84  // create a "insert note" undo action if needed
85  if( bNewNote )
86  pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( *pDocShell, aNotePos, pNote->GetNoteData(), true, std::move(pCalcUndo) ) );
87  else
88  pUndoMgr->AddUndoAction( std::move(pCalcUndo) );
89  }
90  }
91 
92  if( pNote )
93  rDoc.LockStreamValid(true); // only the affected sheet is invalidated below
94 
95  /* SdrObjEditView::SdrEndTextEdit() may try to delete the entire drawing
96  object, if it does not contain text and has invisible border and fill.
97  This must not happen for note caption objects. They will be removed
98  below together with the cell note if the text is empty (independent of
99  border and area formatting). It is possible to prevent automatic
100  deletion by passing sal_True to this function. The return value changes
101  from SdrEndTextEditKind::Deleted to SdrEndTextEditKind::ShouldBeDeleted in this
102  case. */
103  /*SdrEndTextEditKind eResult =*/ pView->SdrEndTextEdit( pNote != nullptr );
104 
105  // or ScEndTextEdit (with drawview.hxx)
106  rViewShell.SetDrawTextUndo( nullptr );
107 
108  vcl::Cursor* pCur = pWindow->GetCursor();
109  if( pCur && pCur->IsVisible() )
110  pCur->Hide();
111 
112  if( !pNote )
113  return;
114 
116 
117  // hide the caption object if it is in hidden state
118  pNote->ShowCaptionTemp( aNotePos, false );
119 
120  // update author and date
121  pNote->AutoStamp();
122 
123  /* If the entire text has been cleared, the cell note and its caption
124  object have to be removed. */
125  SdrTextObj* pTextObject = dynamic_cast< SdrTextObj* >( pObject );
126  bool bDeleteNote = !pTextObject || !pTextObject->HasText();
127  if( bDeleteNote )
128  {
129  if( pUndoMgr )
130  {
131  // collect the "remove object" drawing undo action created by DeleteNote()
132  pDrawLayer->BeginCalcUndo(false);
133  // rescue note data before deletion
134  ScNoteData aNoteData( pNote->GetNoteData() );
135  // delete note from document (removes caption, but does not delete it)
136  rDoc.ReleaseNote(aNotePos);
137  // create undo action for removed note
138  pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( *pDocShell, aNotePos, aNoteData, false, pDrawLayer->GetCalcUndo() ) );
139  }
140  else
141  {
142  rDoc.ReleaseNote(aNotePos);
143  }
144  // ScDocument::DeleteNote has deleted the note that pNote points to
145  pNote = nullptr;
146  }
147 
148  // finalize the undo list action
149  if( pUndoMgr )
150  {
151  pUndoMgr->LeaveListAction();
152 
153  /* #i94039# Update the default name "Edit Note" of the undo action
154  if the note has been created before editing or is deleted due
155  to deleted text. If the note has been created *and* is deleted,
156  the last undo action can be removed completely. Note: The
157  function LeaveListAction() removes the last action by itself,
158  if it is empty (when result is SdrEndTextEditKind::Unchanged). */
159  if( bNewNote && bDeleteNote )
160  {
161  pUndoMgr->RemoveLastUndoAction();
162 
163  // Make sure the former area of the note anchor is invalidated.
164  ScRangeList aRangeList(aNotePos);
165  ScMarkData aMarkData(rDoc.GetSheetLimits(), aRangeList);
166  rViewShell.UpdateSelectionArea(aMarkData);
167  }
168  else if( bNewNote || bDeleteNote )
169  {
170  SfxListUndoAction* pAction = dynamic_cast< SfxListUndoAction* >( pUndoMgr->GetUndoAction() );
171  OSL_ENSURE( pAction, "FuText::StopEditMode - list undo action expected" );
172  if( pAction )
173  pAction->SetComment( ScResId( bNewNote ? STR_UNDO_INSERTNOTE : STR_UNDO_DELETENOTE ) );
174  }
175  }
176 
177  // invalidate stream positions only for the affected sheet
178  rDoc.LockStreamValid(false);
179  rDoc.SetStreamValid(aNotePos.Tab(), false);
180 }
181 
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void BeginCalcUndo(bool bDisableTextEditUsesCommonUndoManager)
Definition: drwlayer.cxx:1413
SfxUndoAction * GetUndoAction(size_t nNo=0) const
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:353
todo: It should be possible to have MarkArrays for each table, in order to enable "search all" across...
Definition: markdata.hxx:42
SdrTextObj * GetTextEditObject() const
void LockInternalLayer(bool bLock=true)
Locks/unlocks the internal layer that contains caption objects of cell notes.
Definition: drawview.hxx:135
SCTAB GetTabNo() const
Definition: viewdata.hxx:394
void AutoStamp()
Sets date and author from system settings.
Definition: postit.cxx:893
SdrModel * pDrDoc
Definition: fupoor.hxx:45
ScDocument & GetDocument() const
Definition: viewdata.hxx:379
EmbeddedObjectRef * pObject
void SetDrawTextUndo(SfxUndoManager *pUndoMgr)
Definition: tabvwsh4.cxx:1016
virtual bool HasText() const override
virtual SfxUndoManager * GetUndoManager() override
Definition: docsh.cxx:2901
SC_DLLPUBLIC ScPostIt * GetNote(const ScAddress &rPos)
Notes.
Definition: document.cxx:6534
Additional class containing cell annotation data.
Definition: postit.hxx:159
SCTAB Tab() const
Definition: address.hxx:270
ScTabViewShell & rViewShell
Definition: fupoor.hxx:43
ScViewData & GetViewData()
Definition: tabview.hxx:333
void ShowCaptionTemp(const ScAddress &rPos, bool bShow=true)
Shows or hides the caption temporarily (does not change internal visibility state).
Definition: postit.cxx:994
SC_DLLPUBLIC ScDrawLayer * GetDrawLayer()
Definition: document.hxx:1058
virtual void AddUndoAction(std::unique_ptr< SfxUndoAction > pAction, bool bTryMerg=false)
static ScDrawObjData * GetNoteCaptionData(SdrObject *pObj, SCTAB nTab)
Returns the object data, if the passed object is a cell note caption.
Definition: drwlayer.cxx:2624
ScSheetLimits & GetSheetLimits() const
Definition: document.hxx:873
vcl::Cursor * GetCursor() const
void RemoveLastUndoAction()
void LockStreamValid(bool bLock)
Definition: document.cxx:930
size_t LeaveListAction()
virtual SdrLayerID GetLayer() const
virtual SdrEndTextEditKind SdrEndTextEdit(bool bDontDeleteReally=false) override
Definition: drawview.cxx:588
SdrCaptionObj * GetCaption() const
Returns an existing note caption object.
Definition: postit.hxx:236
OUString ScResId(std::string_view aId)
Definition: scdll.cxx:89
void SetStreamValid(SCTAB nTab, bool bSet, bool bIgnoreLock=false)
Definition: document.cxx:924
void SetComment(const OUString &rComment)
std::unique_ptr< SdrUndoGroup > GetCalcUndo()
Definition: drwlayer.cxx:1420
const ScNoteData & GetNoteData() const
Returns the data struct containing all note settings.
Definition: postit.hxx:205
Internal data for a cell annotation.
Definition: postit.hxx:143
void StopEditMode()
Definition: futext3.cxx:42
constexpr SdrLayerID SC_LAYER_INTERN(2)
ScDrawView * pView
Definition: fupoor.hxx:42
virtual void EnterListAction(const OUString &rComment, const OUString &rRepeatComment, sal_uInt16 nId, ViewShellId nViewShellId)
static void OnLOKNoteStateChanged(const ScPostIt *pNote)
Definition: tabview5.cxx:662
bool IsVisible() const
void UpdateSelectionArea(const ScMarkData &rSel, ScPatternAttr *pAttr=nullptr)
Definition: viewfunc.cxx:2991
bool IsUndoEnabled() const
Definition: document.hxx:1535
SC_DLLPUBLIC std::unique_ptr< ScPostIt > ReleaseNote(const ScAddress &rPos)
Definition: document.cxx:6630
VclPtr< vcl::Window > pWindow
Definition: fupoor.hxx:44
ViewShellId GetViewShellId() const override