summaryrefslogtreecommitdiff
path: root/widgets/FormattedListboxTextItem.cpp
blob: b9362cc901067f51470facede6a5a626efcc5bb3 (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
#include "FormattedListboxTextItem.h"

namespace CEGUI
{
//----------------------------------------------------------------------------//
FormattedListboxTextItem::FormattedListboxTextItem(const String& text,
                const HorizontalTextFormatting format,
                const uint item_id,
                void* const item_data,
                const bool disabled,
                const bool auto_delete) :
    // initialise base class
    ListboxTextItem(text, item_id, item_data, disabled, auto_delete),
    // initialise subclass fields
    d_formatting(format),
    d_formattedRenderedString(0)
{
}

//----------------------------------------------------------------------------//
FormattedListboxTextItem::~FormattedListboxTextItem()
{
    delete d_formattedRenderedString;
}

//----------------------------------------------------------------------------//
HorizontalTextFormatting FormattedListboxTextItem::getFormatting() const
{
    return d_formatting;
}

//----------------------------------------------------------------------------//
void FormattedListboxTextItem::setFormatting(const HorizontalTextFormatting fmt)
{
    if (fmt == d_formatting)
        return;

    d_formatting = fmt;
    delete d_formattedRenderedString;
    d_formattedRenderedString = 0;
    d_formattingAreaSize = Size(0, 0);
}

//----------------------------------------------------------------------------//
Size FormattedListboxTextItem::getPixelSize(void) const
{
    if (!d_owner)
        return Size(0, 0);

    // reparse text if we need to.
    if (!d_renderedStringValid)
        parseTextString();

    // create formatter if needed
    if (!d_formattedRenderedString)
        setupStringFormatter();

    // get size of render area from target window, to see if we need to reformat
    const Size area_sz(static_cast<const Listbox*>(d_owner)->
        getListRenderArea().getSize());
    if (area_sz != d_formattingAreaSize)
    {
        d_formattedRenderedString->format(area_sz);
        d_formattingAreaSize = area_sz;
    }

    return Size(d_formattedRenderedString->getHorizontalExtent(),
                d_formattedRenderedString->getVerticalExtent());
}

//----------------------------------------------------------------------------//
void FormattedListboxTextItem::draw(GeometryBuffer& buffer,
                                    const Rect& targetRect,
                                    float alpha, const Rect* clipper) const
{
    // reparse text if we need to.
    if (!d_renderedStringValid)
        parseTextString();

    // create formatter if needed
    if (!d_formattedRenderedString)
        setupStringFormatter();

    // get size of render area from target window, to see if we need to reformat
    // NB: We do not use targetRect, since it may not represent the same area.
    const Size area_sz(static_cast<const Listbox*>(d_owner)->
        getListRenderArea().getSize());
    if (area_sz != d_formattingAreaSize)
    {
        d_formattedRenderedString->format(area_sz);
        d_formattingAreaSize = area_sz;
    }

    // draw selection imagery
    if (d_selected && d_selectBrush != 0)
        d_selectBrush->draw(buffer, targetRect, clipper,
                            getModulateAlphaColourRect(d_selectCols, alpha));

    // factor the window alpha into our colours.
    const ColourRect final_colours(
        getModulateAlphaColourRect(ColourRect(0xFFFFFFFF), alpha));

    // draw the formatted text
    d_formattedRenderedString->draw(buffer, targetRect.getPosition(),
                                    &final_colours, clipper);
}

//----------------------------------------------------------------------------//
void FormattedListboxTextItem::setupStringFormatter() const
{
    // delete any existing formatter
    delete d_formattedRenderedString;
    d_formattedRenderedString = 0;

    // create new formatter of whichever type...
    switch(d_formatting)
    {
    case HTF_LEFT_ALIGNED:
        d_formattedRenderedString =
            new LeftAlignedRenderedString(d_renderedString);
        break;

    case HTF_RIGHT_ALIGNED:
        d_formattedRenderedString =
            new RightAlignedRenderedString(d_renderedString);
        break;

    case HTF_CENTRE_ALIGNED:
        d_formattedRenderedString =
            new CentredRenderedString(d_renderedString);
        break;

    case HTF_JUSTIFIED:
        d_formattedRenderedString =
            new JustifiedRenderedString(d_renderedString);
        break;

    case HTF_WORDWRAP_LEFT_ALIGNED:
        d_formattedRenderedString =
            new RenderedStringWordWrapper
                <LeftAlignedRenderedString>(d_renderedString);
        break;

    case HTF_WORDWRAP_RIGHT_ALIGNED:
        d_formattedRenderedString =
            new RenderedStringWordWrapper
                <RightAlignedRenderedString>(d_renderedString);
        break;

    case HTF_WORDWRAP_CENTRE_ALIGNED:
        d_formattedRenderedString =
            new RenderedStringWordWrapper
                <CentredRenderedString>(d_renderedString);
        break;

    case HTF_WORDWRAP_JUSTIFIED:
        d_formattedRenderedString =
            new RenderedStringWordWrapper
                <JustifiedRenderedString>(d_renderedString);
        break;
    }
}

//----------------------------------------------------------------------------//
}