// Add a 'fixed width' item to the font styles menu.
// (C) 2006 Ryan Lortie <desrt@desrt.ca>, LGPL 2.1 or later.
// vim:set sw=8 noet:

using Mono.Unix;
using System;
using Gtk;

using Tomboy;

class UnderlineMenuItem : CheckMenuItem
{
	NotePlugin Plugin;
	bool event_freeze;

	public UnderlineMenuItem (NotePlugin plugin) 
		: base ("<u>" +
			Catalog.GetString ("_Underline") +
			"</u>")
	{
		((Label) Child).UseUnderline = true;
		((Label) Child).UseMarkup = true;

		Plugin = plugin;
		Plugin.Window.TextMenu.Shown += MenuShown;

		ShowAll();
	}

	protected void MenuShown (object sender, EventArgs e)
	{
		event_freeze = true;
		Active = Plugin.Buffer.IsActiveTag ("underline");
		event_freeze = false;
	}

	protected override void OnActivated ()
	{
		if (!event_freeze)
			Plugin.Buffer.ToggleActiveTag ("underline");

		base.OnActivated();
	}

	protected override void OnDestroyed ()
	{
		Plugin.Window.TextMenu.Shown -= MenuShown;
		base.OnDestroyed();
	}
}

public class UnderlinePlugin : NotePlugin
{
	NoteTag tag;

	protected override void Initialize ()
	{
		// If a tag of this name already exists, don't install.
		if (Note.TagTable.Lookup ("underline") == null) {
			tag = new NoteTag ("underline");
			tag.Underline = Pango.Underline.Single;
			Note.TagTable.Add (tag);
		}
	}

	protected override void Shutdown ()
	{
		// Remove the tag only if we installed it.
		if (tag != null)
			Note.TagTable.Remove (tag);
	}

	protected override void OnNoteOpened () 
	{
		// Add here instead of in Initialize to avoid creating unopened
		// notes' windows/buffers.
		AddTextMenuItem (new UnderlineMenuItem (this));
	}
}
