Sha256: 43673d3247cace3e4d334aca376bb054ae22f8db693388b1255abcd964d187d3

Contents?: true

Size: 1.92 KB

Versions: 24

Compression:

Stored size: 1.92 KB

Contents

=begin
  drawing.rb - Gtk::Drawing sample script.

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

  $Id: drawing.rb,v 1.7 2006/06/17 13:18:12 mutoh Exp $
=end

require 'gtk3'

class Canvas < Gtk::DrawingArea
  def initialize
    super
    signal_connect("expose_event") { |w,e| expose_event(w,e) }
    signal_connect("configure_event") { |w, e| configure_event(w,e) }
    @buffer = nil
    @bgc = nil
  end

  def expose_event(w,e)
    unless @buffer.nil?
      rec = e.area
      w.window.draw_drawable(@bgc, @buffer, rec.x, rec.y,
			   rec.x, rec.y, rec.width, rec.height)
    end
    false
  end

  def clear(b = @buffer)
    return if b.nil?

    g = b.size
    @bgc = self.style.bg_gc(self.state) if @bgc.nil?
    if (g[0] > 0 && g[1] > 0)
      b.draw_rectangle(@bgc, true, 0,0, g[0], g[1])
    end
  end

  def configure_event(w,e)
    g = w.window.geometry
    if (g[2] > 0 && g[3] > 0)
      b = Gdk::Pixmap::new(w.window, g[2], g[3], -1)
      clear(b)
      if not @buffer.nil?
	g = @buffer.size
	b.draw_drawable(@bgc, @buffer, 0, 0, 0, 0, g[0], g[1])
      end
      @buffer = b
    end
    true
  end
end

class A < Canvas
  def initialize
    super
    signal_connect("button_press_event") { |w,e| pressed(w,e) }
    set_events(Gdk::Event::BUTTON_PRESS_MASK)
  end

  def pressed(widget, ev)
    if not @last.nil?
      @buffer.draw_line(widget.style.fg_gc(widget.state),
			@last.x, @last.y, ev.x, ev.y)

      x1,x2 = if (@last.x < ev.x)
	      then [@last.x, ev.x]
	      else [ev.x,    @last.x]
	      end
      y1,y2 = if (@last.y < ev.y)
	    then [@last.y, ev.y]
	    else [ev.y,    @last.y]
	    end
      widget.queue_draw_area(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
    end
    @last = nil
    @last = ev
    true
  end
end

window = Gtk::Window.new("drawing test")
window.signal_connect("destroy") { Gtk.main_quit }

canvas = A.new
window.add(canvas)

window.show_all
Gtk::main

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
gtk3-2.1.0-x86-mingw32 sample/misc/drawing.rb
gtk3-2.1.0 sample/misc/drawing.rb
gtk3-2.0.3-x86-mingw32 sample/misc/drawing.rb
gtk3-2.0.3 sample/misc/drawing.rb
gtk3-2.0.2-x86-mingw32 sample/misc/drawing.rb
gtk3-2.0.2 sample/misc/drawing.rb
gtk3-2.0.1-x86-mingw32 sample/misc/drawing.rb
gtk3-2.0.1 sample/misc/drawing.rb
gtk3-2.0.0-x86-mingw32 sample/misc/drawing.rb
gtk3-2.0.0 sample/misc/drawing.rb
gtk3-1.2.6-x86-mingw32 sample/misc/drawing.rb
gtk3-1.2.6 sample/misc/drawing.rb
gtk3-1.2.5-x86-mingw32 sample/misc/drawing.rb
gtk3-1.2.5 sample/misc/drawing.rb
gtk3-1.2.4-x86-mingw32 sample/misc/drawing.rb
gtk3-1.2.4 sample/misc/drawing.rb
gtk3-1.2.3-x86-mingw32 sample/misc/drawing.rb
gtk3-1.2.3 sample/misc/drawing.rb
gtk3-1.2.2-x86-mingw32 sample/misc/drawing.rb
gtk3-1.2.2 sample/misc/drawing.rb