# Commands covered:  treectrl's widget command style
#
# This file contains a collection of tests for the style widget command of
# the tktreectrl extension.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2000 by Scriptics Corporation.
# Copyright (c) 2002 by Christian Krone.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2
    namespace import ::tcltest::*
}

package require Tk
package require treectrl

test style-0.1 {some needed preparations} -body {
    pack [treectrl .t]
} -result {}

test style-0.2 {some other preparations} -body {
    .t element create eBorder border
    .t element create eText text
    .t element create eImage image
    .t element create eRect rect
    list
} -result {}

test style-1.1 {style: missing args} -body {
    .t style
} -returnCodes error -result {wrong # args: should be ".t style command ?arg arg ...?"}

test style-1.2 {style: invalid command} -body {
    .t style foo
} -returnCodes error -result {bad command "foo": must be *} -match glob

test style-1.3 {style names: no style exists yet} -body {
    .t style names
} -result {}

test style-2.1 {style create: missing args} -body {
    .t style create
} -returnCodes error -result {wrong # args: should be ".t style create name ?option value ...?"}

test style-2.2 {style create: invalid option} -body {
    .t style create testStyle -foo bar
} -returnCodes error -result {unknown option "-foo"}

test style-2.3 {style create} -body {
    .t style create testStyle
} -result {testStyle}

test style-2.4 {style create: already existing style} -body {
    .t style create testStyle
} -returnCodes error -result {style "testStyle" already exists}

test style-3.1 {style configure: invalid option} -body {
    .t style configure testStyle -foo bar
} -returnCodes error -result {unknown option "-foo"}

test style-3.2 {style configure: all options} -body {
    .t style configure testStyle
} -result {{-buttony {} {} {} {}} {-orient {} {} horizontal horizontal}}

test style-3.3 {style configure: the only option} -body {
    .t style configure testStyle -orient
} -result {-orient {} {} horizontal horizontal}

test style-3.4 {style configure: invalid option -orient} -body {
    .t style configure testStyle -orient diagonal
} -returnCodes error -result {bad orient "diagonal": must be horizontal or vertical}

test style-3.5 {style configure/cget: option -orient} -body {
    .t style configure testStyle -orient vertical
    .t style cget testStyle -orient
} -result {vertical}

test style-4.1 {style delete: unknown style} -body {
    .t style delete testStyle2
} -returnCodes error -result {style "testStyle2" doesn't exist}

test style-4.2 {style delete: unknown style} -body {
    .t style names
} -result {testStyle}

test style-4.3 {style delete} -body {
    .t style delete testStyle
} -result {}

test style-4.4 {style names: no style defined} -body {
    .t style names
} -result {}

test style-5.1 {style elements: missing args} -body {
    .t style elements
} -returnCodes error -result {wrong # args: should be ".t style elements name ?elementList?"}

test style-5.2 {style elements: unknown style} -body {
    .t style elements testStyle
} -returnCodes error -result {style "testStyle" doesn't exist}

test style-5.3 {style elements: no element yet} -body {
    .t style create testStyle
    .t style elements testStyle
} -result {}

test style-5.4 {style elements: empty element list} -body {
    .t style elements testStyle {}
} -result {}

test style-5.5 {style elements: add some elements} -setup {
    # Create some items using the style.
    # FIXME: add test to ensure the item-column styles are updated.
    .t column create
    .t item create -count 100 -parent root
    .t item style set all 0 testStyle
} -body {
    .t style elements testStyle {eBorder eImage eText}
    .t style elements testStyle
} -result {eBorder eImage eText}

test style-5.6 {style elements: duplicate elements are ignored} -body {
    .t style elements testStyle {eBorder eImage eImage eText}
    .t style elements testStyle
} -result {eBorder eImage eText}

test style-5.7 {style elements: duplicate elements are ignored} -body {
    .t style elements testStyle {eBorder eImage eText eImage}
    .t style elements testStyle
} -result {eBorder eImage eText}

test style-5.8 {style elements: rearrange elements} -body {
    .t style elements testStyle {eText eImage eBorder}
    .t style elements testStyle
} -result {eText eImage eBorder}

test style-5.9 {style elements: remove elements} -body {
    .t style elements testStyle {eImage}
    .t style elements testStyle
} -result {eImage}

test style-5.10 {style elements: add elements} -body {
    .t style elements testStyle {eBorder eText eImage}
    .t style elements testStyle
} -result {eBorder eText eImage}

test style-6.1 {style layout: missing args} -body {
    .t style layout
} -returnCodes error -result {wrong # args: should be ".t style layout name element ?option? ?value? ?option value ...?"}

test style-6.2 {style layout: no options specified} -body {
    .t style layout testStyle eText
} -result {-center {} -detach no -draw {} *} -match glob

test style-6.3 {style layout: option -padx} -body {
    .t style layout testStyle eText -padx 3
    .t style layout testStyle eText -padx
} -result {3}

test style-6.4 {style layout: invalid 2 element -pady} -body {
    .t style layout testStyle eText -pady {3 ""}
} -returnCodes error -result {bad pad amount "3 """: must be a list of 1 or 2 positive screen distances}

test style-6.5 {style layout: invalid 2 element -pady} -body {
    .t style layout testStyle eText -pady "\{"
} -returnCodes error -result {unmatched open brace in list}

test style-6.6 {style layout: invalid 2 element -pady} -body {
    .t style layout testStyle eText -pady {3 -7}
} -returnCodes error -result {bad pad amount "3 -7": must be a list of 1 or 2 positive screen distances}

test style-6.7 {style layout: invalid 2 element -pady} -body {
    .t style layout testStyle eText -pady {3 7}
    .t style layout testStyle eText -pady
} -result {3 7}

test style-6.8 {style layout: option -expand} -body {
    .t style layout testStyle eText -expand "hello world"
} -returnCodes error -result {bad expand value "hello world": must be a string containing zero or more of n, e, s, and w}

test style-6.9 {style layout: option -expand} -body {
    .t style layout testStyle eText -expand ew
    .t style layout testStyle eText -expand
} -result {we}

test style-6.10 {style layout: option -squeeze} -body {
    .t style layout testStyle eText -squeeze xyzzy
} -returnCodes error -result {bad squeeze value "xyzzy": must be a string containing zero or more of x and y}

test style-6.11 {style layout: option -squeeze} -body {
    .t style layout testStyle eText -squeeze xy
    .t style layout testStyle eText -squeeze
} -result {xy}

test style-6.12 {style layout: option -union invalid list} -body {
    .t style layout testStyle eText -union "\{"
} -returnCodes error -result {unmatched open brace in list}

test style-6.13 {style layout: option -union unknown elements} -body {
    .t style layout testStyle eText -union {foo bar}
} -returnCodes error -result {element "foo" doesn't exist}

test style-6.14 {style layout: option -union element not in style} -body {
    .t style layout testStyle eText -union {eBorder eRect}
} -returnCodes error -result {style testStyle does not use element eRect}

test style-6.15 {style layout: option -union with itself} -body {
    .t style layout testStyle eText -union {eBorder eText}
} -returnCodes error -result {element eText can't form union with itself}

test style-6.16 {style layout: option -union} -body {
    .t style layout testStyle eText -union {eBorder eImage}
    .t style layout testStyle eText -union
} -result {eBorder eImage}

test style-6.17 {style layout: option invalid -detach} -body {
    .t style layout testStyle eText -detach {x y}
} -returnCodes error -match glob -result {expected boolean value but got *}

test style-6.18 {style layout: option -detach} -body {
    .t style layout testStyle eText -detach true
    .t style layout testStyle eText -detach
} -result {yes}

test style-7.1 {layout validation} -setup {
    destroy .t
    pack [treectrl .t]
    .t configure -showheader no -borderwidth 0 -highlightthickness 0
    .t element create e1 rect -width 20 -height 25 -fill blue
    .t element create e2 rect -width 40 -height 45 -fill green
    .t style create s1 -orient horizontal
    .t style elements s1 {e1 e2}
    .t column create -tags C0
    .t item style set root C0 s1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-7.2 {layout validation} -setup {
    .t colu conf C0 -width 1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-7.3 {layout validation} -setup {
    .t style layout s1 e1 -squeeze x
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 0 25} {0 0 40 45}}

test style-7.4 {layout validation} -setup {
    .t style layout s1 e1 -minwidth 10
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 10 25} {10 0 50 45}}

test style-7.5 {layout validation} -setup {
    .t style layout s1 e2 -minwidth 20 -squeeze x
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 10 25} {10 0 30 45}}

test style-7.6 {layout validation} -setup {
    .t column configure C0 -width ""
    .t style layout s1 e1 -minwidth "" -squeeze ""
    .t style layout s1 e2 -minwidth "" -squeeze ""
    .t item configure root -height 1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-7.7 {layout validation} -setup {
    .t style layout s1 e1 -squeeze y
    .t style layout s1 e2 -squeeze ""
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-7.8 {layout validation} -setup {
    .t style layout s1 e1 -squeeze ""
    .t style layout s1 e2 -squeeze y
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 25}}

test style-7.9 {layout validation} -setup {
    .t style layout s1 e1 -squeeze y
    .t style layout s1 e2 -squeeze y
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 1} {20 0 60 1}}

test style-7.10 {layout validation} -setup {
    .t style layout s1 e1 -squeeze ""
    .t style layout s1 e2 -squeeze ""
    .t style configure s1 -orient vertical
    .t item configure root -height ""
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 25 40 70}}

test style-7.11 {layout validation} -setup {
    .t column configure C0 -width 1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 25 40 70}}

test style-7.12 {layout validation} -setup {
    .t style layout s1 e2 -squeeze x
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 25 20 70}}

test style-7.13 {layout validation} -setup {
    .t style layout s1 e1 -squeeze x
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 1 25} {0 25 1 70}}

test style-7.14 {layout validation} -setup {
    .t style layout s1 e1 -minwidth 10
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 10 25} {0 25 10 70}}

test style-7.15 {layout validation} -setup {
    .t column configure C0 -width ""
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 25 40 70}}

test style-7.16 {layout validation} -setup {
    .t style layout s1 e1 -squeeze y
    .t item configure root -height 1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 0} {0 0 40 45}}

test style-7.17 {layout validation} -setup {
    .t style layout s1 e1 -minheight 10
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 10} {0 10 40 55}}

test style-7.18 {layout validation} -setup {
    .t style layout s1 e2 -squeeze y
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 10} {0 10 40 10}}

test style-7.19 {layout validation} -setup {
    .t style layout s1 e2 -minheight 10
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 10} {0 10 40 20}}

test style-8.1 {layout validation} -setup {
    destroy .t
    pack [treectrl .t]
    .t configure -showheader no -borderwidth 0 -highlightthickness 0
    .t element create e1 rect -width 20 -height 25 -fill blue
    .t element create e2 rect -width 40 -height 45 -fill green
    .t style create s1 -orient horizontal
    .t style elements s1 {e1 e2}
    .t column create -tags C0
    .t item style set root C0 s1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-8.2 {layout validation} -setup {
    .t column configure C0 -width 200
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-8.3 {layout validation} -setup {
    .t column configure C0 -justify right
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{140 0 160 25} {160 0 200 45}}

test style-8.4 {layout validation} -setup {
    .t column configure C0 -justify center
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{70 0 90 25} {90 0 130 45}}

test style-8.5 {layout validation} -setup {
    .t column configure C0 -justify left
    .t style layout s1 e2 -detach yes
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 0 40 45}}

test style-8.6 {layout validation} -setup {
    .t column configure C0 -justify center
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{90 0 110 25} {0 0 40 45}}

test style-8.7 {layout validation} -setup {
    .t column configure C0 -justify right
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{180 0 200 25} {0 0 40 45}}

test style-8.8 {layout validation} -setup {
    .t column configure C0 -justify left
    .t style layout s1 e2 -detach no
    .t style configure s1 -orient vertical
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 25 40 70}}

test style-8.9 {layout validation} -setup {
    .t column configure C0 -justify center
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{80 0 100 25} {80 25 120 70}}

test style-8.10 {layout validation} -setup {
    .t column configure C0 -justify right
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{160 0 180 25} {160 25 200 70}}

test style-8.11 {layout validation} -setup {
    .t column configure C0 -justify left
    .t style layout s1 e2 -detach yes
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {0 0 40 45}}

test style-8.12 {layout validation} -setup {
    .t column configure C0 -justify center
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{90 0 110 25} {0 0 40 45}}

test style-8.13 {layout validation} -setup {
    .t column configure C0 -justify right
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{180 0 200 25} {0 0 40 45}}

test style-9.1 {layout validation} -setup {
    destroy .t
    pack [treectrl .t]
    .t configure -showheader no -borderwidth 0 -highlightthickness 0
    .t element create e1 rect -width 20 -height 25 -fill blue
    .t element create e2 rect -width 40 -height 45 -fill green
    .t style create s1 -orient horizontal
    .t style elements s1 {e1 e2}
    .t column create -tags C0
    .t item style set root C0 s1
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{0 0 20 25} {20 0 60 45}}

test style-9.2 {layout validation} -setup {
    .t style layout s1 e1 -padx 5
    .t style layout s1 e2 -padx {0 5}
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{5 0 25 25} {30 0 70 45}}

test style-9.3 {layout validation} -setup {
    .t column configure C0 -width 200 -justify center
    # (200 - 75) / 2 = 62
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{67 0 87 25} {92 0 132 45}}

test style-9.4 {layout validation} -setup {
    .t column configure C0 -justify right
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{130 0 150 25} {155 0 195 45}}

test style-9.5 {layout validation} -setup {
    .t column configure C0 -justify left
    .t style configure s1 -orient vertical
    .t style layout s1 e1 -pady 5
    .t style layout s1 e2 -padx 5 -pady {0 5}
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{5 5 25 30} {5 35 45 80}}

test style-9.6 {layout validation} -setup {
    .t column configure C0 -justify center
    # (200 - 50) / 2 = 75
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{80 5 100 30} {80 35 120 80}}

test style-9.7 {layout validation} -setup {
    .t column configure C0 -justify right
} -body {
    list [.t item bbox root C0 e1] [.t item bbox root C0 e2]
} -result {{155 5 175 30} {155 35 195 80}}

test style-10.1 {style layout -center: invalid value} -setup {
    .t style create testStyle -orient horizontal
    .t element create testElem rect -width 20 -height 20
    .t style elements testStyle testElem
} -body {
    .t style layout testStyle testElem -center xyzzy
} -returnCodes error -result {bad center value "xyzzy": must be a string containing zero or more of x and y}

test style-10.2 {style layout -center: valid value} -body {
    .t style layout testStyle testElem -center xy
    .t style layout testStyle testElem -center
} -result {xy}

test style-10.3 {style layout -center: column width is unspecified, no centering} -setup {
    .t column configure C0 -width ""
    .t item style set root C0 testStyle
} -body {
    .t item bbox root C0 testElem
} -result {0 0 20 20}

test style-10.4 {style layout -center: equal space on both sides} -setup {
    .t column configure C0 -width 100
} -body {
    # (100 - 20) / 2 = 40
    .t item bbox root C0 testElem
} -result {40 0 60 20}

test style-10.5 {style layout -center: -expand does nothing} -body {
    .t style layout testStyle testElem -expand w
    .t item bbox root C0 testElem
} -result {40 0 60 20}

test style-10.6 {style layout -center: -expand does nothing} -body {
    .t style layout testStyle testElem -expand e
    .t item bbox root C0 testElem
} -result {40 0 60 20}

test style-10.7 {style layout -center: padding is still respected} -setup {
    .t column configure C0 -width ""
} -body {
    .t style layout testStyle testElem -padx {15 25}
    .t item bbox root C0 testElem
} -result {15 0 35 20}

test style-10.8 {style layout -center: padding is still respected} -setup {
    # 15 + 20 + 25 = 60
    .t column configure C0 -width 60
} -body {
    .t item bbox root C0 testElem
} -result {15 0 35 20}

test style-10.9 {style layout -center: padding is still respected} -setup {
    # 8 more pixels than needed, should result in (68-20)/2=24 on each side
    # but that would push it too far right, so: 23 + 20 + 25 = 68
    .t column configure C0 -width 68
} -body {
    .t item bbox root C0 testElem
} -result {23 0 43 20}

test style-10.10 {style layout -center: 2 centered elements} -setup {
    .t column configure C0 -width 100
    .t element create testElem2 rect -width 20 -height 20
    .t style elements testStyle {testElem testElem2}
    .t style layout testStyle testElem2 -center xy -padx 5
} -body {
    # (100 - (20 + 25 + 5 + 20)) / 2 = (100 - 70) / 2 = 15
    list [.t item bbox root C0 testElem] [.t item bbox root C0 testElem2]
} -result {{15 0 35 20} {65 0 85 20}}

test style-10.11 {style layout -center: 2 centered elements} -body {
    .t style layout testStyle testElem -padx 5
    # (100 - (20 + 5 + 5 + 20)) / 2 = 25
    list [.t item bbox root C0 testElem] [.t item bbox root C0 testElem2]
} -result {{25 0 45 20} {55 0 75 20}}

test style-10.12 {style layout -center: 1 centered, 1 not centered} -setup {
    .t style layout testStyle testElem2 -center ""
} -body {
    # (100 - 20) / 2 = 40
    list [.t item bbox root C0 testElem] [.t item bbox root C0 testElem2]
} -result {{40 0 60 20} {70 0 90 20}}

test style-10.13 {style layout -center: 1 centered, 1 not centered} -setup {
    .t style layout testStyle testElem2 -expand w
} -body {
    list [.t item bbox root C0 testElem] [.t item bbox root C0 testElem2]
} -result {{40 0 60 20} {75 0 95 20}}

test style-99.1 {some needed cleanup} -body {
    destroy .t
} -result {}

# cleanup
::tcltest::cleanupTests
return
