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