Adjust includes
[LibreOffice.git] / svx / source / svdraw / textchaincursor.cxx
blobd6d1f4de2894ccf3495ec70da50a101ad1384cc4
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 <svx/textchain.hxx>
21 #include <svx/textchaincursor.hxx>
22 #include <svx/svdedxv.hxx>
23 #include <svx/svdoutl.hxx>
25 // XXX: Possible duplication of code in behavior with stuff in ImpEditView (or ImpEditEngine) and OutlinerView
27 // XXX: We violate Demeter's Law several times here, I'm afraid
29 TextChainCursorManager::TextChainCursorManager(SdrObjEditView *pEditView, const SdrTextObj *pTextObj) :
30 mpEditView(pEditView),
31 mpTextObj(pTextObj),
32 mbHandlingDel(false)
34 assert(mpEditView);
35 assert(mpTextObj);
39 bool TextChainCursorManager::HandleKeyEvent( const KeyEvent& rKEvt )
41 ESelection aNewSel;
42 CursorChainingEvent aCursorEvent;
44 // check what the cursor/event situation looks like
45 bool bCompletelyHandled = false;
46 impDetectEvent(rKEvt, aCursorEvent, aNewSel, bCompletelyHandled);
48 if (aCursorEvent == CursorChainingEvent::NULL_EVENT)
49 return false;
50 else {
51 HandleCursorEvent(aCursorEvent, aNewSel);
52 // return value depends on the situation we are in
53 return bCompletelyHandled;
57 void TextChainCursorManager::impDetectEvent(const KeyEvent& rKEvt,
58 CursorChainingEvent& rOutCursorEvt,
59 ESelection& rOutSel,
60 bool& rOutHandled)
62 SdrOutliner *pOutl = mpEditView->GetTextEditOutliner();
63 OutlinerView *pOLV = mpEditView->GetTextEditOutlinerView();
65 SdrTextObj *pNextLink = mpTextObj->GetNextLinkInChain();
66 SdrTextObj *pPrevLink = mpTextObj->GetPrevLinkInChain();
68 KeyFuncType eFunc = rKEvt.GetKeyCode().GetFunction();
70 // We need to have this KeyFuncType
71 if (eFunc != KeyFuncType::DONTKNOW && eFunc != KeyFuncType::DELETE)
73 rOutCursorEvt = CursorChainingEvent::NULL_EVENT;
74 return;
77 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
78 ESelection aCurSel = pOLV->GetSelection();
80 ESelection aEndSelPrevBox(100000, 100000);
82 sal_Int32 nLastPara = pOutl->GetParagraphCount()-1;
83 OUString aLastParaText = pOutl->GetText(pOutl->GetParagraph(nLastPara));
84 sal_Int32 nLastParaLen = aLastParaText.getLength();
86 ESelection aEndSel = ESelection(nLastPara, nLastParaLen);
87 bool bAtEndOfTextContent = aCurSel == aEndSel;
89 // Possibility: Are we "pushing" at the end of the object?
90 if (nCode == KEY_RIGHT && bAtEndOfTextContent && pNextLink)
92 rOutCursorEvt = CursorChainingEvent::TO_NEXT_LINK;
93 // Selection unchanged: we are at the beginning of the box
94 rOutHandled = true; // Nothing more to do than move cursor
95 return;
98 // Possibility: Are we "pushing" at the end of the object?
99 if (eFunc == KeyFuncType::DELETE && bAtEndOfTextContent && pNextLink)
101 rOutCursorEvt = CursorChainingEvent::TO_NEXT_LINK;
102 // Selection unchanged: we are at the beginning of the box
103 rOutHandled = false; // We still need to delete the characters
104 mbHandlingDel = true;
105 return;
108 ESelection aStartSel = ESelection(0, 0);
109 bool bAtStartOfTextContent = aCurSel == aStartSel;
111 // Possibility: Are we "pushing" at the start of the object?
112 if (nCode == KEY_LEFT && bAtStartOfTextContent && pPrevLink)
114 rOutCursorEvt = CursorChainingEvent::TO_PREV_LINK;
115 rOutSel = aEndSelPrevBox; // Set at end of selection
116 rOutHandled = true; // Nothing more to do than move cursor
117 return;
120 // Possibility: Are we "pushing" at the start of the object and deleting left?
121 if (nCode == KEY_BACKSPACE && bAtStartOfTextContent && pPrevLink)
123 rOutCursorEvt = CursorChainingEvent::TO_PREV_LINK;
124 rOutSel = aEndSelPrevBox; // Set at end of selection
125 rOutHandled = false; // We need to delete characters after moving cursor
126 return;
129 // If arrived here there is no event detected
130 rOutCursorEvt = CursorChainingEvent::NULL_EVENT;
134 void TextChainCursorManager::HandleCursorEventAfterChaining(
135 const CursorChainingEvent aCurEvt,
136 const ESelection& aNewSel)
139 // Special case for DELETE handling: we need to get back at the end of the prev box
140 if (mbHandlingDel) {
141 // reset flag
142 mbHandlingDel = false;
144 // Move to end of prev box
145 SdrTextObj *pPrevLink = mpTextObj->GetPrevLinkInChain();
146 ESelection aEndSel(100000, 100000);
147 impChangeEditingTextObj(pPrevLink, aEndSel);
148 return;
151 // Standard handling
152 HandleCursorEvent(aCurEvt, aNewSel);
156 void TextChainCursorManager::HandleCursorEvent(
157 const CursorChainingEvent aCurEvt,
158 const ESelection& aNewSel)
162 OutlinerView* pOLV = mpEditView->GetTextEditOutlinerView();
163 SdrTextObj *pNextLink = mpTextObj->GetNextLinkInChain();
164 SdrTextObj *pPrevLink = mpTextObj->GetPrevLinkInChain();
167 switch ( aCurEvt ) {
168 case CursorChainingEvent::UNCHANGED:
169 // Set same selection as before the chaining (which is saved as PostChainingSel)
170 // We need an explicit set because the Outliner is messed up
171 // after text transfer and otherwise it brings us at arbitrary positions.
172 pOLV->SetSelection(aNewSel);
173 break;
174 case CursorChainingEvent::TO_NEXT_LINK:
175 mpTextObj->GetTextChain()->SetSwitchingToNextBox(mpTextObj, true);
176 impChangeEditingTextObj(pNextLink, aNewSel);
177 break;
178 case CursorChainingEvent::TO_PREV_LINK:
179 impChangeEditingTextObj(pPrevLink, aNewSel);
180 break;
181 case CursorChainingEvent::NULL_EVENT:
182 // Do nothing here
183 break;
188 void TextChainCursorManager::impChangeEditingTextObj(SdrTextObj *pTargetTextObj, ESelection aNewSel)
190 assert(pTargetTextObj);
192 // To ensure that we check for overflow in the next box // This is handled in SdrTextObj::EndTextEdit
193 SdrTextObj *pNextLink = mpTextObj->GetNextLinkInChain();
194 TextChain *pTextChain = mpTextObj->GetTextChain();
195 // If we are moving forward
196 if (pNextLink && pTargetTextObj == pNextLink)
197 pTextChain->SetPendingOverflowCheck(pNextLink, true);
199 mpEditView->SdrEndTextEdit();
200 mpEditView->SdrBeginTextEdit(pTargetTextObj);
201 // OutlinerView has changed, so we update the pointer
202 OutlinerView *pOLV = mpEditView->GetTextEditOutlinerView();
203 pOLV->SetSelection(aNewSel);
205 // Update reference text obj
206 mpTextObj = pTargetTextObj;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */