summaryrefslogtreecommitdiffstats
path: root/ds9/library/crosshair.tcl
blob: 13264fe1d5dd180fb9192db650155beb988772f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#  Copyright (C) 1999-2018
#  Smithsonian Astrophysical Observatory, Cambridge, MA, USA
#  For conditions of distribution and use, see copyright notice in "copyright"

package provide DS9 1.0

proc CrosshairDef {} {
    global crosshair
    global icrosshair

    set icrosshair(top) .ch
    set icrosshair(mb) .chmb

    set crosshair(lock) none

    # set via wcs()
    set crosshair(system) wcs
    set crosshair(sky) fk5
    set crosshair(skyformat) degrees
}

proc CrosshairButton {which x y} {
    global ds9
    global crosshair

    $which crosshair canvas $x $y
    UpdateCrosshairDialog
    LockCrosshair $which
}

proc CrosshairArrowKey {which x y} {
    global ds9
    global crosshair

    $which crosshair warp $x $y
    UpdateCrosshairDialog
    LockCrosshair $which

    set coord [$which get crosshair canvas]
    set X [lindex $coord 0]
    set Y [lindex $coord 1]

    UpdateColormapLevelMosaic $which $X $Y canvas
    UpdateInfoBox $which $X $Y canvas
    UpdatePixelTableDialog $which $X $Y canvas
    UpdateGraphData $which $X $Y canvas
}

proc CrosshairTo {x y sys sky} {
    global crosshair
    global current
    global ds9

    set current(mode) crosshair
    ChangeMode

    if {$current(frame) != {}} {
	$current(frame) crosshair $sys $sky $x $y
	set coord [$current(frame) get crosshair canvas]
	UpdateColormapLevelMosaic $current(frame) \
	    [lindex $coord 0] [lindex $coord 1] canvas
	UpdateInfoBox $current(frame) \
	    [lindex $coord 0] [lindex $coord 1] canvas

	if {$crosshair(lock) != "none"} {
	    set coord [$current(frame) get crosshair $crosshair(lock)]
	    foreach f $ds9(frames) {
		if {$f != $current(frame) && [$f has system $crosshair(lock)]} {
		    $f crosshair $crosshair(lock) $coord
		}
	    }
	}
	UpdateCrosshairDialog
    }
}

proc MatchCrosshairCurrent {sys} {
    global current

    if {$current(frame) != {}} {
	MatchCrosshair $current(frame) $sys
    }
}

proc MatchCrosshair {which sys} {
    global crosshair
    global ds9
    global current

    if {$current(mode) != {crosshair}} {
	return
    }

    switch -- $sys {
	image -
	physical -
	amplifier -
	detector {
	    set coord [$which get crosshair $sys]
	    foreach ff $ds9(frames) {
		if {$ff != $which} {
		    $ff crosshair $sys $coord 
		}
	    }
	}
	wcs {
	    set ss [lindex [$which get wcs] 0]
	    if {[$which has wcs $ss]} {
		set coord [$which get crosshair $ss]
		foreach ff $ds9(frames) {
		    if {$ff != $which} {
			if {[$ff has wcs $ss]} {
			    $ff crosshair $ss $coord 
			}
		    }
		}
	    }
	}
    }
}

proc LockCrosshairCurrent {} {
    global current
    
    if {$current(frame) != {}} {
	LockCrosshair $current(frame)
    }
}

proc LockCrosshair {which} {
    global crosshair

    switch -- $crosshair(lock) {
	none {}
	default {MatchCrosshair $which $crosshair(lock)}
    }
}

proc CrosshairDialog {} {
    global crosshair
    global icrosshair
    global dcrosshair
    global current

    # see if we already have a window visible
    if {[winfo exists $icrosshair(top)]} {
	raise $icrosshair(top)
	return
    }

    # create the window
    set w $icrosshair(top)
    set mb $icrosshair(mb)

    Toplevel $w $mb 6 [msgcat::mc {Crosshair Parameters}] \
	CrosshairDestroyDialog

    # for CoordMenuButton
    set crosshair(frame) $current(frame)

    $mb add cascade -label [msgcat::mc {File}] -menu $mb.file
    $mb add cascade -label [msgcat::mc {Edit}] -menu $mb.edit

    menu $mb.file
    $mb.file add command -label [msgcat::mc {Apply}] \
	-command CrosshairApplyDialog
    $mb.file add separator
    $mb.file add command -label [msgcat::mc {Close}] \
	-command CrosshairDestroyDialog

    EditMenu $mb icrosshair

    # Param
    set f [ttk::frame $w.param]

    ttk::label $f.title -text [msgcat::mc {Crosshair}]
    ttk::entry $f.x -textvariable dcrosshair(x) -width 14
    ttk::entry $f.y -textvariable dcrosshair(y) -width 14
    set dcrosshair(cb) $f.system 
    CoordMenuButton $dcrosshair(cb) crosshair system 1 sky skyformat \
	UpdateCrosshairDialog

    grid $f.title $f.x $f.y $f.system -padx 2 -pady 2

    # Buttons
    set f [ttk::frame $w.buttons]
    ttk::button $f.apply -text [msgcat::mc {Apply}] \
	-command CrosshairApplyDialog
    ttk::button $f.close -text [msgcat::mc {Close}] \
	-command CrosshairDestroyDialog
    pack $f.apply $f.close -side left -expand true -padx 2 -pady 4

    # Fini
    ttk::separator $w.sep -orient horizontal
    pack $w.buttons $w.sep -side bottom -fill x
    pack $w.param -side top -fill both -expand true

    $w.param.x select range 0 end

    UpdateCrosshairDialog
}

proc CrosshairApplyDialog {} {
    global crosshair
    global dcrosshair

    CrosshairTo $dcrosshair(x) $dcrosshair(y) $crosshair(system) $crosshair(sky)
}

proc CrosshairDestroyDialog {} {
    global icrosshair
    global dcrosshair

    if {[winfo exists $icrosshair(top)]} {
	destroy $icrosshair(top)
	destroy $icrosshair(mb)
	unset dcrosshair
    }
}

proc UpdateCrosshairDialog {} {
    global crosshair
    global icrosshair
    global dcrosshair
    global current

    global debug
    if {$debug(tcl,update)} {
	puts stderr "UpdateCrosshairDialog"
    }

    if {![winfo exists $icrosshair(top)]} {
	return
    }

    if {$current(frame) != {}} {
	set crosshair(frame) $current(frame)
	if {[$current(frame) has fits]} {
	    # now make sure we have the coord systems
	    AdjustCoordSystem crosshair system
	    CoordMenuEnable $dcrosshair(cb).menu crosshair system 1 sky skyformat
	    CoordMenuButtonCmd crosshair system sky {}
	} else {
	    CoordMenuReset $dcrosshair(cb).menu crosshair system 1 sky skyformat
	}
    }

    if {$current(frame) != {}} {
	set coord [$current(frame) get crosshair $crosshair(system) \
		       $crosshair(sky) $crosshair(skyformat)]
	set dcrosshair(x) [lindex $coord 0]
	set dcrosshair(y) [lindex $coord 1]
    } else {
	set dcrosshair(x) {}
	set dcrosshair(y) {}
    }
}

proc ProcessCrosshairCmd {varname iname} {
    upvar $varname var
    upvar $iname i

    # we need to be realized
    ProcessRealizeDS9

    global debug
    if {$debug(tcl,parser)} {
	crosshair::YY_FLUSH_BUFFER
	crosshair::yy_scan_string [lrange $var $i end]
	crosshair::yyparse
	incr i [expr $crosshair::yycnt-1]
    } else {

    switch -- [string tolower [lindex $var $i]] {
	match {
	    incr i
	    MatchCrosshairCurrent [lindex $var $i]
	}
	lock {
	    incr i
	    set crosshair(lock) [lindex $var $i]
	    LockCrosshairCurrent
	}
	default {
	    set x [lindex $var [expr $i+0]]
	    set y [lindex $var [expr $i+1]]
	    set sys [lindex $var [expr $i+2]]
	    set sky [lindex $var [expr $i+3]]
	    set format {}

	    incr i 1
	    incr i [FixSpec sys sky format physical fk5 degrees]

	    CrosshairTo $x $y $sys $sky
	    UpdateCrosshairDialog
	}
    }
}
}

proc CrosshairCmdSet {which value {cmd {}}} {
    global crosshair

    set crosshair($which) $value
    if {$cmd != {}} {
	eval $cmd
    }
}

proc ProcessSendCrosshairCmd {proc id param} {
    global crosshair
    global current

    switch -- [string tolower $param] {
	lock {$proc $id "$crosshair(lock)\n"}
	default {
	    set sys [lindex $param 0]
	    set sky [lindex $param 1]
	    set format [lindex $param 2]
	    FixSpec sys sky format physical fk5 degrees

	    if {$current(frame) != {}} {
		$proc $id "[$current(frame) get crosshair $sys $sky $format]\n"
	    }
	}
    }
}