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 D language’s interface which is 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 similar enough in that the window’s moving somewhere, but the destinations 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’s code, we can see that it:
- returns on value,
- has a meaningful name (
- it has one function that all derived buttons must also have (
- it takes a GTK
MainWindowas an argument.
Inheritance in D
RightButton are the derived
Buttons. They inherit from both our interface 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 an 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.
If you'd like to leave a comment...
Until I get time to research and install a commenting system, I ask that you try one of these options:
- 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.
© Copyright 2019 Ron Tarrant