0071: Statusbar Part II - Expanding on the Statusbar

Something about the Statusbar widget that isn’t apparent at first glance is that it’s a type of GTK Container, a Box to be exact. And what magic gives it the ability to display strings? It contains a Label and the push() function is wired straight into it.

But even more interesting is that you can stuff other things into the Box.

Multiple Context IDs

Results of this example:
Current example output
Current example output
Current example terminal output
Current example terminal output (click for enlarged view)

So, I whipped up an example with a second message. All it took was a Separator and a second Label which changed the MyStatusbar class to this:

class MyStatusbar : Statusbar
{
	Separator separatorV1;
	Label directionLabel;
	uint contextIDUp, contextIDDown;
	string contextDescriptionUp = "UP", contextDescriptionDown = "DOWN";
	uint padding = 0;
	int labelWidth = 50, labelHeight = 20;
	int marginWidth = 10;
	
	this()
	{
		separatorV1 = new Separator(Orientation.VERTICAL);
		packStart(separatorV1, false, false, padding);
		separatorV1.setMarginLeft(marginWidth);
		separatorV1.setMarginRight(marginWidth);

		directionLabel = new Label("READY");
		directionLabel.setSizeRequest(labelWidth, labelHeight);
		packStart(directionLabel, false, false, padding);

		contextIDUp = getContextId(contextDescriptionUp);
		contextIDDown = getContextId(contextDescriptionDown);
		
	} // this()

} // class MyStatusBar

Thing to keep in mind when stuffing extra widgets into the Statusbar:

  • for visual appeal, make sure to setMargin() to the left and right of the Separator so the status report strings have ‘breathing’ room (as is done here in this example), and
  • use setSizeRequest() for the Label—giving it a bit more space than you’ll actually need—so the report strings don’t appear to jump back and forth if their lengths change over time, and finally
  • setting the text of the first Label is still done with push(), but
  • setting the text in additional Labels has to be done by calling each Label’s setText() function.

The last two points are illustrated by the onMotion() callback in MyDrawingArea:

public bool onMotion(Event event, Widget widget)
{
	// make sure we're not reacting to the wrong event
	if(event.type == EventType.MOTION_NOTIFY)
	{
		if(event.motion.y < currentY)
		{
			_myStatusbar.push(_myStatusbar.contextIDUp, "Mouse position: " ~ format("%s, %s", event.motion.x, event.motion.y));

       myStatusbar.directionLabel.setText(_myStatusbar.contextDescriptionUp);
		}
		else
		{
			_myStatusbar.push(_myStatusbar.contextIDDown, "Mouse position: " ~ format("%s, %s", event.motion.x, event.motion.y));
			_myStatusbar.directionLabel.setText(_myStatusbar.contextDescriptionDown);
		}

		currentY = event.motion.y;
	}

	return(true);
		
} // onMotion()

Notice that because the Statusbar is monitoring and reporting on Events in the DrawingArea, both of these Labels have to be updated by this callback.

Now, we have one more example to look at…

The Statusbar’s Signal

Results of this example:
Current example output
Current example output
Current example terminal output
Current example terminal output (click for enlarged view)

The Statusbar signal, unless we import information from other application objects, only allows access to whatever the Statusbar itself contains. The most obvious bit of data to bring in to illustrate a third report is the contextID, so we add a second Separator and a third Label to the MyStatusbar constructor and then hook up the signal:

addOnTextPushed(&doSomething);

Then we can write a callback to handle the data we glean from the Statusbar:

void doSomething(uint contextID, string statusMessage, Statusbar statusbar)
{
	string message = format("Context ID: %d", contextID); 
	contextLabel.setText(message);
	directionLabel.setText(contextDescriptionDown);

} // doSomething()

Now the Statusbar as three sections:

  • mouse coordinates,
  • mouse direction, and
  • context ID.

Conclusion

And that’s just about everything I can think of to do with the lowly Statusbar. They’re being phased out of most applications these days, but they haven’t been deprecated, so they may very well make a comeback, so we might as well be ready.

Comments? Questions? Observations?

Did we miss a tidbit of information that would make this post even more informative? Let's talk about it in the comments.

You can also subscribe via RSS so you won't miss anything. Thank you very much for dropping by.

© Copyright 2023 Ron Tarrant