Search code examples
rubygtk3

ruby and Gtk::FontChooserDialog.new font size


I am using ruby 2.6.3, installed by compiling the source. When using Gtk::FontChooserDialog.new, the default font size given is 10. Is it possible to call Gtk::FontChooserDialog.new with a different size, such as 24, so that I can avoid having to change the size each time I select a font. Here is how I am doing things:

dialog = Gtk::FontChooserDialog.new(:title => "Select font",
                                          :parent => self,                                         
                                          :action =>  Gtk::FileChooserAction::OPEN,
                                      :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])

I have tried (in the argument list) :size => 24, :default_size => 24, etc. This does not work. I'm just guessing here. I have searched a lot, with no luck. I also looked in the gem sample dirs at test-gtk-font-chooser-dialog.rb and other files but no luck.

I am using Linux Mint Mate 19.1, installed a couple of weeks ago.


Solution

  • You need to set the size through a Pango.FontDescription. A short example in Python would be:

    font_chooser = Gtk.FontChooserDialog.new(title = "Select font", parent = self)
    font_description = Pango.FontDescription.new()
    font_description.set_size(24 * Pango.SCALE)
    font_chooser.set_font_desc(font_description)
    

    EDIT And here is a complete example in Ruby:

    #!/usr/bin/ruby
    
    '''
    ZetCode Ruby GTK tutorial
    
    This program runs a font dialog with a default (settable) font size.
    
    Author: Jan Bodnar
    Website: www.zetcode.com
    Last modified: May 2014
    '''
    
    require 'gtk3'
    require 'pango'
    
    class RubyApp < Gtk::Window
    
        def initialize
            super
            init_ui
        end
    
        def init_ui
    
            set_title "Center"
            signal_connect "destroy" do 
                Gtk.main_quit 
            end
    
            button = Gtk::Button.new
            button.set_label "Font"    
            button.signal_connect "clicked" do 
                dialog = Gtk::FontChooserDialog.new(:title => "Select font", :parent => self, :action =>  Gtk::FileChooserAction::OPEN, :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
                font_description = Pango::FontDescription.new
                font_description.set_size 24 * Pango::SCALE
                dialog.set_font_desc font_description
                dialog.run
            end
    
            add button
            set_default_size 300, 200
            set_window_position Gtk::WindowPosition::CENTER
            show_all
        end   
    end
    
    
    window = RubyApp.new
    Gtk.main