chiark / gitweb /
Mostly style cleanups and commentary.
[rocl] / elite-editor
index 82ac25cb8717230c5a60c1e81b2d04224e9c5997..f367c1c19b84d863e614ff488008257f0fb5e4fc 100755 (executable)
@@ -1,10 +1,10 @@
 #! /usr/bin/wish
 #
-# $Id: elite-editor,v 1.8 2003/03/07 00:43:12 mdw Exp $
+# $Id$
 
 package require "elite" "1.0.1"
 
-# --- Utility procedures ----------------------------------------------------
+#----- Utility procedures ---------------------------------------------------
 
 proc moan {msg} {
   global argv0
@@ -23,8 +23,8 @@ proc debug-array {name} {
     set k [array nextelement a $s]
     label $tl.k-$n -text $k -justify right
     entry $tl.v-$n -textvariable ${name}($k) -state disabled
-    grid configure $tl.k-$n -row $r -column 0 -sticky e
-    grid configure $tl.v-$n -row $r -column 1 -sticky we
+    grid configure $tl.k-$n -row $r -column 0 -sticky e -padx 1 -pady 1
+    grid configure $tl.v-$n -row $r -column 1 -sticky we -padx 1 -pady 1
     incr r
     incr n
   }
@@ -44,7 +44,7 @@ proc get-line {tl title prompt def cmd} {
   }
   toplevel $tl
   wm title $tl $title
-  label $tl.label -text "$prompt: "
+  label $tl.label -text "$prompt:"
   entry $tl.entry; $tl.entry insert 0 $def
   button $tl.ok -text OK -default active \
       -command [list get-line-done $tl $cmd]
@@ -58,9 +58,59 @@ proc entry-on-change {widget what} {
   bind $widget <FocusOut> $what
 }
 
+if {$tk_version >= 8.4} {
+  set entry_readonly readonly
+} else {
+  set entry_readonly disabled
+}
+
+#----- About box ------------------------------------------------------------
+
+proc about-box {} {
+  if {[winfo exists .about]} {
+    # raise .about
+    return
+  }
+  toplevel .about
+  wm title .about "About elite-editor"
+  label .about.rocl -font {helvetica 16 bold} -justify left \
+      -text "Right On Command-Line"
+  label .about.ee -font {helvetica 10 italic} -justify left \
+      -text "elite-editor"
+  label .about.gpl -font {helvetica 8 normal} -justify left -text {
+Copyright (c) 2003 Mark Wooding
+Partly based on code by Ian Bell and Christian Pinder
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software Foundation,
+Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+  }
+  button .about.dismiss -text "Dismiss" -command { destroy .about }
+  pack .about.rocl -padx 4 -side top -anchor w
+  pack .about.ee -padx 4 -side top -anchor e
+  pack .about.gpl -padx 24 -side top -anchor center -expand 1
+  pack .about.dismiss -side top -padx 4 -pady 4 -anchor e
+}
+
+proc help-menu {m} {
+  menu $m.help
+  $m.help add command -label "About..." -command about-box
+  $m add cascade -label "Help" -menu $m.help
+}
+
 #----- Map editing machinery ------------------------------------------------
 
-tab col red orange yellow green blue magenta violet white
+_tab col red orange yellow green blue magenta violet white
 
 set seq 0
 set nwin 0
@@ -102,7 +152,7 @@ proc colour-by {seq} {
   set tl .map-$seq
   global col
   switch -exact -- $map(colourby) {
-    off { 
+    off {
       foreach-world $map(galaxy) p {
        $tl.map itemconfigure $p(seed) -fill white -outline white
       }
@@ -250,7 +300,7 @@ proc hide-path {seq} {
   }
 }
 
-proc path-to-text {seq} { 
+proc path-to-text {seq} {
   upvar \#0 map-$seq map
   set t {}
   foreach n $map(path) {
@@ -290,12 +340,13 @@ proc list-path {seq} {
   $tl.text delete 1.0 end
   $tl.text insert end [path-to-text $seq]
   $tl.text configure -state disabled
-}  
+}
 
 proc load-path {seq} {
   upvar \#0 map-$seq map
   set tl .map-$seq
   set file [tk_getOpenFile -title "Load path"]
+  if {[string equal $file ""]} { return }
   if {[catch {
     set f [open $file]
     set path {}
@@ -324,7 +375,7 @@ proc load-path {seq} {
     $tl.menu.path entryconfigure $i -state normal
   }
   show-path $seq
-}  
+}
 
 proc show-shortest-path {seq weight} {
   upvar \#0 map-$seq map
@@ -354,7 +405,7 @@ proc show-shortest-path {seq weight} {
 # --- Planet information box ---
 
 proc show-worldinfo {tag p} {
-  global economy government
+  global economy government entry_readonly
   upvar \#0 info-$tag info
   set tl .world-info-$tag
   elite-worldinfo info $p
@@ -374,10 +425,10 @@ proc show-worldinfo {tag p} {
       prod-str "Productivity"
       radius-km        "Radius"
     } {
-      label $tl.l-$item -text "$label: " -justify right
-      entry $tl.$item -textvariable info-${tag}($item) -state disabled
-      grid configure $tl.l-$item -row $r -column 0 -sticky e
-      grid configure $tl.$item -row $r -column 1 -columnspan 2 -sticky we
+      label $tl.l-$item -text "$label:" -justify right
+      entry $tl.$item -textvariable info-${tag}($item) -state $entry_readonly
+      grid configure $tl.l-$item -row $r -column 0 -sticky e -padx 1 -pady 1
+      grid configure $tl.$item -row $r -column 1 -columnspan 2 -sticky we -padx 1 -pady 1
       incr r
     }
     scrollbar $tl.descscr -orient vertical -command [list $tl.desc yview]
@@ -513,6 +564,7 @@ proc info-byname {seq name seed proc} {
 
 proc set-selection {seq p} {
   upvar \#0 map-$seq map
+  set tl .map-$seq
   if {[info exists map(cmdr)]} {
     set p [cmdr-set-world $map(cmdr) $p]
   }
@@ -525,7 +577,10 @@ proc set-selection {seq p} {
   } else {
     set-destination $seq $map(dest)
   }
-}  
+  foreach i {5 6 7 8 9} {
+    $tl.menu.path entryconfigure $i -state normal
+  }
+}
 
 proc do-select {seq x y} {
   set-selection $seq [find-click $seq $x $y]
@@ -627,7 +682,7 @@ proc map-set-title {seq} {
 }
 
 proc map-set-galaxy {seq ng g} {
-  upvar \#0 map-$seq map  
+  upvar \#0 map-$seq map
   if {[string equal $g $map(galaxy)]} { return }
   set map(galaxy-num) $ng
   map-set-title $seq
@@ -648,7 +703,7 @@ proc map-set-fuel {seq qty} {
 # --- Making a new map window ---
 
 proc map-new {ng g} {
-  global seq nwin default
+  global seq nwin default entry_readonly
   incr seq
   incr nwin
   upvar \#0 map-$seq map
@@ -669,19 +724,19 @@ proc map-new {ng g} {
       -yscrollcommand [list $tl.vscr set] \
       -width $vwd -height $vht
   frame $tl.info
-  label $tl.info.lhome -text "Home: "
+  label $tl.info.lhome -text "Home:"
   entry $tl.info.home -textvariable map-${seq}(sel-name)
-  label $tl.info.ldest -text "Destination: "
+  label $tl.info.ldest -text " Destination:"
   entry $tl.info.dest -textvariable map-${seq}(dest-name)
-  label $tl.info.ldist -text "Distance: "
+  label $tl.info.ldist -text " Distance:"
   entry $tl.info.dist -textvariable map-${seq}(distance) \
-      -state disabled -width 6
+      -state $entry_readonly -width 6
   pack \
       $tl.info.lhome $tl.info.home \
       $tl.info.ldest $tl.info.dest \
       $tl.info.ldist $tl.info.dist \
-      -side left
-  
+      -side left -pady 2
+
   scrollbar $tl.hscr -orient horizontal \
       -command [list $tl.map xview]
   scrollbar $tl.vscr -orient vertical \
@@ -730,24 +785,25 @@ proc map-new {ng g} {
   $tl.menu.path add command -label "Load path..." \
       -command [list load-path $seq]
   $tl.menu.path add command -label "Save path..." -state disabled \
-      -command [list save-path $seq] 
+      -command [list save-path $seq]
   $tl.menu.path add command -label "List path..." -state disabled \
       -command [list list-path $seq]
   $tl.menu.path add separator
-  $tl.menu.path add command -label "Minimize hops" \
+  $tl.menu.path add command -label "Minimize hops" -state disabled \
       -command [list show-shortest-path $seq weight-hops]
-  $tl.menu.path add command -label "Minimize fuel" \
+  $tl.menu.path add command -label "Minimize fuel" -state disabled \
       -command [list show-shortest-path $seq weight-fuel]
-  $tl.menu.path add command -label "Maximize safety" \
+  $tl.menu.path add command -label "Maximize safety" -state disabled \
       -command [list show-shortest-path $seq weight-safety]
-  $tl.menu.path add command -label "Minimize safety" \
+  $tl.menu.path add command -label "Minimize safety" -state disabled \
       -command [list show-shortest-path $seq weight-encounters]
-  $tl.menu.path add command -label "Maximize trading" \
+  $tl.menu.path add command -label "Maximize trading" -state disabled \
       -command [list show-shortest-path $seq weight-trading]
   $tl.menu.path add separator
   $tl.menu.path add command -label "Hide path" -state disabled \
       -command [list hide-path $seq]
   $tl.menu add cascade -label "Path" -menu $tl.menu.path
+  help-menu $tl.menu
   $tl configure -menu $tl.menu
 
   wm protocol $tl WM_DELETE_WINDOW [list map-destroy $seq]
@@ -801,7 +857,7 @@ proc integerp {min max n} {
 proc galaxyp {s} {
   if {![regexp {^[0-9a-fA-F]{12}$} $s]} { return 0 }
   return 1
-}  
+}
 
 proc cmdr-do-validate {seq widget check value} {
   upvar \#0 cmdr-$seq cmdr
@@ -838,6 +894,7 @@ proc cmdr-set-fluc {seq} {
 }
 
 proc cmdr-cargo {seq} {
+  global entry_readonly
   upvar \#0 cmdr-$seq cmdr
   set tl .cmdr-$seq.cargo-qty
   if {[winfo exists $tl]} {
@@ -848,34 +905,34 @@ proc cmdr-cargo {seq} {
   wm title $tl "Cargo for commander $cmdr(name)"
   global products
   set r 0
-  label $tl.l-fluc -text "Fluctuation: " -justify right
+  label $tl.l-fluc -text "Fluctuation:" -justify right
   entry $tl.fluc -textvariable cmdr-${seq}(market-fluc) -justify right
   cmdr-validate-widget $seq $tl.fluc [list integerp 0 255]
   entry-on-change $tl.fluc [list cmdr-set-fluc $seq]
-  grid configure $tl.l-fluc -row $r -column 0 -sticky e
-  grid configure $tl.fluc -row $r -column 1 -columnspan 3 -sticky we
+  grid configure $tl.l-fluc -row $r -column 0 -sticky e -padx 1 -pady 1
+  grid configure $tl.fluc -row $r -column 1 -columnspan 3 -sticky we -padx 1 -pady 1
   incr r
   label $tl.l-item -text "Item" -justify center
   label $tl.l-price -text "Price" -justify center
   label $tl.l-station -text "Station" -justify center
   label $tl.l-hold -text "Hold" -justify center
-  grid configure $tl.l-item -row $r -column 0 -sticky e
-  grid configure $tl.l-price -row $r -column 1 -sticky we
-  grid configure $tl.l-station -row $r -column 2 -sticky we
-  grid configure $tl.l-hold -row $r -column 3 -sticky we
+  grid configure $tl.l-item -row $r -column 0 -sticky e -padx 1 -pady 1
+  grid configure $tl.l-price -row $r -column 1 -sticky we -padx 1 -pady 1
+  grid configure $tl.l-station -row $r -column 2 -sticky we -padx 1 -pady 1
+  grid configure $tl.l-hold -row $r -column 3 -sticky we -padx 1 -pady 1
   incr r
   foreach {tag label} $products {
-    label $tl.l-$tag -text "$label: " -justify right
+    label $tl.l-$tag -text "$label:" -justify right
     entry $tl.price-$tag -textvariable cmdr-${seq}(price-${tag}) \
-       -justify right -state disabled -width 4
+       -justify right -state $entry_readonly -width 4
     foreach {pre col} {station 2 hold 3} {
       entry $tl.${pre}-${tag} -textvariable cmdr-${seq}(${pre}-${tag}) \
          -justify right -width 4
       cmdr-validate-widget $seq $tl.${pre}-${tag} [list integerp 0 255]
       grid configure $tl.${pre}-${tag} -row $r -column $col -stick we
     }
-    grid configure $tl.l-$tag -row $r -column 0 -sticky e
-    grid configure $tl.price-$tag -row $r -column 1 -sticky we
+    grid configure $tl.l-$tag -row $r -column 0 -sticky e -padx 1 -pady 1
+    grid configure $tl.price-$tag -row $r -column 1 -sticky we -padx 1 -pady 1
     incr r
   }
   grid columnconfigure $tl 1 -weight 1
@@ -896,6 +953,17 @@ proc cmdr-destroy {seq} {
   if {!$nwin} { exit }
 }
 
+proc cmdr-new-map {seq} {
+  upvar \#0 cmdr-$seq cmdr
+  set tl .cmdr-$seq
+  if {$cmdr(std-gal)} {
+    set g $cmdr(gal-number)
+  } else {
+    set g $cmdr(gal-seed)
+  }
+  get-line .new-view "New map..." "Galaxy" $g new-view
+}
+
 proc cmdrdb-set {seq tag value} {
   upvar \#0 cmdr-$seq cmdr
   set tl .cmdr-$seq
@@ -1024,7 +1092,7 @@ proc cmdr-check {seq} {
 
 proc cmdr-open {seq} {
   upvar \#0 cmdr-$seq cmdr
-  global cmdr-$seq
+  global cmdr-$seq entry_readonly
   set tl .cmdr-$seq
   global nwin
   toplevel $tl
@@ -1040,16 +1108,16 @@ proc cmdr-open {seq} {
   set cmdr(bogus) 0
   foreach {tag label kind} [list \
     mission            "Mission"               { entry 2 255 } \
-    score              "Rating"                { dropbox 65535\
-                                                 "Harmless"            0 \
-                                                 "Mostly harmless"     8 \
+    score              "Rating"                { dropbox 65535 \
+                                                 "Harmless"            0 \
+                                                 "Mostly harmless"     8 \
                                                  "Poor"               16 \
-                                                 "Average"            32 \
+                                                 "Average"            32 \
                                                  "Above average"      64 \
-                                                 "Competent"         128 \
-                                                 "Dangerous"         512 \
-                                                 "Deadly"           2560 \
-                                                 "Elite"            6400 } \
+                                                 "Competent"         128 \
+                                                 "Dangerous"         512 \
+                                                 "Deadly"           2560 \
+                                                 "Elite"            6400 } \
     legal-status       "Legal status"          { dropbox 255 \
                                                  "Clean"       0 \
                                                  "Offender"    1 \
@@ -1059,11 +1127,11 @@ proc cmdr-open {seq} {
     fuel               "Fuel"                  { tenth 4 25.5 } \
     missiles           "Missiles"              { entry 4 255 } \
     energy-unit                "Energy unit"           { dropbox 255 \
-                                                 "None"        0 \
+                                                 "None"        0 \
                                                  "Standard"    1 \
                                                  "Naval"       2 } \
     front-laser                "Front laser"           $laser \
-    rear-laser         "Front laser"           $laser \
+    rear-laser         "Rear laser"            $laser \
     left-laser         "Left laser"            $laser \
     right-laser                "Right laser"           $laser \
     ecm                        "ECM"                   toggle \
@@ -1078,30 +1146,30 @@ proc cmdr-open {seq} {
     switch -exact -- [lindex $kind 0] {
       entry {
        destructure {. wd max} $kind
-       label $tl.l-$tag -text "$label: " -justify right
+       label $tl.l-$tag -text "$label:" -justify right
        entry $tl.$tag -textvariable cmdr-${seq}($tag) \
            -width $wd -justify right
        cmdr-validate-widget $seq $tl.$tag [list integerp 0 $max]
-       grid configure $tl.l-$tag -row $r -column 0 -sticky e
-       grid configure $tl.$tag -row $r -column 1 -columnspan 2 -sticky we
+       grid configure $tl.l-$tag -row $r -column 0 -sticky e -padx 1 -pady 1
+       grid configure $tl.$tag -row $r -column 1 -columnspan 2 -sticky we -padx 1 -pady 1
       }
       tenth {
        destructure {. wd max} $kind
-       label $tl.l-$tag -text "$label: " -justify right
+       label $tl.l-$tag -text "$label:" -justify right
        entry $tl.$tag -textvariable cmdr-${seq}(div-$tag) \
            -width $wd -justify right
        set cmdr(div-$tag) [format "%.1f" [expr {$cmdr($tag) / 10.0}]]
        trace variable cmdr-${seq}(div-$tag) w [list fix-tenth $tag]
        cmdr-validate-widget $seq $tl.$tag [list numericp 0 $max]
-       grid configure $tl.l-$tag -row $r -column 0 -sticky e
-       grid configure $tl.$tag -row $r -column 1 -columnspan 2 -sticky we
+       grid configure $tl.l-$tag -row $r -column 0 -sticky e -padx 1 -pady 1
+       grid configure $tl.$tag -row $r -column 1 -columnspan 2 -sticky we -padx 1 -pady 1
       }
       toggle {
        checkbutton $tl.$tag -text $label -variable cmdr-${seq}($tag)
-       grid configure $tl.$tag -row $r -column 0 -columnspan 3 -sticky w
+       grid configure $tl.$tag -row $r -column 0 -columnspan 3 -sticky w -padx 1 -pady 1
       }
       dropbox {
-       label $tl.l-$tag -text "$label: " -justify right
+       label $tl.l-$tag -text "$label:" -justify right
        set menu $tl.m-$tag.menu
        menubutton $tl.m-$tag -textvariable cmdr-${seq}(r-${tag}) \
            -indicatoron 1 -relief raised -menu $menu -width 8 \
@@ -1124,34 +1192,34 @@ proc cmdr-open {seq} {
        $menu add radiobutton -label "Custom" \
            -value "Custom" -variable cmdr-${seq}(r-$tag) \
            -command [list cmdrdb-custom $seq $tag]
-       grid configure $tl.l-$tag -row $r -column 0 -sticky e
-       grid configure $tl.m-$tag -row $r -column 1 -sticky we
-       grid configure $tl.$tag -row $r -column 2 -sticky we
+       grid configure $tl.l-$tag -row $r -column 0 -sticky e -padx 1 -pady 1
+       grid configure $tl.m-$tag -row $r -column 1 -sticky we -padx 1 -pady 1
+       grid configure $tl.$tag -row $r -column 2 -sticky we -padx 1 -pady 1
       }
       cargo {
        button $tl.$tag -text $label -command [list cmdr-cargo $seq]
-       grid configure $tl.$tag -row $r -column 0 -columnspan 3 -sticky we
+       grid configure $tl.$tag -row $r -column 0 -columnspan 3 -sticky we -padx 1 -pady 1
       }
       where {
-       label $tl.l-gal-number -text "Galaxy number: " -justify right
+       label $tl.l-gal-number -text "Galaxy number:" -justify right
        entry $tl.gal-number -textvariable cmdr-${seq}(gal-number) \
            -justify right -width 2
        cmdr-validate-widget $seq $tl.gal-number [list integerp 1 8]
        checkbutton $tl.std-gal -text "Standard galaxy" \
            -variable cmdr-${seq}(std-gal) -justify left \
-           -command [list cmdr-std-gal $seq]     
+           -command [list cmdr-std-gal $seq]
        entry-on-change $tl.gal-number [list cmdr-set-gal-num $seq]
-       grid configure $tl.l-gal-number -row $r -column 0 -sticky e
-       grid configure $tl.std-gal -row $r -column 1 -sticky w
-       grid configure $tl.gal-number -row $r -column 2 -sticky we
+       grid configure $tl.l-gal-number -row $r -column 0 -sticky e -padx 1 -pady 1
+       grid configure $tl.std-gal -row $r -column 1 -sticky w -padx 1 -pady 1
+       grid configure $tl.gal-number -row $r -column 2 -sticky we -padx 1 -pady 1
        incr r
-       label $tl.l-gal-seed -text "Galaxy seed: " -justify right
+       label $tl.l-gal-seed -text "Galaxy seed:" -justify right
        entry $tl.gal-seed -textvariable cmdr-${seq}(gal-seed) -width 12
        cmdr-validate-widget $seq $tl.gal-seed galaxyp
        entry-on-change $tl.gal-seed [list cmdr-update-world $seq]
-       grid configure $tl.l-gal-seed -row $r -column 0 -sticky e
+       grid configure $tl.l-gal-seed -row $r -column 0 -sticky e -padx 1 -pady 1
        grid configure $tl.gal-seed -row $r \
-           -column 1 -columnspan 2 -sticky we
+           -column 1 -columnspan 2 -sticky we -padx 1 -pady 1
        incr r
        if {[string equal $cmdr(gal-seed) [galaxy $cmdr(gal-number)]]} {
          set cmdr(std-gal) 1
@@ -1159,20 +1227,20 @@ proc cmdr-open {seq} {
        } else {
          set cmdr(std-gal) 0
        }
-       label $tl.l-world-name -text "Planet: " -justify right
+       label $tl.l-world-name -text "Planet:" -justify right
        entry $tl.world-name -textvariable cmdr-${seq}(world-name) \
-           -state disabled -width 10 -justify left
-       grid configure $tl.l-world-name -row $r -column 0 -sticky e
+           -state $entry_readonly -width 10 -justify left
+       grid configure $tl.l-world-name -row $r -column 0 -sticky e -padx 1 -pady 1
        grid configure $tl.world-name -row $r \
-           -column 1 -columnspan 2 -sticky we
+           -column 1 -columnspan 2 -sticky we -padx 1 -pady 1
        incr r
        button $tl.$tag -text "Show galaxy map" \
            -command [list cmdr-show-map $seq]
-       grid configure $tl.$tag -row $r -column 0 -columnspan 3 -sticky we
+       grid configure $tl.$tag -row $r -column 0 -columnspan 3 -sticky we -padx 1 -pady 1
       }
       default {
        label $tl.l-$tag -text "($label)" -justify left
-       grid configure $tl.l-$tag -row $r -column 0 -sticky w
+       grid configure $tl.l-$tag -row $r -column 0 -sticky w -padx 1 -pady 1
       }
     }
     incr r
@@ -1188,9 +1256,13 @@ proc cmdr-open {seq} {
   $tl.menu.file add command -label "Save as..." \
       -command [list cmdr-saveas $seq]
   $tl.menu.file add separator
+  $tl.menu.file add command -label "New map..." \
+      -command [list cmdr-new-map $seq]
+  $tl.menu.file add separator
   $tl.menu.file add command -label "Close" -command [list cmdr-destroy $seq]
   $tl.menu.file add command -label "Quit" -command { exit }
   $tl.menu add cascade -label "File" -menu $tl.menu.file
+  help-menu $tl.menu
   $tl configure -menu $tl.menu
   grid columnconfigure $tl 2 -weight 1
   wm protocol $tl WM_DELETE_WINDOW [list cmdr-destroy $seq]