view NotebookUtil.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 50d774f164dd
children
line wrap: on
line source

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

using System;
using Gtk;

namespace IBBoard.GtkSharp
{
	public class NotebookUtil
	{
		/// <summary>
		/// Adds a page to a notebook, but also includes a close button in the tab. Returns the page number added
		/// </summary>
		/// <param name="notebook">
		/// The <see cref="Notebook"/> to add a page to
		/// </param>
		/// <param name="page">
		/// The <see cref="Widget"/> to use as the content of the page
		/// </param>
		/// <param name="title">
		/// The title of the tab
		/// </param>
		/// <returns>
		/// The page number created
		/// </returns>
		public static int AddPageToNotebookWithCloseButton(Notebook notebook, Widget page, string title)
		{
			return notebook.AppendPage(page, CreateNotebookTabLabelWithClose(notebook, page, title)); 
		}
		
		/// <summary>
		/// Creates a widget to be used as the label for a notebook tab with text and a close button
		/// </summary>
		/// <param name="notebook">
		/// The <see cref="Notebook"/> the page is on
		/// </param>
		/// <param name="page">
		/// The <see cref="Widget"/> that is the content of the page
		/// </param>
		/// <param name="title">
		/// The text to display on the tab
		/// </param>
		/// <returns>
		/// A <see cref="Widget"/> that can be used as a tab label that contains the label with the title text and a close button
		/// </returns>
		public static Widget CreateNotebookTabLabelWithClose(Notebook notebook, Widget page, String title)
		{
			EventBox eventBox = new EventBox();
			HBox hbox = new HBox();
			eventBox.Add(hbox);
			eventBox.ButtonReleaseEvent += delegate(object o, ButtonReleaseEventArgs args) {
				if (args.Event.Button == (uint)MouseButton.Middle)
				{
					notebook.Remove(page);
				}
			};
			hbox.PackStart(new Label(title));
			Button close = new Button();
			Gtk.Rc.ParseString("style \"NotebookTab.CloseButton\" {\n GtkWidget::focus-padding = 0\n GtkWidget::focus-line-width = 0\n xthickness = 0\n ythickness = 0\n GtkButton::inner-border = {0,0,0,0}\n }\n");
			Gtk.Rc.ParseString("widget \"*.NotebookTab.CloseButton\" style  \"NotebookTab.CloseButton\"\n");
			Image icon = Image.NewFromIconName("gtk-close", IconSize.Menu);
			icon.SetPadding(0, 0);
			close.Image = icon;
			close.Relief = ReliefStyle.None;
			close.FocusOnClick = false;
			close.BorderWidth = 0;
			close.Name = "NotebookTab.CloseButton";
			close.Clicked += delegate {
				notebook.Remove(page);
			};

			hbox.PackStart(close);			
			hbox.ShowAll();	
			return eventBox;
		}
	}
}