gis_set.tcl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. #############################################################################
  2. #
  3. # MODULE: Grass Tcl/Tk Initialization
  4. # AUTHOR(S): Original author unknown - probably CERL
  5. # Justin Hickey - Thailand - jhickey hpcc.nectec.or.th
  6. # Markus Neteler - Germany - neteler geog.uni-hannover.de, itc.it
  7. # Michael Barton - USA - Arizona State University
  8. # Maris Nartiss - Latvia - maris.gis gmail.com
  9. # PURPOSE: The source file for this shell script is in
  10. # src/tcltkgrass/main/gis_set.tcl. It allows the user to choose
  11. # the database, location, and mapset to use with grass by
  12. # presenting a user interface window.
  13. # COPYRIGHT: (C) 2000,2006 by the GRASS Development Team
  14. #
  15. # This program is free software under the GNU General Public
  16. # License (>=v2). Read the file COPYING that comes with GRASS
  17. # for details.
  18. #
  19. #############################################################################
  20. if {[info exists env(OS)] && $env(OS) == "Windows_NT"} {
  21. set mingw "1"
  22. } else {
  23. set mingw "0"
  24. }
  25. source $env(GISBASE)/etc/gtcltk/gmsg.tcl
  26. source $env(GISBASE)/etc/gtcltk/options.tcl
  27. source $env(GISBASE)/etc/epsg_option.tcl
  28. source $env(GISBASE)/etc/file_option.tcl
  29. #fetch GRASS Version number:
  30. set fp [open $env(GISBASE)/etc/VERSIONNUMBER r]
  31. set GRASSVERSION [read -nonewline $fp]
  32. close $fp
  33. #############################################################################
  34. proc searchGISRC { filename } {
  35. global database
  36. global location
  37. global mapset
  38. global oldDb
  39. global oldLoc
  40. global oldMap
  41. global grassrc_list
  42. set grassrc_list ""
  43. set flag 0
  44. if { [file exists $filename] } {
  45. set ifp [open $filename "r"]
  46. set thisline [gets $ifp]
  47. while { [eof $ifp] == 0 } {
  48. lappend grassrc_list "$thisline"
  49. if { [regexp -- {^GISDBASE: *(.*)$} $thisline dummy env_database] } {
  50. set database $env_database
  51. }
  52. if { [scan $thisline "LOCATION_NAME: %s" env_location] } {
  53. set location $env_location
  54. }
  55. if { [scan $thisline "MAPSET: %s" env_mapset] } {
  56. set mapset $env_mapset
  57. }
  58. set thisline [gets $ifp]
  59. }
  60. set oldDb $database
  61. set oldLoc $location
  62. set oldMap $mapset
  63. close $ifp
  64. if { $database != "" && $location != "" && $mapset != "" } {
  65. set flag 1
  66. }
  67. }
  68. return $flag
  69. }
  70. #############################################################################
  71. proc putGRASSRC { filename } {
  72. # create grassrc file with new values
  73. global database
  74. global location
  75. global mapset
  76. global grassrc_list
  77. set ofp [open $filename "w"]
  78. foreach i $grassrc_list {
  79. if { [regexp {^GISDBASE:} $i] } {
  80. puts $ofp "GISDBASE: $database"
  81. } elseif { [regexp {^LOCATION_NAME:} $i] } {
  82. puts $ofp "LOCATION_NAME: $location"
  83. } elseif { [regexp {^MAPSET:} $i] } {
  84. puts $ofp "MAPSET: $mapset"
  85. } else {
  86. puts $ofp $i
  87. }
  88. }
  89. if { [ catch { close $ofp } error ] } {
  90. DialogGen .wrnDlg [G_msg "WARNING: can not save"] warning \
  91. [format [G_msg "Warning: unable to save data to <%s> file.\nError message: %s"] \
  92. $filename $error] \
  93. 0 OK;
  94. }
  95. }
  96. #############################################################################
  97. proc CheckLocation {} {
  98. # Returns 0, if location is invalid, 1 othervise.
  99. global database location
  100. set found 0
  101. set dir $database
  102. append dir "/$location"
  103. set currDir [pwd]
  104. # Special case - wrong GISDBASE
  105. if {[file isdirectory $dir] == 0} {
  106. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  107. [format [G_msg "Warning: location <%s> at GISDBASE <%s> is not a directory or does not exist."] \
  108. $location $database] \
  109. 0 OK;
  110. .frame0.frameMS.listbox delete 0 end
  111. .frame0.frameNMS.second.entry configure -state disabled
  112. .frame0.frameBUTTONS.ok configure -state disabled
  113. } else {
  114. cdir $dir
  115. .frame0.frameNMS.second.entry configure -state disabled
  116. if {[file isdirectory "PERMANENT"] && [file exists "$dir/PERMANENT/DEFAULT_WIND"]} {
  117. set found 1
  118. .frame0.frameNMS.second.entry configure -state normal
  119. }
  120. }
  121. cdir $currDir
  122. return $found
  123. }
  124. proc CheckMapset {} {
  125. global database location mapset
  126. if { $mapset == "" } { return 0; }
  127. if { [file exists "$database/$location/$mapset/WIND"] } {
  128. return 1
  129. }
  130. return 0
  131. }
  132. #############################################################################
  133. proc gisSetWindow {} {
  134. # create main GRASS startup panel
  135. global GRASSVERSION
  136. global database
  137. global location
  138. global mymapset
  139. global mapset
  140. global oldDb oldLoc oldMap
  141. global env
  142. global grassrc_list
  143. global gisrc_name
  144. global refresh
  145. set refresh 0
  146. global mingw
  147. # Window manager configurations
  148. wm title . [format [G_msg "GRASS %s Startup"] $GRASSVERSION]
  149. # ---------------------------
  150. # build .frame0 with panel title
  151. # ---------------------------
  152. set mainfr [frame .frame0 \
  153. -borderwidth {2} \
  154. -relief {raised}]
  155. set titlefrm [frame .frame0.intro -borderwidth 2 ]
  156. set introimg [label $titlefrm.img -image [image create photo -file \
  157. "$env(GISBASE)/etc/gintro.gif"]]
  158. set introtitle [text $titlefrm.msg -height 5 \
  159. -relief flat -fg darkgreen \
  160. -bg #dddddd \
  161. -font introfont \
  162. -width 50 ]
  163. pack $titlefrm -side top
  164. pack $introimg -side top
  165. pack $introtitle -side top
  166. .frame0.intro.msg tag configure all -justify center
  167. .frame0.intro.msg insert end [G_msg "Welcome to GRASS GIS Version"]
  168. .frame0.intro.msg insert end [G_msg " $GRASSVERSION\n"]
  169. .frame0.intro.msg insert end [G_msg "The world's leading open source GIS\n\n"]
  170. .frame0.intro.msg insert end [G_msg "Select an existing project location and mapset\n"]
  171. .frame0.intro.msg insert end [G_msg "or define a new location\n"]
  172. .frame0.intro.msg tag add all 1.0 end
  173. .frame0.intro.msg configure -state disabled
  174. # -----------------------------------
  175. # build .frame0.frameDB - panel top section for database selection
  176. # -----------------------------------
  177. frame .frame0.frameDB \
  178. -borderwidth {2}
  179. frame .frame0.frameDB.left \
  180. -borderwidth {2}
  181. frame .frame0.frameDB.mid \
  182. -borderwidth {2}
  183. frame .frame0.frameDB.right \
  184. -borderwidth {2}
  185. label .frame0.frameDB.left.label \
  186. -justify right \
  187. -wraplength 200 \
  188. -text [G_msg "GIS Data Directory: "]
  189. entry .frame0.frameDB.mid.entry \
  190. -relief {sunken} \
  191. -textvariable database \
  192. -width 40 \
  193. -xscrollcommand { .frame0.frameDB.mid.hscrollbar set}
  194. scrollbar .frame0.frameDB.mid.hscrollbar \
  195. -command { .frame0.frameDB.mid.entry xview} \
  196. -relief {sunken} \
  197. -orient {horizontal}
  198. button .frame0.frameDB.right.button \
  199. -text [G_msg "Browse..."] -padx 10 -bd 1 \
  200. -command { set tmp [tk_chooseDirectory -initialdir $database \
  201. -parent .frame0 -title [G_msg "New GIS data directory"] -mustexist true]
  202. if {$tmp != ""} {
  203. set database $tmp
  204. refresh_loc
  205. .frame0.frameBUTTONS.ok configure -state disabled }
  206. }
  207. pack .frame0.frameDB.left.label -side top
  208. pack .frame0.frameDB.mid.entry -side top
  209. pack .frame0.frameDB.mid.hscrollbar -side bottom -fill x
  210. pack .frame0.frameDB.right.button -side left
  211. pack .frame0.frameDB.left -side left -anchor n
  212. pack .frame0.frameDB.mid -side left -anchor n
  213. pack .frame0.frameDB.right -side left -anchor n -padx 10
  214. # -----------------------------------
  215. # build .frame0.frameLOC - middle, left section for location selection listbox
  216. # -----------------------------------
  217. frame .frame0.frameLOC \
  218. -borderwidth {2}
  219. label .frame0.frameLOC.label \
  220. -wraplength 170 \
  221. -text [G_msg "Project Location (projection/coordinate system)"]
  222. listbox .frame0.frameLOC.listbox \
  223. -relief {sunken} \
  224. -exportselection false \
  225. -yscrollcommand {.frame0.frameLOC.vscrollbar set} \
  226. -xscrollcommand {.frame0.frameLOC.hscrollbar set} \
  227. -selectmode single
  228. scrollbar .frame0.frameLOC.vscrollbar \
  229. -command {.frame0.frameLOC.listbox yview} \
  230. -relief {sunken}
  231. scrollbar .frame0.frameLOC.hscrollbar \
  232. -command {.frame0.frameLOC.listbox xview} \
  233. -orient {horizontal} \
  234. -relief {sunken}
  235. pack append .frame0.frameLOC \
  236. .frame0.frameLOC.label { top fill } \
  237. .frame0.frameLOC.vscrollbar { right filly } \
  238. .frame0.frameLOC.hscrollbar { bottom fillx } \
  239. .frame0.frameLOC.listbox { left expand fill }
  240. # -----------------------------------
  241. # build .frame0.frameMS - middle, right section for mapset selection listbox
  242. # -----------------------------------
  243. frame .frame0.frameMS \
  244. -borderwidth {2}
  245. label .frame0.frameMS.label \
  246. -wraplength 170 \
  247. -text [G_msg "Accessible Mapsets (directories of GIS files)"]
  248. listbox .frame0.frameMS.listbox \
  249. -relief {sunken} \
  250. -exportselection false \
  251. -yscrollcommand {.frame0.frameMS.vscrollbar set} \
  252. -xscrollcommand {.frame0.frameMS.hscrollbar set} \
  253. -selectmode single
  254. scrollbar .frame0.frameMS.vscrollbar \
  255. -command {.frame0.frameMS.listbox yview} \
  256. -relief {sunken}
  257. scrollbar .frame0.frameMS.hscrollbar \
  258. -command {.frame0.frameMS.listbox xview} \
  259. -orient {horizontal} \
  260. -relief {sunken}
  261. pack append .frame0.frameMS \
  262. .frame0.frameMS.label { top fill } \
  263. .frame0.frameMS.vscrollbar { right filly } \
  264. .frame0.frameMS.hscrollbar { bottom fillx } \
  265. .frame0.frameMS.listbox { left expand fill }
  266. # -----------------------------------
  267. # build .frame0.frameNMS - middle far right section with buttons for
  268. # creating new mapset and location
  269. # -----------------------------------
  270. frame .frame0.frameNMS \
  271. -borderwidth {2}
  272. frame .frame0.frameNMS.first \
  273. -borderwidth {2}
  274. frame .frame0.frameNMS.second \
  275. -borderwidth {2}
  276. frame .frame0.frameNMS.third \
  277. -borderwidth {2}
  278. frame .frame0.frameNMS.spacer \
  279. -borderwidth {2} -height {10}
  280. frame .frame0.frameNMS.fourth \
  281. -borderwidth {2}
  282. frame .frame0.frameNMS.fifth \
  283. -borderwidth {2}
  284. frame .frame0.frameNMS.sixth \
  285. -borderwidth {2}
  286. frame .frame0.frameNMS.seventh \
  287. -borderwidth {2}
  288. label .frame0.frameNMS.first.label \
  289. -wraplength 200 \
  290. -text [G_msg "Create new mapset in selected location"]
  291. entry .frame0.frameNMS.second.entry \
  292. -relief {sunken} \
  293. -textvariable mymapset \
  294. -width 22
  295. button .frame0.frameNMS.third.button \
  296. -text [G_msg "Create new mapset"] \
  297. -padx 10 -bd 1 -wraplength 150 \
  298. -command {
  299. set mymapset [ string trim $mymapset ]
  300. if { [file exists $mymapset] } {
  301. DialogGen .wrnDlg [G_msg "WARNING: invalid mapset name"] warning \
  302. [format [G_msg "Warning: Mapset with name <%s> already exists. \nNew mapset is NOT created. \nChoose different mapset name and try again."] $mymapset] \
  303. 0 OK;
  304. return
  305. }
  306. .frame0.frameNMS.third.button configure -state disabled
  307. if { $mymapset != "" } {
  308. if {[CheckLocation] == 0} {
  309. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  310. [format [G_msg "Warning: selected location <%s> is not valid. \n New mapset is NOT created. \n Select valid location and try again."] $location] \
  311. 0 OK;
  312. set mapset ""
  313. } else {
  314. cdir $database
  315. cdir $location
  316. if { [ catch { file mkdir $mymapset } error ] } {
  317. DialogGen .wrnDlg [G_msg "WARNING: unable to mkdir"] warning \
  318. [format [G_msg "Warning: Unable to create directory for new mapset. \nError message: %s"] $error] \
  319. 0 OK;
  320. } else {
  321. #generate default DB definition, create dbf subdirectory:
  322. set varfp [open $mymapset/VAR "w"]
  323. puts $varfp "DB_DRIVER: sqlite"
  324. puts $varfp "DB_DATABASE: \$GISDBASE/\$LOCATION_NAME/\$MAPSET/sqlite.db"
  325. close $varfp
  326. catch {file attributes $mymapset/VAR -permissions u+rw,go+r}
  327. # file mkdir $mymapset/dbf
  328. #copy over the WIND definition:
  329. catch {file copy $mymapset/../PERMANENT/DEFAULT_WIND $mymapset/WIND}
  330. catch {file attributes $mymapset/WIND -permissions u+rw,go+r}
  331. .frame0.frameMS.listbox insert end $mymapset
  332. selFromList .frame0.frameMS.listbox $mymapset
  333. set mapset $mymapset
  334. .frame0.frameNMS.second.entry delete 0 end
  335. .frame0.frameBUTTONS.ok configure -state normal
  336. }
  337. }
  338. }
  339. }
  340. label .frame0.frameNMS.fourth.label \
  341. -wraplength 200 \
  342. -text [G_msg "Define new location with..."]
  343. button .frame0.frameNMS.fifth.button \
  344. -text [G_msg "Georeferenced file"] \
  345. -width 22 -bd 1 -wraplength 150\
  346. -relief raised \
  347. -command {putGRASSRC $gisrc_name
  348. fileOpt::fileLocCom
  349. tkwait window .fileloc
  350. refresh_loc
  351. refresh_ms
  352. selFromList .frame0.frameLOC.listbox $location
  353. selFromList .frame0.frameMS.listbox $mapset
  354. .frame0.frameBUTTONS.ok configure -state normal}
  355. button .frame0.frameNMS.sixth.button \
  356. -text [G_msg "EPSG codes"] \
  357. -width 22 -bd 1 -wraplength 150\
  358. -relief raised \
  359. -command { putGRASSRC $gisrc_name
  360. if { [epsgOpt::epsgLocCom] } {
  361. tkwait window .optPopup
  362. refresh_loc
  363. refresh_ms
  364. selFromList .frame0.frameLOC.listbox $location
  365. selFromList .frame0.frameMS.listbox $mapset
  366. .frame0.frameBUTTONS.ok configure -state normal} }
  367. button .frame0.frameNMS.seventh.button \
  368. -text [G_msg "Projection values"] \
  369. -width 22 -bd 1 -wraplength 150\
  370. -relief raised \
  371. -command {
  372. if { $mingw == "1" } {
  373. exec -- cmd.exe /c start $env(GISBASE)/etc/set_data
  374. } else {
  375. exec -- $env(GISBASE)/etc/grass-xterm-wrapper -name xterm-grass -e $env(GISBASE)/etc/grass-run.sh $env(GISBASE)/etc/set_data
  376. }
  377. # Now we should refresh the list of locations!
  378. refresh_loc ;# Could it look like this? Maris.
  379. }
  380. pack append .frame0.frameNMS
  381. pack .frame0.frameNMS.first.label
  382. pack .frame0.frameNMS.second.entry
  383. pack .frame0.frameNMS.third.button
  384. pack .frame0.frameNMS.fourth.label
  385. pack .frame0.frameNMS.fifth.button
  386. pack .frame0.frameNMS.sixth.button
  387. pack .frame0.frameNMS.seventh.button
  388. pack .frame0.frameNMS.first
  389. pack .frame0.frameNMS.second
  390. pack .frame0.frameNMS.third
  391. pack .frame0.frameNMS.spacer
  392. pack .frame0.frameNMS.fourth
  393. pack .frame0.frameNMS.fifth
  394. pack .frame0.frameNMS.sixth
  395. pack .frame0.frameNMS.seventh
  396. # ----------------------------------
  397. # build .frame0.frameBUTTONS
  398. # ----------------------------------
  399. frame .frame0.frameBUTTONS \
  400. -borderwidth {2}
  401. button .frame0.frameBUTTONS.ok \
  402. -text [G_msg "Enter GRASS"] \
  403. -padx 10 -bd 1 -fg green4 -default active -wraplength 100 \
  404. -command {
  405. if {[CheckLocation] == 0} {
  406. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  407. [format [G_msg "Warning: selected location <%s> is not valid. \n Select valid location and try again."] $location] \
  408. 0 OK;
  409. set mapset ""
  410. } else {
  411. if {[CheckMapset] == 0} {
  412. DialogGen .wrnDlg [G_msg "WARNING: invalid mapset"] warning \
  413. [format [G_msg "Warning: <%s> is not a valid mapset"] $mapset] \
  414. 0 OK;
  415. } else {
  416. puts stdout "GISDBASE='$database';"
  417. puts stdout "LOCATION_NAME='$location';"
  418. puts stdout "MAPSET='$mapset';"
  419. putGRASSRC $gisrc_name
  420. exit 0
  421. }
  422. }
  423. }
  424. bind . <Return> {.frame0.frameBUTTONS.ok invoke}
  425. button .frame0.frameBUTTONS.help \
  426. -text [G_msg "Help"] \
  427. -padx 10 -bd 1 -wraplength 100 \
  428. -bg honeydew2 \
  429. -command {
  430. if { [winfo exists .help] } {
  431. puts [G_msg "Help already opened"]
  432. wm deiconify .help
  433. raise .help
  434. return
  435. }
  436. if { $mingw == "1" } {
  437. exec -- $env(GRASS_HTML_BROWSER) file://$env(GISBASE)/docs/html/helptext.html &;
  438. } else {
  439. exec -- $env(GRASS_HTML_BROWSER) file://$env(GISBASE)/docs/html/helptext.html >@stdout 2>@stderr &;
  440. }
  441. }
  442. button .frame0.frameBUTTONS.cancel \
  443. -text [G_msg "Exit"] \
  444. -padx 10 -bd 1 -wraplength 100 \
  445. -command { exit 2 }
  446. pack append .frame0.frameBUTTONS \
  447. .frame0.frameBUTTONS.ok { left } \
  448. .frame0.frameBUTTONS.cancel { left } \
  449. .frame0.frameBUTTONS.help { right }
  450. # ----------------------------------
  451. # packed it all
  452. # ----------------------------------
  453. frame .frame0.frameSpacer \
  454. -borderwidth {2} -height {5}
  455. # pack widget .frame0
  456. pack append .frame0 \
  457. .frame0.frameDB { top } \
  458. .frame0.frameBUTTONS { bottom expand fill } \
  459. .frame0.frameSpacer { bottom } \
  460. .frame0.frameLOC { left expand } \
  461. .frame0.frameMS { left expand } \
  462. .frame0.frameNMS { left expand fill }
  463. .frame0.frameNMS.third.button configure -state disabled
  464. pack append . \
  465. .frame0 { top frame center expand fill }
  466. .frame0.frameDB.mid.entry xview moveto 1
  467. if { ! [file exists $database] } {
  468. DialogGen .wrnDlg [G_msg "WARNING: Invalid Database"] warning \
  469. [G_msg "WARNING: Invalid database. Finding first valid directory in parent tree"] \
  470. 0 OK
  471. while { ! [file exists $database] } {
  472. set database [file dirname $database]
  473. }
  474. }
  475. # setting list of locations
  476. refresh_loc
  477. selFromList .frame0.frameLOC.listbox $location
  478. if { [CheckLocation] } {
  479. # setting list of mapsets
  480. refresh_ms
  481. selFromList .frame0.frameMS.listbox $mapset
  482. if { [.frame0.frameMS.listbox get [.frame0.frameMS.listbox curselection]] == $mapset } {
  483. .frame0.frameBUTTONS.ok configure -state normal
  484. }
  485. }
  486. bind .frame0.frameDB.mid.entry <Return> {
  487. set new_path [%W get]
  488. if { "$new_path" != "" \
  489. && [file exists $new_path] && [file isdirectory $new_path] } {
  490. %W delete 0 end
  491. %W insert 0 $new_path
  492. cdir $new_path
  493. set location ""
  494. set mapset ""
  495. refresh_loc
  496. set database [pwd]
  497. }
  498. .frame0.frameBUTTONS.ok configure -state disabled
  499. }
  500. bind .frame0.frameLOC.listbox <Double-ButtonPress-1> {
  501. # Do something only if there IS atleast one location
  502. if {[%W size] > 0} {
  503. %W selection clear 0 end
  504. %W select set [%W nearest %y]
  505. cdir $database
  506. set location [%W get [%W nearest %y]]
  507. .frame0.frameMS.listbox delete 0 end
  508. .frame0.frameBUTTONS.ok configure -state disabled
  509. set mapset ""
  510. if {[CheckLocation] == 0} {
  511. # Notice - %%s prevents %s capturing by bind
  512. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  513. [format [G_msg "Warning: selected location <%%s> is not valid. \n Select valid location and try again."] $location] \
  514. 0 OK;
  515. } else {
  516. refresh_ms
  517. }
  518. }
  519. }
  520. bind .frame0.frameLOC.listbox <ButtonPress-1> {
  521. # Do something only if there IS atleast one location
  522. if {[%W size] > 0} {
  523. %W selection clear 0 end
  524. %W select set [%W nearest %y]
  525. cdir $database
  526. set location [%W get [%W nearest %y]]
  527. .frame0.frameMS.listbox delete 0 end
  528. .frame0.frameBUTTONS.ok configure -state disabled
  529. set mapset ""
  530. if {[CheckLocation] == 0} {
  531. # Notice - %%s prevents %s capturing by bind
  532. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  533. [format [G_msg "Warning: selected location <%%s> is not valid. \n Select valid location and try again."] $location] \
  534. 0 OK;
  535. } else {
  536. refresh_ms
  537. }
  538. }
  539. }
  540. bind .frame0.frameMS.listbox <Double-ButtonPress-1> {
  541. # Do something only if there IS atleast one mapset
  542. if {[%W size] > 0} {
  543. %W selection clear 0 end
  544. %W select set [%W nearest %y]
  545. set mapset [%W get [%W nearest %y]]
  546. .frame0.frameBUTTONS.ok configure -state normal
  547. if {[CheckLocation] == 0} {
  548. # Notice - %%s prevents %s capturing by bind
  549. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  550. [format [G_msg "Warning: selected location <%%s> is not valid. \n Select valid location and try again."] $location] \
  551. 0 OK;
  552. set mapset ""
  553. } else {
  554. if {[CheckMapset] == 0} {
  555. DialogGen .wrnDlg [G_msg "WARNING: invalid mapset"] warning \
  556. [format [G_msg "Warning: <%%s> is not a valid mapset"] $mapset] \
  557. 0 OK;
  558. } else {
  559. puts stdout "GISDBASE='$database';"
  560. puts stdout "LOCATION_NAME='$location';"
  561. puts stdout "MAPSET='$mapset';"
  562. putGRASSRC $gisrc_name
  563. exit 0
  564. }
  565. }
  566. }
  567. }
  568. bind .frame0.frameMS.listbox <ButtonPress-1> {
  569. # Do something only if there IS atleast one mapset
  570. if {[%W size] > 0} {
  571. %W selection clear 0 end
  572. %W select set [%W nearest %y]
  573. set mapset [%W get [%W nearest %y]]
  574. .frame0.frameBUTTONS.ok configure -state normal
  575. if {[CheckLocation] == 0} {
  576. DialogGen .wrnDlg [G_msg "WARNING: invalid location"] warning \
  577. [format [G_msg "Warning: selected location <%%s> is not valid. \n Select valid location and try again."] $location] \
  578. 0 OK;
  579. set mapset ""
  580. }
  581. }
  582. }
  583. bind .frame0.frameNMS.second.entry <KeyRelease> {
  584. .frame0.frameNMS.third.button configure -state normal
  585. }
  586. # Exit GRASS, if window gets closed.
  587. wm protocol . WM_DELETE_WINDOW {
  588. exit 2
  589. }
  590. grab .
  591. tkwait window .
  592. }
  593. #############################################################################
  594. proc refresh_loc {} {
  595. # refresh location listbox entries
  596. global database
  597. set locList .frame0.frameLOC.listbox
  598. set mapList .frame0.frameMS.listbox
  599. if { "$database" != "" \
  600. && [file exists $database] && [file isdirectory $database] } {
  601. cdir $database
  602. $locList delete 0 end
  603. foreach i [lsort [glob -nocomplain -directory [pwd] *]] {
  604. if { [file isdirectory $i] } {
  605. $locList insert end [file tail $i]
  606. }
  607. }
  608. $mapList delete 0 end
  609. }
  610. .frame0.frameBUTTONS.ok configure -state disabled
  611. update idletasks
  612. }
  613. proc refresh_ms {} {
  614. # refresh location listbox entries
  615. global database
  616. global location
  617. set mapList .frame0.frameMS.listbox
  618. $mapList delete 0 end
  619. if { [CheckLocation] } {
  620. cdir $database
  621. cdir $location
  622. foreach i [lsort [glob -directory [pwd] *]] {
  623. if {[file isdirectory $i] && [file owned $i] } {
  624. $mapList insert end [file tail $i]
  625. }
  626. }
  627. }
  628. .frame0.frameBUTTONS.ok configure -state disabled
  629. }
  630. #############################################################################
  631. proc cdir { dir } {
  632. # cd wrapper
  633. if { [catch { cd $dir }] } {
  634. DialogGen .wrnDlg [G_msg "WARNING: change directory failed"] warning \
  635. [format [G_msg "Warning: could not change directory to <%s>.\nCheck directory permissions."] $dir ]\
  636. 0 OK;
  637. return 1
  638. } else {
  639. return 0
  640. }
  641. }
  642. proc selFromList { lis str } {
  643. # Selects list entry, if there is match
  644. set siz [$lis size]
  645. set curSelected 0
  646. for { set x 0 } { $x < $siz } { incr x } {
  647. if { $str == [$lis get $x] } {
  648. set curSelected $x
  649. break
  650. }
  651. }
  652. $lis yview $curSelected
  653. $lis selection clear 0 end
  654. $lis select set $curSelected
  655. }
  656. #############################################################################
  657. #
  658. # proc DialogGen {widget title bitmap text default buttons}
  659. #
  660. # PURPOSE: This function simply pops up a dialog box with a given message.
  661. # Note that it is similar to tk_dialog but has a slightly
  662. # different look to the dialog.
  663. # Example call:
  664. # set val [DialogGen .warnDlg "WARNING: List Changed" \
  665. # warning "WARNING: You have changed the current list.\
  666. # Do you want to discard the changes and open a new \
  667. # file?" 0 OK Cancel]
  668. # if { $val == 0 } { puts stderr "OK button pressed" }
  669. # if { $val == 1 } { puts stderr "Cancel button pressed" }
  670. # INPUT VARS: widget => name of the dialog box starting with . eg .errDlg
  671. # title => title to display in window border
  672. # bitmap => bitmap icon to display - must be one of
  673. # error gray12
  674. # gray50 hourglass
  675. # info questhead
  676. # question warning
  677. # text => text of the message to be displayed
  678. # default => index of default button (0, 1, 2...) must be less
  679. # than number of buttons
  680. # buttons => text to be used for each button eg OK Cancel
  681. # RETURN VAL: index of button that was clicked - can be ignored if only one
  682. # button is defined
  683. #
  684. #############################################################################
  685. # Procedure to generate the dialog box
  686. proc DialogGen {widget title bitmap text default buttons} \
  687. {
  688. global buttonNum
  689. # Create a popup window to warn the user
  690. toplevel $widget
  691. wm title $widget $title
  692. wm resizable $widget 0 0
  693. wm protocol $widget WM_DELETE_WINDOW "CancelDialog $widget"
  694. # Create a label for the bitmap and a message for the text
  695. frame $widget.dlgFrame
  696. pack $widget.dlgFrame -side top -fill both
  697. label $widget.dlgFrame.icon -bitmap $bitmap
  698. message $widget.dlgFrame.text -text $text -width 10c
  699. pack $widget.dlgFrame.icon $widget.dlgFrame.text -side left -fill x \
  700. -padx 10
  701. # Create a frame for the pushbuttons
  702. frame $widget.sepFrame -height 4 -bd 2 -relief raised
  703. frame $widget.buttonFrame
  704. pack $widget.buttonFrame $widget.sepFrame -side bottom -fill x
  705. # Create the pushbuttons
  706. set i 0
  707. foreach buttonLabel $buttons {
  708. button $widget.buttonFrame.$i -bd 1 -text $buttonLabel -command "set buttonNum $i"
  709. pack $widget.buttonFrame.$i -side left -expand 1 -padx 10 -pady 5
  710. incr i
  711. }
  712. # Position the top left corner of the window over the root window
  713. wm withdraw $widget
  714. update idletasks
  715. wm geometry $widget +[expr [winfo rootx .] + ([winfo width .] \
  716. -[winfo width $widget]) / 2]+[expr [winfo rooty .] + ([winfo \
  717. height .] - [winfo height $widget]) / 2]
  718. wm deiconify $widget
  719. # Grab the pointer to make sure this window is closed before continuing
  720. grab set $widget
  721. if {$default >= 0} {
  722. focus $widget.buttonFrame.$default
  723. }
  724. tkwait variable buttonNum
  725. # Destroy the popup window
  726. destroy $widget
  727. # Return the number of the button that was pushed
  728. return "$buttonNum"
  729. }
  730. # Procedure to cancel the dialog
  731. proc CancelDialog {widget} {
  732. global buttonNum
  733. # Set the wait variable so that the dialog box can cancel properly
  734. set buttonNum 999
  735. }
  736. #############################################################################
  737. global database
  738. global location
  739. global mapset
  740. global grassrc_list
  741. global gisrc_name
  742. set ver [info tclversion]
  743. if { [string compare $ver "8.0"] < 0} {
  744. puts stderr "Sorry your version of the Tcl/Tk libraries is $ver and is too"
  745. puts stderr "old for GRASS which requires a Tcl/Tk library version of 8.0 or later."
  746. puts stderr "Reverting default settings back to GRASS text mode interface."
  747. exit 1
  748. }
  749. set database ""
  750. set location ""
  751. set mapset ""
  752. set gisrc_name ""
  753. if { [info exists env(GISRC)] } {
  754. set gisrc_name $env(GISRC)
  755. }
  756. if { [searchGISRC $gisrc_name] } {
  757. gisSetWindow
  758. }