##  Dialog to display a message box.
#
# Copyright (c) 2021 <r.zaumseil@freenet.de>

#=== CLASS =====================================================================

##  Dialogbox to display messages.
::oo::class create ::tko::dialogbox {
    ::tko_createsuperwidget ::tko::dialog
    if {[catch {private variable _tko}]} {
        variable _tko
    }
}

#-------------------------------------------------------------------------------

## Displayed image in dialogbox.
::oo::define ::tko::dialogbox ::tko_option -image image Image {} {
    $_tko(.chs).i configure -image $tko(-image)
}

#-------------------------------------------------------------------------------

## Dialogbox message text.
::oo::define ::tko::dialogbox ::tko_option -message message Message {} {}

#-------------------------------------------------------------------------------

## Dialogbox additional detail text.
::oo::define ::tko::dialogbox ::tko_option -detail detail Detail {} {
    if {$tko(-detail) eq {}} {
        destroy $_tko(.dtl)
    } else {
        if {![winfo exists $_tko(.dtl)]} {
            set myHeight 0
            set myWidth 0
            foreach myLine [split $tko(-detail) \n] {
                incr myHeight
                if {[set myW [string length $myLine]]>$myWidth} {set myWidth $myW}
            }
            if {$myHeight > 20} {set myHeight 20}
            if {$myWidth > 80} {set myWidth 80}
            ::tko::grid 2 2 -sticky nesw -cweight 1 -rweight 1 -sb ase\
                ::text      $_tko(.dtl)\
                -relief     flat\
                -background [::ttk::style lookup TFrame -background]\
                -font       TkTextFont\
                -height     $myHeight\
                -width      $myWidth\
                -wrap       none
        } else {
            $_tko(.dtl) delete 1.0 end
            $_tko(.dtl) configure -state normal
        }
        $_tko(.dtl) insert end $tko(-detail)
        $_tko(.dtl) configure -state disabled
    }
}

#-------------------------------------------------------------------------------

##  Create variable dialog widget.
#
# \param args   Configuration arguments.
::oo::define ::tko::dialogbox constructor {args} {
    next {*}$args
    set _tko(.chs)    [my childsite]
    set _tko(.dtl)   $_tko(.chs).dtl

    # message
    ::tko::grid 1 1 ::ttk::label    $_tko(.chs).i
    ::tko::grid 1 2 -sticky ew\
    ::ttk::label    $_tko(.chs).l\
    -padding    {3 3 3 3}\
    -compound   left\
    -justify    left\
    -anchor     nw\
    -font       TkDefaultFontBold\
    -textvariable   [my varname tko](-message)

    my _tko_init
}

#-------------------------------------------------------------------------------

## Close dialogbox.
#
# \param returnvalue Optional value to return on closing.
::oo::define ::tko::dialogbox method deactivate {{returnvalue {}}} {
    next $returnvalue
    after 10 destroy $tko(.)
}

#-------------------------------------------------------------------------------

# vim: set ts=4 sw=4 sts=4 et :
