xref: /aoo41x/main/sc/source/ui/drawfunc/futext3.cxx (revision a840a559)
1b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b3f79822SAndrew Rist  * distributed with this work for additional information
6b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b3f79822SAndrew Rist  *
11b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b3f79822SAndrew Rist  *
13b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17b3f79822SAndrew Rist  * specific language governing permissions and limitations
18b3f79822SAndrew Rist  * under the License.
19b3f79822SAndrew Rist  *
20b3f79822SAndrew Rist  *************************************************************/
21b3f79822SAndrew Rist 
22b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <editeng/editeng.hxx>
28cdf0e10cSrcweir #include <editeng/outlobj.hxx>
29cdf0e10cSrcweir #include <svx/svdocapt.hxx>
30cdf0e10cSrcweir #include <svx/svdpage.hxx>
31cdf0e10cSrcweir #include <svx/svdundo.hxx>
32cdf0e10cSrcweir #include <svx/svdview.hxx>
33cdf0e10cSrcweir #include <editeng/editobj.hxx>
34cdf0e10cSrcweir #include <vcl/cursor.hxx>
35cdf0e10cSrcweir #include <sfx2/objsh.hxx>
36cdf0e10cSrcweir #include <editeng/writingmodeitem.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "global.hxx"
39cdf0e10cSrcweir #include "drwlayer.hxx"
40cdf0e10cSrcweir #include "userdat.hxx"
41cdf0e10cSrcweir #include "tabvwsh.hxx"			// oder GetDocument irgendwo
42cdf0e10cSrcweir #include "document.hxx"
43cdf0e10cSrcweir #include "editutil.hxx"
44cdf0e10cSrcweir #include "futext.hxx"
45cdf0e10cSrcweir #include "docsh.hxx"
46cdf0e10cSrcweir #include "postit.hxx"
47cdf0e10cSrcweir #include "globstr.hrc"
48cdf0e10cSrcweir #include "attrib.hxx"
49cdf0e10cSrcweir #include "scitems.hxx"
50cdf0e10cSrcweir #include "drawview.hxx"
51cdf0e10cSrcweir #include "undocell.hxx"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir // ------------------------------------------------------------------------------------
54cdf0e10cSrcweir //	Editieren von Notiz-Legendenobjekten muss immer ueber StopEditMode beendet werden,
55cdf0e10cSrcweir //	damit die Aenderungen ins Dokument uebernommen werden!
56cdf0e10cSrcweir //	(Fontwork-Execute in drawsh und drtxtob passiert nicht fuer Legendenobjekte)
57cdf0e10cSrcweir //	bTextDirection=sal_True means that this function is called from SID_TEXTDIRECTION_XXX(drtxtob.cxx).
58cdf0e10cSrcweir // ------------------------------------------------------------------------------------
59cdf0e10cSrcweir 
StopEditMode(sal_Bool)60cdf0e10cSrcweir void FuText::StopEditMode(sal_Bool /*bTextDirection*/)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     SdrObject* pObject = pView->GetTextEditObject();
63cdf0e10cSrcweir     if( !pObject ) return;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     // relock the internal layer that has been unlocked in FuText::SetInEditMode()
66cdf0e10cSrcweir 	if ( pObject->GetLayer() == SC_LAYER_INTERN )
67cdf0e10cSrcweir         pView->LockInternalLayer();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     ScViewData& rViewData = *pViewShell->GetViewData();
70cdf0e10cSrcweir     ScDocument& rDoc = *rViewData.GetDocument();
71cdf0e10cSrcweir     ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
72cdf0e10cSrcweir     DBG_ASSERT( pDrawLayer && (pDrawLayer == pDrDoc), "FuText::StopEditMode - missing or different drawing layers" );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     ScAddress aNotePos;
75cdf0e10cSrcweir     ScPostIt* pNote = 0;
76cdf0e10cSrcweir     if( const ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( pObject, rViewData.GetTabNo() ) )
77cdf0e10cSrcweir     {
78cdf0e10cSrcweir         aNotePos = pCaptData->maStart;
79cdf0e10cSrcweir         pNote = rDoc.GetNote( aNotePos );
80cdf0e10cSrcweir         DBG_ASSERT( pNote && (pNote->GetCaption() == pObject), "FuText::StopEditMode - missing or invalid cell note" );
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     ScDocShell* pDocShell = rViewData.GetDocShell();
84cdf0e10cSrcweir     ::svl::IUndoManager* pUndoMgr = rDoc.IsUndoEnabled() ? pDocShell->GetUndoManager() : 0;
85cdf0e10cSrcweir     bool bNewNote = false;
86cdf0e10cSrcweir     if( pNote && pUndoMgr )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         /*  Put all undo actions already collected (e.g. create caption object)
89cdf0e10cSrcweir             and all following undo actions (text changed) together into a ListAction. */
90*a840a559SArmin Le Grand         SdrUndoGroup* pCalcUndo = pDrawLayer->GetCalcUndo();
91*a840a559SArmin Le Grand 
92*a840a559SArmin Le Grand         if(pCalcUndo)
93cdf0e10cSrcweir         {
94*a840a559SArmin Le Grand             const String aUndoStr = ScGlobal::GetRscString( STR_UNDO_EDITNOTE );
95*a840a559SArmin Le Grand             pUndoMgr->EnterListAction( aUndoStr, aUndoStr );
96*a840a559SArmin Le Grand 
97cdf0e10cSrcweir             /*  Note has been created before editing, if first undo action is
98cdf0e10cSrcweir                 an insert action. Needed below to decide whether to drop the
99cdf0e10cSrcweir                 undo if editing a new note has been cancelled. */
100cdf0e10cSrcweir             bNewNote = (pCalcUndo->GetActionCount() > 0) && pCalcUndo->GetAction( 0 )->ISA( SdrUndoNewObj );
101*a840a559SArmin Le Grand 
102cdf0e10cSrcweir             // create a "insert note" undo action if needed
103cdf0e10cSrcweir             if( bNewNote )
104cdf0e10cSrcweir                 pUndoMgr->AddUndoAction( new ScUndoReplaceNote( *pDocShell, aNotePos, pNote->GetNoteData(), true, pCalcUndo ) );
105cdf0e10cSrcweir             else
106cdf0e10cSrcweir                 pUndoMgr->AddUndoAction( pCalcUndo );
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     if( pNote )
111cdf0e10cSrcweir         rDoc.LockStreamValid(true);     // only the affected sheet is invalidated below
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     /*  SdrObjEditView::SdrEndTextEdit() may try to delete the entire drawing
114cdf0e10cSrcweir         object, if it does not contain text and has invisible border and fill.
115cdf0e10cSrcweir         This must not happen for note caption objects. They will be removed
116cdf0e10cSrcweir         below together with the cell note if the text is empty (independent of
117cdf0e10cSrcweir         border and area formatting). It is possible to prevent automatic
118cdf0e10cSrcweir         deletion by passing sal_True to this function. The return value changes
119cdf0e10cSrcweir         from SDRENDTEXTEDIT_DELETED to SDRENDTEXTEDIT_SHOULDBEDELETED in this
120cdf0e10cSrcweir         case. */
121cdf0e10cSrcweir     /*SdrEndTextEditKind eResult =*/ pView->SdrEndTextEdit( pNote != 0 );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     // or ScEndTextEdit (with drawview.hxx)
124cdf0e10cSrcweir     pViewShell->SetDrawTextUndo( 0 );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     Cursor* pCur = pWindow->GetCursor();
127cdf0e10cSrcweir     if( pCur && pCur->IsVisible() )
128cdf0e10cSrcweir         pCur->Hide();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     if( pNote )
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         // hide the caption object if it is in hidden state
133cdf0e10cSrcweir         pNote->ShowCaptionTemp( aNotePos, false );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         // update author and date
136cdf0e10cSrcweir         pNote->AutoStamp();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         /*  If the entire text has been cleared, the cell note and its caption
139cdf0e10cSrcweir             object have to be removed. */
140cdf0e10cSrcweir         SdrTextObj* pTextObject = dynamic_cast< SdrTextObj* >( pObject );
141cdf0e10cSrcweir         bool bDeleteNote = !pTextObject || !pTextObject->HasText();
142cdf0e10cSrcweir         if( bDeleteNote )
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             if( pUndoMgr )
145cdf0e10cSrcweir             {
146cdf0e10cSrcweir                 // collect the "remove object" drawing undo action created by DeleteNote()
147*a840a559SArmin Le Grand                 pDrawLayer->BeginCalcUndo(false);
148cdf0e10cSrcweir                 // rescue note data before deletion
149cdf0e10cSrcweir                 ScNoteData aNoteData( pNote->GetNoteData() );
150cdf0e10cSrcweir                 // delete note from document (removes caption, but does not delete it)
151cdf0e10cSrcweir                 rDoc.DeleteNote( aNotePos );
152cdf0e10cSrcweir                 // create undo action for removed note
153cdf0e10cSrcweir                 pUndoMgr->AddUndoAction( new ScUndoReplaceNote( *pDocShell, aNotePos, aNoteData, false, pDrawLayer->GetCalcUndo() ) );
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir             else
156cdf0e10cSrcweir             {
157cdf0e10cSrcweir                 rDoc.DeleteNote( aNotePos );
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir             // ScDocument::DeleteNote has deleted the note that pNote points to
160cdf0e10cSrcweir             pNote = 0;
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         // finalize the undo list action
164cdf0e10cSrcweir         if( pUndoMgr )
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir             pUndoMgr->LeaveListAction();
167cdf0e10cSrcweir 
168cdf0e10cSrcweir             /*  #i94039# Update the default name "Edit Note" of the undo action
169cdf0e10cSrcweir                 if the note has been created before editing or is deleted due
170cdf0e10cSrcweir                 to deleted text. If the note has been created *and* is deleted,
171cdf0e10cSrcweir                 the last undo action can be removed completely. Note: The
172cdf0e10cSrcweir                 function LeaveListAction() removes the last action by itself,
173cdf0e10cSrcweir                 if it is empty (when result is SDRENDTEXTEDIT_UNCHANGED). */
174cdf0e10cSrcweir             if( bNewNote && bDeleteNote )
175cdf0e10cSrcweir             {
176cdf0e10cSrcweir                 pUndoMgr->RemoveLastUndoAction();
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir             else if( bNewNote || bDeleteNote )
179cdf0e10cSrcweir             {
180cdf0e10cSrcweir                 SfxListUndoAction* pAction = dynamic_cast< SfxListUndoAction* >( pUndoMgr->GetUndoAction() );
181cdf0e10cSrcweir                 DBG_ASSERT( pAction, "FuText::StopEditMode - list undo action expected" );
182cdf0e10cSrcweir                 if( pAction )
183cdf0e10cSrcweir                     pAction->SetComment( ScGlobal::GetRscString( bNewNote ? STR_UNDO_INSERTNOTE : STR_UNDO_DELETENOTE ) );
184cdf0e10cSrcweir             }
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         // invalidate stream positions only for the affected sheet
188cdf0e10cSrcweir         rDoc.LockStreamValid(false);
189cdf0e10cSrcweir         if (rDoc.IsStreamValid(aNotePos.Tab()))
190cdf0e10cSrcweir             rDoc.SetStreamValid(aNotePos.Tab(), sal_False);
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // Called following an EndDragObj() to update the new note rectangle position
StopDragMode(SdrObject *)195cdf0e10cSrcweir void FuText::StopDragMode(SdrObject* /*pObject*/)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir #if 0 // DR
198cdf0e10cSrcweir     ScViewData& rViewData = *pViewShell->GetViewData();
199cdf0e10cSrcweir     if( ScDrawObjData* pData = ScDrawLayer::GetNoteCaptionData( pObject, rViewData.GetTabNo() ) )
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         ScDocument& rDoc = *rViewData.GetDocument();
202cdf0e10cSrcweir         const ScAddress& rPos = pData->maStart;
203cdf0e10cSrcweir         ScPostIt* pNote = rDoc.GetNote( rPos );
204cdf0e10cSrcweir         DBG_ASSERT( pNote && (pNote->GetCaption() == pObject), "FuText::StopDragMode - missing or invalid cell note" );
205cdf0e10cSrcweir         if( pNote )
206cdf0e10cSrcweir         {
207cdf0e10cSrcweir             Rectangle aOldRect = pNote->CalcRectangle( rDoc, rPos );
208cdf0e10cSrcweir             Rectangle aNewRect = pObject->GetLogicRect();
209cdf0e10cSrcweir             if( aOldRect != aNewRect )
210cdf0e10cSrcweir             {
211cdf0e10cSrcweir                 pNote->UpdateFromRectangle( rDoc, rPos, aNewRect );
212cdf0e10cSrcweir                 OutlinerParaObject* pPObj = pCaption->GetOutlinerParaObject();
213cdf0e10cSrcweir                 bool bVertical = (pPObj && pPObj->IsVertical());
214cdf0e10cSrcweir                 // The new height/width is honoured if property item is reset.
215cdf0e10cSrcweir                 if(!bVertical && aNewRect.Bottom() - aNewRect.Top() > aOldRect.Bottom() - aOldRect.Top())
216cdf0e10cSrcweir                 {
217cdf0e10cSrcweir                     if(pCaption->IsAutoGrowHeight() && !bVertical)
218cdf0e10cSrcweir                     {
219cdf0e10cSrcweir                         pCaption->SetMergedItem( SdrTextAutoGrowHeightItem( false ) );
220cdf0e10cSrcweir                         aNote.SetItemSet( *pDoc, pCaption->GetMergedItemSet() );
221cdf0e10cSrcweir                     }
222cdf0e10cSrcweir                 }
223cdf0e10cSrcweir                 else if(bVertical && aNewRect.Right() - aNewRect.Left() > aOldRect.Right() - aOldRect.Left())
224cdf0e10cSrcweir                 {
225cdf0e10cSrcweir                     if(pCaption->IsAutoGrowWidth() && bVertical)
226cdf0e10cSrcweir                     {
227cdf0e10cSrcweir                         pCaption->SetMergedItem( SdrTextAutoGrowWidthItem( false ) );
228cdf0e10cSrcweir                         aNote.SetItemSet( *pDoc, pCaption->GetMergedItemSet() );
229cdf0e10cSrcweir                     }
230cdf0e10cSrcweir                 }
231cdf0e10cSrcweir                 pViewShell->SetNote( aTabPos.Col(), aTabPos.Row(), aTabPos.Tab(), aNote );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir                 // This repaint should not be necessary but it cleans
234cdf0e10cSrcweir                 // up the 'marks' left behind  by the note handles
235cdf0e10cSrcweir                 // now that notes can simultaineously have handles and edit active.
236cdf0e10cSrcweir                 ScRange aDrawRange = rDoc.GetRange( rPos.Tab(), aOldRect );
237cdf0e10cSrcweir                 // Set Start/End Row to previous/next row to allow for handles.
238cdf0e10cSrcweir                 if( aDrawRange.aStart.Row() > 0 )
239cdf0e10cSrcweir                     aDrawRange.aStart.IncRow( -1 );
240cdf0e10cSrcweir                 if( aDrawRange.aEnd.Row() < MAXROW )
241cdf0e10cSrcweir                     aDrawRange.aEnd.IncRow( 1 );
242cdf0e10cSrcweir                 ScDocShell* pDocSh = rViewData.GetDocShell();
243cdf0e10cSrcweir                 pDocSh->PostPaint( aDrawRange, PAINT_GRID| PAINT_EXTRAS);
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir #endif
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250