File: webview.py

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 (283 lines) | stat: -rw-r--r-- 11,077 bytes parent folder | download
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#---------------------------------------------------------------------------
# Name:        etg/webview.py
# Author:      Robin Dunn
#
# Created:     20-Nov-2012
# Copyright:   (c) 2012-2020 by Total Control Software
# License:     wxWindows License
#---------------------------------------------------------------------------

import etgtools
import etgtools.tweaker_tools as tools

PACKAGE   = "wx"
MODULE    = "_html2"
NAME      = "webview"   # Base name of the file to generate to for this script
DOCSTRING = ""

# The classes and/or the basename of the Doxygen XML files to be processed by
# this script.
ITEMS  = [ 'wxWebViewHistoryItem',
           'wxWebViewHandler',
           'wxWebViewArchiveHandler',
           'wxWebViewFSHandler',
           'wxWebView',
           'wxWebViewEvent',
           'wxWebViewFactory',
           'wxWebViewIE',
           ]

#---------------------------------------------------------------------------

def run():
    # Parse the XML file(s) building a collection of Extractor objects
    module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
    etgtools.parseDoxyXML(module, ITEMS)

    #-----------------------------------------------------------------
    # Tweak the parsed meta objects in the module object as needed for
    # customizing the generated code and docstrings.

    module.addHeaderCode("""\
        #include <wx/webview.h>
        #if wxUSE_WEBVIEW_IE && defined(__WXMSW__)
            #include <wx/msw/webview_ie.h>
            #include <wx/msw/webview_edge.h>
        #endif
        """)
    module.addHeaderCode('#include <wx/filesys.h>')

    module.addGlobalStr('wxWebViewBackendDefault', 0)
    module.addGlobalStr('wxWebViewBackendIE', 0)
    module.addGlobalStr('wxWebViewBackendEdge', 0)
    module.addGlobalStr('wxWebViewBackendWebKit', 0)
    module.addGlobalStr('wxWebViewNameStr', 0)
    module.addGlobalStr('wxWebViewDefaultURLStr', 0)

    module.addHeaderCode("""\
        #if wxUSE_WEBVIEW && !defined(wxWebViewIE_H)
        enum wxWebViewIE_EmulationLevel
        {
            wxWEBVIEWIE_EMU_DEFAULT =    0,
            wxWEBVIEWIE_EMU_IE7 =        7000,
            wxWEBVIEWIE_EMU_IE8 =        8000,
            wxWEBVIEWIE_EMU_IE8_FORCE =  8888,
            wxWEBVIEWIE_EMU_IE9 =        9000,
            wxWEBVIEWIE_EMU_IE9_FORCE =  9999,
            wxWEBVIEWIE_EMU_IE10 =       10000,
            wxWEBVIEWIE_EMU_IE10_FORCE = 10001,
            wxWEBVIEWIE_EMU_IE11 =       11000,
            wxWEBVIEWIE_EMU_IE11_FORCE = 11001
        };
        #endif
        """)

    module.addPyCode(order=15, code="""\
        # On Windows we need to ensure that the wx package folder is on on the
        # PATH, so the MS Edge Loader DLLs can be found when they are dynamically
        # loaded.
        import os
        if os.name == 'nt':
            _path = os.environ.get('PATH')
            _pkg_path = os.path.abspath(os.path.dirname(wx.__file__))
            if _pkg_path.lower() not in _path.lower():
                os.environ['PATH'] = _path + os.pathsep + _pkg_path
        """)

    # Pull out the obj for wxWebViewIE so we can use it later, but not include it in the stubs
    wvie = module.find('wxWebViewIE')
    module.items.remove(wvie)

    # This tweak is needed only for the stub code
    module.find('wxWebViewHandler.wxWebViewHandler').argsString = '(const wxString& scheme="")'

    # Documented wrongly in 3.1.6 (needs to be fixed in stubs too)
    c = module.find('wxWebViewFactory')
    c.find('GetVersionInfo').argsString = '()'
    c.find('GetVersionInfo').items = []

    tools.generateStubs('wxUSE_WEBVIEW', module,
                        typeValMap={
                            'wxWebViewNavigationActionFlags': 'wxWEBVIEW_NAV_ACTION_NONE',
                            'wxWebViewZoom': 'wxWEBVIEW_ZOOM_MEDIUM',
                            'wxVersionInfo': 'wxVersionInfo()',
                            })

    c = module.find('wxWebView')
    assert isinstance(c, etgtools.ClassDef)
    tools.fixWindowClass(c)
    c.abstract = True

    for m in c.find('New').all():
        m.factory = True
    c.find('New.id').default = 'wxID_ANY'
    c.find('New.parent').transferThis = True


    c.find('RegisterHandler.handler').type = 'wxWebViewHandler*'
    c.find('RegisterHandler.handler').transfer = True
    c.find('RegisterHandler').setCppCode_sip(
        "sipCpp->RegisterHandler(wxSharedPtr<wxWebViewHandler>(handler));")


    c.find('RegisterFactory.factory').type = 'wxWebViewFactory*'
    c.find('RegisterFactory.factory').transfer = True
    c.find('RegisterFactory').setCppCode_sip(
        "wxWebView::RegisterFactory(*backend, wxSharedPtr<wxWebViewFactory>(factory));")

    c.find('RunScript.output').out = True


    # Custom code to deal with the
    # wxVector<wxSharedPtr<wxWebViewHistoryItem>> return type of these two
    # methods. We'll just convert them to a Python list of history items.
    code = """\
        wxPyThreadBlocker blocker;
        PyObject* result = PyList_New(0);
        wxVector<wxSharedPtr<wxWebViewHistoryItem> >  vector = self->{method}();
        for (size_t idx=0; idx < vector.size(); idx++) {{
            PyObject* obj;
            wxWebViewHistoryItem* item = new wxWebViewHistoryItem(*vector[idx].get());
            obj = wxPyConstructObject((void*)item, "wxWebViewHistoryItem", true);
            PyList_Append(result, obj);
            Py_DECREF(obj);
        }}
        return result;
        """
    c.find('GetBackwardHistory').type = 'PyObject*'
    c.find('GetBackwardHistory').setCppCode(code.format(method='GetBackwardHistory'))
    c.find('GetForwardHistory').type = 'PyObject*'
    c.find('GetForwardHistory').setCppCode(code.format(method='GetForwardHistory'))


    # Since LoadHistoryItem expects to get an actual item in the history
    # list, and since we make copies of the items in the cppCode above, then
    # this won't be possible to do from the Python wrappers. However, it's
    # just as easy to use LoadURL to reload a history item so it's not a
    # great loss.
    c.find('LoadHistoryItem').ignore()
    ##c.find('LoadHistoryItem.item').type = 'wxWebViewHistoryItem*'
    ##c.find('LoadHistoryItem.item').transfer = True
    ##c.find('LoadHistoryItem').setCppCode_sip(
    ##    "sipCpp->LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem>(item));")


    # Add the MSW methods in wxWebViewIE into the main WebView class like they were before.
    c.addItem(wvie.find('MSWSetEmulationLevel'))
    c.addItem(wvie.find('MSWSetModernEmulationLevel'))

    # Give them an implementation that doesn't matter which class they are actually located in.
    c.find('MSWSetEmulationLevel').setCppCode("""\
        #if wxUSE_WEBVIEW_IE && defined(__WXMSW__)
            return _do_MSWSetEmulationLevel(level);
        #else
            return false;
        #endif
        """)

    c.find('MSWSetModernEmulationLevel').setCppCode("""\
        #if wxUSE_WEBVIEW_IE && defined(__WXMSW__)
            return _do_MSWSetEmulationLevel(modernLevel ? wxWEBVIEWIE_EMU_IE8
                                                        : wxWEBVIEWIE_EMU_DEFAULT);
        #else
            return false;
        #endif
        """)

    # The emulation level is set as a per-application value in the Windows
    # Registry. The way this is implemented in the C++ code we end up with the
    # name of the _core extransion module in the Reistry instead of the .exe
    # name, which is what is really needed.
    #
    # So instead of doing simple wrappers with #if checks like normal, replace
    # these methods with a new implementation that does the RightThing using
    # sys.executable.
    c.addCppCode(r"""
        #if wxUSE_WEBVIEW_IE && defined(__WXMSW__)
        #include <wx/msw/webview_ie.h>
        #include <wx/msw/registry.h>

        bool _do_MSWSetEmulationLevel(wxWebViewIE_EmulationLevel level)
        {
            wxString programName;
            wxPyBLOCK_THREADS(
                programName = Py2wxString(PySys_GetObject("executable")));
            programName = programName.AfterLast('\\');

            // Registry key where emulation level for programs are set
            static const wxChar* IE_EMULATION_KEY =
                wxT("SOFTWARE\\Microsoft\\Internet Explorer\\Main")
                wxT("\\FeatureControl\\FEATURE_BROWSER_EMULATION");

            wxRegKey key(wxRegKey::HKCU, IE_EMULATION_KEY);
            if ( !key.Exists() )
            {
                wxLogWarning(_("Failed to find web view emulation level in the registry"));
                return false;
            }
            if ( level != wxWEBVIEWIE_EMU_DEFAULT )
            {
                if ( !key.SetValue(programName, level) )
                {
                    wxLogWarning(_("Failed to set web view to modern emulation level"));
                    return false;
                }
            }
            else
            {
                if ( !key.DeleteValue(programName) )
                {
                    wxLogWarning(_("Failed to reset web view to standard emulation level"));
                    return false;
                }
            }
            return true;
        }
        #endif
        """)


    c = module.find('wxWebViewEvent')
    tools.fixEventClass(c)

    module.addPyCode("""\
        EVT_WEBVIEW_NAVIGATING = wx.PyEventBinder( wxEVT_WEBVIEW_NAVIGATING, 1 )
        EVT_WEBVIEW_NAVIGATED = wx.PyEventBinder( wxEVT_WEBVIEW_NAVIGATED, 1 )
        EVT_WEBVIEW_LOADED = wx.PyEventBinder( wxEVT_WEBVIEW_LOADED, 1 )
        EVT_WEBVIEW_ERROR = wx.PyEventBinder( wxEVT_WEBVIEW_ERROR, 1 )
        EVT_WEBVIEW_NEWWINDOW = wx.PyEventBinder( wxEVT_WEBVIEW_NEWWINDOW, 1 )
        EVT_WEBVIEW_TITLE_CHANGED = wx.PyEventBinder( wxEVT_WEBVIEW_TITLE_CHANGED, 1 )
        EVT_WEBVIEW_FULLSCREEN_CHANGED = wx.PyEventBinder( wxEVT_WEBVIEW_FULLSCREEN_CHANGED, 1)
        EVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED = wx.PyEventBinder( wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, 1)
        EVT_WEBVIEW_SCRIPT_RESULT = wx.PyEventBinder( wxEVT_WEBVIEW_SCRIPT_RESULT, 1)

        # deprecated wxEVT aliases
        wxEVT_COMMAND_WEBVIEW_NAVIGATING     = wxEVT_WEBVIEW_NAVIGATING
        wxEVT_COMMAND_WEBVIEW_NAVIGATED      = wxEVT_WEBVIEW_NAVIGATED
        wxEVT_COMMAND_WEBVIEW_LOADED         = wxEVT_WEBVIEW_LOADED
        wxEVT_COMMAND_WEBVIEW_ERROR          = wxEVT_WEBVIEW_ERROR
        wxEVT_COMMAND_WEBVIEW_NEWWINDOW      = wxEVT_WEBVIEW_NEWWINDOW
        wxEVT_COMMAND_WEBVIEW_TITLE_CHANGED  = wxEVT_WEBVIEW_TITLE_CHANGED
        """)


    c = module.find('wxWebViewHistoryItem')
    tools.addAutoProperties(c)


    for name in [ 'wxWebViewHandler',
                  'wxWebViewArchiveHandler',
                  'wxWebViewFSHandler' ]:
        c = module.find(name)
        c.find('GetFile').factory = True


    #-----------------------------------------------------------------
    tools.doCommonTweaks(module)
    tools.runGenerators(module)


#---------------------------------------------------------------------------
if __name__ == '__main__':
    run()