Created
January 14, 2016 00:20
-
-
Save ptomato/dd8fd4b49a162e5039e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from gi.repository import Gdk, Gtk, Pango | |
WIDTH = 150 | |
HEIGHT = 207 | |
CSS = ''' | |
.frame { border: 1px solid red; } | |
''' | |
class FixedSize(Gtk.Frame): | |
def __init__(self, **props): | |
Gtk.Frame.__init__(self, **props) | |
def do_get_request_mode(self): | |
return Gtk.SizeRequestMode.CONSTANT_SIZE | |
def do_get_preferred_width(self): | |
return WIDTH, WIDTH | |
def do_get_preferred_height(self): | |
return HEIGHT, HEIGHT | |
class AdaptiveLabel(Gtk.Label): | |
def __init__(self, **props): | |
Gtk.Label.__init__(self, **props) | |
def do_size_allocate(self, alloc): | |
Gtk.Label.do_size_allocate(self, alloc) | |
layout = self.create_pango_layout('0') | |
ink_rect, logical_rect = layout.get_pixel_extents() | |
lines = int(math.floor(alloc.height / logical_rect.height)) | |
self.props.lines = lines | |
provider = Gtk.CssProvider() | |
provider.load_from_data(CSS) | |
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), provider, | |
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) | |
win = Gtk.Window() | |
card = FixedSize(expand=False, halign=Gtk.Align.START, valign=Gtk.Align.START) | |
text = AdaptiveLabel(label='Drive the road / to your surrender / time comes ' | |
'around / out of my hands / Small boats / on the beach at dead of night / ' | |
'Come and go / before first light / First light / Leave me running / into ' | |
'the wind / King of the world / How do you feel / What is there to feel', | |
wrap=True, wrap_mode=Pango.WrapMode.WORD_CHAR, | |
ellipsize=Pango.EllipsizeMode.END, lines=15) | |
card.add(text) | |
win.add(card) | |
win.show_all() | |
win.connect('destroy', Gtk.main_quit) | |
Gtk.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment