0093: An Alternate Way to Gather Window Stats
In the two previous blog posts, we looked at how to gather size, position and maximize state info and—if you didn’t think I was serious before—we’re looking at it one more time.
One Signal Tells All… Sort of
The configure-event
struct gives us access to almost all the info we need. It tells us the Window
’s dimensions and its position in some rather obvious fields:
x
andy
store the position, andwidth
andheight
store the dimensions.
We still have to go elsewhere for the maximized state, but that’s no big hardship.
Anyway, let’s dig in…
The Constructor
this()
{
super(title);
addOnDestroy(&quitApp);
addOnConfigure(&onConfigureEvent);
appBox = new AppBox(this);
add(appBox);
showAll();
} // this()
This has reverted back to the same state we saw in our first demo in Blog Post #90, proving that we really only need to work with the one signal (configure-event
) to get what we need for a configuration file.
The onDestroy() Callback
I’ll throw this in so you can see exactly where we’re reporting (or, in the real world, storing) the info we gather:
void quitApp(Widget widget)
{
string exitMessage = "Save the window stats for next time the user runs this application.";
writeln(exitMessage);
writeln("xPosition: ", xPosition, ", yPosition: ", yPosition, ", width: ", width, ", height: ", height);
checkMaxState();
Main.quit();
} // quitApp()
All the stats are pulled from local variables (remember: we have to store them before the Window.onDestroy
signal fires) and reported here as the program exits.
But now let’s get into the alternate stuff…
Where are Those Window Stats Again?
Well, they’re where they always were. In fact, the maximize state can only be found in the Window
property list, but the rest of the stats can also be found in the configure-event
struct.
And the ConfigureEvent
property names leave no mysteries to solve:
x
andy
: the window position, andwidth
andheight
: the window dimensions.
But breaking that info out can be a bit tricky, so let’s look at that in…
The onConfigureEvent Callback
Here’s what we’re looking at:
bool onConfigureEvent(GdkEventConfigure* event, Widget widget)
{
if(event.type is EventType.CONFIGURE)
{
xPosition = event.x;
yPosition = event.y;
width = event.width;
height = event.height;
}
if(isMaximized())
{
_isMaximized = true;
}
else
{
_isMaximized = false;
}
writeln("Window position - x: ", xPosition, ", y: ", yPosition, ", Window area - width: ", width, ", height: ", height);
return(false); // must be false or the window doesn't redraw properly.
} // onConfigure()
I’ll draw your attention to the function definition… this line:
bool onConfigureEvent(GdkEventConfigure* event, Widget widget)
We’re getting specific about the Event
type so we can pull out information specific to the type of Event
, in this case, a GdkEventConfigure
… which, for some reason, didn’t get aliased to EventConfigure
in the GtkD wrapper. Any-who…
We do have the option, with an overload to this function, to react to an ordinary Event
, but that won’t give us all the Window
stats.
Now let’s look a little further down:
if(event.type is EventType.CONFIGURE)
{
xPosition = event.x;
yPosition = event.y;
width = event.width;
height = event.height;
}
We grab all the values from the event and stuff them into our class properties. The if()
statement is there just to make sure we didn’t go astray in our code somewhere in the preamble of this function.
Conclusion
Now you know everything you need to know (perhaps more than you want to know) about gathering Window
stats. Next time, we start a new series on hardware detection with GTK.
Until then…
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.
- come on over to the D Language Forum and look for one of the gtkDcoding announcement posts,
- drop by the GtkD Forum,
- follow the link below to email me, or
- go to the gtkDcoding Facebook page.
You can also subscribe via RSS so you won't miss anything. Thank you very much for dropping by.
© Copyright 2024 Ron Tarrant