New font.char_width property to return approximate char width. Set Palette's text_maxlen to a half of the screen by defualt. --- src/sugar/graphics/palette.py | 21 +++++++++++++++++---- src/sugar/graphics/style.py | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 8 deletions(-)
diff --git a/src/sugar/graphics/palette.py b/src/sugar/graphics/palette.py index 46f167b..b542407 100644 --- a/src/sugar/graphics/palette.py +++ b/src/sugar/graphics/palette.py @@ -45,10 +45,23 @@ class Palette(PaletteWindow): __gtype_name__ = 'SugarPalette' def __init__(self, label=None, accel_path=None, menu_after_content=False, - text_maxlen=60, **kwargs): - # DEPRECATED: label is passed with the primary-text property, - # accel_path is set via the invoker property, and menu_after_content - # is not used + text_maxlen=None, **kwargs): + """Constructor. + + Keyword arguments: + label -- DEPRECATED, is passed with the primary-text property + accel_path -- DEPRECATED, is set via the invoker property + menu_after_content -- DEPRECATED, is not used + text_maxlen -- max length of palette titles after that it becomes + ellipsized, possible values: + None -- autodetect most appropriate value, by default + <= 0 -- do not ellipsize titles + > 0 -- valid width value + + """ + if text_maxlen is None: + text_maxlen = (gtk.gdk.screen_width() / 2) / \ + style.FONT_DEFAULT.char_width self._primary_text = None self._secondary_text = None diff --git a/src/sugar/graphics/style.py b/src/sugar/graphics/style.py index 7f48d9a..e8dc768 100644 --- a/src/sugar/graphics/style.py +++ b/src/sugar/graphics/style.py @@ -45,15 +45,46 @@ def _compute_zoom_factor(): class Font(object): + """High level font information.""" - def __init__(self, desc): - self._desc = desc + def __init__(self, desc=None): + """Constructor. + + Keyword arguments: + desc -- string with font description + + """ + default_context = gtk.Label().get_pango_context() + + if desc is None: + self._desc = default_context.get_font_description() + else: + self._desc = pango.FontDescription(desc) + + metrics = default_context.get_metrics(self._desc) + self._char_width = max(metrics.get_approximate_char_width(), + metrics.get_approximate_digit_width()) / pango.SCALE def __str__(self): - return self._desc + return self._desc.to_string() def get_pango_desc(self): - return pango.FontDescription(self._desc) + """Returns pango font description object.""" + return self._desc + + pango_desc = property(get_pango_desc) + + def get_char_width(self): + """Approximate char width. + + This is merely a representative value that is useful, for example, for + determining the initial size for a window. Actual characters in text + will be wider and narrower than this. + + """ + return self._char_width + + char_width = property(get_char_width) class Color(object): @@ -119,6 +150,7 @@ client = gconf.client_get_default() FONT_SIZE = client.get_float('/desktop/sugar/font/default_size') FONT_FACE = client.get_string('/desktop/sugar/font/default_face') +FONT_DEFAULT = Font() FONT_NORMAL = Font('%s %f' % (FONT_FACE, FONT_SIZE)) FONT_BOLD = Font('%s bold %f' % (FONT_FACE, FONT_SIZE)) FONT_NORMAL_H = zoom(24) -- 1.7.3.3 _______________________________________________ Sugar-devel mailing list Sugar-devel@lists.sugarlabs.org http://lists.sugarlabs.org/listinfo/sugar-devel