view Translatable/ControlTranslator.cs @ 42:f04e973e5ea0 default tip

* Drop back to old Tooltips to support GTK# before 2.12 * Make sure we're not 2.12 specific to allow building on the server (running 2.10)
author IBBoard <dev@ibboard.co.uk>
date Sun, 07 Oct 2012 19:58:40 +0100
parents 70c46caee508
children
line wrap: on
line source

//  This file (ControlTranslator.cs) is a part of the IBBoard.GtkSharp project and is copyright 2010 IBBoard
// 
// // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.

using Gtk;
using IBBoard.Lang;

namespace IBBoard.GtkSharp.Translatable
{
	/// <summary>
	/// A custom cascading translator. It takes any widget type and translates them if they implement <see>ITranslatable</see>
	/// and optionally cascades the process to translate all children.
	/// </summary>
	public class ControlTranslator
	{
		//Tooltips is now obselete, but we need it to support GTK# pre-2.12
		private static Tooltips tooltips = new Tooltips();
		
		public static void TranslateWidget(Widget toTranslate)
		{
			TranslateWidget(toTranslate, true);
		}

		public static void TranslateWidget(Widget toTranslate, bool cascade)
		{
			if (toTranslate is ITranslatable)
			{
				Translation.Translate((ITranslatable)toTranslate);
			}
			else if (toTranslate is MenuItem || toTranslate is ToolButton)
			{
				TranslateWidgetWithAction((Widget)toTranslate);
			}

			if (cascade && toTranslate is Container)
			{
				CascadeTranslations((Container)toTranslate, cascade);
			}
		}

		private static void TranslateWidgetWithAction(Widget widget)
		{
			Action action = widget.Action;
			string translation = Translation.GetTranslation(widget.Name, "");

			if (translation == "")
			{
				tooltips.SetTip(widget, translation, null);
			}
			else if (action != null)
			{
				tooltips.SetTip(widget, action.Label, null);
			}
		}

		private static void CascadeTranslations(Container container, bool cascade)
		{
			if (container is ITranslatableWithActions)
			{
				TranslateActions((ITranslatableWithActions)container);
			}

			foreach (Widget childWidget in container.AllChildren)
			{
				TranslateWidget(childWidget, cascade);
			}
		}

		private static void TranslateActions(ITranslatableWithActions actionContainer)
		{
			foreach (Action action in actionContainer.Actions)
			{
				TranslateAction(action);
			}
		}

		private static void TranslateAction(Gtk.Action action)
		{
			action.Label = Translation.GetTranslation(action.Name, action.Label);
		}

		public static void TranslateButtons(params Button[] buttons)
		{
			foreach (Button button in buttons)
			{
				TranslateButton(button);
			}
		}

		public static void TranslateButton(Button button)
		{
			Widget image = button.Image;
			button.Label = Translation.GetTranslation(button.Name, button.Label);
			button.Image = image;
		}
	}
}