0006: Position a Window
Sometimes you need a window to land in a specific spot on the screen. As a user, I like windows and dialogs to open in the last position I used and closed them. This is pretty much mandatory behaviour with a three-monitor set-up so I don’t waste time scanning all that screen real estate to figure out where my application or dialog just opened.
For this example, I used two buttons, one to move the window up and to the left, the other to move it down to the right. And because of that, I had to use a
Box object because (if you remember) we can’t put more than one object into a window, but we can if we use a
Box and put the
Box in the window.
The other thing I introduce here is the
interface. It’s used to sketch out a plan for classes which will be similar enough to share functions and data, but not so similar that the functions will all do exactly the same things in the same way. In our case, we want one button to move the window to the left, the other to the right. That’s similar enough that it makes sense to derive these two from an interface because, with both buttons, the window’s moving somewhere. It’s just the destinations that are different.
The interface could also be said to lay down the rules for
Buttons that change the position of the window. By reading through the interface code, we can see that it:
- has a meaningful name (
- has one function that all derived buttons must also have (
- takes a GTK
MainWindowas an argument.
Inheritance in D
RightButton are the derived
Buttons. They inherit from the interface—
PositionButton—as well as the top-level
The rules regarding inheritance in D are:
- an interface can inherit only from another interface,
- a class can inherit from one other class, but
- a class can also inherit from as many interfaces as you need it to, and
- when listing what a class inherits from, you have to list the class first, then any interfaces.
RightButton are set up with the same string of inheritances, but implementation of functions? That’s another story.
moveWindow() function gets the current window position as
y coordinates, then subtracts
60 (respectively) from
y, then calls the window’s
RightButton does the same thing, but adds to
y instead of subtracting. In fact, all I did to create
RightButton was copy/paste, change minus signs to plus and replace
Okay, a short one this time. Until next time, keep those cards and letters coming in and stay happy.
You're free to accept or decline this invitation to become our newest sponsor.
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 2022 Ron Tarrant