#------------------------------------------------------------------------------
# /var/install/include/ecelib - script interface for edit-conf.cui
#
# Creation:     2007-11-01 dv
# Last update:  $Id: ecelib 44992 2017-02-18 16:45:48Z hbfl $
#
# Copyright (c) 2007-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# ece API codes
ECE_API_GETVALUE=1000

value=""

#------------------------------------------------------------------------------
# Get variable within current configuration loaded in editor
# Expects: $1  <-- MainWin        : Window Handle
#          $2  <-- Variable-Name  : String (something like "FOO_2_NAME")
# Returns: $p2 --> Value          : String
#------------------------------------------------------------------------------
ece_get_value()
{
    cui_send "C" "${ECE_API_GETVALUE}" "$@"
    cui_wait_ack
    return $?
}

#----------------------------------------------------------------------------
# setdata
# ece --> preinitialize value before the dialog is opened
#----------------------------------------------------------------------------
setdata()
{
    value="$p2"
    cui_return ""
}

#----------------------------------------------------------------------------
# getdata
# ece --> request modified value after the dialog has been closed with IDOK
#----------------------------------------------------------------------------
getdata()
{
    cui_return "$value"
}

#----------------------------------------------------------------------------
# SELECT LIST DIALOG BEGIN
#----------------------------------------------------------------------------

ECE_SELECT_LIST_DLG_IDC_COLORS=10
ECE_SELECT_LIST_DLG_IDC_OK=11
ECE_SELECT_LIST_DLG_IDC_CANCEL=12

#----------------------------------------------------------------------------
# ece_select_list_dlg_ok_button
# OK button clicked callback
#----------------------------------------------------------------------------
ece_select_list_dlg_ok_button()
{
    local win="$p2"
    local ctrl
    local sel

    if cui_window_getctrl "$win" "${ECE_SELECT_LIST_DLG_IDC_COLORS}"
    then
        ctrl="$p2"

        if cui_listbox_getsel "$ctrl"
        then
            sel="$p2"

            cui_listbox_get "$ctrl" "$sel"
            value="$p2"
        fi
    fi
    cui_window_close "$win" "${IDOK}"
    cui_return 1
}

#----------------------------------------------------------------------------
# ece_select_list_dlg_cancel_button
# Cancel button clicked callback
#----------------------------------------------------------------------------
ece_select_list_dlg_cancel_button()
{
    local win="$p2"
    cui_window_close "$win" "${IDCANCEL}"
    cui_return 1
}

#----------------------------------------------------------------------------
# ece_select_list_dlg_create_hook (for creation of child windows)
#    $p2 --> dialog window handle
#----------------------------------------------------------------------------
ece_select_list_create_hook()
{
    local dlg="$p2"
    local ctrl
    local count="0"        # min list height
    local width="25"       # min list width

    # get listbox metrics
    LOCAL_OLD_IFS="$IFS"
    IFS=","
    for sel in ${ece_select_list}
    do
        if [ ${#sel} -gt ${width} ]
        then
            width=${#sel}
        fi
        count=$[$count + 1]
    done
    IFS="$LOCAL_OLD_IFS"

    # correct spaces
    width=$[$width + 3]

    # limit list box height
    if [ "$count" -gt 17 ]
    then
        count=17
    fi

    # limit list box width
    if [ "$width" -gt 65 ]
    then
        width="65"
    fi

    # create listbox
    if cui_listbox_new "$dlg" "" 2 1 ${width} ${count} \
        ${ECE_SELECT_LIST_DLG_IDC_COLORS} $CWS_NONE $CWS_BORDER
    then
        ctrl="$p2"
        cui_window_setcolors  "$ctrl" "MENU"
        cui_window_create     "$ctrl"

        LOCAL_OLD_IFS="$IFS"
        IFS=","
        for sel in ${ece_select_list}
        do
            cui_listbox_add   "$ctrl" "$sel"
        done
        IFS="$LOCAL_OLD_IFS"

        cui_listbox_select    "$ctrl" "$value"
    fi

    # create buttons
    if cui_button_new "$dlg" "&OK" "$[$width / 2 - 11 + 2]" "$[$count + 2]" 10 1 \
        ${ECE_SELECT_LIST_DLG_IDC_OK} ${CWS_DEFOK} ${CWS_NONE}
    then
        ctrl="$p2"
        cui_button_callback   "$ctrl" "$BUTTON_CLICKED" "$dlg" "ece_select_list_dlg_ok_button"
        cui_window_create     "$ctrl"
    fi
    if cui_button_new "$dlg" "&Cancel" "$[$width / 2 + 1 + 2]" "$[$count + 2]" 10 1 \
        ${ECE_SELECT_LIST_DLG_IDC_CANCEL} ${CWS_DEFCANCEL} ${CWS_NONE}
    then
        ctrl="$p2"
        cui_button_callback   "$ctrl" "$BUTTON_CLICKED" "$dlg" "ece_select_list_dlg_cancel_button"
        cui_window_create     "$ctrl"
    fi

    # move the window
    if cui_getwindowrect "$dlg"
    then
        local wx="$p2"
        local wy="$p3"
        local deltay="$[7 - $count]"
        local deltax="$[28 - $width]"
        cui_window_move "$dlg" \
           "$[$wx + $deltax / 2]" \
           "$[$wy + $deltay / 2]" \
           "$[34 - $deltax]" \
           "$[13 - $deltay]"
    fi

    cui_return 1
}

#----------------------------------------------------------------------------
# ece_select_list_dlg - Select an item from a single list box
# Expects: $1  <-- Parent window : Window Handle
#          $2  <-- Title         : String
#          $3  <-- SelList       : String ("Value1,Value2,Value3")
# Returns: Nothing
#----------------------------------------------------------------------------
ece_select_list_dlg()
{
    local win="$1"
    local title="$2"
    local result="$IDCANCEL"

    ece_select_list="`echo $3 | sed -e ':a' -e '/$/N;s/\n/,/;ta' | sed 's/ *, */,/g'`"

    if cui_window_new "$win" 0 0 34 13 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED]
    then
        dlg="$p2"
        cui_window_setcolors  "$dlg" "DIALOG"
        cui_window_settext    "$dlg" "$title"
        cui_window_sethook    "$dlg" "$HOOK_CREATE"  ece_select_list_create_hook
        cui_window_create     "$dlg"
        cui_window_modal      "$dlg" && result="$p2"
        cui_window_destroy    "$dlg"
    fi

    cui_return $result
}

#----------------------------------------------------------------------------
# SELECT LIST DIALOG END
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# COMMENT LIST DIALOG BEGIN
#----------------------------------------------------------------------------

ECE_COMMENT_LIST_DLG_IDC_COLORS=10
ECE_COMMENT_LIST_DLG_IDC_OK=11
ECE_COMMENT_LIST_DLG_IDC_CANCEL=12

#----------------------------------------------------------------------------
# ece_comment_list_dlg_ok_button
# OK button clicked callback
#----------------------------------------------------------------------------
ece_comment_list_dlg_ok_button()
{
    local win="$p2"
    local ctrl
    local sel

    if cui_window_getctrl "$win" "${ECE_COMMENT_LIST_DLG_IDC_COLORS}"
    then
        ctrl="$p2"

        if cui_listview_getsel "$ctrl"
        then
            sel="$p2"

            cui_listview_gettext "$ctrl" "$sel" "0"
            value="$p2"
        fi
    fi

    cui_window_close "$win" "${IDOK}"
    cui_return 1
}

#----------------------------------------------------------------------------
# ece_comment_list_dlg_cancel_button
# Cancel button clicked callback
#----------------------------------------------------------------------------
ece_comment_list_dlg_cancel_button()
{
    local win="$p2"
    cui_window_close "$win" "${IDCANCEL}"
    cui_return 1
}

#----------------------------------------------------------------------------
# ece_comment_list_dlg_create_hook (for creation of child windows)
#    $p2 --> dialog window handle
#----------------------------------------------------------------------------
ece_comment_list_create_hook()
{
    local dlg="$p2"
    local ctrl
    local count="0"
    local idx
    local selection=""

    local maxwidth1="0"
    local maxwidth2="0"

    local reqwidth="42"
    local reqheight="10"
    local termwidth="80"
    local termheight="25"

    # get real terminal size
    local desktop
    cui_getdesktop && desktop="$p2"
    if cui_valid_handle "$desktop"
    then
        if cui_getwindowrect "$desktop"
        then
            termwidth="$p4"
            termheight="$p5"
        fi
    fi

    # create listview
    if cui_listview_new "$dlg" "" 0 0 42 9 2 ${ECE_COMMENT_LIST_DLG_IDC_COLORS} ${CWS_NONE} ${CWS_BORDER}
    then
        ctrl="$p2"
        cui_window_create        "$ctrl"
        cui_listview_setcoltext  "$ctrl" "0" " Value "
        cui_listview_setcoltext  "$ctrl" "1" " Description "

        cui_listview_settitlealignment  "$ctrl" "0" "${ALIGN_LEFT}"
        cui_listview_settitlealignment  "$ctrl" "1" "${ALIGN_LEFT}"

        # transfer entries into the listview control
        LOCAL_OLD_IFS="$IFS"
        IFS=","
        for sel in ${ece_comment_list}
        do
            local column1=${sel%|*}
            local column2=${sel#*|}

            [ "${#column1}" -gt "${maxwidth1}" ] && maxwidth1="${#column1}"
            [ "${#column2}" -gt "${maxwidth2}" ] && maxwidth2="${#column2}"

            cui_listview_add     "$ctrl" && idx="$p2"
            cui_listview_settext "$ctrl" "$idx" "0" "$column1"
            cui_listview_settext "$ctrl" "$idx" "1" "$column2"

            if [ "$column1" == "$value" ]
            then
                selection="$idx"
            fi
            count=$[$count + 1]
        done
        IFS="$LOCAL_OLD_IFS"

        # calculate height of entire window
        reqheight=$[$count + 7]
        [ "$reqheight" -lt "13" ]                 && reqheight="13"
        [ "$reqheight" -gt "$[$termheight - 3]" ] && reqheight="$[$termheight - 3]"

        # calculate width of entire window
        reqwidth=$[$maxwidth1 + $maxwidth2 + 3]
        [ "$reqwidth" -lt "44" ]                  && reqwidth="44"
        [ "$reqwidth" -gt "$[$termwidth - 4]" ]   && reqwidth="$[$termwidth - 4]"

        # resize listbox according to the count of entries
        cui_window_move       "$ctrl" 0 0 "$[$reqwidth - 2]" "$[$reqheight - 4]"
        cui_listview_update   "$ctrl"

        # set selection
        if [ ! -z "$selection" ]
        then
            cui_listview_setsel   "$ctrl" "$selection"
        else
            cui_listview_setsel   "$ctrl" "0"
        fi
    fi

    # create buttons
    if cui_button_new "$dlg" "&OK" "$[$reqwidth / 2 - 11]" "$[$reqheight - 3]" 10 1 ${ECE_COMMENT_LIST_DLG_IDC_OK} $CWS_DEFOK $CWS_NONE
    then
        ctrl="$p2"
        cui_button_callback   "$ctrl" "$BUTTON_CLICKED" "$dlg" "ece_comment_list_dlg_ok_button"
        cui_window_create     "$ctrl"
    fi
    if cui_button_new "$dlg" "&Cancel" "$[$reqwidth / 2 + 1]" "$[$reqheight - 3]" 10 1 ${ECE_COMMENT_LIST_DLG_IDC_CANCEL} $CWS_DEFCANCEL $CWS_NONE
    then
        ctrl="$p2"
        cui_button_callback   "$ctrl" "$BUTTON_CLICKED" "$dlg" "ece_comment_list_dlg_cancel_button"
        cui_window_create     "$ctrl"
    fi

    # move the window
    if cui_getwindowrect "$dlg"
    then
        local wx="$p2"
        local wy="$p3"
        local ww="$p4"
        local wh="$p5"
        local deltay="$[13 - $reqheight]"
        local deltax="$[44 - $reqwidth]"

        cui_window_move "$dlg" "$[$wx + $deltax / 2]" "$[$wy + $deltay / 2]" "$[44 - $deltax]" "$[13 - $deltay]"
    fi

    cui_return 1
}

#----------------------------------------------------------------------------
# ece_comment_list_dlg - Select an item from a list view window decorated with
# with comments
# Expects: $1  <-- Parent window : Window Handle
#          $2  <-- Title         : String
#          $3  <-- SelList       : String ("Value1,Value2,Value3")
# Returns: Nothing
#----------------------------------------------------------------------------
ece_comment_list_dlg()
{
    local win="$1"
    local title="$2"
    local result="$IDCANCEL"

    ece_comment_list="`echo $3 | sed -e ':a' -e '/$/N;s/\n/,/;ta' | sed 's/ *, */,/g'`"

    if cui_window_new "$win" 0 0 44 13 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED]
    then
        dlg="$p2"
        cui_window_setcolors  "$dlg" "DIALOG"
        cui_window_settext    "$dlg" "$title"
        cui_window_sethook    "$dlg" "$HOOK_CREATE"  ece_comment_list_create_hook
        cui_window_create     "$dlg"
        cui_window_modal      "$dlg" && result="$p2"
        cui_window_destroy    "$dlg"
    fi

    cui_return $result
}

#----------------------------------------------------------------------------
# COMMENT LIST DIALOG END
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# SELECT CBLIST DIALOG BEGIN
#----------------------------------------------------------------------------

ECE_SELECT_CBLIST_DLG_IDC_COLORS=10
ECE_SELECT_CBLIST_DLG_IDC_OK=11
ECE_SELECT_CBLIST_DLG_IDC_CANCEL=12
ECE_SELECT_CBLIST_VAL_SEPARATOR=',' # default separator for multi-value field

# array of checkbox values
declare -a cbvalues
# array of checkbox length
declare -a cblength
# array of checkbox ids
declare -a cbids
# array of checkbox handles
declare -a cbhandles

# number of elements
ecounter=0

# array of set elements
declare -A eset

#----------------------------------------------------------------------------
# ece_select_cblist_dlg_ok_button
# OK button clicked callback
#----------------------------------------------------------------------------
ece_select_cblist_dlg_ok_button()
{
    local win="$p2"
    local ctrl
    local lvalue=''  # local result list

    if cui_window_getctrl "$win" "${ECE_SELECT_CBLIST_DLG_IDC_COLORS}"
    then
        ctrl="$p2"

        # create result list
        for (( ix=0; ix < $ecounter; ix++ ))
        do
            cui_checkbox_getcheck ${cbhandles[$ix]}
            if [ $p2 = 1 ]
            then
               [ "$lvalue" = '' ] && lvalue="${cbvalues[$ix]}" || lvalue="$lvalue${ECE_SELECT_CBLIST_VAL_SEPARATOR}${cbvalues[$ix]}"
            fi
        done

        value=$lvalue

    fi
    cui_window_close "$win" "${IDOK}"
    cui_return 1
}

#----------------------------------------------------------------------------
# ece_select_cblist_dlg_cancel_button
# Cancel button clicked callback
#----------------------------------------------------------------------------
ece_select_cblist_dlg_cancel_button()
{
    local win="$p2"
    cui_window_close "$win" "${IDCANCEL}"
    cui_return 1
}

#----------------------------------------------------------------------------
# ece_select_cblist_dlg_create_hook (for creation of child windows)
#    $p2 --> dialog window handle
#----------------------------------------------------------------------------
ece_select_cblist_create_hook()
{
    local dlg="$p2"
    local ctrl
    local count="0"                   # min list height
    local width="${titlewidth}"       # min list width
    local iid=101                     # checkbox ids
    local setter                      # checkbox setter
    local sel

    # get listbox metrics and array values
    LOCAL_OLD_IFS="$IFS"
    IFS=","
    for sel in ${ece_select_list}
    do
	# limit element length
	# length > 60, cut to 58 and add ..
	if [ ${#sel} -gt 60 ]
        then
           sel="${sel:0:58}.."
        fi
        if [ ${#sel} -gt ${width} ]
        then
            width=$[${#sel} + 2]
        fi
        cbvalues[$count]="$sel"
        cblength[$count]=${#sel}
        cbids[$count]=$iid
        count=$[$count + 1]
        iid=$[$iid + 1]
    done
    IFS="$LOCAL_OLD_IFS"

    ecounter=$count
    # limit number of checkboxes
    if [ "$count" -gt 16 ]
    then
        count=16
    fi

    # limit check box width
    if [ "$width" -gt 67 ]
    then
        width="67"
    fi

    # create the checkboxes
    idx=0
    posy=1
    while [ "$idx" -lt "$count" ]
    do
       cui_checkbox_new "$dlg" "${cbvalues[$idx]}" 2 "$posy" $[ 4 + ${cblength[$idx]} ] 1 "${cbids[$idx]}" $CWS_NONE $CWS_NONE
       r_ctrl="$p2"
       cbhandles[$idx]=$r_ctrl
       setter=${eset[${cbvalues[$idx]}]}
       [ "$setter" = '' ] && setter=0
       cui_checkbox_setcheck "$r_ctrl" "$setter"
       cui_window_create     "$r_ctrl"
       idx=$[$idx + 1]
       posy=$[$posy + 1]
    done

    # create buttons
    if cui_button_new "$dlg" "&OK" "$[$width / 2 - 11 + 2]" "$[$count + 2]" 10 1 \
        ${ECE_SELECT_CBLIST_DLG_IDC_OK} ${CWS_DEFOK} ${CWS_NONE}
    then
        ctrl="$p2"
        cui_button_callback   "$ctrl" "$BUTTON_CLICKED" "$dlg" "ece_select_cblist_dlg_ok_button"
        cui_window_create     "$ctrl"
    fi
    if cui_button_new "$dlg" "&Cancel" "$[$width / 2 + 1 + 2]" "$[$count + 2]" 10 1 \
        ${ECE_SELECT_CBLIST_DLG_IDC_CANCEL} ${CWS_DEFCANCEL} ${CWS_NONE}
    then
        ctrl="$p2"
        cui_button_callback   "$ctrl" "$BUTTON_CLICKED" "$dlg" "ece_select_cblist_dlg_cancel_button"
        cui_window_create     "$ctrl"
    fi

    # move the window
    if cui_getwindowrect "$dlg"
    then
        local wx="$p2"
        local wy="$p3"
        # was ist 7 ?
        local deltay="$[7 - $count]"
        # was ist 28
        local deltax="$[28 - $width]"
        cui_window_move "$dlg" \
           "$[$wx + $deltax / 2]" \
           "$[$wy + $deltay / 2]" \
           "$[34 - $deltax]" \
           "$[13 - $deltay]"
    fi

    cui_return 1
}

#----------------------------------------------------------------------------
# ece_select_cblist_dlg - Select items from a checkbox list
# Expects: $1  <-- Parent window : Window Handle
#          $2  <-- Title         : String
#          $3  <-- SelList       : String ("Value1,Value2,Value3")
# Returns: Nothing
#----------------------------------------------------------------------------
ece_select_cblist_dlg()
{
    local win="$1"
    local result="$IDCANCEL"
    local count="0"
    local sel
    local title

    # store selected elements
    # read current selection list and preset the checkboxes appropriately
    LOCAL_OLD_IFS="$IFS"
    IFS="${ECE_SELECT_CBLIST_VAL_SEPARATOR}"
    for sel in ${value}
    do
        eset[$sel]=1
    done
    IFS="$LOCAL_OLD_IFS"

    ece_select_list="$3"

    # check selectable elements
    LOCAL_OLD_IFS="$IFS"
    IFS=","
    for sel in ${ece_select_list}
    do
	count=$[$count + 1]
    done
    IFS="$LOCAL_OLD_IFS"

    # limit number of checkboxes
    if [ "$count" -gt 15 ]
    then
	cui_message "$win" "Too many elements ($count/15)." "Checkbox selection" "$MB_OK"
    else
	# get listbox metrics
	title="$2"
	# add a line character left and right
	titlewidth=$[ 2 + ${#title} ]

	if cui_window_new "$win" 0 0 ${titlewidth} 13 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED]
	then
	    dlg="$p2"
	    cui_window_setcolors  "$dlg" "DIALOG"
	    cui_window_settext    "$dlg" "$title"
	    cui_window_sethook    "$dlg" "$HOOK_CREATE"  ece_select_cblist_create_hook
	    cui_window_create     "$dlg"
	    cui_window_modal      "$dlg" && result="$p2"
	    cui_window_destroy    "$dlg"
	fi
    fi

    cui_return $result
}

#----------------------------------------------------------------------------
# SELECT CBLIST DIALOG END
#----------------------------------------------------------------------------
