1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <DropDownFormFieldDialog.hxx>
#include <vcl/event.hxx>
#include <IMark.hxx>
#include <xmloff/odffields.hxx>
#include <vcl/svapp.hxx>
#include <strings.hrc>
#include <swtypes.hxx>

namespace sw
{
DropDownFormFieldDialog::DropDownFormFieldDialog(weld::Widget* pParent,
                                                 mark::IFieldmark* pDropDownField)
    : GenericDialogController(pParent, "modules/swriter/ui/dropdownformfielddialog.ui",
                              "DropDownFormFieldDialog")
    , m_pDropDownField(pDropDownField)
    , m_bListHasChanged(false)
    , m_xListItemEntry(m_xBuilder->weld_entry("item_entry"))
    , m_xListAddButton(m_xBuilder->weld_button("add_button"))
    , m_xListItemsTreeView(m_xBuilder->weld_tree_view("items_treeview"))
    , m_xListRemoveButton(m_xBuilder->weld_button("remove_button"))
    , m_xListUpButton(m_xBuilder->weld_button("up_button"))
    , m_xListDownButton(m_xBuilder->weld_button("down_button"))
{
    m_xListItemEntry->connect_key_press(LINK(this, DropDownFormFieldDialog, KeyPressedHdl));
    m_xListItemEntry->connect_changed(LINK(this, DropDownFormFieldDialog, EntryChangedHdl));

    m_xListItemsTreeView->set_size_request(m_xListItemEntry->get_preferred_size().Width(),
                                           m_xListItemEntry->get_preferred_size().Height() * 5);
    m_xListItemsTreeView->connect_changed(LINK(this, DropDownFormFieldDialog, ListChangedHdl));

    Link<weld::Button&, void> aPushButtonLink(LINK(this, DropDownFormFieldDialog, ButtonPushedHdl));
    m_xListAddButton->connect_clicked(aPushButtonLink);
    m_xListRemoveButton->connect_clicked(aPushButtonLink);
    m_xListUpButton->connect_clicked(aPushButtonLink);
    m_xListDownButton->connect_clicked(aPushButtonLink);

    InitControls();
}

DropDownFormFieldDialog::~DropDownFormFieldDialog() {}

IMPL_LINK_NOARG(DropDownFormFieldDialog, ListChangedHdl, weld::TreeView&, void) { UpdateButtons(); }<--- syntax error

IMPL_LINK(DropDownFormFieldDialog, KeyPressedHdl, const KeyEvent&, rEvent, bool)
{
    if (rEvent.GetKeyCode().GetCode() == KEY_RETURN && !m_xListItemEntry->get_text().isEmpty())
    {
        AppendItemToList();
        return true;
    }
    return false;
}

IMPL_LINK_NOARG(DropDownFormFieldDialog, EntryChangedHdl, weld::Entry&, void) { UpdateButtons(); }

IMPL_LINK(DropDownFormFieldDialog, ButtonPushedHdl, weld::Button&, rButton, void)
{
    if (&rButton == m_xListAddButton.get())
    {
        AppendItemToList();
    }
    else if (m_xListItemsTreeView->get_selected_index() != -1)
    {
        int nSelPos = m_xListItemsTreeView->get_selected_index();
        if (&rButton == m_xListRemoveButton.get())
        {
            m_xListItemsTreeView->remove(nSelPos);
            if (m_xListItemsTreeView->n_children() > 0)
                m_xListItemsTreeView->select(nSelPos ? nSelPos - 1 : 0);
        }
        else if (&rButton == m_xListUpButton.get())
        {
            const OUString sEntry = m_xListItemsTreeView->get_selected_text();
            m_xListItemsTreeView->remove(nSelPos);
            nSelPos--;
            m_xListItemsTreeView->insert_text(nSelPos, sEntry);
            m_xListItemsTreeView->select(nSelPos);
        }
        else if (&rButton == m_xListDownButton.get())
        {
            const OUString sEntry = m_xListItemsTreeView->get_selected_text();
            m_xListItemsTreeView->remove(nSelPos);
            nSelPos++;
            m_xListItemsTreeView->insert_text(nSelPos, sEntry);
            m_xListItemsTreeView->select(nSelPos);
        }
        m_bListHasChanged = true;
    }
    UpdateButtons();
}

void DropDownFormFieldDialog::InitControls()
{
    if (m_pDropDownField != nullptr)
    {
        const mark::IFieldmark::parameter_map_t* const pParameters
            = m_pDropDownField->GetParameters();

        auto pListEntries = pParameters->find(ODF_FORMDROPDOWN_LISTENTRY);
        if (pListEntries != pParameters->end())
        {
            css::uno::Sequence<OUString> vListEntries;
            pListEntries->second >>= vListEntries;
            for (const OUString& rItem : std::as_const(vListEntries))
                m_xListItemsTreeView->append_text(rItem);

            // Select the current one
            auto pResult = pParameters->find(ODF_FORMDROPDOWN_RESULT);
            if (pResult != pParameters->end())
            {
                sal_Int32 nSelection = -1;
                pResult->second >>= nSelection;
                if (vListEntries.getLength() > nSelection)
                    m_xListItemsTreeView->select_text(vListEntries[nSelection]);
            }
        }
    }
    UpdateButtons();
}

void DropDownFormFieldDialog::AppendItemToList()
{
    if (m_xListAddButton->get_sensitive())
    {
        if (m_xListItemsTreeView->n_children() >= ODF_FORMDROPDOWN_ENTRY_COUNT_LIMIT)
        {
            std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(
                m_xDialog.get(), VclMessageType::Info, VclButtonsType::Ok,
                SwResId(STR_DROP_DOWN_FIELD_ITEM_LIMIT)));
            xInfoBox->run();
            return;
        }

        const OUString sEntry(m_xListItemEntry->get_text());
        if (!sEntry.isEmpty())
        {
            m_xListItemsTreeView->append_text(sEntry);
            m_xListItemsTreeView->select_text(sEntry);
            m_bListHasChanged = true;

            // Clear entry
            m_xListItemEntry->set_text(OUString());
            m_xListItemEntry->grab_focus();
        }
        UpdateButtons();
    }
}

void DropDownFormFieldDialog::UpdateButtons()
{
    m_xListAddButton->set_sensitive(!m_xListItemEntry->get_text().isEmpty()
                                    && m_xListItemsTreeView->find_text(m_xListItemEntry->get_text())
                                           == -1);

    int nSelPos = m_xListItemsTreeView->get_selected_index();
    m_xListRemoveButton->set_sensitive(nSelPos != -1);
    m_xListUpButton->set_sensitive(nSelPos > 0);
    m_xListDownButton->set_sensitive(nSelPos != -1
                                     && nSelPos < m_xListItemsTreeView->n_children() - 1);
}

void DropDownFormFieldDialog::Apply()
{
    if (m_pDropDownField != nullptr && m_bListHasChanged)
    {
        mark::IFieldmark::parameter_map_t* pParameters = m_pDropDownField->GetParameters();

        css::uno::Sequence<OUString> vListEntries(m_xListItemsTreeView->n_children());
        for (int nIndex = 0; nIndex < m_xListItemsTreeView->n_children(); ++nIndex)
        {
            vListEntries[nIndex] = m_xListItemsTreeView->get_text(nIndex);
        }

        if (m_xListItemsTreeView->n_children() != 0)
        {
            (*pParameters)[ODF_FORMDROPDOWN_LISTENTRY] <<= vListEntries;
        }
        else
        {
            pParameters->erase(ODF_FORMDROPDOWN_LISTENTRY);
        }

        // After editing the drop down field's list we don't specify the selected item
        pParameters->erase(ODF_FORMDROPDOWN_RESULT);
    }
}

} // namespace sw

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */