# $Id: snoopstatus.tcl 160 2008-11-17 13:20:12Z kostix $
# "Snoop status messages" -- plugin for Tkabber which collects
# notification messages reported via [set_status] into a separate
# window with a text widget and a scrollbar.
# Written by Konstantin Khomoutov <flatworm@users.sourceforge.net>
# See README for usage notes.
# See license.terms for the terms of distribution.

namespace eval snoopstatus {
    variable win .snoopstatus

    hook::add finload_hook \
	[namespace current]::install_menu_item
    hook::add set_status_hook \
	[namespace current]::process_status

    hook::add save_session_hook \
	[namespace current]::save_session
}

proc snoopstatus::install_menu_item {} {
    set m  [.mainframe getmenu plugins]
    $m insert end command \
	-label [::msgcat::mc "Snoop status messages"] \
	-command [namespace current]::snoop_status_messages
}

proc snoopstatus::snoop_status_messages {} {
    variable win
    if {[winfo exists $win]} return

    add_win $win -class SnoopStatus \
	-title [::msgcat::mc "Status messages"] \
	-tabtitle [::msgcat::mc "Status messages"] \
	-raisecmd [list focus $win.text]

    text $win.text -state disabled -wrap word \
	-yscrollcommand [list $win.vsb set]
    scrollbar $win.vsb -command [list $win.text yview]

    grid $win.text $win.vsb -sticky ns
    grid $win.text -sticky news
    grid rowconfigure $win 0 -weight 1
    grid columnconfigure $win 0 -weight 1

    setup_search_panel $win $win.text
}

proc snoopstatus::process_status text {
    variable win
    set t $win.text
    if {![winfo exists $t]} return

    append msg [clock format [clock seconds] \
	-format $::plugins::options(timestamp_format)] " " $text \n

    $t configure -state normal
    $t insert end $msg
    $t configure -state disabled
    $t see end
}

proc snoopstatus::open_search_panel {text sf} {
    grid $sf - -sticky news
    update idletasks
    $text see end
}

proc snoopstatus::close_search_panel {text sf} {
    $text tag remove search_highlight 0.0 end
    grid forget $sf
    focus $text
}

proc snoopstatus::setup_search_panel {master text} {
    $text mark set sel_start end
    $text mark set sel_end 0.0

    set sf [plugins::search::spanel $master.search \
	-searchcommand [list [namespace parent]::search::do_text_search $text] \
	-closecommand  [list [namespace current]::close_search_panel $text]]

    bind $text <<OpenSearchPanel>> \
	[double% [list [namespace current]::open_search_panel $text $sf]]
}

proc snoopstatus::save_session sessionVar {
    upvar 2 $sessionVar session

    lappend session [list 0 {} {} {} [namespace code restore_session]]
}

proc snoopstatus::restore_session args {
    snoop_status_messages
}

# vim:ts=8:sw=4:sts=4:noet
