#!/usr/bin/tclsh
# =======
# LICENSE
# =======
# This program can be used and modified freely and you are welcome to
# redistribute it under the following terms:
# 
# - 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.
# - Any derived works should be also distributed freely under an all-permisive
# license. Share the spirit of open source.
#
# Version 0.1 by puppy forum member "gray" 28-Aug-07
#
package require Gnocl

#############
# PROCEDURES
#############

proc funcchoice {} {
 global CURRDPI

  if { [catch {exec cat /root/.Xresources | grep -v "Xft\\.dpi:" }  TMP1] } {
      exit
  }
  catch { exec echo $TMP1 > /root/.Xresources }
  catch { exec echo "Xft.dpi: $CURRDPI" >> /root/.Xresources }
  catch { exec sync }
  exit
}

proc getDPI {widget path} {
 global CURRDPI
   set CURRDPI [$widget get $path 0]
}

#############
# MAIN CODE
#############

if { [catch {exec cat /root/.Xresources | grep "Xft\\.dpi:" | tr -s ' ' | cut -f 2 -d " "}  CURRDPI] } {
    set CURRDPI 78
}
set CURRINDEX [expr $CURRDPI / 6 -8]

#set up GUI elements
set mainBox [gnocl::box -orientation vertical -borderWidth small -padding small]
set cbox [gnocl::box -orientation horizontal -borderWidth small -padding small]
set butbox [gnocl::box -orientation horizontal -buttonType true -layout end -spacing big -borderWidth small]

set finfo [gnocl::text -editable 0 -scrollbar never -cursorVisible 0 -sensitive 0]
set BLURB "画面用の dpi (dots per inch) を選択して下さい。\
\n数値を大きくすると、アンチエイリアスフォントは画面上で大きく\
\n表示されます。フォントが画面上で小さすぎたり大きすぎて表示\
\nされる時は、上下にスライドさせて調整して下さい。\
\n\n注意１: アンチエイリアスレンダリングを使っていないためこの\
\n            設定が有効にならないアプリケーションもあります。\
\n\n注意２: 変更はXの再スタート後に反映されます。"
$finfo insert end $BLURB

set dpilist [gnocl::list -titles {"サイズ"} \
      -scrollbar {never always} -selectionMode single \
      -types {string}  -headersClickable false\
      -onSelectionChanged "getDPI %w %p" ]

$dpilist add [list 48 54 60 66 72 78 84 90 96 102 108]
$dpilist columnConfigure 0 -titleAlign center -align center 
$dpilist setSelection $CURRINDEX
$dpilist scrollToPosition -path $CURRINDEX

$cbox add $finfo -fill 1 -expand 1 
$cbox add $dpilist -fill 1 -expand 1
$mainBox add $cbox
$mainBox add $butbox

$butbox add [gnocl::button -text "%#Cancel" -onClicked exit -widthGroup "1"] 
$butbox add [gnocl::button -text "%#Ok" -onClicked funcchoice -widthGroup "1"] 

# activate (realize) GUI
gnocl::window -title "DPIの設定" -child $mainBox \
          -onDestroy exit

# enter gtk main loop
gnocl::mainLoop
