Skip to content

Instantly share code, notes, and snippets.

@ashbb
Created June 12, 2012 11:54
Show Gist options
  • Save ashbb/2917098 to your computer and use it in GitHub Desktop.
Save ashbb/2917098 to your computer and use it in GitHub Desktop.
Trying to create original StackLayout class and FlowLayout class for Shoes 4 inherited from Swt::Widgets::Layout class.
require 'java'
require 'swt'
module Swt::Widgets
import org.eclipse.swt.widgets.Layout
end
display = Swt::Widgets::Display.new
shell = Swt::Widgets::Shell.new display, Swt::SWT::SHELL_TRIM
shell.setLayout Swt::Layout::RowLayout.new
shell.set_background display.getSystemColor Swt::SWT::COLOR_WHITE
shell.background_mode = Swt::SWT::INHERIT_DEFAULT
shell.addListener Swt::SWT::Close, proc{Swt.display.dispose}
class ShoesLayout < Swt::Widgets::Layout
def initialize *args
opts = args.last.class == Hash ? args.pop : {}
@width, @height = opts[:width], opts[:height]
super *args
end
end
class StackLayout < ShoesLayout
def computeSize *args
eles = args[0].getChildren
@width ||= eles.map{|e| e.is_a?(Swt::Custom::StyledText) ? 0 : get_size(e).x}.max
@height ||= eles.map{|e| get_size(e).y}.inject(:+)
Swt::Graphics::Point.new @width, @height
end
def layout *args
eles = args[0].getChildren
x, y = 0, 0
eles.each do |e|
size = get_size e
e.setBounds x, y, size.x, size.y
y += size.y
end
end
def get_size e
w = e.is_a?(Swt::Custom::StyledText) ? @width : Swt::SWT::DEFAULT
e.computeSize w, Swt::SWT::DEFAULT
end
end
class FlowLayout < ShoesLayout
def computeSize *args
eles = args[0].getChildren
@width ||= eles.map{|e| 0}.max # not yet
@height ||= eles.map{|e| 0}.inject(:+) # not yet
Swt::Graphics::Point.new @width, @height
end
def layout *args
# not yet
end
end
def button cs, name
b = Swt::Widgets::Button.new cs, Swt::SWT::PUSH
b.setText name
end
def para cs, str
st = Swt::Custom::StyledText.new cs, Swt::SWT::WRAP
st.setText str
end
def stack parent, opts={}, &blk
cs = Swt::Widgets::Composite.new parent, Swt::SWT::NULL
cs.setLayout StackLayout.new opts
blk[cs]
end
def flow parent, opts={}, &blk
cs = Swt::Widgets::Composite.new parent, Swt::SWT::NULL
cs.setLayout FlowLayout.new opts
blk[cs]
end
stack shell do |cs|
2.times{|i| button cs, "Button #{i}"}
stack cs do |cs1|
#flow cs, width: 300, height: 100 do |cs1|
button cs1, 'long ' * 5
para cs1, 'hello ' * 28
button cs1, 'short'
end
2.times{|i| button cs, "Button #{i}"}
end
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