Skip to content

Instantly share code, notes, and snippets.

@alpinechough
Created January 26, 2011 22:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpinechough/797575 to your computer and use it in GitHub Desktop.
Save alpinechough/797575 to your computer and use it in GitHub Desktop.
Gtk Notebook Tab Label with Close Button
// Copyright (c) 2010 Alpine Chough Software.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Gtk;
namespace Alpinechough.Common.GtkUtilities
{
public class NotebookTabLabel : EventBox
{
public NotebookTabLabel (string title)
{
Button button = new Button ();
button.Image = new Gtk.Image (Stock.Close, IconSize.Menu);
button.TooltipText = "Close Tab";
button.Relief = ReliefStyle.None;
RcStyle rcStyle = new RcStyle ();
rcStyle.Xthickness = 0;
rcStyle.Ythickness = 0;
button.ModifyStyle (rcStyle);
button.FocusOnClick = false;
button.Clicked += OnCloseClicked;
button.Show ();
Label label = new Label (title);
label.UseMarkup = false;
label.UseUnderline = false;
label.Show ();
HBox hbox = new HBox (false, 0);
hbox.Spacing = 0;
hbox.Add (label);
hbox.Add (button);
hbox.Show ();
this.Add (hbox);
}
public event EventHandler<EventArgs> CloseClicked;
public void OnCloseClicked (object sender, EventArgs e)
{
if (CloseClicked != null)
CloseClicked (sender, e);
}
public void OnCloseClicked ()
{
OnCloseClicked (this, EventArgs.Empty);
}
}
}
// Copyright (c) 2010 Alpine Chough Software.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Gtk;
namespace Alpinechough.Common.GtkUtilities
{
public class NotebookTabLabelExample
{
public static void Main (string[] args)
{
Application.Init ();
Window window = new Window (WindowType.Toplevel);
window.DeleteEvent += new DeleteEventHandler (delegate(object sender, DeleteEventArgs e) {
Application.Quit ();
e.RetVal = true;
});
window.Show ();
Notebook notebook = new Notebook ();
window.Add (notebook);
notebook.Show ();
for (int i = 1; i < 10; i++)
AddTab (notebook, new Label ("I'm tab " + i), "Tab " + i);
Application.Run ();
}
private static void AddTab (Notebook notebook, Widget widget, string str)
{
NotebookTabLabel notebookTabLabel = new NotebookTabLabel (str);
notebook.AppendPage (widget, notebookTabLabel);
notebookTabLabel.CloseClicked += delegate(object obj, EventArgs eventArgs) {
notebook.RemovePage (notebook.PageNum (widget));
};
widget.Show ();
notebookTabLabel.Show ();
}
}
}
@omerfeyyaz
Copy link

Hello i need c version for this

@Allen-B1
Copy link

Same

@m-schier
Copy link

m-schier commented Mar 7, 2020

Thank you for providing this nice example! Ported this to C++ without issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment