File: textwrapper.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (100 lines) | stat: -rw-r--r-- 3,044 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
/////////////////////////////////////////////////////////////////////////////
// Name:        wx/textwrapper.h
// Purpose:     documentation of wxTextWrapper interface
// Author:      Vadim Zeitlin
// Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/**
    @class wxTextWrapper

    Helps wrap lines of text to given width.

    This is a generic purpose class which can be used to wrap lines of text to
    the specified width. It doesn't do anything by itself but simply calls its
    virtual OnOutputLine() and OnNewLine() methods for each wrapped line of
    text, you need to implement them in your derived class to actually do
    something useful.

    Here is an example function using this class which inserts hard line breaks
    into a string of text at the positions where it would be wrapped:
    
    @code
    wxString WrapText(wxWindow *win, const wxString& text, int widthMax)
    {
        class HardBreakWrapper : public wxTextWrapper
        {
        public:
            HardBreakWrapper(wxWindow *win, const wxString& text, int widthMax)
            {
                Wrap(win, text, widthMax);
            }

            wxString const& GetWrapped() const { return m_wrapped; }

        protected:
            virtual void OnOutputLine(const wxString& line)
            {
                m_wrapped += line;
            }

            virtual void OnNewLine()
            {
                m_wrapped += '\n';
            }

        private:
            wxString m_wrapped;
        };

        HardBreakWrapper wrapper(win, text, widthMax);
        return wrapper.GetWrapped();
    }
    @endcode

    @nolibrary
    @category{gdi}
 */
class wxTextWrapper
{
public:
    /**
        Trivial default constructor.
     */
    wxTextWrapper();

    /**
        Wrap the given text.

        This method will call OnOutputLine() for every line of wrapped text and
        OnNewLine() before the beginning of every new line after the first one
        (so it might be never called at all if the width of entire @a text is
        less than @a widthMax).

        @param win
            A non-@NULL window used for measuring the text extents.
        @param text
            The text to wrap.
        @param widthMax
            Maximal width of each line of text or @c -1 to disable wrapping.
     */
    void Wrap(wxWindow *win, const wxString& text, int widthMax);

protected:
    /**
        Called by Wrap() for each wrapped line of text.

        This method will always be called at least once by Wrap(). Notice that
        @a line may be empty if the text passed to Wrap() was empty itself.
     */
    virtual void OnOutputLine(const wxString& line) = 0;

    /**
        Called at the start of each subsequent line of text by Wrap().

        This method may not be called at all if the entire text passed to
        Wrap() fits into the specified width.
     */
    virtual void OnNewLine();
};