package provide xtcl 0.1


# diam (30/05/00) : integration dans pist::xtcl
# a faire : fusionner avec urlView (du sript "net")
#
# 
# Quelques procedures de lancement de browser HTML
#  
# URL : http://mini.net/cgi-bin/wikit/557.html
# 
# 
# Invoking browsers
# 
# This page describes how to call upon an external browser to display a
# page, given the URL. It's far from done complete, but it's a start. -
# DL 
# 
# There was a potential problem with embedded spaces in the path in the
# unix branch. 'auto_execok' returns a list, but I assume that the value
# of $::env(BROWSER) should be a simple string. I've changed the code to
# reflect that assumption, but it may cause trouble in the unlikely event
# that 'auto_execok' actually returns a list of length greater than 1. -
# DGP 
# 
# The windows branch can be simplified in Tcl interpreters of release 8.3
# or later. The command 
# 
#         [eval exec [auto_execok start] [list $url]]
# 
# should launch the browser on both NT and 95/98. -DGP 

proc browser::findExecutable {progname varname} {
    upvar 1 $varname result
    set progs [auto_execok $progname]
    if {[llength $progs]} {
        set result [lindex $progs]
    }
    return [llength $progs]
}

proc browser::urlOpen {url} {
   global env tcl_platform
   switch $tcl_platform(platform) {
       "unix" {
           expr {
               [info exists env(BROWSER) ||
               [findExecutable netscape       env(BROWSER)] ||
               [findExecutable iexplorer      env(BROWSER)] ||
               [findExecutable $env(NETSCAPE) env(BROWSER)] ||
               [findExecutable lynx           env(BROWSER)]
           }
           # lynx can also output formatted text to a variable
           # with the -dump option, as a last resort:
           # set formatted_text [ exec lynx -dump $url ] - PSE
           if {[catch {exec $env(BROWSER) -remote $url}]} {
               # perhaps browser doesn't understand -remote flag
               if {[catch {exec $env(BROWSER) $url &} emsg]} {
                   error "Error displaying $url in browser\n$emsg"
                   # Another possibility is to just pop a window up
                   # with the URL to visit in it. - DKF
               }
           }
       }
       "windows" {
           if {$tcl_platform(os) == "Windows NT"} {
               set rc [catch {exec $env(COMSPEC) /c start $url &} emsg]
           } else {
              # Windows 95/98
              set rc [catch {exec start $url} emsg]
           }
           if {$rc} {
               error "Error displaying $url in browser\n$emsg"
           }
       }
       "macintosh" {
           # AppleScript should be able to provide the default browser,
           # but I'm not enough of an AppleScript hacker to figure out.
           # Help!
           if {0 == [info exists env(BROWSER)]} {
               error "Couldn't find a usable browser.  Set env(BROWSER)\
                      in your application's preferences file."
           }
           if {[catch {
               AppleScript execute\
                   "tell application \"$env(BROWSER)\"
                        open url \"$url\"
                    end tell
               "} emsg]
           } then {
               error "Error displaying $url in browser\n$emsg"
           }
       }
   } ;## end of switch
}


# Fixed two things in Windows NT section: changed tcl_platform to
# ::tcl_platform and added & to end of cmd.exe invocation. Now it works
# on NT. 
# 
# Tero 



# # Here's an example from my Tcl/Tk Programmer's Reference [1] which uses
# # the registry on Win32. This example can browse to URLs with anchors
# # (foo.html#bar). It can be adapted to open any file based on it's
# # association by replacing ".html" with the desired extention in the
# # first registry get command. 
# # 
# # (perSub does percent substitutions like those in event bindings. It's
# # available in the regexp example at the site noted above.) 
# # 
# # Chris Nelson 
# 
# package require registry
# proc showHtml { htmlFile } {
#     # Look for the application under
#     # HKEY_CLASSES_ROOT
#     set root HKEY_CLASSES_ROOT
# 
#     # Get the application key for HTML files
#     set appKey [registry get $root\\.html ""]
# 
#     # Get the command for opening HTML files
#     set appCmd [registry get \
#          $root\\$appKey\\shell\\open\\command ""]
# 
#     # Substitute the HTML filename into the
#     # command for %1
#     set appCmd [perSub $appCmd %1 $htmlFile]
# 
#     # Double up the backslashes for eval (below)
#     regsub -all {\\} $appCmd  {\\\\} appCmd
# 
#     # Invoke the command
#     eval exec $appCmd &
# }
# showHtml C:/foobar.html
# 

# Another example for use on Windows is the following simple item to open
# any url in the user's selected default: 
# 
#     exec rundll32 url.dll,FileProtocolHandler $url
# 
# (you may want to add '&' etc). 
# 
# Vince
# 
