File: window.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 (100 lines) | stat: -rw-r--r-- 2,599 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
/////////////////////////////////////////////////////////////////////////////
// Name:        wx/private/window.h
// Purpose:     misc wxWindow helpers
// Author:      Vaclav Slavik
// Created:     2010-01-21
// Copyright:   (c) 2010 Vaclav Slavik
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_PRIVATE_WINDOW_H_
#define _WX_PRIVATE_WINDOW_H_

#include "wx/gdicmn.h"
#include "wx/dynlib.h"

namespace wxPrivate
{

// Windows' computes dialog units using average character width over upper-
// and lower-case ASCII alphabet and not using the average character width
// metadata stored in the font; see
// http://support.microsoft.com/default.aspx/kb/145994 for detailed discussion.
//
// This helper function computes font dimensions in the same way. It works with
// either wxDC or wxWindow argument.
template<typename T>
inline wxSize GetAverageASCIILetterSize(const T& of_what)
{
    const wxStringCharType *TEXT_TO_MEASURE =
        wxS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

    wxSize s = of_what.GetTextExtent(TEXT_TO_MEASURE);
    s.x = (s.x / 26 + 1) / 2;
    return s;
}

namespace
{

inline bool SupportsPerMonitorDPI()
{
    static bool s_checkDPI =
#if defined(__WXMSW__) && wxUSE_DYNLIB_CLASS
        // Only check the DPI when GetDpiForWindow is available because the old
        // method (GetDeviceCaps) is a lot slower (about 1500 times).
        // And when GetDpiForWindow is not available (for example older Windows
        // versions), per-monitor DPI (V2) is also not available.
        wxLoadedDLL("user32.dll").HasSymbol("GetDpiForWindow");
#else
        false;
#endif
    return s_checkDPI;
}

}

template <typename T>
class DpiDependentValue
{
public:
    // Explicit initialization is needed if T is a primitive type.
    DpiDependentValue()
        : m_value(), m_dpi()
    { }

    bool HasChanged(const wxWindowBase* win)
    {
        if ( win && SupportsPerMonitorDPI() )
        {
            const wxSize dpi = win->GetDPI();
            if ( dpi != m_dpi )
            {
                m_dpi = dpi;
                return true;
            }
        }

        // Ensure that we return true the first time we're called,
        // assuming that the value will always be set to a non-default value.
        return m_value == T();
    }

    void SetAtNewDPI(const T& value)
    {
        m_value = value;
    }

    T& Get()
    {
        return m_value;
    }

private:
    T m_value;
    wxSize m_dpi;
};

} // namespace wxPrivate

#endif // _WX_PRIVATE_WINDOW_H_