File: waitforpaint.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (102 lines) | stat: -rw-r--r-- 2,544 bytes parent folder | download | duplicates (4)
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
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/waitforpaint.h
// Purpose:     Helper WaitForPaint class
// Author:      Vadim Zeitlin
// Created:     2019-10-17 (extracted from tests/window/setsize.cpp)
// Copyright:   (c) 2019 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_TESTS_WAITFORPAINT_H_
#define _WX_TESTS_WAITFORPAINT_H_

#include "wx/stopwatch.h"
#include "wx/window.h"

// Class used to check if we received the (first) paint event: this is
// currently used under GTK only, as MSW doesn't seem to need to wait for the
// things to work, while under Mac nothing works anyhow.
#ifdef __WXGTK__

class WaitForPaint
{
public:
    explicit WaitForPaint(wxWindow* win)
        : m_win(*win),
          m_painted(false),
          m_handler(&m_painted)
    {
        m_win.Bind(wxEVT_PAINT, m_handler);
    }

    // This function waits up to the given number of milliseconds for the paint
    // event to come and logs a warning if we didn't get it.
    void YieldUntilPainted(int timeoutInMS = 250)
    {
        wxStopWatch sw;
        for ( ;; )
        {
            wxYield();

            if ( m_painted )
            {
                // Reset it in case YieldUntilPainted() is called again.
                m_painted = false;
                break;
            }

            if ( sw.Time() > timeoutInMS )
            {
                WARN("Didn't get a paint event until timeout expiration");
                break;
            }
        }
    }

    ~WaitForPaint()
    {
        m_win.Unbind(wxEVT_PAINT, m_handler);
    }

private:
    wxWindow& m_win;
    bool m_painted;

    class PaintHandler
    {
    public:
        // Note that we have to use a pointer here, i.e. we can't just store
        // the flag inside the class itself because it's going to be cloned
        // inside wx and querying the flag of the original copy wouldtn' work.
        explicit PaintHandler(bool* painted)
            : m_painted(*painted)
        {
        }

        void operator()(wxPaintEvent& event)
        {
            event.Skip();
            m_painted = true;
        }

    private:
        bool& m_painted;
    } m_handler;
};

#else // !__WXGTK__

class WaitForPaint
{
public:
    explicit WaitForPaint(wxWindow* WXUNUSED(win))
    {
    }

    void YieldUntilPainted(int WXUNUSED(timeoutInMS) = 250)
    {
    }
};

#endif // __WXGTK__/!__WXGTK__

#endif // _WX_TESTS_WAITFORPAINT_H_