summaryrefslogtreecommitdiff
path: root/vcl/inc/vcl/arrange.hxx
blob: f98197231be9f51a49aebed28dee84edb84c928c (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef _VCL_ARRANGE_HXX
#define _VCL_ARRANGE_HXX

#include "vcl/window.hxx"

#include <vector>
#include <map>
#include <boost/shared_ptr.hpp>

namespace vcl
{
    /* some helper classes for simple window layouting
       guidelines:
       - a WindowArranger is not a Window
       - a WindowArranger hierarchy manages exactly one level of child windows inside a common parent
         this is to keep the vcl Window hierarchy flat, as some code like accelerators depend on such behavior
       - a WindowArranger never becomes owner of a Window, windows need to be destroyed separately
       - a WindowArranger however always is owner of its child WindowArrangers, that is the
         WindowArranger hierarchy will keep track of its objects and delete them
       - a managed element of a WindowArranger can either be a Window (a leaf in the hierarchy)
         or a child WindowArranger (a node in the hierarchy), but never both
    */

    class VCL_DLLPUBLIC WindowArranger
    {
    protected:
        struct Element
        {
            Window*                            m_pElement;
            boost::shared_ptr<WindowArranger>  m_pChild;
            sal_Int32                          m_nExpandPriority;
            Size                               m_aMinSize;
            bool                               m_bHidden;
            long                               m_nLeftBorder;
            long                               m_nTopBorder;
            long                               m_nRightBorder;
            long                               m_nBottomBorder;

            Element()
            : m_pElement( NULL )
            , m_pChild()
            , m_nExpandPriority( 0 )
            , m_bHidden( false )
            , m_nLeftBorder( 0 )
            , m_nTopBorder( 0 )
            , m_nRightBorder( 0 )
            , m_nBottomBorder( 0 )
            {}

            Element( Window* i_pWin,
                     boost::shared_ptr<WindowArranger> const & i_pChild = boost::shared_ptr<WindowArranger>(),
                     sal_Int32 i_nExpandPriority = 0
                   )
            : m_pElement( i_pWin )
            , m_pChild( i_pChild )
            , m_nExpandPriority( i_nExpandPriority )
            , m_bHidden( false )
            , m_nLeftBorder( 0 )
            , m_nTopBorder( 0 )
            , m_nRightBorder( 0 )
            , m_nBottomBorder( 0 )
            {}

            void deleteChild() { m_pChild.reset(); }

            sal_Int32 getExpandPriority() const;
            Size getOptimalSize( WindowSizeType ) const;
            bool isVisible() const;
            void setPosSize( const Point&, const Size& );
        };

        Window*                     m_pParentWindow;
        WindowArranger*             m_pParentArranger;
        Rectangle                   m_aManagedArea;
        long                        m_nOuterBorder;

        virtual Element* getElement( size_t i_nIndex ) = 0;
        const Element* getConstElement( size_t i_nIndex ) const
        { return const_cast<WindowArranger*>(this)->getElement( i_nIndex ); }


    public:
        WindowArranger( WindowArranger* i_pParent = NULL )
        : m_pParentWindow( i_pParent ? i_pParent->m_pParentWindow : NULL )
        , m_pParentArranger( i_pParent )
        , m_nOuterBorder( 0 )
        {}
        virtual ~WindowArranger();

        // ask what would be the optimal size
        virtual Size getOptimalSize( WindowSizeType ) const = 0;
        // call Resize to trigger layouting inside the managed area
        // without function while parent window is unset
        virtual void resize() = 0;
        // avoid this if possible, using the constructor instead
        // there can be only one parent window and all managed windows MUST
        // be direct children of that window
        // violating that condition will result in undefined behavior
        virtual void setParentWindow( Window* );

        virtual void setParent( WindowArranger* );

        virtual size_t countElements() const = 0;
        boost::shared_ptr<WindowArranger> getChild( size_t i_nIndex ) const
        {
            const Element* pEle = getConstElement( i_nIndex );
            return pEle ? pEle->m_pChild : boost::shared_ptr<WindowArranger>();
        }
        Window* getWindow( size_t i_nIndex ) const
        {
            const Element* pEle = getConstElement( i_nIndex );
            return pEle ? pEle->m_pElement : NULL;
        }

        virtual bool isVisible() const; // true if any element is visible

        sal_Int32 getExpandPriority( size_t i_nIndex ) const
        {
            const Element* pEle = getConstElement( i_nIndex );
            return pEle ? pEle->getExpandPriority() : 0;
        }

        Size getMinimumSize( size_t i_nIndex ) const
        {
            const Element* pEle = getConstElement( i_nIndex );
            return pEle ? pEle->m_aMinSize : Size();
        }

        bool setMinimumSize( size_t i_nIndex, const Size& i_rMinSize )
        {
            Element* pEle = getElement( i_nIndex );
            if( pEle )
                pEle->m_aMinSize = i_rMinSize;
            return pEle != NULL;
        }

        void setBorders( size_t i_nIndex, long i_nLeft, long i_nTop, long i_nRight, long i_nBottom  )
        {
            Element* pEle = getElement( i_nIndex );
            if( pEle )
            {
                pEle->m_nLeftBorder   = i_nLeft;
                pEle->m_nRightBorder  = i_nRight;
                pEle->m_nTopBorder    = i_nTop;
                pEle->m_nBottomBorder = i_nBottom;
            }
        }

        void show( bool i_bShow = true, bool i_bImmediateUpdate = true );

        void setManagedArea( const Rectangle& i_rArea )
        {
            m_aManagedArea = i_rArea;
            resize();
        }
        const Rectangle& getManagedArea() const { return m_aManagedArea; }

        void setOuterBorder( long i_nBorder )
        {
            m_nOuterBorder = i_nBorder;
            resize();
        }
    };

    class VCL_DLLPUBLIC RowOrColumn : public WindowArranger
    {
        long    m_nBorderWidth;
        bool    m_bColumn;

        std::vector< WindowArranger::Element > m_aElements;

        void distributeRowWidth( std::vector< Size >& io_rSizes, long i_nUsedWidth, long i_nExtraWidth );
        void distributeColumnHeight( std::vector< Size >& io_rSizes, long i_nUsedHeight, long i_nExtraHeight );
    protected:
        virtual Element* getElement( size_t i_nIndex )
        { return i_nIndex < m_aElements.size() ? &m_aElements[ i_nIndex ] : 0; }

    public:
        RowOrColumn( WindowArranger* i_pParent = NULL,
                     bool bColumn = true, long i_nBorderWidth = 5 )
        : WindowArranger( i_pParent )
        , m_nBorderWidth( i_nBorderWidth )
        , m_bColumn( bColumn )
        {}

        virtual ~RowOrColumn();

        virtual Size getOptimalSize( WindowSizeType ) const;
        virtual void resize();
        virtual size_t countElements() const { return m_aElements.size(); }

        // add a managed window at the given index
        // an index smaller than zero means add the window at the end
        size_t addWindow( Window*, sal_Int32 i_nExpandPrio = 0, size_t i_nIndex = ~0 );
        void remove( Window* );

        size_t addChild( boost::shared_ptr<WindowArranger> const &, sal_Int32 i_nExpandPrio = 0, size_t i_nIndex = ~0 );
        // convenience: use for addChild( new WindowArranger( ... ) ) constructs
        size_t addChild( WindowArranger* i_pNewChild, sal_Int32 i_nExpandPrio = 0, size_t i_nIndex = ~0 )
        { return addChild( boost::shared_ptr<WindowArranger>( i_pNewChild ), i_nExpandPrio, i_nIndex ); }
        void remove( boost::shared_ptr<WindowArranger> const & );

        long getBorderWidth() const { return m_nBorderWidth; }
    };

    class VCL_DLLPUBLIC LabeledElement : public WindowArranger
    {
        WindowArranger::Element m_aLabel;
        WindowArranger::Element m_aElement;
        long                    m_nDistance;
        long                    m_nLabelColumnWidth;
        int                     m_nLabelStyle;
    protected:
        virtual Element* getElement( size_t i_nIndex )
        {
            if( i_nIndex == 0 )
                return &m_aLabel;
            else if( i_nIndex == 1 )
                return &m_aElement;
            return 0;
        }

    public:
        LabeledElement( WindowArranger* i_pParent = NULL, int i_nLabelStyle = 0, long i_nDistance = 5 )
        : WindowArranger( i_pParent )
        , m_nDistance( i_nDistance )
        , m_nLabelColumnWidth( 0 )
        , m_nLabelStyle( i_nLabelStyle )
        {}

        virtual ~LabeledElement();

        virtual Size getOptimalSize( WindowSizeType ) const;
        virtual void resize();
        virtual size_t countElements() const { return 2; }

        void setLabel( Window* );
        void setLabel( boost::shared_ptr<WindowArranger> const & );
        void setElement( Window* );
        void setElement( boost::shared_ptr<WindowArranger> const & );
        void setLabelColumnWidth( long i_nWidth )
        { m_nLabelColumnWidth = i_nWidth; }

        Size getLabelSize( WindowSizeType i_eType ) const
        { return m_aLabel.getOptimalSize( i_eType ); }
        Size getElementSize( WindowSizeType i_eType ) const
        { return m_aElement.getOptimalSize( i_eType ); }
    };

    class VCL_DLLPUBLIC LabelColumn : public RowOrColumn
    {
        long getLabelWidth() const;
    public:
        LabelColumn( WindowArranger* i_pParent = NULL, long i_nBorderWidth = 5 )
        : RowOrColumn( i_pParent, true, i_nBorderWidth )
        {}
        virtual ~LabelColumn();

        virtual Size getOptimalSize( WindowSizeType ) const;
        virtual void resize();

        // returns the index of the added label
        size_t addRow( Window* i_pLabel, boost::shared_ptr<WindowArranger> const& i_rElement, long i_nIndent = 0 );
        size_t addRow( Window* i_pLabel, Window* i_pElement, long i_nIndent = 0 );
    };

    class VCL_DLLPUBLIC Indenter : public WindowArranger
    {
        long                        m_nIndent;
        WindowArranger::Element     m_aElement;

    protected:
        virtual Element* getElement( size_t i_nIndex )
        { return i_nIndex == 0 ? &m_aElement : NULL; }

    public:
        Indenter( WindowArranger* i_pParent = NULL, long i_nIndent = 15 )
        : WindowArranger( i_pParent )
        , m_nIndent( i_nIndent )
        {}

        virtual ~Indenter();

        virtual Size getOptimalSize( WindowSizeType ) const;
        virtual void resize();
        virtual size_t countElements() const { return (m_aElement.m_pElement != 0 || m_aElement.m_pChild != 0) ? 1 : 0; }

        void setIndent( long i_nIndent )
        {
            m_nIndent = i_nIndent;
            resize();
        }

        void setWindow( Window*, sal_Int32 i_nExpandPrio = 0 );
        void setChild( boost::shared_ptr<WindowArranger> const &, sal_Int32 i_nExpandPrio = 0 );
        // convenience: use for setChild( new WindowArranger( ... ) ) constructs
        void setChild( WindowArranger* i_pChild, sal_Int32 i_nExpandPrio = 0 )
        { setChild( boost::shared_ptr<WindowArranger>( i_pChild ), i_nExpandPrio ); }
    };

    class VCL_DLLPUBLIC Spacer : public WindowArranger
    {
        WindowArranger::Element     m_aElement;
        Size                        m_aSize;

    protected:
        virtual Element* getElement( size_t i_nIndex )
        { return i_nIndex == 0 ? &m_aElement : NULL; }

    public:
        Spacer( WindowArranger* i_pParent = NULL, sal_Int32 i_nPrio = 20, const Size& i_rSize = Size( 0, 0 ) )
        : WindowArranger( i_pParent )
        , m_aElement( NULL, boost::shared_ptr<WindowArranger>(), i_nPrio )
        , m_aSize( i_rSize )
        {}

        virtual ~Spacer() {}

        virtual Size getOptimalSize( WindowSizeType ) const
        { return m_aSize; }
        virtual void resize() {}
        virtual void setParentWindow( Window* ) {}
        virtual size_t countElements() const { return 1; }
        virtual bool isVisible() const { return true; }
    };

    class VCL_DLLPUBLIC MatrixArranger : public WindowArranger
    {
        long    m_nBorderX;
        long    m_nBorderY;

        struct MatrixElement : public WindowArranger::Element
        {
            sal_uInt32  m_nX;
            sal_uInt32  m_nY;

            MatrixElement()
            : WindowArranger::Element()
            , m_nX( 0 )
            , m_nY( 0 )
            {}

            MatrixElement( Window* i_pWin,
                           sal_uInt32 i_nX, sal_uInt32 i_nY,
                           boost::shared_ptr<WindowArranger> const & i_pChild = boost::shared_ptr<WindowArranger>(),
                           sal_Int32 i_nExpandPriority = 0
                          )
            : WindowArranger::Element( i_pWin, i_pChild, i_nExpandPriority )
            , m_nX( i_nX )
            , m_nY( i_nY )
            {
            }
        };

        std::vector< MatrixElement >            m_aElements;
        std::map< sal_uInt64, size_t >          m_aMatrixMap;  // maps (x | (y << 32)) to index in m_aElements

        sal_uInt64 getMap( sal_uInt32 i_nX, sal_uInt32 i_nY )
        { return static_cast< sal_uInt64 >(i_nX) | (static_cast< sal_uInt64>(i_nY) << 32 ); }

        static void distributeExtraSize( std::vector<long>& io_rSizes, const std::vector<sal_Int32>& i_rPrios, long i_nExtraWidth );

        Size getOptimalSize( WindowSizeType,
                             std::vector<long>& o_rColumnWidths, std::vector<long>& o_rRowHeights,
                             std::vector<sal_Int32>& o_rColumnPrio, std::vector<sal_Int32>& o_rRowPrio
                            ) const;
    protected:
        virtual Element* getElement( size_t i_nIndex )
        { return i_nIndex < m_aElements.size() ? &m_aElements[ i_nIndex ] : 0; }

    public:
        MatrixArranger( WindowArranger* i_pParent = NULL,
                        long i_nBorderX = 5,
                        long i_nBorderY = 5 )
        : WindowArranger( i_pParent )
        , m_nBorderX( i_nBorderX )
        , m_nBorderY( i_nBorderY )
        {}

        virtual ~MatrixArranger();

        virtual Size getOptimalSize( WindowSizeType ) const;
        virtual void resize();
        virtual size_t countElements() const { return m_aElements.size(); }

        // add a managed window at the given matrix position
        size_t addWindow( Window*, sal_uInt32 i_nX, sal_uInt32 i_nY, sal_Int32 i_nExpandPrio = 0 );
        void remove( Window* );

        size_t addChild( boost::shared_ptr<WindowArranger> const &, sal_uInt32 i_nX, sal_uInt32 i_nY, sal_Int32 i_nExpandPrio = 0 );
        // convenience: use for addChild( new WindowArranger( ... ) ) constructs
        size_t addChild( WindowArranger* i_pNewChild, sal_uInt32 i_nX, sal_uInt32 i_nY, sal_Int32 i_nExpandPrio = 0 )
        { return addChild( boost::shared_ptr<WindowArranger>( i_pNewChild ), i_nX, i_nY, i_nExpandPrio ); }
        void remove( boost::shared_ptr<WindowArranger> const & );
    };

}

#endif