* updated kde-dev-utils (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / package / base / stone / stone_gui_dialog.sh
bloba48eaecdebaa5d0b4827d2caf7732e7b4934b11c
1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # This copyright note is auto-generated by scripts/Create-CopyPatch.
3 #
4 # T2 SDE: package/*/stone/stone_gui_dialog.sh
5 # Copyright (C) 2004 - 2021 The T2 SDE Project
6 # Copyright (C) 1998 - 2003 ROCK Linux Project
7 #
8 # More information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- T2-COPYRIGHT-NOTE-END ---
16 # Filename: gui_dialog.sh
17 # Description:
18 # ============
19 # This file provides the gui-functions implemented with
20 # the use of dialog, i.e. a curses based menu-frontend.
22 gui_dialog_lines="$( stty size | cut -d' ' -f1 )"
23 gui_dialog_columns="$( stty size | cut -d' ' -f2 )"
25 gui_dialog_s70=$(( gui_dialog_columns - 10 ))
26 gui_dialog_s62=$(( gui_dialog_columns - 18 ))
27 gui_dialog_s15=$(( gui_dialog_lines - 10 ))
29 gui_dialog() {
30 dialog --stdout --title 'STONE - Setup Tool ONE - T2 System Configuration' "$@"
34 # [ the following variables act as staticly declared variables in functions (in
35 # C), i.e. they keep their content even after return of the certain function
36 # to be used again and again in this function. ]
38 # Important is to know that an element of `gui_menu_tree_name' corresponds
39 # to an element of `gui_menu_tree_value' by the same number, i.e.
40 # gui_menu_tree_value[0] contains the value of gui_menu_tree_name[0]
42 declare -a gui_menu_tree_name
43 declare -a gui_menu_tree_value
44 gui_menu_tree_id=-1
46 # Use: gui_menu "ID" "Title" "Text" "Action" [ "Text" "Action" [ .. ] ]
48 gui_menu() {
49 # `cmd_ar' acts as a kind of mapper between the choosen category
50 # and the function's/command's name that is responsible for what
51 # next is to do. This command/function is finally executed.
52 local -a cmd_ar
54 # Although dialog does folding itself, we're forced
55 # to do it directly, because we need the number of
56 # lines to compute the size of a widget.
57 local id="$1" title="$( echo "$2" | fmt -$gui_dialog_s62 )"
58 local y_text=$( echo "$title" | wc -l ) y_menu=$(( ($#-2) / 2 ))
59 local nr=1 x="'" y choosen='' ; shift 2
61 [ $(( $y_text + $y_menu )) -gt $gui_dialog_s15 ] && \
62 y_menu=$(( gui_dialog_s15 - y_text ))
64 if [ $id = main ] ; then local cmd="gui_dialog --cancel-label Exit"
65 elif [ "$gui_nocancel" = 1 ] ; then cmd="gui_dialog --no-cancel"
66 else local cmd="gui_dialog --cancel-label Back" ; fi
68 # In case of having been in the current menu before (found out by
69 # checking the current ID for the ones saved in `gui_menu_tree_name[]'),
70 # make the old item be cursored again.
71 local default='' count
72 for (( count=$gui_menu_tree_id; $count >= 0; count-- ))
74 if [ "${gui_menu_tree_name[$count]}" = $id ] ; then
75 default="${gui_menu_tree_value[$count]}"
76 gui_menu_tree_id=$(( $count - 1 ))
77 break
79 done
81 cmd="$cmd --default-item \${default:-0}"
82 cmd="$cmd --menu '${title//$x/$x\\$x$x}'"
83 cmd="$cmd $(( $y_text + $y_menu + 6 )) $gui_dialog_s70 $y_menu"
85 while [ $# -gt 0 ] ; do
86 y="${2#\*}"
87 if [ -z "$default" -a "$y" != "$2" ] ; then
88 default="$nr"
90 if [ -z "$y" ] ; then
91 if [ -z "$1" ] ; then
92 # this line should become a blank one
93 cmd="$cmd ' ' ' '"
94 else
95 # the purpose of this line is only to
96 # display additional information about
97 # the item before
98 cmd="$cmd '-' '${1//$x/$x\\$x$x}'"
100 else
101 cmd="$cmd $nr '${1//$x/$x\\$x$x}'"
102 cmd_ar[$nr]="$y"
103 ((nr++))
106 shift ; shift
107 done
109 # `choosen' gets the choosen item that represents in fact
110 # the dereferencer for `cmd_ar'.
111 choosen="$(eval "$cmd")"
113 if [ $? -eq 0 ]; then
114 # if enter is pressed on an ``additional information line'',
115 # do nothing.
116 [ "$choosen" = "-" ] && return 0
118 (( gui_menu_tree_id++ ))
119 gui_menu_tree_name[$gui_menu_tree_id]=$id
120 gui_menu_tree_value[$gui_menu_tree_id]=$choosen
122 eval "${cmd_ar[$choosen]}"
123 return 0
124 else
125 return 1
129 # Use: gui_input "Text" "Default" "VarName"
131 gui_input() {
132 local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )" \
133 height=$(( $(echo "$headlines" | wc -l) + 7 )) tmp cmd
135 if [ "$gui_nocancel" = 1 ] ; then cmd="gui_dialog --no-cancel"
136 else local cmd="gui_dialog --cancel-label Back" ; fi
138 if tmp="$($cmd --inputbox "$headlines" $height $gui_dialog_s70 "$2")"; then
139 eval "$3='$tmp'"
143 # Use: gui_message "Text"
145 gui_message() {
146 local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )"
147 gui_dialog --msgbox "$headlines" \
148 $(( $( echo "$headlines" | wc -l ) + 4 )) $gui_dialog_s70
151 # Use: gui_yesno "Text"
153 gui_yesno() {
154 local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )"
155 gui_dialog --yesno "$headlines" \
156 $(( $( echo "$headlines" | wc -l ) + 4 )) $gui_dialog_s70
160 # Use: gui_edit "Text" "File"
162 gui_edit() {
163 # find editor
164 for x in $EDITOR vi nvi emacs xemacs pico ; do
165 if type -p $x > /dev/null
166 then xx=$x ; break ; fi
167 done
168 if [ "$xx" ] ; then
169 eval "$xx $2"
170 else
171 gui_message "Cannot find any editor. Make sure \$EDITOR is set."
175 # Use: gui_cmd "Title" "Command"
176 # (Title isn't used in this GUI type)
177 gui_cmd() {
178 shift ; eval "$@"
179 read -p "Press ENTER to continue."