class Demo::CairoOperator

Public Class Methods

new() click to toggle source
Calls superclass method
# File gtk2/sample/gtk-demo/cairo-operator.rb, line 25
def initialize
  super('cairo operator')
  @operator = Cairo::OPERATOR_ADD

  set_default_size(400, 400)
  
  @drawing_area = child
  remove(@drawing_area)
  
  vbox = Gtk::VBox.new
  vbox.pack_start(@drawing_area, true, true)
  vbox.pack_start(operator_selector, false, false)
  add(vbox)
end

Public Instance Methods

draw(cr) click to toggle source
# File gtk2/sample/gtk-demo/cairo-operator.rb, line 40
def draw(cr)
  cr.save do
    image = Cairo::ImageSurface.from_png("ruby-gnome2-logo.png")
    cr.translate(0.5, 0.5)
    cr.rotate(-45 * Math::PI / 180)
    cr.scale(0.8 / image.width, 0.8 / image.height)
    cr.translate(-0.5 * image.width, -0.5 * image.height)
    cr.set_source(image, 0.0, 0.0)
    cr.paint
  end
  
  cr.set_operator(@operator)
  
  cr.set_source_rgba(1, 0, 0, 0.5)
  cr.rectangle(0.2, 0.2, 0.5, 0.5)
  cr.fill
  cr.set_source_rgba(0, 1, 0)
  cr.rectangle(0.4, 0.4, 0.4, 0.4)
  cr.fill
  cr.set_source_rgba(0, 0, 1)
  cr.rectangle(0.6, 0.6, 0.3, 0.3)
  cr.fill
end
operator_selector() click to toggle source
# File gtk2/sample/gtk-demo/cairo-operator.rb, line 64
def operator_selector
  combo = Gtk::ComboBox.new
  operators = []
  Cairo.constants.each do |name|
    operators << name if /^OPERATOR_/ =~ name
  end
  operators.sort.each_with_index do |name, i|
    combo.append_text(name.to_s)
    combo.set_active(i) if Cairo.const_get(name) == @operator
  end

  combo.signal_connect("changed") do |widget|
    text = widget.active_text
    @operator = Cairo.const_get(text) if text
    @drawing_area.queue_draw
  end
  combo
end