Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / drawfunc / futext3.cxx
blobe2f301b12f0c908ceb940a0753a3df5831f1d576
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <editeng/editeng.hxx>
22 #include <editeng/outlobj.hxx>
23 #include <svx/svdocapt.hxx>
24 #include <svx/svdpage.hxx>
25 #include <svx/svdundo.hxx>
26 #include <svx/svdview.hxx>
27 #include <editeng/editobj.hxx>
28 #include <vcl/cursor.hxx>
29 #include <sfx2/objsh.hxx>
30 #include <editeng/writingmodeitem.hxx>
32 #include "global.hxx"
33 #include "drwlayer.hxx"
34 #include "userdat.hxx"
35 #include "tabvwsh.hxx"
36 #include "document.hxx"
37 #include "editutil.hxx"
38 #include "futext.hxx"
39 #include "docsh.hxx"
40 #include "postit.hxx"
41 #include "globstr.hrc"
42 #include "attrib.hxx"
43 #include "scitems.hxx"
44 #include "drawview.hxx"
45 #include "undocell.hxx"
47 // ------------------------------------------------------------------------------------
48 // Editieren von Notiz-Legendenobjekten muss immer ueber StopEditMode beendet werden,
49 // damit die Aenderungen ins Dokument uebernommen werden!
50 // (Fontwork-Execute in drawsh und drtxtob passiert nicht fuer Legendenobjekte)
51 // bTextDirection=sal_True means that this function is called from SID_TEXTDIRECTION_XXX(drtxtob.cxx).
52 // ------------------------------------------------------------------------------------
54 void FuText::StopEditMode(sal_Bool /*bTextDirection*/)
56 SdrObject* pObject = pView->GetTextEditObject();
57 if( !pObject ) return;
59 // relock the internal layer that has been unlocked in FuText::SetInEditMode()
60 if ( pObject->GetLayer() == SC_LAYER_INTERN )
61 pView->LockInternalLayer();
63 ScViewData& rViewData = *pViewShell->GetViewData();
64 ScDocument& rDoc = *rViewData.GetDocument();
65 ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
66 OSL_ENSURE( pDrawLayer && (pDrawLayer == pDrDoc), "FuText::StopEditMode - missing or different drawing layers" );
68 ScAddress aNotePos;
69 ScPostIt* pNote = 0;
70 if( const ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( pObject, rViewData.GetTabNo() ) )
72 aNotePos = pCaptData->maStart;
73 pNote = rDoc.GetNote( aNotePos );
74 OSL_ENSURE( pNote && (pNote->GetCaption() == pObject), "FuText::StopEditMode - missing or invalid cell note" );
77 ScDocShell* pDocShell = rViewData.GetDocShell();
78 ::svl::IUndoManager* pUndoMgr = rDoc.IsUndoEnabled() ? pDocShell->GetUndoManager() : 0;
79 bool bNewNote = false;
80 if( pNote && pUndoMgr )
82 /* Put all undo actions already collected (e.g. create caption object)
83 and all following undo actions (text changed) together into a ListAction. */
84 SdrUndoGroup* pCalcUndo = pDrawLayer->GetCalcUndo();
86 if(pCalcUndo)
88 const OUString aUndoStr = ScGlobal::GetRscString( STR_UNDO_EDITNOTE );
89 pUndoMgr->EnterListAction( aUndoStr, aUndoStr );
91 /* Note has been created before editing, if first undo action is
92 an insert action. Needed below to decide whether to drop the
93 undo if editing a new note has been cancelled. */
94 bNewNote = (pCalcUndo->GetActionCount() > 0) && pCalcUndo->GetAction( 0 )->ISA( SdrUndoNewObj );
96 // create a "insert note" undo action if needed
97 if( bNewNote )
98 pUndoMgr->AddUndoAction( new ScUndoReplaceNote( *pDocShell, aNotePos, pNote->GetNoteData(), true, pCalcUndo ) );
99 else
100 pUndoMgr->AddUndoAction( pCalcUndo );
104 if( pNote )
105 rDoc.LockStreamValid(true); // only the affected sheet is invalidated below
107 /* SdrObjEditView::SdrEndTextEdit() may try to delete the entire drawing
108 object, if it does not contain text and has invisible border and fill.
109 This must not happen for note caption objects. They will be removed
110 below together with the cell note if the text is empty (independent of
111 border and area formatting). It is possible to prevent automatic
112 deletion by passing sal_True to this function. The return value changes
113 from SDRENDTEXTEDIT_DELETED to SDRENDTEXTEDIT_SHOULDBEDELETED in this
114 case. */
115 /*SdrEndTextEditKind eResult =*/ pView->SdrEndTextEdit( pNote != 0 );
117 // or ScEndTextEdit (with drawview.hxx)
118 pViewShell->SetDrawTextUndo( 0 );
120 Cursor* pCur = pWindow->GetCursor();
121 if( pCur && pCur->IsVisible() )
122 pCur->Hide();
124 if( pNote )
126 // hide the caption object if it is in hidden state
127 pNote->ShowCaptionTemp( aNotePos, false );
129 // update author and date
130 pNote->AutoStamp();
132 /* If the entire text has been cleared, the cell note and its caption
133 object have to be removed. */
134 SdrTextObj* pTextObject = dynamic_cast< SdrTextObj* >( pObject );
135 bool bDeleteNote = !pTextObject || !pTextObject->HasText();
136 if( bDeleteNote )
138 if( pUndoMgr )
140 // collect the "remove object" drawing undo action created by DeleteNote()
141 pDrawLayer->BeginCalcUndo(false);
142 // rescue note data before deletion
143 ScNoteData aNoteData( pNote->GetNoteData() );
144 // delete note from document (removes caption, but does not delete it)
145 rDoc.ReleaseNote(aNotePos);
146 // create undo action for removed note
147 pUndoMgr->AddUndoAction( new ScUndoReplaceNote( *pDocShell, aNotePos, aNoteData, false, pDrawLayer->GetCalcUndo() ) );
149 else
151 rDoc.ReleaseNote(aNotePos);
153 // ScDocument::DeleteNote has deleted the note that pNote points to
154 pNote = 0;
157 // finalize the undo list action
158 if( pUndoMgr )
160 pUndoMgr->LeaveListAction();
162 /* #i94039# Update the default name "Edit Note" of the undo action
163 if the note has been created before editing or is deleted due
164 to deleted text. If the note has been created *and* is deleted,
165 the last undo action can be removed completely. Note: The
166 function LeaveListAction() removes the last action by itself,
167 if it is empty (when result is SDRENDTEXTEDIT_UNCHANGED). */
168 if( bNewNote && bDeleteNote )
170 pUndoMgr->RemoveLastUndoAction();
172 else if( bNewNote || bDeleteNote )
174 SfxListUndoAction* pAction = dynamic_cast< SfxListUndoAction* >( pUndoMgr->GetUndoAction() );
175 OSL_ENSURE( pAction, "FuText::StopEditMode - list undo action expected" );
176 if( pAction )
177 pAction->SetComment( ScGlobal::GetRscString( bNewNote ? STR_UNDO_INSERTNOTE : STR_UNDO_DELETENOTE ) );
181 // invalidate stream positions only for the affected sheet
182 rDoc.LockStreamValid(false);
183 if (rDoc.IsStreamValid(aNotePos.Tab()))
184 rDoc.SetStreamValid(aNotePos.Tab(), false);
188 // Called following an EndDragObj() to update the new note rectangle position
189 void FuText::StopDragMode(SdrObject* /*pObject*/)
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */