OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.2 KiB

  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 - 2006 The T2 SDE Project
  6. # Copyright (C) 1998 - 2003 Clifford Wolf
  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 ---
  15. #
  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.
  21. gui_dialog_lines="$( stty size | cut -d' ' -f1 )"
  22. gui_dialog_columns="$( stty size | cut -d' ' -f2 )"
  23. gui_dialog_s70=$(( gui_dialog_columns - 10 ))
  24. gui_dialog_s62=$(( gui_dialog_columns - 18 ))
  25. gui_dialog_s15=$(( gui_dialog_lines - 10 ))
  26. gui_dialog() {
  27. dialog --stdout --title 'STONE - Setup Tool ONE - T2 System Configuration' "$@"
  28. }
  29. # [ the following variables act as staticly declared variables in functions (in
  30. # C), i.e. they keep their content even after return of the certain function
  31. # to be used again and again in this function. ]
  32. #
  33. # Important is to know that an element of `gui_menu_tree_name' corresponds
  34. # to an element of `gui_menu_tree_value' by the same number, i.e.
  35. # gui_menu_tree_value[0] contains the value of gui_menu_tree_name[0]
  36. #
  37. declare -a gui_menu_tree_name
  38. declare -a gui_menu_tree_value
  39. gui_menu_tree_id=-1
  40. # Use: gui_menu "ID" "Title" "Text" "Action" [ "Text" "Action" [ .. ] ]
  41. #
  42. gui_menu() {
  43. # `cmd_ar' acts as a kind of mapper between the choosen category
  44. # and the function's/command's name that is responsible for what
  45. # next is to do. This command/function is finally executed.
  46. local -a cmd_ar
  47. # Although dialog does folding itself, we're forced
  48. # to do it directly, because we need the number of
  49. # lines to compute the size of a widget.
  50. local id="$1" title="$( echo "$2" | fmt -$gui_dialog_s62 )"
  51. local y_text=$( echo "$title" | wc -l ) y_menu=$(( ($#-2) / 2 ))
  52. local nr=1 x="'" y choosen='' ; shift 2
  53. [ $(( $y_text + $y_menu )) -gt $gui_dialog_s15 ] && \
  54. y_menu=$(( gui_dialog_s15 - y_text ))
  55. if [ $id = main ] ; then local cmd="gui_dialog --cancel-label Exit"
  56. elif [ "$gui_nocancel" = 1 ] ; then cmd="gui_dialog --no-cancel"
  57. else local cmd="gui_dialog --cancel-label Back" ; fi
  58. # In case of having been in the current menu before (found out by
  59. # checking the current ID for the ones saved in `gui_menu_tree_name[]'),
  60. # make the old item be cursored again.
  61. local default='' count
  62. for (( count=$gui_menu_tree_id; $count >= 0; count-- ))
  63. do
  64. if [ "${gui_menu_tree_name[$count]}" = $id ] ; then
  65. default="${gui_menu_tree_value[$count]}"
  66. gui_menu_tree_id=$(( $count - 1 ))
  67. break
  68. fi
  69. done
  70. cmd="$cmd --default-item \${default:-0}"
  71. cmd="$cmd --menu '${title//$x/$x\\$x$x}'"
  72. cmd="$cmd $(( $y_text + $y_menu + 6 )) $gui_dialog_s70 $y_menu"
  73. while [ $# -gt 0 ] ; do
  74. y="${2#\*}"
  75. if [ -z "$default" -a "$y" != "$2" ] ; then
  76. default="$nr"
  77. fi
  78. if [ -z "$y" ] ; then
  79. if [ -z "$1" ] ; then
  80. # this line should become a blank one
  81. cmd="$cmd ' ' ' '"
  82. else
  83. # the purpose of this line is only to
  84. # display additional information about
  85. # the item before
  86. cmd="$cmd '-' '${1//$x/$x\\$x$x}'"
  87. fi
  88. else
  89. cmd="$cmd $nr '${1//$x/$x\\$x$x}'"
  90. cmd_ar[$nr]="$y"
  91. ((nr++))
  92. fi
  93. shift ; shift
  94. done
  95. # `choosen' gets the choosen item that represents in fact
  96. # the dereferencer for `cmd_ar'.
  97. choosen="$(eval "$cmd")"
  98. if [ $? -eq 0 ]; then
  99. # if enter is pressed on an ``additional information line'',
  100. # do nothing.
  101. [ "$choosen" = "-" ] && return 0
  102. gui_menu_tree_id=$(( $gui_menu_tree_id + 1 ))
  103. gui_menu_tree_name[$gui_menu_tree_id]=$id
  104. gui_menu_tree_value[$gui_menu_tree_id]=$choosen
  105. eval "${cmd_ar[$choosen]}"
  106. return 0
  107. else
  108. return 1
  109. fi
  110. }
  111. # Use: gui_input "Text" "Default" "VarName"
  112. #
  113. gui_input() {
  114. local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )" \
  115. height=$(( $(echo "$headlines" | wc -l) + 7 )) tmp cmd
  116. if [ "$gui_nocancel" = 1 ] ; then cmd="gui_dialog --no-cancel"
  117. else local cmd="gui_dialog --cancel-label Back" ; fi
  118. if tmp="$($cmd --inputbox "$headlines" $height $gui_dialog_s70 "$2")"; then
  119. eval "$3='$tmp'"
  120. fi
  121. }
  122. # Use: gui_message "Text"
  123. #
  124. gui_message() {
  125. local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )"
  126. gui_dialog --msgbox "$headlines" \
  127. $(( $( echo "$headlines" | wc -l ) + 4 )) $gui_dialog_s70
  128. }
  129. # Use: gui_yesno "Text"
  130. #
  131. gui_yesno() {
  132. local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )"
  133. gui_dialog --yesno "$headlines" \
  134. $(( $( echo "$headlines" | wc -l ) + 4 )) $gui_dialog_s70
  135. }
  136. # Use: gui_edit "Text" "File"
  137. #
  138. gui_edit() {
  139. # find editor
  140. for x in $EDITOR vi nvi emacs xemacs pico ; do
  141. if type -p $x > /dev/null
  142. then xx=$x ; break ; fi
  143. done
  144. if [ "$xx" ] ; then
  145. eval "$xx $2"
  146. else
  147. gui_message "Cannot find any editor. Make sure \$EDITOR is set."
  148. fi
  149. }
  150. # Use: gui_cmd "Title" "Command"
  151. # (Title isn't used in this GUI type)
  152. gui_cmd() {
  153. shift ; eval "$@"
  154. read -p "Press ENTER to continue."
  155. }