--- tkchat_orig.tcl	2011-03-23 15:29:37.000000000 -0400
+++ bin/tkchat.tcl	2011-05-05 10:34:43.000000000 -0400
@@ -20,6 +20,15 @@
 # \
       exec wish "$0" ${1+"$@"}
 
+
+#log background errors to console under Aqua, avoid locking up window
+if {[tk windowingsystem] == "aqua"} {
+    proc bgerror {args} {
+        exec syslog -s -l Error "TkChat: An error occurred: $args"
+    }
+}
+
+
 variable Features {
     "http://jabber.org/protocol/disco#info"
     "http://jabber.org/protocol/disco#items"
@@ -94,41 +103,12 @@
 
 # enable OSX-specific (un-hide window) alerting if available
 if {[tk windowingsystem] eq "aqua"} {
-    if {[catch {package present Ffidl 0.6}]} {
-	# look for ffidl.kit
-	if {[set ffidl [auto_execok ffidl.kit]] ne ""} {
-		source $ffidl
-	} elseif {[info exists starkit::topdir]} {
-		# look next to tkchat.kit/tkchat.vfs
-		set ffidl [file join [file dirname $starkit::topdir] ffidl.kit]
-		if {[file exists $ffidl]} {
-			source $ffidl
-		}
-	} else {
-		# look next to tkchat.tcl
-		set ffidl [file join $tkchat_dir ffidl.kit]
-		if {[file exists $ffidl]} {
-			source $ffidl
-		}
-	}
-    }
-    if {![catch {package require Ffidl 0.6}]} {
-        # Ffidl code + carbon hooks courtesy of Daniel Steffen - see
-        # http://wiki.tcl.tk/ffidl
-        catch {
-            # hooks to Carbon API to set application name
-            ::ffidl::callout CPSSetProcessName {pointer-byte pointer-utf8} \
-                            sint32 \
-                [::ffidl::symbol /System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/CoreGraphics CPSSetProcessName]
-              CPSSetProcessName [binary format n2 {0 2}] "TkChat"
-
-            # hooks to Carbon API to Show + Hide process
-            ::ffidl::callout ShowHideProcess {pointer-byte int} sint32 \
-                    [::ffidl::symbol /System/Library/Frameworks/Carbon.framework/Carbon ShowHideProcess]
-        }
-    }
+    # package require tclCarbonProcesses
+    package require windowlist
+    package require tclgrowl
 }
 
+
 # 8.4 doesn't provide this - we use it for i18n on menus.
 if {[llength [info commands ::tk::AmpMenuArgs]] == 0} {
     proc ::tk::AmpMenuArgs {widget add type args} {
@@ -199,6 +179,20 @@
             rename ::ttk::scrollbar ::ttk::_scrollbar
             interp alias {} ::ttk::scrollbar {} ::tk::scrollbar
         }
+
+	if {[info exists starkit::topdir]} {
+	    variable Resources
+	    variable tkdock	1
+	    set MacOS [file dirname $starkit::topdir]
+	    set Resources [file dirname $MacOS]/Resources
+	    # tkdock sites under lib beside tkchat.kit in the app bundle
+	    lappend auto_path $MacOS/lib
+	    if {[catch {package require tkdock}]} {
+		set tkdock 0
+	    } else {
+		bind all <FocusIn> +::tkdock::origIcon
+	    }
+	}
     }
     if {$useTile && [tk windowingsystem] eq "win32"} {
         # [PT]: experimental ttk styled pane closebutton.
@@ -1151,7 +1145,7 @@
 # to alert in a row and we want to batch them all together into one
 # action.
 #
-proc ::tkchat::alertWhenIdle { w } {
+proc ::tkchat::alertWhenIdle { w nick msg} {
     variable alert_pending
 
     if { ![info exists alert_pending] } {
@@ -1160,11 +1154,11 @@
 	    .txt mark set AddBookmark "end - 1 line linestart"
 	    BookmarkToggle auto
 	}
-	after idle [list [namespace origin alertCallback] $w]
+	after idle [list [namespace origin alertCallback] $w $nick $msg]
     }
 }
 
-proc ::tkchat::alertCallback {w} {
+proc ::tkchat::alertCallback {w nick msg} {
     global Options
     variable alert_pending
 
@@ -1172,13 +1166,19 @@
     unset -nocomplain alert_pending
     if {$Options(Alert,RAISE) && [llength [focus -displayof $top]]==0} {
 	# Only call this if the window doesn't already have focus
-        if {[tk windowingsystem] eq "aqua" \
-            && [llength [package provide Ffidl]] > 0} {
-            ShowHideProcess [binary format n2 {0 2}] 1
+        	if {$Options(Alert,GROWL)} {
+	    growl::message TkChat "Message from $nick" $msg
         }
 	wm deiconify $top
 	raise $top
     }
+ if {[tk windowingsystem] eq "aqua"} {
+	variable tkdock
+	if {$tkdock} {
+	    variable Resources
+	    ::tkdock::switchIcon $Resources/tkchat_warn.icns
+	}
+    }
     if {$Options(Alert,SOUND)} bell
 }
 
@@ -1225,7 +1225,7 @@
 	}
     }
     if { $alert } {
-	alertWhenIdle $w
+	alertWhenIdle $w $nick $msg
     }
     set LastPost($nick) $now
     return $subjectFound
@@ -1387,9 +1387,14 @@
 	wm title . $title
 	wm iconname . $title
 	catch {::tkchat::winico::Update}
+	if {[tk windowingsystem] eq "aqua"} {
+	    variable tkdock
+	    if {$tkdock} { ::tkdock::origIcon }
+	}
     }
 }
 
+
 proc ::tkchat::InsertTimestamp { w nick mark timestamp {tags {}} } {
     # The nick argument is here, so we can display the local time for
     # each nick.
@@ -2677,12 +2682,20 @@
     foreach { tag text } {
 	SOUND	"&Beep on alert"
 	RAISE	"&Raise to top on alert"
+	GROWL	"&Use Growl for alerts"
+	BOUNCE	"&Bounce icon on alert"
     } {
+	if {($tag eq "GROWL" || $tag eq "BOUNCE") \
+	    	&& [tk windowingsystem] ne "aqua"} continue
 	tk::AmpMenuArgs $m add checkbutton \
             -label [mc $text] \
             -variable Options(Alert,$tag) \
             -onvalue 1 \
             -offvalue 0
+	if {$tag eq "GROWL" && ![growl::available]} {
+	    set Options(Alert,GROWL) 0
+	    $m entryconfigure end -variable "" -state disabled 
+	}
     }
     $m add separator
     foreach {tag text} {
@@ -2798,8 +2811,15 @@
     ## Help Menu
     ##
     set m .mbar.help
-    tk::AmpMenuArgs $m add command -label [mc "&Quick Help..."] \
-        -command [list [namespace origin Help]]
+
+    if {[tk windowingsystem] == "aqua"} {
+        proc ::tk::mac::ShowHelp {} {
+            ::tkchat::Help
+        }
+    } else {
+        tk::AmpMenuArgs $m add command -label [mc "&Quick Help..."] \
+            -command [list [namespace origin Help]]
+    }
     tk::AmpMenuArgs $m add command -label [mc "Help (&wiki)..."] \
         -command [list [namespace origin gotoURL] http://wiki.tcl.tk/tkchat]
     $m add separator
@@ -2859,7 +2879,12 @@
 
     CreateTxtAndSbar
 
-    bind .txt <Button-3> [namespace code [list OnTextPopup %W %x %y]]
+    #button-3 is the scrollwheel on Aqua--button-2 is better for this
+    if { [tk windowingsystem] eq "aqua"} {
+        bind .txt <Button-2> [namespace code [list OnTextPopup %W %x %y]] 
+    } else {
+        bind .txt <Button-3> [namespace code [list OnTextPopup %W %x %y]]
+    }
     bind .txt <Button-1> [namespace code [list OnTextFocus %W]]
 
     # user display
@@ -2904,7 +2929,13 @@
     bind .eMsg <Key-Next>	{ .txt yview scroll  1 pages }
     bind .eMsg <Shift-Key-Up>   { .txt yview scroll -1 units }
     bind .eMsg <Shift-Key-Down> { .txt yview scroll  1 units }
-    bind .eMsg <Button-3>       [namespace code [list OnEntryPopup %W %X %Y]]
+    #button-3 is the scrollwheel on Aqua--button-2 is better for this
+    if { [tk windowingsystem] eq "aqua"} {
+        bind .eMsg <Button-2>       [namespace code [list OnEntryPopup %W %X %Y]]
+    } else {
+        bind .eMsg <Button-3>       [namespace code [list OnEntryPopup %W %X %Y]]
+    }
+
 
     text .tMsg -height 6 -font FNT
     bind .tMsg <Key-Tab>	{ ::tkchat::nickComplete ; break }
@@ -3275,7 +3306,13 @@
 
     set post [list ::tkchat::userPostOneToOne $parent $jid]
 
-    bind $parent.txt  <Button-3>  { ::tkchat::OnTextPopup %W %x %y }
+
+ #button-3 is the scrollwheel on Aqua--button-2 is better for this
+    if { [tk windowingsystem] eq "aqua"} {
+        bind $parent.txt  <Button-2>  { ::tkchat::OnTextPopup %W %x %y }
+    } else {
+        bind $parent.txt  <Button-3>  { ::tkchat::OnTextPopup %W %x %y }
+    }
     bind $parent.eMsg <Return>	  $post
     bind $parent.eMsg <KP_Enter>  $post
     $parent.post configure -command $post
@@ -3596,7 +3633,7 @@
     $w.text insert end \
 	"TkChat v$rcsVersion\n" title "$ver\n\n" {h1 center} \
 	"$rcsid\n\n" center \
-	[mc "Copyright (c) %s by following authors:" "2001-2008"] {} "\n\n" {}
+	[mc "Copyright (c) %s by following authors:" "2001-2011"] {} "\n\n" {}
 
     lappend txt "Bruce B Hartweg"       "<brhartweg@bigfoot.com>"
     lappend txt "Don Porter"		"<dgp@users.sourceforge.net>"
@@ -3613,6 +3650,7 @@
     lappend txt "Daniel South"		"<wildcard_25@users.sourceforge.net>"
     lappend txt "Steve Landers"		"<steve@digitalsmarties.com>"
     lappend txt "Elchonon Edelson"	"<eee@users.sourceforge.net>"
+    lappend txt "Kevin Walzer"          "<kw@codebykevin.com>"
 
     insertHelpText $w.text $txt
 
@@ -5484,7 +5522,7 @@
         set q [mc "Are you sure you want to quit?"]
         set a [tk_messageBox -type yesno -default yes \
                    -title [mc "Tkchat confirm quit"] \
-                   -message $q]
+                   -message $q -icon info]
     }
     if { $a eq "yes" } {
         ::tkchat::saveRC
@@ -6081,7 +6119,11 @@
     # Process command line args
     set nologin 0
     set tkonly $Options(UseTkOnly)
-    while {[string match -* [set option [lindex $args 0]]]} {
+ while {[string match -* [set option [lindex $args 0]]]} {
+	if {[tk windowingsystem] eq "aqua" && [string match "-psn*" $option]} {
+	    Pop args
+	    continue
+	}
 	switch -exact -- $option {
 	    -nologin   { set nologin 1 }
 	    -tkonly    { set tkonly 1 }
@@ -9106,8 +9148,17 @@
 		.pane.names insert $mark "$nick\n" \
 			[list NICK NICK-$nick URL-[incr ::URLID]]
 	    }
-	    .pane.names tag bind URL-$::URLID <Button-3> \
+
+            
+            #button-3 is the scrollwheel on Aqua--button-2 is better for this
+            if { [tk windowingsystem] eq "aqua"} {
+                .pane.names tag bind URL-$::URLID <Button-2> \
 		    [list ::tkchat::OnNamePopup $nick $network %X %Y]
+            } else {
+                .pane.names tag bind URL-$::URLID <Button-3> \
+		    [list ::tkchat::OnNamePopup $nick $network %X %Y]
+            }
+            
 	    .pane.names tag bind URL-$::URLID <Control-Button-1> \
 		    [list ::tkchat::OnNamePopup $nick $network %X %Y]
 	}
@@ -10007,7 +10058,14 @@
     $m add command -label [mc "Clear"] -command [list $f.txt delete 0.0 end]
     $m add command -label [mc "Eval in whiteboard"] \
         -command [list [namespace origin PasteEval] $dlg]
-    bind $f.txt <Button-3> [list tk_popup $m %X %Y]
+
+
+    #button-3 is the scrollwheel on Aqua--button-2 is better for this
+    if { [tk windowingsystem] eq "aqua"} {
+        bind $f.txt <Button-2> [list tk_popup $m %X %Y]
+    } else {
+        bind $f.txt <Button-3> [list tk_popup $m %X %Y]
+    }
     
     bind $dlg <Key-Escape> [list $cancel invoke]
     pack $f2.lbl -side left
