summaryrefslogtreecommitdiffstats
path: root/ds9/library/ds9.tcl
blob: 7545b211cebcccd9bbe1d33494fea6838cf38a31 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#  Copyright (C) 1999-2018
#  Smithsonian Astrophysical Observatory, Cambridge, MA, USA
#  For conditions of distribution and use, see copyright notice in "copyright"

proc DS9Def {} {
    global ds9
    global pds9

    switch $ds9(wm) {
	x11 -
	aqua {set ds9(title) $ds9(app)}
	win32 {set ds9(title) [file rootname $ds9(app)]}
    }

    # Must be major,minor
    set ds9(version) {8.2}
    # For display purposes only
    set ds9(version,display) {8.2b2}

    set ds9(visual) {}
    set ds9(depth) 8
    set ds9(FTY_MAXAXES) 10
    set ds9(threads) [GetNumCores]
    set ds9(prefs) 1

    set ds9(helvetica) [font configure TkDefaultFont -family]
    set ds9(courier) [font configure TkFixedFont -family]
    switch $ds9(wm) {
	x11 {
	    set ds9(times) serif

	    # These look better if normal weight
	    font configure TkCaptionFont -weight normal
	    font configure TkHeadingFont -weight normal
	}
	aqua {set ds9(times) times}
	win32 {set ds9(times) times}
    }
    set ds9(main) {}
    set ds9(image) {}
    set ds9(canvas) {}
    set ds9(panel) {}
    set ds9(info) {}
    set ds9(panner) {}
    set ds9(magnifier) {}
    set ds9(buttons) {}
    set ds9(graph,sp) {}
    set ds9(graph,horz) {}
    set ds9(graph,vert) {}

    set ds9(frames) {}
    set ds9(active) {}
    set ds9(active,num) 0
    set ds9(lock) 0
    set ds9(next) {}
    set ds9(next,num) 1
    set ds9(last) {}

    set ds9(event,opendoc) {}
    set ds9(event,printdoc) {}

    set ds9(tmpdir) {}

    switch $ds9(wm) {
	x11 -
	win32 {set ds9(menu,start) 1}
	aqua {set ds9(menu,start) 0}
    }
    set ds9(menu,size,frame,goto) [expr $ds9(menu,start)+0]
    set ds9(menu,size,frame,active) [expr $ds9(menu,start)+3]
    set ds9(menu,size,analysis) [expr $ds9(menu,start)+33]
    set ds9(menu,size,wrap) 20

    set ds9(display) single

    set ds9(array,x) 512
    set ds9(array,y) 512
    set ds9(array,bitpix) -32
    set ds9(array,skip) 0
    set ds9(array,arch) bigendian

    set ds9(row) -1
    set ds9(freeze) 0

    set ds9(b1) 0
    set ds9(b2) 0
    set ds9(b3) 0

    set ds9(sb1) 0
    set ds9(sb2) 0
    set ds9(sb3) 0

    set ds9(cb1) 0
    set ds9(cb2) 0
    set ds9(cb3) 0

    set ds9(csb1) 0
    set ds9(csb2) 0
    set ds9(csb3) 0

    set ds9(modifier) 0

    set ds9(ext,file) ".$ds9(app).fil"
    set ds9(ext,alt) ".$ds9(app).file"

    set ds9(msg) {}
    set ds9(msg,level) info
    set ds9(msg,src) {}
    set ds9(msg,timeout) 1000

    switch $ds9(wm) {
	x11 -
	win32 {
	    set ds9(ctrl) "Control-"
	    set ds9(shiftctrl) "Shift-Control-"
	}
	aqua {
	    set ds9(ctrl) "Command-"
	    set ds9(shiftctrl) "Shift-Command-"
	}
    }

    # prefs only
    InitDefaultFont
    InitDefaultTextFont

    set pds9(backup) 1
    set pds9(automarker) 1
    switch $ds9(wm) {
	x11 -
	aqua {set pds9(xpa) 1}
	win32 {set pds9(xpa) 0}
    }
    
    set pds9(prec,linear) 8
    set pds9(prec,deg) 7
    set pds9(prec,hms) 4
    set pds9(prec,dms) 3
    set pds9(prec,len,linear) 8
    set pds9(prec,len,deg) 7
    set pds9(prec,len,arcmin) 5
    set pds9(prec,len,arcsec) 3
    set pds9(prec,angle) 8

    set pds9(bg) white
    set pds9(bg,use) 0
    set pds9(nan) white

    set pds9(samp) 1
    set pds9(confirm) 1
    set pds9(iraf) 1
    switch $ds9(wm) {
	x11 {set pds9(dialog) motif}
	aqua -
	win32 {set pds9(dialog) native}
    }
    set pds9(dialog,center) 0
    set pds9(dialog,all) 0
    set pds9(language) locale
    set pds9(language,name) [LanguageToName $pds9(language)]
    set pds9(language,dir) {}

    set pds9(theme) default
}

# ds9(wm)
if {[catch {tk windowingsystem} ds9(wm)]} {
    puts stderr "Unable to initialize window system."
    exit
}

# ds9(app)
set ds9(app) [file tail [info nameofexecutable]]

# ds9(top)
# need for load
set ds9(top) .
set ds9(mb) .mb

# pre package load
switch $ds9(wm) {
    x11 {
	set ds9(root) "[::tcl::zipfs::root]mntpt"
	set auto_path [list $ds9(root) $ds9(root)/tcl8.6 $ds9(root)/tk8.6 $ds9(root)/tk8.6/ttk]
    }
    aqua {
	# set to absolute path
	set ds9(root) [file normalize [file dirname [file dirname $argv0]]]
	set bb [file dirname [file dirname $ds9(root)]]
	set auto_path [list $ds9(root) $bb/Tcl.framework/Resources $bb/Tcl.framework/Resources/Scripts $bb/Tk.framework/Resources $bb/Tk.framework/Resources/Scripts $bb/Tk.framework/Resources/Scripts/ttk]
    }
    win32 {
	set ds9(root) [file dirname [file dirname $argv0]]
	set auto_path [list $ds9(root) $ds9(root)/tcl8.6 $ds9(root)/tk8.6 $ds9(root)/tk8.6/ttk]
    }
}

package require msgcat
package require http

package require base64
package require ftp
package require math::fuzzy

package require tkcon
package require Tkblt
package require Tls
package require xmlrpc

package require DS9

# post package load
switch $ds9(wm) {
    x11 {
	# awthemes
	package require awthemes
	package require colorutils
	package require awdark
	package require awlight
	package require awblack
#	package require awwinxpblue
	package require awbreeze

	# tkthemes
	package require ttk::theme::aquativo
#	package require ttk::theme::black
	package require ttk::theme::blue
	package require ttk::theme::clearlooks
	package require ttk::theme::elegance
#	package require ttk::theme::itft1
#	package require ttk::theme::keramik
#	package require ttk::theme::kroc
	package require ttk::theme::plastik
	package require ttk::theme::radiance
#	package require ttk::theme::smog
	package require ttk::theme::winxpblue

	# scidthemes
	package require ttk::theme::scidthemes

	# edit theme list
	set ds9(themes) [lsort [ttk::style theme names]]
	set ds9(themes) [lsearch -all -inline -not -exact $ds9(themes) alt]
	set ds9(themes) [lsearch -all -inline -not -exact $ds9(themes) classic]

	# colors
	ttk::style theme use default

	set ds9(bold) cornflowerblue
	set ds9(foreground) [ttk::style lookup . -foreground]
	set ds9(background) [ttk::style lookup . -background]
	set ds9(foreground,active) \
	    [ttk::style lookup TMenubutton -foreground active]
	set ds9(background,active) \
	    [ttk::style lookup TMenubutton -background active]
	set ds9(foreground,disabled) \
	    [ttk::style lookup TMenubutton -foreground disabled]
	set ds9(background,disabled) \
	    [ttk::style lookup TMenubutton -background disabled]
	set ds9(selectforeground) [ttk::style lookup . -selectforeground]
	set ds9(selectbackground) [ttk::style lookup . -selectbackground]

	# fix TEntry/Treeview/TLabel widgets
	foreach tt [ttk::style theme names] {
	    ttk::style theme use $tt
	    set fg [ttk::style lookup . -foreground]
	    if {$fg == {}} {
		set fg $ds9(foreground)
	    }
	    set bg [ttk::style lookup . -background]
	    if {$bg == {}} {
		set bg $ds9(background)
	    }
	    ttk::style configure TEntry -padding 1 \
		-fieldbackground $bg -background $bg -foreground $fg
	    ttk::style configure Treeview -background $bg -fieldbackground $bg
	    ttk::style configure TLabel -borderwidth 2 -padding 1
	}
	ttk::style theme use default

	# fix ::tk::dialog::file
	set ::tk::dialog::file::showHiddenVar 0
	set ::tk::dialog::file::showHiddenBtn 1
    }
    aqua {
	# colors
	set ds9(bold) systemControlAccentColor
	set ds9(foreground) systemTextColor
	set ds9(background) systemTextBackgroundColor
	set ds9(foreground,active) systemTextColor
	set ds9(background,active) systemTextBackgroundColor
	set ds9(foreground,disabled) systemWindowBackgroundColor4
	set ds9(background,disabled) systemTextColor
	set ds9(selectforeground) systemSelectedTextColor
	set ds9(selectbackground) systemSelectedTextBackgroundColor

	set pap(fg) systemTextColor
	set pap(bg) systemTextBackgroundColor

	::tk::unsupported::MacWindowStyle style $ds9(top) document \
	    "closeBox fullZoom collapseBox resizable"

	proc ::tk::mac::ShowPreferences {} {
	    PrefsDialog
	}

	proc ::tk::mac::OpenApplication {} {
	}

	proc ::tk::mac::ReopenApplication {} {
	    if {[wm state .] eq "withdrawn"} {
		wm state . normal
	    } else {
		wm deiconify .
	    }
	    raise .
	}

	proc ::tk::mac::OpenDocument {args} {
	    global ds9

	    # we can recieve this event before everything has been
	    # initiated, so save and process later
	    set ds9(event,opendoc) $args
	    if {!$ds9(init)} {
		MacOSOpenDocEvent
	    }
	}

	proc ::tk::mac::PrintDocument {args} {
	    global ds9

	    regsub -all {file://} $args {} ds9(event,printdoc)
	    if {!$ds9(init)} {
		MacOSPrintDocEvent 0
	    }
	}

	proc ::tk::mac::Quit {} {
	    QuitDS9
	}

	proc ::tk::mac::ShowHelp {} {
	    HelpRef
	}

	proc ::tk::mac::DoScriptFile {args} {
	}

	proc ::tk::mac::DoScriptText {args} {
	}

	proc ::tk::mac::LaunchURL {} {
	}
    }
    win32 {
	# colors
	ttk::style theme use xpnative

	set ds9(bold) cornflowerblue
	set ds9(foreground) [ttk::style lookup . -foreground]
	set ds9(background) [ttk::style lookup . -background]
	set ds9(foreground,active) \
	    [ttk::style lookup TMenubutton -foreground active]
	set ds9(background,active) \
	    [ttk::style lookup TMenubutton -background active]
	set ds9(foreground,disabled) \
	    [ttk::style lookup TMenubutton -foreground disabled]
	set ds9(background,disabled) \
	    [ttk::style lookup TMenubutton -background disabled]
	set ds9(selectforeground) [ttk::style lookup . -selectforeground]
	set ds9(selectbackground) [ttk::style lookup . -selectbackground]
    }
}

# Define Variables
DS9Def
2MASSDef
3DDef
AnalysisDef
BinDef
BlinkDef
BlockDef
ButtonsDef
CanvasDef
CATDef
CATSymDef
CATCDSSrchDef
CentroidDef
ColorbarDef
ContourDef
CoordDef
CrosshairDef
CubeDef
CurrentDef
CursorDef
DebugDef 
ESODef
ExamineDef
ExportDef
FPDef
GraphDef
GridDef
GroupDef
HelpDef
HTTPDef
HVDef
IExamDef
IISDef
MagnifierDef
MarkerDef
MaskDef
MinMaxDef
MovieDef
NRESDef
NVSSDef
PannerDef
PanZoomDef
CropDef
PixelDef
PlotDef
PrefsDef
PSDef
RGBDef
SAMPDef
SAODef
SaveDef
SaveImageDef
ScaleDef
SIADef
SimpleTextDef
SkyViewDef
SmoothDef
STSCIDef
TemplateDef
TileDef
ViewDef
VLADef
VLSSDef
VODef
WCSDef
ZScaleDef

# let's start
set ds9(init) 1

# set up signal trap
# not supported under windows
switch $tcl_platform(platform) {
    unix {signal add SIGINT QuitDS9}
    windows {}
}

# environment vars
# we don't want to see any error messages if xpa is not available
if { [info exists env(XPA_VERBOSITY)] == 0 } {
  set env(XPA_VERBOSITY) 0
}
# lower XPA connection timeout
set env(XPA_CONNECT_TIMEOUT) 3
# set filter ptype to contained (default is process)
set env(FILTER_PTYPE) c
# set filter error proc so it will not kill ds9
set env(GERROR) 0

# Events
event add <<Open>> <${ds9(ctrl)}o>
event add <<Save>> <${ds9(ctrl)}s>
event add <<Print>> <${ds9(ctrl)}p>
event add <<SelectAll>> <${ds9(ctrl)}a>
event add <<Find>> <${ds9(ctrl)}f>
event add <<FindNext>> <${ds9(ctrl)}g>
event add <<Close>> <${ds9(ctrl)}w>
event add <<ZoomIn>> <${ds9(ctrl)}plus>
event add <<ZoomOut>> <${ds9(ctrl)}minus>
switch $ds9(wm) {
    x11 -
    win32 {
	event add <<PageSetup>> <${ds9(shiftctrl)}P>
	event add <<Quit>> <${ds9(ctrl)}q>
	event add <<Pref>> <${ds9(ctrl)}comma>
	event add <<SelectNone>> <${ds9(shiftctrl)}A>
    }
    aqua {
	event add <<PageSetup>> <${ds9(shiftctrl)}p>
	event add <<SelectNone>> <${ds9(shiftctrl)}a>
    }
}

# We want to withdraw the window til everything is ready to go
wm withdraw $ds9(top)

wm title $ds9(top) "SAOImage $ds9(title)"
wm iconname $ds9(top) "SAOImage $ds9(title)"
wm protocol $ds9(top) WM_DELETE_WINDOW QuitDS9

# set the visual
set ds9(visual) [winfo visual .]
set ds9(depth) [winfo depth .]

switch $ds9(wm) {
    x11 {
	if {$ds9(depth)==15} {
	    set ds9(depth) 16
	}
	if {$ds9(depth)==32} {
	    set ds9(depth) 24
	}
    } 
    aqua {
	if {$ds9(depth)==15} {
	    set ds9(depth) 16
	}
    }
    win32 {
	if {$ds9(depth)==32} {
	    set ds9(depth) 24
	}
    }
}

switch -- $ds9(visual)$ds9(depth) {
    truecolor8 {}
    truecolor16 {}
    truecolor24 {}
    default {BadVisualError}
}

# Init Temporary Dir before prefs
InitTempDir

# Init the filter compiler
InitFilterCompiler

# we need to check to see to run prefs first
ProcessCommandLineFirst

# Load any preferences here, before we do any real work
if {$ds9(prefs)} {
    LoadPrefs
}

# set fonts
SetDefaultFont false
SetDefaultTextFont false

# we need to set certain variables such as debug, title, language, xpa
ProcessCommandLineSecond

# initialize language
switch $pds9(language) {
    locale {
	switch $ds9(wm) {
	    x11 {
		foreach ee {LC_MESSAGES LC_ALL LANG} {
		    if {[info exists env($ee)]} {
			set ll [string tolower [string range $env($ee) 0 1]]
			if {[SetLanguage $ll]} {
			    break
			}
		    }
		}
	    }
	    aqua {
		foreach ll [MacOSXGetLocale] {
		    if {[SetLanguage $ll]} {
			break
		    }
		}
	    }
	    win32 {}
	}
    }
    default {SetLanguage $pds9(language)}
}

# create our main frame
set ds9(main) [ttk::frame ${ds9(top)}ds9]
pack $ds9(main) -fill both -expand true

# Create image canvas
CreateCanvas

# Create Colorbar
CreateColorbar

# Create other parts of the display
CreateMenuBar
CreateInfoPanel
CreatePanner
CreateMagnifier
CreateButtons
CreateGraphs

# Make sure that the wm knows when to swap in the colormap (if needed)
wm colormapwindows . "$ds9(main) $ds9(canvas)"

# Initialize the display
InitColorbar
InitPanner
InitDialogBox

# Set our current state of things
ChangeMode

# force a update, then layout
update
ConfigureView

# our first frame
CreateFrame

# do this last so we don't get an ConfigureView event
InitCanvas

# ok, ready to show the window
wm deiconify $ds9(top)

# Init external File Formats
# we want this before processing the command line
InitExternalFile

# Init analysis file formats
InitAnalysisFile

# Configure HTTP
ConfigHTTP

# SAMP
InitSAMP

# XPA
if {$pds9(xpa)} {
    InitXPA
}

# and process any command line items
# we want to see something before any fits files are loaded
ProcessCommandLine

# Initialize IIS
# after command line options to set port/fifo/unix...
catch {IISInit}

# any os events received?
switch $ds9(wm) {
    x11 -
    win32 {}
    aqua {
	MacOSOpenDocEvent
	MacOSPrintDocEvent 1
    }
}

# Load any initalization tcl code
SourceInitFileDir {.ini}

# do we have the correct prefs file?
CheckPrefs

# kludge for aqua. We need to trigger the trap to update buttons vars
switch $ds9(wm) {
    x11 -
    win32 {}
    aqua {
	set current(display) $current(display)
	set colorbar(map) $colorbar(map)
    }
}

# start error monitor
after $ds9(msg,timeout) [list ErrorTimer]

# ok, we're done
set ds9(init) 0

# everything must be realized
update

# major kludges
switch $ds9(wm) {
    x11 {
	# be sure theme has been set
	# could be changed in prefs or command line
	ThemeChange

	# lock down geometry at statup
	# so unneeded configure events are not generated
	# a problem with recent versions of linux
	wm geometry $ds9(top) \
	    "[winfo width $ds9(top)]x[winfo height $ds9(top)]"
    }
    aqua {}
    win32 {
	# jump start keyevents for windows
	event generate $ds9(canvas) <Tab> -x 0 -y 0
    }
}