#@+leo-ver=4
#@+node:@file hoist.py
"""Add Hoist/De-Hoist buttons to the toolbar.
"""

# By Davide Salomoni.

__version__ = "0.2" # Color mod by EKR.

from leoPlugins import *
from leoGlobals import *

try:
	import Tkinter
except ImportError:
	Tkinter = None
Tk = Tkinter

activeHoistColor = "pink1" # The Tk color to use for the active hoist button.

# Set this to 0 if the sizing of the toolbar controls doesn't look good on your platform. 
USE_FIXED_SIZES = 1 

#@+others
#@+node:class HoistButtons
class HoistButtons:
	"""Hoist/dehoist buttons for the toolbar."""
	#@	@+others
	#@+node:_getSizer
	def _getSizer(self, parent, height, width):
		"""Return a sizer object to force a Tk widget to be the right size"""
		if USE_FIXED_SIZES: 
			sizer = Tkinter.Frame(parent, height=height, width=width)
			sizer.pack_propagate(0) # don't shrink 
			sizer.pack(side="right")
			return sizer
		else:
			return parent
	#@nonl
	#@-node:_getSizer
	#@+node:addWidgets
	def addWidgets(self, tags, keywords):
		"""Add the widgets to the toolbar."""
		self.c = keywords['c'] 
		toolbar = self.c.frame.iconFrame
		# Button De-Hoist
		self.hoistOff = Tkinter.Button(self._getSizer(toolbar, 25, 70), text="De-Hoist", command=self.doHoistOff)
		self.hoistOff.pack(side="right", fill="both", expand=1)
		# Button Hoist
		self.hoistOn = Tkinter.Button(self._getSizer(toolbar, 25, 70), text="Hoist", command=self.doHoistOn)
		self.hoistOn.pack(side="left", fill="both", expand=1)
		self.bgColor = self.hoistOn["background"]
		self.activeBgColor = self.hoistOn["activebackground"]
	#@-node:addWidgets
	#@+node:doHoistOn
	def doHoistOn(self, *args, **keys):
		self.c.hoist()
	#@-node:doHoistOn
	#@+node:doHoistOff
	def doHoistOff(self, *args, **keys):
		self.c.dehoist()
	#@-node:doHoistOff
	#@+node:onIdle
	def onIdle(self, tag, keywords):
		
		c = self.c
	
		if not hasattr(c,"hoistStack"):
			return
		
		if len(c.hoistStack) > 0:
			self.hoistOn.config(bg=activeHoistColor)
			self.hoistOn.config(activebackground=activeHoistColor)
			self.hoistOn.config(text="Hoist %s" % len(c.hoistStack))
		else:
			self.hoistOn.config(bg=self.bgColor)
			self.hoistOn.config(activebackground=self.activeBgColor)
			self.hoistOn.config(text="Hoist")
	#@nonl
	#@-node:onIdle
	#@-others
#@nonl
#@-node:class HoistButtons
#@-others

if Tkinter:
	hoist = HoistButtons()
	
	if app.gui is None:
		app.createTkGui(__file__)
	
	if app.gui.guiName() == "tkinter":
		registerHandler("after-create-leo-frame", hoist.addWidgets)
		registerHandler("idle", hoist.onIdle)
		plugin_signon(__name__)
#@-node:@file hoist.py
#@-leo
