bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / wrtsh / delete.cxx
blobf19455e0d6811826659859890bdc4b0f40693e0d
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 .
20 #include <wrtsh.hxx>
21 #include <crsskip.hxx>
22 #include <swcrsr.hxx>
23 #include <editeng/lrspitem.hxx> // #i23725#
24 // #134369#
25 #include <view.hxx>
26 #include <drawbase.hxx>
28 inline void SwWrtShell::OpenMark()
30 StartAllAction();
31 ResetCursorStack();
32 KillPams();
33 SetMark();
36 inline void SwWrtShell::CloseMark( bool bOkFlag )
38 if( bOkFlag )
39 UpdateAttr();
40 else
41 SwapPam();
43 ClearMark();
44 EndAllAction();
47 // #i23725#
48 bool SwWrtShell::TryRemoveIndent()
50 bool bResult = false;
52 SfxItemSet aAttrSet(GetAttrPool(), RES_LR_SPACE, RES_LR_SPACE);
53 GetCurAttr(aAttrSet);
55 SvxLRSpaceItem aItem = (const SvxLRSpaceItem &)aAttrSet.Get(RES_LR_SPACE);
56 short aOldFirstLineOfst = aItem.GetTxtFirstLineOfst();
58 if (aOldFirstLineOfst > 0)
60 aItem.SetTxtFirstLineOfst(0);
61 bResult = true;
63 else if (aOldFirstLineOfst < 0)
65 aItem.SetTxtFirstLineOfst(0);
66 aItem.SetLeft(aItem.GetLeft() + aOldFirstLineOfst);
68 bResult = true;
70 else if (aItem.GetLeft() != 0)
72 aItem.SetLeft(0);
73 bResult = true;
76 if (bResult)
78 aAttrSet.Put(aItem);
79 SetAttr(aAttrSet);
82 return bResult;
85 /** Description: Erase the line. */
87 long SwWrtShell::DelLine()
89 SwActContext aActContext(this);
90 ResetCursorStack();
91 // remember the old cursor
92 Push();
93 ClearMark();
94 SwCrsrShell::LeftMargin();
95 SetMark();
96 SwCrsrShell::RightMargin();
98 long nRet = Delete();
99 Pop(sal_False);
100 if( nRet )
101 UpdateAttr();
102 return nRet;
107 long SwWrtShell::DelToStartOfLine()
109 OpenMark();
110 SwCrsrShell::LeftMargin();
111 long nRet = Delete();
112 CloseMark( 0 != nRet );
113 return nRet;
118 long SwWrtShell::DelToEndOfLine()
120 OpenMark();
121 SwCrsrShell::RightMargin();
122 long nRet = Delete();
123 CloseMark( 0 != nRet );
124 return 1;
127 long SwWrtShell::DelLeft()
129 // If it's a Fly, throw it away
130 int nSelType = GetSelectionType();
131 const int nCmp = nsSelectionType::SEL_FRM | nsSelectionType::SEL_GRF | nsSelectionType::SEL_OLE | nsSelectionType::SEL_DRW;
132 if( nCmp & nSelType )
134 // #108205# Remember object's position.
135 Point aTmpPt = GetObjRect().TopLeft();
137 DelSelectedObj();
139 // #108205# Set cursor to remembered position.
140 SetCrsr(&aTmpPt);
142 LeaveSelFrmMode();
143 UnSelectFrm();
145 nSelType = GetSelectionType();
146 if ( nCmp & nSelType )
148 EnterSelFrmMode();
149 GotoNextFly();
152 return 1L;
155 // If a selection exists, erase this
156 if ( IsSelection() )
158 if( !IsBlockMode() || HasSelection() )
160 //OS: Once again Basic: SwActContext must be leaved
161 //before EnterStdMode!
163 SwActContext aActContext(this);
164 ResetCursorStack();
165 Delete();
166 UpdateAttr();
168 if( IsBlockMode() )
170 NormalizePam();
171 ClearMark();
172 EnterBlockMode();
174 else
175 EnterStdMode();
176 return 1L;
178 else
179 EnterStdMode();
182 // JP 29.06.95: never erase a table standing in front of it.
183 bool bSwap = false;
184 const SwTableNode * pWasInTblNd = SwCrsrShell::IsCrsrInTbl();
186 if( SwCrsrShell::IsSttPara())
188 // #i4032# Don't actually call a 'delete' if we
189 // changed the table cell, compare DelRight().
190 const SwStartNode * pSNdOld = pWasInTblNd ?
191 GetSwCrsr()->GetNode()->FindTableBoxStartNode() :
194 // If the cursor is at the beginning of a paragraph, try to step
195 // backwards. On failure we are done.
196 if( !SwCrsrShell::Left(1,CRSR_SKIP_CHARS) )
197 return 0;
199 // If the cursor entered or left a table (or both) we are done. No step
200 // back.
201 const SwTableNode* pIsInTblNd = SwCrsrShell::IsCrsrInTbl();
202 if( pIsInTblNd != pWasInTblNd )
203 return 0;
205 const SwStartNode* pSNdNew = pIsInTblNd ?
206 GetSwCrsr()->GetNode()->FindTableBoxStartNode() :
209 // #i4032# Don't actually call a 'delete' if we
210 // changed the table cell, compare DelRight().
211 if ( pSNdOld != pSNdNew )
212 return 0;
214 OpenMark();
215 SwCrsrShell::Right(1,CRSR_SKIP_CHARS);
216 SwCrsrShell::SwapPam();
217 bSwap = true;
219 else
221 OpenMark();
222 SwCrsrShell::Left(1,CRSR_SKIP_CHARS);
224 long nRet = Delete();
225 if( !nRet && bSwap )
226 SwCrsrShell::SwapPam();
227 CloseMark( 0 != nRet );
228 return nRet;
231 long SwWrtShell::DelRight()
233 // Will be or'ed, if a tableselection exists;
234 // will here be implemented on nsSelectionType::SEL_TBL
235 long nRet = 0;
236 int nSelection = GetSelectionType();
237 if(nSelection & nsSelectionType::SEL_TBL_CELLS)
238 nSelection = nsSelectionType::SEL_TBL;
239 if(nSelection & nsSelectionType::SEL_TXT)
240 nSelection = nsSelectionType::SEL_TXT;
242 const SwTableNode * pWasInTblNd = NULL;
244 switch( nSelection & ~(nsSelectionType::SEL_BEZ) )
246 case nsSelectionType::SEL_POSTIT:
247 case nsSelectionType::SEL_TXT:
248 case nsSelectionType::SEL_TBL:
249 case nsSelectionType::SEL_NUM:
250 // If a selection exists, erase it.
251 if( IsSelection() )
253 if( !IsBlockMode() || HasSelection() )
255 //OS: And once again Basic: SwActContext must be
256 //leaved before EnterStdMode !
258 SwActContext aActContext(this);
259 ResetCursorStack();
260 Delete();
261 UpdateAttr();
263 if( IsBlockMode() )
265 NormalizePam();
266 ClearMark();
267 EnterBlockMode();
269 else
270 EnterStdMode();
271 nRet = 1L;
272 break;
274 else
275 EnterStdMode();
278 pWasInTblNd = IsCrsrInTbl();
280 if( nsSelectionType::SEL_TXT & nSelection && SwCrsrShell::IsSttPara() &&
281 SwCrsrShell::IsEndPara() )
283 // save cursor
284 SwCrsrShell::Push();
286 bool bDelFull = false;
287 if ( SwCrsrShell::Right(1,CRSR_SKIP_CHARS) )
289 const SwTableNode * pCurrTblNd = IsCrsrInTbl();
290 bDelFull = pCurrTblNd && pCurrTblNd != pWasInTblNd;
293 // restore cursor
294 SwCrsrShell::Pop( sal_False );
296 if( bDelFull )
298 DelFullPara();
299 UpdateAttr();
300 break;
305 // #108049# Save the startnode of the current cell
306 const SwStartNode * pSNdOld;
307 pSNdOld = GetSwCrsr()->GetNode()->
308 FindTableBoxStartNode();
310 if ( SwCrsrShell::IsEndPara() )
312 // #i41424# Introduced a couple of
313 // Push()-Pop() pairs here. The reason for this is that a
314 // Right()-Left() combination does not make sure, that
315 // the cursor will be in its initial state, because there
316 // may be a numbering in front of the next paragraph.
317 SwCrsrShell::Push();
319 if ( SwCrsrShell::Right(1, CRSR_SKIP_CHARS) )
321 if (IsCrsrInTbl() || (pWasInTblNd != IsCrsrInTbl()))
323 /** #108049# Save the startnode of the current
324 cell. May be different to pSNdOld as we have
325 moved. */
326 const SwStartNode * pSNdNew = GetSwCrsr()
327 ->GetNode()->FindTableBoxStartNode();
329 /** #108049# Only move instead of deleting if we
330 have moved to a different cell */
331 if (pSNdOld != pSNdNew)
333 SwCrsrShell::Pop( sal_True );
334 break;
339 // restore cursor
340 SwCrsrShell::Pop( sal_False );
344 OpenMark();
345 SwCrsrShell::Right(1,CRSR_SKIP_CELLS);
346 nRet = Delete();
347 CloseMark( 0 != nRet );
348 break;
350 case nsSelectionType::SEL_FRM:
351 case nsSelectionType::SEL_GRF:
352 case nsSelectionType::SEL_OLE:
353 case nsSelectionType::SEL_DRW:
354 case nsSelectionType::SEL_DRW_TXT:
355 case nsSelectionType::SEL_DRW_FORM:
357 // #108205# Remember object's position.
358 Point aTmpPt = GetObjRect().TopLeft();
360 DelSelectedObj();
362 // #108205# Set cursor to remembered position.
363 SetCrsr(&aTmpPt);
365 LeaveSelFrmMode();
366 UnSelectFrm();
367 // #134369#
368 OSL_ENSURE( !IsFrmSelected(),
369 "<SwWrtShell::DelRight(..)> - <SwWrtShell::UnSelectFrm()> should unmark all objects" );
370 // #134369#
371 // leave draw mode, if necessary.
373 if (GetView().GetDrawFuncPtr())
375 GetView().GetDrawFuncPtr()->Deactivate();
376 GetView().SetDrawFuncPtr(NULL);
378 if ( GetView().IsDrawMode() )
380 GetView().LeaveDrawCreate();
385 // #134369#
386 // <IsFrmSelected()> can't be true - see above.
388 nSelection = GetSelectionType();
389 if ( nsSelectionType::SEL_FRM & nSelection ||
390 nsSelectionType::SEL_GRF & nSelection ||
391 nsSelectionType::SEL_OLE & nSelection ||
392 nsSelectionType::SEL_DRW & nSelection )
394 EnterSelFrmMode();
395 GotoNextFly();
398 nRet = 1;
399 break;
401 return nRet;
404 long SwWrtShell::DelToEndOfPara()
406 SwActContext aActContext(this);
407 ResetCursorStack();
408 Push();
409 SetMark();
410 if( !MovePara(fnParaCurr,fnParaEnd))
412 Pop(sal_False);
413 return 0;
415 long nRet = Delete();
416 Pop(sal_False);
417 if( nRet )
418 UpdateAttr();
419 return nRet;
422 long SwWrtShell::DelToStartOfPara()
424 SwActContext aActContext(this);
425 ResetCursorStack();
426 Push();
427 SetMark();
428 if( !MovePara(fnParaCurr,fnParaStart))
430 Pop(sal_False);
431 return 0;
433 long nRet = Delete();
434 Pop(sal_False);
435 if( nRet )
436 UpdateAttr();
437 return nRet;
440 // All erase operations should work with Find instead with
441 // Nxt-/PrvDelim, because the latter works with Wrap Around
442 // -- that's probably not wished.
444 long SwWrtShell::DelToStartOfSentence()
446 if(IsStartOfDoc())
447 return 0;
448 OpenMark();
449 long nRet = _BwdSentence() ? Delete() : 0;
450 CloseMark( 0 != nRet );
451 return nRet;
454 long SwWrtShell::DelToEndOfSentence()
456 if(IsEndOfDoc())
457 return 0;
458 OpenMark();
459 long nRet(0);
460 // fdo#60967: special case that is documented in help: delete
461 // paragraph following table if cursor is at end of last cell in table
462 if (IsEndOfTable())
464 Push();
465 ClearMark();
466 if (SwCrsrShell::Right(1,CRSR_SKIP_CHARS))
468 SetMark();
469 SwCrsrShell::MovePara(fnParaCurr, fnParaEnd);
470 if (!IsEndOfDoc()) // do not delete last paragraph in body text
472 nRet = DelFullPara() ? 1 : 0;
475 Pop(false);
477 else
479 nRet = _FwdSentence() ? Delete() : 0;
481 CloseMark( 0 != nRet );
482 return nRet;
485 long SwWrtShell::DelNxtWord()
487 if(IsEndOfDoc())
488 return 0;
489 SwActContext aActContext(this);
490 ResetCursorStack();
491 EnterStdMode();
492 SetMark();
493 if(IsEndWrd() && !IsSttWrd())
494 _NxtWrdForDelete(); // #i92468#
495 if(IsSttWrd() || IsEndPara())
496 _NxtWrdForDelete(); // #i92468#
497 else
498 _EndWrd();
500 long nRet = Delete();
501 if( nRet )
502 UpdateAttr();
503 else
504 SwapPam();
505 ClearMark();
506 return nRet;
509 long SwWrtShell::DelPrvWord()
511 if(IsStartOfDoc())
512 return 0;
513 SwActContext aActContext(this);
514 ResetCursorStack();
515 EnterStdMode();
516 SetMark();
517 if ( !IsSttWrd() ||
518 !_PrvWrdForDelete() ) // #i92468#
520 if( IsEndWrd() )
522 if ( _PrvWrdForDelete() ) // #i92468#
524 // skip over all-1 spaces
525 short n = -1;
526 while( ' ' == GetChar( sal_False, n ))
527 --n;
529 if( ++n )
530 ExtendSelection( sal_False, -n );
533 else if( IsSttPara())
534 _PrvWrdForDelete(); // #i92468#
535 else
536 _SttWrd();
538 long nRet = Delete();
539 if( nRet )
540 UpdateAttr();
541 else
542 SwapPam();
543 ClearMark();
544 return nRet;
550 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */