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.

181 lines
5.2 KiB

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