1
# Miro - an RSS based video player application
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
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.
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.
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
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
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.
32
from Foundation import *
33
from objc import YES, NO, nil
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
39
"""A collection of various simple widgets."""
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
48
def resize(self, width, height):
49
return ResizedImage(self, width, height)
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))
60
class NSImageDisplay (NSView):
61
def initWithImage_(self, image):
62
self = super(NSImageDisplay, self).init()
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)
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):
75
self.image.nsimage.setCacheMode_(NSImageCacheNever)
76
self.view = NSImageDisplay.alloc().initWithImage_(self.image)
78
def calc_size_request(self):
79
return self.image.width, self.image.height
81
class AnimatedImageDisplay(Widget):
82
def __init__(self, path):
84
self.nsimage = NSImage.alloc().initByReferencingFile_(filenameToUnicode(path))
85
self.view = NSImageView.alloc().init()
86
self.view.setImage_(self.nsimage)
88
def calc_size_request(self):
89
return self.nsimage.size().width, self.nsimage.size().height
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):
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)
102
self.size = NSFont.systemFontSize()
103
self.sizer_cell = self.view.cell().copy()
106
self.__color = self.view.textColor()
108
def set_bold(self, bold):
112
def set_size(self, size):
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()
120
raise ValueError("Unknown size constant: %s" % size)
124
def set_color(self, color):
125
self.__color = self.make_color(color)
127
if self.view.isEnabled():
128
self.view.setTextColor_(self.__color)
130
self.view.setTextColor_(self.__color.colorWithAlphaComponent_(0.5))
132
def set_background_color(self, color):
133
self.view.setBackgroundColor_(self.make_color(color))
134
self.view.setDrawsBackground_(YES)
138
font = NSFont.boldSystemFontOfSize_(self.size)
140
font= NSFont.systemFontOfSize_(self.size)
141
self.view.setFont_(font)
142
self.sizer_cell.setFont_(font)
143
self.invalidate_size_request()
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,
152
size = self.sizer_cell.cellSize()
153
return math.ceil(size.width), math.ceil(size.height)
156
return -self.view.font().descender()
158
def set_text(self, text):
159
self.view.setStringValue_(text)
160
self.sizer_cell.setStringValue_(text)
161
self.invalidate_size_request()
164
self.view.stringValue()
166
def set_wrap(self, wrap):
168
self.invalidate_size_request()
172
self.view.setTextColor_(self.__color)
173
self.view.setEnabled_(True)
177
self.view.setTextColor_(self.__color.colorWithAlphaComponent_(0.5))
178
self.view.setEnabled_(False)
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)
187
def set_background_color(self, color):
188
self.view.setBackgroundColor_(self.make_color(color))
190
class ProgressBar(Widget):
192
Widget.__init__(self)
193
self.view = NSProgressIndicator.alloc().init()
194
self.view.setMaxValue_(1.0)
195
self.view.setIndeterminate_(False)
197
def calc_size_request(self):
200
def set_progress(self, fraction):
201
self.view.setIndeterminate_(False)
202
self.view.setDoubleValue_(fraction)
204
def start_pulsing(self):
205
self.view.setIndeterminate_(True)
206
self.view.startAnimation_(nil)
208
def stop_pulsing(self):
209
self.view.stopAnimation_(nil)