Sign up

Customized Notebook Tabs

Hi Mike,

Looking at the appendPage() function, I get the impression that a Notebook tab can be pretty much any Widget. This seems to be borne out by a few discussions and examples I've read:

C# Gtk Notebook Tab Label with Close Button
PyGtk Notebook with Closeable Tabs
And a few others.

In these examples/discussions, a tab can be populated with a Box which, in turn, contains a Label and a Button.

But when I tried this in GtkD, even though everything looks right, tab switching is disabled. Any idea why?

// Description of example

import std.stdio;
import std.math;

import gtk.MainWindow;
import gtk.Main;
import gtk.Box;
import gtk.Widget;
import gtk.Notebook;
import gtk.Button;
import gtk.TextView;
import gtk.TextBuffer;
import cairo.Context;
import gtk.DrawingArea;

void main(string[] args)
{
	TestRigWindow testRigWindow;
	
	Main.init(args);

	testRigWindow = new TestRigWindow();
	
	Main.run();
	
} // main()


class TestRigWindow : MainWindow
{
	string title = "Notebook Demo - Simple";
	AppBox appBox;
	
	this()
	{
		super(title);
		addOnDestroy(&quitApp);
		
		appBox = new AppBox();
		add(appBox);
		
		showAll();

	} // this()
	
		
	void quitApp(Widget widget)
	{
		string exitMessage = "Bye.";
		
		writeln(exitMessage);
		
		Main.quit();
		
	} // quitApp()

} // class TestRigWindow


class AppBox : Box
{
	bool expand = false, fill = false;
	uint globalPadding = 10, localPadding = 5;
	MyNotebook myNotebook;
	
	this()
	{
		super(Orientation.VERTICAL, globalPadding);
		
		myNotebook = new MyNotebook();
		
		packStart(myNotebook, expand, fill, localPadding); // LEFT justify
		
	} // this()

} // class AppBox


class MyNotebook : Notebook
{
	PositionType tabPosition = PositionType.TOP;
	string tabButtonOne = "Tab One", tabButtonTwo = "Tab Two", tabButtonThree = "Tab Three";
	Button myTabButtonOne, myTabButtonTwo, myTabButtonThree;
	int tabIndex;
	MyTextView myTextViewOne, myTextViewTwo, myTextViewThree;
	
	this()
	{
		super();
		setTabPos(tabPosition);

		myTabButtonOne = new Button(tabButtonOne);
		myTextViewOne = new MyTextView("Now is the witness of our discontinent.");
		tabIndex = appendPage(myTextViewOne, myTabButtonOne);

		myTabButtonTwo = new Button(tabButtonTwo);
		myTextViewTwo = new MyTextView("Four stores and seven pounds ago...");
		tabIndex = appendPage(myTextViewTwo, myTabButtonTwo);

		myTabButtonThree = new Button(tabButtonThree);
		myTextViewThree = new MyTextView("Help me open yon cantelope.");
		tabIndex = appendPage(myTextViewThree, myTabButtonThree);
		
	} // this()
	
} // class MyNotebook


class MyTextView : TextView
{
	TextBuffer textBuffer;
	int width = 400, height = 350;
	
	this(string content)
	{
		super();
		setSizeRequest(width, height);
		textBuffer = getBuffer();
		textBuffer.setText(content);
		
	} // this()

} // class MyTextView

Re: Customized Notebook Tabs

I figured out what's going on, but I still have a question...

What's going on: Switching tabs can still be done, but one has to be careful to avoid clicking on the 'customized' area of the tab. In other words, if you click on the Button embedded in the tab, the Button absorbs the click and the tab sees nothing.

Where this becomes a problem is when I embed a DrawingArea on which I draw a custom tab shape. The DrawingArea absorbs the click and so, still, the tabs doesn't see it.

So, now I'm looking for a way to pass along the click from the DrawingArea to the tab. I guess this could be done by having the DrawingArea react to the signal and pass it along... Hmmm... Not sure.

I just assumed the tab would get the click signal no matter what was stuffed into it.

Anyway, I'm still digging around for an answer, but if you know of a way, I'd appreciate if you passed it along, Mike.

Re: Customized Notebook Tabs

On 12-09-2019 14:58, Ron Tarrant wrote:

Hi Mike,

Looking at the appendPage() function, I get the impression that a Notebook tab can be pretty much any Widget. This seems to be borne out by a few discussions and examples I've read:

C# Gtk Notebook Tab Label with Close Button
PyGtk Notebook with Closeable Tabs
And a few others.

In these examples/discussions, a tab can be populated with a Box which, in turn, contains a Label and a Button.

But when I tried this in GtkD, even though everything looks right, tab switching is disabled. Any idea why?

....

In the C# and PyGtk examples, there is a label and a button in the tab,
and the clicks on the close button not activating the tab would be the
desired behavior.

In your case when there is only a Button, connecting addOnClicked to a
function that would activate the tab with a call to setCurrentPage,
would be the easiest.

Re: Customized Notebook Tabs

On Thu, 12 Sep 2019 23:08:44 +0200, Mike Wey wrote:

In the C# and PyGtk examples, there is a label and a button in the tab,
and the clicks on the close button not activating the tab would be the
desired behavior.

In your case when there is only a Button, connecting addOnClicked to a
function that would activate the tab with a call to setCurrentPage,
would be the easiest.

Right. And addOnButtonPress() should do the trick for a DrawingArea.

Thanks, Mike.