~ubuntu-branches/ubuntu/wily/phatch/wily

« back to all changes in this revision

Viewing changes to .pc/wxpy3.0-compat.patch/phatch/lib/pyWx/paint.py

  • Committer: Package Import Robot
  • Author(s): Olly Betts
  • Date: 2014-08-31 18:10:50 UTC
  • mfrom: (4.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20140831181050-bsuwtc59i0whjxcw
Tags: 0.2.7.1-3.1
* Non-maintainer upload.
* Update for wxpython3.0 (Closes: #758942):
  + New patch: wxpy3.0-compat.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: UTF-8 -*-
 
2
 
 
3
# Copyright (C) 2007-2008 www.stani.be
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program.  If not, see http://www.gnu.org/licenses/
 
17
#
 
18
# Follow PEP8
 
19
 
 
20
import wx
 
21
from compatible import GCDC, FONT_SIZE
 
22
import graphics
 
23
MARGIN = 2 * 10
 
24
 
 
25
 
 
26
class Mixin:
 
27
    paint_message = ''
 
28
    paint_logo = None
 
29
    paint_color = wx.Colour(60, 60, 60)  # ,200)
 
30
    paint_border_color = None
 
31
    paint_opacity = 200
 
32
    paint_radius = 8
 
33
 
 
34
    def OnEraseBackground(self, event=None, paint_object=None):
 
35
        paint_object = event.GetEventObject()
 
36
        if not paint_object.IsShown():
 
37
            return
 
38
        _dc = event.GetDC()
 
39
        if not _dc:
 
40
            _dc = wx.ClientDC(paint_object)
 
41
            rect = paint_object.GetUpdateRegion().GetBox()
 
42
            _dc.SetClippingRect(rect)
 
43
        dc = GCDC(_dc)
 
44
        dc.Clear()
 
45
        # Calculate text extents.
 
46
        paint_message = self.GetPaintMessage()
 
47
        if paint_message:
 
48
            tw, th = self.GetClientSize()
 
49
            cw, ch = tw - MARGIN, th - MARGIN
 
50
            font_size = FONT_SIZE + 1
 
51
            while (tw >= cw or th >= ch) and font_size > 5:
 
52
                font_size -= 1
 
53
                font = wx.Font(font_size, wx.FONTFAMILY_SWISS,
 
54
                                wx.FONTSTYLE_NORMAL, wx.FONTSTYLE_NORMAL,
 
55
                                encoding=wx.FONTENCODING_SYSTEM)
 
56
                dc.SetFont(font)
 
57
                tw, th = dc.GetTextExtent(paint_message)
 
58
            td = font_size / 2
 
59
            twd = tw + 2 * td
 
60
            thd = th + 2 * td
 
61
        else:
 
62
            tw = th = 0
 
63
        # Draw logo.
 
64
        ew, eh = paint_object.GetSize()
 
65
        if self.paint_logo:
 
66
            # Draw logo.
 
67
            lw, lh = self._paint_logo.GetSize()
 
68
            lx, ly = (ew - lw) / 2, (eh - lh + 2 * thd) / 2
 
69
            dc.DrawBitmap(self._paint_logo, lx, ly, True)
 
70
        else:
 
71
            # Skip logo.
 
72
            lx, ly = ew / 2, eh / 2
 
73
        # Check if text is necessary too.
 
74
        if not paint_message:
 
75
            return
 
76
        # Draw rounded rectangle.
 
77
        if self.paint_logo:
 
78
            rx, ry = (ew - twd) / 2, ly - 2 * thd
 
79
        else:
 
80
            rx, ry = (ew - twd) / 2, (eh - thd) / 2
 
81
        rect = wx.Rect(rx, ry, twd, thd)
 
82
        if self.paint_border_color:
 
83
            penclr = self.paint_border_color
 
84
        else:
 
85
            penclr = self.paint_color
 
86
        dc.SetPen(wx.Pen(penclr))
 
87
        dc.SetBrush(wx.Brush(self.paint_color))
 
88
        dc.DrawRoundedRectangleRect(rect, self.paint_radius)
 
89
        # Draw text.
 
90
        dc.SetTextForeground(paint_object.GetBackgroundColour())
 
91
        dc.DrawText(paint_message, rx + td, ry + td)
 
92
 
 
93
    def EnableBackgroundPainting(self, object, state=True, color=wx.WHITE):
 
94
        if state:
 
95
            if self.paint_logo:
 
96
                self._paint_logo = graphics.bitmap(self.paint_logo)
 
97
            object.SetBackgroundColour(color)
 
98
            object.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
 
99
        else:
 
100
            object.Unbind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
 
101
 
 
102
    def GetPaintMessage(self):
 
103
        return self.paint_message
 
104
 
 
105
 
 
106
#---begin
 
107
def example():
 
108
    import sys
 
109
    sys.path.extend(['..'])
 
110
    import images
 
111
 
 
112
    class TestFrame(Mixin, wx.Frame):
 
113
        paint_message = 'hello world'
 
114
        paint_logo = images.LOGO
 
115
 
 
116
    class TestApp(wx.App):
 
117
        def OnInit(self):
 
118
            wx.InitAllImageHandlers()
 
119
            frame = TestFrame(None, -1, "Test", size=(600, 400))
 
120
            frame.EnableBackgroundPainting(frame)  # ,color=(245,245,255))
 
121
            self.SetTopWindow(frame)
 
122
            frame.Show(True)
 
123
            return 1
 
124
 
 
125
    app = TestApp(0)
 
126
    app.MainLoop()
 
127
 
 
128
if __name__ == '__main__':
 
129
    example()