mirror of the now-defunct rocklinux.org
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.

187 lines
5.5 KiB

  1. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  2. #
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. # Please add additional copyright information _after_ the line containing
  5. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  6. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  7. #
  8. # ROCK Linux: rock-src/package/base/sysfiles/stone_gui_dialog.sh
  9. # ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
  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; either version 2 of the License, or
  14. # (at your option) any later version. A copy of the GNU General Public
  15. # License can be found at Documentation/COPYING.
  16. #
  17. # Many people helped and are helping developing ROCK Linux. Please
  18. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  19. # file for details.
  20. #
  21. # --- ROCK-COPYRIGHT-NOTE-END ---
  22. #
  23. # Filename: gui_dialog.sh
  24. # Description:
  25. # ============
  26. # This file provides the gui-functions implemented with
  27. # the use of dialog, i.e. a curses based menu-frontend.
  28. gui_dialog_lines="$( stty size | cut -d' ' -f1 )"
  29. gui_dialog_columns="$( stty size | cut -d' ' -f2 )"
  30. gui_dialog_s70=$(( gui_dialog_columns - 10 ))
  31. gui_dialog_s62=$(( gui_dialog_columns - 18 ))
  32. gui_dialog_s15=$(( gui_dialog_lines - 10 ))
  33. gui_dialog() {
  34. dialog --stdout --title 'STONE - Setup Tool ONE - ROCK Linux System Configuration' "$@"
  35. }
  36. # [ the following variables act as staticly declared variables in functions (in
  37. # C), i.e. they keep their content even after return of the certain function
  38. # to be used again and again in this function. ]
  39. #
  40. # Important is to know that an element of `gui_menu_tree_name' corresponds
  41. # to an element of `gui_menu_tree_value' by the same number, i.e.
  42. # gui_menu_tree_value[0] contains the value of gui_menu_tree_name[0]
  43. #
  44. declare -a gui_menu_tree_name
  45. declare -a gui_menu_tree_value
  46. gui_menu_tree_id=-1
  47. # Use: gui_menu "ID" "Title" "Text" "Action" [ "Text" "Action" [ .. ] ]
  48. #
  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
  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
  60. [ $(( $y_text + $y_menu )) -gt $gui_dialog_s15 ] && \
  61. y_menu=$(( gui_dialog_s15 - y_text ))
  62. if [ $id = main ] ; then local cmd="gui_dialog --cancel-label Exit"
  63. elif [ "$gui_nocancel" = 1 ] ; then cmd="gui_dialog --no-cancel"
  64. else local cmd="gui_dialog --cancel-label Back" ; fi
  65. # In case of having been in the current menu before (found out by
  66. # checking the current ID for the ones saved in `gui_menu_tree_name[]'),
  67. # make the old item be cursored again.
  68. local default='' count
  69. for (( count=$gui_menu_tree_id; $count >= 0; count-- ))
  70. do
  71. if [ "${gui_menu_tree_name[$count]}" = $id ] ; then
  72. default="${gui_menu_tree_value[$count]}"
  73. gui_menu_tree_id=$(( $count - 1 ))
  74. break
  75. fi
  76. done
  77. cmd="$cmd --default-item \${default:-0}"
  78. cmd="$cmd --menu '${title//$x/$x\\$x$x}'"
  79. cmd="$cmd $(( $y_text + $y_menu + 6 )) $gui_dialog_s70 $y_menu"
  80. while [ $# -gt 0 ] ; do
  81. y="${2#\*}"
  82. if [ -z "$default" -a "$y" != "$2" ] ; then
  83. default="$nr"
  84. fi
  85. if [ -z "$y" ] ; then
  86. if [ -z "$1" ] ; then
  87. # this line should become a blank one
  88. cmd="$cmd ' ' ' '"
  89. else
  90. # the purpose of this line is only to
  91. # display additional information about
  92. # the item before
  93. cmd="$cmd '-' '${1//$x/$x\\$x$x}'"
  94. fi
  95. else
  96. cmd="$cmd $nr '${1//$x/$x\\$x$x}'"
  97. cmd_ar[$nr]="$y"
  98. ((nr++))
  99. fi
  100. shift ; shift
  101. done
  102. # `choosen' gets the choosen item that represents in fact
  103. # the dereferencer for `cmd_ar'.
  104. choosen="$(eval "$cmd")"
  105. if [ $? -eq 0 ]; then
  106. # if enter is pressed on an ``additional information line'',
  107. # do nothing.
  108. [ "$choosen" = "-" ] && return 0
  109. gui_menu_tree_id=$(( $gui_menu_tree_id + 1 ))
  110. gui_menu_tree_name[$gui_menu_tree_id]=$id
  111. gui_menu_tree_value[$gui_menu_tree_id]=$choosen
  112. eval "${cmd_ar[$choosen]}"
  113. return 0
  114. else
  115. return 1
  116. fi
  117. }
  118. # Use: gui_input "Text" "Default" "VarName"
  119. #
  120. gui_input() {
  121. local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )" \
  122. height=$(( $(echo "$headlines" | wc -l) + 7 )) tmp cmd
  123. if [ "$gui_nocancel" = 1 ] ; then cmd="gui_dialog --no-cancel"
  124. else local cmd="gui_dialog --cancel-label Back" ; fi
  125. if tmp="$($cmd --inputbox "$headlines" $height $gui_dialog_s70 "$2")"; then
  126. eval "$3='$tmp'"
  127. fi
  128. }
  129. # Use: gui_message "Text"
  130. #
  131. gui_message() {
  132. local headlines="$( echo "$1" | fold -w $gui_dialog_s62 )"
  133. gui_dialog --msgbox "$headlines" \
  134. $(( $( echo "$headlines" | wc -l ) + 5 )) $gui_dialog_s70
  135. }
  136. # Use: gui_yesno "Text"
  137. #
  138. gui_yesno() {
  139. local headlines="$( echo "$1" | fmt -$gui_dialog_s62 )"
  140. gui_dialog --yesno "$headlines" \
  141. $(( $( echo "$headlines" | wc -l ) + 4 )) $gui_dialog_s70
  142. }
  143. # Use: gui_edit "Text" "File"
  144. #
  145. gui_edit() {
  146. # find editor
  147. for x in $EDITOR vi nvi emacs xemacs pico ; do
  148. if type -p $x > /dev/null
  149. then xx=$x ; break ; fi
  150. done
  151. if [ "$xx" ] ; then
  152. eval "$xx $2"
  153. else
  154. gui_message "Cannot find any editor. Make sure \$EDITOR is set."
  155. fi
  156. }
  157. # Use: gui_cmd "Title" "Command"
  158. # (Title isn't used in this GUI type)
  159. gui_cmd() {
  160. shift ; eval "$@"
  161. read -p "Press ENTER to continue."
  162. }