#!/bin/bash
#------------------------------------------------------------------------------
# /var/install/include/ecelib - script interface for edit-conf.cui
#
# Copyright (c) 2007 eisfair-Team
#
# Creation:    2007-11-01 dv
# Last update: $Id: ecelib 23498 2010-03-14 21:57:47Z dv $
#
# 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"

    # create listbox
    if cui_listbox_new "$dlg" "" 2 1 28 7 ${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"
            count=$[$count + 1]
        done
        IFS="$LOCAL_OLD_IFS"

        if [ "$count" -gt 7 ]
        then
            count=7
        fi 

        cui_window_move       "$ctrl" 2 1 28 $count
        cui_listbox_select    "$ctrl" "$value"
    fi

    # create buttons
    if cui_button_new "$dlg" "&OK" 5 "$[$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" 17 "$[$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 ww="$p4"
        local wh="$p5"
        local delta="$[7 - $count]"
        cui_window_move "$dlg" "$wx" "$[$wy + $delta / 2]" 34 "$[13 - $delta]"
    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
#----------------------------------------------------------------------------
