forked from diffpy/diffpy.pdfgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwx12.py
97 lines (65 loc) · 2.57 KB
/
wx12.py
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
#!/usr/bin/env python
##############################################################################
#
# diffpy.pdfgui Complex Modeling Initiative
# (c) 2019 Brookhaven Science Associates,
# Brookhaven National Laboratory.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
"""\
Support for WX4-like methods and functions for WX3.
Notes
-----
This module should be removed after ending support for WX3.
Replace instances of its use with plain ``wx``.
"""
import types
import wx
WX3 = wx.VERSION[0] == 3
WX4 = wx.VERSION[0] == 4
# ----------------------------------------------------------------------------
class Menu(wx.Menu):
def Append(self, *args, **kwargs):
na = len(args)
if isinstance(args[0], wx.MenuItem):
return super(Menu, self).AppendItem(*args, **kwargs)
if na > 2 and isinstance(args[2], wx.Menu):
return super(Menu, self).AppendMenu(*args, **kwargs)
plain_append = isinstance(args[0], int) and (na > 1 and isinstance(args[1], str) or "item" in kwargs)
if plain_append:
return super(Menu, self).Append(*args, **kwargs)
assert False, "unexpected argument types"
if WX4:
Menu = wx.Menu # noqa: F811
# ----------------------------------------------------------------------------
class ListCtrl(wx.ListCtrl):
InsertItem = wx.ListCtrl.InsertStringItem
SetItem = wx.ListCtrl.SetStringItem
if WX4:
ListCtrl = wx.ListCtrl # noqa: F811
# ----------------------------------------------------------------------------
class TreeCtrl(wx.TreeCtrl):
GetItemData = wx.TreeCtrl.GetPyData
SetItemData = wx.TreeCtrl.SetPyData
if WX4:
TreeCtrl = wx.TreeCtrl # noqa: F811
# wx.ToolBar -----------------------------------------------------------------
def AddTool(self, *args, **kwargs):
return super(wx.ToolBar, self).AddLabelTool(*args, **kwargs)
def patchToolBarMethods(toolbar):
if WX3:
toolbar.AddTool = types.MethodType(AddTool, toolbar)
return
# Functions ------------------------------------------------------------------
IsMainThread = wx.Thread_IsMain if WX3 else wx.IsMainThread
NewIdRef = wx.NewIdRef if WX4 else wx.NewId
# Final checks ---------------------------------------------------------------
assert WX3 ^ (Menu is wx.Menu)
assert WX3 ^ (TreeCtrl is wx.TreeCtrl)
assert WX3 ^ (ListCtrl is wx.ListCtrl)