summaryrefslogtreecommitdiff
path: root/vcl/inc/vcl/builder.hxx
blob: ae0bc7fb7e51168210991b5a5c0de58b47409f4a (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *        Caolán McNamara <caolanm@redhat.com> (Red Hat, Inc.)
 * Portions created by the Initial Developer are Copyright (C) 2011 the
 * Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Caolán McNamara <caolanm@redhat.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
 * instead of those above.
 */
#ifndef _VCLBUILDER_HXX
#define _VCLBUILDER_HXX

#include <vcl/dllapi.h>
#include <vcl/window.hxx>
#include <xmlreader/xmlreader.hxx>
#include <map>
#include <stack>
#include <vector>

class ListBox;
class NumericFormatter;

class VCL_DLLPUBLIC VclBuilder
{
public:
    typedef std::map<rtl::OString, rtl::OString> stringmap;
    typedef Window* (*customMakeWidget)(Window *pParent);
private:
    struct WinAndId
    {
        rtl::OString m_sID;
        Window *m_pWindow;
        sal_Int32 m_nPosition;
        bool m_bOwned;
        WinAndId(const rtl::OString &rId, Window *pWindow)
            : m_sID(rId)
            , m_pWindow(pWindow)
            , m_nPosition(-1)
            , m_bOwned(true)
        {
        }
    };
    std::vector<WinAndId> m_aChildren;

    struct ListStore
    {
        typedef std::vector<rtl::OString> row;
        std::vector<row> m_aEntries;
    };

    struct ModelAndId
    {
        rtl::OString m_sID;
        ListStore *m_pModel;
        ModelAndId(const rtl::OString &rId, ListStore *pListStore)
            : m_sID(rId)
            , m_pModel(pListStore)
        {
        }
    };

    struct StringPair
    {
        rtl::OString m_sID;
        rtl::OString m_sValue;
        StringPair(const rtl::OString &rId, const rtl::OString &rValue)
            : m_sID(rId)
            , m_sValue(rValue)
        {
        }
    };

    typedef StringPair RadioButtonGroupMap;
    typedef StringPair ComboBoxModelMap;

    ListStore *get_model_by_name(rtl::OString sID);
    static void mungemodel(ListBox &rTarget, ListStore &rStore);

    typedef stringmap Adjustment;

    struct AdjustmentAndId
    {
        rtl::OString m_sID;
        Adjustment m_aAdjustment;
        AdjustmentAndId(const rtl::OString &rId, Adjustment &rAdjustment)
            : m_sID(rId)
        {
            m_aAdjustment.swap(rAdjustment);
        }
    };

    typedef StringPair SpinButtonAdjustmentMap;

    Adjustment *get_adjustment_by_name(rtl::OString sID);
    static void mungeadjustment(NumericFormatter &rTarget, Adjustment &rAdjustment);

    typedef std::map<rtl::OString, rtl::OString> WidgetTranslations;
    typedef std::map<rtl::OString, WidgetTranslations> Translations;

    struct ParserState
    {
        std::vector<RadioButtonGroupMap> m_aGroupMaps;
        std::vector<ComboBoxModelMap> m_aModelMaps;
        std::vector<ModelAndId> m_aModels;
        std::vector<AdjustmentAndId> m_aAdjustments;
        std::vector<SpinButtonAdjustmentMap> m_aAdjustmentMaps;
        Translations m_aTranslations;
    };

    rtl::OString getTranslation(const rtl::OString &rId, const rtl::OString &rProperty) const;

    rtl::OString m_sID;
    rtl::OString m_sHelpRoot;
    Window *m_pParent;
    ParserState *m_pParserState;

    Window *get_by_name(rtl::OString sID);
public:
    VclBuilder(Window *pParent, rtl::OUString sUIRootDir, rtl::OUString sUIFile, rtl::OString sID = rtl::OString());
    ~VclBuilder();
    Window *get_widget_root();
    template <typename T> T* get(T*& ret, rtl::OString sID)
    {
        Window *w = get_by_name(sID);
        ret = static_cast<T*>(w);

#if OSL_DEBUG_LEVEL > 0
        if (w)
        {
            ret = dynamic_cast<T*>(w);
            assert(ret);
        }
#endif

        return ret;
    }
    template <typename T /*=Window if we had c++11*/> T* get(rtl::OString sID)
    {
        Window *w = get_by_name(sID);
        T* ret = static_cast<T*>(w);

#if OSL_DEBUG_LEVEL > 0
        if (w)
        {
            ret = dynamic_cast<T*>(w);
            assert(ret);
        }
#endif

        return ret;
    }

    rtl::OString get_by_window(const Window *pWindow) const;
    //for the purposes of retrofitting this to the existing code
    //look up sID, clone its properties into replacement and
    //splice replacement into the tree instead of it, without
    //taking ownership of it
    bool replace(rtl::OString sID, Window &rReplacement);
private:
    Window *insertObject(Window *pParent, const rtl::OString &rClass, const rtl::OString &rID, stringmap &rVec);
    Window *makeObject(Window *pParent, const rtl::OString &rClass, const rtl::OString &rID, stringmap &rVec);
    bool extractGroup(const rtl::OString &id, stringmap &rVec);
    bool extractModel(const rtl::OString &id, stringmap &rVec);
    bool extractAdjustment(const rtl::OString &id, stringmap &rVec);

    void handleTranslations(xmlreader::XmlReader &reader);

    void handleChild(Window *pParent, xmlreader::XmlReader &reader);
    Window* handleObject(Window *pParent, xmlreader::XmlReader &reader);
    void handlePacking(Window *pCurrent, xmlreader::XmlReader &reader);
    void applyPackingProperty(Window *pCurrent, xmlreader::XmlReader &reader);
    void collectProperty(xmlreader::XmlReader &reader, const rtl::OString &rID, stringmap &rVec);

    void handleListStore(xmlreader::XmlReader &reader, const rtl::OString &rID);
    void handleRow(xmlreader::XmlReader &reader, const rtl::OString &rID, sal_Int32 nRowIndex);
    void handleAdjustment(const rtl::OString &rID, stringmap &rProperties);
    void handleTabChild(Window *pParent, xmlreader::XmlReader &reader);

    sal_Int32 get_window_packing_position(const Window *pWindow) const;
    void set_window_packing_position(const Window *pWindow, sal_Int32 nPosition);

    //Helpers to retrofit all the existing code the the builder
    static void swapGuts(Window &rOrig, Window &rReplacement);
    static sal_uInt16 getPositionWithinParent(Window &rWindow);
    static void reorderWithinParent(Window &rWindow, sal_uInt16 nNewPosition);
};


//allows retro fitting existing dialogs/tabpages that load a resource
//to load a .ui file instead
//
//vcl requires the Window Children of a Parent Window to be destroyed before
//the Parent Window.  VclBuilderContainer owns the VclBuilder which owns the
//Children Window. So the VclBuilderContainer dtor must be called before
//the Parent Window dtor.
//
//i.e.  class Dialog : public SystemWindow, public VclBuilderContainer
//not   class Dialog : public VclBuilderContainer, public SystemWindow
class ResId;

class VCL_DLLPUBLIC VclBuilderContainer
{
protected:
    VclBuilder *m_pUIBuilder;
public:
    VclBuilderContainer();
    virtual ~VclBuilderContainer();
    static VclBuilder* overrideResourceWithUIXML(Window *pWindow, const ResId& rResId);
    static bool replace_buildable(Window *pParent, const ResId& rResId, Window &rReplacement);
    template <typename T> T* get(T*& ret, rtl::OString sID)
    {
        return m_pUIBuilder->get<T>(ret, sID);
    }
    template <typename T /*=Window if we had c++11*/> T* get(rtl::OString sID)
    {
        return m_pUIBuilder->get<T>(sID);
    }
};


#endif

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