Instantly share code, notes, and snippets.
Created
June 29, 2012 14:19
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save ashbb/3018227 to your computer and use it in GitHub Desktop.
Have a trial of ShoesComposite and ShoesLayout for Shoes 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'swt' | |
module Swt | |
include_package 'org.eclipse.swt.graphics' | |
include_package 'org.eclipse.swt.events' | |
module Widgets | |
import org.eclipse.swt.widgets.Layout | |
end | |
end | |
class ShoesComposite < Swt::Widgets::Composite | |
def initialize *cs | |
@cs = cs[0] | |
super @cs, Swt::SWT::DEFAULT | |
end | |
def setSize(w,h) | |
@width, @height = w, h | |
end | |
def setLocation(x,y) | |
@x, @y = x, y | |
end | |
def add_paint_listener blk | |
# Wrap the block so the paint callback happens at the right (x,y) | |
wrapped_blk = proc do |e| | |
gc = e.gc | |
transform = ::Swt::Transform.new(::Swt.display) | |
transform.translate @x, @y | |
gc.transform = transform | |
blk.call(e) | |
transform.dispose | |
end | |
@cs.add_paint_listener wrapped_blk | |
end | |
end | |
class ShoesStackLayout < Swt::Widgets::Layout | |
def computeSize *args | |
Swt::Graphics::Point.new 200, 200-1 | |
end | |
def layout *args | |
eles = args[0].getChildren | |
x, y = 0, 0 | |
eles.each do |e| | |
size = e.computeSize Swt::SWT::DEFAULT, Swt::SWT::DEFAULT | |
e.setBounds x, y, size.x, size.y | |
y += size.y | |
end | |
end | |
end | |
$display = display = Swt::Widgets::Display.new | |
white = display.getSystemColor(Swt::SWT::COLOR_WHITE) | |
red = display.getSystemColor(Swt::SWT::COLOR_RED) | |
green = display.getSystemColor(Swt::SWT::COLOR_GREEN) | |
shell = Swt::Widgets::Shell.new display, Swt::SWT::SHELL_TRIM | |
shell.setLayout ShoesStackLayout.new | |
shell.addListener Swt::SWT::Close, proc{Swt.display.dispose} | |
# Implicit top slot | |
cs0 = Swt::Widgets::Composite.new shell, Swt::SWT::NULL | |
cs0.setBackground white | |
cs0.setSize 200, 200 | |
cs0.setLayout ShoesStackLayout.new | |
# Slot 1 (green circle) | |
cs1 = ShoesComposite.new cs0 | |
cs1.setLayout ShoesStackLayout.new | |
blk = proc do |e| | |
gc = e.gc | |
gc.setBackground green | |
gc.fillOval 25, 25, 100, 100 | |
end | |
cs1.add_paint_listener(blk) | |
# Slot 2 (red circle) | |
cs2 = ShoesComposite.new cs0 | |
cs2.setLayout ShoesStackLayout.new | |
cs2.setLocation 50, 50 | |
blk = proc do |e| | |
gc = e.gc | |
gc.setBackground red | |
gc.fillOval 25, 25, 100, 100 | |
end | |
cs2.add_paint_listener(blk) | |
# Three buttons on Slot 1 | |
3.times do |i| | |
b = Swt::Widgets::Button.new cs1, Swt::SWT::PUSH | |
b.setText "button #{i}" | |
end | |
# Button | |
b = Swt::Widgets::Button.new cs0, Swt::SWT::PUSH | |
b.setText 'button' | |
b.pack | |
# Animation | |
class Anim | |
def initialize &blk | |
@blk = blk | |
end | |
def run | |
@blk.call | |
$display.timerExec 100, self | |
end | |
end | |
i = 0 | |
a = Anim.new do | |
i+=1 | |
b.setLocation i, i | |
shell.redraw | |
end | |
display.timerExec 100, a | |
shell.pack | |
shell.open | |
Swt.event_loop{Swt.display.isDisposed} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'swt' | |
module Swt | |
include_package 'org.eclipse.swt.graphics' | |
include_package 'org.eclipse.swt.events' | |
module Widgets | |
import org.eclipse.swt.widgets.Layout | |
end | |
end | |
class ShoesComposite < Swt::Widgets::Composite | |
def initialize *cs | |
@cs = cs[0] | |
super @cs, Swt::SWT::DEFAULT | |
end | |
def setSize(w,h) | |
@width, @height = w, h | |
end | |
def setLocation(x,y) | |
@x, @y = x, y | |
end | |
def add_paint_listener blk | |
# Wrap the block so the paint callback happens at the right (x,y) | |
wrapped_blk = proc do |e| | |
gc = e.gc | |
transform = ::Swt::Transform.new(::Swt.display) | |
transform.translate @x, @y | |
gc.transform = transform | |
blk.call(e) | |
transform.dispose | |
end | |
@cs.add_paint_listener wrapped_blk | |
end | |
end | |
class ShoesStackLayout < Swt::Widgets::Layout | |
def computeSize *args | |
Swt::Graphics::Point.new 200, 200-1 | |
end | |
def layout *args | |
eles = args[0].getChildren | |
x, y = 0, 0 | |
eles.each do |e| | |
size = e.computeSize Swt::SWT::DEFAULT, Swt::SWT::DEFAULT | |
e.setBounds x, y, size.x, size.y | |
y += size.y | |
end | |
end | |
end | |
$display = display = Swt::Widgets::Display.new | |
white = display.getSystemColor(Swt::SWT::COLOR_WHITE) | |
red = display.getSystemColor(Swt::SWT::COLOR_RED) | |
green = display.getSystemColor(Swt::SWT::COLOR_GREEN) | |
shell = Swt::Widgets::Shell.new display, Swt::SWT::SHELL_TRIM | |
shell.setLayout ShoesStackLayout.new | |
shell.addListener Swt::SWT::Close, proc{Swt.display.dispose} | |
# Implicit top slot | |
cs0 = Swt::Widgets::Composite.new shell, Swt::SWT::NULL | |
cs0.setBackground white | |
cs0.setSize 200, 200 | |
cs0.setLayout ShoesStackLayout.new | |
# Slot 1 (green circle) | |
cs1 = ShoesComposite.new cs0 | |
cs1.setLayout ShoesStackLayout.new | |
blk = proc do |e| | |
gc = e.gc | |
gc.setBackground green | |
gc.fillOval 25, 25, 100, 100 | |
end | |
cs1.add_paint_listener(blk) | |
# Slot 2 (red circle) | |
cs2 = ShoesComposite.new cs0 | |
cs2.setLayout ShoesStackLayout.new | |
cs2.setLocation 50, 50 | |
blk = proc do |e| | |
gc = e.gc | |
gc.setBackground red | |
gc.fillOval 25, 25, 100, 100 | |
end | |
cs2.add_paint_listener(blk) | |
# Three buttons on Slot 2 | |
3.times do |i| | |
b = Swt::Widgets::Button.new cs2, Swt::SWT::PUSH | |
b.setText "button #{i}" | |
end | |
# Button | |
b = Swt::Widgets::Button.new cs0, Swt::SWT::PUSH | |
b.setText 'button' | |
b.pack | |
# Animation | |
class Anim | |
def initialize &blk | |
@blk = blk | |
end | |
def run | |
@blk.call | |
$display.timerExec 100, self | |
end | |
end | |
i = 0 | |
a = Anim.new do | |
i+=1 | |
b.setLocation i, i | |
shell.redraw | |
end | |
display.timerExec 100, a | |
shell.pack | |
shell.open | |
Swt.event_loop{Swt.display.isDisposed} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above two files are almost the same. This is a diff.