~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to platform/osx/plat/frontends/widgets/simple.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17
 
#
18
 
# In addition, as a special exception, the copyright holders give
19
 
# permission to link the code of portions of this program with the OpenSSL
20
 
# library.
21
 
#
22
 
# You must obey the GNU General Public License in all respects for all of
23
 
# the code used other than OpenSSL. If you modify file(s) with this
24
 
# exception, you may extend this exception to your version of the file(s),
25
 
# but you are not obligated to do so. If you do not wish to do so, delete
26
 
# this exception statement from your version. If you delete this exception
27
 
# statement from all source files in the program, then also delete it here.
28
 
 
29
 
import math
30
 
 
31
 
from AppKit import *
32
 
from Foundation import *
33
 
from objc import YES, NO, nil
34
 
 
35
 
from miro.plat.utils import filenameToUnicode
36
 
from miro.frontends.widgets import widgetconst
37
 
from miro.plat.frontends.widgets.base import Widget, SimpleBin, FlippedView
38
 
 
39
 
"""A collection of various simple widgets."""
40
 
 
41
 
class Image(object):
42
 
    """See https://develop.participatoryculture.org/trac/democracy/wiki/WidgetAPI for a description of the API for this class."""
43
 
    def __init__(self, path):
44
 
        self.nsimage = NSImage.alloc().initByReferencingFile_(filenameToUnicode(path))
45
 
        self.width = self.nsimage.size().width
46
 
        self.height = self.nsimage.size().height
47
 
 
48
 
    def resize(self, width, height):
49
 
        return ResizedImage(self, width, height)
50
 
 
51
 
class ResizedImage(Image):
52
 
    def __init__(self, image, width, height):
53
 
        self.nsimage = image.nsimage.copy()
54
 
        self.nsimage.setCacheMode_(NSImageCacheNever)
55
 
        self.nsimage.setScalesWhenResized_(YES)
56
 
        self.nsimage.setSize_(NSSize(width, height))
57
 
        self.width = width
58
 
        self.height = height
59
 
 
60
 
class NSImageDisplay (NSView):
61
 
    def initWithImage_(self, image):
62
 
        self = super(NSImageDisplay, self).init()
63
 
        self.image = image
64
 
        return self
65
 
    def drawRect_(self, rect):
66
 
        NSGraphicsContext.currentContext().setShouldAntialias_(YES)
67
 
        NSGraphicsContext.currentContext().setImageInterpolation_(NSImageInterpolationHigh)
68
 
        self.image.nsimage.drawInRect_fromRect_operation_fraction_(rect, NSZeroRect, NSCompositeSourceOver, 1.0)
69
 
 
70
 
class ImageDisplay(Widget):
71
 
    """See https://develop.participatoryculture.org/trac/democracy/wiki/WidgetAPI for a description of the API for this class."""
72
 
    def __init__(self, image):
73
 
        Widget.__init__(self)
74
 
        self.image = image
75
 
        self.image.nsimage.setCacheMode_(NSImageCacheNever)
76
 
        self.view = NSImageDisplay.alloc().initWithImage_(self.image)
77
 
 
78
 
    def calc_size_request(self):
79
 
        return self.image.width, self.image.height
80
 
 
81
 
class AnimatedImageDisplay(Widget):
82
 
    def __init__(self, path):
83
 
        Widget.__init__(self)
84
 
        self.nsimage = NSImage.alloc().initByReferencingFile_(filenameToUnicode(path))
85
 
        self.view = NSImageView.alloc().init()
86
 
        self.view.setImage_(self.nsimage)
87
 
 
88
 
    def calc_size_request(self):
89
 
        return self.nsimage.size().width, self.nsimage.size().height
90
 
 
91
 
class Label(Widget):
92
 
    """See https://develop.participatoryculture.org/trac/democracy/wiki/WidgetAPI for a description of the API for this class."""
93
 
    def __init__(self, text="", wrap=False):
94
 
        Widget.__init__(self)
95
 
        self.view = NSTextField.alloc().init()
96
 
        self.view.setEditable_(NO)
97
 
        self.view.setBezeled_(NO)
98
 
        self.view.setBordered_(NO)
99
 
        self.view.setDrawsBackground_(NO)
100
 
        self.wrap = wrap
101
 
        self.bold = False
102
 
        self.size = NSFont.systemFontSize()
103
 
        self.sizer_cell = self.view.cell().copy()
104
 
        self.set_font()
105
 
        self.set_text(text)
106
 
        self.__color = self.view.textColor()
107
 
 
108
 
    def set_bold(self, bold):
109
 
        self.bold = bold
110
 
        self.set_font()
111
 
 
112
 
    def set_size(self, size):
113
 
        if size > 0:
114
 
            self.size = NSFont.systemFontSize() * size
115
 
        elif size == widgetconst.SIZE_SMALL:
116
 
            self.size = NSFont.smallSystemFontSize()
117
 
        elif size == widgetconst.SIZE_NORMAL:
118
 
            self.size = NSFont.systemFontSize()
119
 
        else:
120
 
            raise ValueError("Unknown size constant: %s" % size)
121
 
 
122
 
        self.set_font()
123
 
 
124
 
    def set_color(self, color):
125
 
        self.__color = self.make_color(color)
126
 
 
127
 
        if self.view.isEnabled():
128
 
            self.view.setTextColor_(self.__color)
129
 
        else:
130
 
            self.view.setTextColor_(self.__color.colorWithAlphaComponent_(0.5))
131
 
 
132
 
    def set_background_color(self, color):
133
 
        self.view.setBackgroundColor_(self.make_color(color))
134
 
        self.view.setDrawsBackground_(YES)
135
 
 
136
 
    def set_font(self):
137
 
        if self.bold:
138
 
            font = NSFont.boldSystemFontOfSize_(self.size)
139
 
        else:
140
 
            font= NSFont.systemFontOfSize_(self.size)
141
 
        self.view.setFont_(font)
142
 
        self.sizer_cell.setFont_(font)
143
 
        self.invalidate_size_request()
144
 
 
145
 
    def calc_size_request(self):
146
 
        if (self.wrap and self.manual_size_request is not None and 
147
 
                self.manual_size_request[0] > 0):
148
 
            wrap_width = self.manual_size_request[0]
149
 
            size = self.sizer_cell.cellSizeForBounds_(NSMakeRect(0, 0,
150
 
                wrap_width, 10000))
151
 
        else:
152
 
            size = self.sizer_cell.cellSize()
153
 
        return math.ceil(size.width), math.ceil(size.height)
154
 
 
155
 
    def baseline(self):
156
 
        return -self.view.font().descender()
157
 
 
158
 
    def set_text(self, text):
159
 
        self.view.setStringValue_(text)
160
 
        self.sizer_cell.setStringValue_(text)
161
 
        self.invalidate_size_request()
162
 
 
163
 
    def get_text(self):
164
 
        self.view.stringValue()
165
 
 
166
 
    def set_wrap(self, wrap):
167
 
        self.wrap = True
168
 
        self.invalidate_size_request()
169
 
 
170
 
    def enable(self):
171
 
        Widget.enable(self)
172
 
        self.view.setTextColor_(self.__color)
173
 
        self.view.setEnabled_(True)
174
 
 
175
 
    def disable(self):
176
 
        Widget.disable(self)
177
 
        self.view.setTextColor_(self.__color.colorWithAlphaComponent_(0.5))
178
 
        self.view.setEnabled_(False)
179
 
 
180
 
class SolidBackground(SimpleBin):
181
 
    def __init__(self,  color=None):
182
 
        SimpleBin.__init__(self)
183
 
        self.view = FlippedView.alloc().init()
184
 
        if color is not None:
185
 
            self.set_background_color(color)
186
 
 
187
 
    def set_background_color(self, color):
188
 
        self.view.setBackgroundColor_(self.make_color(color))
189
 
 
190
 
class ProgressBar(Widget):
191
 
    def __init__(self):
192
 
        Widget.__init__(self)
193
 
        self.view = NSProgressIndicator.alloc().init()
194
 
        self.view.setMaxValue_(1.0)
195
 
        self.view.setIndeterminate_(False)
196
 
 
197
 
    def calc_size_request(self):
198
 
        return 20, 20
199
 
 
200
 
    def set_progress(self, fraction):
201
 
        self.view.setIndeterminate_(False)
202
 
        self.view.setDoubleValue_(fraction)
203
 
 
204
 
    def start_pulsing(self):
205
 
        self.view.setIndeterminate_(True)
206
 
        self.view.startAnimation_(nil)
207
 
 
208
 
    def stop_pulsing(self):
209
 
        self.view.stopAnimation_(nil)