File: cairo-operator.rb

package info (click to toggle)
ruby-gnome2 0.15.0-1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 7,692 kB
  • ctags: 8,558
  • sloc: ansic: 69,912; ruby: 19,511; makefile: 97; xml: 35; sql: 13
file content (83 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright (c) 2005 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
# $Id: cairo-operator.rb,v 1.1 2005/10/12 05:38:30 ktou Exp $
=begin
= cairo/Operator

This demo shows how to use GDK and cairo to use Cairo::OPERATOR_*
From http://cairographics.org/samples/operator_add.html,
     http://cairographics.org/samples/operator_atop.html,
     http://cairographics.org/samples/operator_atop_reverse.html,
     http://cairographics.org/samples/operator_in.html,
     http://cairographics.org/samples/operator_in_reverse.html,
     http://cairographics.org/samples/operator_out.html,
     http://cairographics.org/samples/operator_out_reverse.html,
     http://cairographics.org/samples/operator_over.html,
     http://cairographics.org/samples/operator_over_reverse.html,
     http://cairographics.org/samples/operator_saturate.html and
     http://cairographics.org/samples/operator_xor.html
=end
require 'common'

module Demo
  class CairoOperator < CairoWindow
    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

    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

    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)
        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
  end
end