0006: Position a Window

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

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.

Something New

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 (PositionButton),
  • has one function that all derived buttons must also have (moveWindow), and
  • takes a GTK MainWindow as an argument.

Inheritance in D

LeftButton and RightButton are the derived Buttons. They inherit from the interface—PositionButton—as well as the top-level Button class.

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.

Both LeftButton and RightButton are set up with the same string of inheritances, but implementation of functions? That’s another story.

In LeftButton, the moveWindow() function gets the current window position as x and y coordinates, then subtracts 40 and 60 (respectively) from x and y, then calls the window’s move() function.

RightButton does the same thing, but adds to x and y instead of subtracting. In fact, all I did to create RightButton was copy/paste, change minus signs to plus and replace Left with Right.


Okay, a short one this time. Until next time, keep those cards and letters coming in and stay happy.

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