text 25 Mar Set Gtk::Label wrap dinamically

Ahime` come si sa` le Gtk::Label non supportano il wrapping dinamico su resize.
Vabbe`, estendiamo Gtk::Label e iimplementiamo il wrap dinamico.
Ecco la classe in Ruby:

require 'gtk2'
class WrapLabel < Gtk::Label
    def initialize(s = '')
      super()
      self.wrap = true
      self.wrap_mode = Pango::WRAP_WORD_CHAR
      self.set_alignment(0.0, 0.0)
      self.text = s
      self.signal_connect_after('size-allocate') do |w, a|
        lw_old, lh_old = self.layout.size
        if lw_old / Pango::SCALE != a.width
          self.layout.set_width(a.width * Pango::SCALE)
          lw, lh = self.layout.size
          self.set_size_request(-1, lh / Pango::SCALE) if lh_old != lh
        end
      end
    end
  end
end

blog comments powered by Disqus