Skip to content

Instantly share code, notes, and snippets.

@narumitukasa
Created November 30, 2014 11:23
Show Gist options
  • Save narumitukasa/7aa493043dc49d1ab405 to your computer and use it in GitHub Desktop.
Save narumitukasa/7aa493043dc49d1ab405 to your computer and use it in GitHub Desktop.
コンテナを後から追加可能なSplitContainerテスト
# coding: utf-8
require 'dxruby'
require_relative '../lib/dxrubyws'
require_relative '../lib/standardgui'
require_relative '../lib/fontcache'
module WS
class SeparatorTest < WSWindow
def initialize
super(100,100,400,300)
sch = WSSplitContainer.new(4,8,384, 260, :h)
sch.separator_size = 8
sch.space = 4
sch.add_client(WSPanel.new(0,0,24,24))
sch.add_client(WSPanel.new(0,0,24,24))
sch.add_client(WSPanel.new(0,0,24,24))
sch.add_client(WSPanel.new(0,0,24,24))
sch.init_layout
client.add_control(sch)
client.layout(:hbox) do
set_margin(8,8,8,8)
add sch, true, true
end
end
end
### セパレーター付きコンテナの定義 ###
class WSSplitContainer < WSContainer
### セパレータの定義 ###
class WSSeparator < WSControl
include Draggable
attr_accessor :client
def initialize(x, y, width, height, client, type=:h)
super(x, y, width, height)
@client = client
@type = type
init_handler
self.image = Image.new(width, height, COLOR[:shadow])
end
def init_handler
case @type
when :h # 水平セパレータ
add_handler(:drag_move) do |obj, x, y|
obj.x = obj.x + x
@client.resize(obj.x - @client.x, @client.height)
obj.signal(:slide)
end
when :v # 垂直セパレータ
add_handler(:drag_move) do |obj, x, y|
obj.y = obj.y + y
@client.resize(@client.width, obj.y - @client.y)
obj.signal(:slide)
end
end
end
def set_image
self.image.dispose if self.image
self.image = Image.new(width, height, COLOR[:shadow])
end
def resize(width, height)
super(width, height)
set_image
end
end
attr_reader :clients
attr_reader :separators
attr_accessor :space
def initialize(x, y, width, height, type=:h)
super(x, y, width, height)
@type = type
@clients = []
@separators = []
@separator_size = 8
@space = 0
end
# セパレーターサイズ
def separator_size=(v)
@separator_size = v
@separators.each do |separator|
separator.resize(@separator_size, @separator_size)
end
end
# クライアント追加
def add_client(obj, name=nil)
# セパレータの追加
if @clients.size > 0
separator = WSSeparator.new(0, 0, @separator_size, @separator_size, @clients.last, @type)
separator.add_handler(:slide){ @layout.auto_layout }
add_control(separator)
@separators << separator
end
# クライアントの追加
add_control(obj, name)
@clients << obj
end
# オートレイアウト
def init_layout
case @type
when :h # 水平レイアウト
layout(:hbox) do
s = obj.clients.size-1
self.space = obj.space
for i in 0..s
add obj.clients[i], (i == s), true
add obj.separators[i], false, true if s > 1 && i != s
end
end
when :v # 垂直レイアウト
layout(:vbox) do
s = obj.clients.size-1
self.space = obj.space
for i in 0..s
add obj.clients[i], true, (i == s)
add obj.separators[i], true, false if s > 1 && i != s
end
end
end
end
end
end
w = WS::SeparatorTest.new
WS.desktop.add_control w
Window.loop do
WS.update
break if Input.key_push?(K_ESCAPE)
Window.caption = Window.get_load.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment