Skip to content

Commit

Permalink
additions
Browse files Browse the repository at this point in the history
  • Loading branch information
TamasSzekeres committed Jul 31, 2018
1 parent fb22518 commit efdf635
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 130 deletions.
10 changes: 6 additions & 4 deletions examples/layout/src/layout.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ module Layout
# Creating The Main Window.
main_win = MainWindow.new
main_win.title = "Ltk-Win"
main_win.geometry = Rect.new 10, 10, 400, 300
main_win.margins = Margins.new 20, 20, 20, 20
main_win.geometry = Rect.new 0, 0, 400, 300
#main_win.margins = Margins.new 20, 20, 20, 20

# Creating a PushButton on the Main Window.
one_button = PushButton.new "One", main_win
one_button.object_name = "one_button"
one_button.geometry = Rect.new 20, 20, 150, 23
one_button.minimum_size = Size.new 200, 23
#one_button.minimum_size = Size.new 200, 23
#one_button.maximum_size = Size.new 200, 23
one_button.on_click = ->close_button_click

two_button = PushButton.new "Two", main_win
two_button.object_name = "two_button"
two_button.geometry = Rect.new 20, 60, 150, 23
one_button.minimum_size = Size.new 100, 23
#one_button.minimum_size = Size.new 100, 23
#one_button.maximum_size = Size.new 100, 23
two_button.on_click = ->close_button_click

layout = HorizontalLayout.new nil
Expand Down
18 changes: 18 additions & 0 deletions spec/layout/layout_math_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "../spec_helper"
require "../../src/ltk/base/size"
require "../../src/ltk/layout/box_layout_item_data"
require "../../src/ltk/layout/layout_math"

include Ltk

describe Ltk do
describe LayoutMath do
describe "#calculateWidths" do
it "properly calculates width for one item" do
item = BoxLayoutItemData.new(Size.new(100, 0), Size.new(50, 0), Size.new(150, 0), 0, Alignment::Center)
LayoutMath.calculateWidths([item], 100).should eq([100])
true.should eq(true)
end
end
end
end
4 changes: 2 additions & 2 deletions src/ltk/base/painter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ module Ltk

def fill_rectangle(x : Int32, y : Int32, w : Int32, h : Int32)
#@ctx.set_source_rgb 0.8_f64, 0.8_f64, 0.8_f64
# @ctx.rectangle x, y, w, h
# @ctx.fill
@ctx.rectangle x.to_f, y.to_f, w.to_f, h.to_f
@ctx.fill
end

def fill_round_rect(x : Int32, y : Int32, w : Int32, h : Int32, r : Int32)
Expand Down
6 changes: 3 additions & 3 deletions src/ltk/base/point.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ module Ltk
end

@[AlwaysInline]
def *(factor)
def *(factor : Int)
Point.new @x * factor, @y * factor
end

@[AlwaysInline]
def *(factor : Float)
def *(factor : Float32 | Float64)
Point.new (@x * factor).round.to_i, (@y * factor).round.to_i
end

@[AlwaysInline]
def /(c : Float)
def /(c : Float32 | Float64)
Point.new (@x / c).round.to_i, (@y / c).round.to_i
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/ltk/event/event_listener.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ require "../base/base_object"

module Ltk
abstract class EventListener < BaseObject
abstract def event(event : X11::Event) : Bool
def event(event : X11::Event) : Bool
false
end

getter window : X11::C::Window = 0_u64
end
Expand Down
5 changes: 1 addition & 4 deletions src/ltk/layout/alignment.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ module Ltk
Left = 0x0001_u32
Right = 0x0002_u32
HCenter = 0x0004_u32
Justify = 0x0008_u32

# Vertical flags
Top = 0x0020_u32
Bottom = 0x0040_u32
VCenter = 0x0080_u32

Center = VCenter | HCenter

Absolute = 0x0010_u32
Leading = Left
Trailing = Right

HMask = Left | Right | HCenter | Justify | Absolute
HMask = Left | Right | HCenter
VMask = Top | Bottom | VCenter
end
end
15 changes: 15 additions & 0 deletions src/ltk/layout/box_layout_item.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "./layout_item"

module Ltk
class BoxLayoutItem < LayoutItem
DEFAULT_STRETCH = 0
MINIMUM_STRETCH = 0

property stretch : Int32 = DEFAULT_STRETCH

def initialize(item : (Widget | Layout), @stretch : Int32 = DEFAULT_STETCH, alignment : Alignment = Alignment::None)
raise ArgumentError.new("stretch cannot be less than #{MINIMUM_STRETCH}") if @stretch < MINIMUM_STRETCH
super(item, alignment)
end
end
end
58 changes: 58 additions & 0 deletions src/ltk/layout/box_layout_item_data.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "../base/rect"
require "./alignment"
require "./box_layout_item"

module Ltk
struct BoxLayoutItemData
getter preferred_size : Size
getter minimum_size : Size
getter maximum_size : Size
getter stretch : Int32
getter alignment : Alignment

def initialize(@preferred_size,
@minimum_size = Size::ZERO,
@maximum_size = Size::MAX,
@stretch = 0,
@alignment : Alignment = Alignment::Center)
end

def initialize(item : BoxLayoutItem)
@preferred_size = item.preferred_size
@minimum_size = item.minimum_size
@maximum_size = item.maximum_size
@stretch = item.stretch
@alignment = item.alignment
end

@[AlwaysInline]
def preferred_width : Int32
@preferred_size.width
end

@[AlwaysInline]
def preferred_height : Int32
@preferred_size.height
end

@[AlwaysInline]
def minimum_width : Int32
@minimum_size.width
end

@[AlwaysInline]
def minimum_height : Int32
@minimum_size.height
end

@[AlwaysInline]
def maximum_width : Int32
@maximum_size.width
end

@[AlwaysInline]
def maximum_height : Int32
@maximum_size.height
end
end
end
127 changes: 22 additions & 105 deletions src/ltk/layout/horizontal_layout.cr
Original file line number Diff line number Diff line change
@@ -1,100 +1,10 @@
require "./box_layout_item"

module Ltk
class HorizontalLayout < Layout
DEFAULT_STETCH = 1

struct Item
getter item : (Widget | Layout)
property stretch : Int32 = 1

def initialize(@item : (Widget | Layout), @stretch : Int32 = DEFAULT_STETCH)
end

@[AlwaysInline]
def x : Int32
geometry.x
end

@[AlwaysInline]
def y : Int32
geometry.y
end

@[AlwaysInline]
def width : Int32
geometry.width
end

@[AlwaysInline]
def height : Int32
geometry.height
end

def geometry : Rect
case @item
when Widget then @item.as(Widget).geometry
when Layout then Rect.new
else
Rect.new
end
end

def geometry=(r : Rect)
case @item
when Widget
@item.as(Widget).geometry = r
end
end

def preferred_width : Int32
case @item
when Widget then @item.as(Widget).preferred_width
else
0
end
end

def preferred_height : Int32
case @item
when Widget then @item.as(Widget).preferred_width
else
0
end
end

def minimum_width : Int32
case @item
when Widget then @item.as(Widget).minimum_width
else
0
end
end

def minimum_height : Int32
case @item
when Widget then @item.as(Widget).minimum_height
else
0
end
end
DEFAULT_STETCH = 0

def maximum_width : Int32
case @item
when Widget then @item.as(Widget).maximum_width
else
0
end
end

def maximum_height : Int32
case @item
when Widget then @item.as(Widget).maximum_height
else
0
end
end
end

alias Items = Array(Item)
alias Items = Array(BoxLayoutItem)

private getter items : Items = Items.new

Expand All @@ -104,13 +14,13 @@ module Ltk
end
end

def add_item(item : (Widget | Layout), stretch : Int32 = DEFAULT_STETCH)
def add_item(item : (Widget | Layout), stretch : Int32 = DEFAULT_STETCH, alignment : Alignment = Alignment::None)
raise ArgumentError.new("Stretch cannot be less then 1!") if stretch < 1
@items << Item.new(item, stretch)
@items << BoxLayoutItem.new(item, stretch, alignment)
end

def add_widget(widget : Widget, stretch : Int32 = DEFAULT_STETCH)
add_item widget, stretch
def add_widget(widget : Widget, stretch : Int32 = DEFAULT_STETCH, alignment : Alignment = Alignment::None)
add_item widget, stretch, alignment
end

protected def arrange_items
Expand All @@ -121,32 +31,39 @@ module Ltk
sum_preferred_width = 0_i64
sum_min_width = 0_i64
sum_max_width = 0_i64
puts "-------------------------------------------------"
@items.each_with_index do |item, index|
sum_stretch += item.stretch
preferred_width = item.preferred_width
minimum_width = item.minimum_width
maximum_width = item.maximum_width
puts "item.preferred_width = #{preferred_width}"
puts "item.minimum_width = #{minimum_width}"
puts "item.maximum_width = #{maximum_width}"
puts "item[#{index}].preferred_width = #{preferred_width}"
#puts "item.minimum_width = #{minimum_width}"
#puts "item.maximum_width = #{maximum_width}"
sum_preferred_width += preferred_width if preferred_width > 0
sum_min_width += minimum_width if minimum_width > 0
sum_max_width += maximum_width if maximum_width > 0

item_widths[index] = preferred_width
end
puts "-------------------------------------------------"
puts "layout-width = #{@geometry.width}"
puts "sum-stretch = #{sum_stretch}"
puts "sum-preferred-width = #{sum_preferred_width}"
puts "sum-min-width = #{sum_min_width}"
puts "sum-max-width = #{sum_max_width}"
#puts "sum-min-width = #{sum_min_width}"
#puts "sum-max-width = #{sum_max_width}"
p item_widths

if sum_preferred_width < @geometry.width
remaining_width = @geometry.width - sum_preferred_width
puts "remaining-width = #{remaining_width}"
@items.each_with_index do |item, index|
item_widths[index] += remaining_width * item.stretch / sum_stretch
w_stretch = @geometry.width * item.stretch / sum_stretch
if item_widths[index] < w_stretch && item.stretch > 0
item_widths[index] = Math.max(w_stretch, item_widths[index])
end
end
#else if sum_preferred_width > @geometry.width

end
p item_widths
x = @geometry.x
Expand Down
Loading

0 comments on commit efdf635

Please sign in to comment.