# Cupcake editor preferences dialog module.
# Rick Smereka, Copyright (C) 2004-2005.

# 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, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com

# Initial version Nov/2004, Rick Smereka
global prefsda

proc prefsdialog {} {
   # Display save/restore preferences dialog, get
   # prefrences and save them into the 'prefs'
   # array.
   global prefs prefsda SR_NO SR_LAST SR_SPEC
   global pd_close

   # check for already existing prefs window open
   if {[winfo exists .prefsdialog]} {
      focus .prefsdialog
      return
   }

   set pd_close -1
   init_prefsda

   # create dialog window
   set dlg .prefsdialog
   toplevel $dlg
   wm protocol $dlg WM_DELETE_WINDOW "set pd_close 0"
   wm title $dlg "Preferences"

   # window position and size
   set fr $dlg.winps
   frame $fr -bd 5 -relief groove
   label $fr.lab -text "Window Position and Size" -anchor w
   checkbutton $fr.winpos -text "Position on screen" \
      -variable prefsda(srwinpos) -anchor w
   checkbutton $fr.winsize -text "Size on screen" \
      -variable prefsda(srwinsize) -anchor w
   pack $fr.lab -fill x
   pack $fr.winsize $fr.winpos -fill x -side top
   pack $fr -fill x

   # open/save directory
   set fr $dlg.opsa
   frame $fr -bd 5 -relief groove
   label $fr.lab -text "Open/Save Directory" -anchor w
   radiobutton $fr.last -text "Last accessed directory" \
      -variable prefsda(sropensave) -value $SR_LAST -anchor w \
      -command "$fr.spec.set configure -state disabled"
   pack $fr.lab $fr.last -fill x -side top
   frame $fr.spec -bd 0
   radiobutton $fr.spec.srad -text "Specific directory" \
      -variable prefsda(sropensave) -value $SR_SPEC -anchor w \
      -command "$fr.spec.set configure -state normal"

   if {$prefsda(sropensave) == $SR_SPEC} {
      button $fr.spec.set -text "Set" -command "set_dir"
   } else {
      button $fr.spec.set -text "Set" -state disabled \
         -command "set_dir"
   }

   pack $fr.spec.srad $fr.spec.set -fill x -side left
   pack $fr.spec -fill x
   radiobutton $fr.no -text "Don't retain" \
      -variable prefsda(sropensave) -value $SR_NO -anchor w \
      -command "$fr.spec.set configure -state disabled"
   pack $fr.no -fill x -side top
   pack $fr -fill x

   # font
   set fr $dlg.font
   frame $fr -bd 5 -relief groove
   label $fr.lab -text "Font" -anchor w
   radiobutton $fr.last -text "Current font" \
      -variable prefsda(srfont) -value $SR_LAST -anchor w \
      -command "$fr.spec.set configure -state disabled"
   pack $fr.lab $fr.last -fill x -side top
   frame $fr.spec -bd 0
   radiobutton $fr.spec.srad -text "Specific font" \
      -variable prefsda(srfont) -value $SR_SPEC -anchor w \
      -command "$fr.spec.set configure -state normal"

   if {$prefsda(srfont) == $SR_SPEC} {
      button $fr.spec.set -text "Set" -command "fontdialog editfont"
   } else {
      button $fr.spec.set -text "Set" -state disabled \
         -command "fontdialog editfont"
   }

   pack $fr.spec.srad $fr.spec.set -fill x -side left
   pack $fr.spec -fill x
   radiobutton $fr.no -text "Don't retain" \
      -variable prefsda(srfont) -value $SR_NO -anchor w \
      -command "$fr.spec.set configure -state disabled"
   pack $fr.no -fill x -side top
   pack $fr -fill x

   # misc
   set fr $dlg.misc
   frame $fr -bd 0
   checkbutton $fr.ww -text "Word wrap" \
      -variable prefsda(srwordwrap) -anchor w
   checkbutton $fr.status -text "Status line" \
      -variable prefsda(srstatusline) -anchor w
   pack $fr.ww $fr.status -fill x -side left
   pack $fr -fill x

   # buttons
   set fr $dlg.buttons
   frame $fr -bd 0
   button $fr.ok -text "Ok" -command "set pd_close 1" -default active
   bind all <Key-Return> "set pd_close 1"
   button $fr.cancel -text "Cancel" -command "set pd_close 0"
   pack $fr.ok $fr.cancel -fill x -side left -expand true
   pack $fr -side left -fill x -expand true

   focus $dlg
   update

   # make this a model dialog
   tkwait variable pd_close

   # remove dialog
   destroy $dlg

   # refresh 'prefs' array on 'ok'
   if {$pd_close} {
      set_prefs
   }
}

proc init_prefsda {} {
   # Load the 'prefsda' array from the 'prefs' array.
   global prefs prefsda

   set prefsda(srwinpos) $prefs(srwinpos)
   set prefsda(srwinsize) $prefs(srwinsize)
   set prefsda(sropensave) $prefs(sropensave)
   set prefsda(srfont) $prefs(srfont)
   set prefsda(srwordwrap) $prefs(srwordwrap)
   set prefsda(srstatusline) $prefs(srstatusline)
}

proc set_prefs {} {
   # Load the 'prefs' array from the 'prefsda' array.
   global prefs prefsda

   set prefs(srwinpos) $prefsda(srwinpos)
   set prefs(srwinsize) $prefsda(srwinsize)
   set prefs(sropensave) $prefsda(sropensave)
   set prefs(srfont) $prefsda(srfont)
   set prefs(srwordwrap) $prefsda(srwordwrap)
   set prefs(srstatusline) $prefsda(srstatusline)
}

proc set_dir {} {
   # Set a specific directory for open/save of files.
   global prefs

   set ret [pathdlg $prefs(lastdirectory)]

   if {$ret != ""} {
      set prefs(lastdirectory) $ret
   }
}
