#@+leo
#@+node:0::@file test.py
#@+body
#@@language python

"""Test file for Plugins menu protocols"""

from leoPlugins import *
from leoGlobals import *


#@+others
#@+node:1::applyConfiguration
#@+body
def applyConfiguration(config):
	
	sections = config.sections()
	sections.sort()

	for section in sections:
		options = config.options(section)
		options.sort()
		for option in options:
			print section, option, config.get(section, option)

#@-body
#@-node:1::applyConfiguration
#@+node:2::cmd_fn1/2/3
#@+body
def cmd_fn1(x):
	es("Fn1",color="blue")

def cmd_fn2(x):
	es("Fn2",color="blue")

def cmd_fn3(x):
	es("Fn3",color="blue")

#@-body
#@-node:2::cmd_fn1/2/3
#@+node:3::onSelect
#@+body
def onSelect (tag,keywords):

	import Tkinter,os

	new_v = keywords.get("new_v")
	h = new_v.headString()
	if h[:7] == "@image ":
		filename = h[7:]
		
		#@<< Select Image >>
		#@+node:1::<< Select Image >>
		#@+body
		# Display the image file in the text pane, if you can find the file
		a = app()
		c = keywords.get("c")
		body = c.frame.body
		
		if os.path.isfile(filename):
			try:
				# Note that Tkinter only understands GIF
				photo = Tkinter.PhotoImage(master=a.root, file=filename)
			except:
				es("error: cannot load image")
				return
			# Nicely display the image at the center top and push the text below.
			a.gsphoto = photo # This is soooo important.
			photoWidth = photo.width()
			bodyWidth = body.winfo_width()
			padding = int((bodyWidth - photoWidth - 16) / 2)
			padding = max(0,padding)
			a.gsimage = body.image_create("1.0",image=photo,padx=padding)
		else:
			es("warning: missing image file")
		#@-body
		#@-node:1::<< Select Image >>
#@-body
#@-node:3::onSelect
#@+node:4::onUnselect
#@+body
def onUnselect (tag,keywords):

	import Tkinter, os.path
	a = app()
	c = keywords.get("c")
	old_v = keywords.get("old_v")

	if old_v:
		h = old_v.headString()
		if h[:7] == "@image ":
			
			#@<< Unselect Image >>
			#@+node:1::<< Unselect Image >>
			#@+body
			# Erase image if it was previously displayed
			if a.gsimage:
				try:
					 c.frame.body.delete(a.gsimage)
				except:
					es("info: no image to erase")
			
			# And forget about it
			a.gsimage = None
			a.gsphoto = None
			
			#@-body
			#@-node:1::<< Unselect Image >>

	else: # Leo is initializing.
		a.gsphoto = None # Holds our photo file
		a.gsimage = None # Holds our image instance within the text pane
#@-body
#@-node:4::onUnselect
#@-others


if 0: # Register the handlers...

	registerHandler("select2", onSelect)
	registerHandler("unselect1", onUnselect)
	
	__version__ = "1.1"
	plugin_signon(__name__)
#@-body
#@-node:0::@file test.py
#@-leo
