7 Ekim 2013 Pazartesi

Ruby Gtk3 Binding Tutorial

I want to work on a project using Gtk3 & Ruby so I looked up tutorial about Ruby & Gtk3. But I couldn't find anywhere. Actually Ruby & Gtk3 binding available here. We can use it installing Gtk3 gem.

Before I studied using Gtk2 & Ruby and utilized zetcode and Ruby Gtk2 page.
Zetcode has some samples about Ruby & Gtk2. I thought, I can convert Gtk2 samples to Gtk3 at Zetcode so I can see differences between Gtk2 and Gtk3. Because really Gtk3 is necessary for my project.

 I tried to convert and got errors. Some errors outputs are very clear, I could figure out them. Some errors aren't clear but I could figure out them using PRY interactive shell. It is very usefull, It is more than better IRB shell :) I added differences between Gtk2 and Gtk3 below. I put Gtk3 examples to my  github repo and I explain differences between Gtk2 and Gtk3 on my github repo. If I can find new differences, I will add them to github repo.

Horizontal And Vertical Scales
# deprecated -> scale = Gtk::HScale.new
# use Gtk::Scale.new(:horizontal, *args)

scale = Gtk::Scale.new(:horizontal)

Button:
# deprecated -> button = Gtk::Button.new "About"
button = Gtk::Button.new(:label => "About")

Color Selection Dialog:
# deprecated -> cdia = Gtk::ColorSelectionDialog.new "Select color"
cdia = Gtk::ColorSelectionDialog.new(:title => "Select color")

colorsel = cdia.color_selection
# color = colorsel.current_color, .current_color returns Gtk::Color but it should return Gdk::RGBA
color = colorsel.current_rgba

Font Selection Dialog:
# deprecated fdia = Gtk::FontSelectionDialog.new "Select font name"
fdia = Gtk::FontChooserDialog.new(:title => "Select font name", :parent => nil)

# deprecated if response == Gtk::Dialog::RESPONSE_OK, you can use Gtk::ResponseType::OK
if response == Gtk::ResponseType::OK

# deprecated -> font_desc = Pango::FontDescription.new fdia.font_name
font_desc_ = Pango::FontDescription.new fdia.font_desc

@label = Gtk::Label.new "The only victory over love is flight."
# deprecated -> @label.modify_font font_desc
@label.override_font(font_desc_)

Dialog Response Type:
# deprecated ->  md = Gtk::MessageDialog.new(self, Gtk::Dialog::MODAL |
# Gtk::Dialog::DESTROY_WITH_PARENT, Gtk::MessageDialog::ERROR, 
# Gtk::MessageDialog::BUTTONS_CLOSE, "Error loading file")
md = Gtk::MessageDialog.new(:parent => self, :flags => :destroy_with_parent,
                            :type => :info, :buttons_type => :close, 
                            :message => "Download completed")
                            
# deprecated -> Gtk::MessageDialog::ERROR, you should use :error
# deprecated -> Gtk::MessageDialog::QUESTION, you should use :question
# deprecated -> Gtk::MessageDialog::WARNING, you should use :warning

Window:
window = Gtk::Window.new
# deprecated -> window.set_window_position Gtk::Window::POS_CENTER
# you can use :center or Gtk::Window::Position::CENTER
window.set_window_position(Gtk::Window::Position::CENTER)


# deprecated -> window.modify_bg Gtk::STATE_NORMAL, Gdk::Color.new(6400, 6400, 6440) 
window.override_background_color = :normal, Gdk::Color.new(6400, 6400, 6440)

Vertical And Horizontal Box:
# deprecated -> vbox = Gtk::VBox.new false, 5 
vbox = Gtk::Box.new(:vertical, 5)
# deprecated -> hbox = Gtk::HBox.new true, 3

# deprecated -> vbox.pack_start halign, false, false, 3
# 'Gtk::Box#pack_start(child, :expand => true, :fill => true, :padding => 0)'
vbox.pack_start(halign, :expand => false, :fill => false, :padding => 3) 

Table:
# deprecated -> table.attach Gtk::Button.new("Cls"), 0, 1, 0, 1
table.attach Gtk::Button.new(:label => "Cls"), 0, 1, 0, 1

# use Gtk::AttachOptions::FILL instead of Gtk::FILL
table.attach(halign, 0, 1, 0, 1, Gtk::AttachOptions::FILL,
             Gtk::AttachOptions::FILL, 0, 0)

# use Gtk::AttachOptions::EXPAND instead of Gtk::EXPAND
# use Gtk::AttachOptions::SHRINK instead of Gtk::SHRINK

PixBuf & Image:
bardejov = Gdk::Pixbuf.new(:file => "sample_image.jpg")
# deprecated -> image1 = Gtk::Image.new bardejov
# all options
# image1 = Gtk::Image.new(:stock => nil, :icon_name => nil, :icon_set => nil, 
# :gicon => nil, :file => nil, :pixbuf => bardejov, :animation => nil, :size => nil)
image1 = Gtk::Image.new(:pixbuf => bardejov)

# deprecated ->  newi = Gtk::ImageMenuItem.new Gtk::Stock::NEW, agr
newi = Gtk::ImageMenuItem.new(:stock_id => Gtk::Stock::NEW, :accel_group => agr)

# deprecated -> newi.add_accelerator("activate", agr, key, 
#                                    mod, Gtk::ACCEL_VISIBLE)
# you can use ":visible" instead of Gtk::AccelFlags::VISIBLE
newi.add_accelerator("activate", agr, key,
                     mod, Gtk::AccelFlags::VISIBLE)

Tool Button & Bar:
# deprecated -> newtb = Gtk::ToolButton.new Gtk::Stock::NEW
newtb = Gtk::ToolButton.new(:stock_id => Gtk::Stock::NEW)

# deprecated -> toolbar.insert 0, newtb
toolbar.insert(newtb, 0)

Draw:
darea = Gtk::DrawingArea.new  
# deprecated -> @darea.signal_connect("expose-event"){..}
darea.signal_connect('draw'){...}

Combo Box:
# deprecated -> cb = Gtk::ComboBox.new
cb = Gtk::ComboBoxText.new