File: markupparserattr.h

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (231 lines) | stat: -rw-r--r-- 7,759 bytes parent folder | download | duplicates (10)
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
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/private/markupparserattr.h
// Purpose:     Classes mapping markup attributes to wxFont/wxColour.
// Author:      Vadim Zeitlin
// Created:     2011-02-18
// Copyright:   (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_PRIVATE_MARKUPPARSERATTR_H_
#define _WX_PRIVATE_MARKUPPARSERATTR_H_

#include "wx/private/markupparser.h"

#include "wx/stack.h"

#include "wx/colour.h"
#include "wx/font.h"

// ----------------------------------------------------------------------------
// wxMarkupParserAttrOutput: simplified wxFont-using version of the above.
// ----------------------------------------------------------------------------

// This class assumes that wxFont and wxColour are used to perform all the
// markup tags and implements the base class virtual functions in terms of
// OnAttr{Start,End}() only.
//
// Notice that you still must implement OnText() inherited from the base class
// when deriving from this one.
class wxMarkupParserAttrOutput : public wxMarkupParserOutput
{
public:
    // A simple container of font and colours.
    struct Attr
    {
        Attr(const wxFont& font_,
             const wxColour& foreground_ = wxColour(),
             const wxColour& background_ = wxColour())
            : font(font_), foreground(foreground_), background(background_)
        {
        }

        wxFont font;
        wxColour foreground,
                 background;
    };


    // This object must be initialized with the font and colours to use
    // initially, i.e. the ones used before any tags in the string.
    wxMarkupParserAttrOutput(const wxFont& font,
                             const wxColour& foreground,
                             const wxColour& background)
    {
        m_attrs.push(Attr(font, foreground, background));
    }

    // Indicates the change of the font and/or colours used. Any of the
    // fields of the argument may be invalid indicating that the corresponding
    // attribute didn't actually change.
    virtual void OnAttrStart(const Attr& attr) = 0;

    // Indicates the end of the region affected by the given attributes
    // (the same ones that were passed to the matching OnAttrStart(), use
    // GetAttr() to get the ones that will be used from now on).
    virtual void OnAttrEnd(const Attr& attr) = 0;


    // Implement all pure virtual methods inherited from the base class in
    // terms of our own ones.
    virtual void OnBoldStart() { DoChangeFont(&wxFont::Bold); }
    virtual void OnBoldEnd() { DoEndAttr(); }

    virtual void OnItalicStart() { DoChangeFont(&wxFont::Italic); }
    virtual void OnItalicEnd() { DoEndAttr(); }

    virtual void OnUnderlinedStart() { DoChangeFont(&wxFont::Underlined); }
    virtual void OnUnderlinedEnd() { DoEndAttr(); }

    virtual void OnStrikethroughStart() { DoChangeFont(&wxFont::Strikethrough); }
    virtual void OnStrikethroughEnd() { DoEndAttr(); }

    virtual void OnBigStart() { DoChangeFont(&wxFont::Larger); }
    virtual void OnBigEnd() { DoEndAttr(); }

    virtual void OnSmallStart() { DoChangeFont(&wxFont::Smaller); }
    virtual void OnSmallEnd() { DoEndAttr(); }

    virtual void OnTeletypeStart()
    {
        wxFont font(GetFont());
        font.SetFamily(wxFONTFAMILY_TELETYPE);
        DoSetFont(font);
    }
    virtual void OnTeletypeEnd() { DoEndAttr(); }

    virtual void OnSpanStart(const wxMarkupSpanAttributes& spanAttr)
    {
        wxFont font(GetFont());
        if ( !spanAttr.m_fontFace.empty() )
            font.SetFaceName(spanAttr.m_fontFace);

        FontModifier<wxFontWeight>()(spanAttr.m_isBold,
                                     font, &wxFont::SetWeight,
                                     wxFONTWEIGHT_NORMAL, wxFONTWEIGHT_BOLD);

        FontModifier<wxFontStyle>()(spanAttr.m_isItalic,
                                    font, &wxFont::SetStyle,
                                    wxFONTSTYLE_NORMAL, wxFONTSTYLE_ITALIC);

        FontModifier<bool>()(spanAttr.m_isUnderlined,
                             font, &wxFont::SetUnderlined,
                             false, true);

        // TODO: No support for strike-through yet.

        switch ( spanAttr.m_sizeKind )
        {
            case wxMarkupSpanAttributes::Size_Unspecified:
                break;

            case wxMarkupSpanAttributes::Size_Relative:
                if ( spanAttr.m_fontSize > 0 )
                    font.MakeLarger();
                else
                    font.MakeSmaller();
                break;

            case wxMarkupSpanAttributes::Size_Symbolic:
                // The values of font size intentionally coincide with the
                // values of wxFontSymbolicSize enum elements so simply cast
                // one to the other.
                font.SetSymbolicSize(
                    static_cast<wxFontSymbolicSize>(spanAttr.m_fontSize)
                );
                break;

            case wxMarkupSpanAttributes::Size_PointParts:
                font.SetPointSize((spanAttr.m_fontSize + 1023)/1024);
                break;
        }


        const Attr attr(font, spanAttr.m_fgCol, spanAttr.m_bgCol);
        OnAttrStart(attr);

        m_attrs.push(attr);
    }

    virtual void OnSpanEnd(const wxMarkupSpanAttributes& WXUNUSED(spanAttr))
    {
        DoEndAttr();
    }

protected:
    // Get the current attributes, i.e. the ones that should be used for
    // rendering (or measuring or whatever) the text at the current position in
    // the string.
    //
    // It may be called from OnAttrStart() to get the old attributes used
    // before and from OnAttrEnd() to get the new attributes that will be used
    // from now on but is mostly meant to be used from overridden OnText()
    // implementations.
    const Attr& GetAttr() const { return m_attrs.top(); }

    // A shortcut for accessing the font of the current attribute.
    const wxFont& GetFont() const { return GetAttr().font; }

private:
    // Change only the font to the given one. Call OnAttrStart() to notify
    // about the change and update the attributes stack.
    void DoSetFont(const wxFont& font)
    {
        const Attr attr(font);

        OnAttrStart(attr);

        m_attrs.push(attr);
    }

    // Apply the given function to the font currently on top of the font stack,
    // push the new font on the stack and call OnAttrStart() with it.
    void DoChangeFont(wxFont (wxFont::*func)() const)
    {
        DoSetFont((GetFont().*func)());
    }

    void DoEndAttr()
    {
        const Attr attr(m_attrs.top());
        m_attrs.pop();

        OnAttrEnd(attr);
    }

    // A helper class used to apply the given function to a wxFont object
    // depending on the value of an OptionalBool.
    template <typename T>
    struct FontModifier
    {
        FontModifier() { }

        void operator()(wxMarkupSpanAttributes::OptionalBool isIt,
                        wxFont& font,
                        void (wxFont::*func)(T),
                        T noValue,
                        T yesValue)
        {
            switch ( isIt )
            {
                case wxMarkupSpanAttributes::Unspecified:
                    break;

                case wxMarkupSpanAttributes::No:
                    (font.*func)(noValue);
                    break;

                case wxMarkupSpanAttributes::Yes:
                    (font.*func)(yesValue);
                    break;
            }
        }
    };


    wxStack<Attr> m_attrs;

    wxDECLARE_NO_COPY_CLASS(wxMarkupParserAttrOutput);
};

#endif // _WX_PRIVATE_MARKUPPARSERATTR_H_