eclutil.py
6.9 KB
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
###############################################################################
# Name: eclutil.py #
# Purpose: Common library utilities. #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
# Licence: wxWindows Licence #
###############################################################################
"""
Editra Control Library: Editra Control Library Utility
Miscellaneous utility functions and gui helpers
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: eclutil.py 71178 2012-04-11 22:42:28Z CJP $"
__revision__ = "$Revision: 71178 $"
__all__ = ['AdjustAlpha', 'AdjustColour', 'BestLabelColour', 'HexToRGB',
'GetHighlightColour', 'EmptyBitmapRGBA', 'Freezer',
'DRAW_CIRCLE_SMALL', 'DRAW_CIRCLE_NORMAL', 'DRAW_CIRCLE_LARGE',
'DrawCircleCloseBmp' ]
#-----------------------------------------------------------------------------#
# Imports
import wx
if wx.Platform == '__WXMAC__':
try:
import Carbon.Appearance
except ImportError:
CARBON = False
else:
CARBON = True
#-----------------------------------------------------------------------------#
# DrawCircleCloseBmp options
DRAW_CIRCLE_SMALL = 0
DRAW_CIRCLE_NORMAL = 1
DRAW_CIRCLE_LARGE = 2
__CircleDefs = { DRAW_CIRCLE_SMALL : dict(size=(8, 8),
xpath=((1.75, 2), (4.75, 5),
(1.75, 5), (4.75, 2))),
DRAW_CIRCLE_NORMAL : dict(size=(16, 16),
xpath=((4.5, 4), (10.5, 10),
(4.5, 10), (10.5, 4))),
DRAW_CIRCLE_LARGE : dict(size=(32, 32),
xpath=((8, 8), (20, 20),
(8, 20), (20, 8))) }
#-----------------------------------------------------------------------------#
# Colour Utilities
def AdjustAlpha(colour, alpha):
"""Adjust the alpha of a given colour"""
return wx.Colour(colour.Red(), colour.Green(), colour.Blue(), alpha)
def AdjustColour(color, percent, alpha=wx.ALPHA_OPAQUE):
""" Brighten/Darken input colour by percent and adjust alpha
channel if needed. Returns the modified color.
@param color: color object to adjust
@param percent: percent to adjust +(brighten) or -(darken)
@keyword alpha: amount to adjust alpha channel
"""
radj, gadj, badj = [ int(val * (abs(percent) / 100.0))
for val in color.Get() ]
if percent < 0:
radj, gadj, badj = [ val * -1 for val in [radj, gadj, badj] ]
else:
radj, gadj, badj = [ val or 255 for val in [radj, gadj, badj] ]
red = min(color.Red() + radj, 255)
green = min(color.Green() + gadj, 255)
blue = min(color.Blue() + badj, 255)
return wx.Colour(red, green, blue, alpha)
def BestLabelColour(color):
"""Get the best color to use for the label that will be drawn on
top of the given color.
@param color: background color that text will be drawn on
"""
avg = sum(color.Get()) // 3
if avg > 128:
txt_color = wx.BLACK
else:
txt_color = wx.WHITE
return txt_color
def GetHighlightColour():
"""Get the default highlight color
@return: wx.Colour
"""
if wx.Platform == '__WXMAC__':
if CARBON:
if hasattr(wx, 'MacThemeColour'):
color = wx.MacThemeColour(Carbon.Appearance.kThemeBrushFocusHighlight)
return color
else:
# kThemeBrushButtonPressedLightHighlight
brush = wx.Brush(wx.BLACK)
brush.MacSetTheme(Carbon.Appearance.kThemeBrushFocusHighlight)
return brush.GetColour()
# Fallback to text highlight color
return wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT)
def HexToRGB(hex_str):
"""Returns a list of red/green/blue values from a
hex string.
@param hex_str: hex string to convert to rgb
"""
hexval = hex_str
if hexval[0] == u"#":
hexval = hexval[1:]
ldiff = 6 - len(hexval)
hexval += ldiff * u"0"
# Convert hex values to integer
red = int(hexval[0:2], 16)
green = int(hexval[2:4], 16)
blue = int(hexval[4:], 16)
return [red, green, blue]
#-----------------------------------------------------------------------------#
def EmptyBitmapRGBA(width, height):
"""Create an empty bitmap with an alpha channel"""
if hasattr(wx, 'EmptyBitmapRGBA'):
bmp = wx.EmptyBitmapRGBA(width, height, alpha=0)
else:
bmp = wx.EmptyBitmap(width, height, -1)
if hasattr(bmp, 'UseAlpha'):
bmp.UseAlpha()
return bmp
#-----------------------------------------------------------------------------#
class Freezer(object):
"""Context manager for freezing window redraws"""
def __init__(self, window):
super(Freezer, self).__init__()
# Attributes
self.window = window
def __enter__(self):
if self.window:
self.window.Freeze()
def __exit__( self, type, value, tb):
if self.window:
self.window.Thaw()
#-----------------------------------------------------------------------------#
# Drawing helpers
def DrawCircleCloseBmp(colour, backColour=None, option=DRAW_CIRCLE_SMALL):
"""
Draws a small circular close button.
@param colour: Circle's background colour
@keyword backColour: pen colour for border and X
@keyword option: DRAW_CIRCLE_* value
@return: wxBitmap
"""
assert option in __CircleDefs, "Invalid DRAW option!"
defs = __CircleDefs.get(option)
size = defs['size']
if option != DRAW_CIRCLE_SMALL:
# Adjust for border
diameter = size[0] - 1
else:
diameter = size[0]
radius = float(diameter) / 2.0
xpath = defs['xpath']
bmp = EmptyBitmapRGBA(size[0], size[1])
dc = wx.MemoryDC()
dc.SelectObject(bmp)
dc.Clear()
gc = wx.GraphicsContext.Create(dc)
gc.SetBrush(wx.Brush(colour))
if option > DRAW_CIRCLE_SMALL:
gc.SetPen(wx.Pen(AdjustColour(colour, -30)))
else:
gc.SetPen(wx.TRANSPARENT_PEN)
path = gc.CreatePath()
path.AddCircle(radius, radius, radius)
path.CloseSubpath()
gc.FillPath(path)
gc.StrokePath(path)
path = gc.CreatePath()
if backColour is not None:
pen = wx.Pen(backColour, 1)
else:
pen = wx.Pen("white", 1)
pen.SetCap(wx.CAP_BUTT)
pen.SetJoin(wx.JOIN_BEVEL)
gc.SetPen(pen)
path.MoveToPoint(*xpath[0])
path.AddLineToPoint(*xpath[1])
path.MoveToPoint(*xpath[2])
path.AddLineToPoint(*xpath[3])
path.CloseSubpath()
gc.DrawPath(path)
dc.SelectObject(wx.NullBitmap)
return bmp