source: grass/branches/develbranch_6/gui/tcltk/gis.m/runandoutput.tcl

Last change on this file was 38296, checked in by marisn, 15 years ago

Fix displaying vector attribute table in gis.m if db path contains spaces (fixes #637); Don't run commands on map (info, attributes) if map is not selected.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/x-tcl
File size: 8.8 KB
Line 
1############################################################################
2#
3# LIBRARY: runandoutput.tcl
4# AUTHOR(S): Cedric Shock (cedricgrass AT shockfamily.net)
5# PURPOSE: Interactive console for gis.m and other run commands
6# COPYRIGHT: (C) 2006 GRASS Development Team
7#
8# This program is free software under the GNU General Public
9# License (>=v2). Read the file COPYING that comes with GRASS
10# for details.
11#
12############################################################################
13
14#############################################
15#Overloaded gui.tcl behaviour:
16
17# Overload run_cmd (proc called when run button is pushed)
18proc run_cmd {dlg} {
19 global gronsole
20 global opt
21
22 set cmd [dialog_get_command $dlg]
23
24 catch {$opt($dlg,run_button) configure -state disabled}
25
26 $gronsole run $cmd {} "catch {$opt($dlg,run_button) configure -state active}"
27
28 # Bring that output window up:
29 raise [winfo toplevel $gronsole]
30}
31
32# no output or progress or buttons:
33proc make_output {dlg path root} {}
34proc make_progress {dlg path root} {}
35proc make_buttons {dlg path root} {}
36
37###########################################
38
39proc make_fun_buttons {dlg path} {
40 global opt env
41 set pgm_name $opt($dlg,pgm_name)
42
43 set buttonframe [frame $path.buttonframe]
44 button $buttonframe.run -text [G_msg "Run"] -command "run_cmd $dlg"
45 # In the future we'll have a button to make a layer from here:
46 # button $buttonframe.layer -text [G_msg "Layer"] -command "layer_cmd $dlg"
47 button $buttonframe.help -text [G_msg "Help"] -command "help_cmd $dlg"
48 button $buttonframe.close -text [G_msg "Close"] -command "close_cmd $dlg"
49
50 set opt($dlg,run_button) $buttonframe.run
51
52 # Turn off help button if the help file doesn't exist
53 if {! [file exists [file join "$env(GISBASE)" "docs" "html" "$pgm_name.html"]]} {
54 $buttonframe.help configure -state disabled
55 }
56
57 pack $buttonframe.run $buttonframe.help $buttonframe.close \
58 -side left -expand yes -padx 5 -pady 5
59 pack $buttonframe -expand no -fill x -side bottom -before [lindex [pack slaves $path] 0]
60
61 # Set the starting window size if this is a toplevel window
62 if {[winfo toplevel $path] == $path} {
63 wm geometry $path "560x400"
64 }
65}
66
67proc run_ui {cmd} {
68 global dlg
69 global path
70 global devnull
71
72 set program [lindex $cmd 0]
73 set code ""
74
75 if {[catch {set code [exec -- "$program" --tcltk 2> $devnull]} error]} {
76 GmLib::errmsg $error
77 }
78
79 set path .dialog$dlg
80 toplevel $path
81
82 if {$code == ""} { return }
83 eval $code
84
85 # Push the command to the dialog
86 set thisdialog $dlg
87 dialog_set_command $dlg $cmd
88
89 # Add our ui
90 make_fun_buttons $dlg $path
91}
92
93#################################################
94
95proc run_disabled {gronsole button cmd} {
96 if {[catch {$button configure -state disabled} error]} {
97 GmLib::errmsg $error
98 }
99
100 $gronsole run $cmd {running} "catch {$button configure -state active}"
101}
102
103proc gronsole_history {cmdtext ci cmd} {
104 $cmdtext delete 1.0 end
105 $cmdtext insert end $cmd
106}
107
108proc command_window {where} {
109 global keycontrol
110 global bgcolor
111 global mingw
112
113 set cmdpane [frame $where.command -bg $bgcolor]
114 set cmdwin [ScrolledWindow $where.win -relief flat -borderwidth 1]
115 set gronsole [Gronsole $where.gronsole -clickcmd "gronsole_history $cmdwin.text"]
116 set cmdtext [text $cmdwin.text -height 2 -width 80]
117 $cmdwin setwidget $cmdtext
118 set runbutton [button $cmdpane.run -text [G_msg "Run"] -width 17 -wraplength 100 \
119 -default active -bd 1 -highlightbackground $bgcolor \
120 -command "run_disabled $gronsole $cmdpane.run \[string trim \[$cmdtext get 1.0 end\]\]"]
121 set run2button [button $cmdpane.run2 -text [G_msg "Run (background)"] -width 17 \
122 -wraplength 100 -bd 1 -highlightbackground $bgcolor \
123 -command "$gronsole run \[string trim \[$cmdtext get 1.0 end\]\] {} {}"]
124 set runuibutton [button $cmdpane.runui -text [G_msg "Run (GUI)"] -width 17 \
125 -wraplength 100 -bd 1 -highlightbackground $bgcolor \
126 -command "run_ui \[string trim \[$cmdtext get 1.0 end\]\]"]
127 if { $mingw } {
128 set termbox "DOS box"
129 } else {
130 set termbox "Xterm"
131 }
132 set runxterm [button $cmdpane.xterm -text [G_msg "Run (in $termbox)"] -width 17 \
133 -wraplength 100 -bd 1 -highlightbackground $bgcolor \
134 -command "$gronsole run_xterm \[string trim \[$cmdtext get 1.0 end\]\] {}"]
135 set outpane [frame $where.output -bg $bgcolor]
136 set savebutton [button $outpane.save -text [G_msg " Save "] -command "$gronsole save" \
137 -bd 1 -padx 10 -highlightbackground $bgcolor]
138 set clearbutton [button $outpane.clear -text [G_msg " Clear "] -command "$gronsole clear" \
139 -bd 1 -padx 10 -highlightbackground $bgcolor]
140
141 pack $runbutton $run2button $runuibutton $runxterm \
142 -side left -expand yes -padx 2 -pady 5
143 pack $savebutton $clearbutton \
144 -side left -expand yes -padx 5 -pady 5
145
146 pack $cmdpane -fill x -side bottom
147 pack $cmdwin -fill x -side bottom
148 pack $outpane -fill x -side bottom
149 pack $gronsole -side top -fill both -expand yes
150
151 bind $cmdtext <$keycontrol-c> "tk_textCopy %W"
152 bind $cmdtext <$keycontrol-v> "tk_textPaste %W"
153 bind $cmdtext <$keycontrol-x> "tk_textCut %W"
154
155 return $gronsole
156}
157
158toplevel .gronsole
159wm title .gronsole [G_msg "Output - GIS.m"]
160
161# If we had our own window manager we could withdraw windows instead of iconifying them:
162wm protocol .gronsole WM_DELETE_WINDOW "wm iconify .gronsole"
163
164set gronsole [command_window {.gronsole}]
165
166###############################################################################
167# Run procs for gis.m:
168
169################################################################################
170
171proc execute {cmd} {
172 # Use run and output
173 run_ui [list $cmd]
174}
175
176###############################################################################
177proc spawn {cmd args} {
178
179 if {[catch {eval [list exec -- $cmd] $args &} error]} {
180 GmLib::errmsg $error
181 }
182
183}
184
185###############################################################################
186proc spawn_panel {cmd args} {
187 global gronsole
188
189 $gronsole run $cmd gism {}
190}
191
192###############################################################################
193
194proc run_panel {cmd} {
195 global gronsole
196
197 $gronsole run_wait $cmd gism
198}
199
200###############################################################################
201proc run {cmd args} {
202 global devnull
203
204 # This and runcmd are being used to run command in the background
205 # These used to go to stdout and stderr
206 # but we don't want to pollute that console.
207 # eval exec -- $cmd $args >@ stdout 2>@ stderr
208
209 if {[catch {eval [list exec -- $cmd] $args >& $devnull} error]} {
210 GmLib::errmsg $error
211 }
212
213
214}
215
216###############################################################################
217
218proc runcmd {cmd args} {
219 global gronsole
220
221 set ci [$gronsole annotate $cmd [list gism running]]
222
223 eval run $cmd $args
224
225 $gronsole remove_tag $ci running
226}
227
228###############################################################################
229proc term_panel {cmd} {
230 global gronsole
231
232 $gronsole run_xterm $cmd gism
233}
234
235###############################################################################
236proc term {cmd args} {
237 global env
238 global mingw
239
240 if { $mingw == "1" } {
241 if {[catch {eval [list exec -- cmd.exe /c start $env(GISBASE)/etc/grass-run.bat $cmd] $args &} error]} {
242 GmLib::errmsg $error
243 }
244
245 } else {
246 if {[catch {eval [list exec -- $env(GISBASE)/etc/grass-xterm-wrapper -name xterm-grass -e $env(GISBASE)/etc/grass-run.sh $cmd] $args &} error]} {
247 GmLib::errmsg $error
248 }
249
250 }
251}
252
253###############################################################################
254# Make sure there's an xmon before running some commands.
255# Used in menus.
256
257proc guarantee_xmon {} {
258 global mingw
259
260 if {$mingw == 1} {
261 tk_messageBox -type ok -icon error -title [G_msg "X Windows not available"] \
262 -message [G_msg "Functions that require X Windows are not available in Windows"]
263 return
264 }
265 if {![catch {open "|d.mon -L" r} input]} {
266 while {[gets $input line] >= 0 } {
267 if {[regexp -nocase {(x[0-9]+).*not running} $line dummy monitor]} {
268 # $monnum is the monitor number
269 #create list of non-running monitors
270 lappend xmonlist $monitor
271 }
272 }
273
274 }
275
276 if {[catch {close $input} error]} {
277 tk_messageBox -type ok -icon error -title [G_msg "Error"] \
278 -message [G_msg "d.mon error: problem launching xmon, $error"]
279 return
280 }
281
282 if { ![info exists xmonlist] } {
283 tk_messageBox -type ok -icon error -title [G_msg "Error"] \
284 -message [G_msg "This module requires X11 support, but no xmons were found"]
285 return
286 }
287
288 set xmon [lindex $xmonlist 0]
289 spawn d.mon start=$xmon
290}
291
292###############################################################################
293# Annotation procs for gis.m:
294
295proc monitor_annotation_start {mon title tags} {
296 global gronsole
297 set handle [$gronsole annotate $title $tags]
298 $gronsole set_click_command $handle {}
299 return $handle
300}
301
302proc monitor_annotate {handle text} {
303 global gronsole
304 $gronsole annotate_text $handle $text
305}
Note: See TracBrowser for help on using the repository browser.