1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <editeng/editeng.hxx>
21 #include <editeng/outlobj.hxx>
22 #include <svx/svdocapt.hxx>
23 #include <svx/svdpage.hxx>
24 #include <svx/svdundo.hxx>
25 #include <svx/svdview.hxx>
26 #include <editeng/editobj.hxx>
27 #include <vcl/cursor.hxx>
28 #include <sfx2/objsh.hxx>
29 #include <editeng/writingmodeitem.hxx>
32 #include "drwlayer.hxx"
33 #include "userdat.hxx"
34 #include "tabvwsh.hxx"
35 #include "document.hxx"
36 #include "editutil.hxx"
40 #include "globstr.hrc"
42 #include "scitems.hxx"
43 #include "drawview.hxx"
44 #include "undocell.hxx"
46 // Editieren von Notiz-Legendenobjekten muss immer ueber StopEditMode beendet werden,
47 // damit die Aenderungen ins Dokument uebernommen werden!
48 // (Fontwork-Execute in drawsh und drtxtob passiert nicht fuer Legendenobjekte)
49 // bTextDirection=sal_True means that this function is called from SID_TEXTDIRECTION_XXX(drtxtob.cxx).
51 void FuText::StopEditMode(bool /*bTextDirection*/)
53 SdrObject
* pObject
= pView
->GetTextEditObject();
54 if( !pObject
) return;
56 // relock the internal layer that has been unlocked in FuText::SetInEditMode()
57 if ( pObject
->GetLayer() == SC_LAYER_INTERN
)
58 pView
->LockInternalLayer();
60 ScViewData
& rViewData
= pViewShell
->GetViewData();
61 ScDocument
& rDoc
= *rViewData
.GetDocument();
62 ScDrawLayer
* pDrawLayer
= rDoc
.GetDrawLayer();
63 OSL_ENSURE( pDrawLayer
&& (pDrawLayer
== pDrDoc
), "FuText::StopEditMode - missing or different drawing layers" );
67 if( const ScDrawObjData
* pCaptData
= ScDrawLayer::GetNoteCaptionData( pObject
, rViewData
.GetTabNo() ) )
69 aNotePos
= pCaptData
->maStart
;
70 pNote
= rDoc
.GetNote( aNotePos
);
71 OSL_ENSURE( pNote
&& (pNote
->GetCaption() == pObject
), "FuText::StopEditMode - missing or invalid cell note" );
74 ScDocShell
* pDocShell
= rViewData
.GetDocShell();
75 ::svl::IUndoManager
* pUndoMgr
= rDoc
.IsUndoEnabled() ? pDocShell
->GetUndoManager() : 0;
76 bool bNewNote
= false;
77 if( pNote
&& pUndoMgr
)
79 /* Put all undo actions already collected (e.g. create caption object)
80 and all following undo actions (text changed) together into a ListAction. */
81 SdrUndoGroup
* pCalcUndo
= pDrawLayer
->GetCalcUndo();
85 const OUString aUndoStr
= ScGlobal::GetRscString( STR_UNDO_EDITNOTE
);
86 pUndoMgr
->EnterListAction( aUndoStr
, aUndoStr
);
88 /* Note has been created before editing, if first undo action is
89 an insert action. Needed below to decide whether to drop the
90 undo if editing a new note has been cancelled. */
91 bNewNote
= (pCalcUndo
->GetActionCount() > 0) && pCalcUndo
->GetAction( 0 )->ISA( SdrUndoNewObj
);
93 // create a "insert note" undo action if needed
95 pUndoMgr
->AddUndoAction( new ScUndoReplaceNote( *pDocShell
, aNotePos
, pNote
->GetNoteData(), true, pCalcUndo
) );
97 pUndoMgr
->AddUndoAction( pCalcUndo
);
102 rDoc
.LockStreamValid(true); // only the affected sheet is invalidated below
104 /* SdrObjEditView::SdrEndTextEdit() may try to delete the entire drawing
105 object, if it does not contain text and has invisible border and fill.
106 This must not happen for note caption objects. They will be removed
107 below together with the cell note if the text is empty (independent of
108 border and area formatting). It is possible to prevent automatic
109 deletion by passing sal_True to this function. The return value changes
110 from SDRENDTEXTEDIT_DELETED to SDRENDTEXTEDIT_SHOULDBEDELETED in this
112 /*SdrEndTextEditKind eResult =*/ pView
->SdrEndTextEdit( pNote
!= 0 );
114 // or ScEndTextEdit (with drawview.hxx)
115 pViewShell
->SetDrawTextUndo( 0 );
117 vcl::Cursor
* pCur
= pWindow
->GetCursor();
118 if( pCur
&& pCur
->IsVisible() )
123 // hide the caption object if it is in hidden state
124 pNote
->ShowCaptionTemp( aNotePos
, false );
126 // update author and date
129 /* If the entire text has been cleared, the cell note and its caption
130 object have to be removed. */
131 SdrTextObj
* pTextObject
= dynamic_cast< SdrTextObj
* >( pObject
);
132 bool bDeleteNote
= !pTextObject
|| !pTextObject
->HasText();
137 // collect the "remove object" drawing undo action created by DeleteNote()
138 pDrawLayer
->BeginCalcUndo(false);
139 // rescue note data before deletion
140 ScNoteData
aNoteData( pNote
->GetNoteData() );
141 // delete note from document (removes caption, but does not delete it)
142 rDoc
.ReleaseNote(aNotePos
);
143 // create undo action for removed note
144 pUndoMgr
->AddUndoAction( new ScUndoReplaceNote( *pDocShell
, aNotePos
, aNoteData
, false, pDrawLayer
->GetCalcUndo() ) );
148 rDoc
.ReleaseNote(aNotePos
);
150 // ScDocument::DeleteNote has deleted the note that pNote points to
154 // finalize the undo list action
157 pUndoMgr
->LeaveListAction();
159 /* #i94039# Update the default name "Edit Note" of the undo action
160 if the note has been created before editing or is deleted due
161 to deleted text. If the note has been created *and* is deleted,
162 the last undo action can be removed completely. Note: The
163 function LeaveListAction() removes the last action by itself,
164 if it is empty (when result is SDRENDTEXTEDIT_UNCHANGED). */
165 if( bNewNote
&& bDeleteNote
)
167 pUndoMgr
->RemoveLastUndoAction();
169 else if( bNewNote
|| bDeleteNote
)
171 SfxListUndoAction
* pAction
= dynamic_cast< SfxListUndoAction
* >( pUndoMgr
->GetUndoAction() );
172 OSL_ENSURE( pAction
, "FuText::StopEditMode - list undo action expected" );
174 pAction
->SetComment( ScGlobal::GetRscString( bNewNote
? STR_UNDO_INSERTNOTE
: STR_UNDO_DELETENOTE
) );
178 // invalidate stream positions only for the affected sheet
179 rDoc
.LockStreamValid(false);
180 if (rDoc
.IsStreamValid(aNotePos
.Tab()))
181 rDoc
.SetStreamValid(aNotePos
.Tab(), false);
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */