summaryrefslogtreecommitdiff
path: root/fpicker/source/office/breadcrumb.cxx
blob: 1216a543a00d794944036447408b46df5b6cfe96 (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
/* -*- 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/.
 */

#include <tools/urlobj.hxx>
#include <vcl/svapp.hxx>
#include "breadcrumb.hxx"

Breadcrumb::Breadcrumb(weld::Container* pParent)
    : m_pParent(pParent)
    , m_nMaxWidth(m_pParent->get_preferred_size().Width())
{
    m_pParent->connect_size_allocate(LINK(this, Breadcrumb, SizeAllocHdl));
    m_eMode = SvtBreadcrumbMode::ONLY_CURRENT_PATH;
    appendField(); // root
}

IMPL_LINK(Breadcrumb, SizeAllocHdl, const Size&, rSize, void)
{
    m_nMaxWidth = rSize.Width();
}

Breadcrumb::~Breadcrumb()
{
    m_pParent->connect_size_allocate(Link<const Size&, void>());
}

void Breadcrumb::EnableFields( bool bEnable )
{
    if( bEnable )
    {
        INetURLObject aURL( m_aCurrentURL );
        int nSegments = aURL.getSegmentCount();
        m_aSegments[nSegments]->m_xLink->set_sensitive(false);
    }
}

void Breadcrumb::connect_clicked( const Link<Breadcrumb*,void>& rLink )
{
    m_aClickHdl = rLink;
}

const OUString& Breadcrumb::GetHdlURL() const
{
    return m_sClickedURL;
}

void Breadcrumb::SetRootName( const OUString& rURL )
{
    m_sRootName = rURL;

    // we changed root - clear all fields
    for (size_t i = 1; i < m_aSegments.size(); ++i)
    {
        m_aSegments[i]->m_xLink->set_label("");

        m_aSegments[i]->m_xLink->hide();
        m_aSegments[i]->m_xSeparator->hide();
        m_aSegments[i]->m_xLink->set_sensitive(true);
    }
}

void Breadcrumb::SetURL( const OUString& rURL )
{
    m_aCurrentURL = rURL;
    INetURLObject aURL( rURL );
    aURL.setFinalSlash();
    //prepare the Host port
    OUString sHostPort;

    if( aURL.HasPort() )
    {
        sHostPort += ":";
        sHostPort += OUString::number( aURL.GetPort() );
    }

    OUString sUser = aURL.GetUser( INetURLObject::DecodeMechanism::NONE );
    OUString sPath = aURL.GetURLPath(INetURLObject::DecodeMechanism::WithCharset);
    OUString sRootPath = INetURLObject::GetScheme( aURL.GetProtocol() )
                        + sUser
                        + ( sUser.isEmpty() ? OUString() : "@" )
                        + aURL.GetHost()
                        + sHostPort;

    int nSegments = aURL.getSegmentCount();
    unsigned int nPos = 0;

    bool bClear = ( m_eMode == SvtBreadcrumbMode::ONLY_CURRENT_PATH );

    // root field
    m_aSegments[0]->m_xLink->set_label( m_sRootName );
    m_aSegments[0]->m_xLink->set_sensitive(true);
    m_aSegments[0]->m_xLink->set_uri(sRootPath);
    m_aUris[m_aSegments[0]->m_xLink.get()] = sRootPath;

    // fill the other fields

    for( unsigned int i = 1; i < static_cast<unsigned int>(nSegments) + 1; i++ )
    {
        if( i >= m_aSegments.size() )
            appendField();

        unsigned int nEnd = sPath.indexOf( '/', nPos + 1 );
        OUString sLabel = sPath.copy( nPos + 1, nEnd - nPos - 1 );

        if( m_eMode == SvtBreadcrumbMode::ALL_VISITED )
        {
            if( m_aSegments[i]->m_xLink->get_label() != sLabel )
                bClear = true;
        }

        m_aSegments[i]->m_xLink->set_label( sLabel );
        m_aUris[m_aSegments[i]->m_xLink.get()] = sRootPath + sPath.copy(0, nEnd);
        m_aSegments[i]->m_xLink->hide();
        m_aSegments[i]->m_xLink->set_sensitive(true);

        m_aSegments[i]->m_xSeparator->hide();

        nPos = nEnd;
    }

    // clear unused fields
    for (size_t i = nSegments + 1; i < m_aSegments.size(); i++ )
    {
        if( bClear )
            m_aSegments[i]->m_xLink->set_label( "" );

        m_aSegments[i]->m_xLink->hide();
        m_aSegments[i]->m_xSeparator->hide();
        m_aSegments[i]->m_xLink->set_sensitive(true);
    }

    // show fields
    unsigned int nSeparatorWidth = m_aSegments[0]->m_xSeparator->get_preferred_size().Width();
    unsigned int nCurrentWidth = 0;
    unsigned int nLastVisible = nSegments;

    bool bRight = ( m_eMode == SvtBreadcrumbMode::ALL_VISITED );
    bool bLeft = true;

    int i = 0;

    while( bLeft || bRight )
    {
        if( nSegments - i == -1 )
            bLeft = false;

        if( bLeft )
        {
            unsigned int nIndex = nSegments - i;

            if( showField( nIndex, m_nMaxWidth - nCurrentWidth ) )
            {
                nCurrentWidth += m_aSegments[nIndex]->m_xLink->get_preferred_size().Width()
                                + nSeparatorWidth + 2*SPACING;
            }
            else
            {
                // label is too long
                if( nSegments != 0 )
                {
                    m_aSegments[0]->m_xLink->set_label("...");
                    m_aSegments[0]->m_xLink->set_sensitive(false);
                }
                bLeft = false;
            }
        }

        if( nSegments + i == static_cast<int>(m_aSegments.size()) )
            bRight = false;

        if( i != 0 && bRight )
        {
            unsigned int nIndex = nSegments + i;

            if( m_aSegments[nIndex]->m_xLink->get_label().isEmpty() )
            {
                bRight = false;
            }
            else if( showField( nIndex, m_nMaxWidth - nCurrentWidth ) )
            {
                nCurrentWidth += m_aSegments[nIndex]->m_xLink->get_preferred_size().Width()
                                + nSeparatorWidth + 3*SPACING;
                nLastVisible = nIndex;
            }
            else
            {
                bRight = false;
            }
        }

        i++;
    }

    // current dir should be inactive
    m_aSegments[nSegments]->m_xLink->set_sensitive(false);

    // hide last separator
    m_aSegments[nLastVisible]->m_xSeparator->hide();
}

void Breadcrumb::SetMode( SvtBreadcrumbMode eMode )
{
    m_eMode = eMode;
}

void Breadcrumb::appendField()
{
    m_aSegments.emplace_back(std::make_unique<BreadcrumbPath>(m_pParent));
    size_t nIndex = m_aSegments.size() - 1;
    m_aSegments[nIndex]->m_xLink->hide();
    m_aSegments[nIndex]->m_xLink->connect_clicked( LINK( this, Breadcrumb, ClickLinkHdl ) );
    m_aSegments[nIndex]->m_xSeparator->set_label( ">" );
    m_aSegments[nIndex]->m_xSeparator->hide();
}

bool Breadcrumb::showField( unsigned int nIndex, unsigned int nWidthMax )
{
    m_aSegments[nIndex]->m_xLink->show();
    m_aSegments[nIndex]->m_xSeparator->show();

    unsigned int nSeparatorWidth = m_aSegments[0]->m_xSeparator->get_preferred_size().Width();
    unsigned int nWidth = m_aSegments[nIndex]->m_xLink->get_preferred_size().Width()
            + nSeparatorWidth + 3*SPACING;

    if( nWidth > nWidthMax )
    {
        if( nIndex != 0 )
        {
            m_aSegments[nIndex]->m_xLink->hide();
            m_aSegments[nIndex]->m_xSeparator->hide();
        }

        return false;
    }

    return true;
}

IMPL_LINK( Breadcrumb, ClickLinkHdl, weld::LinkButton&, rLink, void )
{
    m_sClickedURL = m_aUris[&rLink];
    m_aClickHdl.Call( this );
}

BreadcrumbPath::BreadcrumbPath(weld::Container* pContainer)
    : m_xBuilder(Application::CreateBuilder(pContainer, "fps/ui/breadcrumb.ui"))
    , m_xContainer(m_xBuilder->weld_container("container"))
    , m_xLink(m_xBuilder->weld_link_button("link"))
    , m_xSeparator(m_xBuilder->weld_label("label"))
{
}

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