Version 16 of COM on! - a tiny web browser

Updated 2002-09-20 02:20:52

http://mini.net/files/comon.jpg


Richard Suchenwirth 2002-09-18 - I've learnt and preached that Tcl/Tk can be used to glue software components together, but how strong this is I experienced only last night. Steve Offutt hinted me in the Tcl chatroom at optcl, an extension that allows exploring and using Windows COM modules, and mailed me a little 2 KB script that implements a web Browser (by embedding part of Internet Explorer via optcl), animated GIFs as well as directory browsing supported, and even Acrobat Reader gets embedded in turn if you point it to a PDF document.

I played with it at home, adjusted and simplified it somehow, and noticed that the whole browser, capable of navigating the WWW as well as your local file system, can fit in less than 1 KB (if you exempt these explanations, and assuming you're on Windows >= 95 and accept the GPL), while allowing you to style it at your heart's delight, just as we know it from Tcl. (Only printing is not supported, but we know that from Tcl too ;-) So here goes my "lean and mean" version: }

 wm title . "COM on!"
 package require optcl
 set home  c:/html/index.htm ;# local setting on my box at home
 option add *Button.pady        0
 option add *Button.borderWidth 1
 option add *Button.relief      flat
 bind Button <Enter> {+ %W config -relief raised}
 bind Button <Leave> {+ %W config -relief flat}

 set htm [optcl::new -window .htm Shell.Explorer.2]
 .htm config -width 600 -height 400

 frame  .fr
 button .fr.h -text ^   -command {catch {$htm navigate [set url $home]}}
 button .fr.o -text ... -command {$htm navigate [set url [tk_getOpenFile]]}
 button .fr.b -text <   -command {catch {$htm goBack}}
 button .fr.f -text >   -command {catch {$htm goForward}}
 entry  .fr.e -textvar url -width 80
 bind   .fr.e <Return> {$htm navigate $url}
 set url $home
 eval pack [winfo children .fr] -side left -fill y

 pack .fr  -fill x    -expand 1
 pack .htm -fill both -expand 1
 catch {$htm navigate $url}
 bind . <Escape> {exec wish $argv0 &; exit} ;# useful in development

SO 9/19/02 - I appreciate Richard mentioning our conversation in the chatroom, and the little script I mailed him. I would be remiss, however, if I failed to point out that my little hack was based on a script provided by Michael Jacobson, on the optcl page. Also, I would love to see other examples of using optcl/tcom appear on the wiki. These are truly powerful tools for those of us who spend most of our programming time in the world of windows.


MPJ 9/19/02 - Note there is a little bug in Tk or optcl that does not allow the text box to grab the focus correctly [L1 ]. I notice that your little app has this problem too. I mention it on the optcl page with this solution (using my other favorite dll ... ffidl). UPDATE I like the way GPS solved this on his WippleWobble - A Mini Web Browser (for Windows) page using forceFocus proc.

 load ffidl05.dll
 ffidl::callout dll_SetFocus {int} int [ffidl::symbol user32.dll SetFocus]
 proc GrabFocus {args} {dll_SetFocus [winfo id .]}
 bind . <Button> +GrabFocus

We can also add the use of event bindings from the ActiveX componet to this interface. Here is a little code from NewzPoint to display the MouseOver urls a status bar at the bottom.

 proc linkchanged {id page} {
   global link htm
   if {$page == "" || [string equal -nocase "done" $page]} {
      set link [$htm : LocationURL]
   } else {
      set link $page
   } 
 }
 optcl::bind $htm StatusTextChange linkchanged
 label .sta -relief sunken -borderwidth 2  -anchor w -textvariable link
 pack .sta -fill x -expand 1

Another item that is a must have for a browser is security level (icon). Here is how to implement it in with optcl.

 optcl::bind $html SetSecureLockIcon security
 proc security {id secureid} {
        global securitylevel
        #0 secureLockIconUnsecure  "There is no security encryption present."
        #1 secureLockIconMixed "There are multiple security encryption methods present."
        #2 secureLockIconUnknownBits "The security encryption level is not known."
        #3 secureLockIcon40Bit "There is 40-bit security encryption present."
        #4 secureLockIcon56Bit "There is 56-bit security encryption present."
        #5 secureLockIconFortezza "There is Fortezza security encryption present."
        #6 secureLockIcon128Bit "There is 128-bit security encryption present."
        set secure [lindex "Unsecure Mixed Unknown 40Bits 56Bits Fortezza 128Bits" $secureid]
        set securitylevel $secure
 }
 # now put securitylevel in a status bar