summaryrefslogtreecommitdiff
path: root/svtools/source/control/breadcrumb.cxx
blob: 7ed6d4671ea0909ff65f46e622a8f19e81deb2fe (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
/* -*- 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 <svtools/breadcrumb.hxx>

class CustomLink : public FixedHyperlink
{
public:
    CustomLink( vcl::Window* pParent, WinBits nWinStyle )
    : FixedHyperlink( pParent, nWinStyle )
    {
        vcl::Font aFont = GetControlFont( );
        aFont.SetUnderline( LINESTYLE_NONE );
        SetControlFont( aFont );
    }

protected:
    virtual void MouseMove( const MouseEvent& rMEvt ) override
    {
        // changes the style if the control is enabled
        if ( !rMEvt.IsLeaveWindow() && IsEnabled() )
        {
            vcl::Font aFont = GetControlFont( );
            aFont.SetUnderline( LINESTYLE_SINGLE );
            SetControlFont( aFont );
        }
        else
        {
            vcl::Font aFont = GetControlFont( );
            aFont.SetUnderline( LINESTYLE_NONE );
            SetControlFont( aFont );
        }

        FixedHyperlink::MouseMove( rMEvt );
    }
};

Breadcrumb::Breadcrumb( vcl::Window* pParent ) : VclHBox( pParent )
{
    m_eMode = SvtBreadcrumbMode::ONLY_CURRENT_PATH;
    set_spacing( SPACING );
    appendField(); // root
}

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

void Breadcrumb::dispose()
{
    for( std::vector<VclPtr<FixedHyperlink>>::size_type i = 0; i < m_aLinks.size(); i++ )
    {
        m_aSeparators[i].disposeAndClear();
        m_aLinks[i].disposeAndClear();
    }

    VclHBox::dispose();
}

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

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

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

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

    // we changed root - clear all fields
    for( std::vector<VclPtr<FixedHyperlink>>::size_type i = 1; i < m_aLinks.size(); i++ )
    {
        m_aLinks[i]->SetText( "" );

        m_aLinks[i]->Hide();
        m_aSeparators[i]->Hide();
        m_aLinks[i]->Enable();
    }
}

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::NO_DECODE );
    OUString sPath = aURL.GetURLPath(INetURLObject::DECODE_WITH_CHARSET);
    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_aLinks[0]->SetText( m_sRootName );
    m_aLinks[0]->Enable();
    m_aLinks[0]->SetURL( sRootPath );

    // fill the other fields

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

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

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

        m_aLinks[i]->SetText( sLabel );
        m_aLinks[i]->SetURL( sRootPath + OUString( sPath.getStr(), nEnd ) );
        m_aLinks[i]->Hide();
        m_aLinks[i]->Enable();

        m_aSeparators[i]->Hide();

        nPos = nEnd;
    }

    // clear unused fields

    for( std::vector<VclPtr<FixedHyperlink>>::size_type i = nSegments + 1; i < m_aLinks.size(); i++ )
    {
        if( bClear )
            m_aLinks[i]->SetText( "" );

        m_aLinks[i]->Hide();
        m_aSeparators[i]->Hide();
        m_aLinks[i]->Enable();
    }

    // show fields

    Resize();
    unsigned int nMaxWidth = GetSizePixel().Width();
    unsigned int nSeparatorWidth = m_aSeparators[0]->GetSizePixel().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, nMaxWidth - nCurrentWidth ) )
            {
                nCurrentWidth += m_aLinks[nIndex]->GetSizePixel().Width()
                                + nSeparatorWidth + 2*SPACING;
            }
            else
            {
                // label is too long
                if( nSegments != 0 )
                {
                    m_aLinks[0]->SetText( "..." );
                    m_aLinks[0]->Enable( false );
                }
                bLeft = false;
            }
        }

        if( nSegments + i == (int)m_aLinks.size() )
            bRight = false;

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

            if( m_aLinks[nIndex]->GetText().isEmpty() )
            {
                bRight = false;
            }
            else if( showField( nIndex, nMaxWidth - nCurrentWidth ) )
            {
                nCurrentWidth += m_aLinks[nIndex]->GetSizePixel().Width()
                                + nSeparatorWidth + 3*SPACING;
                nLastVisible = nIndex;
            }
            else
            {
                bRight = false;
            }
        }

        i++;
    }

    // current dir should be inactive
    m_aLinks[nSegments]->Enable( false );

    // hide last separator
    m_aSeparators[nLastVisible]->Hide();
}

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

void Breadcrumb::appendField()
{
    m_aLinks.push_back( VclPtr< CustomLink >::Create( this, WB_TABSTOP ) );
    m_aLinks[m_aLinks.size() - 1]->Hide();
    m_aLinks[m_aLinks.size() - 1]->SetClickHdl( LINK( this, Breadcrumb, ClickLinkHdl ) );

    m_aSeparators.push_back( VclPtr< FixedText >::Create( this ) );
    m_aSeparators[m_aLinks.size() - 1]->SetText( ">" );
    m_aSeparators[m_aLinks.size() - 1]->Hide();
}

bool Breadcrumb::showField( unsigned int nIndex, unsigned int nWidthMax )
{
    m_aLinks[nIndex]->Show();
    m_aSeparators[nIndex]->Show();

    unsigned int nSeparatorWidth = m_aSeparators[0]->GetSizePixel().Width();
    unsigned int nWidth = m_aLinks[nIndex]->GetSizePixel().Width()
            + nSeparatorWidth + 3*SPACING;

    if( nWidth > nWidthMax )
    {
        if( nIndex != 0 )
        {
            m_aLinks[nIndex]->Hide();
            m_aSeparators[nIndex]->Hide();
        }

        return false;
    }

    return true;
}

IMPL_LINK( Breadcrumb, ClickLinkHdl, FixedHyperlink&, rLink, void )
{
    m_sClickedURL = rLink.GetURL();
    m_aClickHdl.Call( this );
}

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