# "Bold Nicks" plugin for Tkabber.
# Renji <webrenji@gmail.com>
# 5:26 25.10.2010

namespace eval boldnicks {
	variable options
	
	::msgcat::mcload [file join [file dirname [info script]] msgs]
	
	custom::defgroup Plugins [::msgcat::mc "Plugins options."] \
	-group Tkabber	
	
    custom::defgroup "Bold Nicks" [::msgcat::mc "\"Bold Nicks\" plugin options."] \
	-group Plugins	
	
	custom::defvar options(use_bold_style) 1 \
	[::msgcat::mc "Use bold style for nicks."] \
	-group "Bold Nicks" -type boolean \
	-command [namespace current]::change_options
	
	custom::defvar options(change_cursor) 1 \
	[::msgcat::mc "Change cursor when mouse is hover nick."] \
	-group "Bold Nicks" -type boolean \
	-command [namespace current]::change_options
	
	custom::defvar options(emphasize_when_hover) 1 \
	[::msgcat::mc "Use underline when cursor is hover on nickname."] \
	-group "Bold Nicks" -type boolean \
	-command [namespace current]::change_options
	
	if {![::plugins::is_registered boldnicks]} {
		::plugins::register boldnicks \
				-namespace [namespace current] \
				-source [info script] \
				-description [::msgcat::mc "Whether the \"Bold Nicks\" plugin is loaded."] \
				-loadcommand [namespace code load] \
				-unloadcommand [namespace code unload]
		return
	}
	
	proc load {} {
		hook::add draw_message_hook \
		[namespace current]::check_nick 60
		
		hook::add open_chat_post_hook \
		[namespace current]::chat_add
		
		hook::add close_chat_post_hook \
		[namespace current]::chat_delete
		
		chats_state_config active
	}
	
	proc unload {} {
		hook::remove draw_message_hook \
		[namespace current]::check_nick 60
		
		hook::remove open_chat_post_hook \
		[namespace current]::chat_add
		
		hook::remove close_chat_post_hook \
		[namespace current]::chat_delete
		
		chats_state_config deactive
	}
}

proc boldnicks::chats_state_config {state} {
	variable chat_nicks
	
	foreach chatid [chat::opened] {
		set w [chat::chat_win $chatid]
		switch -- $state {
			active {
				chat_add $chatid {}
				foreach tag [lsearch -all -glob -inline [$w tag names] NICK-*] {
					if [regsub -- "NICK-" $tag "" nick] {
					set_bold $chatid $nick
					lappend chat_nicks($chatid) $nick
					}
				}
			}
			deactive {
				foreach nick $chat_nicks($chatid) {
					$w tag configure NICK-$nick -font ChatFont
					$w tag bind NICK-$nick <Any-Enter> {}
					$w tag bind NICK-$nick <Any-Leave> {}
					chat_delete $chatid
				}
			}
		}
	}
}

proc boldnicks::change_options {args} {
    variable chat_nicks
    foreach chatid [chat::opened] {
		if {[winfo exists [chat::chat_win $chatid]]} {
			foreach nick $chat_nicks($chatid) {set_bold $chatid $nick}
		}
	}
}

proc boldnicks::chat_add {chatid type} {
    variable chat_nicks

    set chat_nicks($chatid) {}
}

proc boldnicks::chat_delete {chatid} {
    variable chat_nicks

    catch {unset chat_nicks($chatid)}
}

proc boldnicks::check_nick {chatid from type body x} {
	variable chat_nicks
	
    set xlib [chat::get_xlib $chatid]
    set nick [chat::get_nick $xlib $from $type]
	
	if {[lsearch -exact $chat_nicks($chatid) $nick] < 0} {
		lappend chat_nicks($chatid) $nick
		set_bold $chatid $nick
    }
}

proc boldnicks::set_bold {chatid nick} {
	variable options

    if {[catch {set w [chat::chat_win $chatid]}] || ![winfo exists $w]} {
	return
	}

    if {$options(use_bold_style)} {  
		$w tag configure NICK-$nick -font ChatBoldFont
	} else {
		$w tag configure NICK-$nick -font ChatFont
	}
	
	$w tag bind NICK-$nick <Any-Enter> \
		[list [namespace current]::mouse_on_nick yes $w NICK-$nick]
		
	$w tag bind NICK-$nick <Any-Leave> \
		[list [namespace current]::mouse_on_nick no $w NICK-$nick]
}

proc boldnicks::mouse_on_nick {bool w tag} {
	variable options

	$w tag configure $tag -underline \
		[expr {$options(emphasize_when_hover) && $bool ? \
			1 : [lindex [$w tag configure $tag -underline] 3]}]
	$w configure -cursor \
		[expr {$options(change_cursor) && $bool ? \
			[option get $w urlcursor Text] : [lindex [$w configure -cursor] 3]}]
}