Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / svdraw / textchaincursor.cxx
blob51c4d1d8ef8799df4b2b69bedbd4fe8d7c1dc94f
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 <textchain.hxx>
21 #include <textchaincursor.hxx>
22 #include <svx/svdedxv.hxx>
23 #include <svx/svdoutl.hxx>
24 #include <vcl/event.hxx>
26 // XXX: Possible duplication of code in behavior with stuff in ImpEditView (or ImpEditEngine) and OutlinerView
28 // XXX: We violate Demeter's Law several times here, I'm afraid
30 TextChainCursorManager::TextChainCursorManager(SdrObjEditView *pEditView, const SdrTextObj *pTextObj) :
31 mpEditView(pEditView),
32 mpTextObj(pTextObj),
33 mbHandlingDel(false)
35 assert(mpEditView);
36 assert(mpTextObj);
40 bool TextChainCursorManager::HandleKeyEvent( const KeyEvent& rKEvt )
42 ESelection aNewSel;
43 CursorChainingEvent aCursorEvent;
45 // check what the cursor/event situation looks like
46 bool bCompletelyHandled = false;
47 impDetectEvent(rKEvt, aCursorEvent, aNewSel, bCompletelyHandled);
49 if (aCursorEvent == CursorChainingEvent::NULL_EVENT)
50 return false;
51 else {
52 HandleCursorEvent(aCursorEvent, aNewSel);
53 // return value depends on the situation we are in
54 return bCompletelyHandled;
58 void TextChainCursorManager::impDetectEvent(const KeyEvent& rKEvt,
59 CursorChainingEvent& rOutCursorEvt,
60 ESelection& rOutSel,
61 bool& rOutHandled)
63 SdrOutliner *pOutl = mpEditView->GetTextEditOutliner();
64 OutlinerView *pOLV = mpEditView->GetTextEditOutlinerView();
66 SdrTextObj *pNextLink = mpTextObj->GetNextLinkInChain();
67 SdrTextObj *pPrevLink = mpTextObj->GetPrevLinkInChain();
69 KeyFuncType eFunc = rKEvt.GetKeyCode().GetFunction();
71 // We need to have this KeyFuncType
72 if (eFunc != KeyFuncType::DONTKNOW && eFunc != KeyFuncType::DELETE)
74 rOutCursorEvt = CursorChainingEvent::NULL_EVENT;
75 return;
78 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
79 ESelection aCurSel = pOLV->GetSelection();
81 ESelection aEndSelPrevBox(100000, 100000);
83 sal_Int32 nLastPara = pOutl->GetParagraphCount()-1;
84 OUString aLastParaText = pOutl->GetText(pOutl->GetParagraph(nLastPara));
85 sal_Int32 nLastParaLen = aLastParaText.getLength();
87 ESelection aEndSel(nLastPara, nLastParaLen);
88 bool bAtEndOfTextContent = aCurSel == aEndSel;
90 // Possibility: Are we "pushing" at the end of the object?
91 if (nCode == KEY_RIGHT && bAtEndOfTextContent && pNextLink)
93 rOutCursorEvt = CursorChainingEvent::TO_NEXT_LINK;
94 // Selection unchanged: we are at the beginning of the box
95 rOutHandled = true; // Nothing more to do than move cursor
96 return;
99 // Possibility: Are we "pushing" at the end of the object?
100 if (eFunc == KeyFuncType::DELETE && bAtEndOfTextContent && pNextLink)
102 rOutCursorEvt = CursorChainingEvent::TO_NEXT_LINK;
103 // Selection unchanged: we are at the beginning of the box
104 rOutHandled = false; // We still need to delete the characters
105 mbHandlingDel = true;
106 return;
109 ESelection aStartSel(0, 0);
110 bool bAtStartOfTextContent = aCurSel == aStartSel;
112 // Possibility: Are we "pushing" at the start of the object?
113 if (nCode == KEY_LEFT && bAtStartOfTextContent && pPrevLink)
115 rOutCursorEvt = CursorChainingEvent::TO_PREV_LINK;
116 rOutSel = aEndSelPrevBox; // Set at end of selection
117 rOutHandled = true; // Nothing more to do than move cursor
118 return;
121 // Possibility: Are we "pushing" at the start of the object and deleting left?
122 if (nCode == KEY_BACKSPACE && bAtStartOfTextContent && pPrevLink)
124 rOutCursorEvt = CursorChainingEvent::TO_PREV_LINK;
125 rOutSel = aEndSelPrevBox; // Set at end of selection
126 rOutHandled = false; // We need to delete characters after moving cursor
127 return;
130 // If arrived here there is no event detected
131 rOutCursorEvt = CursorChainingEvent::NULL_EVENT;
135 void TextChainCursorManager::HandleCursorEventAfterChaining(
136 const CursorChainingEvent aCurEvt,
137 const ESelection& aNewSel)
140 // Special case for DELETE handling: we need to get back at the end of the prev box
141 if (mbHandlingDel) {
142 // reset flag
143 mbHandlingDel = false;
145 // Move to end of prev box
146 SdrTextObj *pPrevLink = mpTextObj->GetPrevLinkInChain();
147 ESelection aEndSel(100000, 100000);
148 impChangeEditingTextObj(pPrevLink, aEndSel);
149 return;
152 // Standard handling
153 HandleCursorEvent(aCurEvt, aNewSel);
157 void TextChainCursorManager::HandleCursorEvent(
158 const CursorChainingEvent aCurEvt,
159 const ESelection& aNewSel)
163 OutlinerView* pOLV = mpEditView->GetTextEditOutlinerView();
164 SdrTextObj *pNextLink = mpTextObj->GetNextLinkInChain();
165 SdrTextObj *pPrevLink = mpTextObj->GetPrevLinkInChain();
168 switch ( aCurEvt ) {
169 case CursorChainingEvent::UNCHANGED:
170 // Set same selection as before the chaining (which is saved as PostChainingSel)
171 // We need an explicit set because the Outliner is messed up
172 // after text transfer and otherwise it brings us at arbitrary positions.
173 pOLV->SetSelection(aNewSel);
174 break;
175 case CursorChainingEvent::TO_NEXT_LINK:
176 mpTextObj->GetTextChain()->SetSwitchingToNextBox(mpTextObj, true);
177 impChangeEditingTextObj(pNextLink, aNewSel);
178 break;
179 case CursorChainingEvent::TO_PREV_LINK:
180 impChangeEditingTextObj(pPrevLink, aNewSel);
181 break;
182 case CursorChainingEvent::NULL_EVENT:
183 // Do nothing here
184 break;
189 void TextChainCursorManager::impChangeEditingTextObj(SdrTextObj *pTargetTextObj, ESelection aNewSel)
191 assert(pTargetTextObj);
193 mpEditView->SdrEndTextEdit();
194 mpEditView->SdrBeginTextEdit(pTargetTextObj);
195 // OutlinerView has changed, so we update the pointer
196 OutlinerView *pOLV = mpEditView->GetTextEditOutlinerView();
197 pOLV->SetSelection(aNewSel);
199 // Update reference text obj
200 mpTextObj = pTargetTextObj;
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */