sc: factor out common code
[LibreOffice.git] / svx / source / svdraw / textchaincursor.cxx
blobf9bb278f4272a7438a1a8da7e67c0aee4046cdc2
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(ESelection::AtEnd());
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 impChangeEditingTextObj(pPrevLink, ESelection::AtEnd());
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 mpEditView->SdrEndTextEdit();
193 mpEditView->SdrBeginTextEdit(pTargetTextObj);
194 // OutlinerView has changed, so we update the pointer
195 OutlinerView *pOLV = mpEditView->GetTextEditOutlinerView();
196 pOLV->SetSelection(aNewSel);
198 // Update reference text obj
199 mpTextObj = pTargetTextObj;
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */