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