# $Id: hidemuctopic.tcl 232 2009-10-09 11:43:55Z kostix $
# Provides for hiding of the topic window in MUC rooms
# to save a bit of screen space.
# Written by Konstantin Khomoutov <flatworm@users.sourceforge.net>
# See license.terms for the terms of distribution.
# See README for the usage details.

event add <<ToggleTopicWindow>> <Control-Key-t>

namespace eval hidemuctopic {
	variable options
	variable packinfo

	custom::defgroup Plugins [::msgcat::mc "Plugins options."] \
		-group Tkabber
	custom::defgroup {MUC Hide Topic} \
		[::msgcat::mc "Support for toggling visibility of\
			room topic window in MUC."] \
		-group Plugins
	custom::defvar options(hidden_by_default) 1 \
		[::msgcat::mc "Hide room topic window in newly\
			opened MUC windows."] \
		-type boolean -group {MUC Hide Topic}

	hook::add open_chat_post_hook \
		[namespace current]::setup
}

proc hidemuctopic::topicwin {chatid} {
	return [chat::winid $chatid].status
}

proc hidemuctopic::setup {chatid type} {
	if {![string equal $type groupchat]} return

	variable options
	variable packinfo
	upvar 0 packinfo($chatid) pinfo

	set mw [chat::winid $chatid]
	set tw $mw.status
	set slaves [pack slaves $mw]

	set pinfo [pack info $tw]
	set ix [lsearch -exact $slaves $tw]
	lappend pinfo -before [lindex $slaves [expr {$ix + 1}]]

	set iw [chat::input_win $chatid]
	bind $iw <<ToggleTopicWindow>> \
		[list [namespace current]::toggle [double% $chatid]]
	bind $iw <<ToggleTopicWindow>> +break

	if {$options(hidden_by_default) && ![winfo ismapped $tw]} {
		bind $tw <Map> {
			pack forget %W
			bind %W <Map> {}
		}
	}
}

proc hidemuctopic::hide {chatid} {
	pack forget [topicwin $chatid]
}

proc hidemuctopic::show {chatid} {
	variable packinfo
	eval [list pack [topicwin $chatid]] $packinfo($chatid)
}

proc hidemuctopic::toggle {chatid} {
	set w [topicwin $chatid]
	if {[winfo ismapped $w]} {
		hide $chatid
	} else {
		show $chatid
	}
}

