Class: Zyps::GTK2Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/zyps/views/canvas/gtk2.rb

Overview

Called by View objects for use in Ruby-GNOME2 applications. Assign an instance to a View, then add the drawing_area attribute to a GUI container object. The drawing area will be updated whenever the View is.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGTK2Canvas

Returns a new instance of GTK2Canvas.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zyps/views/canvas/gtk2.rb', line 37

def initialize

	#Will be resized later.
	@width = 1
	@height = 1

	#Create a drawing area.
	@drawing_area = Gtk::DrawingArea.new
	#Set to correct size.
	resize
	
	#Whenever the drawing area needs updating...
	@drawing_area.signal_connect("expose_event") do
		#Copy buffer bitmap to canvas.
		@drawing_area.window.draw_drawable(
			@drawing_area.style.fg_gc(@drawing_area.state), #Gdk::GC (graphics context) to use when drawing.
			buffer, #Gdk::Drawable source to copy onto canvas.
			0, 0, #Pull from upper left of source.
			0, 0, #Copy to upper left of canvas.
			-1, -1 #-1 width and height signals to copy entire source over.
		)
	end
	
end

Instance Attribute Details

#drawing_areaObject (readonly)

A Gtk::DrawingArea that will be painted on.



32
33
34
# File 'lib/zyps/views/canvas/gtk2.rb', line 32

def drawing_area
  @drawing_area
end

#heightObject

Dimensions of the drawing area. Control should normally be left to the owner View object.



35
36
37
# File 'lib/zyps/views/canvas/gtk2.rb', line 35

def height
  @height
end

#widthObject

Dimensions of the drawing area. Control should normally be left to the owner View object.



35
36
37
# File 'lib/zyps/views/canvas/gtk2.rb', line 35

def width
  @width
end

Instance Method Details

#draw_line(options = {}) ⇒ Object

Takes a hash with these keys and defaults: :color => nil :width => nil :x1 => nil :y1 => nil :x2 => nil :y2 => nil



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zyps/views/canvas/gtk2.rb', line 103

def draw_line(options = {})
	graphics_context = Gdk::GC.new(buffer)
	graphics_context.rgb_fg_color = convert_color(options[:color])
	graphics_context.set_line_attributes(
		options[:width].ceil,
		Gdk::GC::LINE_SOLID,
		Gdk::GC::CAP_ROUND,
		Gdk::GC::JOIN_MITER #Only used for polygons.
	)
	buffer.draw_line(
		graphics_context,
		options[:x1], options[:y1],
		options[:x2], options[:y2]
	)
end

#draw_rectangle(options = {}) ⇒ Object

Takes a hash with these keys and defaults: :color => nil :border_width => 1 :filled => true :x => nil :y => nil :width => nil :height => nil



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zyps/views/canvas/gtk2.rb', line 81

def draw_rectangle(options = {})
	options = {
		:filled => true
	}.merge(options)
	graphics_context = Gdk::GC.new(buffer)
	graphics_context.rgb_fg_color = convert_color(options[:color])
	buffer.draw_rectangle(
		graphics_context,
		options[:filled],
		options[:x], options[:y],
		options[:width], options[:height]
	)
end

#renderObject

Draw all objects to the drawing area.



121
122
123
# File 'lib/zyps/views/canvas/gtk2.rb', line 121

def render
	@drawing_area.queue_draw_area(0, 0, @width, @height)
end