summaryrefslogtreecommitdiffstats
path: root/extensions/source/propctrlr/browserline.cxx
blob: 814c24f4b306f01aeb279624e706319b0a13af95 (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* -*- 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 "browserline.hxx"

#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/inspection/PropertyLineElement.hpp>
#include <com/sun/star/graphic/GraphicProvider.hpp>
#include <com/sun/star/graphic/XGraphicProvider.hpp>

#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <comphelper/string.hxx>
#include <tools/debug.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/settings.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <vcl/weldutils.hxx>

namespace pcr
{


    using ::com::sun::star::uno::Reference;
    using ::com::sun::star::uno::XComponentContext;
    using ::com::sun::star::inspection::XPropertyControl;
    using ::com::sun::star::inspection::XPropertyControlContext;
    using ::com::sun::star::uno::UNO_QUERY_THROW;
    using ::com::sun::star::uno::Exception;
    using ::com::sun::star::graphic::GraphicProvider;
    using ::com::sun::star::graphic::XGraphicProvider;
    using ::com::sun::star::uno::Sequence;
    using ::com::sun::star::beans::PropertyValue;
    using ::com::sun::star::graphic::XGraphic;

    namespace PropertyLineElement = ::com::sun::star::inspection::PropertyLineElement;

    OBrowserLine::OBrowserLine(const OUString& rEntryName, weld::Container* pParent, weld::SizeGroup* pLabelGroup,
                               weld::Container* pInitialControlParent)
        : m_sEntryName(rEntryName)
        , m_xBuilder(Application::CreateBuilder(pParent, "modules/spropctrlr/ui/browserline.ui"))
        , m_xContainer(m_xBuilder->weld_container("BrowserLine"))
        , m_xFtTitle(m_xBuilder->weld_label("label"))
        , m_xBrowseButton(m_xBuilder->weld_button("browse"))
        , m_xAdditionalBrowseButton(m_xBuilder->weld_button("morebrowse"))
        , m_pInitialControlParent(pInitialControlParent) // controls start with this as their parent and need to be moved into m_xContainer
        , m_pParent(pParent)
        , m_pControlWindow( nullptr )
        , m_pBrowseButton(nullptr)
        , m_pAdditionalBrowseButton( nullptr )
        , m_pClickListener( nullptr )
        , m_nNameWidth(0)
        , m_nEnableFlags( 0xFFFF )
        , m_bIndentTitle( false )
        , m_bReadOnly( false )
    {
        pLabelGroup->add_widget(m_xFtTitle.get());
    }

    OBrowserLine::~OBrowserLine()
    {
        implHideBrowseButton(true);
        implHideBrowseButton(false);
        m_pParent->move(m_xContainer.get(), nullptr);
    }

    void OBrowserLine::IndentTitle( bool _bIndent )
    {
        if ( m_bIndentTitle != _bIndent )
        {
            m_bIndentTitle = _bIndent;
        }
    }

    void OBrowserLine::SetComponentHelpIds(const OString& rHelpId)
    {
        if (m_pControlWindow)
            m_pControlWindow->set_help_id(rHelpId);

        if ( m_pBrowseButton )
        {
            m_pBrowseButton->set_help_id(rHelpId);

            if ( m_pAdditionalBrowseButton )
            {
                m_pAdditionalBrowseButton->set_help_id(rHelpId);
            }
        }
    }

    void OBrowserLine::setControl( const Reference< XPropertyControl >& rxControl )
    {
        m_xControl = rxControl;
        auto xWindow = m_xControl->getControlWindow();
        if (weld::TransportAsXWindow* pTunnel = dynamic_cast<weld::TransportAsXWindow*>(xWindow.get()))
            m_pControlWindow = pTunnel->getWidget();
        else
            m_pControlWindow = nullptr;
        DBG_ASSERT( m_pControlWindow, "OBrowserLine::setControl: setting NULL controls/windows is not allowed!" );

        if ( m_pControlWindow )
        {
            m_pInitialControlParent->move(m_pControlWindow, m_xContainer.get());
            m_pControlWindow->set_grid_left_attach(1);
            m_xFtTitle->set_mnemonic_widget(m_pControlWindow);
            m_pControlWindow->show();
        }
    }

    bool OBrowserLine::GrabFocus()
    {
        bool bRes=false;

        if (m_pControlWindow && m_pControlWindow->get_sensitive())
        {
            m_pControlWindow->grab_focus();
            bRes = true;
        }
        else if ( m_pAdditionalBrowseButton && m_pAdditionalBrowseButton->get_sensitive() )
        {
            m_pAdditionalBrowseButton->grab_focus();
            bRes = true;
        }
        else if ( m_pBrowseButton && m_pBrowseButton->get_sensitive() )
        {
            m_pBrowseButton->grab_focus();
            bRes = true;
        }
        return bRes;
    }

    void OBrowserLine::Show(bool bFlag)
    {
        m_xFtTitle->set_visible(bFlag);
        if (m_pControlWindow)
            m_pControlWindow->set_visible( bFlag );
        if ( m_pBrowseButton )
            m_pBrowseButton->set_visible( bFlag );
        if ( m_pAdditionalBrowseButton )
            m_pAdditionalBrowseButton->set_visible( bFlag );
    }

    void OBrowserLine::Hide()
    {
        Show(false);
    }

    void OBrowserLine::SetTitle(const OUString& rNewTitle )
    {
        if ( GetTitle() == rNewTitle )
            return;
        m_xFtTitle->set_label( rNewTitle );
        if (m_pControlWindow)
            m_pControlWindow->set_accessible_name(rNewTitle);
        if ( m_pBrowseButton )
            m_pBrowseButton->set_accessible_name( rNewTitle );
        FullFillTitleString();
    }

    void OBrowserLine::FullFillTitleString()
    {
        OUStringBuffer aText(m_xFtTitle->get_label());

        int n10DotsWidth = m_xFtTitle->get_pixel_size("..........").Width();
        int nTextWidth = m_xFtTitle->get_pixel_size(OUString::unacquired(aText)).Width();
        int nDiff = m_nNameWidth - nTextWidth;
        int nExtraChars = (nDiff * 10) / n10DotsWidth;
        for (int i = 0; i < nExtraChars; ++i)
            aText.append(".");

        // for Issue 69452
        if (AllSettings::GetLayoutRTL())
        {
            sal_Unicode const cRTL_mark = 0x200F;
            aText.append( cRTL_mark );
        }

        m_xFtTitle->set_label(aText.makeStringAndClear());
    }

    OUString OBrowserLine::GetTitle() const
    {
        OUString sDisplayName = m_xFtTitle->get_label();

        // for Issue 69452
        if (AllSettings::GetLayoutRTL())
        {
            sal_Unicode const cRTL_mark = 0x200F;
            sDisplayName = comphelper::string::stripEnd(sDisplayName, cRTL_mark);
        }

        sDisplayName = comphelper::string::stripEnd(sDisplayName, '.');

        return sDisplayName;
    }

    void OBrowserLine::SetReadOnly( bool _bReadOnly )
    {
        if ( m_bReadOnly != _bReadOnly )
        {
            m_bReadOnly = _bReadOnly;
            implUpdateEnabledDisabled();
        }
    }

    namespace
    {
        void implSetBitIfAffected(sal_uInt16& nEnabledBits, sal_Int16 _nAffectedMask, sal_Int16 _nTestBit, bool _bSet)
        {
            if ( _nAffectedMask & _nTestBit )
            {
                if ( _bSet )
                    nEnabledBits |= _nTestBit;
                else
                    nEnabledBits &= ~_nTestBit;
            }
        }

        void implEnable(weld::Widget* pWindow, bool bEnable)
        {
            // tdf#138131 get_sensitive comparison as bodge for
            // vcl's recursive Enable behavior
            if (pWindow && pWindow->get_sensitive() != bEnable)
                pWindow->set_sensitive(bEnable);
        }

        void implEnable(weld::Widget* pWindow, sal_uInt16 nEnabledBits, sal_uInt16 nMatchBits)
        {
            bool bEnable = ((nEnabledBits & nMatchBits) == nMatchBits);
            implEnable(pWindow, bEnable);
        }
    }

    void OBrowserLine::implUpdateEnabledDisabled()
    {
        implEnable( m_xFtTitle.get(),           m_nEnableFlags, PropertyLineElement::CompleteLine );
        if ( m_pControlWindow )
            implEnable( m_pControlWindow,       m_nEnableFlags, PropertyLineElement::CompleteLine | PropertyLineElement::InputControl );

        if ( m_bReadOnly )
        {
            implEnable( m_pBrowseButton,            false );
            implEnable( m_pAdditionalBrowseButton,  false );
        }
        else
        {
            implEnable( m_pBrowseButton,            m_nEnableFlags, PropertyLineElement::CompleteLine | PropertyLineElement::PrimaryButton );
            implEnable( m_pAdditionalBrowseButton,  m_nEnableFlags, PropertyLineElement::CompleteLine | PropertyLineElement::SecondaryButton );
        }
    }

    void OBrowserLine::EnablePropertyLine( bool _bEnable )
    {
        implSetBitIfAffected( m_nEnableFlags, PropertyLineElement::CompleteLine, PropertyLineElement::CompleteLine, _bEnable );
        implUpdateEnabledDisabled();
    }


    void OBrowserLine::EnablePropertyControls( sal_Int16 _nControls, bool _bEnable )
    {
        implSetBitIfAffected( m_nEnableFlags, _nControls, PropertyLineElement::InputControl, _bEnable );
        implSetBitIfAffected( m_nEnableFlags, _nControls, PropertyLineElement::PrimaryButton, _bEnable );
        implSetBitIfAffected( m_nEnableFlags, _nControls, PropertyLineElement::SecondaryButton, _bEnable );
        implUpdateEnabledDisabled();
    }

    weld::Button& OBrowserLine::impl_ensureButton(bool bPrimary)
    {
        weld::Button* pButton;
        if (bPrimary)
            pButton = m_pBrowseButton;
        else
            pButton = m_pAdditionalBrowseButton;

        if (!pButton )
        {
            if (bPrimary)
                pButton = m_pBrowseButton = m_xBrowseButton.get();
            else
                pButton = m_pAdditionalBrowseButton = m_xAdditionalBrowseButton.get();
            pButton->connect_focus_in(LINK(this, OBrowserLine, OnButtonFocus));
            pButton->connect_clicked(LINK(this, OBrowserLine, OnButtonClicked));
        }

        pButton->show();

        return *pButton;
    }

    void OBrowserLine::ShowBrowseButton( const OUString& rImageURL, bool bPrimary )
    {
        weld::Button& rButton( impl_ensureButton( bPrimary ) );

        OSL_PRECOND( !rImageURL.isEmpty(), "OBrowserLine::ShowBrowseButton: use the other version if you don't have an image!" );
        Reference<XGraphic> xGraphic;
        try
        {
            Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
            Reference< XGraphicProvider > xGraphicProvider( GraphicProvider::create(xContext) );

            Sequence aMediaProperties{ comphelper::makePropertyValue("URL", rImageURL) };

            xGraphic = Reference<XGraphic>(xGraphicProvider->queryGraphic(aMediaProperties), css::uno::UNO_SET_THROW);
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
        }

        rButton.set_image(xGraphic);
    }

    void OBrowserLine::ShowBrowseButton(const css::uno::Reference<css::graphic::XGraphic>& rGraphic, bool bPrimary)
    {
        weld::Button& rButton( impl_ensureButton( bPrimary ) );
        rButton.set_image(rGraphic);
    }

    void OBrowserLine::ShowBrowseButton( bool bPrimary )
    {
        impl_ensureButton(bPrimary);
    }

    void OBrowserLine::implHideBrowseButton(bool bPrimary)
    {
        if (bPrimary)
        {
            if (m_pBrowseButton)
            {
                m_pBrowseButton->hide();
                m_pBrowseButton->connect_focus_in(Link<weld::Widget&, void>());
                m_pBrowseButton = nullptr;
            }
        }
        else
        {
            if (m_pAdditionalBrowseButton)
            {
                m_pAdditionalBrowseButton->hide();
                m_pAdditionalBrowseButton->connect_focus_in(Link<weld::Widget&, void>());
                m_pAdditionalBrowseButton = nullptr;
            }
        }
    }

    void OBrowserLine::HideBrowseButton(bool bPrimary)
    {
        implHideBrowseButton(bPrimary);
    }

    void OBrowserLine::SetTitleWidth(sal_uInt16 nWidth)
    {
        int nMinDotsWidth = m_xFtTitle->get_pixel_size("...").Width();
        if (m_nNameWidth != nWidth + nMinDotsWidth)
            m_nNameWidth = nWidth + nMinDotsWidth;
        FullFillTitleString();
    }

    void OBrowserLine::SetClickListener( IButtonClickListener* _pListener )
    {
        m_pClickListener = _pListener;
    }

    IMPL_LINK(OBrowserLine, OnButtonClicked, weld::Button&, rButton, void)
    {
        if ( m_pClickListener )
            m_pClickListener->buttonClicked(this, &rButton == m_pBrowseButton);
    }

    IMPL_LINK_NOARG( OBrowserLine, OnButtonFocus, weld::Widget&, void )
    {
        if ( m_xControl.is() )
        {
            try
            {
                Reference< XPropertyControlContext > xContext( m_xControl->getControlContext(), css::uno::UNO_SET_THROW );
                xContext->focusGained( m_xControl );
            }
            catch( const Exception& )
            {
                DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
            }
        }
    }

} // namespace pcr

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