File: common.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 (88 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download | duplicates (5)
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
84
85
86
87
88
=begin
  common.rb - Common class for gtk-demo.

  Copyright (c) 2003-2005 Ruby-GNOME2 Project Team
  This program is licenced under the same licence as Ruby-GNOME2.

  $Id: common.rb,v 1.9 2005/10/15 03:41:36 mutoh Exp $
=end

require 'gtk2'

module Demo
  def self.find_file(basename)
    %w(. /usr/share/gtk-2.0/demo /usr/local/share/gtk-2.0/demo/).each do |dirname|
      path = File.join(dirname, basename)
      if File.exist?(path)
	return path
      end
    end

    raise "#{basename}: No such file or directory"
  end

  class BasicWindow < Gtk::Window
    def initialize(title = nil)
      super(Gtk::Window::TOPLEVEL)
      if title
	set_title("#{title} in Ruby/GTK")
      end

      signal_connect("key_press_event") do |widget, event|
        if event.state.control_mask? and event.keyval == Gdk::Keyval::GDK_q
          destroy
          true
        else
          false
        end
      end

      signal_connect("delete_event") do |widget, event|
	quit
      end
    end

    def quit
      destroy
      true
    end
  end

  class CairoWindow < BasicWindow

    def initialize(title=nil)
      super
      
      unless Gdk.cairo_available?
        add_cairo_require_label
        return
      end

      drawing_area = Gtk::DrawingArea.new
      add(drawing_area)

      drawing_area.signal_connect("expose_event") do |widget, event|
        cr = widget.window.create_cairo_context
        cr.scale(*widget.window.size)
        cr.set_line_width(0.04)

        cr.save do
          cr.set_source_color(Gdk::Color.new(65535, 65535, 65535))
          cr.gdk_rectangle(Gdk::Rectangle.new(0, 0, 1, 1))
          cr.fill
        end
        
        draw(cr)
      end
    end
    
    def add_cairo_require_label
      message = "This sample requires GTK+ 2.8.0 or later and cairo support"
      add(Gtk::Label.new(message))
    end

    def draw(cr)
      raise "not implemented"
    end
  end
end