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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <uielement/comboboxtoolbarcontroller.hxx>

#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/util/Color.hpp>

#include <vcl/InterimItemWindow.hxx>
#include <svtools/toolboxcontroller.hxx>
#include <vcl/svapp.hxx>
#include <vcl/toolbox.hxx>

using namespace ::com::sun::star;
using namespace css::uno;
using namespace css::beans;
using namespace css::lang;
using namespace css::frame;
using namespace css::util;

namespace framework
{

// Wrapper class to notify controller about events from combobox.
// Unfortunately the events are notified through virtual methods instead
// of Listeners.

class ComboBoxControl final : public InterimItemWindow
{
public:
    ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController);
    virtual ~ComboBoxControl() override;
    virtual void dispose() override;

    void set_active_or_entry_text(const OUString& rText);
    OUString get_active_text() const { return m_xWidget->get_active_text(); }

    void clear() { m_xWidget->clear(); }
    void remove(int nIndex) { m_xWidget->remove(nIndex); }
    void append_text(const OUString& rStr) { m_xWidget->append_text(rStr); }
    void insert_text(int pos, const OUString& rStr) { m_xWidget->insert_text(pos, rStr); }
    int get_count() const { return m_xWidget->get_count(); }
    int find_text(const OUString& rStr) const { return m_xWidget->find_text(rStr); }

    DECL_LINK(FocusInHdl, weld::Widget&, void);
    DECL_LINK(FocusOutHdl, weld::Widget&, void);
    DECL_LINK(ModifyHdl, weld::ComboBox&, void);
    DECL_LINK(ActivateHdl, weld::ComboBox&, bool);

private:
    std::unique_ptr<weld::ComboBox> m_xWidget;
    ComboboxToolbarController* m_pComboboxToolbarController;
};

ComboBoxControl::ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController)
    : InterimItemWindow(pParent, "svt/ui/combocontrol.ui", "ComboControl")
    , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
    , m_pComboboxToolbarController(pComboboxToolbarController)
{
    m_xWidget->connect_focus_in(LINK(this, ComboBoxControl, FocusInHdl));
    m_xWidget->connect_focus_out(LINK(this, ComboBoxControl, FocusOutHdl));
    m_xWidget->connect_changed(LINK(this, ComboBoxControl, ModifyHdl));
    m_xWidget->connect_entry_activate(LINK(this, ComboBoxControl, ActivateHdl));

    m_xWidget->set_entry_width_chars(1); // so a smaller than default width can be used by ComboboxToolbarController
    SetSizePixel(get_preferred_size());
}

void ComboBoxControl::set_active_or_entry_text(const OUString& rText)
{
    const int nFound = m_xWidget->find_text(rText);
    if (nFound != -1)
        m_xWidget->set_active(nFound);
    else
        m_xWidget->set_entry_text(rText);
}

ComboBoxControl::~ComboBoxControl()
{
    disposeOnce();
}

void ComboBoxControl::dispose()
{
    m_pComboboxToolbarController = nullptr;
    m_xWidget.reset();
    InterimItemWindow::dispose();
}

IMPL_LINK_NOARG(ComboBoxControl, ModifyHdl, weld::ComboBox&, void)<--- syntax error
{
    if (m_pComboboxToolbarController)
    {
        if (m_xWidget->get_count() && m_xWidget->changed_by_direct_pick())
            m_pComboboxToolbarController->Select();
        else
            m_pComboboxToolbarController->Modify();
    }
}

IMPL_LINK_NOARG(ComboBoxControl, FocusInHdl, weld::Widget&, void)
{
    if (m_pComboboxToolbarController)
        m_pComboboxToolbarController->GetFocus();
}

IMPL_LINK_NOARG(ComboBoxControl, FocusOutHdl, weld::Widget&, void)
{
    if (m_pComboboxToolbarController)
        m_pComboboxToolbarController->LoseFocus();
}

IMPL_LINK_NOARG(ComboBoxControl, ActivateHdl, weld::ComboBox&, bool)
{
    if (m_pComboboxToolbarController)
        m_pComboboxToolbarController->Activate();
    return true;
}

ComboboxToolbarController::ComboboxToolbarController(
    const Reference< XComponentContext >& rxContext,
    const Reference< XFrame >&            rFrame,
    ToolBox*                              pToolbar,
    sal_uInt16                            nID,
    sal_Int32                             nWidth,
    const OUString&                       aCommand ) :
    ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
    ,   m_pComboBox( nullptr )
{
    m_pComboBox = VclPtr<ComboBoxControl>::Create(m_xToolbar, this);
    if ( nWidth == 0 )
        nWidth = 100;

    // ComboBoxControl ctor has set a suitable height already
    auto nHeight = m_pComboBox->GetSizePixel().Height();

    m_pComboBox->SetSizePixel( ::Size( nWidth, nHeight ));
    m_xToolbar->SetItemWindow( m_nID, m_pComboBox );
}

ComboboxToolbarController::~ComboboxToolbarController()
{
}

void SAL_CALL ComboboxToolbarController::dispose()
{
    SolarMutexGuard aSolarMutexGuard;

    m_xToolbar->SetItemWindow( m_nID, nullptr );
    m_pComboBox.disposeAndClear();

    ComplexToolbarController::dispose();
}

Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
{
    Sequence<PropertyValue> aArgs( 2 );
    OUString aSelectedText = m_pComboBox->get_active_text();

    // Add key modifier to argument list
    aArgs[0].Name = "KeyModifier";
    aArgs[0].Value <<= KeyModifier;
    aArgs[1].Name = "Text";
    aArgs[1].Value <<= aSelectedText;
    return aArgs;
}

void ComboboxToolbarController::Select()
{
    vcl::Window::PointerState aState = m_pComboBox->GetPointerState();

    sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODIFIERS_MASK );
    execute( nKeyModifier );
}

void ComboboxToolbarController::Modify()
{
    notifyTextChanged(m_pComboBox->get_active_text());
}

void ComboboxToolbarController::GetFocus()
{
    notifyFocusGet();
}

void ComboboxToolbarController::LoseFocus()
{
    notifyFocusLost();
}

void ComboboxToolbarController::Activate()
{
    // Call execute only with non-empty text
    if (!m_pComboBox->get_active_text().isEmpty())
        execute(0);
}

void ComboboxToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
{
    if ( rControlCommand.Command == "SetText" )
    {
        for ( const NamedValue& rArg : rControlCommand.Arguments )
        {
            if ( rArg.Name == "Text" )
            {
                OUString aText;
                rArg.Value >>= aText;
                m_pComboBox->set_active_or_entry_text(aText);

                // send notification
                notifyTextChanged( aText );
                break;
            }
        }
    }
    else if ( rControlCommand.Command == "SetList" )
    {
        for ( const NamedValue& rArg : rControlCommand.Arguments )
        {
            if ( rArg.Name == "List" )
            {
                Sequence< OUString > aList;
                m_pComboBox->clear();

                rArg.Value >>= aList;
                for (OUString const & rName : std::as_const(aList))
                    m_pComboBox->append_text(rName);

                // send notification
                uno::Sequence< beans::NamedValue > aInfo { { "List", css::uno::makeAny(aList) } };
                addNotifyInfo( "ListChanged",
                               getDispatchFromCommand( m_aCommandURL ),
                               aInfo );

                break;
            }
        }
    }
    else if ( rControlCommand.Command == "AddEntry" )
    {
        OUString   aText;
        for ( const NamedValue& rArg : rControlCommand.Arguments )
        {
            if ( rArg.Name == "Text" )
            {
                if ( rArg.Value >>= aText )
                    m_pComboBox->append_text(aText);
                break;
            }
        }
    }
    else if ( rControlCommand.Command == "InsertEntry" )
    {
        sal_Int32 nPos(-1);
        OUString   aText;
        for ( const NamedValue& rArg : rControlCommand.Arguments )
        {
            if ( rArg.Name == "Pos" )
            {
                sal_Int32 nTmpPos = 0;
                if ( rArg.Value >>= nTmpPos )
                {
                    if (( nTmpPos >= 0 ) &&
                        ( nTmpPos < m_pComboBox->get_count() ))
                        nPos = nTmpPos;
                }
            }
            else if ( rArg.Name == "Text" )
                rArg.Value >>= aText;
        }

        m_pComboBox->insert_text(nPos, aText);
    }
    else if ( rControlCommand.Command == "RemoveEntryPos" )
    {
        for ( const NamedValue& rArg : rControlCommand.Arguments )
        {
            if ( rArg.Name == "Pos" )
            {
                sal_Int32 nPos( -1 );
                if ( rArg.Value >>= nPos )
                {
                    if (0 <= nPos && nPos < m_pComboBox->get_count())
                        m_pComboBox->remove(nPos);
                }
                break;
            }
        }
    }
    else if ( rControlCommand.Command == "RemoveEntryText" )
    {
        for ( const NamedValue& rArg : rControlCommand.Arguments )
        {
            if ( rArg.Name == "Text")
            {
                OUString aText;
                if ( rArg.Value >>= aText )
                {
                    auto nPos = m_pComboBox->find_text(aText);
                    if (nPos != -1)
                        m_pComboBox->remove(nPos);
                }
                break;
            }
        }
    }
}

} // namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */