1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  */
11 
12 #include <PivotLayoutTreeListBase.hxx>
13 #include <PivotLayoutDialog.hxx>
14 
ScPivotLayoutTreeListBase(std::unique_ptr<weld::TreeView> xControl,SvPivotTreeListType eType)15 ScPivotLayoutTreeListBase::ScPivotLayoutTreeListBase(std::unique_ptr<weld::TreeView> xControl, SvPivotTreeListType eType)
16     : mxControl(std::move(xControl))
17     , maDropTargetHelper(*this)
18     , meType(eType)
19     , mpParent(nullptr)
20 {
21     mxControl->connect_focus_in(LINK(this, ScPivotLayoutTreeListBase, GetFocusHdl));
22     mxControl->connect_mnemonic_activate(LINK(this, ScPivotLayoutTreeListBase, MnemonicActivateHdl));
23     mxControl->connect_focus_out(LINK(this, ScPivotLayoutTreeListBase, LoseFocusHdl));
24 }
25 
~ScPivotLayoutTreeListBase()26 ScPivotLayoutTreeListBase::~ScPivotLayoutTreeListBase()
27 {
28 }
29 
Setup(ScPivotLayoutDialog * pParent)30 void ScPivotLayoutTreeListBase::Setup(ScPivotLayoutDialog* pParent)
31 {
32     mpParent = pParent;
33 }
34 
ScPivotLayoutTreeDropTarget(ScPivotLayoutTreeListBase & rTreeView)35 ScPivotLayoutTreeDropTarget::ScPivotLayoutTreeDropTarget(ScPivotLayoutTreeListBase& rTreeView)
36     : DropTargetHelper(rTreeView.get_widget().get_drop_target())
37     , m_rTreeView(rTreeView)
38 {
39 }
40 
AcceptDrop(const AcceptDropEvent & rEvt)41 sal_Int8 ScPivotLayoutTreeDropTarget::AcceptDrop(const AcceptDropEvent& rEvt)
42 {
43     // to enable the autoscroll when we're close to the edges
44     weld::TreeView& rWidget = m_rTreeView.get_widget();
45     rWidget.get_dest_row_at_pos(rEvt.maPosPixel, nullptr);
46     return DND_ACTION_MOVE;
47 }
48 
ExecuteDrop(const ExecuteDropEvent & rEvt)49 sal_Int8 ScPivotLayoutTreeDropTarget::ExecuteDrop( const ExecuteDropEvent& rEvt )
50 {
51     weld::TreeView& rWidget = m_rTreeView.get_widget();
52     weld::TreeView* pSource = rWidget.get_drag_source();
53     if (!pSource)
54         return DND_ACTION_NONE;
55 
56     std::unique_ptr<weld::TreeIter> xTarget(rWidget.make_iterator());
57     int nTargetPos = -1;
58     if (rWidget.get_dest_row_at_pos(rEvt.maPosPixel, xTarget.get()))
59         nTargetPos = rWidget.get_iter_index_in_parent(*xTarget);
60     m_rTreeView.InsertEntryForSourceTarget(*pSource, nTargetPos);
61     return DND_ACTION_NONE;
62 }
63 
PushEntriesToPivotFieldVector(ScPivotFieldVector & rVector)64 void ScPivotLayoutTreeListBase::PushEntriesToPivotFieldVector(ScPivotFieldVector& rVector)
65 {
66     std::unique_ptr<weld::TreeIter> xEachEntry(mxControl->make_iterator());
67     if (!mxControl->get_iter_first(*xEachEntry))
68         return;
69     do
70     {
71         ScItemValue* pItemValue = reinterpret_cast<ScItemValue*>(mxControl->get_id(*xEachEntry).toInt64());
72         ScPivotFuncData& rFunctionData = pItemValue->maFunctionData;
73 
74         ScPivotField aField;
75         aField.nCol          = rFunctionData.mnCol;
76         aField.mnOriginalDim = rFunctionData.mnOriginalDim;
77         aField.nFuncMask     = rFunctionData.mnFuncMask;
78         aField.mnDupCount    = rFunctionData.mnDupCount;
79         aField.maFieldRef    = rFunctionData.maFieldRef;
80         rVector.push_back(aField);
81     } while (mxControl->iter_next(*xEachEntry));
82 }
83 
InsertEntryForSourceTarget(weld::TreeView &,int)84 void ScPivotLayoutTreeListBase::InsertEntryForSourceTarget(weld::TreeView& /*pSource*/, int /*nTarget*/)
85 {
86 }
87 
RemoveEntryForItem(const ScItemValue * pItemValue)88 void ScPivotLayoutTreeListBase::RemoveEntryForItem(const ScItemValue* pItemValue)
89 {
90     OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pItemValue)));
91     int nPos = mxControl->find_id(sId);
92     if (nPos == -1)
93         return;
94     mxControl->remove(nPos);
95 }
96 
IMPL_LINK_NOARG(ScPivotLayoutTreeListBase,GetFocusHdl,weld::Widget &,void)97 IMPL_LINK_NOARG(ScPivotLayoutTreeListBase, GetFocusHdl, weld::Widget&, void)
98 {
99     if (!mpParent)
100         return;
101     mpParent->mpPreviouslyFocusedListBox = this;
102 }
103 
IMPL_LINK_NOARG(ScPivotLayoutTreeListBase,MnemonicActivateHdl,weld::Widget &,bool)104 IMPL_LINK_NOARG(ScPivotLayoutTreeListBase, MnemonicActivateHdl, weld::Widget&, bool)
105 {
106     if (!mpParent || !mpParent->mpPreviouslyFocusedListBox)
107         return false;
108     weld::TreeView& rSource = mpParent->mpPreviouslyFocusedListBox->get_widget();
109     int nEntry = rSource.get_cursor_index();
110     if (nEntry != -1)
111         InsertEntryForSourceTarget(rSource, -1);
112     return true;
113 }
114 
IMPL_LINK_NOARG(ScPivotLayoutTreeListBase,LoseFocusHdl,weld::Widget &,void)115 IMPL_LINK_NOARG(ScPivotLayoutTreeListBase, LoseFocusHdl, weld::Widget&, void)
116 {
117     if (!mpParent)
118         return;
119     mpParent->mpPreviouslyFocusedListBox = nullptr;
120 }
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
122