/src/libreoffice/sc/source/ui/drawfunc/futext3.cxx
Line | Count | Source |
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 | | |
42 | | void FuText::StopEditMode() |
43 | 0 | { |
44 | 0 | SdrObject* pObject = pView->GetTextEditObject(); |
45 | 0 | if( !pObject ) return; |
46 | | |
47 | | // relock the internal layer that has been unlocked in FuText::SetInEditMode() |
48 | 0 | if ( pObject->GetLayer() == SC_LAYER_INTERN ) |
49 | 0 | pView->LockInternalLayer(); |
50 | |
|
51 | 0 | ScViewData& rViewData = rViewShell.GetViewData(); |
52 | 0 | ScDocument& rDoc = rViewData.GetDocument(); |
53 | 0 | ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer(); |
54 | 0 | OSL_ENSURE( pDrawLayer && (pDrawLayer == &rDrDoc), "FuText::StopEditMode - missing or different drawing layers" ); |
55 | |
|
56 | 0 | ScAddress aNotePos; |
57 | 0 | ScPostIt* pNote = nullptr; |
58 | 0 | if( const ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( pObject, rViewData.CurrentTabForData() ) ) |
59 | 0 | { |
60 | 0 | aNotePos = pCaptData->maStart; |
61 | 0 | pNote = rDoc.GetNote( aNotePos ); |
62 | 0 | OSL_ENSURE( pNote && (pNote->GetCaption() == pObject), "FuText::StopEditMode - missing or invalid cell note" ); |
63 | 0 | } |
64 | |
|
65 | 0 | ScDocShell* pDocShell = rViewData.GetDocShell(); |
66 | 0 | SfxUndoManager* pUndoMgr = rDoc.IsUndoEnabled() ? pDocShell->GetUndoManager() : nullptr; |
67 | 0 | if (pUndoMgr && !pUndoMgr->GetMaxUndoActionCount()) // tdf#134308 if max undo is 0, treat as if no undo |
68 | 0 | pUndoMgr = nullptr; |
69 | 0 | bool bNewNote = false; |
70 | 0 | if( pNote && pUndoMgr ) |
71 | 0 | { |
72 | | /* Put all undo actions already collected (e.g. create caption object) |
73 | | and all following undo actions (text changed) together into a ListAction. */ |
74 | 0 | std::unique_ptr<SdrUndoGroup> pCalcUndo = pDrawLayer->GetCalcUndo(); |
75 | |
|
76 | 0 | if(pCalcUndo) |
77 | 0 | { |
78 | 0 | const OUString aUndoStr = ScResId( STR_UNDO_EDITNOTE ); |
79 | 0 | pUndoMgr->EnterListAction( aUndoStr, aUndoStr, 0, rViewShell.GetViewShellId() ); |
80 | | |
81 | | /* Note has been created before editing, if first undo action is |
82 | | an insert action. Needed below to decide whether to drop the |
83 | | undo if editing a new note has been cancelled. */ |
84 | 0 | bNewNote = (pCalcUndo->GetActionCount() > 0) && dynamic_cast< SdrUndoNewObj* >(pCalcUndo->GetAction( 0 )); |
85 | | |
86 | | // create a "insert note" undo action if needed |
87 | 0 | if( bNewNote ) |
88 | 0 | pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( *pDocShell, aNotePos, pNote->GetNoteData(), true, std::move(pCalcUndo) ) ); |
89 | 0 | else |
90 | 0 | pUndoMgr->AddUndoAction( std::move(pCalcUndo) ); |
91 | 0 | } |
92 | 0 | } |
93 | |
|
94 | 0 | if( pNote ) |
95 | 0 | rDoc.LockStreamValid(true); // only the affected sheet is invalidated below |
96 | | |
97 | | /* Unset the outliner undo manager before the call to SdrEndTextEdit. |
98 | | SdrObjEditView::SdrEndTextEdit destroys it, but then ScDrawView::SdrEndTextEdit |
99 | | initiates some UI update which might try to access the now invalid pointer. */ |
100 | 0 | rViewShell.SetDrawTextUndo( nullptr ); |
101 | | |
102 | | /* SdrObjEditView::SdrEndTextEdit() may try to delete the entire drawing |
103 | | object, if it does not contain text and has invisible border and fill. |
104 | | This must not happen for note caption objects. They will be removed |
105 | | below together with the cell note if the text is empty (independent of |
106 | | border and area formatting). It is possible to prevent automatic |
107 | | deletion by passing sal_True to this function. The return value changes |
108 | | from SdrEndTextEditKind::Deleted to SdrEndTextEditKind::ShouldBeDeleted in this |
109 | | case. */ |
110 | | /*SdrEndTextEditKind eResult =*/ pView->SdrEndTextEdit( pNote != nullptr ); |
111 | |
|
112 | 0 | vcl::Cursor* pCur = pWindow->GetCursor(); |
113 | 0 | if( pCur && pCur->IsVisible() ) |
114 | 0 | pCur->Hide(); |
115 | |
|
116 | 0 | if( !pNote ) |
117 | 0 | return; |
118 | | |
119 | 0 | ScTabView::OnLOKNoteStateChanged( pNote ); |
120 | | |
121 | | // hide the caption object if it is in hidden state |
122 | 0 | pNote->ShowCaptionTemp( aNotePos, false ); |
123 | | |
124 | | // update author and date |
125 | 0 | pNote->AutoStamp(); |
126 | | |
127 | | /* If the entire text has been cleared, the cell note and its caption |
128 | | object have to be removed. */ |
129 | 0 | SdrTextObj* pTextObject = DynCastSdrTextObj( pObject ); |
130 | 0 | bool bDeleteNote = !pTextObject || !pTextObject->HasText(); |
131 | 0 | if( bDeleteNote ) |
132 | 0 | { |
133 | 0 | if( pUndoMgr ) |
134 | 0 | { |
135 | | // collect the "remove object" drawing undo action created by DeleteNote() |
136 | 0 | pDrawLayer->BeginCalcUndo(false); |
137 | | // rescue note data before deletion |
138 | 0 | ScNoteData aNoteData( pNote->GetNoteData() ); |
139 | | // delete note from document (removes caption, but does not delete it) |
140 | 0 | rDoc.ReleaseNote(aNotePos); |
141 | | // create undo action for removed note |
142 | 0 | pUndoMgr->AddUndoAction( std::make_unique<ScUndoReplaceNote>( *pDocShell, aNotePos, aNoteData, false, pDrawLayer->GetCalcUndo() ) ); |
143 | 0 | } |
144 | 0 | else |
145 | 0 | { |
146 | 0 | rDoc.ReleaseNote(aNotePos); |
147 | 0 | } |
148 | | // ScDocument::DeleteNote has deleted the note that pNote points to |
149 | 0 | pNote = nullptr; |
150 | 0 | } |
151 | | |
152 | | // finalize the undo list action |
153 | 0 | if( pUndoMgr ) |
154 | 0 | { |
155 | 0 | pUndoMgr->LeaveListAction(); |
156 | | |
157 | | /* #i94039# Update the default name "Edit Note" of the undo action |
158 | | if the note has been created before editing or is deleted due |
159 | | to deleted text. If the note has been created *and* is deleted, |
160 | | the last undo action can be removed completely. Note: The |
161 | | function LeaveListAction() removes the last action by itself, |
162 | | if it is empty (when result is SdrEndTextEditKind::Unchanged). */ |
163 | 0 | if( bNewNote && bDeleteNote ) |
164 | 0 | { |
165 | 0 | pUndoMgr->RemoveLastUndoAction(); |
166 | | |
167 | | // Make sure the former area of the note anchor is invalidated. |
168 | 0 | ScRangeList aRangeList((ScRange(aNotePos))); |
169 | 0 | ScMarkData aMarkData(rDoc.GetSheetLimits(), aRangeList); |
170 | 0 | rViewShell.UpdateSelectionArea(aMarkData); |
171 | 0 | } |
172 | 0 | else if( bNewNote || bDeleteNote ) |
173 | 0 | { |
174 | 0 | SfxListUndoAction* pAction = dynamic_cast< SfxListUndoAction* >( pUndoMgr->GetUndoAction() ); |
175 | 0 | OSL_ENSURE( pAction, "FuText::StopEditMode - list undo action expected" ); |
176 | 0 | if( pAction ) |
177 | 0 | pAction->SetComment( ScResId( bNewNote ? STR_UNDO_INSERTNOTE : STR_UNDO_DELETENOTE ) ); |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | | // invalidate stream positions only for the affected sheet |
182 | 0 | rDoc.LockStreamValid(false); |
183 | 0 | rDoc.SetStreamValid(aNotePos.Tab(), false); |
184 | 0 | } |
185 | | |
186 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |