dtext.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. ##########################################################################
  2. # dtext.tcl new version - standard text layer options file for GRASS GIS Manager
  3. # November 2006 Michael Barton, Arizona State University
  4. # COPYRIGHT: (C) 1999 - 2006 by the GRASS Development Team
  5. #
  6. # This program is free software under the GNU General Public
  7. # License (>=v2). Read the file COPYING that comes with GRASS
  8. # for details.
  9. #
  10. ##########################################################################
  11. namespace eval GmDtext {
  12. variable array opt # d.text options
  13. variable count 1
  14. variable array lfile
  15. variable array lfilemask
  16. variable optlist
  17. variable first
  18. variable array dup
  19. variable placement
  20. variable optlist
  21. global env
  22. }
  23. proc GmDtext::create { tree parent } {
  24. variable opt
  25. variable count
  26. variable lfile
  27. variable lfilemask
  28. variable optlist
  29. variable first
  30. variable dup
  31. variable optlist
  32. global iconpath env
  33. set node "dtext:$count"
  34. set frm [ frame .texticon$count]
  35. set check [checkbutton $frm.check \
  36. -variable GmDtext::opt($count,1,_check) \
  37. -height 1 -padx 0 -width 0]
  38. image create photo textico -file "$iconpath/module-d.text.gif"
  39. set ico [label $frm.ico -image textico -bd 1 -relief raised]
  40. bind $ico <ButtonPress-1> "GmTree::selectn $tree $node"
  41. pack $check $ico -side left
  42. #insert new layer
  43. if {[$tree selection get] != "" } {
  44. set sellayer [$tree index [$tree selection get]]
  45. } else {
  46. set sellayer "end"
  47. }
  48. $tree insert $sellayer $parent $node \
  49. -text "text layer $count"\
  50. -window $frm \
  51. -drawcross auto
  52. set opt($count,1,_check) 1
  53. set dup($count) 0
  54. set opt($count,_check) 1
  55. set opt($count,1,opacity) 1.0
  56. set opt($count,1,text) ""
  57. set opt($count,1,at) "10,10"
  58. set opt($count,1,coordinates) "percent"
  59. set opt($count,1,mouseset) 0
  60. set opt($count,1,align) "lower_left"
  61. set opt($count,1,line) 10
  62. set opt($count,1,rotate) 0
  63. set opt($count,1,font) ""
  64. set opt($count,1,bold) 0
  65. set opt($count,1,size) 10
  66. set opt($count,1,color) \#000000
  67. set opt($count,1,linespace) 1.25
  68. set first 1
  69. set optlist { _check text at coordinates mouseset\
  70. align line rotate font bold size color linespace}
  71. foreach key $optlist {
  72. set opt($count,0,$key) $opt($count,1,$key)
  73. }
  74. # create files in tmp diretory for layer output
  75. set mappid [pid]
  76. if {[catch {set lfile($count) [exec g.tempfile pid=$mappid]} error]} {
  77. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  78. }
  79. set lfilemask($count) $lfile($count)
  80. append lfile($count) ".ppm"
  81. append lfilemask($count) ".pgm"
  82. incr count
  83. return $node
  84. }
  85. ###############################################################################
  86. proc GmDtext::set_option { node key value } {
  87. variable opt
  88. set id [GmTree::node_id $node]
  89. set opt($id,1,$key) $value
  90. }
  91. ##########################################################################
  92. proc GmDtext::set_font { id } {
  93. variable opt
  94. if {$GmDtext::opt($id,1,font) != "" } {
  95. set Gm::dfont $GmDtext::opt($id,1,font)
  96. }
  97. Gm::defaultfont dtext
  98. tkwait variable Gm::dfont
  99. set GmDtext::opt($id,1,font) $Gm::dfont
  100. set Gm::dfont ""
  101. }
  102. ###############################################################################
  103. proc GmDtext::mouseset { id } {
  104. # use mouse to set text placement coordinates
  105. global mon pctentry pixelentry geogentry
  106. variable placement
  107. if { $GmDtext::opt($id,1,mouseset) == 1} {
  108. if {$GmDtext::opt($id,1,coordinates) == "pixels"} {
  109. set pixelentry $GmDtext::placement
  110. } else {
  111. set pixelentry ""
  112. }
  113. if {$GmDtext::opt($id,1,coordinates) == "percent"} {
  114. set pctentry $GmDtext::placement
  115. } else {
  116. set pctentry ""
  117. }
  118. if {$GmDtext::opt($id,1,coordinates) == "geographic"} {
  119. set geogentry $GmDtext::placement
  120. } else {
  121. set geogentry ""
  122. }
  123. }
  124. }
  125. ###############################################################################
  126. # dtext options
  127. proc GmDtext::options { id frm } {
  128. variable opt
  129. variable placement
  130. variable first
  131. global iconpath bgcolor
  132. # Panel heading
  133. set row [ frame $frm.heading ]
  134. Label $row.a -text [G_msg "Display text"] \
  135. -fg MediumBlue
  136. pack $row.a -side left
  137. pack $row -side top -fill both -expand yes
  138. #opacity
  139. set row [ frame $frm.opc]
  140. Label $row.a -text [G_msg "Opaque "]
  141. scale $row.b -from 1.0 -to 0.0 -showvalue 1 \
  142. -orient horizontal -length 300 -resolution 0.01 -fg "#656565"\
  143. -variable GmDtext::opt($id,1,opacity)
  144. Label $row.c -text [G_msg " Transparent"]
  145. pack $row.a $row.b $row.c -side left
  146. pack $row -side top -fill both -expand yes
  147. # text
  148. set row [ frame $frm.text ]
  149. LabelEntry $row.a -label [G_msg "Text to display: "] -textvariable GmDtext::opt($id,1,text) -width 51
  150. Button $row.b -text [G_msg "Help"] \
  151. -image [image create photo -file "$iconpath/gui-help.gif"] \
  152. -command "spawn g.manual --q d.text" \
  153. -background $bgcolor \
  154. -helptext [G_msg "Help"]
  155. pack $row.a $row.b -side left
  156. pack $row -side top -fill both -expand yes
  157. # coordinates1
  158. set row [ frame $frm.east_north ]
  159. set placement [LabelEntry $row.a -textvariable GmDtext::opt($id,1,at) -width 25 \
  160. -label [G_msg "Text placement: x,y coordinates (from upper left) "]]
  161. pack $row.a -side left
  162. pack $row -side top -fill both -expand yes
  163. # coordinates2
  164. set row [ frame $frm.textcoord2 ]
  165. Label $row.a -text [G_msg " coordinate type for text placement "]
  166. ComboBox $row.b -padx 2 -width 10 -textvariable GmDtext::opt($id,1,coordinates) \
  167. -values {"pixels" "percent" "geographic" } -modifycmd "GmDtext::mouseset $id"
  168. checkbutton $row.c -text [G_msg "place with mouse"] \
  169. -variable GmDtext::opt($id,1,mouseset) \
  170. -command "GmDtext::mouseset $id"
  171. pack $row.a $row.b $row.c -side left
  172. pack $row -side top -fill both -expand yes
  173. # alignment
  174. set row [ frame $frm.textalign ]
  175. Label $row.a -text [G_msg " align text with coordinate point "]
  176. ComboBox $row.b -padx 2 -width 12 -textvariable GmDtext::opt($id,1,align) \
  177. -values {"lower_left" "bottom_center" "lower_right" "center_left" "center"
  178. "center_right" "upper_left" "top_center" "upper_right" }
  179. pack $row.a $row.b -side left
  180. pack $row -side top -fill both -expand yes
  181. # rotation
  182. set row [ frame $frm.textrotate ]
  183. Label $row.a -text [G_msg " text rotation (degrees)"]
  184. set rotation [SpinBox $row.b -range {-360 360 1} -textvariable GmDtext::opt($id,1,rotate) \
  185. -entrybg white -width 6]
  186. if {$first==1} {$rotation setvalue @360}
  187. pack $row.a $row.b -side left
  188. pack $row -side top -fill both -expand yes
  189. # font options1
  190. set row [ frame $frm.fontopt1 ]
  191. Label $row.a -text [G_msg "Text options: font "]
  192. Button $row.b -image [image create photo -file "$iconpath/gui-font.gif"] \
  193. -highlightthickness 0 -takefocus 0 -relief raised -borderwidth 1 \
  194. -helptext [G_msg "select font for text"] \
  195. -command "GmDtext::set_font $id"
  196. Label $row.c -text [G_msg " color"]
  197. SelectColor $row.d -type menubutton -variable GmDtext::opt($id,1,color)
  198. checkbutton $row.e -padx 10 -text [G_msg "bold text"] -variable \
  199. GmDtext::opt($id,1,bold)
  200. pack $row.a $row.b $row.c $row.d $row.e -side left
  201. pack $row -side top -fill both -expand yes
  202. # font options3
  203. set row [ frame $frm.fontopt3 ]
  204. LabelEntry $row.a -label [G_msg " text height in pixels "]\
  205. -textvariable GmDtext::opt($id,1,size) -width 6
  206. LabelEntry $row.b -label [G_msg " line spacing"]\
  207. -textvariable GmDtext::opt($id,1,linespace) -width 6
  208. pack $row.a $row.b -side left
  209. pack $row -side top -fill both -expand yes
  210. }
  211. ###############################################################################
  212. proc GmDtext::save { tree depth node } {
  213. variable opt
  214. variable optlist
  215. global mon
  216. set id [GmTree::node_id $node]
  217. foreach key $optlist {
  218. GmTree::rc_write $depth "$key $opt($id,1,$key)"
  219. }
  220. }
  221. ###############################################################################
  222. proc GmDtext::display { node mod } {
  223. global mon env
  224. variable optlist
  225. variable lfile
  226. variable lfilemask
  227. variable opt
  228. variable tree
  229. variable dup
  230. variable count
  231. variable first
  232. set line ""
  233. set input ""
  234. set cmd ""
  235. set tree($mon) $GmTree::tree($mon)
  236. set id [GmTree::node_id $node]
  237. # If we are told dirty (for zoom) force dirty
  238. # Don't remove a dirty from a previous unrendered zoom
  239. if {$mod} {set opt($id,1,mod) 1}
  240. # set hex colors to rgb
  241. set color [GmLib::color $opt($id,1,color)]
  242. if { ! ( $opt($id,1,_check) ) } { return }
  243. if { $opt($id,1,text) == "" } { return }
  244. switch $opt($id,1,align) {
  245. "lower_left" { set align "ll"}
  246. "bottom_center" { set align "lc" }
  247. "lower_right" { set align "lr"}
  248. "center_left" { set align "cl" }
  249. "center" { set align "cc" }
  250. "center_right" { set align "cr" }
  251. "upper_left" { set align "ul"}
  252. "top_center" { set align "uc" }
  253. "upper_right" { set align "ur"}
  254. }
  255. if {$opt($id,1,coordinates) == "percent"} {
  256. set atlist [split $opt($id,1,at) ","]
  257. set xcoord [lindex $atlist 0]
  258. set ycoord [expr 100 - [lindex $atlist 1]]
  259. set at "$xcoord,$ycoord"
  260. } else {
  261. set at $opt($id,1,at)
  262. }
  263. set cmd "d.text -s size=$opt($id,1,size) color=$color \
  264. at=$at align=$align rotation=$opt($id,1,rotate) \
  265. linespacing=$opt($id,1,linespace) --q {text=$opt($id,1,text)}"
  266. # bold text
  267. if { $opt($id,1,bold) != 0 } {
  268. append cmd " -b"
  269. }
  270. # coordinates in pixels or geographic coordinates
  271. if {$opt($id,1,coordinates) == "pixels"} {
  272. append cmd " -p"
  273. } elseif {$opt($id,1,coordinates) == "geographic"} {
  274. append cmd " -g"
  275. }
  276. # check value of GRASS_FONT variable prior to display
  277. if {![catch {set env(GRASS_FONT)}]} {
  278. set currfont $env(GRASS_FONT)
  279. } else {
  280. set currfont "romans"
  281. }
  282. # set grass font environmental variable to user selection"
  283. if { $GmDtext::opt($id,1,font) != ""} { set env(GRASS_FONT) $GmDtext::opt($id,1,font) }
  284. # Decide whether to run, run command, and copy files to temp
  285. GmCommonLayer::display_command [namespace current] $id $cmd
  286. # set grass font environmental variable to whatever it was when we started
  287. # this lets different text layers have different fonts
  288. set env(GRASS_FONT) $currfont
  289. }
  290. ###############################################################################
  291. # text duplicate layer
  292. proc GmDtext::duplicate { tree parent node id } {
  293. variable opt
  294. variable count
  295. variable lfile
  296. variable lfilemask
  297. variable optlist
  298. variable first
  299. variable dup
  300. global iconpath
  301. set node "dtext:$count"
  302. set dup($count) 1
  303. set frm [ frame .texticon$count]
  304. set check [checkbutton $frm.check \
  305. -variable GmDtext::opt($count,1,_check) \
  306. -height 1 -padx 0 -width 0]
  307. image create photo textico -file "$iconpath/module-d.text.gif"
  308. set ico [label $frm.ico -image textico -bd 1 -relief raised]
  309. bind $ico <ButtonPress-1> "GmTree::selectn $tree $node"
  310. pack $check $ico -side left
  311. #insert new layer
  312. if {[$tree selection get] != "" } {
  313. set sellayer [$tree index [$tree selection get]]
  314. } else {
  315. set sellayer "end"
  316. }
  317. $tree insert $sellayer $parent $node \
  318. -text "text layer $count"\
  319. -window $frm \
  320. -drawcross auto
  321. set opt($count,1,opacity) $opt($id,1,opacity)
  322. set first 1
  323. foreach key $optlist {
  324. set opt($count,1,$key) $opt($id,1,$key)
  325. set opt($count,0,$key) $opt($count,1,$key)
  326. }
  327. set id $count
  328. # create files in tmp directory for layer output
  329. set mappid [pid]
  330. if {[catch {set lfile($count) [exec g.tempfile pid=$mappid]} error]} {
  331. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  332. }
  333. set lfilemask($count) $lfile($count)
  334. append lfile($count) ".ppm"
  335. append lfilemask($count) ".pgm"
  336. incr count
  337. return $node
  338. }